blob: 3affd96a98ba70da09f76fd8b0ad96f4ee1ac1f3 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030049#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070050
51#include <linux/sched/signal.h>
52#include <linux/fs.h>
53#include <linux/file.h>
54#include <linux/fdtable.h>
55#include <linux/mm.h>
56#include <linux/mman.h>
57#include <linux/mmu_context.h>
58#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070079
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020080#define CREATE_TRACE_POINTS
81#include <trace/events/io_uring.h>
82
Jens Axboe2b188cc2019-01-07 10:46:33 -070083#include <uapi/linux/io_uring.h>
84
85#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060086#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070087
Daniel Xu5277dea2019-09-14 14:23:45 -070088#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060089#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060090
91/*
92 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
93 */
94#define IORING_FILE_TABLE_SHIFT 9
95#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
96#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
97#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070098
99struct io_uring {
100 u32 head ____cacheline_aligned_in_smp;
101 u32 tail ____cacheline_aligned_in_smp;
102};
103
Stefan Bühler1e84b972019-04-24 23:54:16 +0200104/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000105 * This data is shared with the application through the mmap at offsets
106 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107 *
108 * The offsets to the member fields are published through struct
109 * io_sqring_offsets when calling io_uring_setup.
110 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000111struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112 /*
113 * Head and tail offsets into the ring; the offsets need to be
114 * masked to get valid indices.
115 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 * The kernel controls head of the sq ring and the tail of the cq ring,
117 * and the application controls tail of the sq ring and the head of the
118 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000120 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 * ring_entries - 1)
124 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 u32 sq_ring_mask, cq_ring_mask;
126 /* Ring sizes (constant, power of 2) */
127 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200128 /*
129 * Number of invalid entries dropped by the kernel due to
130 * invalid index stored in array
131 *
132 * Written by the kernel, shouldn't be modified by the
133 * application (i.e. get number of "new events" by comparing to
134 * cached value).
135 *
136 * After a new SQ head value was read by the application this
137 * counter includes all submissions that were dropped reaching
138 * the new SQ head (and possibly more).
139 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000140 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200141 /*
142 * Runtime flags
143 *
144 * Written by the kernel, shouldn't be modified by the
145 * application.
146 *
147 * The application needs a full memory barrier before checking
148 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
149 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000150 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 /*
152 * Number of completion events lost because the queue was full;
153 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800154 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200155 * the completion queue.
156 *
157 * Written by the kernel, shouldn't be modified by the
158 * application (i.e. get number of "new events" by comparing to
159 * cached value).
160 *
161 * As completion events come in out of order this counter is not
162 * ordered with any other data.
163 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000164 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 /*
166 * Ring buffer of completion events.
167 *
168 * The kernel writes completion events fresh every time they are
169 * produced, so the application is allowed to modify pending
170 * entries.
171 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000172 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700173};
174
Jens Axboeedafcce2019-01-09 09:16:05 -0700175struct io_mapped_ubuf {
176 u64 ubuf;
177 size_t len;
178 struct bio_vec *bvec;
179 unsigned int nr_bvecs;
180};
181
Jens Axboe65e19f52019-10-26 07:20:21 -0600182struct fixed_file_table {
183 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700184};
185
Jens Axboe05f3fb32019-12-09 11:22:50 -0700186struct fixed_file_data {
187 struct fixed_file_table *table;
188 struct io_ring_ctx *ctx;
189
190 struct percpu_ref refs;
191 struct llist_head put_llist;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700192 struct work_struct ref_work;
193 struct completion done;
194};
195
Jens Axboe2b188cc2019-01-07 10:46:33 -0700196struct io_ring_ctx {
197 struct {
198 struct percpu_ref refs;
199 } ____cacheline_aligned_in_smp;
200
201 struct {
202 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800203 unsigned int compat: 1;
204 unsigned int account_mem: 1;
205 unsigned int cq_overflow_flushed: 1;
206 unsigned int drain_next: 1;
207 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208
Hristo Venev75b28af2019-08-26 17:23:46 +0000209 /*
210 * Ring buffer of indices into array of io_uring_sqe, which is
211 * mmapped by the application using the IORING_OFF_SQES offset.
212 *
213 * This indirection could e.g. be used to assign fixed
214 * io_uring_sqe entries to operations and only submit them to
215 * the queue when needed.
216 *
217 * The kernel modifies neither the indices array nor the entries
218 * array.
219 */
220 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 unsigned cached_sq_head;
222 unsigned sq_entries;
223 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700224 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600225 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700226 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700227 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600228
229 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600230 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700231 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700232
Jens Axboefcb323c2019-10-24 12:39:47 -0600233 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700234 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700235 } ____cacheline_aligned_in_smp;
236
Hristo Venev75b28af2019-08-26 17:23:46 +0000237 struct io_rings *rings;
238
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600240 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241 struct task_struct *sqo_thread; /* if using sq thread polling */
242 struct mm_struct *sqo_mm;
243 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700244
Jens Axboe6b063142019-01-10 22:13:58 -0700245 /*
246 * If used, fixed file set. Writers must ensure that ->refs is dead,
247 * readers must ensure that ->refs is alive as long as the file* is
248 * used. Only updated through io_uring_register(2).
249 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700250 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700251 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300252 int ring_fd;
253 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700254
Jens Axboeedafcce2019-01-09 09:16:05 -0700255 /* if used, fixed mapped user buffers */
256 unsigned nr_user_bufs;
257 struct io_mapped_ubuf *user_bufs;
258
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259 struct user_struct *user;
260
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700261 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700262
Jens Axboe206aefd2019-11-07 18:27:42 -0700263 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
264 struct completion *completions;
265
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700266 /* if all else fails... */
267 struct io_kiocb *fallback_req;
268
Jens Axboe206aefd2019-11-07 18:27:42 -0700269#if defined(CONFIG_UNIX)
270 struct socket *ring_sock;
271#endif
272
Jens Axboe071698e2020-01-28 10:04:42 -0700273 struct idr personality_idr;
274
Jens Axboe206aefd2019-11-07 18:27:42 -0700275 struct {
276 unsigned cached_cq_tail;
277 unsigned cq_entries;
278 unsigned cq_mask;
279 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700280 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700281 struct wait_queue_head cq_wait;
282 struct fasync_struct *cq_fasync;
283 struct eventfd_ctx *cq_ev_fd;
284 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285
286 struct {
287 struct mutex uring_lock;
288 wait_queue_head_t wait;
289 } ____cacheline_aligned_in_smp;
290
291 struct {
292 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700293 struct llist_head poll_llist;
294
Jens Axboedef596e2019-01-09 08:59:42 -0700295 /*
296 * ->poll_list is protected by the ctx->uring_lock for
297 * io_uring instances that don't use IORING_SETUP_SQPOLL.
298 * For SQPOLL, only the single threaded io_sq_thread() will
299 * manipulate the list, hence no extra locking is needed there.
300 */
301 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700302 struct hlist_head *cancel_hash;
303 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700304 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600305
306 spinlock_t inflight_lock;
307 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700308 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700309};
310
Jens Axboe09bb8392019-03-13 12:39:28 -0600311/*
312 * First field must be the file pointer in all the
313 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
314 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700315struct io_poll_iocb {
316 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700317 union {
318 struct wait_queue_head *head;
319 u64 addr;
320 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700321 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600322 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700323 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700324 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700325};
326
Jens Axboeb5dba592019-12-11 14:02:38 -0700327struct io_close {
328 struct file *file;
329 struct file *put_file;
330 int fd;
331};
332
Jens Axboead8a48a2019-11-15 08:49:11 -0700333struct io_timeout_data {
334 struct io_kiocb *req;
335 struct hrtimer timer;
336 struct timespec64 ts;
337 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300338 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700339};
340
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700341struct io_accept {
342 struct file *file;
343 struct sockaddr __user *addr;
344 int __user *addr_len;
345 int flags;
Jens Axboe09952e32020-03-19 20:16:56 -0600346 unsigned long nofile;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700347};
348
349struct io_sync {
350 struct file *file;
351 loff_t len;
352 loff_t off;
353 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700354 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700355};
356
Jens Axboefbf23842019-12-17 18:45:56 -0700357struct io_cancel {
358 struct file *file;
359 u64 addr;
360};
361
Jens Axboeb29472e2019-12-17 18:50:29 -0700362struct io_timeout {
363 struct file *file;
364 u64 addr;
365 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700366 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700367};
368
Jens Axboe9adbd452019-12-20 08:45:55 -0700369struct io_rw {
370 /* NOTE: kiocb has the file as the first member, so don't do it here */
371 struct kiocb kiocb;
372 u64 addr;
373 u64 len;
374};
375
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700376struct io_connect {
377 struct file *file;
378 struct sockaddr __user *addr;
379 int addr_len;
380};
381
Jens Axboee47293f2019-12-20 08:58:21 -0700382struct io_sr_msg {
383 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700384 union {
385 struct user_msghdr __user *msg;
386 void __user *buf;
387 };
Jens Axboee47293f2019-12-20 08:58:21 -0700388 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700389 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700390};
391
Jens Axboe15b71ab2019-12-11 11:20:36 -0700392struct io_open {
393 struct file *file;
394 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700395 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700396 unsigned mask;
397 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700398 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700399 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700400 struct open_how how;
Jens Axboe4022e7a2020-03-19 19:23:18 -0600401 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700402};
403
Jens Axboe05f3fb32019-12-09 11:22:50 -0700404struct io_files_update {
405 struct file *file;
406 u64 arg;
407 u32 nr_args;
408 u32 offset;
409};
410
Jens Axboe4840e412019-12-25 22:03:45 -0700411struct io_fadvise {
412 struct file *file;
413 u64 offset;
414 u32 len;
415 u32 advice;
416};
417
Jens Axboec1ca7572019-12-25 22:18:28 -0700418struct io_madvise {
419 struct file *file;
420 u64 addr;
421 u32 len;
422 u32 advice;
423};
424
Jens Axboe3e4827b2020-01-08 15:18:09 -0700425struct io_epoll {
426 struct file *file;
427 int epfd;
428 int op;
429 int fd;
430 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700431};
432
Jens Axboef499a022019-12-02 16:28:46 -0700433struct io_async_connect {
434 struct sockaddr_storage address;
435};
436
Jens Axboe03b12302019-12-02 18:50:25 -0700437struct io_async_msghdr {
438 struct iovec fast_iov[UIO_FASTIOV];
439 struct iovec *iov;
440 struct sockaddr __user *uaddr;
441 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700442 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700443};
444
Jens Axboef67676d2019-12-02 11:03:47 -0700445struct io_async_rw {
446 struct iovec fast_iov[UIO_FASTIOV];
447 struct iovec *iov;
448 ssize_t nr_segs;
449 ssize_t size;
450};
451
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700452struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700453 union {
454 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700455 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700456 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700457 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700458 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700459};
460
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300461enum {
462 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
463 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
464 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
465 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
466 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
467
468 REQ_F_LINK_NEXT_BIT,
469 REQ_F_FAIL_LINK_BIT,
470 REQ_F_INFLIGHT_BIT,
471 REQ_F_CUR_POS_BIT,
472 REQ_F_NOWAIT_BIT,
473 REQ_F_IOPOLL_COMPLETED_BIT,
474 REQ_F_LINK_TIMEOUT_BIT,
475 REQ_F_TIMEOUT_BIT,
476 REQ_F_ISREG_BIT,
477 REQ_F_MUST_PUNT_BIT,
478 REQ_F_TIMEOUT_NOSEQ_BIT,
479 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300480 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700481 REQ_F_OVERFLOW_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300482};
483
484enum {
485 /* ctx owns file */
486 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
487 /* drain existing IO first */
488 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
489 /* linked sqes */
490 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
491 /* doesn't sever on completion < 0 */
492 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
493 /* IOSQE_ASYNC */
494 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
495
496 /* already grabbed next link */
497 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
498 /* fail rest of links */
499 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
500 /* on inflight list */
501 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
502 /* read/write uses file position */
503 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
504 /* must not punt to workers */
505 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
506 /* polled IO has completed */
507 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
508 /* has linked timeout */
509 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
510 /* timeout request */
511 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
512 /* regular file */
513 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
514 /* must be punted even for NONBLOCK */
515 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
516 /* no timeout sequence */
517 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
518 /* completion under lock */
519 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300520 /* needs cleanup */
521 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700522 /* in overflow list */
523 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300524};
525
Jens Axboe09bb8392019-03-13 12:39:28 -0600526/*
527 * NOTE! Each of the iocb union members has the file pointer
528 * as the first entry in their struct definition. So you can
529 * access the file pointer through any of the sub-structs,
530 * or directly as just 'ki_filp' in this struct.
531 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700532struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700533 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600534 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700535 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700536 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700537 struct io_accept accept;
538 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700539 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700540 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700541 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700542 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700543 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700544 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700545 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700546 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700547 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700548 struct io_epoll epoll;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700549 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700550
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700551 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300552 /*
553 * llist_node is only used for poll deferred completions
554 */
555 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300556 bool in_async;
557 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700558 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700559
560 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700561 union {
562 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700563 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700564 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600565 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700566 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700567 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700568 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600569 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600570 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700571
Jens Axboefcb323c2019-10-24 12:39:47 -0600572 struct list_head inflight_entry;
573
Jens Axboe561fb042019-10-24 07:25:42 -0600574 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700575};
576
577#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700578#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700579
Jens Axboe9a56a232019-01-09 09:06:50 -0700580struct io_submit_state {
581 struct blk_plug plug;
582
583 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700584 * io_kiocb alloc cache
585 */
586 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300587 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700588
589 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700590 * File reference cache
591 */
592 struct file *file;
593 unsigned int fd;
594 unsigned int has_refs;
595 unsigned int used_refs;
596 unsigned int ios_left;
597};
598
Jens Axboed3656342019-12-18 09:50:26 -0700599struct io_op_def {
600 /* needs req->io allocated for deferral/async */
601 unsigned async_ctx : 1;
602 /* needs current->mm setup, does mm access */
603 unsigned needs_mm : 1;
604 /* needs req->file assigned */
605 unsigned needs_file : 1;
606 /* needs req->file assigned IFF fd is >= 0 */
607 unsigned fd_non_neg : 1;
608 /* hash wq insertion if file is a regular file */
609 unsigned hash_reg_file : 1;
610 /* unbound wq insertion if file is a non-regular file */
611 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700612 /* opcode is not supported by this kernel */
613 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700614 /* needs file table */
615 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700616 /* needs ->fs */
617 unsigned needs_fs : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700618};
619
620static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300621 [IORING_OP_NOP] = {},
622 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700623 .async_ctx = 1,
624 .needs_mm = 1,
625 .needs_file = 1,
626 .unbound_nonreg_file = 1,
627 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300628 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700629 .async_ctx = 1,
630 .needs_mm = 1,
631 .needs_file = 1,
632 .hash_reg_file = 1,
633 .unbound_nonreg_file = 1,
634 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300635 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700636 .needs_file = 1,
637 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300638 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700639 .needs_file = 1,
640 .unbound_nonreg_file = 1,
641 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300642 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700643 .needs_file = 1,
644 .hash_reg_file = 1,
645 .unbound_nonreg_file = 1,
646 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300647 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700648 .needs_file = 1,
649 .unbound_nonreg_file = 1,
650 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300651 [IORING_OP_POLL_REMOVE] = {},
652 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700653 .needs_file = 1,
654 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300655 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700656 .async_ctx = 1,
657 .needs_mm = 1,
658 .needs_file = 1,
659 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700660 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700661 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300662 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700663 .async_ctx = 1,
664 .needs_mm = 1,
665 .needs_file = 1,
666 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700667 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700668 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300669 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700670 .async_ctx = 1,
671 .needs_mm = 1,
672 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300673 [IORING_OP_TIMEOUT_REMOVE] = {},
674 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700675 .needs_mm = 1,
676 .needs_file = 1,
677 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700678 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700679 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300680 [IORING_OP_ASYNC_CANCEL] = {},
681 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700682 .async_ctx = 1,
683 .needs_mm = 1,
684 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300685 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700686 .async_ctx = 1,
687 .needs_mm = 1,
688 .needs_file = 1,
689 .unbound_nonreg_file = 1,
690 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300691 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700692 .needs_file = 1,
693 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300694 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700695 .needs_file = 1,
696 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700697 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700698 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700699 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300700 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700701 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700702 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700703 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300704 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700705 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700706 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700707 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300708 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700709 .needs_mm = 1,
710 .needs_file = 1,
711 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700712 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700713 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300714 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700715 .needs_mm = 1,
716 .needs_file = 1,
717 .unbound_nonreg_file = 1,
718 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300719 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700720 .needs_mm = 1,
721 .needs_file = 1,
722 .unbound_nonreg_file = 1,
723 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300724 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700725 .needs_file = 1,
726 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300727 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700728 .needs_mm = 1,
729 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300730 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700731 .needs_mm = 1,
732 .needs_file = 1,
733 .unbound_nonreg_file = 1,
734 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300735 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700736 .needs_mm = 1,
737 .needs_file = 1,
738 .unbound_nonreg_file = 1,
739 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300740 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700741 .needs_file = 1,
742 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700743 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700744 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700745 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700746 [IORING_OP_EPOLL_CTL] = {
747 .unbound_nonreg_file = 1,
748 .file_table = 1,
749 },
Jens Axboed3656342019-12-18 09:50:26 -0700750};
751
Jens Axboe561fb042019-10-24 07:25:42 -0600752static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700753static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800754static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700755static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700756static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
757static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700758static int __io_sqe_files_update(struct io_ring_ctx *ctx,
759 struct io_uring_files_update *ip,
760 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700761static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700762static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300763static void io_cleanup_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600764
Jens Axboe2b188cc2019-01-07 10:46:33 -0700765static struct kmem_cache *req_cachep;
766
767static const struct file_operations io_uring_fops;
768
769struct sock *io_uring_get_socket(struct file *file)
770{
771#if defined(CONFIG_UNIX)
772 if (file->f_op == &io_uring_fops) {
773 struct io_ring_ctx *ctx = file->private_data;
774
775 return ctx->ring_sock->sk;
776 }
777#endif
778 return NULL;
779}
780EXPORT_SYMBOL(io_uring_get_socket);
781
782static void io_ring_ctx_ref_free(struct percpu_ref *ref)
783{
784 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
785
Jens Axboe206aefd2019-11-07 18:27:42 -0700786 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700787}
788
789static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
790{
791 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700792 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700793
794 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
795 if (!ctx)
796 return NULL;
797
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700798 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
799 if (!ctx->fallback_req)
800 goto err;
801
Jens Axboe206aefd2019-11-07 18:27:42 -0700802 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
803 if (!ctx->completions)
804 goto err;
805
Jens Axboe78076bb2019-12-04 19:56:40 -0700806 /*
807 * Use 5 bits less than the max cq entries, that should give us around
808 * 32 entries per hash list if totally full and uniformly spread.
809 */
810 hash_bits = ilog2(p->cq_entries);
811 hash_bits -= 5;
812 if (hash_bits <= 0)
813 hash_bits = 1;
814 ctx->cancel_hash_bits = hash_bits;
815 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
816 GFP_KERNEL);
817 if (!ctx->cancel_hash)
818 goto err;
819 __hash_init(ctx->cancel_hash, 1U << hash_bits);
820
Roman Gushchin21482892019-05-07 10:01:48 -0700821 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700822 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
823 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700824
825 ctx->flags = p->flags;
826 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700827 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700828 init_completion(&ctx->completions[0]);
829 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700830 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700831 mutex_init(&ctx->uring_lock);
832 init_waitqueue_head(&ctx->wait);
833 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700834 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700835 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600836 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600837 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600838 init_waitqueue_head(&ctx->inflight_wait);
839 spin_lock_init(&ctx->inflight_lock);
840 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700841 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700842err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700843 if (ctx->fallback_req)
844 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700845 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700846 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700847 kfree(ctx);
848 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700849}
850
Bob Liu9d858b22019-11-13 18:06:25 +0800851static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600852{
Jackie Liua197f662019-11-08 08:09:12 -0700853 struct io_ring_ctx *ctx = req->ctx;
854
Jens Axboe498ccd92019-10-25 10:04:25 -0600855 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
856 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600857}
858
Bob Liu9d858b22019-11-13 18:06:25 +0800859static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600860{
Pavel Begunkov87987892020-01-18 01:22:30 +0300861 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800862 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600863
Bob Liu9d858b22019-11-13 18:06:25 +0800864 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600865}
866
867static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600868{
869 struct io_kiocb *req;
870
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600871 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800872 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600873 list_del_init(&req->list);
874 return req;
875 }
876
877 return NULL;
878}
879
Jens Axboe5262f562019-09-17 12:26:57 -0600880static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
881{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600882 struct io_kiocb *req;
883
884 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700885 if (req) {
886 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
887 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800888 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700889 list_del_init(&req->list);
890 return req;
891 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600892 }
893
894 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600895}
896
Jens Axboede0617e2019-04-06 21:51:27 -0600897static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700898{
Hristo Venev75b28af2019-08-26 17:23:46 +0000899 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700900
Pavel Begunkov07910152020-01-17 03:52:46 +0300901 /* order cqe stores with ring update */
902 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700903
Pavel Begunkov07910152020-01-17 03:52:46 +0300904 if (wq_has_sleeper(&ctx->cq_wait)) {
905 wake_up_interruptible(&ctx->cq_wait);
906 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700907 }
908}
909
Jens Axboecccf0ee2020-01-27 16:34:48 -0700910static inline void io_req_work_grab_env(struct io_kiocb *req,
911 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600912{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700913 if (!req->work.mm && def->needs_mm) {
914 mmgrab(current->mm);
915 req->work.mm = current->mm;
916 }
917 if (!req->work.creds)
918 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700919 if (!req->work.fs && def->needs_fs) {
920 spin_lock(&current->fs->lock);
921 if (!current->fs->in_exec) {
922 req->work.fs = current->fs;
923 req->work.fs->users++;
924 } else {
925 req->work.flags |= IO_WQ_WORK_CANCEL;
926 }
927 spin_unlock(&current->fs->lock);
928 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700929 if (!req->work.task_pid)
930 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700931}
932
933static inline void io_req_work_drop_env(struct io_kiocb *req)
934{
935 if (req->work.mm) {
936 mmdrop(req->work.mm);
937 req->work.mm = NULL;
938 }
939 if (req->work.creds) {
940 put_cred(req->work.creds);
941 req->work.creds = NULL;
942 }
Jens Axboeff002b32020-02-07 16:05:21 -0700943 if (req->work.fs) {
944 struct fs_struct *fs = req->work.fs;
945
946 spin_lock(&req->work.fs->lock);
947 if (--fs->users)
948 fs = NULL;
949 spin_unlock(&req->work.fs->lock);
950 if (fs)
951 free_fs_struct(fs);
952 }
Jens Axboe561fb042019-10-24 07:25:42 -0600953}
954
Jens Axboe94ae5e72019-11-14 19:39:52 -0700955static inline bool io_prep_async_work(struct io_kiocb *req,
956 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600957{
Jens Axboed3656342019-12-18 09:50:26 -0700958 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600959 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600960
Jens Axboed3656342019-12-18 09:50:26 -0700961 if (req->flags & REQ_F_ISREG) {
962 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700963 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700964 } else {
965 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700966 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600967 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700968
969 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600970
Jens Axboe94ae5e72019-11-14 19:39:52 -0700971 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600972 return do_hashed;
973}
974
Jackie Liua197f662019-11-08 08:09:12 -0700975static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600976{
Jackie Liua197f662019-11-08 08:09:12 -0700977 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700978 struct io_kiocb *link;
979 bool do_hashed;
980
981 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600982
983 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
984 req->flags);
985 if (!do_hashed) {
986 io_wq_enqueue(ctx->io_wq, &req->work);
987 } else {
988 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
989 file_inode(req->file));
990 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700991
992 if (link)
993 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600994}
995
Jens Axboe5262f562019-09-17 12:26:57 -0600996static void io_kill_timeout(struct io_kiocb *req)
997{
998 int ret;
999
Jens Axboe2d283902019-12-04 11:08:05 -07001000 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001001 if (ret != -1) {
1002 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001003 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001004 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001005 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001006 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001007 }
1008}
1009
1010static void io_kill_timeouts(struct io_ring_ctx *ctx)
1011{
1012 struct io_kiocb *req, *tmp;
1013
1014 spin_lock_irq(&ctx->completion_lock);
1015 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1016 io_kill_timeout(req);
1017 spin_unlock_irq(&ctx->completion_lock);
1018}
1019
Jens Axboede0617e2019-04-06 21:51:27 -06001020static void io_commit_cqring(struct io_ring_ctx *ctx)
1021{
1022 struct io_kiocb *req;
1023
Jens Axboe5262f562019-09-17 12:26:57 -06001024 while ((req = io_get_timeout_req(ctx)) != NULL)
1025 io_kill_timeout(req);
1026
Jens Axboede0617e2019-04-06 21:51:27 -06001027 __io_commit_cqring(ctx);
1028
Pavel Begunkov87987892020-01-18 01:22:30 +03001029 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001030 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001031}
1032
Jens Axboe2b188cc2019-01-07 10:46:33 -07001033static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1034{
Hristo Venev75b28af2019-08-26 17:23:46 +00001035 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001036 unsigned tail;
1037
1038 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001039 /*
1040 * writes to the cq entry need to come after reading head; the
1041 * control dependency is enough as we're using WRITE_ONCE to
1042 * fill the cq entry
1043 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001044 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001045 return NULL;
1046
1047 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001048 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049}
1050
Jens Axboef2842ab2020-01-08 11:04:00 -07001051static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1052{
Jens Axboef0b493e2020-02-01 21:30:11 -07001053 if (!ctx->cq_ev_fd)
1054 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001055 if (!ctx->eventfd_async)
1056 return true;
1057 return io_wq_current_is_worker() || in_interrupt();
1058}
1059
Jens Axboef0b493e2020-02-01 21:30:11 -07001060static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001061{
1062 if (waitqueue_active(&ctx->wait))
1063 wake_up(&ctx->wait);
1064 if (waitqueue_active(&ctx->sqo_wait))
1065 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001066 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001067 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001068}
1069
Jens Axboef0b493e2020-02-01 21:30:11 -07001070static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1071{
1072 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1073}
1074
Jens Axboec4a2ed72019-11-21 21:01:26 -07001075/* Returns true if there are no backlogged entries after the flush */
1076static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001077{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001078 struct io_rings *rings = ctx->rings;
1079 struct io_uring_cqe *cqe;
1080 struct io_kiocb *req;
1081 unsigned long flags;
1082 LIST_HEAD(list);
1083
1084 if (!force) {
1085 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001086 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001087 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1088 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001089 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001090 }
1091
1092 spin_lock_irqsave(&ctx->completion_lock, flags);
1093
1094 /* if force is set, the ring is going away. always drop after that */
1095 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001096 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001097
Jens Axboec4a2ed72019-11-21 21:01:26 -07001098 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001099 while (!list_empty(&ctx->cq_overflow_list)) {
1100 cqe = io_get_cqring(ctx);
1101 if (!cqe && !force)
1102 break;
1103
1104 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1105 list);
1106 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001107 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001108 if (cqe) {
1109 WRITE_ONCE(cqe->user_data, req->user_data);
1110 WRITE_ONCE(cqe->res, req->result);
1111 WRITE_ONCE(cqe->flags, 0);
1112 } else {
1113 WRITE_ONCE(ctx->rings->cq_overflow,
1114 atomic_inc_return(&ctx->cached_cq_overflow));
1115 }
1116 }
1117
1118 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001119 if (cqe) {
1120 clear_bit(0, &ctx->sq_check_overflow);
1121 clear_bit(0, &ctx->cq_check_overflow);
1122 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001123 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1124 io_cqring_ev_posted(ctx);
1125
1126 while (!list_empty(&list)) {
1127 req = list_first_entry(&list, struct io_kiocb, list);
1128 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001129 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001130 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001131
1132 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001133}
1134
Jens Axboe78e19bb2019-11-06 15:21:34 -07001135static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001136{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001137 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001138 struct io_uring_cqe *cqe;
1139
Jens Axboe78e19bb2019-11-06 15:21:34 -07001140 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001141
Jens Axboe2b188cc2019-01-07 10:46:33 -07001142 /*
1143 * If we can't get a cq entry, userspace overflowed the
1144 * submission (by quite a lot). Increment the overflow count in
1145 * the ring.
1146 */
1147 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001148 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001149 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001150 WRITE_ONCE(cqe->res, res);
1151 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001152 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001153 WRITE_ONCE(ctx->rings->cq_overflow,
1154 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001155 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001156 if (list_empty(&ctx->cq_overflow_list)) {
1157 set_bit(0, &ctx->sq_check_overflow);
1158 set_bit(0, &ctx->cq_check_overflow);
1159 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001160 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001161 refcount_inc(&req->refs);
1162 req->result = res;
1163 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001164 }
1165}
1166
Jens Axboe78e19bb2019-11-06 15:21:34 -07001167static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001168{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001169 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001170 unsigned long flags;
1171
1172 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001173 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001174 io_commit_cqring(ctx);
1175 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1176
Jens Axboe8c838782019-03-12 15:48:16 -06001177 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001178}
1179
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001180static inline bool io_is_fallback_req(struct io_kiocb *req)
1181{
1182 return req == (struct io_kiocb *)
1183 ((unsigned long) req->ctx->fallback_req & ~1UL);
1184}
1185
1186static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1187{
1188 struct io_kiocb *req;
1189
1190 req = ctx->fallback_req;
1191 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1192 return req;
1193
1194 return NULL;
1195}
1196
Jens Axboe2579f912019-01-09 09:10:43 -07001197static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1198 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001199{
Jens Axboefd6fab22019-03-14 16:30:06 -06001200 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001201 struct io_kiocb *req;
1202
Jens Axboe2579f912019-01-09 09:10:43 -07001203 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001204 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001205 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001206 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001207 } else if (!state->free_reqs) {
1208 size_t sz;
1209 int ret;
1210
1211 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001212 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1213
1214 /*
1215 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1216 * retry single alloc to be on the safe side.
1217 */
1218 if (unlikely(ret <= 0)) {
1219 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1220 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001221 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001222 ret = 1;
1223 }
Jens Axboe2579f912019-01-09 09:10:43 -07001224 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001225 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001226 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001227 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001228 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001229 }
1230
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001231got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001232 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001233 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001234 req->ctx = ctx;
1235 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001236 /* one is dropped after submission, the other at completion */
1237 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001238 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001239 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001240 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001241fallback:
1242 req = io_get_fallback_req(ctx);
1243 if (req)
1244 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001245 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001246 return NULL;
1247}
1248
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001249static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001250{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001251 if (likely(!io_is_fallback_req(req)))
1252 kmem_cache_free(req_cachep, req);
1253 else
1254 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1255}
1256
Jens Axboec6ca97b302019-12-28 12:11:08 -07001257static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001258{
Jens Axboefcb323c2019-10-24 12:39:47 -06001259 struct io_ring_ctx *ctx = req->ctx;
1260
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001261 if (req->flags & REQ_F_NEED_CLEANUP)
1262 io_cleanup_req(req);
1263
YueHaibing96fd84d2020-01-07 22:22:44 +08001264 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001265 if (req->file) {
1266 if (req->flags & REQ_F_FIXED_FILE)
1267 percpu_ref_put(&ctx->file_data->refs);
1268 else
1269 fput(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001271
1272 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001273}
1274
1275static void __io_free_req(struct io_kiocb *req)
1276{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001277 __io_req_aux_free(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001278
Jens Axboefcb323c2019-10-24 12:39:47 -06001279 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001280 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001281 unsigned long flags;
1282
1283 spin_lock_irqsave(&ctx->inflight_lock, flags);
1284 list_del(&req->inflight_entry);
1285 if (waitqueue_active(&ctx->inflight_wait))
1286 wake_up(&ctx->inflight_wait);
1287 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1288 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001289
1290 percpu_ref_put(&req->ctx->refs);
1291 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001292}
1293
Jens Axboec6ca97b302019-12-28 12:11:08 -07001294struct req_batch {
1295 void *reqs[IO_IOPOLL_BATCH];
1296 int to_free;
1297 int need_iter;
1298};
1299
1300static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1301{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001302 int fixed_refs = rb->to_free;
1303
Jens Axboec6ca97b302019-12-28 12:11:08 -07001304 if (!rb->to_free)
1305 return;
1306 if (rb->need_iter) {
1307 int i, inflight = 0;
1308 unsigned long flags;
1309
Jens Axboe10fef4b2020-01-09 07:52:28 -07001310 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001311 for (i = 0; i < rb->to_free; i++) {
1312 struct io_kiocb *req = rb->reqs[i];
1313
Jens Axboe10fef4b2020-01-09 07:52:28 -07001314 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001315 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001316 fixed_refs++;
1317 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001318 if (req->flags & REQ_F_INFLIGHT)
1319 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001320 __io_req_aux_free(req);
1321 }
1322 if (!inflight)
1323 goto do_free;
1324
1325 spin_lock_irqsave(&ctx->inflight_lock, flags);
1326 for (i = 0; i < rb->to_free; i++) {
1327 struct io_kiocb *req = rb->reqs[i];
1328
Jens Axboe10fef4b2020-01-09 07:52:28 -07001329 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001330 list_del(&req->inflight_entry);
1331 if (!--inflight)
1332 break;
1333 }
1334 }
1335 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1336
1337 if (waitqueue_active(&ctx->inflight_wait))
1338 wake_up(&ctx->inflight_wait);
1339 }
1340do_free:
1341 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001342 if (fixed_refs)
1343 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001344 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001345 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001346}
1347
Jackie Liua197f662019-11-08 08:09:12 -07001348static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001349{
Jackie Liua197f662019-11-08 08:09:12 -07001350 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001351 int ret;
1352
Jens Axboe2d283902019-12-04 11:08:05 -07001353 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001354 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001355 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001356 io_commit_cqring(ctx);
1357 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001358 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001359 return true;
1360 }
1361
1362 return false;
1363}
1364
Jens Axboeba816ad2019-09-28 11:36:45 -06001365static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001366{
Jens Axboe2665abf2019-11-05 12:40:47 -07001367 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001368 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001369
Jens Axboe4d7dd462019-11-20 13:03:52 -07001370 /* Already got next link */
1371 if (req->flags & REQ_F_LINK_NEXT)
1372 return;
1373
Jens Axboe9e645e112019-05-10 16:07:28 -06001374 /*
1375 * The list should never be empty when we are called here. But could
1376 * potentially happen if the chain is messed up, check to be on the
1377 * safe side.
1378 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001379 while (!list_empty(&req->link_list)) {
1380 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1381 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001382
Pavel Begunkov44932332019-12-05 16:16:35 +03001383 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1384 (nxt->flags & REQ_F_TIMEOUT))) {
1385 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001386 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001387 req->flags &= ~REQ_F_LINK_TIMEOUT;
1388 continue;
1389 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001390
Pavel Begunkov44932332019-12-05 16:16:35 +03001391 list_del_init(&req->link_list);
1392 if (!list_empty(&nxt->link_list))
1393 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001394 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001395 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001396 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001397
Jens Axboe4d7dd462019-11-20 13:03:52 -07001398 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001399 if (wake_ev)
1400 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001401}
1402
1403/*
1404 * Called if REQ_F_LINK is set, and we fail the head request
1405 */
1406static void io_fail_links(struct io_kiocb *req)
1407{
Jens Axboe2665abf2019-11-05 12:40:47 -07001408 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001409 unsigned long flags;
1410
1411 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001412
1413 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001414 struct io_kiocb *link = list_first_entry(&req->link_list,
1415 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001416
Pavel Begunkov44932332019-12-05 16:16:35 +03001417 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001418 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001419
1420 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001421 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001422 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001423 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001424 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001425 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001426 }
Jens Axboe5d960722019-11-19 15:31:28 -07001427 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001428 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001429
1430 io_commit_cqring(ctx);
1431 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1432 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001433}
1434
Jens Axboe4d7dd462019-11-20 13:03:52 -07001435static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001436{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001437 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001438 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001439
Jens Axboe9e645e112019-05-10 16:07:28 -06001440 /*
1441 * If LINK is set, we have dependent requests in this chain. If we
1442 * didn't fail this request, queue the first one up, moving any other
1443 * dependencies to the next request. In case of failure, fail the rest
1444 * of the chain.
1445 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001446 if (req->flags & REQ_F_FAIL_LINK) {
1447 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001448 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1449 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001450 struct io_ring_ctx *ctx = req->ctx;
1451 unsigned long flags;
1452
1453 /*
1454 * If this is a timeout link, we could be racing with the
1455 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001456 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001457 */
1458 spin_lock_irqsave(&ctx->completion_lock, flags);
1459 io_req_link_next(req, nxt);
1460 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1461 } else {
1462 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001463 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001464}
Jens Axboe9e645e112019-05-10 16:07:28 -06001465
Jackie Liuc69f8db2019-11-09 11:00:08 +08001466static void io_free_req(struct io_kiocb *req)
1467{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001468 struct io_kiocb *nxt = NULL;
1469
1470 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001471 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001472
1473 if (nxt)
1474 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001475}
1476
Jens Axboeba816ad2019-09-28 11:36:45 -06001477/*
1478 * Drop reference to request, return next in chain (if there is one) if this
1479 * was the last reference to this request.
1480 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001481__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001482static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001483{
Jens Axboe2a44f462020-02-25 13:25:41 -07001484 if (refcount_dec_and_test(&req->refs)) {
1485 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001486 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001487 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001488}
1489
Jens Axboe2b188cc2019-01-07 10:46:33 -07001490static void io_put_req(struct io_kiocb *req)
1491{
Jens Axboedef596e2019-01-09 08:59:42 -07001492 if (refcount_dec_and_test(&req->refs))
1493 io_free_req(req);
1494}
1495
Jens Axboe978db572019-11-14 22:39:04 -07001496/*
1497 * Must only be used if we don't need to care about links, usually from
1498 * within the completion handling itself.
1499 */
1500static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001501{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001502 /* drop both submit and complete references */
1503 if (refcount_sub_and_test(2, &req->refs))
1504 __io_free_req(req);
1505}
1506
Jens Axboe978db572019-11-14 22:39:04 -07001507static void io_double_put_req(struct io_kiocb *req)
1508{
1509 /* drop both submit and complete references */
1510 if (refcount_sub_and_test(2, &req->refs))
1511 io_free_req(req);
1512}
1513
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001514static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001515{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001516 struct io_rings *rings = ctx->rings;
1517
Jens Axboead3eb2c2019-12-18 17:12:20 -07001518 if (test_bit(0, &ctx->cq_check_overflow)) {
1519 /*
1520 * noflush == true is from the waitqueue handler, just ensure
1521 * we wake up the task, and the next invocation will flush the
1522 * entries. We cannot safely to it from here.
1523 */
1524 if (noflush && !list_empty(&ctx->cq_overflow_list))
1525 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001526
Jens Axboead3eb2c2019-12-18 17:12:20 -07001527 io_cqring_overflow_flush(ctx, false);
1528 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001529
Jens Axboea3a0e432019-08-20 11:03:11 -06001530 /* See comment at the top of this file */
1531 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001532 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001533}
1534
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001535static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1536{
1537 struct io_rings *rings = ctx->rings;
1538
1539 /* make sure SQ entry isn't read before tail */
1540 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1541}
1542
Jens Axboe8237e042019-12-28 10:48:22 -07001543static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001544{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001545 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1546 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001547
Jens Axboec6ca97b302019-12-28 12:11:08 -07001548 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1549 rb->need_iter++;
1550
1551 rb->reqs[rb->to_free++] = req;
1552 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1553 io_free_req_many(req->ctx, rb);
1554 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001555}
1556
Jens Axboedef596e2019-01-09 08:59:42 -07001557/*
1558 * Find and free completed poll iocbs
1559 */
1560static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1561 struct list_head *done)
1562{
Jens Axboe8237e042019-12-28 10:48:22 -07001563 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001564 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001565
Jens Axboec6ca97b302019-12-28 12:11:08 -07001566 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001567 while (!list_empty(done)) {
1568 req = list_first_entry(done, struct io_kiocb, list);
1569 list_del(&req->list);
1570
Jens Axboe78e19bb2019-11-06 15:21:34 -07001571 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001572 (*nr_events)++;
1573
Jens Axboe8237e042019-12-28 10:48:22 -07001574 if (refcount_dec_and_test(&req->refs) &&
1575 !io_req_multi_free(&rb, req))
1576 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001577 }
Jens Axboedef596e2019-01-09 08:59:42 -07001578
Jens Axboe09bb8392019-03-13 12:39:28 -06001579 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001580 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001581}
1582
1583static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1584 long min)
1585{
1586 struct io_kiocb *req, *tmp;
1587 LIST_HEAD(done);
1588 bool spin;
1589 int ret;
1590
1591 /*
1592 * Only spin for completions if we don't have multiple devices hanging
1593 * off our complete list, and we're under the requested amount.
1594 */
1595 spin = !ctx->poll_multi_file && *nr_events < min;
1596
1597 ret = 0;
1598 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001599 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001600
1601 /*
1602 * Move completed entries to our local list. If we find a
1603 * request that requires polling, break out and complete
1604 * the done list first, if we have entries there.
1605 */
1606 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1607 list_move_tail(&req->list, &done);
1608 continue;
1609 }
1610 if (!list_empty(&done))
1611 break;
1612
1613 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1614 if (ret < 0)
1615 break;
1616
1617 if (ret && spin)
1618 spin = false;
1619 ret = 0;
1620 }
1621
1622 if (!list_empty(&done))
1623 io_iopoll_complete(ctx, nr_events, &done);
1624
1625 return ret;
1626}
1627
1628/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001629 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001630 * non-spinning poll check - we'll still enter the driver poll loop, but only
1631 * as a non-spinning completion check.
1632 */
1633static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1634 long min)
1635{
Jens Axboe08f54392019-08-21 22:19:11 -06001636 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001637 int ret;
1638
1639 ret = io_do_iopoll(ctx, nr_events, min);
1640 if (ret < 0)
1641 return ret;
1642 if (!min || *nr_events >= min)
1643 return 0;
1644 }
1645
1646 return 1;
1647}
1648
1649/*
1650 * We can't just wait for polled events to come to us, we have to actively
1651 * find and complete them.
1652 */
1653static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1654{
1655 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1656 return;
1657
1658 mutex_lock(&ctx->uring_lock);
1659 while (!list_empty(&ctx->poll_list)) {
1660 unsigned int nr_events = 0;
1661
1662 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001663
1664 /*
1665 * Ensure we allow local-to-the-cpu processing to take place,
1666 * in this case we need to ensure that we reap all events.
1667 */
1668 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001669 }
1670 mutex_unlock(&ctx->uring_lock);
1671}
1672
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001673static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1674 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001675{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001676 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001677
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001678 /*
1679 * We disallow the app entering submit/complete with polling, but we
1680 * still need to lock the ring to prevent racing with polled issue
1681 * that got punted to a workqueue.
1682 */
1683 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001684 do {
1685 int tmin = 0;
1686
Jens Axboe500f9fb2019-08-19 12:15:59 -06001687 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001688 * Don't enter poll loop if we already have events pending.
1689 * If we do, we can potentially be spinning for commands that
1690 * already triggered a CQE (eg in error).
1691 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001692 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001693 break;
1694
1695 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001696 * If a submit got punted to a workqueue, we can have the
1697 * application entering polling for a command before it gets
1698 * issued. That app will hold the uring_lock for the duration
1699 * of the poll right here, so we need to take a breather every
1700 * now and then to ensure that the issue has a chance to add
1701 * the poll to the issued list. Otherwise we can spin here
1702 * forever, while the workqueue is stuck trying to acquire the
1703 * very same mutex.
1704 */
1705 if (!(++iters & 7)) {
1706 mutex_unlock(&ctx->uring_lock);
1707 mutex_lock(&ctx->uring_lock);
1708 }
1709
Jens Axboedef596e2019-01-09 08:59:42 -07001710 if (*nr_events < min)
1711 tmin = min - *nr_events;
1712
1713 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1714 if (ret <= 0)
1715 break;
1716 ret = 0;
1717 } while (min && !*nr_events && !need_resched());
1718
Jens Axboe500f9fb2019-08-19 12:15:59 -06001719 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001720 return ret;
1721}
1722
Jens Axboe491381ce2019-10-17 09:20:46 -06001723static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001724{
Jens Axboe491381ce2019-10-17 09:20:46 -06001725 /*
1726 * Tell lockdep we inherited freeze protection from submission
1727 * thread.
1728 */
1729 if (req->flags & REQ_F_ISREG) {
1730 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001731
Jens Axboe491381ce2019-10-17 09:20:46 -06001732 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001733 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001734 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735}
1736
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001737static inline void req_set_fail_links(struct io_kiocb *req)
1738{
1739 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1740 req->flags |= REQ_F_FAIL_LINK;
1741}
1742
Jens Axboeba816ad2019-09-28 11:36:45 -06001743static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001744{
Jens Axboe9adbd452019-12-20 08:45:55 -07001745 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001746
Jens Axboe491381ce2019-10-17 09:20:46 -06001747 if (kiocb->ki_flags & IOCB_WRITE)
1748 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001749
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001750 if (res != req->result)
1751 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001752 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001753}
1754
1755static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1756{
Jens Axboe9adbd452019-12-20 08:45:55 -07001757 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001758
1759 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001760 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001761}
1762
Jens Axboeba816ad2019-09-28 11:36:45 -06001763static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1764{
Jens Axboe9adbd452019-12-20 08:45:55 -07001765 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001766 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001767
1768 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001769 io_put_req_find_next(req, &nxt);
1770
1771 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001772}
1773
Jens Axboedef596e2019-01-09 08:59:42 -07001774static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1775{
Jens Axboe9adbd452019-12-20 08:45:55 -07001776 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001777
Jens Axboe491381ce2019-10-17 09:20:46 -06001778 if (kiocb->ki_flags & IOCB_WRITE)
1779 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001780
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001781 if (res != req->result)
1782 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001783 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001784 if (res != -EAGAIN)
1785 req->flags |= REQ_F_IOPOLL_COMPLETED;
1786}
1787
1788/*
1789 * After the iocb has been issued, it's safe to be found on the poll list.
1790 * Adding the kiocb to the list AFTER submission ensures that we don't
1791 * find it from a io_iopoll_getevents() thread before the issuer is done
1792 * accessing the kiocb cookie.
1793 */
1794static void io_iopoll_req_issued(struct io_kiocb *req)
1795{
1796 struct io_ring_ctx *ctx = req->ctx;
1797
1798 /*
1799 * Track whether we have multiple files in our lists. This will impact
1800 * how we do polling eventually, not spinning if we're on potentially
1801 * different devices.
1802 */
1803 if (list_empty(&ctx->poll_list)) {
1804 ctx->poll_multi_file = false;
1805 } else if (!ctx->poll_multi_file) {
1806 struct io_kiocb *list_req;
1807
1808 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1809 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001810 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001811 ctx->poll_multi_file = true;
1812 }
1813
1814 /*
1815 * For fast devices, IO may have already completed. If it has, add
1816 * it to the front so we find it first.
1817 */
1818 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1819 list_add(&req->list, &ctx->poll_list);
1820 else
1821 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001822
1823 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1824 wq_has_sleeper(&ctx->sqo_wait))
1825 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001826}
1827
Jens Axboe3d6770f2019-04-13 11:50:54 -06001828static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001829{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001830 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001831 int diff = state->has_refs - state->used_refs;
1832
1833 if (diff)
1834 fput_many(state->file, diff);
1835 state->file = NULL;
1836 }
1837}
1838
1839/*
1840 * Get as many references to a file as we have IOs left in this submission,
1841 * assuming most submissions are for one file, or at least that each file
1842 * has more than one submission.
1843 */
1844static struct file *io_file_get(struct io_submit_state *state, int fd)
1845{
1846 if (!state)
1847 return fget(fd);
1848
1849 if (state->file) {
1850 if (state->fd == fd) {
1851 state->used_refs++;
1852 state->ios_left--;
1853 return state->file;
1854 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001855 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001856 }
1857 state->file = fget_many(fd, state->ios_left);
1858 if (!state->file)
1859 return NULL;
1860
1861 state->fd = fd;
1862 state->has_refs = state->ios_left;
1863 state->used_refs = 1;
1864 state->ios_left--;
1865 return state->file;
1866}
1867
Jens Axboe2b188cc2019-01-07 10:46:33 -07001868/*
1869 * If we tracked the file through the SCM inflight mechanism, we could support
1870 * any file. For now, just ensure that anything potentially problematic is done
1871 * inline.
1872 */
1873static bool io_file_supports_async(struct file *file)
1874{
1875 umode_t mode = file_inode(file)->i_mode;
1876
Jens Axboe10d59342019-12-09 20:16:22 -07001877 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001878 return true;
1879 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1880 return true;
1881
1882 return false;
1883}
1884
Jens Axboe3529d8c2019-12-19 18:24:38 -07001885static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1886 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001887{
Jens Axboedef596e2019-01-09 08:59:42 -07001888 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001889 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001890 unsigned ioprio;
1891 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001892
Jens Axboe491381ce2019-10-17 09:20:46 -06001893 if (S_ISREG(file_inode(req->file)->i_mode))
1894 req->flags |= REQ_F_ISREG;
1895
Jens Axboe2b188cc2019-01-07 10:46:33 -07001896 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001897 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1898 req->flags |= REQ_F_CUR_POS;
1899 kiocb->ki_pos = req->file->f_pos;
1900 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001901 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001902 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1903 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1904 if (unlikely(ret))
1905 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001906
1907 ioprio = READ_ONCE(sqe->ioprio);
1908 if (ioprio) {
1909 ret = ioprio_check_cap(ioprio);
1910 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001911 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001912
1913 kiocb->ki_ioprio = ioprio;
1914 } else
1915 kiocb->ki_ioprio = get_current_ioprio();
1916
Stefan Bühler8449eed2019-04-27 20:34:19 +02001917 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001918 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1919 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001920 req->flags |= REQ_F_NOWAIT;
1921
1922 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001923 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001924
Jens Axboedef596e2019-01-09 08:59:42 -07001925 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001926 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1927 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001928 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001929
Jens Axboedef596e2019-01-09 08:59:42 -07001930 kiocb->ki_flags |= IOCB_HIPRI;
1931 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001932 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001933 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001934 if (kiocb->ki_flags & IOCB_HIPRI)
1935 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001936 kiocb->ki_complete = io_complete_rw;
1937 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001938
Jens Axboe3529d8c2019-12-19 18:24:38 -07001939 req->rw.addr = READ_ONCE(sqe->addr);
1940 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001941 /* we own ->private, reuse it for the buffer index */
1942 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001943 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001944 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001945}
1946
1947static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1948{
1949 switch (ret) {
1950 case -EIOCBQUEUED:
1951 break;
1952 case -ERESTARTSYS:
1953 case -ERESTARTNOINTR:
1954 case -ERESTARTNOHAND:
1955 case -ERESTART_RESTARTBLOCK:
1956 /*
1957 * We can't just restart the syscall, since previously
1958 * submitted sqes may already be in progress. Just fail this
1959 * IO with EINTR.
1960 */
1961 ret = -EINTR;
1962 /* fall through */
1963 default:
1964 kiocb->ki_complete(kiocb, ret, 0);
1965 }
1966}
1967
Jens Axboeba816ad2019-09-28 11:36:45 -06001968static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1969 bool in_async)
1970{
Jens Axboeba042912019-12-25 16:33:42 -07001971 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1972
1973 if (req->flags & REQ_F_CUR_POS)
1974 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001975 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001976 *nxt = __io_complete_rw(kiocb, ret);
1977 else
1978 io_rw_done(kiocb, ret);
1979}
1980
Jens Axboe9adbd452019-12-20 08:45:55 -07001981static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001982 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001983{
Jens Axboe9adbd452019-12-20 08:45:55 -07001984 struct io_ring_ctx *ctx = req->ctx;
1985 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001986 struct io_mapped_ubuf *imu;
1987 unsigned index, buf_index;
1988 size_t offset;
1989 u64 buf_addr;
1990
1991 /* attempt to use fixed buffers without having provided iovecs */
1992 if (unlikely(!ctx->user_bufs))
1993 return -EFAULT;
1994
Jens Axboe9adbd452019-12-20 08:45:55 -07001995 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001996 if (unlikely(buf_index >= ctx->nr_user_bufs))
1997 return -EFAULT;
1998
1999 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2000 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002001 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002002
2003 /* overflow */
2004 if (buf_addr + len < buf_addr)
2005 return -EFAULT;
2006 /* not inside the mapped region */
2007 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2008 return -EFAULT;
2009
2010 /*
2011 * May not be a start of buffer, set size appropriately
2012 * and advance us to the beginning.
2013 */
2014 offset = buf_addr - imu->ubuf;
2015 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002016
2017 if (offset) {
2018 /*
2019 * Don't use iov_iter_advance() here, as it's really slow for
2020 * using the latter parts of a big fixed buffer - it iterates
2021 * over each segment manually. We can cheat a bit here, because
2022 * we know that:
2023 *
2024 * 1) it's a BVEC iter, we set it up
2025 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2026 * first and last bvec
2027 *
2028 * So just find our index, and adjust the iterator afterwards.
2029 * If the offset is within the first bvec (or the whole first
2030 * bvec, just use iov_iter_advance(). This makes it easier
2031 * since we can just skip the first segment, which may not
2032 * be PAGE_SIZE aligned.
2033 */
2034 const struct bio_vec *bvec = imu->bvec;
2035
2036 if (offset <= bvec->bv_len) {
2037 iov_iter_advance(iter, offset);
2038 } else {
2039 unsigned long seg_skip;
2040
2041 /* skip first vec */
2042 offset -= bvec->bv_len;
2043 seg_skip = 1 + (offset >> PAGE_SHIFT);
2044
2045 iter->bvec = bvec + seg_skip;
2046 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002047 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002048 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002049 }
2050 }
2051
Jens Axboe5e559562019-11-13 16:12:46 -07002052 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002053}
2054
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002055static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2056 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002057{
Jens Axboe9adbd452019-12-20 08:45:55 -07002058 void __user *buf = u64_to_user_ptr(req->rw.addr);
2059 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002060 u8 opcode;
2061
Jens Axboed625c6e2019-12-17 19:53:05 -07002062 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002063 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002064 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002065 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002066 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002067
Jens Axboe9adbd452019-12-20 08:45:55 -07002068 /* buffer index only valid with fixed read/write */
2069 if (req->rw.kiocb.private)
2070 return -EINVAL;
2071
Jens Axboe3a6820f2019-12-22 15:19:35 -07002072 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2073 ssize_t ret;
2074 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2075 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002076 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002077 }
2078
Jens Axboef67676d2019-12-02 11:03:47 -07002079 if (req->io) {
2080 struct io_async_rw *iorw = &req->io->rw;
2081
2082 *iovec = iorw->iov;
2083 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2084 if (iorw->iov == iorw->fast_iov)
2085 *iovec = NULL;
2086 return iorw->size;
2087 }
2088
Jens Axboe2b188cc2019-01-07 10:46:33 -07002089#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002090 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002091 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2092 iovec, iter);
2093#endif
2094
2095 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2096}
2097
Jens Axboe32960612019-09-23 11:05:34 -06002098/*
2099 * For files that don't have ->read_iter() and ->write_iter(), handle them
2100 * by looping over ->read() or ->write() manually.
2101 */
2102static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2103 struct iov_iter *iter)
2104{
2105 ssize_t ret = 0;
2106
2107 /*
2108 * Don't support polled IO through this interface, and we can't
2109 * support non-blocking either. For the latter, this just causes
2110 * the kiocb to be handled from an async context.
2111 */
2112 if (kiocb->ki_flags & IOCB_HIPRI)
2113 return -EOPNOTSUPP;
2114 if (kiocb->ki_flags & IOCB_NOWAIT)
2115 return -EAGAIN;
2116
2117 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002118 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002119 ssize_t nr;
2120
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002121 if (!iov_iter_is_bvec(iter)) {
2122 iovec = iov_iter_iovec(iter);
2123 } else {
2124 /* fixed buffers import bvec */
2125 iovec.iov_base = kmap(iter->bvec->bv_page)
2126 + iter->iov_offset;
2127 iovec.iov_len = min(iter->count,
2128 iter->bvec->bv_len - iter->iov_offset);
2129 }
2130
Jens Axboe32960612019-09-23 11:05:34 -06002131 if (rw == READ) {
2132 nr = file->f_op->read(file, iovec.iov_base,
2133 iovec.iov_len, &kiocb->ki_pos);
2134 } else {
2135 nr = file->f_op->write(file, iovec.iov_base,
2136 iovec.iov_len, &kiocb->ki_pos);
2137 }
2138
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002139 if (iov_iter_is_bvec(iter))
2140 kunmap(iter->bvec->bv_page);
2141
Jens Axboe32960612019-09-23 11:05:34 -06002142 if (nr < 0) {
2143 if (!ret)
2144 ret = nr;
2145 break;
2146 }
2147 ret += nr;
2148 if (nr != iovec.iov_len)
2149 break;
2150 iov_iter_advance(iter, nr);
2151 }
2152
2153 return ret;
2154}
2155
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002156static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002157 struct iovec *iovec, struct iovec *fast_iov,
2158 struct iov_iter *iter)
2159{
2160 req->io->rw.nr_segs = iter->nr_segs;
2161 req->io->rw.size = io_size;
2162 req->io->rw.iov = iovec;
2163 if (!req->io->rw.iov) {
2164 req->io->rw.iov = req->io->rw.fast_iov;
2165 memcpy(req->io->rw.iov, fast_iov,
2166 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002167 } else {
2168 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002169 }
2170}
2171
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002172static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002173{
Jens Axboed3656342019-12-18 09:50:26 -07002174 if (!io_op_defs[req->opcode].async_ctx)
2175 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002176 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002177 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002178}
2179
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002180static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2181 struct iovec *iovec, struct iovec *fast_iov,
2182 struct iov_iter *iter)
2183{
Jens Axboe980ad262020-01-24 23:08:54 -07002184 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002185 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002186 if (!req->io) {
2187 if (io_alloc_async_ctx(req))
2188 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002189
Jens Axboe5d204bc2020-01-31 12:06:52 -07002190 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2191 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002192 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002193}
2194
Jens Axboe3529d8c2019-12-19 18:24:38 -07002195static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2196 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002197{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002198 struct io_async_ctx *io;
2199 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002200 ssize_t ret;
2201
Jens Axboe3529d8c2019-12-19 18:24:38 -07002202 ret = io_prep_rw(req, sqe, force_nonblock);
2203 if (ret)
2204 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002205
Jens Axboe3529d8c2019-12-19 18:24:38 -07002206 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2207 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002208
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002209 /* either don't need iovec imported or already have it */
2210 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002211 return 0;
2212
2213 io = req->io;
2214 io->rw.iov = io->rw.fast_iov;
2215 req->io = NULL;
2216 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2217 req->io = io;
2218 if (ret < 0)
2219 return ret;
2220
2221 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2222 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002223}
2224
Pavel Begunkov267bc902019-11-07 01:41:08 +03002225static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002226 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002227{
2228 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002229 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002230 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002231 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002232 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002233
Jens Axboe3529d8c2019-12-19 18:24:38 -07002234 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002235 if (ret < 0)
2236 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002237
Jens Axboefd6c2e42019-12-18 12:19:41 -07002238 /* Ensure we clear previously set non-block flag */
2239 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002240 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002241
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002242 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002243 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002244 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002245 req->result = io_size;
2246
2247 /*
2248 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2249 * we know to async punt it even if it was opened O_NONBLOCK
2250 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002251 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002252 req->flags |= REQ_F_MUST_PUNT;
2253 goto copy_iov;
2254 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002255
Jens Axboe31b51512019-01-18 22:56:34 -07002256 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002257 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002258 if (!ret) {
2259 ssize_t ret2;
2260
Jens Axboe9adbd452019-12-20 08:45:55 -07002261 if (req->file->f_op->read_iter)
2262 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002263 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002264 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002265
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002266 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002267 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002268 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002269 } else {
2270copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002271 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002272 inline_vecs, &iter);
2273 if (ret)
2274 goto out_free;
2275 return -EAGAIN;
2276 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002277 }
Jens Axboef67676d2019-12-02 11:03:47 -07002278out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002279 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002280 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002281 return ret;
2282}
2283
Jens Axboe3529d8c2019-12-19 18:24:38 -07002284static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2285 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002286{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002287 struct io_async_ctx *io;
2288 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002289 ssize_t ret;
2290
Jens Axboe3529d8c2019-12-19 18:24:38 -07002291 ret = io_prep_rw(req, sqe, force_nonblock);
2292 if (ret)
2293 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002294
Jens Axboe3529d8c2019-12-19 18:24:38 -07002295 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2296 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002297
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002298 /* either don't need iovec imported or already have it */
2299 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002300 return 0;
2301
2302 io = req->io;
2303 io->rw.iov = io->rw.fast_iov;
2304 req->io = NULL;
2305 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2306 req->io = io;
2307 if (ret < 0)
2308 return ret;
2309
2310 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2311 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002312}
2313
Pavel Begunkov267bc902019-11-07 01:41:08 +03002314static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002315 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002316{
2317 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002318 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002319 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002320 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002321 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002322
Jens Axboe3529d8c2019-12-19 18:24:38 -07002323 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002324 if (ret < 0)
2325 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002326
Jens Axboefd6c2e42019-12-18 12:19:41 -07002327 /* Ensure we clear previously set non-block flag */
2328 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002329 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002330
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002331 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002332 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002333 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002334 req->result = io_size;
2335
2336 /*
2337 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2338 * we know to async punt it even if it was opened O_NONBLOCK
2339 */
2340 if (force_nonblock && !io_file_supports_async(req->file)) {
2341 req->flags |= REQ_F_MUST_PUNT;
2342 goto copy_iov;
2343 }
2344
Jens Axboe10d59342019-12-09 20:16:22 -07002345 /* file path doesn't support NOWAIT for non-direct_IO */
2346 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2347 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002348 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002349
Jens Axboe31b51512019-01-18 22:56:34 -07002350 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002351 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002352 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002353 ssize_t ret2;
2354
Jens Axboe2b188cc2019-01-07 10:46:33 -07002355 /*
2356 * Open-code file_start_write here to grab freeze protection,
2357 * which will be released by another thread in
2358 * io_complete_rw(). Fool lockdep by telling it the lock got
2359 * released so that it doesn't complain about the held lock when
2360 * we return to userspace.
2361 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002362 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002363 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002364 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002365 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002366 SB_FREEZE_WRITE);
2367 }
2368 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002369
Jens Axboe9adbd452019-12-20 08:45:55 -07002370 if (req->file->f_op->write_iter)
2371 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002372 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002373 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002374 /*
2375 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2376 * retry them without IOCB_NOWAIT.
2377 */
2378 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2379 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002380 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002381 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002382 } else {
2383copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002384 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002385 inline_vecs, &iter);
2386 if (ret)
2387 goto out_free;
2388 return -EAGAIN;
2389 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002390 }
Jens Axboe31b51512019-01-18 22:56:34 -07002391out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002392 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002393 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002394 return ret;
2395}
2396
2397/*
2398 * IORING_OP_NOP just posts a completion event, nothing else.
2399 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002400static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002401{
2402 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002403
Jens Axboedef596e2019-01-09 08:59:42 -07002404 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2405 return -EINVAL;
2406
Jens Axboe78e19bb2019-11-06 15:21:34 -07002407 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002408 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002409 return 0;
2410}
2411
Jens Axboe3529d8c2019-12-19 18:24:38 -07002412static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002413{
Jens Axboe6b063142019-01-10 22:13:58 -07002414 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002415
Jens Axboe09bb8392019-03-13 12:39:28 -06002416 if (!req->file)
2417 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002418
Jens Axboe6b063142019-01-10 22:13:58 -07002419 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002420 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002421 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002422 return -EINVAL;
2423
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002424 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2425 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2426 return -EINVAL;
2427
2428 req->sync.off = READ_ONCE(sqe->off);
2429 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002430 return 0;
2431}
2432
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002433static bool io_req_cancelled(struct io_kiocb *req)
2434{
2435 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2436 req_set_fail_links(req);
2437 io_cqring_add_event(req, -ECANCELED);
2438 io_put_req(req);
2439 return true;
2440 }
2441
2442 return false;
2443}
2444
Jens Axboe78912932020-01-14 22:09:06 -07002445static void io_link_work_cb(struct io_wq_work **workptr)
2446{
2447 struct io_wq_work *work = *workptr;
2448 struct io_kiocb *link = work->data;
2449
2450 io_queue_linked_timeout(link);
2451 work->func = io_wq_submit_work;
2452}
2453
2454static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2455{
2456 struct io_kiocb *link;
2457
2458 io_prep_async_work(nxt, &link);
2459 *workptr = &nxt->work;
2460 if (link) {
2461 nxt->work.flags |= IO_WQ_WORK_CB;
2462 nxt->work.func = io_link_work_cb;
2463 nxt->work.data = link;
2464 }
2465}
2466
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002467static void io_fsync_finish(struct io_wq_work **workptr)
2468{
2469 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2470 loff_t end = req->sync.off + req->sync.len;
2471 struct io_kiocb *nxt = NULL;
2472 int ret;
2473
2474 if (io_req_cancelled(req))
2475 return;
2476
Jens Axboe9adbd452019-12-20 08:45:55 -07002477 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002478 end > 0 ? end : LLONG_MAX,
2479 req->sync.flags & IORING_FSYNC_DATASYNC);
2480 if (ret < 0)
2481 req_set_fail_links(req);
2482 io_cqring_add_event(req, ret);
2483 io_put_req_find_next(req, &nxt);
2484 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002485 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002486}
2487
Jens Axboefc4df992019-12-10 14:38:45 -07002488static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2489 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002490{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002491 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002492
2493 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002494 if (force_nonblock) {
2495 io_put_req(req);
2496 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002497 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002498 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002499
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002500 work = old_work = &req->work;
2501 io_fsync_finish(&work);
2502 if (work && work != old_work)
2503 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002504 return 0;
2505}
2506
Jens Axboed63d1b52019-12-10 10:38:56 -07002507static void io_fallocate_finish(struct io_wq_work **workptr)
2508{
2509 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2510 struct io_kiocb *nxt = NULL;
2511 int ret;
2512
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002513 if (io_req_cancelled(req))
2514 return;
2515
Jens Axboed63d1b52019-12-10 10:38:56 -07002516 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2517 req->sync.len);
2518 if (ret < 0)
2519 req_set_fail_links(req);
2520 io_cqring_add_event(req, ret);
2521 io_put_req_find_next(req, &nxt);
2522 if (nxt)
2523 io_wq_assign_next(workptr, nxt);
2524}
2525
2526static int io_fallocate_prep(struct io_kiocb *req,
2527 const struct io_uring_sqe *sqe)
2528{
2529 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2530 return -EINVAL;
2531
2532 req->sync.off = READ_ONCE(sqe->off);
2533 req->sync.len = READ_ONCE(sqe->addr);
2534 req->sync.mode = READ_ONCE(sqe->len);
2535 return 0;
2536}
2537
2538static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2539 bool force_nonblock)
2540{
2541 struct io_wq_work *work, *old_work;
2542
2543 /* fallocate always requiring blocking context */
2544 if (force_nonblock) {
2545 io_put_req(req);
2546 req->work.func = io_fallocate_finish;
2547 return -EAGAIN;
2548 }
2549
2550 work = old_work = &req->work;
2551 io_fallocate_finish(&work);
2552 if (work && work != old_work)
2553 *nxt = container_of(work, struct io_kiocb, work);
2554
2555 return 0;
2556}
2557
Jens Axboe15b71ab2019-12-11 11:20:36 -07002558static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2559{
Jens Axboef8748882020-01-08 17:47:02 -07002560 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002561 int ret;
2562
2563 if (sqe->ioprio || sqe->buf_index)
2564 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002565 if (sqe->flags & IOSQE_FIXED_FILE)
2566 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002567 if (req->flags & REQ_F_NEED_CLEANUP)
2568 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002569
2570 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002571 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002572 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002573 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002574
Jens Axboef8748882020-01-08 17:47:02 -07002575 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002576 if (IS_ERR(req->open.filename)) {
2577 ret = PTR_ERR(req->open.filename);
2578 req->open.filename = NULL;
2579 return ret;
2580 }
2581
Jens Axboe4022e7a2020-03-19 19:23:18 -06002582 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002583 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002584 return 0;
2585}
2586
Jens Axboecebdb982020-01-08 17:59:24 -07002587static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2588{
2589 struct open_how __user *how;
2590 const char __user *fname;
2591 size_t len;
2592 int ret;
2593
2594 if (sqe->ioprio || sqe->buf_index)
2595 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002596 if (sqe->flags & IOSQE_FIXED_FILE)
2597 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002598 if (req->flags & REQ_F_NEED_CLEANUP)
2599 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002600
2601 req->open.dfd = READ_ONCE(sqe->fd);
2602 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2603 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2604 len = READ_ONCE(sqe->len);
2605
2606 if (len < OPEN_HOW_SIZE_VER0)
2607 return -EINVAL;
2608
2609 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2610 len);
2611 if (ret)
2612 return ret;
2613
2614 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2615 req->open.how.flags |= O_LARGEFILE;
2616
2617 req->open.filename = getname(fname);
2618 if (IS_ERR(req->open.filename)) {
2619 ret = PTR_ERR(req->open.filename);
2620 req->open.filename = NULL;
2621 return ret;
2622 }
2623
Jens Axboe4022e7a2020-03-19 19:23:18 -06002624 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002625 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002626 return 0;
2627}
2628
2629static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2630 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002631{
2632 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002633 struct file *file;
2634 int ret;
2635
Jens Axboef86cd202020-01-29 13:46:44 -07002636 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002637 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002638
Jens Axboecebdb982020-01-08 17:59:24 -07002639 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002640 if (ret)
2641 goto err;
2642
Jens Axboe4022e7a2020-03-19 19:23:18 -06002643 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002644 if (ret < 0)
2645 goto err;
2646
2647 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2648 if (IS_ERR(file)) {
2649 put_unused_fd(ret);
2650 ret = PTR_ERR(file);
2651 } else {
2652 fsnotify_open(file);
2653 fd_install(ret, file);
2654 }
2655err:
2656 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002657 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002658 if (ret < 0)
2659 req_set_fail_links(req);
2660 io_cqring_add_event(req, ret);
2661 io_put_req_find_next(req, nxt);
2662 return 0;
2663}
2664
Jens Axboecebdb982020-01-08 17:59:24 -07002665static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2666 bool force_nonblock)
2667{
2668 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2669 return io_openat2(req, nxt, force_nonblock);
2670}
2671
Jens Axboe3e4827b2020-01-08 15:18:09 -07002672static int io_epoll_ctl_prep(struct io_kiocb *req,
2673 const struct io_uring_sqe *sqe)
2674{
2675#if defined(CONFIG_EPOLL)
2676 if (sqe->ioprio || sqe->buf_index)
2677 return -EINVAL;
2678
2679 req->epoll.epfd = READ_ONCE(sqe->fd);
2680 req->epoll.op = READ_ONCE(sqe->len);
2681 req->epoll.fd = READ_ONCE(sqe->off);
2682
2683 if (ep_op_has_event(req->epoll.op)) {
2684 struct epoll_event __user *ev;
2685
2686 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2687 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2688 return -EFAULT;
2689 }
2690
2691 return 0;
2692#else
2693 return -EOPNOTSUPP;
2694#endif
2695}
2696
2697static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2698 bool force_nonblock)
2699{
2700#if defined(CONFIG_EPOLL)
2701 struct io_epoll *ie = &req->epoll;
2702 int ret;
2703
2704 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2705 if (force_nonblock && ret == -EAGAIN)
2706 return -EAGAIN;
2707
2708 if (ret < 0)
2709 req_set_fail_links(req);
2710 io_cqring_add_event(req, ret);
2711 io_put_req_find_next(req, nxt);
2712 return 0;
2713#else
2714 return -EOPNOTSUPP;
2715#endif
2716}
2717
Jens Axboec1ca7572019-12-25 22:18:28 -07002718static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2719{
2720#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2721 if (sqe->ioprio || sqe->buf_index || sqe->off)
2722 return -EINVAL;
2723
2724 req->madvise.addr = READ_ONCE(sqe->addr);
2725 req->madvise.len = READ_ONCE(sqe->len);
2726 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2727 return 0;
2728#else
2729 return -EOPNOTSUPP;
2730#endif
2731}
2732
2733static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2734 bool force_nonblock)
2735{
2736#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2737 struct io_madvise *ma = &req->madvise;
2738 int ret;
2739
2740 if (force_nonblock)
2741 return -EAGAIN;
2742
2743 ret = do_madvise(ma->addr, ma->len, ma->advice);
2744 if (ret < 0)
2745 req_set_fail_links(req);
2746 io_cqring_add_event(req, ret);
2747 io_put_req_find_next(req, nxt);
2748 return 0;
2749#else
2750 return -EOPNOTSUPP;
2751#endif
2752}
2753
Jens Axboe4840e412019-12-25 22:03:45 -07002754static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2755{
2756 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2757 return -EINVAL;
2758
2759 req->fadvise.offset = READ_ONCE(sqe->off);
2760 req->fadvise.len = READ_ONCE(sqe->len);
2761 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2762 return 0;
2763}
2764
2765static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2766 bool force_nonblock)
2767{
2768 struct io_fadvise *fa = &req->fadvise;
2769 int ret;
2770
Jens Axboe3e694262020-02-01 09:22:49 -07002771 if (force_nonblock) {
2772 switch (fa->advice) {
2773 case POSIX_FADV_NORMAL:
2774 case POSIX_FADV_RANDOM:
2775 case POSIX_FADV_SEQUENTIAL:
2776 break;
2777 default:
2778 return -EAGAIN;
2779 }
2780 }
Jens Axboe4840e412019-12-25 22:03:45 -07002781
2782 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2783 if (ret < 0)
2784 req_set_fail_links(req);
2785 io_cqring_add_event(req, ret);
2786 io_put_req_find_next(req, nxt);
2787 return 0;
2788}
2789
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002790static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2791{
Jens Axboef8748882020-01-08 17:47:02 -07002792 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002793 unsigned lookup_flags;
2794 int ret;
2795
2796 if (sqe->ioprio || sqe->buf_index)
2797 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002798 if (sqe->flags & IOSQE_FIXED_FILE)
2799 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002800 if (req->flags & REQ_F_NEED_CLEANUP)
2801 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002802
2803 req->open.dfd = READ_ONCE(sqe->fd);
2804 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002805 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002806 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002807 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002808
Jens Axboec12cedf2020-01-08 17:41:21 -07002809 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002810 return -EINVAL;
2811
Jens Axboef8748882020-01-08 17:47:02 -07002812 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002813 if (IS_ERR(req->open.filename)) {
2814 ret = PTR_ERR(req->open.filename);
2815 req->open.filename = NULL;
2816 return ret;
2817 }
2818
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002819 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002820 return 0;
2821}
2822
2823static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2824 bool force_nonblock)
2825{
2826 struct io_open *ctx = &req->open;
2827 unsigned lookup_flags;
2828 struct path path;
2829 struct kstat stat;
2830 int ret;
2831
2832 if (force_nonblock)
2833 return -EAGAIN;
2834
Jens Axboec12cedf2020-01-08 17:41:21 -07002835 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002836 return -EINVAL;
2837
2838retry:
2839 /* filename_lookup() drops it, keep a reference */
2840 ctx->filename->refcnt++;
2841
2842 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2843 NULL);
2844 if (ret)
2845 goto err;
2846
Jens Axboec12cedf2020-01-08 17:41:21 -07002847 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002848 path_put(&path);
2849 if (retry_estale(ret, lookup_flags)) {
2850 lookup_flags |= LOOKUP_REVAL;
2851 goto retry;
2852 }
2853 if (!ret)
2854 ret = cp_statx(&stat, ctx->buffer);
2855err:
2856 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002857 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002858 if (ret < 0)
2859 req_set_fail_links(req);
2860 io_cqring_add_event(req, ret);
2861 io_put_req_find_next(req, nxt);
2862 return 0;
2863}
2864
Jens Axboeb5dba592019-12-11 14:02:38 -07002865static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2866{
2867 /*
2868 * If we queue this for async, it must not be cancellable. That would
2869 * leave the 'file' in an undeterminate state.
2870 */
2871 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2872
2873 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2874 sqe->rw_flags || sqe->buf_index)
2875 return -EINVAL;
2876 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002877 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002878
2879 req->close.fd = READ_ONCE(sqe->fd);
2880 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002881 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002882 return -EBADF;
2883
2884 return 0;
2885}
2886
Pavel Begunkova93b3332020-02-08 14:04:34 +03002887/* only called when __close_fd_get_file() is done */
2888static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt)
2889{
2890 int ret;
2891
2892 ret = filp_close(req->close.put_file, req->work.files);
2893 if (ret < 0)
2894 req_set_fail_links(req);
2895 io_cqring_add_event(req, ret);
2896 fput(req->close.put_file);
2897 io_put_req_find_next(req, nxt);
2898}
2899
Jens Axboeb5dba592019-12-11 14:02:38 -07002900static void io_close_finish(struct io_wq_work **workptr)
2901{
2902 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2903 struct io_kiocb *nxt = NULL;
2904
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002905 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002906 __io_close_finish(req, &nxt);
Jens Axboeb5dba592019-12-11 14:02:38 -07002907 if (nxt)
2908 io_wq_assign_next(workptr, nxt);
2909}
2910
2911static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2912 bool force_nonblock)
2913{
2914 int ret;
2915
2916 req->close.put_file = NULL;
2917 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2918 if (ret < 0)
2919 return ret;
2920
2921 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002922 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002923 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002924
2925 /*
2926 * No ->flush(), safely close from here and just punt the
2927 * fput() to async context.
2928 */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002929 __io_close_finish(req, nxt);
2930 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002931eagain:
2932 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07002933 /*
2934 * Do manual async queue here to avoid grabbing files - we don't
2935 * need the files, and it'll cause io_close_finish() to close
2936 * the file again and cause a double CQE entry for this request
2937 */
2938 io_queue_async_work(req);
2939 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002940}
2941
Jens Axboe3529d8c2019-12-19 18:24:38 -07002942static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002943{
2944 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002945
2946 if (!req->file)
2947 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002948
2949 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2950 return -EINVAL;
2951 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2952 return -EINVAL;
2953
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002954 req->sync.off = READ_ONCE(sqe->off);
2955 req->sync.len = READ_ONCE(sqe->len);
2956 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002957 return 0;
2958}
2959
2960static void io_sync_file_range_finish(struct io_wq_work **workptr)
2961{
2962 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2963 struct io_kiocb *nxt = NULL;
2964 int ret;
2965
2966 if (io_req_cancelled(req))
2967 return;
2968
Jens Axboe9adbd452019-12-20 08:45:55 -07002969 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002970 req->sync.flags);
2971 if (ret < 0)
2972 req_set_fail_links(req);
2973 io_cqring_add_event(req, ret);
2974 io_put_req_find_next(req, &nxt);
2975 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002976 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002977}
2978
Jens Axboefc4df992019-12-10 14:38:45 -07002979static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002980 bool force_nonblock)
2981{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002982 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002983
2984 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002985 if (force_nonblock) {
2986 io_put_req(req);
2987 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002988 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002989 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002990
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002991 work = old_work = &req->work;
2992 io_sync_file_range_finish(&work);
2993 if (work && work != old_work)
2994 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002995 return 0;
2996}
2997
Jens Axboe3529d8c2019-12-19 18:24:38 -07002998static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002999{
Jens Axboe03b12302019-12-02 18:50:25 -07003000#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003001 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003002 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003003 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003004
Jens Axboee47293f2019-12-20 08:58:21 -07003005 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3006 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003007 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003008
Jens Axboed8768362020-02-27 14:17:49 -07003009#ifdef CONFIG_COMPAT
3010 if (req->ctx->compat)
3011 sr->msg_flags |= MSG_CMSG_COMPAT;
3012#endif
3013
Jens Axboefddafac2020-01-04 20:19:44 -07003014 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003015 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003016 /* iovec is already imported */
3017 if (req->flags & REQ_F_NEED_CLEANUP)
3018 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003019
Jens Axboed9688562019-12-09 19:35:20 -07003020 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003021 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003022 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003023 if (!ret)
3024 req->flags |= REQ_F_NEED_CLEANUP;
3025 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003026#else
Jens Axboee47293f2019-12-20 08:58:21 -07003027 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003028#endif
3029}
3030
Jens Axboefc4df992019-12-10 14:38:45 -07003031static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3032 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003033{
3034#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003035 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003036 struct socket *sock;
3037 int ret;
3038
3039 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3040 return -EINVAL;
3041
3042 sock = sock_from_file(req->file, &ret);
3043 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003044 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003045 unsigned flags;
3046
Jens Axboe03b12302019-12-02 18:50:25 -07003047 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003048 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003049 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003050 /* if iov is set, it's allocated already */
3051 if (!kmsg->iov)
3052 kmsg->iov = kmsg->fast_iov;
3053 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003054 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003055 struct io_sr_msg *sr = &req->sr_msg;
3056
Jens Axboe0b416c32019-12-15 10:57:46 -07003057 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003058 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003059
3060 io.msg.iov = io.msg.fast_iov;
3061 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3062 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003063 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003064 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003065 }
3066
Jens Axboee47293f2019-12-20 08:58:21 -07003067 flags = req->sr_msg.msg_flags;
3068 if (flags & MSG_DONTWAIT)
3069 req->flags |= REQ_F_NOWAIT;
3070 else if (force_nonblock)
3071 flags |= MSG_DONTWAIT;
3072
Jens Axboe0b416c32019-12-15 10:57:46 -07003073 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003074 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003075 if (req->io)
3076 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003077 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003078 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003079 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003080 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003081 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003082 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003083 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003084 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003085 }
3086 if (ret == -ERESTARTSYS)
3087 ret = -EINTR;
3088 }
3089
Pavel Begunkov1e950812020-02-06 19:51:16 +03003090 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003091 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003092 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003093 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003094 if (ret < 0)
3095 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003096 io_put_req_find_next(req, nxt);
3097 return 0;
3098#else
3099 return -EOPNOTSUPP;
3100#endif
3101}
3102
Jens Axboefddafac2020-01-04 20:19:44 -07003103static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3104 bool force_nonblock)
3105{
3106#if defined(CONFIG_NET)
3107 struct socket *sock;
3108 int ret;
3109
3110 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3111 return -EINVAL;
3112
3113 sock = sock_from_file(req->file, &ret);
3114 if (sock) {
3115 struct io_sr_msg *sr = &req->sr_msg;
3116 struct msghdr msg;
3117 struct iovec iov;
3118 unsigned flags;
3119
3120 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3121 &msg.msg_iter);
3122 if (ret)
3123 return ret;
3124
3125 msg.msg_name = NULL;
3126 msg.msg_control = NULL;
3127 msg.msg_controllen = 0;
3128 msg.msg_namelen = 0;
3129
3130 flags = req->sr_msg.msg_flags;
3131 if (flags & MSG_DONTWAIT)
3132 req->flags |= REQ_F_NOWAIT;
3133 else if (force_nonblock)
3134 flags |= MSG_DONTWAIT;
3135
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003136 msg.msg_flags = flags;
3137 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003138 if (force_nonblock && ret == -EAGAIN)
3139 return -EAGAIN;
3140 if (ret == -ERESTARTSYS)
3141 ret = -EINTR;
3142 }
3143
3144 io_cqring_add_event(req, ret);
3145 if (ret < 0)
3146 req_set_fail_links(req);
3147 io_put_req_find_next(req, nxt);
3148 return 0;
3149#else
3150 return -EOPNOTSUPP;
3151#endif
3152}
3153
Jens Axboe3529d8c2019-12-19 18:24:38 -07003154static int io_recvmsg_prep(struct io_kiocb *req,
3155 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003156{
3157#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003158 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003159 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003160 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003161
Jens Axboe3529d8c2019-12-19 18:24:38 -07003162 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3163 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003164 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003165
Jens Axboed8768362020-02-27 14:17:49 -07003166#ifdef CONFIG_COMPAT
3167 if (req->ctx->compat)
3168 sr->msg_flags |= MSG_CMSG_COMPAT;
3169#endif
3170
Jens Axboefddafac2020-01-04 20:19:44 -07003171 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003172 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003173 /* iovec is already imported */
3174 if (req->flags & REQ_F_NEED_CLEANUP)
3175 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003176
Jens Axboed9688562019-12-09 19:35:20 -07003177 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003178 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003179 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003180 if (!ret)
3181 req->flags |= REQ_F_NEED_CLEANUP;
3182 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003183#else
Jens Axboee47293f2019-12-20 08:58:21 -07003184 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003185#endif
3186}
3187
Jens Axboefc4df992019-12-10 14:38:45 -07003188static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3189 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003190{
3191#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003192 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003193 struct socket *sock;
3194 int ret;
3195
3196 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3197 return -EINVAL;
3198
3199 sock = sock_from_file(req->file, &ret);
3200 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003201 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003202 unsigned flags;
3203
Jens Axboe03b12302019-12-02 18:50:25 -07003204 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003205 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003206 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003207 /* if iov is set, it's allocated already */
3208 if (!kmsg->iov)
3209 kmsg->iov = kmsg->fast_iov;
3210 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003211 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003212 struct io_sr_msg *sr = &req->sr_msg;
3213
Jens Axboe0b416c32019-12-15 10:57:46 -07003214 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003215 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003216
3217 io.msg.iov = io.msg.fast_iov;
3218 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3219 sr->msg_flags, &io.msg.uaddr,
3220 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003221 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003222 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003223 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003224
Jens Axboee47293f2019-12-20 08:58:21 -07003225 flags = req->sr_msg.msg_flags;
3226 if (flags & MSG_DONTWAIT)
3227 req->flags |= REQ_F_NOWAIT;
3228 else if (force_nonblock)
3229 flags |= MSG_DONTWAIT;
3230
3231 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3232 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003233 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003234 if (req->io)
3235 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003236 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003237 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003238 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003239 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003240 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003241 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003242 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe0b416c32019-12-15 10:57:46 -07003243 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003244 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003245 if (ret == -ERESTARTSYS)
3246 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003247 }
3248
Pavel Begunkov1e950812020-02-06 19:51:16 +03003249 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003250 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003251 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003252 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003253 if (ret < 0)
3254 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003255 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003256 return 0;
3257#else
3258 return -EOPNOTSUPP;
3259#endif
3260}
3261
Jens Axboefddafac2020-01-04 20:19:44 -07003262static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3263 bool force_nonblock)
3264{
3265#if defined(CONFIG_NET)
3266 struct socket *sock;
3267 int ret;
3268
3269 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3270 return -EINVAL;
3271
3272 sock = sock_from_file(req->file, &ret);
3273 if (sock) {
3274 struct io_sr_msg *sr = &req->sr_msg;
3275 struct msghdr msg;
3276 struct iovec iov;
3277 unsigned flags;
3278
3279 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3280 &msg.msg_iter);
3281 if (ret)
3282 return ret;
3283
3284 msg.msg_name = NULL;
3285 msg.msg_control = NULL;
3286 msg.msg_controllen = 0;
3287 msg.msg_namelen = 0;
3288 msg.msg_iocb = NULL;
3289 msg.msg_flags = 0;
3290
3291 flags = req->sr_msg.msg_flags;
3292 if (flags & MSG_DONTWAIT)
3293 req->flags |= REQ_F_NOWAIT;
3294 else if (force_nonblock)
3295 flags |= MSG_DONTWAIT;
3296
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003297 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003298 if (force_nonblock && ret == -EAGAIN)
3299 return -EAGAIN;
3300 if (ret == -ERESTARTSYS)
3301 ret = -EINTR;
3302 }
3303
3304 io_cqring_add_event(req, ret);
3305 if (ret < 0)
3306 req_set_fail_links(req);
3307 io_put_req_find_next(req, nxt);
3308 return 0;
3309#else
3310 return -EOPNOTSUPP;
3311#endif
3312}
3313
3314
Jens Axboe3529d8c2019-12-19 18:24:38 -07003315static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003316{
3317#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003318 struct io_accept *accept = &req->accept;
3319
Jens Axboe17f2fe32019-10-17 14:42:58 -06003320 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3321 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003322 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003323 return -EINVAL;
3324
Jens Axboed55e5f52019-12-11 16:12:15 -07003325 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3326 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003327 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe09952e32020-03-19 20:16:56 -06003328 accept->nofile = rlimit(RLIMIT_NOFILE);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003329 return 0;
3330#else
3331 return -EOPNOTSUPP;
3332#endif
3333}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003334
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003335#if defined(CONFIG_NET)
3336static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3337 bool force_nonblock)
3338{
3339 struct io_accept *accept = &req->accept;
3340 unsigned file_flags;
3341 int ret;
3342
3343 file_flags = force_nonblock ? O_NONBLOCK : 0;
3344 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
Jens Axboe09952e32020-03-19 20:16:56 -06003345 accept->addr_len, accept->flags,
3346 accept->nofile);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003347 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003348 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003349 if (ret == -ERESTARTSYS)
3350 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003351 if (ret < 0)
3352 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003353 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003354 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003355 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003356}
3357
3358static void io_accept_finish(struct io_wq_work **workptr)
3359{
3360 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3361 struct io_kiocb *nxt = NULL;
3362
3363 if (io_req_cancelled(req))
3364 return;
3365 __io_accept(req, &nxt, false);
3366 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003367 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003368}
3369#endif
3370
3371static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3372 bool force_nonblock)
3373{
3374#if defined(CONFIG_NET)
3375 int ret;
3376
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003377 ret = __io_accept(req, nxt, force_nonblock);
3378 if (ret == -EAGAIN && force_nonblock) {
3379 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003380 io_put_req(req);
3381 return -EAGAIN;
3382 }
3383 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003384#else
3385 return -EOPNOTSUPP;
3386#endif
3387}
3388
Jens Axboe3529d8c2019-12-19 18:24:38 -07003389static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003390{
3391#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003392 struct io_connect *conn = &req->connect;
3393 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003394
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003395 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3396 return -EINVAL;
3397 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3398 return -EINVAL;
3399
Jens Axboe3529d8c2019-12-19 18:24:38 -07003400 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3401 conn->addr_len = READ_ONCE(sqe->addr2);
3402
3403 if (!io)
3404 return 0;
3405
3406 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003407 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003408#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003409 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003410#endif
3411}
3412
Jens Axboefc4df992019-12-10 14:38:45 -07003413static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3414 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003415{
3416#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003417 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003418 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003419 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003420
Jens Axboef499a022019-12-02 16:28:46 -07003421 if (req->io) {
3422 io = req->io;
3423 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003424 ret = move_addr_to_kernel(req->connect.addr,
3425 req->connect.addr_len,
3426 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003427 if (ret)
3428 goto out;
3429 io = &__io;
3430 }
3431
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003432 file_flags = force_nonblock ? O_NONBLOCK : 0;
3433
3434 ret = __sys_connect_file(req->file, &io->connect.address,
3435 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003436 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003437 if (req->io)
3438 return -EAGAIN;
3439 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003440 ret = -ENOMEM;
3441 goto out;
3442 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003443 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003444 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003445 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003446 if (ret == -ERESTARTSYS)
3447 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003448out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003449 if (ret < 0)
3450 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003451 io_cqring_add_event(req, ret);
3452 io_put_req_find_next(req, nxt);
3453 return 0;
3454#else
3455 return -EOPNOTSUPP;
3456#endif
3457}
3458
Jens Axboe221c5eb2019-01-17 09:41:58 -07003459static void io_poll_remove_one(struct io_kiocb *req)
3460{
3461 struct io_poll_iocb *poll = &req->poll;
3462
3463 spin_lock(&poll->head->lock);
3464 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003465 if (!list_empty(&poll->wait.entry)) {
3466 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003467 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003468 }
3469 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003470 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003471}
3472
3473static void io_poll_remove_all(struct io_ring_ctx *ctx)
3474{
Jens Axboe78076bb2019-12-04 19:56:40 -07003475 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003476 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003477 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003478
3479 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003480 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3481 struct hlist_head *list;
3482
3483 list = &ctx->cancel_hash[i];
3484 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3485 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003486 }
3487 spin_unlock_irq(&ctx->completion_lock);
3488}
3489
Jens Axboe47f46762019-11-09 17:43:02 -07003490static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3491{
Jens Axboe78076bb2019-12-04 19:56:40 -07003492 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003493 struct io_kiocb *req;
3494
Jens Axboe78076bb2019-12-04 19:56:40 -07003495 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3496 hlist_for_each_entry(req, list, hash_node) {
3497 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003498 io_poll_remove_one(req);
3499 return 0;
3500 }
Jens Axboe47f46762019-11-09 17:43:02 -07003501 }
3502
3503 return -ENOENT;
3504}
3505
Jens Axboe3529d8c2019-12-19 18:24:38 -07003506static int io_poll_remove_prep(struct io_kiocb *req,
3507 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003508{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003509 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3510 return -EINVAL;
3511 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3512 sqe->poll_events)
3513 return -EINVAL;
3514
Jens Axboe0969e782019-12-17 18:40:57 -07003515 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003516 return 0;
3517}
3518
3519/*
3520 * Find a running poll command that matches one specified in sqe->addr,
3521 * and remove it if found.
3522 */
3523static int io_poll_remove(struct io_kiocb *req)
3524{
3525 struct io_ring_ctx *ctx = req->ctx;
3526 u64 addr;
3527 int ret;
3528
Jens Axboe0969e782019-12-17 18:40:57 -07003529 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003530 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003531 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003532 spin_unlock_irq(&ctx->completion_lock);
3533
Jens Axboe78e19bb2019-11-06 15:21:34 -07003534 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003535 if (ret < 0)
3536 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003537 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003538 return 0;
3539}
3540
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003541static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003542{
Jackie Liua197f662019-11-08 08:09:12 -07003543 struct io_ring_ctx *ctx = req->ctx;
3544
Jens Axboe8c838782019-03-12 15:48:16 -06003545 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003546 if (error)
3547 io_cqring_fill_event(req, error);
3548 else
3549 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003550 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003551}
3552
Jens Axboe561fb042019-10-24 07:25:42 -06003553static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003554{
Jens Axboe561fb042019-10-24 07:25:42 -06003555 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003556 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3557 struct io_poll_iocb *poll = &req->poll;
3558 struct poll_table_struct pt = { ._key = poll->events };
3559 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003560 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003561 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003562 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003563
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003564 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003565 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003566 ret = -ECANCELED;
3567 } else if (READ_ONCE(poll->canceled)) {
3568 ret = -ECANCELED;
3569 }
Jens Axboe561fb042019-10-24 07:25:42 -06003570
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003571 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003572 mask = vfs_poll(poll->file, &pt) & poll->events;
3573
3574 /*
3575 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3576 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3577 * synchronize with them. In the cancellation case the list_del_init
3578 * itself is not actually needed, but harmless so we keep it in to
3579 * avoid further branches in the fast path.
3580 */
3581 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003582 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003583 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003584 spin_unlock_irq(&ctx->completion_lock);
3585 return;
3586 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003587 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003588 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003589 spin_unlock_irq(&ctx->completion_lock);
3590
Jens Axboe8c838782019-03-12 15:48:16 -06003591 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003592
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003593 if (ret < 0)
3594 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003595 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003596 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003597 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003598}
3599
Jens Axboee94f1412019-12-19 12:06:02 -07003600static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3601{
Jens Axboee94f1412019-12-19 12:06:02 -07003602 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003603 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003604
Jens Axboec6ca97b302019-12-28 12:11:08 -07003605 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003606 spin_lock_irq(&ctx->completion_lock);
3607 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3608 hash_del(&req->hash_node);
3609 io_poll_complete(req, req->result, 0);
3610
Jens Axboe8237e042019-12-28 10:48:22 -07003611 if (refcount_dec_and_test(&req->refs) &&
3612 !io_req_multi_free(&rb, req)) {
3613 req->flags |= REQ_F_COMP_LOCKED;
3614 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003615 }
3616 }
3617 spin_unlock_irq(&ctx->completion_lock);
3618
3619 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003620 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003621}
3622
3623static void io_poll_flush(struct io_wq_work **workptr)
3624{
3625 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3626 struct llist_node *nodes;
3627
3628 nodes = llist_del_all(&req->ctx->poll_llist);
3629 if (nodes)
3630 __io_poll_flush(req->ctx, nodes);
3631}
3632
Jens Axboef0b493e2020-02-01 21:30:11 -07003633static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3634{
3635 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3636
3637 eventfd_signal(req->ctx->cq_ev_fd, 1);
3638 io_put_req(req);
3639}
3640
Jens Axboe221c5eb2019-01-17 09:41:58 -07003641static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3642 void *key)
3643{
Jens Axboee9444752019-11-26 15:02:04 -07003644 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003645 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3646 struct io_ring_ctx *ctx = req->ctx;
3647 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003648
3649 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003650 if (mask && !(mask & poll->events))
3651 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003652
Jens Axboe392edb42019-12-09 17:52:20 -07003653 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003654
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003655 /*
3656 * Run completion inline if we can. We're using trylock here because
3657 * we are violating the completion_lock -> poll wq lock ordering.
3658 * If we have a link timeout we're going to need the completion_lock
3659 * for finalizing the request, mark us as having grabbed that already.
3660 */
Jens Axboee94f1412019-12-19 12:06:02 -07003661 if (mask) {
3662 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003663
Jens Axboee94f1412019-12-19 12:06:02 -07003664 if (llist_empty(&ctx->poll_llist) &&
3665 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003666 bool trigger_ev;
3667
Jens Axboee94f1412019-12-19 12:06:02 -07003668 hash_del(&req->hash_node);
3669 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003670
Jens Axboef0b493e2020-02-01 21:30:11 -07003671 trigger_ev = io_should_trigger_evfd(ctx);
3672 if (trigger_ev && eventfd_signal_count()) {
3673 trigger_ev = false;
3674 req->work.func = io_poll_trigger_evfd;
3675 } else {
3676 req->flags |= REQ_F_COMP_LOCKED;
3677 io_put_req(req);
3678 req = NULL;
3679 }
3680 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3681 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003682 } else {
3683 req->result = mask;
3684 req->llist_node.next = NULL;
3685 /* if the list wasn't empty, we're done */
3686 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3687 req = NULL;
3688 else
3689 req->work.func = io_poll_flush;
3690 }
Jens Axboe8c838782019-03-12 15:48:16 -06003691 }
Jens Axboee94f1412019-12-19 12:06:02 -07003692 if (req)
3693 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003694
Jens Axboe221c5eb2019-01-17 09:41:58 -07003695 return 1;
3696}
3697
3698struct io_poll_table {
3699 struct poll_table_struct pt;
3700 struct io_kiocb *req;
3701 int error;
3702};
3703
3704static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3705 struct poll_table_struct *p)
3706{
3707 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3708
3709 if (unlikely(pt->req->poll.head)) {
3710 pt->error = -EINVAL;
3711 return;
3712 }
3713
3714 pt->error = 0;
3715 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003716 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003717}
3718
Jens Axboeeac406c2019-11-14 12:09:58 -07003719static void io_poll_req_insert(struct io_kiocb *req)
3720{
3721 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003722 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003723
Jens Axboe78076bb2019-12-04 19:56:40 -07003724 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3725 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003726}
3727
Jens Axboe3529d8c2019-12-19 18:24:38 -07003728static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003729{
3730 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003731 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003732
3733 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3734 return -EINVAL;
3735 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3736 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003737 if (!poll->file)
3738 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003739
Jens Axboe221c5eb2019-01-17 09:41:58 -07003740 events = READ_ONCE(sqe->poll_events);
3741 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003742 return 0;
3743}
3744
3745static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3746{
3747 struct io_poll_iocb *poll = &req->poll;
3748 struct io_ring_ctx *ctx = req->ctx;
3749 struct io_poll_table ipt;
3750 bool cancel = false;
3751 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003752
3753 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003754 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003755
Jens Axboe221c5eb2019-01-17 09:41:58 -07003756 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003757 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003758 poll->canceled = false;
3759
3760 ipt.pt._qproc = io_poll_queue_proc;
3761 ipt.pt._key = poll->events;
3762 ipt.req = req;
3763 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3764
3765 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003766 INIT_LIST_HEAD(&poll->wait.entry);
3767 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3768 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003769
Jens Axboe36703242019-07-25 10:20:18 -06003770 INIT_LIST_HEAD(&req->list);
3771
Jens Axboe221c5eb2019-01-17 09:41:58 -07003772 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003773
3774 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003775 if (likely(poll->head)) {
3776 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003777 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003778 if (ipt.error)
3779 cancel = true;
3780 ipt.error = 0;
3781 mask = 0;
3782 }
3783 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003784 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003785 else if (cancel)
3786 WRITE_ONCE(poll->canceled, true);
3787 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003788 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003789 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003790 }
Jens Axboe8c838782019-03-12 15:48:16 -06003791 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003792 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003793 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003794 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003795 spin_unlock_irq(&ctx->completion_lock);
3796
Jens Axboe8c838782019-03-12 15:48:16 -06003797 if (mask) {
3798 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003799 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003800 }
Jens Axboe8c838782019-03-12 15:48:16 -06003801 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003802}
3803
Jens Axboe5262f562019-09-17 12:26:57 -06003804static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3805{
Jens Axboead8a48a2019-11-15 08:49:11 -07003806 struct io_timeout_data *data = container_of(timer,
3807 struct io_timeout_data, timer);
3808 struct io_kiocb *req = data->req;
3809 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003810 unsigned long flags;
3811
Jens Axboe5262f562019-09-17 12:26:57 -06003812 atomic_inc(&ctx->cq_timeouts);
3813
3814 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003815 /*
Jens Axboe11365042019-10-16 09:08:32 -06003816 * We could be racing with timeout deletion. If the list is empty,
3817 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003818 */
Jens Axboe842f9612019-10-29 12:34:10 -06003819 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003820 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003821
Jens Axboe11365042019-10-16 09:08:32 -06003822 /*
3823 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003824 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003825 * pointer will be increased, otherwise other timeout reqs may
3826 * return in advance without waiting for enough wait_nr.
3827 */
3828 prev = req;
3829 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3830 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003831 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003832 }
Jens Axboe842f9612019-10-29 12:34:10 -06003833
Jens Axboe78e19bb2019-11-06 15:21:34 -07003834 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003835 io_commit_cqring(ctx);
3836 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3837
3838 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003839 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003840 io_put_req(req);
3841 return HRTIMER_NORESTART;
3842}
3843
Jens Axboe47f46762019-11-09 17:43:02 -07003844static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3845{
3846 struct io_kiocb *req;
3847 int ret = -ENOENT;
3848
3849 list_for_each_entry(req, &ctx->timeout_list, list) {
3850 if (user_data == req->user_data) {
3851 list_del_init(&req->list);
3852 ret = 0;
3853 break;
3854 }
3855 }
3856
3857 if (ret == -ENOENT)
3858 return ret;
3859
Jens Axboe2d283902019-12-04 11:08:05 -07003860 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003861 if (ret == -1)
3862 return -EALREADY;
3863
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003864 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003865 io_cqring_fill_event(req, -ECANCELED);
3866 io_put_req(req);
3867 return 0;
3868}
3869
Jens Axboe3529d8c2019-12-19 18:24:38 -07003870static int io_timeout_remove_prep(struct io_kiocb *req,
3871 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003872{
Jens Axboeb29472e2019-12-17 18:50:29 -07003873 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3874 return -EINVAL;
3875 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3876 return -EINVAL;
3877
3878 req->timeout.addr = READ_ONCE(sqe->addr);
3879 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3880 if (req->timeout.flags)
3881 return -EINVAL;
3882
Jens Axboeb29472e2019-12-17 18:50:29 -07003883 return 0;
3884}
3885
Jens Axboe11365042019-10-16 09:08:32 -06003886/*
3887 * Remove or update an existing timeout command
3888 */
Jens Axboefc4df992019-12-10 14:38:45 -07003889static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003890{
3891 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003892 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003893
Jens Axboe11365042019-10-16 09:08:32 -06003894 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003895 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003896
Jens Axboe47f46762019-11-09 17:43:02 -07003897 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003898 io_commit_cqring(ctx);
3899 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003900 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003901 if (ret < 0)
3902 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003903 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003904 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003905}
3906
Jens Axboe3529d8c2019-12-19 18:24:38 -07003907static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003908 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003909{
Jens Axboead8a48a2019-11-15 08:49:11 -07003910 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003911 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003912
Jens Axboead8a48a2019-11-15 08:49:11 -07003913 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003914 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003915 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003916 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003917 if (sqe->off && is_timeout_link)
3918 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003919 flags = READ_ONCE(sqe->timeout_flags);
3920 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003921 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003922
Jens Axboe26a61672019-12-20 09:02:01 -07003923 req->timeout.count = READ_ONCE(sqe->off);
3924
Jens Axboe3529d8c2019-12-19 18:24:38 -07003925 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003926 return -ENOMEM;
3927
3928 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003929 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003930 req->flags |= REQ_F_TIMEOUT;
3931
3932 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003933 return -EFAULT;
3934
Jens Axboe11365042019-10-16 09:08:32 -06003935 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003936 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003937 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003938 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003939
Jens Axboead8a48a2019-11-15 08:49:11 -07003940 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3941 return 0;
3942}
3943
Jens Axboefc4df992019-12-10 14:38:45 -07003944static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003945{
3946 unsigned count;
3947 struct io_ring_ctx *ctx = req->ctx;
3948 struct io_timeout_data *data;
3949 struct list_head *entry;
3950 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003951
Jens Axboe2d283902019-12-04 11:08:05 -07003952 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003953
Jens Axboe5262f562019-09-17 12:26:57 -06003954 /*
3955 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003956 * timeout event to be satisfied. If it isn't set, then this is
3957 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003958 */
Jens Axboe26a61672019-12-20 09:02:01 -07003959 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003960 if (!count) {
3961 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3962 spin_lock_irq(&ctx->completion_lock);
3963 entry = ctx->timeout_list.prev;
3964 goto add;
3965 }
Jens Axboe5262f562019-09-17 12:26:57 -06003966
3967 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003968 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003969
3970 /*
3971 * Insertion sort, ensuring the first entry in the list is always
3972 * the one we need first.
3973 */
Jens Axboe5262f562019-09-17 12:26:57 -06003974 spin_lock_irq(&ctx->completion_lock);
3975 list_for_each_prev(entry, &ctx->timeout_list) {
3976 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003977 unsigned nxt_sq_head;
3978 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003979 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003980
Jens Axboe93bd25b2019-11-11 23:34:31 -07003981 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3982 continue;
3983
yangerkun5da0fb12019-10-15 21:59:29 +08003984 /*
3985 * Since cached_sq_head + count - 1 can overflow, use type long
3986 * long to store it.
3987 */
3988 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003989 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3990 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003991
3992 /*
3993 * cached_sq_head may overflow, and it will never overflow twice
3994 * once there is some timeout req still be valid.
3995 */
3996 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003997 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003998
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003999 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004000 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004001
4002 /*
4003 * Sequence of reqs after the insert one and itself should
4004 * be adjusted because each timeout req consumes a slot.
4005 */
4006 span++;
4007 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004008 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004009 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004010add:
Jens Axboe5262f562019-09-17 12:26:57 -06004011 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004012 data->timer.function = io_timeout_fn;
4013 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004014 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004015 return 0;
4016}
4017
Jens Axboe62755e32019-10-28 21:49:21 -06004018static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004019{
Jens Axboe62755e32019-10-28 21:49:21 -06004020 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004021
Jens Axboe62755e32019-10-28 21:49:21 -06004022 return req->user_data == (unsigned long) data;
4023}
4024
Jens Axboee977d6d2019-11-05 12:39:45 -07004025static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004026{
Jens Axboe62755e32019-10-28 21:49:21 -06004027 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004028 int ret = 0;
4029
Jens Axboe62755e32019-10-28 21:49:21 -06004030 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4031 switch (cancel_ret) {
4032 case IO_WQ_CANCEL_OK:
4033 ret = 0;
4034 break;
4035 case IO_WQ_CANCEL_RUNNING:
4036 ret = -EALREADY;
4037 break;
4038 case IO_WQ_CANCEL_NOTFOUND:
4039 ret = -ENOENT;
4040 break;
4041 }
4042
Jens Axboee977d6d2019-11-05 12:39:45 -07004043 return ret;
4044}
4045
Jens Axboe47f46762019-11-09 17:43:02 -07004046static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4047 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004048 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004049{
4050 unsigned long flags;
4051 int ret;
4052
4053 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4054 if (ret != -ENOENT) {
4055 spin_lock_irqsave(&ctx->completion_lock, flags);
4056 goto done;
4057 }
4058
4059 spin_lock_irqsave(&ctx->completion_lock, flags);
4060 ret = io_timeout_cancel(ctx, sqe_addr);
4061 if (ret != -ENOENT)
4062 goto done;
4063 ret = io_poll_cancel(ctx, sqe_addr);
4064done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004065 if (!ret)
4066 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004067 io_cqring_fill_event(req, ret);
4068 io_commit_cqring(ctx);
4069 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4070 io_cqring_ev_posted(ctx);
4071
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004072 if (ret < 0)
4073 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004074 io_put_req_find_next(req, nxt);
4075}
4076
Jens Axboe3529d8c2019-12-19 18:24:38 -07004077static int io_async_cancel_prep(struct io_kiocb *req,
4078 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004079{
Jens Axboefbf23842019-12-17 18:45:56 -07004080 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004081 return -EINVAL;
4082 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4083 sqe->cancel_flags)
4084 return -EINVAL;
4085
Jens Axboefbf23842019-12-17 18:45:56 -07004086 req->cancel.addr = READ_ONCE(sqe->addr);
4087 return 0;
4088}
4089
4090static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4091{
4092 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004093
4094 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004095 return 0;
4096}
4097
Jens Axboe05f3fb32019-12-09 11:22:50 -07004098static int io_files_update_prep(struct io_kiocb *req,
4099 const struct io_uring_sqe *sqe)
4100{
4101 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4102 return -EINVAL;
4103
4104 req->files_update.offset = READ_ONCE(sqe->off);
4105 req->files_update.nr_args = READ_ONCE(sqe->len);
4106 if (!req->files_update.nr_args)
4107 return -EINVAL;
4108 req->files_update.arg = READ_ONCE(sqe->addr);
4109 return 0;
4110}
4111
4112static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4113{
4114 struct io_ring_ctx *ctx = req->ctx;
4115 struct io_uring_files_update up;
4116 int ret;
4117
Jens Axboef86cd202020-01-29 13:46:44 -07004118 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004119 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004120
4121 up.offset = req->files_update.offset;
4122 up.fds = req->files_update.arg;
4123
4124 mutex_lock(&ctx->uring_lock);
4125 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4126 mutex_unlock(&ctx->uring_lock);
4127
4128 if (ret < 0)
4129 req_set_fail_links(req);
4130 io_cqring_add_event(req, ret);
4131 io_put_req(req);
4132 return 0;
4133}
4134
Jens Axboe3529d8c2019-12-19 18:24:38 -07004135static int io_req_defer_prep(struct io_kiocb *req,
4136 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004137{
Jens Axboee7815732019-12-17 19:45:06 -07004138 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004139
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03004140 if (!sqe)
4141 return 0;
4142
Jens Axboef86cd202020-01-29 13:46:44 -07004143 if (io_op_defs[req->opcode].file_table) {
4144 ret = io_grab_files(req);
4145 if (unlikely(ret))
4146 return ret;
4147 }
4148
Jens Axboecccf0ee2020-01-27 16:34:48 -07004149 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4150
Jens Axboed625c6e2019-12-17 19:53:05 -07004151 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004152 case IORING_OP_NOP:
4153 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004154 case IORING_OP_READV:
4155 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004156 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004157 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004158 break;
4159 case IORING_OP_WRITEV:
4160 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004161 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004162 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004163 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004164 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004165 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004166 break;
4167 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004168 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004169 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004170 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004171 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004172 break;
4173 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004174 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004175 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004176 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004177 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004178 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004179 break;
4180 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004181 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004182 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004183 break;
Jens Axboef499a022019-12-02 16:28:46 -07004184 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004185 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004186 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004187 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004188 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004189 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004190 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004191 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004192 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004193 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004194 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004195 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004196 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004197 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004198 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004199 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004200 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004201 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004202 case IORING_OP_FALLOCATE:
4203 ret = io_fallocate_prep(req, sqe);
4204 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004205 case IORING_OP_OPENAT:
4206 ret = io_openat_prep(req, sqe);
4207 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004208 case IORING_OP_CLOSE:
4209 ret = io_close_prep(req, sqe);
4210 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004211 case IORING_OP_FILES_UPDATE:
4212 ret = io_files_update_prep(req, sqe);
4213 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004214 case IORING_OP_STATX:
4215 ret = io_statx_prep(req, sqe);
4216 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004217 case IORING_OP_FADVISE:
4218 ret = io_fadvise_prep(req, sqe);
4219 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004220 case IORING_OP_MADVISE:
4221 ret = io_madvise_prep(req, sqe);
4222 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004223 case IORING_OP_OPENAT2:
4224 ret = io_openat2_prep(req, sqe);
4225 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004226 case IORING_OP_EPOLL_CTL:
4227 ret = io_epoll_ctl_prep(req, sqe);
4228 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004229 default:
Jens Axboee7815732019-12-17 19:45:06 -07004230 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4231 req->opcode);
4232 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004233 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004234 }
4235
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004236 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004237}
4238
Jens Axboe3529d8c2019-12-19 18:24:38 -07004239static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004240{
Jackie Liua197f662019-11-08 08:09:12 -07004241 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004242 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004243
Bob Liu9d858b22019-11-13 18:06:25 +08004244 /* Still need defer if there is pending req in defer list. */
4245 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004246 return 0;
4247
Jens Axboe3529d8c2019-12-19 18:24:38 -07004248 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004249 return -EAGAIN;
4250
Jens Axboe3529d8c2019-12-19 18:24:38 -07004251 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004252 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004253 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004254
Jens Axboede0617e2019-04-06 21:51:27 -06004255 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004256 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004257 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004258 return 0;
4259 }
4260
Jens Axboe915967f2019-11-21 09:01:20 -07004261 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004262 list_add_tail(&req->list, &ctx->defer_list);
4263 spin_unlock_irq(&ctx->completion_lock);
4264 return -EIOCBQUEUED;
4265}
4266
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004267static void io_cleanup_req(struct io_kiocb *req)
4268{
4269 struct io_async_ctx *io = req->io;
4270
4271 switch (req->opcode) {
4272 case IORING_OP_READV:
4273 case IORING_OP_READ_FIXED:
4274 case IORING_OP_READ:
4275 case IORING_OP_WRITEV:
4276 case IORING_OP_WRITE_FIXED:
4277 case IORING_OP_WRITE:
4278 if (io->rw.iov != io->rw.fast_iov)
4279 kfree(io->rw.iov);
4280 break;
4281 case IORING_OP_SENDMSG:
4282 case IORING_OP_RECVMSG:
4283 if (io->msg.iov != io->msg.fast_iov)
4284 kfree(io->msg.iov);
4285 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004286 case IORING_OP_OPENAT:
4287 case IORING_OP_OPENAT2:
4288 case IORING_OP_STATX:
4289 putname(req->open.filename);
4290 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004291 }
4292
4293 req->flags &= ~REQ_F_NEED_CLEANUP;
4294}
4295
Jens Axboe3529d8c2019-12-19 18:24:38 -07004296static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4297 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004298{
Jackie Liua197f662019-11-08 08:09:12 -07004299 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004300 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004301
Jens Axboed625c6e2019-12-17 19:53:05 -07004302 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004303 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004304 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004305 break;
4306 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004307 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004308 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004309 if (sqe) {
4310 ret = io_read_prep(req, sqe, force_nonblock);
4311 if (ret < 0)
4312 break;
4313 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004314 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004315 break;
4316 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004317 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004318 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004319 if (sqe) {
4320 ret = io_write_prep(req, sqe, force_nonblock);
4321 if (ret < 0)
4322 break;
4323 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004324 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004325 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004326 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004327 if (sqe) {
4328 ret = io_prep_fsync(req, sqe);
4329 if (ret < 0)
4330 break;
4331 }
Jens Axboefc4df992019-12-10 14:38:45 -07004332 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004333 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004334 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004335 if (sqe) {
4336 ret = io_poll_add_prep(req, sqe);
4337 if (ret)
4338 break;
4339 }
Jens Axboefc4df992019-12-10 14:38:45 -07004340 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004341 break;
4342 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004343 if (sqe) {
4344 ret = io_poll_remove_prep(req, sqe);
4345 if (ret < 0)
4346 break;
4347 }
Jens Axboefc4df992019-12-10 14:38:45 -07004348 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004349 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004350 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004351 if (sqe) {
4352 ret = io_prep_sfr(req, sqe);
4353 if (ret < 0)
4354 break;
4355 }
Jens Axboefc4df992019-12-10 14:38:45 -07004356 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004357 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004358 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004359 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004360 if (sqe) {
4361 ret = io_sendmsg_prep(req, sqe);
4362 if (ret < 0)
4363 break;
4364 }
Jens Axboefddafac2020-01-04 20:19:44 -07004365 if (req->opcode == IORING_OP_SENDMSG)
4366 ret = io_sendmsg(req, nxt, force_nonblock);
4367 else
4368 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004369 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004370 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004371 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004372 if (sqe) {
4373 ret = io_recvmsg_prep(req, sqe);
4374 if (ret)
4375 break;
4376 }
Jens Axboefddafac2020-01-04 20:19:44 -07004377 if (req->opcode == IORING_OP_RECVMSG)
4378 ret = io_recvmsg(req, nxt, force_nonblock);
4379 else
4380 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004381 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004382 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004383 if (sqe) {
4384 ret = io_timeout_prep(req, sqe, false);
4385 if (ret)
4386 break;
4387 }
Jens Axboefc4df992019-12-10 14:38:45 -07004388 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004389 break;
Jens Axboe11365042019-10-16 09:08:32 -06004390 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004391 if (sqe) {
4392 ret = io_timeout_remove_prep(req, sqe);
4393 if (ret)
4394 break;
4395 }
Jens Axboefc4df992019-12-10 14:38:45 -07004396 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004397 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004398 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004399 if (sqe) {
4400 ret = io_accept_prep(req, sqe);
4401 if (ret)
4402 break;
4403 }
Jens Axboefc4df992019-12-10 14:38:45 -07004404 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004405 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004406 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004407 if (sqe) {
4408 ret = io_connect_prep(req, sqe);
4409 if (ret)
4410 break;
4411 }
Jens Axboefc4df992019-12-10 14:38:45 -07004412 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004413 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004414 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004415 if (sqe) {
4416 ret = io_async_cancel_prep(req, sqe);
4417 if (ret)
4418 break;
4419 }
Jens Axboefc4df992019-12-10 14:38:45 -07004420 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004421 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004422 case IORING_OP_FALLOCATE:
4423 if (sqe) {
4424 ret = io_fallocate_prep(req, sqe);
4425 if (ret)
4426 break;
4427 }
4428 ret = io_fallocate(req, nxt, force_nonblock);
4429 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004430 case IORING_OP_OPENAT:
4431 if (sqe) {
4432 ret = io_openat_prep(req, sqe);
4433 if (ret)
4434 break;
4435 }
4436 ret = io_openat(req, nxt, force_nonblock);
4437 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004438 case IORING_OP_CLOSE:
4439 if (sqe) {
4440 ret = io_close_prep(req, sqe);
4441 if (ret)
4442 break;
4443 }
4444 ret = io_close(req, nxt, force_nonblock);
4445 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004446 case IORING_OP_FILES_UPDATE:
4447 if (sqe) {
4448 ret = io_files_update_prep(req, sqe);
4449 if (ret)
4450 break;
4451 }
4452 ret = io_files_update(req, force_nonblock);
4453 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004454 case IORING_OP_STATX:
4455 if (sqe) {
4456 ret = io_statx_prep(req, sqe);
4457 if (ret)
4458 break;
4459 }
4460 ret = io_statx(req, nxt, force_nonblock);
4461 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004462 case IORING_OP_FADVISE:
4463 if (sqe) {
4464 ret = io_fadvise_prep(req, sqe);
4465 if (ret)
4466 break;
4467 }
4468 ret = io_fadvise(req, nxt, force_nonblock);
4469 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004470 case IORING_OP_MADVISE:
4471 if (sqe) {
4472 ret = io_madvise_prep(req, sqe);
4473 if (ret)
4474 break;
4475 }
4476 ret = io_madvise(req, nxt, force_nonblock);
4477 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004478 case IORING_OP_OPENAT2:
4479 if (sqe) {
4480 ret = io_openat2_prep(req, sqe);
4481 if (ret)
4482 break;
4483 }
4484 ret = io_openat2(req, nxt, force_nonblock);
4485 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004486 case IORING_OP_EPOLL_CTL:
4487 if (sqe) {
4488 ret = io_epoll_ctl_prep(req, sqe);
4489 if (ret)
4490 break;
4491 }
4492 ret = io_epoll_ctl(req, nxt, force_nonblock);
4493 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004494 default:
4495 ret = -EINVAL;
4496 break;
4497 }
4498
Jens Axboedef596e2019-01-09 08:59:42 -07004499 if (ret)
4500 return ret;
4501
4502 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004503 const bool in_async = io_wq_current_is_worker();
4504
Jens Axboe9e645e112019-05-10 16:07:28 -06004505 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004506 return -EAGAIN;
4507
Jens Axboe11ba8202020-01-15 21:51:17 -07004508 /* workqueue context doesn't hold uring_lock, grab it now */
4509 if (in_async)
4510 mutex_lock(&ctx->uring_lock);
4511
Jens Axboedef596e2019-01-09 08:59:42 -07004512 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004513
4514 if (in_async)
4515 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004516 }
4517
4518 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004519}
4520
Jens Axboe561fb042019-10-24 07:25:42 -06004521static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004522{
Jens Axboe561fb042019-10-24 07:25:42 -06004523 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004524 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004525 struct io_kiocb *nxt = NULL;
4526 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004527
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004528 /* if NO_CANCEL is set, we must still run the work */
4529 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4530 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004531 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004532 }
Jens Axboe31b51512019-01-18 22:56:34 -07004533
Jens Axboe561fb042019-10-24 07:25:42 -06004534 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004535 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004536 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004537 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004538 /*
4539 * We can get EAGAIN for polled IO even though we're
4540 * forcing a sync submission from here, since we can't
4541 * wait for request slots on the block side.
4542 */
4543 if (ret != -EAGAIN)
4544 break;
4545 cond_resched();
4546 } while (1);
4547 }
Jens Axboe31b51512019-01-18 22:56:34 -07004548
Jens Axboe561fb042019-10-24 07:25:42 -06004549 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004550 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004551
Jens Axboe561fb042019-10-24 07:25:42 -06004552 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004553 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004554 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004555 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004556 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004557
Jens Axboe561fb042019-10-24 07:25:42 -06004558 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004559 if (!ret && nxt)
4560 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004561}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004562
Jens Axboe15b71ab2019-12-11 11:20:36 -07004563static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004564{
Jens Axboed3656342019-12-18 09:50:26 -07004565 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004566 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004567 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004568 return 0;
4569 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004570}
4571
Jens Axboe65e19f52019-10-26 07:20:21 -06004572static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4573 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004574{
Jens Axboe65e19f52019-10-26 07:20:21 -06004575 struct fixed_file_table *table;
4576
Jens Axboe05f3fb32019-12-09 11:22:50 -07004577 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4578 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004579}
4580
Jens Axboe3529d8c2019-12-19 18:24:38 -07004581static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4582 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004583{
Jackie Liua197f662019-11-08 08:09:12 -07004584 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004585 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004586 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004587
Jens Axboe3529d8c2019-12-19 18:24:38 -07004588 flags = READ_ONCE(sqe->flags);
4589 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004590
Jens Axboed3656342019-12-18 09:50:26 -07004591 if (!io_req_needs_file(req, fd))
4592 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004593
4594 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004595 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004596 (unsigned) fd >= ctx->nr_user_files))
4597 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004598 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004599 req->file = io_file_from_index(ctx, fd);
4600 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004601 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004602 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004603 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004604 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004605 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004606 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004607 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004608 req->file = io_file_get(state, fd);
4609 if (unlikely(!req->file))
4610 return -EBADF;
4611 }
4612
4613 return 0;
4614}
4615
Jackie Liua197f662019-11-08 08:09:12 -07004616static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004617{
Jens Axboefcb323c2019-10-24 12:39:47 -06004618 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004619 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004620
Jens Axboef86cd202020-01-29 13:46:44 -07004621 if (req->work.files)
4622 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004623 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004624 return -EBADF;
4625
Jens Axboefcb323c2019-10-24 12:39:47 -06004626 rcu_read_lock();
4627 spin_lock_irq(&ctx->inflight_lock);
4628 /*
4629 * We use the f_ops->flush() handler to ensure that we can flush
4630 * out work accessing these files if the fd is closed. Check if
4631 * the fd has changed since we started down this path, and disallow
4632 * this operation if it has.
4633 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004634 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004635 list_add(&req->inflight_entry, &ctx->inflight_list);
4636 req->flags |= REQ_F_INFLIGHT;
4637 req->work.files = current->files;
4638 ret = 0;
4639 }
4640 spin_unlock_irq(&ctx->inflight_lock);
4641 rcu_read_unlock();
4642
4643 return ret;
4644}
4645
Jens Axboe2665abf2019-11-05 12:40:47 -07004646static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4647{
Jens Axboead8a48a2019-11-15 08:49:11 -07004648 struct io_timeout_data *data = container_of(timer,
4649 struct io_timeout_data, timer);
4650 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004651 struct io_ring_ctx *ctx = req->ctx;
4652 struct io_kiocb *prev = NULL;
4653 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004654
4655 spin_lock_irqsave(&ctx->completion_lock, flags);
4656
4657 /*
4658 * We don't expect the list to be empty, that will only happen if we
4659 * race with the completion of the linked work.
4660 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004661 if (!list_empty(&req->link_list)) {
4662 prev = list_entry(req->link_list.prev, struct io_kiocb,
4663 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004664 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004665 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004666 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4667 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004668 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004669 }
4670
4671 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4672
4673 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004674 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004675 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4676 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004677 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004678 } else {
4679 io_cqring_add_event(req, -ETIME);
4680 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004681 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004682 return HRTIMER_NORESTART;
4683}
4684
Jens Axboead8a48a2019-11-15 08:49:11 -07004685static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004686{
Jens Axboe76a46e02019-11-10 23:34:16 -07004687 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004688
Jens Axboe76a46e02019-11-10 23:34:16 -07004689 /*
4690 * If the list is now empty, then our linked request finished before
4691 * we got a chance to setup the timer
4692 */
4693 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004694 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004695 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004696
Jens Axboead8a48a2019-11-15 08:49:11 -07004697 data->timer.function = io_link_timeout_fn;
4698 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4699 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004700 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004701 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004702
Jens Axboe2665abf2019-11-05 12:40:47 -07004703 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004704 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004705}
4706
Jens Axboead8a48a2019-11-15 08:49:11 -07004707static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004708{
4709 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004710
Jens Axboe2665abf2019-11-05 12:40:47 -07004711 if (!(req->flags & REQ_F_LINK))
4712 return NULL;
4713
Pavel Begunkov44932332019-12-05 16:16:35 +03004714 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4715 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004716 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004717 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004718
Jens Axboe76a46e02019-11-10 23:34:16 -07004719 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004720 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004721}
4722
Jens Axboe3529d8c2019-12-19 18:24:38 -07004723static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004724{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004725 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004726 struct io_kiocb *nxt = NULL;
Jens Axboe193155c2020-02-22 23:22:19 -07004727 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004728 int ret;
4729
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004730again:
4731 linked_timeout = io_prep_linked_timeout(req);
4732
Jens Axboe193155c2020-02-22 23:22:19 -07004733 if (req->work.creds && req->work.creds != current_cred()) {
4734 if (old_creds)
4735 revert_creds(old_creds);
4736 if (old_creds == req->work.creds)
4737 old_creds = NULL; /* restored original creds */
4738 else
4739 old_creds = override_creds(req->work.creds);
4740 }
4741
Jens Axboe3529d8c2019-12-19 18:24:38 -07004742 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004743
4744 /*
4745 * We async punt it if the file wasn't marked NOWAIT, or if the file
4746 * doesn't support non-blocking read/write attempts
4747 */
4748 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4749 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004750punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004751 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004752 ret = io_grab_files(req);
4753 if (ret)
4754 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004755 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004756
4757 /*
4758 * Queued up for async execution, worker will release
4759 * submit reference when the iocb is actually submitted.
4760 */
4761 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004762 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004763 }
Jens Axboee65ef562019-03-12 10:16:44 -06004764
Jens Axboefcb323c2019-10-24 12:39:47 -06004765err:
Jens Axboee65ef562019-03-12 10:16:44 -06004766 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004767 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004768
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004769 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004770 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004771 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004772 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004773 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004774 }
4775
Jens Axboee65ef562019-03-12 10:16:44 -06004776 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004777 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004778 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004779 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004780 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004781 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004782done_req:
4783 if (nxt) {
4784 req = nxt;
4785 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004786
4787 if (req->flags & REQ_F_FORCE_ASYNC)
4788 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004789 goto again;
4790 }
Jens Axboe193155c2020-02-22 23:22:19 -07004791 if (old_creds)
4792 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004793}
4794
Jens Axboe3529d8c2019-12-19 18:24:38 -07004795static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004796{
4797 int ret;
4798
Jens Axboe3529d8c2019-12-19 18:24:38 -07004799 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004800 if (ret) {
4801 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004802fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004803 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004804 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004805 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004806 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004807 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004808 ret = io_req_defer_prep(req, sqe);
4809 if (unlikely(ret < 0))
4810 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004811 /*
4812 * Never try inline submit of IOSQE_ASYNC is set, go straight
4813 * to async execution.
4814 */
4815 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4816 io_queue_async_work(req);
4817 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004818 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004819 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004820}
4821
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004822static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004823{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004824 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004825 io_cqring_add_event(req, -ECANCELED);
4826 io_double_put_req(req);
4827 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004828 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004829}
4830
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004831#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004832 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004833
Jens Axboe3529d8c2019-12-19 18:24:38 -07004834static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4835 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004836{
Jackie Liua197f662019-11-08 08:09:12 -07004837 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004838 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004839 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004840
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004841 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004842
4843 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004844 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004845 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004846 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004847 }
4848
Jens Axboe75c6a032020-01-28 10:15:23 -07004849 id = READ_ONCE(sqe->personality);
4850 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07004851 req->work.creds = idr_find(&ctx->personality_idr, id);
4852 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07004853 ret = -EINVAL;
4854 goto err_req;
4855 }
Jens Axboe193155c2020-02-22 23:22:19 -07004856 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07004857 }
4858
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004859 /* same numerical values with corresponding REQ_F_*, safe to copy */
4860 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4861 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004862
Jens Axboe3529d8c2019-12-19 18:24:38 -07004863 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004864 if (unlikely(ret)) {
4865err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004866 io_cqring_add_event(req, ret);
4867 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004868 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004869 }
4870
Jens Axboe9e645e112019-05-10 16:07:28 -06004871 /*
4872 * If we already have a head request, queue this one for async
4873 * submittal once the head completes. If we don't have a head but
4874 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4875 * submitted sync once the chain is complete. If none of those
4876 * conditions are true (normal request), then just queue it.
4877 */
4878 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004879 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004880
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004881 /*
4882 * Taking sequential execution of a link, draining both sides
4883 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4884 * requests in the link. So, it drains the head and the
4885 * next after the link request. The last one is done via
4886 * drain_next flag to persist the effect across calls.
4887 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004888 if (sqe_flags & IOSQE_IO_DRAIN) {
4889 head->flags |= REQ_F_IO_DRAIN;
4890 ctx->drain_next = 1;
4891 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004892 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004893 ret = -EAGAIN;
4894 goto err_req;
4895 }
4896
Jens Axboe3529d8c2019-12-19 18:24:38 -07004897 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004898 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004899 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004900 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004901 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004902 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004903 trace_io_uring_link(ctx, req, head);
4904 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004905
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004906 /* last request of a link, enqueue the link */
4907 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4908 io_queue_link_head(head);
4909 *link = NULL;
4910 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004911 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004912 if (unlikely(ctx->drain_next)) {
4913 req->flags |= REQ_F_IO_DRAIN;
4914 req->ctx->drain_next = 0;
4915 }
4916 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4917 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004918 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03004919
4920 if (io_alloc_async_ctx(req)) {
4921 ret = -EAGAIN;
4922 goto err_req;
4923 }
Pavel Begunkov711be032020-01-17 03:57:59 +03004924 ret = io_req_defer_prep(req, sqe);
4925 if (ret)
4926 req->flags |= REQ_F_FAIL_LINK;
4927 *link = req;
4928 } else {
4929 io_queue_sqe(req, sqe);
4930 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004931 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004932
4933 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004934}
4935
Jens Axboe9a56a232019-01-09 09:06:50 -07004936/*
4937 * Batched submission is done, ensure local IO is flushed out.
4938 */
4939static void io_submit_state_end(struct io_submit_state *state)
4940{
4941 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004942 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004943 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004944 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004945}
4946
4947/*
4948 * Start submission side cache.
4949 */
4950static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004951 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004952{
4953 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004954 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004955 state->file = NULL;
4956 state->ios_left = max_ios;
4957}
4958
Jens Axboe2b188cc2019-01-07 10:46:33 -07004959static void io_commit_sqring(struct io_ring_ctx *ctx)
4960{
Hristo Venev75b28af2019-08-26 17:23:46 +00004961 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004962
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004963 /*
4964 * Ensure any loads from the SQEs are done at this point,
4965 * since once we write the new head, the application could
4966 * write new data to them.
4967 */
4968 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004969}
4970
4971/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004972 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004973 * that is mapped by userspace. This means that care needs to be taken to
4974 * ensure that reads are stable, as we cannot rely on userspace always
4975 * being a good citizen. If members of the sqe are validated and then later
4976 * used, it's important that those reads are done through READ_ONCE() to
4977 * prevent a re-load down the line.
4978 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004979static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4980 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004981{
Hristo Venev75b28af2019-08-26 17:23:46 +00004982 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004983 unsigned head;
4984
4985 /*
4986 * The cached sq head (or cq tail) serves two purposes:
4987 *
4988 * 1) allows us to batch the cost of updating the user visible
4989 * head updates.
4990 * 2) allows the kernel side to track the head on its own, even
4991 * though the application is the one updating it.
4992 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004993 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004994 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004995 /*
4996 * All io need record the previous position, if LINK vs DARIN,
4997 * it can be used to mark the position of the first IO in the
4998 * link list.
4999 */
5000 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005001 *sqe_ptr = &ctx->sq_sqes[head];
5002 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5003 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005004 ctx->cached_sq_head++;
5005 return true;
5006 }
5007
5008 /* drop invalid entries */
5009 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005010 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005011 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005012 return false;
5013}
5014
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005015static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005016 struct file *ring_file, int ring_fd,
5017 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005018{
5019 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005020 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005021 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005022 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005023
Jens Axboec4a2ed72019-11-21 21:01:26 -07005024 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005025 if (test_bit(0, &ctx->sq_check_overflow)) {
5026 if (!list_empty(&ctx->cq_overflow_list) &&
5027 !io_cqring_overflow_flush(ctx, false))
5028 return -EBUSY;
5029 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005030
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005031 /* make sure SQ entry isn't read before tail */
5032 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005033
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005034 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5035 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005036
5037 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005038 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005039 statep = &state;
5040 }
5041
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005042 ctx->ring_fd = ring_fd;
5043 ctx->ring_file = ring_file;
5044
Jens Axboe6c271ce2019-01-10 11:22:30 -07005045 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005046 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005047 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005048 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005049
Pavel Begunkov196be952019-11-07 01:41:06 +03005050 req = io_get_req(ctx, statep);
5051 if (unlikely(!req)) {
5052 if (!submitted)
5053 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005054 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005055 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005056 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005057 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005058 break;
5059 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005060
Jens Axboed3656342019-12-18 09:50:26 -07005061 /* will complete beyond this point, count as submitted */
5062 submitted++;
5063
5064 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005065 err = -EINVAL;
5066fail_req:
5067 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005068 io_double_put_req(req);
5069 break;
5070 }
5071
5072 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005073 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005074 if (unlikely(mm_fault)) {
5075 err = -EFAULT;
5076 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005077 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005078 use_mm(ctx->sqo_mm);
5079 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005080 }
5081
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005082 req->in_async = async;
5083 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005084 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5085 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005086 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005087 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005088 }
5089
Pavel Begunkov9466f432020-01-25 22:34:01 +03005090 if (unlikely(submitted != nr)) {
5091 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5092
5093 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5094 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005095 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005096 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005097 if (statep)
5098 io_submit_state_end(&state);
5099
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005100 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5101 io_commit_sqring(ctx);
5102
Jens Axboe6c271ce2019-01-10 11:22:30 -07005103 return submitted;
5104}
5105
5106static int io_sq_thread(void *data)
5107{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005108 struct io_ring_ctx *ctx = data;
5109 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005110 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005111 mm_segment_t old_fs;
5112 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005113 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005114 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005115
Jens Axboe206aefd2019-11-07 18:27:42 -07005116 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005117
Jens Axboe6c271ce2019-01-10 11:22:30 -07005118 old_fs = get_fs();
5119 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005120 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005121
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005122 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005123 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005124 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005125
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005126 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005127 unsigned nr_events = 0;
5128
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005129 mutex_lock(&ctx->uring_lock);
5130 if (!list_empty(&ctx->poll_list))
5131 io_iopoll_getevents(ctx, &nr_events, 0);
5132 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005133 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005134 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005135 }
5136
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005137 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005138
5139 /*
5140 * If submit got -EBUSY, flag us as needing the application
5141 * to enter the kernel to reap and flush events.
5142 */
5143 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005144 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005145 * Drop cur_mm before scheduling, we can't hold it for
5146 * long periods (or over schedule()). Do this before
5147 * adding ourselves to the waitqueue, as the unuse/drop
5148 * may sleep.
5149 */
5150 if (cur_mm) {
5151 unuse_mm(cur_mm);
5152 mmput(cur_mm);
5153 cur_mm = NULL;
5154 }
5155
5156 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005157 * We're polling. If we're within the defined idle
5158 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005159 * to sleep. The exception is if we got EBUSY doing
5160 * more IO, we should wait for the application to
5161 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005162 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005163 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005164 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5165 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005166 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005167 continue;
5168 }
5169
Jens Axboe6c271ce2019-01-10 11:22:30 -07005170 prepare_to_wait(&ctx->sqo_wait, &wait,
5171 TASK_INTERRUPTIBLE);
5172
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005173 /*
5174 * While doing polled IO, before going to sleep, we need
5175 * to check if there are new reqs added to poll_list, it
5176 * is because reqs may have been punted to io worker and
5177 * will be added to poll_list later, hence check the
5178 * poll_list again.
5179 */
5180 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5181 !list_empty_careful(&ctx->poll_list)) {
5182 finish_wait(&ctx->sqo_wait, &wait);
5183 continue;
5184 }
5185
Jens Axboe6c271ce2019-01-10 11:22:30 -07005186 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005187 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005188 /* make sure to read SQ tail after writing flags */
5189 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005190
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005191 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005192 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005193 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005194 finish_wait(&ctx->sqo_wait, &wait);
5195 break;
5196 }
5197 if (signal_pending(current))
5198 flush_signals(current);
5199 schedule();
5200 finish_wait(&ctx->sqo_wait, &wait);
5201
Hristo Venev75b28af2019-08-26 17:23:46 +00005202 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005203 continue;
5204 }
5205 finish_wait(&ctx->sqo_wait, &wait);
5206
Hristo Venev75b28af2019-08-26 17:23:46 +00005207 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005208 }
5209
Jens Axboe8a4955f2019-12-09 14:52:35 -07005210 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005211 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005212 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005213 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005214 }
5215
5216 set_fs(old_fs);
5217 if (cur_mm) {
5218 unuse_mm(cur_mm);
5219 mmput(cur_mm);
5220 }
Jens Axboe181e4482019-11-25 08:52:30 -07005221 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005222
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005223 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005224
Jens Axboe6c271ce2019-01-10 11:22:30 -07005225 return 0;
5226}
5227
Jens Axboebda52162019-09-24 13:47:15 -06005228struct io_wait_queue {
5229 struct wait_queue_entry wq;
5230 struct io_ring_ctx *ctx;
5231 unsigned to_wait;
5232 unsigned nr_timeouts;
5233};
5234
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005235static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005236{
5237 struct io_ring_ctx *ctx = iowq->ctx;
5238
5239 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005240 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005241 * started waiting. For timeouts, we always want to return to userspace,
5242 * regardless of event count.
5243 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005244 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005245 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5246}
5247
5248static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5249 int wake_flags, void *key)
5250{
5251 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5252 wq);
5253
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005254 /* use noflush == true, as we can't safely rely on locking context */
5255 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005256 return -1;
5257
5258 return autoremove_wake_function(curr, mode, wake_flags, key);
5259}
5260
Jens Axboe2b188cc2019-01-07 10:46:33 -07005261/*
5262 * Wait until events become available, if we don't already have some. The
5263 * application must reap them itself, as they reside on the shared cq ring.
5264 */
5265static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5266 const sigset_t __user *sig, size_t sigsz)
5267{
Jens Axboebda52162019-09-24 13:47:15 -06005268 struct io_wait_queue iowq = {
5269 .wq = {
5270 .private = current,
5271 .func = io_wake_function,
5272 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5273 },
5274 .ctx = ctx,
5275 .to_wait = min_events,
5276 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005277 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005278 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005279
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005280 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005281 return 0;
5282
5283 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005284#ifdef CONFIG_COMPAT
5285 if (in_compat_syscall())
5286 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005287 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005288 else
5289#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005290 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005291
Jens Axboe2b188cc2019-01-07 10:46:33 -07005292 if (ret)
5293 return ret;
5294 }
5295
Jens Axboebda52162019-09-24 13:47:15 -06005296 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005297 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005298 do {
5299 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5300 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005301 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005302 break;
5303 schedule();
5304 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005305 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005306 break;
5307 }
5308 } while (1);
5309 finish_wait(&ctx->wait, &iowq.wq);
5310
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005311 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005312
Hristo Venev75b28af2019-08-26 17:23:46 +00005313 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005314}
5315
Jens Axboe6b063142019-01-10 22:13:58 -07005316static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5317{
5318#if defined(CONFIG_UNIX)
5319 if (ctx->ring_sock) {
5320 struct sock *sock = ctx->ring_sock->sk;
5321 struct sk_buff *skb;
5322
5323 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5324 kfree_skb(skb);
5325 }
5326#else
5327 int i;
5328
Jens Axboe65e19f52019-10-26 07:20:21 -06005329 for (i = 0; i < ctx->nr_user_files; i++) {
5330 struct file *file;
5331
5332 file = io_file_from_index(ctx, i);
5333 if (file)
5334 fput(file);
5335 }
Jens Axboe6b063142019-01-10 22:13:58 -07005336#endif
5337}
5338
Jens Axboe05f3fb32019-12-09 11:22:50 -07005339static void io_file_ref_kill(struct percpu_ref *ref)
5340{
5341 struct fixed_file_data *data;
5342
5343 data = container_of(ref, struct fixed_file_data, refs);
5344 complete(&data->done);
5345}
5346
Jens Axboe805b13a2020-03-08 20:07:28 -06005347static void io_file_ref_exit_and_free(struct work_struct *work)
Jens Axboec1e21482020-03-04 07:25:50 -07005348{
Jens Axboe805b13a2020-03-08 20:07:28 -06005349 struct fixed_file_data *data;
5350
5351 data = container_of(work, struct fixed_file_data, ref_work);
5352
5353 /*
5354 * Ensure any percpu-ref atomic switch callback has run, it could have
5355 * been in progress when the files were being unregistered. Once
5356 * that's done, we can safely exit and free the ref and containing
5357 * data structure.
5358 */
5359 rcu_barrier();
Jens Axboec1e21482020-03-04 07:25:50 -07005360 percpu_ref_exit(&data->refs);
5361 kfree(data);
5362}
5363
Jens Axboe6b063142019-01-10 22:13:58 -07005364static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5365{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005366 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005367 unsigned nr_tables, i;
5368
Jens Axboe05f3fb32019-12-09 11:22:50 -07005369 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005370 return -ENXIO;
5371
Jens Axboe05f3fb32019-12-09 11:22:50 -07005372 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005373 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005374 wait_for_completion(&data->done);
5375 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005376
Jens Axboe6b063142019-01-10 22:13:58 -07005377 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005378 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5379 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005380 kfree(data->table[i].files);
5381 kfree(data->table);
Jens Axboe805b13a2020-03-08 20:07:28 -06005382 INIT_WORK(&data->ref_work, io_file_ref_exit_and_free);
5383 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005384 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005385 ctx->nr_user_files = 0;
5386 return 0;
5387}
5388
Jens Axboe6c271ce2019-01-10 11:22:30 -07005389static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5390{
5391 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005392 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005393 /*
5394 * The park is a bit of a work-around, without it we get
5395 * warning spews on shutdown with SQPOLL set and affinity
5396 * set to a single CPU.
5397 */
Jens Axboe06058632019-04-13 09:26:03 -06005398 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005399 kthread_stop(ctx->sqo_thread);
5400 ctx->sqo_thread = NULL;
5401 }
5402}
5403
Jens Axboe6b063142019-01-10 22:13:58 -07005404static void io_finish_async(struct io_ring_ctx *ctx)
5405{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005406 io_sq_thread_stop(ctx);
5407
Jens Axboe561fb042019-10-24 07:25:42 -06005408 if (ctx->io_wq) {
5409 io_wq_destroy(ctx->io_wq);
5410 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005411 }
5412}
5413
5414#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005415/*
5416 * Ensure the UNIX gc is aware of our file set, so we are certain that
5417 * the io_uring can be safely unregistered on process exit, even if we have
5418 * loops in the file referencing.
5419 */
5420static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5421{
5422 struct sock *sk = ctx->ring_sock->sk;
5423 struct scm_fp_list *fpl;
5424 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005425 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005426
5427 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5428 unsigned long inflight = ctx->user->unix_inflight + nr;
5429
5430 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5431 return -EMFILE;
5432 }
5433
5434 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5435 if (!fpl)
5436 return -ENOMEM;
5437
5438 skb = alloc_skb(0, GFP_KERNEL);
5439 if (!skb) {
5440 kfree(fpl);
5441 return -ENOMEM;
5442 }
5443
5444 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005445
Jens Axboe08a45172019-10-03 08:11:03 -06005446 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005447 fpl->user = get_uid(ctx->user);
5448 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005449 struct file *file = io_file_from_index(ctx, i + offset);
5450
5451 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005452 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005453 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005454 unix_inflight(fpl->user, fpl->fp[nr_files]);
5455 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005456 }
5457
Jens Axboe08a45172019-10-03 08:11:03 -06005458 if (nr_files) {
5459 fpl->max = SCM_MAX_FD;
5460 fpl->count = nr_files;
5461 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005462 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005463 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5464 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005465
Jens Axboe08a45172019-10-03 08:11:03 -06005466 for (i = 0; i < nr_files; i++)
5467 fput(fpl->fp[i]);
5468 } else {
5469 kfree_skb(skb);
5470 kfree(fpl);
5471 }
Jens Axboe6b063142019-01-10 22:13:58 -07005472
5473 return 0;
5474}
5475
5476/*
5477 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5478 * causes regular reference counting to break down. We rely on the UNIX
5479 * garbage collection to take care of this problem for us.
5480 */
5481static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5482{
5483 unsigned left, total;
5484 int ret = 0;
5485
5486 total = 0;
5487 left = ctx->nr_user_files;
5488 while (left) {
5489 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005490
5491 ret = __io_sqe_files_scm(ctx, this_files, total);
5492 if (ret)
5493 break;
5494 left -= this_files;
5495 total += this_files;
5496 }
5497
5498 if (!ret)
5499 return 0;
5500
5501 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005502 struct file *file = io_file_from_index(ctx, total);
5503
5504 if (file)
5505 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005506 total++;
5507 }
5508
5509 return ret;
5510}
5511#else
5512static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5513{
5514 return 0;
5515}
5516#endif
5517
Jens Axboe65e19f52019-10-26 07:20:21 -06005518static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5519 unsigned nr_files)
5520{
5521 int i;
5522
5523 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005524 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005525 unsigned this_files;
5526
5527 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5528 table->files = kcalloc(this_files, sizeof(struct file *),
5529 GFP_KERNEL);
5530 if (!table->files)
5531 break;
5532 nr_files -= this_files;
5533 }
5534
5535 if (i == nr_tables)
5536 return 0;
5537
5538 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005539 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005540 kfree(table->files);
5541 }
5542 return 1;
5543}
5544
Jens Axboe05f3fb32019-12-09 11:22:50 -07005545static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005546{
5547#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005548 struct sock *sock = ctx->ring_sock->sk;
5549 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5550 struct sk_buff *skb;
5551 int i;
5552
5553 __skb_queue_head_init(&list);
5554
5555 /*
5556 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5557 * remove this entry and rearrange the file array.
5558 */
5559 skb = skb_dequeue(head);
5560 while (skb) {
5561 struct scm_fp_list *fp;
5562
5563 fp = UNIXCB(skb).fp;
5564 for (i = 0; i < fp->count; i++) {
5565 int left;
5566
5567 if (fp->fp[i] != file)
5568 continue;
5569
5570 unix_notinflight(fp->user, fp->fp[i]);
5571 left = fp->count - 1 - i;
5572 if (left) {
5573 memmove(&fp->fp[i], &fp->fp[i + 1],
5574 left * sizeof(struct file *));
5575 }
5576 fp->count--;
5577 if (!fp->count) {
5578 kfree_skb(skb);
5579 skb = NULL;
5580 } else {
5581 __skb_queue_tail(&list, skb);
5582 }
5583 fput(file);
5584 file = NULL;
5585 break;
5586 }
5587
5588 if (!file)
5589 break;
5590
5591 __skb_queue_tail(&list, skb);
5592
5593 skb = skb_dequeue(head);
5594 }
5595
5596 if (skb_peek(&list)) {
5597 spin_lock_irq(&head->lock);
5598 while ((skb = __skb_dequeue(&list)) != NULL)
5599 __skb_queue_tail(head, skb);
5600 spin_unlock_irq(&head->lock);
5601 }
5602#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005603 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005604#endif
5605}
5606
Jens Axboe05f3fb32019-12-09 11:22:50 -07005607struct io_file_put {
5608 struct llist_node llist;
5609 struct file *file;
5610 struct completion *done;
5611};
5612
Jens Axboe2faf8522020-02-04 19:54:55 -07005613static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005614{
5615 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005616 struct llist_node *node;
5617
Jens Axboe05f3fb32019-12-09 11:22:50 -07005618 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5619 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5620 io_ring_file_put(data->ctx, pfile->file);
5621 if (pfile->done)
5622 complete(pfile->done);
5623 else
5624 kfree(pfile);
5625 }
5626 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005627}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005628
Jens Axboe2faf8522020-02-04 19:54:55 -07005629static void io_ring_file_ref_switch(struct work_struct *work)
5630{
5631 struct fixed_file_data *data;
5632
5633 data = container_of(work, struct fixed_file_data, ref_work);
5634 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005635 percpu_ref_switch_to_percpu(&data->refs);
5636}
5637
5638static void io_file_data_ref_zero(struct percpu_ref *ref)
5639{
5640 struct fixed_file_data *data;
5641
5642 data = container_of(ref, struct fixed_file_data, refs);
5643
Jens Axboe2faf8522020-02-04 19:54:55 -07005644 /*
5645 * We can't safely switch from inside this context, punt to wq. If
5646 * the table ref is going away, the table is being unregistered.
5647 * Don't queue up the async work for that case, the caller will
5648 * handle it.
5649 */
5650 if (!percpu_ref_is_dying(&data->refs))
5651 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005652}
5653
5654static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5655 unsigned nr_args)
5656{
5657 __s32 __user *fds = (__s32 __user *) arg;
5658 unsigned nr_tables;
5659 struct file *file;
5660 int fd, ret = 0;
5661 unsigned i;
5662
5663 if (ctx->file_data)
5664 return -EBUSY;
5665 if (!nr_args)
5666 return -EINVAL;
5667 if (nr_args > IORING_MAX_FIXED_FILES)
5668 return -EMFILE;
5669
5670 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5671 if (!ctx->file_data)
5672 return -ENOMEM;
5673 ctx->file_data->ctx = ctx;
5674 init_completion(&ctx->file_data->done);
5675
5676 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5677 ctx->file_data->table = kcalloc(nr_tables,
5678 sizeof(struct fixed_file_table),
5679 GFP_KERNEL);
5680 if (!ctx->file_data->table) {
5681 kfree(ctx->file_data);
5682 ctx->file_data = NULL;
5683 return -ENOMEM;
5684 }
5685
5686 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5687 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5688 kfree(ctx->file_data->table);
5689 kfree(ctx->file_data);
5690 ctx->file_data = NULL;
5691 return -ENOMEM;
5692 }
5693 ctx->file_data->put_llist.first = NULL;
5694 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5695
5696 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5697 percpu_ref_exit(&ctx->file_data->refs);
5698 kfree(ctx->file_data->table);
5699 kfree(ctx->file_data);
5700 ctx->file_data = NULL;
5701 return -ENOMEM;
5702 }
5703
5704 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5705 struct fixed_file_table *table;
5706 unsigned index;
5707
5708 ret = -EFAULT;
5709 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5710 break;
5711 /* allow sparse sets */
5712 if (fd == -1) {
5713 ret = 0;
5714 continue;
5715 }
5716
5717 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5718 index = i & IORING_FILE_TABLE_MASK;
5719 file = fget(fd);
5720
5721 ret = -EBADF;
5722 if (!file)
5723 break;
5724
5725 /*
5726 * Don't allow io_uring instances to be registered. If UNIX
5727 * isn't enabled, then this causes a reference cycle and this
5728 * instance can never get freed. If UNIX is enabled we'll
5729 * handle it just fine, but there's still no point in allowing
5730 * a ring fd as it doesn't support regular read/write anyway.
5731 */
5732 if (file->f_op == &io_uring_fops) {
5733 fput(file);
5734 break;
5735 }
5736 ret = 0;
5737 table->files[index] = file;
5738 }
5739
5740 if (ret) {
5741 for (i = 0; i < ctx->nr_user_files; i++) {
5742 file = io_file_from_index(ctx, i);
5743 if (file)
5744 fput(file);
5745 }
5746 for (i = 0; i < nr_tables; i++)
5747 kfree(ctx->file_data->table[i].files);
5748
5749 kfree(ctx->file_data->table);
5750 kfree(ctx->file_data);
5751 ctx->file_data = NULL;
5752 ctx->nr_user_files = 0;
5753 return ret;
5754 }
5755
5756 ret = io_sqe_files_scm(ctx);
5757 if (ret)
5758 io_sqe_files_unregister(ctx);
5759
5760 return ret;
5761}
5762
Jens Axboec3a31e62019-10-03 13:59:56 -06005763static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5764 int index)
5765{
5766#if defined(CONFIG_UNIX)
5767 struct sock *sock = ctx->ring_sock->sk;
5768 struct sk_buff_head *head = &sock->sk_receive_queue;
5769 struct sk_buff *skb;
5770
5771 /*
5772 * See if we can merge this file into an existing skb SCM_RIGHTS
5773 * file set. If there's no room, fall back to allocating a new skb
5774 * and filling it in.
5775 */
5776 spin_lock_irq(&head->lock);
5777 skb = skb_peek(head);
5778 if (skb) {
5779 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5780
5781 if (fpl->count < SCM_MAX_FD) {
5782 __skb_unlink(skb, head);
5783 spin_unlock_irq(&head->lock);
5784 fpl->fp[fpl->count] = get_file(file);
5785 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5786 fpl->count++;
5787 spin_lock_irq(&head->lock);
5788 __skb_queue_head(head, skb);
5789 } else {
5790 skb = NULL;
5791 }
5792 }
5793 spin_unlock_irq(&head->lock);
5794
5795 if (skb) {
5796 fput(file);
5797 return 0;
5798 }
5799
5800 return __io_sqe_files_scm(ctx, 1, index);
5801#else
5802 return 0;
5803#endif
5804}
5805
Jens Axboe05f3fb32019-12-09 11:22:50 -07005806static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005807{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005808 struct fixed_file_data *data;
5809
Jens Axboedd3db2a2020-02-26 10:23:43 -07005810 /*
5811 * Juggle reference to ensure we hit zero, if needed, so we can
5812 * switch back to percpu mode
5813 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005814 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07005815 percpu_ref_put(&data->refs);
5816 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005817}
5818
5819static bool io_queue_file_removal(struct fixed_file_data *data,
5820 struct file *file)
5821{
5822 struct io_file_put *pfile, pfile_stack;
5823 DECLARE_COMPLETION_ONSTACK(done);
5824
5825 /*
5826 * If we fail allocating the struct we need for doing async reomval
5827 * of this file, just punt to sync and wait for it.
5828 */
5829 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5830 if (!pfile) {
5831 pfile = &pfile_stack;
5832 pfile->done = &done;
5833 }
5834
5835 pfile->file = file;
5836 llist_add(&pfile->llist, &data->put_llist);
5837
5838 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07005839 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005840 wait_for_completion(&done);
5841 flush_work(&data->ref_work);
5842 return false;
5843 }
5844
5845 return true;
5846}
5847
5848static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5849 struct io_uring_files_update *up,
5850 unsigned nr_args)
5851{
5852 struct fixed_file_data *data = ctx->file_data;
5853 bool ref_switch = false;
5854 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005855 __s32 __user *fds;
5856 int fd, i, err;
5857 __u32 done;
5858
Jens Axboe05f3fb32019-12-09 11:22:50 -07005859 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005860 return -EOVERFLOW;
5861 if (done > ctx->nr_user_files)
5862 return -EINVAL;
5863
5864 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005865 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005866 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005867 struct fixed_file_table *table;
5868 unsigned index;
5869
Jens Axboec3a31e62019-10-03 13:59:56 -06005870 err = 0;
5871 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5872 err = -EFAULT;
5873 break;
5874 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005875 i = array_index_nospec(up->offset, ctx->nr_user_files);
5876 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005877 index = i & IORING_FILE_TABLE_MASK;
5878 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005879 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005880 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005881 if (io_queue_file_removal(data, file))
5882 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005883 }
5884 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005885 file = fget(fd);
5886 if (!file) {
5887 err = -EBADF;
5888 break;
5889 }
5890 /*
5891 * Don't allow io_uring instances to be registered. If
5892 * UNIX isn't enabled, then this causes a reference
5893 * cycle and this instance can never get freed. If UNIX
5894 * is enabled we'll handle it just fine, but there's
5895 * still no point in allowing a ring fd as it doesn't
5896 * support regular read/write anyway.
5897 */
5898 if (file->f_op == &io_uring_fops) {
5899 fput(file);
5900 err = -EBADF;
5901 break;
5902 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005903 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005904 err = io_sqe_file_register(ctx, file, i);
5905 if (err)
5906 break;
5907 }
5908 nr_args--;
5909 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005910 up->offset++;
5911 }
5912
Jens Axboedd3db2a2020-02-26 10:23:43 -07005913 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005914 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005915
5916 return done ? done : err;
5917}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005918static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5919 unsigned nr_args)
5920{
5921 struct io_uring_files_update up;
5922
5923 if (!ctx->file_data)
5924 return -ENXIO;
5925 if (!nr_args)
5926 return -EINVAL;
5927 if (copy_from_user(&up, arg, sizeof(up)))
5928 return -EFAULT;
5929 if (up.resv)
5930 return -EINVAL;
5931
5932 return __io_sqe_files_update(ctx, &up, nr_args);
5933}
Jens Axboec3a31e62019-10-03 13:59:56 -06005934
Jens Axboe7d723062019-11-12 22:31:31 -07005935static void io_put_work(struct io_wq_work *work)
5936{
5937 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5938
5939 io_put_req(req);
5940}
5941
5942static void io_get_work(struct io_wq_work *work)
5943{
5944 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5945
5946 refcount_inc(&req->refs);
5947}
5948
Pavel Begunkov24369c22020-01-28 03:15:48 +03005949static int io_init_wq_offload(struct io_ring_ctx *ctx,
5950 struct io_uring_params *p)
5951{
5952 struct io_wq_data data;
5953 struct fd f;
5954 struct io_ring_ctx *ctx_attach;
5955 unsigned int concurrency;
5956 int ret = 0;
5957
5958 data.user = ctx->user;
5959 data.get_work = io_get_work;
5960 data.put_work = io_put_work;
5961
5962 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5963 /* Do QD, or 4 * CPUS, whatever is smallest */
5964 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5965
5966 ctx->io_wq = io_wq_create(concurrency, &data);
5967 if (IS_ERR(ctx->io_wq)) {
5968 ret = PTR_ERR(ctx->io_wq);
5969 ctx->io_wq = NULL;
5970 }
5971 return ret;
5972 }
5973
5974 f = fdget(p->wq_fd);
5975 if (!f.file)
5976 return -EBADF;
5977
5978 if (f.file->f_op != &io_uring_fops) {
5979 ret = -EINVAL;
5980 goto out_fput;
5981 }
5982
5983 ctx_attach = f.file->private_data;
5984 /* @io_wq is protected by holding the fd */
5985 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5986 ret = -EINVAL;
5987 goto out_fput;
5988 }
5989
5990 ctx->io_wq = ctx_attach->io_wq;
5991out_fput:
5992 fdput(f);
5993 return ret;
5994}
5995
Jens Axboe6c271ce2019-01-10 11:22:30 -07005996static int io_sq_offload_start(struct io_ring_ctx *ctx,
5997 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005998{
5999 int ret;
6000
Jens Axboe6c271ce2019-01-10 11:22:30 -07006001 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006002 mmgrab(current->mm);
6003 ctx->sqo_mm = current->mm;
6004
Jens Axboe6c271ce2019-01-10 11:22:30 -07006005 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006006 ret = -EPERM;
6007 if (!capable(CAP_SYS_ADMIN))
6008 goto err;
6009
Jens Axboe917257d2019-04-13 09:28:55 -06006010 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6011 if (!ctx->sq_thread_idle)
6012 ctx->sq_thread_idle = HZ;
6013
Jens Axboe6c271ce2019-01-10 11:22:30 -07006014 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006015 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006016
Jens Axboe917257d2019-04-13 09:28:55 -06006017 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006018 if (cpu >= nr_cpu_ids)
6019 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006020 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006021 goto err;
6022
Jens Axboe6c271ce2019-01-10 11:22:30 -07006023 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6024 ctx, cpu,
6025 "io_uring-sq");
6026 } else {
6027 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6028 "io_uring-sq");
6029 }
6030 if (IS_ERR(ctx->sqo_thread)) {
6031 ret = PTR_ERR(ctx->sqo_thread);
6032 ctx->sqo_thread = NULL;
6033 goto err;
6034 }
6035 wake_up_process(ctx->sqo_thread);
6036 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6037 /* Can't have SQ_AFF without SQPOLL */
6038 ret = -EINVAL;
6039 goto err;
6040 }
6041
Pavel Begunkov24369c22020-01-28 03:15:48 +03006042 ret = io_init_wq_offload(ctx, p);
6043 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006044 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006045
6046 return 0;
6047err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006048 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006049 mmdrop(ctx->sqo_mm);
6050 ctx->sqo_mm = NULL;
6051 return ret;
6052}
6053
6054static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6055{
6056 atomic_long_sub(nr_pages, &user->locked_vm);
6057}
6058
6059static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6060{
6061 unsigned long page_limit, cur_pages, new_pages;
6062
6063 /* Don't allow more pages than we can safely lock */
6064 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6065
6066 do {
6067 cur_pages = atomic_long_read(&user->locked_vm);
6068 new_pages = cur_pages + nr_pages;
6069 if (new_pages > page_limit)
6070 return -ENOMEM;
6071 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6072 new_pages) != cur_pages);
6073
6074 return 0;
6075}
6076
6077static void io_mem_free(void *ptr)
6078{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006079 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006080
Mark Rutland52e04ef2019-04-30 17:30:21 +01006081 if (!ptr)
6082 return;
6083
6084 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006085 if (put_page_testzero(page))
6086 free_compound_page(page);
6087}
6088
6089static void *io_mem_alloc(size_t size)
6090{
6091 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6092 __GFP_NORETRY;
6093
6094 return (void *) __get_free_pages(gfp_flags, get_order(size));
6095}
6096
Hristo Venev75b28af2019-08-26 17:23:46 +00006097static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6098 size_t *sq_offset)
6099{
6100 struct io_rings *rings;
6101 size_t off, sq_array_size;
6102
6103 off = struct_size(rings, cqes, cq_entries);
6104 if (off == SIZE_MAX)
6105 return SIZE_MAX;
6106
6107#ifdef CONFIG_SMP
6108 off = ALIGN(off, SMP_CACHE_BYTES);
6109 if (off == 0)
6110 return SIZE_MAX;
6111#endif
6112
6113 sq_array_size = array_size(sizeof(u32), sq_entries);
6114 if (sq_array_size == SIZE_MAX)
6115 return SIZE_MAX;
6116
6117 if (check_add_overflow(off, sq_array_size, &off))
6118 return SIZE_MAX;
6119
6120 if (sq_offset)
6121 *sq_offset = off;
6122
6123 return off;
6124}
6125
Jens Axboe2b188cc2019-01-07 10:46:33 -07006126static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6127{
Hristo Venev75b28af2019-08-26 17:23:46 +00006128 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006129
Hristo Venev75b28af2019-08-26 17:23:46 +00006130 pages = (size_t)1 << get_order(
6131 rings_size(sq_entries, cq_entries, NULL));
6132 pages += (size_t)1 << get_order(
6133 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006134
Hristo Venev75b28af2019-08-26 17:23:46 +00006135 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006136}
6137
Jens Axboeedafcce2019-01-09 09:16:05 -07006138static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6139{
6140 int i, j;
6141
6142 if (!ctx->user_bufs)
6143 return -ENXIO;
6144
6145 for (i = 0; i < ctx->nr_user_bufs; i++) {
6146 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6147
6148 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006149 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006150
6151 if (ctx->account_mem)
6152 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006153 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006154 imu->nr_bvecs = 0;
6155 }
6156
6157 kfree(ctx->user_bufs);
6158 ctx->user_bufs = NULL;
6159 ctx->nr_user_bufs = 0;
6160 return 0;
6161}
6162
6163static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6164 void __user *arg, unsigned index)
6165{
6166 struct iovec __user *src;
6167
6168#ifdef CONFIG_COMPAT
6169 if (ctx->compat) {
6170 struct compat_iovec __user *ciovs;
6171 struct compat_iovec ciov;
6172
6173 ciovs = (struct compat_iovec __user *) arg;
6174 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6175 return -EFAULT;
6176
Jens Axboed55e5f52019-12-11 16:12:15 -07006177 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006178 dst->iov_len = ciov.iov_len;
6179 return 0;
6180 }
6181#endif
6182 src = (struct iovec __user *) arg;
6183 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6184 return -EFAULT;
6185 return 0;
6186}
6187
6188static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6189 unsigned nr_args)
6190{
6191 struct vm_area_struct **vmas = NULL;
6192 struct page **pages = NULL;
6193 int i, j, got_pages = 0;
6194 int ret = -EINVAL;
6195
6196 if (ctx->user_bufs)
6197 return -EBUSY;
6198 if (!nr_args || nr_args > UIO_MAXIOV)
6199 return -EINVAL;
6200
6201 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6202 GFP_KERNEL);
6203 if (!ctx->user_bufs)
6204 return -ENOMEM;
6205
6206 for (i = 0; i < nr_args; i++) {
6207 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6208 unsigned long off, start, end, ubuf;
6209 int pret, nr_pages;
6210 struct iovec iov;
6211 size_t size;
6212
6213 ret = io_copy_iov(ctx, &iov, arg, i);
6214 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006215 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006216
6217 /*
6218 * Don't impose further limits on the size and buffer
6219 * constraints here, we'll -EINVAL later when IO is
6220 * submitted if they are wrong.
6221 */
6222 ret = -EFAULT;
6223 if (!iov.iov_base || !iov.iov_len)
6224 goto err;
6225
6226 /* arbitrary limit, but we need something */
6227 if (iov.iov_len > SZ_1G)
6228 goto err;
6229
6230 ubuf = (unsigned long) iov.iov_base;
6231 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6232 start = ubuf >> PAGE_SHIFT;
6233 nr_pages = end - start;
6234
6235 if (ctx->account_mem) {
6236 ret = io_account_mem(ctx->user, nr_pages);
6237 if (ret)
6238 goto err;
6239 }
6240
6241 ret = 0;
6242 if (!pages || nr_pages > got_pages) {
6243 kfree(vmas);
6244 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006245 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006246 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006247 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006248 sizeof(struct vm_area_struct *),
6249 GFP_KERNEL);
6250 if (!pages || !vmas) {
6251 ret = -ENOMEM;
6252 if (ctx->account_mem)
6253 io_unaccount_mem(ctx->user, nr_pages);
6254 goto err;
6255 }
6256 got_pages = nr_pages;
6257 }
6258
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006259 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006260 GFP_KERNEL);
6261 ret = -ENOMEM;
6262 if (!imu->bvec) {
6263 if (ctx->account_mem)
6264 io_unaccount_mem(ctx->user, nr_pages);
6265 goto err;
6266 }
6267
6268 ret = 0;
6269 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006270 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006271 FOLL_WRITE | FOLL_LONGTERM,
6272 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006273 if (pret == nr_pages) {
6274 /* don't support file backed memory */
6275 for (j = 0; j < nr_pages; j++) {
6276 struct vm_area_struct *vma = vmas[j];
6277
6278 if (vma->vm_file &&
6279 !is_file_hugepages(vma->vm_file)) {
6280 ret = -EOPNOTSUPP;
6281 break;
6282 }
6283 }
6284 } else {
6285 ret = pret < 0 ? pret : -EFAULT;
6286 }
6287 up_read(&current->mm->mmap_sem);
6288 if (ret) {
6289 /*
6290 * if we did partial map, or found file backed vmas,
6291 * release any pages we did get
6292 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006293 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006294 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006295 if (ctx->account_mem)
6296 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006297 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006298 goto err;
6299 }
6300
6301 off = ubuf & ~PAGE_MASK;
6302 size = iov.iov_len;
6303 for (j = 0; j < nr_pages; j++) {
6304 size_t vec_len;
6305
6306 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6307 imu->bvec[j].bv_page = pages[j];
6308 imu->bvec[j].bv_len = vec_len;
6309 imu->bvec[j].bv_offset = off;
6310 off = 0;
6311 size -= vec_len;
6312 }
6313 /* store original address for later verification */
6314 imu->ubuf = ubuf;
6315 imu->len = iov.iov_len;
6316 imu->nr_bvecs = nr_pages;
6317
6318 ctx->nr_user_bufs++;
6319 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006320 kvfree(pages);
6321 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006322 return 0;
6323err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006324 kvfree(pages);
6325 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006326 io_sqe_buffer_unregister(ctx);
6327 return ret;
6328}
6329
Jens Axboe9b402842019-04-11 11:45:41 -06006330static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6331{
6332 __s32 __user *fds = arg;
6333 int fd;
6334
6335 if (ctx->cq_ev_fd)
6336 return -EBUSY;
6337
6338 if (copy_from_user(&fd, fds, sizeof(*fds)))
6339 return -EFAULT;
6340
6341 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6342 if (IS_ERR(ctx->cq_ev_fd)) {
6343 int ret = PTR_ERR(ctx->cq_ev_fd);
6344 ctx->cq_ev_fd = NULL;
6345 return ret;
6346 }
6347
6348 return 0;
6349}
6350
6351static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6352{
6353 if (ctx->cq_ev_fd) {
6354 eventfd_ctx_put(ctx->cq_ev_fd);
6355 ctx->cq_ev_fd = NULL;
6356 return 0;
6357 }
6358
6359 return -ENXIO;
6360}
6361
Jens Axboe2b188cc2019-01-07 10:46:33 -07006362static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6363{
Jens Axboe6b063142019-01-10 22:13:58 -07006364 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006365 if (ctx->sqo_mm)
6366 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006367
6368 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006369 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006370 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006371 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006372 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006373
Jens Axboe2b188cc2019-01-07 10:46:33 -07006374#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006375 if (ctx->ring_sock) {
6376 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006377 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006378 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006379#endif
6380
Hristo Venev75b28af2019-08-26 17:23:46 +00006381 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006382 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006383
6384 percpu_ref_exit(&ctx->refs);
6385 if (ctx->account_mem)
6386 io_unaccount_mem(ctx->user,
6387 ring_pages(ctx->sq_entries, ctx->cq_entries));
6388 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006389 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006390 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006391 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006392 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006393 kfree(ctx);
6394}
6395
6396static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6397{
6398 struct io_ring_ctx *ctx = file->private_data;
6399 __poll_t mask = 0;
6400
6401 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006402 /*
6403 * synchronizes with barrier from wq_has_sleeper call in
6404 * io_commit_cqring
6405 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006406 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006407 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6408 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006409 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006410 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006411 mask |= EPOLLIN | EPOLLRDNORM;
6412
6413 return mask;
6414}
6415
6416static int io_uring_fasync(int fd, struct file *file, int on)
6417{
6418 struct io_ring_ctx *ctx = file->private_data;
6419
6420 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6421}
6422
Jens Axboe071698e2020-01-28 10:04:42 -07006423static int io_remove_personalities(int id, void *p, void *data)
6424{
6425 struct io_ring_ctx *ctx = data;
6426 const struct cred *cred;
6427
6428 cred = idr_remove(&ctx->personality_idr, id);
6429 if (cred)
6430 put_cred(cred);
6431 return 0;
6432}
6433
Jens Axboe2b188cc2019-01-07 10:46:33 -07006434static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6435{
6436 mutex_lock(&ctx->uring_lock);
6437 percpu_ref_kill(&ctx->refs);
6438 mutex_unlock(&ctx->uring_lock);
6439
Jens Axboedf069d82020-02-04 16:48:34 -07006440 /*
6441 * Wait for sq thread to idle, if we have one. It won't spin on new
6442 * work after we've killed the ctx ref above. This is important to do
6443 * before we cancel existing commands, as the thread could otherwise
6444 * be queueing new work post that. If that's work we need to cancel,
6445 * it could cause shutdown to hang.
6446 */
6447 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6448 cpu_relax();
6449
Jens Axboe5262f562019-09-17 12:26:57 -06006450 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006451 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006452
6453 if (ctx->io_wq)
6454 io_wq_cancel_all(ctx->io_wq);
6455
Jens Axboedef596e2019-01-09 08:59:42 -07006456 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006457 /* if we failed setting up the ctx, we might not have any rings */
6458 if (ctx->rings)
6459 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006460 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006461 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006462 io_ring_ctx_free(ctx);
6463}
6464
6465static int io_uring_release(struct inode *inode, struct file *file)
6466{
6467 struct io_ring_ctx *ctx = file->private_data;
6468
6469 file->private_data = NULL;
6470 io_ring_ctx_wait_and_kill(ctx);
6471 return 0;
6472}
6473
Jens Axboefcb323c2019-10-24 12:39:47 -06006474static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6475 struct files_struct *files)
6476{
6477 struct io_kiocb *req;
6478 DEFINE_WAIT(wait);
6479
6480 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006481 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006482
6483 spin_lock_irq(&ctx->inflight_lock);
6484 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006485 if (req->work.files != files)
6486 continue;
6487 /* req is being completed, ignore */
6488 if (!refcount_inc_not_zero(&req->refs))
6489 continue;
6490 cancel_req = req;
6491 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006492 }
Jens Axboe768134d2019-11-10 20:30:53 -07006493 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006494 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006495 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006496 spin_unlock_irq(&ctx->inflight_lock);
6497
Jens Axboe768134d2019-11-10 20:30:53 -07006498 /* We need to keep going until we don't find a matching req */
6499 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006500 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006501
Jens Axboe2ca10252020-02-13 17:17:35 -07006502 if (cancel_req->flags & REQ_F_OVERFLOW) {
6503 spin_lock_irq(&ctx->completion_lock);
6504 list_del(&cancel_req->list);
6505 cancel_req->flags &= ~REQ_F_OVERFLOW;
6506 if (list_empty(&ctx->cq_overflow_list)) {
6507 clear_bit(0, &ctx->sq_check_overflow);
6508 clear_bit(0, &ctx->cq_check_overflow);
6509 }
6510 spin_unlock_irq(&ctx->completion_lock);
6511
6512 WRITE_ONCE(ctx->rings->cq_overflow,
6513 atomic_inc_return(&ctx->cached_cq_overflow));
6514
6515 /*
6516 * Put inflight ref and overflow ref. If that's
6517 * all we had, then we're done with this request.
6518 */
6519 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6520 io_put_req(cancel_req);
6521 continue;
6522 }
6523 }
6524
Bob Liu2f6d9b92019-11-13 18:06:24 +08006525 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6526 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006527 schedule();
6528 }
Jens Axboe768134d2019-11-10 20:30:53 -07006529 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006530}
6531
6532static int io_uring_flush(struct file *file, void *data)
6533{
6534 struct io_ring_ctx *ctx = file->private_data;
6535
6536 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006537
6538 /*
6539 * If the task is going away, cancel work it may have pending
6540 */
6541 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6542 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6543
Jens Axboefcb323c2019-10-24 12:39:47 -06006544 return 0;
6545}
6546
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006547static void *io_uring_validate_mmap_request(struct file *file,
6548 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006549{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006550 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006551 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006552 struct page *page;
6553 void *ptr;
6554
6555 switch (offset) {
6556 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006557 case IORING_OFF_CQ_RING:
6558 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006559 break;
6560 case IORING_OFF_SQES:
6561 ptr = ctx->sq_sqes;
6562 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006563 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006564 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006565 }
6566
6567 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006568 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006569 return ERR_PTR(-EINVAL);
6570
6571 return ptr;
6572}
6573
6574#ifdef CONFIG_MMU
6575
6576static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6577{
6578 size_t sz = vma->vm_end - vma->vm_start;
6579 unsigned long pfn;
6580 void *ptr;
6581
6582 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6583 if (IS_ERR(ptr))
6584 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006585
6586 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6587 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6588}
6589
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006590#else /* !CONFIG_MMU */
6591
6592static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6593{
6594 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6595}
6596
6597static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6598{
6599 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6600}
6601
6602static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6603 unsigned long addr, unsigned long len,
6604 unsigned long pgoff, unsigned long flags)
6605{
6606 void *ptr;
6607
6608 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6609 if (IS_ERR(ptr))
6610 return PTR_ERR(ptr);
6611
6612 return (unsigned long) ptr;
6613}
6614
6615#endif /* !CONFIG_MMU */
6616
Jens Axboe2b188cc2019-01-07 10:46:33 -07006617SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6618 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6619 size_t, sigsz)
6620{
6621 struct io_ring_ctx *ctx;
6622 long ret = -EBADF;
6623 int submitted = 0;
6624 struct fd f;
6625
Jens Axboe6c271ce2019-01-10 11:22:30 -07006626 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006627 return -EINVAL;
6628
6629 f = fdget(fd);
6630 if (!f.file)
6631 return -EBADF;
6632
6633 ret = -EOPNOTSUPP;
6634 if (f.file->f_op != &io_uring_fops)
6635 goto out_fput;
6636
6637 ret = -ENXIO;
6638 ctx = f.file->private_data;
6639 if (!percpu_ref_tryget(&ctx->refs))
6640 goto out_fput;
6641
Jens Axboe6c271ce2019-01-10 11:22:30 -07006642 /*
6643 * For SQ polling, the thread will do all submissions and completions.
6644 * Just return the requested submit count, and wake the thread if
6645 * we were asked to.
6646 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006647 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006648 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006649 if (!list_empty_careful(&ctx->cq_overflow_list))
6650 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006651 if (flags & IORING_ENTER_SQ_WAKEUP)
6652 wake_up(&ctx->sqo_wait);
6653 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006654 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006655 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006656
6657 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006658 /* already have mm, so io_submit_sqes() won't try to grab it */
6659 cur_mm = ctx->sqo_mm;
6660 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6661 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006662 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006663
6664 if (submitted != to_submit)
6665 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006666 }
6667 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006668 unsigned nr_events = 0;
6669
Jens Axboe2b188cc2019-01-07 10:46:33 -07006670 min_complete = min(min_complete, ctx->cq_entries);
6671
Jens Axboedef596e2019-01-09 08:59:42 -07006672 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006673 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006674 } else {
6675 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6676 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006677 }
6678
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006679out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006680 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006681out_fput:
6682 fdput(f);
6683 return submitted ? submitted : ret;
6684}
6685
Tobias Klauserbebdb652020-02-26 18:38:32 +01006686#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006687static int io_uring_show_cred(int id, void *p, void *data)
6688{
6689 const struct cred *cred = p;
6690 struct seq_file *m = data;
6691 struct user_namespace *uns = seq_user_ns(m);
6692 struct group_info *gi;
6693 kernel_cap_t cap;
6694 unsigned __capi;
6695 int g;
6696
6697 seq_printf(m, "%5d\n", id);
6698 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6699 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6700 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6701 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6702 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6703 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6704 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6705 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6706 seq_puts(m, "\n\tGroups:\t");
6707 gi = cred->group_info;
6708 for (g = 0; g < gi->ngroups; g++) {
6709 seq_put_decimal_ull(m, g ? " " : "",
6710 from_kgid_munged(uns, gi->gid[g]));
6711 }
6712 seq_puts(m, "\n\tCapEff:\t");
6713 cap = cred->cap_effective;
6714 CAP_FOR_EACH_U32(__capi)
6715 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6716 seq_putc(m, '\n');
6717 return 0;
6718}
6719
6720static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6721{
6722 int i;
6723
6724 mutex_lock(&ctx->uring_lock);
6725 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6726 for (i = 0; i < ctx->nr_user_files; i++) {
6727 struct fixed_file_table *table;
6728 struct file *f;
6729
6730 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6731 f = table->files[i & IORING_FILE_TABLE_MASK];
6732 if (f)
6733 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6734 else
6735 seq_printf(m, "%5u: <none>\n", i);
6736 }
6737 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6738 for (i = 0; i < ctx->nr_user_bufs; i++) {
6739 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6740
6741 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6742 (unsigned int) buf->len);
6743 }
6744 if (!idr_is_empty(&ctx->personality_idr)) {
6745 seq_printf(m, "Personalities:\n");
6746 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6747 }
6748 mutex_unlock(&ctx->uring_lock);
6749}
6750
6751static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6752{
6753 struct io_ring_ctx *ctx = f->private_data;
6754
6755 if (percpu_ref_tryget(&ctx->refs)) {
6756 __io_uring_show_fdinfo(ctx, m);
6757 percpu_ref_put(&ctx->refs);
6758 }
6759}
Tobias Klauserbebdb652020-02-26 18:38:32 +01006760#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07006761
Jens Axboe2b188cc2019-01-07 10:46:33 -07006762static const struct file_operations io_uring_fops = {
6763 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006764 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006765 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006766#ifndef CONFIG_MMU
6767 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6768 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6769#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006770 .poll = io_uring_poll,
6771 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006772#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006773 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006774#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006775};
6776
6777static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6778 struct io_uring_params *p)
6779{
Hristo Venev75b28af2019-08-26 17:23:46 +00006780 struct io_rings *rings;
6781 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006782
Hristo Venev75b28af2019-08-26 17:23:46 +00006783 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6784 if (size == SIZE_MAX)
6785 return -EOVERFLOW;
6786
6787 rings = io_mem_alloc(size);
6788 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006789 return -ENOMEM;
6790
Hristo Venev75b28af2019-08-26 17:23:46 +00006791 ctx->rings = rings;
6792 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6793 rings->sq_ring_mask = p->sq_entries - 1;
6794 rings->cq_ring_mask = p->cq_entries - 1;
6795 rings->sq_ring_entries = p->sq_entries;
6796 rings->cq_ring_entries = p->cq_entries;
6797 ctx->sq_mask = rings->sq_ring_mask;
6798 ctx->cq_mask = rings->cq_ring_mask;
6799 ctx->sq_entries = rings->sq_ring_entries;
6800 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006801
6802 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006803 if (size == SIZE_MAX) {
6804 io_mem_free(ctx->rings);
6805 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006806 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006807 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006808
6809 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006810 if (!ctx->sq_sqes) {
6811 io_mem_free(ctx->rings);
6812 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006813 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006814 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006815
Jens Axboe2b188cc2019-01-07 10:46:33 -07006816 return 0;
6817}
6818
6819/*
6820 * Allocate an anonymous fd, this is what constitutes the application
6821 * visible backing of an io_uring instance. The application mmaps this
6822 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6823 * we have to tie this fd to a socket for file garbage collection purposes.
6824 */
6825static int io_uring_get_fd(struct io_ring_ctx *ctx)
6826{
6827 struct file *file;
6828 int ret;
6829
6830#if defined(CONFIG_UNIX)
6831 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6832 &ctx->ring_sock);
6833 if (ret)
6834 return ret;
6835#endif
6836
6837 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6838 if (ret < 0)
6839 goto err;
6840
6841 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6842 O_RDWR | O_CLOEXEC);
6843 if (IS_ERR(file)) {
6844 put_unused_fd(ret);
6845 ret = PTR_ERR(file);
6846 goto err;
6847 }
6848
6849#if defined(CONFIG_UNIX)
6850 ctx->ring_sock->file = file;
6851#endif
6852 fd_install(ret, file);
6853 return ret;
6854err:
6855#if defined(CONFIG_UNIX)
6856 sock_release(ctx->ring_sock);
6857 ctx->ring_sock = NULL;
6858#endif
6859 return ret;
6860}
6861
6862static int io_uring_create(unsigned entries, struct io_uring_params *p)
6863{
6864 struct user_struct *user = NULL;
6865 struct io_ring_ctx *ctx;
6866 bool account_mem;
6867 int ret;
6868
Jens Axboe8110c1a2019-12-28 15:39:54 -07006869 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006870 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006871 if (entries > IORING_MAX_ENTRIES) {
6872 if (!(p->flags & IORING_SETUP_CLAMP))
6873 return -EINVAL;
6874 entries = IORING_MAX_ENTRIES;
6875 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006876
6877 /*
6878 * Use twice as many entries for the CQ ring. It's possible for the
6879 * application to drive a higher depth than the size of the SQ ring,
6880 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006881 * some flexibility in overcommitting a bit. If the application has
6882 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6883 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006884 */
6885 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006886 if (p->flags & IORING_SETUP_CQSIZE) {
6887 /*
6888 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6889 * to a power-of-two, if it isn't already. We do NOT impose
6890 * any cq vs sq ring sizing.
6891 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006892 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006893 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006894 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6895 if (!(p->flags & IORING_SETUP_CLAMP))
6896 return -EINVAL;
6897 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6898 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006899 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6900 } else {
6901 p->cq_entries = 2 * p->sq_entries;
6902 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006903
6904 user = get_uid(current_user());
6905 account_mem = !capable(CAP_IPC_LOCK);
6906
6907 if (account_mem) {
6908 ret = io_account_mem(user,
6909 ring_pages(p->sq_entries, p->cq_entries));
6910 if (ret) {
6911 free_uid(user);
6912 return ret;
6913 }
6914 }
6915
6916 ctx = io_ring_ctx_alloc(p);
6917 if (!ctx) {
6918 if (account_mem)
6919 io_unaccount_mem(user, ring_pages(p->sq_entries,
6920 p->cq_entries));
6921 free_uid(user);
6922 return -ENOMEM;
6923 }
6924 ctx->compat = in_compat_syscall();
6925 ctx->account_mem = account_mem;
6926 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006927 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006928
6929 ret = io_allocate_scq_urings(ctx, p);
6930 if (ret)
6931 goto err;
6932
Jens Axboe6c271ce2019-01-10 11:22:30 -07006933 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006934 if (ret)
6935 goto err;
6936
Jens Axboe2b188cc2019-01-07 10:46:33 -07006937 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006938 p->sq_off.head = offsetof(struct io_rings, sq.head);
6939 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6940 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6941 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6942 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6943 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6944 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006945
6946 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006947 p->cq_off.head = offsetof(struct io_rings, cq.head);
6948 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6949 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6950 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6951 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6952 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006953
Jens Axboe044c1ab2019-10-28 09:15:33 -06006954 /*
6955 * Install ring fd as the very last thing, so we don't risk someone
6956 * having closed it before we finish setup
6957 */
6958 ret = io_uring_get_fd(ctx);
6959 if (ret < 0)
6960 goto err;
6961
Jens Axboeda8c9692019-12-02 18:51:26 -07006962 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006963 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6964 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006965 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006966 return ret;
6967err:
6968 io_ring_ctx_wait_and_kill(ctx);
6969 return ret;
6970}
6971
6972/*
6973 * Sets up an aio uring context, and returns the fd. Applications asks for a
6974 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6975 * params structure passed in.
6976 */
6977static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6978{
6979 struct io_uring_params p;
6980 long ret;
6981 int i;
6982
6983 if (copy_from_user(&p, params, sizeof(p)))
6984 return -EFAULT;
6985 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6986 if (p.resv[i])
6987 return -EINVAL;
6988 }
6989
Jens Axboe6c271ce2019-01-10 11:22:30 -07006990 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006991 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006992 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006993 return -EINVAL;
6994
6995 ret = io_uring_create(entries, &p);
6996 if (ret < 0)
6997 return ret;
6998
6999 if (copy_to_user(params, &p, sizeof(p)))
7000 return -EFAULT;
7001
7002 return ret;
7003}
7004
7005SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7006 struct io_uring_params __user *, params)
7007{
7008 return io_uring_setup(entries, params);
7009}
7010
Jens Axboe66f4af92020-01-16 15:36:52 -07007011static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7012{
7013 struct io_uring_probe *p;
7014 size_t size;
7015 int i, ret;
7016
7017 size = struct_size(p, ops, nr_args);
7018 if (size == SIZE_MAX)
7019 return -EOVERFLOW;
7020 p = kzalloc(size, GFP_KERNEL);
7021 if (!p)
7022 return -ENOMEM;
7023
7024 ret = -EFAULT;
7025 if (copy_from_user(p, arg, size))
7026 goto out;
7027 ret = -EINVAL;
7028 if (memchr_inv(p, 0, size))
7029 goto out;
7030
7031 p->last_op = IORING_OP_LAST - 1;
7032 if (nr_args > IORING_OP_LAST)
7033 nr_args = IORING_OP_LAST;
7034
7035 for (i = 0; i < nr_args; i++) {
7036 p->ops[i].op = i;
7037 if (!io_op_defs[i].not_supported)
7038 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7039 }
7040 p->ops_len = i;
7041
7042 ret = 0;
7043 if (copy_to_user(arg, p, size))
7044 ret = -EFAULT;
7045out:
7046 kfree(p);
7047 return ret;
7048}
7049
Jens Axboe071698e2020-01-28 10:04:42 -07007050static int io_register_personality(struct io_ring_ctx *ctx)
7051{
7052 const struct cred *creds = get_current_cred();
7053 int id;
7054
7055 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7056 USHRT_MAX, GFP_KERNEL);
7057 if (id < 0)
7058 put_cred(creds);
7059 return id;
7060}
7061
7062static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7063{
7064 const struct cred *old_creds;
7065
7066 old_creds = idr_remove(&ctx->personality_idr, id);
7067 if (old_creds) {
7068 put_cred(old_creds);
7069 return 0;
7070 }
7071
7072 return -EINVAL;
7073}
7074
7075static bool io_register_op_must_quiesce(int op)
7076{
7077 switch (op) {
7078 case IORING_UNREGISTER_FILES:
7079 case IORING_REGISTER_FILES_UPDATE:
7080 case IORING_REGISTER_PROBE:
7081 case IORING_REGISTER_PERSONALITY:
7082 case IORING_UNREGISTER_PERSONALITY:
7083 return false;
7084 default:
7085 return true;
7086 }
7087}
7088
Jens Axboeedafcce2019-01-09 09:16:05 -07007089static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7090 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007091 __releases(ctx->uring_lock)
7092 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007093{
7094 int ret;
7095
Jens Axboe35fa71a2019-04-22 10:23:23 -06007096 /*
7097 * We're inside the ring mutex, if the ref is already dying, then
7098 * someone else killed the ctx or is already going through
7099 * io_uring_register().
7100 */
7101 if (percpu_ref_is_dying(&ctx->refs))
7102 return -ENXIO;
7103
Jens Axboe071698e2020-01-28 10:04:42 -07007104 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007105 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007106
Jens Axboe05f3fb32019-12-09 11:22:50 -07007107 /*
7108 * Drop uring mutex before waiting for references to exit. If
7109 * another thread is currently inside io_uring_enter() it might
7110 * need to grab the uring_lock to make progress. If we hold it
7111 * here across the drain wait, then we can deadlock. It's safe
7112 * to drop the mutex here, since no new references will come in
7113 * after we've killed the percpu ref.
7114 */
7115 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007116 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007117 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007118 if (ret) {
7119 percpu_ref_resurrect(&ctx->refs);
7120 ret = -EINTR;
7121 goto out;
7122 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007123 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007124
7125 switch (opcode) {
7126 case IORING_REGISTER_BUFFERS:
7127 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7128 break;
7129 case IORING_UNREGISTER_BUFFERS:
7130 ret = -EINVAL;
7131 if (arg || nr_args)
7132 break;
7133 ret = io_sqe_buffer_unregister(ctx);
7134 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007135 case IORING_REGISTER_FILES:
7136 ret = io_sqe_files_register(ctx, arg, nr_args);
7137 break;
7138 case IORING_UNREGISTER_FILES:
7139 ret = -EINVAL;
7140 if (arg || nr_args)
7141 break;
7142 ret = io_sqe_files_unregister(ctx);
7143 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007144 case IORING_REGISTER_FILES_UPDATE:
7145 ret = io_sqe_files_update(ctx, arg, nr_args);
7146 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007147 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007148 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007149 ret = -EINVAL;
7150 if (nr_args != 1)
7151 break;
7152 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007153 if (ret)
7154 break;
7155 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7156 ctx->eventfd_async = 1;
7157 else
7158 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007159 break;
7160 case IORING_UNREGISTER_EVENTFD:
7161 ret = -EINVAL;
7162 if (arg || nr_args)
7163 break;
7164 ret = io_eventfd_unregister(ctx);
7165 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007166 case IORING_REGISTER_PROBE:
7167 ret = -EINVAL;
7168 if (!arg || nr_args > 256)
7169 break;
7170 ret = io_probe(ctx, arg, nr_args);
7171 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007172 case IORING_REGISTER_PERSONALITY:
7173 ret = -EINVAL;
7174 if (arg || nr_args)
7175 break;
7176 ret = io_register_personality(ctx);
7177 break;
7178 case IORING_UNREGISTER_PERSONALITY:
7179 ret = -EINVAL;
7180 if (arg)
7181 break;
7182 ret = io_unregister_personality(ctx, nr_args);
7183 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007184 default:
7185 ret = -EINVAL;
7186 break;
7187 }
7188
Jens Axboe071698e2020-01-28 10:04:42 -07007189 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007190 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007191 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007192out:
7193 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007194 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007195 return ret;
7196}
7197
7198SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7199 void __user *, arg, unsigned int, nr_args)
7200{
7201 struct io_ring_ctx *ctx;
7202 long ret = -EBADF;
7203 struct fd f;
7204
7205 f = fdget(fd);
7206 if (!f.file)
7207 return -EBADF;
7208
7209 ret = -EOPNOTSUPP;
7210 if (f.file->f_op != &io_uring_fops)
7211 goto out_fput;
7212
7213 ctx = f.file->private_data;
7214
7215 mutex_lock(&ctx->uring_lock);
7216 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7217 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007218 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7219 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007220out_fput:
7221 fdput(f);
7222 return ret;
7223}
7224
Jens Axboe2b188cc2019-01-07 10:46:33 -07007225static int __init io_uring_init(void)
7226{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007227#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7228 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7229 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7230} while (0)
7231
7232#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7233 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7234 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7235 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7236 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7237 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7238 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7239 BUILD_BUG_SQE_ELEM(8, __u64, off);
7240 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7241 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7242 BUILD_BUG_SQE_ELEM(24, __u32, len);
7243 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7244 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7245 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7246 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7247 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7248 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7249 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7250 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7251 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7252 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7253 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7254 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7255 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7256 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7257 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7258 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7259
Jens Axboed3656342019-12-18 09:50:26 -07007260 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007261 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7262 return 0;
7263};
7264__initcall(io_uring_init);