blob: e6829d1bf4b425b665b544adb90c40b11f3bee67 [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 Axboe2b188cc2019-01-07 10:46:33 -070078
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020079#define CREATE_TRACE_POINTS
80#include <trace/events/io_uring.h>
81
Jens Axboe2b188cc2019-01-07 10:46:33 -070082#include <uapi/linux/io_uring.h>
83
84#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060085#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070086
Daniel Xu5277dea2019-09-14 14:23:45 -070087#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060088#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060089
90/*
91 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
92 */
93#define IORING_FILE_TABLE_SHIFT 9
94#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
95#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
96#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070097
98struct io_uring {
99 u32 head ____cacheline_aligned_in_smp;
100 u32 tail ____cacheline_aligned_in_smp;
101};
102
Stefan Bühler1e84b972019-04-24 23:54:16 +0200103/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000104 * This data is shared with the application through the mmap at offsets
105 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106 *
107 * The offsets to the member fields are published through struct
108 * io_sqring_offsets when calling io_uring_setup.
109 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000110struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200111 /*
112 * Head and tail offsets into the ring; the offsets need to be
113 * masked to get valid indices.
114 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000115 * The kernel controls head of the sq ring and the tail of the cq ring,
116 * and the application controls tail of the sq ring and the head of the
117 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000121 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 * ring_entries - 1)
123 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 u32 sq_ring_mask, cq_ring_mask;
125 /* Ring sizes (constant, power of 2) */
126 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 /*
128 * Number of invalid entries dropped by the kernel due to
129 * invalid index stored in array
130 *
131 * Written by the kernel, shouldn't be modified by the
132 * application (i.e. get number of "new events" by comparing to
133 * cached value).
134 *
135 * After a new SQ head value was read by the application this
136 * counter includes all submissions that were dropped reaching
137 * the new SQ head (and possibly more).
138 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000139 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200140 /*
141 * Runtime flags
142 *
143 * Written by the kernel, shouldn't be modified by the
144 * application.
145 *
146 * The application needs a full memory barrier before checking
147 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
148 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000149 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200150 /*
151 * Number of completion events lost because the queue was full;
152 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800153 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200154 * the completion queue.
155 *
156 * Written by the kernel, shouldn't be modified by the
157 * application (i.e. get number of "new events" by comparing to
158 * cached value).
159 *
160 * As completion events come in out of order this counter is not
161 * ordered with any other data.
162 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000163 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200164 /*
165 * Ring buffer of completion events.
166 *
167 * The kernel writes completion events fresh every time they are
168 * produced, so the application is allowed to modify pending
169 * entries.
170 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000171 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700172};
173
Jens Axboeedafcce2019-01-09 09:16:05 -0700174struct io_mapped_ubuf {
175 u64 ubuf;
176 size_t len;
177 struct bio_vec *bvec;
178 unsigned int nr_bvecs;
179};
180
Jens Axboe65e19f52019-10-26 07:20:21 -0600181struct fixed_file_table {
182 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700183};
184
Jens Axboe05f3fb32019-12-09 11:22:50 -0700185enum {
186 FFD_F_ATOMIC,
187};
188
189struct fixed_file_data {
190 struct fixed_file_table *table;
191 struct io_ring_ctx *ctx;
192
193 struct percpu_ref refs;
194 struct llist_head put_llist;
195 unsigned long state;
196 struct work_struct ref_work;
197 struct completion done;
198};
199
Jens Axboe2b188cc2019-01-07 10:46:33 -0700200struct io_ring_ctx {
201 struct {
202 struct percpu_ref refs;
203 } ____cacheline_aligned_in_smp;
204
205 struct {
206 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800207 unsigned int compat: 1;
208 unsigned int account_mem: 1;
209 unsigned int cq_overflow_flushed: 1;
210 unsigned int drain_next: 1;
211 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700212
Hristo Venev75b28af2019-08-26 17:23:46 +0000213 /*
214 * Ring buffer of indices into array of io_uring_sqe, which is
215 * mmapped by the application using the IORING_OFF_SQES offset.
216 *
217 * This indirection could e.g. be used to assign fixed
218 * io_uring_sqe entries to operations and only submit them to
219 * the queue when needed.
220 *
221 * The kernel modifies neither the indices array nor the entries
222 * array.
223 */
224 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700225 unsigned cached_sq_head;
226 unsigned sq_entries;
227 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700228 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600229 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700230 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700231 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600232
233 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600234 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700235 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700236
Jens Axboefcb323c2019-10-24 12:39:47 -0600237 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700238 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 } ____cacheline_aligned_in_smp;
240
Hristo Venev75b28af2019-08-26 17:23:46 +0000241 struct io_rings *rings;
242
Jens Axboe2b188cc2019-01-07 10:46:33 -0700243 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600244 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700245 struct task_struct *sqo_thread; /* if using sq thread polling */
246 struct mm_struct *sqo_mm;
247 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700248
Jens Axboe6b063142019-01-10 22:13:58 -0700249 /*
250 * If used, fixed file set. Writers must ensure that ->refs is dead,
251 * readers must ensure that ->refs is alive as long as the file* is
252 * used. Only updated through io_uring_register(2).
253 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700254 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700255 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300256 int ring_fd;
257 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700258
Jens Axboeedafcce2019-01-09 09:16:05 -0700259 /* if used, fixed mapped user buffers */
260 unsigned nr_user_bufs;
261 struct io_mapped_ubuf *user_bufs;
262
Jens Axboe2b188cc2019-01-07 10:46:33 -0700263 struct user_struct *user;
264
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700265 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700266
Jens Axboe206aefd2019-11-07 18:27:42 -0700267 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
268 struct completion *completions;
269
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700270 /* if all else fails... */
271 struct io_kiocb *fallback_req;
272
Jens Axboe206aefd2019-11-07 18:27:42 -0700273#if defined(CONFIG_UNIX)
274 struct socket *ring_sock;
275#endif
276
Jens Axboe071698e2020-01-28 10:04:42 -0700277 struct idr personality_idr;
278
Jens Axboe206aefd2019-11-07 18:27:42 -0700279 struct {
280 unsigned cached_cq_tail;
281 unsigned cq_entries;
282 unsigned cq_mask;
283 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700284 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700285 struct wait_queue_head cq_wait;
286 struct fasync_struct *cq_fasync;
287 struct eventfd_ctx *cq_ev_fd;
288 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700289
290 struct {
291 struct mutex uring_lock;
292 wait_queue_head_t wait;
293 } ____cacheline_aligned_in_smp;
294
295 struct {
296 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700297 struct llist_head poll_llist;
298
Jens Axboedef596e2019-01-09 08:59:42 -0700299 /*
300 * ->poll_list is protected by the ctx->uring_lock for
301 * io_uring instances that don't use IORING_SETUP_SQPOLL.
302 * For SQPOLL, only the single threaded io_sq_thread() will
303 * manipulate the list, hence no extra locking is needed there.
304 */
305 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700306 struct hlist_head *cancel_hash;
307 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700308 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600309
310 spinlock_t inflight_lock;
311 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700312 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313};
314
Jens Axboe09bb8392019-03-13 12:39:28 -0600315/*
316 * First field must be the file pointer in all the
317 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
318 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700319struct io_poll_iocb {
320 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700321 union {
322 struct wait_queue_head *head;
323 u64 addr;
324 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700325 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600326 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700327 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700328 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700329};
330
Jens Axboeb5dba592019-12-11 14:02:38 -0700331struct io_close {
332 struct file *file;
333 struct file *put_file;
334 int fd;
335};
336
Jens Axboead8a48a2019-11-15 08:49:11 -0700337struct io_timeout_data {
338 struct io_kiocb *req;
339 struct hrtimer timer;
340 struct timespec64 ts;
341 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300342 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700343};
344
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700345struct io_accept {
346 struct file *file;
347 struct sockaddr __user *addr;
348 int __user *addr_len;
349 int flags;
350};
351
352struct io_sync {
353 struct file *file;
354 loff_t len;
355 loff_t off;
356 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700357 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700358};
359
Jens Axboefbf23842019-12-17 18:45:56 -0700360struct io_cancel {
361 struct file *file;
362 u64 addr;
363};
364
Jens Axboeb29472e2019-12-17 18:50:29 -0700365struct io_timeout {
366 struct file *file;
367 u64 addr;
368 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700369 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700370};
371
Jens Axboe9adbd452019-12-20 08:45:55 -0700372struct io_rw {
373 /* NOTE: kiocb has the file as the first member, so don't do it here */
374 struct kiocb kiocb;
375 u64 addr;
376 u64 len;
377};
378
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700379struct io_connect {
380 struct file *file;
381 struct sockaddr __user *addr;
382 int addr_len;
383};
384
Jens Axboee47293f2019-12-20 08:58:21 -0700385struct io_sr_msg {
386 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700387 union {
388 struct user_msghdr __user *msg;
389 void __user *buf;
390 };
Jens Axboee47293f2019-12-20 08:58:21 -0700391 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700392 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700393};
394
Jens Axboe15b71ab2019-12-11 11:20:36 -0700395struct io_open {
396 struct file *file;
397 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700398 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700399 unsigned mask;
400 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700401 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700402 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700403 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700404};
405
Jens Axboe05f3fb32019-12-09 11:22:50 -0700406struct io_files_update {
407 struct file *file;
408 u64 arg;
409 u32 nr_args;
410 u32 offset;
411};
412
Jens Axboe4840e412019-12-25 22:03:45 -0700413struct io_fadvise {
414 struct file *file;
415 u64 offset;
416 u32 len;
417 u32 advice;
418};
419
Jens Axboec1ca7572019-12-25 22:18:28 -0700420struct io_madvise {
421 struct file *file;
422 u64 addr;
423 u32 len;
424 u32 advice;
425};
426
Jens Axboe3e4827b2020-01-08 15:18:09 -0700427struct io_epoll {
428 struct file *file;
429 int epfd;
430 int op;
431 int fd;
432 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700433};
434
Jens Axboef499a022019-12-02 16:28:46 -0700435struct io_async_connect {
436 struct sockaddr_storage address;
437};
438
Jens Axboe03b12302019-12-02 18:50:25 -0700439struct io_async_msghdr {
440 struct iovec fast_iov[UIO_FASTIOV];
441 struct iovec *iov;
442 struct sockaddr __user *uaddr;
443 struct msghdr msg;
444};
445
Jens Axboef67676d2019-12-02 11:03:47 -0700446struct io_async_rw {
447 struct iovec fast_iov[UIO_FASTIOV];
448 struct iovec *iov;
449 ssize_t nr_segs;
450 ssize_t size;
451};
452
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700453struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700454 union {
455 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700456 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700457 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700458 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700459 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700460};
461
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300462enum {
463 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
464 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
465 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
466 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
467 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
468
469 REQ_F_LINK_NEXT_BIT,
470 REQ_F_FAIL_LINK_BIT,
471 REQ_F_INFLIGHT_BIT,
472 REQ_F_CUR_POS_BIT,
473 REQ_F_NOWAIT_BIT,
474 REQ_F_IOPOLL_COMPLETED_BIT,
475 REQ_F_LINK_TIMEOUT_BIT,
476 REQ_F_TIMEOUT_BIT,
477 REQ_F_ISREG_BIT,
478 REQ_F_MUST_PUNT_BIT,
479 REQ_F_TIMEOUT_NOSEQ_BIT,
480 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300481 REQ_F_NEED_CLEANUP_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),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300522};
523
Jens Axboe09bb8392019-03-13 12:39:28 -0600524/*
525 * NOTE! Each of the iocb union members has the file pointer
526 * as the first entry in their struct definition. So you can
527 * access the file pointer through any of the sub-structs,
528 * or directly as just 'ki_filp' in this struct.
529 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700530struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700531 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600532 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700533 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700534 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700535 struct io_accept accept;
536 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700537 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700538 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700539 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700540 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700541 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700542 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700543 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700544 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700545 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700546 struct io_epoll epoll;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700547 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700548
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700549 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300550 /*
551 * llist_node is only used for poll deferred completions
552 */
553 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300554 bool in_async;
555 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700556 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700557
558 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700559 union {
560 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700561 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700562 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600563 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700564 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700565 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700566 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600567 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600568 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700569
Jens Axboefcb323c2019-10-24 12:39:47 -0600570 struct list_head inflight_entry;
571
Jens Axboe561fb042019-10-24 07:25:42 -0600572 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700573};
574
575#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700576#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700577
Jens Axboe9a56a232019-01-09 09:06:50 -0700578struct io_submit_state {
579 struct blk_plug plug;
580
581 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700582 * io_kiocb alloc cache
583 */
584 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300585 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700586
587 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700588 * File reference cache
589 */
590 struct file *file;
591 unsigned int fd;
592 unsigned int has_refs;
593 unsigned int used_refs;
594 unsigned int ios_left;
595};
596
Jens Axboed3656342019-12-18 09:50:26 -0700597struct io_op_def {
598 /* needs req->io allocated for deferral/async */
599 unsigned async_ctx : 1;
600 /* needs current->mm setup, does mm access */
601 unsigned needs_mm : 1;
602 /* needs req->file assigned */
603 unsigned needs_file : 1;
604 /* needs req->file assigned IFF fd is >= 0 */
605 unsigned fd_non_neg : 1;
606 /* hash wq insertion if file is a regular file */
607 unsigned hash_reg_file : 1;
608 /* unbound wq insertion if file is a non-regular file */
609 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700610 /* opcode is not supported by this kernel */
611 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700612 /* needs file table */
613 unsigned file_table : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700614};
615
616static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300617 [IORING_OP_NOP] = {},
618 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700619 .async_ctx = 1,
620 .needs_mm = 1,
621 .needs_file = 1,
622 .unbound_nonreg_file = 1,
623 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300624 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700625 .async_ctx = 1,
626 .needs_mm = 1,
627 .needs_file = 1,
628 .hash_reg_file = 1,
629 .unbound_nonreg_file = 1,
630 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300631 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700632 .needs_file = 1,
633 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300634 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700635 .needs_file = 1,
636 .unbound_nonreg_file = 1,
637 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300638 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700639 .needs_file = 1,
640 .hash_reg_file = 1,
641 .unbound_nonreg_file = 1,
642 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300643 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700644 .needs_file = 1,
645 .unbound_nonreg_file = 1,
646 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300647 [IORING_OP_POLL_REMOVE] = {},
648 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700649 .needs_file = 1,
650 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300651 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700652 .async_ctx = 1,
653 .needs_mm = 1,
654 .needs_file = 1,
655 .unbound_nonreg_file = 1,
656 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300657 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700658 .async_ctx = 1,
659 .needs_mm = 1,
660 .needs_file = 1,
661 .unbound_nonreg_file = 1,
662 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300663 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700664 .async_ctx = 1,
665 .needs_mm = 1,
666 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300667 [IORING_OP_TIMEOUT_REMOVE] = {},
668 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700669 .needs_mm = 1,
670 .needs_file = 1,
671 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700672 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700673 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300674 [IORING_OP_ASYNC_CANCEL] = {},
675 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700676 .async_ctx = 1,
677 .needs_mm = 1,
678 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300679 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700680 .async_ctx = 1,
681 .needs_mm = 1,
682 .needs_file = 1,
683 .unbound_nonreg_file = 1,
684 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300685 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700686 .needs_file = 1,
687 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300688 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700689 .needs_file = 1,
690 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700691 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700692 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300693 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700694 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700695 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700696 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300697 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700698 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700699 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700700 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300701 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700702 .needs_mm = 1,
703 .needs_file = 1,
704 .fd_non_neg = 1,
705 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300706 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700707 .needs_mm = 1,
708 .needs_file = 1,
709 .unbound_nonreg_file = 1,
710 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300711 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700712 .needs_mm = 1,
713 .needs_file = 1,
714 .unbound_nonreg_file = 1,
715 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300716 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700717 .needs_file = 1,
718 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300719 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700720 .needs_mm = 1,
721 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300722 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700723 .needs_mm = 1,
724 .needs_file = 1,
725 .unbound_nonreg_file = 1,
726 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300727 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700728 .needs_mm = 1,
729 .needs_file = 1,
730 .unbound_nonreg_file = 1,
731 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300732 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700733 .needs_file = 1,
734 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700735 .file_table = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700736 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700737 [IORING_OP_EPOLL_CTL] = {
738 .unbound_nonreg_file = 1,
739 .file_table = 1,
740 },
Jens Axboed3656342019-12-18 09:50:26 -0700741};
742
Jens Axboe561fb042019-10-24 07:25:42 -0600743static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700744static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800745static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700746static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700747static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
748static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700749static int __io_sqe_files_update(struct io_ring_ctx *ctx,
750 struct io_uring_files_update *ip,
751 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700752static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700753static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300754static void io_cleanup_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600755
Jens Axboe2b188cc2019-01-07 10:46:33 -0700756static struct kmem_cache *req_cachep;
757
758static const struct file_operations io_uring_fops;
759
760struct sock *io_uring_get_socket(struct file *file)
761{
762#if defined(CONFIG_UNIX)
763 if (file->f_op == &io_uring_fops) {
764 struct io_ring_ctx *ctx = file->private_data;
765
766 return ctx->ring_sock->sk;
767 }
768#endif
769 return NULL;
770}
771EXPORT_SYMBOL(io_uring_get_socket);
772
773static void io_ring_ctx_ref_free(struct percpu_ref *ref)
774{
775 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
776
Jens Axboe206aefd2019-11-07 18:27:42 -0700777 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700778}
779
780static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
781{
782 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700783 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700784
785 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
786 if (!ctx)
787 return NULL;
788
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700789 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
790 if (!ctx->fallback_req)
791 goto err;
792
Jens Axboe206aefd2019-11-07 18:27:42 -0700793 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
794 if (!ctx->completions)
795 goto err;
796
Jens Axboe78076bb2019-12-04 19:56:40 -0700797 /*
798 * Use 5 bits less than the max cq entries, that should give us around
799 * 32 entries per hash list if totally full and uniformly spread.
800 */
801 hash_bits = ilog2(p->cq_entries);
802 hash_bits -= 5;
803 if (hash_bits <= 0)
804 hash_bits = 1;
805 ctx->cancel_hash_bits = hash_bits;
806 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
807 GFP_KERNEL);
808 if (!ctx->cancel_hash)
809 goto err;
810 __hash_init(ctx->cancel_hash, 1U << hash_bits);
811
Roman Gushchin21482892019-05-07 10:01:48 -0700812 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700813 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
814 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700815
816 ctx->flags = p->flags;
817 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700818 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700819 init_completion(&ctx->completions[0]);
820 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700821 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700822 mutex_init(&ctx->uring_lock);
823 init_waitqueue_head(&ctx->wait);
824 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700825 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700826 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600827 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600828 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600829 init_waitqueue_head(&ctx->inflight_wait);
830 spin_lock_init(&ctx->inflight_lock);
831 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700832 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700833err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700834 if (ctx->fallback_req)
835 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700836 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700837 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700838 kfree(ctx);
839 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700840}
841
Bob Liu9d858b22019-11-13 18:06:25 +0800842static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600843{
Jackie Liua197f662019-11-08 08:09:12 -0700844 struct io_ring_ctx *ctx = req->ctx;
845
Jens Axboe498ccd92019-10-25 10:04:25 -0600846 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
847 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600848}
849
Bob Liu9d858b22019-11-13 18:06:25 +0800850static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600851{
Pavel Begunkov87987892020-01-18 01:22:30 +0300852 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800853 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600854
Bob Liu9d858b22019-11-13 18:06:25 +0800855 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600856}
857
858static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600859{
860 struct io_kiocb *req;
861
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600862 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800863 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600864 list_del_init(&req->list);
865 return req;
866 }
867
868 return NULL;
869}
870
Jens Axboe5262f562019-09-17 12:26:57 -0600871static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
872{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600873 struct io_kiocb *req;
874
875 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700876 if (req) {
877 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
878 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800879 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700880 list_del_init(&req->list);
881 return req;
882 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600883 }
884
885 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600886}
887
Jens Axboede0617e2019-04-06 21:51:27 -0600888static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700889{
Hristo Venev75b28af2019-08-26 17:23:46 +0000890 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700891
Pavel Begunkov07910152020-01-17 03:52:46 +0300892 /* order cqe stores with ring update */
893 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700894
Pavel Begunkov07910152020-01-17 03:52:46 +0300895 if (wq_has_sleeper(&ctx->cq_wait)) {
896 wake_up_interruptible(&ctx->cq_wait);
897 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700898 }
899}
900
Jens Axboecccf0ee2020-01-27 16:34:48 -0700901static inline void io_req_work_grab_env(struct io_kiocb *req,
902 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600903{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700904 if (!req->work.mm && def->needs_mm) {
905 mmgrab(current->mm);
906 req->work.mm = current->mm;
907 }
908 if (!req->work.creds)
909 req->work.creds = get_current_cred();
910}
911
912static inline void io_req_work_drop_env(struct io_kiocb *req)
913{
914 if (req->work.mm) {
915 mmdrop(req->work.mm);
916 req->work.mm = NULL;
917 }
918 if (req->work.creds) {
919 put_cred(req->work.creds);
920 req->work.creds = NULL;
921 }
Jens Axboe561fb042019-10-24 07:25:42 -0600922}
923
Jens Axboe94ae5e72019-11-14 19:39:52 -0700924static inline bool io_prep_async_work(struct io_kiocb *req,
925 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600926{
Jens Axboed3656342019-12-18 09:50:26 -0700927 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600928 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600929
Jens Axboed3656342019-12-18 09:50:26 -0700930 if (req->flags & REQ_F_ISREG) {
931 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700932 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700933 } else {
934 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700935 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600936 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700937
938 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600939
Jens Axboe94ae5e72019-11-14 19:39:52 -0700940 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600941 return do_hashed;
942}
943
Jackie Liua197f662019-11-08 08:09:12 -0700944static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600945{
Jackie Liua197f662019-11-08 08:09:12 -0700946 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700947 struct io_kiocb *link;
948 bool do_hashed;
949
950 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600951
952 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
953 req->flags);
954 if (!do_hashed) {
955 io_wq_enqueue(ctx->io_wq, &req->work);
956 } else {
957 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
958 file_inode(req->file));
959 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700960
961 if (link)
962 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600963}
964
Jens Axboe5262f562019-09-17 12:26:57 -0600965static void io_kill_timeout(struct io_kiocb *req)
966{
967 int ret;
968
Jens Axboe2d283902019-12-04 11:08:05 -0700969 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600970 if (ret != -1) {
971 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600972 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700973 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800974 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600975 }
976}
977
978static void io_kill_timeouts(struct io_ring_ctx *ctx)
979{
980 struct io_kiocb *req, *tmp;
981
982 spin_lock_irq(&ctx->completion_lock);
983 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
984 io_kill_timeout(req);
985 spin_unlock_irq(&ctx->completion_lock);
986}
987
Jens Axboede0617e2019-04-06 21:51:27 -0600988static void io_commit_cqring(struct io_ring_ctx *ctx)
989{
990 struct io_kiocb *req;
991
Jens Axboe5262f562019-09-17 12:26:57 -0600992 while ((req = io_get_timeout_req(ctx)) != NULL)
993 io_kill_timeout(req);
994
Jens Axboede0617e2019-04-06 21:51:27 -0600995 __io_commit_cqring(ctx);
996
Pavel Begunkov87987892020-01-18 01:22:30 +0300997 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -0700998 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600999}
1000
Jens Axboe2b188cc2019-01-07 10:46:33 -07001001static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1002{
Hristo Venev75b28af2019-08-26 17:23:46 +00001003 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001004 unsigned tail;
1005
1006 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001007 /*
1008 * writes to the cq entry need to come after reading head; the
1009 * control dependency is enough as we're using WRITE_ONCE to
1010 * fill the cq entry
1011 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001012 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001013 return NULL;
1014
1015 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001016 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001017}
1018
Jens Axboef2842ab2020-01-08 11:04:00 -07001019static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1020{
Jens Axboef0b493e2020-02-01 21:30:11 -07001021 if (!ctx->cq_ev_fd)
1022 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001023 if (!ctx->eventfd_async)
1024 return true;
1025 return io_wq_current_is_worker() || in_interrupt();
1026}
1027
Jens Axboef0b493e2020-02-01 21:30:11 -07001028static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001029{
1030 if (waitqueue_active(&ctx->wait))
1031 wake_up(&ctx->wait);
1032 if (waitqueue_active(&ctx->sqo_wait))
1033 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001034 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001035 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001036}
1037
Jens Axboef0b493e2020-02-01 21:30:11 -07001038static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1039{
1040 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1041}
1042
Jens Axboec4a2ed72019-11-21 21:01:26 -07001043/* Returns true if there are no backlogged entries after the flush */
1044static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001045{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001046 struct io_rings *rings = ctx->rings;
1047 struct io_uring_cqe *cqe;
1048 struct io_kiocb *req;
1049 unsigned long flags;
1050 LIST_HEAD(list);
1051
1052 if (!force) {
1053 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001054 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001055 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1056 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001057 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001058 }
1059
1060 spin_lock_irqsave(&ctx->completion_lock, flags);
1061
1062 /* if force is set, the ring is going away. always drop after that */
1063 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001064 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001065
Jens Axboec4a2ed72019-11-21 21:01:26 -07001066 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001067 while (!list_empty(&ctx->cq_overflow_list)) {
1068 cqe = io_get_cqring(ctx);
1069 if (!cqe && !force)
1070 break;
1071
1072 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1073 list);
1074 list_move(&req->list, &list);
1075 if (cqe) {
1076 WRITE_ONCE(cqe->user_data, req->user_data);
1077 WRITE_ONCE(cqe->res, req->result);
1078 WRITE_ONCE(cqe->flags, 0);
1079 } else {
1080 WRITE_ONCE(ctx->rings->cq_overflow,
1081 atomic_inc_return(&ctx->cached_cq_overflow));
1082 }
1083 }
1084
1085 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001086 if (cqe) {
1087 clear_bit(0, &ctx->sq_check_overflow);
1088 clear_bit(0, &ctx->cq_check_overflow);
1089 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001090 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1091 io_cqring_ev_posted(ctx);
1092
1093 while (!list_empty(&list)) {
1094 req = list_first_entry(&list, struct io_kiocb, list);
1095 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001096 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001097 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001098
1099 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001100}
1101
Jens Axboe78e19bb2019-11-06 15:21:34 -07001102static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001103{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001104 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001105 struct io_uring_cqe *cqe;
1106
Jens Axboe78e19bb2019-11-06 15:21:34 -07001107 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001108
Jens Axboe2b188cc2019-01-07 10:46:33 -07001109 /*
1110 * If we can't get a cq entry, userspace overflowed the
1111 * submission (by quite a lot). Increment the overflow count in
1112 * the ring.
1113 */
1114 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001115 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001116 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001117 WRITE_ONCE(cqe->res, res);
1118 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001119 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001120 WRITE_ONCE(ctx->rings->cq_overflow,
1121 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001122 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001123 if (list_empty(&ctx->cq_overflow_list)) {
1124 set_bit(0, &ctx->sq_check_overflow);
1125 set_bit(0, &ctx->cq_check_overflow);
1126 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001127 refcount_inc(&req->refs);
1128 req->result = res;
1129 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001130 }
1131}
1132
Jens Axboe78e19bb2019-11-06 15:21:34 -07001133static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001134{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001135 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001136 unsigned long flags;
1137
1138 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001139 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001140 io_commit_cqring(ctx);
1141 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1142
Jens Axboe8c838782019-03-12 15:48:16 -06001143 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001144}
1145
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001146static inline bool io_is_fallback_req(struct io_kiocb *req)
1147{
1148 return req == (struct io_kiocb *)
1149 ((unsigned long) req->ctx->fallback_req & ~1UL);
1150}
1151
1152static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1153{
1154 struct io_kiocb *req;
1155
1156 req = ctx->fallback_req;
1157 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1158 return req;
1159
1160 return NULL;
1161}
1162
Jens Axboe2579f912019-01-09 09:10:43 -07001163static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1164 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001165{
Jens Axboefd6fab22019-03-14 16:30:06 -06001166 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001167 struct io_kiocb *req;
1168
Jens Axboe2579f912019-01-09 09:10:43 -07001169 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001170 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001171 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001172 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001173 } else if (!state->free_reqs) {
1174 size_t sz;
1175 int ret;
1176
1177 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001178 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1179
1180 /*
1181 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1182 * retry single alloc to be on the safe side.
1183 */
1184 if (unlikely(ret <= 0)) {
1185 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1186 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001187 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001188 ret = 1;
1189 }
Jens Axboe2579f912019-01-09 09:10:43 -07001190 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001191 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001192 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001193 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001194 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001195 }
1196
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001197got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001198 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001199 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001200 req->ctx = ctx;
1201 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001202 /* one is dropped after submission, the other at completion */
1203 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001204 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001205 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001206 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001207fallback:
1208 req = io_get_fallback_req(ctx);
1209 if (req)
1210 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001211 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001212 return NULL;
1213}
1214
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001215static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001216{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001217 if (likely(!io_is_fallback_req(req)))
1218 kmem_cache_free(req_cachep, req);
1219 else
1220 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1221}
1222
Jens Axboec6ca97b302019-12-28 12:11:08 -07001223static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001224{
Jens Axboefcb323c2019-10-24 12:39:47 -06001225 struct io_ring_ctx *ctx = req->ctx;
1226
YueHaibing96fd84d2020-01-07 22:22:44 +08001227 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001228 if (req->file) {
1229 if (req->flags & REQ_F_FIXED_FILE)
1230 percpu_ref_put(&ctx->file_data->refs);
1231 else
1232 fput(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001233 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001234
1235 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001236}
1237
1238static void __io_free_req(struct io_kiocb *req)
1239{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001240 __io_req_aux_free(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001241
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03001242 if (req->flags & REQ_F_NEED_CLEANUP)
1243 io_cleanup_req(req);
1244
Jens Axboefcb323c2019-10-24 12:39:47 -06001245 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001246 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001247 unsigned long flags;
1248
1249 spin_lock_irqsave(&ctx->inflight_lock, flags);
1250 list_del(&req->inflight_entry);
1251 if (waitqueue_active(&ctx->inflight_wait))
1252 wake_up(&ctx->inflight_wait);
1253 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1254 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001255
1256 percpu_ref_put(&req->ctx->refs);
1257 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001258}
1259
Jens Axboec6ca97b302019-12-28 12:11:08 -07001260struct req_batch {
1261 void *reqs[IO_IOPOLL_BATCH];
1262 int to_free;
1263 int need_iter;
1264};
1265
1266static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1267{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001268 int fixed_refs = rb->to_free;
1269
Jens Axboec6ca97b302019-12-28 12:11:08 -07001270 if (!rb->to_free)
1271 return;
1272 if (rb->need_iter) {
1273 int i, inflight = 0;
1274 unsigned long flags;
1275
Jens Axboe10fef4b2020-01-09 07:52:28 -07001276 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001277 for (i = 0; i < rb->to_free; i++) {
1278 struct io_kiocb *req = rb->reqs[i];
1279
Jens Axboe10fef4b2020-01-09 07:52:28 -07001280 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001281 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001282 fixed_refs++;
1283 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001284 if (req->flags & REQ_F_INFLIGHT)
1285 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001286 __io_req_aux_free(req);
1287 }
1288 if (!inflight)
1289 goto do_free;
1290
1291 spin_lock_irqsave(&ctx->inflight_lock, flags);
1292 for (i = 0; i < rb->to_free; i++) {
1293 struct io_kiocb *req = rb->reqs[i];
1294
Jens Axboe10fef4b2020-01-09 07:52:28 -07001295 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001296 list_del(&req->inflight_entry);
1297 if (!--inflight)
1298 break;
1299 }
1300 }
1301 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1302
1303 if (waitqueue_active(&ctx->inflight_wait))
1304 wake_up(&ctx->inflight_wait);
1305 }
1306do_free:
1307 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001308 if (fixed_refs)
1309 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001310 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001311 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001312}
1313
Jackie Liua197f662019-11-08 08:09:12 -07001314static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001315{
Jackie Liua197f662019-11-08 08:09:12 -07001316 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001317 int ret;
1318
Jens Axboe2d283902019-12-04 11:08:05 -07001319 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001320 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001321 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001322 io_commit_cqring(ctx);
1323 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001324 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001325 return true;
1326 }
1327
1328 return false;
1329}
1330
Jens Axboeba816ad2019-09-28 11:36:45 -06001331static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001332{
Jens Axboe2665abf2019-11-05 12:40:47 -07001333 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001334 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001335
Jens Axboe4d7dd462019-11-20 13:03:52 -07001336 /* Already got next link */
1337 if (req->flags & REQ_F_LINK_NEXT)
1338 return;
1339
Jens Axboe9e645e112019-05-10 16:07:28 -06001340 /*
1341 * The list should never be empty when we are called here. But could
1342 * potentially happen if the chain is messed up, check to be on the
1343 * safe side.
1344 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001345 while (!list_empty(&req->link_list)) {
1346 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1347 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001348
Pavel Begunkov44932332019-12-05 16:16:35 +03001349 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1350 (nxt->flags & REQ_F_TIMEOUT))) {
1351 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001352 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001353 req->flags &= ~REQ_F_LINK_TIMEOUT;
1354 continue;
1355 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001356
Pavel Begunkov44932332019-12-05 16:16:35 +03001357 list_del_init(&req->link_list);
1358 if (!list_empty(&nxt->link_list))
1359 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001360 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001361 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001362 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001363
Jens Axboe4d7dd462019-11-20 13:03:52 -07001364 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001365 if (wake_ev)
1366 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001367}
1368
1369/*
1370 * Called if REQ_F_LINK is set, and we fail the head request
1371 */
1372static void io_fail_links(struct io_kiocb *req)
1373{
Jens Axboe2665abf2019-11-05 12:40:47 -07001374 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001375 unsigned long flags;
1376
1377 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001378
1379 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001380 struct io_kiocb *link = list_first_entry(&req->link_list,
1381 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001382
Pavel Begunkov44932332019-12-05 16:16:35 +03001383 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001384 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001385
1386 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001387 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001388 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001389 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001390 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001391 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001392 }
Jens Axboe5d960722019-11-19 15:31:28 -07001393 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001394 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001395
1396 io_commit_cqring(ctx);
1397 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1398 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001399}
1400
Jens Axboe4d7dd462019-11-20 13:03:52 -07001401static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001402{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001403 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001404 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001405
Jens Axboe9e645e112019-05-10 16:07:28 -06001406 /*
1407 * If LINK is set, we have dependent requests in this chain. If we
1408 * didn't fail this request, queue the first one up, moving any other
1409 * dependencies to the next request. In case of failure, fail the rest
1410 * of the chain.
1411 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001412 if (req->flags & REQ_F_FAIL_LINK) {
1413 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001414 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1415 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001416 struct io_ring_ctx *ctx = req->ctx;
1417 unsigned long flags;
1418
1419 /*
1420 * If this is a timeout link, we could be racing with the
1421 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001422 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001423 */
1424 spin_lock_irqsave(&ctx->completion_lock, flags);
1425 io_req_link_next(req, nxt);
1426 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1427 } else {
1428 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001429 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001430}
Jens Axboe9e645e112019-05-10 16:07:28 -06001431
Jackie Liuc69f8db2019-11-09 11:00:08 +08001432static void io_free_req(struct io_kiocb *req)
1433{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001434 struct io_kiocb *nxt = NULL;
1435
1436 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001437 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001438
1439 if (nxt)
1440 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001441}
1442
Jens Axboeba816ad2019-09-28 11:36:45 -06001443/*
1444 * Drop reference to request, return next in chain (if there is one) if this
1445 * was the last reference to this request.
1446 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001447__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001448static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001449{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001450 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001451
Jens Axboee65ef562019-03-12 10:16:44 -06001452 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001453 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001454}
1455
Jens Axboe2b188cc2019-01-07 10:46:33 -07001456static void io_put_req(struct io_kiocb *req)
1457{
Jens Axboedef596e2019-01-09 08:59:42 -07001458 if (refcount_dec_and_test(&req->refs))
1459 io_free_req(req);
1460}
1461
Jens Axboe978db572019-11-14 22:39:04 -07001462/*
1463 * Must only be used if we don't need to care about links, usually from
1464 * within the completion handling itself.
1465 */
1466static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001467{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001468 /* drop both submit and complete references */
1469 if (refcount_sub_and_test(2, &req->refs))
1470 __io_free_req(req);
1471}
1472
Jens Axboe978db572019-11-14 22:39:04 -07001473static void io_double_put_req(struct io_kiocb *req)
1474{
1475 /* drop both submit and complete references */
1476 if (refcount_sub_and_test(2, &req->refs))
1477 io_free_req(req);
1478}
1479
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001480static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001481{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001482 struct io_rings *rings = ctx->rings;
1483
Jens Axboead3eb2c2019-12-18 17:12:20 -07001484 if (test_bit(0, &ctx->cq_check_overflow)) {
1485 /*
1486 * noflush == true is from the waitqueue handler, just ensure
1487 * we wake up the task, and the next invocation will flush the
1488 * entries. We cannot safely to it from here.
1489 */
1490 if (noflush && !list_empty(&ctx->cq_overflow_list))
1491 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001492
Jens Axboead3eb2c2019-12-18 17:12:20 -07001493 io_cqring_overflow_flush(ctx, false);
1494 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001495
Jens Axboea3a0e432019-08-20 11:03:11 -06001496 /* See comment at the top of this file */
1497 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001498 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001499}
1500
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001501static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1502{
1503 struct io_rings *rings = ctx->rings;
1504
1505 /* make sure SQ entry isn't read before tail */
1506 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1507}
1508
Jens Axboe8237e042019-12-28 10:48:22 -07001509static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001510{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001511 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1512 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001513
Jens Axboec6ca97b302019-12-28 12:11:08 -07001514 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1515 rb->need_iter++;
1516
1517 rb->reqs[rb->to_free++] = req;
1518 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1519 io_free_req_many(req->ctx, rb);
1520 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001521}
1522
Jens Axboedef596e2019-01-09 08:59:42 -07001523/*
1524 * Find and free completed poll iocbs
1525 */
1526static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1527 struct list_head *done)
1528{
Jens Axboe8237e042019-12-28 10:48:22 -07001529 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001530 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001531
Jens Axboec6ca97b302019-12-28 12:11:08 -07001532 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001533 while (!list_empty(done)) {
1534 req = list_first_entry(done, struct io_kiocb, list);
1535 list_del(&req->list);
1536
Jens Axboe78e19bb2019-11-06 15:21:34 -07001537 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001538 (*nr_events)++;
1539
Jens Axboe8237e042019-12-28 10:48:22 -07001540 if (refcount_dec_and_test(&req->refs) &&
1541 !io_req_multi_free(&rb, req))
1542 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001543 }
Jens Axboedef596e2019-01-09 08:59:42 -07001544
Jens Axboe09bb8392019-03-13 12:39:28 -06001545 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001546 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001547}
1548
1549static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1550 long min)
1551{
1552 struct io_kiocb *req, *tmp;
1553 LIST_HEAD(done);
1554 bool spin;
1555 int ret;
1556
1557 /*
1558 * Only spin for completions if we don't have multiple devices hanging
1559 * off our complete list, and we're under the requested amount.
1560 */
1561 spin = !ctx->poll_multi_file && *nr_events < min;
1562
1563 ret = 0;
1564 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001565 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001566
1567 /*
1568 * Move completed entries to our local list. If we find a
1569 * request that requires polling, break out and complete
1570 * the done list first, if we have entries there.
1571 */
1572 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1573 list_move_tail(&req->list, &done);
1574 continue;
1575 }
1576 if (!list_empty(&done))
1577 break;
1578
1579 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1580 if (ret < 0)
1581 break;
1582
1583 if (ret && spin)
1584 spin = false;
1585 ret = 0;
1586 }
1587
1588 if (!list_empty(&done))
1589 io_iopoll_complete(ctx, nr_events, &done);
1590
1591 return ret;
1592}
1593
1594/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001595 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001596 * non-spinning poll check - we'll still enter the driver poll loop, but only
1597 * as a non-spinning completion check.
1598 */
1599static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1600 long min)
1601{
Jens Axboe08f54392019-08-21 22:19:11 -06001602 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001603 int ret;
1604
1605 ret = io_do_iopoll(ctx, nr_events, min);
1606 if (ret < 0)
1607 return ret;
1608 if (!min || *nr_events >= min)
1609 return 0;
1610 }
1611
1612 return 1;
1613}
1614
1615/*
1616 * We can't just wait for polled events to come to us, we have to actively
1617 * find and complete them.
1618 */
1619static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1620{
1621 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1622 return;
1623
1624 mutex_lock(&ctx->uring_lock);
1625 while (!list_empty(&ctx->poll_list)) {
1626 unsigned int nr_events = 0;
1627
1628 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001629
1630 /*
1631 * Ensure we allow local-to-the-cpu processing to take place,
1632 * in this case we need to ensure that we reap all events.
1633 */
1634 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001635 }
1636 mutex_unlock(&ctx->uring_lock);
1637}
1638
Jens Axboe2b2ed972019-10-25 10:06:15 -06001639static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1640 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001641{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001642 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001643
1644 do {
1645 int tmin = 0;
1646
Jens Axboe500f9fb2019-08-19 12:15:59 -06001647 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001648 * Don't enter poll loop if we already have events pending.
1649 * If we do, we can potentially be spinning for commands that
1650 * already triggered a CQE (eg in error).
1651 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001652 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001653 break;
1654
1655 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001656 * If a submit got punted to a workqueue, we can have the
1657 * application entering polling for a command before it gets
1658 * issued. That app will hold the uring_lock for the duration
1659 * of the poll right here, so we need to take a breather every
1660 * now and then to ensure that the issue has a chance to add
1661 * the poll to the issued list. Otherwise we can spin here
1662 * forever, while the workqueue is stuck trying to acquire the
1663 * very same mutex.
1664 */
1665 if (!(++iters & 7)) {
1666 mutex_unlock(&ctx->uring_lock);
1667 mutex_lock(&ctx->uring_lock);
1668 }
1669
Jens Axboedef596e2019-01-09 08:59:42 -07001670 if (*nr_events < min)
1671 tmin = min - *nr_events;
1672
1673 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1674 if (ret <= 0)
1675 break;
1676 ret = 0;
1677 } while (min && !*nr_events && !need_resched());
1678
Jens Axboe2b2ed972019-10-25 10:06:15 -06001679 return ret;
1680}
1681
1682static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1683 long min)
1684{
1685 int ret;
1686
1687 /*
1688 * We disallow the app entering submit/complete with polling, but we
1689 * still need to lock the ring to prevent racing with polled issue
1690 * that got punted to a workqueue.
1691 */
1692 mutex_lock(&ctx->uring_lock);
1693 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001694 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001695 return ret;
1696}
1697
Jens Axboe491381ce2019-10-17 09:20:46 -06001698static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001699{
Jens Axboe491381ce2019-10-17 09:20:46 -06001700 /*
1701 * Tell lockdep we inherited freeze protection from submission
1702 * thread.
1703 */
1704 if (req->flags & REQ_F_ISREG) {
1705 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001706
Jens Axboe491381ce2019-10-17 09:20:46 -06001707 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001709 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001710}
1711
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001712static inline void req_set_fail_links(struct io_kiocb *req)
1713{
1714 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1715 req->flags |= REQ_F_FAIL_LINK;
1716}
1717
Jens Axboeba816ad2019-09-28 11:36:45 -06001718static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001719{
Jens Axboe9adbd452019-12-20 08:45:55 -07001720 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001721
Jens Axboe491381ce2019-10-17 09:20:46 -06001722 if (kiocb->ki_flags & IOCB_WRITE)
1723 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001724
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001725 if (res != req->result)
1726 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001727 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001728}
1729
1730static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1731{
Jens Axboe9adbd452019-12-20 08:45:55 -07001732 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001733
1734 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001735 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001736}
1737
Jens Axboeba816ad2019-09-28 11:36:45 -06001738static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1739{
Jens Axboe9adbd452019-12-20 08:45:55 -07001740 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001741 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001742
1743 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001744 io_put_req_find_next(req, &nxt);
1745
1746 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001747}
1748
Jens Axboedef596e2019-01-09 08:59:42 -07001749static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1750{
Jens Axboe9adbd452019-12-20 08:45:55 -07001751 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001752
Jens Axboe491381ce2019-10-17 09:20:46 -06001753 if (kiocb->ki_flags & IOCB_WRITE)
1754 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001755
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001756 if (res != req->result)
1757 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001758 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001759 if (res != -EAGAIN)
1760 req->flags |= REQ_F_IOPOLL_COMPLETED;
1761}
1762
1763/*
1764 * After the iocb has been issued, it's safe to be found on the poll list.
1765 * Adding the kiocb to the list AFTER submission ensures that we don't
1766 * find it from a io_iopoll_getevents() thread before the issuer is done
1767 * accessing the kiocb cookie.
1768 */
1769static void io_iopoll_req_issued(struct io_kiocb *req)
1770{
1771 struct io_ring_ctx *ctx = req->ctx;
1772
1773 /*
1774 * Track whether we have multiple files in our lists. This will impact
1775 * how we do polling eventually, not spinning if we're on potentially
1776 * different devices.
1777 */
1778 if (list_empty(&ctx->poll_list)) {
1779 ctx->poll_multi_file = false;
1780 } else if (!ctx->poll_multi_file) {
1781 struct io_kiocb *list_req;
1782
1783 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1784 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001785 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001786 ctx->poll_multi_file = true;
1787 }
1788
1789 /*
1790 * For fast devices, IO may have already completed. If it has, add
1791 * it to the front so we find it first.
1792 */
1793 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1794 list_add(&req->list, &ctx->poll_list);
1795 else
1796 list_add_tail(&req->list, &ctx->poll_list);
1797}
1798
Jens Axboe3d6770f2019-04-13 11:50:54 -06001799static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001800{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001801 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001802 int diff = state->has_refs - state->used_refs;
1803
1804 if (diff)
1805 fput_many(state->file, diff);
1806 state->file = NULL;
1807 }
1808}
1809
1810/*
1811 * Get as many references to a file as we have IOs left in this submission,
1812 * assuming most submissions are for one file, or at least that each file
1813 * has more than one submission.
1814 */
1815static struct file *io_file_get(struct io_submit_state *state, int fd)
1816{
1817 if (!state)
1818 return fget(fd);
1819
1820 if (state->file) {
1821 if (state->fd == fd) {
1822 state->used_refs++;
1823 state->ios_left--;
1824 return state->file;
1825 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001826 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001827 }
1828 state->file = fget_many(fd, state->ios_left);
1829 if (!state->file)
1830 return NULL;
1831
1832 state->fd = fd;
1833 state->has_refs = state->ios_left;
1834 state->used_refs = 1;
1835 state->ios_left--;
1836 return state->file;
1837}
1838
Jens Axboe2b188cc2019-01-07 10:46:33 -07001839/*
1840 * If we tracked the file through the SCM inflight mechanism, we could support
1841 * any file. For now, just ensure that anything potentially problematic is done
1842 * inline.
1843 */
1844static bool io_file_supports_async(struct file *file)
1845{
1846 umode_t mode = file_inode(file)->i_mode;
1847
Jens Axboe10d59342019-12-09 20:16:22 -07001848 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001849 return true;
1850 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1851 return true;
1852
1853 return false;
1854}
1855
Jens Axboe3529d8c2019-12-19 18:24:38 -07001856static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1857 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001858{
Jens Axboedef596e2019-01-09 08:59:42 -07001859 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001860 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001861 unsigned ioprio;
1862 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001863
Jens Axboe491381ce2019-10-17 09:20:46 -06001864 if (S_ISREG(file_inode(req->file)->i_mode))
1865 req->flags |= REQ_F_ISREG;
1866
Jens Axboe2b188cc2019-01-07 10:46:33 -07001867 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001868 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1869 req->flags |= REQ_F_CUR_POS;
1870 kiocb->ki_pos = req->file->f_pos;
1871 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001872 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001873 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1874 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1875 if (unlikely(ret))
1876 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001877
1878 ioprio = READ_ONCE(sqe->ioprio);
1879 if (ioprio) {
1880 ret = ioprio_check_cap(ioprio);
1881 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001882 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001883
1884 kiocb->ki_ioprio = ioprio;
1885 } else
1886 kiocb->ki_ioprio = get_current_ioprio();
1887
Stefan Bühler8449eed2019-04-27 20:34:19 +02001888 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001889 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1890 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001891 req->flags |= REQ_F_NOWAIT;
1892
1893 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001894 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001895
Jens Axboedef596e2019-01-09 08:59:42 -07001896 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001897 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1898 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001899 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001900
Jens Axboedef596e2019-01-09 08:59:42 -07001901 kiocb->ki_flags |= IOCB_HIPRI;
1902 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001903 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001904 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001905 if (kiocb->ki_flags & IOCB_HIPRI)
1906 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001907 kiocb->ki_complete = io_complete_rw;
1908 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001909
Jens Axboe3529d8c2019-12-19 18:24:38 -07001910 req->rw.addr = READ_ONCE(sqe->addr);
1911 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001912 /* we own ->private, reuse it for the buffer index */
1913 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001914 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001915 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001916}
1917
1918static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1919{
1920 switch (ret) {
1921 case -EIOCBQUEUED:
1922 break;
1923 case -ERESTARTSYS:
1924 case -ERESTARTNOINTR:
1925 case -ERESTARTNOHAND:
1926 case -ERESTART_RESTARTBLOCK:
1927 /*
1928 * We can't just restart the syscall, since previously
1929 * submitted sqes may already be in progress. Just fail this
1930 * IO with EINTR.
1931 */
1932 ret = -EINTR;
1933 /* fall through */
1934 default:
1935 kiocb->ki_complete(kiocb, ret, 0);
1936 }
1937}
1938
Jens Axboeba816ad2019-09-28 11:36:45 -06001939static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1940 bool in_async)
1941{
Jens Axboeba042912019-12-25 16:33:42 -07001942 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1943
1944 if (req->flags & REQ_F_CUR_POS)
1945 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001946 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001947 *nxt = __io_complete_rw(kiocb, ret);
1948 else
1949 io_rw_done(kiocb, ret);
1950}
1951
Jens Axboe9adbd452019-12-20 08:45:55 -07001952static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001953 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001954{
Jens Axboe9adbd452019-12-20 08:45:55 -07001955 struct io_ring_ctx *ctx = req->ctx;
1956 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001957 struct io_mapped_ubuf *imu;
1958 unsigned index, buf_index;
1959 size_t offset;
1960 u64 buf_addr;
1961
1962 /* attempt to use fixed buffers without having provided iovecs */
1963 if (unlikely(!ctx->user_bufs))
1964 return -EFAULT;
1965
Jens Axboe9adbd452019-12-20 08:45:55 -07001966 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001967 if (unlikely(buf_index >= ctx->nr_user_bufs))
1968 return -EFAULT;
1969
1970 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1971 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001972 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001973
1974 /* overflow */
1975 if (buf_addr + len < buf_addr)
1976 return -EFAULT;
1977 /* not inside the mapped region */
1978 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1979 return -EFAULT;
1980
1981 /*
1982 * May not be a start of buffer, set size appropriately
1983 * and advance us to the beginning.
1984 */
1985 offset = buf_addr - imu->ubuf;
1986 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001987
1988 if (offset) {
1989 /*
1990 * Don't use iov_iter_advance() here, as it's really slow for
1991 * using the latter parts of a big fixed buffer - it iterates
1992 * over each segment manually. We can cheat a bit here, because
1993 * we know that:
1994 *
1995 * 1) it's a BVEC iter, we set it up
1996 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1997 * first and last bvec
1998 *
1999 * So just find our index, and adjust the iterator afterwards.
2000 * If the offset is within the first bvec (or the whole first
2001 * bvec, just use iov_iter_advance(). This makes it easier
2002 * since we can just skip the first segment, which may not
2003 * be PAGE_SIZE aligned.
2004 */
2005 const struct bio_vec *bvec = imu->bvec;
2006
2007 if (offset <= bvec->bv_len) {
2008 iov_iter_advance(iter, offset);
2009 } else {
2010 unsigned long seg_skip;
2011
2012 /* skip first vec */
2013 offset -= bvec->bv_len;
2014 seg_skip = 1 + (offset >> PAGE_SHIFT);
2015
2016 iter->bvec = bvec + seg_skip;
2017 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002018 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002019 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002020 }
2021 }
2022
Jens Axboe5e559562019-11-13 16:12:46 -07002023 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002024}
2025
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002026static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2027 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002028{
Jens Axboe9adbd452019-12-20 08:45:55 -07002029 void __user *buf = u64_to_user_ptr(req->rw.addr);
2030 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002031 u8 opcode;
2032
Jens Axboed625c6e2019-12-17 19:53:05 -07002033 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002034 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002035 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002036 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002037 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002038
Jens Axboe9adbd452019-12-20 08:45:55 -07002039 /* buffer index only valid with fixed read/write */
2040 if (req->rw.kiocb.private)
2041 return -EINVAL;
2042
Jens Axboe3a6820f2019-12-22 15:19:35 -07002043 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2044 ssize_t ret;
2045 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2046 *iovec = NULL;
2047 return ret;
2048 }
2049
Jens Axboef67676d2019-12-02 11:03:47 -07002050 if (req->io) {
2051 struct io_async_rw *iorw = &req->io->rw;
2052
2053 *iovec = iorw->iov;
2054 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2055 if (iorw->iov == iorw->fast_iov)
2056 *iovec = NULL;
2057 return iorw->size;
2058 }
2059
Jens Axboe2b188cc2019-01-07 10:46:33 -07002060#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002061 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002062 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2063 iovec, iter);
2064#endif
2065
2066 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2067}
2068
Jens Axboe32960612019-09-23 11:05:34 -06002069/*
2070 * For files that don't have ->read_iter() and ->write_iter(), handle them
2071 * by looping over ->read() or ->write() manually.
2072 */
2073static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2074 struct iov_iter *iter)
2075{
2076 ssize_t ret = 0;
2077
2078 /*
2079 * Don't support polled IO through this interface, and we can't
2080 * support non-blocking either. For the latter, this just causes
2081 * the kiocb to be handled from an async context.
2082 */
2083 if (kiocb->ki_flags & IOCB_HIPRI)
2084 return -EOPNOTSUPP;
2085 if (kiocb->ki_flags & IOCB_NOWAIT)
2086 return -EAGAIN;
2087
2088 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002089 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002090 ssize_t nr;
2091
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002092 if (!iov_iter_is_bvec(iter)) {
2093 iovec = iov_iter_iovec(iter);
2094 } else {
2095 /* fixed buffers import bvec */
2096 iovec.iov_base = kmap(iter->bvec->bv_page)
2097 + iter->iov_offset;
2098 iovec.iov_len = min(iter->count,
2099 iter->bvec->bv_len - iter->iov_offset);
2100 }
2101
Jens Axboe32960612019-09-23 11:05:34 -06002102 if (rw == READ) {
2103 nr = file->f_op->read(file, iovec.iov_base,
2104 iovec.iov_len, &kiocb->ki_pos);
2105 } else {
2106 nr = file->f_op->write(file, iovec.iov_base,
2107 iovec.iov_len, &kiocb->ki_pos);
2108 }
2109
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002110 if (iov_iter_is_bvec(iter))
2111 kunmap(iter->bvec->bv_page);
2112
Jens Axboe32960612019-09-23 11:05:34 -06002113 if (nr < 0) {
2114 if (!ret)
2115 ret = nr;
2116 break;
2117 }
2118 ret += nr;
2119 if (nr != iovec.iov_len)
2120 break;
2121 iov_iter_advance(iter, nr);
2122 }
2123
2124 return ret;
2125}
2126
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002127static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002128 struct iovec *iovec, struct iovec *fast_iov,
2129 struct iov_iter *iter)
2130{
2131 req->io->rw.nr_segs = iter->nr_segs;
2132 req->io->rw.size = io_size;
2133 req->io->rw.iov = iovec;
2134 if (!req->io->rw.iov) {
2135 req->io->rw.iov = req->io->rw.fast_iov;
2136 memcpy(req->io->rw.iov, fast_iov,
2137 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002138 } else {
2139 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002140 }
2141}
2142
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002143static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002144{
Jens Axboed3656342019-12-18 09:50:26 -07002145 if (!io_op_defs[req->opcode].async_ctx)
2146 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002147 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002148 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002149}
2150
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002151static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2152 struct iovec *iovec, struct iovec *fast_iov,
2153 struct iov_iter *iter)
2154{
Jens Axboe980ad262020-01-24 23:08:54 -07002155 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002156 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002157 if (!req->io) {
2158 if (io_alloc_async_ctx(req))
2159 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002160
Jens Axboe5d204bc2020-01-31 12:06:52 -07002161 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2162 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002163 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002164}
2165
Jens Axboe3529d8c2019-12-19 18:24:38 -07002166static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2167 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002168{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002169 struct io_async_ctx *io;
2170 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002171 ssize_t ret;
2172
Jens Axboe3529d8c2019-12-19 18:24:38 -07002173 ret = io_prep_rw(req, sqe, force_nonblock);
2174 if (ret)
2175 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002176
Jens Axboe3529d8c2019-12-19 18:24:38 -07002177 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2178 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002179
Jens Axboe3529d8c2019-12-19 18:24:38 -07002180 if (!req->io)
2181 return 0;
2182
2183 io = req->io;
2184 io->rw.iov = io->rw.fast_iov;
2185 req->io = NULL;
2186 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2187 req->io = io;
2188 if (ret < 0)
2189 return ret;
2190
2191 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2192 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002193}
2194
Pavel Begunkov267bc902019-11-07 01:41:08 +03002195static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002196 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002197{
2198 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002199 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002200 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002201 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002202 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002203
Jens Axboe3529d8c2019-12-19 18:24:38 -07002204 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002205 if (ret < 0)
2206 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002207
Jens Axboefd6c2e42019-12-18 12:19:41 -07002208 /* Ensure we clear previously set non-block flag */
2209 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002210 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002211
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002212 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002213 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002214 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002215 req->result = io_size;
2216
2217 /*
2218 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2219 * we know to async punt it even if it was opened O_NONBLOCK
2220 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002221 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002222 req->flags |= REQ_F_MUST_PUNT;
2223 goto copy_iov;
2224 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002225
Jens Axboe31b51512019-01-18 22:56:34 -07002226 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002227 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002228 if (!ret) {
2229 ssize_t ret2;
2230
Jens Axboe9adbd452019-12-20 08:45:55 -07002231 if (req->file->f_op->read_iter)
2232 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002233 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002234 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002235
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002236 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002237 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002238 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002239 } else {
2240copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002241 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002242 inline_vecs, &iter);
2243 if (ret)
2244 goto out_free;
2245 return -EAGAIN;
2246 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002247 }
Jens Axboef67676d2019-12-02 11:03:47 -07002248out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002249 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002250 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002251 return ret;
2252}
2253
Jens Axboe3529d8c2019-12-19 18:24:38 -07002254static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2255 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002256{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002257 struct io_async_ctx *io;
2258 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002259 ssize_t ret;
2260
Jens Axboe3529d8c2019-12-19 18:24:38 -07002261 ret = io_prep_rw(req, sqe, force_nonblock);
2262 if (ret)
2263 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002264
Jens Axboe3529d8c2019-12-19 18:24:38 -07002265 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2266 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002267
Jens Axboe3529d8c2019-12-19 18:24:38 -07002268 if (!req->io)
2269 return 0;
2270
2271 io = req->io;
2272 io->rw.iov = io->rw.fast_iov;
2273 req->io = NULL;
2274 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2275 req->io = io;
2276 if (ret < 0)
2277 return ret;
2278
2279 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2280 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002281}
2282
Pavel Begunkov267bc902019-11-07 01:41:08 +03002283static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002284 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002285{
2286 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002287 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002288 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002289 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002290 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002291
Jens Axboe3529d8c2019-12-19 18:24:38 -07002292 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002293 if (ret < 0)
2294 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002295
Jens Axboefd6c2e42019-12-18 12:19:41 -07002296 /* Ensure we clear previously set non-block flag */
2297 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002298 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002299
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002300 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002301 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002302 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002303 req->result = io_size;
2304
2305 /*
2306 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2307 * we know to async punt it even if it was opened O_NONBLOCK
2308 */
2309 if (force_nonblock && !io_file_supports_async(req->file)) {
2310 req->flags |= REQ_F_MUST_PUNT;
2311 goto copy_iov;
2312 }
2313
Jens Axboe10d59342019-12-09 20:16:22 -07002314 /* file path doesn't support NOWAIT for non-direct_IO */
2315 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2316 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002317 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002318
Jens Axboe31b51512019-01-18 22:56:34 -07002319 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002320 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002322 ssize_t ret2;
2323
Jens Axboe2b188cc2019-01-07 10:46:33 -07002324 /*
2325 * Open-code file_start_write here to grab freeze protection,
2326 * which will be released by another thread in
2327 * io_complete_rw(). Fool lockdep by telling it the lock got
2328 * released so that it doesn't complain about the held lock when
2329 * we return to userspace.
2330 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002331 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002332 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002333 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002334 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002335 SB_FREEZE_WRITE);
2336 }
2337 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002338
Jens Axboe9adbd452019-12-20 08:45:55 -07002339 if (req->file->f_op->write_iter)
2340 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002341 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002342 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002343 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002344 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002345 } else {
2346copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002347 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002348 inline_vecs, &iter);
2349 if (ret)
2350 goto out_free;
2351 return -EAGAIN;
2352 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002353 }
Jens Axboe31b51512019-01-18 22:56:34 -07002354out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002355 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002356 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002357 return ret;
2358}
2359
2360/*
2361 * IORING_OP_NOP just posts a completion event, nothing else.
2362 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002363static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002364{
2365 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002366
Jens Axboedef596e2019-01-09 08:59:42 -07002367 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2368 return -EINVAL;
2369
Jens Axboe78e19bb2019-11-06 15:21:34 -07002370 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002371 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002372 return 0;
2373}
2374
Jens Axboe3529d8c2019-12-19 18:24:38 -07002375static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002376{
Jens Axboe6b063142019-01-10 22:13:58 -07002377 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002378
Jens Axboe09bb8392019-03-13 12:39:28 -06002379 if (!req->file)
2380 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002381
Jens Axboe6b063142019-01-10 22:13:58 -07002382 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002383 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002384 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002385 return -EINVAL;
2386
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002387 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2388 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2389 return -EINVAL;
2390
2391 req->sync.off = READ_ONCE(sqe->off);
2392 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002393 return 0;
2394}
2395
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002396static bool io_req_cancelled(struct io_kiocb *req)
2397{
2398 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2399 req_set_fail_links(req);
2400 io_cqring_add_event(req, -ECANCELED);
2401 io_put_req(req);
2402 return true;
2403 }
2404
2405 return false;
2406}
2407
Jens Axboe78912932020-01-14 22:09:06 -07002408static void io_link_work_cb(struct io_wq_work **workptr)
2409{
2410 struct io_wq_work *work = *workptr;
2411 struct io_kiocb *link = work->data;
2412
2413 io_queue_linked_timeout(link);
2414 work->func = io_wq_submit_work;
2415}
2416
2417static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2418{
2419 struct io_kiocb *link;
2420
2421 io_prep_async_work(nxt, &link);
2422 *workptr = &nxt->work;
2423 if (link) {
2424 nxt->work.flags |= IO_WQ_WORK_CB;
2425 nxt->work.func = io_link_work_cb;
2426 nxt->work.data = link;
2427 }
2428}
2429
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002430static void io_fsync_finish(struct io_wq_work **workptr)
2431{
2432 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2433 loff_t end = req->sync.off + req->sync.len;
2434 struct io_kiocb *nxt = NULL;
2435 int ret;
2436
2437 if (io_req_cancelled(req))
2438 return;
2439
Jens Axboe9adbd452019-12-20 08:45:55 -07002440 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002441 end > 0 ? end : LLONG_MAX,
2442 req->sync.flags & IORING_FSYNC_DATASYNC);
2443 if (ret < 0)
2444 req_set_fail_links(req);
2445 io_cqring_add_event(req, ret);
2446 io_put_req_find_next(req, &nxt);
2447 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002448 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002449}
2450
Jens Axboefc4df992019-12-10 14:38:45 -07002451static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2452 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002453{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002454 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002455
2456 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002457 if (force_nonblock) {
2458 io_put_req(req);
2459 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002460 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002461 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002462
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002463 work = old_work = &req->work;
2464 io_fsync_finish(&work);
2465 if (work && work != old_work)
2466 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002467 return 0;
2468}
2469
Jens Axboed63d1b52019-12-10 10:38:56 -07002470static void io_fallocate_finish(struct io_wq_work **workptr)
2471{
2472 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2473 struct io_kiocb *nxt = NULL;
2474 int ret;
2475
2476 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2477 req->sync.len);
2478 if (ret < 0)
2479 req_set_fail_links(req);
2480 io_cqring_add_event(req, ret);
2481 io_put_req_find_next(req, &nxt);
2482 if (nxt)
2483 io_wq_assign_next(workptr, nxt);
2484}
2485
2486static int io_fallocate_prep(struct io_kiocb *req,
2487 const struct io_uring_sqe *sqe)
2488{
2489 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2490 return -EINVAL;
2491
2492 req->sync.off = READ_ONCE(sqe->off);
2493 req->sync.len = READ_ONCE(sqe->addr);
2494 req->sync.mode = READ_ONCE(sqe->len);
2495 return 0;
2496}
2497
2498static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2499 bool force_nonblock)
2500{
2501 struct io_wq_work *work, *old_work;
2502
2503 /* fallocate always requiring blocking context */
2504 if (force_nonblock) {
2505 io_put_req(req);
2506 req->work.func = io_fallocate_finish;
2507 return -EAGAIN;
2508 }
2509
2510 work = old_work = &req->work;
2511 io_fallocate_finish(&work);
2512 if (work && work != old_work)
2513 *nxt = container_of(work, struct io_kiocb, work);
2514
2515 return 0;
2516}
2517
Jens Axboe15b71ab2019-12-11 11:20:36 -07002518static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2519{
Jens Axboef8748882020-01-08 17:47:02 -07002520 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002521 int ret;
2522
2523 if (sqe->ioprio || sqe->buf_index)
2524 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002525 if (sqe->flags & IOSQE_FIXED_FILE)
2526 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002527
2528 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002529 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002530 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002531 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002532
Jens Axboef8748882020-01-08 17:47:02 -07002533 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002534 if (IS_ERR(req->open.filename)) {
2535 ret = PTR_ERR(req->open.filename);
2536 req->open.filename = NULL;
2537 return ret;
2538 }
2539
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002540 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002541 return 0;
2542}
2543
Jens Axboecebdb982020-01-08 17:59:24 -07002544static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2545{
2546 struct open_how __user *how;
2547 const char __user *fname;
2548 size_t len;
2549 int ret;
2550
2551 if (sqe->ioprio || sqe->buf_index)
2552 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002553 if (sqe->flags & IOSQE_FIXED_FILE)
2554 return -EBADF;
Jens Axboecebdb982020-01-08 17:59:24 -07002555
2556 req->open.dfd = READ_ONCE(sqe->fd);
2557 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2558 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2559 len = READ_ONCE(sqe->len);
2560
2561 if (len < OPEN_HOW_SIZE_VER0)
2562 return -EINVAL;
2563
2564 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2565 len);
2566 if (ret)
2567 return ret;
2568
2569 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2570 req->open.how.flags |= O_LARGEFILE;
2571
2572 req->open.filename = getname(fname);
2573 if (IS_ERR(req->open.filename)) {
2574 ret = PTR_ERR(req->open.filename);
2575 req->open.filename = NULL;
2576 return ret;
2577 }
2578
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002579 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002580 return 0;
2581}
2582
2583static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2584 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002585{
2586 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002587 struct file *file;
2588 int ret;
2589
Jens Axboef86cd202020-01-29 13:46:44 -07002590 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002591 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002592
Jens Axboecebdb982020-01-08 17:59:24 -07002593 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002594 if (ret)
2595 goto err;
2596
Jens Axboecebdb982020-01-08 17:59:24 -07002597 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002598 if (ret < 0)
2599 goto err;
2600
2601 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2602 if (IS_ERR(file)) {
2603 put_unused_fd(ret);
2604 ret = PTR_ERR(file);
2605 } else {
2606 fsnotify_open(file);
2607 fd_install(ret, file);
2608 }
2609err:
2610 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002611 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002612 if (ret < 0)
2613 req_set_fail_links(req);
2614 io_cqring_add_event(req, ret);
2615 io_put_req_find_next(req, nxt);
2616 return 0;
2617}
2618
Jens Axboecebdb982020-01-08 17:59:24 -07002619static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2620 bool force_nonblock)
2621{
2622 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2623 return io_openat2(req, nxt, force_nonblock);
2624}
2625
Jens Axboe3e4827b2020-01-08 15:18:09 -07002626static int io_epoll_ctl_prep(struct io_kiocb *req,
2627 const struct io_uring_sqe *sqe)
2628{
2629#if defined(CONFIG_EPOLL)
2630 if (sqe->ioprio || sqe->buf_index)
2631 return -EINVAL;
2632
2633 req->epoll.epfd = READ_ONCE(sqe->fd);
2634 req->epoll.op = READ_ONCE(sqe->len);
2635 req->epoll.fd = READ_ONCE(sqe->off);
2636
2637 if (ep_op_has_event(req->epoll.op)) {
2638 struct epoll_event __user *ev;
2639
2640 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2641 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2642 return -EFAULT;
2643 }
2644
2645 return 0;
2646#else
2647 return -EOPNOTSUPP;
2648#endif
2649}
2650
2651static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2652 bool force_nonblock)
2653{
2654#if defined(CONFIG_EPOLL)
2655 struct io_epoll *ie = &req->epoll;
2656 int ret;
2657
2658 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2659 if (force_nonblock && ret == -EAGAIN)
2660 return -EAGAIN;
2661
2662 if (ret < 0)
2663 req_set_fail_links(req);
2664 io_cqring_add_event(req, ret);
2665 io_put_req_find_next(req, nxt);
2666 return 0;
2667#else
2668 return -EOPNOTSUPP;
2669#endif
2670}
2671
Jens Axboec1ca7572019-12-25 22:18:28 -07002672static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2673{
2674#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2675 if (sqe->ioprio || sqe->buf_index || sqe->off)
2676 return -EINVAL;
2677
2678 req->madvise.addr = READ_ONCE(sqe->addr);
2679 req->madvise.len = READ_ONCE(sqe->len);
2680 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2681 return 0;
2682#else
2683 return -EOPNOTSUPP;
2684#endif
2685}
2686
2687static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2688 bool force_nonblock)
2689{
2690#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2691 struct io_madvise *ma = &req->madvise;
2692 int ret;
2693
2694 if (force_nonblock)
2695 return -EAGAIN;
2696
2697 ret = do_madvise(ma->addr, ma->len, ma->advice);
2698 if (ret < 0)
2699 req_set_fail_links(req);
2700 io_cqring_add_event(req, ret);
2701 io_put_req_find_next(req, nxt);
2702 return 0;
2703#else
2704 return -EOPNOTSUPP;
2705#endif
2706}
2707
Jens Axboe4840e412019-12-25 22:03:45 -07002708static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2709{
2710 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2711 return -EINVAL;
2712
2713 req->fadvise.offset = READ_ONCE(sqe->off);
2714 req->fadvise.len = READ_ONCE(sqe->len);
2715 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2716 return 0;
2717}
2718
2719static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2720 bool force_nonblock)
2721{
2722 struct io_fadvise *fa = &req->fadvise;
2723 int ret;
2724
Jens Axboe3e694262020-02-01 09:22:49 -07002725 if (force_nonblock) {
2726 switch (fa->advice) {
2727 case POSIX_FADV_NORMAL:
2728 case POSIX_FADV_RANDOM:
2729 case POSIX_FADV_SEQUENTIAL:
2730 break;
2731 default:
2732 return -EAGAIN;
2733 }
2734 }
Jens Axboe4840e412019-12-25 22:03:45 -07002735
2736 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2737 if (ret < 0)
2738 req_set_fail_links(req);
2739 io_cqring_add_event(req, ret);
2740 io_put_req_find_next(req, nxt);
2741 return 0;
2742}
2743
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002744static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2745{
Jens Axboef8748882020-01-08 17:47:02 -07002746 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002747 unsigned lookup_flags;
2748 int ret;
2749
2750 if (sqe->ioprio || sqe->buf_index)
2751 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002752 if (sqe->flags & IOSQE_FIXED_FILE)
2753 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002754
2755 req->open.dfd = READ_ONCE(sqe->fd);
2756 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002757 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002758 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002759 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002760
Jens Axboec12cedf2020-01-08 17:41:21 -07002761 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002762 return -EINVAL;
2763
Jens Axboef8748882020-01-08 17:47:02 -07002764 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002765 if (IS_ERR(req->open.filename)) {
2766 ret = PTR_ERR(req->open.filename);
2767 req->open.filename = NULL;
2768 return ret;
2769 }
2770
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002771 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002772 return 0;
2773}
2774
2775static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2776 bool force_nonblock)
2777{
2778 struct io_open *ctx = &req->open;
2779 unsigned lookup_flags;
2780 struct path path;
2781 struct kstat stat;
2782 int ret;
2783
2784 if (force_nonblock)
2785 return -EAGAIN;
2786
Jens Axboec12cedf2020-01-08 17:41:21 -07002787 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002788 return -EINVAL;
2789
2790retry:
2791 /* filename_lookup() drops it, keep a reference */
2792 ctx->filename->refcnt++;
2793
2794 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2795 NULL);
2796 if (ret)
2797 goto err;
2798
Jens Axboec12cedf2020-01-08 17:41:21 -07002799 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002800 path_put(&path);
2801 if (retry_estale(ret, lookup_flags)) {
2802 lookup_flags |= LOOKUP_REVAL;
2803 goto retry;
2804 }
2805 if (!ret)
2806 ret = cp_statx(&stat, ctx->buffer);
2807err:
2808 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002809 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002810 if (ret < 0)
2811 req_set_fail_links(req);
2812 io_cqring_add_event(req, ret);
2813 io_put_req_find_next(req, nxt);
2814 return 0;
2815}
2816
Jens Axboeb5dba592019-12-11 14:02:38 -07002817static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2818{
2819 /*
2820 * If we queue this for async, it must not be cancellable. That would
2821 * leave the 'file' in an undeterminate state.
2822 */
2823 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2824
2825 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2826 sqe->rw_flags || sqe->buf_index)
2827 return -EINVAL;
2828 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002829 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002830
2831 req->close.fd = READ_ONCE(sqe->fd);
2832 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002833 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002834 return -EBADF;
2835
2836 return 0;
2837}
2838
2839static void io_close_finish(struct io_wq_work **workptr)
2840{
2841 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2842 struct io_kiocb *nxt = NULL;
2843
2844 /* Invoked with files, we need to do the close */
2845 if (req->work.files) {
2846 int ret;
2847
2848 ret = filp_close(req->close.put_file, req->work.files);
Jens Axboe1a417f42020-01-31 17:16:48 -07002849 if (ret < 0)
Jens Axboeb5dba592019-12-11 14:02:38 -07002850 req_set_fail_links(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07002851 io_cqring_add_event(req, ret);
2852 }
2853
2854 fput(req->close.put_file);
2855
Jens Axboeb5dba592019-12-11 14:02:38 -07002856 io_put_req_find_next(req, &nxt);
2857 if (nxt)
2858 io_wq_assign_next(workptr, nxt);
2859}
2860
2861static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2862 bool force_nonblock)
2863{
2864 int ret;
2865
2866 req->close.put_file = NULL;
2867 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2868 if (ret < 0)
2869 return ret;
2870
2871 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002872 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002873 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002874
2875 /*
2876 * No ->flush(), safely close from here and just punt the
2877 * fput() to async context.
2878 */
2879 ret = filp_close(req->close.put_file, current->files);
2880
2881 if (ret < 0)
2882 req_set_fail_links(req);
2883 io_cqring_add_event(req, ret);
2884
2885 if (io_wq_current_is_worker()) {
2886 struct io_wq_work *old_work, *work;
2887
2888 old_work = work = &req->work;
2889 io_close_finish(&work);
2890 if (work && work != old_work)
2891 *nxt = container_of(work, struct io_kiocb, work);
2892 return 0;
2893 }
2894
2895eagain:
2896 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07002897 /*
2898 * Do manual async queue here to avoid grabbing files - we don't
2899 * need the files, and it'll cause io_close_finish() to close
2900 * the file again and cause a double CQE entry for this request
2901 */
2902 io_queue_async_work(req);
2903 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002904}
2905
Jens Axboe3529d8c2019-12-19 18:24:38 -07002906static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002907{
2908 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002909
2910 if (!req->file)
2911 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002912
2913 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2914 return -EINVAL;
2915 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2916 return -EINVAL;
2917
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002918 req->sync.off = READ_ONCE(sqe->off);
2919 req->sync.len = READ_ONCE(sqe->len);
2920 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002921 return 0;
2922}
2923
2924static void io_sync_file_range_finish(struct io_wq_work **workptr)
2925{
2926 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2927 struct io_kiocb *nxt = NULL;
2928 int ret;
2929
2930 if (io_req_cancelled(req))
2931 return;
2932
Jens Axboe9adbd452019-12-20 08:45:55 -07002933 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002934 req->sync.flags);
2935 if (ret < 0)
2936 req_set_fail_links(req);
2937 io_cqring_add_event(req, ret);
2938 io_put_req_find_next(req, &nxt);
2939 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002940 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002941}
2942
Jens Axboefc4df992019-12-10 14:38:45 -07002943static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002944 bool force_nonblock)
2945{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002946 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002947
2948 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002949 if (force_nonblock) {
2950 io_put_req(req);
2951 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002952 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002953 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002954
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002955 work = old_work = &req->work;
2956 io_sync_file_range_finish(&work);
2957 if (work && work != old_work)
2958 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002959 return 0;
2960}
2961
Jens Axboe3529d8c2019-12-19 18:24:38 -07002962static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002963{
Jens Axboe03b12302019-12-02 18:50:25 -07002964#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002965 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002966 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002967 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002968
Jens Axboee47293f2019-12-20 08:58:21 -07002969 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2970 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002971 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002972
Jens Axboefddafac2020-01-04 20:19:44 -07002973 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002974 return 0;
2975
Jens Axboed9688562019-12-09 19:35:20 -07002976 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002977 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002978 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002979 if (!ret)
2980 req->flags |= REQ_F_NEED_CLEANUP;
2981 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002982#else
Jens Axboee47293f2019-12-20 08:58:21 -07002983 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002984#endif
2985}
2986
Jens Axboefc4df992019-12-10 14:38:45 -07002987static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2988 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002989{
2990#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002991 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002992 struct socket *sock;
2993 int ret;
2994
2995 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2996 return -EINVAL;
2997
2998 sock = sock_from_file(req->file, &ret);
2999 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003000 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003001 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07003002 unsigned flags;
3003
Jens Axboe03b12302019-12-02 18:50:25 -07003004 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003005 kmsg = &req->io->msg;
3006 kmsg->msg.msg_name = &addr;
3007 /* if iov is set, it's allocated already */
3008 if (!kmsg->iov)
3009 kmsg->iov = kmsg->fast_iov;
3010 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003011 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003012 struct io_sr_msg *sr = &req->sr_msg;
3013
Jens Axboe0b416c32019-12-15 10:57:46 -07003014 kmsg = &io.msg;
3015 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003016
3017 io.msg.iov = io.msg.fast_iov;
3018 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3019 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003020 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003021 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003022 }
3023
Jens Axboee47293f2019-12-20 08:58:21 -07003024 flags = req->sr_msg.msg_flags;
3025 if (flags & MSG_DONTWAIT)
3026 req->flags |= REQ_F_NOWAIT;
3027 else if (force_nonblock)
3028 flags |= MSG_DONTWAIT;
3029
Jens Axboe0b416c32019-12-15 10:57:46 -07003030 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003031 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003032 if (req->io)
3033 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003034 if (io_alloc_async_ctx(req)) {
3035 if (kmsg && kmsg->iov != kmsg->fast_iov)
3036 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003037 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003038 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003039 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003040 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003041 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003042 }
3043 if (ret == -ERESTARTSYS)
3044 ret = -EINTR;
3045 }
3046
Pavel Begunkov1e950812020-02-06 19:51:16 +03003047 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003048 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003049 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003050 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003051 if (ret < 0)
3052 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003053 io_put_req_find_next(req, nxt);
3054 return 0;
3055#else
3056 return -EOPNOTSUPP;
3057#endif
3058}
3059
Jens Axboefddafac2020-01-04 20:19:44 -07003060static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3061 bool force_nonblock)
3062{
3063#if defined(CONFIG_NET)
3064 struct socket *sock;
3065 int ret;
3066
3067 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3068 return -EINVAL;
3069
3070 sock = sock_from_file(req->file, &ret);
3071 if (sock) {
3072 struct io_sr_msg *sr = &req->sr_msg;
3073 struct msghdr msg;
3074 struct iovec iov;
3075 unsigned flags;
3076
3077 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3078 &msg.msg_iter);
3079 if (ret)
3080 return ret;
3081
3082 msg.msg_name = NULL;
3083 msg.msg_control = NULL;
3084 msg.msg_controllen = 0;
3085 msg.msg_namelen = 0;
3086
3087 flags = req->sr_msg.msg_flags;
3088 if (flags & MSG_DONTWAIT)
3089 req->flags |= REQ_F_NOWAIT;
3090 else if (force_nonblock)
3091 flags |= MSG_DONTWAIT;
3092
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003093 msg.msg_flags = flags;
3094 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003095 if (force_nonblock && ret == -EAGAIN)
3096 return -EAGAIN;
3097 if (ret == -ERESTARTSYS)
3098 ret = -EINTR;
3099 }
3100
3101 io_cqring_add_event(req, ret);
3102 if (ret < 0)
3103 req_set_fail_links(req);
3104 io_put_req_find_next(req, nxt);
3105 return 0;
3106#else
3107 return -EOPNOTSUPP;
3108#endif
3109}
3110
Jens Axboe3529d8c2019-12-19 18:24:38 -07003111static int io_recvmsg_prep(struct io_kiocb *req,
3112 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003113{
3114#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003115 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003116 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003117 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003118
Jens Axboe3529d8c2019-12-19 18:24:38 -07003119 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3120 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003121 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003122
Jens Axboefddafac2020-01-04 20:19:44 -07003123 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003124 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003125
Jens Axboed9688562019-12-09 19:35:20 -07003126 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003127 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003128 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003129 if (!ret)
3130 req->flags |= REQ_F_NEED_CLEANUP;
3131 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003132#else
Jens Axboee47293f2019-12-20 08:58:21 -07003133 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003134#endif
3135}
3136
Jens Axboefc4df992019-12-10 14:38:45 -07003137static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3138 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003139{
3140#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003141 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003142 struct socket *sock;
3143 int ret;
3144
3145 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3146 return -EINVAL;
3147
3148 sock = sock_from_file(req->file, &ret);
3149 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003150 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003151 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003152 unsigned flags;
3153
Jens Axboe03b12302019-12-02 18:50:25 -07003154 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003155 kmsg = &req->io->msg;
3156 kmsg->msg.msg_name = &addr;
3157 /* if iov is set, it's allocated already */
3158 if (!kmsg->iov)
3159 kmsg->iov = kmsg->fast_iov;
3160 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003161 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003162 struct io_sr_msg *sr = &req->sr_msg;
3163
Jens Axboe0b416c32019-12-15 10:57:46 -07003164 kmsg = &io.msg;
3165 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003166
3167 io.msg.iov = io.msg.fast_iov;
3168 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3169 sr->msg_flags, &io.msg.uaddr,
3170 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003171 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003172 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003173 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003174
Jens Axboee47293f2019-12-20 08:58:21 -07003175 flags = req->sr_msg.msg_flags;
3176 if (flags & MSG_DONTWAIT)
3177 req->flags |= REQ_F_NOWAIT;
3178 else if (force_nonblock)
3179 flags |= MSG_DONTWAIT;
3180
3181 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3182 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003183 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003184 if (req->io)
3185 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003186 if (io_alloc_async_ctx(req)) {
3187 if (kmsg && kmsg->iov != kmsg->fast_iov)
3188 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003189 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003190 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003191 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003192 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe0b416c32019-12-15 10:57:46 -07003193 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003194 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003195 if (ret == -ERESTARTSYS)
3196 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003197 }
3198
Pavel Begunkov1e950812020-02-06 19:51:16 +03003199 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003200 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003201 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003202 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003203 if (ret < 0)
3204 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003205 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003206 return 0;
3207#else
3208 return -EOPNOTSUPP;
3209#endif
3210}
3211
Jens Axboefddafac2020-01-04 20:19:44 -07003212static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3213 bool force_nonblock)
3214{
3215#if defined(CONFIG_NET)
3216 struct socket *sock;
3217 int ret;
3218
3219 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3220 return -EINVAL;
3221
3222 sock = sock_from_file(req->file, &ret);
3223 if (sock) {
3224 struct io_sr_msg *sr = &req->sr_msg;
3225 struct msghdr msg;
3226 struct iovec iov;
3227 unsigned flags;
3228
3229 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3230 &msg.msg_iter);
3231 if (ret)
3232 return ret;
3233
3234 msg.msg_name = NULL;
3235 msg.msg_control = NULL;
3236 msg.msg_controllen = 0;
3237 msg.msg_namelen = 0;
3238 msg.msg_iocb = NULL;
3239 msg.msg_flags = 0;
3240
3241 flags = req->sr_msg.msg_flags;
3242 if (flags & MSG_DONTWAIT)
3243 req->flags |= REQ_F_NOWAIT;
3244 else if (force_nonblock)
3245 flags |= MSG_DONTWAIT;
3246
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003247 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003248 if (force_nonblock && ret == -EAGAIN)
3249 return -EAGAIN;
3250 if (ret == -ERESTARTSYS)
3251 ret = -EINTR;
3252 }
3253
3254 io_cqring_add_event(req, ret);
3255 if (ret < 0)
3256 req_set_fail_links(req);
3257 io_put_req_find_next(req, nxt);
3258 return 0;
3259#else
3260 return -EOPNOTSUPP;
3261#endif
3262}
3263
3264
Jens Axboe3529d8c2019-12-19 18:24:38 -07003265static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003266{
3267#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003268 struct io_accept *accept = &req->accept;
3269
Jens Axboe17f2fe32019-10-17 14:42:58 -06003270 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3271 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003272 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003273 return -EINVAL;
3274
Jens Axboed55e5f52019-12-11 16:12:15 -07003275 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3276 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003277 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003278 return 0;
3279#else
3280 return -EOPNOTSUPP;
3281#endif
3282}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003283
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003284#if defined(CONFIG_NET)
3285static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3286 bool force_nonblock)
3287{
3288 struct io_accept *accept = &req->accept;
3289 unsigned file_flags;
3290 int ret;
3291
3292 file_flags = force_nonblock ? O_NONBLOCK : 0;
3293 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3294 accept->addr_len, accept->flags);
3295 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003296 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003297 if (ret == -ERESTARTSYS)
3298 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003299 if (ret < 0)
3300 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003301 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003302 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003303 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003304}
3305
3306static void io_accept_finish(struct io_wq_work **workptr)
3307{
3308 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3309 struct io_kiocb *nxt = NULL;
3310
3311 if (io_req_cancelled(req))
3312 return;
3313 __io_accept(req, &nxt, false);
3314 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003315 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003316}
3317#endif
3318
3319static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3320 bool force_nonblock)
3321{
3322#if defined(CONFIG_NET)
3323 int ret;
3324
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003325 ret = __io_accept(req, nxt, force_nonblock);
3326 if (ret == -EAGAIN && force_nonblock) {
3327 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003328 io_put_req(req);
3329 return -EAGAIN;
3330 }
3331 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003332#else
3333 return -EOPNOTSUPP;
3334#endif
3335}
3336
Jens Axboe3529d8c2019-12-19 18:24:38 -07003337static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003338{
3339#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003340 struct io_connect *conn = &req->connect;
3341 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003342
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003343 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3344 return -EINVAL;
3345 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3346 return -EINVAL;
3347
Jens Axboe3529d8c2019-12-19 18:24:38 -07003348 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3349 conn->addr_len = READ_ONCE(sqe->addr2);
3350
3351 if (!io)
3352 return 0;
3353
3354 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003355 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003356#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003357 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003358#endif
3359}
3360
Jens Axboefc4df992019-12-10 14:38:45 -07003361static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3362 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003363{
3364#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003365 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003366 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003367 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003368
Jens Axboef499a022019-12-02 16:28:46 -07003369 if (req->io) {
3370 io = req->io;
3371 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003372 ret = move_addr_to_kernel(req->connect.addr,
3373 req->connect.addr_len,
3374 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003375 if (ret)
3376 goto out;
3377 io = &__io;
3378 }
3379
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003380 file_flags = force_nonblock ? O_NONBLOCK : 0;
3381
3382 ret = __sys_connect_file(req->file, &io->connect.address,
3383 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003384 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003385 if (req->io)
3386 return -EAGAIN;
3387 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003388 ret = -ENOMEM;
3389 goto out;
3390 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003391 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003392 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003393 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003394 if (ret == -ERESTARTSYS)
3395 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003396out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003397 if (ret < 0)
3398 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003399 io_cqring_add_event(req, ret);
3400 io_put_req_find_next(req, nxt);
3401 return 0;
3402#else
3403 return -EOPNOTSUPP;
3404#endif
3405}
3406
Jens Axboe221c5eb2019-01-17 09:41:58 -07003407static void io_poll_remove_one(struct io_kiocb *req)
3408{
3409 struct io_poll_iocb *poll = &req->poll;
3410
3411 spin_lock(&poll->head->lock);
3412 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003413 if (!list_empty(&poll->wait.entry)) {
3414 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003415 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003416 }
3417 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003418 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003419}
3420
3421static void io_poll_remove_all(struct io_ring_ctx *ctx)
3422{
Jens Axboe78076bb2019-12-04 19:56:40 -07003423 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003424 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003425 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003426
3427 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003428 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3429 struct hlist_head *list;
3430
3431 list = &ctx->cancel_hash[i];
3432 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3433 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003434 }
3435 spin_unlock_irq(&ctx->completion_lock);
3436}
3437
Jens Axboe47f46762019-11-09 17:43:02 -07003438static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3439{
Jens Axboe78076bb2019-12-04 19:56:40 -07003440 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003441 struct io_kiocb *req;
3442
Jens Axboe78076bb2019-12-04 19:56:40 -07003443 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3444 hlist_for_each_entry(req, list, hash_node) {
3445 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003446 io_poll_remove_one(req);
3447 return 0;
3448 }
Jens Axboe47f46762019-11-09 17:43:02 -07003449 }
3450
3451 return -ENOENT;
3452}
3453
Jens Axboe3529d8c2019-12-19 18:24:38 -07003454static int io_poll_remove_prep(struct io_kiocb *req,
3455 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003456{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003457 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3458 return -EINVAL;
3459 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3460 sqe->poll_events)
3461 return -EINVAL;
3462
Jens Axboe0969e782019-12-17 18:40:57 -07003463 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003464 return 0;
3465}
3466
3467/*
3468 * Find a running poll command that matches one specified in sqe->addr,
3469 * and remove it if found.
3470 */
3471static int io_poll_remove(struct io_kiocb *req)
3472{
3473 struct io_ring_ctx *ctx = req->ctx;
3474 u64 addr;
3475 int ret;
3476
Jens Axboe0969e782019-12-17 18:40:57 -07003477 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003478 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003479 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003480 spin_unlock_irq(&ctx->completion_lock);
3481
Jens Axboe78e19bb2019-11-06 15:21:34 -07003482 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003483 if (ret < 0)
3484 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003485 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003486 return 0;
3487}
3488
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003489static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003490{
Jackie Liua197f662019-11-08 08:09:12 -07003491 struct io_ring_ctx *ctx = req->ctx;
3492
Jens Axboe8c838782019-03-12 15:48:16 -06003493 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003494 if (error)
3495 io_cqring_fill_event(req, error);
3496 else
3497 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003498 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003499}
3500
Jens Axboe561fb042019-10-24 07:25:42 -06003501static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003502{
Jens Axboe561fb042019-10-24 07:25:42 -06003503 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003504 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3505 struct io_poll_iocb *poll = &req->poll;
3506 struct poll_table_struct pt = { ._key = poll->events };
3507 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003508 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003509 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003510 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003511
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003512 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003513 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003514 ret = -ECANCELED;
3515 } else if (READ_ONCE(poll->canceled)) {
3516 ret = -ECANCELED;
3517 }
Jens Axboe561fb042019-10-24 07:25:42 -06003518
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003519 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003520 mask = vfs_poll(poll->file, &pt) & poll->events;
3521
3522 /*
3523 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3524 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3525 * synchronize with them. In the cancellation case the list_del_init
3526 * itself is not actually needed, but harmless so we keep it in to
3527 * avoid further branches in the fast path.
3528 */
3529 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003530 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003531 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003532 spin_unlock_irq(&ctx->completion_lock);
3533 return;
3534 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003535 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003536 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003537 spin_unlock_irq(&ctx->completion_lock);
3538
Jens Axboe8c838782019-03-12 15:48:16 -06003539 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003540
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003541 if (ret < 0)
3542 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003543 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003544 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003545 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003546}
3547
Jens Axboee94f1412019-12-19 12:06:02 -07003548static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3549{
Jens Axboee94f1412019-12-19 12:06:02 -07003550 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003551 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003552
Jens Axboec6ca97b302019-12-28 12:11:08 -07003553 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003554 spin_lock_irq(&ctx->completion_lock);
3555 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3556 hash_del(&req->hash_node);
3557 io_poll_complete(req, req->result, 0);
3558
Jens Axboe8237e042019-12-28 10:48:22 -07003559 if (refcount_dec_and_test(&req->refs) &&
3560 !io_req_multi_free(&rb, req)) {
3561 req->flags |= REQ_F_COMP_LOCKED;
3562 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003563 }
3564 }
3565 spin_unlock_irq(&ctx->completion_lock);
3566
3567 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003568 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003569}
3570
3571static void io_poll_flush(struct io_wq_work **workptr)
3572{
3573 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3574 struct llist_node *nodes;
3575
3576 nodes = llist_del_all(&req->ctx->poll_llist);
3577 if (nodes)
3578 __io_poll_flush(req->ctx, nodes);
3579}
3580
Jens Axboef0b493e2020-02-01 21:30:11 -07003581static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3582{
3583 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3584
3585 eventfd_signal(req->ctx->cq_ev_fd, 1);
3586 io_put_req(req);
3587}
3588
Jens Axboe221c5eb2019-01-17 09:41:58 -07003589static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3590 void *key)
3591{
Jens Axboee9444752019-11-26 15:02:04 -07003592 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003593 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3594 struct io_ring_ctx *ctx = req->ctx;
3595 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003596
3597 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003598 if (mask && !(mask & poll->events))
3599 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003600
Jens Axboe392edb42019-12-09 17:52:20 -07003601 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003602
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003603 /*
3604 * Run completion inline if we can. We're using trylock here because
3605 * we are violating the completion_lock -> poll wq lock ordering.
3606 * If we have a link timeout we're going to need the completion_lock
3607 * for finalizing the request, mark us as having grabbed that already.
3608 */
Jens Axboee94f1412019-12-19 12:06:02 -07003609 if (mask) {
3610 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003611
Jens Axboee94f1412019-12-19 12:06:02 -07003612 if (llist_empty(&ctx->poll_llist) &&
3613 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003614 bool trigger_ev;
3615
Jens Axboee94f1412019-12-19 12:06:02 -07003616 hash_del(&req->hash_node);
3617 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003618
Jens Axboef0b493e2020-02-01 21:30:11 -07003619 trigger_ev = io_should_trigger_evfd(ctx);
3620 if (trigger_ev && eventfd_signal_count()) {
3621 trigger_ev = false;
3622 req->work.func = io_poll_trigger_evfd;
3623 } else {
3624 req->flags |= REQ_F_COMP_LOCKED;
3625 io_put_req(req);
3626 req = NULL;
3627 }
3628 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3629 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003630 } else {
3631 req->result = mask;
3632 req->llist_node.next = NULL;
3633 /* if the list wasn't empty, we're done */
3634 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3635 req = NULL;
3636 else
3637 req->work.func = io_poll_flush;
3638 }
Jens Axboe8c838782019-03-12 15:48:16 -06003639 }
Jens Axboee94f1412019-12-19 12:06:02 -07003640 if (req)
3641 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003642
Jens Axboe221c5eb2019-01-17 09:41:58 -07003643 return 1;
3644}
3645
3646struct io_poll_table {
3647 struct poll_table_struct pt;
3648 struct io_kiocb *req;
3649 int error;
3650};
3651
3652static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3653 struct poll_table_struct *p)
3654{
3655 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3656
3657 if (unlikely(pt->req->poll.head)) {
3658 pt->error = -EINVAL;
3659 return;
3660 }
3661
3662 pt->error = 0;
3663 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003664 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003665}
3666
Jens Axboeeac406c2019-11-14 12:09:58 -07003667static void io_poll_req_insert(struct io_kiocb *req)
3668{
3669 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003670 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003671
Jens Axboe78076bb2019-12-04 19:56:40 -07003672 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3673 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003674}
3675
Jens Axboe3529d8c2019-12-19 18:24:38 -07003676static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003677{
3678 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003679 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003680
3681 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3682 return -EINVAL;
3683 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3684 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003685 if (!poll->file)
3686 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003687
Jens Axboe221c5eb2019-01-17 09:41:58 -07003688 events = READ_ONCE(sqe->poll_events);
3689 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003690 return 0;
3691}
3692
3693static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3694{
3695 struct io_poll_iocb *poll = &req->poll;
3696 struct io_ring_ctx *ctx = req->ctx;
3697 struct io_poll_table ipt;
3698 bool cancel = false;
3699 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003700
3701 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003702 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003703
Jens Axboe221c5eb2019-01-17 09:41:58 -07003704 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003705 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003706 poll->canceled = false;
3707
3708 ipt.pt._qproc = io_poll_queue_proc;
3709 ipt.pt._key = poll->events;
3710 ipt.req = req;
3711 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3712
3713 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003714 INIT_LIST_HEAD(&poll->wait.entry);
3715 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3716 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003717
Jens Axboe36703242019-07-25 10:20:18 -06003718 INIT_LIST_HEAD(&req->list);
3719
Jens Axboe221c5eb2019-01-17 09:41:58 -07003720 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003721
3722 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003723 if (likely(poll->head)) {
3724 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003725 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003726 if (ipt.error)
3727 cancel = true;
3728 ipt.error = 0;
3729 mask = 0;
3730 }
3731 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003732 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003733 else if (cancel)
3734 WRITE_ONCE(poll->canceled, true);
3735 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003736 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003737 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003738 }
Jens Axboe8c838782019-03-12 15:48:16 -06003739 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003740 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003741 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003742 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003743 spin_unlock_irq(&ctx->completion_lock);
3744
Jens Axboe8c838782019-03-12 15:48:16 -06003745 if (mask) {
3746 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003747 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003748 }
Jens Axboe8c838782019-03-12 15:48:16 -06003749 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003750}
3751
Jens Axboe5262f562019-09-17 12:26:57 -06003752static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3753{
Jens Axboead8a48a2019-11-15 08:49:11 -07003754 struct io_timeout_data *data = container_of(timer,
3755 struct io_timeout_data, timer);
3756 struct io_kiocb *req = data->req;
3757 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003758 unsigned long flags;
3759
Jens Axboe5262f562019-09-17 12:26:57 -06003760 atomic_inc(&ctx->cq_timeouts);
3761
3762 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003763 /*
Jens Axboe11365042019-10-16 09:08:32 -06003764 * We could be racing with timeout deletion. If the list is empty,
3765 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003766 */
Jens Axboe842f9612019-10-29 12:34:10 -06003767 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003768 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003769
Jens Axboe11365042019-10-16 09:08:32 -06003770 /*
3771 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003772 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003773 * pointer will be increased, otherwise other timeout reqs may
3774 * return in advance without waiting for enough wait_nr.
3775 */
3776 prev = req;
3777 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3778 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003779 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003780 }
Jens Axboe842f9612019-10-29 12:34:10 -06003781
Jens Axboe78e19bb2019-11-06 15:21:34 -07003782 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003783 io_commit_cqring(ctx);
3784 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3785
3786 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003787 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003788 io_put_req(req);
3789 return HRTIMER_NORESTART;
3790}
3791
Jens Axboe47f46762019-11-09 17:43:02 -07003792static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3793{
3794 struct io_kiocb *req;
3795 int ret = -ENOENT;
3796
3797 list_for_each_entry(req, &ctx->timeout_list, list) {
3798 if (user_data == req->user_data) {
3799 list_del_init(&req->list);
3800 ret = 0;
3801 break;
3802 }
3803 }
3804
3805 if (ret == -ENOENT)
3806 return ret;
3807
Jens Axboe2d283902019-12-04 11:08:05 -07003808 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003809 if (ret == -1)
3810 return -EALREADY;
3811
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003812 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003813 io_cqring_fill_event(req, -ECANCELED);
3814 io_put_req(req);
3815 return 0;
3816}
3817
Jens Axboe3529d8c2019-12-19 18:24:38 -07003818static int io_timeout_remove_prep(struct io_kiocb *req,
3819 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003820{
Jens Axboeb29472e2019-12-17 18:50:29 -07003821 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3822 return -EINVAL;
3823 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3824 return -EINVAL;
3825
3826 req->timeout.addr = READ_ONCE(sqe->addr);
3827 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3828 if (req->timeout.flags)
3829 return -EINVAL;
3830
Jens Axboeb29472e2019-12-17 18:50:29 -07003831 return 0;
3832}
3833
Jens Axboe11365042019-10-16 09:08:32 -06003834/*
3835 * Remove or update an existing timeout command
3836 */
Jens Axboefc4df992019-12-10 14:38:45 -07003837static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003838{
3839 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003840 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003841
Jens Axboe11365042019-10-16 09:08:32 -06003842 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003843 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003844
Jens Axboe47f46762019-11-09 17:43:02 -07003845 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003846 io_commit_cqring(ctx);
3847 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003848 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003849 if (ret < 0)
3850 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003851 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003852 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003853}
3854
Jens Axboe3529d8c2019-12-19 18:24:38 -07003855static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003856 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003857{
Jens Axboead8a48a2019-11-15 08:49:11 -07003858 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003859 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003860
Jens Axboead8a48a2019-11-15 08:49:11 -07003861 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003862 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003863 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003864 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003865 if (sqe->off && is_timeout_link)
3866 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003867 flags = READ_ONCE(sqe->timeout_flags);
3868 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003869 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003870
Jens Axboe26a61672019-12-20 09:02:01 -07003871 req->timeout.count = READ_ONCE(sqe->off);
3872
Jens Axboe3529d8c2019-12-19 18:24:38 -07003873 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003874 return -ENOMEM;
3875
3876 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003877 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003878 req->flags |= REQ_F_TIMEOUT;
3879
3880 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003881 return -EFAULT;
3882
Jens Axboe11365042019-10-16 09:08:32 -06003883 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003884 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003885 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003886 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003887
Jens Axboead8a48a2019-11-15 08:49:11 -07003888 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3889 return 0;
3890}
3891
Jens Axboefc4df992019-12-10 14:38:45 -07003892static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003893{
3894 unsigned count;
3895 struct io_ring_ctx *ctx = req->ctx;
3896 struct io_timeout_data *data;
3897 struct list_head *entry;
3898 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003899
Jens Axboe2d283902019-12-04 11:08:05 -07003900 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003901
Jens Axboe5262f562019-09-17 12:26:57 -06003902 /*
3903 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003904 * timeout event to be satisfied. If it isn't set, then this is
3905 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003906 */
Jens Axboe26a61672019-12-20 09:02:01 -07003907 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003908 if (!count) {
3909 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3910 spin_lock_irq(&ctx->completion_lock);
3911 entry = ctx->timeout_list.prev;
3912 goto add;
3913 }
Jens Axboe5262f562019-09-17 12:26:57 -06003914
3915 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003916 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003917
3918 /*
3919 * Insertion sort, ensuring the first entry in the list is always
3920 * the one we need first.
3921 */
Jens Axboe5262f562019-09-17 12:26:57 -06003922 spin_lock_irq(&ctx->completion_lock);
3923 list_for_each_prev(entry, &ctx->timeout_list) {
3924 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003925 unsigned nxt_sq_head;
3926 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003927 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003928
Jens Axboe93bd25b2019-11-11 23:34:31 -07003929 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3930 continue;
3931
yangerkun5da0fb12019-10-15 21:59:29 +08003932 /*
3933 * Since cached_sq_head + count - 1 can overflow, use type long
3934 * long to store it.
3935 */
3936 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003937 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3938 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003939
3940 /*
3941 * cached_sq_head may overflow, and it will never overflow twice
3942 * once there is some timeout req still be valid.
3943 */
3944 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003945 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003946
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003947 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003948 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003949
3950 /*
3951 * Sequence of reqs after the insert one and itself should
3952 * be adjusted because each timeout req consumes a slot.
3953 */
3954 span++;
3955 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003956 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003957 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003958add:
Jens Axboe5262f562019-09-17 12:26:57 -06003959 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003960 data->timer.function = io_timeout_fn;
3961 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003962 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003963 return 0;
3964}
3965
Jens Axboe62755e32019-10-28 21:49:21 -06003966static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003967{
Jens Axboe62755e32019-10-28 21:49:21 -06003968 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003969
Jens Axboe62755e32019-10-28 21:49:21 -06003970 return req->user_data == (unsigned long) data;
3971}
3972
Jens Axboee977d6d2019-11-05 12:39:45 -07003973static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003974{
Jens Axboe62755e32019-10-28 21:49:21 -06003975 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003976 int ret = 0;
3977
Jens Axboe62755e32019-10-28 21:49:21 -06003978 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3979 switch (cancel_ret) {
3980 case IO_WQ_CANCEL_OK:
3981 ret = 0;
3982 break;
3983 case IO_WQ_CANCEL_RUNNING:
3984 ret = -EALREADY;
3985 break;
3986 case IO_WQ_CANCEL_NOTFOUND:
3987 ret = -ENOENT;
3988 break;
3989 }
3990
Jens Axboee977d6d2019-11-05 12:39:45 -07003991 return ret;
3992}
3993
Jens Axboe47f46762019-11-09 17:43:02 -07003994static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3995 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003996 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003997{
3998 unsigned long flags;
3999 int ret;
4000
4001 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4002 if (ret != -ENOENT) {
4003 spin_lock_irqsave(&ctx->completion_lock, flags);
4004 goto done;
4005 }
4006
4007 spin_lock_irqsave(&ctx->completion_lock, flags);
4008 ret = io_timeout_cancel(ctx, sqe_addr);
4009 if (ret != -ENOENT)
4010 goto done;
4011 ret = io_poll_cancel(ctx, sqe_addr);
4012done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004013 if (!ret)
4014 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004015 io_cqring_fill_event(req, ret);
4016 io_commit_cqring(ctx);
4017 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4018 io_cqring_ev_posted(ctx);
4019
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004020 if (ret < 0)
4021 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004022 io_put_req_find_next(req, nxt);
4023}
4024
Jens Axboe3529d8c2019-12-19 18:24:38 -07004025static int io_async_cancel_prep(struct io_kiocb *req,
4026 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004027{
Jens Axboefbf23842019-12-17 18:45:56 -07004028 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004029 return -EINVAL;
4030 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4031 sqe->cancel_flags)
4032 return -EINVAL;
4033
Jens Axboefbf23842019-12-17 18:45:56 -07004034 req->cancel.addr = READ_ONCE(sqe->addr);
4035 return 0;
4036}
4037
4038static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4039{
4040 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004041
4042 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004043 return 0;
4044}
4045
Jens Axboe05f3fb32019-12-09 11:22:50 -07004046static int io_files_update_prep(struct io_kiocb *req,
4047 const struct io_uring_sqe *sqe)
4048{
4049 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4050 return -EINVAL;
4051
4052 req->files_update.offset = READ_ONCE(sqe->off);
4053 req->files_update.nr_args = READ_ONCE(sqe->len);
4054 if (!req->files_update.nr_args)
4055 return -EINVAL;
4056 req->files_update.arg = READ_ONCE(sqe->addr);
4057 return 0;
4058}
4059
4060static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4061{
4062 struct io_ring_ctx *ctx = req->ctx;
4063 struct io_uring_files_update up;
4064 int ret;
4065
Jens Axboef86cd202020-01-29 13:46:44 -07004066 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004067 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004068
4069 up.offset = req->files_update.offset;
4070 up.fds = req->files_update.arg;
4071
4072 mutex_lock(&ctx->uring_lock);
4073 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4074 mutex_unlock(&ctx->uring_lock);
4075
4076 if (ret < 0)
4077 req_set_fail_links(req);
4078 io_cqring_add_event(req, ret);
4079 io_put_req(req);
4080 return 0;
4081}
4082
Jens Axboe3529d8c2019-12-19 18:24:38 -07004083static int io_req_defer_prep(struct io_kiocb *req,
4084 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004085{
Jens Axboee7815732019-12-17 19:45:06 -07004086 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004087
Jens Axboef86cd202020-01-29 13:46:44 -07004088 if (io_op_defs[req->opcode].file_table) {
4089 ret = io_grab_files(req);
4090 if (unlikely(ret))
4091 return ret;
4092 }
4093
Jens Axboecccf0ee2020-01-27 16:34:48 -07004094 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4095
Jens Axboed625c6e2019-12-17 19:53:05 -07004096 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004097 case IORING_OP_NOP:
4098 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004099 case IORING_OP_READV:
4100 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004101 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004102 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004103 break;
4104 case IORING_OP_WRITEV:
4105 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004106 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004107 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004108 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004109 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004110 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004111 break;
4112 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004113 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004114 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004115 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004116 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004117 break;
4118 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004119 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004120 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004121 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004122 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004123 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004124 break;
4125 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004126 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004127 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004128 break;
Jens Axboef499a022019-12-02 16:28:46 -07004129 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004130 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004131 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004132 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004133 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004134 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004135 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004136 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004137 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004138 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004139 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004140 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004141 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004142 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004143 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004144 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004145 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004146 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004147 case IORING_OP_FALLOCATE:
4148 ret = io_fallocate_prep(req, sqe);
4149 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004150 case IORING_OP_OPENAT:
4151 ret = io_openat_prep(req, sqe);
4152 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004153 case IORING_OP_CLOSE:
4154 ret = io_close_prep(req, sqe);
4155 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004156 case IORING_OP_FILES_UPDATE:
4157 ret = io_files_update_prep(req, sqe);
4158 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004159 case IORING_OP_STATX:
4160 ret = io_statx_prep(req, sqe);
4161 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004162 case IORING_OP_FADVISE:
4163 ret = io_fadvise_prep(req, sqe);
4164 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004165 case IORING_OP_MADVISE:
4166 ret = io_madvise_prep(req, sqe);
4167 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004168 case IORING_OP_OPENAT2:
4169 ret = io_openat2_prep(req, sqe);
4170 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004171 case IORING_OP_EPOLL_CTL:
4172 ret = io_epoll_ctl_prep(req, sqe);
4173 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004174 default:
Jens Axboee7815732019-12-17 19:45:06 -07004175 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4176 req->opcode);
4177 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004178 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004179 }
4180
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004181 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004182}
4183
Jens Axboe3529d8c2019-12-19 18:24:38 -07004184static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004185{
Jackie Liua197f662019-11-08 08:09:12 -07004186 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004187 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004188
Bob Liu9d858b22019-11-13 18:06:25 +08004189 /* Still need defer if there is pending req in defer list. */
4190 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004191 return 0;
4192
Jens Axboe3529d8c2019-12-19 18:24:38 -07004193 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004194 return -EAGAIN;
4195
Jens Axboe3529d8c2019-12-19 18:24:38 -07004196 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004197 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004198 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004199
Jens Axboede0617e2019-04-06 21:51:27 -06004200 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004201 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004202 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004203 return 0;
4204 }
4205
Jens Axboe915967f2019-11-21 09:01:20 -07004206 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004207 list_add_tail(&req->list, &ctx->defer_list);
4208 spin_unlock_irq(&ctx->completion_lock);
4209 return -EIOCBQUEUED;
4210}
4211
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004212static void io_cleanup_req(struct io_kiocb *req)
4213{
4214 struct io_async_ctx *io = req->io;
4215
4216 switch (req->opcode) {
4217 case IORING_OP_READV:
4218 case IORING_OP_READ_FIXED:
4219 case IORING_OP_READ:
4220 case IORING_OP_WRITEV:
4221 case IORING_OP_WRITE_FIXED:
4222 case IORING_OP_WRITE:
4223 if (io->rw.iov != io->rw.fast_iov)
4224 kfree(io->rw.iov);
4225 break;
4226 case IORING_OP_SENDMSG:
4227 case IORING_OP_RECVMSG:
4228 if (io->msg.iov != io->msg.fast_iov)
4229 kfree(io->msg.iov);
4230 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004231 case IORING_OP_OPENAT:
4232 case IORING_OP_OPENAT2:
4233 case IORING_OP_STATX:
4234 putname(req->open.filename);
4235 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004236 }
4237
4238 req->flags &= ~REQ_F_NEED_CLEANUP;
4239}
4240
Jens Axboe3529d8c2019-12-19 18:24:38 -07004241static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4242 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004243{
Jackie Liua197f662019-11-08 08:09:12 -07004244 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004245 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004246
Jens Axboed625c6e2019-12-17 19:53:05 -07004247 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004248 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004249 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004250 break;
4251 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004252 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004253 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004254 if (sqe) {
4255 ret = io_read_prep(req, sqe, force_nonblock);
4256 if (ret < 0)
4257 break;
4258 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004259 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004260 break;
4261 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004262 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004263 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004264 if (sqe) {
4265 ret = io_write_prep(req, sqe, force_nonblock);
4266 if (ret < 0)
4267 break;
4268 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004269 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004270 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004271 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004272 if (sqe) {
4273 ret = io_prep_fsync(req, sqe);
4274 if (ret < 0)
4275 break;
4276 }
Jens Axboefc4df992019-12-10 14:38:45 -07004277 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004278 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004279 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004280 if (sqe) {
4281 ret = io_poll_add_prep(req, sqe);
4282 if (ret)
4283 break;
4284 }
Jens Axboefc4df992019-12-10 14:38:45 -07004285 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004286 break;
4287 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004288 if (sqe) {
4289 ret = io_poll_remove_prep(req, sqe);
4290 if (ret < 0)
4291 break;
4292 }
Jens Axboefc4df992019-12-10 14:38:45 -07004293 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004294 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004295 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004296 if (sqe) {
4297 ret = io_prep_sfr(req, sqe);
4298 if (ret < 0)
4299 break;
4300 }
Jens Axboefc4df992019-12-10 14:38:45 -07004301 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004302 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004303 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004304 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004305 if (sqe) {
4306 ret = io_sendmsg_prep(req, sqe);
4307 if (ret < 0)
4308 break;
4309 }
Jens Axboefddafac2020-01-04 20:19:44 -07004310 if (req->opcode == IORING_OP_SENDMSG)
4311 ret = io_sendmsg(req, nxt, force_nonblock);
4312 else
4313 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004314 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004315 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004316 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004317 if (sqe) {
4318 ret = io_recvmsg_prep(req, sqe);
4319 if (ret)
4320 break;
4321 }
Jens Axboefddafac2020-01-04 20:19:44 -07004322 if (req->opcode == IORING_OP_RECVMSG)
4323 ret = io_recvmsg(req, nxt, force_nonblock);
4324 else
4325 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004326 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004327 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004328 if (sqe) {
4329 ret = io_timeout_prep(req, sqe, false);
4330 if (ret)
4331 break;
4332 }
Jens Axboefc4df992019-12-10 14:38:45 -07004333 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004334 break;
Jens Axboe11365042019-10-16 09:08:32 -06004335 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004336 if (sqe) {
4337 ret = io_timeout_remove_prep(req, sqe);
4338 if (ret)
4339 break;
4340 }
Jens Axboefc4df992019-12-10 14:38:45 -07004341 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004342 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004343 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004344 if (sqe) {
4345 ret = io_accept_prep(req, sqe);
4346 if (ret)
4347 break;
4348 }
Jens Axboefc4df992019-12-10 14:38:45 -07004349 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004350 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004351 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004352 if (sqe) {
4353 ret = io_connect_prep(req, sqe);
4354 if (ret)
4355 break;
4356 }
Jens Axboefc4df992019-12-10 14:38:45 -07004357 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004358 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004359 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004360 if (sqe) {
4361 ret = io_async_cancel_prep(req, sqe);
4362 if (ret)
4363 break;
4364 }
Jens Axboefc4df992019-12-10 14:38:45 -07004365 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004366 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004367 case IORING_OP_FALLOCATE:
4368 if (sqe) {
4369 ret = io_fallocate_prep(req, sqe);
4370 if (ret)
4371 break;
4372 }
4373 ret = io_fallocate(req, nxt, force_nonblock);
4374 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004375 case IORING_OP_OPENAT:
4376 if (sqe) {
4377 ret = io_openat_prep(req, sqe);
4378 if (ret)
4379 break;
4380 }
4381 ret = io_openat(req, nxt, force_nonblock);
4382 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004383 case IORING_OP_CLOSE:
4384 if (sqe) {
4385 ret = io_close_prep(req, sqe);
4386 if (ret)
4387 break;
4388 }
4389 ret = io_close(req, nxt, force_nonblock);
4390 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004391 case IORING_OP_FILES_UPDATE:
4392 if (sqe) {
4393 ret = io_files_update_prep(req, sqe);
4394 if (ret)
4395 break;
4396 }
4397 ret = io_files_update(req, force_nonblock);
4398 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004399 case IORING_OP_STATX:
4400 if (sqe) {
4401 ret = io_statx_prep(req, sqe);
4402 if (ret)
4403 break;
4404 }
4405 ret = io_statx(req, nxt, force_nonblock);
4406 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004407 case IORING_OP_FADVISE:
4408 if (sqe) {
4409 ret = io_fadvise_prep(req, sqe);
4410 if (ret)
4411 break;
4412 }
4413 ret = io_fadvise(req, nxt, force_nonblock);
4414 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004415 case IORING_OP_MADVISE:
4416 if (sqe) {
4417 ret = io_madvise_prep(req, sqe);
4418 if (ret)
4419 break;
4420 }
4421 ret = io_madvise(req, nxt, force_nonblock);
4422 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004423 case IORING_OP_OPENAT2:
4424 if (sqe) {
4425 ret = io_openat2_prep(req, sqe);
4426 if (ret)
4427 break;
4428 }
4429 ret = io_openat2(req, nxt, force_nonblock);
4430 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004431 case IORING_OP_EPOLL_CTL:
4432 if (sqe) {
4433 ret = io_epoll_ctl_prep(req, sqe);
4434 if (ret)
4435 break;
4436 }
4437 ret = io_epoll_ctl(req, nxt, force_nonblock);
4438 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004439 default:
4440 ret = -EINVAL;
4441 break;
4442 }
4443
Jens Axboedef596e2019-01-09 08:59:42 -07004444 if (ret)
4445 return ret;
4446
4447 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004448 const bool in_async = io_wq_current_is_worker();
4449
Jens Axboe9e645e112019-05-10 16:07:28 -06004450 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004451 return -EAGAIN;
4452
Jens Axboe11ba8202020-01-15 21:51:17 -07004453 /* workqueue context doesn't hold uring_lock, grab it now */
4454 if (in_async)
4455 mutex_lock(&ctx->uring_lock);
4456
Jens Axboedef596e2019-01-09 08:59:42 -07004457 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004458
4459 if (in_async)
4460 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004461 }
4462
4463 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004464}
4465
Jens Axboe561fb042019-10-24 07:25:42 -06004466static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004467{
Jens Axboe561fb042019-10-24 07:25:42 -06004468 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004469 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004470 struct io_kiocb *nxt = NULL;
4471 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004472
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004473 /* if NO_CANCEL is set, we must still run the work */
4474 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4475 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004476 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004477 }
Jens Axboe31b51512019-01-18 22:56:34 -07004478
Jens Axboe561fb042019-10-24 07:25:42 -06004479 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004480 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004481 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004482 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004483 /*
4484 * We can get EAGAIN for polled IO even though we're
4485 * forcing a sync submission from here, since we can't
4486 * wait for request slots on the block side.
4487 */
4488 if (ret != -EAGAIN)
4489 break;
4490 cond_resched();
4491 } while (1);
4492 }
Jens Axboe31b51512019-01-18 22:56:34 -07004493
Jens Axboe561fb042019-10-24 07:25:42 -06004494 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004495 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004496
Jens Axboe561fb042019-10-24 07:25:42 -06004497 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004498 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004499 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004500 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004501 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004502
Jens Axboe561fb042019-10-24 07:25:42 -06004503 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004504 if (!ret && nxt)
4505 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004506}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004507
Jens Axboe15b71ab2019-12-11 11:20:36 -07004508static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004509{
Jens Axboed3656342019-12-18 09:50:26 -07004510 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004511 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004512 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4513 return 0;
4514 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004515}
4516
Jens Axboe65e19f52019-10-26 07:20:21 -06004517static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4518 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004519{
Jens Axboe65e19f52019-10-26 07:20:21 -06004520 struct fixed_file_table *table;
4521
Jens Axboe05f3fb32019-12-09 11:22:50 -07004522 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4523 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004524}
4525
Jens Axboe3529d8c2019-12-19 18:24:38 -07004526static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4527 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004528{
Jackie Liua197f662019-11-08 08:09:12 -07004529 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004530 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004531 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004532
Jens Axboe3529d8c2019-12-19 18:24:38 -07004533 flags = READ_ONCE(sqe->flags);
4534 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004535
Jens Axboed3656342019-12-18 09:50:26 -07004536 if (!io_req_needs_file(req, fd))
4537 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004538
4539 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004540 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004541 (unsigned) fd >= ctx->nr_user_files))
4542 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004543 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004544 req->file = io_file_from_index(ctx, fd);
4545 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004546 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004547 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004548 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004549 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004550 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004551 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004552 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004553 req->file = io_file_get(state, fd);
4554 if (unlikely(!req->file))
4555 return -EBADF;
4556 }
4557
4558 return 0;
4559}
4560
Jackie Liua197f662019-11-08 08:09:12 -07004561static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004562{
Jens Axboefcb323c2019-10-24 12:39:47 -06004563 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004564 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004565
Jens Axboef86cd202020-01-29 13:46:44 -07004566 if (req->work.files)
4567 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004568 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004569 return -EBADF;
4570
Jens Axboefcb323c2019-10-24 12:39:47 -06004571 rcu_read_lock();
4572 spin_lock_irq(&ctx->inflight_lock);
4573 /*
4574 * We use the f_ops->flush() handler to ensure that we can flush
4575 * out work accessing these files if the fd is closed. Check if
4576 * the fd has changed since we started down this path, and disallow
4577 * this operation if it has.
4578 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004579 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004580 list_add(&req->inflight_entry, &ctx->inflight_list);
4581 req->flags |= REQ_F_INFLIGHT;
4582 req->work.files = current->files;
4583 ret = 0;
4584 }
4585 spin_unlock_irq(&ctx->inflight_lock);
4586 rcu_read_unlock();
4587
4588 return ret;
4589}
4590
Jens Axboe2665abf2019-11-05 12:40:47 -07004591static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4592{
Jens Axboead8a48a2019-11-15 08:49:11 -07004593 struct io_timeout_data *data = container_of(timer,
4594 struct io_timeout_data, timer);
4595 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004596 struct io_ring_ctx *ctx = req->ctx;
4597 struct io_kiocb *prev = NULL;
4598 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004599
4600 spin_lock_irqsave(&ctx->completion_lock, flags);
4601
4602 /*
4603 * We don't expect the list to be empty, that will only happen if we
4604 * race with the completion of the linked work.
4605 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004606 if (!list_empty(&req->link_list)) {
4607 prev = list_entry(req->link_list.prev, struct io_kiocb,
4608 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004609 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004610 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004611 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4612 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004613 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004614 }
4615
4616 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4617
4618 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004619 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004620 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4621 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004622 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004623 } else {
4624 io_cqring_add_event(req, -ETIME);
4625 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004626 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004627 return HRTIMER_NORESTART;
4628}
4629
Jens Axboead8a48a2019-11-15 08:49:11 -07004630static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004631{
Jens Axboe76a46e02019-11-10 23:34:16 -07004632 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004633
Jens Axboe76a46e02019-11-10 23:34:16 -07004634 /*
4635 * If the list is now empty, then our linked request finished before
4636 * we got a chance to setup the timer
4637 */
4638 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004639 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004640 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004641
Jens Axboead8a48a2019-11-15 08:49:11 -07004642 data->timer.function = io_link_timeout_fn;
4643 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4644 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004645 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004646 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004647
Jens Axboe2665abf2019-11-05 12:40:47 -07004648 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004649 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004650}
4651
Jens Axboead8a48a2019-11-15 08:49:11 -07004652static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004653{
4654 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004655
Jens Axboe2665abf2019-11-05 12:40:47 -07004656 if (!(req->flags & REQ_F_LINK))
4657 return NULL;
4658
Pavel Begunkov44932332019-12-05 16:16:35 +03004659 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4660 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004661 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004662 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004663
Jens Axboe76a46e02019-11-10 23:34:16 -07004664 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004665 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004666}
4667
Jens Axboe3529d8c2019-12-19 18:24:38 -07004668static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004669{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004670 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004671 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004672 int ret;
4673
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004674again:
4675 linked_timeout = io_prep_linked_timeout(req);
4676
Jens Axboe3529d8c2019-12-19 18:24:38 -07004677 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004678
4679 /*
4680 * We async punt it if the file wasn't marked NOWAIT, or if the file
4681 * doesn't support non-blocking read/write attempts
4682 */
4683 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4684 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004685punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004686 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004687 ret = io_grab_files(req);
4688 if (ret)
4689 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004690 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004691
4692 /*
4693 * Queued up for async execution, worker will release
4694 * submit reference when the iocb is actually submitted.
4695 */
4696 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004697 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004698 }
Jens Axboee65ef562019-03-12 10:16:44 -06004699
Jens Axboefcb323c2019-10-24 12:39:47 -06004700err:
Jens Axboee65ef562019-03-12 10:16:44 -06004701 /* drop submission reference */
4702 io_put_req(req);
4703
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004704 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004705 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004706 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004707 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004708 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004709 }
4710
Jens Axboee65ef562019-03-12 10:16:44 -06004711 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004712 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004713 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004714 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004715 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004716 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004717done_req:
4718 if (nxt) {
4719 req = nxt;
4720 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004721
4722 if (req->flags & REQ_F_FORCE_ASYNC)
4723 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004724 goto again;
4725 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004726}
4727
Jens Axboe3529d8c2019-12-19 18:24:38 -07004728static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004729{
4730 int ret;
4731
Jens Axboe3529d8c2019-12-19 18:24:38 -07004732 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004733 if (ret) {
4734 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004735fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004736 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004737 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004738 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004739 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004740 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004741 ret = io_req_defer_prep(req, sqe);
4742 if (unlikely(ret < 0))
4743 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004744 /*
4745 * Never try inline submit of IOSQE_ASYNC is set, go straight
4746 * to async execution.
4747 */
4748 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4749 io_queue_async_work(req);
4750 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004751 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004752 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004753}
4754
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004755static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004756{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004757 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004758 io_cqring_add_event(req, -ECANCELED);
4759 io_double_put_req(req);
4760 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004761 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004762}
4763
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004764#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004765 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004766
Jens Axboe3529d8c2019-12-19 18:24:38 -07004767static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4768 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004769{
Jens Axboe75c6a032020-01-28 10:15:23 -07004770 const struct cred *old_creds = NULL;
Jackie Liua197f662019-11-08 08:09:12 -07004771 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004772 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004773 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004774
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004775 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004776
4777 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004778 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004779 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004780 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004781 }
4782
Jens Axboe75c6a032020-01-28 10:15:23 -07004783 id = READ_ONCE(sqe->personality);
4784 if (id) {
4785 const struct cred *personality_creds;
4786
4787 personality_creds = idr_find(&ctx->personality_idr, id);
4788 if (unlikely(!personality_creds)) {
4789 ret = -EINVAL;
4790 goto err_req;
4791 }
4792 old_creds = override_creds(personality_creds);
4793 }
4794
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004795 /* same numerical values with corresponding REQ_F_*, safe to copy */
4796 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4797 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004798
Jens Axboe3529d8c2019-12-19 18:24:38 -07004799 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004800 if (unlikely(ret)) {
4801err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004802 io_cqring_add_event(req, ret);
4803 io_double_put_req(req);
Jens Axboe75c6a032020-01-28 10:15:23 -07004804 if (old_creds)
4805 revert_creds(old_creds);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004806 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004807 }
4808
Jens Axboe9e645e112019-05-10 16:07:28 -06004809 /*
4810 * If we already have a head request, queue this one for async
4811 * submittal once the head completes. If we don't have a head but
4812 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4813 * submitted sync once the chain is complete. If none of those
4814 * conditions are true (normal request), then just queue it.
4815 */
4816 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004817 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004818
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004819 /*
4820 * Taking sequential execution of a link, draining both sides
4821 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4822 * requests in the link. So, it drains the head and the
4823 * next after the link request. The last one is done via
4824 * drain_next flag to persist the effect across calls.
4825 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004826 if (sqe_flags & IOSQE_IO_DRAIN) {
4827 head->flags |= REQ_F_IO_DRAIN;
4828 ctx->drain_next = 1;
4829 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004830 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004831 ret = -EAGAIN;
4832 goto err_req;
4833 }
4834
Jens Axboe3529d8c2019-12-19 18:24:38 -07004835 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004836 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004837 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004838 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004839 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004840 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004841 trace_io_uring_link(ctx, req, head);
4842 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004843
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004844 /* last request of a link, enqueue the link */
4845 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4846 io_queue_link_head(head);
4847 *link = NULL;
4848 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004849 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004850 if (unlikely(ctx->drain_next)) {
4851 req->flags |= REQ_F_IO_DRAIN;
4852 req->ctx->drain_next = 0;
4853 }
4854 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4855 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004856 INIT_LIST_HEAD(&req->link_list);
4857 ret = io_req_defer_prep(req, sqe);
4858 if (ret)
4859 req->flags |= REQ_F_FAIL_LINK;
4860 *link = req;
4861 } else {
4862 io_queue_sqe(req, sqe);
4863 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004864 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004865
Jens Axboe75c6a032020-01-28 10:15:23 -07004866 if (old_creds)
4867 revert_creds(old_creds);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004868 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004869}
4870
Jens Axboe9a56a232019-01-09 09:06:50 -07004871/*
4872 * Batched submission is done, ensure local IO is flushed out.
4873 */
4874static void io_submit_state_end(struct io_submit_state *state)
4875{
4876 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004877 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004878 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004879 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004880}
4881
4882/*
4883 * Start submission side cache.
4884 */
4885static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004886 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004887{
4888 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004889 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004890 state->file = NULL;
4891 state->ios_left = max_ios;
4892}
4893
Jens Axboe2b188cc2019-01-07 10:46:33 -07004894static void io_commit_sqring(struct io_ring_ctx *ctx)
4895{
Hristo Venev75b28af2019-08-26 17:23:46 +00004896 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004897
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004898 /*
4899 * Ensure any loads from the SQEs are done at this point,
4900 * since once we write the new head, the application could
4901 * write new data to them.
4902 */
4903 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004904}
4905
4906/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004907 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004908 * that is mapped by userspace. This means that care needs to be taken to
4909 * ensure that reads are stable, as we cannot rely on userspace always
4910 * being a good citizen. If members of the sqe are validated and then later
4911 * used, it's important that those reads are done through READ_ONCE() to
4912 * prevent a re-load down the line.
4913 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004914static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4915 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004916{
Hristo Venev75b28af2019-08-26 17:23:46 +00004917 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004918 unsigned head;
4919
4920 /*
4921 * The cached sq head (or cq tail) serves two purposes:
4922 *
4923 * 1) allows us to batch the cost of updating the user visible
4924 * head updates.
4925 * 2) allows the kernel side to track the head on its own, even
4926 * though the application is the one updating it.
4927 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004928 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004929 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004930 /*
4931 * All io need record the previous position, if LINK vs DARIN,
4932 * it can be used to mark the position of the first IO in the
4933 * link list.
4934 */
4935 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004936 *sqe_ptr = &ctx->sq_sqes[head];
4937 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4938 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004939 ctx->cached_sq_head++;
4940 return true;
4941 }
4942
4943 /* drop invalid entries */
4944 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004945 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004946 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004947 return false;
4948}
4949
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004950static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004951 struct file *ring_file, int ring_fd,
4952 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004953{
4954 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004955 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004956 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004957 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004958
Jens Axboec4a2ed72019-11-21 21:01:26 -07004959 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004960 if (test_bit(0, &ctx->sq_check_overflow)) {
4961 if (!list_empty(&ctx->cq_overflow_list) &&
4962 !io_cqring_overflow_flush(ctx, false))
4963 return -EBUSY;
4964 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004965
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004966 /* make sure SQ entry isn't read before tail */
4967 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004968
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004969 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4970 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004971
4972 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004973 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004974 statep = &state;
4975 }
4976
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004977 ctx->ring_fd = ring_fd;
4978 ctx->ring_file = ring_file;
4979
Jens Axboe6c271ce2019-01-10 11:22:30 -07004980 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004981 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004982 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03004983 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004984
Pavel Begunkov196be952019-11-07 01:41:06 +03004985 req = io_get_req(ctx, statep);
4986 if (unlikely(!req)) {
4987 if (!submitted)
4988 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004989 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004990 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004991 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004992 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004993 break;
4994 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004995
Jens Axboed3656342019-12-18 09:50:26 -07004996 /* will complete beyond this point, count as submitted */
4997 submitted++;
4998
4999 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005000 err = -EINVAL;
5001fail_req:
5002 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005003 io_double_put_req(req);
5004 break;
5005 }
5006
5007 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005008 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005009 if (unlikely(mm_fault)) {
5010 err = -EFAULT;
5011 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005012 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005013 use_mm(ctx->sqo_mm);
5014 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005015 }
5016
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005017 req->in_async = async;
5018 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005019 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5020 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005021 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005022 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005023 }
5024
Pavel Begunkov9466f432020-01-25 22:34:01 +03005025 if (unlikely(submitted != nr)) {
5026 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5027
5028 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5029 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005030 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005031 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005032 if (statep)
5033 io_submit_state_end(&state);
5034
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005035 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5036 io_commit_sqring(ctx);
5037
Jens Axboe6c271ce2019-01-10 11:22:30 -07005038 return submitted;
5039}
5040
5041static int io_sq_thread(void *data)
5042{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005043 struct io_ring_ctx *ctx = data;
5044 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005045 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005046 mm_segment_t old_fs;
5047 DEFINE_WAIT(wait);
5048 unsigned inflight;
5049 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07005050 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005051
Jens Axboe206aefd2019-11-07 18:27:42 -07005052 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005053
Jens Axboe6c271ce2019-01-10 11:22:30 -07005054 old_fs = get_fs();
5055 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005056 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005057
Jens Axboec1edbf52019-11-10 16:56:04 -07005058 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005059 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005060 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005061
5062 if (inflight) {
5063 unsigned nr_events = 0;
5064
5065 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06005066 /*
5067 * inflight is the count of the maximum possible
5068 * entries we submitted, but it can be smaller
5069 * if we dropped some of them. If we don't have
5070 * poll entries available, then we know that we
5071 * have nothing left to poll for. Reset the
5072 * inflight count to zero in that case.
5073 */
5074 mutex_lock(&ctx->uring_lock);
5075 if (!list_empty(&ctx->poll_list))
5076 __io_iopoll_check(ctx, &nr_events, 0);
5077 else
5078 inflight = 0;
5079 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005080 } else {
5081 /*
5082 * Normal IO, just pretend everything completed.
5083 * We don't have to poll completions for that.
5084 */
5085 nr_events = inflight;
5086 }
5087
5088 inflight -= nr_events;
5089 if (!inflight)
5090 timeout = jiffies + ctx->sq_thread_idle;
5091 }
5092
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005093 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005094
5095 /*
5096 * If submit got -EBUSY, flag us as needing the application
5097 * to enter the kernel to reap and flush events.
5098 */
5099 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005100 /*
5101 * We're polling. If we're within the defined idle
5102 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005103 * to sleep. The exception is if we got EBUSY doing
5104 * more IO, we should wait for the application to
5105 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005106 */
Jens Axboec1edbf52019-11-10 16:56:04 -07005107 if (inflight ||
Jens Axboedf069d82020-02-04 16:48:34 -07005108 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5109 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005110 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005111 continue;
5112 }
5113
5114 /*
5115 * Drop cur_mm before scheduling, we can't hold it for
5116 * long periods (or over schedule()). Do this before
5117 * adding ourselves to the waitqueue, as the unuse/drop
5118 * may sleep.
5119 */
5120 if (cur_mm) {
5121 unuse_mm(cur_mm);
5122 mmput(cur_mm);
5123 cur_mm = NULL;
5124 }
5125
5126 prepare_to_wait(&ctx->sqo_wait, &wait,
5127 TASK_INTERRUPTIBLE);
5128
5129 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005130 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005131 /* make sure to read SQ tail after writing flags */
5132 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005133
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005134 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005135 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005136 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005137 finish_wait(&ctx->sqo_wait, &wait);
5138 break;
5139 }
5140 if (signal_pending(current))
5141 flush_signals(current);
5142 schedule();
5143 finish_wait(&ctx->sqo_wait, &wait);
5144
Hristo Venev75b28af2019-08-26 17:23:46 +00005145 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005146 continue;
5147 }
5148 finish_wait(&ctx->sqo_wait, &wait);
5149
Hristo Venev75b28af2019-08-26 17:23:46 +00005150 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005151 }
5152
Jens Axboe8a4955f2019-12-09 14:52:35 -07005153 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005154 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005155 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005156 if (ret > 0)
5157 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005158 }
5159
5160 set_fs(old_fs);
5161 if (cur_mm) {
5162 unuse_mm(cur_mm);
5163 mmput(cur_mm);
5164 }
Jens Axboe181e4482019-11-25 08:52:30 -07005165 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005166
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005167 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005168
Jens Axboe6c271ce2019-01-10 11:22:30 -07005169 return 0;
5170}
5171
Jens Axboebda52162019-09-24 13:47:15 -06005172struct io_wait_queue {
5173 struct wait_queue_entry wq;
5174 struct io_ring_ctx *ctx;
5175 unsigned to_wait;
5176 unsigned nr_timeouts;
5177};
5178
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005179static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005180{
5181 struct io_ring_ctx *ctx = iowq->ctx;
5182
5183 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005184 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005185 * started waiting. For timeouts, we always want to return to userspace,
5186 * regardless of event count.
5187 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005188 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005189 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5190}
5191
5192static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5193 int wake_flags, void *key)
5194{
5195 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5196 wq);
5197
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005198 /* use noflush == true, as we can't safely rely on locking context */
5199 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005200 return -1;
5201
5202 return autoremove_wake_function(curr, mode, wake_flags, key);
5203}
5204
Jens Axboe2b188cc2019-01-07 10:46:33 -07005205/*
5206 * Wait until events become available, if we don't already have some. The
5207 * application must reap them itself, as they reside on the shared cq ring.
5208 */
5209static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5210 const sigset_t __user *sig, size_t sigsz)
5211{
Jens Axboebda52162019-09-24 13:47:15 -06005212 struct io_wait_queue iowq = {
5213 .wq = {
5214 .private = current,
5215 .func = io_wake_function,
5216 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5217 },
5218 .ctx = ctx,
5219 .to_wait = min_events,
5220 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005221 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005222 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005223
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005224 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005225 return 0;
5226
5227 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005228#ifdef CONFIG_COMPAT
5229 if (in_compat_syscall())
5230 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005231 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005232 else
5233#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005234 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005235
Jens Axboe2b188cc2019-01-07 10:46:33 -07005236 if (ret)
5237 return ret;
5238 }
5239
Jens Axboebda52162019-09-24 13:47:15 -06005240 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005241 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005242 do {
5243 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5244 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005245 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005246 break;
5247 schedule();
5248 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005249 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005250 break;
5251 }
5252 } while (1);
5253 finish_wait(&ctx->wait, &iowq.wq);
5254
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005255 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005256
Hristo Venev75b28af2019-08-26 17:23:46 +00005257 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005258}
5259
Jens Axboe6b063142019-01-10 22:13:58 -07005260static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5261{
5262#if defined(CONFIG_UNIX)
5263 if (ctx->ring_sock) {
5264 struct sock *sock = ctx->ring_sock->sk;
5265 struct sk_buff *skb;
5266
5267 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5268 kfree_skb(skb);
5269 }
5270#else
5271 int i;
5272
Jens Axboe65e19f52019-10-26 07:20:21 -06005273 for (i = 0; i < ctx->nr_user_files; i++) {
5274 struct file *file;
5275
5276 file = io_file_from_index(ctx, i);
5277 if (file)
5278 fput(file);
5279 }
Jens Axboe6b063142019-01-10 22:13:58 -07005280#endif
5281}
5282
Jens Axboe05f3fb32019-12-09 11:22:50 -07005283static void io_file_ref_kill(struct percpu_ref *ref)
5284{
5285 struct fixed_file_data *data;
5286
5287 data = container_of(ref, struct fixed_file_data, refs);
5288 complete(&data->done);
5289}
5290
Jens Axboe6b063142019-01-10 22:13:58 -07005291static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5292{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005293 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005294 unsigned nr_tables, i;
5295
Jens Axboe05f3fb32019-12-09 11:22:50 -07005296 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005297 return -ENXIO;
5298
Jens Axboe05f3fb32019-12-09 11:22:50 -07005299 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005300 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005301 wait_for_completion(&data->done);
5302 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005303 percpu_ref_exit(&data->refs);
5304
Jens Axboe6b063142019-01-10 22:13:58 -07005305 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005306 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5307 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005308 kfree(data->table[i].files);
5309 kfree(data->table);
5310 kfree(data);
5311 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005312 ctx->nr_user_files = 0;
5313 return 0;
5314}
5315
Jens Axboe6c271ce2019-01-10 11:22:30 -07005316static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5317{
5318 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005319 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005320 /*
5321 * The park is a bit of a work-around, without it we get
5322 * warning spews on shutdown with SQPOLL set and affinity
5323 * set to a single CPU.
5324 */
Jens Axboe06058632019-04-13 09:26:03 -06005325 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005326 kthread_stop(ctx->sqo_thread);
5327 ctx->sqo_thread = NULL;
5328 }
5329}
5330
Jens Axboe6b063142019-01-10 22:13:58 -07005331static void io_finish_async(struct io_ring_ctx *ctx)
5332{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005333 io_sq_thread_stop(ctx);
5334
Jens Axboe561fb042019-10-24 07:25:42 -06005335 if (ctx->io_wq) {
5336 io_wq_destroy(ctx->io_wq);
5337 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005338 }
5339}
5340
5341#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005342/*
5343 * Ensure the UNIX gc is aware of our file set, so we are certain that
5344 * the io_uring can be safely unregistered on process exit, even if we have
5345 * loops in the file referencing.
5346 */
5347static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5348{
5349 struct sock *sk = ctx->ring_sock->sk;
5350 struct scm_fp_list *fpl;
5351 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005352 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005353
5354 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5355 unsigned long inflight = ctx->user->unix_inflight + nr;
5356
5357 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5358 return -EMFILE;
5359 }
5360
5361 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5362 if (!fpl)
5363 return -ENOMEM;
5364
5365 skb = alloc_skb(0, GFP_KERNEL);
5366 if (!skb) {
5367 kfree(fpl);
5368 return -ENOMEM;
5369 }
5370
5371 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005372
Jens Axboe08a45172019-10-03 08:11:03 -06005373 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005374 fpl->user = get_uid(ctx->user);
5375 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005376 struct file *file = io_file_from_index(ctx, i + offset);
5377
5378 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005379 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005380 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005381 unix_inflight(fpl->user, fpl->fp[nr_files]);
5382 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005383 }
5384
Jens Axboe08a45172019-10-03 08:11:03 -06005385 if (nr_files) {
5386 fpl->max = SCM_MAX_FD;
5387 fpl->count = nr_files;
5388 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005389 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005390 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5391 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005392
Jens Axboe08a45172019-10-03 08:11:03 -06005393 for (i = 0; i < nr_files; i++)
5394 fput(fpl->fp[i]);
5395 } else {
5396 kfree_skb(skb);
5397 kfree(fpl);
5398 }
Jens Axboe6b063142019-01-10 22:13:58 -07005399
5400 return 0;
5401}
5402
5403/*
5404 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5405 * causes regular reference counting to break down. We rely on the UNIX
5406 * garbage collection to take care of this problem for us.
5407 */
5408static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5409{
5410 unsigned left, total;
5411 int ret = 0;
5412
5413 total = 0;
5414 left = ctx->nr_user_files;
5415 while (left) {
5416 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005417
5418 ret = __io_sqe_files_scm(ctx, this_files, total);
5419 if (ret)
5420 break;
5421 left -= this_files;
5422 total += this_files;
5423 }
5424
5425 if (!ret)
5426 return 0;
5427
5428 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005429 struct file *file = io_file_from_index(ctx, total);
5430
5431 if (file)
5432 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005433 total++;
5434 }
5435
5436 return ret;
5437}
5438#else
5439static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5440{
5441 return 0;
5442}
5443#endif
5444
Jens Axboe65e19f52019-10-26 07:20:21 -06005445static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5446 unsigned nr_files)
5447{
5448 int i;
5449
5450 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005451 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005452 unsigned this_files;
5453
5454 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5455 table->files = kcalloc(this_files, sizeof(struct file *),
5456 GFP_KERNEL);
5457 if (!table->files)
5458 break;
5459 nr_files -= this_files;
5460 }
5461
5462 if (i == nr_tables)
5463 return 0;
5464
5465 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005466 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005467 kfree(table->files);
5468 }
5469 return 1;
5470}
5471
Jens Axboe05f3fb32019-12-09 11:22:50 -07005472static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005473{
5474#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005475 struct sock *sock = ctx->ring_sock->sk;
5476 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5477 struct sk_buff *skb;
5478 int i;
5479
5480 __skb_queue_head_init(&list);
5481
5482 /*
5483 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5484 * remove this entry and rearrange the file array.
5485 */
5486 skb = skb_dequeue(head);
5487 while (skb) {
5488 struct scm_fp_list *fp;
5489
5490 fp = UNIXCB(skb).fp;
5491 for (i = 0; i < fp->count; i++) {
5492 int left;
5493
5494 if (fp->fp[i] != file)
5495 continue;
5496
5497 unix_notinflight(fp->user, fp->fp[i]);
5498 left = fp->count - 1 - i;
5499 if (left) {
5500 memmove(&fp->fp[i], &fp->fp[i + 1],
5501 left * sizeof(struct file *));
5502 }
5503 fp->count--;
5504 if (!fp->count) {
5505 kfree_skb(skb);
5506 skb = NULL;
5507 } else {
5508 __skb_queue_tail(&list, skb);
5509 }
5510 fput(file);
5511 file = NULL;
5512 break;
5513 }
5514
5515 if (!file)
5516 break;
5517
5518 __skb_queue_tail(&list, skb);
5519
5520 skb = skb_dequeue(head);
5521 }
5522
5523 if (skb_peek(&list)) {
5524 spin_lock_irq(&head->lock);
5525 while ((skb = __skb_dequeue(&list)) != NULL)
5526 __skb_queue_tail(head, skb);
5527 spin_unlock_irq(&head->lock);
5528 }
5529#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005530 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005531#endif
5532}
5533
Jens Axboe05f3fb32019-12-09 11:22:50 -07005534struct io_file_put {
5535 struct llist_node llist;
5536 struct file *file;
5537 struct completion *done;
5538};
5539
Jens Axboe2faf8522020-02-04 19:54:55 -07005540static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005541{
5542 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005543 struct llist_node *node;
5544
Jens Axboe05f3fb32019-12-09 11:22:50 -07005545 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5546 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5547 io_ring_file_put(data->ctx, pfile->file);
5548 if (pfile->done)
5549 complete(pfile->done);
5550 else
5551 kfree(pfile);
5552 }
5553 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005554}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005555
Jens Axboe2faf8522020-02-04 19:54:55 -07005556static void io_ring_file_ref_switch(struct work_struct *work)
5557{
5558 struct fixed_file_data *data;
5559
5560 data = container_of(work, struct fixed_file_data, ref_work);
5561 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005562 percpu_ref_get(&data->refs);
5563 percpu_ref_switch_to_percpu(&data->refs);
5564}
5565
5566static void io_file_data_ref_zero(struct percpu_ref *ref)
5567{
5568 struct fixed_file_data *data;
5569
5570 data = container_of(ref, struct fixed_file_data, refs);
5571
Jens Axboe2faf8522020-02-04 19:54:55 -07005572 /*
5573 * We can't safely switch from inside this context, punt to wq. If
5574 * the table ref is going away, the table is being unregistered.
5575 * Don't queue up the async work for that case, the caller will
5576 * handle it.
5577 */
5578 if (!percpu_ref_is_dying(&data->refs))
5579 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005580}
5581
5582static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5583 unsigned nr_args)
5584{
5585 __s32 __user *fds = (__s32 __user *) arg;
5586 unsigned nr_tables;
5587 struct file *file;
5588 int fd, ret = 0;
5589 unsigned i;
5590
5591 if (ctx->file_data)
5592 return -EBUSY;
5593 if (!nr_args)
5594 return -EINVAL;
5595 if (nr_args > IORING_MAX_FIXED_FILES)
5596 return -EMFILE;
5597
5598 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5599 if (!ctx->file_data)
5600 return -ENOMEM;
5601 ctx->file_data->ctx = ctx;
5602 init_completion(&ctx->file_data->done);
5603
5604 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5605 ctx->file_data->table = kcalloc(nr_tables,
5606 sizeof(struct fixed_file_table),
5607 GFP_KERNEL);
5608 if (!ctx->file_data->table) {
5609 kfree(ctx->file_data);
5610 ctx->file_data = NULL;
5611 return -ENOMEM;
5612 }
5613
5614 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5615 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5616 kfree(ctx->file_data->table);
5617 kfree(ctx->file_data);
5618 ctx->file_data = NULL;
5619 return -ENOMEM;
5620 }
5621 ctx->file_data->put_llist.first = NULL;
5622 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5623
5624 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5625 percpu_ref_exit(&ctx->file_data->refs);
5626 kfree(ctx->file_data->table);
5627 kfree(ctx->file_data);
5628 ctx->file_data = NULL;
5629 return -ENOMEM;
5630 }
5631
5632 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5633 struct fixed_file_table *table;
5634 unsigned index;
5635
5636 ret = -EFAULT;
5637 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5638 break;
5639 /* allow sparse sets */
5640 if (fd == -1) {
5641 ret = 0;
5642 continue;
5643 }
5644
5645 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5646 index = i & IORING_FILE_TABLE_MASK;
5647 file = fget(fd);
5648
5649 ret = -EBADF;
5650 if (!file)
5651 break;
5652
5653 /*
5654 * Don't allow io_uring instances to be registered. If UNIX
5655 * isn't enabled, then this causes a reference cycle and this
5656 * instance can never get freed. If UNIX is enabled we'll
5657 * handle it just fine, but there's still no point in allowing
5658 * a ring fd as it doesn't support regular read/write anyway.
5659 */
5660 if (file->f_op == &io_uring_fops) {
5661 fput(file);
5662 break;
5663 }
5664 ret = 0;
5665 table->files[index] = file;
5666 }
5667
5668 if (ret) {
5669 for (i = 0; i < ctx->nr_user_files; i++) {
5670 file = io_file_from_index(ctx, i);
5671 if (file)
5672 fput(file);
5673 }
5674 for (i = 0; i < nr_tables; i++)
5675 kfree(ctx->file_data->table[i].files);
5676
5677 kfree(ctx->file_data->table);
5678 kfree(ctx->file_data);
5679 ctx->file_data = NULL;
5680 ctx->nr_user_files = 0;
5681 return ret;
5682 }
5683
5684 ret = io_sqe_files_scm(ctx);
5685 if (ret)
5686 io_sqe_files_unregister(ctx);
5687
5688 return ret;
5689}
5690
Jens Axboec3a31e62019-10-03 13:59:56 -06005691static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5692 int index)
5693{
5694#if defined(CONFIG_UNIX)
5695 struct sock *sock = ctx->ring_sock->sk;
5696 struct sk_buff_head *head = &sock->sk_receive_queue;
5697 struct sk_buff *skb;
5698
5699 /*
5700 * See if we can merge this file into an existing skb SCM_RIGHTS
5701 * file set. If there's no room, fall back to allocating a new skb
5702 * and filling it in.
5703 */
5704 spin_lock_irq(&head->lock);
5705 skb = skb_peek(head);
5706 if (skb) {
5707 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5708
5709 if (fpl->count < SCM_MAX_FD) {
5710 __skb_unlink(skb, head);
5711 spin_unlock_irq(&head->lock);
5712 fpl->fp[fpl->count] = get_file(file);
5713 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5714 fpl->count++;
5715 spin_lock_irq(&head->lock);
5716 __skb_queue_head(head, skb);
5717 } else {
5718 skb = NULL;
5719 }
5720 }
5721 spin_unlock_irq(&head->lock);
5722
5723 if (skb) {
5724 fput(file);
5725 return 0;
5726 }
5727
5728 return __io_sqe_files_scm(ctx, 1, index);
5729#else
5730 return 0;
5731#endif
5732}
5733
Jens Axboe05f3fb32019-12-09 11:22:50 -07005734static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005735{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005736 struct fixed_file_data *data;
5737
5738 data = container_of(ref, struct fixed_file_data, refs);
5739 clear_bit(FFD_F_ATOMIC, &data->state);
5740}
5741
5742static bool io_queue_file_removal(struct fixed_file_data *data,
5743 struct file *file)
5744{
5745 struct io_file_put *pfile, pfile_stack;
5746 DECLARE_COMPLETION_ONSTACK(done);
5747
5748 /*
5749 * If we fail allocating the struct we need for doing async reomval
5750 * of this file, just punt to sync and wait for it.
5751 */
5752 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5753 if (!pfile) {
5754 pfile = &pfile_stack;
5755 pfile->done = &done;
5756 }
5757
5758 pfile->file = file;
5759 llist_add(&pfile->llist, &data->put_llist);
5760
5761 if (pfile == &pfile_stack) {
5762 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5763 percpu_ref_put(&data->refs);
5764 percpu_ref_switch_to_atomic(&data->refs,
5765 io_atomic_switch);
5766 }
5767 wait_for_completion(&done);
5768 flush_work(&data->ref_work);
5769 return false;
5770 }
5771
5772 return true;
5773}
5774
5775static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5776 struct io_uring_files_update *up,
5777 unsigned nr_args)
5778{
5779 struct fixed_file_data *data = ctx->file_data;
5780 bool ref_switch = false;
5781 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005782 __s32 __user *fds;
5783 int fd, i, err;
5784 __u32 done;
5785
Jens Axboe05f3fb32019-12-09 11:22:50 -07005786 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005787 return -EOVERFLOW;
5788 if (done > ctx->nr_user_files)
5789 return -EINVAL;
5790
5791 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005792 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005793 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005794 struct fixed_file_table *table;
5795 unsigned index;
5796
Jens Axboec3a31e62019-10-03 13:59:56 -06005797 err = 0;
5798 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5799 err = -EFAULT;
5800 break;
5801 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005802 i = array_index_nospec(up->offset, ctx->nr_user_files);
5803 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005804 index = i & IORING_FILE_TABLE_MASK;
5805 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005806 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005807 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005808 if (io_queue_file_removal(data, file))
5809 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005810 }
5811 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005812 file = fget(fd);
5813 if (!file) {
5814 err = -EBADF;
5815 break;
5816 }
5817 /*
5818 * Don't allow io_uring instances to be registered. If
5819 * UNIX isn't enabled, then this causes a reference
5820 * cycle and this instance can never get freed. If UNIX
5821 * is enabled we'll handle it just fine, but there's
5822 * still no point in allowing a ring fd as it doesn't
5823 * support regular read/write anyway.
5824 */
5825 if (file->f_op == &io_uring_fops) {
5826 fput(file);
5827 err = -EBADF;
5828 break;
5829 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005830 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005831 err = io_sqe_file_register(ctx, file, i);
5832 if (err)
5833 break;
5834 }
5835 nr_args--;
5836 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005837 up->offset++;
5838 }
5839
5840 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5841 percpu_ref_put(&data->refs);
5842 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005843 }
5844
5845 return done ? done : err;
5846}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005847static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5848 unsigned nr_args)
5849{
5850 struct io_uring_files_update up;
5851
5852 if (!ctx->file_data)
5853 return -ENXIO;
5854 if (!nr_args)
5855 return -EINVAL;
5856 if (copy_from_user(&up, arg, sizeof(up)))
5857 return -EFAULT;
5858 if (up.resv)
5859 return -EINVAL;
5860
5861 return __io_sqe_files_update(ctx, &up, nr_args);
5862}
Jens Axboec3a31e62019-10-03 13:59:56 -06005863
Jens Axboe7d723062019-11-12 22:31:31 -07005864static void io_put_work(struct io_wq_work *work)
5865{
5866 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5867
5868 io_put_req(req);
5869}
5870
5871static void io_get_work(struct io_wq_work *work)
5872{
5873 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5874
5875 refcount_inc(&req->refs);
5876}
5877
Pavel Begunkov24369c22020-01-28 03:15:48 +03005878static int io_init_wq_offload(struct io_ring_ctx *ctx,
5879 struct io_uring_params *p)
5880{
5881 struct io_wq_data data;
5882 struct fd f;
5883 struct io_ring_ctx *ctx_attach;
5884 unsigned int concurrency;
5885 int ret = 0;
5886
5887 data.user = ctx->user;
5888 data.get_work = io_get_work;
5889 data.put_work = io_put_work;
5890
5891 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5892 /* Do QD, or 4 * CPUS, whatever is smallest */
5893 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5894
5895 ctx->io_wq = io_wq_create(concurrency, &data);
5896 if (IS_ERR(ctx->io_wq)) {
5897 ret = PTR_ERR(ctx->io_wq);
5898 ctx->io_wq = NULL;
5899 }
5900 return ret;
5901 }
5902
5903 f = fdget(p->wq_fd);
5904 if (!f.file)
5905 return -EBADF;
5906
5907 if (f.file->f_op != &io_uring_fops) {
5908 ret = -EINVAL;
5909 goto out_fput;
5910 }
5911
5912 ctx_attach = f.file->private_data;
5913 /* @io_wq is protected by holding the fd */
5914 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5915 ret = -EINVAL;
5916 goto out_fput;
5917 }
5918
5919 ctx->io_wq = ctx_attach->io_wq;
5920out_fput:
5921 fdput(f);
5922 return ret;
5923}
5924
Jens Axboe6c271ce2019-01-10 11:22:30 -07005925static int io_sq_offload_start(struct io_ring_ctx *ctx,
5926 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005927{
5928 int ret;
5929
Jens Axboe6c271ce2019-01-10 11:22:30 -07005930 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005931 mmgrab(current->mm);
5932 ctx->sqo_mm = current->mm;
5933
Jens Axboe6c271ce2019-01-10 11:22:30 -07005934 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005935 ret = -EPERM;
5936 if (!capable(CAP_SYS_ADMIN))
5937 goto err;
5938
Jens Axboe917257d2019-04-13 09:28:55 -06005939 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5940 if (!ctx->sq_thread_idle)
5941 ctx->sq_thread_idle = HZ;
5942
Jens Axboe6c271ce2019-01-10 11:22:30 -07005943 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005944 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005945
Jens Axboe917257d2019-04-13 09:28:55 -06005946 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005947 if (cpu >= nr_cpu_ids)
5948 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005949 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005950 goto err;
5951
Jens Axboe6c271ce2019-01-10 11:22:30 -07005952 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5953 ctx, cpu,
5954 "io_uring-sq");
5955 } else {
5956 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5957 "io_uring-sq");
5958 }
5959 if (IS_ERR(ctx->sqo_thread)) {
5960 ret = PTR_ERR(ctx->sqo_thread);
5961 ctx->sqo_thread = NULL;
5962 goto err;
5963 }
5964 wake_up_process(ctx->sqo_thread);
5965 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5966 /* Can't have SQ_AFF without SQPOLL */
5967 ret = -EINVAL;
5968 goto err;
5969 }
5970
Pavel Begunkov24369c22020-01-28 03:15:48 +03005971 ret = io_init_wq_offload(ctx, p);
5972 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005973 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005974
5975 return 0;
5976err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005977 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005978 mmdrop(ctx->sqo_mm);
5979 ctx->sqo_mm = NULL;
5980 return ret;
5981}
5982
5983static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5984{
5985 atomic_long_sub(nr_pages, &user->locked_vm);
5986}
5987
5988static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5989{
5990 unsigned long page_limit, cur_pages, new_pages;
5991
5992 /* Don't allow more pages than we can safely lock */
5993 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5994
5995 do {
5996 cur_pages = atomic_long_read(&user->locked_vm);
5997 new_pages = cur_pages + nr_pages;
5998 if (new_pages > page_limit)
5999 return -ENOMEM;
6000 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6001 new_pages) != cur_pages);
6002
6003 return 0;
6004}
6005
6006static void io_mem_free(void *ptr)
6007{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006008 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006009
Mark Rutland52e04ef2019-04-30 17:30:21 +01006010 if (!ptr)
6011 return;
6012
6013 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006014 if (put_page_testzero(page))
6015 free_compound_page(page);
6016}
6017
6018static void *io_mem_alloc(size_t size)
6019{
6020 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6021 __GFP_NORETRY;
6022
6023 return (void *) __get_free_pages(gfp_flags, get_order(size));
6024}
6025
Hristo Venev75b28af2019-08-26 17:23:46 +00006026static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6027 size_t *sq_offset)
6028{
6029 struct io_rings *rings;
6030 size_t off, sq_array_size;
6031
6032 off = struct_size(rings, cqes, cq_entries);
6033 if (off == SIZE_MAX)
6034 return SIZE_MAX;
6035
6036#ifdef CONFIG_SMP
6037 off = ALIGN(off, SMP_CACHE_BYTES);
6038 if (off == 0)
6039 return SIZE_MAX;
6040#endif
6041
6042 sq_array_size = array_size(sizeof(u32), sq_entries);
6043 if (sq_array_size == SIZE_MAX)
6044 return SIZE_MAX;
6045
6046 if (check_add_overflow(off, sq_array_size, &off))
6047 return SIZE_MAX;
6048
6049 if (sq_offset)
6050 *sq_offset = off;
6051
6052 return off;
6053}
6054
Jens Axboe2b188cc2019-01-07 10:46:33 -07006055static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6056{
Hristo Venev75b28af2019-08-26 17:23:46 +00006057 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006058
Hristo Venev75b28af2019-08-26 17:23:46 +00006059 pages = (size_t)1 << get_order(
6060 rings_size(sq_entries, cq_entries, NULL));
6061 pages += (size_t)1 << get_order(
6062 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006063
Hristo Venev75b28af2019-08-26 17:23:46 +00006064 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006065}
6066
Jens Axboeedafcce2019-01-09 09:16:05 -07006067static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6068{
6069 int i, j;
6070
6071 if (!ctx->user_bufs)
6072 return -ENXIO;
6073
6074 for (i = 0; i < ctx->nr_user_bufs; i++) {
6075 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6076
6077 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006078 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006079
6080 if (ctx->account_mem)
6081 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006082 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006083 imu->nr_bvecs = 0;
6084 }
6085
6086 kfree(ctx->user_bufs);
6087 ctx->user_bufs = NULL;
6088 ctx->nr_user_bufs = 0;
6089 return 0;
6090}
6091
6092static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6093 void __user *arg, unsigned index)
6094{
6095 struct iovec __user *src;
6096
6097#ifdef CONFIG_COMPAT
6098 if (ctx->compat) {
6099 struct compat_iovec __user *ciovs;
6100 struct compat_iovec ciov;
6101
6102 ciovs = (struct compat_iovec __user *) arg;
6103 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6104 return -EFAULT;
6105
Jens Axboed55e5f52019-12-11 16:12:15 -07006106 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006107 dst->iov_len = ciov.iov_len;
6108 return 0;
6109 }
6110#endif
6111 src = (struct iovec __user *) arg;
6112 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6113 return -EFAULT;
6114 return 0;
6115}
6116
6117static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6118 unsigned nr_args)
6119{
6120 struct vm_area_struct **vmas = NULL;
6121 struct page **pages = NULL;
6122 int i, j, got_pages = 0;
6123 int ret = -EINVAL;
6124
6125 if (ctx->user_bufs)
6126 return -EBUSY;
6127 if (!nr_args || nr_args > UIO_MAXIOV)
6128 return -EINVAL;
6129
6130 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6131 GFP_KERNEL);
6132 if (!ctx->user_bufs)
6133 return -ENOMEM;
6134
6135 for (i = 0; i < nr_args; i++) {
6136 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6137 unsigned long off, start, end, ubuf;
6138 int pret, nr_pages;
6139 struct iovec iov;
6140 size_t size;
6141
6142 ret = io_copy_iov(ctx, &iov, arg, i);
6143 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006144 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006145
6146 /*
6147 * Don't impose further limits on the size and buffer
6148 * constraints here, we'll -EINVAL later when IO is
6149 * submitted if they are wrong.
6150 */
6151 ret = -EFAULT;
6152 if (!iov.iov_base || !iov.iov_len)
6153 goto err;
6154
6155 /* arbitrary limit, but we need something */
6156 if (iov.iov_len > SZ_1G)
6157 goto err;
6158
6159 ubuf = (unsigned long) iov.iov_base;
6160 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6161 start = ubuf >> PAGE_SHIFT;
6162 nr_pages = end - start;
6163
6164 if (ctx->account_mem) {
6165 ret = io_account_mem(ctx->user, nr_pages);
6166 if (ret)
6167 goto err;
6168 }
6169
6170 ret = 0;
6171 if (!pages || nr_pages > got_pages) {
6172 kfree(vmas);
6173 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006174 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006175 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006176 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006177 sizeof(struct vm_area_struct *),
6178 GFP_KERNEL);
6179 if (!pages || !vmas) {
6180 ret = -ENOMEM;
6181 if (ctx->account_mem)
6182 io_unaccount_mem(ctx->user, nr_pages);
6183 goto err;
6184 }
6185 got_pages = nr_pages;
6186 }
6187
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006188 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006189 GFP_KERNEL);
6190 ret = -ENOMEM;
6191 if (!imu->bvec) {
6192 if (ctx->account_mem)
6193 io_unaccount_mem(ctx->user, nr_pages);
6194 goto err;
6195 }
6196
6197 ret = 0;
6198 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006199 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006200 FOLL_WRITE | FOLL_LONGTERM,
6201 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006202 if (pret == nr_pages) {
6203 /* don't support file backed memory */
6204 for (j = 0; j < nr_pages; j++) {
6205 struct vm_area_struct *vma = vmas[j];
6206
6207 if (vma->vm_file &&
6208 !is_file_hugepages(vma->vm_file)) {
6209 ret = -EOPNOTSUPP;
6210 break;
6211 }
6212 }
6213 } else {
6214 ret = pret < 0 ? pret : -EFAULT;
6215 }
6216 up_read(&current->mm->mmap_sem);
6217 if (ret) {
6218 /*
6219 * if we did partial map, or found file backed vmas,
6220 * release any pages we did get
6221 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006222 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006223 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006224 if (ctx->account_mem)
6225 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006226 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006227 goto err;
6228 }
6229
6230 off = ubuf & ~PAGE_MASK;
6231 size = iov.iov_len;
6232 for (j = 0; j < nr_pages; j++) {
6233 size_t vec_len;
6234
6235 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6236 imu->bvec[j].bv_page = pages[j];
6237 imu->bvec[j].bv_len = vec_len;
6238 imu->bvec[j].bv_offset = off;
6239 off = 0;
6240 size -= vec_len;
6241 }
6242 /* store original address for later verification */
6243 imu->ubuf = ubuf;
6244 imu->len = iov.iov_len;
6245 imu->nr_bvecs = nr_pages;
6246
6247 ctx->nr_user_bufs++;
6248 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006249 kvfree(pages);
6250 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006251 return 0;
6252err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006253 kvfree(pages);
6254 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006255 io_sqe_buffer_unregister(ctx);
6256 return ret;
6257}
6258
Jens Axboe9b402842019-04-11 11:45:41 -06006259static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6260{
6261 __s32 __user *fds = arg;
6262 int fd;
6263
6264 if (ctx->cq_ev_fd)
6265 return -EBUSY;
6266
6267 if (copy_from_user(&fd, fds, sizeof(*fds)))
6268 return -EFAULT;
6269
6270 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6271 if (IS_ERR(ctx->cq_ev_fd)) {
6272 int ret = PTR_ERR(ctx->cq_ev_fd);
6273 ctx->cq_ev_fd = NULL;
6274 return ret;
6275 }
6276
6277 return 0;
6278}
6279
6280static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6281{
6282 if (ctx->cq_ev_fd) {
6283 eventfd_ctx_put(ctx->cq_ev_fd);
6284 ctx->cq_ev_fd = NULL;
6285 return 0;
6286 }
6287
6288 return -ENXIO;
6289}
6290
Jens Axboe2b188cc2019-01-07 10:46:33 -07006291static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6292{
Jens Axboe6b063142019-01-10 22:13:58 -07006293 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006294 if (ctx->sqo_mm)
6295 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006296
6297 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006298 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006299 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006300 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006301
Jens Axboe2b188cc2019-01-07 10:46:33 -07006302#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006303 if (ctx->ring_sock) {
6304 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006305 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006306 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006307#endif
6308
Hristo Venev75b28af2019-08-26 17:23:46 +00006309 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006310 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006311
6312 percpu_ref_exit(&ctx->refs);
6313 if (ctx->account_mem)
6314 io_unaccount_mem(ctx->user,
6315 ring_pages(ctx->sq_entries, ctx->cq_entries));
6316 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006317 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006318 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006319 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006320 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006321 kfree(ctx);
6322}
6323
6324static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6325{
6326 struct io_ring_ctx *ctx = file->private_data;
6327 __poll_t mask = 0;
6328
6329 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006330 /*
6331 * synchronizes with barrier from wq_has_sleeper call in
6332 * io_commit_cqring
6333 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006334 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006335 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6336 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006337 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006338 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006339 mask |= EPOLLIN | EPOLLRDNORM;
6340
6341 return mask;
6342}
6343
6344static int io_uring_fasync(int fd, struct file *file, int on)
6345{
6346 struct io_ring_ctx *ctx = file->private_data;
6347
6348 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6349}
6350
Jens Axboe071698e2020-01-28 10:04:42 -07006351static int io_remove_personalities(int id, void *p, void *data)
6352{
6353 struct io_ring_ctx *ctx = data;
6354 const struct cred *cred;
6355
6356 cred = idr_remove(&ctx->personality_idr, id);
6357 if (cred)
6358 put_cred(cred);
6359 return 0;
6360}
6361
Jens Axboe2b188cc2019-01-07 10:46:33 -07006362static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6363{
6364 mutex_lock(&ctx->uring_lock);
6365 percpu_ref_kill(&ctx->refs);
6366 mutex_unlock(&ctx->uring_lock);
6367
Jens Axboedf069d82020-02-04 16:48:34 -07006368 /*
6369 * Wait for sq thread to idle, if we have one. It won't spin on new
6370 * work after we've killed the ctx ref above. This is important to do
6371 * before we cancel existing commands, as the thread could otherwise
6372 * be queueing new work post that. If that's work we need to cancel,
6373 * it could cause shutdown to hang.
6374 */
6375 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6376 cpu_relax();
6377
Jens Axboe5262f562019-09-17 12:26:57 -06006378 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006379 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006380
6381 if (ctx->io_wq)
6382 io_wq_cancel_all(ctx->io_wq);
6383
Jens Axboedef596e2019-01-09 08:59:42 -07006384 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006385 /* if we failed setting up the ctx, we might not have any rings */
6386 if (ctx->rings)
6387 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006388 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006389 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006390 io_ring_ctx_free(ctx);
6391}
6392
6393static int io_uring_release(struct inode *inode, struct file *file)
6394{
6395 struct io_ring_ctx *ctx = file->private_data;
6396
6397 file->private_data = NULL;
6398 io_ring_ctx_wait_and_kill(ctx);
6399 return 0;
6400}
6401
Jens Axboefcb323c2019-10-24 12:39:47 -06006402static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6403 struct files_struct *files)
6404{
6405 struct io_kiocb *req;
6406 DEFINE_WAIT(wait);
6407
6408 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006409 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006410
6411 spin_lock_irq(&ctx->inflight_lock);
6412 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006413 if (req->work.files != files)
6414 continue;
6415 /* req is being completed, ignore */
6416 if (!refcount_inc_not_zero(&req->refs))
6417 continue;
6418 cancel_req = req;
6419 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006420 }
Jens Axboe768134d2019-11-10 20:30:53 -07006421 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006422 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006423 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006424 spin_unlock_irq(&ctx->inflight_lock);
6425
Jens Axboe768134d2019-11-10 20:30:53 -07006426 /* We need to keep going until we don't find a matching req */
6427 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006428 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006429
6430 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6431 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006432 schedule();
6433 }
Jens Axboe768134d2019-11-10 20:30:53 -07006434 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006435}
6436
6437static int io_uring_flush(struct file *file, void *data)
6438{
6439 struct io_ring_ctx *ctx = file->private_data;
6440
6441 io_uring_cancel_files(ctx, data);
Jens Axboefcb323c2019-10-24 12:39:47 -06006442 return 0;
6443}
6444
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006445static void *io_uring_validate_mmap_request(struct file *file,
6446 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006447{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006448 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006449 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006450 struct page *page;
6451 void *ptr;
6452
6453 switch (offset) {
6454 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006455 case IORING_OFF_CQ_RING:
6456 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006457 break;
6458 case IORING_OFF_SQES:
6459 ptr = ctx->sq_sqes;
6460 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006461 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006462 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006463 }
6464
6465 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006466 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006467 return ERR_PTR(-EINVAL);
6468
6469 return ptr;
6470}
6471
6472#ifdef CONFIG_MMU
6473
6474static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6475{
6476 size_t sz = vma->vm_end - vma->vm_start;
6477 unsigned long pfn;
6478 void *ptr;
6479
6480 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6481 if (IS_ERR(ptr))
6482 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006483
6484 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6485 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6486}
6487
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006488#else /* !CONFIG_MMU */
6489
6490static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6491{
6492 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6493}
6494
6495static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6496{
6497 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6498}
6499
6500static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6501 unsigned long addr, unsigned long len,
6502 unsigned long pgoff, unsigned long flags)
6503{
6504 void *ptr;
6505
6506 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6507 if (IS_ERR(ptr))
6508 return PTR_ERR(ptr);
6509
6510 return (unsigned long) ptr;
6511}
6512
6513#endif /* !CONFIG_MMU */
6514
Jens Axboe2b188cc2019-01-07 10:46:33 -07006515SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6516 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6517 size_t, sigsz)
6518{
6519 struct io_ring_ctx *ctx;
6520 long ret = -EBADF;
6521 int submitted = 0;
6522 struct fd f;
6523
Jens Axboe6c271ce2019-01-10 11:22:30 -07006524 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006525 return -EINVAL;
6526
6527 f = fdget(fd);
6528 if (!f.file)
6529 return -EBADF;
6530
6531 ret = -EOPNOTSUPP;
6532 if (f.file->f_op != &io_uring_fops)
6533 goto out_fput;
6534
6535 ret = -ENXIO;
6536 ctx = f.file->private_data;
6537 if (!percpu_ref_tryget(&ctx->refs))
6538 goto out_fput;
6539
Jens Axboe6c271ce2019-01-10 11:22:30 -07006540 /*
6541 * For SQ polling, the thread will do all submissions and completions.
6542 * Just return the requested submit count, and wake the thread if
6543 * we were asked to.
6544 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006545 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006546 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006547 if (!list_empty_careful(&ctx->cq_overflow_list))
6548 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006549 if (flags & IORING_ENTER_SQ_WAKEUP)
6550 wake_up(&ctx->sqo_wait);
6551 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006552 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006553 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006554
6555 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006556 /* already have mm, so io_submit_sqes() won't try to grab it */
6557 cur_mm = ctx->sqo_mm;
6558 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6559 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006560 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006561
6562 if (submitted != to_submit)
6563 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006564 }
6565 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006566 unsigned nr_events = 0;
6567
Jens Axboe2b188cc2019-01-07 10:46:33 -07006568 min_complete = min(min_complete, ctx->cq_entries);
6569
Jens Axboedef596e2019-01-09 08:59:42 -07006570 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006571 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006572 } else {
6573 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6574 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006575 }
6576
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006577out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006578 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006579out_fput:
6580 fdput(f);
6581 return submitted ? submitted : ret;
6582}
6583
Jens Axboe87ce9552020-01-30 08:25:34 -07006584static int io_uring_show_cred(int id, void *p, void *data)
6585{
6586 const struct cred *cred = p;
6587 struct seq_file *m = data;
6588 struct user_namespace *uns = seq_user_ns(m);
6589 struct group_info *gi;
6590 kernel_cap_t cap;
6591 unsigned __capi;
6592 int g;
6593
6594 seq_printf(m, "%5d\n", id);
6595 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6596 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6597 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6598 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6599 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6600 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6601 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6602 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6603 seq_puts(m, "\n\tGroups:\t");
6604 gi = cred->group_info;
6605 for (g = 0; g < gi->ngroups; g++) {
6606 seq_put_decimal_ull(m, g ? " " : "",
6607 from_kgid_munged(uns, gi->gid[g]));
6608 }
6609 seq_puts(m, "\n\tCapEff:\t");
6610 cap = cred->cap_effective;
6611 CAP_FOR_EACH_U32(__capi)
6612 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6613 seq_putc(m, '\n');
6614 return 0;
6615}
6616
6617static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6618{
6619 int i;
6620
6621 mutex_lock(&ctx->uring_lock);
6622 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6623 for (i = 0; i < ctx->nr_user_files; i++) {
6624 struct fixed_file_table *table;
6625 struct file *f;
6626
6627 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6628 f = table->files[i & IORING_FILE_TABLE_MASK];
6629 if (f)
6630 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6631 else
6632 seq_printf(m, "%5u: <none>\n", i);
6633 }
6634 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6635 for (i = 0; i < ctx->nr_user_bufs; i++) {
6636 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6637
6638 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6639 (unsigned int) buf->len);
6640 }
6641 if (!idr_is_empty(&ctx->personality_idr)) {
6642 seq_printf(m, "Personalities:\n");
6643 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6644 }
6645 mutex_unlock(&ctx->uring_lock);
6646}
6647
6648static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6649{
6650 struct io_ring_ctx *ctx = f->private_data;
6651
6652 if (percpu_ref_tryget(&ctx->refs)) {
6653 __io_uring_show_fdinfo(ctx, m);
6654 percpu_ref_put(&ctx->refs);
6655 }
6656}
6657
Jens Axboe2b188cc2019-01-07 10:46:33 -07006658static const struct file_operations io_uring_fops = {
6659 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006660 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006661 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006662#ifndef CONFIG_MMU
6663 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6664 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6665#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006666 .poll = io_uring_poll,
6667 .fasync = io_uring_fasync,
Jens Axboe87ce9552020-01-30 08:25:34 -07006668 .show_fdinfo = io_uring_show_fdinfo,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006669};
6670
6671static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6672 struct io_uring_params *p)
6673{
Hristo Venev75b28af2019-08-26 17:23:46 +00006674 struct io_rings *rings;
6675 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006676
Hristo Venev75b28af2019-08-26 17:23:46 +00006677 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6678 if (size == SIZE_MAX)
6679 return -EOVERFLOW;
6680
6681 rings = io_mem_alloc(size);
6682 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006683 return -ENOMEM;
6684
Hristo Venev75b28af2019-08-26 17:23:46 +00006685 ctx->rings = rings;
6686 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6687 rings->sq_ring_mask = p->sq_entries - 1;
6688 rings->cq_ring_mask = p->cq_entries - 1;
6689 rings->sq_ring_entries = p->sq_entries;
6690 rings->cq_ring_entries = p->cq_entries;
6691 ctx->sq_mask = rings->sq_ring_mask;
6692 ctx->cq_mask = rings->cq_ring_mask;
6693 ctx->sq_entries = rings->sq_ring_entries;
6694 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006695
6696 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006697 if (size == SIZE_MAX) {
6698 io_mem_free(ctx->rings);
6699 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006700 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006701 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006702
6703 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006704 if (!ctx->sq_sqes) {
6705 io_mem_free(ctx->rings);
6706 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006707 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006708 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006709
Jens Axboe2b188cc2019-01-07 10:46:33 -07006710 return 0;
6711}
6712
6713/*
6714 * Allocate an anonymous fd, this is what constitutes the application
6715 * visible backing of an io_uring instance. The application mmaps this
6716 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6717 * we have to tie this fd to a socket for file garbage collection purposes.
6718 */
6719static int io_uring_get_fd(struct io_ring_ctx *ctx)
6720{
6721 struct file *file;
6722 int ret;
6723
6724#if defined(CONFIG_UNIX)
6725 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6726 &ctx->ring_sock);
6727 if (ret)
6728 return ret;
6729#endif
6730
6731 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6732 if (ret < 0)
6733 goto err;
6734
6735 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6736 O_RDWR | O_CLOEXEC);
6737 if (IS_ERR(file)) {
6738 put_unused_fd(ret);
6739 ret = PTR_ERR(file);
6740 goto err;
6741 }
6742
6743#if defined(CONFIG_UNIX)
6744 ctx->ring_sock->file = file;
6745#endif
6746 fd_install(ret, file);
6747 return ret;
6748err:
6749#if defined(CONFIG_UNIX)
6750 sock_release(ctx->ring_sock);
6751 ctx->ring_sock = NULL;
6752#endif
6753 return ret;
6754}
6755
6756static int io_uring_create(unsigned entries, struct io_uring_params *p)
6757{
6758 struct user_struct *user = NULL;
6759 struct io_ring_ctx *ctx;
6760 bool account_mem;
6761 int ret;
6762
Jens Axboe8110c1a2019-12-28 15:39:54 -07006763 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006764 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006765 if (entries > IORING_MAX_ENTRIES) {
6766 if (!(p->flags & IORING_SETUP_CLAMP))
6767 return -EINVAL;
6768 entries = IORING_MAX_ENTRIES;
6769 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006770
6771 /*
6772 * Use twice as many entries for the CQ ring. It's possible for the
6773 * application to drive a higher depth than the size of the SQ ring,
6774 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006775 * some flexibility in overcommitting a bit. If the application has
6776 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6777 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006778 */
6779 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006780 if (p->flags & IORING_SETUP_CQSIZE) {
6781 /*
6782 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6783 * to a power-of-two, if it isn't already. We do NOT impose
6784 * any cq vs sq ring sizing.
6785 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006786 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006787 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006788 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6789 if (!(p->flags & IORING_SETUP_CLAMP))
6790 return -EINVAL;
6791 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6792 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006793 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6794 } else {
6795 p->cq_entries = 2 * p->sq_entries;
6796 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006797
6798 user = get_uid(current_user());
6799 account_mem = !capable(CAP_IPC_LOCK);
6800
6801 if (account_mem) {
6802 ret = io_account_mem(user,
6803 ring_pages(p->sq_entries, p->cq_entries));
6804 if (ret) {
6805 free_uid(user);
6806 return ret;
6807 }
6808 }
6809
6810 ctx = io_ring_ctx_alloc(p);
6811 if (!ctx) {
6812 if (account_mem)
6813 io_unaccount_mem(user, ring_pages(p->sq_entries,
6814 p->cq_entries));
6815 free_uid(user);
6816 return -ENOMEM;
6817 }
6818 ctx->compat = in_compat_syscall();
6819 ctx->account_mem = account_mem;
6820 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006821 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006822
6823 ret = io_allocate_scq_urings(ctx, p);
6824 if (ret)
6825 goto err;
6826
Jens Axboe6c271ce2019-01-10 11:22:30 -07006827 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006828 if (ret)
6829 goto err;
6830
Jens Axboe2b188cc2019-01-07 10:46:33 -07006831 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006832 p->sq_off.head = offsetof(struct io_rings, sq.head);
6833 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6834 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6835 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6836 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6837 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6838 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006839
6840 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006841 p->cq_off.head = offsetof(struct io_rings, cq.head);
6842 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6843 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6844 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6845 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6846 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006847
Jens Axboe044c1ab2019-10-28 09:15:33 -06006848 /*
6849 * Install ring fd as the very last thing, so we don't risk someone
6850 * having closed it before we finish setup
6851 */
6852 ret = io_uring_get_fd(ctx);
6853 if (ret < 0)
6854 goto err;
6855
Jens Axboeda8c9692019-12-02 18:51:26 -07006856 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006857 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6858 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006859 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006860 return ret;
6861err:
6862 io_ring_ctx_wait_and_kill(ctx);
6863 return ret;
6864}
6865
6866/*
6867 * Sets up an aio uring context, and returns the fd. Applications asks for a
6868 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6869 * params structure passed in.
6870 */
6871static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6872{
6873 struct io_uring_params p;
6874 long ret;
6875 int i;
6876
6877 if (copy_from_user(&p, params, sizeof(p)))
6878 return -EFAULT;
6879 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6880 if (p.resv[i])
6881 return -EINVAL;
6882 }
6883
Jens Axboe6c271ce2019-01-10 11:22:30 -07006884 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006885 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006886 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006887 return -EINVAL;
6888
6889 ret = io_uring_create(entries, &p);
6890 if (ret < 0)
6891 return ret;
6892
6893 if (copy_to_user(params, &p, sizeof(p)))
6894 return -EFAULT;
6895
6896 return ret;
6897}
6898
6899SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6900 struct io_uring_params __user *, params)
6901{
6902 return io_uring_setup(entries, params);
6903}
6904
Jens Axboe66f4af92020-01-16 15:36:52 -07006905static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6906{
6907 struct io_uring_probe *p;
6908 size_t size;
6909 int i, ret;
6910
6911 size = struct_size(p, ops, nr_args);
6912 if (size == SIZE_MAX)
6913 return -EOVERFLOW;
6914 p = kzalloc(size, GFP_KERNEL);
6915 if (!p)
6916 return -ENOMEM;
6917
6918 ret = -EFAULT;
6919 if (copy_from_user(p, arg, size))
6920 goto out;
6921 ret = -EINVAL;
6922 if (memchr_inv(p, 0, size))
6923 goto out;
6924
6925 p->last_op = IORING_OP_LAST - 1;
6926 if (nr_args > IORING_OP_LAST)
6927 nr_args = IORING_OP_LAST;
6928
6929 for (i = 0; i < nr_args; i++) {
6930 p->ops[i].op = i;
6931 if (!io_op_defs[i].not_supported)
6932 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6933 }
6934 p->ops_len = i;
6935
6936 ret = 0;
6937 if (copy_to_user(arg, p, size))
6938 ret = -EFAULT;
6939out:
6940 kfree(p);
6941 return ret;
6942}
6943
Jens Axboe071698e2020-01-28 10:04:42 -07006944static int io_register_personality(struct io_ring_ctx *ctx)
6945{
6946 const struct cred *creds = get_current_cred();
6947 int id;
6948
6949 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
6950 USHRT_MAX, GFP_KERNEL);
6951 if (id < 0)
6952 put_cred(creds);
6953 return id;
6954}
6955
6956static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
6957{
6958 const struct cred *old_creds;
6959
6960 old_creds = idr_remove(&ctx->personality_idr, id);
6961 if (old_creds) {
6962 put_cred(old_creds);
6963 return 0;
6964 }
6965
6966 return -EINVAL;
6967}
6968
6969static bool io_register_op_must_quiesce(int op)
6970{
6971 switch (op) {
6972 case IORING_UNREGISTER_FILES:
6973 case IORING_REGISTER_FILES_UPDATE:
6974 case IORING_REGISTER_PROBE:
6975 case IORING_REGISTER_PERSONALITY:
6976 case IORING_UNREGISTER_PERSONALITY:
6977 return false;
6978 default:
6979 return true;
6980 }
6981}
6982
Jens Axboeedafcce2019-01-09 09:16:05 -07006983static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6984 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006985 __releases(ctx->uring_lock)
6986 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006987{
6988 int ret;
6989
Jens Axboe35fa71a2019-04-22 10:23:23 -06006990 /*
6991 * We're inside the ring mutex, if the ref is already dying, then
6992 * someone else killed the ctx or is already going through
6993 * io_uring_register().
6994 */
6995 if (percpu_ref_is_dying(&ctx->refs))
6996 return -ENXIO;
6997
Jens Axboe071698e2020-01-28 10:04:42 -07006998 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006999 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007000
Jens Axboe05f3fb32019-12-09 11:22:50 -07007001 /*
7002 * Drop uring mutex before waiting for references to exit. If
7003 * another thread is currently inside io_uring_enter() it might
7004 * need to grab the uring_lock to make progress. If we hold it
7005 * here across the drain wait, then we can deadlock. It's safe
7006 * to drop the mutex here, since no new references will come in
7007 * after we've killed the percpu ref.
7008 */
7009 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007010 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007011 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007012 if (ret) {
7013 percpu_ref_resurrect(&ctx->refs);
7014 ret = -EINTR;
7015 goto out;
7016 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007017 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007018
7019 switch (opcode) {
7020 case IORING_REGISTER_BUFFERS:
7021 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7022 break;
7023 case IORING_UNREGISTER_BUFFERS:
7024 ret = -EINVAL;
7025 if (arg || nr_args)
7026 break;
7027 ret = io_sqe_buffer_unregister(ctx);
7028 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007029 case IORING_REGISTER_FILES:
7030 ret = io_sqe_files_register(ctx, arg, nr_args);
7031 break;
7032 case IORING_UNREGISTER_FILES:
7033 ret = -EINVAL;
7034 if (arg || nr_args)
7035 break;
7036 ret = io_sqe_files_unregister(ctx);
7037 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007038 case IORING_REGISTER_FILES_UPDATE:
7039 ret = io_sqe_files_update(ctx, arg, nr_args);
7040 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007041 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007042 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007043 ret = -EINVAL;
7044 if (nr_args != 1)
7045 break;
7046 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007047 if (ret)
7048 break;
7049 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7050 ctx->eventfd_async = 1;
7051 else
7052 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007053 break;
7054 case IORING_UNREGISTER_EVENTFD:
7055 ret = -EINVAL;
7056 if (arg || nr_args)
7057 break;
7058 ret = io_eventfd_unregister(ctx);
7059 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007060 case IORING_REGISTER_PROBE:
7061 ret = -EINVAL;
7062 if (!arg || nr_args > 256)
7063 break;
7064 ret = io_probe(ctx, arg, nr_args);
7065 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007066 case IORING_REGISTER_PERSONALITY:
7067 ret = -EINVAL;
7068 if (arg || nr_args)
7069 break;
7070 ret = io_register_personality(ctx);
7071 break;
7072 case IORING_UNREGISTER_PERSONALITY:
7073 ret = -EINVAL;
7074 if (arg)
7075 break;
7076 ret = io_unregister_personality(ctx, nr_args);
7077 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007078 default:
7079 ret = -EINVAL;
7080 break;
7081 }
7082
Jens Axboe071698e2020-01-28 10:04:42 -07007083 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007084 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007085 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007086out:
7087 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007088 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007089 return ret;
7090}
7091
7092SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7093 void __user *, arg, unsigned int, nr_args)
7094{
7095 struct io_ring_ctx *ctx;
7096 long ret = -EBADF;
7097 struct fd f;
7098
7099 f = fdget(fd);
7100 if (!f.file)
7101 return -EBADF;
7102
7103 ret = -EOPNOTSUPP;
7104 if (f.file->f_op != &io_uring_fops)
7105 goto out_fput;
7106
7107 ctx = f.file->private_data;
7108
7109 mutex_lock(&ctx->uring_lock);
7110 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7111 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007112 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7113 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007114out_fput:
7115 fdput(f);
7116 return ret;
7117}
7118
Jens Axboe2b188cc2019-01-07 10:46:33 -07007119static int __init io_uring_init(void)
7120{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007121#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7122 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7123 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7124} while (0)
7125
7126#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7127 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7128 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7129 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7130 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7131 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7132 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7133 BUILD_BUG_SQE_ELEM(8, __u64, off);
7134 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7135 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7136 BUILD_BUG_SQE_ELEM(24, __u32, len);
7137 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7138 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7139 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7140 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7141 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7142 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7143 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7144 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7145 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7146 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7147 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7148 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7149 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7150 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7151 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7152 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7153
Jens Axboed3656342019-12-18 09:50:26 -07007154 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007155 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7156 return 0;
7157};
7158__initcall(io_uring_init);