blob: d74567fc9628607e81f783520ba954e2be6e8326 [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 Axboed3656342019-12-18 09:50:26 -0700606};
607
608static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300609 [IORING_OP_NOP] = {},
610 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700611 .async_ctx = 1,
612 .needs_mm = 1,
613 .needs_file = 1,
614 .unbound_nonreg_file = 1,
615 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300616 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700617 .async_ctx = 1,
618 .needs_mm = 1,
619 .needs_file = 1,
620 .hash_reg_file = 1,
621 .unbound_nonreg_file = 1,
622 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300623 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700624 .needs_file = 1,
625 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300626 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700627 .needs_file = 1,
628 .unbound_nonreg_file = 1,
629 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300630 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700631 .needs_file = 1,
632 .hash_reg_file = 1,
633 .unbound_nonreg_file = 1,
634 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300635 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700636 .needs_file = 1,
637 .unbound_nonreg_file = 1,
638 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300639 [IORING_OP_POLL_REMOVE] = {},
640 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700641 .needs_file = 1,
642 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300643 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700644 .async_ctx = 1,
645 .needs_mm = 1,
646 .needs_file = 1,
647 .unbound_nonreg_file = 1,
648 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300649 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700650 .async_ctx = 1,
651 .needs_mm = 1,
652 .needs_file = 1,
653 .unbound_nonreg_file = 1,
654 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300655 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700656 .async_ctx = 1,
657 .needs_mm = 1,
658 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300659 [IORING_OP_TIMEOUT_REMOVE] = {},
660 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700661 .needs_mm = 1,
662 .needs_file = 1,
663 .unbound_nonreg_file = 1,
664 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300665 [IORING_OP_ASYNC_CANCEL] = {},
666 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700667 .async_ctx = 1,
668 .needs_mm = 1,
669 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300670 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700671 .async_ctx = 1,
672 .needs_mm = 1,
673 .needs_file = 1,
674 .unbound_nonreg_file = 1,
675 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300676 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700677 .needs_file = 1,
678 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300679 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700680 .needs_file = 1,
681 .fd_non_neg = 1,
682 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300683 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700684 .needs_file = 1,
685 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300686 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700687 .needs_mm = 1,
688 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300689 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700690 .needs_mm = 1,
691 .needs_file = 1,
692 .fd_non_neg = 1,
693 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300694 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700695 .needs_mm = 1,
696 .needs_file = 1,
697 .unbound_nonreg_file = 1,
698 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300699 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700700 .needs_mm = 1,
701 .needs_file = 1,
702 .unbound_nonreg_file = 1,
703 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300704 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700705 .needs_file = 1,
706 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300707 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700708 .needs_mm = 1,
709 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300710 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700711 .needs_mm = 1,
712 .needs_file = 1,
713 .unbound_nonreg_file = 1,
714 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300715 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700716 .needs_mm = 1,
717 .needs_file = 1,
718 .unbound_nonreg_file = 1,
719 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300720 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700721 .needs_file = 1,
722 .fd_non_neg = 1,
723 },
Jens Axboed3656342019-12-18 09:50:26 -0700724};
725
Jens Axboe561fb042019-10-24 07:25:42 -0600726static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700727static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800728static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700729static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700730static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
731static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700732static int __io_sqe_files_update(struct io_ring_ctx *ctx,
733 struct io_uring_files_update *ip,
734 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600735
Jens Axboe2b188cc2019-01-07 10:46:33 -0700736static struct kmem_cache *req_cachep;
737
738static const struct file_operations io_uring_fops;
739
740struct sock *io_uring_get_socket(struct file *file)
741{
742#if defined(CONFIG_UNIX)
743 if (file->f_op == &io_uring_fops) {
744 struct io_ring_ctx *ctx = file->private_data;
745
746 return ctx->ring_sock->sk;
747 }
748#endif
749 return NULL;
750}
751EXPORT_SYMBOL(io_uring_get_socket);
752
753static void io_ring_ctx_ref_free(struct percpu_ref *ref)
754{
755 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
756
Jens Axboe206aefd2019-11-07 18:27:42 -0700757 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700758}
759
760static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
761{
762 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700763 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700764
765 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
766 if (!ctx)
767 return NULL;
768
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700769 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
770 if (!ctx->fallback_req)
771 goto err;
772
Jens Axboe206aefd2019-11-07 18:27:42 -0700773 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
774 if (!ctx->completions)
775 goto err;
776
Jens Axboe78076bb2019-12-04 19:56:40 -0700777 /*
778 * Use 5 bits less than the max cq entries, that should give us around
779 * 32 entries per hash list if totally full and uniformly spread.
780 */
781 hash_bits = ilog2(p->cq_entries);
782 hash_bits -= 5;
783 if (hash_bits <= 0)
784 hash_bits = 1;
785 ctx->cancel_hash_bits = hash_bits;
786 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
787 GFP_KERNEL);
788 if (!ctx->cancel_hash)
789 goto err;
790 __hash_init(ctx->cancel_hash, 1U << hash_bits);
791
Roman Gushchin21482892019-05-07 10:01:48 -0700792 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700793 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
794 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700795
796 ctx->flags = p->flags;
797 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700798 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700799 init_completion(&ctx->completions[0]);
800 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700801 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700802 mutex_init(&ctx->uring_lock);
803 init_waitqueue_head(&ctx->wait);
804 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700805 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700806 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600807 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600808 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600809 init_waitqueue_head(&ctx->inflight_wait);
810 spin_lock_init(&ctx->inflight_lock);
811 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700812 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700813err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700814 if (ctx->fallback_req)
815 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700816 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700817 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700818 kfree(ctx);
819 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700820}
821
Bob Liu9d858b22019-11-13 18:06:25 +0800822static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600823{
Jackie Liua197f662019-11-08 08:09:12 -0700824 struct io_ring_ctx *ctx = req->ctx;
825
Jens Axboe498ccd92019-10-25 10:04:25 -0600826 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
827 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600828}
829
Bob Liu9d858b22019-11-13 18:06:25 +0800830static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600831{
Pavel Begunkov87987892020-01-18 01:22:30 +0300832 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800833 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600834
Bob Liu9d858b22019-11-13 18:06:25 +0800835 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600836}
837
838static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600839{
840 struct io_kiocb *req;
841
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600842 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800843 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600844 list_del_init(&req->list);
845 return req;
846 }
847
848 return NULL;
849}
850
Jens Axboe5262f562019-09-17 12:26:57 -0600851static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
852{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600853 struct io_kiocb *req;
854
855 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700856 if (req) {
857 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
858 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800859 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700860 list_del_init(&req->list);
861 return req;
862 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600863 }
864
865 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600866}
867
Jens Axboede0617e2019-04-06 21:51:27 -0600868static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700869{
Hristo Venev75b28af2019-08-26 17:23:46 +0000870 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700871
Pavel Begunkov07910152020-01-17 03:52:46 +0300872 /* order cqe stores with ring update */
873 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700874
Pavel Begunkov07910152020-01-17 03:52:46 +0300875 if (wq_has_sleeper(&ctx->cq_wait)) {
876 wake_up_interruptible(&ctx->cq_wait);
877 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700878 }
879}
880
Jens Axboecccf0ee2020-01-27 16:34:48 -0700881static inline void io_req_work_grab_env(struct io_kiocb *req,
882 const struct io_op_def *def)
883{
884 if (!req->work.mm && def->needs_mm) {
885 mmgrab(current->mm);
886 req->work.mm = current->mm;
887 }
888 if (!req->work.creds)
889 req->work.creds = get_current_cred();
890}
891
892static inline void io_req_work_drop_env(struct io_kiocb *req)
893{
894 if (req->work.mm) {
895 mmdrop(req->work.mm);
896 req->work.mm = NULL;
897 }
898 if (req->work.creds) {
899 put_cred(req->work.creds);
900 req->work.creds = NULL;
901 }
902}
903
Jens Axboe94ae5e72019-11-14 19:39:52 -0700904static inline bool io_prep_async_work(struct io_kiocb *req,
905 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600906{
Jens Axboed3656342019-12-18 09:50:26 -0700907 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600908 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600909
Jens Axboed3656342019-12-18 09:50:26 -0700910 if (req->flags & REQ_F_ISREG) {
911 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700912 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700913 } else {
914 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700915 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600916 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700917
918 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600919
Jens Axboe94ae5e72019-11-14 19:39:52 -0700920 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600921 return do_hashed;
922}
923
Jackie Liua197f662019-11-08 08:09:12 -0700924static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600925{
Jackie Liua197f662019-11-08 08:09:12 -0700926 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700927 struct io_kiocb *link;
928 bool do_hashed;
929
930 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600931
932 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
933 req->flags);
934 if (!do_hashed) {
935 io_wq_enqueue(ctx->io_wq, &req->work);
936 } else {
937 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
938 file_inode(req->file));
939 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700940
941 if (link)
942 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600943}
944
Jens Axboe5262f562019-09-17 12:26:57 -0600945static void io_kill_timeout(struct io_kiocb *req)
946{
947 int ret;
948
Jens Axboe2d283902019-12-04 11:08:05 -0700949 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600950 if (ret != -1) {
951 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600952 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700953 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800954 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600955 }
956}
957
958static void io_kill_timeouts(struct io_ring_ctx *ctx)
959{
960 struct io_kiocb *req, *tmp;
961
962 spin_lock_irq(&ctx->completion_lock);
963 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
964 io_kill_timeout(req);
965 spin_unlock_irq(&ctx->completion_lock);
966}
967
Jens Axboede0617e2019-04-06 21:51:27 -0600968static void io_commit_cqring(struct io_ring_ctx *ctx)
969{
970 struct io_kiocb *req;
971
Jens Axboe5262f562019-09-17 12:26:57 -0600972 while ((req = io_get_timeout_req(ctx)) != NULL)
973 io_kill_timeout(req);
974
Jens Axboede0617e2019-04-06 21:51:27 -0600975 __io_commit_cqring(ctx);
976
Pavel Begunkov87987892020-01-18 01:22:30 +0300977 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -0700978 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600979}
980
Jens Axboe2b188cc2019-01-07 10:46:33 -0700981static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
982{
Hristo Venev75b28af2019-08-26 17:23:46 +0000983 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700984 unsigned tail;
985
986 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200987 /*
988 * writes to the cq entry need to come after reading head; the
989 * control dependency is enough as we're using WRITE_ONCE to
990 * fill the cq entry
991 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000992 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700993 return NULL;
994
995 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000996 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700997}
998
Jens Axboef2842ab2020-01-08 11:04:00 -0700999static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1000{
1001 if (!ctx->eventfd_async)
1002 return true;
1003 return io_wq_current_is_worker() || in_interrupt();
1004}
1005
Jens Axboe8c838782019-03-12 15:48:16 -06001006static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1007{
1008 if (waitqueue_active(&ctx->wait))
1009 wake_up(&ctx->wait);
1010 if (waitqueue_active(&ctx->sqo_wait))
1011 wake_up(&ctx->sqo_wait);
Jens Axboef2842ab2020-01-08 11:04:00 -07001012 if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001013 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001014}
1015
Jens Axboec4a2ed72019-11-21 21:01:26 -07001016/* Returns true if there are no backlogged entries after the flush */
1017static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001018{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001019 struct io_rings *rings = ctx->rings;
1020 struct io_uring_cqe *cqe;
1021 struct io_kiocb *req;
1022 unsigned long flags;
1023 LIST_HEAD(list);
1024
1025 if (!force) {
1026 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001027 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001028 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1029 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001030 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001031 }
1032
1033 spin_lock_irqsave(&ctx->completion_lock, flags);
1034
1035 /* if force is set, the ring is going away. always drop after that */
1036 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001037 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001038
Jens Axboec4a2ed72019-11-21 21:01:26 -07001039 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001040 while (!list_empty(&ctx->cq_overflow_list)) {
1041 cqe = io_get_cqring(ctx);
1042 if (!cqe && !force)
1043 break;
1044
1045 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1046 list);
1047 list_move(&req->list, &list);
1048 if (cqe) {
1049 WRITE_ONCE(cqe->user_data, req->user_data);
1050 WRITE_ONCE(cqe->res, req->result);
1051 WRITE_ONCE(cqe->flags, 0);
1052 } else {
1053 WRITE_ONCE(ctx->rings->cq_overflow,
1054 atomic_inc_return(&ctx->cached_cq_overflow));
1055 }
1056 }
1057
1058 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001059 if (cqe) {
1060 clear_bit(0, &ctx->sq_check_overflow);
1061 clear_bit(0, &ctx->cq_check_overflow);
1062 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001063 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1064 io_cqring_ev_posted(ctx);
1065
1066 while (!list_empty(&list)) {
1067 req = list_first_entry(&list, struct io_kiocb, list);
1068 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001069 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001070 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001071
1072 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001073}
1074
Jens Axboe78e19bb2019-11-06 15:21:34 -07001075static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001076{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001077 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001078 struct io_uring_cqe *cqe;
1079
Jens Axboe78e19bb2019-11-06 15:21:34 -07001080 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001081
Jens Axboe2b188cc2019-01-07 10:46:33 -07001082 /*
1083 * If we can't get a cq entry, userspace overflowed the
1084 * submission (by quite a lot). Increment the overflow count in
1085 * the ring.
1086 */
1087 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001088 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001089 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001090 WRITE_ONCE(cqe->res, res);
1091 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001092 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001093 WRITE_ONCE(ctx->rings->cq_overflow,
1094 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001095 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001096 if (list_empty(&ctx->cq_overflow_list)) {
1097 set_bit(0, &ctx->sq_check_overflow);
1098 set_bit(0, &ctx->cq_check_overflow);
1099 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001100 refcount_inc(&req->refs);
1101 req->result = res;
1102 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001103 }
1104}
1105
Jens Axboe78e19bb2019-11-06 15:21:34 -07001106static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001107{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001108 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001109 unsigned long flags;
1110
1111 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001112 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001113 io_commit_cqring(ctx);
1114 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1115
Jens Axboe8c838782019-03-12 15:48:16 -06001116 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001117}
1118
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001119static inline bool io_is_fallback_req(struct io_kiocb *req)
1120{
1121 return req == (struct io_kiocb *)
1122 ((unsigned long) req->ctx->fallback_req & ~1UL);
1123}
1124
1125static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1126{
1127 struct io_kiocb *req;
1128
1129 req = ctx->fallback_req;
1130 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1131 return req;
1132
1133 return NULL;
1134}
1135
Jens Axboe2579f912019-01-09 09:10:43 -07001136static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1137 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001138{
Jens Axboefd6fab22019-03-14 16:30:06 -06001139 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001140 struct io_kiocb *req;
1141
Jens Axboe2579f912019-01-09 09:10:43 -07001142 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001143 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001144 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001145 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001146 } else if (!state->free_reqs) {
1147 size_t sz;
1148 int ret;
1149
1150 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001151 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1152
1153 /*
1154 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1155 * retry single alloc to be on the safe side.
1156 */
1157 if (unlikely(ret <= 0)) {
1158 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1159 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001160 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001161 ret = 1;
1162 }
Jens Axboe2579f912019-01-09 09:10:43 -07001163 state->free_reqs = ret - 1;
1164 state->cur_req = 1;
1165 req = state->reqs[0];
1166 } else {
1167 req = state->reqs[state->cur_req];
1168 state->free_reqs--;
1169 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001170 }
1171
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001172got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001173 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001174 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001175 req->ctx = ctx;
1176 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001177 /* one is dropped after submission, the other at completion */
1178 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001179 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001180 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001181 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001182fallback:
1183 req = io_get_fallback_req(ctx);
1184 if (req)
1185 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001186 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001187 return NULL;
1188}
1189
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001190static void __io_req_do_free(struct io_kiocb *req)
1191{
1192 if (likely(!io_is_fallback_req(req)))
1193 kmem_cache_free(req_cachep, req);
1194 else
1195 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1196}
1197
Jens Axboec6ca97b302019-12-28 12:11:08 -07001198static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001199{
Jens Axboefcb323c2019-10-24 12:39:47 -06001200 struct io_ring_ctx *ctx = req->ctx;
1201
YueHaibing96fd84d2020-01-07 22:22:44 +08001202 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001203 if (req->file) {
1204 if (req->flags & REQ_F_FIXED_FILE)
1205 percpu_ref_put(&ctx->file_data->refs);
1206 else
1207 fput(req->file);
1208 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001209
1210 io_req_work_drop_env(req);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001211}
1212
1213static void __io_free_req(struct io_kiocb *req)
1214{
1215 __io_req_aux_free(req);
1216
Jens Axboefcb323c2019-10-24 12:39:47 -06001217 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001218 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001219 unsigned long flags;
1220
1221 spin_lock_irqsave(&ctx->inflight_lock, flags);
1222 list_del(&req->inflight_entry);
1223 if (waitqueue_active(&ctx->inflight_wait))
1224 wake_up(&ctx->inflight_wait);
1225 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1226 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001227
1228 percpu_ref_put(&req->ctx->refs);
1229 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001230}
1231
Jens Axboec6ca97b302019-12-28 12:11:08 -07001232struct req_batch {
1233 void *reqs[IO_IOPOLL_BATCH];
1234 int to_free;
1235 int need_iter;
1236};
1237
1238static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1239{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001240 int fixed_refs = rb->to_free;
1241
Jens Axboec6ca97b302019-12-28 12:11:08 -07001242 if (!rb->to_free)
1243 return;
1244 if (rb->need_iter) {
1245 int i, inflight = 0;
1246 unsigned long flags;
1247
Jens Axboe10fef4b2020-01-09 07:52:28 -07001248 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001249 for (i = 0; i < rb->to_free; i++) {
1250 struct io_kiocb *req = rb->reqs[i];
1251
Jens Axboe10fef4b2020-01-09 07:52:28 -07001252 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001253 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001254 fixed_refs++;
1255 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001256 if (req->flags & REQ_F_INFLIGHT)
1257 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001258 __io_req_aux_free(req);
1259 }
1260 if (!inflight)
1261 goto do_free;
1262
1263 spin_lock_irqsave(&ctx->inflight_lock, flags);
1264 for (i = 0; i < rb->to_free; i++) {
1265 struct io_kiocb *req = rb->reqs[i];
1266
Jens Axboe10fef4b2020-01-09 07:52:28 -07001267 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001268 list_del(&req->inflight_entry);
1269 if (!--inflight)
1270 break;
1271 }
1272 }
1273 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1274
1275 if (waitqueue_active(&ctx->inflight_wait))
1276 wake_up(&ctx->inflight_wait);
1277 }
1278do_free:
1279 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001280 if (fixed_refs)
1281 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001282 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001283 rb->to_free = rb->need_iter = 0;
1284}
1285
Jackie Liua197f662019-11-08 08:09:12 -07001286static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001287{
Jackie Liua197f662019-11-08 08:09:12 -07001288 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001289 int ret;
1290
Jens Axboe2d283902019-12-04 11:08:05 -07001291 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001292 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001293 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001294 io_commit_cqring(ctx);
1295 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001296 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001297 return true;
1298 }
1299
1300 return false;
1301}
1302
Jens Axboeba816ad2019-09-28 11:36:45 -06001303static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001304{
Jens Axboe2665abf2019-11-05 12:40:47 -07001305 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001306 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001307
Jens Axboe4d7dd462019-11-20 13:03:52 -07001308 /* Already got next link */
1309 if (req->flags & REQ_F_LINK_NEXT)
1310 return;
1311
Jens Axboe9e645e112019-05-10 16:07:28 -06001312 /*
1313 * The list should never be empty when we are called here. But could
1314 * potentially happen if the chain is messed up, check to be on the
1315 * safe side.
1316 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001317 while (!list_empty(&req->link_list)) {
1318 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1319 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001320
Pavel Begunkov44932332019-12-05 16:16:35 +03001321 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1322 (nxt->flags & REQ_F_TIMEOUT))) {
1323 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001324 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001325 req->flags &= ~REQ_F_LINK_TIMEOUT;
1326 continue;
1327 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001328
Pavel Begunkov44932332019-12-05 16:16:35 +03001329 list_del_init(&req->link_list);
1330 if (!list_empty(&nxt->link_list))
1331 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001332 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001333 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001334 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001335
Jens Axboe4d7dd462019-11-20 13:03:52 -07001336 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001337 if (wake_ev)
1338 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001339}
1340
1341/*
1342 * Called if REQ_F_LINK is set, and we fail the head request
1343 */
1344static void io_fail_links(struct io_kiocb *req)
1345{
Jens Axboe2665abf2019-11-05 12:40:47 -07001346 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001347 unsigned long flags;
1348
1349 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001350
1351 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001352 struct io_kiocb *link = list_first_entry(&req->link_list,
1353 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001354
Pavel Begunkov44932332019-12-05 16:16:35 +03001355 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001356 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001357
1358 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001359 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001360 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001361 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001362 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001363 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001364 }
Jens Axboe5d960722019-11-19 15:31:28 -07001365 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001366 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001367
1368 io_commit_cqring(ctx);
1369 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1370 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001371}
1372
Jens Axboe4d7dd462019-11-20 13:03:52 -07001373static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001374{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001375 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001376 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001377
Jens Axboe9e645e112019-05-10 16:07:28 -06001378 /*
1379 * If LINK is set, we have dependent requests in this chain. If we
1380 * didn't fail this request, queue the first one up, moving any other
1381 * dependencies to the next request. In case of failure, fail the rest
1382 * of the chain.
1383 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001384 if (req->flags & REQ_F_FAIL_LINK) {
1385 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001386 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1387 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001388 struct io_ring_ctx *ctx = req->ctx;
1389 unsigned long flags;
1390
1391 /*
1392 * If this is a timeout link, we could be racing with the
1393 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001394 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001395 */
1396 spin_lock_irqsave(&ctx->completion_lock, flags);
1397 io_req_link_next(req, nxt);
1398 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1399 } else {
1400 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001401 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001402}
Jens Axboe9e645e112019-05-10 16:07:28 -06001403
Jackie Liuc69f8db2019-11-09 11:00:08 +08001404static void io_free_req(struct io_kiocb *req)
1405{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001406 struct io_kiocb *nxt = NULL;
1407
1408 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001409 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001410
1411 if (nxt)
1412 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001413}
1414
Jens Axboeba816ad2019-09-28 11:36:45 -06001415/*
1416 * Drop reference to request, return next in chain (if there is one) if this
1417 * was the last reference to this request.
1418 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001419__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001420static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001421{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001422 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001423
Jens Axboee65ef562019-03-12 10:16:44 -06001424 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001425 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001426}
1427
Jens Axboe2b188cc2019-01-07 10:46:33 -07001428static void io_put_req(struct io_kiocb *req)
1429{
Jens Axboedef596e2019-01-09 08:59:42 -07001430 if (refcount_dec_and_test(&req->refs))
1431 io_free_req(req);
1432}
1433
Jens Axboe978db572019-11-14 22:39:04 -07001434/*
1435 * Must only be used if we don't need to care about links, usually from
1436 * within the completion handling itself.
1437 */
1438static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001439{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001440 /* drop both submit and complete references */
1441 if (refcount_sub_and_test(2, &req->refs))
1442 __io_free_req(req);
1443}
1444
Jens Axboe978db572019-11-14 22:39:04 -07001445static void io_double_put_req(struct io_kiocb *req)
1446{
1447 /* drop both submit and complete references */
1448 if (refcount_sub_and_test(2, &req->refs))
1449 io_free_req(req);
1450}
1451
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001452static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001453{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001454 struct io_rings *rings = ctx->rings;
1455
Jens Axboead3eb2c2019-12-18 17:12:20 -07001456 if (test_bit(0, &ctx->cq_check_overflow)) {
1457 /*
1458 * noflush == true is from the waitqueue handler, just ensure
1459 * we wake up the task, and the next invocation will flush the
1460 * entries. We cannot safely to it from here.
1461 */
1462 if (noflush && !list_empty(&ctx->cq_overflow_list))
1463 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001464
Jens Axboead3eb2c2019-12-18 17:12:20 -07001465 io_cqring_overflow_flush(ctx, false);
1466 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001467
Jens Axboea3a0e432019-08-20 11:03:11 -06001468 /* See comment at the top of this file */
1469 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001470 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001471}
1472
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001473static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1474{
1475 struct io_rings *rings = ctx->rings;
1476
1477 /* make sure SQ entry isn't read before tail */
1478 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1479}
1480
Jens Axboe8237e042019-12-28 10:48:22 -07001481static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001482{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001483 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1484 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001485
Jens Axboec6ca97b302019-12-28 12:11:08 -07001486 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1487 rb->need_iter++;
1488
1489 rb->reqs[rb->to_free++] = req;
1490 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1491 io_free_req_many(req->ctx, rb);
1492 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001493}
1494
Jens Axboedef596e2019-01-09 08:59:42 -07001495/*
1496 * Find and free completed poll iocbs
1497 */
1498static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1499 struct list_head *done)
1500{
Jens Axboe8237e042019-12-28 10:48:22 -07001501 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001502 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001503
Jens Axboec6ca97b302019-12-28 12:11:08 -07001504 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001505 while (!list_empty(done)) {
1506 req = list_first_entry(done, struct io_kiocb, list);
1507 list_del(&req->list);
1508
Jens Axboe78e19bb2019-11-06 15:21:34 -07001509 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001510 (*nr_events)++;
1511
Jens Axboe8237e042019-12-28 10:48:22 -07001512 if (refcount_dec_and_test(&req->refs) &&
1513 !io_req_multi_free(&rb, req))
1514 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001515 }
Jens Axboedef596e2019-01-09 08:59:42 -07001516
Jens Axboe09bb8392019-03-13 12:39:28 -06001517 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001518 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001519}
1520
1521static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1522 long min)
1523{
1524 struct io_kiocb *req, *tmp;
1525 LIST_HEAD(done);
1526 bool spin;
1527 int ret;
1528
1529 /*
1530 * Only spin for completions if we don't have multiple devices hanging
1531 * off our complete list, and we're under the requested amount.
1532 */
1533 spin = !ctx->poll_multi_file && *nr_events < min;
1534
1535 ret = 0;
1536 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001537 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001538
1539 /*
1540 * Move completed entries to our local list. If we find a
1541 * request that requires polling, break out and complete
1542 * the done list first, if we have entries there.
1543 */
1544 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1545 list_move_tail(&req->list, &done);
1546 continue;
1547 }
1548 if (!list_empty(&done))
1549 break;
1550
1551 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1552 if (ret < 0)
1553 break;
1554
1555 if (ret && spin)
1556 spin = false;
1557 ret = 0;
1558 }
1559
1560 if (!list_empty(&done))
1561 io_iopoll_complete(ctx, nr_events, &done);
1562
1563 return ret;
1564}
1565
1566/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001567 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001568 * non-spinning poll check - we'll still enter the driver poll loop, but only
1569 * as a non-spinning completion check.
1570 */
1571static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1572 long min)
1573{
Jens Axboe08f54392019-08-21 22:19:11 -06001574 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001575 int ret;
1576
1577 ret = io_do_iopoll(ctx, nr_events, min);
1578 if (ret < 0)
1579 return ret;
1580 if (!min || *nr_events >= min)
1581 return 0;
1582 }
1583
1584 return 1;
1585}
1586
1587/*
1588 * We can't just wait for polled events to come to us, we have to actively
1589 * find and complete them.
1590 */
1591static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1592{
1593 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1594 return;
1595
1596 mutex_lock(&ctx->uring_lock);
1597 while (!list_empty(&ctx->poll_list)) {
1598 unsigned int nr_events = 0;
1599
1600 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001601
1602 /*
1603 * Ensure we allow local-to-the-cpu processing to take place,
1604 * in this case we need to ensure that we reap all events.
1605 */
1606 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001607 }
1608 mutex_unlock(&ctx->uring_lock);
1609}
1610
Jens Axboe2b2ed972019-10-25 10:06:15 -06001611static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1612 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001613{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001614 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001615
1616 do {
1617 int tmin = 0;
1618
Jens Axboe500f9fb2019-08-19 12:15:59 -06001619 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001620 * Don't enter poll loop if we already have events pending.
1621 * If we do, we can potentially be spinning for commands that
1622 * already triggered a CQE (eg in error).
1623 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001624 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001625 break;
1626
1627 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001628 * If a submit got punted to a workqueue, we can have the
1629 * application entering polling for a command before it gets
1630 * issued. That app will hold the uring_lock for the duration
1631 * of the poll right here, so we need to take a breather every
1632 * now and then to ensure that the issue has a chance to add
1633 * the poll to the issued list. Otherwise we can spin here
1634 * forever, while the workqueue is stuck trying to acquire the
1635 * very same mutex.
1636 */
1637 if (!(++iters & 7)) {
1638 mutex_unlock(&ctx->uring_lock);
1639 mutex_lock(&ctx->uring_lock);
1640 }
1641
Jens Axboedef596e2019-01-09 08:59:42 -07001642 if (*nr_events < min)
1643 tmin = min - *nr_events;
1644
1645 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1646 if (ret <= 0)
1647 break;
1648 ret = 0;
1649 } while (min && !*nr_events && !need_resched());
1650
Jens Axboe2b2ed972019-10-25 10:06:15 -06001651 return ret;
1652}
1653
1654static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1655 long min)
1656{
1657 int ret;
1658
1659 /*
1660 * We disallow the app entering submit/complete with polling, but we
1661 * still need to lock the ring to prevent racing with polled issue
1662 * that got punted to a workqueue.
1663 */
1664 mutex_lock(&ctx->uring_lock);
1665 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001666 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001667 return ret;
1668}
1669
Jens Axboe491381ce2019-10-17 09:20:46 -06001670static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001671{
Jens Axboe491381ce2019-10-17 09:20:46 -06001672 /*
1673 * Tell lockdep we inherited freeze protection from submission
1674 * thread.
1675 */
1676 if (req->flags & REQ_F_ISREG) {
1677 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001678
Jens Axboe491381ce2019-10-17 09:20:46 -06001679 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001680 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001681 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001682}
1683
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001684static inline void req_set_fail_links(struct io_kiocb *req)
1685{
1686 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1687 req->flags |= REQ_F_FAIL_LINK;
1688}
1689
Jens Axboeba816ad2019-09-28 11:36:45 -06001690static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001691{
Jens Axboe9adbd452019-12-20 08:45:55 -07001692 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001693
Jens Axboe491381ce2019-10-17 09:20:46 -06001694 if (kiocb->ki_flags & IOCB_WRITE)
1695 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001696
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001697 if (res != req->result)
1698 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001699 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001700}
1701
1702static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1703{
Jens Axboe9adbd452019-12-20 08:45:55 -07001704 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001705
1706 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001707 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708}
1709
Jens Axboeba816ad2019-09-28 11:36:45 -06001710static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1711{
Jens Axboe9adbd452019-12-20 08:45:55 -07001712 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001713 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001714
1715 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001716 io_put_req_find_next(req, &nxt);
1717
1718 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001719}
1720
Jens Axboedef596e2019-01-09 08:59:42 -07001721static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1722{
Jens Axboe9adbd452019-12-20 08:45:55 -07001723 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001724
Jens Axboe491381ce2019-10-17 09:20:46 -06001725 if (kiocb->ki_flags & IOCB_WRITE)
1726 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001727
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001728 if (res != req->result)
1729 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001730 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001731 if (res != -EAGAIN)
1732 req->flags |= REQ_F_IOPOLL_COMPLETED;
1733}
1734
1735/*
1736 * After the iocb has been issued, it's safe to be found on the poll list.
1737 * Adding the kiocb to the list AFTER submission ensures that we don't
1738 * find it from a io_iopoll_getevents() thread before the issuer is done
1739 * accessing the kiocb cookie.
1740 */
1741static void io_iopoll_req_issued(struct io_kiocb *req)
1742{
1743 struct io_ring_ctx *ctx = req->ctx;
1744
1745 /*
1746 * Track whether we have multiple files in our lists. This will impact
1747 * how we do polling eventually, not spinning if we're on potentially
1748 * different devices.
1749 */
1750 if (list_empty(&ctx->poll_list)) {
1751 ctx->poll_multi_file = false;
1752 } else if (!ctx->poll_multi_file) {
1753 struct io_kiocb *list_req;
1754
1755 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1756 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001757 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001758 ctx->poll_multi_file = true;
1759 }
1760
1761 /*
1762 * For fast devices, IO may have already completed. If it has, add
1763 * it to the front so we find it first.
1764 */
1765 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1766 list_add(&req->list, &ctx->poll_list);
1767 else
1768 list_add_tail(&req->list, &ctx->poll_list);
1769}
1770
Jens Axboe3d6770f2019-04-13 11:50:54 -06001771static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001772{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001773 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001774 int diff = state->has_refs - state->used_refs;
1775
1776 if (diff)
1777 fput_many(state->file, diff);
1778 state->file = NULL;
1779 }
1780}
1781
1782/*
1783 * Get as many references to a file as we have IOs left in this submission,
1784 * assuming most submissions are for one file, or at least that each file
1785 * has more than one submission.
1786 */
1787static struct file *io_file_get(struct io_submit_state *state, int fd)
1788{
1789 if (!state)
1790 return fget(fd);
1791
1792 if (state->file) {
1793 if (state->fd == fd) {
1794 state->used_refs++;
1795 state->ios_left--;
1796 return state->file;
1797 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001798 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001799 }
1800 state->file = fget_many(fd, state->ios_left);
1801 if (!state->file)
1802 return NULL;
1803
1804 state->fd = fd;
1805 state->has_refs = state->ios_left;
1806 state->used_refs = 1;
1807 state->ios_left--;
1808 return state->file;
1809}
1810
Jens Axboe2b188cc2019-01-07 10:46:33 -07001811/*
1812 * If we tracked the file through the SCM inflight mechanism, we could support
1813 * any file. For now, just ensure that anything potentially problematic is done
1814 * inline.
1815 */
1816static bool io_file_supports_async(struct file *file)
1817{
1818 umode_t mode = file_inode(file)->i_mode;
1819
Jens Axboe10d59342019-12-09 20:16:22 -07001820 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001821 return true;
1822 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1823 return true;
1824
1825 return false;
1826}
1827
Jens Axboe3529d8c2019-12-19 18:24:38 -07001828static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1829 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001830{
Jens Axboedef596e2019-01-09 08:59:42 -07001831 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001832 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001833 unsigned ioprio;
1834 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001835
Jens Axboe09bb8392019-03-13 12:39:28 -06001836 if (!req->file)
1837 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001838
Jens Axboe491381ce2019-10-17 09:20:46 -06001839 if (S_ISREG(file_inode(req->file)->i_mode))
1840 req->flags |= REQ_F_ISREG;
1841
Jens Axboe2b188cc2019-01-07 10:46:33 -07001842 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001843 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1844 req->flags |= REQ_F_CUR_POS;
1845 kiocb->ki_pos = req->file->f_pos;
1846 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001847 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1848 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1849
1850 ioprio = READ_ONCE(sqe->ioprio);
1851 if (ioprio) {
1852 ret = ioprio_check_cap(ioprio);
1853 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001854 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001855
1856 kiocb->ki_ioprio = ioprio;
1857 } else
1858 kiocb->ki_ioprio = get_current_ioprio();
1859
1860 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1861 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001862 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001863
1864 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001865 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1866 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001867 req->flags |= REQ_F_NOWAIT;
1868
1869 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001870 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001871
Jens Axboedef596e2019-01-09 08:59:42 -07001872 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001873 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1874 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001875 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001876
Jens Axboedef596e2019-01-09 08:59:42 -07001877 kiocb->ki_flags |= IOCB_HIPRI;
1878 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001879 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001880 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001881 if (kiocb->ki_flags & IOCB_HIPRI)
1882 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001883 kiocb->ki_complete = io_complete_rw;
1884 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001885
Jens Axboe3529d8c2019-12-19 18:24:38 -07001886 req->rw.addr = READ_ONCE(sqe->addr);
1887 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001888 /* we own ->private, reuse it for the buffer index */
1889 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001890 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001891 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001892}
1893
1894static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1895{
1896 switch (ret) {
1897 case -EIOCBQUEUED:
1898 break;
1899 case -ERESTARTSYS:
1900 case -ERESTARTNOINTR:
1901 case -ERESTARTNOHAND:
1902 case -ERESTART_RESTARTBLOCK:
1903 /*
1904 * We can't just restart the syscall, since previously
1905 * submitted sqes may already be in progress. Just fail this
1906 * IO with EINTR.
1907 */
1908 ret = -EINTR;
1909 /* fall through */
1910 default:
1911 kiocb->ki_complete(kiocb, ret, 0);
1912 }
1913}
1914
Jens Axboeba816ad2019-09-28 11:36:45 -06001915static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1916 bool in_async)
1917{
Jens Axboeba042912019-12-25 16:33:42 -07001918 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1919
1920 if (req->flags & REQ_F_CUR_POS)
1921 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001922 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001923 *nxt = __io_complete_rw(kiocb, ret);
1924 else
1925 io_rw_done(kiocb, ret);
1926}
1927
Jens Axboe9adbd452019-12-20 08:45:55 -07001928static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001929 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001930{
Jens Axboe9adbd452019-12-20 08:45:55 -07001931 struct io_ring_ctx *ctx = req->ctx;
1932 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001933 struct io_mapped_ubuf *imu;
1934 unsigned index, buf_index;
1935 size_t offset;
1936 u64 buf_addr;
1937
1938 /* attempt to use fixed buffers without having provided iovecs */
1939 if (unlikely(!ctx->user_bufs))
1940 return -EFAULT;
1941
Jens Axboe9adbd452019-12-20 08:45:55 -07001942 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001943 if (unlikely(buf_index >= ctx->nr_user_bufs))
1944 return -EFAULT;
1945
1946 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1947 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001948 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001949
1950 /* overflow */
1951 if (buf_addr + len < buf_addr)
1952 return -EFAULT;
1953 /* not inside the mapped region */
1954 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1955 return -EFAULT;
1956
1957 /*
1958 * May not be a start of buffer, set size appropriately
1959 * and advance us to the beginning.
1960 */
1961 offset = buf_addr - imu->ubuf;
1962 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001963
1964 if (offset) {
1965 /*
1966 * Don't use iov_iter_advance() here, as it's really slow for
1967 * using the latter parts of a big fixed buffer - it iterates
1968 * over each segment manually. We can cheat a bit here, because
1969 * we know that:
1970 *
1971 * 1) it's a BVEC iter, we set it up
1972 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1973 * first and last bvec
1974 *
1975 * So just find our index, and adjust the iterator afterwards.
1976 * If the offset is within the first bvec (or the whole first
1977 * bvec, just use iov_iter_advance(). This makes it easier
1978 * since we can just skip the first segment, which may not
1979 * be PAGE_SIZE aligned.
1980 */
1981 const struct bio_vec *bvec = imu->bvec;
1982
1983 if (offset <= bvec->bv_len) {
1984 iov_iter_advance(iter, offset);
1985 } else {
1986 unsigned long seg_skip;
1987
1988 /* skip first vec */
1989 offset -= bvec->bv_len;
1990 seg_skip = 1 + (offset >> PAGE_SHIFT);
1991
1992 iter->bvec = bvec + seg_skip;
1993 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001994 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001995 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001996 }
1997 }
1998
Jens Axboe5e559562019-11-13 16:12:46 -07001999 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002000}
2001
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002002static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2003 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002004{
Jens Axboe9adbd452019-12-20 08:45:55 -07002005 void __user *buf = u64_to_user_ptr(req->rw.addr);
2006 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002007 u8 opcode;
2008
Jens Axboed625c6e2019-12-17 19:53:05 -07002009 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002010 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002011 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002012 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002013 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002014
Jens Axboe9adbd452019-12-20 08:45:55 -07002015 /* buffer index only valid with fixed read/write */
2016 if (req->rw.kiocb.private)
2017 return -EINVAL;
2018
Jens Axboe3a6820f2019-12-22 15:19:35 -07002019 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2020 ssize_t ret;
2021 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2022 *iovec = NULL;
2023 return ret;
2024 }
2025
Jens Axboef67676d2019-12-02 11:03:47 -07002026 if (req->io) {
2027 struct io_async_rw *iorw = &req->io->rw;
2028
2029 *iovec = iorw->iov;
2030 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2031 if (iorw->iov == iorw->fast_iov)
2032 *iovec = NULL;
2033 return iorw->size;
2034 }
2035
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002036 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002037 return -EFAULT;
2038
2039#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002040 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002041 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2042 iovec, iter);
2043#endif
2044
2045 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2046}
2047
Jens Axboe32960612019-09-23 11:05:34 -06002048/*
2049 * For files that don't have ->read_iter() and ->write_iter(), handle them
2050 * by looping over ->read() or ->write() manually.
2051 */
2052static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2053 struct iov_iter *iter)
2054{
2055 ssize_t ret = 0;
2056
2057 /*
2058 * Don't support polled IO through this interface, and we can't
2059 * support non-blocking either. For the latter, this just causes
2060 * the kiocb to be handled from an async context.
2061 */
2062 if (kiocb->ki_flags & IOCB_HIPRI)
2063 return -EOPNOTSUPP;
2064 if (kiocb->ki_flags & IOCB_NOWAIT)
2065 return -EAGAIN;
2066
2067 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002068 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002069 ssize_t nr;
2070
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002071 if (!iov_iter_is_bvec(iter)) {
2072 iovec = iov_iter_iovec(iter);
2073 } else {
2074 /* fixed buffers import bvec */
2075 iovec.iov_base = kmap(iter->bvec->bv_page)
2076 + iter->iov_offset;
2077 iovec.iov_len = min(iter->count,
2078 iter->bvec->bv_len - iter->iov_offset);
2079 }
2080
Jens Axboe32960612019-09-23 11:05:34 -06002081 if (rw == READ) {
2082 nr = file->f_op->read(file, iovec.iov_base,
2083 iovec.iov_len, &kiocb->ki_pos);
2084 } else {
2085 nr = file->f_op->write(file, iovec.iov_base,
2086 iovec.iov_len, &kiocb->ki_pos);
2087 }
2088
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002089 if (iov_iter_is_bvec(iter))
2090 kunmap(iter->bvec->bv_page);
2091
Jens Axboe32960612019-09-23 11:05:34 -06002092 if (nr < 0) {
2093 if (!ret)
2094 ret = nr;
2095 break;
2096 }
2097 ret += nr;
2098 if (nr != iovec.iov_len)
2099 break;
2100 iov_iter_advance(iter, nr);
2101 }
2102
2103 return ret;
2104}
2105
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002106static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002107 struct iovec *iovec, struct iovec *fast_iov,
2108 struct iov_iter *iter)
2109{
2110 req->io->rw.nr_segs = iter->nr_segs;
2111 req->io->rw.size = io_size;
2112 req->io->rw.iov = iovec;
2113 if (!req->io->rw.iov) {
2114 req->io->rw.iov = req->io->rw.fast_iov;
2115 memcpy(req->io->rw.iov, fast_iov,
2116 sizeof(struct iovec) * iter->nr_segs);
2117 }
2118}
2119
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002120static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002121{
Jens Axboed3656342019-12-18 09:50:26 -07002122 if (!io_op_defs[req->opcode].async_ctx)
2123 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002124 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002125 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002126}
2127
2128static void io_rw_async(struct io_wq_work **workptr)
2129{
2130 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2131 struct iovec *iov = NULL;
2132
2133 if (req->io->rw.iov != req->io->rw.fast_iov)
2134 iov = req->io->rw.iov;
2135 io_wq_submit_work(workptr);
2136 kfree(iov);
2137}
2138
2139static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2140 struct iovec *iovec, struct iovec *fast_iov,
2141 struct iov_iter *iter)
2142{
Jens Axboe980ad262020-01-24 23:08:54 -07002143 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002144 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002145 if (!req->io && io_alloc_async_ctx(req))
2146 return -ENOMEM;
2147
2148 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2149 req->work.func = io_rw_async;
2150 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002151}
2152
Jens Axboe3529d8c2019-12-19 18:24:38 -07002153static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2154 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002155{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002156 struct io_async_ctx *io;
2157 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002158 ssize_t ret;
2159
Jens Axboe3529d8c2019-12-19 18:24:38 -07002160 ret = io_prep_rw(req, sqe, force_nonblock);
2161 if (ret)
2162 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002163
Jens Axboe3529d8c2019-12-19 18:24:38 -07002164 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2165 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002166
Jens Axboe3529d8c2019-12-19 18:24:38 -07002167 if (!req->io)
2168 return 0;
2169
2170 io = req->io;
2171 io->rw.iov = io->rw.fast_iov;
2172 req->io = NULL;
2173 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2174 req->io = io;
2175 if (ret < 0)
2176 return ret;
2177
2178 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2179 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002180}
2181
Pavel Begunkov267bc902019-11-07 01:41:08 +03002182static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002183 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002184{
2185 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002186 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002187 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002188 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002189 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002190
Jens Axboe3529d8c2019-12-19 18:24:38 -07002191 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002192 if (ret < 0)
2193 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002194
Jens Axboefd6c2e42019-12-18 12:19:41 -07002195 /* Ensure we clear previously set non-block flag */
2196 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002197 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002198
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002199 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002200 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002201 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002202 req->result = io_size;
2203
2204 /*
2205 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2206 * we know to async punt it even if it was opened O_NONBLOCK
2207 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002208 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002209 req->flags |= REQ_F_MUST_PUNT;
2210 goto copy_iov;
2211 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002212
Jens Axboe31b51512019-01-18 22:56:34 -07002213 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002214 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002215 if (!ret) {
2216 ssize_t ret2;
2217
Jens Axboe9adbd452019-12-20 08:45:55 -07002218 if (req->file->f_op->read_iter)
2219 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002220 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002221 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002222
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002223 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002224 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002225 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002226 } else {
2227copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002228 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002229 inline_vecs, &iter);
2230 if (ret)
2231 goto out_free;
2232 return -EAGAIN;
2233 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002234 }
Jens Axboef67676d2019-12-02 11:03:47 -07002235out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002236 if (!io_wq_current_is_worker())
2237 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002238 return ret;
2239}
2240
Jens Axboe3529d8c2019-12-19 18:24:38 -07002241static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2242 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002243{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002244 struct io_async_ctx *io;
2245 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002246 ssize_t ret;
2247
Jens Axboe3529d8c2019-12-19 18:24:38 -07002248 ret = io_prep_rw(req, sqe, force_nonblock);
2249 if (ret)
2250 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002251
Jens Axboe3529d8c2019-12-19 18:24:38 -07002252 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2253 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002254
Jens Axboe3529d8c2019-12-19 18:24:38 -07002255 if (!req->io)
2256 return 0;
2257
2258 io = req->io;
2259 io->rw.iov = io->rw.fast_iov;
2260 req->io = NULL;
2261 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2262 req->io = io;
2263 if (ret < 0)
2264 return ret;
2265
2266 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2267 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002268}
2269
Pavel Begunkov267bc902019-11-07 01:41:08 +03002270static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002271 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002272{
2273 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002274 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002275 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002276 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002277 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002278
Jens Axboe3529d8c2019-12-19 18:24:38 -07002279 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002280 if (ret < 0)
2281 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002282
Jens Axboefd6c2e42019-12-18 12:19:41 -07002283 /* Ensure we clear previously set non-block flag */
2284 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002285 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002286
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002287 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002288 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002289 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002290 req->result = io_size;
2291
2292 /*
2293 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2294 * we know to async punt it even if it was opened O_NONBLOCK
2295 */
2296 if (force_nonblock && !io_file_supports_async(req->file)) {
2297 req->flags |= REQ_F_MUST_PUNT;
2298 goto copy_iov;
2299 }
2300
Jens Axboe10d59342019-12-09 20:16:22 -07002301 /* file path doesn't support NOWAIT for non-direct_IO */
2302 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2303 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002304 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002305
Jens Axboe31b51512019-01-18 22:56:34 -07002306 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002307 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002308 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002309 ssize_t ret2;
2310
Jens Axboe2b188cc2019-01-07 10:46:33 -07002311 /*
2312 * Open-code file_start_write here to grab freeze protection,
2313 * which will be released by another thread in
2314 * io_complete_rw(). Fool lockdep by telling it the lock got
2315 * released so that it doesn't complain about the held lock when
2316 * we return to userspace.
2317 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002318 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002319 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002320 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002321 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002322 SB_FREEZE_WRITE);
2323 }
2324 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002325
Jens Axboe9adbd452019-12-20 08:45:55 -07002326 if (req->file->f_op->write_iter)
2327 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002328 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002329 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002330 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002331 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002332 } else {
2333copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002334 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002335 inline_vecs, &iter);
2336 if (ret)
2337 goto out_free;
2338 return -EAGAIN;
2339 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002340 }
Jens Axboe31b51512019-01-18 22:56:34 -07002341out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002342 if (!io_wq_current_is_worker())
2343 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002344 return ret;
2345}
2346
2347/*
2348 * IORING_OP_NOP just posts a completion event, nothing else.
2349 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002350static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002351{
2352 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002353
Jens Axboedef596e2019-01-09 08:59:42 -07002354 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2355 return -EINVAL;
2356
Jens Axboe78e19bb2019-11-06 15:21:34 -07002357 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002358 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002359 return 0;
2360}
2361
Jens Axboe3529d8c2019-12-19 18:24:38 -07002362static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002363{
Jens Axboe6b063142019-01-10 22:13:58 -07002364 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002365
Jens Axboe09bb8392019-03-13 12:39:28 -06002366 if (!req->file)
2367 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002368
Jens Axboe6b063142019-01-10 22:13:58 -07002369 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002370 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002371 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002372 return -EINVAL;
2373
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002374 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2375 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2376 return -EINVAL;
2377
2378 req->sync.off = READ_ONCE(sqe->off);
2379 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002380 return 0;
2381}
2382
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002383static bool io_req_cancelled(struct io_kiocb *req)
2384{
2385 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2386 req_set_fail_links(req);
2387 io_cqring_add_event(req, -ECANCELED);
2388 io_put_req(req);
2389 return true;
2390 }
2391
2392 return false;
2393}
2394
Jens Axboe78912932020-01-14 22:09:06 -07002395static void io_link_work_cb(struct io_wq_work **workptr)
2396{
2397 struct io_wq_work *work = *workptr;
2398 struct io_kiocb *link = work->data;
2399
2400 io_queue_linked_timeout(link);
2401 work->func = io_wq_submit_work;
2402}
2403
2404static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2405{
2406 struct io_kiocb *link;
2407
2408 io_prep_async_work(nxt, &link);
2409 *workptr = &nxt->work;
2410 if (link) {
2411 nxt->work.flags |= IO_WQ_WORK_CB;
2412 nxt->work.func = io_link_work_cb;
2413 nxt->work.data = link;
2414 }
2415}
2416
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002417static void io_fsync_finish(struct io_wq_work **workptr)
2418{
2419 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2420 loff_t end = req->sync.off + req->sync.len;
2421 struct io_kiocb *nxt = NULL;
2422 int ret;
2423
2424 if (io_req_cancelled(req))
2425 return;
2426
Jens Axboe9adbd452019-12-20 08:45:55 -07002427 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002428 end > 0 ? end : LLONG_MAX,
2429 req->sync.flags & IORING_FSYNC_DATASYNC);
2430 if (ret < 0)
2431 req_set_fail_links(req);
2432 io_cqring_add_event(req, ret);
2433 io_put_req_find_next(req, &nxt);
2434 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002435 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002436}
2437
Jens Axboefc4df992019-12-10 14:38:45 -07002438static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2439 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002440{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002441 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002442
2443 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002444 if (force_nonblock) {
2445 io_put_req(req);
2446 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002447 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002448 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002449
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002450 work = old_work = &req->work;
2451 io_fsync_finish(&work);
2452 if (work && work != old_work)
2453 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002454 return 0;
2455}
2456
Jens Axboed63d1b52019-12-10 10:38:56 -07002457static void io_fallocate_finish(struct io_wq_work **workptr)
2458{
2459 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2460 struct io_kiocb *nxt = NULL;
2461 int ret;
2462
2463 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2464 req->sync.len);
2465 if (ret < 0)
2466 req_set_fail_links(req);
2467 io_cqring_add_event(req, ret);
2468 io_put_req_find_next(req, &nxt);
2469 if (nxt)
2470 io_wq_assign_next(workptr, nxt);
2471}
2472
2473static int io_fallocate_prep(struct io_kiocb *req,
2474 const struct io_uring_sqe *sqe)
2475{
2476 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2477 return -EINVAL;
2478
2479 req->sync.off = READ_ONCE(sqe->off);
2480 req->sync.len = READ_ONCE(sqe->addr);
2481 req->sync.mode = READ_ONCE(sqe->len);
2482 return 0;
2483}
2484
2485static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2486 bool force_nonblock)
2487{
2488 struct io_wq_work *work, *old_work;
2489
2490 /* fallocate always requiring blocking context */
2491 if (force_nonblock) {
2492 io_put_req(req);
2493 req->work.func = io_fallocate_finish;
2494 return -EAGAIN;
2495 }
2496
2497 work = old_work = &req->work;
2498 io_fallocate_finish(&work);
2499 if (work && work != old_work)
2500 *nxt = container_of(work, struct io_kiocb, work);
2501
2502 return 0;
2503}
2504
Jens Axboe15b71ab2019-12-11 11:20:36 -07002505static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2506{
Jens Axboef8748882020-01-08 17:47:02 -07002507 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002508 int ret;
2509
2510 if (sqe->ioprio || sqe->buf_index)
2511 return -EINVAL;
2512
2513 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002514 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002515 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002516 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002517
Jens Axboef8748882020-01-08 17:47:02 -07002518 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002519 if (IS_ERR(req->open.filename)) {
2520 ret = PTR_ERR(req->open.filename);
2521 req->open.filename = NULL;
2522 return ret;
2523 }
2524
2525 return 0;
2526}
2527
Jens Axboecebdb982020-01-08 17:59:24 -07002528static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2529{
2530 struct open_how __user *how;
2531 const char __user *fname;
2532 size_t len;
2533 int ret;
2534
2535 if (sqe->ioprio || sqe->buf_index)
2536 return -EINVAL;
2537
2538 req->open.dfd = READ_ONCE(sqe->fd);
2539 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2540 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2541 len = READ_ONCE(sqe->len);
2542
2543 if (len < OPEN_HOW_SIZE_VER0)
2544 return -EINVAL;
2545
2546 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2547 len);
2548 if (ret)
2549 return ret;
2550
2551 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2552 req->open.how.flags |= O_LARGEFILE;
2553
2554 req->open.filename = getname(fname);
2555 if (IS_ERR(req->open.filename)) {
2556 ret = PTR_ERR(req->open.filename);
2557 req->open.filename = NULL;
2558 return ret;
2559 }
2560
2561 return 0;
2562}
2563
2564static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2565 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002566{
2567 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002568 struct file *file;
2569 int ret;
2570
2571 if (force_nonblock) {
2572 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2573 return -EAGAIN;
2574 }
2575
Jens Axboecebdb982020-01-08 17:59:24 -07002576 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002577 if (ret)
2578 goto err;
2579
Jens Axboecebdb982020-01-08 17:59:24 -07002580 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002581 if (ret < 0)
2582 goto err;
2583
2584 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2585 if (IS_ERR(file)) {
2586 put_unused_fd(ret);
2587 ret = PTR_ERR(file);
2588 } else {
2589 fsnotify_open(file);
2590 fd_install(ret, file);
2591 }
2592err:
2593 putname(req->open.filename);
2594 if (ret < 0)
2595 req_set_fail_links(req);
2596 io_cqring_add_event(req, ret);
2597 io_put_req_find_next(req, nxt);
2598 return 0;
2599}
2600
Jens Axboecebdb982020-01-08 17:59:24 -07002601static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2602 bool force_nonblock)
2603{
2604 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2605 return io_openat2(req, nxt, force_nonblock);
2606}
2607
Jens Axboec1ca7572019-12-25 22:18:28 -07002608static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2609{
2610#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2611 if (sqe->ioprio || sqe->buf_index || sqe->off)
2612 return -EINVAL;
2613
2614 req->madvise.addr = READ_ONCE(sqe->addr);
2615 req->madvise.len = READ_ONCE(sqe->len);
2616 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2617 return 0;
2618#else
2619 return -EOPNOTSUPP;
2620#endif
2621}
2622
2623static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2624 bool force_nonblock)
2625{
2626#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2627 struct io_madvise *ma = &req->madvise;
2628 int ret;
2629
2630 if (force_nonblock)
2631 return -EAGAIN;
2632
2633 ret = do_madvise(ma->addr, ma->len, ma->advice);
2634 if (ret < 0)
2635 req_set_fail_links(req);
2636 io_cqring_add_event(req, ret);
2637 io_put_req_find_next(req, nxt);
2638 return 0;
2639#else
2640 return -EOPNOTSUPP;
2641#endif
2642}
2643
Jens Axboe4840e412019-12-25 22:03:45 -07002644static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2645{
2646 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2647 return -EINVAL;
2648
2649 req->fadvise.offset = READ_ONCE(sqe->off);
2650 req->fadvise.len = READ_ONCE(sqe->len);
2651 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2652 return 0;
2653}
2654
2655static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2656 bool force_nonblock)
2657{
2658 struct io_fadvise *fa = &req->fadvise;
2659 int ret;
2660
2661 /* DONTNEED may block, others _should_ not */
2662 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2663 return -EAGAIN;
2664
2665 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2666 if (ret < 0)
2667 req_set_fail_links(req);
2668 io_cqring_add_event(req, ret);
2669 io_put_req_find_next(req, nxt);
2670 return 0;
2671}
2672
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002673static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2674{
Jens Axboef8748882020-01-08 17:47:02 -07002675 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002676 unsigned lookup_flags;
2677 int ret;
2678
2679 if (sqe->ioprio || sqe->buf_index)
2680 return -EINVAL;
2681
2682 req->open.dfd = READ_ONCE(sqe->fd);
2683 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002684 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002685 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002686 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002687
Jens Axboec12cedf2020-01-08 17:41:21 -07002688 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002689 return -EINVAL;
2690
Jens Axboef8748882020-01-08 17:47:02 -07002691 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002692 if (IS_ERR(req->open.filename)) {
2693 ret = PTR_ERR(req->open.filename);
2694 req->open.filename = NULL;
2695 return ret;
2696 }
2697
2698 return 0;
2699}
2700
2701static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2702 bool force_nonblock)
2703{
2704 struct io_open *ctx = &req->open;
2705 unsigned lookup_flags;
2706 struct path path;
2707 struct kstat stat;
2708 int ret;
2709
2710 if (force_nonblock)
2711 return -EAGAIN;
2712
Jens Axboec12cedf2020-01-08 17:41:21 -07002713 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002714 return -EINVAL;
2715
2716retry:
2717 /* filename_lookup() drops it, keep a reference */
2718 ctx->filename->refcnt++;
2719
2720 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2721 NULL);
2722 if (ret)
2723 goto err;
2724
Jens Axboec12cedf2020-01-08 17:41:21 -07002725 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002726 path_put(&path);
2727 if (retry_estale(ret, lookup_flags)) {
2728 lookup_flags |= LOOKUP_REVAL;
2729 goto retry;
2730 }
2731 if (!ret)
2732 ret = cp_statx(&stat, ctx->buffer);
2733err:
2734 putname(ctx->filename);
2735 if (ret < 0)
2736 req_set_fail_links(req);
2737 io_cqring_add_event(req, ret);
2738 io_put_req_find_next(req, nxt);
2739 return 0;
2740}
2741
Jens Axboeb5dba592019-12-11 14:02:38 -07002742static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2743{
2744 /*
2745 * If we queue this for async, it must not be cancellable. That would
2746 * leave the 'file' in an undeterminate state.
2747 */
2748 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2749
2750 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2751 sqe->rw_flags || sqe->buf_index)
2752 return -EINVAL;
2753 if (sqe->flags & IOSQE_FIXED_FILE)
2754 return -EINVAL;
2755
2756 req->close.fd = READ_ONCE(sqe->fd);
2757 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002758 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002759 return -EBADF;
2760
2761 return 0;
2762}
2763
2764static void io_close_finish(struct io_wq_work **workptr)
2765{
2766 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2767 struct io_kiocb *nxt = NULL;
2768
2769 /* Invoked with files, we need to do the close */
2770 if (req->work.files) {
2771 int ret;
2772
2773 ret = filp_close(req->close.put_file, req->work.files);
2774 if (ret < 0) {
2775 req_set_fail_links(req);
2776 }
2777 io_cqring_add_event(req, ret);
2778 }
2779
2780 fput(req->close.put_file);
2781
2782 /* we bypassed the re-issue, drop the submission reference */
2783 io_put_req(req);
2784 io_put_req_find_next(req, &nxt);
2785 if (nxt)
2786 io_wq_assign_next(workptr, nxt);
2787}
2788
2789static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2790 bool force_nonblock)
2791{
2792 int ret;
2793
2794 req->close.put_file = NULL;
2795 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2796 if (ret < 0)
2797 return ret;
2798
2799 /* if the file has a flush method, be safe and punt to async */
2800 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2801 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2802 goto eagain;
2803 }
2804
2805 /*
2806 * No ->flush(), safely close from here and just punt the
2807 * fput() to async context.
2808 */
2809 ret = filp_close(req->close.put_file, current->files);
2810
2811 if (ret < 0)
2812 req_set_fail_links(req);
2813 io_cqring_add_event(req, ret);
2814
2815 if (io_wq_current_is_worker()) {
2816 struct io_wq_work *old_work, *work;
2817
2818 old_work = work = &req->work;
2819 io_close_finish(&work);
2820 if (work && work != old_work)
2821 *nxt = container_of(work, struct io_kiocb, work);
2822 return 0;
2823 }
2824
2825eagain:
2826 req->work.func = io_close_finish;
2827 return -EAGAIN;
2828}
2829
Jens Axboe3529d8c2019-12-19 18:24:38 -07002830static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002831{
2832 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002833
2834 if (!req->file)
2835 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002836
2837 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2838 return -EINVAL;
2839 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2840 return -EINVAL;
2841
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002842 req->sync.off = READ_ONCE(sqe->off);
2843 req->sync.len = READ_ONCE(sqe->len);
2844 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002845 return 0;
2846}
2847
2848static void io_sync_file_range_finish(struct io_wq_work **workptr)
2849{
2850 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2851 struct io_kiocb *nxt = NULL;
2852 int ret;
2853
2854 if (io_req_cancelled(req))
2855 return;
2856
Jens Axboe9adbd452019-12-20 08:45:55 -07002857 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002858 req->sync.flags);
2859 if (ret < 0)
2860 req_set_fail_links(req);
2861 io_cqring_add_event(req, ret);
2862 io_put_req_find_next(req, &nxt);
2863 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002864 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002865}
2866
Jens Axboefc4df992019-12-10 14:38:45 -07002867static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002868 bool force_nonblock)
2869{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002870 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002871
2872 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002873 if (force_nonblock) {
2874 io_put_req(req);
2875 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002876 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002877 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002878
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002879 work = old_work = &req->work;
2880 io_sync_file_range_finish(&work);
2881 if (work && work != old_work)
2882 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002883 return 0;
2884}
2885
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002886#if defined(CONFIG_NET)
2887static void io_sendrecv_async(struct io_wq_work **workptr)
2888{
2889 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2890 struct iovec *iov = NULL;
2891
2892 if (req->io->rw.iov != req->io->rw.fast_iov)
2893 iov = req->io->msg.iov;
2894 io_wq_submit_work(workptr);
2895 kfree(iov);
2896}
2897#endif
2898
Jens Axboe3529d8c2019-12-19 18:24:38 -07002899static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002900{
Jens Axboe03b12302019-12-02 18:50:25 -07002901#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002902 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002903 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002904
Jens Axboee47293f2019-12-20 08:58:21 -07002905 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2906 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002907 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002908
Jens Axboefddafac2020-01-04 20:19:44 -07002909 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002910 return 0;
2911
Jens Axboed9688562019-12-09 19:35:20 -07002912 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002913 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002914 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002915#else
Jens Axboee47293f2019-12-20 08:58:21 -07002916 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002917#endif
2918}
2919
Jens Axboefc4df992019-12-10 14:38:45 -07002920static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2921 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002922{
2923#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002924 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002925 struct socket *sock;
2926 int ret;
2927
2928 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2929 return -EINVAL;
2930
2931 sock = sock_from_file(req->file, &ret);
2932 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002933 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002934 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002935 unsigned flags;
2936
Jens Axboe03b12302019-12-02 18:50:25 -07002937 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002938 kmsg = &req->io->msg;
2939 kmsg->msg.msg_name = &addr;
2940 /* if iov is set, it's allocated already */
2941 if (!kmsg->iov)
2942 kmsg->iov = kmsg->fast_iov;
2943 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002944 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002945 struct io_sr_msg *sr = &req->sr_msg;
2946
Jens Axboe0b416c32019-12-15 10:57:46 -07002947 kmsg = &io.msg;
2948 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002949
2950 io.msg.iov = io.msg.fast_iov;
2951 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2952 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002953 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002954 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002955 }
2956
Jens Axboee47293f2019-12-20 08:58:21 -07002957 flags = req->sr_msg.msg_flags;
2958 if (flags & MSG_DONTWAIT)
2959 req->flags |= REQ_F_NOWAIT;
2960 else if (force_nonblock)
2961 flags |= MSG_DONTWAIT;
2962
Jens Axboe0b416c32019-12-15 10:57:46 -07002963 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002964 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002965 if (req->io)
2966 return -EAGAIN;
2967 if (io_alloc_async_ctx(req))
2968 return -ENOMEM;
2969 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2970 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002971 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002972 }
2973 if (ret == -ERESTARTSYS)
2974 ret = -EINTR;
2975 }
2976
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002977 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002978 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002979 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002980 if (ret < 0)
2981 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002982 io_put_req_find_next(req, nxt);
2983 return 0;
2984#else
2985 return -EOPNOTSUPP;
2986#endif
2987}
2988
Jens Axboefddafac2020-01-04 20:19:44 -07002989static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2990 bool force_nonblock)
2991{
2992#if defined(CONFIG_NET)
2993 struct socket *sock;
2994 int ret;
2995
2996 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2997 return -EINVAL;
2998
2999 sock = sock_from_file(req->file, &ret);
3000 if (sock) {
3001 struct io_sr_msg *sr = &req->sr_msg;
3002 struct msghdr msg;
3003 struct iovec iov;
3004 unsigned flags;
3005
3006 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3007 &msg.msg_iter);
3008 if (ret)
3009 return ret;
3010
3011 msg.msg_name = NULL;
3012 msg.msg_control = NULL;
3013 msg.msg_controllen = 0;
3014 msg.msg_namelen = 0;
3015
3016 flags = req->sr_msg.msg_flags;
3017 if (flags & MSG_DONTWAIT)
3018 req->flags |= REQ_F_NOWAIT;
3019 else if (force_nonblock)
3020 flags |= MSG_DONTWAIT;
3021
3022 ret = __sys_sendmsg_sock(sock, &msg, flags);
3023 if (force_nonblock && ret == -EAGAIN)
3024 return -EAGAIN;
3025 if (ret == -ERESTARTSYS)
3026 ret = -EINTR;
3027 }
3028
3029 io_cqring_add_event(req, ret);
3030 if (ret < 0)
3031 req_set_fail_links(req);
3032 io_put_req_find_next(req, nxt);
3033 return 0;
3034#else
3035 return -EOPNOTSUPP;
3036#endif
3037}
3038
Jens Axboe3529d8c2019-12-19 18:24:38 -07003039static int io_recvmsg_prep(struct io_kiocb *req,
3040 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003041{
3042#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003043 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003044 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07003045
Jens Axboe3529d8c2019-12-19 18:24:38 -07003046 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3047 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
3048
Jens Axboefddafac2020-01-04 20:19:44 -07003049 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003050 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003051
Jens Axboed9688562019-12-09 19:35:20 -07003052 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003053 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003054 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003055#else
Jens Axboee47293f2019-12-20 08:58:21 -07003056 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003057#endif
3058}
3059
Jens Axboefc4df992019-12-10 14:38:45 -07003060static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3061 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003062{
3063#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003064 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003065 struct socket *sock;
3066 int ret;
3067
3068 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3069 return -EINVAL;
3070
3071 sock = sock_from_file(req->file, &ret);
3072 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003073 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003074 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003075 unsigned flags;
3076
Jens Axboe03b12302019-12-02 18:50:25 -07003077 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003078 kmsg = &req->io->msg;
3079 kmsg->msg.msg_name = &addr;
3080 /* if iov is set, it's allocated already */
3081 if (!kmsg->iov)
3082 kmsg->iov = kmsg->fast_iov;
3083 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003084 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003085 struct io_sr_msg *sr = &req->sr_msg;
3086
Jens Axboe0b416c32019-12-15 10:57:46 -07003087 kmsg = &io.msg;
3088 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003089
3090 io.msg.iov = io.msg.fast_iov;
3091 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3092 sr->msg_flags, &io.msg.uaddr,
3093 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003094 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003095 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003096 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003097
Jens Axboee47293f2019-12-20 08:58:21 -07003098 flags = req->sr_msg.msg_flags;
3099 if (flags & MSG_DONTWAIT)
3100 req->flags |= REQ_F_NOWAIT;
3101 else if (force_nonblock)
3102 flags |= MSG_DONTWAIT;
3103
3104 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3105 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003106 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003107 if (req->io)
3108 return -EAGAIN;
3109 if (io_alloc_async_ctx(req))
3110 return -ENOMEM;
3111 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3112 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003113 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003114 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003115 if (ret == -ERESTARTSYS)
3116 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003117 }
3118
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003119 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003120 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003121 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003122 if (ret < 0)
3123 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003124 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003125 return 0;
3126#else
3127 return -EOPNOTSUPP;
3128#endif
3129}
3130
Jens Axboefddafac2020-01-04 20:19:44 -07003131static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3132 bool force_nonblock)
3133{
3134#if defined(CONFIG_NET)
3135 struct socket *sock;
3136 int ret;
3137
3138 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3139 return -EINVAL;
3140
3141 sock = sock_from_file(req->file, &ret);
3142 if (sock) {
3143 struct io_sr_msg *sr = &req->sr_msg;
3144 struct msghdr msg;
3145 struct iovec iov;
3146 unsigned flags;
3147
3148 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3149 &msg.msg_iter);
3150 if (ret)
3151 return ret;
3152
3153 msg.msg_name = NULL;
3154 msg.msg_control = NULL;
3155 msg.msg_controllen = 0;
3156 msg.msg_namelen = 0;
3157 msg.msg_iocb = NULL;
3158 msg.msg_flags = 0;
3159
3160 flags = req->sr_msg.msg_flags;
3161 if (flags & MSG_DONTWAIT)
3162 req->flags |= REQ_F_NOWAIT;
3163 else if (force_nonblock)
3164 flags |= MSG_DONTWAIT;
3165
3166 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3167 if (force_nonblock && ret == -EAGAIN)
3168 return -EAGAIN;
3169 if (ret == -ERESTARTSYS)
3170 ret = -EINTR;
3171 }
3172
3173 io_cqring_add_event(req, ret);
3174 if (ret < 0)
3175 req_set_fail_links(req);
3176 io_put_req_find_next(req, nxt);
3177 return 0;
3178#else
3179 return -EOPNOTSUPP;
3180#endif
3181}
3182
3183
Jens Axboe3529d8c2019-12-19 18:24:38 -07003184static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003185{
3186#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003187 struct io_accept *accept = &req->accept;
3188
Jens Axboe17f2fe32019-10-17 14:42:58 -06003189 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3190 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003191 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003192 return -EINVAL;
3193
Jens Axboed55e5f52019-12-11 16:12:15 -07003194 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3195 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003196 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003197 return 0;
3198#else
3199 return -EOPNOTSUPP;
3200#endif
3201}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003202
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003203#if defined(CONFIG_NET)
3204static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3205 bool force_nonblock)
3206{
3207 struct io_accept *accept = &req->accept;
3208 unsigned file_flags;
3209 int ret;
3210
3211 file_flags = force_nonblock ? O_NONBLOCK : 0;
3212 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3213 accept->addr_len, accept->flags);
3214 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003215 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003216 if (ret == -ERESTARTSYS)
3217 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003218 if (ret < 0)
3219 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003220 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003221 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003222 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003223}
3224
3225static void io_accept_finish(struct io_wq_work **workptr)
3226{
3227 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3228 struct io_kiocb *nxt = NULL;
3229
3230 if (io_req_cancelled(req))
3231 return;
3232 __io_accept(req, &nxt, false);
3233 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003234 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003235}
3236#endif
3237
3238static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3239 bool force_nonblock)
3240{
3241#if defined(CONFIG_NET)
3242 int ret;
3243
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003244 ret = __io_accept(req, nxt, force_nonblock);
3245 if (ret == -EAGAIN && force_nonblock) {
3246 req->work.func = io_accept_finish;
3247 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3248 io_put_req(req);
3249 return -EAGAIN;
3250 }
3251 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003252#else
3253 return -EOPNOTSUPP;
3254#endif
3255}
3256
Jens Axboe3529d8c2019-12-19 18:24:38 -07003257static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003258{
3259#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003260 struct io_connect *conn = &req->connect;
3261 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003262
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003263 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3264 return -EINVAL;
3265 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3266 return -EINVAL;
3267
Jens Axboe3529d8c2019-12-19 18:24:38 -07003268 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3269 conn->addr_len = READ_ONCE(sqe->addr2);
3270
3271 if (!io)
3272 return 0;
3273
3274 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003275 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003276#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003277 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003278#endif
3279}
3280
Jens Axboefc4df992019-12-10 14:38:45 -07003281static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3282 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003283{
3284#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003285 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003286 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003287 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003288
Jens Axboef499a022019-12-02 16:28:46 -07003289 if (req->io) {
3290 io = req->io;
3291 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003292 ret = move_addr_to_kernel(req->connect.addr,
3293 req->connect.addr_len,
3294 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003295 if (ret)
3296 goto out;
3297 io = &__io;
3298 }
3299
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003300 file_flags = force_nonblock ? O_NONBLOCK : 0;
3301
3302 ret = __sys_connect_file(req->file, &io->connect.address,
3303 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003304 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003305 if (req->io)
3306 return -EAGAIN;
3307 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003308 ret = -ENOMEM;
3309 goto out;
3310 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003311 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003312 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003313 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003314 if (ret == -ERESTARTSYS)
3315 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003316out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003317 if (ret < 0)
3318 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003319 io_cqring_add_event(req, ret);
3320 io_put_req_find_next(req, nxt);
3321 return 0;
3322#else
3323 return -EOPNOTSUPP;
3324#endif
3325}
3326
Jens Axboe221c5eb2019-01-17 09:41:58 -07003327static void io_poll_remove_one(struct io_kiocb *req)
3328{
3329 struct io_poll_iocb *poll = &req->poll;
3330
3331 spin_lock(&poll->head->lock);
3332 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003333 if (!list_empty(&poll->wait.entry)) {
3334 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003335 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003336 }
3337 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003338 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003339}
3340
3341static void io_poll_remove_all(struct io_ring_ctx *ctx)
3342{
Jens Axboe78076bb2019-12-04 19:56:40 -07003343 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003344 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003345 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003346
3347 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003348 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3349 struct hlist_head *list;
3350
3351 list = &ctx->cancel_hash[i];
3352 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3353 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003354 }
3355 spin_unlock_irq(&ctx->completion_lock);
3356}
3357
Jens Axboe47f46762019-11-09 17:43:02 -07003358static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3359{
Jens Axboe78076bb2019-12-04 19:56:40 -07003360 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003361 struct io_kiocb *req;
3362
Jens Axboe78076bb2019-12-04 19:56:40 -07003363 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3364 hlist_for_each_entry(req, list, hash_node) {
3365 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003366 io_poll_remove_one(req);
3367 return 0;
3368 }
Jens Axboe47f46762019-11-09 17:43:02 -07003369 }
3370
3371 return -ENOENT;
3372}
3373
Jens Axboe3529d8c2019-12-19 18:24:38 -07003374static int io_poll_remove_prep(struct io_kiocb *req,
3375 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003376{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003377 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3378 return -EINVAL;
3379 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3380 sqe->poll_events)
3381 return -EINVAL;
3382
Jens Axboe0969e782019-12-17 18:40:57 -07003383 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003384 return 0;
3385}
3386
3387/*
3388 * Find a running poll command that matches one specified in sqe->addr,
3389 * and remove it if found.
3390 */
3391static int io_poll_remove(struct io_kiocb *req)
3392{
3393 struct io_ring_ctx *ctx = req->ctx;
3394 u64 addr;
3395 int ret;
3396
Jens Axboe0969e782019-12-17 18:40:57 -07003397 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003398 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003399 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003400 spin_unlock_irq(&ctx->completion_lock);
3401
Jens Axboe78e19bb2019-11-06 15:21:34 -07003402 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003403 if (ret < 0)
3404 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003405 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003406 return 0;
3407}
3408
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003409static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003410{
Jackie Liua197f662019-11-08 08:09:12 -07003411 struct io_ring_ctx *ctx = req->ctx;
3412
Jens Axboe8c838782019-03-12 15:48:16 -06003413 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003414 if (error)
3415 io_cqring_fill_event(req, error);
3416 else
3417 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003418 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003419}
3420
Jens Axboe561fb042019-10-24 07:25:42 -06003421static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003422{
Jens Axboe561fb042019-10-24 07:25:42 -06003423 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003424 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3425 struct io_poll_iocb *poll = &req->poll;
3426 struct poll_table_struct pt = { ._key = poll->events };
3427 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003428 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003429 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003430 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003431
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003432 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003433 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003434 ret = -ECANCELED;
3435 } else if (READ_ONCE(poll->canceled)) {
3436 ret = -ECANCELED;
3437 }
Jens Axboe561fb042019-10-24 07:25:42 -06003438
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003439 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003440 mask = vfs_poll(poll->file, &pt) & poll->events;
3441
3442 /*
3443 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3444 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3445 * synchronize with them. In the cancellation case the list_del_init
3446 * itself is not actually needed, but harmless so we keep it in to
3447 * avoid further branches in the fast path.
3448 */
3449 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003450 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003451 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003452 spin_unlock_irq(&ctx->completion_lock);
3453 return;
3454 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003455 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003456 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003457 spin_unlock_irq(&ctx->completion_lock);
3458
Jens Axboe8c838782019-03-12 15:48:16 -06003459 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003460
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003461 if (ret < 0)
3462 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003463 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003464 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003465 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003466}
3467
Jens Axboee94f1412019-12-19 12:06:02 -07003468static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3469{
Jens Axboee94f1412019-12-19 12:06:02 -07003470 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003471 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003472
Jens Axboec6ca97b302019-12-28 12:11:08 -07003473 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003474 spin_lock_irq(&ctx->completion_lock);
3475 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3476 hash_del(&req->hash_node);
3477 io_poll_complete(req, req->result, 0);
3478
Jens Axboe8237e042019-12-28 10:48:22 -07003479 if (refcount_dec_and_test(&req->refs) &&
3480 !io_req_multi_free(&rb, req)) {
3481 req->flags |= REQ_F_COMP_LOCKED;
3482 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003483 }
3484 }
3485 spin_unlock_irq(&ctx->completion_lock);
3486
3487 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003488 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003489}
3490
3491static void io_poll_flush(struct io_wq_work **workptr)
3492{
3493 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3494 struct llist_node *nodes;
3495
3496 nodes = llist_del_all(&req->ctx->poll_llist);
3497 if (nodes)
3498 __io_poll_flush(req->ctx, nodes);
3499}
3500
Jens Axboe221c5eb2019-01-17 09:41:58 -07003501static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3502 void *key)
3503{
Jens Axboee9444752019-11-26 15:02:04 -07003504 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003505 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3506 struct io_ring_ctx *ctx = req->ctx;
3507 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003508
3509 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003510 if (mask && !(mask & poll->events))
3511 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003512
Jens Axboe392edb42019-12-09 17:52:20 -07003513 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003514
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003515 /*
3516 * Run completion inline if we can. We're using trylock here because
3517 * we are violating the completion_lock -> poll wq lock ordering.
3518 * If we have a link timeout we're going to need the completion_lock
3519 * for finalizing the request, mark us as having grabbed that already.
3520 */
Jens Axboee94f1412019-12-19 12:06:02 -07003521 if (mask) {
3522 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003523
Jens Axboee94f1412019-12-19 12:06:02 -07003524 if (llist_empty(&ctx->poll_llist) &&
3525 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3526 hash_del(&req->hash_node);
3527 io_poll_complete(req, mask, 0);
3528 req->flags |= REQ_F_COMP_LOCKED;
3529 io_put_req(req);
3530 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3531
3532 io_cqring_ev_posted(ctx);
3533 req = NULL;
3534 } else {
3535 req->result = mask;
3536 req->llist_node.next = NULL;
3537 /* if the list wasn't empty, we're done */
3538 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3539 req = NULL;
3540 else
3541 req->work.func = io_poll_flush;
3542 }
Jens Axboe8c838782019-03-12 15:48:16 -06003543 }
Jens Axboee94f1412019-12-19 12:06:02 -07003544 if (req)
3545 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003546
Jens Axboe221c5eb2019-01-17 09:41:58 -07003547 return 1;
3548}
3549
3550struct io_poll_table {
3551 struct poll_table_struct pt;
3552 struct io_kiocb *req;
3553 int error;
3554};
3555
3556static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3557 struct poll_table_struct *p)
3558{
3559 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3560
3561 if (unlikely(pt->req->poll.head)) {
3562 pt->error = -EINVAL;
3563 return;
3564 }
3565
3566 pt->error = 0;
3567 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003568 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003569}
3570
Jens Axboeeac406c2019-11-14 12:09:58 -07003571static void io_poll_req_insert(struct io_kiocb *req)
3572{
3573 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003574 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003575
Jens Axboe78076bb2019-12-04 19:56:40 -07003576 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3577 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003578}
3579
Jens Axboe3529d8c2019-12-19 18:24:38 -07003580static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003581{
3582 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003583 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003584
3585 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3586 return -EINVAL;
3587 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3588 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003589 if (!poll->file)
3590 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003591
Jens Axboe221c5eb2019-01-17 09:41:58 -07003592 events = READ_ONCE(sqe->poll_events);
3593 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003594 return 0;
3595}
3596
3597static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3598{
3599 struct io_poll_iocb *poll = &req->poll;
3600 struct io_ring_ctx *ctx = req->ctx;
3601 struct io_poll_table ipt;
3602 bool cancel = false;
3603 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003604
3605 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003606 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003607
Jens Axboe221c5eb2019-01-17 09:41:58 -07003608 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003609 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003610 poll->canceled = false;
3611
3612 ipt.pt._qproc = io_poll_queue_proc;
3613 ipt.pt._key = poll->events;
3614 ipt.req = req;
3615 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3616
3617 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003618 INIT_LIST_HEAD(&poll->wait.entry);
3619 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3620 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003621
Jens Axboe36703242019-07-25 10:20:18 -06003622 INIT_LIST_HEAD(&req->list);
3623
Jens Axboe221c5eb2019-01-17 09:41:58 -07003624 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003625
3626 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003627 if (likely(poll->head)) {
3628 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003629 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003630 if (ipt.error)
3631 cancel = true;
3632 ipt.error = 0;
3633 mask = 0;
3634 }
3635 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003636 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003637 else if (cancel)
3638 WRITE_ONCE(poll->canceled, true);
3639 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003640 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003641 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003642 }
Jens Axboe8c838782019-03-12 15:48:16 -06003643 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003644 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003645 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003646 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003647 spin_unlock_irq(&ctx->completion_lock);
3648
Jens Axboe8c838782019-03-12 15:48:16 -06003649 if (mask) {
3650 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003651 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003652 }
Jens Axboe8c838782019-03-12 15:48:16 -06003653 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003654}
3655
Jens Axboe5262f562019-09-17 12:26:57 -06003656static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3657{
Jens Axboead8a48a2019-11-15 08:49:11 -07003658 struct io_timeout_data *data = container_of(timer,
3659 struct io_timeout_data, timer);
3660 struct io_kiocb *req = data->req;
3661 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003662 unsigned long flags;
3663
Jens Axboe5262f562019-09-17 12:26:57 -06003664 atomic_inc(&ctx->cq_timeouts);
3665
3666 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003667 /*
Jens Axboe11365042019-10-16 09:08:32 -06003668 * We could be racing with timeout deletion. If the list is empty,
3669 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003670 */
Jens Axboe842f9612019-10-29 12:34:10 -06003671 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003672 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003673
Jens Axboe11365042019-10-16 09:08:32 -06003674 /*
3675 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003676 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003677 * pointer will be increased, otherwise other timeout reqs may
3678 * return in advance without waiting for enough wait_nr.
3679 */
3680 prev = req;
3681 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3682 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003683 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003684 }
Jens Axboe842f9612019-10-29 12:34:10 -06003685
Jens Axboe78e19bb2019-11-06 15:21:34 -07003686 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003687 io_commit_cqring(ctx);
3688 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3689
3690 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003691 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003692 io_put_req(req);
3693 return HRTIMER_NORESTART;
3694}
3695
Jens Axboe47f46762019-11-09 17:43:02 -07003696static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3697{
3698 struct io_kiocb *req;
3699 int ret = -ENOENT;
3700
3701 list_for_each_entry(req, &ctx->timeout_list, list) {
3702 if (user_data == req->user_data) {
3703 list_del_init(&req->list);
3704 ret = 0;
3705 break;
3706 }
3707 }
3708
3709 if (ret == -ENOENT)
3710 return ret;
3711
Jens Axboe2d283902019-12-04 11:08:05 -07003712 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003713 if (ret == -1)
3714 return -EALREADY;
3715
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003716 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003717 io_cqring_fill_event(req, -ECANCELED);
3718 io_put_req(req);
3719 return 0;
3720}
3721
Jens Axboe3529d8c2019-12-19 18:24:38 -07003722static int io_timeout_remove_prep(struct io_kiocb *req,
3723 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003724{
Jens Axboeb29472e2019-12-17 18:50:29 -07003725 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3726 return -EINVAL;
3727 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3728 return -EINVAL;
3729
3730 req->timeout.addr = READ_ONCE(sqe->addr);
3731 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3732 if (req->timeout.flags)
3733 return -EINVAL;
3734
Jens Axboeb29472e2019-12-17 18:50:29 -07003735 return 0;
3736}
3737
Jens Axboe11365042019-10-16 09:08:32 -06003738/*
3739 * Remove or update an existing timeout command
3740 */
Jens Axboefc4df992019-12-10 14:38:45 -07003741static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003742{
3743 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003744 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003745
Jens Axboe11365042019-10-16 09:08:32 -06003746 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003747 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003748
Jens Axboe47f46762019-11-09 17:43:02 -07003749 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003750 io_commit_cqring(ctx);
3751 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003752 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003753 if (ret < 0)
3754 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003755 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003756 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003757}
3758
Jens Axboe3529d8c2019-12-19 18:24:38 -07003759static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003760 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003761{
Jens Axboead8a48a2019-11-15 08:49:11 -07003762 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003763 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003764
Jens Axboead8a48a2019-11-15 08:49:11 -07003765 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003766 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003767 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003768 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003769 if (sqe->off && is_timeout_link)
3770 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003771 flags = READ_ONCE(sqe->timeout_flags);
3772 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003773 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003774
Jens Axboe26a61672019-12-20 09:02:01 -07003775 req->timeout.count = READ_ONCE(sqe->off);
3776
Jens Axboe3529d8c2019-12-19 18:24:38 -07003777 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003778 return -ENOMEM;
3779
3780 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003781 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003782 req->flags |= REQ_F_TIMEOUT;
3783
3784 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003785 return -EFAULT;
3786
Jens Axboe11365042019-10-16 09:08:32 -06003787 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003788 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003789 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003790 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003791
Jens Axboead8a48a2019-11-15 08:49:11 -07003792 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3793 return 0;
3794}
3795
Jens Axboefc4df992019-12-10 14:38:45 -07003796static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003797{
3798 unsigned count;
3799 struct io_ring_ctx *ctx = req->ctx;
3800 struct io_timeout_data *data;
3801 struct list_head *entry;
3802 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003803
Jens Axboe2d283902019-12-04 11:08:05 -07003804 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003805
Jens Axboe5262f562019-09-17 12:26:57 -06003806 /*
3807 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003808 * timeout event to be satisfied. If it isn't set, then this is
3809 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003810 */
Jens Axboe26a61672019-12-20 09:02:01 -07003811 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003812 if (!count) {
3813 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3814 spin_lock_irq(&ctx->completion_lock);
3815 entry = ctx->timeout_list.prev;
3816 goto add;
3817 }
Jens Axboe5262f562019-09-17 12:26:57 -06003818
3819 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003820 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003821
3822 /*
3823 * Insertion sort, ensuring the first entry in the list is always
3824 * the one we need first.
3825 */
Jens Axboe5262f562019-09-17 12:26:57 -06003826 spin_lock_irq(&ctx->completion_lock);
3827 list_for_each_prev(entry, &ctx->timeout_list) {
3828 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003829 unsigned nxt_sq_head;
3830 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003831 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003832
Jens Axboe93bd25b2019-11-11 23:34:31 -07003833 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3834 continue;
3835
yangerkun5da0fb12019-10-15 21:59:29 +08003836 /*
3837 * Since cached_sq_head + count - 1 can overflow, use type long
3838 * long to store it.
3839 */
3840 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003841 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3842 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003843
3844 /*
3845 * cached_sq_head may overflow, and it will never overflow twice
3846 * once there is some timeout req still be valid.
3847 */
3848 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003849 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003850
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003851 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003852 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003853
3854 /*
3855 * Sequence of reqs after the insert one and itself should
3856 * be adjusted because each timeout req consumes a slot.
3857 */
3858 span++;
3859 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003860 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003861 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003862add:
Jens Axboe5262f562019-09-17 12:26:57 -06003863 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003864 data->timer.function = io_timeout_fn;
3865 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003866 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003867 return 0;
3868}
3869
Jens Axboe62755e32019-10-28 21:49:21 -06003870static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003871{
Jens Axboe62755e32019-10-28 21:49:21 -06003872 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003873
Jens Axboe62755e32019-10-28 21:49:21 -06003874 return req->user_data == (unsigned long) data;
3875}
3876
Jens Axboee977d6d2019-11-05 12:39:45 -07003877static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003878{
Jens Axboe62755e32019-10-28 21:49:21 -06003879 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003880 int ret = 0;
3881
Jens Axboe62755e32019-10-28 21:49:21 -06003882 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3883 switch (cancel_ret) {
3884 case IO_WQ_CANCEL_OK:
3885 ret = 0;
3886 break;
3887 case IO_WQ_CANCEL_RUNNING:
3888 ret = -EALREADY;
3889 break;
3890 case IO_WQ_CANCEL_NOTFOUND:
3891 ret = -ENOENT;
3892 break;
3893 }
3894
Jens Axboee977d6d2019-11-05 12:39:45 -07003895 return ret;
3896}
3897
Jens Axboe47f46762019-11-09 17:43:02 -07003898static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3899 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003900 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003901{
3902 unsigned long flags;
3903 int ret;
3904
3905 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3906 if (ret != -ENOENT) {
3907 spin_lock_irqsave(&ctx->completion_lock, flags);
3908 goto done;
3909 }
3910
3911 spin_lock_irqsave(&ctx->completion_lock, flags);
3912 ret = io_timeout_cancel(ctx, sqe_addr);
3913 if (ret != -ENOENT)
3914 goto done;
3915 ret = io_poll_cancel(ctx, sqe_addr);
3916done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003917 if (!ret)
3918 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003919 io_cqring_fill_event(req, ret);
3920 io_commit_cqring(ctx);
3921 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3922 io_cqring_ev_posted(ctx);
3923
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003924 if (ret < 0)
3925 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003926 io_put_req_find_next(req, nxt);
3927}
3928
Jens Axboe3529d8c2019-12-19 18:24:38 -07003929static int io_async_cancel_prep(struct io_kiocb *req,
3930 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003931{
Jens Axboefbf23842019-12-17 18:45:56 -07003932 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003933 return -EINVAL;
3934 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3935 sqe->cancel_flags)
3936 return -EINVAL;
3937
Jens Axboefbf23842019-12-17 18:45:56 -07003938 req->cancel.addr = READ_ONCE(sqe->addr);
3939 return 0;
3940}
3941
3942static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3943{
3944 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003945
3946 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003947 return 0;
3948}
3949
Jens Axboe05f3fb32019-12-09 11:22:50 -07003950static int io_files_update_prep(struct io_kiocb *req,
3951 const struct io_uring_sqe *sqe)
3952{
3953 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3954 return -EINVAL;
3955
3956 req->files_update.offset = READ_ONCE(sqe->off);
3957 req->files_update.nr_args = READ_ONCE(sqe->len);
3958 if (!req->files_update.nr_args)
3959 return -EINVAL;
3960 req->files_update.arg = READ_ONCE(sqe->addr);
3961 return 0;
3962}
3963
3964static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3965{
3966 struct io_ring_ctx *ctx = req->ctx;
3967 struct io_uring_files_update up;
3968 int ret;
3969
3970 if (force_nonblock) {
3971 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3972 return -EAGAIN;
3973 }
3974
3975 up.offset = req->files_update.offset;
3976 up.fds = req->files_update.arg;
3977
3978 mutex_lock(&ctx->uring_lock);
3979 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3980 mutex_unlock(&ctx->uring_lock);
3981
3982 if (ret < 0)
3983 req_set_fail_links(req);
3984 io_cqring_add_event(req, ret);
3985 io_put_req(req);
3986 return 0;
3987}
3988
Jens Axboe3529d8c2019-12-19 18:24:38 -07003989static int io_req_defer_prep(struct io_kiocb *req,
3990 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003991{
Jens Axboee7815732019-12-17 19:45:06 -07003992 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003993
Jens Axboecccf0ee2020-01-27 16:34:48 -07003994 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
3995
Jens Axboed625c6e2019-12-17 19:53:05 -07003996 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003997 case IORING_OP_NOP:
3998 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003999 case IORING_OP_READV:
4000 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004001 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004002 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004003 break;
4004 case IORING_OP_WRITEV:
4005 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004006 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004007 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004008 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004009 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004010 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004011 break;
4012 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004013 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004014 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004015 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004016 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004017 break;
4018 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004019 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004020 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004021 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004022 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004023 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004024 break;
4025 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004026 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004027 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004028 break;
Jens Axboef499a022019-12-02 16:28:46 -07004029 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004030 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004031 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004032 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004033 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004034 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004035 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004036 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004037 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004038 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004039 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004040 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004041 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004042 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004043 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004044 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004045 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004046 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004047 case IORING_OP_FALLOCATE:
4048 ret = io_fallocate_prep(req, sqe);
4049 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004050 case IORING_OP_OPENAT:
4051 ret = io_openat_prep(req, sqe);
4052 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004053 case IORING_OP_CLOSE:
4054 ret = io_close_prep(req, sqe);
4055 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004056 case IORING_OP_FILES_UPDATE:
4057 ret = io_files_update_prep(req, sqe);
4058 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004059 case IORING_OP_STATX:
4060 ret = io_statx_prep(req, sqe);
4061 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004062 case IORING_OP_FADVISE:
4063 ret = io_fadvise_prep(req, sqe);
4064 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004065 case IORING_OP_MADVISE:
4066 ret = io_madvise_prep(req, sqe);
4067 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004068 case IORING_OP_OPENAT2:
4069 ret = io_openat2_prep(req, sqe);
4070 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004071 default:
Jens Axboee7815732019-12-17 19:45:06 -07004072 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4073 req->opcode);
4074 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004075 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004076 }
4077
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004078 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004079}
4080
Jens Axboe3529d8c2019-12-19 18:24:38 -07004081static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004082{
Jackie Liua197f662019-11-08 08:09:12 -07004083 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004084 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004085
Bob Liu9d858b22019-11-13 18:06:25 +08004086 /* Still need defer if there is pending req in defer list. */
4087 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004088 return 0;
4089
Jens Axboe3529d8c2019-12-19 18:24:38 -07004090 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004091 return -EAGAIN;
4092
Jens Axboe3529d8c2019-12-19 18:24:38 -07004093 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004094 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004095 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004096
Jens Axboede0617e2019-04-06 21:51:27 -06004097 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004098 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004099 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004100 return 0;
4101 }
4102
Jens Axboe915967f2019-11-21 09:01:20 -07004103 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004104 list_add_tail(&req->list, &ctx->defer_list);
4105 spin_unlock_irq(&ctx->completion_lock);
4106 return -EIOCBQUEUED;
4107}
4108
Jens Axboe3529d8c2019-12-19 18:24:38 -07004109static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4110 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004111{
Jackie Liua197f662019-11-08 08:09:12 -07004112 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004113 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004114
Jens Axboed625c6e2019-12-17 19:53:05 -07004115 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004116 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004117 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004118 break;
4119 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004120 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004121 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004122 if (sqe) {
4123 ret = io_read_prep(req, sqe, force_nonblock);
4124 if (ret < 0)
4125 break;
4126 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004127 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004128 break;
4129 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004130 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004131 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004132 if (sqe) {
4133 ret = io_write_prep(req, sqe, force_nonblock);
4134 if (ret < 0)
4135 break;
4136 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004137 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004138 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004139 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004140 if (sqe) {
4141 ret = io_prep_fsync(req, sqe);
4142 if (ret < 0)
4143 break;
4144 }
Jens Axboefc4df992019-12-10 14:38:45 -07004145 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004146 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004147 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004148 if (sqe) {
4149 ret = io_poll_add_prep(req, sqe);
4150 if (ret)
4151 break;
4152 }
Jens Axboefc4df992019-12-10 14:38:45 -07004153 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004154 break;
4155 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004156 if (sqe) {
4157 ret = io_poll_remove_prep(req, sqe);
4158 if (ret < 0)
4159 break;
4160 }
Jens Axboefc4df992019-12-10 14:38:45 -07004161 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004162 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004163 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004164 if (sqe) {
4165 ret = io_prep_sfr(req, sqe);
4166 if (ret < 0)
4167 break;
4168 }
Jens Axboefc4df992019-12-10 14:38:45 -07004169 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004170 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004171 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004172 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004173 if (sqe) {
4174 ret = io_sendmsg_prep(req, sqe);
4175 if (ret < 0)
4176 break;
4177 }
Jens Axboefddafac2020-01-04 20:19:44 -07004178 if (req->opcode == IORING_OP_SENDMSG)
4179 ret = io_sendmsg(req, nxt, force_nonblock);
4180 else
4181 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004182 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004183 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004184 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004185 if (sqe) {
4186 ret = io_recvmsg_prep(req, sqe);
4187 if (ret)
4188 break;
4189 }
Jens Axboefddafac2020-01-04 20:19:44 -07004190 if (req->opcode == IORING_OP_RECVMSG)
4191 ret = io_recvmsg(req, nxt, force_nonblock);
4192 else
4193 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004194 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004195 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004196 if (sqe) {
4197 ret = io_timeout_prep(req, sqe, false);
4198 if (ret)
4199 break;
4200 }
Jens Axboefc4df992019-12-10 14:38:45 -07004201 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004202 break;
Jens Axboe11365042019-10-16 09:08:32 -06004203 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004204 if (sqe) {
4205 ret = io_timeout_remove_prep(req, sqe);
4206 if (ret)
4207 break;
4208 }
Jens Axboefc4df992019-12-10 14:38:45 -07004209 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004210 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004211 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004212 if (sqe) {
4213 ret = io_accept_prep(req, sqe);
4214 if (ret)
4215 break;
4216 }
Jens Axboefc4df992019-12-10 14:38:45 -07004217 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004218 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004219 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004220 if (sqe) {
4221 ret = io_connect_prep(req, sqe);
4222 if (ret)
4223 break;
4224 }
Jens Axboefc4df992019-12-10 14:38:45 -07004225 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004226 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004227 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004228 if (sqe) {
4229 ret = io_async_cancel_prep(req, sqe);
4230 if (ret)
4231 break;
4232 }
Jens Axboefc4df992019-12-10 14:38:45 -07004233 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004234 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004235 case IORING_OP_FALLOCATE:
4236 if (sqe) {
4237 ret = io_fallocate_prep(req, sqe);
4238 if (ret)
4239 break;
4240 }
4241 ret = io_fallocate(req, nxt, force_nonblock);
4242 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004243 case IORING_OP_OPENAT:
4244 if (sqe) {
4245 ret = io_openat_prep(req, sqe);
4246 if (ret)
4247 break;
4248 }
4249 ret = io_openat(req, nxt, force_nonblock);
4250 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004251 case IORING_OP_CLOSE:
4252 if (sqe) {
4253 ret = io_close_prep(req, sqe);
4254 if (ret)
4255 break;
4256 }
4257 ret = io_close(req, nxt, force_nonblock);
4258 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004259 case IORING_OP_FILES_UPDATE:
4260 if (sqe) {
4261 ret = io_files_update_prep(req, sqe);
4262 if (ret)
4263 break;
4264 }
4265 ret = io_files_update(req, force_nonblock);
4266 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004267 case IORING_OP_STATX:
4268 if (sqe) {
4269 ret = io_statx_prep(req, sqe);
4270 if (ret)
4271 break;
4272 }
4273 ret = io_statx(req, nxt, force_nonblock);
4274 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004275 case IORING_OP_FADVISE:
4276 if (sqe) {
4277 ret = io_fadvise_prep(req, sqe);
4278 if (ret)
4279 break;
4280 }
4281 ret = io_fadvise(req, nxt, force_nonblock);
4282 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004283 case IORING_OP_MADVISE:
4284 if (sqe) {
4285 ret = io_madvise_prep(req, sqe);
4286 if (ret)
4287 break;
4288 }
4289 ret = io_madvise(req, nxt, force_nonblock);
4290 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004291 case IORING_OP_OPENAT2:
4292 if (sqe) {
4293 ret = io_openat2_prep(req, sqe);
4294 if (ret)
4295 break;
4296 }
4297 ret = io_openat2(req, nxt, force_nonblock);
4298 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004299 default:
4300 ret = -EINVAL;
4301 break;
4302 }
4303
Jens Axboedef596e2019-01-09 08:59:42 -07004304 if (ret)
4305 return ret;
4306
4307 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004308 const bool in_async = io_wq_current_is_worker();
4309
Jens Axboe9e645e112019-05-10 16:07:28 -06004310 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004311 return -EAGAIN;
4312
Jens Axboe11ba8202020-01-15 21:51:17 -07004313 /* workqueue context doesn't hold uring_lock, grab it now */
4314 if (in_async)
4315 mutex_lock(&ctx->uring_lock);
4316
Jens Axboedef596e2019-01-09 08:59:42 -07004317 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004318
4319 if (in_async)
4320 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004321 }
4322
4323 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004324}
4325
Jens Axboe561fb042019-10-24 07:25:42 -06004326static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004327{
Jens Axboe561fb042019-10-24 07:25:42 -06004328 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004329 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004330 struct io_kiocb *nxt = NULL;
4331 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004332
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004333 /* if NO_CANCEL is set, we must still run the work */
4334 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4335 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004336 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004337 }
Jens Axboe31b51512019-01-18 22:56:34 -07004338
Jens Axboe561fb042019-10-24 07:25:42 -06004339 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004340 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4341 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004342 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004343 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004344 /*
4345 * We can get EAGAIN for polled IO even though we're
4346 * forcing a sync submission from here, since we can't
4347 * wait for request slots on the block side.
4348 */
4349 if (ret != -EAGAIN)
4350 break;
4351 cond_resched();
4352 } while (1);
4353 }
Jens Axboe31b51512019-01-18 22:56:34 -07004354
Jens Axboe561fb042019-10-24 07:25:42 -06004355 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004356 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004357
Jens Axboe561fb042019-10-24 07:25:42 -06004358 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004359 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004360 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004361 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004362 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004363
Jens Axboe561fb042019-10-24 07:25:42 -06004364 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004365 if (!ret && nxt)
4366 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004367}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004368
Jens Axboe15b71ab2019-12-11 11:20:36 -07004369static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004370{
Jens Axboed3656342019-12-18 09:50:26 -07004371 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004372 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004373 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4374 return 0;
4375 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004376}
4377
Jens Axboe65e19f52019-10-26 07:20:21 -06004378static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4379 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004380{
Jens Axboe65e19f52019-10-26 07:20:21 -06004381 struct fixed_file_table *table;
4382
Jens Axboe05f3fb32019-12-09 11:22:50 -07004383 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4384 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004385}
4386
Jens Axboe3529d8c2019-12-19 18:24:38 -07004387static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4388 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004389{
Jackie Liua197f662019-11-08 08:09:12 -07004390 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004391 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004392 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004393
Jens Axboe3529d8c2019-12-19 18:24:38 -07004394 flags = READ_ONCE(sqe->flags);
4395 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004396
Jens Axboed3656342019-12-18 09:50:26 -07004397 if (!io_req_needs_file(req, fd))
4398 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004399
4400 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004401 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004402 (unsigned) fd >= ctx->nr_user_files))
4403 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004404 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004405 req->file = io_file_from_index(ctx, fd);
4406 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004407 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004408 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004409 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004410 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004411 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004412 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004413 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004414 req->file = io_file_get(state, fd);
4415 if (unlikely(!req->file))
4416 return -EBADF;
4417 }
4418
4419 return 0;
4420}
4421
Jackie Liua197f662019-11-08 08:09:12 -07004422static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004423{
Jens Axboefcb323c2019-10-24 12:39:47 -06004424 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004425 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004426
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004427 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004428 return -EBADF;
4429
Jens Axboefcb323c2019-10-24 12:39:47 -06004430 rcu_read_lock();
4431 spin_lock_irq(&ctx->inflight_lock);
4432 /*
4433 * We use the f_ops->flush() handler to ensure that we can flush
4434 * out work accessing these files if the fd is closed. Check if
4435 * the fd has changed since we started down this path, and disallow
4436 * this operation if it has.
4437 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004438 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004439 list_add(&req->inflight_entry, &ctx->inflight_list);
4440 req->flags |= REQ_F_INFLIGHT;
4441 req->work.files = current->files;
4442 ret = 0;
4443 }
4444 spin_unlock_irq(&ctx->inflight_lock);
4445 rcu_read_unlock();
4446
4447 return ret;
4448}
4449
Jens Axboe2665abf2019-11-05 12:40:47 -07004450static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4451{
Jens Axboead8a48a2019-11-15 08:49:11 -07004452 struct io_timeout_data *data = container_of(timer,
4453 struct io_timeout_data, timer);
4454 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004455 struct io_ring_ctx *ctx = req->ctx;
4456 struct io_kiocb *prev = NULL;
4457 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004458
4459 spin_lock_irqsave(&ctx->completion_lock, flags);
4460
4461 /*
4462 * We don't expect the list to be empty, that will only happen if we
4463 * race with the completion of the linked work.
4464 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004465 if (!list_empty(&req->link_list)) {
4466 prev = list_entry(req->link_list.prev, struct io_kiocb,
4467 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004468 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004469 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004470 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4471 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004472 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004473 }
4474
4475 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4476
4477 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004478 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004479 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4480 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004481 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004482 } else {
4483 io_cqring_add_event(req, -ETIME);
4484 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004485 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004486 return HRTIMER_NORESTART;
4487}
4488
Jens Axboead8a48a2019-11-15 08:49:11 -07004489static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004490{
Jens Axboe76a46e02019-11-10 23:34:16 -07004491 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004492
Jens Axboe76a46e02019-11-10 23:34:16 -07004493 /*
4494 * If the list is now empty, then our linked request finished before
4495 * we got a chance to setup the timer
4496 */
4497 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004498 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004499 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004500
Jens Axboead8a48a2019-11-15 08:49:11 -07004501 data->timer.function = io_link_timeout_fn;
4502 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4503 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004504 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004505 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004506
Jens Axboe2665abf2019-11-05 12:40:47 -07004507 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004508 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004509}
4510
Jens Axboead8a48a2019-11-15 08:49:11 -07004511static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004512{
4513 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004514
Jens Axboe2665abf2019-11-05 12:40:47 -07004515 if (!(req->flags & REQ_F_LINK))
4516 return NULL;
4517
Pavel Begunkov44932332019-12-05 16:16:35 +03004518 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4519 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004520 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004521 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004522
Jens Axboe76a46e02019-11-10 23:34:16 -07004523 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004524 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004525}
4526
Jens Axboe3529d8c2019-12-19 18:24:38 -07004527static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004528{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004529 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004530 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004531 int ret;
4532
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004533again:
4534 linked_timeout = io_prep_linked_timeout(req);
4535
Jens Axboe3529d8c2019-12-19 18:24:38 -07004536 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004537
4538 /*
4539 * We async punt it if the file wasn't marked NOWAIT, or if the file
4540 * doesn't support non-blocking read/write attempts
4541 */
4542 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4543 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004544punt:
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004545 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4546 ret = io_grab_files(req);
4547 if (ret)
4548 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004549 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004550
4551 /*
4552 * Queued up for async execution, worker will release
4553 * submit reference when the iocb is actually submitted.
4554 */
4555 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004556 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004557 }
Jens Axboee65ef562019-03-12 10:16:44 -06004558
Jens Axboefcb323c2019-10-24 12:39:47 -06004559err:
Jens Axboee65ef562019-03-12 10:16:44 -06004560 /* drop submission reference */
4561 io_put_req(req);
4562
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004563 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004564 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004565 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004566 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004567 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004568 }
4569
Jens Axboee65ef562019-03-12 10:16:44 -06004570 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004571 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004572 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004573 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004574 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004575 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004576done_req:
4577 if (nxt) {
4578 req = nxt;
4579 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004580
4581 if (req->flags & REQ_F_FORCE_ASYNC)
4582 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004583 goto again;
4584 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004585}
4586
Jens Axboe3529d8c2019-12-19 18:24:38 -07004587static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004588{
4589 int ret;
4590
Jens Axboe3529d8c2019-12-19 18:24:38 -07004591 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004592 if (ret) {
4593 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004594fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004595 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004596 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004597 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004598 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004599 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004600 ret = io_req_defer_prep(req, sqe);
4601 if (unlikely(ret < 0))
4602 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004603 /*
4604 * Never try inline submit of IOSQE_ASYNC is set, go straight
4605 * to async execution.
4606 */
4607 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4608 io_queue_async_work(req);
4609 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004610 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004611 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004612}
4613
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004614static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004615{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004616 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004617 io_cqring_add_event(req, -ECANCELED);
4618 io_double_put_req(req);
4619 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004620 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004621}
4622
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004623#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004624 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004625
Jens Axboe3529d8c2019-12-19 18:24:38 -07004626static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4627 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004628{
Jackie Liua197f662019-11-08 08:09:12 -07004629 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004630 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004631 int ret;
4632
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004633 sqe_flags = READ_ONCE(sqe->flags);
4634
Jens Axboe9e645e112019-05-10 16:07:28 -06004635 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004636 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004637 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004638 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004639 }
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004640 /* same numerical values with corresponding REQ_F_*, safe to copy */
4641 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4642 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004643
Jens Axboe3529d8c2019-12-19 18:24:38 -07004644 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004645 if (unlikely(ret)) {
4646err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004647 io_cqring_add_event(req, ret);
4648 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004649 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004650 }
4651
Jens Axboe9e645e112019-05-10 16:07:28 -06004652 /*
4653 * If we already have a head request, queue this one for async
4654 * submittal once the head completes. If we don't have a head but
4655 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4656 * submitted sync once the chain is complete. If none of those
4657 * conditions are true (normal request), then just queue it.
4658 */
4659 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004660 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004661
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004662 /*
4663 * Taking sequential execution of a link, draining both sides
4664 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4665 * requests in the link. So, it drains the head and the
4666 * next after the link request. The last one is done via
4667 * drain_next flag to persist the effect across calls.
4668 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004669 if (sqe_flags & IOSQE_IO_DRAIN) {
4670 head->flags |= REQ_F_IO_DRAIN;
4671 ctx->drain_next = 1;
4672 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004673 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004674 ret = -EAGAIN;
4675 goto err_req;
4676 }
4677
Jens Axboe3529d8c2019-12-19 18:24:38 -07004678 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004679 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004680 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004681 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004682 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004683 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004684 trace_io_uring_link(ctx, req, head);
4685 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004686
4687 /* last request of a link, enqueue the link */
4688 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4689 io_queue_link_head(head);
4690 *link = NULL;
4691 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004692 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004693 if (unlikely(ctx->drain_next)) {
4694 req->flags |= REQ_F_IO_DRAIN;
4695 req->ctx->drain_next = 0;
4696 }
4697 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4698 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004699 INIT_LIST_HEAD(&req->link_list);
4700 ret = io_req_defer_prep(req, sqe);
4701 if (ret)
4702 req->flags |= REQ_F_FAIL_LINK;
4703 *link = req;
4704 } else {
4705 io_queue_sqe(req, sqe);
4706 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004707 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004708
4709 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004710}
4711
Jens Axboe9a56a232019-01-09 09:06:50 -07004712/*
4713 * Batched submission is done, ensure local IO is flushed out.
4714 */
4715static void io_submit_state_end(struct io_submit_state *state)
4716{
4717 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004718 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004719 if (state->free_reqs)
4720 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4721 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004722}
4723
4724/*
4725 * Start submission side cache.
4726 */
4727static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004728 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004729{
4730 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004731 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004732 state->file = NULL;
4733 state->ios_left = max_ios;
4734}
4735
Jens Axboe2b188cc2019-01-07 10:46:33 -07004736static void io_commit_sqring(struct io_ring_ctx *ctx)
4737{
Hristo Venev75b28af2019-08-26 17:23:46 +00004738 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004739
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004740 /*
4741 * Ensure any loads from the SQEs are done at this point,
4742 * since once we write the new head, the application could
4743 * write new data to them.
4744 */
4745 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004746}
4747
4748/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004749 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004750 * that is mapped by userspace. This means that care needs to be taken to
4751 * ensure that reads are stable, as we cannot rely on userspace always
4752 * being a good citizen. If members of the sqe are validated and then later
4753 * used, it's important that those reads are done through READ_ONCE() to
4754 * prevent a re-load down the line.
4755 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004756static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4757 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004758{
Hristo Venev75b28af2019-08-26 17:23:46 +00004759 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004760 unsigned head;
4761
4762 /*
4763 * The cached sq head (or cq tail) serves two purposes:
4764 *
4765 * 1) allows us to batch the cost of updating the user visible
4766 * head updates.
4767 * 2) allows the kernel side to track the head on its own, even
4768 * though the application is the one updating it.
4769 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004770 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004771 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004772 /*
4773 * All io need record the previous position, if LINK vs DARIN,
4774 * it can be used to mark the position of the first IO in the
4775 * link list.
4776 */
4777 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004778 *sqe_ptr = &ctx->sq_sqes[head];
4779 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4780 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004781 ctx->cached_sq_head++;
4782 return true;
4783 }
4784
4785 /* drop invalid entries */
4786 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004787 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004788 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004789 return false;
4790}
4791
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004792static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004793 struct file *ring_file, int ring_fd,
4794 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004795{
4796 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004797 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004798 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004799 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004800
Jens Axboec4a2ed72019-11-21 21:01:26 -07004801 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004802 if (test_bit(0, &ctx->sq_check_overflow)) {
4803 if (!list_empty(&ctx->cq_overflow_list) &&
4804 !io_cqring_overflow_flush(ctx, false))
4805 return -EBUSY;
4806 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004807
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004808 /* make sure SQ entry isn't read before tail */
4809 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004810
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004811 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4812 return -EAGAIN;
4813
Jens Axboe6c271ce2019-01-10 11:22:30 -07004814 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004815 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004816 statep = &state;
4817 }
4818
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004819 ctx->ring_fd = ring_fd;
4820 ctx->ring_file = ring_file;
4821
Jens Axboe6c271ce2019-01-10 11:22:30 -07004822 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004823 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004824 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004825
Pavel Begunkov196be952019-11-07 01:41:06 +03004826 req = io_get_req(ctx, statep);
4827 if (unlikely(!req)) {
4828 if (!submitted)
4829 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004830 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004831 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004832 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004833 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004834 break;
4835 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004836
Jens Axboed3656342019-12-18 09:50:26 -07004837 /* will complete beyond this point, count as submitted */
4838 submitted++;
4839
4840 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4841 io_cqring_add_event(req, -EINVAL);
4842 io_double_put_req(req);
4843 break;
4844 }
4845
4846 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004847 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4848 if (!mm_fault) {
4849 use_mm(ctx->sqo_mm);
4850 *mm = ctx->sqo_mm;
4851 }
4852 }
4853
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004854 req->has_user = *mm != NULL;
4855 req->in_async = async;
4856 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07004857 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
4858 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004859 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004860 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004861 }
4862
Pavel Begunkov9466f432020-01-25 22:34:01 +03004863 if (unlikely(submitted != nr)) {
4864 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
4865
4866 percpu_ref_put_many(&ctx->refs, nr - ref_used);
4867 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004868 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004869 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004870 if (statep)
4871 io_submit_state_end(&state);
4872
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004873 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4874 io_commit_sqring(ctx);
4875
Jens Axboe6c271ce2019-01-10 11:22:30 -07004876 return submitted;
4877}
4878
4879static int io_sq_thread(void *data)
4880{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004881 struct io_ring_ctx *ctx = data;
4882 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004883 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004884 mm_segment_t old_fs;
4885 DEFINE_WAIT(wait);
4886 unsigned inflight;
4887 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004888 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004889
Jens Axboe206aefd2019-11-07 18:27:42 -07004890 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004891
Jens Axboe6c271ce2019-01-10 11:22:30 -07004892 old_fs = get_fs();
4893 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004894 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004895
Jens Axboec1edbf52019-11-10 16:56:04 -07004896 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004897 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004898 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004899
4900 if (inflight) {
4901 unsigned nr_events = 0;
4902
4903 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004904 /*
4905 * inflight is the count of the maximum possible
4906 * entries we submitted, but it can be smaller
4907 * if we dropped some of them. If we don't have
4908 * poll entries available, then we know that we
4909 * have nothing left to poll for. Reset the
4910 * inflight count to zero in that case.
4911 */
4912 mutex_lock(&ctx->uring_lock);
4913 if (!list_empty(&ctx->poll_list))
4914 __io_iopoll_check(ctx, &nr_events, 0);
4915 else
4916 inflight = 0;
4917 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004918 } else {
4919 /*
4920 * Normal IO, just pretend everything completed.
4921 * We don't have to poll completions for that.
4922 */
4923 nr_events = inflight;
4924 }
4925
4926 inflight -= nr_events;
4927 if (!inflight)
4928 timeout = jiffies + ctx->sq_thread_idle;
4929 }
4930
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004931 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004932
4933 /*
4934 * If submit got -EBUSY, flag us as needing the application
4935 * to enter the kernel to reap and flush events.
4936 */
4937 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004938 /*
4939 * We're polling. If we're within the defined idle
4940 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004941 * to sleep. The exception is if we got EBUSY doing
4942 * more IO, we should wait for the application to
4943 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004944 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004945 if (inflight ||
4946 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004947 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004948 continue;
4949 }
4950
4951 /*
4952 * Drop cur_mm before scheduling, we can't hold it for
4953 * long periods (or over schedule()). Do this before
4954 * adding ourselves to the waitqueue, as the unuse/drop
4955 * may sleep.
4956 */
4957 if (cur_mm) {
4958 unuse_mm(cur_mm);
4959 mmput(cur_mm);
4960 cur_mm = NULL;
4961 }
4962
4963 prepare_to_wait(&ctx->sqo_wait, &wait,
4964 TASK_INTERRUPTIBLE);
4965
4966 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004967 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004968 /* make sure to read SQ tail after writing flags */
4969 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004970
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004971 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004972 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004973 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004974 finish_wait(&ctx->sqo_wait, &wait);
4975 break;
4976 }
4977 if (signal_pending(current))
4978 flush_signals(current);
4979 schedule();
4980 finish_wait(&ctx->sqo_wait, &wait);
4981
Hristo Venev75b28af2019-08-26 17:23:46 +00004982 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004983 continue;
4984 }
4985 finish_wait(&ctx->sqo_wait, &wait);
4986
Hristo Venev75b28af2019-08-26 17:23:46 +00004987 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004988 }
4989
Jens Axboe8a4955f2019-12-09 14:52:35 -07004990 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004991 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004992 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004993 if (ret > 0)
4994 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004995 }
4996
4997 set_fs(old_fs);
4998 if (cur_mm) {
4999 unuse_mm(cur_mm);
5000 mmput(cur_mm);
5001 }
Jens Axboe181e4482019-11-25 08:52:30 -07005002 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005003
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005004 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005005
Jens Axboe6c271ce2019-01-10 11:22:30 -07005006 return 0;
5007}
5008
Jens Axboebda52162019-09-24 13:47:15 -06005009struct io_wait_queue {
5010 struct wait_queue_entry wq;
5011 struct io_ring_ctx *ctx;
5012 unsigned to_wait;
5013 unsigned nr_timeouts;
5014};
5015
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005016static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005017{
5018 struct io_ring_ctx *ctx = iowq->ctx;
5019
5020 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005021 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005022 * started waiting. For timeouts, we always want to return to userspace,
5023 * regardless of event count.
5024 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005025 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005026 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5027}
5028
5029static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5030 int wake_flags, void *key)
5031{
5032 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5033 wq);
5034
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005035 /* use noflush == true, as we can't safely rely on locking context */
5036 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005037 return -1;
5038
5039 return autoremove_wake_function(curr, mode, wake_flags, key);
5040}
5041
Jens Axboe2b188cc2019-01-07 10:46:33 -07005042/*
5043 * Wait until events become available, if we don't already have some. The
5044 * application must reap them itself, as they reside on the shared cq ring.
5045 */
5046static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5047 const sigset_t __user *sig, size_t sigsz)
5048{
Jens Axboebda52162019-09-24 13:47:15 -06005049 struct io_wait_queue iowq = {
5050 .wq = {
5051 .private = current,
5052 .func = io_wake_function,
5053 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5054 },
5055 .ctx = ctx,
5056 .to_wait = min_events,
5057 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005058 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005059 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005060
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005061 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005062 return 0;
5063
5064 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005065#ifdef CONFIG_COMPAT
5066 if (in_compat_syscall())
5067 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005068 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005069 else
5070#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005071 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005072
Jens Axboe2b188cc2019-01-07 10:46:33 -07005073 if (ret)
5074 return ret;
5075 }
5076
Jens Axboebda52162019-09-24 13:47:15 -06005077 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005078 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005079 do {
5080 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5081 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005082 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005083 break;
5084 schedule();
5085 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005086 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005087 break;
5088 }
5089 } while (1);
5090 finish_wait(&ctx->wait, &iowq.wq);
5091
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005092 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005093
Hristo Venev75b28af2019-08-26 17:23:46 +00005094 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005095}
5096
Jens Axboe6b063142019-01-10 22:13:58 -07005097static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5098{
5099#if defined(CONFIG_UNIX)
5100 if (ctx->ring_sock) {
5101 struct sock *sock = ctx->ring_sock->sk;
5102 struct sk_buff *skb;
5103
5104 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5105 kfree_skb(skb);
5106 }
5107#else
5108 int i;
5109
Jens Axboe65e19f52019-10-26 07:20:21 -06005110 for (i = 0; i < ctx->nr_user_files; i++) {
5111 struct file *file;
5112
5113 file = io_file_from_index(ctx, i);
5114 if (file)
5115 fput(file);
5116 }
Jens Axboe6b063142019-01-10 22:13:58 -07005117#endif
5118}
5119
Jens Axboe05f3fb32019-12-09 11:22:50 -07005120static void io_file_ref_kill(struct percpu_ref *ref)
5121{
5122 struct fixed_file_data *data;
5123
5124 data = container_of(ref, struct fixed_file_data, refs);
5125 complete(&data->done);
5126}
5127
Jens Axboe6b063142019-01-10 22:13:58 -07005128static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5129{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005130 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005131 unsigned nr_tables, i;
5132
Jens Axboe05f3fb32019-12-09 11:22:50 -07005133 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005134 return -ENXIO;
5135
Jens Axboe05f3fb32019-12-09 11:22:50 -07005136 /* protect against inflight atomic switch, which drops the ref */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005137 percpu_ref_get(&data->refs);
Jens Axboee46a7952020-01-17 11:15:34 -07005138 /* wait for existing switches */
5139 flush_work(&data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005140 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5141 wait_for_completion(&data->done);
5142 percpu_ref_put(&data->refs);
Jens Axboee46a7952020-01-17 11:15:34 -07005143 /* flush potential new switch */
5144 flush_work(&data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005145 percpu_ref_exit(&data->refs);
5146
Jens Axboe6b063142019-01-10 22:13:58 -07005147 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005148 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5149 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005150 kfree(data->table[i].files);
5151 kfree(data->table);
5152 kfree(data);
5153 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005154 ctx->nr_user_files = 0;
5155 return 0;
5156}
5157
Jens Axboe6c271ce2019-01-10 11:22:30 -07005158static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5159{
5160 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005161 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005162 /*
5163 * The park is a bit of a work-around, without it we get
5164 * warning spews on shutdown with SQPOLL set and affinity
5165 * set to a single CPU.
5166 */
Jens Axboe06058632019-04-13 09:26:03 -06005167 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005168 kthread_stop(ctx->sqo_thread);
5169 ctx->sqo_thread = NULL;
5170 }
5171}
5172
Jens Axboe6b063142019-01-10 22:13:58 -07005173static void io_finish_async(struct io_ring_ctx *ctx)
5174{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005175 io_sq_thread_stop(ctx);
5176
Jens Axboe561fb042019-10-24 07:25:42 -06005177 if (ctx->io_wq) {
5178 io_wq_destroy(ctx->io_wq);
5179 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005180 }
5181}
5182
5183#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005184/*
5185 * Ensure the UNIX gc is aware of our file set, so we are certain that
5186 * the io_uring can be safely unregistered on process exit, even if we have
5187 * loops in the file referencing.
5188 */
5189static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5190{
5191 struct sock *sk = ctx->ring_sock->sk;
5192 struct scm_fp_list *fpl;
5193 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005194 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005195
5196 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5197 unsigned long inflight = ctx->user->unix_inflight + nr;
5198
5199 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5200 return -EMFILE;
5201 }
5202
5203 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5204 if (!fpl)
5205 return -ENOMEM;
5206
5207 skb = alloc_skb(0, GFP_KERNEL);
5208 if (!skb) {
5209 kfree(fpl);
5210 return -ENOMEM;
5211 }
5212
5213 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005214
Jens Axboe08a45172019-10-03 08:11:03 -06005215 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005216 fpl->user = get_uid(ctx->user);
5217 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005218 struct file *file = io_file_from_index(ctx, i + offset);
5219
5220 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005221 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005222 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005223 unix_inflight(fpl->user, fpl->fp[nr_files]);
5224 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005225 }
5226
Jens Axboe08a45172019-10-03 08:11:03 -06005227 if (nr_files) {
5228 fpl->max = SCM_MAX_FD;
5229 fpl->count = nr_files;
5230 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005231 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005232 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5233 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005234
Jens Axboe08a45172019-10-03 08:11:03 -06005235 for (i = 0; i < nr_files; i++)
5236 fput(fpl->fp[i]);
5237 } else {
5238 kfree_skb(skb);
5239 kfree(fpl);
5240 }
Jens Axboe6b063142019-01-10 22:13:58 -07005241
5242 return 0;
5243}
5244
5245/*
5246 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5247 * causes regular reference counting to break down. We rely on the UNIX
5248 * garbage collection to take care of this problem for us.
5249 */
5250static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5251{
5252 unsigned left, total;
5253 int ret = 0;
5254
5255 total = 0;
5256 left = ctx->nr_user_files;
5257 while (left) {
5258 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005259
5260 ret = __io_sqe_files_scm(ctx, this_files, total);
5261 if (ret)
5262 break;
5263 left -= this_files;
5264 total += this_files;
5265 }
5266
5267 if (!ret)
5268 return 0;
5269
5270 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005271 struct file *file = io_file_from_index(ctx, total);
5272
5273 if (file)
5274 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005275 total++;
5276 }
5277
5278 return ret;
5279}
5280#else
5281static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5282{
5283 return 0;
5284}
5285#endif
5286
Jens Axboe65e19f52019-10-26 07:20:21 -06005287static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5288 unsigned nr_files)
5289{
5290 int i;
5291
5292 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005293 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005294 unsigned this_files;
5295
5296 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5297 table->files = kcalloc(this_files, sizeof(struct file *),
5298 GFP_KERNEL);
5299 if (!table->files)
5300 break;
5301 nr_files -= this_files;
5302 }
5303
5304 if (i == nr_tables)
5305 return 0;
5306
5307 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005308 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005309 kfree(table->files);
5310 }
5311 return 1;
5312}
5313
Jens Axboe05f3fb32019-12-09 11:22:50 -07005314static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005315{
5316#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005317 struct sock *sock = ctx->ring_sock->sk;
5318 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5319 struct sk_buff *skb;
5320 int i;
5321
5322 __skb_queue_head_init(&list);
5323
5324 /*
5325 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5326 * remove this entry and rearrange the file array.
5327 */
5328 skb = skb_dequeue(head);
5329 while (skb) {
5330 struct scm_fp_list *fp;
5331
5332 fp = UNIXCB(skb).fp;
5333 for (i = 0; i < fp->count; i++) {
5334 int left;
5335
5336 if (fp->fp[i] != file)
5337 continue;
5338
5339 unix_notinflight(fp->user, fp->fp[i]);
5340 left = fp->count - 1 - i;
5341 if (left) {
5342 memmove(&fp->fp[i], &fp->fp[i + 1],
5343 left * sizeof(struct file *));
5344 }
5345 fp->count--;
5346 if (!fp->count) {
5347 kfree_skb(skb);
5348 skb = NULL;
5349 } else {
5350 __skb_queue_tail(&list, skb);
5351 }
5352 fput(file);
5353 file = NULL;
5354 break;
5355 }
5356
5357 if (!file)
5358 break;
5359
5360 __skb_queue_tail(&list, skb);
5361
5362 skb = skb_dequeue(head);
5363 }
5364
5365 if (skb_peek(&list)) {
5366 spin_lock_irq(&head->lock);
5367 while ((skb = __skb_dequeue(&list)) != NULL)
5368 __skb_queue_tail(head, skb);
5369 spin_unlock_irq(&head->lock);
5370 }
5371#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005372 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005373#endif
5374}
5375
Jens Axboe05f3fb32019-12-09 11:22:50 -07005376struct io_file_put {
5377 struct llist_node llist;
5378 struct file *file;
5379 struct completion *done;
5380};
5381
5382static void io_ring_file_ref_switch(struct work_struct *work)
5383{
5384 struct io_file_put *pfile, *tmp;
5385 struct fixed_file_data *data;
5386 struct llist_node *node;
5387
5388 data = container_of(work, struct fixed_file_data, ref_work);
5389
5390 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5391 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5392 io_ring_file_put(data->ctx, pfile->file);
5393 if (pfile->done)
5394 complete(pfile->done);
5395 else
5396 kfree(pfile);
5397 }
5398 }
5399
5400 percpu_ref_get(&data->refs);
5401 percpu_ref_switch_to_percpu(&data->refs);
5402}
5403
5404static void io_file_data_ref_zero(struct percpu_ref *ref)
5405{
5406 struct fixed_file_data *data;
5407
5408 data = container_of(ref, struct fixed_file_data, refs);
5409
5410 /* we can't safely switch from inside this context, punt to wq */
5411 queue_work(system_wq, &data->ref_work);
5412}
5413
5414static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5415 unsigned nr_args)
5416{
5417 __s32 __user *fds = (__s32 __user *) arg;
5418 unsigned nr_tables;
5419 struct file *file;
5420 int fd, ret = 0;
5421 unsigned i;
5422
5423 if (ctx->file_data)
5424 return -EBUSY;
5425 if (!nr_args)
5426 return -EINVAL;
5427 if (nr_args > IORING_MAX_FIXED_FILES)
5428 return -EMFILE;
5429
5430 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5431 if (!ctx->file_data)
5432 return -ENOMEM;
5433 ctx->file_data->ctx = ctx;
5434 init_completion(&ctx->file_data->done);
5435
5436 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5437 ctx->file_data->table = kcalloc(nr_tables,
5438 sizeof(struct fixed_file_table),
5439 GFP_KERNEL);
5440 if (!ctx->file_data->table) {
5441 kfree(ctx->file_data);
5442 ctx->file_data = NULL;
5443 return -ENOMEM;
5444 }
5445
5446 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5447 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5448 kfree(ctx->file_data->table);
5449 kfree(ctx->file_data);
5450 ctx->file_data = NULL;
5451 return -ENOMEM;
5452 }
5453 ctx->file_data->put_llist.first = NULL;
5454 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5455
5456 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5457 percpu_ref_exit(&ctx->file_data->refs);
5458 kfree(ctx->file_data->table);
5459 kfree(ctx->file_data);
5460 ctx->file_data = NULL;
5461 return -ENOMEM;
5462 }
5463
5464 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5465 struct fixed_file_table *table;
5466 unsigned index;
5467
5468 ret = -EFAULT;
5469 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5470 break;
5471 /* allow sparse sets */
5472 if (fd == -1) {
5473 ret = 0;
5474 continue;
5475 }
5476
5477 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5478 index = i & IORING_FILE_TABLE_MASK;
5479 file = fget(fd);
5480
5481 ret = -EBADF;
5482 if (!file)
5483 break;
5484
5485 /*
5486 * Don't allow io_uring instances to be registered. If UNIX
5487 * isn't enabled, then this causes a reference cycle and this
5488 * instance can never get freed. If UNIX is enabled we'll
5489 * handle it just fine, but there's still no point in allowing
5490 * a ring fd as it doesn't support regular read/write anyway.
5491 */
5492 if (file->f_op == &io_uring_fops) {
5493 fput(file);
5494 break;
5495 }
5496 ret = 0;
5497 table->files[index] = file;
5498 }
5499
5500 if (ret) {
5501 for (i = 0; i < ctx->nr_user_files; i++) {
5502 file = io_file_from_index(ctx, i);
5503 if (file)
5504 fput(file);
5505 }
5506 for (i = 0; i < nr_tables; i++)
5507 kfree(ctx->file_data->table[i].files);
5508
5509 kfree(ctx->file_data->table);
5510 kfree(ctx->file_data);
5511 ctx->file_data = NULL;
5512 ctx->nr_user_files = 0;
5513 return ret;
5514 }
5515
5516 ret = io_sqe_files_scm(ctx);
5517 if (ret)
5518 io_sqe_files_unregister(ctx);
5519
5520 return ret;
5521}
5522
Jens Axboec3a31e62019-10-03 13:59:56 -06005523static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5524 int index)
5525{
5526#if defined(CONFIG_UNIX)
5527 struct sock *sock = ctx->ring_sock->sk;
5528 struct sk_buff_head *head = &sock->sk_receive_queue;
5529 struct sk_buff *skb;
5530
5531 /*
5532 * See if we can merge this file into an existing skb SCM_RIGHTS
5533 * file set. If there's no room, fall back to allocating a new skb
5534 * and filling it in.
5535 */
5536 spin_lock_irq(&head->lock);
5537 skb = skb_peek(head);
5538 if (skb) {
5539 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5540
5541 if (fpl->count < SCM_MAX_FD) {
5542 __skb_unlink(skb, head);
5543 spin_unlock_irq(&head->lock);
5544 fpl->fp[fpl->count] = get_file(file);
5545 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5546 fpl->count++;
5547 spin_lock_irq(&head->lock);
5548 __skb_queue_head(head, skb);
5549 } else {
5550 skb = NULL;
5551 }
5552 }
5553 spin_unlock_irq(&head->lock);
5554
5555 if (skb) {
5556 fput(file);
5557 return 0;
5558 }
5559
5560 return __io_sqe_files_scm(ctx, 1, index);
5561#else
5562 return 0;
5563#endif
5564}
5565
Jens Axboe05f3fb32019-12-09 11:22:50 -07005566static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005567{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005568 struct fixed_file_data *data;
5569
5570 data = container_of(ref, struct fixed_file_data, refs);
5571 clear_bit(FFD_F_ATOMIC, &data->state);
5572}
5573
5574static bool io_queue_file_removal(struct fixed_file_data *data,
5575 struct file *file)
5576{
5577 struct io_file_put *pfile, pfile_stack;
5578 DECLARE_COMPLETION_ONSTACK(done);
5579
5580 /*
5581 * If we fail allocating the struct we need for doing async reomval
5582 * of this file, just punt to sync and wait for it.
5583 */
5584 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5585 if (!pfile) {
5586 pfile = &pfile_stack;
5587 pfile->done = &done;
5588 }
5589
5590 pfile->file = file;
5591 llist_add(&pfile->llist, &data->put_llist);
5592
5593 if (pfile == &pfile_stack) {
5594 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5595 percpu_ref_put(&data->refs);
5596 percpu_ref_switch_to_atomic(&data->refs,
5597 io_atomic_switch);
5598 }
5599 wait_for_completion(&done);
5600 flush_work(&data->ref_work);
5601 return false;
5602 }
5603
5604 return true;
5605}
5606
5607static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5608 struct io_uring_files_update *up,
5609 unsigned nr_args)
5610{
5611 struct fixed_file_data *data = ctx->file_data;
5612 bool ref_switch = false;
5613 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005614 __s32 __user *fds;
5615 int fd, i, err;
5616 __u32 done;
5617
Jens Axboe05f3fb32019-12-09 11:22:50 -07005618 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005619 return -EOVERFLOW;
5620 if (done > ctx->nr_user_files)
5621 return -EINVAL;
5622
5623 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005624 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005625 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005626 struct fixed_file_table *table;
5627 unsigned index;
5628
Jens Axboec3a31e62019-10-03 13:59:56 -06005629 err = 0;
5630 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5631 err = -EFAULT;
5632 break;
5633 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005634 i = array_index_nospec(up->offset, ctx->nr_user_files);
5635 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005636 index = i & IORING_FILE_TABLE_MASK;
5637 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005638 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005639 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005640 if (io_queue_file_removal(data, file))
5641 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005642 }
5643 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005644 file = fget(fd);
5645 if (!file) {
5646 err = -EBADF;
5647 break;
5648 }
5649 /*
5650 * Don't allow io_uring instances to be registered. If
5651 * UNIX isn't enabled, then this causes a reference
5652 * cycle and this instance can never get freed. If UNIX
5653 * is enabled we'll handle it just fine, but there's
5654 * still no point in allowing a ring fd as it doesn't
5655 * support regular read/write anyway.
5656 */
5657 if (file->f_op == &io_uring_fops) {
5658 fput(file);
5659 err = -EBADF;
5660 break;
5661 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005662 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005663 err = io_sqe_file_register(ctx, file, i);
5664 if (err)
5665 break;
5666 }
5667 nr_args--;
5668 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005669 up->offset++;
5670 }
5671
5672 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5673 percpu_ref_put(&data->refs);
5674 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005675 }
5676
5677 return done ? done : err;
5678}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005679static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5680 unsigned nr_args)
5681{
5682 struct io_uring_files_update up;
5683
5684 if (!ctx->file_data)
5685 return -ENXIO;
5686 if (!nr_args)
5687 return -EINVAL;
5688 if (copy_from_user(&up, arg, sizeof(up)))
5689 return -EFAULT;
5690 if (up.resv)
5691 return -EINVAL;
5692
5693 return __io_sqe_files_update(ctx, &up, nr_args);
5694}
Jens Axboec3a31e62019-10-03 13:59:56 -06005695
Jens Axboe7d723062019-11-12 22:31:31 -07005696static void io_put_work(struct io_wq_work *work)
5697{
5698 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5699
5700 io_put_req(req);
5701}
5702
5703static void io_get_work(struct io_wq_work *work)
5704{
5705 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5706
5707 refcount_inc(&req->refs);
5708}
5709
Pavel Begunkov24369c22020-01-28 03:15:48 +03005710static int io_init_wq_offload(struct io_ring_ctx *ctx,
5711 struct io_uring_params *p)
5712{
5713 struct io_wq_data data;
5714 struct fd f;
5715 struct io_ring_ctx *ctx_attach;
5716 unsigned int concurrency;
5717 int ret = 0;
5718
5719 data.user = ctx->user;
5720 data.get_work = io_get_work;
5721 data.put_work = io_put_work;
5722
5723 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5724 /* Do QD, or 4 * CPUS, whatever is smallest */
5725 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5726
5727 ctx->io_wq = io_wq_create(concurrency, &data);
5728 if (IS_ERR(ctx->io_wq)) {
5729 ret = PTR_ERR(ctx->io_wq);
5730 ctx->io_wq = NULL;
5731 }
5732 return ret;
5733 }
5734
5735 f = fdget(p->wq_fd);
5736 if (!f.file)
5737 return -EBADF;
5738
5739 if (f.file->f_op != &io_uring_fops) {
5740 ret = -EINVAL;
5741 goto out_fput;
5742 }
5743
5744 ctx_attach = f.file->private_data;
5745 /* @io_wq is protected by holding the fd */
5746 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5747 ret = -EINVAL;
5748 goto out_fput;
5749 }
5750
5751 ctx->io_wq = ctx_attach->io_wq;
5752out_fput:
5753 fdput(f);
5754 return ret;
5755}
5756
Jens Axboe6c271ce2019-01-10 11:22:30 -07005757static int io_sq_offload_start(struct io_ring_ctx *ctx,
5758 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005759{
5760 int ret;
5761
Jens Axboe6c271ce2019-01-10 11:22:30 -07005762 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005763 mmgrab(current->mm);
5764 ctx->sqo_mm = current->mm;
5765
Jens Axboe6c271ce2019-01-10 11:22:30 -07005766 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005767 ret = -EPERM;
5768 if (!capable(CAP_SYS_ADMIN))
5769 goto err;
5770
Jens Axboe917257d2019-04-13 09:28:55 -06005771 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5772 if (!ctx->sq_thread_idle)
5773 ctx->sq_thread_idle = HZ;
5774
Jens Axboe6c271ce2019-01-10 11:22:30 -07005775 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005776 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005777
Jens Axboe917257d2019-04-13 09:28:55 -06005778 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005779 if (cpu >= nr_cpu_ids)
5780 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005781 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005782 goto err;
5783
Jens Axboe6c271ce2019-01-10 11:22:30 -07005784 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5785 ctx, cpu,
5786 "io_uring-sq");
5787 } else {
5788 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5789 "io_uring-sq");
5790 }
5791 if (IS_ERR(ctx->sqo_thread)) {
5792 ret = PTR_ERR(ctx->sqo_thread);
5793 ctx->sqo_thread = NULL;
5794 goto err;
5795 }
5796 wake_up_process(ctx->sqo_thread);
5797 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5798 /* Can't have SQ_AFF without SQPOLL */
5799 ret = -EINVAL;
5800 goto err;
5801 }
5802
Pavel Begunkov24369c22020-01-28 03:15:48 +03005803 ret = io_init_wq_offload(ctx, p);
5804 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005805 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005806
5807 return 0;
5808err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005809 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005810 mmdrop(ctx->sqo_mm);
5811 ctx->sqo_mm = NULL;
5812 return ret;
5813}
5814
5815static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5816{
5817 atomic_long_sub(nr_pages, &user->locked_vm);
5818}
5819
5820static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5821{
5822 unsigned long page_limit, cur_pages, new_pages;
5823
5824 /* Don't allow more pages than we can safely lock */
5825 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5826
5827 do {
5828 cur_pages = atomic_long_read(&user->locked_vm);
5829 new_pages = cur_pages + nr_pages;
5830 if (new_pages > page_limit)
5831 return -ENOMEM;
5832 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5833 new_pages) != cur_pages);
5834
5835 return 0;
5836}
5837
5838static void io_mem_free(void *ptr)
5839{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005840 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005841
Mark Rutland52e04ef2019-04-30 17:30:21 +01005842 if (!ptr)
5843 return;
5844
5845 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005846 if (put_page_testzero(page))
5847 free_compound_page(page);
5848}
5849
5850static void *io_mem_alloc(size_t size)
5851{
5852 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5853 __GFP_NORETRY;
5854
5855 return (void *) __get_free_pages(gfp_flags, get_order(size));
5856}
5857
Hristo Venev75b28af2019-08-26 17:23:46 +00005858static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5859 size_t *sq_offset)
5860{
5861 struct io_rings *rings;
5862 size_t off, sq_array_size;
5863
5864 off = struct_size(rings, cqes, cq_entries);
5865 if (off == SIZE_MAX)
5866 return SIZE_MAX;
5867
5868#ifdef CONFIG_SMP
5869 off = ALIGN(off, SMP_CACHE_BYTES);
5870 if (off == 0)
5871 return SIZE_MAX;
5872#endif
5873
5874 sq_array_size = array_size(sizeof(u32), sq_entries);
5875 if (sq_array_size == SIZE_MAX)
5876 return SIZE_MAX;
5877
5878 if (check_add_overflow(off, sq_array_size, &off))
5879 return SIZE_MAX;
5880
5881 if (sq_offset)
5882 *sq_offset = off;
5883
5884 return off;
5885}
5886
Jens Axboe2b188cc2019-01-07 10:46:33 -07005887static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5888{
Hristo Venev75b28af2019-08-26 17:23:46 +00005889 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005890
Hristo Venev75b28af2019-08-26 17:23:46 +00005891 pages = (size_t)1 << get_order(
5892 rings_size(sq_entries, cq_entries, NULL));
5893 pages += (size_t)1 << get_order(
5894 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005895
Hristo Venev75b28af2019-08-26 17:23:46 +00005896 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005897}
5898
Jens Axboeedafcce2019-01-09 09:16:05 -07005899static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5900{
5901 int i, j;
5902
5903 if (!ctx->user_bufs)
5904 return -ENXIO;
5905
5906 for (i = 0; i < ctx->nr_user_bufs; i++) {
5907 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5908
5909 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005910 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005911
5912 if (ctx->account_mem)
5913 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005914 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005915 imu->nr_bvecs = 0;
5916 }
5917
5918 kfree(ctx->user_bufs);
5919 ctx->user_bufs = NULL;
5920 ctx->nr_user_bufs = 0;
5921 return 0;
5922}
5923
5924static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5925 void __user *arg, unsigned index)
5926{
5927 struct iovec __user *src;
5928
5929#ifdef CONFIG_COMPAT
5930 if (ctx->compat) {
5931 struct compat_iovec __user *ciovs;
5932 struct compat_iovec ciov;
5933
5934 ciovs = (struct compat_iovec __user *) arg;
5935 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5936 return -EFAULT;
5937
Jens Axboed55e5f52019-12-11 16:12:15 -07005938 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005939 dst->iov_len = ciov.iov_len;
5940 return 0;
5941 }
5942#endif
5943 src = (struct iovec __user *) arg;
5944 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5945 return -EFAULT;
5946 return 0;
5947}
5948
5949static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5950 unsigned nr_args)
5951{
5952 struct vm_area_struct **vmas = NULL;
5953 struct page **pages = NULL;
5954 int i, j, got_pages = 0;
5955 int ret = -EINVAL;
5956
5957 if (ctx->user_bufs)
5958 return -EBUSY;
5959 if (!nr_args || nr_args > UIO_MAXIOV)
5960 return -EINVAL;
5961
5962 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5963 GFP_KERNEL);
5964 if (!ctx->user_bufs)
5965 return -ENOMEM;
5966
5967 for (i = 0; i < nr_args; i++) {
5968 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5969 unsigned long off, start, end, ubuf;
5970 int pret, nr_pages;
5971 struct iovec iov;
5972 size_t size;
5973
5974 ret = io_copy_iov(ctx, &iov, arg, i);
5975 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005976 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005977
5978 /*
5979 * Don't impose further limits on the size and buffer
5980 * constraints here, we'll -EINVAL later when IO is
5981 * submitted if they are wrong.
5982 */
5983 ret = -EFAULT;
5984 if (!iov.iov_base || !iov.iov_len)
5985 goto err;
5986
5987 /* arbitrary limit, but we need something */
5988 if (iov.iov_len > SZ_1G)
5989 goto err;
5990
5991 ubuf = (unsigned long) iov.iov_base;
5992 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5993 start = ubuf >> PAGE_SHIFT;
5994 nr_pages = end - start;
5995
5996 if (ctx->account_mem) {
5997 ret = io_account_mem(ctx->user, nr_pages);
5998 if (ret)
5999 goto err;
6000 }
6001
6002 ret = 0;
6003 if (!pages || nr_pages > got_pages) {
6004 kfree(vmas);
6005 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006006 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006007 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006008 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006009 sizeof(struct vm_area_struct *),
6010 GFP_KERNEL);
6011 if (!pages || !vmas) {
6012 ret = -ENOMEM;
6013 if (ctx->account_mem)
6014 io_unaccount_mem(ctx->user, nr_pages);
6015 goto err;
6016 }
6017 got_pages = nr_pages;
6018 }
6019
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006020 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006021 GFP_KERNEL);
6022 ret = -ENOMEM;
6023 if (!imu->bvec) {
6024 if (ctx->account_mem)
6025 io_unaccount_mem(ctx->user, nr_pages);
6026 goto err;
6027 }
6028
6029 ret = 0;
6030 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07006031 pret = get_user_pages(ubuf, nr_pages,
6032 FOLL_WRITE | FOLL_LONGTERM,
6033 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006034 if (pret == nr_pages) {
6035 /* don't support file backed memory */
6036 for (j = 0; j < nr_pages; j++) {
6037 struct vm_area_struct *vma = vmas[j];
6038
6039 if (vma->vm_file &&
6040 !is_file_hugepages(vma->vm_file)) {
6041 ret = -EOPNOTSUPP;
6042 break;
6043 }
6044 }
6045 } else {
6046 ret = pret < 0 ? pret : -EFAULT;
6047 }
6048 up_read(&current->mm->mmap_sem);
6049 if (ret) {
6050 /*
6051 * if we did partial map, or found file backed vmas,
6052 * release any pages we did get
6053 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006054 if (pret > 0)
6055 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006056 if (ctx->account_mem)
6057 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006058 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006059 goto err;
6060 }
6061
6062 off = ubuf & ~PAGE_MASK;
6063 size = iov.iov_len;
6064 for (j = 0; j < nr_pages; j++) {
6065 size_t vec_len;
6066
6067 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6068 imu->bvec[j].bv_page = pages[j];
6069 imu->bvec[j].bv_len = vec_len;
6070 imu->bvec[j].bv_offset = off;
6071 off = 0;
6072 size -= vec_len;
6073 }
6074 /* store original address for later verification */
6075 imu->ubuf = ubuf;
6076 imu->len = iov.iov_len;
6077 imu->nr_bvecs = nr_pages;
6078
6079 ctx->nr_user_bufs++;
6080 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006081 kvfree(pages);
6082 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006083 return 0;
6084err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006085 kvfree(pages);
6086 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006087 io_sqe_buffer_unregister(ctx);
6088 return ret;
6089}
6090
Jens Axboe9b402842019-04-11 11:45:41 -06006091static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6092{
6093 __s32 __user *fds = arg;
6094 int fd;
6095
6096 if (ctx->cq_ev_fd)
6097 return -EBUSY;
6098
6099 if (copy_from_user(&fd, fds, sizeof(*fds)))
6100 return -EFAULT;
6101
6102 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6103 if (IS_ERR(ctx->cq_ev_fd)) {
6104 int ret = PTR_ERR(ctx->cq_ev_fd);
6105 ctx->cq_ev_fd = NULL;
6106 return ret;
6107 }
6108
6109 return 0;
6110}
6111
6112static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6113{
6114 if (ctx->cq_ev_fd) {
6115 eventfd_ctx_put(ctx->cq_ev_fd);
6116 ctx->cq_ev_fd = NULL;
6117 return 0;
6118 }
6119
6120 return -ENXIO;
6121}
6122
Jens Axboe2b188cc2019-01-07 10:46:33 -07006123static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6124{
Jens Axboe6b063142019-01-10 22:13:58 -07006125 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006126 if (ctx->sqo_mm)
6127 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006128
6129 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006130 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006131 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006132 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006133
Jens Axboe2b188cc2019-01-07 10:46:33 -07006134#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006135 if (ctx->ring_sock) {
6136 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006137 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006138 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006139#endif
6140
Hristo Venev75b28af2019-08-26 17:23:46 +00006141 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006142 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006143
6144 percpu_ref_exit(&ctx->refs);
6145 if (ctx->account_mem)
6146 io_unaccount_mem(ctx->user,
6147 ring_pages(ctx->sq_entries, ctx->cq_entries));
6148 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006149 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006150 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006151 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006152 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006153 kfree(ctx);
6154}
6155
6156static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6157{
6158 struct io_ring_ctx *ctx = file->private_data;
6159 __poll_t mask = 0;
6160
6161 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006162 /*
6163 * synchronizes with barrier from wq_has_sleeper call in
6164 * io_commit_cqring
6165 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006166 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006167 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6168 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006169 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006170 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006171 mask |= EPOLLIN | EPOLLRDNORM;
6172
6173 return mask;
6174}
6175
6176static int io_uring_fasync(int fd, struct file *file, int on)
6177{
6178 struct io_ring_ctx *ctx = file->private_data;
6179
6180 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6181}
6182
Jens Axboe071698e2020-01-28 10:04:42 -07006183static int io_remove_personalities(int id, void *p, void *data)
6184{
6185 struct io_ring_ctx *ctx = data;
6186 const struct cred *cred;
6187
6188 cred = idr_remove(&ctx->personality_idr, id);
6189 if (cred)
6190 put_cred(cred);
6191 return 0;
6192}
6193
Jens Axboe2b188cc2019-01-07 10:46:33 -07006194static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6195{
6196 mutex_lock(&ctx->uring_lock);
6197 percpu_ref_kill(&ctx->refs);
6198 mutex_unlock(&ctx->uring_lock);
6199
Jens Axboe5262f562019-09-17 12:26:57 -06006200 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006201 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006202
6203 if (ctx->io_wq)
6204 io_wq_cancel_all(ctx->io_wq);
6205
Jens Axboedef596e2019-01-09 08:59:42 -07006206 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006207 /* if we failed setting up the ctx, we might not have any rings */
6208 if (ctx->rings)
6209 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006210 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006211 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006212 io_ring_ctx_free(ctx);
6213}
6214
6215static int io_uring_release(struct inode *inode, struct file *file)
6216{
6217 struct io_ring_ctx *ctx = file->private_data;
6218
6219 file->private_data = NULL;
6220 io_ring_ctx_wait_and_kill(ctx);
6221 return 0;
6222}
6223
Jens Axboefcb323c2019-10-24 12:39:47 -06006224static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6225 struct files_struct *files)
6226{
6227 struct io_kiocb *req;
6228 DEFINE_WAIT(wait);
6229
6230 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006231 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006232
6233 spin_lock_irq(&ctx->inflight_lock);
6234 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006235 if (req->work.files != files)
6236 continue;
6237 /* req is being completed, ignore */
6238 if (!refcount_inc_not_zero(&req->refs))
6239 continue;
6240 cancel_req = req;
6241 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006242 }
Jens Axboe768134d2019-11-10 20:30:53 -07006243 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006244 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006245 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006246 spin_unlock_irq(&ctx->inflight_lock);
6247
Jens Axboe768134d2019-11-10 20:30:53 -07006248 /* We need to keep going until we don't find a matching req */
6249 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006250 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006251
6252 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6253 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006254 schedule();
6255 }
Jens Axboe768134d2019-11-10 20:30:53 -07006256 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006257}
6258
6259static int io_uring_flush(struct file *file, void *data)
6260{
6261 struct io_ring_ctx *ctx = file->private_data;
6262
6263 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006264 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6265 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006266 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006267 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006268 return 0;
6269}
6270
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006271static void *io_uring_validate_mmap_request(struct file *file,
6272 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006273{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006274 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006275 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006276 struct page *page;
6277 void *ptr;
6278
6279 switch (offset) {
6280 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006281 case IORING_OFF_CQ_RING:
6282 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006283 break;
6284 case IORING_OFF_SQES:
6285 ptr = ctx->sq_sqes;
6286 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006287 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006288 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006289 }
6290
6291 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006292 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006293 return ERR_PTR(-EINVAL);
6294
6295 return ptr;
6296}
6297
6298#ifdef CONFIG_MMU
6299
6300static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6301{
6302 size_t sz = vma->vm_end - vma->vm_start;
6303 unsigned long pfn;
6304 void *ptr;
6305
6306 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6307 if (IS_ERR(ptr))
6308 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006309
6310 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6311 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6312}
6313
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006314#else /* !CONFIG_MMU */
6315
6316static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6317{
6318 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6319}
6320
6321static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6322{
6323 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6324}
6325
6326static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6327 unsigned long addr, unsigned long len,
6328 unsigned long pgoff, unsigned long flags)
6329{
6330 void *ptr;
6331
6332 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6333 if (IS_ERR(ptr))
6334 return PTR_ERR(ptr);
6335
6336 return (unsigned long) ptr;
6337}
6338
6339#endif /* !CONFIG_MMU */
6340
Jens Axboe2b188cc2019-01-07 10:46:33 -07006341SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6342 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6343 size_t, sigsz)
6344{
6345 struct io_ring_ctx *ctx;
6346 long ret = -EBADF;
6347 int submitted = 0;
6348 struct fd f;
6349
Jens Axboe6c271ce2019-01-10 11:22:30 -07006350 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006351 return -EINVAL;
6352
6353 f = fdget(fd);
6354 if (!f.file)
6355 return -EBADF;
6356
6357 ret = -EOPNOTSUPP;
6358 if (f.file->f_op != &io_uring_fops)
6359 goto out_fput;
6360
6361 ret = -ENXIO;
6362 ctx = f.file->private_data;
6363 if (!percpu_ref_tryget(&ctx->refs))
6364 goto out_fput;
6365
Jens Axboe6c271ce2019-01-10 11:22:30 -07006366 /*
6367 * For SQ polling, the thread will do all submissions and completions.
6368 * Just return the requested submit count, and wake the thread if
6369 * we were asked to.
6370 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006371 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006372 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006373 if (!list_empty_careful(&ctx->cq_overflow_list))
6374 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006375 if (flags & IORING_ENTER_SQ_WAKEUP)
6376 wake_up(&ctx->sqo_wait);
6377 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006378 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006379 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006380
Jens Axboe44d28272020-01-16 19:00:24 -07006381 if (current->mm != ctx->sqo_mm ||
6382 current_cred() != ctx->creds) {
6383 ret = -EPERM;
6384 goto out;
6385 }
6386
Jens Axboe2b188cc2019-01-07 10:46:33 -07006387 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006388 /* already have mm, so io_submit_sqes() won't try to grab it */
6389 cur_mm = ctx->sqo_mm;
6390 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6391 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006392 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006393
6394 if (submitted != to_submit)
6395 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006396 }
6397 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006398 unsigned nr_events = 0;
6399
Jens Axboe2b188cc2019-01-07 10:46:33 -07006400 min_complete = min(min_complete, ctx->cq_entries);
6401
Jens Axboedef596e2019-01-09 08:59:42 -07006402 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006403 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006404 } else {
6405 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6406 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006407 }
6408
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006409out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006410 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006411out_fput:
6412 fdput(f);
6413 return submitted ? submitted : ret;
6414}
6415
6416static const struct file_operations io_uring_fops = {
6417 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006418 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006419 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006420#ifndef CONFIG_MMU
6421 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6422 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6423#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006424 .poll = io_uring_poll,
6425 .fasync = io_uring_fasync,
6426};
6427
6428static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6429 struct io_uring_params *p)
6430{
Hristo Venev75b28af2019-08-26 17:23:46 +00006431 struct io_rings *rings;
6432 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006433
Hristo Venev75b28af2019-08-26 17:23:46 +00006434 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6435 if (size == SIZE_MAX)
6436 return -EOVERFLOW;
6437
6438 rings = io_mem_alloc(size);
6439 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006440 return -ENOMEM;
6441
Hristo Venev75b28af2019-08-26 17:23:46 +00006442 ctx->rings = rings;
6443 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6444 rings->sq_ring_mask = p->sq_entries - 1;
6445 rings->cq_ring_mask = p->cq_entries - 1;
6446 rings->sq_ring_entries = p->sq_entries;
6447 rings->cq_ring_entries = p->cq_entries;
6448 ctx->sq_mask = rings->sq_ring_mask;
6449 ctx->cq_mask = rings->cq_ring_mask;
6450 ctx->sq_entries = rings->sq_ring_entries;
6451 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006452
6453 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006454 if (size == SIZE_MAX) {
6455 io_mem_free(ctx->rings);
6456 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006457 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006458 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006459
6460 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006461 if (!ctx->sq_sqes) {
6462 io_mem_free(ctx->rings);
6463 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006464 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006465 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006466
Jens Axboe2b188cc2019-01-07 10:46:33 -07006467 return 0;
6468}
6469
6470/*
6471 * Allocate an anonymous fd, this is what constitutes the application
6472 * visible backing of an io_uring instance. The application mmaps this
6473 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6474 * we have to tie this fd to a socket for file garbage collection purposes.
6475 */
6476static int io_uring_get_fd(struct io_ring_ctx *ctx)
6477{
6478 struct file *file;
6479 int ret;
6480
6481#if defined(CONFIG_UNIX)
6482 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6483 &ctx->ring_sock);
6484 if (ret)
6485 return ret;
6486#endif
6487
6488 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6489 if (ret < 0)
6490 goto err;
6491
6492 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6493 O_RDWR | O_CLOEXEC);
6494 if (IS_ERR(file)) {
6495 put_unused_fd(ret);
6496 ret = PTR_ERR(file);
6497 goto err;
6498 }
6499
6500#if defined(CONFIG_UNIX)
6501 ctx->ring_sock->file = file;
6502#endif
6503 fd_install(ret, file);
6504 return ret;
6505err:
6506#if defined(CONFIG_UNIX)
6507 sock_release(ctx->ring_sock);
6508 ctx->ring_sock = NULL;
6509#endif
6510 return ret;
6511}
6512
6513static int io_uring_create(unsigned entries, struct io_uring_params *p)
6514{
6515 struct user_struct *user = NULL;
6516 struct io_ring_ctx *ctx;
6517 bool account_mem;
6518 int ret;
6519
Jens Axboe8110c1a2019-12-28 15:39:54 -07006520 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006521 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006522 if (entries > IORING_MAX_ENTRIES) {
6523 if (!(p->flags & IORING_SETUP_CLAMP))
6524 return -EINVAL;
6525 entries = IORING_MAX_ENTRIES;
6526 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006527
6528 /*
6529 * Use twice as many entries for the CQ ring. It's possible for the
6530 * application to drive a higher depth than the size of the SQ ring,
6531 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006532 * some flexibility in overcommitting a bit. If the application has
6533 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6534 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006535 */
6536 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006537 if (p->flags & IORING_SETUP_CQSIZE) {
6538 /*
6539 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6540 * to a power-of-two, if it isn't already. We do NOT impose
6541 * any cq vs sq ring sizing.
6542 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006543 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006544 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006545 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6546 if (!(p->flags & IORING_SETUP_CLAMP))
6547 return -EINVAL;
6548 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6549 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006550 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6551 } else {
6552 p->cq_entries = 2 * p->sq_entries;
6553 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006554
6555 user = get_uid(current_user());
6556 account_mem = !capable(CAP_IPC_LOCK);
6557
6558 if (account_mem) {
6559 ret = io_account_mem(user,
6560 ring_pages(p->sq_entries, p->cq_entries));
6561 if (ret) {
6562 free_uid(user);
6563 return ret;
6564 }
6565 }
6566
6567 ctx = io_ring_ctx_alloc(p);
6568 if (!ctx) {
6569 if (account_mem)
6570 io_unaccount_mem(user, ring_pages(p->sq_entries,
6571 p->cq_entries));
6572 free_uid(user);
6573 return -ENOMEM;
6574 }
6575 ctx->compat = in_compat_syscall();
6576 ctx->account_mem = account_mem;
6577 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006578 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006579
6580 ret = io_allocate_scq_urings(ctx, p);
6581 if (ret)
6582 goto err;
6583
Jens Axboe6c271ce2019-01-10 11:22:30 -07006584 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006585 if (ret)
6586 goto err;
6587
Jens Axboe2b188cc2019-01-07 10:46:33 -07006588 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006589 p->sq_off.head = offsetof(struct io_rings, sq.head);
6590 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6591 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6592 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6593 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6594 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6595 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006596
6597 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006598 p->cq_off.head = offsetof(struct io_rings, cq.head);
6599 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6600 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6601 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6602 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6603 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006604
Jens Axboe044c1ab2019-10-28 09:15:33 -06006605 /*
6606 * Install ring fd as the very last thing, so we don't risk someone
6607 * having closed it before we finish setup
6608 */
6609 ret = io_uring_get_fd(ctx);
6610 if (ret < 0)
6611 goto err;
6612
Jens Axboeda8c9692019-12-02 18:51:26 -07006613 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006614 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6615 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006616 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006617 return ret;
6618err:
6619 io_ring_ctx_wait_and_kill(ctx);
6620 return ret;
6621}
6622
6623/*
6624 * Sets up an aio uring context, and returns the fd. Applications asks for a
6625 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6626 * params structure passed in.
6627 */
6628static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6629{
6630 struct io_uring_params p;
6631 long ret;
6632 int i;
6633
6634 if (copy_from_user(&p, params, sizeof(p)))
6635 return -EFAULT;
6636 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6637 if (p.resv[i])
6638 return -EINVAL;
6639 }
6640
Jens Axboe6c271ce2019-01-10 11:22:30 -07006641 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006642 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006643 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006644 return -EINVAL;
6645
6646 ret = io_uring_create(entries, &p);
6647 if (ret < 0)
6648 return ret;
6649
6650 if (copy_to_user(params, &p, sizeof(p)))
6651 return -EFAULT;
6652
6653 return ret;
6654}
6655
6656SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6657 struct io_uring_params __user *, params)
6658{
6659 return io_uring_setup(entries, params);
6660}
6661
Jens Axboe66f4af92020-01-16 15:36:52 -07006662static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6663{
6664 struct io_uring_probe *p;
6665 size_t size;
6666 int i, ret;
6667
6668 size = struct_size(p, ops, nr_args);
6669 if (size == SIZE_MAX)
6670 return -EOVERFLOW;
6671 p = kzalloc(size, GFP_KERNEL);
6672 if (!p)
6673 return -ENOMEM;
6674
6675 ret = -EFAULT;
6676 if (copy_from_user(p, arg, size))
6677 goto out;
6678 ret = -EINVAL;
6679 if (memchr_inv(p, 0, size))
6680 goto out;
6681
6682 p->last_op = IORING_OP_LAST - 1;
6683 if (nr_args > IORING_OP_LAST)
6684 nr_args = IORING_OP_LAST;
6685
6686 for (i = 0; i < nr_args; i++) {
6687 p->ops[i].op = i;
6688 if (!io_op_defs[i].not_supported)
6689 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6690 }
6691 p->ops_len = i;
6692
6693 ret = 0;
6694 if (copy_to_user(arg, p, size))
6695 ret = -EFAULT;
6696out:
6697 kfree(p);
6698 return ret;
6699}
6700
Jens Axboe071698e2020-01-28 10:04:42 -07006701static int io_register_personality(struct io_ring_ctx *ctx)
6702{
6703 const struct cred *creds = get_current_cred();
6704 int id;
6705
6706 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
6707 USHRT_MAX, GFP_KERNEL);
6708 if (id < 0)
6709 put_cred(creds);
6710 return id;
6711}
6712
6713static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
6714{
6715 const struct cred *old_creds;
6716
6717 old_creds = idr_remove(&ctx->personality_idr, id);
6718 if (old_creds) {
6719 put_cred(old_creds);
6720 return 0;
6721 }
6722
6723 return -EINVAL;
6724}
6725
6726static bool io_register_op_must_quiesce(int op)
6727{
6728 switch (op) {
6729 case IORING_UNREGISTER_FILES:
6730 case IORING_REGISTER_FILES_UPDATE:
6731 case IORING_REGISTER_PROBE:
6732 case IORING_REGISTER_PERSONALITY:
6733 case IORING_UNREGISTER_PERSONALITY:
6734 return false;
6735 default:
6736 return true;
6737 }
6738}
6739
Jens Axboeedafcce2019-01-09 09:16:05 -07006740static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6741 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006742 __releases(ctx->uring_lock)
6743 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006744{
6745 int ret;
6746
Jens Axboe35fa71a2019-04-22 10:23:23 -06006747 /*
6748 * We're inside the ring mutex, if the ref is already dying, then
6749 * someone else killed the ctx or is already going through
6750 * io_uring_register().
6751 */
6752 if (percpu_ref_is_dying(&ctx->refs))
6753 return -ENXIO;
6754
Jens Axboe071698e2020-01-28 10:04:42 -07006755 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006756 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006757
Jens Axboe05f3fb32019-12-09 11:22:50 -07006758 /*
6759 * Drop uring mutex before waiting for references to exit. If
6760 * another thread is currently inside io_uring_enter() it might
6761 * need to grab the uring_lock to make progress. If we hold it
6762 * here across the drain wait, then we can deadlock. It's safe
6763 * to drop the mutex here, since no new references will come in
6764 * after we've killed the percpu ref.
6765 */
6766 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006767 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006768 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006769 if (ret) {
6770 percpu_ref_resurrect(&ctx->refs);
6771 ret = -EINTR;
6772 goto out;
6773 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006774 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006775
6776 switch (opcode) {
6777 case IORING_REGISTER_BUFFERS:
6778 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6779 break;
6780 case IORING_UNREGISTER_BUFFERS:
6781 ret = -EINVAL;
6782 if (arg || nr_args)
6783 break;
6784 ret = io_sqe_buffer_unregister(ctx);
6785 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006786 case IORING_REGISTER_FILES:
6787 ret = io_sqe_files_register(ctx, arg, nr_args);
6788 break;
6789 case IORING_UNREGISTER_FILES:
6790 ret = -EINVAL;
6791 if (arg || nr_args)
6792 break;
6793 ret = io_sqe_files_unregister(ctx);
6794 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006795 case IORING_REGISTER_FILES_UPDATE:
6796 ret = io_sqe_files_update(ctx, arg, nr_args);
6797 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006798 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006799 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006800 ret = -EINVAL;
6801 if (nr_args != 1)
6802 break;
6803 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006804 if (ret)
6805 break;
6806 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6807 ctx->eventfd_async = 1;
6808 else
6809 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006810 break;
6811 case IORING_UNREGISTER_EVENTFD:
6812 ret = -EINVAL;
6813 if (arg || nr_args)
6814 break;
6815 ret = io_eventfd_unregister(ctx);
6816 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07006817 case IORING_REGISTER_PROBE:
6818 ret = -EINVAL;
6819 if (!arg || nr_args > 256)
6820 break;
6821 ret = io_probe(ctx, arg, nr_args);
6822 break;
Jens Axboe071698e2020-01-28 10:04:42 -07006823 case IORING_REGISTER_PERSONALITY:
6824 ret = -EINVAL;
6825 if (arg || nr_args)
6826 break;
6827 ret = io_register_personality(ctx);
6828 break;
6829 case IORING_UNREGISTER_PERSONALITY:
6830 ret = -EINVAL;
6831 if (arg)
6832 break;
6833 ret = io_unregister_personality(ctx, nr_args);
6834 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006835 default:
6836 ret = -EINVAL;
6837 break;
6838 }
6839
Jens Axboe071698e2020-01-28 10:04:42 -07006840 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006841 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006842 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07006843out:
6844 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006845 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006846 return ret;
6847}
6848
6849SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6850 void __user *, arg, unsigned int, nr_args)
6851{
6852 struct io_ring_ctx *ctx;
6853 long ret = -EBADF;
6854 struct fd f;
6855
6856 f = fdget(fd);
6857 if (!f.file)
6858 return -EBADF;
6859
6860 ret = -EOPNOTSUPP;
6861 if (f.file->f_op != &io_uring_fops)
6862 goto out_fput;
6863
6864 ctx = f.file->private_data;
6865
6866 mutex_lock(&ctx->uring_lock);
6867 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6868 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006869 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6870 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006871out_fput:
6872 fdput(f);
6873 return ret;
6874}
6875
Jens Axboe2b188cc2019-01-07 10:46:33 -07006876static int __init io_uring_init(void)
6877{
Jens Axboed3656342019-12-18 09:50:26 -07006878 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006879 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6880 return 0;
6881};
6882__initcall(io_uring_init);