blob: 1a3de73372745c1fe57df70a1a4485000e6221ff [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030049#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070050
51#include <linux/sched/signal.h>
52#include <linux/fs.h>
53#include <linux/file.h>
54#include <linux/fdtable.h>
55#include <linux/mm.h>
56#include <linux/mman.h>
57#include <linux/mmu_context.h>
58#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070079
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020080#define CREATE_TRACE_POINTS
81#include <trace/events/io_uring.h>
82
Jens Axboe2b188cc2019-01-07 10:46:33 -070083#include <uapi/linux/io_uring.h>
84
85#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060086#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070087
Daniel Xu5277dea2019-09-14 14:23:45 -070088#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060089#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060090
91/*
92 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
93 */
94#define IORING_FILE_TABLE_SHIFT 9
95#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
96#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
97#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070098
99struct io_uring {
100 u32 head ____cacheline_aligned_in_smp;
101 u32 tail ____cacheline_aligned_in_smp;
102};
103
Stefan Bühler1e84b972019-04-24 23:54:16 +0200104/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000105 * This data is shared with the application through the mmap at offsets
106 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107 *
108 * The offsets to the member fields are published through struct
109 * io_sqring_offsets when calling io_uring_setup.
110 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000111struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112 /*
113 * Head and tail offsets into the ring; the offsets need to be
114 * masked to get valid indices.
115 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 * The kernel controls head of the sq ring and the tail of the cq ring,
117 * and the application controls tail of the sq ring and the head of the
118 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000120 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 * ring_entries - 1)
124 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 u32 sq_ring_mask, cq_ring_mask;
126 /* Ring sizes (constant, power of 2) */
127 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200128 /*
129 * Number of invalid entries dropped by the kernel due to
130 * invalid index stored in array
131 *
132 * Written by the kernel, shouldn't be modified by the
133 * application (i.e. get number of "new events" by comparing to
134 * cached value).
135 *
136 * After a new SQ head value was read by the application this
137 * counter includes all submissions that were dropped reaching
138 * the new SQ head (and possibly more).
139 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000140 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200141 /*
142 * Runtime flags
143 *
144 * Written by the kernel, shouldn't be modified by the
145 * application.
146 *
147 * The application needs a full memory barrier before checking
148 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
149 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000150 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 /*
152 * Number of completion events lost because the queue was full;
153 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800154 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200155 * the completion queue.
156 *
157 * Written by the kernel, shouldn't be modified by the
158 * application (i.e. get number of "new events" by comparing to
159 * cached value).
160 *
161 * As completion events come in out of order this counter is not
162 * ordered with any other data.
163 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000164 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 /*
166 * Ring buffer of completion events.
167 *
168 * The kernel writes completion events fresh every time they are
169 * produced, so the application is allowed to modify pending
170 * entries.
171 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000172 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700173};
174
Jens Axboeedafcce2019-01-09 09:16:05 -0700175struct io_mapped_ubuf {
176 u64 ubuf;
177 size_t len;
178 struct bio_vec *bvec;
179 unsigned int nr_bvecs;
180};
181
Jens Axboe65e19f52019-10-26 07:20:21 -0600182struct fixed_file_table {
183 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700184};
185
Jens Axboe05f3fb32019-12-09 11:22:50 -0700186struct fixed_file_data {
187 struct fixed_file_table *table;
188 struct io_ring_ctx *ctx;
189
190 struct percpu_ref refs;
191 struct llist_head put_llist;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700192 struct work_struct ref_work;
193 struct completion done;
194};
195
Jens Axboe2b188cc2019-01-07 10:46:33 -0700196struct io_ring_ctx {
197 struct {
198 struct percpu_ref refs;
199 } ____cacheline_aligned_in_smp;
200
201 struct {
202 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800203 unsigned int compat: 1;
204 unsigned int account_mem: 1;
205 unsigned int cq_overflow_flushed: 1;
206 unsigned int drain_next: 1;
207 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208
Hristo Venev75b28af2019-08-26 17:23:46 +0000209 /*
210 * Ring buffer of indices into array of io_uring_sqe, which is
211 * mmapped by the application using the IORING_OFF_SQES offset.
212 *
213 * This indirection could e.g. be used to assign fixed
214 * io_uring_sqe entries to operations and only submit them to
215 * the queue when needed.
216 *
217 * The kernel modifies neither the indices array nor the entries
218 * array.
219 */
220 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 unsigned cached_sq_head;
222 unsigned sq_entries;
223 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700224 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600225 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700226 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700227 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600228
229 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600230 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700231 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700232
Jens Axboefcb323c2019-10-24 12:39:47 -0600233 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700234 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700235 } ____cacheline_aligned_in_smp;
236
Hristo Venev75b28af2019-08-26 17:23:46 +0000237 struct io_rings *rings;
238
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600240 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241 struct task_struct *sqo_thread; /* if using sq thread polling */
242 struct mm_struct *sqo_mm;
243 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700244
Jens Axboe6b063142019-01-10 22:13:58 -0700245 /*
246 * If used, fixed file set. Writers must ensure that ->refs is dead,
247 * readers must ensure that ->refs is alive as long as the file* is
248 * used. Only updated through io_uring_register(2).
249 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700250 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700251 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300252 int ring_fd;
253 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700254
Jens Axboeedafcce2019-01-09 09:16:05 -0700255 /* if used, fixed mapped user buffers */
256 unsigned nr_user_bufs;
257 struct io_mapped_ubuf *user_bufs;
258
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259 struct user_struct *user;
260
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700261 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700262
Jens Axboe206aefd2019-11-07 18:27:42 -0700263 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
264 struct completion *completions;
265
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700266 /* if all else fails... */
267 struct io_kiocb *fallback_req;
268
Jens Axboe206aefd2019-11-07 18:27:42 -0700269#if defined(CONFIG_UNIX)
270 struct socket *ring_sock;
271#endif
272
Jens Axboe071698e2020-01-28 10:04:42 -0700273 struct idr personality_idr;
274
Jens Axboe206aefd2019-11-07 18:27:42 -0700275 struct {
276 unsigned cached_cq_tail;
277 unsigned cq_entries;
278 unsigned cq_mask;
279 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700280 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700281 struct wait_queue_head cq_wait;
282 struct fasync_struct *cq_fasync;
283 struct eventfd_ctx *cq_ev_fd;
284 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285
286 struct {
287 struct mutex uring_lock;
288 wait_queue_head_t wait;
289 } ____cacheline_aligned_in_smp;
290
291 struct {
292 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700293 struct llist_head poll_llist;
294
Jens Axboedef596e2019-01-09 08:59:42 -0700295 /*
296 * ->poll_list is protected by the ctx->uring_lock for
297 * io_uring instances that don't use IORING_SETUP_SQPOLL.
298 * For SQPOLL, only the single threaded io_sq_thread() will
299 * manipulate the list, hence no extra locking is needed there.
300 */
301 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700302 struct hlist_head *cancel_hash;
303 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700304 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600305
306 spinlock_t inflight_lock;
307 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700308 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700309};
310
Jens Axboe09bb8392019-03-13 12:39:28 -0600311/*
312 * First field must be the file pointer in all the
313 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
314 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700315struct io_poll_iocb {
316 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700317 union {
318 struct wait_queue_head *head;
319 u64 addr;
320 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700321 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600322 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700323 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700324 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700325};
326
Jens Axboeb5dba592019-12-11 14:02:38 -0700327struct io_close {
328 struct file *file;
329 struct file *put_file;
330 int fd;
331};
332
Jens Axboead8a48a2019-11-15 08:49:11 -0700333struct io_timeout_data {
334 struct io_kiocb *req;
335 struct hrtimer timer;
336 struct timespec64 ts;
337 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300338 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700339};
340
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700341struct io_accept {
342 struct file *file;
343 struct sockaddr __user *addr;
344 int __user *addr_len;
345 int flags;
346};
347
348struct io_sync {
349 struct file *file;
350 loff_t len;
351 loff_t off;
352 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700353 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700354};
355
Jens Axboefbf23842019-12-17 18:45:56 -0700356struct io_cancel {
357 struct file *file;
358 u64 addr;
359};
360
Jens Axboeb29472e2019-12-17 18:50:29 -0700361struct io_timeout {
362 struct file *file;
363 u64 addr;
364 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700365 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700366};
367
Jens Axboe9adbd452019-12-20 08:45:55 -0700368struct io_rw {
369 /* NOTE: kiocb has the file as the first member, so don't do it here */
370 struct kiocb kiocb;
371 u64 addr;
372 u64 len;
373};
374
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700375struct io_connect {
376 struct file *file;
377 struct sockaddr __user *addr;
378 int addr_len;
379};
380
Jens Axboee47293f2019-12-20 08:58:21 -0700381struct io_sr_msg {
382 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700383 union {
384 struct user_msghdr __user *msg;
385 void __user *buf;
386 };
Jens Axboee47293f2019-12-20 08:58:21 -0700387 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700388 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700389};
390
Jens Axboe15b71ab2019-12-11 11:20:36 -0700391struct io_open {
392 struct file *file;
393 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700394 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700395 unsigned mask;
396 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700397 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700398 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700399 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700400};
401
Jens Axboe05f3fb32019-12-09 11:22:50 -0700402struct io_files_update {
403 struct file *file;
404 u64 arg;
405 u32 nr_args;
406 u32 offset;
407};
408
Jens Axboe4840e412019-12-25 22:03:45 -0700409struct io_fadvise {
410 struct file *file;
411 u64 offset;
412 u32 len;
413 u32 advice;
414};
415
Jens Axboec1ca7572019-12-25 22:18:28 -0700416struct io_madvise {
417 struct file *file;
418 u64 addr;
419 u32 len;
420 u32 advice;
421};
422
Jens Axboe3e4827b2020-01-08 15:18:09 -0700423struct io_epoll {
424 struct file *file;
425 int epfd;
426 int op;
427 int fd;
428 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700429};
430
Jens Axboef499a022019-12-02 16:28:46 -0700431struct io_async_connect {
432 struct sockaddr_storage address;
433};
434
Jens Axboe03b12302019-12-02 18:50:25 -0700435struct io_async_msghdr {
436 struct iovec fast_iov[UIO_FASTIOV];
437 struct iovec *iov;
438 struct sockaddr __user *uaddr;
439 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700440 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700441};
442
Jens Axboef67676d2019-12-02 11:03:47 -0700443struct io_async_rw {
444 struct iovec fast_iov[UIO_FASTIOV];
445 struct iovec *iov;
446 ssize_t nr_segs;
447 ssize_t size;
448};
449
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700450struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700451 union {
452 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700453 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700454 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700455 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700456 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700457};
458
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300459enum {
460 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
461 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
462 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
463 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
464 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
465
466 REQ_F_LINK_NEXT_BIT,
467 REQ_F_FAIL_LINK_BIT,
468 REQ_F_INFLIGHT_BIT,
469 REQ_F_CUR_POS_BIT,
470 REQ_F_NOWAIT_BIT,
471 REQ_F_IOPOLL_COMPLETED_BIT,
472 REQ_F_LINK_TIMEOUT_BIT,
473 REQ_F_TIMEOUT_BIT,
474 REQ_F_ISREG_BIT,
475 REQ_F_MUST_PUNT_BIT,
476 REQ_F_TIMEOUT_NOSEQ_BIT,
477 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300478 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700479 REQ_F_OVERFLOW_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300480};
481
482enum {
483 /* ctx owns file */
484 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
485 /* drain existing IO first */
486 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
487 /* linked sqes */
488 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
489 /* doesn't sever on completion < 0 */
490 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
491 /* IOSQE_ASYNC */
492 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
493
494 /* already grabbed next link */
495 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
496 /* fail rest of links */
497 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
498 /* on inflight list */
499 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
500 /* read/write uses file position */
501 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
502 /* must not punt to workers */
503 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
504 /* polled IO has completed */
505 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
506 /* has linked timeout */
507 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
508 /* timeout request */
509 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
510 /* regular file */
511 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
512 /* must be punted even for NONBLOCK */
513 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
514 /* no timeout sequence */
515 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
516 /* completion under lock */
517 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300518 /* needs cleanup */
519 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700520 /* in overflow list */
521 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300522};
523
Jens Axboe09bb8392019-03-13 12:39:28 -0600524/*
525 * NOTE! Each of the iocb union members has the file pointer
526 * as the first entry in their struct definition. So you can
527 * access the file pointer through any of the sub-structs,
528 * or directly as just 'ki_filp' in this struct.
529 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700530struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700531 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600532 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700533 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700534 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700535 struct io_accept accept;
536 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700537 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700538 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700539 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700540 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700541 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700542 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700543 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700544 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700545 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700546 struct io_epoll epoll;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700547 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700548
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700549 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300550 /*
551 * llist_node is only used for poll deferred completions
552 */
553 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300554 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700555 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700556
557 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700558 union {
559 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700560 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700561 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600562 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700563 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700564 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700565 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600566 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600567 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700568
Jens Axboefcb323c2019-10-24 12:39:47 -0600569 struct list_head inflight_entry;
570
Jens Axboe561fb042019-10-24 07:25:42 -0600571 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700572};
573
574#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700575#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700576
Jens Axboe9a56a232019-01-09 09:06:50 -0700577struct io_submit_state {
578 struct blk_plug plug;
579
580 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700581 * io_kiocb alloc cache
582 */
583 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300584 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700585
586 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700587 * File reference cache
588 */
589 struct file *file;
590 unsigned int fd;
591 unsigned int has_refs;
592 unsigned int used_refs;
593 unsigned int ios_left;
594};
595
Jens Axboed3656342019-12-18 09:50:26 -0700596struct io_op_def {
597 /* needs req->io allocated for deferral/async */
598 unsigned async_ctx : 1;
599 /* needs current->mm setup, does mm access */
600 unsigned needs_mm : 1;
601 /* needs req->file assigned */
602 unsigned needs_file : 1;
603 /* needs req->file assigned IFF fd is >= 0 */
604 unsigned fd_non_neg : 1;
605 /* hash wq insertion if file is a regular file */
606 unsigned hash_reg_file : 1;
607 /* unbound wq insertion if file is a non-regular file */
608 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700609 /* opcode is not supported by this kernel */
610 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700611 /* needs file table */
612 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700613 /* needs ->fs */
614 unsigned needs_fs : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700615};
616
617static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300618 [IORING_OP_NOP] = {},
619 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700620 .async_ctx = 1,
621 .needs_mm = 1,
622 .needs_file = 1,
623 .unbound_nonreg_file = 1,
624 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300625 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700626 .async_ctx = 1,
627 .needs_mm = 1,
628 .needs_file = 1,
629 .hash_reg_file = 1,
630 .unbound_nonreg_file = 1,
631 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300632 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700633 .needs_file = 1,
634 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300635 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700636 .needs_file = 1,
637 .unbound_nonreg_file = 1,
638 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300639 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700640 .needs_file = 1,
641 .hash_reg_file = 1,
642 .unbound_nonreg_file = 1,
643 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300644 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700645 .needs_file = 1,
646 .unbound_nonreg_file = 1,
647 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300648 [IORING_OP_POLL_REMOVE] = {},
649 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700650 .needs_file = 1,
651 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300652 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700653 .async_ctx = 1,
654 .needs_mm = 1,
655 .needs_file = 1,
656 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700657 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700658 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300659 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700660 .async_ctx = 1,
661 .needs_mm = 1,
662 .needs_file = 1,
663 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700664 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700665 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300666 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700667 .async_ctx = 1,
668 .needs_mm = 1,
669 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300670 [IORING_OP_TIMEOUT_REMOVE] = {},
671 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700672 .needs_mm = 1,
673 .needs_file = 1,
674 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700675 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700676 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300677 [IORING_OP_ASYNC_CANCEL] = {},
678 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700679 .async_ctx = 1,
680 .needs_mm = 1,
681 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300682 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700683 .async_ctx = 1,
684 .needs_mm = 1,
685 .needs_file = 1,
686 .unbound_nonreg_file = 1,
687 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300688 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700689 .needs_file = 1,
690 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300691 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700692 .needs_file = 1,
693 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700694 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700695 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700696 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300697 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700698 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700699 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700700 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300701 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700702 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700703 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700704 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300705 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700706 .needs_mm = 1,
707 .needs_file = 1,
708 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700709 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700710 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300711 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700712 .needs_mm = 1,
713 .needs_file = 1,
714 .unbound_nonreg_file = 1,
715 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300716 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700717 .needs_mm = 1,
718 .needs_file = 1,
719 .unbound_nonreg_file = 1,
720 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300721 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700722 .needs_file = 1,
723 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300724 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700725 .needs_mm = 1,
726 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300727 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700728 .needs_mm = 1,
729 .needs_file = 1,
730 .unbound_nonreg_file = 1,
731 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300732 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700733 .needs_mm = 1,
734 .needs_file = 1,
735 .unbound_nonreg_file = 1,
736 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300737 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700738 .needs_file = 1,
739 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700740 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700741 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700742 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700743 [IORING_OP_EPOLL_CTL] = {
744 .unbound_nonreg_file = 1,
745 .file_table = 1,
746 },
Jens Axboed3656342019-12-18 09:50:26 -0700747};
748
Jens Axboe561fb042019-10-24 07:25:42 -0600749static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700750static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800751static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700752static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700753static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
754static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700755static int __io_sqe_files_update(struct io_ring_ctx *ctx,
756 struct io_uring_files_update *ip,
757 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700758static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700759static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300760static void io_cleanup_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600761
Jens Axboe2b188cc2019-01-07 10:46:33 -0700762static struct kmem_cache *req_cachep;
763
764static const struct file_operations io_uring_fops;
765
766struct sock *io_uring_get_socket(struct file *file)
767{
768#if defined(CONFIG_UNIX)
769 if (file->f_op == &io_uring_fops) {
770 struct io_ring_ctx *ctx = file->private_data;
771
772 return ctx->ring_sock->sk;
773 }
774#endif
775 return NULL;
776}
777EXPORT_SYMBOL(io_uring_get_socket);
778
779static void io_ring_ctx_ref_free(struct percpu_ref *ref)
780{
781 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
782
Jens Axboe206aefd2019-11-07 18:27:42 -0700783 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700784}
785
786static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
787{
788 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700789 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700790
791 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
792 if (!ctx)
793 return NULL;
794
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700795 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
796 if (!ctx->fallback_req)
797 goto err;
798
Jens Axboe206aefd2019-11-07 18:27:42 -0700799 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
800 if (!ctx->completions)
801 goto err;
802
Jens Axboe78076bb2019-12-04 19:56:40 -0700803 /*
804 * Use 5 bits less than the max cq entries, that should give us around
805 * 32 entries per hash list if totally full and uniformly spread.
806 */
807 hash_bits = ilog2(p->cq_entries);
808 hash_bits -= 5;
809 if (hash_bits <= 0)
810 hash_bits = 1;
811 ctx->cancel_hash_bits = hash_bits;
812 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
813 GFP_KERNEL);
814 if (!ctx->cancel_hash)
815 goto err;
816 __hash_init(ctx->cancel_hash, 1U << hash_bits);
817
Roman Gushchin21482892019-05-07 10:01:48 -0700818 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700819 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
820 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700821
822 ctx->flags = p->flags;
823 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700824 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700825 init_completion(&ctx->completions[0]);
826 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700827 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700828 mutex_init(&ctx->uring_lock);
829 init_waitqueue_head(&ctx->wait);
830 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700831 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700832 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600833 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600834 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600835 init_waitqueue_head(&ctx->inflight_wait);
836 spin_lock_init(&ctx->inflight_lock);
837 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700838 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700839err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700840 if (ctx->fallback_req)
841 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700842 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700843 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700844 kfree(ctx);
845 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700846}
847
Bob Liu9d858b22019-11-13 18:06:25 +0800848static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600849{
Jackie Liua197f662019-11-08 08:09:12 -0700850 struct io_ring_ctx *ctx = req->ctx;
851
Jens Axboe498ccd92019-10-25 10:04:25 -0600852 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
853 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600854}
855
Bob Liu9d858b22019-11-13 18:06:25 +0800856static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600857{
Pavel Begunkov87987892020-01-18 01:22:30 +0300858 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800859 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600860
Bob Liu9d858b22019-11-13 18:06:25 +0800861 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600862}
863
864static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600865{
866 struct io_kiocb *req;
867
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600868 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800869 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600870 list_del_init(&req->list);
871 return req;
872 }
873
874 return NULL;
875}
876
Jens Axboe5262f562019-09-17 12:26:57 -0600877static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
878{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600879 struct io_kiocb *req;
880
881 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700882 if (req) {
883 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
884 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800885 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700886 list_del_init(&req->list);
887 return req;
888 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600889 }
890
891 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600892}
893
Jens Axboede0617e2019-04-06 21:51:27 -0600894static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700895{
Hristo Venev75b28af2019-08-26 17:23:46 +0000896 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700897
Pavel Begunkov07910152020-01-17 03:52:46 +0300898 /* order cqe stores with ring update */
899 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700900
Pavel Begunkov07910152020-01-17 03:52:46 +0300901 if (wq_has_sleeper(&ctx->cq_wait)) {
902 wake_up_interruptible(&ctx->cq_wait);
903 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700904 }
905}
906
Jens Axboecccf0ee2020-01-27 16:34:48 -0700907static inline void io_req_work_grab_env(struct io_kiocb *req,
908 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600909{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700910 if (!req->work.mm && def->needs_mm) {
911 mmgrab(current->mm);
912 req->work.mm = current->mm;
913 }
914 if (!req->work.creds)
915 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700916 if (!req->work.fs && def->needs_fs) {
917 spin_lock(&current->fs->lock);
918 if (!current->fs->in_exec) {
919 req->work.fs = current->fs;
920 req->work.fs->users++;
921 } else {
922 req->work.flags |= IO_WQ_WORK_CANCEL;
923 }
924 spin_unlock(&current->fs->lock);
925 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700926 if (!req->work.task_pid)
927 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700928}
929
930static inline void io_req_work_drop_env(struct io_kiocb *req)
931{
932 if (req->work.mm) {
933 mmdrop(req->work.mm);
934 req->work.mm = NULL;
935 }
936 if (req->work.creds) {
937 put_cred(req->work.creds);
938 req->work.creds = NULL;
939 }
Jens Axboeff002b32020-02-07 16:05:21 -0700940 if (req->work.fs) {
941 struct fs_struct *fs = req->work.fs;
942
943 spin_lock(&req->work.fs->lock);
944 if (--fs->users)
945 fs = NULL;
946 spin_unlock(&req->work.fs->lock);
947 if (fs)
948 free_fs_struct(fs);
949 }
Jens Axboe561fb042019-10-24 07:25:42 -0600950}
951
Pavel Begunkovdeb6dc02020-02-24 11:30:17 +0300952static inline void io_prep_next_work(struct io_kiocb *req,
953 struct io_kiocb **link)
954{
955 const struct io_op_def *def = &io_op_defs[req->opcode];
956
957 if (!(req->flags & REQ_F_ISREG) && def->unbound_nonreg_file)
958 req->work.flags |= IO_WQ_WORK_UNBOUND;
959
960 *link = io_prep_linked_timeout(req);
961}
962
Jens Axboe94ae5e72019-11-14 19:39:52 -0700963static inline bool io_prep_async_work(struct io_kiocb *req,
964 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600965{
Jens Axboed3656342019-12-18 09:50:26 -0700966 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600967 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600968
Jens Axboed3656342019-12-18 09:50:26 -0700969 if (req->flags & REQ_F_ISREG) {
970 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700971 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700972 } else {
973 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700974 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600975 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700976
977 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600978
Jens Axboe94ae5e72019-11-14 19:39:52 -0700979 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600980 return do_hashed;
981}
982
Jackie Liua197f662019-11-08 08:09:12 -0700983static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600984{
Jackie Liua197f662019-11-08 08:09:12 -0700985 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700986 struct io_kiocb *link;
987 bool do_hashed;
988
989 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600990
991 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
992 req->flags);
993 if (!do_hashed) {
994 io_wq_enqueue(ctx->io_wq, &req->work);
995 } else {
996 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
997 file_inode(req->file));
998 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700999
1000 if (link)
1001 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001002}
1003
Jens Axboe5262f562019-09-17 12:26:57 -06001004static void io_kill_timeout(struct io_kiocb *req)
1005{
1006 int ret;
1007
Jens Axboe2d283902019-12-04 11:08:05 -07001008 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001009 if (ret != -1) {
1010 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001011 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001012 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001013 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001014 }
1015}
1016
1017static void io_kill_timeouts(struct io_ring_ctx *ctx)
1018{
1019 struct io_kiocb *req, *tmp;
1020
1021 spin_lock_irq(&ctx->completion_lock);
1022 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1023 io_kill_timeout(req);
1024 spin_unlock_irq(&ctx->completion_lock);
1025}
1026
Jens Axboede0617e2019-04-06 21:51:27 -06001027static void io_commit_cqring(struct io_ring_ctx *ctx)
1028{
1029 struct io_kiocb *req;
1030
Jens Axboe5262f562019-09-17 12:26:57 -06001031 while ((req = io_get_timeout_req(ctx)) != NULL)
1032 io_kill_timeout(req);
1033
Jens Axboede0617e2019-04-06 21:51:27 -06001034 __io_commit_cqring(ctx);
1035
Pavel Begunkov87987892020-01-18 01:22:30 +03001036 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001037 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001038}
1039
Jens Axboe2b188cc2019-01-07 10:46:33 -07001040static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1041{
Hristo Venev75b28af2019-08-26 17:23:46 +00001042 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001043 unsigned tail;
1044
1045 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001046 /*
1047 * writes to the cq entry need to come after reading head; the
1048 * control dependency is enough as we're using WRITE_ONCE to
1049 * fill the cq entry
1050 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001051 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001052 return NULL;
1053
1054 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001055 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001056}
1057
Jens Axboef2842ab2020-01-08 11:04:00 -07001058static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1059{
Jens Axboef0b493e2020-02-01 21:30:11 -07001060 if (!ctx->cq_ev_fd)
1061 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001062 if (!ctx->eventfd_async)
1063 return true;
1064 return io_wq_current_is_worker() || in_interrupt();
1065}
1066
Jens Axboef0b493e2020-02-01 21:30:11 -07001067static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001068{
1069 if (waitqueue_active(&ctx->wait))
1070 wake_up(&ctx->wait);
1071 if (waitqueue_active(&ctx->sqo_wait))
1072 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001073 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001074 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001075}
1076
Jens Axboef0b493e2020-02-01 21:30:11 -07001077static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1078{
1079 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1080}
1081
Jens Axboec4a2ed72019-11-21 21:01:26 -07001082/* Returns true if there are no backlogged entries after the flush */
1083static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001085 struct io_rings *rings = ctx->rings;
1086 struct io_uring_cqe *cqe;
1087 struct io_kiocb *req;
1088 unsigned long flags;
1089 LIST_HEAD(list);
1090
1091 if (!force) {
1092 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001093 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001094 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1095 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001096 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001097 }
1098
1099 spin_lock_irqsave(&ctx->completion_lock, flags);
1100
1101 /* if force is set, the ring is going away. always drop after that */
1102 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001103 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001104
Jens Axboec4a2ed72019-11-21 21:01:26 -07001105 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001106 while (!list_empty(&ctx->cq_overflow_list)) {
1107 cqe = io_get_cqring(ctx);
1108 if (!cqe && !force)
1109 break;
1110
1111 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1112 list);
1113 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001114 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001115 if (cqe) {
1116 WRITE_ONCE(cqe->user_data, req->user_data);
1117 WRITE_ONCE(cqe->res, req->result);
1118 WRITE_ONCE(cqe->flags, 0);
1119 } else {
1120 WRITE_ONCE(ctx->rings->cq_overflow,
1121 atomic_inc_return(&ctx->cached_cq_overflow));
1122 }
1123 }
1124
1125 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001126 if (cqe) {
1127 clear_bit(0, &ctx->sq_check_overflow);
1128 clear_bit(0, &ctx->cq_check_overflow);
1129 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001130 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1131 io_cqring_ev_posted(ctx);
1132
1133 while (!list_empty(&list)) {
1134 req = list_first_entry(&list, struct io_kiocb, list);
1135 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001136 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001137 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001138
1139 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001140}
1141
Jens Axboe78e19bb2019-11-06 15:21:34 -07001142static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001143{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001144 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001145 struct io_uring_cqe *cqe;
1146
Jens Axboe78e19bb2019-11-06 15:21:34 -07001147 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001148
Jens Axboe2b188cc2019-01-07 10:46:33 -07001149 /*
1150 * If we can't get a cq entry, userspace overflowed the
1151 * submission (by quite a lot). Increment the overflow count in
1152 * the ring.
1153 */
1154 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001155 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001156 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001157 WRITE_ONCE(cqe->res, res);
1158 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001159 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001160 WRITE_ONCE(ctx->rings->cq_overflow,
1161 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001162 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001163 if (list_empty(&ctx->cq_overflow_list)) {
1164 set_bit(0, &ctx->sq_check_overflow);
1165 set_bit(0, &ctx->cq_check_overflow);
1166 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001167 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001168 refcount_inc(&req->refs);
1169 req->result = res;
1170 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001171 }
1172}
1173
Jens Axboe78e19bb2019-11-06 15:21:34 -07001174static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001175{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001176 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177 unsigned long flags;
1178
1179 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001180 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001181 io_commit_cqring(ctx);
1182 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1183
Jens Axboe8c838782019-03-12 15:48:16 -06001184 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001185}
1186
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001187static inline bool io_is_fallback_req(struct io_kiocb *req)
1188{
1189 return req == (struct io_kiocb *)
1190 ((unsigned long) req->ctx->fallback_req & ~1UL);
1191}
1192
1193static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1194{
1195 struct io_kiocb *req;
1196
1197 req = ctx->fallback_req;
1198 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1199 return req;
1200
1201 return NULL;
1202}
1203
Jens Axboe2579f912019-01-09 09:10:43 -07001204static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1205 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001206{
Jens Axboefd6fab22019-03-14 16:30:06 -06001207 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001208 struct io_kiocb *req;
1209
Jens Axboe2579f912019-01-09 09:10:43 -07001210 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001211 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001212 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001213 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001214 } else if (!state->free_reqs) {
1215 size_t sz;
1216 int ret;
1217
1218 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001219 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1220
1221 /*
1222 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1223 * retry single alloc to be on the safe side.
1224 */
1225 if (unlikely(ret <= 0)) {
1226 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1227 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001228 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001229 ret = 1;
1230 }
Jens Axboe2579f912019-01-09 09:10:43 -07001231 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001232 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001233 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001234 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001235 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001236 }
1237
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001238got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001239 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001240 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001241 req->ctx = ctx;
1242 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001243 /* one is dropped after submission, the other at completion */
1244 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001245 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001246 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001247 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001248fallback:
1249 req = io_get_fallback_req(ctx);
1250 if (req)
1251 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001252 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001253 return NULL;
1254}
1255
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001256static inline void io_put_file(struct io_kiocb *req, struct file *file,
1257 bool fixed)
1258{
1259 if (fixed)
1260 percpu_ref_put(&req->ctx->file_data->refs);
1261 else
1262 fput(file);
1263}
1264
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001265static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001266{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001267 if (likely(!io_is_fallback_req(req)))
1268 kmem_cache_free(req_cachep, req);
1269 else
1270 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1271}
1272
Jens Axboec6ca97b302019-12-28 12:11:08 -07001273static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001274{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001275 if (req->flags & REQ_F_NEED_CLEANUP)
1276 io_cleanup_req(req);
1277
YueHaibing96fd84d2020-01-07 22:22:44 +08001278 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001279 if (req->file)
1280 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001281
1282 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001283}
1284
1285static void __io_free_req(struct io_kiocb *req)
1286{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001287 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001288
Jens Axboefcb323c2019-10-24 12:39:47 -06001289 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001290 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001291 unsigned long flags;
1292
1293 spin_lock_irqsave(&ctx->inflight_lock, flags);
1294 list_del(&req->inflight_entry);
1295 if (waitqueue_active(&ctx->inflight_wait))
1296 wake_up(&ctx->inflight_wait);
1297 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1298 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001299
1300 percpu_ref_put(&req->ctx->refs);
1301 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001302}
1303
Jens Axboec6ca97b302019-12-28 12:11:08 -07001304struct req_batch {
1305 void *reqs[IO_IOPOLL_BATCH];
1306 int to_free;
1307 int need_iter;
1308};
1309
1310static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1311{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001312 int fixed_refs = rb->to_free;
1313
Jens Axboec6ca97b302019-12-28 12:11:08 -07001314 if (!rb->to_free)
1315 return;
1316 if (rb->need_iter) {
1317 int i, inflight = 0;
1318 unsigned long flags;
1319
Jens Axboe10fef4b2020-01-09 07:52:28 -07001320 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001321 for (i = 0; i < rb->to_free; i++) {
1322 struct io_kiocb *req = rb->reqs[i];
1323
Jens Axboe10fef4b2020-01-09 07:52:28 -07001324 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001325 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001326 fixed_refs++;
1327 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001328 if (req->flags & REQ_F_INFLIGHT)
1329 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001330 __io_req_aux_free(req);
1331 }
1332 if (!inflight)
1333 goto do_free;
1334
1335 spin_lock_irqsave(&ctx->inflight_lock, flags);
1336 for (i = 0; i < rb->to_free; i++) {
1337 struct io_kiocb *req = rb->reqs[i];
1338
Jens Axboe10fef4b2020-01-09 07:52:28 -07001339 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001340 list_del(&req->inflight_entry);
1341 if (!--inflight)
1342 break;
1343 }
1344 }
1345 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1346
1347 if (waitqueue_active(&ctx->inflight_wait))
1348 wake_up(&ctx->inflight_wait);
1349 }
1350do_free:
1351 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001352 if (fixed_refs)
1353 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001354 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001355 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001356}
1357
Jackie Liua197f662019-11-08 08:09:12 -07001358static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001359{
Jackie Liua197f662019-11-08 08:09:12 -07001360 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001361 int ret;
1362
Jens Axboe2d283902019-12-04 11:08:05 -07001363 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001364 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001365 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001366 io_commit_cqring(ctx);
1367 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001368 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001369 return true;
1370 }
1371
1372 return false;
1373}
1374
Jens Axboeba816ad2019-09-28 11:36:45 -06001375static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001376{
Jens Axboe2665abf2019-11-05 12:40:47 -07001377 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001378 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001379
Jens Axboe4d7dd462019-11-20 13:03:52 -07001380 /* Already got next link */
1381 if (req->flags & REQ_F_LINK_NEXT)
1382 return;
1383
Jens Axboe9e645e112019-05-10 16:07:28 -06001384 /*
1385 * The list should never be empty when we are called here. But could
1386 * potentially happen if the chain is messed up, check to be on the
1387 * safe side.
1388 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001389 while (!list_empty(&req->link_list)) {
1390 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1391 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001392
Pavel Begunkov44932332019-12-05 16:16:35 +03001393 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1394 (nxt->flags & REQ_F_TIMEOUT))) {
1395 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001396 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001397 req->flags &= ~REQ_F_LINK_TIMEOUT;
1398 continue;
1399 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001400
Pavel Begunkov44932332019-12-05 16:16:35 +03001401 list_del_init(&req->link_list);
1402 if (!list_empty(&nxt->link_list))
1403 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001404 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001405 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001406 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001407
Jens Axboe4d7dd462019-11-20 13:03:52 -07001408 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001409 if (wake_ev)
1410 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001411}
1412
1413/*
1414 * Called if REQ_F_LINK is set, and we fail the head request
1415 */
1416static void io_fail_links(struct io_kiocb *req)
1417{
Jens Axboe2665abf2019-11-05 12:40:47 -07001418 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001419 unsigned long flags;
1420
1421 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001422
1423 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001424 struct io_kiocb *link = list_first_entry(&req->link_list,
1425 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001426
Pavel Begunkov44932332019-12-05 16:16:35 +03001427 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001428 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001429
1430 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001431 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001432 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001433 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001434 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001435 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001436 }
Jens Axboe5d960722019-11-19 15:31:28 -07001437 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001438 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001439
1440 io_commit_cqring(ctx);
1441 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1442 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001443}
1444
Jens Axboe4d7dd462019-11-20 13:03:52 -07001445static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001446{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001447 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001448 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001449
Jens Axboe9e645e112019-05-10 16:07:28 -06001450 /*
1451 * If LINK is set, we have dependent requests in this chain. If we
1452 * didn't fail this request, queue the first one up, moving any other
1453 * dependencies to the next request. In case of failure, fail the rest
1454 * of the chain.
1455 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001456 if (req->flags & REQ_F_FAIL_LINK) {
1457 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001458 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1459 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001460 struct io_ring_ctx *ctx = req->ctx;
1461 unsigned long flags;
1462
1463 /*
1464 * If this is a timeout link, we could be racing with the
1465 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001466 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001467 */
1468 spin_lock_irqsave(&ctx->completion_lock, flags);
1469 io_req_link_next(req, nxt);
1470 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1471 } else {
1472 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001473 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001474}
Jens Axboe9e645e112019-05-10 16:07:28 -06001475
Jackie Liuc69f8db2019-11-09 11:00:08 +08001476static void io_free_req(struct io_kiocb *req)
1477{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001478 struct io_kiocb *nxt = NULL;
1479
1480 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001481 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001482
1483 if (nxt)
1484 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001485}
1486
Jens Axboeba816ad2019-09-28 11:36:45 -06001487/*
1488 * Drop reference to request, return next in chain (if there is one) if this
1489 * was the last reference to this request.
1490 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001491__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001492static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001493{
Jens Axboe2a44f462020-02-25 13:25:41 -07001494 if (refcount_dec_and_test(&req->refs)) {
1495 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001496 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001497 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001498}
1499
Jens Axboe2b188cc2019-01-07 10:46:33 -07001500static void io_put_req(struct io_kiocb *req)
1501{
Jens Axboedef596e2019-01-09 08:59:42 -07001502 if (refcount_dec_and_test(&req->refs))
1503 io_free_req(req);
1504}
1505
Jens Axboe978db572019-11-14 22:39:04 -07001506/*
1507 * Must only be used if we don't need to care about links, usually from
1508 * within the completion handling itself.
1509 */
1510static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001511{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001512 /* drop both submit and complete references */
1513 if (refcount_sub_and_test(2, &req->refs))
1514 __io_free_req(req);
1515}
1516
Jens Axboe978db572019-11-14 22:39:04 -07001517static void io_double_put_req(struct io_kiocb *req)
1518{
1519 /* drop both submit and complete references */
1520 if (refcount_sub_and_test(2, &req->refs))
1521 io_free_req(req);
1522}
1523
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001524static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001525{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001526 struct io_rings *rings = ctx->rings;
1527
Jens Axboead3eb2c2019-12-18 17:12:20 -07001528 if (test_bit(0, &ctx->cq_check_overflow)) {
1529 /*
1530 * noflush == true is from the waitqueue handler, just ensure
1531 * we wake up the task, and the next invocation will flush the
1532 * entries. We cannot safely to it from here.
1533 */
1534 if (noflush && !list_empty(&ctx->cq_overflow_list))
1535 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001536
Jens Axboead3eb2c2019-12-18 17:12:20 -07001537 io_cqring_overflow_flush(ctx, false);
1538 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001539
Jens Axboea3a0e432019-08-20 11:03:11 -06001540 /* See comment at the top of this file */
1541 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001542 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001543}
1544
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001545static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1546{
1547 struct io_rings *rings = ctx->rings;
1548
1549 /* make sure SQ entry isn't read before tail */
1550 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1551}
1552
Jens Axboe8237e042019-12-28 10:48:22 -07001553static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001554{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001555 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1556 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001557
Jens Axboec6ca97b302019-12-28 12:11:08 -07001558 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1559 rb->need_iter++;
1560
1561 rb->reqs[rb->to_free++] = req;
1562 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1563 io_free_req_many(req->ctx, rb);
1564 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001565}
1566
Jens Axboedef596e2019-01-09 08:59:42 -07001567/*
1568 * Find and free completed poll iocbs
1569 */
1570static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1571 struct list_head *done)
1572{
Jens Axboe8237e042019-12-28 10:48:22 -07001573 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001574 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001575
Jens Axboec6ca97b302019-12-28 12:11:08 -07001576 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001577 while (!list_empty(done)) {
1578 req = list_first_entry(done, struct io_kiocb, list);
1579 list_del(&req->list);
1580
Jens Axboe78e19bb2019-11-06 15:21:34 -07001581 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001582 (*nr_events)++;
1583
Jens Axboe8237e042019-12-28 10:48:22 -07001584 if (refcount_dec_and_test(&req->refs) &&
1585 !io_req_multi_free(&rb, req))
1586 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001587 }
Jens Axboedef596e2019-01-09 08:59:42 -07001588
Jens Axboe09bb8392019-03-13 12:39:28 -06001589 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001590 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001591}
1592
1593static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1594 long min)
1595{
1596 struct io_kiocb *req, *tmp;
1597 LIST_HEAD(done);
1598 bool spin;
1599 int ret;
1600
1601 /*
1602 * Only spin for completions if we don't have multiple devices hanging
1603 * off our complete list, and we're under the requested amount.
1604 */
1605 spin = !ctx->poll_multi_file && *nr_events < min;
1606
1607 ret = 0;
1608 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001609 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001610
1611 /*
1612 * Move completed entries to our local list. If we find a
1613 * request that requires polling, break out and complete
1614 * the done list first, if we have entries there.
1615 */
1616 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1617 list_move_tail(&req->list, &done);
1618 continue;
1619 }
1620 if (!list_empty(&done))
1621 break;
1622
1623 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1624 if (ret < 0)
1625 break;
1626
1627 if (ret && spin)
1628 spin = false;
1629 ret = 0;
1630 }
1631
1632 if (!list_empty(&done))
1633 io_iopoll_complete(ctx, nr_events, &done);
1634
1635 return ret;
1636}
1637
1638/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001639 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001640 * non-spinning poll check - we'll still enter the driver poll loop, but only
1641 * as a non-spinning completion check.
1642 */
1643static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1644 long min)
1645{
Jens Axboe08f54392019-08-21 22:19:11 -06001646 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001647 int ret;
1648
1649 ret = io_do_iopoll(ctx, nr_events, min);
1650 if (ret < 0)
1651 return ret;
1652 if (!min || *nr_events >= min)
1653 return 0;
1654 }
1655
1656 return 1;
1657}
1658
1659/*
1660 * We can't just wait for polled events to come to us, we have to actively
1661 * find and complete them.
1662 */
1663static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1664{
1665 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1666 return;
1667
1668 mutex_lock(&ctx->uring_lock);
1669 while (!list_empty(&ctx->poll_list)) {
1670 unsigned int nr_events = 0;
1671
1672 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001673
1674 /*
1675 * Ensure we allow local-to-the-cpu processing to take place,
1676 * in this case we need to ensure that we reap all events.
1677 */
1678 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001679 }
1680 mutex_unlock(&ctx->uring_lock);
1681}
1682
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001683static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1684 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001685{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001686 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001687
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001688 /*
1689 * We disallow the app entering submit/complete with polling, but we
1690 * still need to lock the ring to prevent racing with polled issue
1691 * that got punted to a workqueue.
1692 */
1693 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001694 do {
1695 int tmin = 0;
1696
Jens Axboe500f9fb2019-08-19 12:15:59 -06001697 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001698 * Don't enter poll loop if we already have events pending.
1699 * If we do, we can potentially be spinning for commands that
1700 * already triggered a CQE (eg in error).
1701 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001702 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001703 break;
1704
1705 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001706 * If a submit got punted to a workqueue, we can have the
1707 * application entering polling for a command before it gets
1708 * issued. That app will hold the uring_lock for the duration
1709 * of the poll right here, so we need to take a breather every
1710 * now and then to ensure that the issue has a chance to add
1711 * the poll to the issued list. Otherwise we can spin here
1712 * forever, while the workqueue is stuck trying to acquire the
1713 * very same mutex.
1714 */
1715 if (!(++iters & 7)) {
1716 mutex_unlock(&ctx->uring_lock);
1717 mutex_lock(&ctx->uring_lock);
1718 }
1719
Jens Axboedef596e2019-01-09 08:59:42 -07001720 if (*nr_events < min)
1721 tmin = min - *nr_events;
1722
1723 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1724 if (ret <= 0)
1725 break;
1726 ret = 0;
1727 } while (min && !*nr_events && !need_resched());
1728
Jens Axboe500f9fb2019-08-19 12:15:59 -06001729 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001730 return ret;
1731}
1732
Jens Axboe491381ce2019-10-17 09:20:46 -06001733static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001734{
Jens Axboe491381ce2019-10-17 09:20:46 -06001735 /*
1736 * Tell lockdep we inherited freeze protection from submission
1737 * thread.
1738 */
1739 if (req->flags & REQ_F_ISREG) {
1740 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001741
Jens Axboe491381ce2019-10-17 09:20:46 -06001742 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001743 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001744 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001745}
1746
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001747static inline void req_set_fail_links(struct io_kiocb *req)
1748{
1749 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1750 req->flags |= REQ_F_FAIL_LINK;
1751}
1752
Jens Axboeba816ad2019-09-28 11:36:45 -06001753static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001754{
Jens Axboe9adbd452019-12-20 08:45:55 -07001755 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001756
Jens Axboe491381ce2019-10-17 09:20:46 -06001757 if (kiocb->ki_flags & IOCB_WRITE)
1758 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001759
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001760 if (res != req->result)
1761 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001762 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001763}
1764
1765static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1766{
Jens Axboe9adbd452019-12-20 08:45:55 -07001767 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001768
1769 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001770 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001771}
1772
Jens Axboeba816ad2019-09-28 11:36:45 -06001773static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1774{
Jens Axboe9adbd452019-12-20 08:45:55 -07001775 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001776 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001777
1778 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001779 io_put_req_find_next(req, &nxt);
1780
1781 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001782}
1783
Jens Axboedef596e2019-01-09 08:59:42 -07001784static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1785{
Jens Axboe9adbd452019-12-20 08:45:55 -07001786 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001787
Jens Axboe491381ce2019-10-17 09:20:46 -06001788 if (kiocb->ki_flags & IOCB_WRITE)
1789 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001790
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001791 if (res != req->result)
1792 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001793 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001794 if (res != -EAGAIN)
1795 req->flags |= REQ_F_IOPOLL_COMPLETED;
1796}
1797
1798/*
1799 * After the iocb has been issued, it's safe to be found on the poll list.
1800 * Adding the kiocb to the list AFTER submission ensures that we don't
1801 * find it from a io_iopoll_getevents() thread before the issuer is done
1802 * accessing the kiocb cookie.
1803 */
1804static void io_iopoll_req_issued(struct io_kiocb *req)
1805{
1806 struct io_ring_ctx *ctx = req->ctx;
1807
1808 /*
1809 * Track whether we have multiple files in our lists. This will impact
1810 * how we do polling eventually, not spinning if we're on potentially
1811 * different devices.
1812 */
1813 if (list_empty(&ctx->poll_list)) {
1814 ctx->poll_multi_file = false;
1815 } else if (!ctx->poll_multi_file) {
1816 struct io_kiocb *list_req;
1817
1818 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1819 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001820 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001821 ctx->poll_multi_file = true;
1822 }
1823
1824 /*
1825 * For fast devices, IO may have already completed. If it has, add
1826 * it to the front so we find it first.
1827 */
1828 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1829 list_add(&req->list, &ctx->poll_list);
1830 else
1831 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001832
1833 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1834 wq_has_sleeper(&ctx->sqo_wait))
1835 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001836}
1837
Jens Axboe3d6770f2019-04-13 11:50:54 -06001838static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001839{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001840 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001841 int diff = state->has_refs - state->used_refs;
1842
1843 if (diff)
1844 fput_many(state->file, diff);
1845 state->file = NULL;
1846 }
1847}
1848
1849/*
1850 * Get as many references to a file as we have IOs left in this submission,
1851 * assuming most submissions are for one file, or at least that each file
1852 * has more than one submission.
1853 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001854static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001855{
1856 if (!state)
1857 return fget(fd);
1858
1859 if (state->file) {
1860 if (state->fd == fd) {
1861 state->used_refs++;
1862 state->ios_left--;
1863 return state->file;
1864 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001865 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001866 }
1867 state->file = fget_many(fd, state->ios_left);
1868 if (!state->file)
1869 return NULL;
1870
1871 state->fd = fd;
1872 state->has_refs = state->ios_left;
1873 state->used_refs = 1;
1874 state->ios_left--;
1875 return state->file;
1876}
1877
Jens Axboe2b188cc2019-01-07 10:46:33 -07001878/*
1879 * If we tracked the file through the SCM inflight mechanism, we could support
1880 * any file. For now, just ensure that anything potentially problematic is done
1881 * inline.
1882 */
1883static bool io_file_supports_async(struct file *file)
1884{
1885 umode_t mode = file_inode(file)->i_mode;
1886
Jens Axboe10d59342019-12-09 20:16:22 -07001887 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001888 return true;
1889 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1890 return true;
1891
1892 return false;
1893}
1894
Jens Axboe3529d8c2019-12-19 18:24:38 -07001895static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1896 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001897{
Jens Axboedef596e2019-01-09 08:59:42 -07001898 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001899 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001900 unsigned ioprio;
1901 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001902
Jens Axboe491381ce2019-10-17 09:20:46 -06001903 if (S_ISREG(file_inode(req->file)->i_mode))
1904 req->flags |= REQ_F_ISREG;
1905
Jens Axboe2b188cc2019-01-07 10:46:33 -07001906 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001907 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1908 req->flags |= REQ_F_CUR_POS;
1909 kiocb->ki_pos = req->file->f_pos;
1910 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001911 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001912 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1913 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1914 if (unlikely(ret))
1915 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001916
1917 ioprio = READ_ONCE(sqe->ioprio);
1918 if (ioprio) {
1919 ret = ioprio_check_cap(ioprio);
1920 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001921 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001922
1923 kiocb->ki_ioprio = ioprio;
1924 } else
1925 kiocb->ki_ioprio = get_current_ioprio();
1926
Stefan Bühler8449eed2019-04-27 20:34:19 +02001927 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001928 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1929 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001930 req->flags |= REQ_F_NOWAIT;
1931
1932 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001933 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001934
Jens Axboedef596e2019-01-09 08:59:42 -07001935 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001936 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1937 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001938 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001939
Jens Axboedef596e2019-01-09 08:59:42 -07001940 kiocb->ki_flags |= IOCB_HIPRI;
1941 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001942 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001943 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001944 if (kiocb->ki_flags & IOCB_HIPRI)
1945 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001946 kiocb->ki_complete = io_complete_rw;
1947 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001948
Jens Axboe3529d8c2019-12-19 18:24:38 -07001949 req->rw.addr = READ_ONCE(sqe->addr);
1950 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001951 /* we own ->private, reuse it for the buffer index */
1952 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001953 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001954 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001955}
1956
1957static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1958{
1959 switch (ret) {
1960 case -EIOCBQUEUED:
1961 break;
1962 case -ERESTARTSYS:
1963 case -ERESTARTNOINTR:
1964 case -ERESTARTNOHAND:
1965 case -ERESTART_RESTARTBLOCK:
1966 /*
1967 * We can't just restart the syscall, since previously
1968 * submitted sqes may already be in progress. Just fail this
1969 * IO with EINTR.
1970 */
1971 ret = -EINTR;
1972 /* fall through */
1973 default:
1974 kiocb->ki_complete(kiocb, ret, 0);
1975 }
1976}
1977
Pavel Begunkovbcaec082020-02-24 11:30:18 +03001978static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt)
Jens Axboeba816ad2019-09-28 11:36:45 -06001979{
Jens Axboeba042912019-12-25 16:33:42 -07001980 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1981
1982 if (req->flags & REQ_F_CUR_POS)
1983 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03001984 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001985 *nxt = __io_complete_rw(kiocb, ret);
1986 else
1987 io_rw_done(kiocb, ret);
1988}
1989
Jens Axboe9adbd452019-12-20 08:45:55 -07001990static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001991 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001992{
Jens Axboe9adbd452019-12-20 08:45:55 -07001993 struct io_ring_ctx *ctx = req->ctx;
1994 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001995 struct io_mapped_ubuf *imu;
1996 unsigned index, buf_index;
1997 size_t offset;
1998 u64 buf_addr;
1999
2000 /* attempt to use fixed buffers without having provided iovecs */
2001 if (unlikely(!ctx->user_bufs))
2002 return -EFAULT;
2003
Jens Axboe9adbd452019-12-20 08:45:55 -07002004 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002005 if (unlikely(buf_index >= ctx->nr_user_bufs))
2006 return -EFAULT;
2007
2008 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2009 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002010 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002011
2012 /* overflow */
2013 if (buf_addr + len < buf_addr)
2014 return -EFAULT;
2015 /* not inside the mapped region */
2016 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2017 return -EFAULT;
2018
2019 /*
2020 * May not be a start of buffer, set size appropriately
2021 * and advance us to the beginning.
2022 */
2023 offset = buf_addr - imu->ubuf;
2024 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002025
2026 if (offset) {
2027 /*
2028 * Don't use iov_iter_advance() here, as it's really slow for
2029 * using the latter parts of a big fixed buffer - it iterates
2030 * over each segment manually. We can cheat a bit here, because
2031 * we know that:
2032 *
2033 * 1) it's a BVEC iter, we set it up
2034 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2035 * first and last bvec
2036 *
2037 * So just find our index, and adjust the iterator afterwards.
2038 * If the offset is within the first bvec (or the whole first
2039 * bvec, just use iov_iter_advance(). This makes it easier
2040 * since we can just skip the first segment, which may not
2041 * be PAGE_SIZE aligned.
2042 */
2043 const struct bio_vec *bvec = imu->bvec;
2044
2045 if (offset <= bvec->bv_len) {
2046 iov_iter_advance(iter, offset);
2047 } else {
2048 unsigned long seg_skip;
2049
2050 /* skip first vec */
2051 offset -= bvec->bv_len;
2052 seg_skip = 1 + (offset >> PAGE_SHIFT);
2053
2054 iter->bvec = bvec + seg_skip;
2055 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002056 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002057 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002058 }
2059 }
2060
Jens Axboe5e559562019-11-13 16:12:46 -07002061 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002062}
2063
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002064static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2065 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002066{
Jens Axboe9adbd452019-12-20 08:45:55 -07002067 void __user *buf = u64_to_user_ptr(req->rw.addr);
2068 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002069 u8 opcode;
2070
Jens Axboed625c6e2019-12-17 19:53:05 -07002071 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002072 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002073 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002074 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002075 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002076
Jens Axboe9adbd452019-12-20 08:45:55 -07002077 /* buffer index only valid with fixed read/write */
2078 if (req->rw.kiocb.private)
2079 return -EINVAL;
2080
Jens Axboe3a6820f2019-12-22 15:19:35 -07002081 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2082 ssize_t ret;
2083 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2084 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002085 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002086 }
2087
Jens Axboef67676d2019-12-02 11:03:47 -07002088 if (req->io) {
2089 struct io_async_rw *iorw = &req->io->rw;
2090
2091 *iovec = iorw->iov;
2092 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2093 if (iorw->iov == iorw->fast_iov)
2094 *iovec = NULL;
2095 return iorw->size;
2096 }
2097
Jens Axboe2b188cc2019-01-07 10:46:33 -07002098#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002099 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002100 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2101 iovec, iter);
2102#endif
2103
2104 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2105}
2106
Jens Axboe32960612019-09-23 11:05:34 -06002107/*
2108 * For files that don't have ->read_iter() and ->write_iter(), handle them
2109 * by looping over ->read() or ->write() manually.
2110 */
2111static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2112 struct iov_iter *iter)
2113{
2114 ssize_t ret = 0;
2115
2116 /*
2117 * Don't support polled IO through this interface, and we can't
2118 * support non-blocking either. For the latter, this just causes
2119 * the kiocb to be handled from an async context.
2120 */
2121 if (kiocb->ki_flags & IOCB_HIPRI)
2122 return -EOPNOTSUPP;
2123 if (kiocb->ki_flags & IOCB_NOWAIT)
2124 return -EAGAIN;
2125
2126 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002127 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002128 ssize_t nr;
2129
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002130 if (!iov_iter_is_bvec(iter)) {
2131 iovec = iov_iter_iovec(iter);
2132 } else {
2133 /* fixed buffers import bvec */
2134 iovec.iov_base = kmap(iter->bvec->bv_page)
2135 + iter->iov_offset;
2136 iovec.iov_len = min(iter->count,
2137 iter->bvec->bv_len - iter->iov_offset);
2138 }
2139
Jens Axboe32960612019-09-23 11:05:34 -06002140 if (rw == READ) {
2141 nr = file->f_op->read(file, iovec.iov_base,
2142 iovec.iov_len, &kiocb->ki_pos);
2143 } else {
2144 nr = file->f_op->write(file, iovec.iov_base,
2145 iovec.iov_len, &kiocb->ki_pos);
2146 }
2147
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002148 if (iov_iter_is_bvec(iter))
2149 kunmap(iter->bvec->bv_page);
2150
Jens Axboe32960612019-09-23 11:05:34 -06002151 if (nr < 0) {
2152 if (!ret)
2153 ret = nr;
2154 break;
2155 }
2156 ret += nr;
2157 if (nr != iovec.iov_len)
2158 break;
2159 iov_iter_advance(iter, nr);
2160 }
2161
2162 return ret;
2163}
2164
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002165static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002166 struct iovec *iovec, struct iovec *fast_iov,
2167 struct iov_iter *iter)
2168{
2169 req->io->rw.nr_segs = iter->nr_segs;
2170 req->io->rw.size = io_size;
2171 req->io->rw.iov = iovec;
2172 if (!req->io->rw.iov) {
2173 req->io->rw.iov = req->io->rw.fast_iov;
2174 memcpy(req->io->rw.iov, fast_iov,
2175 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002176 } else {
2177 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002178 }
2179}
2180
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002181static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002182{
Jens Axboed3656342019-12-18 09:50:26 -07002183 if (!io_op_defs[req->opcode].async_ctx)
2184 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002185 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002186 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002187}
2188
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002189static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2190 struct iovec *iovec, struct iovec *fast_iov,
2191 struct iov_iter *iter)
2192{
Jens Axboe980ad262020-01-24 23:08:54 -07002193 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002194 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002195 if (!req->io) {
2196 if (io_alloc_async_ctx(req))
2197 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002198
Jens Axboe5d204bc2020-01-31 12:06:52 -07002199 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2200 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002201 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002202}
2203
Jens Axboe3529d8c2019-12-19 18:24:38 -07002204static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2205 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002206{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002207 struct io_async_ctx *io;
2208 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002209 ssize_t ret;
2210
Jens Axboe3529d8c2019-12-19 18:24:38 -07002211 ret = io_prep_rw(req, sqe, force_nonblock);
2212 if (ret)
2213 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002214
Jens Axboe3529d8c2019-12-19 18:24:38 -07002215 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2216 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002217
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002218 /* either don't need iovec imported or already have it */
2219 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002220 return 0;
2221
2222 io = req->io;
2223 io->rw.iov = io->rw.fast_iov;
2224 req->io = NULL;
2225 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2226 req->io = io;
2227 if (ret < 0)
2228 return ret;
2229
2230 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2231 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002232}
2233
Pavel Begunkov267bc902019-11-07 01:41:08 +03002234static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002235 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002236{
2237 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002238 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002239 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002240 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002241 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002242
Jens Axboe3529d8c2019-12-19 18:24:38 -07002243 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002244 if (ret < 0)
2245 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002246
Jens Axboefd6c2e42019-12-18 12:19:41 -07002247 /* Ensure we clear previously set non-block flag */
2248 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002249 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002250
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002251 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002252 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002253 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002254 req->result = io_size;
2255
2256 /*
2257 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2258 * we know to async punt it even if it was opened O_NONBLOCK
2259 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002260 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002261 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002262
Jens Axboe31b51512019-01-18 22:56:34 -07002263 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002264 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002265 if (!ret) {
2266 ssize_t ret2;
2267
Jens Axboe9adbd452019-12-20 08:45:55 -07002268 if (req->file->f_op->read_iter)
2269 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002270 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002271 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002272
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002273 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002274 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002275 kiocb_done(kiocb, ret2, nxt);
Jens Axboef67676d2019-12-02 11:03:47 -07002276 } else {
2277copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002278 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002279 inline_vecs, &iter);
2280 if (ret)
2281 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002282 /* any defer here is final, must blocking retry */
2283 if (!(req->flags & REQ_F_NOWAIT))
2284 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002285 return -EAGAIN;
2286 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002287 }
Jens Axboef67676d2019-12-02 11:03:47 -07002288out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002289 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002290 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002291 return ret;
2292}
2293
Jens Axboe3529d8c2019-12-19 18:24:38 -07002294static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2295 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002296{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002297 struct io_async_ctx *io;
2298 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002299 ssize_t ret;
2300
Jens Axboe3529d8c2019-12-19 18:24:38 -07002301 ret = io_prep_rw(req, sqe, force_nonblock);
2302 if (ret)
2303 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002304
Jens Axboe3529d8c2019-12-19 18:24:38 -07002305 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2306 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002307
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002308 /* either don't need iovec imported or already have it */
2309 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002310 return 0;
2311
2312 io = req->io;
2313 io->rw.iov = io->rw.fast_iov;
2314 req->io = NULL;
2315 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2316 req->io = io;
2317 if (ret < 0)
2318 return ret;
2319
2320 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2321 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002322}
2323
Pavel Begunkov267bc902019-11-07 01:41:08 +03002324static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002325 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002326{
2327 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002328 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002329 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002330 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002331 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002332
Jens Axboe3529d8c2019-12-19 18:24:38 -07002333 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002334 if (ret < 0)
2335 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002336
Jens Axboefd6c2e42019-12-18 12:19:41 -07002337 /* Ensure we clear previously set non-block flag */
2338 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002339 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002340
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002341 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002342 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002343 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002344 req->result = io_size;
2345
2346 /*
2347 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2348 * we know to async punt it even if it was opened O_NONBLOCK
2349 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002350 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002351 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002352
Jens Axboe10d59342019-12-09 20:16:22 -07002353 /* file path doesn't support NOWAIT for non-direct_IO */
2354 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2355 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002356 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002357
Jens Axboe31b51512019-01-18 22:56:34 -07002358 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002359 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002360 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002361 ssize_t ret2;
2362
Jens Axboe2b188cc2019-01-07 10:46:33 -07002363 /*
2364 * Open-code file_start_write here to grab freeze protection,
2365 * which will be released by another thread in
2366 * io_complete_rw(). Fool lockdep by telling it the lock got
2367 * released so that it doesn't complain about the held lock when
2368 * we return to userspace.
2369 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002370 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002371 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002372 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002373 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002374 SB_FREEZE_WRITE);
2375 }
2376 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002377
Jens Axboe9adbd452019-12-20 08:45:55 -07002378 if (req->file->f_op->write_iter)
2379 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002380 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002381 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002382 /*
2383 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2384 * retry them without IOCB_NOWAIT.
2385 */
2386 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2387 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002388 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002389 kiocb_done(kiocb, ret2, nxt);
Jens Axboef67676d2019-12-02 11:03:47 -07002390 } else {
2391copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002392 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002393 inline_vecs, &iter);
2394 if (ret)
2395 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002396 /* any defer here is final, must blocking retry */
2397 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002398 return -EAGAIN;
2399 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002400 }
Jens Axboe31b51512019-01-18 22:56:34 -07002401out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002402 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002403 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002404 return ret;
2405}
2406
2407/*
2408 * IORING_OP_NOP just posts a completion event, nothing else.
2409 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002410static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002411{
2412 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002413
Jens Axboedef596e2019-01-09 08:59:42 -07002414 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2415 return -EINVAL;
2416
Jens Axboe78e19bb2019-11-06 15:21:34 -07002417 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002418 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002419 return 0;
2420}
2421
Jens Axboe3529d8c2019-12-19 18:24:38 -07002422static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002423{
Jens Axboe6b063142019-01-10 22:13:58 -07002424 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002425
Jens Axboe09bb8392019-03-13 12:39:28 -06002426 if (!req->file)
2427 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002428
Jens Axboe6b063142019-01-10 22:13:58 -07002429 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002430 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002431 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002432 return -EINVAL;
2433
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002434 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2435 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2436 return -EINVAL;
2437
2438 req->sync.off = READ_ONCE(sqe->off);
2439 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002440 return 0;
2441}
2442
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002443static bool io_req_cancelled(struct io_kiocb *req)
2444{
2445 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2446 req_set_fail_links(req);
2447 io_cqring_add_event(req, -ECANCELED);
2448 io_put_req(req);
2449 return true;
2450 }
2451
2452 return false;
2453}
2454
Jens Axboe78912932020-01-14 22:09:06 -07002455static void io_link_work_cb(struct io_wq_work **workptr)
2456{
2457 struct io_wq_work *work = *workptr;
2458 struct io_kiocb *link = work->data;
2459
2460 io_queue_linked_timeout(link);
2461 work->func = io_wq_submit_work;
2462}
2463
2464static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2465{
2466 struct io_kiocb *link;
2467
Pavel Begunkovdeb6dc02020-02-24 11:30:17 +03002468 io_prep_next_work(nxt, &link);
Jens Axboe78912932020-01-14 22:09:06 -07002469 *workptr = &nxt->work;
2470 if (link) {
2471 nxt->work.flags |= IO_WQ_WORK_CB;
2472 nxt->work.func = io_link_work_cb;
2473 nxt->work.data = link;
2474 }
2475}
2476
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002477static void __io_fsync(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002478{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002479 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002480 int ret;
2481
Jens Axboe9adbd452019-12-20 08:45:55 -07002482 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002483 end > 0 ? end : LLONG_MAX,
2484 req->sync.flags & IORING_FSYNC_DATASYNC);
2485 if (ret < 0)
2486 req_set_fail_links(req);
2487 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002488 io_put_req_find_next(req, nxt);
2489}
2490
2491static void io_fsync_finish(struct io_wq_work **workptr)
2492{
2493 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2494 struct io_kiocb *nxt = NULL;
2495
2496 if (io_req_cancelled(req))
2497 return;
2498 __io_fsync(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002499 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002500 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002501}
2502
Jens Axboefc4df992019-12-10 14:38:45 -07002503static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2504 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002505{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002506 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002507 if (force_nonblock) {
2508 io_put_req(req);
2509 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002510 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002511 }
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002512 __io_fsync(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002513 return 0;
2514}
2515
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002516static void __io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboed63d1b52019-12-10 10:38:56 -07002517{
Jens Axboed63d1b52019-12-10 10:38:56 -07002518 int ret;
2519
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002520 if (io_req_cancelled(req))
2521 return;
2522
Jens Axboed63d1b52019-12-10 10:38:56 -07002523 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2524 req->sync.len);
2525 if (ret < 0)
2526 req_set_fail_links(req);
2527 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002528 io_put_req_find_next(req, nxt);
2529}
2530
2531static void io_fallocate_finish(struct io_wq_work **workptr)
2532{
2533 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2534 struct io_kiocb *nxt = NULL;
2535
2536 __io_fallocate(req, &nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002537 if (nxt)
2538 io_wq_assign_next(workptr, nxt);
2539}
2540
2541static int io_fallocate_prep(struct io_kiocb *req,
2542 const struct io_uring_sqe *sqe)
2543{
2544 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2545 return -EINVAL;
2546
2547 req->sync.off = READ_ONCE(sqe->off);
2548 req->sync.len = READ_ONCE(sqe->addr);
2549 req->sync.mode = READ_ONCE(sqe->len);
2550 return 0;
2551}
2552
2553static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2554 bool force_nonblock)
2555{
Jens Axboed63d1b52019-12-10 10:38:56 -07002556 /* fallocate always requiring blocking context */
2557 if (force_nonblock) {
2558 io_put_req(req);
2559 req->work.func = io_fallocate_finish;
2560 return -EAGAIN;
2561 }
2562
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002563 __io_fallocate(req, nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002564 return 0;
2565}
2566
Jens Axboe15b71ab2019-12-11 11:20:36 -07002567static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2568{
Jens Axboef8748882020-01-08 17:47:02 -07002569 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002570 int ret;
2571
2572 if (sqe->ioprio || sqe->buf_index)
2573 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002574 if (sqe->flags & IOSQE_FIXED_FILE)
2575 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002576 if (req->flags & REQ_F_NEED_CLEANUP)
2577 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002578
2579 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002580 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002581 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002582 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002583
Jens Axboef8748882020-01-08 17:47:02 -07002584 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002585 if (IS_ERR(req->open.filename)) {
2586 ret = PTR_ERR(req->open.filename);
2587 req->open.filename = NULL;
2588 return ret;
2589 }
2590
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002591 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002592 return 0;
2593}
2594
Jens Axboecebdb982020-01-08 17:59:24 -07002595static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2596{
2597 struct open_how __user *how;
2598 const char __user *fname;
2599 size_t len;
2600 int ret;
2601
2602 if (sqe->ioprio || sqe->buf_index)
2603 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002604 if (sqe->flags & IOSQE_FIXED_FILE)
2605 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002606 if (req->flags & REQ_F_NEED_CLEANUP)
2607 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002608
2609 req->open.dfd = READ_ONCE(sqe->fd);
2610 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2611 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2612 len = READ_ONCE(sqe->len);
2613
2614 if (len < OPEN_HOW_SIZE_VER0)
2615 return -EINVAL;
2616
2617 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2618 len);
2619 if (ret)
2620 return ret;
2621
2622 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2623 req->open.how.flags |= O_LARGEFILE;
2624
2625 req->open.filename = getname(fname);
2626 if (IS_ERR(req->open.filename)) {
2627 ret = PTR_ERR(req->open.filename);
2628 req->open.filename = NULL;
2629 return ret;
2630 }
2631
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002632 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002633 return 0;
2634}
2635
2636static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2637 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002638{
2639 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002640 struct file *file;
2641 int ret;
2642
Jens Axboef86cd202020-01-29 13:46:44 -07002643 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002644 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002645
Jens Axboecebdb982020-01-08 17:59:24 -07002646 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002647 if (ret)
2648 goto err;
2649
Jens Axboecebdb982020-01-08 17:59:24 -07002650 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002651 if (ret < 0)
2652 goto err;
2653
2654 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2655 if (IS_ERR(file)) {
2656 put_unused_fd(ret);
2657 ret = PTR_ERR(file);
2658 } else {
2659 fsnotify_open(file);
2660 fd_install(ret, file);
2661 }
2662err:
2663 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002664 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002665 if (ret < 0)
2666 req_set_fail_links(req);
2667 io_cqring_add_event(req, ret);
2668 io_put_req_find_next(req, nxt);
2669 return 0;
2670}
2671
Jens Axboecebdb982020-01-08 17:59:24 -07002672static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2673 bool force_nonblock)
2674{
2675 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2676 return io_openat2(req, nxt, force_nonblock);
2677}
2678
Jens Axboe3e4827b2020-01-08 15:18:09 -07002679static int io_epoll_ctl_prep(struct io_kiocb *req,
2680 const struct io_uring_sqe *sqe)
2681{
2682#if defined(CONFIG_EPOLL)
2683 if (sqe->ioprio || sqe->buf_index)
2684 return -EINVAL;
2685
2686 req->epoll.epfd = READ_ONCE(sqe->fd);
2687 req->epoll.op = READ_ONCE(sqe->len);
2688 req->epoll.fd = READ_ONCE(sqe->off);
2689
2690 if (ep_op_has_event(req->epoll.op)) {
2691 struct epoll_event __user *ev;
2692
2693 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2694 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2695 return -EFAULT;
2696 }
2697
2698 return 0;
2699#else
2700 return -EOPNOTSUPP;
2701#endif
2702}
2703
2704static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2705 bool force_nonblock)
2706{
2707#if defined(CONFIG_EPOLL)
2708 struct io_epoll *ie = &req->epoll;
2709 int ret;
2710
2711 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2712 if (force_nonblock && ret == -EAGAIN)
2713 return -EAGAIN;
2714
2715 if (ret < 0)
2716 req_set_fail_links(req);
2717 io_cqring_add_event(req, ret);
2718 io_put_req_find_next(req, nxt);
2719 return 0;
2720#else
2721 return -EOPNOTSUPP;
2722#endif
2723}
2724
Jens Axboec1ca7572019-12-25 22:18:28 -07002725static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2726{
2727#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2728 if (sqe->ioprio || sqe->buf_index || sqe->off)
2729 return -EINVAL;
2730
2731 req->madvise.addr = READ_ONCE(sqe->addr);
2732 req->madvise.len = READ_ONCE(sqe->len);
2733 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2734 return 0;
2735#else
2736 return -EOPNOTSUPP;
2737#endif
2738}
2739
2740static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2741 bool force_nonblock)
2742{
2743#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2744 struct io_madvise *ma = &req->madvise;
2745 int ret;
2746
2747 if (force_nonblock)
2748 return -EAGAIN;
2749
2750 ret = do_madvise(ma->addr, ma->len, ma->advice);
2751 if (ret < 0)
2752 req_set_fail_links(req);
2753 io_cqring_add_event(req, ret);
2754 io_put_req_find_next(req, nxt);
2755 return 0;
2756#else
2757 return -EOPNOTSUPP;
2758#endif
2759}
2760
Jens Axboe4840e412019-12-25 22:03:45 -07002761static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2762{
2763 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2764 return -EINVAL;
2765
2766 req->fadvise.offset = READ_ONCE(sqe->off);
2767 req->fadvise.len = READ_ONCE(sqe->len);
2768 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2769 return 0;
2770}
2771
2772static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2773 bool force_nonblock)
2774{
2775 struct io_fadvise *fa = &req->fadvise;
2776 int ret;
2777
Jens Axboe3e694262020-02-01 09:22:49 -07002778 if (force_nonblock) {
2779 switch (fa->advice) {
2780 case POSIX_FADV_NORMAL:
2781 case POSIX_FADV_RANDOM:
2782 case POSIX_FADV_SEQUENTIAL:
2783 break;
2784 default:
2785 return -EAGAIN;
2786 }
2787 }
Jens Axboe4840e412019-12-25 22:03:45 -07002788
2789 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2790 if (ret < 0)
2791 req_set_fail_links(req);
2792 io_cqring_add_event(req, ret);
2793 io_put_req_find_next(req, nxt);
2794 return 0;
2795}
2796
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002797static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2798{
Jens Axboef8748882020-01-08 17:47:02 -07002799 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002800 unsigned lookup_flags;
2801 int ret;
2802
2803 if (sqe->ioprio || sqe->buf_index)
2804 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002805 if (sqe->flags & IOSQE_FIXED_FILE)
2806 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002807 if (req->flags & REQ_F_NEED_CLEANUP)
2808 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002809
2810 req->open.dfd = READ_ONCE(sqe->fd);
2811 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002812 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002813 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002814 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002815
Jens Axboec12cedf2020-01-08 17:41:21 -07002816 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002817 return -EINVAL;
2818
Jens Axboef8748882020-01-08 17:47:02 -07002819 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002820 if (IS_ERR(req->open.filename)) {
2821 ret = PTR_ERR(req->open.filename);
2822 req->open.filename = NULL;
2823 return ret;
2824 }
2825
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002826 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002827 return 0;
2828}
2829
2830static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2831 bool force_nonblock)
2832{
2833 struct io_open *ctx = &req->open;
2834 unsigned lookup_flags;
2835 struct path path;
2836 struct kstat stat;
2837 int ret;
2838
2839 if (force_nonblock)
2840 return -EAGAIN;
2841
Jens Axboec12cedf2020-01-08 17:41:21 -07002842 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002843 return -EINVAL;
2844
2845retry:
2846 /* filename_lookup() drops it, keep a reference */
2847 ctx->filename->refcnt++;
2848
2849 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2850 NULL);
2851 if (ret)
2852 goto err;
2853
Jens Axboec12cedf2020-01-08 17:41:21 -07002854 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002855 path_put(&path);
2856 if (retry_estale(ret, lookup_flags)) {
2857 lookup_flags |= LOOKUP_REVAL;
2858 goto retry;
2859 }
2860 if (!ret)
2861 ret = cp_statx(&stat, ctx->buffer);
2862err:
2863 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002864 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002865 if (ret < 0)
2866 req_set_fail_links(req);
2867 io_cqring_add_event(req, ret);
2868 io_put_req_find_next(req, nxt);
2869 return 0;
2870}
2871
Jens Axboeb5dba592019-12-11 14:02:38 -07002872static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2873{
2874 /*
2875 * If we queue this for async, it must not be cancellable. That would
2876 * leave the 'file' in an undeterminate state.
2877 */
2878 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2879
2880 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2881 sqe->rw_flags || sqe->buf_index)
2882 return -EINVAL;
2883 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002884 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002885
2886 req->close.fd = READ_ONCE(sqe->fd);
2887 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002888 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002889 return -EBADF;
2890
2891 return 0;
2892}
2893
Pavel Begunkova93b3332020-02-08 14:04:34 +03002894/* only called when __close_fd_get_file() is done */
2895static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt)
2896{
2897 int ret;
2898
2899 ret = filp_close(req->close.put_file, req->work.files);
2900 if (ret < 0)
2901 req_set_fail_links(req);
2902 io_cqring_add_event(req, ret);
2903 fput(req->close.put_file);
2904 io_put_req_find_next(req, nxt);
2905}
2906
Jens Axboeb5dba592019-12-11 14:02:38 -07002907static void io_close_finish(struct io_wq_work **workptr)
2908{
2909 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2910 struct io_kiocb *nxt = NULL;
2911
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002912 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002913 __io_close_finish(req, &nxt);
Jens Axboeb5dba592019-12-11 14:02:38 -07002914 if (nxt)
2915 io_wq_assign_next(workptr, nxt);
2916}
2917
2918static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2919 bool force_nonblock)
2920{
2921 int ret;
2922
2923 req->close.put_file = NULL;
2924 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2925 if (ret < 0)
2926 return ret;
2927
2928 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002929 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002930 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002931
2932 /*
2933 * No ->flush(), safely close from here and just punt the
2934 * fput() to async context.
2935 */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002936 __io_close_finish(req, nxt);
2937 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002938eagain:
2939 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07002940 /*
2941 * Do manual async queue here to avoid grabbing files - we don't
2942 * need the files, and it'll cause io_close_finish() to close
2943 * the file again and cause a double CQE entry for this request
2944 */
2945 io_queue_async_work(req);
2946 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002947}
2948
Jens Axboe3529d8c2019-12-19 18:24:38 -07002949static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002950{
2951 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002952
2953 if (!req->file)
2954 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002955
2956 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2957 return -EINVAL;
2958 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2959 return -EINVAL;
2960
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002961 req->sync.off = READ_ONCE(sqe->off);
2962 req->sync.len = READ_ONCE(sqe->len);
2963 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002964 return 0;
2965}
2966
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002967static void __io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002968{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002969 int ret;
2970
Jens Axboe9adbd452019-12-20 08:45:55 -07002971 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002972 req->sync.flags);
2973 if (ret < 0)
2974 req_set_fail_links(req);
2975 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002976 io_put_req_find_next(req, nxt);
2977}
2978
2979
2980static void io_sync_file_range_finish(struct io_wq_work **workptr)
2981{
2982 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2983 struct io_kiocb *nxt = NULL;
2984
2985 if (io_req_cancelled(req))
2986 return;
2987 __io_sync_file_range(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002988 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002989 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002990}
2991
Jens Axboefc4df992019-12-10 14:38:45 -07002992static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002993 bool force_nonblock)
2994{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002995 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002996 if (force_nonblock) {
2997 io_put_req(req);
2998 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002999 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003000 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003001
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003002 __io_sync_file_range(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003003 return 0;
3004}
3005
Jens Axboe3529d8c2019-12-19 18:24:38 -07003006static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003007{
Jens Axboe03b12302019-12-02 18:50:25 -07003008#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003009 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003010 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003011 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003012
Jens Axboee47293f2019-12-20 08:58:21 -07003013 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3014 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003015 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003016
Jens Axboed8768362020-02-27 14:17:49 -07003017#ifdef CONFIG_COMPAT
3018 if (req->ctx->compat)
3019 sr->msg_flags |= MSG_CMSG_COMPAT;
3020#endif
3021
Jens Axboefddafac2020-01-04 20:19:44 -07003022 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003023 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003024 /* iovec is already imported */
3025 if (req->flags & REQ_F_NEED_CLEANUP)
3026 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003027
Jens Axboed9688562019-12-09 19:35:20 -07003028 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003029 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003030 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003031 if (!ret)
3032 req->flags |= REQ_F_NEED_CLEANUP;
3033 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003034#else
Jens Axboee47293f2019-12-20 08:58:21 -07003035 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003036#endif
3037}
3038
Jens Axboefc4df992019-12-10 14:38:45 -07003039static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3040 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003041{
3042#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003043 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003044 struct socket *sock;
3045 int ret;
3046
3047 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3048 return -EINVAL;
3049
3050 sock = sock_from_file(req->file, &ret);
3051 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003052 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003053 unsigned flags;
3054
Jens Axboe03b12302019-12-02 18:50:25 -07003055 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003056 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003057 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003058 /* if iov is set, it's allocated already */
3059 if (!kmsg->iov)
3060 kmsg->iov = kmsg->fast_iov;
3061 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003062 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003063 struct io_sr_msg *sr = &req->sr_msg;
3064
Jens Axboe0b416c32019-12-15 10:57:46 -07003065 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003066 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003067
3068 io.msg.iov = io.msg.fast_iov;
3069 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3070 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003071 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003072 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003073 }
3074
Jens Axboee47293f2019-12-20 08:58:21 -07003075 flags = req->sr_msg.msg_flags;
3076 if (flags & MSG_DONTWAIT)
3077 req->flags |= REQ_F_NOWAIT;
3078 else if (force_nonblock)
3079 flags |= MSG_DONTWAIT;
3080
Jens Axboe0b416c32019-12-15 10:57:46 -07003081 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003082 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003083 if (req->io)
3084 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003085 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003086 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003087 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003088 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003089 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003090 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003091 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003092 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003093 }
3094 if (ret == -ERESTARTSYS)
3095 ret = -EINTR;
3096 }
3097
Pavel Begunkov1e950812020-02-06 19:51:16 +03003098 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003099 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003100 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003101 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003102 if (ret < 0)
3103 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003104 io_put_req_find_next(req, nxt);
3105 return 0;
3106#else
3107 return -EOPNOTSUPP;
3108#endif
3109}
3110
Jens Axboefddafac2020-01-04 20:19:44 -07003111static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3112 bool force_nonblock)
3113{
3114#if defined(CONFIG_NET)
3115 struct socket *sock;
3116 int ret;
3117
3118 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3119 return -EINVAL;
3120
3121 sock = sock_from_file(req->file, &ret);
3122 if (sock) {
3123 struct io_sr_msg *sr = &req->sr_msg;
3124 struct msghdr msg;
3125 struct iovec iov;
3126 unsigned flags;
3127
3128 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3129 &msg.msg_iter);
3130 if (ret)
3131 return ret;
3132
3133 msg.msg_name = NULL;
3134 msg.msg_control = NULL;
3135 msg.msg_controllen = 0;
3136 msg.msg_namelen = 0;
3137
3138 flags = req->sr_msg.msg_flags;
3139 if (flags & MSG_DONTWAIT)
3140 req->flags |= REQ_F_NOWAIT;
3141 else if (force_nonblock)
3142 flags |= MSG_DONTWAIT;
3143
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003144 msg.msg_flags = flags;
3145 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003146 if (force_nonblock && ret == -EAGAIN)
3147 return -EAGAIN;
3148 if (ret == -ERESTARTSYS)
3149 ret = -EINTR;
3150 }
3151
3152 io_cqring_add_event(req, ret);
3153 if (ret < 0)
3154 req_set_fail_links(req);
3155 io_put_req_find_next(req, nxt);
3156 return 0;
3157#else
3158 return -EOPNOTSUPP;
3159#endif
3160}
3161
Jens Axboe3529d8c2019-12-19 18:24:38 -07003162static int io_recvmsg_prep(struct io_kiocb *req,
3163 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003164{
3165#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003166 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003167 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003168 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003169
Jens Axboe3529d8c2019-12-19 18:24:38 -07003170 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3171 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003172 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003173
Jens Axboed8768362020-02-27 14:17:49 -07003174#ifdef CONFIG_COMPAT
3175 if (req->ctx->compat)
3176 sr->msg_flags |= MSG_CMSG_COMPAT;
3177#endif
3178
Jens Axboefddafac2020-01-04 20:19:44 -07003179 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003180 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003181 /* iovec is already imported */
3182 if (req->flags & REQ_F_NEED_CLEANUP)
3183 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003184
Jens Axboed9688562019-12-09 19:35:20 -07003185 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003186 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003187 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003188 if (!ret)
3189 req->flags |= REQ_F_NEED_CLEANUP;
3190 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003191#else
Jens Axboee47293f2019-12-20 08:58:21 -07003192 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003193#endif
3194}
3195
Jens Axboefc4df992019-12-10 14:38:45 -07003196static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3197 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003198{
3199#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003200 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003201 struct socket *sock;
3202 int ret;
3203
3204 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3205 return -EINVAL;
3206
3207 sock = sock_from_file(req->file, &ret);
3208 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003209 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003210 unsigned flags;
3211
Jens Axboe03b12302019-12-02 18:50:25 -07003212 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003213 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003214 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003215 /* if iov is set, it's allocated already */
3216 if (!kmsg->iov)
3217 kmsg->iov = kmsg->fast_iov;
3218 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003219 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003220 struct io_sr_msg *sr = &req->sr_msg;
3221
Jens Axboe0b416c32019-12-15 10:57:46 -07003222 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003223 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003224
3225 io.msg.iov = io.msg.fast_iov;
3226 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3227 sr->msg_flags, &io.msg.uaddr,
3228 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003229 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003230 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003231 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003232
Jens Axboee47293f2019-12-20 08:58:21 -07003233 flags = req->sr_msg.msg_flags;
3234 if (flags & MSG_DONTWAIT)
3235 req->flags |= REQ_F_NOWAIT;
3236 else if (force_nonblock)
3237 flags |= MSG_DONTWAIT;
3238
3239 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3240 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003241 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003242 if (req->io)
3243 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003244 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003245 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003246 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003247 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003248 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003249 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003250 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe0b416c32019-12-15 10:57:46 -07003251 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003252 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003253 if (ret == -ERESTARTSYS)
3254 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003255 }
3256
Pavel Begunkov1e950812020-02-06 19:51:16 +03003257 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003258 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003259 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003260 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003261 if (ret < 0)
3262 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003263 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003264 return 0;
3265#else
3266 return -EOPNOTSUPP;
3267#endif
3268}
3269
Jens Axboefddafac2020-01-04 20:19:44 -07003270static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3271 bool force_nonblock)
3272{
3273#if defined(CONFIG_NET)
3274 struct socket *sock;
3275 int ret;
3276
3277 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3278 return -EINVAL;
3279
3280 sock = sock_from_file(req->file, &ret);
3281 if (sock) {
3282 struct io_sr_msg *sr = &req->sr_msg;
3283 struct msghdr msg;
3284 struct iovec iov;
3285 unsigned flags;
3286
3287 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3288 &msg.msg_iter);
3289 if (ret)
3290 return ret;
3291
3292 msg.msg_name = NULL;
3293 msg.msg_control = NULL;
3294 msg.msg_controllen = 0;
3295 msg.msg_namelen = 0;
3296 msg.msg_iocb = NULL;
3297 msg.msg_flags = 0;
3298
3299 flags = req->sr_msg.msg_flags;
3300 if (flags & MSG_DONTWAIT)
3301 req->flags |= REQ_F_NOWAIT;
3302 else if (force_nonblock)
3303 flags |= MSG_DONTWAIT;
3304
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003305 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003306 if (force_nonblock && ret == -EAGAIN)
3307 return -EAGAIN;
3308 if (ret == -ERESTARTSYS)
3309 ret = -EINTR;
3310 }
3311
3312 io_cqring_add_event(req, ret);
3313 if (ret < 0)
3314 req_set_fail_links(req);
3315 io_put_req_find_next(req, nxt);
3316 return 0;
3317#else
3318 return -EOPNOTSUPP;
3319#endif
3320}
3321
3322
Jens Axboe3529d8c2019-12-19 18:24:38 -07003323static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003324{
3325#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003326 struct io_accept *accept = &req->accept;
3327
Jens Axboe17f2fe32019-10-17 14:42:58 -06003328 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3329 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003330 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003331 return -EINVAL;
3332
Jens Axboed55e5f52019-12-11 16:12:15 -07003333 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3334 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003335 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003336 return 0;
3337#else
3338 return -EOPNOTSUPP;
3339#endif
3340}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003341
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003342#if defined(CONFIG_NET)
3343static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3344 bool force_nonblock)
3345{
3346 struct io_accept *accept = &req->accept;
3347 unsigned file_flags;
3348 int ret;
3349
3350 file_flags = force_nonblock ? O_NONBLOCK : 0;
3351 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3352 accept->addr_len, accept->flags);
3353 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003354 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003355 if (ret == -ERESTARTSYS)
3356 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003357 if (ret < 0)
3358 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003359 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003360 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003361 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003362}
3363
3364static void io_accept_finish(struct io_wq_work **workptr)
3365{
3366 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3367 struct io_kiocb *nxt = NULL;
3368
Jens Axboee441d1c2020-02-20 09:59:02 -07003369 io_put_req(req);
3370
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003371 if (io_req_cancelled(req))
3372 return;
3373 __io_accept(req, &nxt, false);
3374 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003375 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003376}
3377#endif
3378
3379static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3380 bool force_nonblock)
3381{
3382#if defined(CONFIG_NET)
3383 int ret;
3384
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003385 ret = __io_accept(req, nxt, force_nonblock);
3386 if (ret == -EAGAIN && force_nonblock) {
3387 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003388 return -EAGAIN;
3389 }
3390 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003391#else
3392 return -EOPNOTSUPP;
3393#endif
3394}
3395
Jens Axboe3529d8c2019-12-19 18:24:38 -07003396static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003397{
3398#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003399 struct io_connect *conn = &req->connect;
3400 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003401
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003402 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3403 return -EINVAL;
3404 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3405 return -EINVAL;
3406
Jens Axboe3529d8c2019-12-19 18:24:38 -07003407 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3408 conn->addr_len = READ_ONCE(sqe->addr2);
3409
3410 if (!io)
3411 return 0;
3412
3413 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003414 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003415#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003416 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003417#endif
3418}
3419
Jens Axboefc4df992019-12-10 14:38:45 -07003420static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3421 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003422{
3423#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003424 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003425 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003426 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003427
Jens Axboef499a022019-12-02 16:28:46 -07003428 if (req->io) {
3429 io = req->io;
3430 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003431 ret = move_addr_to_kernel(req->connect.addr,
3432 req->connect.addr_len,
3433 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003434 if (ret)
3435 goto out;
3436 io = &__io;
3437 }
3438
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003439 file_flags = force_nonblock ? O_NONBLOCK : 0;
3440
3441 ret = __sys_connect_file(req->file, &io->connect.address,
3442 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003443 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003444 if (req->io)
3445 return -EAGAIN;
3446 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003447 ret = -ENOMEM;
3448 goto out;
3449 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003450 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003451 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003452 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003453 if (ret == -ERESTARTSYS)
3454 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003455out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003456 if (ret < 0)
3457 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003458 io_cqring_add_event(req, ret);
3459 io_put_req_find_next(req, nxt);
3460 return 0;
3461#else
3462 return -EOPNOTSUPP;
3463#endif
3464}
3465
Jens Axboe221c5eb2019-01-17 09:41:58 -07003466static void io_poll_remove_one(struct io_kiocb *req)
3467{
3468 struct io_poll_iocb *poll = &req->poll;
3469
3470 spin_lock(&poll->head->lock);
3471 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003472 if (!list_empty(&poll->wait.entry)) {
3473 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003474 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003475 }
3476 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003477 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003478}
3479
3480static void io_poll_remove_all(struct io_ring_ctx *ctx)
3481{
Jens Axboe78076bb2019-12-04 19:56:40 -07003482 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003483 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003484 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003485
3486 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003487 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3488 struct hlist_head *list;
3489
3490 list = &ctx->cancel_hash[i];
3491 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3492 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003493 }
3494 spin_unlock_irq(&ctx->completion_lock);
3495}
3496
Jens Axboe47f46762019-11-09 17:43:02 -07003497static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3498{
Jens Axboe78076bb2019-12-04 19:56:40 -07003499 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003500 struct io_kiocb *req;
3501
Jens Axboe78076bb2019-12-04 19:56:40 -07003502 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3503 hlist_for_each_entry(req, list, hash_node) {
3504 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003505 io_poll_remove_one(req);
3506 return 0;
3507 }
Jens Axboe47f46762019-11-09 17:43:02 -07003508 }
3509
3510 return -ENOENT;
3511}
3512
Jens Axboe3529d8c2019-12-19 18:24:38 -07003513static int io_poll_remove_prep(struct io_kiocb *req,
3514 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003515{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003516 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3517 return -EINVAL;
3518 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3519 sqe->poll_events)
3520 return -EINVAL;
3521
Jens Axboe0969e782019-12-17 18:40:57 -07003522 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003523 return 0;
3524}
3525
3526/*
3527 * Find a running poll command that matches one specified in sqe->addr,
3528 * and remove it if found.
3529 */
3530static int io_poll_remove(struct io_kiocb *req)
3531{
3532 struct io_ring_ctx *ctx = req->ctx;
3533 u64 addr;
3534 int ret;
3535
Jens Axboe0969e782019-12-17 18:40:57 -07003536 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003537 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003538 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003539 spin_unlock_irq(&ctx->completion_lock);
3540
Jens Axboe78e19bb2019-11-06 15:21:34 -07003541 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003542 if (ret < 0)
3543 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003544 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003545 return 0;
3546}
3547
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003548static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003549{
Jackie Liua197f662019-11-08 08:09:12 -07003550 struct io_ring_ctx *ctx = req->ctx;
3551
Jens Axboe8c838782019-03-12 15:48:16 -06003552 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003553 if (error)
3554 io_cqring_fill_event(req, error);
3555 else
3556 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003557 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003558}
3559
Jens Axboe561fb042019-10-24 07:25:42 -06003560static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003561{
Jens Axboe561fb042019-10-24 07:25:42 -06003562 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003563 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3564 struct io_poll_iocb *poll = &req->poll;
3565 struct poll_table_struct pt = { ._key = poll->events };
3566 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003567 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003568 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003569 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003570
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003571 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003572 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003573 ret = -ECANCELED;
3574 } else if (READ_ONCE(poll->canceled)) {
3575 ret = -ECANCELED;
3576 }
Jens Axboe561fb042019-10-24 07:25:42 -06003577
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003578 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003579 mask = vfs_poll(poll->file, &pt) & poll->events;
3580
3581 /*
3582 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3583 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3584 * synchronize with them. In the cancellation case the list_del_init
3585 * itself is not actually needed, but harmless so we keep it in to
3586 * avoid further branches in the fast path.
3587 */
3588 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003589 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003590 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003591 spin_unlock_irq(&ctx->completion_lock);
3592 return;
3593 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003594 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003595 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003596 spin_unlock_irq(&ctx->completion_lock);
3597
Jens Axboe8c838782019-03-12 15:48:16 -06003598 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003599
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003600 if (ret < 0)
3601 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003602 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003603 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003604 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003605}
3606
Jens Axboee94f1412019-12-19 12:06:02 -07003607static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3608{
Jens Axboee94f1412019-12-19 12:06:02 -07003609 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003610 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003611
Jens Axboec6ca97b302019-12-28 12:11:08 -07003612 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003613 spin_lock_irq(&ctx->completion_lock);
3614 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3615 hash_del(&req->hash_node);
3616 io_poll_complete(req, req->result, 0);
3617
Jens Axboe8237e042019-12-28 10:48:22 -07003618 if (refcount_dec_and_test(&req->refs) &&
3619 !io_req_multi_free(&rb, req)) {
3620 req->flags |= REQ_F_COMP_LOCKED;
3621 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003622 }
3623 }
3624 spin_unlock_irq(&ctx->completion_lock);
3625
3626 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003627 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003628}
3629
3630static void io_poll_flush(struct io_wq_work **workptr)
3631{
3632 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3633 struct llist_node *nodes;
3634
3635 nodes = llist_del_all(&req->ctx->poll_llist);
3636 if (nodes)
3637 __io_poll_flush(req->ctx, nodes);
3638}
3639
Jens Axboef0b493e2020-02-01 21:30:11 -07003640static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3641{
3642 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3643
3644 eventfd_signal(req->ctx->cq_ev_fd, 1);
3645 io_put_req(req);
3646}
3647
Jens Axboe221c5eb2019-01-17 09:41:58 -07003648static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3649 void *key)
3650{
Jens Axboee9444752019-11-26 15:02:04 -07003651 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003652 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3653 struct io_ring_ctx *ctx = req->ctx;
3654 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003655
3656 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003657 if (mask && !(mask & poll->events))
3658 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003659
Jens Axboe392edb42019-12-09 17:52:20 -07003660 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003661
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003662 /*
3663 * Run completion inline if we can. We're using trylock here because
3664 * we are violating the completion_lock -> poll wq lock ordering.
3665 * If we have a link timeout we're going to need the completion_lock
3666 * for finalizing the request, mark us as having grabbed that already.
3667 */
Jens Axboee94f1412019-12-19 12:06:02 -07003668 if (mask) {
3669 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003670
Jens Axboee94f1412019-12-19 12:06:02 -07003671 if (llist_empty(&ctx->poll_llist) &&
3672 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003673 bool trigger_ev;
3674
Jens Axboee94f1412019-12-19 12:06:02 -07003675 hash_del(&req->hash_node);
3676 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003677
Jens Axboef0b493e2020-02-01 21:30:11 -07003678 trigger_ev = io_should_trigger_evfd(ctx);
3679 if (trigger_ev && eventfd_signal_count()) {
3680 trigger_ev = false;
3681 req->work.func = io_poll_trigger_evfd;
3682 } else {
3683 req->flags |= REQ_F_COMP_LOCKED;
3684 io_put_req(req);
3685 req = NULL;
3686 }
3687 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3688 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003689 } else {
3690 req->result = mask;
3691 req->llist_node.next = NULL;
3692 /* if the list wasn't empty, we're done */
3693 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3694 req = NULL;
3695 else
3696 req->work.func = io_poll_flush;
3697 }
Jens Axboe8c838782019-03-12 15:48:16 -06003698 }
Jens Axboee94f1412019-12-19 12:06:02 -07003699 if (req)
3700 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003701
Jens Axboe221c5eb2019-01-17 09:41:58 -07003702 return 1;
3703}
3704
3705struct io_poll_table {
3706 struct poll_table_struct pt;
3707 struct io_kiocb *req;
3708 int error;
3709};
3710
3711static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3712 struct poll_table_struct *p)
3713{
3714 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3715
3716 if (unlikely(pt->req->poll.head)) {
3717 pt->error = -EINVAL;
3718 return;
3719 }
3720
3721 pt->error = 0;
3722 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003723 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003724}
3725
Jens Axboeeac406c2019-11-14 12:09:58 -07003726static void io_poll_req_insert(struct io_kiocb *req)
3727{
3728 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003729 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003730
Jens Axboe78076bb2019-12-04 19:56:40 -07003731 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3732 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003733}
3734
Jens Axboe3529d8c2019-12-19 18:24:38 -07003735static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003736{
3737 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003738 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003739
3740 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3741 return -EINVAL;
3742 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3743 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003744 if (!poll->file)
3745 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003746
Jens Axboe221c5eb2019-01-17 09:41:58 -07003747 events = READ_ONCE(sqe->poll_events);
3748 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003749 return 0;
3750}
3751
3752static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3753{
3754 struct io_poll_iocb *poll = &req->poll;
3755 struct io_ring_ctx *ctx = req->ctx;
3756 struct io_poll_table ipt;
3757 bool cancel = false;
3758 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003759
3760 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003761 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003762
Jens Axboe221c5eb2019-01-17 09:41:58 -07003763 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003764 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003765 poll->canceled = false;
3766
3767 ipt.pt._qproc = io_poll_queue_proc;
3768 ipt.pt._key = poll->events;
3769 ipt.req = req;
3770 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3771
3772 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003773 INIT_LIST_HEAD(&poll->wait.entry);
3774 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3775 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003776
Jens Axboe36703242019-07-25 10:20:18 -06003777 INIT_LIST_HEAD(&req->list);
3778
Jens Axboe221c5eb2019-01-17 09:41:58 -07003779 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003780
3781 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003782 if (likely(poll->head)) {
3783 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003784 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003785 if (ipt.error)
3786 cancel = true;
3787 ipt.error = 0;
3788 mask = 0;
3789 }
3790 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003791 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003792 else if (cancel)
3793 WRITE_ONCE(poll->canceled, true);
3794 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003795 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003796 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003797 }
Jens Axboe8c838782019-03-12 15:48:16 -06003798 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003799 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003800 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003801 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003802 spin_unlock_irq(&ctx->completion_lock);
3803
Jens Axboe8c838782019-03-12 15:48:16 -06003804 if (mask) {
3805 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003806 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003807 }
Jens Axboe8c838782019-03-12 15:48:16 -06003808 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003809}
3810
Jens Axboe5262f562019-09-17 12:26:57 -06003811static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3812{
Jens Axboead8a48a2019-11-15 08:49:11 -07003813 struct io_timeout_data *data = container_of(timer,
3814 struct io_timeout_data, timer);
3815 struct io_kiocb *req = data->req;
3816 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003817 unsigned long flags;
3818
Jens Axboe5262f562019-09-17 12:26:57 -06003819 atomic_inc(&ctx->cq_timeouts);
3820
3821 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003822 /*
Jens Axboe11365042019-10-16 09:08:32 -06003823 * We could be racing with timeout deletion. If the list is empty,
3824 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003825 */
Jens Axboe842f9612019-10-29 12:34:10 -06003826 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003827 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003828
Jens Axboe11365042019-10-16 09:08:32 -06003829 /*
3830 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003831 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003832 * pointer will be increased, otherwise other timeout reqs may
3833 * return in advance without waiting for enough wait_nr.
3834 */
3835 prev = req;
3836 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3837 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003838 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003839 }
Jens Axboe842f9612019-10-29 12:34:10 -06003840
Jens Axboe78e19bb2019-11-06 15:21:34 -07003841 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003842 io_commit_cqring(ctx);
3843 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3844
3845 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003846 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003847 io_put_req(req);
3848 return HRTIMER_NORESTART;
3849}
3850
Jens Axboe47f46762019-11-09 17:43:02 -07003851static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3852{
3853 struct io_kiocb *req;
3854 int ret = -ENOENT;
3855
3856 list_for_each_entry(req, &ctx->timeout_list, list) {
3857 if (user_data == req->user_data) {
3858 list_del_init(&req->list);
3859 ret = 0;
3860 break;
3861 }
3862 }
3863
3864 if (ret == -ENOENT)
3865 return ret;
3866
Jens Axboe2d283902019-12-04 11:08:05 -07003867 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003868 if (ret == -1)
3869 return -EALREADY;
3870
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003871 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003872 io_cqring_fill_event(req, -ECANCELED);
3873 io_put_req(req);
3874 return 0;
3875}
3876
Jens Axboe3529d8c2019-12-19 18:24:38 -07003877static int io_timeout_remove_prep(struct io_kiocb *req,
3878 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003879{
Jens Axboeb29472e2019-12-17 18:50:29 -07003880 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3881 return -EINVAL;
3882 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3883 return -EINVAL;
3884
3885 req->timeout.addr = READ_ONCE(sqe->addr);
3886 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3887 if (req->timeout.flags)
3888 return -EINVAL;
3889
Jens Axboeb29472e2019-12-17 18:50:29 -07003890 return 0;
3891}
3892
Jens Axboe11365042019-10-16 09:08:32 -06003893/*
3894 * Remove or update an existing timeout command
3895 */
Jens Axboefc4df992019-12-10 14:38:45 -07003896static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003897{
3898 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003899 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003900
Jens Axboe11365042019-10-16 09:08:32 -06003901 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003902 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003903
Jens Axboe47f46762019-11-09 17:43:02 -07003904 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003905 io_commit_cqring(ctx);
3906 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003907 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003908 if (ret < 0)
3909 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003910 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003911 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003912}
3913
Jens Axboe3529d8c2019-12-19 18:24:38 -07003914static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003915 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003916{
Jens Axboead8a48a2019-11-15 08:49:11 -07003917 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003918 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003919
Jens Axboead8a48a2019-11-15 08:49:11 -07003920 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003921 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003922 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003923 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003924 if (sqe->off && is_timeout_link)
3925 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003926 flags = READ_ONCE(sqe->timeout_flags);
3927 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003928 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003929
Jens Axboe26a61672019-12-20 09:02:01 -07003930 req->timeout.count = READ_ONCE(sqe->off);
3931
Jens Axboe3529d8c2019-12-19 18:24:38 -07003932 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003933 return -ENOMEM;
3934
3935 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003936 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003937 req->flags |= REQ_F_TIMEOUT;
3938
3939 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003940 return -EFAULT;
3941
Jens Axboe11365042019-10-16 09:08:32 -06003942 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003943 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003944 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003945 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003946
Jens Axboead8a48a2019-11-15 08:49:11 -07003947 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3948 return 0;
3949}
3950
Jens Axboefc4df992019-12-10 14:38:45 -07003951static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003952{
3953 unsigned count;
3954 struct io_ring_ctx *ctx = req->ctx;
3955 struct io_timeout_data *data;
3956 struct list_head *entry;
3957 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003958
Jens Axboe2d283902019-12-04 11:08:05 -07003959 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003960
Jens Axboe5262f562019-09-17 12:26:57 -06003961 /*
3962 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003963 * timeout event to be satisfied. If it isn't set, then this is
3964 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003965 */
Jens Axboe26a61672019-12-20 09:02:01 -07003966 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003967 if (!count) {
3968 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3969 spin_lock_irq(&ctx->completion_lock);
3970 entry = ctx->timeout_list.prev;
3971 goto add;
3972 }
Jens Axboe5262f562019-09-17 12:26:57 -06003973
3974 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003975 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003976
3977 /*
3978 * Insertion sort, ensuring the first entry in the list is always
3979 * the one we need first.
3980 */
Jens Axboe5262f562019-09-17 12:26:57 -06003981 spin_lock_irq(&ctx->completion_lock);
3982 list_for_each_prev(entry, &ctx->timeout_list) {
3983 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003984 unsigned nxt_sq_head;
3985 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003986 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003987
Jens Axboe93bd25b2019-11-11 23:34:31 -07003988 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3989 continue;
3990
yangerkun5da0fb12019-10-15 21:59:29 +08003991 /*
3992 * Since cached_sq_head + count - 1 can overflow, use type long
3993 * long to store it.
3994 */
3995 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003996 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3997 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003998
3999 /*
4000 * cached_sq_head may overflow, and it will never overflow twice
4001 * once there is some timeout req still be valid.
4002 */
4003 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004004 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004005
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004006 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004007 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004008
4009 /*
4010 * Sequence of reqs after the insert one and itself should
4011 * be adjusted because each timeout req consumes a slot.
4012 */
4013 span++;
4014 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004015 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004016 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004017add:
Jens Axboe5262f562019-09-17 12:26:57 -06004018 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004019 data->timer.function = io_timeout_fn;
4020 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004021 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004022 return 0;
4023}
4024
Jens Axboe62755e32019-10-28 21:49:21 -06004025static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004026{
Jens Axboe62755e32019-10-28 21:49:21 -06004027 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004028
Jens Axboe62755e32019-10-28 21:49:21 -06004029 return req->user_data == (unsigned long) data;
4030}
4031
Jens Axboee977d6d2019-11-05 12:39:45 -07004032static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004033{
Jens Axboe62755e32019-10-28 21:49:21 -06004034 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004035 int ret = 0;
4036
Jens Axboe62755e32019-10-28 21:49:21 -06004037 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4038 switch (cancel_ret) {
4039 case IO_WQ_CANCEL_OK:
4040 ret = 0;
4041 break;
4042 case IO_WQ_CANCEL_RUNNING:
4043 ret = -EALREADY;
4044 break;
4045 case IO_WQ_CANCEL_NOTFOUND:
4046 ret = -ENOENT;
4047 break;
4048 }
4049
Jens Axboee977d6d2019-11-05 12:39:45 -07004050 return ret;
4051}
4052
Jens Axboe47f46762019-11-09 17:43:02 -07004053static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4054 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004055 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004056{
4057 unsigned long flags;
4058 int ret;
4059
4060 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4061 if (ret != -ENOENT) {
4062 spin_lock_irqsave(&ctx->completion_lock, flags);
4063 goto done;
4064 }
4065
4066 spin_lock_irqsave(&ctx->completion_lock, flags);
4067 ret = io_timeout_cancel(ctx, sqe_addr);
4068 if (ret != -ENOENT)
4069 goto done;
4070 ret = io_poll_cancel(ctx, sqe_addr);
4071done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004072 if (!ret)
4073 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004074 io_cqring_fill_event(req, ret);
4075 io_commit_cqring(ctx);
4076 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4077 io_cqring_ev_posted(ctx);
4078
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004079 if (ret < 0)
4080 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004081 io_put_req_find_next(req, nxt);
4082}
4083
Jens Axboe3529d8c2019-12-19 18:24:38 -07004084static int io_async_cancel_prep(struct io_kiocb *req,
4085 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004086{
Jens Axboefbf23842019-12-17 18:45:56 -07004087 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004088 return -EINVAL;
4089 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4090 sqe->cancel_flags)
4091 return -EINVAL;
4092
Jens Axboefbf23842019-12-17 18:45:56 -07004093 req->cancel.addr = READ_ONCE(sqe->addr);
4094 return 0;
4095}
4096
4097static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4098{
4099 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004100
4101 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004102 return 0;
4103}
4104
Jens Axboe05f3fb32019-12-09 11:22:50 -07004105static int io_files_update_prep(struct io_kiocb *req,
4106 const struct io_uring_sqe *sqe)
4107{
4108 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4109 return -EINVAL;
4110
4111 req->files_update.offset = READ_ONCE(sqe->off);
4112 req->files_update.nr_args = READ_ONCE(sqe->len);
4113 if (!req->files_update.nr_args)
4114 return -EINVAL;
4115 req->files_update.arg = READ_ONCE(sqe->addr);
4116 return 0;
4117}
4118
4119static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4120{
4121 struct io_ring_ctx *ctx = req->ctx;
4122 struct io_uring_files_update up;
4123 int ret;
4124
Jens Axboef86cd202020-01-29 13:46:44 -07004125 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004126 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004127
4128 up.offset = req->files_update.offset;
4129 up.fds = req->files_update.arg;
4130
4131 mutex_lock(&ctx->uring_lock);
4132 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4133 mutex_unlock(&ctx->uring_lock);
4134
4135 if (ret < 0)
4136 req_set_fail_links(req);
4137 io_cqring_add_event(req, ret);
4138 io_put_req(req);
4139 return 0;
4140}
4141
Jens Axboe3529d8c2019-12-19 18:24:38 -07004142static int io_req_defer_prep(struct io_kiocb *req,
4143 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004144{
Jens Axboee7815732019-12-17 19:45:06 -07004145 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004146
Jens Axboef86cd202020-01-29 13:46:44 -07004147 if (io_op_defs[req->opcode].file_table) {
4148 ret = io_grab_files(req);
4149 if (unlikely(ret))
4150 return ret;
4151 }
4152
Jens Axboecccf0ee2020-01-27 16:34:48 -07004153 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4154
Jens Axboed625c6e2019-12-17 19:53:05 -07004155 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004156 case IORING_OP_NOP:
4157 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004158 case IORING_OP_READV:
4159 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004160 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004161 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004162 break;
4163 case IORING_OP_WRITEV:
4164 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004165 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004166 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004167 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004168 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004169 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004170 break;
4171 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004172 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004173 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004174 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004175 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004176 break;
4177 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004178 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004179 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004180 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004181 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004182 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004183 break;
4184 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004185 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004186 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004187 break;
Jens Axboef499a022019-12-02 16:28:46 -07004188 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004189 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004190 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004191 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004192 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004193 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004194 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004195 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004196 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004197 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004198 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004199 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004200 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004201 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004202 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004203 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004204 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004205 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004206 case IORING_OP_FALLOCATE:
4207 ret = io_fallocate_prep(req, sqe);
4208 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004209 case IORING_OP_OPENAT:
4210 ret = io_openat_prep(req, sqe);
4211 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004212 case IORING_OP_CLOSE:
4213 ret = io_close_prep(req, sqe);
4214 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004215 case IORING_OP_FILES_UPDATE:
4216 ret = io_files_update_prep(req, sqe);
4217 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004218 case IORING_OP_STATX:
4219 ret = io_statx_prep(req, sqe);
4220 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004221 case IORING_OP_FADVISE:
4222 ret = io_fadvise_prep(req, sqe);
4223 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004224 case IORING_OP_MADVISE:
4225 ret = io_madvise_prep(req, sqe);
4226 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004227 case IORING_OP_OPENAT2:
4228 ret = io_openat2_prep(req, sqe);
4229 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004230 case IORING_OP_EPOLL_CTL:
4231 ret = io_epoll_ctl_prep(req, sqe);
4232 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004233 default:
Jens Axboee7815732019-12-17 19:45:06 -07004234 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4235 req->opcode);
4236 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004237 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004238 }
4239
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004240 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004241}
4242
Jens Axboe3529d8c2019-12-19 18:24:38 -07004243static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004244{
Jackie Liua197f662019-11-08 08:09:12 -07004245 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004246 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004247
Bob Liu9d858b22019-11-13 18:06:25 +08004248 /* Still need defer if there is pending req in defer list. */
4249 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004250 return 0;
4251
Jens Axboe3529d8c2019-12-19 18:24:38 -07004252 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004253 return -EAGAIN;
4254
Jens Axboe3529d8c2019-12-19 18:24:38 -07004255 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004256 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004257 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004258
Jens Axboede0617e2019-04-06 21:51:27 -06004259 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004260 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004261 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004262 return 0;
4263 }
4264
Jens Axboe915967f2019-11-21 09:01:20 -07004265 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004266 list_add_tail(&req->list, &ctx->defer_list);
4267 spin_unlock_irq(&ctx->completion_lock);
4268 return -EIOCBQUEUED;
4269}
4270
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004271static void io_cleanup_req(struct io_kiocb *req)
4272{
4273 struct io_async_ctx *io = req->io;
4274
4275 switch (req->opcode) {
4276 case IORING_OP_READV:
4277 case IORING_OP_READ_FIXED:
4278 case IORING_OP_READ:
4279 case IORING_OP_WRITEV:
4280 case IORING_OP_WRITE_FIXED:
4281 case IORING_OP_WRITE:
4282 if (io->rw.iov != io->rw.fast_iov)
4283 kfree(io->rw.iov);
4284 break;
4285 case IORING_OP_SENDMSG:
4286 case IORING_OP_RECVMSG:
4287 if (io->msg.iov != io->msg.fast_iov)
4288 kfree(io->msg.iov);
4289 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004290 case IORING_OP_OPENAT:
4291 case IORING_OP_OPENAT2:
4292 case IORING_OP_STATX:
4293 putname(req->open.filename);
4294 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004295 }
4296
4297 req->flags &= ~REQ_F_NEED_CLEANUP;
4298}
4299
Jens Axboe3529d8c2019-12-19 18:24:38 -07004300static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4301 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004302{
Jackie Liua197f662019-11-08 08:09:12 -07004303 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004304 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004305
Jens Axboed625c6e2019-12-17 19:53:05 -07004306 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004307 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004308 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004309 break;
4310 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004311 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004312 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004313 if (sqe) {
4314 ret = io_read_prep(req, sqe, force_nonblock);
4315 if (ret < 0)
4316 break;
4317 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004318 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004319 break;
4320 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004321 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004322 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004323 if (sqe) {
4324 ret = io_write_prep(req, sqe, force_nonblock);
4325 if (ret < 0)
4326 break;
4327 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004328 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004329 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004330 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004331 if (sqe) {
4332 ret = io_prep_fsync(req, sqe);
4333 if (ret < 0)
4334 break;
4335 }
Jens Axboefc4df992019-12-10 14:38:45 -07004336 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004337 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004338 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004339 if (sqe) {
4340 ret = io_poll_add_prep(req, sqe);
4341 if (ret)
4342 break;
4343 }
Jens Axboefc4df992019-12-10 14:38:45 -07004344 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004345 break;
4346 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004347 if (sqe) {
4348 ret = io_poll_remove_prep(req, sqe);
4349 if (ret < 0)
4350 break;
4351 }
Jens Axboefc4df992019-12-10 14:38:45 -07004352 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004353 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004354 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004355 if (sqe) {
4356 ret = io_prep_sfr(req, sqe);
4357 if (ret < 0)
4358 break;
4359 }
Jens Axboefc4df992019-12-10 14:38:45 -07004360 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004361 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004362 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004363 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004364 if (sqe) {
4365 ret = io_sendmsg_prep(req, sqe);
4366 if (ret < 0)
4367 break;
4368 }
Jens Axboefddafac2020-01-04 20:19:44 -07004369 if (req->opcode == IORING_OP_SENDMSG)
4370 ret = io_sendmsg(req, nxt, force_nonblock);
4371 else
4372 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004373 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004374 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004375 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004376 if (sqe) {
4377 ret = io_recvmsg_prep(req, sqe);
4378 if (ret)
4379 break;
4380 }
Jens Axboefddafac2020-01-04 20:19:44 -07004381 if (req->opcode == IORING_OP_RECVMSG)
4382 ret = io_recvmsg(req, nxt, force_nonblock);
4383 else
4384 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004385 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004386 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004387 if (sqe) {
4388 ret = io_timeout_prep(req, sqe, false);
4389 if (ret)
4390 break;
4391 }
Jens Axboefc4df992019-12-10 14:38:45 -07004392 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004393 break;
Jens Axboe11365042019-10-16 09:08:32 -06004394 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004395 if (sqe) {
4396 ret = io_timeout_remove_prep(req, sqe);
4397 if (ret)
4398 break;
4399 }
Jens Axboefc4df992019-12-10 14:38:45 -07004400 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004401 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004402 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004403 if (sqe) {
4404 ret = io_accept_prep(req, sqe);
4405 if (ret)
4406 break;
4407 }
Jens Axboefc4df992019-12-10 14:38:45 -07004408 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004409 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004410 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004411 if (sqe) {
4412 ret = io_connect_prep(req, sqe);
4413 if (ret)
4414 break;
4415 }
Jens Axboefc4df992019-12-10 14:38:45 -07004416 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004417 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004418 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004419 if (sqe) {
4420 ret = io_async_cancel_prep(req, sqe);
4421 if (ret)
4422 break;
4423 }
Jens Axboefc4df992019-12-10 14:38:45 -07004424 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004425 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004426 case IORING_OP_FALLOCATE:
4427 if (sqe) {
4428 ret = io_fallocate_prep(req, sqe);
4429 if (ret)
4430 break;
4431 }
4432 ret = io_fallocate(req, nxt, force_nonblock);
4433 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004434 case IORING_OP_OPENAT:
4435 if (sqe) {
4436 ret = io_openat_prep(req, sqe);
4437 if (ret)
4438 break;
4439 }
4440 ret = io_openat(req, nxt, force_nonblock);
4441 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004442 case IORING_OP_CLOSE:
4443 if (sqe) {
4444 ret = io_close_prep(req, sqe);
4445 if (ret)
4446 break;
4447 }
4448 ret = io_close(req, nxt, force_nonblock);
4449 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004450 case IORING_OP_FILES_UPDATE:
4451 if (sqe) {
4452 ret = io_files_update_prep(req, sqe);
4453 if (ret)
4454 break;
4455 }
4456 ret = io_files_update(req, force_nonblock);
4457 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004458 case IORING_OP_STATX:
4459 if (sqe) {
4460 ret = io_statx_prep(req, sqe);
4461 if (ret)
4462 break;
4463 }
4464 ret = io_statx(req, nxt, force_nonblock);
4465 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004466 case IORING_OP_FADVISE:
4467 if (sqe) {
4468 ret = io_fadvise_prep(req, sqe);
4469 if (ret)
4470 break;
4471 }
4472 ret = io_fadvise(req, nxt, force_nonblock);
4473 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004474 case IORING_OP_MADVISE:
4475 if (sqe) {
4476 ret = io_madvise_prep(req, sqe);
4477 if (ret)
4478 break;
4479 }
4480 ret = io_madvise(req, nxt, force_nonblock);
4481 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004482 case IORING_OP_OPENAT2:
4483 if (sqe) {
4484 ret = io_openat2_prep(req, sqe);
4485 if (ret)
4486 break;
4487 }
4488 ret = io_openat2(req, nxt, force_nonblock);
4489 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004490 case IORING_OP_EPOLL_CTL:
4491 if (sqe) {
4492 ret = io_epoll_ctl_prep(req, sqe);
4493 if (ret)
4494 break;
4495 }
4496 ret = io_epoll_ctl(req, nxt, force_nonblock);
4497 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004498 default:
4499 ret = -EINVAL;
4500 break;
4501 }
4502
Jens Axboedef596e2019-01-09 08:59:42 -07004503 if (ret)
4504 return ret;
4505
4506 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004507 const bool in_async = io_wq_current_is_worker();
4508
Jens Axboe9e645e112019-05-10 16:07:28 -06004509 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004510 return -EAGAIN;
4511
Jens Axboe11ba8202020-01-15 21:51:17 -07004512 /* workqueue context doesn't hold uring_lock, grab it now */
4513 if (in_async)
4514 mutex_lock(&ctx->uring_lock);
4515
Jens Axboedef596e2019-01-09 08:59:42 -07004516 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004517
4518 if (in_async)
4519 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004520 }
4521
4522 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004523}
4524
Jens Axboe561fb042019-10-24 07:25:42 -06004525static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004526{
Jens Axboe561fb042019-10-24 07:25:42 -06004527 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004528 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004529 struct io_kiocb *nxt = NULL;
4530 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004531
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004532 /* if NO_CANCEL is set, we must still run the work */
4533 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4534 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004535 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004536 }
Jens Axboe31b51512019-01-18 22:56:34 -07004537
Jens Axboe561fb042019-10-24 07:25:42 -06004538 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06004539 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004540 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004541 /*
4542 * We can get EAGAIN for polled IO even though we're
4543 * forcing a sync submission from here, since we can't
4544 * wait for request slots on the block side.
4545 */
4546 if (ret != -EAGAIN)
4547 break;
4548 cond_resched();
4549 } while (1);
4550 }
Jens Axboe31b51512019-01-18 22:56:34 -07004551
Jens Axboe561fb042019-10-24 07:25:42 -06004552 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004553 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004554
Jens Axboe561fb042019-10-24 07:25:42 -06004555 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004556 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004557 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004558 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004559 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004560
Jens Axboe561fb042019-10-24 07:25:42 -06004561 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004562 if (!ret && nxt)
4563 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004564}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004565
Jens Axboe15b71ab2019-12-11 11:20:36 -07004566static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004567{
Jens Axboed3656342019-12-18 09:50:26 -07004568 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004569 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004570 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004571 return 0;
4572 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004573}
4574
Jens Axboe65e19f52019-10-26 07:20:21 -06004575static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4576 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004577{
Jens Axboe65e19f52019-10-26 07:20:21 -06004578 struct fixed_file_table *table;
4579
Jens Axboe05f3fb32019-12-09 11:22:50 -07004580 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4581 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004582}
4583
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004584static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
4585 int fd, struct file **out_file, bool fixed)
4586{
4587 struct io_ring_ctx *ctx = req->ctx;
4588 struct file *file;
4589
4590 if (fixed) {
4591 if (unlikely(!ctx->file_data ||
4592 (unsigned) fd >= ctx->nr_user_files))
4593 return -EBADF;
4594 fd = array_index_nospec(fd, ctx->nr_user_files);
4595 file = io_file_from_index(ctx, fd);
4596 if (!file)
4597 return -EBADF;
4598 percpu_ref_get(&ctx->file_data->refs);
4599 } else {
4600 trace_io_uring_file_get(ctx, fd);
4601 file = __io_file_get(state, fd);
4602 if (unlikely(!file))
4603 return -EBADF;
4604 }
4605
4606 *out_file = file;
4607 return 0;
4608}
4609
Jens Axboe3529d8c2019-12-19 18:24:38 -07004610static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4611 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004612{
4613 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004614 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004615 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06004616
Jens Axboe3529d8c2019-12-19 18:24:38 -07004617 flags = READ_ONCE(sqe->flags);
4618 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004619
Jens Axboed3656342019-12-18 09:50:26 -07004620 if (!io_req_needs_file(req, fd))
4621 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004622
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004623 fixed = (flags & IOSQE_FIXED_FILE);
4624 if (unlikely(!fixed && req->needs_fixed_file))
4625 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004626
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004627 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06004628}
4629
Jackie Liua197f662019-11-08 08:09:12 -07004630static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004631{
Jens Axboefcb323c2019-10-24 12:39:47 -06004632 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004633 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004634
Jens Axboef86cd202020-01-29 13:46:44 -07004635 if (req->work.files)
4636 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004637 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004638 return -EBADF;
4639
Jens Axboefcb323c2019-10-24 12:39:47 -06004640 rcu_read_lock();
4641 spin_lock_irq(&ctx->inflight_lock);
4642 /*
4643 * We use the f_ops->flush() handler to ensure that we can flush
4644 * out work accessing these files if the fd is closed. Check if
4645 * the fd has changed since we started down this path, and disallow
4646 * this operation if it has.
4647 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004648 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004649 list_add(&req->inflight_entry, &ctx->inflight_list);
4650 req->flags |= REQ_F_INFLIGHT;
4651 req->work.files = current->files;
4652 ret = 0;
4653 }
4654 spin_unlock_irq(&ctx->inflight_lock);
4655 rcu_read_unlock();
4656
4657 return ret;
4658}
4659
Jens Axboe2665abf2019-11-05 12:40:47 -07004660static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4661{
Jens Axboead8a48a2019-11-15 08:49:11 -07004662 struct io_timeout_data *data = container_of(timer,
4663 struct io_timeout_data, timer);
4664 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004665 struct io_ring_ctx *ctx = req->ctx;
4666 struct io_kiocb *prev = NULL;
4667 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004668
4669 spin_lock_irqsave(&ctx->completion_lock, flags);
4670
4671 /*
4672 * We don't expect the list to be empty, that will only happen if we
4673 * race with the completion of the linked work.
4674 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004675 if (!list_empty(&req->link_list)) {
4676 prev = list_entry(req->link_list.prev, struct io_kiocb,
4677 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004678 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004679 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004680 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4681 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004682 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004683 }
4684
4685 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4686
4687 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004688 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004689 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4690 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004691 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004692 } else {
4693 io_cqring_add_event(req, -ETIME);
4694 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004695 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004696 return HRTIMER_NORESTART;
4697}
4698
Jens Axboead8a48a2019-11-15 08:49:11 -07004699static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004700{
Jens Axboe76a46e02019-11-10 23:34:16 -07004701 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004702
Jens Axboe76a46e02019-11-10 23:34:16 -07004703 /*
4704 * If the list is now empty, then our linked request finished before
4705 * we got a chance to setup the timer
4706 */
4707 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004708 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004709 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004710
Jens Axboead8a48a2019-11-15 08:49:11 -07004711 data->timer.function = io_link_timeout_fn;
4712 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4713 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004714 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004715 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004716
Jens Axboe2665abf2019-11-05 12:40:47 -07004717 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004718 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004719}
4720
Jens Axboead8a48a2019-11-15 08:49:11 -07004721static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004722{
4723 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004724
Jens Axboe2665abf2019-11-05 12:40:47 -07004725 if (!(req->flags & REQ_F_LINK))
4726 return NULL;
4727
Pavel Begunkov44932332019-12-05 16:16:35 +03004728 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4729 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004730 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004731 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004732
Jens Axboe76a46e02019-11-10 23:34:16 -07004733 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004734 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004735}
4736
Jens Axboe3529d8c2019-12-19 18:24:38 -07004737static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004738{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004739 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004740 struct io_kiocb *nxt = NULL;
Jens Axboe193155c2020-02-22 23:22:19 -07004741 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004742 int ret;
4743
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004744again:
4745 linked_timeout = io_prep_linked_timeout(req);
4746
Jens Axboe193155c2020-02-22 23:22:19 -07004747 if (req->work.creds && req->work.creds != current_cred()) {
4748 if (old_creds)
4749 revert_creds(old_creds);
4750 if (old_creds == req->work.creds)
4751 old_creds = NULL; /* restored original creds */
4752 else
4753 old_creds = override_creds(req->work.creds);
4754 }
4755
Jens Axboe3529d8c2019-12-19 18:24:38 -07004756 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004757
4758 /*
4759 * We async punt it if the file wasn't marked NOWAIT, or if the file
4760 * doesn't support non-blocking read/write attempts
4761 */
4762 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4763 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004764punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004765 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004766 ret = io_grab_files(req);
4767 if (ret)
4768 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004769 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004770
4771 /*
4772 * Queued up for async execution, worker will release
4773 * submit reference when the iocb is actually submitted.
4774 */
4775 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004776 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004777 }
Jens Axboee65ef562019-03-12 10:16:44 -06004778
Jens Axboefcb323c2019-10-24 12:39:47 -06004779err:
Jens Axboee65ef562019-03-12 10:16:44 -06004780 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004781 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004782
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004783 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004784 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004785 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004786 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004787 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004788 }
4789
Jens Axboee65ef562019-03-12 10:16:44 -06004790 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004791 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004792 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004793 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004794 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004795 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004796done_req:
4797 if (nxt) {
4798 req = nxt;
4799 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004800
4801 if (req->flags & REQ_F_FORCE_ASYNC)
4802 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004803 goto again;
4804 }
Jens Axboe193155c2020-02-22 23:22:19 -07004805 if (old_creds)
4806 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004807}
4808
Jens Axboe3529d8c2019-12-19 18:24:38 -07004809static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004810{
4811 int ret;
4812
Jens Axboe3529d8c2019-12-19 18:24:38 -07004813 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004814 if (ret) {
4815 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004816fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004817 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004818 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004819 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004820 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004821 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004822 ret = io_req_defer_prep(req, sqe);
4823 if (unlikely(ret < 0))
4824 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004825 /*
4826 * Never try inline submit of IOSQE_ASYNC is set, go straight
4827 * to async execution.
4828 */
4829 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4830 io_queue_async_work(req);
4831 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004832 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004833 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004834}
4835
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004836static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004837{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004838 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004839 io_cqring_add_event(req, -ECANCELED);
4840 io_double_put_req(req);
4841 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004842 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004843}
4844
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004845#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004846 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004847
Jens Axboe3529d8c2019-12-19 18:24:38 -07004848static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4849 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004850{
Jackie Liua197f662019-11-08 08:09:12 -07004851 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004852 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004853 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004854
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004855 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004856
4857 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004858 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004859 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004860 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004861 }
4862
Jens Axboe75c6a032020-01-28 10:15:23 -07004863 id = READ_ONCE(sqe->personality);
4864 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07004865 req->work.creds = idr_find(&ctx->personality_idr, id);
4866 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07004867 ret = -EINVAL;
4868 goto err_req;
4869 }
Jens Axboe193155c2020-02-22 23:22:19 -07004870 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07004871 }
4872
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004873 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004874 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
4875 IOSQE_ASYNC | IOSQE_FIXED_FILE);
Jens Axboe9e645e112019-05-10 16:07:28 -06004876
Jens Axboe3529d8c2019-12-19 18:24:38 -07004877 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004878 if (unlikely(ret)) {
4879err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004880 io_cqring_add_event(req, ret);
4881 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004882 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004883 }
4884
Jens Axboe9e645e112019-05-10 16:07:28 -06004885 /*
4886 * If we already have a head request, queue this one for async
4887 * submittal once the head completes. If we don't have a head but
4888 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4889 * submitted sync once the chain is complete. If none of those
4890 * conditions are true (normal request), then just queue it.
4891 */
4892 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004893 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004894
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004895 /*
4896 * Taking sequential execution of a link, draining both sides
4897 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4898 * requests in the link. So, it drains the head and the
4899 * next after the link request. The last one is done via
4900 * drain_next flag to persist the effect across calls.
4901 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004902 if (sqe_flags & IOSQE_IO_DRAIN) {
4903 head->flags |= REQ_F_IO_DRAIN;
4904 ctx->drain_next = 1;
4905 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004906 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004907 ret = -EAGAIN;
4908 goto err_req;
4909 }
4910
Jens Axboe3529d8c2019-12-19 18:24:38 -07004911 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004912 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004913 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004914 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004915 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004916 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004917 trace_io_uring_link(ctx, req, head);
4918 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004919
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004920 /* last request of a link, enqueue the link */
4921 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4922 io_queue_link_head(head);
4923 *link = NULL;
4924 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004925 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004926 if (unlikely(ctx->drain_next)) {
4927 req->flags |= REQ_F_IO_DRAIN;
4928 req->ctx->drain_next = 0;
4929 }
4930 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4931 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004932 INIT_LIST_HEAD(&req->link_list);
4933 ret = io_req_defer_prep(req, sqe);
4934 if (ret)
4935 req->flags |= REQ_F_FAIL_LINK;
4936 *link = req;
4937 } else {
4938 io_queue_sqe(req, sqe);
4939 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004940 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004941
4942 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004943}
4944
Jens Axboe9a56a232019-01-09 09:06:50 -07004945/*
4946 * Batched submission is done, ensure local IO is flushed out.
4947 */
4948static void io_submit_state_end(struct io_submit_state *state)
4949{
4950 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004951 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004952 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004953 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004954}
4955
4956/*
4957 * Start submission side cache.
4958 */
4959static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004960 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004961{
4962 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004963 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004964 state->file = NULL;
4965 state->ios_left = max_ios;
4966}
4967
Jens Axboe2b188cc2019-01-07 10:46:33 -07004968static void io_commit_sqring(struct io_ring_ctx *ctx)
4969{
Hristo Venev75b28af2019-08-26 17:23:46 +00004970 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004971
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004972 /*
4973 * Ensure any loads from the SQEs are done at this point,
4974 * since once we write the new head, the application could
4975 * write new data to them.
4976 */
4977 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004978}
4979
4980/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004981 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004982 * that is mapped by userspace. This means that care needs to be taken to
4983 * ensure that reads are stable, as we cannot rely on userspace always
4984 * being a good citizen. If members of the sqe are validated and then later
4985 * used, it's important that those reads are done through READ_ONCE() to
4986 * prevent a re-load down the line.
4987 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004988static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4989 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004990{
Hristo Venev75b28af2019-08-26 17:23:46 +00004991 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004992 unsigned head;
4993
4994 /*
4995 * The cached sq head (or cq tail) serves two purposes:
4996 *
4997 * 1) allows us to batch the cost of updating the user visible
4998 * head updates.
4999 * 2) allows the kernel side to track the head on its own, even
5000 * though the application is the one updating it.
5001 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005002 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005003 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005004 /*
5005 * All io need record the previous position, if LINK vs DARIN,
5006 * it can be used to mark the position of the first IO in the
5007 * link list.
5008 */
5009 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005010 *sqe_ptr = &ctx->sq_sqes[head];
5011 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5012 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005013 ctx->cached_sq_head++;
5014 return true;
5015 }
5016
5017 /* drop invalid entries */
5018 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005019 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005020 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005021 return false;
5022}
5023
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005024static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005025 struct file *ring_file, int ring_fd,
5026 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005027{
5028 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005029 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005030 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005031 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005032
Jens Axboec4a2ed72019-11-21 21:01:26 -07005033 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005034 if (test_bit(0, &ctx->sq_check_overflow)) {
5035 if (!list_empty(&ctx->cq_overflow_list) &&
5036 !io_cqring_overflow_flush(ctx, false))
5037 return -EBUSY;
5038 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005039
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005040 /* make sure SQ entry isn't read before tail */
5041 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005042
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005043 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5044 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005045
5046 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005047 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005048 statep = &state;
5049 }
5050
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005051 ctx->ring_fd = ring_fd;
5052 ctx->ring_file = ring_file;
5053
Jens Axboe6c271ce2019-01-10 11:22:30 -07005054 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005055 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005056 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005057 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005058
Pavel Begunkov196be952019-11-07 01:41:06 +03005059 req = io_get_req(ctx, statep);
5060 if (unlikely(!req)) {
5061 if (!submitted)
5062 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005063 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005064 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005065 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005066 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005067 break;
5068 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005069
Jens Axboed3656342019-12-18 09:50:26 -07005070 /* will complete beyond this point, count as submitted */
5071 submitted++;
5072
5073 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005074 err = -EINVAL;
5075fail_req:
5076 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005077 io_double_put_req(req);
5078 break;
5079 }
5080
5081 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005082 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005083 if (unlikely(mm_fault)) {
5084 err = -EFAULT;
5085 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005086 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005087 use_mm(ctx->sqo_mm);
5088 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005089 }
5090
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005091 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005092 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5093 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005094 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005095 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005096 }
5097
Pavel Begunkov9466f432020-01-25 22:34:01 +03005098 if (unlikely(submitted != nr)) {
5099 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5100
5101 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5102 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005103 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005104 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005105 if (statep)
5106 io_submit_state_end(&state);
5107
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005108 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5109 io_commit_sqring(ctx);
5110
Jens Axboe6c271ce2019-01-10 11:22:30 -07005111 return submitted;
5112}
5113
5114static int io_sq_thread(void *data)
5115{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005116 struct io_ring_ctx *ctx = data;
5117 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005118 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005119 mm_segment_t old_fs;
5120 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005121 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005122 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005123
Jens Axboe206aefd2019-11-07 18:27:42 -07005124 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005125
Jens Axboe6c271ce2019-01-10 11:22:30 -07005126 old_fs = get_fs();
5127 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005128 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005129
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005130 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005131 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005132 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005133
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005134 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005135 unsigned nr_events = 0;
5136
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005137 mutex_lock(&ctx->uring_lock);
5138 if (!list_empty(&ctx->poll_list))
5139 io_iopoll_getevents(ctx, &nr_events, 0);
5140 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005141 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005142 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005143 }
5144
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005145 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005146
5147 /*
5148 * If submit got -EBUSY, flag us as needing the application
5149 * to enter the kernel to reap and flush events.
5150 */
5151 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005152 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005153 * Drop cur_mm before scheduling, we can't hold it for
5154 * long periods (or over schedule()). Do this before
5155 * adding ourselves to the waitqueue, as the unuse/drop
5156 * may sleep.
5157 */
5158 if (cur_mm) {
5159 unuse_mm(cur_mm);
5160 mmput(cur_mm);
5161 cur_mm = NULL;
5162 }
5163
5164 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005165 * We're polling. If we're within the defined idle
5166 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005167 * to sleep. The exception is if we got EBUSY doing
5168 * more IO, we should wait for the application to
5169 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005170 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005171 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005172 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5173 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005174 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005175 continue;
5176 }
5177
Jens Axboe6c271ce2019-01-10 11:22:30 -07005178 prepare_to_wait(&ctx->sqo_wait, &wait,
5179 TASK_INTERRUPTIBLE);
5180
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005181 /*
5182 * While doing polled IO, before going to sleep, we need
5183 * to check if there are new reqs added to poll_list, it
5184 * is because reqs may have been punted to io worker and
5185 * will be added to poll_list later, hence check the
5186 * poll_list again.
5187 */
5188 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5189 !list_empty_careful(&ctx->poll_list)) {
5190 finish_wait(&ctx->sqo_wait, &wait);
5191 continue;
5192 }
5193
Jens Axboe6c271ce2019-01-10 11:22:30 -07005194 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005195 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005196 /* make sure to read SQ tail after writing flags */
5197 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005198
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005199 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005200 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005201 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005202 finish_wait(&ctx->sqo_wait, &wait);
5203 break;
5204 }
5205 if (signal_pending(current))
5206 flush_signals(current);
5207 schedule();
5208 finish_wait(&ctx->sqo_wait, &wait);
5209
Hristo Venev75b28af2019-08-26 17:23:46 +00005210 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005211 continue;
5212 }
5213 finish_wait(&ctx->sqo_wait, &wait);
5214
Hristo Venev75b28af2019-08-26 17:23:46 +00005215 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005216 }
5217
Jens Axboe8a4955f2019-12-09 14:52:35 -07005218 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005219 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005220 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005221 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005222 }
5223
5224 set_fs(old_fs);
5225 if (cur_mm) {
5226 unuse_mm(cur_mm);
5227 mmput(cur_mm);
5228 }
Jens Axboe181e4482019-11-25 08:52:30 -07005229 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005230
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005231 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005232
Jens Axboe6c271ce2019-01-10 11:22:30 -07005233 return 0;
5234}
5235
Jens Axboebda52162019-09-24 13:47:15 -06005236struct io_wait_queue {
5237 struct wait_queue_entry wq;
5238 struct io_ring_ctx *ctx;
5239 unsigned to_wait;
5240 unsigned nr_timeouts;
5241};
5242
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005243static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005244{
5245 struct io_ring_ctx *ctx = iowq->ctx;
5246
5247 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005248 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005249 * started waiting. For timeouts, we always want to return to userspace,
5250 * regardless of event count.
5251 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005252 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005253 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5254}
5255
5256static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5257 int wake_flags, void *key)
5258{
5259 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5260 wq);
5261
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005262 /* use noflush == true, as we can't safely rely on locking context */
5263 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005264 return -1;
5265
5266 return autoremove_wake_function(curr, mode, wake_flags, key);
5267}
5268
Jens Axboe2b188cc2019-01-07 10:46:33 -07005269/*
5270 * Wait until events become available, if we don't already have some. The
5271 * application must reap them itself, as they reside on the shared cq ring.
5272 */
5273static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5274 const sigset_t __user *sig, size_t sigsz)
5275{
Jens Axboebda52162019-09-24 13:47:15 -06005276 struct io_wait_queue iowq = {
5277 .wq = {
5278 .private = current,
5279 .func = io_wake_function,
5280 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5281 },
5282 .ctx = ctx,
5283 .to_wait = min_events,
5284 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005285 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005286 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005287
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005288 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005289 return 0;
5290
5291 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005292#ifdef CONFIG_COMPAT
5293 if (in_compat_syscall())
5294 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005295 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005296 else
5297#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005298 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005299
Jens Axboe2b188cc2019-01-07 10:46:33 -07005300 if (ret)
5301 return ret;
5302 }
5303
Jens Axboebda52162019-09-24 13:47:15 -06005304 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005305 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005306 do {
5307 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5308 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005309 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005310 break;
5311 schedule();
5312 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005313 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005314 break;
5315 }
5316 } while (1);
5317 finish_wait(&ctx->wait, &iowq.wq);
5318
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005319 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005320
Hristo Venev75b28af2019-08-26 17:23:46 +00005321 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005322}
5323
Jens Axboe6b063142019-01-10 22:13:58 -07005324static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5325{
5326#if defined(CONFIG_UNIX)
5327 if (ctx->ring_sock) {
5328 struct sock *sock = ctx->ring_sock->sk;
5329 struct sk_buff *skb;
5330
5331 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5332 kfree_skb(skb);
5333 }
5334#else
5335 int i;
5336
Jens Axboe65e19f52019-10-26 07:20:21 -06005337 for (i = 0; i < ctx->nr_user_files; i++) {
5338 struct file *file;
5339
5340 file = io_file_from_index(ctx, i);
5341 if (file)
5342 fput(file);
5343 }
Jens Axboe6b063142019-01-10 22:13:58 -07005344#endif
5345}
5346
Jens Axboe05f3fb32019-12-09 11:22:50 -07005347static void io_file_ref_kill(struct percpu_ref *ref)
5348{
5349 struct fixed_file_data *data;
5350
5351 data = container_of(ref, struct fixed_file_data, refs);
5352 complete(&data->done);
5353}
5354
Jens Axboe6b063142019-01-10 22:13:58 -07005355static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5356{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005357 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005358 unsigned nr_tables, i;
5359
Jens Axboe05f3fb32019-12-09 11:22:50 -07005360 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005361 return -ENXIO;
5362
Jens Axboe05f3fb32019-12-09 11:22:50 -07005363 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005364 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005365 wait_for_completion(&data->done);
5366 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005367 percpu_ref_exit(&data->refs);
5368
Jens Axboe6b063142019-01-10 22:13:58 -07005369 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005370 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5371 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005372 kfree(data->table[i].files);
5373 kfree(data->table);
5374 kfree(data);
5375 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005376 ctx->nr_user_files = 0;
5377 return 0;
5378}
5379
Jens Axboe6c271ce2019-01-10 11:22:30 -07005380static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5381{
5382 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005383 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005384 /*
5385 * The park is a bit of a work-around, without it we get
5386 * warning spews on shutdown with SQPOLL set and affinity
5387 * set to a single CPU.
5388 */
Jens Axboe06058632019-04-13 09:26:03 -06005389 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005390 kthread_stop(ctx->sqo_thread);
5391 ctx->sqo_thread = NULL;
5392 }
5393}
5394
Jens Axboe6b063142019-01-10 22:13:58 -07005395static void io_finish_async(struct io_ring_ctx *ctx)
5396{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005397 io_sq_thread_stop(ctx);
5398
Jens Axboe561fb042019-10-24 07:25:42 -06005399 if (ctx->io_wq) {
5400 io_wq_destroy(ctx->io_wq);
5401 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005402 }
5403}
5404
5405#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005406/*
5407 * Ensure the UNIX gc is aware of our file set, so we are certain that
5408 * the io_uring can be safely unregistered on process exit, even if we have
5409 * loops in the file referencing.
5410 */
5411static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5412{
5413 struct sock *sk = ctx->ring_sock->sk;
5414 struct scm_fp_list *fpl;
5415 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005416 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005417
5418 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5419 unsigned long inflight = ctx->user->unix_inflight + nr;
5420
5421 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5422 return -EMFILE;
5423 }
5424
5425 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5426 if (!fpl)
5427 return -ENOMEM;
5428
5429 skb = alloc_skb(0, GFP_KERNEL);
5430 if (!skb) {
5431 kfree(fpl);
5432 return -ENOMEM;
5433 }
5434
5435 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005436
Jens Axboe08a45172019-10-03 08:11:03 -06005437 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005438 fpl->user = get_uid(ctx->user);
5439 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005440 struct file *file = io_file_from_index(ctx, i + offset);
5441
5442 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005443 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005444 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005445 unix_inflight(fpl->user, fpl->fp[nr_files]);
5446 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005447 }
5448
Jens Axboe08a45172019-10-03 08:11:03 -06005449 if (nr_files) {
5450 fpl->max = SCM_MAX_FD;
5451 fpl->count = nr_files;
5452 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005453 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005454 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5455 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005456
Jens Axboe08a45172019-10-03 08:11:03 -06005457 for (i = 0; i < nr_files; i++)
5458 fput(fpl->fp[i]);
5459 } else {
5460 kfree_skb(skb);
5461 kfree(fpl);
5462 }
Jens Axboe6b063142019-01-10 22:13:58 -07005463
5464 return 0;
5465}
5466
5467/*
5468 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5469 * causes regular reference counting to break down. We rely on the UNIX
5470 * garbage collection to take care of this problem for us.
5471 */
5472static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5473{
5474 unsigned left, total;
5475 int ret = 0;
5476
5477 total = 0;
5478 left = ctx->nr_user_files;
5479 while (left) {
5480 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005481
5482 ret = __io_sqe_files_scm(ctx, this_files, total);
5483 if (ret)
5484 break;
5485 left -= this_files;
5486 total += this_files;
5487 }
5488
5489 if (!ret)
5490 return 0;
5491
5492 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005493 struct file *file = io_file_from_index(ctx, total);
5494
5495 if (file)
5496 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005497 total++;
5498 }
5499
5500 return ret;
5501}
5502#else
5503static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5504{
5505 return 0;
5506}
5507#endif
5508
Jens Axboe65e19f52019-10-26 07:20:21 -06005509static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5510 unsigned nr_files)
5511{
5512 int i;
5513
5514 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005515 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005516 unsigned this_files;
5517
5518 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5519 table->files = kcalloc(this_files, sizeof(struct file *),
5520 GFP_KERNEL);
5521 if (!table->files)
5522 break;
5523 nr_files -= this_files;
5524 }
5525
5526 if (i == nr_tables)
5527 return 0;
5528
5529 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005530 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005531 kfree(table->files);
5532 }
5533 return 1;
5534}
5535
Jens Axboe05f3fb32019-12-09 11:22:50 -07005536static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005537{
5538#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005539 struct sock *sock = ctx->ring_sock->sk;
5540 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5541 struct sk_buff *skb;
5542 int i;
5543
5544 __skb_queue_head_init(&list);
5545
5546 /*
5547 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5548 * remove this entry and rearrange the file array.
5549 */
5550 skb = skb_dequeue(head);
5551 while (skb) {
5552 struct scm_fp_list *fp;
5553
5554 fp = UNIXCB(skb).fp;
5555 for (i = 0; i < fp->count; i++) {
5556 int left;
5557
5558 if (fp->fp[i] != file)
5559 continue;
5560
5561 unix_notinflight(fp->user, fp->fp[i]);
5562 left = fp->count - 1 - i;
5563 if (left) {
5564 memmove(&fp->fp[i], &fp->fp[i + 1],
5565 left * sizeof(struct file *));
5566 }
5567 fp->count--;
5568 if (!fp->count) {
5569 kfree_skb(skb);
5570 skb = NULL;
5571 } else {
5572 __skb_queue_tail(&list, skb);
5573 }
5574 fput(file);
5575 file = NULL;
5576 break;
5577 }
5578
5579 if (!file)
5580 break;
5581
5582 __skb_queue_tail(&list, skb);
5583
5584 skb = skb_dequeue(head);
5585 }
5586
5587 if (skb_peek(&list)) {
5588 spin_lock_irq(&head->lock);
5589 while ((skb = __skb_dequeue(&list)) != NULL)
5590 __skb_queue_tail(head, skb);
5591 spin_unlock_irq(&head->lock);
5592 }
5593#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005594 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005595#endif
5596}
5597
Jens Axboe05f3fb32019-12-09 11:22:50 -07005598struct io_file_put {
5599 struct llist_node llist;
5600 struct file *file;
5601 struct completion *done;
5602};
5603
Jens Axboe2faf8522020-02-04 19:54:55 -07005604static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005605{
5606 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005607 struct llist_node *node;
5608
Jens Axboe05f3fb32019-12-09 11:22:50 -07005609 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5610 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5611 io_ring_file_put(data->ctx, pfile->file);
5612 if (pfile->done)
5613 complete(pfile->done);
5614 else
5615 kfree(pfile);
5616 }
5617 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005618}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005619
Jens Axboe2faf8522020-02-04 19:54:55 -07005620static void io_ring_file_ref_switch(struct work_struct *work)
5621{
5622 struct fixed_file_data *data;
5623
5624 data = container_of(work, struct fixed_file_data, ref_work);
5625 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005626 percpu_ref_switch_to_percpu(&data->refs);
5627}
5628
5629static void io_file_data_ref_zero(struct percpu_ref *ref)
5630{
5631 struct fixed_file_data *data;
5632
5633 data = container_of(ref, struct fixed_file_data, refs);
5634
Jens Axboe2faf8522020-02-04 19:54:55 -07005635 /*
5636 * We can't safely switch from inside this context, punt to wq. If
5637 * the table ref is going away, the table is being unregistered.
5638 * Don't queue up the async work for that case, the caller will
5639 * handle it.
5640 */
5641 if (!percpu_ref_is_dying(&data->refs))
5642 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005643}
5644
5645static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5646 unsigned nr_args)
5647{
5648 __s32 __user *fds = (__s32 __user *) arg;
5649 unsigned nr_tables;
5650 struct file *file;
5651 int fd, ret = 0;
5652 unsigned i;
5653
5654 if (ctx->file_data)
5655 return -EBUSY;
5656 if (!nr_args)
5657 return -EINVAL;
5658 if (nr_args > IORING_MAX_FIXED_FILES)
5659 return -EMFILE;
5660
5661 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5662 if (!ctx->file_data)
5663 return -ENOMEM;
5664 ctx->file_data->ctx = ctx;
5665 init_completion(&ctx->file_data->done);
5666
5667 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5668 ctx->file_data->table = kcalloc(nr_tables,
5669 sizeof(struct fixed_file_table),
5670 GFP_KERNEL);
5671 if (!ctx->file_data->table) {
5672 kfree(ctx->file_data);
5673 ctx->file_data = NULL;
5674 return -ENOMEM;
5675 }
5676
5677 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5678 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5679 kfree(ctx->file_data->table);
5680 kfree(ctx->file_data);
5681 ctx->file_data = NULL;
5682 return -ENOMEM;
5683 }
5684 ctx->file_data->put_llist.first = NULL;
5685 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5686
5687 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5688 percpu_ref_exit(&ctx->file_data->refs);
5689 kfree(ctx->file_data->table);
5690 kfree(ctx->file_data);
5691 ctx->file_data = NULL;
5692 return -ENOMEM;
5693 }
5694
5695 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5696 struct fixed_file_table *table;
5697 unsigned index;
5698
5699 ret = -EFAULT;
5700 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5701 break;
5702 /* allow sparse sets */
5703 if (fd == -1) {
5704 ret = 0;
5705 continue;
5706 }
5707
5708 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5709 index = i & IORING_FILE_TABLE_MASK;
5710 file = fget(fd);
5711
5712 ret = -EBADF;
5713 if (!file)
5714 break;
5715
5716 /*
5717 * Don't allow io_uring instances to be registered. If UNIX
5718 * isn't enabled, then this causes a reference cycle and this
5719 * instance can never get freed. If UNIX is enabled we'll
5720 * handle it just fine, but there's still no point in allowing
5721 * a ring fd as it doesn't support regular read/write anyway.
5722 */
5723 if (file->f_op == &io_uring_fops) {
5724 fput(file);
5725 break;
5726 }
5727 ret = 0;
5728 table->files[index] = file;
5729 }
5730
5731 if (ret) {
5732 for (i = 0; i < ctx->nr_user_files; i++) {
5733 file = io_file_from_index(ctx, i);
5734 if (file)
5735 fput(file);
5736 }
5737 for (i = 0; i < nr_tables; i++)
5738 kfree(ctx->file_data->table[i].files);
5739
5740 kfree(ctx->file_data->table);
5741 kfree(ctx->file_data);
5742 ctx->file_data = NULL;
5743 ctx->nr_user_files = 0;
5744 return ret;
5745 }
5746
5747 ret = io_sqe_files_scm(ctx);
5748 if (ret)
5749 io_sqe_files_unregister(ctx);
5750
5751 return ret;
5752}
5753
Jens Axboec3a31e62019-10-03 13:59:56 -06005754static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5755 int index)
5756{
5757#if defined(CONFIG_UNIX)
5758 struct sock *sock = ctx->ring_sock->sk;
5759 struct sk_buff_head *head = &sock->sk_receive_queue;
5760 struct sk_buff *skb;
5761
5762 /*
5763 * See if we can merge this file into an existing skb SCM_RIGHTS
5764 * file set. If there's no room, fall back to allocating a new skb
5765 * and filling it in.
5766 */
5767 spin_lock_irq(&head->lock);
5768 skb = skb_peek(head);
5769 if (skb) {
5770 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5771
5772 if (fpl->count < SCM_MAX_FD) {
5773 __skb_unlink(skb, head);
5774 spin_unlock_irq(&head->lock);
5775 fpl->fp[fpl->count] = get_file(file);
5776 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5777 fpl->count++;
5778 spin_lock_irq(&head->lock);
5779 __skb_queue_head(head, skb);
5780 } else {
5781 skb = NULL;
5782 }
5783 }
5784 spin_unlock_irq(&head->lock);
5785
5786 if (skb) {
5787 fput(file);
5788 return 0;
5789 }
5790
5791 return __io_sqe_files_scm(ctx, 1, index);
5792#else
5793 return 0;
5794#endif
5795}
5796
Jens Axboe05f3fb32019-12-09 11:22:50 -07005797static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005798{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005799 struct fixed_file_data *data;
5800
Jens Axboedd3db2a2020-02-26 10:23:43 -07005801 /*
5802 * Juggle reference to ensure we hit zero, if needed, so we can
5803 * switch back to percpu mode
5804 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005805 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07005806 percpu_ref_put(&data->refs);
5807 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005808}
5809
5810static bool io_queue_file_removal(struct fixed_file_data *data,
5811 struct file *file)
5812{
5813 struct io_file_put *pfile, pfile_stack;
5814 DECLARE_COMPLETION_ONSTACK(done);
5815
5816 /*
5817 * If we fail allocating the struct we need for doing async reomval
5818 * of this file, just punt to sync and wait for it.
5819 */
5820 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5821 if (!pfile) {
5822 pfile = &pfile_stack;
5823 pfile->done = &done;
5824 }
5825
5826 pfile->file = file;
5827 llist_add(&pfile->llist, &data->put_llist);
5828
5829 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07005830 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005831 wait_for_completion(&done);
5832 flush_work(&data->ref_work);
5833 return false;
5834 }
5835
5836 return true;
5837}
5838
5839static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5840 struct io_uring_files_update *up,
5841 unsigned nr_args)
5842{
5843 struct fixed_file_data *data = ctx->file_data;
5844 bool ref_switch = false;
5845 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005846 __s32 __user *fds;
5847 int fd, i, err;
5848 __u32 done;
5849
Jens Axboe05f3fb32019-12-09 11:22:50 -07005850 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005851 return -EOVERFLOW;
5852 if (done > ctx->nr_user_files)
5853 return -EINVAL;
5854
5855 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005856 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005857 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005858 struct fixed_file_table *table;
5859 unsigned index;
5860
Jens Axboec3a31e62019-10-03 13:59:56 -06005861 err = 0;
5862 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5863 err = -EFAULT;
5864 break;
5865 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005866 i = array_index_nospec(up->offset, ctx->nr_user_files);
5867 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005868 index = i & IORING_FILE_TABLE_MASK;
5869 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005870 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005871 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005872 if (io_queue_file_removal(data, file))
5873 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005874 }
5875 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005876 file = fget(fd);
5877 if (!file) {
5878 err = -EBADF;
5879 break;
5880 }
5881 /*
5882 * Don't allow io_uring instances to be registered. If
5883 * UNIX isn't enabled, then this causes a reference
5884 * cycle and this instance can never get freed. If UNIX
5885 * is enabled we'll handle it just fine, but there's
5886 * still no point in allowing a ring fd as it doesn't
5887 * support regular read/write anyway.
5888 */
5889 if (file->f_op == &io_uring_fops) {
5890 fput(file);
5891 err = -EBADF;
5892 break;
5893 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005894 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005895 err = io_sqe_file_register(ctx, file, i);
5896 if (err)
5897 break;
5898 }
5899 nr_args--;
5900 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005901 up->offset++;
5902 }
5903
Jens Axboedd3db2a2020-02-26 10:23:43 -07005904 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005905 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005906
5907 return done ? done : err;
5908}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005909static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5910 unsigned nr_args)
5911{
5912 struct io_uring_files_update up;
5913
5914 if (!ctx->file_data)
5915 return -ENXIO;
5916 if (!nr_args)
5917 return -EINVAL;
5918 if (copy_from_user(&up, arg, sizeof(up)))
5919 return -EFAULT;
5920 if (up.resv)
5921 return -EINVAL;
5922
5923 return __io_sqe_files_update(ctx, &up, nr_args);
5924}
Jens Axboec3a31e62019-10-03 13:59:56 -06005925
Jens Axboe7d723062019-11-12 22:31:31 -07005926static void io_put_work(struct io_wq_work *work)
5927{
5928 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5929
5930 io_put_req(req);
5931}
5932
5933static void io_get_work(struct io_wq_work *work)
5934{
5935 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5936
5937 refcount_inc(&req->refs);
5938}
5939
Pavel Begunkov24369c22020-01-28 03:15:48 +03005940static int io_init_wq_offload(struct io_ring_ctx *ctx,
5941 struct io_uring_params *p)
5942{
5943 struct io_wq_data data;
5944 struct fd f;
5945 struct io_ring_ctx *ctx_attach;
5946 unsigned int concurrency;
5947 int ret = 0;
5948
5949 data.user = ctx->user;
5950 data.get_work = io_get_work;
5951 data.put_work = io_put_work;
5952
5953 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5954 /* Do QD, or 4 * CPUS, whatever is smallest */
5955 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5956
5957 ctx->io_wq = io_wq_create(concurrency, &data);
5958 if (IS_ERR(ctx->io_wq)) {
5959 ret = PTR_ERR(ctx->io_wq);
5960 ctx->io_wq = NULL;
5961 }
5962 return ret;
5963 }
5964
5965 f = fdget(p->wq_fd);
5966 if (!f.file)
5967 return -EBADF;
5968
5969 if (f.file->f_op != &io_uring_fops) {
5970 ret = -EINVAL;
5971 goto out_fput;
5972 }
5973
5974 ctx_attach = f.file->private_data;
5975 /* @io_wq is protected by holding the fd */
5976 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5977 ret = -EINVAL;
5978 goto out_fput;
5979 }
5980
5981 ctx->io_wq = ctx_attach->io_wq;
5982out_fput:
5983 fdput(f);
5984 return ret;
5985}
5986
Jens Axboe6c271ce2019-01-10 11:22:30 -07005987static int io_sq_offload_start(struct io_ring_ctx *ctx,
5988 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005989{
5990 int ret;
5991
Jens Axboe6c271ce2019-01-10 11:22:30 -07005992 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005993 mmgrab(current->mm);
5994 ctx->sqo_mm = current->mm;
5995
Jens Axboe6c271ce2019-01-10 11:22:30 -07005996 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005997 ret = -EPERM;
5998 if (!capable(CAP_SYS_ADMIN))
5999 goto err;
6000
Jens Axboe917257d2019-04-13 09:28:55 -06006001 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6002 if (!ctx->sq_thread_idle)
6003 ctx->sq_thread_idle = HZ;
6004
Jens Axboe6c271ce2019-01-10 11:22:30 -07006005 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006006 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006007
Jens Axboe917257d2019-04-13 09:28:55 -06006008 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006009 if (cpu >= nr_cpu_ids)
6010 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006011 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006012 goto err;
6013
Jens Axboe6c271ce2019-01-10 11:22:30 -07006014 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6015 ctx, cpu,
6016 "io_uring-sq");
6017 } else {
6018 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6019 "io_uring-sq");
6020 }
6021 if (IS_ERR(ctx->sqo_thread)) {
6022 ret = PTR_ERR(ctx->sqo_thread);
6023 ctx->sqo_thread = NULL;
6024 goto err;
6025 }
6026 wake_up_process(ctx->sqo_thread);
6027 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6028 /* Can't have SQ_AFF without SQPOLL */
6029 ret = -EINVAL;
6030 goto err;
6031 }
6032
Pavel Begunkov24369c22020-01-28 03:15:48 +03006033 ret = io_init_wq_offload(ctx, p);
6034 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006035 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006036
6037 return 0;
6038err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006039 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006040 mmdrop(ctx->sqo_mm);
6041 ctx->sqo_mm = NULL;
6042 return ret;
6043}
6044
6045static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6046{
6047 atomic_long_sub(nr_pages, &user->locked_vm);
6048}
6049
6050static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6051{
6052 unsigned long page_limit, cur_pages, new_pages;
6053
6054 /* Don't allow more pages than we can safely lock */
6055 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6056
6057 do {
6058 cur_pages = atomic_long_read(&user->locked_vm);
6059 new_pages = cur_pages + nr_pages;
6060 if (new_pages > page_limit)
6061 return -ENOMEM;
6062 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6063 new_pages) != cur_pages);
6064
6065 return 0;
6066}
6067
6068static void io_mem_free(void *ptr)
6069{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006070 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006071
Mark Rutland52e04ef2019-04-30 17:30:21 +01006072 if (!ptr)
6073 return;
6074
6075 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006076 if (put_page_testzero(page))
6077 free_compound_page(page);
6078}
6079
6080static void *io_mem_alloc(size_t size)
6081{
6082 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6083 __GFP_NORETRY;
6084
6085 return (void *) __get_free_pages(gfp_flags, get_order(size));
6086}
6087
Hristo Venev75b28af2019-08-26 17:23:46 +00006088static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6089 size_t *sq_offset)
6090{
6091 struct io_rings *rings;
6092 size_t off, sq_array_size;
6093
6094 off = struct_size(rings, cqes, cq_entries);
6095 if (off == SIZE_MAX)
6096 return SIZE_MAX;
6097
6098#ifdef CONFIG_SMP
6099 off = ALIGN(off, SMP_CACHE_BYTES);
6100 if (off == 0)
6101 return SIZE_MAX;
6102#endif
6103
6104 sq_array_size = array_size(sizeof(u32), sq_entries);
6105 if (sq_array_size == SIZE_MAX)
6106 return SIZE_MAX;
6107
6108 if (check_add_overflow(off, sq_array_size, &off))
6109 return SIZE_MAX;
6110
6111 if (sq_offset)
6112 *sq_offset = off;
6113
6114 return off;
6115}
6116
Jens Axboe2b188cc2019-01-07 10:46:33 -07006117static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6118{
Hristo Venev75b28af2019-08-26 17:23:46 +00006119 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006120
Hristo Venev75b28af2019-08-26 17:23:46 +00006121 pages = (size_t)1 << get_order(
6122 rings_size(sq_entries, cq_entries, NULL));
6123 pages += (size_t)1 << get_order(
6124 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006125
Hristo Venev75b28af2019-08-26 17:23:46 +00006126 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006127}
6128
Jens Axboeedafcce2019-01-09 09:16:05 -07006129static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6130{
6131 int i, j;
6132
6133 if (!ctx->user_bufs)
6134 return -ENXIO;
6135
6136 for (i = 0; i < ctx->nr_user_bufs; i++) {
6137 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6138
6139 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006140 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006141
6142 if (ctx->account_mem)
6143 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006144 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006145 imu->nr_bvecs = 0;
6146 }
6147
6148 kfree(ctx->user_bufs);
6149 ctx->user_bufs = NULL;
6150 ctx->nr_user_bufs = 0;
6151 return 0;
6152}
6153
6154static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6155 void __user *arg, unsigned index)
6156{
6157 struct iovec __user *src;
6158
6159#ifdef CONFIG_COMPAT
6160 if (ctx->compat) {
6161 struct compat_iovec __user *ciovs;
6162 struct compat_iovec ciov;
6163
6164 ciovs = (struct compat_iovec __user *) arg;
6165 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6166 return -EFAULT;
6167
Jens Axboed55e5f52019-12-11 16:12:15 -07006168 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006169 dst->iov_len = ciov.iov_len;
6170 return 0;
6171 }
6172#endif
6173 src = (struct iovec __user *) arg;
6174 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6175 return -EFAULT;
6176 return 0;
6177}
6178
6179static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6180 unsigned nr_args)
6181{
6182 struct vm_area_struct **vmas = NULL;
6183 struct page **pages = NULL;
6184 int i, j, got_pages = 0;
6185 int ret = -EINVAL;
6186
6187 if (ctx->user_bufs)
6188 return -EBUSY;
6189 if (!nr_args || nr_args > UIO_MAXIOV)
6190 return -EINVAL;
6191
6192 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6193 GFP_KERNEL);
6194 if (!ctx->user_bufs)
6195 return -ENOMEM;
6196
6197 for (i = 0; i < nr_args; i++) {
6198 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6199 unsigned long off, start, end, ubuf;
6200 int pret, nr_pages;
6201 struct iovec iov;
6202 size_t size;
6203
6204 ret = io_copy_iov(ctx, &iov, arg, i);
6205 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006206 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006207
6208 /*
6209 * Don't impose further limits on the size and buffer
6210 * constraints here, we'll -EINVAL later when IO is
6211 * submitted if they are wrong.
6212 */
6213 ret = -EFAULT;
6214 if (!iov.iov_base || !iov.iov_len)
6215 goto err;
6216
6217 /* arbitrary limit, but we need something */
6218 if (iov.iov_len > SZ_1G)
6219 goto err;
6220
6221 ubuf = (unsigned long) iov.iov_base;
6222 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6223 start = ubuf >> PAGE_SHIFT;
6224 nr_pages = end - start;
6225
6226 if (ctx->account_mem) {
6227 ret = io_account_mem(ctx->user, nr_pages);
6228 if (ret)
6229 goto err;
6230 }
6231
6232 ret = 0;
6233 if (!pages || nr_pages > got_pages) {
6234 kfree(vmas);
6235 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006236 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006237 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006238 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006239 sizeof(struct vm_area_struct *),
6240 GFP_KERNEL);
6241 if (!pages || !vmas) {
6242 ret = -ENOMEM;
6243 if (ctx->account_mem)
6244 io_unaccount_mem(ctx->user, nr_pages);
6245 goto err;
6246 }
6247 got_pages = nr_pages;
6248 }
6249
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006250 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006251 GFP_KERNEL);
6252 ret = -ENOMEM;
6253 if (!imu->bvec) {
6254 if (ctx->account_mem)
6255 io_unaccount_mem(ctx->user, nr_pages);
6256 goto err;
6257 }
6258
6259 ret = 0;
6260 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006261 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006262 FOLL_WRITE | FOLL_LONGTERM,
6263 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006264 if (pret == nr_pages) {
6265 /* don't support file backed memory */
6266 for (j = 0; j < nr_pages; j++) {
6267 struct vm_area_struct *vma = vmas[j];
6268
6269 if (vma->vm_file &&
6270 !is_file_hugepages(vma->vm_file)) {
6271 ret = -EOPNOTSUPP;
6272 break;
6273 }
6274 }
6275 } else {
6276 ret = pret < 0 ? pret : -EFAULT;
6277 }
6278 up_read(&current->mm->mmap_sem);
6279 if (ret) {
6280 /*
6281 * if we did partial map, or found file backed vmas,
6282 * release any pages we did get
6283 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006284 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006285 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006286 if (ctx->account_mem)
6287 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006288 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006289 goto err;
6290 }
6291
6292 off = ubuf & ~PAGE_MASK;
6293 size = iov.iov_len;
6294 for (j = 0; j < nr_pages; j++) {
6295 size_t vec_len;
6296
6297 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6298 imu->bvec[j].bv_page = pages[j];
6299 imu->bvec[j].bv_len = vec_len;
6300 imu->bvec[j].bv_offset = off;
6301 off = 0;
6302 size -= vec_len;
6303 }
6304 /* store original address for later verification */
6305 imu->ubuf = ubuf;
6306 imu->len = iov.iov_len;
6307 imu->nr_bvecs = nr_pages;
6308
6309 ctx->nr_user_bufs++;
6310 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006311 kvfree(pages);
6312 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006313 return 0;
6314err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006315 kvfree(pages);
6316 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006317 io_sqe_buffer_unregister(ctx);
6318 return ret;
6319}
6320
Jens Axboe9b402842019-04-11 11:45:41 -06006321static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6322{
6323 __s32 __user *fds = arg;
6324 int fd;
6325
6326 if (ctx->cq_ev_fd)
6327 return -EBUSY;
6328
6329 if (copy_from_user(&fd, fds, sizeof(*fds)))
6330 return -EFAULT;
6331
6332 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6333 if (IS_ERR(ctx->cq_ev_fd)) {
6334 int ret = PTR_ERR(ctx->cq_ev_fd);
6335 ctx->cq_ev_fd = NULL;
6336 return ret;
6337 }
6338
6339 return 0;
6340}
6341
6342static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6343{
6344 if (ctx->cq_ev_fd) {
6345 eventfd_ctx_put(ctx->cq_ev_fd);
6346 ctx->cq_ev_fd = NULL;
6347 return 0;
6348 }
6349
6350 return -ENXIO;
6351}
6352
Jens Axboe2b188cc2019-01-07 10:46:33 -07006353static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6354{
Jens Axboe6b063142019-01-10 22:13:58 -07006355 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006356 if (ctx->sqo_mm)
6357 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006358
6359 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006360 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006361 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006362 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006363 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006364
Jens Axboe2b188cc2019-01-07 10:46:33 -07006365#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006366 if (ctx->ring_sock) {
6367 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006368 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006369 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006370#endif
6371
Hristo Venev75b28af2019-08-26 17:23:46 +00006372 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006373 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006374
6375 percpu_ref_exit(&ctx->refs);
6376 if (ctx->account_mem)
6377 io_unaccount_mem(ctx->user,
6378 ring_pages(ctx->sq_entries, ctx->cq_entries));
6379 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006380 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006381 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006382 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006383 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006384 kfree(ctx);
6385}
6386
6387static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6388{
6389 struct io_ring_ctx *ctx = file->private_data;
6390 __poll_t mask = 0;
6391
6392 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006393 /*
6394 * synchronizes with barrier from wq_has_sleeper call in
6395 * io_commit_cqring
6396 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006397 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006398 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6399 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006400 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006401 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006402 mask |= EPOLLIN | EPOLLRDNORM;
6403
6404 return mask;
6405}
6406
6407static int io_uring_fasync(int fd, struct file *file, int on)
6408{
6409 struct io_ring_ctx *ctx = file->private_data;
6410
6411 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6412}
6413
Jens Axboe071698e2020-01-28 10:04:42 -07006414static int io_remove_personalities(int id, void *p, void *data)
6415{
6416 struct io_ring_ctx *ctx = data;
6417 const struct cred *cred;
6418
6419 cred = idr_remove(&ctx->personality_idr, id);
6420 if (cred)
6421 put_cred(cred);
6422 return 0;
6423}
6424
Jens Axboe2b188cc2019-01-07 10:46:33 -07006425static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6426{
6427 mutex_lock(&ctx->uring_lock);
6428 percpu_ref_kill(&ctx->refs);
6429 mutex_unlock(&ctx->uring_lock);
6430
Jens Axboedf069d82020-02-04 16:48:34 -07006431 /*
6432 * Wait for sq thread to idle, if we have one. It won't spin on new
6433 * work after we've killed the ctx ref above. This is important to do
6434 * before we cancel existing commands, as the thread could otherwise
6435 * be queueing new work post that. If that's work we need to cancel,
6436 * it could cause shutdown to hang.
6437 */
6438 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6439 cpu_relax();
6440
Jens Axboe5262f562019-09-17 12:26:57 -06006441 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006442 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006443
6444 if (ctx->io_wq)
6445 io_wq_cancel_all(ctx->io_wq);
6446
Jens Axboedef596e2019-01-09 08:59:42 -07006447 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006448 /* if we failed setting up the ctx, we might not have any rings */
6449 if (ctx->rings)
6450 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006451 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006452 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006453 io_ring_ctx_free(ctx);
6454}
6455
6456static int io_uring_release(struct inode *inode, struct file *file)
6457{
6458 struct io_ring_ctx *ctx = file->private_data;
6459
6460 file->private_data = NULL;
6461 io_ring_ctx_wait_and_kill(ctx);
6462 return 0;
6463}
6464
Jens Axboefcb323c2019-10-24 12:39:47 -06006465static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6466 struct files_struct *files)
6467{
6468 struct io_kiocb *req;
6469 DEFINE_WAIT(wait);
6470
6471 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006472 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006473
6474 spin_lock_irq(&ctx->inflight_lock);
6475 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006476 if (req->work.files != files)
6477 continue;
6478 /* req is being completed, ignore */
6479 if (!refcount_inc_not_zero(&req->refs))
6480 continue;
6481 cancel_req = req;
6482 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006483 }
Jens Axboe768134d2019-11-10 20:30:53 -07006484 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006485 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006486 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006487 spin_unlock_irq(&ctx->inflight_lock);
6488
Jens Axboe768134d2019-11-10 20:30:53 -07006489 /* We need to keep going until we don't find a matching req */
6490 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006491 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006492
Jens Axboe2ca10252020-02-13 17:17:35 -07006493 if (cancel_req->flags & REQ_F_OVERFLOW) {
6494 spin_lock_irq(&ctx->completion_lock);
6495 list_del(&cancel_req->list);
6496 cancel_req->flags &= ~REQ_F_OVERFLOW;
6497 if (list_empty(&ctx->cq_overflow_list)) {
6498 clear_bit(0, &ctx->sq_check_overflow);
6499 clear_bit(0, &ctx->cq_check_overflow);
6500 }
6501 spin_unlock_irq(&ctx->completion_lock);
6502
6503 WRITE_ONCE(ctx->rings->cq_overflow,
6504 atomic_inc_return(&ctx->cached_cq_overflow));
6505
6506 /*
6507 * Put inflight ref and overflow ref. If that's
6508 * all we had, then we're done with this request.
6509 */
6510 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6511 io_put_req(cancel_req);
6512 continue;
6513 }
6514 }
6515
Bob Liu2f6d9b92019-11-13 18:06:24 +08006516 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6517 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006518 schedule();
6519 }
Jens Axboe768134d2019-11-10 20:30:53 -07006520 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006521}
6522
6523static int io_uring_flush(struct file *file, void *data)
6524{
6525 struct io_ring_ctx *ctx = file->private_data;
6526
6527 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006528
6529 /*
6530 * If the task is going away, cancel work it may have pending
6531 */
6532 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6533 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6534
Jens Axboefcb323c2019-10-24 12:39:47 -06006535 return 0;
6536}
6537
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006538static void *io_uring_validate_mmap_request(struct file *file,
6539 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006540{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006541 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006542 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006543 struct page *page;
6544 void *ptr;
6545
6546 switch (offset) {
6547 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006548 case IORING_OFF_CQ_RING:
6549 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006550 break;
6551 case IORING_OFF_SQES:
6552 ptr = ctx->sq_sqes;
6553 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006554 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006555 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006556 }
6557
6558 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006559 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006560 return ERR_PTR(-EINVAL);
6561
6562 return ptr;
6563}
6564
6565#ifdef CONFIG_MMU
6566
6567static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6568{
6569 size_t sz = vma->vm_end - vma->vm_start;
6570 unsigned long pfn;
6571 void *ptr;
6572
6573 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6574 if (IS_ERR(ptr))
6575 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006576
6577 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6578 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6579}
6580
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006581#else /* !CONFIG_MMU */
6582
6583static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6584{
6585 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6586}
6587
6588static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6589{
6590 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6591}
6592
6593static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6594 unsigned long addr, unsigned long len,
6595 unsigned long pgoff, unsigned long flags)
6596{
6597 void *ptr;
6598
6599 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6600 if (IS_ERR(ptr))
6601 return PTR_ERR(ptr);
6602
6603 return (unsigned long) ptr;
6604}
6605
6606#endif /* !CONFIG_MMU */
6607
Jens Axboe2b188cc2019-01-07 10:46:33 -07006608SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6609 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6610 size_t, sigsz)
6611{
6612 struct io_ring_ctx *ctx;
6613 long ret = -EBADF;
6614 int submitted = 0;
6615 struct fd f;
6616
Jens Axboe6c271ce2019-01-10 11:22:30 -07006617 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006618 return -EINVAL;
6619
6620 f = fdget(fd);
6621 if (!f.file)
6622 return -EBADF;
6623
6624 ret = -EOPNOTSUPP;
6625 if (f.file->f_op != &io_uring_fops)
6626 goto out_fput;
6627
6628 ret = -ENXIO;
6629 ctx = f.file->private_data;
6630 if (!percpu_ref_tryget(&ctx->refs))
6631 goto out_fput;
6632
Jens Axboe6c271ce2019-01-10 11:22:30 -07006633 /*
6634 * For SQ polling, the thread will do all submissions and completions.
6635 * Just return the requested submit count, and wake the thread if
6636 * we were asked to.
6637 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006638 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006639 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006640 if (!list_empty_careful(&ctx->cq_overflow_list))
6641 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006642 if (flags & IORING_ENTER_SQ_WAKEUP)
6643 wake_up(&ctx->sqo_wait);
6644 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006645 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006646 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006647
6648 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006649 /* already have mm, so io_submit_sqes() won't try to grab it */
6650 cur_mm = ctx->sqo_mm;
6651 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6652 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006653 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006654
6655 if (submitted != to_submit)
6656 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006657 }
6658 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006659 unsigned nr_events = 0;
6660
Jens Axboe2b188cc2019-01-07 10:46:33 -07006661 min_complete = min(min_complete, ctx->cq_entries);
6662
Jens Axboedef596e2019-01-09 08:59:42 -07006663 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006664 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006665 } else {
6666 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6667 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006668 }
6669
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006670out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006671 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006672out_fput:
6673 fdput(f);
6674 return submitted ? submitted : ret;
6675}
6676
Tobias Klauserbebdb652020-02-26 18:38:32 +01006677#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006678static int io_uring_show_cred(int id, void *p, void *data)
6679{
6680 const struct cred *cred = p;
6681 struct seq_file *m = data;
6682 struct user_namespace *uns = seq_user_ns(m);
6683 struct group_info *gi;
6684 kernel_cap_t cap;
6685 unsigned __capi;
6686 int g;
6687
6688 seq_printf(m, "%5d\n", id);
6689 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6690 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6691 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6692 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6693 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6694 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6695 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6696 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6697 seq_puts(m, "\n\tGroups:\t");
6698 gi = cred->group_info;
6699 for (g = 0; g < gi->ngroups; g++) {
6700 seq_put_decimal_ull(m, g ? " " : "",
6701 from_kgid_munged(uns, gi->gid[g]));
6702 }
6703 seq_puts(m, "\n\tCapEff:\t");
6704 cap = cred->cap_effective;
6705 CAP_FOR_EACH_U32(__capi)
6706 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6707 seq_putc(m, '\n');
6708 return 0;
6709}
6710
6711static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6712{
6713 int i;
6714
6715 mutex_lock(&ctx->uring_lock);
6716 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6717 for (i = 0; i < ctx->nr_user_files; i++) {
6718 struct fixed_file_table *table;
6719 struct file *f;
6720
6721 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6722 f = table->files[i & IORING_FILE_TABLE_MASK];
6723 if (f)
6724 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6725 else
6726 seq_printf(m, "%5u: <none>\n", i);
6727 }
6728 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6729 for (i = 0; i < ctx->nr_user_bufs; i++) {
6730 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6731
6732 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6733 (unsigned int) buf->len);
6734 }
6735 if (!idr_is_empty(&ctx->personality_idr)) {
6736 seq_printf(m, "Personalities:\n");
6737 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6738 }
6739 mutex_unlock(&ctx->uring_lock);
6740}
6741
6742static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6743{
6744 struct io_ring_ctx *ctx = f->private_data;
6745
6746 if (percpu_ref_tryget(&ctx->refs)) {
6747 __io_uring_show_fdinfo(ctx, m);
6748 percpu_ref_put(&ctx->refs);
6749 }
6750}
Tobias Klauserbebdb652020-02-26 18:38:32 +01006751#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07006752
Jens Axboe2b188cc2019-01-07 10:46:33 -07006753static const struct file_operations io_uring_fops = {
6754 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006755 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006756 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006757#ifndef CONFIG_MMU
6758 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6759 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6760#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006761 .poll = io_uring_poll,
6762 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006763#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006764 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006765#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006766};
6767
6768static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6769 struct io_uring_params *p)
6770{
Hristo Venev75b28af2019-08-26 17:23:46 +00006771 struct io_rings *rings;
6772 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006773
Hristo Venev75b28af2019-08-26 17:23:46 +00006774 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6775 if (size == SIZE_MAX)
6776 return -EOVERFLOW;
6777
6778 rings = io_mem_alloc(size);
6779 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006780 return -ENOMEM;
6781
Hristo Venev75b28af2019-08-26 17:23:46 +00006782 ctx->rings = rings;
6783 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6784 rings->sq_ring_mask = p->sq_entries - 1;
6785 rings->cq_ring_mask = p->cq_entries - 1;
6786 rings->sq_ring_entries = p->sq_entries;
6787 rings->cq_ring_entries = p->cq_entries;
6788 ctx->sq_mask = rings->sq_ring_mask;
6789 ctx->cq_mask = rings->cq_ring_mask;
6790 ctx->sq_entries = rings->sq_ring_entries;
6791 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006792
6793 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006794 if (size == SIZE_MAX) {
6795 io_mem_free(ctx->rings);
6796 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006797 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006798 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006799
6800 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006801 if (!ctx->sq_sqes) {
6802 io_mem_free(ctx->rings);
6803 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006804 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006805 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006806
Jens Axboe2b188cc2019-01-07 10:46:33 -07006807 return 0;
6808}
6809
6810/*
6811 * Allocate an anonymous fd, this is what constitutes the application
6812 * visible backing of an io_uring instance. The application mmaps this
6813 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6814 * we have to tie this fd to a socket for file garbage collection purposes.
6815 */
6816static int io_uring_get_fd(struct io_ring_ctx *ctx)
6817{
6818 struct file *file;
6819 int ret;
6820
6821#if defined(CONFIG_UNIX)
6822 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6823 &ctx->ring_sock);
6824 if (ret)
6825 return ret;
6826#endif
6827
6828 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6829 if (ret < 0)
6830 goto err;
6831
6832 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6833 O_RDWR | O_CLOEXEC);
6834 if (IS_ERR(file)) {
6835 put_unused_fd(ret);
6836 ret = PTR_ERR(file);
6837 goto err;
6838 }
6839
6840#if defined(CONFIG_UNIX)
6841 ctx->ring_sock->file = file;
6842#endif
6843 fd_install(ret, file);
6844 return ret;
6845err:
6846#if defined(CONFIG_UNIX)
6847 sock_release(ctx->ring_sock);
6848 ctx->ring_sock = NULL;
6849#endif
6850 return ret;
6851}
6852
6853static int io_uring_create(unsigned entries, struct io_uring_params *p)
6854{
6855 struct user_struct *user = NULL;
6856 struct io_ring_ctx *ctx;
6857 bool account_mem;
6858 int ret;
6859
Jens Axboe8110c1a2019-12-28 15:39:54 -07006860 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006861 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006862 if (entries > IORING_MAX_ENTRIES) {
6863 if (!(p->flags & IORING_SETUP_CLAMP))
6864 return -EINVAL;
6865 entries = IORING_MAX_ENTRIES;
6866 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006867
6868 /*
6869 * Use twice as many entries for the CQ ring. It's possible for the
6870 * application to drive a higher depth than the size of the SQ ring,
6871 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006872 * some flexibility in overcommitting a bit. If the application has
6873 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6874 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006875 */
6876 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006877 if (p->flags & IORING_SETUP_CQSIZE) {
6878 /*
6879 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6880 * to a power-of-two, if it isn't already. We do NOT impose
6881 * any cq vs sq ring sizing.
6882 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006883 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006884 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006885 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6886 if (!(p->flags & IORING_SETUP_CLAMP))
6887 return -EINVAL;
6888 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6889 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006890 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6891 } else {
6892 p->cq_entries = 2 * p->sq_entries;
6893 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006894
6895 user = get_uid(current_user());
6896 account_mem = !capable(CAP_IPC_LOCK);
6897
6898 if (account_mem) {
6899 ret = io_account_mem(user,
6900 ring_pages(p->sq_entries, p->cq_entries));
6901 if (ret) {
6902 free_uid(user);
6903 return ret;
6904 }
6905 }
6906
6907 ctx = io_ring_ctx_alloc(p);
6908 if (!ctx) {
6909 if (account_mem)
6910 io_unaccount_mem(user, ring_pages(p->sq_entries,
6911 p->cq_entries));
6912 free_uid(user);
6913 return -ENOMEM;
6914 }
6915 ctx->compat = in_compat_syscall();
6916 ctx->account_mem = account_mem;
6917 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006918 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006919
6920 ret = io_allocate_scq_urings(ctx, p);
6921 if (ret)
6922 goto err;
6923
Jens Axboe6c271ce2019-01-10 11:22:30 -07006924 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006925 if (ret)
6926 goto err;
6927
Jens Axboe2b188cc2019-01-07 10:46:33 -07006928 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006929 p->sq_off.head = offsetof(struct io_rings, sq.head);
6930 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6931 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6932 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6933 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6934 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6935 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006936
6937 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006938 p->cq_off.head = offsetof(struct io_rings, cq.head);
6939 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6940 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6941 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6942 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6943 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006944
Jens Axboe044c1ab2019-10-28 09:15:33 -06006945 /*
6946 * Install ring fd as the very last thing, so we don't risk someone
6947 * having closed it before we finish setup
6948 */
6949 ret = io_uring_get_fd(ctx);
6950 if (ret < 0)
6951 goto err;
6952
Jens Axboeda8c9692019-12-02 18:51:26 -07006953 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006954 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6955 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006956 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006957 return ret;
6958err:
6959 io_ring_ctx_wait_and_kill(ctx);
6960 return ret;
6961}
6962
6963/*
6964 * Sets up an aio uring context, and returns the fd. Applications asks for a
6965 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6966 * params structure passed in.
6967 */
6968static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6969{
6970 struct io_uring_params p;
6971 long ret;
6972 int i;
6973
6974 if (copy_from_user(&p, params, sizeof(p)))
6975 return -EFAULT;
6976 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6977 if (p.resv[i])
6978 return -EINVAL;
6979 }
6980
Jens Axboe6c271ce2019-01-10 11:22:30 -07006981 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006982 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006983 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006984 return -EINVAL;
6985
6986 ret = io_uring_create(entries, &p);
6987 if (ret < 0)
6988 return ret;
6989
6990 if (copy_to_user(params, &p, sizeof(p)))
6991 return -EFAULT;
6992
6993 return ret;
6994}
6995
6996SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6997 struct io_uring_params __user *, params)
6998{
6999 return io_uring_setup(entries, params);
7000}
7001
Jens Axboe66f4af92020-01-16 15:36:52 -07007002static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7003{
7004 struct io_uring_probe *p;
7005 size_t size;
7006 int i, ret;
7007
7008 size = struct_size(p, ops, nr_args);
7009 if (size == SIZE_MAX)
7010 return -EOVERFLOW;
7011 p = kzalloc(size, GFP_KERNEL);
7012 if (!p)
7013 return -ENOMEM;
7014
7015 ret = -EFAULT;
7016 if (copy_from_user(p, arg, size))
7017 goto out;
7018 ret = -EINVAL;
7019 if (memchr_inv(p, 0, size))
7020 goto out;
7021
7022 p->last_op = IORING_OP_LAST - 1;
7023 if (nr_args > IORING_OP_LAST)
7024 nr_args = IORING_OP_LAST;
7025
7026 for (i = 0; i < nr_args; i++) {
7027 p->ops[i].op = i;
7028 if (!io_op_defs[i].not_supported)
7029 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7030 }
7031 p->ops_len = i;
7032
7033 ret = 0;
7034 if (copy_to_user(arg, p, size))
7035 ret = -EFAULT;
7036out:
7037 kfree(p);
7038 return ret;
7039}
7040
Jens Axboe071698e2020-01-28 10:04:42 -07007041static int io_register_personality(struct io_ring_ctx *ctx)
7042{
7043 const struct cred *creds = get_current_cred();
7044 int id;
7045
7046 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7047 USHRT_MAX, GFP_KERNEL);
7048 if (id < 0)
7049 put_cred(creds);
7050 return id;
7051}
7052
7053static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7054{
7055 const struct cred *old_creds;
7056
7057 old_creds = idr_remove(&ctx->personality_idr, id);
7058 if (old_creds) {
7059 put_cred(old_creds);
7060 return 0;
7061 }
7062
7063 return -EINVAL;
7064}
7065
7066static bool io_register_op_must_quiesce(int op)
7067{
7068 switch (op) {
7069 case IORING_UNREGISTER_FILES:
7070 case IORING_REGISTER_FILES_UPDATE:
7071 case IORING_REGISTER_PROBE:
7072 case IORING_REGISTER_PERSONALITY:
7073 case IORING_UNREGISTER_PERSONALITY:
7074 return false;
7075 default:
7076 return true;
7077 }
7078}
7079
Jens Axboeedafcce2019-01-09 09:16:05 -07007080static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7081 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007082 __releases(ctx->uring_lock)
7083 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007084{
7085 int ret;
7086
Jens Axboe35fa71a2019-04-22 10:23:23 -06007087 /*
7088 * We're inside the ring mutex, if the ref is already dying, then
7089 * someone else killed the ctx or is already going through
7090 * io_uring_register().
7091 */
7092 if (percpu_ref_is_dying(&ctx->refs))
7093 return -ENXIO;
7094
Jens Axboe071698e2020-01-28 10:04:42 -07007095 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007096 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007097
Jens Axboe05f3fb32019-12-09 11:22:50 -07007098 /*
7099 * Drop uring mutex before waiting for references to exit. If
7100 * another thread is currently inside io_uring_enter() it might
7101 * need to grab the uring_lock to make progress. If we hold it
7102 * here across the drain wait, then we can deadlock. It's safe
7103 * to drop the mutex here, since no new references will come in
7104 * after we've killed the percpu ref.
7105 */
7106 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007107 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007108 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007109 if (ret) {
7110 percpu_ref_resurrect(&ctx->refs);
7111 ret = -EINTR;
7112 goto out;
7113 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007114 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007115
7116 switch (opcode) {
7117 case IORING_REGISTER_BUFFERS:
7118 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7119 break;
7120 case IORING_UNREGISTER_BUFFERS:
7121 ret = -EINVAL;
7122 if (arg || nr_args)
7123 break;
7124 ret = io_sqe_buffer_unregister(ctx);
7125 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007126 case IORING_REGISTER_FILES:
7127 ret = io_sqe_files_register(ctx, arg, nr_args);
7128 break;
7129 case IORING_UNREGISTER_FILES:
7130 ret = -EINVAL;
7131 if (arg || nr_args)
7132 break;
7133 ret = io_sqe_files_unregister(ctx);
7134 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007135 case IORING_REGISTER_FILES_UPDATE:
7136 ret = io_sqe_files_update(ctx, arg, nr_args);
7137 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007138 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007139 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007140 ret = -EINVAL;
7141 if (nr_args != 1)
7142 break;
7143 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007144 if (ret)
7145 break;
7146 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7147 ctx->eventfd_async = 1;
7148 else
7149 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007150 break;
7151 case IORING_UNREGISTER_EVENTFD:
7152 ret = -EINVAL;
7153 if (arg || nr_args)
7154 break;
7155 ret = io_eventfd_unregister(ctx);
7156 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007157 case IORING_REGISTER_PROBE:
7158 ret = -EINVAL;
7159 if (!arg || nr_args > 256)
7160 break;
7161 ret = io_probe(ctx, arg, nr_args);
7162 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007163 case IORING_REGISTER_PERSONALITY:
7164 ret = -EINVAL;
7165 if (arg || nr_args)
7166 break;
7167 ret = io_register_personality(ctx);
7168 break;
7169 case IORING_UNREGISTER_PERSONALITY:
7170 ret = -EINVAL;
7171 if (arg)
7172 break;
7173 ret = io_unregister_personality(ctx, nr_args);
7174 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007175 default:
7176 ret = -EINVAL;
7177 break;
7178 }
7179
Jens Axboe071698e2020-01-28 10:04:42 -07007180 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007181 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007182 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007183out:
7184 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007185 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007186 return ret;
7187}
7188
7189SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7190 void __user *, arg, unsigned int, nr_args)
7191{
7192 struct io_ring_ctx *ctx;
7193 long ret = -EBADF;
7194 struct fd f;
7195
7196 f = fdget(fd);
7197 if (!f.file)
7198 return -EBADF;
7199
7200 ret = -EOPNOTSUPP;
7201 if (f.file->f_op != &io_uring_fops)
7202 goto out_fput;
7203
7204 ctx = f.file->private_data;
7205
7206 mutex_lock(&ctx->uring_lock);
7207 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7208 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007209 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7210 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007211out_fput:
7212 fdput(f);
7213 return ret;
7214}
7215
Jens Axboe2b188cc2019-01-07 10:46:33 -07007216static int __init io_uring_init(void)
7217{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007218#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7219 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7220 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7221} while (0)
7222
7223#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7224 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7225 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7226 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7227 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7228 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7229 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7230 BUILD_BUG_SQE_ELEM(8, __u64, off);
7231 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7232 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7233 BUILD_BUG_SQE_ELEM(24, __u32, len);
7234 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7235 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7236 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7237 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7238 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7239 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7240 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7241 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7242 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7243 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7244 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7245 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7246 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7247 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7248 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7249 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7250
Jens Axboed3656342019-12-18 09:50:26 -07007251 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007252 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7253 return 0;
7254};
7255__initcall(io_uring_init);