blob: 0d8d0e217847072803ec0dd984d8b628a76790fc [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 Axboe2b188cc2019-01-07 10:46:33 -070077
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020078#define CREATE_TRACE_POINTS
79#include <trace/events/io_uring.h>
80
Jens Axboe2b188cc2019-01-07 10:46:33 -070081#include <uapi/linux/io_uring.h>
82
83#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060084#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070085
Daniel Xu5277dea2019-09-14 14:23:45 -070086#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060087#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060088
89/*
90 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
91 */
92#define IORING_FILE_TABLE_SHIFT 9
93#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
94#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
95#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070096
97struct io_uring {
98 u32 head ____cacheline_aligned_in_smp;
99 u32 tail ____cacheline_aligned_in_smp;
100};
101
Stefan Bühler1e84b972019-04-24 23:54:16 +0200102/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000103 * This data is shared with the application through the mmap at offsets
104 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 *
106 * The offsets to the member fields are published through struct
107 * io_sqring_offsets when calling io_uring_setup.
108 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000109struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 /*
111 * Head and tail offsets into the ring; the offsets need to be
112 * masked to get valid indices.
113 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000114 * The kernel controls head of the sq ring and the tail of the cq ring,
115 * and the application controls tail of the sq ring and the head of the
116 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200117 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000120 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 * ring_entries - 1)
122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 u32 sq_ring_mask, cq_ring_mask;
124 /* Ring sizes (constant, power of 2) */
125 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 /*
127 * Number of invalid entries dropped by the kernel due to
128 * invalid index stored in array
129 *
130 * Written by the kernel, shouldn't be modified by the
131 * application (i.e. get number of "new events" by comparing to
132 * cached value).
133 *
134 * After a new SQ head value was read by the application this
135 * counter includes all submissions that were dropped reaching
136 * the new SQ head (and possibly more).
137 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000138 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200139 /*
140 * Runtime flags
141 *
142 * Written by the kernel, shouldn't be modified by the
143 * application.
144 *
145 * The application needs a full memory barrier before checking
146 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
147 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000148 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 /*
150 * Number of completion events lost because the queue was full;
151 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800152 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200153 * the completion queue.
154 *
155 * Written by the kernel, shouldn't be modified by the
156 * application (i.e. get number of "new events" by comparing to
157 * cached value).
158 *
159 * As completion events come in out of order this counter is not
160 * ordered with any other data.
161 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000162 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200163 /*
164 * Ring buffer of completion events.
165 *
166 * The kernel writes completion events fresh every time they are
167 * produced, so the application is allowed to modify pending
168 * entries.
169 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000170 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700171};
172
Jens Axboeedafcce2019-01-09 09:16:05 -0700173struct io_mapped_ubuf {
174 u64 ubuf;
175 size_t len;
176 struct bio_vec *bvec;
177 unsigned int nr_bvecs;
178};
179
Jens Axboe65e19f52019-10-26 07:20:21 -0600180struct fixed_file_table {
181 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700182};
183
Jens Axboe05f3fb32019-12-09 11:22:50 -0700184enum {
185 FFD_F_ATOMIC,
186};
187
188struct fixed_file_data {
189 struct fixed_file_table *table;
190 struct io_ring_ctx *ctx;
191
192 struct percpu_ref refs;
193 struct llist_head put_llist;
194 unsigned long state;
195 struct work_struct ref_work;
196 struct completion done;
197};
198
Jens Axboe2b188cc2019-01-07 10:46:33 -0700199struct io_ring_ctx {
200 struct {
201 struct percpu_ref refs;
202 } ____cacheline_aligned_in_smp;
203
204 struct {
205 unsigned int flags;
Jens Axboe69b3e542020-01-08 11:01:46 -0700206 int compat: 1;
207 int account_mem: 1;
208 int cq_overflow_flushed: 1;
209 int drain_next: 1;
Jens Axboef2842ab2020-01-08 11:04:00 -0700210 int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700211
Hristo Venev75b28af2019-08-26 17:23:46 +0000212 /*
213 * Ring buffer of indices into array of io_uring_sqe, which is
214 * mmapped by the application using the IORING_OFF_SQES offset.
215 *
216 * This indirection could e.g. be used to assign fixed
217 * io_uring_sqe entries to operations and only submit them to
218 * the queue when needed.
219 *
220 * The kernel modifies neither the indices array nor the entries
221 * array.
222 */
223 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700224 unsigned cached_sq_head;
225 unsigned sq_entries;
226 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700227 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600228 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700229 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700230 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600231
232 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600233 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700234 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700235
Jens Axboefcb323c2019-10-24 12:39:47 -0600236 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700237 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700238 } ____cacheline_aligned_in_smp;
239
Hristo Venev75b28af2019-08-26 17:23:46 +0000240 struct io_rings *rings;
241
Jens Axboe2b188cc2019-01-07 10:46:33 -0700242 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600243 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700244 struct task_struct *sqo_thread; /* if using sq thread polling */
245 struct mm_struct *sqo_mm;
246 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700247
Jens Axboe6b063142019-01-10 22:13:58 -0700248 /*
249 * If used, fixed file set. Writers must ensure that ->refs is dead,
250 * readers must ensure that ->refs is alive as long as the file* is
251 * used. Only updated through io_uring_register(2).
252 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700253 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700254 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300255 int ring_fd;
256 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700257
Jens Axboeedafcce2019-01-09 09:16:05 -0700258 /* if used, fixed mapped user buffers */
259 unsigned nr_user_bufs;
260 struct io_mapped_ubuf *user_bufs;
261
Jens Axboe2b188cc2019-01-07 10:46:33 -0700262 struct user_struct *user;
263
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700264 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700265
Jens Axboe206aefd2019-11-07 18:27:42 -0700266 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
267 struct completion *completions;
268
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700269 /* if all else fails... */
270 struct io_kiocb *fallback_req;
271
Jens Axboe206aefd2019-11-07 18:27:42 -0700272#if defined(CONFIG_UNIX)
273 struct socket *ring_sock;
274#endif
275
Jens Axboe071698e2020-01-28 10:04:42 -0700276 struct idr personality_idr;
277
Jens Axboe206aefd2019-11-07 18:27:42 -0700278 struct {
279 unsigned cached_cq_tail;
280 unsigned cq_entries;
281 unsigned cq_mask;
282 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700283 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700284 struct wait_queue_head cq_wait;
285 struct fasync_struct *cq_fasync;
286 struct eventfd_ctx *cq_ev_fd;
287 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700288
289 struct {
290 struct mutex uring_lock;
291 wait_queue_head_t wait;
292 } ____cacheline_aligned_in_smp;
293
294 struct {
295 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700296 struct llist_head poll_llist;
297
Jens Axboedef596e2019-01-09 08:59:42 -0700298 /*
299 * ->poll_list is protected by the ctx->uring_lock for
300 * io_uring instances that don't use IORING_SETUP_SQPOLL.
301 * For SQPOLL, only the single threaded io_sq_thread() will
302 * manipulate the list, hence no extra locking is needed there.
303 */
304 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700305 struct hlist_head *cancel_hash;
306 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700307 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600308
309 spinlock_t inflight_lock;
310 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700311 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700312};
313
Jens Axboe09bb8392019-03-13 12:39:28 -0600314/*
315 * First field must be the file pointer in all the
316 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
317 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700318struct io_poll_iocb {
319 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700320 union {
321 struct wait_queue_head *head;
322 u64 addr;
323 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700324 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600325 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700326 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700327 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700328};
329
Jens Axboeb5dba592019-12-11 14:02:38 -0700330struct io_close {
331 struct file *file;
332 struct file *put_file;
333 int fd;
334};
335
Jens Axboead8a48a2019-11-15 08:49:11 -0700336struct io_timeout_data {
337 struct io_kiocb *req;
338 struct hrtimer timer;
339 struct timespec64 ts;
340 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300341 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700342};
343
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700344struct io_accept {
345 struct file *file;
346 struct sockaddr __user *addr;
347 int __user *addr_len;
348 int flags;
349};
350
351struct io_sync {
352 struct file *file;
353 loff_t len;
354 loff_t off;
355 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700356 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700357};
358
Jens Axboefbf23842019-12-17 18:45:56 -0700359struct io_cancel {
360 struct file *file;
361 u64 addr;
362};
363
Jens Axboeb29472e2019-12-17 18:50:29 -0700364struct io_timeout {
365 struct file *file;
366 u64 addr;
367 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700368 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700369};
370
Jens Axboe9adbd452019-12-20 08:45:55 -0700371struct io_rw {
372 /* NOTE: kiocb has the file as the first member, so don't do it here */
373 struct kiocb kiocb;
374 u64 addr;
375 u64 len;
376};
377
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700378struct io_connect {
379 struct file *file;
380 struct sockaddr __user *addr;
381 int addr_len;
382};
383
Jens Axboee47293f2019-12-20 08:58:21 -0700384struct io_sr_msg {
385 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700386 union {
387 struct user_msghdr __user *msg;
388 void __user *buf;
389 };
Jens Axboee47293f2019-12-20 08:58:21 -0700390 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700391 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700392};
393
Jens Axboe15b71ab2019-12-11 11:20:36 -0700394struct io_open {
395 struct file *file;
396 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700397 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700398 unsigned mask;
399 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700400 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700401 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700402 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700403};
404
Jens Axboe05f3fb32019-12-09 11:22:50 -0700405struct io_files_update {
406 struct file *file;
407 u64 arg;
408 u32 nr_args;
409 u32 offset;
410};
411
Jens Axboe4840e412019-12-25 22:03:45 -0700412struct io_fadvise {
413 struct file *file;
414 u64 offset;
415 u32 len;
416 u32 advice;
417};
418
Jens Axboec1ca7572019-12-25 22:18:28 -0700419struct io_madvise {
420 struct file *file;
421 u64 addr;
422 u32 len;
423 u32 advice;
424};
425
Jens Axboef499a022019-12-02 16:28:46 -0700426struct io_async_connect {
427 struct sockaddr_storage address;
428};
429
Jens Axboe03b12302019-12-02 18:50:25 -0700430struct io_async_msghdr {
431 struct iovec fast_iov[UIO_FASTIOV];
432 struct iovec *iov;
433 struct sockaddr __user *uaddr;
434 struct msghdr msg;
435};
436
Jens Axboef67676d2019-12-02 11:03:47 -0700437struct io_async_rw {
438 struct iovec fast_iov[UIO_FASTIOV];
439 struct iovec *iov;
440 ssize_t nr_segs;
441 ssize_t size;
442};
443
Jens Axboe15b71ab2019-12-11 11:20:36 -0700444struct io_async_open {
445 struct filename *filename;
446};
447
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700448struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700449 union {
450 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700451 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700452 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700453 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700454 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700455 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700456};
457
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300458enum {
459 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
460 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
461 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
462 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
463 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
464
465 REQ_F_LINK_NEXT_BIT,
466 REQ_F_FAIL_LINK_BIT,
467 REQ_F_INFLIGHT_BIT,
468 REQ_F_CUR_POS_BIT,
469 REQ_F_NOWAIT_BIT,
470 REQ_F_IOPOLL_COMPLETED_BIT,
471 REQ_F_LINK_TIMEOUT_BIT,
472 REQ_F_TIMEOUT_BIT,
473 REQ_F_ISREG_BIT,
474 REQ_F_MUST_PUNT_BIT,
475 REQ_F_TIMEOUT_NOSEQ_BIT,
476 REQ_F_COMP_LOCKED_BIT,
477};
478
479enum {
480 /* ctx owns file */
481 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
482 /* drain existing IO first */
483 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
484 /* linked sqes */
485 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
486 /* doesn't sever on completion < 0 */
487 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
488 /* IOSQE_ASYNC */
489 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
490
491 /* already grabbed next link */
492 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
493 /* fail rest of links */
494 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
495 /* on inflight list */
496 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
497 /* read/write uses file position */
498 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
499 /* must not punt to workers */
500 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
501 /* polled IO has completed */
502 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
503 /* has linked timeout */
504 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
505 /* timeout request */
506 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
507 /* regular file */
508 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
509 /* must be punted even for NONBLOCK */
510 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
511 /* no timeout sequence */
512 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
513 /* completion under lock */
514 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
515};
516
Jens Axboe09bb8392019-03-13 12:39:28 -0600517/*
518 * NOTE! Each of the iocb union members has the file pointer
519 * as the first entry in their struct definition. So you can
520 * access the file pointer through any of the sub-structs,
521 * or directly as just 'ki_filp' in this struct.
522 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700523struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700524 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600525 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700526 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700527 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700528 struct io_accept accept;
529 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700530 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700531 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700532 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700533 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700534 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700535 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700536 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700537 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700538 struct io_madvise madvise;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700539 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700540
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700541 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300542 /*
543 * llist_node is only used for poll deferred completions
544 */
545 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300546 bool has_user;
547 bool in_async;
548 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700549 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700550
551 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700552 union {
553 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700554 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700555 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600556 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700557 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700558 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700559 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600560 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600561 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700562
Jens Axboefcb323c2019-10-24 12:39:47 -0600563 struct list_head inflight_entry;
564
Jens Axboe561fb042019-10-24 07:25:42 -0600565 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700566};
567
568#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700569#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700570
Jens Axboe9a56a232019-01-09 09:06:50 -0700571struct io_submit_state {
572 struct blk_plug plug;
573
574 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700575 * io_kiocb alloc cache
576 */
577 void *reqs[IO_IOPOLL_BATCH];
578 unsigned int free_reqs;
579 unsigned int cur_req;
580
581 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700582 * File reference cache
583 */
584 struct file *file;
585 unsigned int fd;
586 unsigned int has_refs;
587 unsigned int used_refs;
588 unsigned int ios_left;
589};
590
Jens Axboed3656342019-12-18 09:50:26 -0700591struct io_op_def {
592 /* needs req->io allocated for deferral/async */
593 unsigned async_ctx : 1;
594 /* needs current->mm setup, does mm access */
595 unsigned needs_mm : 1;
596 /* needs req->file assigned */
597 unsigned needs_file : 1;
598 /* needs req->file assigned IFF fd is >= 0 */
599 unsigned fd_non_neg : 1;
600 /* hash wq insertion if file is a regular file */
601 unsigned hash_reg_file : 1;
602 /* unbound wq insertion if file is a non-regular file */
603 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700604 /* opcode is not supported by this kernel */
605 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700606 /* needs file table */
607 unsigned file_table : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700608};
609
610static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300611 [IORING_OP_NOP] = {},
612 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700613 .async_ctx = 1,
614 .needs_mm = 1,
615 .needs_file = 1,
616 .unbound_nonreg_file = 1,
617 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300618 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700619 .async_ctx = 1,
620 .needs_mm = 1,
621 .needs_file = 1,
622 .hash_reg_file = 1,
623 .unbound_nonreg_file = 1,
624 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300625 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700626 .needs_file = 1,
627 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300628 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700629 .needs_file = 1,
630 .unbound_nonreg_file = 1,
631 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300632 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700633 .needs_file = 1,
634 .hash_reg_file = 1,
635 .unbound_nonreg_file = 1,
636 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300637 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700638 .needs_file = 1,
639 .unbound_nonreg_file = 1,
640 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300641 [IORING_OP_POLL_REMOVE] = {},
642 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700643 .needs_file = 1,
644 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300645 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700646 .async_ctx = 1,
647 .needs_mm = 1,
648 .needs_file = 1,
649 .unbound_nonreg_file = 1,
650 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300651 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700652 .async_ctx = 1,
653 .needs_mm = 1,
654 .needs_file = 1,
655 .unbound_nonreg_file = 1,
656 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300657 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700658 .async_ctx = 1,
659 .needs_mm = 1,
660 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300661 [IORING_OP_TIMEOUT_REMOVE] = {},
662 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700663 .needs_mm = 1,
664 .needs_file = 1,
665 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700666 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700667 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300668 [IORING_OP_ASYNC_CANCEL] = {},
669 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700670 .async_ctx = 1,
671 .needs_mm = 1,
672 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300673 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700674 .async_ctx = 1,
675 .needs_mm = 1,
676 .needs_file = 1,
677 .unbound_nonreg_file = 1,
678 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300679 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700680 .needs_file = 1,
681 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300682 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700683 .needs_file = 1,
684 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700685 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700686 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300687 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700688 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700689 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700690 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300691 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700692 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700693 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700694 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300695 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700696 .needs_mm = 1,
697 .needs_file = 1,
698 .fd_non_neg = 1,
699 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300700 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700701 .needs_mm = 1,
702 .needs_file = 1,
703 .unbound_nonreg_file = 1,
704 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300705 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700706 .needs_mm = 1,
707 .needs_file = 1,
708 .unbound_nonreg_file = 1,
709 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300710 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700711 .needs_file = 1,
712 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300713 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700714 .needs_mm = 1,
715 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300716 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700717 .needs_mm = 1,
718 .needs_file = 1,
719 .unbound_nonreg_file = 1,
720 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300721 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700722 .needs_mm = 1,
723 .needs_file = 1,
724 .unbound_nonreg_file = 1,
725 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300726 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700727 .needs_file = 1,
728 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700729 .file_table = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700730 },
Jens Axboed3656342019-12-18 09:50:26 -0700731};
732
Jens Axboe561fb042019-10-24 07:25:42 -0600733static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700734static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800735static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700736static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700737static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
738static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700739static int __io_sqe_files_update(struct io_ring_ctx *ctx,
740 struct io_uring_files_update *ip,
741 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700742static int io_grab_files(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600743
Jens Axboe2b188cc2019-01-07 10:46:33 -0700744static struct kmem_cache *req_cachep;
745
746static const struct file_operations io_uring_fops;
747
748struct sock *io_uring_get_socket(struct file *file)
749{
750#if defined(CONFIG_UNIX)
751 if (file->f_op == &io_uring_fops) {
752 struct io_ring_ctx *ctx = file->private_data;
753
754 return ctx->ring_sock->sk;
755 }
756#endif
757 return NULL;
758}
759EXPORT_SYMBOL(io_uring_get_socket);
760
761static void io_ring_ctx_ref_free(struct percpu_ref *ref)
762{
763 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
764
Jens Axboe206aefd2019-11-07 18:27:42 -0700765 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700766}
767
768static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
769{
770 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700771 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700772
773 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
774 if (!ctx)
775 return NULL;
776
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700777 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
778 if (!ctx->fallback_req)
779 goto err;
780
Jens Axboe206aefd2019-11-07 18:27:42 -0700781 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
782 if (!ctx->completions)
783 goto err;
784
Jens Axboe78076bb2019-12-04 19:56:40 -0700785 /*
786 * Use 5 bits less than the max cq entries, that should give us around
787 * 32 entries per hash list if totally full and uniformly spread.
788 */
789 hash_bits = ilog2(p->cq_entries);
790 hash_bits -= 5;
791 if (hash_bits <= 0)
792 hash_bits = 1;
793 ctx->cancel_hash_bits = hash_bits;
794 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
795 GFP_KERNEL);
796 if (!ctx->cancel_hash)
797 goto err;
798 __hash_init(ctx->cancel_hash, 1U << hash_bits);
799
Roman Gushchin21482892019-05-07 10:01:48 -0700800 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700801 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
802 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700803
804 ctx->flags = p->flags;
805 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700806 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700807 init_completion(&ctx->completions[0]);
808 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700809 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700810 mutex_init(&ctx->uring_lock);
811 init_waitqueue_head(&ctx->wait);
812 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700813 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700814 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600815 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600816 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600817 init_waitqueue_head(&ctx->inflight_wait);
818 spin_lock_init(&ctx->inflight_lock);
819 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700820 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700821err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700822 if (ctx->fallback_req)
823 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700824 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700825 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700826 kfree(ctx);
827 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700828}
829
Bob Liu9d858b22019-11-13 18:06:25 +0800830static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600831{
Jackie Liua197f662019-11-08 08:09:12 -0700832 struct io_ring_ctx *ctx = req->ctx;
833
Jens Axboe498ccd92019-10-25 10:04:25 -0600834 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
835 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600836}
837
Bob Liu9d858b22019-11-13 18:06:25 +0800838static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600839{
Pavel Begunkov87987892020-01-18 01:22:30 +0300840 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800841 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600842
Bob Liu9d858b22019-11-13 18:06:25 +0800843 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600844}
845
846static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600847{
848 struct io_kiocb *req;
849
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600850 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800851 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600852 list_del_init(&req->list);
853 return req;
854 }
855
856 return NULL;
857}
858
Jens Axboe5262f562019-09-17 12:26:57 -0600859static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
860{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600861 struct io_kiocb *req;
862
863 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700864 if (req) {
865 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
866 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800867 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700868 list_del_init(&req->list);
869 return req;
870 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600871 }
872
873 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600874}
875
Jens Axboede0617e2019-04-06 21:51:27 -0600876static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700877{
Hristo Venev75b28af2019-08-26 17:23:46 +0000878 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700879
Pavel Begunkov07910152020-01-17 03:52:46 +0300880 /* order cqe stores with ring update */
881 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700882
Pavel Begunkov07910152020-01-17 03:52:46 +0300883 if (wq_has_sleeper(&ctx->cq_wait)) {
884 wake_up_interruptible(&ctx->cq_wait);
885 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700886 }
887}
888
Jens Axboecccf0ee2020-01-27 16:34:48 -0700889static inline void io_req_work_grab_env(struct io_kiocb *req,
890 const struct io_op_def *def)
891{
892 if (!req->work.mm && def->needs_mm) {
893 mmgrab(current->mm);
894 req->work.mm = current->mm;
895 }
896 if (!req->work.creds)
897 req->work.creds = get_current_cred();
898}
899
900static inline void io_req_work_drop_env(struct io_kiocb *req)
901{
902 if (req->work.mm) {
903 mmdrop(req->work.mm);
904 req->work.mm = NULL;
905 }
906 if (req->work.creds) {
907 put_cred(req->work.creds);
908 req->work.creds = NULL;
909 }
910}
911
Jens Axboe94ae5e72019-11-14 19:39:52 -0700912static inline bool io_prep_async_work(struct io_kiocb *req,
913 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600914{
Jens Axboed3656342019-12-18 09:50:26 -0700915 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600916 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600917
Jens Axboed3656342019-12-18 09:50:26 -0700918 if (req->flags & REQ_F_ISREG) {
919 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700920 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700921 } else {
922 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700923 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600924 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700925
926 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600927
Jens Axboe94ae5e72019-11-14 19:39:52 -0700928 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600929 return do_hashed;
930}
931
Jackie Liua197f662019-11-08 08:09:12 -0700932static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600933{
Jackie Liua197f662019-11-08 08:09:12 -0700934 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700935 struct io_kiocb *link;
936 bool do_hashed;
937
938 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600939
940 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
941 req->flags);
942 if (!do_hashed) {
943 io_wq_enqueue(ctx->io_wq, &req->work);
944 } else {
945 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
946 file_inode(req->file));
947 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700948
949 if (link)
950 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600951}
952
Jens Axboe5262f562019-09-17 12:26:57 -0600953static void io_kill_timeout(struct io_kiocb *req)
954{
955 int ret;
956
Jens Axboe2d283902019-12-04 11:08:05 -0700957 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600958 if (ret != -1) {
959 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600960 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700961 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800962 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600963 }
964}
965
966static void io_kill_timeouts(struct io_ring_ctx *ctx)
967{
968 struct io_kiocb *req, *tmp;
969
970 spin_lock_irq(&ctx->completion_lock);
971 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
972 io_kill_timeout(req);
973 spin_unlock_irq(&ctx->completion_lock);
974}
975
Jens Axboede0617e2019-04-06 21:51:27 -0600976static void io_commit_cqring(struct io_ring_ctx *ctx)
977{
978 struct io_kiocb *req;
979
Jens Axboe5262f562019-09-17 12:26:57 -0600980 while ((req = io_get_timeout_req(ctx)) != NULL)
981 io_kill_timeout(req);
982
Jens Axboede0617e2019-04-06 21:51:27 -0600983 __io_commit_cqring(ctx);
984
Pavel Begunkov87987892020-01-18 01:22:30 +0300985 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -0700986 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600987}
988
Jens Axboe2b188cc2019-01-07 10:46:33 -0700989static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
990{
Hristo Venev75b28af2019-08-26 17:23:46 +0000991 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700992 unsigned tail;
993
994 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200995 /*
996 * writes to the cq entry need to come after reading head; the
997 * control dependency is enough as we're using WRITE_ONCE to
998 * fill the cq entry
999 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001000 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001001 return NULL;
1002
1003 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001004 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001005}
1006
Jens Axboef2842ab2020-01-08 11:04:00 -07001007static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1008{
1009 if (!ctx->eventfd_async)
1010 return true;
1011 return io_wq_current_is_worker() || in_interrupt();
1012}
1013
Jens Axboe8c838782019-03-12 15:48:16 -06001014static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1015{
1016 if (waitqueue_active(&ctx->wait))
1017 wake_up(&ctx->wait);
1018 if (waitqueue_active(&ctx->sqo_wait))
1019 wake_up(&ctx->sqo_wait);
Jens Axboef2842ab2020-01-08 11:04:00 -07001020 if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001021 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001022}
1023
Jens Axboec4a2ed72019-11-21 21:01:26 -07001024/* Returns true if there are no backlogged entries after the flush */
1025static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001026{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001027 struct io_rings *rings = ctx->rings;
1028 struct io_uring_cqe *cqe;
1029 struct io_kiocb *req;
1030 unsigned long flags;
1031 LIST_HEAD(list);
1032
1033 if (!force) {
1034 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001035 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001036 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1037 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001038 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001039 }
1040
1041 spin_lock_irqsave(&ctx->completion_lock, flags);
1042
1043 /* if force is set, the ring is going away. always drop after that */
1044 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001045 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001046
Jens Axboec4a2ed72019-11-21 21:01:26 -07001047 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001048 while (!list_empty(&ctx->cq_overflow_list)) {
1049 cqe = io_get_cqring(ctx);
1050 if (!cqe && !force)
1051 break;
1052
1053 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1054 list);
1055 list_move(&req->list, &list);
1056 if (cqe) {
1057 WRITE_ONCE(cqe->user_data, req->user_data);
1058 WRITE_ONCE(cqe->res, req->result);
1059 WRITE_ONCE(cqe->flags, 0);
1060 } else {
1061 WRITE_ONCE(ctx->rings->cq_overflow,
1062 atomic_inc_return(&ctx->cached_cq_overflow));
1063 }
1064 }
1065
1066 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001067 if (cqe) {
1068 clear_bit(0, &ctx->sq_check_overflow);
1069 clear_bit(0, &ctx->cq_check_overflow);
1070 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001071 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1072 io_cqring_ev_posted(ctx);
1073
1074 while (!list_empty(&list)) {
1075 req = list_first_entry(&list, struct io_kiocb, list);
1076 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001077 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001078 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001079
1080 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001081}
1082
Jens Axboe78e19bb2019-11-06 15:21:34 -07001083static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001085 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001086 struct io_uring_cqe *cqe;
1087
Jens Axboe78e19bb2019-11-06 15:21:34 -07001088 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001089
Jens Axboe2b188cc2019-01-07 10:46:33 -07001090 /*
1091 * If we can't get a cq entry, userspace overflowed the
1092 * submission (by quite a lot). Increment the overflow count in
1093 * the ring.
1094 */
1095 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001096 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001097 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001098 WRITE_ONCE(cqe->res, res);
1099 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001100 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001101 WRITE_ONCE(ctx->rings->cq_overflow,
1102 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001103 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001104 if (list_empty(&ctx->cq_overflow_list)) {
1105 set_bit(0, &ctx->sq_check_overflow);
1106 set_bit(0, &ctx->cq_check_overflow);
1107 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001108 refcount_inc(&req->refs);
1109 req->result = res;
1110 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001111 }
1112}
1113
Jens Axboe78e19bb2019-11-06 15:21:34 -07001114static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001115{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001116 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001117 unsigned long flags;
1118
1119 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001120 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001121 io_commit_cqring(ctx);
1122 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1123
Jens Axboe8c838782019-03-12 15:48:16 -06001124 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001125}
1126
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001127static inline bool io_is_fallback_req(struct io_kiocb *req)
1128{
1129 return req == (struct io_kiocb *)
1130 ((unsigned long) req->ctx->fallback_req & ~1UL);
1131}
1132
1133static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1134{
1135 struct io_kiocb *req;
1136
1137 req = ctx->fallback_req;
1138 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1139 return req;
1140
1141 return NULL;
1142}
1143
Jens Axboe2579f912019-01-09 09:10:43 -07001144static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1145 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146{
Jens Axboefd6fab22019-03-14 16:30:06 -06001147 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001148 struct io_kiocb *req;
1149
Jens Axboe2579f912019-01-09 09:10:43 -07001150 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001151 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001152 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001153 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001154 } else if (!state->free_reqs) {
1155 size_t sz;
1156 int ret;
1157
1158 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001159 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1160
1161 /*
1162 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1163 * retry single alloc to be on the safe side.
1164 */
1165 if (unlikely(ret <= 0)) {
1166 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1167 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001168 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001169 ret = 1;
1170 }
Jens Axboe2579f912019-01-09 09:10:43 -07001171 state->free_reqs = ret - 1;
1172 state->cur_req = 1;
1173 req = state->reqs[0];
1174 } else {
1175 req = state->reqs[state->cur_req];
1176 state->free_reqs--;
1177 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001178 }
1179
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001180got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001181 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001182 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001183 req->ctx = ctx;
1184 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001185 /* one is dropped after submission, the other at completion */
1186 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001187 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001188 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001189 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001190fallback:
1191 req = io_get_fallback_req(ctx);
1192 if (req)
1193 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001194 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001195 return NULL;
1196}
1197
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001198static void __io_req_do_free(struct io_kiocb *req)
1199{
1200 if (likely(!io_is_fallback_req(req)))
1201 kmem_cache_free(req_cachep, req);
1202 else
1203 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1204}
1205
Jens Axboec6ca97b302019-12-28 12:11:08 -07001206static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001207{
Jens Axboefcb323c2019-10-24 12:39:47 -06001208 struct io_ring_ctx *ctx = req->ctx;
1209
YueHaibing96fd84d2020-01-07 22:22:44 +08001210 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001211 if (req->file) {
1212 if (req->flags & REQ_F_FIXED_FILE)
1213 percpu_ref_put(&ctx->file_data->refs);
1214 else
1215 fput(req->file);
1216 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001217
1218 io_req_work_drop_env(req);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001219}
1220
1221static void __io_free_req(struct io_kiocb *req)
1222{
1223 __io_req_aux_free(req);
1224
Jens Axboefcb323c2019-10-24 12:39:47 -06001225 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001226 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001227 unsigned long flags;
1228
1229 spin_lock_irqsave(&ctx->inflight_lock, flags);
1230 list_del(&req->inflight_entry);
1231 if (waitqueue_active(&ctx->inflight_wait))
1232 wake_up(&ctx->inflight_wait);
1233 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1234 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001235
1236 percpu_ref_put(&req->ctx->refs);
1237 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001238}
1239
Jens Axboec6ca97b302019-12-28 12:11:08 -07001240struct req_batch {
1241 void *reqs[IO_IOPOLL_BATCH];
1242 int to_free;
1243 int need_iter;
1244};
1245
1246static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1247{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001248 int fixed_refs = rb->to_free;
1249
Jens Axboec6ca97b302019-12-28 12:11:08 -07001250 if (!rb->to_free)
1251 return;
1252 if (rb->need_iter) {
1253 int i, inflight = 0;
1254 unsigned long flags;
1255
Jens Axboe10fef4b2020-01-09 07:52:28 -07001256 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001257 for (i = 0; i < rb->to_free; i++) {
1258 struct io_kiocb *req = rb->reqs[i];
1259
Jens Axboe10fef4b2020-01-09 07:52:28 -07001260 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001261 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001262 fixed_refs++;
1263 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001264 if (req->flags & REQ_F_INFLIGHT)
1265 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001266 __io_req_aux_free(req);
1267 }
1268 if (!inflight)
1269 goto do_free;
1270
1271 spin_lock_irqsave(&ctx->inflight_lock, flags);
1272 for (i = 0; i < rb->to_free; i++) {
1273 struct io_kiocb *req = rb->reqs[i];
1274
Jens Axboe10fef4b2020-01-09 07:52:28 -07001275 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001276 list_del(&req->inflight_entry);
1277 if (!--inflight)
1278 break;
1279 }
1280 }
1281 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1282
1283 if (waitqueue_active(&ctx->inflight_wait))
1284 wake_up(&ctx->inflight_wait);
1285 }
1286do_free:
1287 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001288 if (fixed_refs)
1289 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001290 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001291 rb->to_free = rb->need_iter = 0;
1292}
1293
Jackie Liua197f662019-11-08 08:09:12 -07001294static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001295{
Jackie Liua197f662019-11-08 08:09:12 -07001296 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001297 int ret;
1298
Jens Axboe2d283902019-12-04 11:08:05 -07001299 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001300 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001301 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001302 io_commit_cqring(ctx);
1303 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001304 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001305 return true;
1306 }
1307
1308 return false;
1309}
1310
Jens Axboeba816ad2019-09-28 11:36:45 -06001311static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001312{
Jens Axboe2665abf2019-11-05 12:40:47 -07001313 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001314 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001315
Jens Axboe4d7dd462019-11-20 13:03:52 -07001316 /* Already got next link */
1317 if (req->flags & REQ_F_LINK_NEXT)
1318 return;
1319
Jens Axboe9e645e112019-05-10 16:07:28 -06001320 /*
1321 * The list should never be empty when we are called here. But could
1322 * potentially happen if the chain is messed up, check to be on the
1323 * safe side.
1324 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001325 while (!list_empty(&req->link_list)) {
1326 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1327 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001328
Pavel Begunkov44932332019-12-05 16:16:35 +03001329 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1330 (nxt->flags & REQ_F_TIMEOUT))) {
1331 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001332 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001333 req->flags &= ~REQ_F_LINK_TIMEOUT;
1334 continue;
1335 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001336
Pavel Begunkov44932332019-12-05 16:16:35 +03001337 list_del_init(&req->link_list);
1338 if (!list_empty(&nxt->link_list))
1339 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001340 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001341 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001342 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001343
Jens Axboe4d7dd462019-11-20 13:03:52 -07001344 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001345 if (wake_ev)
1346 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001347}
1348
1349/*
1350 * Called if REQ_F_LINK is set, and we fail the head request
1351 */
1352static void io_fail_links(struct io_kiocb *req)
1353{
Jens Axboe2665abf2019-11-05 12:40:47 -07001354 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001355 unsigned long flags;
1356
1357 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001358
1359 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001360 struct io_kiocb *link = list_first_entry(&req->link_list,
1361 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001362
Pavel Begunkov44932332019-12-05 16:16:35 +03001363 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001364 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001365
1366 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001367 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001368 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001369 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001370 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001371 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001372 }
Jens Axboe5d960722019-11-19 15:31:28 -07001373 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001374 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001375
1376 io_commit_cqring(ctx);
1377 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1378 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001379}
1380
Jens Axboe4d7dd462019-11-20 13:03:52 -07001381static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001382{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001383 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001384 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001385
Jens Axboe9e645e112019-05-10 16:07:28 -06001386 /*
1387 * If LINK is set, we have dependent requests in this chain. If we
1388 * didn't fail this request, queue the first one up, moving any other
1389 * dependencies to the next request. In case of failure, fail the rest
1390 * of the chain.
1391 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001392 if (req->flags & REQ_F_FAIL_LINK) {
1393 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001394 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1395 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001396 struct io_ring_ctx *ctx = req->ctx;
1397 unsigned long flags;
1398
1399 /*
1400 * If this is a timeout link, we could be racing with the
1401 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001402 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001403 */
1404 spin_lock_irqsave(&ctx->completion_lock, flags);
1405 io_req_link_next(req, nxt);
1406 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1407 } else {
1408 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001409 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001410}
Jens Axboe9e645e112019-05-10 16:07:28 -06001411
Jackie Liuc69f8db2019-11-09 11:00:08 +08001412static void io_free_req(struct io_kiocb *req)
1413{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001414 struct io_kiocb *nxt = NULL;
1415
1416 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001417 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001418
1419 if (nxt)
1420 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001421}
1422
Jens Axboeba816ad2019-09-28 11:36:45 -06001423/*
1424 * Drop reference to request, return next in chain (if there is one) if this
1425 * was the last reference to this request.
1426 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001427__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001428static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001429{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001430 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001431
Jens Axboee65ef562019-03-12 10:16:44 -06001432 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001433 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001434}
1435
Jens Axboe2b188cc2019-01-07 10:46:33 -07001436static void io_put_req(struct io_kiocb *req)
1437{
Jens Axboedef596e2019-01-09 08:59:42 -07001438 if (refcount_dec_and_test(&req->refs))
1439 io_free_req(req);
1440}
1441
Jens Axboe978db572019-11-14 22:39:04 -07001442/*
1443 * Must only be used if we don't need to care about links, usually from
1444 * within the completion handling itself.
1445 */
1446static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001447{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001448 /* drop both submit and complete references */
1449 if (refcount_sub_and_test(2, &req->refs))
1450 __io_free_req(req);
1451}
1452
Jens Axboe978db572019-11-14 22:39:04 -07001453static void io_double_put_req(struct io_kiocb *req)
1454{
1455 /* drop both submit and complete references */
1456 if (refcount_sub_and_test(2, &req->refs))
1457 io_free_req(req);
1458}
1459
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001460static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001461{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001462 struct io_rings *rings = ctx->rings;
1463
Jens Axboead3eb2c2019-12-18 17:12:20 -07001464 if (test_bit(0, &ctx->cq_check_overflow)) {
1465 /*
1466 * noflush == true is from the waitqueue handler, just ensure
1467 * we wake up the task, and the next invocation will flush the
1468 * entries. We cannot safely to it from here.
1469 */
1470 if (noflush && !list_empty(&ctx->cq_overflow_list))
1471 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001472
Jens Axboead3eb2c2019-12-18 17:12:20 -07001473 io_cqring_overflow_flush(ctx, false);
1474 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001475
Jens Axboea3a0e432019-08-20 11:03:11 -06001476 /* See comment at the top of this file */
1477 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001478 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001479}
1480
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001481static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1482{
1483 struct io_rings *rings = ctx->rings;
1484
1485 /* make sure SQ entry isn't read before tail */
1486 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1487}
1488
Jens Axboe8237e042019-12-28 10:48:22 -07001489static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001490{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001491 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1492 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001493
Jens Axboec6ca97b302019-12-28 12:11:08 -07001494 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1495 rb->need_iter++;
1496
1497 rb->reqs[rb->to_free++] = req;
1498 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1499 io_free_req_many(req->ctx, rb);
1500 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001501}
1502
Jens Axboedef596e2019-01-09 08:59:42 -07001503/*
1504 * Find and free completed poll iocbs
1505 */
1506static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1507 struct list_head *done)
1508{
Jens Axboe8237e042019-12-28 10:48:22 -07001509 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001510 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001511
Jens Axboec6ca97b302019-12-28 12:11:08 -07001512 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001513 while (!list_empty(done)) {
1514 req = list_first_entry(done, struct io_kiocb, list);
1515 list_del(&req->list);
1516
Jens Axboe78e19bb2019-11-06 15:21:34 -07001517 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001518 (*nr_events)++;
1519
Jens Axboe8237e042019-12-28 10:48:22 -07001520 if (refcount_dec_and_test(&req->refs) &&
1521 !io_req_multi_free(&rb, req))
1522 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001523 }
Jens Axboedef596e2019-01-09 08:59:42 -07001524
Jens Axboe09bb8392019-03-13 12:39:28 -06001525 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001526 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001527}
1528
1529static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1530 long min)
1531{
1532 struct io_kiocb *req, *tmp;
1533 LIST_HEAD(done);
1534 bool spin;
1535 int ret;
1536
1537 /*
1538 * Only spin for completions if we don't have multiple devices hanging
1539 * off our complete list, and we're under the requested amount.
1540 */
1541 spin = !ctx->poll_multi_file && *nr_events < min;
1542
1543 ret = 0;
1544 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001545 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001546
1547 /*
1548 * Move completed entries to our local list. If we find a
1549 * request that requires polling, break out and complete
1550 * the done list first, if we have entries there.
1551 */
1552 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1553 list_move_tail(&req->list, &done);
1554 continue;
1555 }
1556 if (!list_empty(&done))
1557 break;
1558
1559 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1560 if (ret < 0)
1561 break;
1562
1563 if (ret && spin)
1564 spin = false;
1565 ret = 0;
1566 }
1567
1568 if (!list_empty(&done))
1569 io_iopoll_complete(ctx, nr_events, &done);
1570
1571 return ret;
1572}
1573
1574/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001575 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001576 * non-spinning poll check - we'll still enter the driver poll loop, but only
1577 * as a non-spinning completion check.
1578 */
1579static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1580 long min)
1581{
Jens Axboe08f54392019-08-21 22:19:11 -06001582 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001583 int ret;
1584
1585 ret = io_do_iopoll(ctx, nr_events, min);
1586 if (ret < 0)
1587 return ret;
1588 if (!min || *nr_events >= min)
1589 return 0;
1590 }
1591
1592 return 1;
1593}
1594
1595/*
1596 * We can't just wait for polled events to come to us, we have to actively
1597 * find and complete them.
1598 */
1599static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1600{
1601 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1602 return;
1603
1604 mutex_lock(&ctx->uring_lock);
1605 while (!list_empty(&ctx->poll_list)) {
1606 unsigned int nr_events = 0;
1607
1608 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001609
1610 /*
1611 * Ensure we allow local-to-the-cpu processing to take place,
1612 * in this case we need to ensure that we reap all events.
1613 */
1614 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001615 }
1616 mutex_unlock(&ctx->uring_lock);
1617}
1618
Jens Axboe2b2ed972019-10-25 10:06:15 -06001619static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1620 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001621{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001622 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001623
1624 do {
1625 int tmin = 0;
1626
Jens Axboe500f9fb2019-08-19 12:15:59 -06001627 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001628 * Don't enter poll loop if we already have events pending.
1629 * If we do, we can potentially be spinning for commands that
1630 * already triggered a CQE (eg in error).
1631 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001632 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001633 break;
1634
1635 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001636 * If a submit got punted to a workqueue, we can have the
1637 * application entering polling for a command before it gets
1638 * issued. That app will hold the uring_lock for the duration
1639 * of the poll right here, so we need to take a breather every
1640 * now and then to ensure that the issue has a chance to add
1641 * the poll to the issued list. Otherwise we can spin here
1642 * forever, while the workqueue is stuck trying to acquire the
1643 * very same mutex.
1644 */
1645 if (!(++iters & 7)) {
1646 mutex_unlock(&ctx->uring_lock);
1647 mutex_lock(&ctx->uring_lock);
1648 }
1649
Jens Axboedef596e2019-01-09 08:59:42 -07001650 if (*nr_events < min)
1651 tmin = min - *nr_events;
1652
1653 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1654 if (ret <= 0)
1655 break;
1656 ret = 0;
1657 } while (min && !*nr_events && !need_resched());
1658
Jens Axboe2b2ed972019-10-25 10:06:15 -06001659 return ret;
1660}
1661
1662static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1663 long min)
1664{
1665 int ret;
1666
1667 /*
1668 * We disallow the app entering submit/complete with polling, but we
1669 * still need to lock the ring to prevent racing with polled issue
1670 * that got punted to a workqueue.
1671 */
1672 mutex_lock(&ctx->uring_lock);
1673 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001674 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001675 return ret;
1676}
1677
Jens Axboe491381ce2019-10-17 09:20:46 -06001678static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001679{
Jens Axboe491381ce2019-10-17 09:20:46 -06001680 /*
1681 * Tell lockdep we inherited freeze protection from submission
1682 * thread.
1683 */
1684 if (req->flags & REQ_F_ISREG) {
1685 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001686
Jens Axboe491381ce2019-10-17 09:20:46 -06001687 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001688 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001689 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001690}
1691
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001692static inline void req_set_fail_links(struct io_kiocb *req)
1693{
1694 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1695 req->flags |= REQ_F_FAIL_LINK;
1696}
1697
Jens Axboeba816ad2019-09-28 11:36:45 -06001698static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001699{
Jens Axboe9adbd452019-12-20 08:45:55 -07001700 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001701
Jens Axboe491381ce2019-10-17 09:20:46 -06001702 if (kiocb->ki_flags & IOCB_WRITE)
1703 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001704
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001705 if (res != req->result)
1706 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001707 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001708}
1709
1710static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1711{
Jens Axboe9adbd452019-12-20 08:45:55 -07001712 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001713
1714 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001715 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001716}
1717
Jens Axboeba816ad2019-09-28 11:36:45 -06001718static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1719{
Jens Axboe9adbd452019-12-20 08:45:55 -07001720 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001721 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001722
1723 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001724 io_put_req_find_next(req, &nxt);
1725
1726 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727}
1728
Jens Axboedef596e2019-01-09 08:59:42 -07001729static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1730{
Jens Axboe9adbd452019-12-20 08:45:55 -07001731 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001732
Jens Axboe491381ce2019-10-17 09:20:46 -06001733 if (kiocb->ki_flags & IOCB_WRITE)
1734 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001735
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001736 if (res != req->result)
1737 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001738 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001739 if (res != -EAGAIN)
1740 req->flags |= REQ_F_IOPOLL_COMPLETED;
1741}
1742
1743/*
1744 * After the iocb has been issued, it's safe to be found on the poll list.
1745 * Adding the kiocb to the list AFTER submission ensures that we don't
1746 * find it from a io_iopoll_getevents() thread before the issuer is done
1747 * accessing the kiocb cookie.
1748 */
1749static void io_iopoll_req_issued(struct io_kiocb *req)
1750{
1751 struct io_ring_ctx *ctx = req->ctx;
1752
1753 /*
1754 * Track whether we have multiple files in our lists. This will impact
1755 * how we do polling eventually, not spinning if we're on potentially
1756 * different devices.
1757 */
1758 if (list_empty(&ctx->poll_list)) {
1759 ctx->poll_multi_file = false;
1760 } else if (!ctx->poll_multi_file) {
1761 struct io_kiocb *list_req;
1762
1763 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1764 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001765 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001766 ctx->poll_multi_file = true;
1767 }
1768
1769 /*
1770 * For fast devices, IO may have already completed. If it has, add
1771 * it to the front so we find it first.
1772 */
1773 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1774 list_add(&req->list, &ctx->poll_list);
1775 else
1776 list_add_tail(&req->list, &ctx->poll_list);
1777}
1778
Jens Axboe3d6770f2019-04-13 11:50:54 -06001779static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001780{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001781 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001782 int diff = state->has_refs - state->used_refs;
1783
1784 if (diff)
1785 fput_many(state->file, diff);
1786 state->file = NULL;
1787 }
1788}
1789
1790/*
1791 * Get as many references to a file as we have IOs left in this submission,
1792 * assuming most submissions are for one file, or at least that each file
1793 * has more than one submission.
1794 */
1795static struct file *io_file_get(struct io_submit_state *state, int fd)
1796{
1797 if (!state)
1798 return fget(fd);
1799
1800 if (state->file) {
1801 if (state->fd == fd) {
1802 state->used_refs++;
1803 state->ios_left--;
1804 return state->file;
1805 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001806 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001807 }
1808 state->file = fget_many(fd, state->ios_left);
1809 if (!state->file)
1810 return NULL;
1811
1812 state->fd = fd;
1813 state->has_refs = state->ios_left;
1814 state->used_refs = 1;
1815 state->ios_left--;
1816 return state->file;
1817}
1818
Jens Axboe2b188cc2019-01-07 10:46:33 -07001819/*
1820 * If we tracked the file through the SCM inflight mechanism, we could support
1821 * any file. For now, just ensure that anything potentially problematic is done
1822 * inline.
1823 */
1824static bool io_file_supports_async(struct file *file)
1825{
1826 umode_t mode = file_inode(file)->i_mode;
1827
Jens Axboe10d59342019-12-09 20:16:22 -07001828 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001829 return true;
1830 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1831 return true;
1832
1833 return false;
1834}
1835
Jens Axboe3529d8c2019-12-19 18:24:38 -07001836static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1837 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001838{
Jens Axboedef596e2019-01-09 08:59:42 -07001839 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001840 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001841 unsigned ioprio;
1842 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001843
Jens Axboe09bb8392019-03-13 12:39:28 -06001844 if (!req->file)
1845 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001846
Jens Axboe491381ce2019-10-17 09:20:46 -06001847 if (S_ISREG(file_inode(req->file)->i_mode))
1848 req->flags |= REQ_F_ISREG;
1849
Jens Axboe2b188cc2019-01-07 10:46:33 -07001850 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001851 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1852 req->flags |= REQ_F_CUR_POS;
1853 kiocb->ki_pos = req->file->f_pos;
1854 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001855 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1856 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1857
1858 ioprio = READ_ONCE(sqe->ioprio);
1859 if (ioprio) {
1860 ret = ioprio_check_cap(ioprio);
1861 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001862 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001863
1864 kiocb->ki_ioprio = ioprio;
1865 } else
1866 kiocb->ki_ioprio = get_current_ioprio();
1867
1868 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1869 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001870 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001871
1872 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001873 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1874 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001875 req->flags |= REQ_F_NOWAIT;
1876
1877 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001878 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001879
Jens Axboedef596e2019-01-09 08:59:42 -07001880 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001881 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1882 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001883 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001884
Jens Axboedef596e2019-01-09 08:59:42 -07001885 kiocb->ki_flags |= IOCB_HIPRI;
1886 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001887 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001888 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001889 if (kiocb->ki_flags & IOCB_HIPRI)
1890 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001891 kiocb->ki_complete = io_complete_rw;
1892 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001893
Jens Axboe3529d8c2019-12-19 18:24:38 -07001894 req->rw.addr = READ_ONCE(sqe->addr);
1895 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001896 /* we own ->private, reuse it for the buffer index */
1897 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001898 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001900}
1901
1902static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1903{
1904 switch (ret) {
1905 case -EIOCBQUEUED:
1906 break;
1907 case -ERESTARTSYS:
1908 case -ERESTARTNOINTR:
1909 case -ERESTARTNOHAND:
1910 case -ERESTART_RESTARTBLOCK:
1911 /*
1912 * We can't just restart the syscall, since previously
1913 * submitted sqes may already be in progress. Just fail this
1914 * IO with EINTR.
1915 */
1916 ret = -EINTR;
1917 /* fall through */
1918 default:
1919 kiocb->ki_complete(kiocb, ret, 0);
1920 }
1921}
1922
Jens Axboeba816ad2019-09-28 11:36:45 -06001923static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1924 bool in_async)
1925{
Jens Axboeba042912019-12-25 16:33:42 -07001926 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1927
1928 if (req->flags & REQ_F_CUR_POS)
1929 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001930 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001931 *nxt = __io_complete_rw(kiocb, ret);
1932 else
1933 io_rw_done(kiocb, ret);
1934}
1935
Jens Axboe9adbd452019-12-20 08:45:55 -07001936static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001937 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001938{
Jens Axboe9adbd452019-12-20 08:45:55 -07001939 struct io_ring_ctx *ctx = req->ctx;
1940 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001941 struct io_mapped_ubuf *imu;
1942 unsigned index, buf_index;
1943 size_t offset;
1944 u64 buf_addr;
1945
1946 /* attempt to use fixed buffers without having provided iovecs */
1947 if (unlikely(!ctx->user_bufs))
1948 return -EFAULT;
1949
Jens Axboe9adbd452019-12-20 08:45:55 -07001950 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001951 if (unlikely(buf_index >= ctx->nr_user_bufs))
1952 return -EFAULT;
1953
1954 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1955 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001956 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001957
1958 /* overflow */
1959 if (buf_addr + len < buf_addr)
1960 return -EFAULT;
1961 /* not inside the mapped region */
1962 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1963 return -EFAULT;
1964
1965 /*
1966 * May not be a start of buffer, set size appropriately
1967 * and advance us to the beginning.
1968 */
1969 offset = buf_addr - imu->ubuf;
1970 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001971
1972 if (offset) {
1973 /*
1974 * Don't use iov_iter_advance() here, as it's really slow for
1975 * using the latter parts of a big fixed buffer - it iterates
1976 * over each segment manually. We can cheat a bit here, because
1977 * we know that:
1978 *
1979 * 1) it's a BVEC iter, we set it up
1980 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1981 * first and last bvec
1982 *
1983 * So just find our index, and adjust the iterator afterwards.
1984 * If the offset is within the first bvec (or the whole first
1985 * bvec, just use iov_iter_advance(). This makes it easier
1986 * since we can just skip the first segment, which may not
1987 * be PAGE_SIZE aligned.
1988 */
1989 const struct bio_vec *bvec = imu->bvec;
1990
1991 if (offset <= bvec->bv_len) {
1992 iov_iter_advance(iter, offset);
1993 } else {
1994 unsigned long seg_skip;
1995
1996 /* skip first vec */
1997 offset -= bvec->bv_len;
1998 seg_skip = 1 + (offset >> PAGE_SHIFT);
1999
2000 iter->bvec = bvec + seg_skip;
2001 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002002 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002003 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002004 }
2005 }
2006
Jens Axboe5e559562019-11-13 16:12:46 -07002007 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002008}
2009
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002010static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2011 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002012{
Jens Axboe9adbd452019-12-20 08:45:55 -07002013 void __user *buf = u64_to_user_ptr(req->rw.addr);
2014 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002015 u8 opcode;
2016
Jens Axboed625c6e2019-12-17 19:53:05 -07002017 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002018 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002019 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002020 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002021 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002022
Jens Axboe9adbd452019-12-20 08:45:55 -07002023 /* buffer index only valid with fixed read/write */
2024 if (req->rw.kiocb.private)
2025 return -EINVAL;
2026
Jens Axboe3a6820f2019-12-22 15:19:35 -07002027 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2028 ssize_t ret;
2029 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2030 *iovec = NULL;
2031 return ret;
2032 }
2033
Jens Axboef67676d2019-12-02 11:03:47 -07002034 if (req->io) {
2035 struct io_async_rw *iorw = &req->io->rw;
2036
2037 *iovec = iorw->iov;
2038 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2039 if (iorw->iov == iorw->fast_iov)
2040 *iovec = NULL;
2041 return iorw->size;
2042 }
2043
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002044 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002045 return -EFAULT;
2046
2047#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002048 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002049 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2050 iovec, iter);
2051#endif
2052
2053 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2054}
2055
Jens Axboe32960612019-09-23 11:05:34 -06002056/*
2057 * For files that don't have ->read_iter() and ->write_iter(), handle them
2058 * by looping over ->read() or ->write() manually.
2059 */
2060static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2061 struct iov_iter *iter)
2062{
2063 ssize_t ret = 0;
2064
2065 /*
2066 * Don't support polled IO through this interface, and we can't
2067 * support non-blocking either. For the latter, this just causes
2068 * the kiocb to be handled from an async context.
2069 */
2070 if (kiocb->ki_flags & IOCB_HIPRI)
2071 return -EOPNOTSUPP;
2072 if (kiocb->ki_flags & IOCB_NOWAIT)
2073 return -EAGAIN;
2074
2075 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002076 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002077 ssize_t nr;
2078
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002079 if (!iov_iter_is_bvec(iter)) {
2080 iovec = iov_iter_iovec(iter);
2081 } else {
2082 /* fixed buffers import bvec */
2083 iovec.iov_base = kmap(iter->bvec->bv_page)
2084 + iter->iov_offset;
2085 iovec.iov_len = min(iter->count,
2086 iter->bvec->bv_len - iter->iov_offset);
2087 }
2088
Jens Axboe32960612019-09-23 11:05:34 -06002089 if (rw == READ) {
2090 nr = file->f_op->read(file, iovec.iov_base,
2091 iovec.iov_len, &kiocb->ki_pos);
2092 } else {
2093 nr = file->f_op->write(file, iovec.iov_base,
2094 iovec.iov_len, &kiocb->ki_pos);
2095 }
2096
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002097 if (iov_iter_is_bvec(iter))
2098 kunmap(iter->bvec->bv_page);
2099
Jens Axboe32960612019-09-23 11:05:34 -06002100 if (nr < 0) {
2101 if (!ret)
2102 ret = nr;
2103 break;
2104 }
2105 ret += nr;
2106 if (nr != iovec.iov_len)
2107 break;
2108 iov_iter_advance(iter, nr);
2109 }
2110
2111 return ret;
2112}
2113
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002114static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002115 struct iovec *iovec, struct iovec *fast_iov,
2116 struct iov_iter *iter)
2117{
2118 req->io->rw.nr_segs = iter->nr_segs;
2119 req->io->rw.size = io_size;
2120 req->io->rw.iov = iovec;
2121 if (!req->io->rw.iov) {
2122 req->io->rw.iov = req->io->rw.fast_iov;
2123 memcpy(req->io->rw.iov, fast_iov,
2124 sizeof(struct iovec) * iter->nr_segs);
2125 }
2126}
2127
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002128static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002129{
Jens Axboed3656342019-12-18 09:50:26 -07002130 if (!io_op_defs[req->opcode].async_ctx)
2131 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002132 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002133 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002134}
2135
2136static void io_rw_async(struct io_wq_work **workptr)
2137{
2138 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2139 struct iovec *iov = NULL;
2140
2141 if (req->io->rw.iov != req->io->rw.fast_iov)
2142 iov = req->io->rw.iov;
2143 io_wq_submit_work(workptr);
2144 kfree(iov);
2145}
2146
2147static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2148 struct iovec *iovec, struct iovec *fast_iov,
2149 struct iov_iter *iter)
2150{
Jens Axboe980ad262020-01-24 23:08:54 -07002151 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002152 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002153 if (!req->io && io_alloc_async_ctx(req))
2154 return -ENOMEM;
2155
2156 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2157 req->work.func = io_rw_async;
2158 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002159}
2160
Jens Axboe3529d8c2019-12-19 18:24:38 -07002161static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2162 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002163{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002164 struct io_async_ctx *io;
2165 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002166 ssize_t ret;
2167
Jens Axboe3529d8c2019-12-19 18:24:38 -07002168 ret = io_prep_rw(req, sqe, force_nonblock);
2169 if (ret)
2170 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002171
Jens Axboe3529d8c2019-12-19 18:24:38 -07002172 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2173 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002174
Jens Axboe3529d8c2019-12-19 18:24:38 -07002175 if (!req->io)
2176 return 0;
2177
2178 io = req->io;
2179 io->rw.iov = io->rw.fast_iov;
2180 req->io = NULL;
2181 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2182 req->io = io;
2183 if (ret < 0)
2184 return ret;
2185
2186 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2187 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002188}
2189
Pavel Begunkov267bc902019-11-07 01:41:08 +03002190static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002191 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002192{
2193 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002194 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002195 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002196 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002197 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002198
Jens Axboe3529d8c2019-12-19 18:24:38 -07002199 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002200 if (ret < 0)
2201 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002202
Jens Axboefd6c2e42019-12-18 12:19:41 -07002203 /* Ensure we clear previously set non-block flag */
2204 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002205 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002206
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002207 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002208 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002209 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002210 req->result = io_size;
2211
2212 /*
2213 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2214 * we know to async punt it even if it was opened O_NONBLOCK
2215 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002216 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002217 req->flags |= REQ_F_MUST_PUNT;
2218 goto copy_iov;
2219 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002220
Jens Axboe31b51512019-01-18 22:56:34 -07002221 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002222 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002223 if (!ret) {
2224 ssize_t ret2;
2225
Jens Axboe9adbd452019-12-20 08:45:55 -07002226 if (req->file->f_op->read_iter)
2227 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002228 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002229 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002230
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002231 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002232 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002233 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002234 } else {
2235copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002236 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002237 inline_vecs, &iter);
2238 if (ret)
2239 goto out_free;
2240 return -EAGAIN;
2241 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002242 }
Jens Axboef67676d2019-12-02 11:03:47 -07002243out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002244 if (!io_wq_current_is_worker())
2245 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002246 return ret;
2247}
2248
Jens Axboe3529d8c2019-12-19 18:24:38 -07002249static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2250 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002251{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002252 struct io_async_ctx *io;
2253 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002254 ssize_t ret;
2255
Jens Axboe3529d8c2019-12-19 18:24:38 -07002256 ret = io_prep_rw(req, sqe, force_nonblock);
2257 if (ret)
2258 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002259
Jens Axboe3529d8c2019-12-19 18:24:38 -07002260 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2261 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002262
Jens Axboe3529d8c2019-12-19 18:24:38 -07002263 if (!req->io)
2264 return 0;
2265
2266 io = req->io;
2267 io->rw.iov = io->rw.fast_iov;
2268 req->io = NULL;
2269 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2270 req->io = io;
2271 if (ret < 0)
2272 return ret;
2273
2274 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2275 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002276}
2277
Pavel Begunkov267bc902019-11-07 01:41:08 +03002278static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002279 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002280{
2281 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002282 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002283 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002284 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002285 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002286
Jens Axboe3529d8c2019-12-19 18:24:38 -07002287 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002288 if (ret < 0)
2289 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002290
Jens Axboefd6c2e42019-12-18 12:19:41 -07002291 /* Ensure we clear previously set non-block flag */
2292 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002293 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002294
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002295 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002296 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002297 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002298 req->result = io_size;
2299
2300 /*
2301 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2302 * we know to async punt it even if it was opened O_NONBLOCK
2303 */
2304 if (force_nonblock && !io_file_supports_async(req->file)) {
2305 req->flags |= REQ_F_MUST_PUNT;
2306 goto copy_iov;
2307 }
2308
Jens Axboe10d59342019-12-09 20:16:22 -07002309 /* file path doesn't support NOWAIT for non-direct_IO */
2310 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2311 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002312 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002313
Jens Axboe31b51512019-01-18 22:56:34 -07002314 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002315 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002316 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002317 ssize_t ret2;
2318
Jens Axboe2b188cc2019-01-07 10:46:33 -07002319 /*
2320 * Open-code file_start_write here to grab freeze protection,
2321 * which will be released by another thread in
2322 * io_complete_rw(). Fool lockdep by telling it the lock got
2323 * released so that it doesn't complain about the held lock when
2324 * we return to userspace.
2325 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002326 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002327 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002328 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002329 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002330 SB_FREEZE_WRITE);
2331 }
2332 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002333
Jens Axboe9adbd452019-12-20 08:45:55 -07002334 if (req->file->f_op->write_iter)
2335 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002336 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002337 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002338 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002339 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002340 } else {
2341copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002342 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002343 inline_vecs, &iter);
2344 if (ret)
2345 goto out_free;
2346 return -EAGAIN;
2347 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002348 }
Jens Axboe31b51512019-01-18 22:56:34 -07002349out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002350 if (!io_wq_current_is_worker())
2351 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002352 return ret;
2353}
2354
2355/*
2356 * IORING_OP_NOP just posts a completion event, nothing else.
2357 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002358static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002359{
2360 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002361
Jens Axboedef596e2019-01-09 08:59:42 -07002362 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2363 return -EINVAL;
2364
Jens Axboe78e19bb2019-11-06 15:21:34 -07002365 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002366 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002367 return 0;
2368}
2369
Jens Axboe3529d8c2019-12-19 18:24:38 -07002370static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002371{
Jens Axboe6b063142019-01-10 22:13:58 -07002372 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002373
Jens Axboe09bb8392019-03-13 12:39:28 -06002374 if (!req->file)
2375 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002376
Jens Axboe6b063142019-01-10 22:13:58 -07002377 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002378 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002379 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002380 return -EINVAL;
2381
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002382 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2383 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2384 return -EINVAL;
2385
2386 req->sync.off = READ_ONCE(sqe->off);
2387 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002388 return 0;
2389}
2390
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002391static bool io_req_cancelled(struct io_kiocb *req)
2392{
2393 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2394 req_set_fail_links(req);
2395 io_cqring_add_event(req, -ECANCELED);
2396 io_put_req(req);
2397 return true;
2398 }
2399
2400 return false;
2401}
2402
Jens Axboe78912932020-01-14 22:09:06 -07002403static void io_link_work_cb(struct io_wq_work **workptr)
2404{
2405 struct io_wq_work *work = *workptr;
2406 struct io_kiocb *link = work->data;
2407
2408 io_queue_linked_timeout(link);
2409 work->func = io_wq_submit_work;
2410}
2411
2412static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2413{
2414 struct io_kiocb *link;
2415
2416 io_prep_async_work(nxt, &link);
2417 *workptr = &nxt->work;
2418 if (link) {
2419 nxt->work.flags |= IO_WQ_WORK_CB;
2420 nxt->work.func = io_link_work_cb;
2421 nxt->work.data = link;
2422 }
2423}
2424
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002425static void io_fsync_finish(struct io_wq_work **workptr)
2426{
2427 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2428 loff_t end = req->sync.off + req->sync.len;
2429 struct io_kiocb *nxt = NULL;
2430 int ret;
2431
2432 if (io_req_cancelled(req))
2433 return;
2434
Jens Axboe9adbd452019-12-20 08:45:55 -07002435 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002436 end > 0 ? end : LLONG_MAX,
2437 req->sync.flags & IORING_FSYNC_DATASYNC);
2438 if (ret < 0)
2439 req_set_fail_links(req);
2440 io_cqring_add_event(req, ret);
2441 io_put_req_find_next(req, &nxt);
2442 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002443 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002444}
2445
Jens Axboefc4df992019-12-10 14:38:45 -07002446static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2447 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002448{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002449 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002450
2451 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002452 if (force_nonblock) {
2453 io_put_req(req);
2454 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002455 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002456 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002457
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002458 work = old_work = &req->work;
2459 io_fsync_finish(&work);
2460 if (work && work != old_work)
2461 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002462 return 0;
2463}
2464
Jens Axboed63d1b52019-12-10 10:38:56 -07002465static void io_fallocate_finish(struct io_wq_work **workptr)
2466{
2467 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2468 struct io_kiocb *nxt = NULL;
2469 int ret;
2470
2471 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2472 req->sync.len);
2473 if (ret < 0)
2474 req_set_fail_links(req);
2475 io_cqring_add_event(req, ret);
2476 io_put_req_find_next(req, &nxt);
2477 if (nxt)
2478 io_wq_assign_next(workptr, nxt);
2479}
2480
2481static int io_fallocate_prep(struct io_kiocb *req,
2482 const struct io_uring_sqe *sqe)
2483{
2484 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2485 return -EINVAL;
2486
2487 req->sync.off = READ_ONCE(sqe->off);
2488 req->sync.len = READ_ONCE(sqe->addr);
2489 req->sync.mode = READ_ONCE(sqe->len);
2490 return 0;
2491}
2492
2493static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2494 bool force_nonblock)
2495{
2496 struct io_wq_work *work, *old_work;
2497
2498 /* fallocate always requiring blocking context */
2499 if (force_nonblock) {
2500 io_put_req(req);
2501 req->work.func = io_fallocate_finish;
2502 return -EAGAIN;
2503 }
2504
2505 work = old_work = &req->work;
2506 io_fallocate_finish(&work);
2507 if (work && work != old_work)
2508 *nxt = container_of(work, struct io_kiocb, work);
2509
2510 return 0;
2511}
2512
Jens Axboe15b71ab2019-12-11 11:20:36 -07002513static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2514{
Jens Axboef8748882020-01-08 17:47:02 -07002515 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002516 int ret;
2517
2518 if (sqe->ioprio || sqe->buf_index)
2519 return -EINVAL;
2520
2521 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002522 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002523 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002524 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002525
Jens Axboef8748882020-01-08 17:47:02 -07002526 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002527 if (IS_ERR(req->open.filename)) {
2528 ret = PTR_ERR(req->open.filename);
2529 req->open.filename = NULL;
2530 return ret;
2531 }
2532
2533 return 0;
2534}
2535
Jens Axboecebdb982020-01-08 17:59:24 -07002536static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2537{
2538 struct open_how __user *how;
2539 const char __user *fname;
2540 size_t len;
2541 int ret;
2542
2543 if (sqe->ioprio || sqe->buf_index)
2544 return -EINVAL;
2545
2546 req->open.dfd = READ_ONCE(sqe->fd);
2547 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2548 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2549 len = READ_ONCE(sqe->len);
2550
2551 if (len < OPEN_HOW_SIZE_VER0)
2552 return -EINVAL;
2553
2554 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2555 len);
2556 if (ret)
2557 return ret;
2558
2559 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2560 req->open.how.flags |= O_LARGEFILE;
2561
2562 req->open.filename = getname(fname);
2563 if (IS_ERR(req->open.filename)) {
2564 ret = PTR_ERR(req->open.filename);
2565 req->open.filename = NULL;
2566 return ret;
2567 }
2568
2569 return 0;
2570}
2571
2572static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2573 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002574{
2575 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002576 struct file *file;
2577 int ret;
2578
Jens Axboef86cd202020-01-29 13:46:44 -07002579 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002580 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002581
Jens Axboecebdb982020-01-08 17:59:24 -07002582 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002583 if (ret)
2584 goto err;
2585
Jens Axboecebdb982020-01-08 17:59:24 -07002586 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002587 if (ret < 0)
2588 goto err;
2589
2590 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2591 if (IS_ERR(file)) {
2592 put_unused_fd(ret);
2593 ret = PTR_ERR(file);
2594 } else {
2595 fsnotify_open(file);
2596 fd_install(ret, file);
2597 }
2598err:
2599 putname(req->open.filename);
2600 if (ret < 0)
2601 req_set_fail_links(req);
2602 io_cqring_add_event(req, ret);
2603 io_put_req_find_next(req, nxt);
2604 return 0;
2605}
2606
Jens Axboecebdb982020-01-08 17:59:24 -07002607static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2608 bool force_nonblock)
2609{
2610 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2611 return io_openat2(req, nxt, force_nonblock);
2612}
2613
Jens Axboec1ca7572019-12-25 22:18:28 -07002614static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2615{
2616#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2617 if (sqe->ioprio || sqe->buf_index || sqe->off)
2618 return -EINVAL;
2619
2620 req->madvise.addr = READ_ONCE(sqe->addr);
2621 req->madvise.len = READ_ONCE(sqe->len);
2622 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2623 return 0;
2624#else
2625 return -EOPNOTSUPP;
2626#endif
2627}
2628
2629static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2630 bool force_nonblock)
2631{
2632#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2633 struct io_madvise *ma = &req->madvise;
2634 int ret;
2635
2636 if (force_nonblock)
2637 return -EAGAIN;
2638
2639 ret = do_madvise(ma->addr, ma->len, ma->advice);
2640 if (ret < 0)
2641 req_set_fail_links(req);
2642 io_cqring_add_event(req, ret);
2643 io_put_req_find_next(req, nxt);
2644 return 0;
2645#else
2646 return -EOPNOTSUPP;
2647#endif
2648}
2649
Jens Axboe4840e412019-12-25 22:03:45 -07002650static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2651{
2652 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2653 return -EINVAL;
2654
2655 req->fadvise.offset = READ_ONCE(sqe->off);
2656 req->fadvise.len = READ_ONCE(sqe->len);
2657 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2658 return 0;
2659}
2660
2661static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2662 bool force_nonblock)
2663{
2664 struct io_fadvise *fa = &req->fadvise;
2665 int ret;
2666
2667 /* DONTNEED may block, others _should_ not */
2668 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2669 return -EAGAIN;
2670
2671 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2672 if (ret < 0)
2673 req_set_fail_links(req);
2674 io_cqring_add_event(req, ret);
2675 io_put_req_find_next(req, nxt);
2676 return 0;
2677}
2678
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002679static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2680{
Jens Axboef8748882020-01-08 17:47:02 -07002681 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002682 unsigned lookup_flags;
2683 int ret;
2684
2685 if (sqe->ioprio || sqe->buf_index)
2686 return -EINVAL;
2687
2688 req->open.dfd = READ_ONCE(sqe->fd);
2689 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002690 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002691 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002692 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002693
Jens Axboec12cedf2020-01-08 17:41:21 -07002694 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002695 return -EINVAL;
2696
Jens Axboef8748882020-01-08 17:47:02 -07002697 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002698 if (IS_ERR(req->open.filename)) {
2699 ret = PTR_ERR(req->open.filename);
2700 req->open.filename = NULL;
2701 return ret;
2702 }
2703
2704 return 0;
2705}
2706
2707static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2708 bool force_nonblock)
2709{
2710 struct io_open *ctx = &req->open;
2711 unsigned lookup_flags;
2712 struct path path;
2713 struct kstat stat;
2714 int ret;
2715
2716 if (force_nonblock)
2717 return -EAGAIN;
2718
Jens Axboec12cedf2020-01-08 17:41:21 -07002719 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002720 return -EINVAL;
2721
2722retry:
2723 /* filename_lookup() drops it, keep a reference */
2724 ctx->filename->refcnt++;
2725
2726 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2727 NULL);
2728 if (ret)
2729 goto err;
2730
Jens Axboec12cedf2020-01-08 17:41:21 -07002731 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002732 path_put(&path);
2733 if (retry_estale(ret, lookup_flags)) {
2734 lookup_flags |= LOOKUP_REVAL;
2735 goto retry;
2736 }
2737 if (!ret)
2738 ret = cp_statx(&stat, ctx->buffer);
2739err:
2740 putname(ctx->filename);
2741 if (ret < 0)
2742 req_set_fail_links(req);
2743 io_cqring_add_event(req, ret);
2744 io_put_req_find_next(req, nxt);
2745 return 0;
2746}
2747
Jens Axboeb5dba592019-12-11 14:02:38 -07002748static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2749{
2750 /*
2751 * If we queue this for async, it must not be cancellable. That would
2752 * leave the 'file' in an undeterminate state.
2753 */
2754 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2755
2756 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2757 sqe->rw_flags || sqe->buf_index)
2758 return -EINVAL;
2759 if (sqe->flags & IOSQE_FIXED_FILE)
2760 return -EINVAL;
2761
2762 req->close.fd = READ_ONCE(sqe->fd);
2763 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002764 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002765 return -EBADF;
2766
2767 return 0;
2768}
2769
2770static void io_close_finish(struct io_wq_work **workptr)
2771{
2772 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2773 struct io_kiocb *nxt = NULL;
2774
2775 /* Invoked with files, we need to do the close */
2776 if (req->work.files) {
2777 int ret;
2778
2779 ret = filp_close(req->close.put_file, req->work.files);
2780 if (ret < 0) {
2781 req_set_fail_links(req);
2782 }
2783 io_cqring_add_event(req, ret);
2784 }
2785
2786 fput(req->close.put_file);
2787
2788 /* we bypassed the re-issue, drop the submission reference */
2789 io_put_req(req);
2790 io_put_req_find_next(req, &nxt);
2791 if (nxt)
2792 io_wq_assign_next(workptr, nxt);
2793}
2794
2795static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2796 bool force_nonblock)
2797{
2798 int ret;
2799
2800 req->close.put_file = NULL;
2801 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2802 if (ret < 0)
2803 return ret;
2804
2805 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002806 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002807 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002808
2809 /*
2810 * No ->flush(), safely close from here and just punt the
2811 * fput() to async context.
2812 */
2813 ret = filp_close(req->close.put_file, current->files);
2814
2815 if (ret < 0)
2816 req_set_fail_links(req);
2817 io_cqring_add_event(req, ret);
2818
2819 if (io_wq_current_is_worker()) {
2820 struct io_wq_work *old_work, *work;
2821
2822 old_work = work = &req->work;
2823 io_close_finish(&work);
2824 if (work && work != old_work)
2825 *nxt = container_of(work, struct io_kiocb, work);
2826 return 0;
2827 }
2828
2829eagain:
2830 req->work.func = io_close_finish;
2831 return -EAGAIN;
2832}
2833
Jens Axboe3529d8c2019-12-19 18:24:38 -07002834static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002835{
2836 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002837
2838 if (!req->file)
2839 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002840
2841 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2842 return -EINVAL;
2843 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2844 return -EINVAL;
2845
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002846 req->sync.off = READ_ONCE(sqe->off);
2847 req->sync.len = READ_ONCE(sqe->len);
2848 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002849 return 0;
2850}
2851
2852static void io_sync_file_range_finish(struct io_wq_work **workptr)
2853{
2854 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2855 struct io_kiocb *nxt = NULL;
2856 int ret;
2857
2858 if (io_req_cancelled(req))
2859 return;
2860
Jens Axboe9adbd452019-12-20 08:45:55 -07002861 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002862 req->sync.flags);
2863 if (ret < 0)
2864 req_set_fail_links(req);
2865 io_cqring_add_event(req, ret);
2866 io_put_req_find_next(req, &nxt);
2867 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002868 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002869}
2870
Jens Axboefc4df992019-12-10 14:38:45 -07002871static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002872 bool force_nonblock)
2873{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002874 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002875
2876 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002877 if (force_nonblock) {
2878 io_put_req(req);
2879 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002880 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002881 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002882
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002883 work = old_work = &req->work;
2884 io_sync_file_range_finish(&work);
2885 if (work && work != old_work)
2886 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002887 return 0;
2888}
2889
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002890#if defined(CONFIG_NET)
2891static void io_sendrecv_async(struct io_wq_work **workptr)
2892{
2893 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2894 struct iovec *iov = NULL;
2895
2896 if (req->io->rw.iov != req->io->rw.fast_iov)
2897 iov = req->io->msg.iov;
2898 io_wq_submit_work(workptr);
2899 kfree(iov);
2900}
2901#endif
2902
Jens Axboe3529d8c2019-12-19 18:24:38 -07002903static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002904{
Jens Axboe03b12302019-12-02 18:50:25 -07002905#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002906 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002907 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002908
Jens Axboee47293f2019-12-20 08:58:21 -07002909 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2910 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002911 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002912
Jens Axboefddafac2020-01-04 20:19:44 -07002913 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002914 return 0;
2915
Jens Axboed9688562019-12-09 19:35:20 -07002916 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002917 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002918 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002919#else
Jens Axboee47293f2019-12-20 08:58:21 -07002920 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002921#endif
2922}
2923
Jens Axboefc4df992019-12-10 14:38:45 -07002924static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2925 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002926{
2927#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002928 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002929 struct socket *sock;
2930 int ret;
2931
2932 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2933 return -EINVAL;
2934
2935 sock = sock_from_file(req->file, &ret);
2936 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002937 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002938 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002939 unsigned flags;
2940
Jens Axboe03b12302019-12-02 18:50:25 -07002941 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002942 kmsg = &req->io->msg;
2943 kmsg->msg.msg_name = &addr;
2944 /* if iov is set, it's allocated already */
2945 if (!kmsg->iov)
2946 kmsg->iov = kmsg->fast_iov;
2947 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002948 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002949 struct io_sr_msg *sr = &req->sr_msg;
2950
Jens Axboe0b416c32019-12-15 10:57:46 -07002951 kmsg = &io.msg;
2952 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002953
2954 io.msg.iov = io.msg.fast_iov;
2955 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2956 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002957 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002958 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002959 }
2960
Jens Axboee47293f2019-12-20 08:58:21 -07002961 flags = req->sr_msg.msg_flags;
2962 if (flags & MSG_DONTWAIT)
2963 req->flags |= REQ_F_NOWAIT;
2964 else if (force_nonblock)
2965 flags |= MSG_DONTWAIT;
2966
Jens Axboe0b416c32019-12-15 10:57:46 -07002967 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002968 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002969 if (req->io)
2970 return -EAGAIN;
2971 if (io_alloc_async_ctx(req))
2972 return -ENOMEM;
2973 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2974 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002975 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002976 }
2977 if (ret == -ERESTARTSYS)
2978 ret = -EINTR;
2979 }
2980
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002981 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002982 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002983 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002984 if (ret < 0)
2985 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002986 io_put_req_find_next(req, nxt);
2987 return 0;
2988#else
2989 return -EOPNOTSUPP;
2990#endif
2991}
2992
Jens Axboefddafac2020-01-04 20:19:44 -07002993static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2994 bool force_nonblock)
2995{
2996#if defined(CONFIG_NET)
2997 struct socket *sock;
2998 int ret;
2999
3000 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3001 return -EINVAL;
3002
3003 sock = sock_from_file(req->file, &ret);
3004 if (sock) {
3005 struct io_sr_msg *sr = &req->sr_msg;
3006 struct msghdr msg;
3007 struct iovec iov;
3008 unsigned flags;
3009
3010 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3011 &msg.msg_iter);
3012 if (ret)
3013 return ret;
3014
3015 msg.msg_name = NULL;
3016 msg.msg_control = NULL;
3017 msg.msg_controllen = 0;
3018 msg.msg_namelen = 0;
3019
3020 flags = req->sr_msg.msg_flags;
3021 if (flags & MSG_DONTWAIT)
3022 req->flags |= REQ_F_NOWAIT;
3023 else if (force_nonblock)
3024 flags |= MSG_DONTWAIT;
3025
3026 ret = __sys_sendmsg_sock(sock, &msg, flags);
3027 if (force_nonblock && ret == -EAGAIN)
3028 return -EAGAIN;
3029 if (ret == -ERESTARTSYS)
3030 ret = -EINTR;
3031 }
3032
3033 io_cqring_add_event(req, ret);
3034 if (ret < 0)
3035 req_set_fail_links(req);
3036 io_put_req_find_next(req, nxt);
3037 return 0;
3038#else
3039 return -EOPNOTSUPP;
3040#endif
3041}
3042
Jens Axboe3529d8c2019-12-19 18:24:38 -07003043static int io_recvmsg_prep(struct io_kiocb *req,
3044 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003045{
3046#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003047 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003048 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07003049
Jens Axboe3529d8c2019-12-19 18:24:38 -07003050 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3051 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
3052
Jens Axboefddafac2020-01-04 20:19:44 -07003053 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003054 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003055
Jens Axboed9688562019-12-09 19:35:20 -07003056 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003057 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003058 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003059#else
Jens Axboee47293f2019-12-20 08:58:21 -07003060 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003061#endif
3062}
3063
Jens Axboefc4df992019-12-10 14:38:45 -07003064static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3065 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003066{
3067#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003068 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003069 struct socket *sock;
3070 int ret;
3071
3072 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3073 return -EINVAL;
3074
3075 sock = sock_from_file(req->file, &ret);
3076 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003077 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003078 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003079 unsigned flags;
3080
Jens Axboe03b12302019-12-02 18:50:25 -07003081 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003082 kmsg = &req->io->msg;
3083 kmsg->msg.msg_name = &addr;
3084 /* if iov is set, it's allocated already */
3085 if (!kmsg->iov)
3086 kmsg->iov = kmsg->fast_iov;
3087 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003088 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003089 struct io_sr_msg *sr = &req->sr_msg;
3090
Jens Axboe0b416c32019-12-15 10:57:46 -07003091 kmsg = &io.msg;
3092 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003093
3094 io.msg.iov = io.msg.fast_iov;
3095 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3096 sr->msg_flags, &io.msg.uaddr,
3097 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003098 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003099 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003100 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003101
Jens Axboee47293f2019-12-20 08:58:21 -07003102 flags = req->sr_msg.msg_flags;
3103 if (flags & MSG_DONTWAIT)
3104 req->flags |= REQ_F_NOWAIT;
3105 else if (force_nonblock)
3106 flags |= MSG_DONTWAIT;
3107
3108 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3109 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003110 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003111 if (req->io)
3112 return -EAGAIN;
3113 if (io_alloc_async_ctx(req))
3114 return -ENOMEM;
3115 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3116 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003117 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003118 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003119 if (ret == -ERESTARTSYS)
3120 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003121 }
3122
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003123 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003124 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003125 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003126 if (ret < 0)
3127 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003128 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003129 return 0;
3130#else
3131 return -EOPNOTSUPP;
3132#endif
3133}
3134
Jens Axboefddafac2020-01-04 20:19:44 -07003135static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3136 bool force_nonblock)
3137{
3138#if defined(CONFIG_NET)
3139 struct socket *sock;
3140 int ret;
3141
3142 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3143 return -EINVAL;
3144
3145 sock = sock_from_file(req->file, &ret);
3146 if (sock) {
3147 struct io_sr_msg *sr = &req->sr_msg;
3148 struct msghdr msg;
3149 struct iovec iov;
3150 unsigned flags;
3151
3152 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3153 &msg.msg_iter);
3154 if (ret)
3155 return ret;
3156
3157 msg.msg_name = NULL;
3158 msg.msg_control = NULL;
3159 msg.msg_controllen = 0;
3160 msg.msg_namelen = 0;
3161 msg.msg_iocb = NULL;
3162 msg.msg_flags = 0;
3163
3164 flags = req->sr_msg.msg_flags;
3165 if (flags & MSG_DONTWAIT)
3166 req->flags |= REQ_F_NOWAIT;
3167 else if (force_nonblock)
3168 flags |= MSG_DONTWAIT;
3169
3170 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3171 if (force_nonblock && ret == -EAGAIN)
3172 return -EAGAIN;
3173 if (ret == -ERESTARTSYS)
3174 ret = -EINTR;
3175 }
3176
3177 io_cqring_add_event(req, ret);
3178 if (ret < 0)
3179 req_set_fail_links(req);
3180 io_put_req_find_next(req, nxt);
3181 return 0;
3182#else
3183 return -EOPNOTSUPP;
3184#endif
3185}
3186
3187
Jens Axboe3529d8c2019-12-19 18:24:38 -07003188static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003189{
3190#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003191 struct io_accept *accept = &req->accept;
3192
Jens Axboe17f2fe32019-10-17 14:42:58 -06003193 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3194 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003195 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003196 return -EINVAL;
3197
Jens Axboed55e5f52019-12-11 16:12:15 -07003198 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3199 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003200 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003201 return 0;
3202#else
3203 return -EOPNOTSUPP;
3204#endif
3205}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003206
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003207#if defined(CONFIG_NET)
3208static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3209 bool force_nonblock)
3210{
3211 struct io_accept *accept = &req->accept;
3212 unsigned file_flags;
3213 int ret;
3214
3215 file_flags = force_nonblock ? O_NONBLOCK : 0;
3216 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3217 accept->addr_len, accept->flags);
3218 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003219 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003220 if (ret == -ERESTARTSYS)
3221 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003222 if (ret < 0)
3223 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003224 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003225 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003226 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003227}
3228
3229static void io_accept_finish(struct io_wq_work **workptr)
3230{
3231 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3232 struct io_kiocb *nxt = NULL;
3233
3234 if (io_req_cancelled(req))
3235 return;
3236 __io_accept(req, &nxt, false);
3237 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003238 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003239}
3240#endif
3241
3242static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3243 bool force_nonblock)
3244{
3245#if defined(CONFIG_NET)
3246 int ret;
3247
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003248 ret = __io_accept(req, nxt, force_nonblock);
3249 if (ret == -EAGAIN && force_nonblock) {
3250 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003251 io_put_req(req);
3252 return -EAGAIN;
3253 }
3254 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003255#else
3256 return -EOPNOTSUPP;
3257#endif
3258}
3259
Jens Axboe3529d8c2019-12-19 18:24:38 -07003260static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003261{
3262#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003263 struct io_connect *conn = &req->connect;
3264 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003265
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003266 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3267 return -EINVAL;
3268 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3269 return -EINVAL;
3270
Jens Axboe3529d8c2019-12-19 18:24:38 -07003271 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3272 conn->addr_len = READ_ONCE(sqe->addr2);
3273
3274 if (!io)
3275 return 0;
3276
3277 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003278 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003279#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003280 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003281#endif
3282}
3283
Jens Axboefc4df992019-12-10 14:38:45 -07003284static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3285 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003286{
3287#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003288 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003289 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003290 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003291
Jens Axboef499a022019-12-02 16:28:46 -07003292 if (req->io) {
3293 io = req->io;
3294 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003295 ret = move_addr_to_kernel(req->connect.addr,
3296 req->connect.addr_len,
3297 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003298 if (ret)
3299 goto out;
3300 io = &__io;
3301 }
3302
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003303 file_flags = force_nonblock ? O_NONBLOCK : 0;
3304
3305 ret = __sys_connect_file(req->file, &io->connect.address,
3306 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003307 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003308 if (req->io)
3309 return -EAGAIN;
3310 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003311 ret = -ENOMEM;
3312 goto out;
3313 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003314 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003315 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003316 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003317 if (ret == -ERESTARTSYS)
3318 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003319out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003320 if (ret < 0)
3321 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003322 io_cqring_add_event(req, ret);
3323 io_put_req_find_next(req, nxt);
3324 return 0;
3325#else
3326 return -EOPNOTSUPP;
3327#endif
3328}
3329
Jens Axboe221c5eb2019-01-17 09:41:58 -07003330static void io_poll_remove_one(struct io_kiocb *req)
3331{
3332 struct io_poll_iocb *poll = &req->poll;
3333
3334 spin_lock(&poll->head->lock);
3335 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003336 if (!list_empty(&poll->wait.entry)) {
3337 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003338 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003339 }
3340 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003341 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003342}
3343
3344static void io_poll_remove_all(struct io_ring_ctx *ctx)
3345{
Jens Axboe78076bb2019-12-04 19:56:40 -07003346 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003347 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003348 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003349
3350 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003351 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3352 struct hlist_head *list;
3353
3354 list = &ctx->cancel_hash[i];
3355 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3356 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003357 }
3358 spin_unlock_irq(&ctx->completion_lock);
3359}
3360
Jens Axboe47f46762019-11-09 17:43:02 -07003361static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3362{
Jens Axboe78076bb2019-12-04 19:56:40 -07003363 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003364 struct io_kiocb *req;
3365
Jens Axboe78076bb2019-12-04 19:56:40 -07003366 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3367 hlist_for_each_entry(req, list, hash_node) {
3368 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003369 io_poll_remove_one(req);
3370 return 0;
3371 }
Jens Axboe47f46762019-11-09 17:43:02 -07003372 }
3373
3374 return -ENOENT;
3375}
3376
Jens Axboe3529d8c2019-12-19 18:24:38 -07003377static int io_poll_remove_prep(struct io_kiocb *req,
3378 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003379{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003380 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3381 return -EINVAL;
3382 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3383 sqe->poll_events)
3384 return -EINVAL;
3385
Jens Axboe0969e782019-12-17 18:40:57 -07003386 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003387 return 0;
3388}
3389
3390/*
3391 * Find a running poll command that matches one specified in sqe->addr,
3392 * and remove it if found.
3393 */
3394static int io_poll_remove(struct io_kiocb *req)
3395{
3396 struct io_ring_ctx *ctx = req->ctx;
3397 u64 addr;
3398 int ret;
3399
Jens Axboe0969e782019-12-17 18:40:57 -07003400 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003401 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003402 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003403 spin_unlock_irq(&ctx->completion_lock);
3404
Jens Axboe78e19bb2019-11-06 15:21:34 -07003405 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003406 if (ret < 0)
3407 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003408 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003409 return 0;
3410}
3411
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003412static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003413{
Jackie Liua197f662019-11-08 08:09:12 -07003414 struct io_ring_ctx *ctx = req->ctx;
3415
Jens Axboe8c838782019-03-12 15:48:16 -06003416 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003417 if (error)
3418 io_cqring_fill_event(req, error);
3419 else
3420 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003421 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003422}
3423
Jens Axboe561fb042019-10-24 07:25:42 -06003424static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003425{
Jens Axboe561fb042019-10-24 07:25:42 -06003426 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003427 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3428 struct io_poll_iocb *poll = &req->poll;
3429 struct poll_table_struct pt = { ._key = poll->events };
3430 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003431 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003432 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003433 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003434
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003435 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003436 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003437 ret = -ECANCELED;
3438 } else if (READ_ONCE(poll->canceled)) {
3439 ret = -ECANCELED;
3440 }
Jens Axboe561fb042019-10-24 07:25:42 -06003441
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003442 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003443 mask = vfs_poll(poll->file, &pt) & poll->events;
3444
3445 /*
3446 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3447 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3448 * synchronize with them. In the cancellation case the list_del_init
3449 * itself is not actually needed, but harmless so we keep it in to
3450 * avoid further branches in the fast path.
3451 */
3452 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003453 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003454 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003455 spin_unlock_irq(&ctx->completion_lock);
3456 return;
3457 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003458 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003459 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003460 spin_unlock_irq(&ctx->completion_lock);
3461
Jens Axboe8c838782019-03-12 15:48:16 -06003462 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003463
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003464 if (ret < 0)
3465 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003466 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003467 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003468 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003469}
3470
Jens Axboee94f1412019-12-19 12:06:02 -07003471static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3472{
Jens Axboee94f1412019-12-19 12:06:02 -07003473 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003474 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003475
Jens Axboec6ca97b302019-12-28 12:11:08 -07003476 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003477 spin_lock_irq(&ctx->completion_lock);
3478 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3479 hash_del(&req->hash_node);
3480 io_poll_complete(req, req->result, 0);
3481
Jens Axboe8237e042019-12-28 10:48:22 -07003482 if (refcount_dec_and_test(&req->refs) &&
3483 !io_req_multi_free(&rb, req)) {
3484 req->flags |= REQ_F_COMP_LOCKED;
3485 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003486 }
3487 }
3488 spin_unlock_irq(&ctx->completion_lock);
3489
3490 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003491 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003492}
3493
3494static void io_poll_flush(struct io_wq_work **workptr)
3495{
3496 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3497 struct llist_node *nodes;
3498
3499 nodes = llist_del_all(&req->ctx->poll_llist);
3500 if (nodes)
3501 __io_poll_flush(req->ctx, nodes);
3502}
3503
Jens Axboe221c5eb2019-01-17 09:41:58 -07003504static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3505 void *key)
3506{
Jens Axboee9444752019-11-26 15:02:04 -07003507 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003508 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3509 struct io_ring_ctx *ctx = req->ctx;
3510 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003511
3512 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003513 if (mask && !(mask & poll->events))
3514 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003515
Jens Axboe392edb42019-12-09 17:52:20 -07003516 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003517
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003518 /*
3519 * Run completion inline if we can. We're using trylock here because
3520 * we are violating the completion_lock -> poll wq lock ordering.
3521 * If we have a link timeout we're going to need the completion_lock
3522 * for finalizing the request, mark us as having grabbed that already.
3523 */
Jens Axboee94f1412019-12-19 12:06:02 -07003524 if (mask) {
3525 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003526
Jens Axboee94f1412019-12-19 12:06:02 -07003527 if (llist_empty(&ctx->poll_llist) &&
3528 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3529 hash_del(&req->hash_node);
3530 io_poll_complete(req, mask, 0);
3531 req->flags |= REQ_F_COMP_LOCKED;
3532 io_put_req(req);
3533 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3534
3535 io_cqring_ev_posted(ctx);
3536 req = NULL;
3537 } else {
3538 req->result = mask;
3539 req->llist_node.next = NULL;
3540 /* if the list wasn't empty, we're done */
3541 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3542 req = NULL;
3543 else
3544 req->work.func = io_poll_flush;
3545 }
Jens Axboe8c838782019-03-12 15:48:16 -06003546 }
Jens Axboee94f1412019-12-19 12:06:02 -07003547 if (req)
3548 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003549
Jens Axboe221c5eb2019-01-17 09:41:58 -07003550 return 1;
3551}
3552
3553struct io_poll_table {
3554 struct poll_table_struct pt;
3555 struct io_kiocb *req;
3556 int error;
3557};
3558
3559static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3560 struct poll_table_struct *p)
3561{
3562 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3563
3564 if (unlikely(pt->req->poll.head)) {
3565 pt->error = -EINVAL;
3566 return;
3567 }
3568
3569 pt->error = 0;
3570 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003571 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003572}
3573
Jens Axboeeac406c2019-11-14 12:09:58 -07003574static void io_poll_req_insert(struct io_kiocb *req)
3575{
3576 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003577 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003578
Jens Axboe78076bb2019-12-04 19:56:40 -07003579 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3580 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003581}
3582
Jens Axboe3529d8c2019-12-19 18:24:38 -07003583static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003584{
3585 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003586 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003587
3588 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3589 return -EINVAL;
3590 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3591 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003592 if (!poll->file)
3593 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003594
Jens Axboe221c5eb2019-01-17 09:41:58 -07003595 events = READ_ONCE(sqe->poll_events);
3596 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003597 return 0;
3598}
3599
3600static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3601{
3602 struct io_poll_iocb *poll = &req->poll;
3603 struct io_ring_ctx *ctx = req->ctx;
3604 struct io_poll_table ipt;
3605 bool cancel = false;
3606 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003607
3608 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003609 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003610
Jens Axboe221c5eb2019-01-17 09:41:58 -07003611 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003612 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003613 poll->canceled = false;
3614
3615 ipt.pt._qproc = io_poll_queue_proc;
3616 ipt.pt._key = poll->events;
3617 ipt.req = req;
3618 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3619
3620 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003621 INIT_LIST_HEAD(&poll->wait.entry);
3622 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3623 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003624
Jens Axboe36703242019-07-25 10:20:18 -06003625 INIT_LIST_HEAD(&req->list);
3626
Jens Axboe221c5eb2019-01-17 09:41:58 -07003627 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003628
3629 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003630 if (likely(poll->head)) {
3631 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003632 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003633 if (ipt.error)
3634 cancel = true;
3635 ipt.error = 0;
3636 mask = 0;
3637 }
3638 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003639 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003640 else if (cancel)
3641 WRITE_ONCE(poll->canceled, true);
3642 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003643 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003644 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003645 }
Jens Axboe8c838782019-03-12 15:48:16 -06003646 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003647 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003648 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003649 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003650 spin_unlock_irq(&ctx->completion_lock);
3651
Jens Axboe8c838782019-03-12 15:48:16 -06003652 if (mask) {
3653 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003654 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003655 }
Jens Axboe8c838782019-03-12 15:48:16 -06003656 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003657}
3658
Jens Axboe5262f562019-09-17 12:26:57 -06003659static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3660{
Jens Axboead8a48a2019-11-15 08:49:11 -07003661 struct io_timeout_data *data = container_of(timer,
3662 struct io_timeout_data, timer);
3663 struct io_kiocb *req = data->req;
3664 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003665 unsigned long flags;
3666
Jens Axboe5262f562019-09-17 12:26:57 -06003667 atomic_inc(&ctx->cq_timeouts);
3668
3669 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003670 /*
Jens Axboe11365042019-10-16 09:08:32 -06003671 * We could be racing with timeout deletion. If the list is empty,
3672 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003673 */
Jens Axboe842f9612019-10-29 12:34:10 -06003674 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003675 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003676
Jens Axboe11365042019-10-16 09:08:32 -06003677 /*
3678 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003679 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003680 * pointer will be increased, otherwise other timeout reqs may
3681 * return in advance without waiting for enough wait_nr.
3682 */
3683 prev = req;
3684 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3685 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003686 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003687 }
Jens Axboe842f9612019-10-29 12:34:10 -06003688
Jens Axboe78e19bb2019-11-06 15:21:34 -07003689 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003690 io_commit_cqring(ctx);
3691 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3692
3693 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003694 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003695 io_put_req(req);
3696 return HRTIMER_NORESTART;
3697}
3698
Jens Axboe47f46762019-11-09 17:43:02 -07003699static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3700{
3701 struct io_kiocb *req;
3702 int ret = -ENOENT;
3703
3704 list_for_each_entry(req, &ctx->timeout_list, list) {
3705 if (user_data == req->user_data) {
3706 list_del_init(&req->list);
3707 ret = 0;
3708 break;
3709 }
3710 }
3711
3712 if (ret == -ENOENT)
3713 return ret;
3714
Jens Axboe2d283902019-12-04 11:08:05 -07003715 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003716 if (ret == -1)
3717 return -EALREADY;
3718
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003719 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003720 io_cqring_fill_event(req, -ECANCELED);
3721 io_put_req(req);
3722 return 0;
3723}
3724
Jens Axboe3529d8c2019-12-19 18:24:38 -07003725static int io_timeout_remove_prep(struct io_kiocb *req,
3726 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003727{
Jens Axboeb29472e2019-12-17 18:50:29 -07003728 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3729 return -EINVAL;
3730 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3731 return -EINVAL;
3732
3733 req->timeout.addr = READ_ONCE(sqe->addr);
3734 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3735 if (req->timeout.flags)
3736 return -EINVAL;
3737
Jens Axboeb29472e2019-12-17 18:50:29 -07003738 return 0;
3739}
3740
Jens Axboe11365042019-10-16 09:08:32 -06003741/*
3742 * Remove or update an existing timeout command
3743 */
Jens Axboefc4df992019-12-10 14:38:45 -07003744static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003745{
3746 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003747 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003748
Jens Axboe11365042019-10-16 09:08:32 -06003749 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003750 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003751
Jens Axboe47f46762019-11-09 17:43:02 -07003752 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003753 io_commit_cqring(ctx);
3754 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003755 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003756 if (ret < 0)
3757 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003758 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003759 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003760}
3761
Jens Axboe3529d8c2019-12-19 18:24:38 -07003762static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003763 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003764{
Jens Axboead8a48a2019-11-15 08:49:11 -07003765 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003766 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003767
Jens Axboead8a48a2019-11-15 08:49:11 -07003768 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003769 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003770 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003771 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003772 if (sqe->off && is_timeout_link)
3773 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003774 flags = READ_ONCE(sqe->timeout_flags);
3775 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003776 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003777
Jens Axboe26a61672019-12-20 09:02:01 -07003778 req->timeout.count = READ_ONCE(sqe->off);
3779
Jens Axboe3529d8c2019-12-19 18:24:38 -07003780 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003781 return -ENOMEM;
3782
3783 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003784 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003785 req->flags |= REQ_F_TIMEOUT;
3786
3787 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003788 return -EFAULT;
3789
Jens Axboe11365042019-10-16 09:08:32 -06003790 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003791 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003792 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003793 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003794
Jens Axboead8a48a2019-11-15 08:49:11 -07003795 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3796 return 0;
3797}
3798
Jens Axboefc4df992019-12-10 14:38:45 -07003799static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003800{
3801 unsigned count;
3802 struct io_ring_ctx *ctx = req->ctx;
3803 struct io_timeout_data *data;
3804 struct list_head *entry;
3805 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003806
Jens Axboe2d283902019-12-04 11:08:05 -07003807 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003808
Jens Axboe5262f562019-09-17 12:26:57 -06003809 /*
3810 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003811 * timeout event to be satisfied. If it isn't set, then this is
3812 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003813 */
Jens Axboe26a61672019-12-20 09:02:01 -07003814 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003815 if (!count) {
3816 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3817 spin_lock_irq(&ctx->completion_lock);
3818 entry = ctx->timeout_list.prev;
3819 goto add;
3820 }
Jens Axboe5262f562019-09-17 12:26:57 -06003821
3822 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003823 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003824
3825 /*
3826 * Insertion sort, ensuring the first entry in the list is always
3827 * the one we need first.
3828 */
Jens Axboe5262f562019-09-17 12:26:57 -06003829 spin_lock_irq(&ctx->completion_lock);
3830 list_for_each_prev(entry, &ctx->timeout_list) {
3831 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003832 unsigned nxt_sq_head;
3833 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003834 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003835
Jens Axboe93bd25b2019-11-11 23:34:31 -07003836 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3837 continue;
3838
yangerkun5da0fb12019-10-15 21:59:29 +08003839 /*
3840 * Since cached_sq_head + count - 1 can overflow, use type long
3841 * long to store it.
3842 */
3843 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003844 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3845 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003846
3847 /*
3848 * cached_sq_head may overflow, and it will never overflow twice
3849 * once there is some timeout req still be valid.
3850 */
3851 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003852 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003853
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003854 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003855 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003856
3857 /*
3858 * Sequence of reqs after the insert one and itself should
3859 * be adjusted because each timeout req consumes a slot.
3860 */
3861 span++;
3862 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003863 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003864 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003865add:
Jens Axboe5262f562019-09-17 12:26:57 -06003866 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003867 data->timer.function = io_timeout_fn;
3868 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003869 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003870 return 0;
3871}
3872
Jens Axboe62755e32019-10-28 21:49:21 -06003873static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003874{
Jens Axboe62755e32019-10-28 21:49:21 -06003875 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003876
Jens Axboe62755e32019-10-28 21:49:21 -06003877 return req->user_data == (unsigned long) data;
3878}
3879
Jens Axboee977d6d2019-11-05 12:39:45 -07003880static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003881{
Jens Axboe62755e32019-10-28 21:49:21 -06003882 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003883 int ret = 0;
3884
Jens Axboe62755e32019-10-28 21:49:21 -06003885 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3886 switch (cancel_ret) {
3887 case IO_WQ_CANCEL_OK:
3888 ret = 0;
3889 break;
3890 case IO_WQ_CANCEL_RUNNING:
3891 ret = -EALREADY;
3892 break;
3893 case IO_WQ_CANCEL_NOTFOUND:
3894 ret = -ENOENT;
3895 break;
3896 }
3897
Jens Axboee977d6d2019-11-05 12:39:45 -07003898 return ret;
3899}
3900
Jens Axboe47f46762019-11-09 17:43:02 -07003901static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3902 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003903 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003904{
3905 unsigned long flags;
3906 int ret;
3907
3908 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3909 if (ret != -ENOENT) {
3910 spin_lock_irqsave(&ctx->completion_lock, flags);
3911 goto done;
3912 }
3913
3914 spin_lock_irqsave(&ctx->completion_lock, flags);
3915 ret = io_timeout_cancel(ctx, sqe_addr);
3916 if (ret != -ENOENT)
3917 goto done;
3918 ret = io_poll_cancel(ctx, sqe_addr);
3919done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003920 if (!ret)
3921 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003922 io_cqring_fill_event(req, ret);
3923 io_commit_cqring(ctx);
3924 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3925 io_cqring_ev_posted(ctx);
3926
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003927 if (ret < 0)
3928 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003929 io_put_req_find_next(req, nxt);
3930}
3931
Jens Axboe3529d8c2019-12-19 18:24:38 -07003932static int io_async_cancel_prep(struct io_kiocb *req,
3933 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003934{
Jens Axboefbf23842019-12-17 18:45:56 -07003935 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003936 return -EINVAL;
3937 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3938 sqe->cancel_flags)
3939 return -EINVAL;
3940
Jens Axboefbf23842019-12-17 18:45:56 -07003941 req->cancel.addr = READ_ONCE(sqe->addr);
3942 return 0;
3943}
3944
3945static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3946{
3947 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003948
3949 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003950 return 0;
3951}
3952
Jens Axboe05f3fb32019-12-09 11:22:50 -07003953static int io_files_update_prep(struct io_kiocb *req,
3954 const struct io_uring_sqe *sqe)
3955{
3956 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3957 return -EINVAL;
3958
3959 req->files_update.offset = READ_ONCE(sqe->off);
3960 req->files_update.nr_args = READ_ONCE(sqe->len);
3961 if (!req->files_update.nr_args)
3962 return -EINVAL;
3963 req->files_update.arg = READ_ONCE(sqe->addr);
3964 return 0;
3965}
3966
3967static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3968{
3969 struct io_ring_ctx *ctx = req->ctx;
3970 struct io_uring_files_update up;
3971 int ret;
3972
Jens Axboef86cd202020-01-29 13:46:44 -07003973 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07003974 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003975
3976 up.offset = req->files_update.offset;
3977 up.fds = req->files_update.arg;
3978
3979 mutex_lock(&ctx->uring_lock);
3980 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3981 mutex_unlock(&ctx->uring_lock);
3982
3983 if (ret < 0)
3984 req_set_fail_links(req);
3985 io_cqring_add_event(req, ret);
3986 io_put_req(req);
3987 return 0;
3988}
3989
Jens Axboe3529d8c2019-12-19 18:24:38 -07003990static int io_req_defer_prep(struct io_kiocb *req,
3991 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003992{
Jens Axboee7815732019-12-17 19:45:06 -07003993 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003994
Jens Axboef86cd202020-01-29 13:46:44 -07003995 if (io_op_defs[req->opcode].file_table) {
3996 ret = io_grab_files(req);
3997 if (unlikely(ret))
3998 return ret;
3999 }
4000
Jens Axboecccf0ee2020-01-27 16:34:48 -07004001 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4002
Jens Axboed625c6e2019-12-17 19:53:05 -07004003 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004004 case IORING_OP_NOP:
4005 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004006 case IORING_OP_READV:
4007 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004008 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004009 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004010 break;
4011 case IORING_OP_WRITEV:
4012 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004013 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004014 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004015 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004016 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004017 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004018 break;
4019 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004020 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004021 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004022 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004023 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004024 break;
4025 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004026 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004027 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004028 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004029 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004030 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004031 break;
4032 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004033 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004034 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004035 break;
Jens Axboef499a022019-12-02 16:28:46 -07004036 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004037 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004038 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004039 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004040 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004041 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004042 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004043 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004044 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004045 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004046 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004047 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004048 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004049 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004050 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004051 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004052 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004053 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004054 case IORING_OP_FALLOCATE:
4055 ret = io_fallocate_prep(req, sqe);
4056 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004057 case IORING_OP_OPENAT:
4058 ret = io_openat_prep(req, sqe);
4059 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004060 case IORING_OP_CLOSE:
4061 ret = io_close_prep(req, sqe);
4062 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004063 case IORING_OP_FILES_UPDATE:
4064 ret = io_files_update_prep(req, sqe);
4065 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004066 case IORING_OP_STATX:
4067 ret = io_statx_prep(req, sqe);
4068 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004069 case IORING_OP_FADVISE:
4070 ret = io_fadvise_prep(req, sqe);
4071 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004072 case IORING_OP_MADVISE:
4073 ret = io_madvise_prep(req, sqe);
4074 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004075 case IORING_OP_OPENAT2:
4076 ret = io_openat2_prep(req, sqe);
4077 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004078 default:
Jens Axboee7815732019-12-17 19:45:06 -07004079 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4080 req->opcode);
4081 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004082 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004083 }
4084
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004085 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004086}
4087
Jens Axboe3529d8c2019-12-19 18:24:38 -07004088static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004089{
Jackie Liua197f662019-11-08 08:09:12 -07004090 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004091 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004092
Bob Liu9d858b22019-11-13 18:06:25 +08004093 /* Still need defer if there is pending req in defer list. */
4094 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004095 return 0;
4096
Jens Axboe3529d8c2019-12-19 18:24:38 -07004097 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004098 return -EAGAIN;
4099
Jens Axboe3529d8c2019-12-19 18:24:38 -07004100 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004101 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004102 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004103
Jens Axboede0617e2019-04-06 21:51:27 -06004104 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004105 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004106 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004107 return 0;
4108 }
4109
Jens Axboe915967f2019-11-21 09:01:20 -07004110 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004111 list_add_tail(&req->list, &ctx->defer_list);
4112 spin_unlock_irq(&ctx->completion_lock);
4113 return -EIOCBQUEUED;
4114}
4115
Jens Axboe3529d8c2019-12-19 18:24:38 -07004116static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4117 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004118{
Jackie Liua197f662019-11-08 08:09:12 -07004119 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004120 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004121
Jens Axboed625c6e2019-12-17 19:53:05 -07004122 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004123 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004124 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004125 break;
4126 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004127 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004128 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004129 if (sqe) {
4130 ret = io_read_prep(req, sqe, force_nonblock);
4131 if (ret < 0)
4132 break;
4133 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004134 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004135 break;
4136 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004137 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004138 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004139 if (sqe) {
4140 ret = io_write_prep(req, sqe, force_nonblock);
4141 if (ret < 0)
4142 break;
4143 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004144 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004145 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004146 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004147 if (sqe) {
4148 ret = io_prep_fsync(req, sqe);
4149 if (ret < 0)
4150 break;
4151 }
Jens Axboefc4df992019-12-10 14:38:45 -07004152 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004153 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004154 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004155 if (sqe) {
4156 ret = io_poll_add_prep(req, sqe);
4157 if (ret)
4158 break;
4159 }
Jens Axboefc4df992019-12-10 14:38:45 -07004160 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004161 break;
4162 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004163 if (sqe) {
4164 ret = io_poll_remove_prep(req, sqe);
4165 if (ret < 0)
4166 break;
4167 }
Jens Axboefc4df992019-12-10 14:38:45 -07004168 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004169 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004170 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004171 if (sqe) {
4172 ret = io_prep_sfr(req, sqe);
4173 if (ret < 0)
4174 break;
4175 }
Jens Axboefc4df992019-12-10 14:38:45 -07004176 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004177 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004178 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004179 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004180 if (sqe) {
4181 ret = io_sendmsg_prep(req, sqe);
4182 if (ret < 0)
4183 break;
4184 }
Jens Axboefddafac2020-01-04 20:19:44 -07004185 if (req->opcode == IORING_OP_SENDMSG)
4186 ret = io_sendmsg(req, nxt, force_nonblock);
4187 else
4188 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004189 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004190 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004191 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004192 if (sqe) {
4193 ret = io_recvmsg_prep(req, sqe);
4194 if (ret)
4195 break;
4196 }
Jens Axboefddafac2020-01-04 20:19:44 -07004197 if (req->opcode == IORING_OP_RECVMSG)
4198 ret = io_recvmsg(req, nxt, force_nonblock);
4199 else
4200 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004201 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004202 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004203 if (sqe) {
4204 ret = io_timeout_prep(req, sqe, false);
4205 if (ret)
4206 break;
4207 }
Jens Axboefc4df992019-12-10 14:38:45 -07004208 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004209 break;
Jens Axboe11365042019-10-16 09:08:32 -06004210 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004211 if (sqe) {
4212 ret = io_timeout_remove_prep(req, sqe);
4213 if (ret)
4214 break;
4215 }
Jens Axboefc4df992019-12-10 14:38:45 -07004216 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004217 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004218 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004219 if (sqe) {
4220 ret = io_accept_prep(req, sqe);
4221 if (ret)
4222 break;
4223 }
Jens Axboefc4df992019-12-10 14:38:45 -07004224 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004225 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004226 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004227 if (sqe) {
4228 ret = io_connect_prep(req, sqe);
4229 if (ret)
4230 break;
4231 }
Jens Axboefc4df992019-12-10 14:38:45 -07004232 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004233 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004234 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004235 if (sqe) {
4236 ret = io_async_cancel_prep(req, sqe);
4237 if (ret)
4238 break;
4239 }
Jens Axboefc4df992019-12-10 14:38:45 -07004240 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004241 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004242 case IORING_OP_FALLOCATE:
4243 if (sqe) {
4244 ret = io_fallocate_prep(req, sqe);
4245 if (ret)
4246 break;
4247 }
4248 ret = io_fallocate(req, nxt, force_nonblock);
4249 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004250 case IORING_OP_OPENAT:
4251 if (sqe) {
4252 ret = io_openat_prep(req, sqe);
4253 if (ret)
4254 break;
4255 }
4256 ret = io_openat(req, nxt, force_nonblock);
4257 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004258 case IORING_OP_CLOSE:
4259 if (sqe) {
4260 ret = io_close_prep(req, sqe);
4261 if (ret)
4262 break;
4263 }
4264 ret = io_close(req, nxt, force_nonblock);
4265 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004266 case IORING_OP_FILES_UPDATE:
4267 if (sqe) {
4268 ret = io_files_update_prep(req, sqe);
4269 if (ret)
4270 break;
4271 }
4272 ret = io_files_update(req, force_nonblock);
4273 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004274 case IORING_OP_STATX:
4275 if (sqe) {
4276 ret = io_statx_prep(req, sqe);
4277 if (ret)
4278 break;
4279 }
4280 ret = io_statx(req, nxt, force_nonblock);
4281 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004282 case IORING_OP_FADVISE:
4283 if (sqe) {
4284 ret = io_fadvise_prep(req, sqe);
4285 if (ret)
4286 break;
4287 }
4288 ret = io_fadvise(req, nxt, force_nonblock);
4289 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004290 case IORING_OP_MADVISE:
4291 if (sqe) {
4292 ret = io_madvise_prep(req, sqe);
4293 if (ret)
4294 break;
4295 }
4296 ret = io_madvise(req, nxt, force_nonblock);
4297 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004298 case IORING_OP_OPENAT2:
4299 if (sqe) {
4300 ret = io_openat2_prep(req, sqe);
4301 if (ret)
4302 break;
4303 }
4304 ret = io_openat2(req, nxt, force_nonblock);
4305 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004306 default:
4307 ret = -EINVAL;
4308 break;
4309 }
4310
Jens Axboedef596e2019-01-09 08:59:42 -07004311 if (ret)
4312 return ret;
4313
4314 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004315 const bool in_async = io_wq_current_is_worker();
4316
Jens Axboe9e645e112019-05-10 16:07:28 -06004317 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004318 return -EAGAIN;
4319
Jens Axboe11ba8202020-01-15 21:51:17 -07004320 /* workqueue context doesn't hold uring_lock, grab it now */
4321 if (in_async)
4322 mutex_lock(&ctx->uring_lock);
4323
Jens Axboedef596e2019-01-09 08:59:42 -07004324 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004325
4326 if (in_async)
4327 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004328 }
4329
4330 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004331}
4332
Jens Axboe561fb042019-10-24 07:25:42 -06004333static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004334{
Jens Axboe561fb042019-10-24 07:25:42 -06004335 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004336 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004337 struct io_kiocb *nxt = NULL;
4338 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004339
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004340 /* if NO_CANCEL is set, we must still run the work */
4341 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4342 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004343 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004344 }
Jens Axboe31b51512019-01-18 22:56:34 -07004345
Jens Axboe561fb042019-10-24 07:25:42 -06004346 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004347 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4348 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004349 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004350 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004351 /*
4352 * We can get EAGAIN for polled IO even though we're
4353 * forcing a sync submission from here, since we can't
4354 * wait for request slots on the block side.
4355 */
4356 if (ret != -EAGAIN)
4357 break;
4358 cond_resched();
4359 } while (1);
4360 }
Jens Axboe31b51512019-01-18 22:56:34 -07004361
Jens Axboe561fb042019-10-24 07:25:42 -06004362 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004363 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004364
Jens Axboe561fb042019-10-24 07:25:42 -06004365 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004366 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004367 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004368 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004369 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004370
Jens Axboe561fb042019-10-24 07:25:42 -06004371 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004372 if (!ret && nxt)
4373 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004374}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004375
Jens Axboe15b71ab2019-12-11 11:20:36 -07004376static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004377{
Jens Axboed3656342019-12-18 09:50:26 -07004378 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004379 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004380 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4381 return 0;
4382 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004383}
4384
Jens Axboe65e19f52019-10-26 07:20:21 -06004385static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4386 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004387{
Jens Axboe65e19f52019-10-26 07:20:21 -06004388 struct fixed_file_table *table;
4389
Jens Axboe05f3fb32019-12-09 11:22:50 -07004390 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4391 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004392}
4393
Jens Axboe3529d8c2019-12-19 18:24:38 -07004394static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4395 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004396{
Jackie Liua197f662019-11-08 08:09:12 -07004397 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004398 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004399 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004400
Jens Axboe3529d8c2019-12-19 18:24:38 -07004401 flags = READ_ONCE(sqe->flags);
4402 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004403
Jens Axboed3656342019-12-18 09:50:26 -07004404 if (!io_req_needs_file(req, fd))
4405 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004406
4407 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004408 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004409 (unsigned) fd >= ctx->nr_user_files))
4410 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004411 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004412 req->file = io_file_from_index(ctx, fd);
4413 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004414 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004415 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004416 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004417 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004418 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004419 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004420 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004421 req->file = io_file_get(state, fd);
4422 if (unlikely(!req->file))
4423 return -EBADF;
4424 }
4425
4426 return 0;
4427}
4428
Jackie Liua197f662019-11-08 08:09:12 -07004429static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004430{
Jens Axboefcb323c2019-10-24 12:39:47 -06004431 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004432 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004433
Jens Axboef86cd202020-01-29 13:46:44 -07004434 if (req->work.files)
4435 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004436 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004437 return -EBADF;
4438
Jens Axboefcb323c2019-10-24 12:39:47 -06004439 rcu_read_lock();
4440 spin_lock_irq(&ctx->inflight_lock);
4441 /*
4442 * We use the f_ops->flush() handler to ensure that we can flush
4443 * out work accessing these files if the fd is closed. Check if
4444 * the fd has changed since we started down this path, and disallow
4445 * this operation if it has.
4446 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004447 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004448 list_add(&req->inflight_entry, &ctx->inflight_list);
4449 req->flags |= REQ_F_INFLIGHT;
4450 req->work.files = current->files;
4451 ret = 0;
4452 }
4453 spin_unlock_irq(&ctx->inflight_lock);
4454 rcu_read_unlock();
4455
4456 return ret;
4457}
4458
Jens Axboe2665abf2019-11-05 12:40:47 -07004459static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4460{
Jens Axboead8a48a2019-11-15 08:49:11 -07004461 struct io_timeout_data *data = container_of(timer,
4462 struct io_timeout_data, timer);
4463 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004464 struct io_ring_ctx *ctx = req->ctx;
4465 struct io_kiocb *prev = NULL;
4466 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004467
4468 spin_lock_irqsave(&ctx->completion_lock, flags);
4469
4470 /*
4471 * We don't expect the list to be empty, that will only happen if we
4472 * race with the completion of the linked work.
4473 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004474 if (!list_empty(&req->link_list)) {
4475 prev = list_entry(req->link_list.prev, struct io_kiocb,
4476 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004477 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004478 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004479 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4480 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004481 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004482 }
4483
4484 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4485
4486 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004487 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004488 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4489 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004490 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004491 } else {
4492 io_cqring_add_event(req, -ETIME);
4493 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004494 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004495 return HRTIMER_NORESTART;
4496}
4497
Jens Axboead8a48a2019-11-15 08:49:11 -07004498static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004499{
Jens Axboe76a46e02019-11-10 23:34:16 -07004500 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004501
Jens Axboe76a46e02019-11-10 23:34:16 -07004502 /*
4503 * If the list is now empty, then our linked request finished before
4504 * we got a chance to setup the timer
4505 */
4506 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004507 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004508 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004509
Jens Axboead8a48a2019-11-15 08:49:11 -07004510 data->timer.function = io_link_timeout_fn;
4511 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4512 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004513 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004514 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004515
Jens Axboe2665abf2019-11-05 12:40:47 -07004516 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004517 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004518}
4519
Jens Axboead8a48a2019-11-15 08:49:11 -07004520static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004521{
4522 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004523
Jens Axboe2665abf2019-11-05 12:40:47 -07004524 if (!(req->flags & REQ_F_LINK))
4525 return NULL;
4526
Pavel Begunkov44932332019-12-05 16:16:35 +03004527 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4528 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004529 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004530 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004531
Jens Axboe76a46e02019-11-10 23:34:16 -07004532 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004533 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004534}
4535
Jens Axboe3529d8c2019-12-19 18:24:38 -07004536static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004537{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004538 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004539 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004540 int ret;
4541
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004542again:
4543 linked_timeout = io_prep_linked_timeout(req);
4544
Jens Axboe3529d8c2019-12-19 18:24:38 -07004545 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004546
4547 /*
4548 * We async punt it if the file wasn't marked NOWAIT, or if the file
4549 * doesn't support non-blocking read/write attempts
4550 */
4551 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4552 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004553punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004554 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004555 ret = io_grab_files(req);
4556 if (ret)
4557 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004558 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004559
4560 /*
4561 * Queued up for async execution, worker will release
4562 * submit reference when the iocb is actually submitted.
4563 */
4564 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004565 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004566 }
Jens Axboee65ef562019-03-12 10:16:44 -06004567
Jens Axboefcb323c2019-10-24 12:39:47 -06004568err:
Jens Axboee65ef562019-03-12 10:16:44 -06004569 /* drop submission reference */
4570 io_put_req(req);
4571
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004572 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004573 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004574 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004575 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004576 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004577 }
4578
Jens Axboee65ef562019-03-12 10:16:44 -06004579 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004580 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004581 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004582 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004583 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004584 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004585done_req:
4586 if (nxt) {
4587 req = nxt;
4588 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004589
4590 if (req->flags & REQ_F_FORCE_ASYNC)
4591 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004592 goto again;
4593 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004594}
4595
Jens Axboe3529d8c2019-12-19 18:24:38 -07004596static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004597{
4598 int ret;
4599
Jens Axboe3529d8c2019-12-19 18:24:38 -07004600 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004601 if (ret) {
4602 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004603fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004604 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004605 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004606 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004607 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004608 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004609 ret = io_req_defer_prep(req, sqe);
4610 if (unlikely(ret < 0))
4611 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004612 /*
4613 * Never try inline submit of IOSQE_ASYNC is set, go straight
4614 * to async execution.
4615 */
4616 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4617 io_queue_async_work(req);
4618 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004619 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004620 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004621}
4622
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004623static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004624{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004625 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004626 io_cqring_add_event(req, -ECANCELED);
4627 io_double_put_req(req);
4628 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004629 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004630}
4631
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004632#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004633 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004634
Jens Axboe3529d8c2019-12-19 18:24:38 -07004635static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4636 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004637{
Jens Axboe75c6a032020-01-28 10:15:23 -07004638 const struct cred *old_creds = NULL;
Jackie Liua197f662019-11-08 08:09:12 -07004639 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004640 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004641 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004642
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004643 sqe_flags = READ_ONCE(sqe->flags);
4644
Jens Axboe9e645e112019-05-10 16:07:28 -06004645 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004646 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004647 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004648 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004649 }
Jens Axboe75c6a032020-01-28 10:15:23 -07004650
4651 id = READ_ONCE(sqe->personality);
4652 if (id) {
4653 const struct cred *personality_creds;
4654
4655 personality_creds = idr_find(&ctx->personality_idr, id);
4656 if (unlikely(!personality_creds)) {
4657 ret = -EINVAL;
4658 goto err_req;
4659 }
4660 old_creds = override_creds(personality_creds);
4661 }
4662
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004663 /* same numerical values with corresponding REQ_F_*, safe to copy */
4664 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4665 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004666
Jens Axboe3529d8c2019-12-19 18:24:38 -07004667 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004668 if (unlikely(ret)) {
4669err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004670 io_cqring_add_event(req, ret);
4671 io_double_put_req(req);
Jens Axboe75c6a032020-01-28 10:15:23 -07004672 if (old_creds)
4673 revert_creds(old_creds);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004674 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004675 }
4676
Jens Axboe9e645e112019-05-10 16:07:28 -06004677 /*
4678 * If we already have a head request, queue this one for async
4679 * submittal once the head completes. If we don't have a head but
4680 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4681 * submitted sync once the chain is complete. If none of those
4682 * conditions are true (normal request), then just queue it.
4683 */
4684 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004685 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004686
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004687 /*
4688 * Taking sequential execution of a link, draining both sides
4689 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4690 * requests in the link. So, it drains the head and the
4691 * next after the link request. The last one is done via
4692 * drain_next flag to persist the effect across calls.
4693 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004694 if (sqe_flags & IOSQE_IO_DRAIN) {
4695 head->flags |= REQ_F_IO_DRAIN;
4696 ctx->drain_next = 1;
4697 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004698 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004699 ret = -EAGAIN;
4700 goto err_req;
4701 }
4702
Jens Axboe3529d8c2019-12-19 18:24:38 -07004703 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004704 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004705 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004706 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004707 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004708 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004709 trace_io_uring_link(ctx, req, head);
4710 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004711
4712 /* last request of a link, enqueue the link */
4713 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4714 io_queue_link_head(head);
4715 *link = NULL;
4716 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004717 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004718 if (unlikely(ctx->drain_next)) {
4719 req->flags |= REQ_F_IO_DRAIN;
4720 req->ctx->drain_next = 0;
4721 }
4722 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4723 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004724 INIT_LIST_HEAD(&req->link_list);
4725 ret = io_req_defer_prep(req, sqe);
4726 if (ret)
4727 req->flags |= REQ_F_FAIL_LINK;
4728 *link = req;
4729 } else {
4730 io_queue_sqe(req, sqe);
4731 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004732 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004733
Jens Axboe75c6a032020-01-28 10:15:23 -07004734 if (old_creds)
4735 revert_creds(old_creds);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004736 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004737}
4738
Jens Axboe9a56a232019-01-09 09:06:50 -07004739/*
4740 * Batched submission is done, ensure local IO is flushed out.
4741 */
4742static void io_submit_state_end(struct io_submit_state *state)
4743{
4744 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004745 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004746 if (state->free_reqs)
4747 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4748 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004749}
4750
4751/*
4752 * Start submission side cache.
4753 */
4754static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004755 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004756{
4757 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004758 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004759 state->file = NULL;
4760 state->ios_left = max_ios;
4761}
4762
Jens Axboe2b188cc2019-01-07 10:46:33 -07004763static void io_commit_sqring(struct io_ring_ctx *ctx)
4764{
Hristo Venev75b28af2019-08-26 17:23:46 +00004765 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004766
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004767 /*
4768 * Ensure any loads from the SQEs are done at this point,
4769 * since once we write the new head, the application could
4770 * write new data to them.
4771 */
4772 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004773}
4774
4775/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004776 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004777 * that is mapped by userspace. This means that care needs to be taken to
4778 * ensure that reads are stable, as we cannot rely on userspace always
4779 * being a good citizen. If members of the sqe are validated and then later
4780 * used, it's important that those reads are done through READ_ONCE() to
4781 * prevent a re-load down the line.
4782 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004783static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4784 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004785{
Hristo Venev75b28af2019-08-26 17:23:46 +00004786 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004787 unsigned head;
4788
4789 /*
4790 * The cached sq head (or cq tail) serves two purposes:
4791 *
4792 * 1) allows us to batch the cost of updating the user visible
4793 * head updates.
4794 * 2) allows the kernel side to track the head on its own, even
4795 * though the application is the one updating it.
4796 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004797 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004798 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004799 /*
4800 * All io need record the previous position, if LINK vs DARIN,
4801 * it can be used to mark the position of the first IO in the
4802 * link list.
4803 */
4804 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004805 *sqe_ptr = &ctx->sq_sqes[head];
4806 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4807 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004808 ctx->cached_sq_head++;
4809 return true;
4810 }
4811
4812 /* drop invalid entries */
4813 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004814 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004815 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004816 return false;
4817}
4818
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004819static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004820 struct file *ring_file, int ring_fd,
4821 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004822{
4823 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004824 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004825 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004826 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004827
Jens Axboec4a2ed72019-11-21 21:01:26 -07004828 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004829 if (test_bit(0, &ctx->sq_check_overflow)) {
4830 if (!list_empty(&ctx->cq_overflow_list) &&
4831 !io_cqring_overflow_flush(ctx, false))
4832 return -EBUSY;
4833 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004834
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004835 /* make sure SQ entry isn't read before tail */
4836 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004837
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004838 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4839 return -EAGAIN;
4840
Jens Axboe6c271ce2019-01-10 11:22:30 -07004841 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004842 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004843 statep = &state;
4844 }
4845
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004846 ctx->ring_fd = ring_fd;
4847 ctx->ring_file = ring_file;
4848
Jens Axboe6c271ce2019-01-10 11:22:30 -07004849 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004850 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004851 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004852
Pavel Begunkov196be952019-11-07 01:41:06 +03004853 req = io_get_req(ctx, statep);
4854 if (unlikely(!req)) {
4855 if (!submitted)
4856 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004857 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004858 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004859 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004860 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004861 break;
4862 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004863
Jens Axboed3656342019-12-18 09:50:26 -07004864 /* will complete beyond this point, count as submitted */
4865 submitted++;
4866
4867 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4868 io_cqring_add_event(req, -EINVAL);
4869 io_double_put_req(req);
4870 break;
4871 }
4872
4873 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004874 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4875 if (!mm_fault) {
4876 use_mm(ctx->sqo_mm);
4877 *mm = ctx->sqo_mm;
4878 }
4879 }
4880
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004881 req->has_user = *mm != NULL;
4882 req->in_async = async;
4883 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07004884 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
4885 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004886 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004887 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004888 }
4889
Pavel Begunkov9466f432020-01-25 22:34:01 +03004890 if (unlikely(submitted != nr)) {
4891 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
4892
4893 percpu_ref_put_many(&ctx->refs, nr - ref_used);
4894 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004895 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004896 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004897 if (statep)
4898 io_submit_state_end(&state);
4899
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004900 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4901 io_commit_sqring(ctx);
4902
Jens Axboe6c271ce2019-01-10 11:22:30 -07004903 return submitted;
4904}
4905
4906static int io_sq_thread(void *data)
4907{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004908 struct io_ring_ctx *ctx = data;
4909 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004910 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004911 mm_segment_t old_fs;
4912 DEFINE_WAIT(wait);
4913 unsigned inflight;
4914 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004915 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004916
Jens Axboe206aefd2019-11-07 18:27:42 -07004917 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004918
Jens Axboe6c271ce2019-01-10 11:22:30 -07004919 old_fs = get_fs();
4920 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004921 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004922
Jens Axboec1edbf52019-11-10 16:56:04 -07004923 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004924 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004925 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004926
4927 if (inflight) {
4928 unsigned nr_events = 0;
4929
4930 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004931 /*
4932 * inflight is the count of the maximum possible
4933 * entries we submitted, but it can be smaller
4934 * if we dropped some of them. If we don't have
4935 * poll entries available, then we know that we
4936 * have nothing left to poll for. Reset the
4937 * inflight count to zero in that case.
4938 */
4939 mutex_lock(&ctx->uring_lock);
4940 if (!list_empty(&ctx->poll_list))
4941 __io_iopoll_check(ctx, &nr_events, 0);
4942 else
4943 inflight = 0;
4944 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004945 } else {
4946 /*
4947 * Normal IO, just pretend everything completed.
4948 * We don't have to poll completions for that.
4949 */
4950 nr_events = inflight;
4951 }
4952
4953 inflight -= nr_events;
4954 if (!inflight)
4955 timeout = jiffies + ctx->sq_thread_idle;
4956 }
4957
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004958 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004959
4960 /*
4961 * If submit got -EBUSY, flag us as needing the application
4962 * to enter the kernel to reap and flush events.
4963 */
4964 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004965 /*
4966 * We're polling. If we're within the defined idle
4967 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004968 * to sleep. The exception is if we got EBUSY doing
4969 * more IO, we should wait for the application to
4970 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004971 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004972 if (inflight ||
4973 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004974 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004975 continue;
4976 }
4977
4978 /*
4979 * Drop cur_mm before scheduling, we can't hold it for
4980 * long periods (or over schedule()). Do this before
4981 * adding ourselves to the waitqueue, as the unuse/drop
4982 * may sleep.
4983 */
4984 if (cur_mm) {
4985 unuse_mm(cur_mm);
4986 mmput(cur_mm);
4987 cur_mm = NULL;
4988 }
4989
4990 prepare_to_wait(&ctx->sqo_wait, &wait,
4991 TASK_INTERRUPTIBLE);
4992
4993 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004994 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004995 /* make sure to read SQ tail after writing flags */
4996 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004997
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004998 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004999 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005000 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005001 finish_wait(&ctx->sqo_wait, &wait);
5002 break;
5003 }
5004 if (signal_pending(current))
5005 flush_signals(current);
5006 schedule();
5007 finish_wait(&ctx->sqo_wait, &wait);
5008
Hristo Venev75b28af2019-08-26 17:23:46 +00005009 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005010 continue;
5011 }
5012 finish_wait(&ctx->sqo_wait, &wait);
5013
Hristo Venev75b28af2019-08-26 17:23:46 +00005014 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005015 }
5016
Jens Axboe8a4955f2019-12-09 14:52:35 -07005017 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005018 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005019 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005020 if (ret > 0)
5021 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005022 }
5023
5024 set_fs(old_fs);
5025 if (cur_mm) {
5026 unuse_mm(cur_mm);
5027 mmput(cur_mm);
5028 }
Jens Axboe181e4482019-11-25 08:52:30 -07005029 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005030
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005031 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005032
Jens Axboe6c271ce2019-01-10 11:22:30 -07005033 return 0;
5034}
5035
Jens Axboebda52162019-09-24 13:47:15 -06005036struct io_wait_queue {
5037 struct wait_queue_entry wq;
5038 struct io_ring_ctx *ctx;
5039 unsigned to_wait;
5040 unsigned nr_timeouts;
5041};
5042
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005043static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005044{
5045 struct io_ring_ctx *ctx = iowq->ctx;
5046
5047 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005048 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005049 * started waiting. For timeouts, we always want to return to userspace,
5050 * regardless of event count.
5051 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005052 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005053 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5054}
5055
5056static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5057 int wake_flags, void *key)
5058{
5059 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5060 wq);
5061
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005062 /* use noflush == true, as we can't safely rely on locking context */
5063 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005064 return -1;
5065
5066 return autoremove_wake_function(curr, mode, wake_flags, key);
5067}
5068
Jens Axboe2b188cc2019-01-07 10:46:33 -07005069/*
5070 * Wait until events become available, if we don't already have some. The
5071 * application must reap them itself, as they reside on the shared cq ring.
5072 */
5073static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5074 const sigset_t __user *sig, size_t sigsz)
5075{
Jens Axboebda52162019-09-24 13:47:15 -06005076 struct io_wait_queue iowq = {
5077 .wq = {
5078 .private = current,
5079 .func = io_wake_function,
5080 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5081 },
5082 .ctx = ctx,
5083 .to_wait = min_events,
5084 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005085 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005086 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005087
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005088 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005089 return 0;
5090
5091 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005092#ifdef CONFIG_COMPAT
5093 if (in_compat_syscall())
5094 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005095 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005096 else
5097#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005098 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005099
Jens Axboe2b188cc2019-01-07 10:46:33 -07005100 if (ret)
5101 return ret;
5102 }
5103
Jens Axboebda52162019-09-24 13:47:15 -06005104 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005105 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005106 do {
5107 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5108 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005109 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005110 break;
5111 schedule();
5112 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005113 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005114 break;
5115 }
5116 } while (1);
5117 finish_wait(&ctx->wait, &iowq.wq);
5118
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005119 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005120
Hristo Venev75b28af2019-08-26 17:23:46 +00005121 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005122}
5123
Jens Axboe6b063142019-01-10 22:13:58 -07005124static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5125{
5126#if defined(CONFIG_UNIX)
5127 if (ctx->ring_sock) {
5128 struct sock *sock = ctx->ring_sock->sk;
5129 struct sk_buff *skb;
5130
5131 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5132 kfree_skb(skb);
5133 }
5134#else
5135 int i;
5136
Jens Axboe65e19f52019-10-26 07:20:21 -06005137 for (i = 0; i < ctx->nr_user_files; i++) {
5138 struct file *file;
5139
5140 file = io_file_from_index(ctx, i);
5141 if (file)
5142 fput(file);
5143 }
Jens Axboe6b063142019-01-10 22:13:58 -07005144#endif
5145}
5146
Jens Axboe05f3fb32019-12-09 11:22:50 -07005147static void io_file_ref_kill(struct percpu_ref *ref)
5148{
5149 struct fixed_file_data *data;
5150
5151 data = container_of(ref, struct fixed_file_data, refs);
5152 complete(&data->done);
5153}
5154
Jens Axboe6b063142019-01-10 22:13:58 -07005155static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5156{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005157 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005158 unsigned nr_tables, i;
5159
Jens Axboe05f3fb32019-12-09 11:22:50 -07005160 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005161 return -ENXIO;
5162
Jens Axboe05f3fb32019-12-09 11:22:50 -07005163 /* protect against inflight atomic switch, which drops the ref */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005164 percpu_ref_get(&data->refs);
Jens Axboee46a7952020-01-17 11:15:34 -07005165 /* wait for existing switches */
5166 flush_work(&data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005167 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5168 wait_for_completion(&data->done);
5169 percpu_ref_put(&data->refs);
Jens Axboee46a7952020-01-17 11:15:34 -07005170 /* flush potential new switch */
5171 flush_work(&data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005172 percpu_ref_exit(&data->refs);
5173
Jens Axboe6b063142019-01-10 22:13:58 -07005174 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005175 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5176 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005177 kfree(data->table[i].files);
5178 kfree(data->table);
5179 kfree(data);
5180 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005181 ctx->nr_user_files = 0;
5182 return 0;
5183}
5184
Jens Axboe6c271ce2019-01-10 11:22:30 -07005185static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5186{
5187 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005188 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005189 /*
5190 * The park is a bit of a work-around, without it we get
5191 * warning spews on shutdown with SQPOLL set and affinity
5192 * set to a single CPU.
5193 */
Jens Axboe06058632019-04-13 09:26:03 -06005194 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005195 kthread_stop(ctx->sqo_thread);
5196 ctx->sqo_thread = NULL;
5197 }
5198}
5199
Jens Axboe6b063142019-01-10 22:13:58 -07005200static void io_finish_async(struct io_ring_ctx *ctx)
5201{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005202 io_sq_thread_stop(ctx);
5203
Jens Axboe561fb042019-10-24 07:25:42 -06005204 if (ctx->io_wq) {
5205 io_wq_destroy(ctx->io_wq);
5206 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005207 }
5208}
5209
5210#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005211/*
5212 * Ensure the UNIX gc is aware of our file set, so we are certain that
5213 * the io_uring can be safely unregistered on process exit, even if we have
5214 * loops in the file referencing.
5215 */
5216static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5217{
5218 struct sock *sk = ctx->ring_sock->sk;
5219 struct scm_fp_list *fpl;
5220 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005221 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005222
5223 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5224 unsigned long inflight = ctx->user->unix_inflight + nr;
5225
5226 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5227 return -EMFILE;
5228 }
5229
5230 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5231 if (!fpl)
5232 return -ENOMEM;
5233
5234 skb = alloc_skb(0, GFP_KERNEL);
5235 if (!skb) {
5236 kfree(fpl);
5237 return -ENOMEM;
5238 }
5239
5240 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005241
Jens Axboe08a45172019-10-03 08:11:03 -06005242 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005243 fpl->user = get_uid(ctx->user);
5244 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005245 struct file *file = io_file_from_index(ctx, i + offset);
5246
5247 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005248 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005249 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005250 unix_inflight(fpl->user, fpl->fp[nr_files]);
5251 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005252 }
5253
Jens Axboe08a45172019-10-03 08:11:03 -06005254 if (nr_files) {
5255 fpl->max = SCM_MAX_FD;
5256 fpl->count = nr_files;
5257 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005258 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005259 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5260 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005261
Jens Axboe08a45172019-10-03 08:11:03 -06005262 for (i = 0; i < nr_files; i++)
5263 fput(fpl->fp[i]);
5264 } else {
5265 kfree_skb(skb);
5266 kfree(fpl);
5267 }
Jens Axboe6b063142019-01-10 22:13:58 -07005268
5269 return 0;
5270}
5271
5272/*
5273 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5274 * causes regular reference counting to break down. We rely on the UNIX
5275 * garbage collection to take care of this problem for us.
5276 */
5277static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5278{
5279 unsigned left, total;
5280 int ret = 0;
5281
5282 total = 0;
5283 left = ctx->nr_user_files;
5284 while (left) {
5285 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005286
5287 ret = __io_sqe_files_scm(ctx, this_files, total);
5288 if (ret)
5289 break;
5290 left -= this_files;
5291 total += this_files;
5292 }
5293
5294 if (!ret)
5295 return 0;
5296
5297 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005298 struct file *file = io_file_from_index(ctx, total);
5299
5300 if (file)
5301 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005302 total++;
5303 }
5304
5305 return ret;
5306}
5307#else
5308static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5309{
5310 return 0;
5311}
5312#endif
5313
Jens Axboe65e19f52019-10-26 07:20:21 -06005314static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5315 unsigned nr_files)
5316{
5317 int i;
5318
5319 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005320 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005321 unsigned this_files;
5322
5323 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5324 table->files = kcalloc(this_files, sizeof(struct file *),
5325 GFP_KERNEL);
5326 if (!table->files)
5327 break;
5328 nr_files -= this_files;
5329 }
5330
5331 if (i == nr_tables)
5332 return 0;
5333
5334 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005335 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005336 kfree(table->files);
5337 }
5338 return 1;
5339}
5340
Jens Axboe05f3fb32019-12-09 11:22:50 -07005341static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005342{
5343#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005344 struct sock *sock = ctx->ring_sock->sk;
5345 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5346 struct sk_buff *skb;
5347 int i;
5348
5349 __skb_queue_head_init(&list);
5350
5351 /*
5352 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5353 * remove this entry and rearrange the file array.
5354 */
5355 skb = skb_dequeue(head);
5356 while (skb) {
5357 struct scm_fp_list *fp;
5358
5359 fp = UNIXCB(skb).fp;
5360 for (i = 0; i < fp->count; i++) {
5361 int left;
5362
5363 if (fp->fp[i] != file)
5364 continue;
5365
5366 unix_notinflight(fp->user, fp->fp[i]);
5367 left = fp->count - 1 - i;
5368 if (left) {
5369 memmove(&fp->fp[i], &fp->fp[i + 1],
5370 left * sizeof(struct file *));
5371 }
5372 fp->count--;
5373 if (!fp->count) {
5374 kfree_skb(skb);
5375 skb = NULL;
5376 } else {
5377 __skb_queue_tail(&list, skb);
5378 }
5379 fput(file);
5380 file = NULL;
5381 break;
5382 }
5383
5384 if (!file)
5385 break;
5386
5387 __skb_queue_tail(&list, skb);
5388
5389 skb = skb_dequeue(head);
5390 }
5391
5392 if (skb_peek(&list)) {
5393 spin_lock_irq(&head->lock);
5394 while ((skb = __skb_dequeue(&list)) != NULL)
5395 __skb_queue_tail(head, skb);
5396 spin_unlock_irq(&head->lock);
5397 }
5398#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005399 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005400#endif
5401}
5402
Jens Axboe05f3fb32019-12-09 11:22:50 -07005403struct io_file_put {
5404 struct llist_node llist;
5405 struct file *file;
5406 struct completion *done;
5407};
5408
5409static void io_ring_file_ref_switch(struct work_struct *work)
5410{
5411 struct io_file_put *pfile, *tmp;
5412 struct fixed_file_data *data;
5413 struct llist_node *node;
5414
5415 data = container_of(work, struct fixed_file_data, ref_work);
5416
5417 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5418 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5419 io_ring_file_put(data->ctx, pfile->file);
5420 if (pfile->done)
5421 complete(pfile->done);
5422 else
5423 kfree(pfile);
5424 }
5425 }
5426
5427 percpu_ref_get(&data->refs);
5428 percpu_ref_switch_to_percpu(&data->refs);
5429}
5430
5431static void io_file_data_ref_zero(struct percpu_ref *ref)
5432{
5433 struct fixed_file_data *data;
5434
5435 data = container_of(ref, struct fixed_file_data, refs);
5436
5437 /* we can't safely switch from inside this context, punt to wq */
5438 queue_work(system_wq, &data->ref_work);
5439}
5440
5441static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5442 unsigned nr_args)
5443{
5444 __s32 __user *fds = (__s32 __user *) arg;
5445 unsigned nr_tables;
5446 struct file *file;
5447 int fd, ret = 0;
5448 unsigned i;
5449
5450 if (ctx->file_data)
5451 return -EBUSY;
5452 if (!nr_args)
5453 return -EINVAL;
5454 if (nr_args > IORING_MAX_FIXED_FILES)
5455 return -EMFILE;
5456
5457 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5458 if (!ctx->file_data)
5459 return -ENOMEM;
5460 ctx->file_data->ctx = ctx;
5461 init_completion(&ctx->file_data->done);
5462
5463 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5464 ctx->file_data->table = kcalloc(nr_tables,
5465 sizeof(struct fixed_file_table),
5466 GFP_KERNEL);
5467 if (!ctx->file_data->table) {
5468 kfree(ctx->file_data);
5469 ctx->file_data = NULL;
5470 return -ENOMEM;
5471 }
5472
5473 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5474 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5475 kfree(ctx->file_data->table);
5476 kfree(ctx->file_data);
5477 ctx->file_data = NULL;
5478 return -ENOMEM;
5479 }
5480 ctx->file_data->put_llist.first = NULL;
5481 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5482
5483 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5484 percpu_ref_exit(&ctx->file_data->refs);
5485 kfree(ctx->file_data->table);
5486 kfree(ctx->file_data);
5487 ctx->file_data = NULL;
5488 return -ENOMEM;
5489 }
5490
5491 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5492 struct fixed_file_table *table;
5493 unsigned index;
5494
5495 ret = -EFAULT;
5496 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5497 break;
5498 /* allow sparse sets */
5499 if (fd == -1) {
5500 ret = 0;
5501 continue;
5502 }
5503
5504 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5505 index = i & IORING_FILE_TABLE_MASK;
5506 file = fget(fd);
5507
5508 ret = -EBADF;
5509 if (!file)
5510 break;
5511
5512 /*
5513 * Don't allow io_uring instances to be registered. If UNIX
5514 * isn't enabled, then this causes a reference cycle and this
5515 * instance can never get freed. If UNIX is enabled we'll
5516 * handle it just fine, but there's still no point in allowing
5517 * a ring fd as it doesn't support regular read/write anyway.
5518 */
5519 if (file->f_op == &io_uring_fops) {
5520 fput(file);
5521 break;
5522 }
5523 ret = 0;
5524 table->files[index] = file;
5525 }
5526
5527 if (ret) {
5528 for (i = 0; i < ctx->nr_user_files; i++) {
5529 file = io_file_from_index(ctx, i);
5530 if (file)
5531 fput(file);
5532 }
5533 for (i = 0; i < nr_tables; i++)
5534 kfree(ctx->file_data->table[i].files);
5535
5536 kfree(ctx->file_data->table);
5537 kfree(ctx->file_data);
5538 ctx->file_data = NULL;
5539 ctx->nr_user_files = 0;
5540 return ret;
5541 }
5542
5543 ret = io_sqe_files_scm(ctx);
5544 if (ret)
5545 io_sqe_files_unregister(ctx);
5546
5547 return ret;
5548}
5549
Jens Axboec3a31e62019-10-03 13:59:56 -06005550static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5551 int index)
5552{
5553#if defined(CONFIG_UNIX)
5554 struct sock *sock = ctx->ring_sock->sk;
5555 struct sk_buff_head *head = &sock->sk_receive_queue;
5556 struct sk_buff *skb;
5557
5558 /*
5559 * See if we can merge this file into an existing skb SCM_RIGHTS
5560 * file set. If there's no room, fall back to allocating a new skb
5561 * and filling it in.
5562 */
5563 spin_lock_irq(&head->lock);
5564 skb = skb_peek(head);
5565 if (skb) {
5566 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5567
5568 if (fpl->count < SCM_MAX_FD) {
5569 __skb_unlink(skb, head);
5570 spin_unlock_irq(&head->lock);
5571 fpl->fp[fpl->count] = get_file(file);
5572 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5573 fpl->count++;
5574 spin_lock_irq(&head->lock);
5575 __skb_queue_head(head, skb);
5576 } else {
5577 skb = NULL;
5578 }
5579 }
5580 spin_unlock_irq(&head->lock);
5581
5582 if (skb) {
5583 fput(file);
5584 return 0;
5585 }
5586
5587 return __io_sqe_files_scm(ctx, 1, index);
5588#else
5589 return 0;
5590#endif
5591}
5592
Jens Axboe05f3fb32019-12-09 11:22:50 -07005593static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005594{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005595 struct fixed_file_data *data;
5596
5597 data = container_of(ref, struct fixed_file_data, refs);
5598 clear_bit(FFD_F_ATOMIC, &data->state);
5599}
5600
5601static bool io_queue_file_removal(struct fixed_file_data *data,
5602 struct file *file)
5603{
5604 struct io_file_put *pfile, pfile_stack;
5605 DECLARE_COMPLETION_ONSTACK(done);
5606
5607 /*
5608 * If we fail allocating the struct we need for doing async reomval
5609 * of this file, just punt to sync and wait for it.
5610 */
5611 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5612 if (!pfile) {
5613 pfile = &pfile_stack;
5614 pfile->done = &done;
5615 }
5616
5617 pfile->file = file;
5618 llist_add(&pfile->llist, &data->put_llist);
5619
5620 if (pfile == &pfile_stack) {
5621 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5622 percpu_ref_put(&data->refs);
5623 percpu_ref_switch_to_atomic(&data->refs,
5624 io_atomic_switch);
5625 }
5626 wait_for_completion(&done);
5627 flush_work(&data->ref_work);
5628 return false;
5629 }
5630
5631 return true;
5632}
5633
5634static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5635 struct io_uring_files_update *up,
5636 unsigned nr_args)
5637{
5638 struct fixed_file_data *data = ctx->file_data;
5639 bool ref_switch = false;
5640 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005641 __s32 __user *fds;
5642 int fd, i, err;
5643 __u32 done;
5644
Jens Axboe05f3fb32019-12-09 11:22:50 -07005645 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005646 return -EOVERFLOW;
5647 if (done > ctx->nr_user_files)
5648 return -EINVAL;
5649
5650 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005651 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005652 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005653 struct fixed_file_table *table;
5654 unsigned index;
5655
Jens Axboec3a31e62019-10-03 13:59:56 -06005656 err = 0;
5657 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5658 err = -EFAULT;
5659 break;
5660 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005661 i = array_index_nospec(up->offset, ctx->nr_user_files);
5662 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005663 index = i & IORING_FILE_TABLE_MASK;
5664 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005665 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005666 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005667 if (io_queue_file_removal(data, file))
5668 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005669 }
5670 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005671 file = fget(fd);
5672 if (!file) {
5673 err = -EBADF;
5674 break;
5675 }
5676 /*
5677 * Don't allow io_uring instances to be registered. If
5678 * UNIX isn't enabled, then this causes a reference
5679 * cycle and this instance can never get freed. If UNIX
5680 * is enabled we'll handle it just fine, but there's
5681 * still no point in allowing a ring fd as it doesn't
5682 * support regular read/write anyway.
5683 */
5684 if (file->f_op == &io_uring_fops) {
5685 fput(file);
5686 err = -EBADF;
5687 break;
5688 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005689 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005690 err = io_sqe_file_register(ctx, file, i);
5691 if (err)
5692 break;
5693 }
5694 nr_args--;
5695 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005696 up->offset++;
5697 }
5698
5699 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5700 percpu_ref_put(&data->refs);
5701 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005702 }
5703
5704 return done ? done : err;
5705}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005706static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5707 unsigned nr_args)
5708{
5709 struct io_uring_files_update up;
5710
5711 if (!ctx->file_data)
5712 return -ENXIO;
5713 if (!nr_args)
5714 return -EINVAL;
5715 if (copy_from_user(&up, arg, sizeof(up)))
5716 return -EFAULT;
5717 if (up.resv)
5718 return -EINVAL;
5719
5720 return __io_sqe_files_update(ctx, &up, nr_args);
5721}
Jens Axboec3a31e62019-10-03 13:59:56 -06005722
Jens Axboe7d723062019-11-12 22:31:31 -07005723static void io_put_work(struct io_wq_work *work)
5724{
5725 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5726
5727 io_put_req(req);
5728}
5729
5730static void io_get_work(struct io_wq_work *work)
5731{
5732 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5733
5734 refcount_inc(&req->refs);
5735}
5736
Pavel Begunkov24369c22020-01-28 03:15:48 +03005737static int io_init_wq_offload(struct io_ring_ctx *ctx,
5738 struct io_uring_params *p)
5739{
5740 struct io_wq_data data;
5741 struct fd f;
5742 struct io_ring_ctx *ctx_attach;
5743 unsigned int concurrency;
5744 int ret = 0;
5745
5746 data.user = ctx->user;
5747 data.get_work = io_get_work;
5748 data.put_work = io_put_work;
5749
5750 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5751 /* Do QD, or 4 * CPUS, whatever is smallest */
5752 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5753
5754 ctx->io_wq = io_wq_create(concurrency, &data);
5755 if (IS_ERR(ctx->io_wq)) {
5756 ret = PTR_ERR(ctx->io_wq);
5757 ctx->io_wq = NULL;
5758 }
5759 return ret;
5760 }
5761
5762 f = fdget(p->wq_fd);
5763 if (!f.file)
5764 return -EBADF;
5765
5766 if (f.file->f_op != &io_uring_fops) {
5767 ret = -EINVAL;
5768 goto out_fput;
5769 }
5770
5771 ctx_attach = f.file->private_data;
5772 /* @io_wq is protected by holding the fd */
5773 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5774 ret = -EINVAL;
5775 goto out_fput;
5776 }
5777
5778 ctx->io_wq = ctx_attach->io_wq;
5779out_fput:
5780 fdput(f);
5781 return ret;
5782}
5783
Jens Axboe6c271ce2019-01-10 11:22:30 -07005784static int io_sq_offload_start(struct io_ring_ctx *ctx,
5785 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005786{
5787 int ret;
5788
Jens Axboe6c271ce2019-01-10 11:22:30 -07005789 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005790 mmgrab(current->mm);
5791 ctx->sqo_mm = current->mm;
5792
Jens Axboe6c271ce2019-01-10 11:22:30 -07005793 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005794 ret = -EPERM;
5795 if (!capable(CAP_SYS_ADMIN))
5796 goto err;
5797
Jens Axboe917257d2019-04-13 09:28:55 -06005798 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5799 if (!ctx->sq_thread_idle)
5800 ctx->sq_thread_idle = HZ;
5801
Jens Axboe6c271ce2019-01-10 11:22:30 -07005802 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005803 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005804
Jens Axboe917257d2019-04-13 09:28:55 -06005805 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005806 if (cpu >= nr_cpu_ids)
5807 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005808 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005809 goto err;
5810
Jens Axboe6c271ce2019-01-10 11:22:30 -07005811 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5812 ctx, cpu,
5813 "io_uring-sq");
5814 } else {
5815 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5816 "io_uring-sq");
5817 }
5818 if (IS_ERR(ctx->sqo_thread)) {
5819 ret = PTR_ERR(ctx->sqo_thread);
5820 ctx->sqo_thread = NULL;
5821 goto err;
5822 }
5823 wake_up_process(ctx->sqo_thread);
5824 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5825 /* Can't have SQ_AFF without SQPOLL */
5826 ret = -EINVAL;
5827 goto err;
5828 }
5829
Pavel Begunkov24369c22020-01-28 03:15:48 +03005830 ret = io_init_wq_offload(ctx, p);
5831 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005832 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005833
5834 return 0;
5835err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005836 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005837 mmdrop(ctx->sqo_mm);
5838 ctx->sqo_mm = NULL;
5839 return ret;
5840}
5841
5842static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5843{
5844 atomic_long_sub(nr_pages, &user->locked_vm);
5845}
5846
5847static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5848{
5849 unsigned long page_limit, cur_pages, new_pages;
5850
5851 /* Don't allow more pages than we can safely lock */
5852 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5853
5854 do {
5855 cur_pages = atomic_long_read(&user->locked_vm);
5856 new_pages = cur_pages + nr_pages;
5857 if (new_pages > page_limit)
5858 return -ENOMEM;
5859 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5860 new_pages) != cur_pages);
5861
5862 return 0;
5863}
5864
5865static void io_mem_free(void *ptr)
5866{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005867 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005868
Mark Rutland52e04ef2019-04-30 17:30:21 +01005869 if (!ptr)
5870 return;
5871
5872 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005873 if (put_page_testzero(page))
5874 free_compound_page(page);
5875}
5876
5877static void *io_mem_alloc(size_t size)
5878{
5879 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5880 __GFP_NORETRY;
5881
5882 return (void *) __get_free_pages(gfp_flags, get_order(size));
5883}
5884
Hristo Venev75b28af2019-08-26 17:23:46 +00005885static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5886 size_t *sq_offset)
5887{
5888 struct io_rings *rings;
5889 size_t off, sq_array_size;
5890
5891 off = struct_size(rings, cqes, cq_entries);
5892 if (off == SIZE_MAX)
5893 return SIZE_MAX;
5894
5895#ifdef CONFIG_SMP
5896 off = ALIGN(off, SMP_CACHE_BYTES);
5897 if (off == 0)
5898 return SIZE_MAX;
5899#endif
5900
5901 sq_array_size = array_size(sizeof(u32), sq_entries);
5902 if (sq_array_size == SIZE_MAX)
5903 return SIZE_MAX;
5904
5905 if (check_add_overflow(off, sq_array_size, &off))
5906 return SIZE_MAX;
5907
5908 if (sq_offset)
5909 *sq_offset = off;
5910
5911 return off;
5912}
5913
Jens Axboe2b188cc2019-01-07 10:46:33 -07005914static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5915{
Hristo Venev75b28af2019-08-26 17:23:46 +00005916 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005917
Hristo Venev75b28af2019-08-26 17:23:46 +00005918 pages = (size_t)1 << get_order(
5919 rings_size(sq_entries, cq_entries, NULL));
5920 pages += (size_t)1 << get_order(
5921 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005922
Hristo Venev75b28af2019-08-26 17:23:46 +00005923 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005924}
5925
Jens Axboeedafcce2019-01-09 09:16:05 -07005926static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5927{
5928 int i, j;
5929
5930 if (!ctx->user_bufs)
5931 return -ENXIO;
5932
5933 for (i = 0; i < ctx->nr_user_bufs; i++) {
5934 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5935
5936 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005937 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005938
5939 if (ctx->account_mem)
5940 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005941 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005942 imu->nr_bvecs = 0;
5943 }
5944
5945 kfree(ctx->user_bufs);
5946 ctx->user_bufs = NULL;
5947 ctx->nr_user_bufs = 0;
5948 return 0;
5949}
5950
5951static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5952 void __user *arg, unsigned index)
5953{
5954 struct iovec __user *src;
5955
5956#ifdef CONFIG_COMPAT
5957 if (ctx->compat) {
5958 struct compat_iovec __user *ciovs;
5959 struct compat_iovec ciov;
5960
5961 ciovs = (struct compat_iovec __user *) arg;
5962 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5963 return -EFAULT;
5964
Jens Axboed55e5f52019-12-11 16:12:15 -07005965 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005966 dst->iov_len = ciov.iov_len;
5967 return 0;
5968 }
5969#endif
5970 src = (struct iovec __user *) arg;
5971 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5972 return -EFAULT;
5973 return 0;
5974}
5975
5976static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5977 unsigned nr_args)
5978{
5979 struct vm_area_struct **vmas = NULL;
5980 struct page **pages = NULL;
5981 int i, j, got_pages = 0;
5982 int ret = -EINVAL;
5983
5984 if (ctx->user_bufs)
5985 return -EBUSY;
5986 if (!nr_args || nr_args > UIO_MAXIOV)
5987 return -EINVAL;
5988
5989 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5990 GFP_KERNEL);
5991 if (!ctx->user_bufs)
5992 return -ENOMEM;
5993
5994 for (i = 0; i < nr_args; i++) {
5995 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5996 unsigned long off, start, end, ubuf;
5997 int pret, nr_pages;
5998 struct iovec iov;
5999 size_t size;
6000
6001 ret = io_copy_iov(ctx, &iov, arg, i);
6002 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006003 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006004
6005 /*
6006 * Don't impose further limits on the size and buffer
6007 * constraints here, we'll -EINVAL later when IO is
6008 * submitted if they are wrong.
6009 */
6010 ret = -EFAULT;
6011 if (!iov.iov_base || !iov.iov_len)
6012 goto err;
6013
6014 /* arbitrary limit, but we need something */
6015 if (iov.iov_len > SZ_1G)
6016 goto err;
6017
6018 ubuf = (unsigned long) iov.iov_base;
6019 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6020 start = ubuf >> PAGE_SHIFT;
6021 nr_pages = end - start;
6022
6023 if (ctx->account_mem) {
6024 ret = io_account_mem(ctx->user, nr_pages);
6025 if (ret)
6026 goto err;
6027 }
6028
6029 ret = 0;
6030 if (!pages || nr_pages > got_pages) {
6031 kfree(vmas);
6032 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006033 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006034 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006035 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006036 sizeof(struct vm_area_struct *),
6037 GFP_KERNEL);
6038 if (!pages || !vmas) {
6039 ret = -ENOMEM;
6040 if (ctx->account_mem)
6041 io_unaccount_mem(ctx->user, nr_pages);
6042 goto err;
6043 }
6044 got_pages = nr_pages;
6045 }
6046
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006047 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006048 GFP_KERNEL);
6049 ret = -ENOMEM;
6050 if (!imu->bvec) {
6051 if (ctx->account_mem)
6052 io_unaccount_mem(ctx->user, nr_pages);
6053 goto err;
6054 }
6055
6056 ret = 0;
6057 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07006058 pret = get_user_pages(ubuf, nr_pages,
6059 FOLL_WRITE | FOLL_LONGTERM,
6060 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006061 if (pret == nr_pages) {
6062 /* don't support file backed memory */
6063 for (j = 0; j < nr_pages; j++) {
6064 struct vm_area_struct *vma = vmas[j];
6065
6066 if (vma->vm_file &&
6067 !is_file_hugepages(vma->vm_file)) {
6068 ret = -EOPNOTSUPP;
6069 break;
6070 }
6071 }
6072 } else {
6073 ret = pret < 0 ? pret : -EFAULT;
6074 }
6075 up_read(&current->mm->mmap_sem);
6076 if (ret) {
6077 /*
6078 * if we did partial map, or found file backed vmas,
6079 * release any pages we did get
6080 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006081 if (pret > 0)
6082 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006083 if (ctx->account_mem)
6084 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006085 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006086 goto err;
6087 }
6088
6089 off = ubuf & ~PAGE_MASK;
6090 size = iov.iov_len;
6091 for (j = 0; j < nr_pages; j++) {
6092 size_t vec_len;
6093
6094 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6095 imu->bvec[j].bv_page = pages[j];
6096 imu->bvec[j].bv_len = vec_len;
6097 imu->bvec[j].bv_offset = off;
6098 off = 0;
6099 size -= vec_len;
6100 }
6101 /* store original address for later verification */
6102 imu->ubuf = ubuf;
6103 imu->len = iov.iov_len;
6104 imu->nr_bvecs = nr_pages;
6105
6106 ctx->nr_user_bufs++;
6107 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006108 kvfree(pages);
6109 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006110 return 0;
6111err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006112 kvfree(pages);
6113 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006114 io_sqe_buffer_unregister(ctx);
6115 return ret;
6116}
6117
Jens Axboe9b402842019-04-11 11:45:41 -06006118static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6119{
6120 __s32 __user *fds = arg;
6121 int fd;
6122
6123 if (ctx->cq_ev_fd)
6124 return -EBUSY;
6125
6126 if (copy_from_user(&fd, fds, sizeof(*fds)))
6127 return -EFAULT;
6128
6129 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6130 if (IS_ERR(ctx->cq_ev_fd)) {
6131 int ret = PTR_ERR(ctx->cq_ev_fd);
6132 ctx->cq_ev_fd = NULL;
6133 return ret;
6134 }
6135
6136 return 0;
6137}
6138
6139static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6140{
6141 if (ctx->cq_ev_fd) {
6142 eventfd_ctx_put(ctx->cq_ev_fd);
6143 ctx->cq_ev_fd = NULL;
6144 return 0;
6145 }
6146
6147 return -ENXIO;
6148}
6149
Jens Axboe2b188cc2019-01-07 10:46:33 -07006150static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6151{
Jens Axboe6b063142019-01-10 22:13:58 -07006152 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006153 if (ctx->sqo_mm)
6154 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006155
6156 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006157 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006158 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006159 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006160
Jens Axboe2b188cc2019-01-07 10:46:33 -07006161#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006162 if (ctx->ring_sock) {
6163 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006164 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006165 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006166#endif
6167
Hristo Venev75b28af2019-08-26 17:23:46 +00006168 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006169 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006170
6171 percpu_ref_exit(&ctx->refs);
6172 if (ctx->account_mem)
6173 io_unaccount_mem(ctx->user,
6174 ring_pages(ctx->sq_entries, ctx->cq_entries));
6175 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006176 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006177 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006178 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006179 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006180 kfree(ctx);
6181}
6182
6183static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6184{
6185 struct io_ring_ctx *ctx = file->private_data;
6186 __poll_t mask = 0;
6187
6188 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006189 /*
6190 * synchronizes with barrier from wq_has_sleeper call in
6191 * io_commit_cqring
6192 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006193 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006194 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6195 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006196 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006197 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006198 mask |= EPOLLIN | EPOLLRDNORM;
6199
6200 return mask;
6201}
6202
6203static int io_uring_fasync(int fd, struct file *file, int on)
6204{
6205 struct io_ring_ctx *ctx = file->private_data;
6206
6207 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6208}
6209
Jens Axboe071698e2020-01-28 10:04:42 -07006210static int io_remove_personalities(int id, void *p, void *data)
6211{
6212 struct io_ring_ctx *ctx = data;
6213 const struct cred *cred;
6214
6215 cred = idr_remove(&ctx->personality_idr, id);
6216 if (cred)
6217 put_cred(cred);
6218 return 0;
6219}
6220
Jens Axboe2b188cc2019-01-07 10:46:33 -07006221static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6222{
6223 mutex_lock(&ctx->uring_lock);
6224 percpu_ref_kill(&ctx->refs);
6225 mutex_unlock(&ctx->uring_lock);
6226
Jens Axboe5262f562019-09-17 12:26:57 -06006227 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006228 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006229
6230 if (ctx->io_wq)
6231 io_wq_cancel_all(ctx->io_wq);
6232
Jens Axboedef596e2019-01-09 08:59:42 -07006233 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006234 /* if we failed setting up the ctx, we might not have any rings */
6235 if (ctx->rings)
6236 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006237 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006238 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006239 io_ring_ctx_free(ctx);
6240}
6241
6242static int io_uring_release(struct inode *inode, struct file *file)
6243{
6244 struct io_ring_ctx *ctx = file->private_data;
6245
6246 file->private_data = NULL;
6247 io_ring_ctx_wait_and_kill(ctx);
6248 return 0;
6249}
6250
Jens Axboefcb323c2019-10-24 12:39:47 -06006251static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6252 struct files_struct *files)
6253{
6254 struct io_kiocb *req;
6255 DEFINE_WAIT(wait);
6256
6257 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006258 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006259
6260 spin_lock_irq(&ctx->inflight_lock);
6261 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006262 if (req->work.files != files)
6263 continue;
6264 /* req is being completed, ignore */
6265 if (!refcount_inc_not_zero(&req->refs))
6266 continue;
6267 cancel_req = req;
6268 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006269 }
Jens Axboe768134d2019-11-10 20:30:53 -07006270 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006271 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006272 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006273 spin_unlock_irq(&ctx->inflight_lock);
6274
Jens Axboe768134d2019-11-10 20:30:53 -07006275 /* We need to keep going until we don't find a matching req */
6276 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006277 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006278
6279 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6280 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006281 schedule();
6282 }
Jens Axboe768134d2019-11-10 20:30:53 -07006283 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006284}
6285
6286static int io_uring_flush(struct file *file, void *data)
6287{
6288 struct io_ring_ctx *ctx = file->private_data;
6289
6290 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006291 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6292 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006293 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006294 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006295 return 0;
6296}
6297
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006298static void *io_uring_validate_mmap_request(struct file *file,
6299 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006300{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006301 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006302 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006303 struct page *page;
6304 void *ptr;
6305
6306 switch (offset) {
6307 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006308 case IORING_OFF_CQ_RING:
6309 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006310 break;
6311 case IORING_OFF_SQES:
6312 ptr = ctx->sq_sqes;
6313 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006314 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006315 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006316 }
6317
6318 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006319 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006320 return ERR_PTR(-EINVAL);
6321
6322 return ptr;
6323}
6324
6325#ifdef CONFIG_MMU
6326
6327static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6328{
6329 size_t sz = vma->vm_end - vma->vm_start;
6330 unsigned long pfn;
6331 void *ptr;
6332
6333 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6334 if (IS_ERR(ptr))
6335 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006336
6337 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6338 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6339}
6340
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006341#else /* !CONFIG_MMU */
6342
6343static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6344{
6345 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6346}
6347
6348static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6349{
6350 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6351}
6352
6353static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6354 unsigned long addr, unsigned long len,
6355 unsigned long pgoff, unsigned long flags)
6356{
6357 void *ptr;
6358
6359 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6360 if (IS_ERR(ptr))
6361 return PTR_ERR(ptr);
6362
6363 return (unsigned long) ptr;
6364}
6365
6366#endif /* !CONFIG_MMU */
6367
Jens Axboe2b188cc2019-01-07 10:46:33 -07006368SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6369 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6370 size_t, sigsz)
6371{
6372 struct io_ring_ctx *ctx;
6373 long ret = -EBADF;
6374 int submitted = 0;
6375 struct fd f;
6376
Jens Axboe6c271ce2019-01-10 11:22:30 -07006377 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006378 return -EINVAL;
6379
6380 f = fdget(fd);
6381 if (!f.file)
6382 return -EBADF;
6383
6384 ret = -EOPNOTSUPP;
6385 if (f.file->f_op != &io_uring_fops)
6386 goto out_fput;
6387
6388 ret = -ENXIO;
6389 ctx = f.file->private_data;
6390 if (!percpu_ref_tryget(&ctx->refs))
6391 goto out_fput;
6392
Jens Axboe6c271ce2019-01-10 11:22:30 -07006393 /*
6394 * For SQ polling, the thread will do all submissions and completions.
6395 * Just return the requested submit count, and wake the thread if
6396 * we were asked to.
6397 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006398 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006399 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006400 if (!list_empty_careful(&ctx->cq_overflow_list))
6401 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006402 if (flags & IORING_ENTER_SQ_WAKEUP)
6403 wake_up(&ctx->sqo_wait);
6404 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006405 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006406 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006407
Jens Axboe44d28272020-01-16 19:00:24 -07006408 if (current->mm != ctx->sqo_mm ||
6409 current_cred() != ctx->creds) {
6410 ret = -EPERM;
6411 goto out;
6412 }
6413
Jens Axboe2b188cc2019-01-07 10:46:33 -07006414 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006415 /* already have mm, so io_submit_sqes() won't try to grab it */
6416 cur_mm = ctx->sqo_mm;
6417 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6418 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006419 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006420
6421 if (submitted != to_submit)
6422 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006423 }
6424 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006425 unsigned nr_events = 0;
6426
Jens Axboe2b188cc2019-01-07 10:46:33 -07006427 min_complete = min(min_complete, ctx->cq_entries);
6428
Jens Axboedef596e2019-01-09 08:59:42 -07006429 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006430 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006431 } else {
6432 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6433 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006434 }
6435
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006436out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006437 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006438out_fput:
6439 fdput(f);
6440 return submitted ? submitted : ret;
6441}
6442
6443static const struct file_operations io_uring_fops = {
6444 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006445 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006446 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006447#ifndef CONFIG_MMU
6448 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6449 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6450#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006451 .poll = io_uring_poll,
6452 .fasync = io_uring_fasync,
6453};
6454
6455static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6456 struct io_uring_params *p)
6457{
Hristo Venev75b28af2019-08-26 17:23:46 +00006458 struct io_rings *rings;
6459 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006460
Hristo Venev75b28af2019-08-26 17:23:46 +00006461 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6462 if (size == SIZE_MAX)
6463 return -EOVERFLOW;
6464
6465 rings = io_mem_alloc(size);
6466 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006467 return -ENOMEM;
6468
Hristo Venev75b28af2019-08-26 17:23:46 +00006469 ctx->rings = rings;
6470 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6471 rings->sq_ring_mask = p->sq_entries - 1;
6472 rings->cq_ring_mask = p->cq_entries - 1;
6473 rings->sq_ring_entries = p->sq_entries;
6474 rings->cq_ring_entries = p->cq_entries;
6475 ctx->sq_mask = rings->sq_ring_mask;
6476 ctx->cq_mask = rings->cq_ring_mask;
6477 ctx->sq_entries = rings->sq_ring_entries;
6478 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006479
6480 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006481 if (size == SIZE_MAX) {
6482 io_mem_free(ctx->rings);
6483 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006484 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006485 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006486
6487 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006488 if (!ctx->sq_sqes) {
6489 io_mem_free(ctx->rings);
6490 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006491 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006492 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006493
Jens Axboe2b188cc2019-01-07 10:46:33 -07006494 return 0;
6495}
6496
6497/*
6498 * Allocate an anonymous fd, this is what constitutes the application
6499 * visible backing of an io_uring instance. The application mmaps this
6500 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6501 * we have to tie this fd to a socket for file garbage collection purposes.
6502 */
6503static int io_uring_get_fd(struct io_ring_ctx *ctx)
6504{
6505 struct file *file;
6506 int ret;
6507
6508#if defined(CONFIG_UNIX)
6509 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6510 &ctx->ring_sock);
6511 if (ret)
6512 return ret;
6513#endif
6514
6515 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6516 if (ret < 0)
6517 goto err;
6518
6519 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6520 O_RDWR | O_CLOEXEC);
6521 if (IS_ERR(file)) {
6522 put_unused_fd(ret);
6523 ret = PTR_ERR(file);
6524 goto err;
6525 }
6526
6527#if defined(CONFIG_UNIX)
6528 ctx->ring_sock->file = file;
6529#endif
6530 fd_install(ret, file);
6531 return ret;
6532err:
6533#if defined(CONFIG_UNIX)
6534 sock_release(ctx->ring_sock);
6535 ctx->ring_sock = NULL;
6536#endif
6537 return ret;
6538}
6539
6540static int io_uring_create(unsigned entries, struct io_uring_params *p)
6541{
6542 struct user_struct *user = NULL;
6543 struct io_ring_ctx *ctx;
6544 bool account_mem;
6545 int ret;
6546
Jens Axboe8110c1a2019-12-28 15:39:54 -07006547 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006548 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006549 if (entries > IORING_MAX_ENTRIES) {
6550 if (!(p->flags & IORING_SETUP_CLAMP))
6551 return -EINVAL;
6552 entries = IORING_MAX_ENTRIES;
6553 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006554
6555 /*
6556 * Use twice as many entries for the CQ ring. It's possible for the
6557 * application to drive a higher depth than the size of the SQ ring,
6558 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006559 * some flexibility in overcommitting a bit. If the application has
6560 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6561 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006562 */
6563 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006564 if (p->flags & IORING_SETUP_CQSIZE) {
6565 /*
6566 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6567 * to a power-of-two, if it isn't already. We do NOT impose
6568 * any cq vs sq ring sizing.
6569 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006570 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006571 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006572 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6573 if (!(p->flags & IORING_SETUP_CLAMP))
6574 return -EINVAL;
6575 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6576 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006577 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6578 } else {
6579 p->cq_entries = 2 * p->sq_entries;
6580 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006581
6582 user = get_uid(current_user());
6583 account_mem = !capable(CAP_IPC_LOCK);
6584
6585 if (account_mem) {
6586 ret = io_account_mem(user,
6587 ring_pages(p->sq_entries, p->cq_entries));
6588 if (ret) {
6589 free_uid(user);
6590 return ret;
6591 }
6592 }
6593
6594 ctx = io_ring_ctx_alloc(p);
6595 if (!ctx) {
6596 if (account_mem)
6597 io_unaccount_mem(user, ring_pages(p->sq_entries,
6598 p->cq_entries));
6599 free_uid(user);
6600 return -ENOMEM;
6601 }
6602 ctx->compat = in_compat_syscall();
6603 ctx->account_mem = account_mem;
6604 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006605 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006606
6607 ret = io_allocate_scq_urings(ctx, p);
6608 if (ret)
6609 goto err;
6610
Jens Axboe6c271ce2019-01-10 11:22:30 -07006611 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006612 if (ret)
6613 goto err;
6614
Jens Axboe2b188cc2019-01-07 10:46:33 -07006615 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006616 p->sq_off.head = offsetof(struct io_rings, sq.head);
6617 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6618 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6619 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6620 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6621 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6622 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006623
6624 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006625 p->cq_off.head = offsetof(struct io_rings, cq.head);
6626 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6627 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6628 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6629 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6630 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006631
Jens Axboe044c1ab2019-10-28 09:15:33 -06006632 /*
6633 * Install ring fd as the very last thing, so we don't risk someone
6634 * having closed it before we finish setup
6635 */
6636 ret = io_uring_get_fd(ctx);
6637 if (ret < 0)
6638 goto err;
6639
Jens Axboeda8c9692019-12-02 18:51:26 -07006640 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006641 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6642 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006643 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006644 return ret;
6645err:
6646 io_ring_ctx_wait_and_kill(ctx);
6647 return ret;
6648}
6649
6650/*
6651 * Sets up an aio uring context, and returns the fd. Applications asks for a
6652 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6653 * params structure passed in.
6654 */
6655static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6656{
6657 struct io_uring_params p;
6658 long ret;
6659 int i;
6660
6661 if (copy_from_user(&p, params, sizeof(p)))
6662 return -EFAULT;
6663 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6664 if (p.resv[i])
6665 return -EINVAL;
6666 }
6667
Jens Axboe6c271ce2019-01-10 11:22:30 -07006668 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006669 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006670 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006671 return -EINVAL;
6672
6673 ret = io_uring_create(entries, &p);
6674 if (ret < 0)
6675 return ret;
6676
6677 if (copy_to_user(params, &p, sizeof(p)))
6678 return -EFAULT;
6679
6680 return ret;
6681}
6682
6683SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6684 struct io_uring_params __user *, params)
6685{
6686 return io_uring_setup(entries, params);
6687}
6688
Jens Axboe66f4af92020-01-16 15:36:52 -07006689static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6690{
6691 struct io_uring_probe *p;
6692 size_t size;
6693 int i, ret;
6694
6695 size = struct_size(p, ops, nr_args);
6696 if (size == SIZE_MAX)
6697 return -EOVERFLOW;
6698 p = kzalloc(size, GFP_KERNEL);
6699 if (!p)
6700 return -ENOMEM;
6701
6702 ret = -EFAULT;
6703 if (copy_from_user(p, arg, size))
6704 goto out;
6705 ret = -EINVAL;
6706 if (memchr_inv(p, 0, size))
6707 goto out;
6708
6709 p->last_op = IORING_OP_LAST - 1;
6710 if (nr_args > IORING_OP_LAST)
6711 nr_args = IORING_OP_LAST;
6712
6713 for (i = 0; i < nr_args; i++) {
6714 p->ops[i].op = i;
6715 if (!io_op_defs[i].not_supported)
6716 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6717 }
6718 p->ops_len = i;
6719
6720 ret = 0;
6721 if (copy_to_user(arg, p, size))
6722 ret = -EFAULT;
6723out:
6724 kfree(p);
6725 return ret;
6726}
6727
Jens Axboe071698e2020-01-28 10:04:42 -07006728static int io_register_personality(struct io_ring_ctx *ctx)
6729{
6730 const struct cred *creds = get_current_cred();
6731 int id;
6732
6733 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
6734 USHRT_MAX, GFP_KERNEL);
6735 if (id < 0)
6736 put_cred(creds);
6737 return id;
6738}
6739
6740static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
6741{
6742 const struct cred *old_creds;
6743
6744 old_creds = idr_remove(&ctx->personality_idr, id);
6745 if (old_creds) {
6746 put_cred(old_creds);
6747 return 0;
6748 }
6749
6750 return -EINVAL;
6751}
6752
6753static bool io_register_op_must_quiesce(int op)
6754{
6755 switch (op) {
6756 case IORING_UNREGISTER_FILES:
6757 case IORING_REGISTER_FILES_UPDATE:
6758 case IORING_REGISTER_PROBE:
6759 case IORING_REGISTER_PERSONALITY:
6760 case IORING_UNREGISTER_PERSONALITY:
6761 return false;
6762 default:
6763 return true;
6764 }
6765}
6766
Jens Axboeedafcce2019-01-09 09:16:05 -07006767static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6768 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006769 __releases(ctx->uring_lock)
6770 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006771{
6772 int ret;
6773
Jens Axboe35fa71a2019-04-22 10:23:23 -06006774 /*
6775 * We're inside the ring mutex, if the ref is already dying, then
6776 * someone else killed the ctx or is already going through
6777 * io_uring_register().
6778 */
6779 if (percpu_ref_is_dying(&ctx->refs))
6780 return -ENXIO;
6781
Jens Axboe071698e2020-01-28 10:04:42 -07006782 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006783 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006784
Jens Axboe05f3fb32019-12-09 11:22:50 -07006785 /*
6786 * Drop uring mutex before waiting for references to exit. If
6787 * another thread is currently inside io_uring_enter() it might
6788 * need to grab the uring_lock to make progress. If we hold it
6789 * here across the drain wait, then we can deadlock. It's safe
6790 * to drop the mutex here, since no new references will come in
6791 * after we've killed the percpu ref.
6792 */
6793 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006794 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006795 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006796 if (ret) {
6797 percpu_ref_resurrect(&ctx->refs);
6798 ret = -EINTR;
6799 goto out;
6800 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006801 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006802
6803 switch (opcode) {
6804 case IORING_REGISTER_BUFFERS:
6805 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6806 break;
6807 case IORING_UNREGISTER_BUFFERS:
6808 ret = -EINVAL;
6809 if (arg || nr_args)
6810 break;
6811 ret = io_sqe_buffer_unregister(ctx);
6812 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006813 case IORING_REGISTER_FILES:
6814 ret = io_sqe_files_register(ctx, arg, nr_args);
6815 break;
6816 case IORING_UNREGISTER_FILES:
6817 ret = -EINVAL;
6818 if (arg || nr_args)
6819 break;
6820 ret = io_sqe_files_unregister(ctx);
6821 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006822 case IORING_REGISTER_FILES_UPDATE:
6823 ret = io_sqe_files_update(ctx, arg, nr_args);
6824 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006825 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006826 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006827 ret = -EINVAL;
6828 if (nr_args != 1)
6829 break;
6830 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006831 if (ret)
6832 break;
6833 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6834 ctx->eventfd_async = 1;
6835 else
6836 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006837 break;
6838 case IORING_UNREGISTER_EVENTFD:
6839 ret = -EINVAL;
6840 if (arg || nr_args)
6841 break;
6842 ret = io_eventfd_unregister(ctx);
6843 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07006844 case IORING_REGISTER_PROBE:
6845 ret = -EINVAL;
6846 if (!arg || nr_args > 256)
6847 break;
6848 ret = io_probe(ctx, arg, nr_args);
6849 break;
Jens Axboe071698e2020-01-28 10:04:42 -07006850 case IORING_REGISTER_PERSONALITY:
6851 ret = -EINVAL;
6852 if (arg || nr_args)
6853 break;
6854 ret = io_register_personality(ctx);
6855 break;
6856 case IORING_UNREGISTER_PERSONALITY:
6857 ret = -EINVAL;
6858 if (arg)
6859 break;
6860 ret = io_unregister_personality(ctx, nr_args);
6861 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006862 default:
6863 ret = -EINVAL;
6864 break;
6865 }
6866
Jens Axboe071698e2020-01-28 10:04:42 -07006867 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006868 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006869 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07006870out:
6871 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006872 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006873 return ret;
6874}
6875
6876SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6877 void __user *, arg, unsigned int, nr_args)
6878{
6879 struct io_ring_ctx *ctx;
6880 long ret = -EBADF;
6881 struct fd f;
6882
6883 f = fdget(fd);
6884 if (!f.file)
6885 return -EBADF;
6886
6887 ret = -EOPNOTSUPP;
6888 if (f.file->f_op != &io_uring_fops)
6889 goto out_fput;
6890
6891 ctx = f.file->private_data;
6892
6893 mutex_lock(&ctx->uring_lock);
6894 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6895 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006896 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6897 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006898out_fput:
6899 fdput(f);
6900 return ret;
6901}
6902
Jens Axboe2b188cc2019-01-07 10:46:33 -07006903static int __init io_uring_init(void)
6904{
Jens Axboed3656342019-12-18 09:50:26 -07006905 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006906 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6907 return 0;
6908};
6909__initcall(io_uring_init);