blob: 4bde5da2c2f55068d39b2dc9a3dd77231b05254d [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030049#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070050
51#include <linux/sched/signal.h>
52#include <linux/fs.h>
53#include <linux/file.h>
54#include <linux/fdtable.h>
55#include <linux/mm.h>
56#include <linux/mman.h>
57#include <linux/mmu_context.h>
58#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070077
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020078#define CREATE_TRACE_POINTS
79#include <trace/events/io_uring.h>
80
Jens Axboe2b188cc2019-01-07 10:46:33 -070081#include <uapi/linux/io_uring.h>
82
83#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060084#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070085
Daniel Xu5277dea2019-09-14 14:23:45 -070086#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060087#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060088
89/*
90 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
91 */
92#define IORING_FILE_TABLE_SHIFT 9
93#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
94#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
95#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070096
97struct io_uring {
98 u32 head ____cacheline_aligned_in_smp;
99 u32 tail ____cacheline_aligned_in_smp;
100};
101
Stefan Bühler1e84b972019-04-24 23:54:16 +0200102/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000103 * This data is shared with the application through the mmap at offsets
104 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 *
106 * The offsets to the member fields are published through struct
107 * io_sqring_offsets when calling io_uring_setup.
108 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000109struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 /*
111 * Head and tail offsets into the ring; the offsets need to be
112 * masked to get valid indices.
113 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000114 * The kernel controls head of the sq ring and the tail of the cq ring,
115 * and the application controls tail of the sq ring and the head of the
116 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200117 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000120 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 * ring_entries - 1)
122 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 u32 sq_ring_mask, cq_ring_mask;
124 /* Ring sizes (constant, power of 2) */
125 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200126 /*
127 * Number of invalid entries dropped by the kernel due to
128 * invalid index stored in array
129 *
130 * Written by the kernel, shouldn't be modified by the
131 * application (i.e. get number of "new events" by comparing to
132 * cached value).
133 *
134 * After a new SQ head value was read by the application this
135 * counter includes all submissions that were dropped reaching
136 * the new SQ head (and possibly more).
137 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000138 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200139 /*
140 * Runtime flags
141 *
142 * Written by the kernel, shouldn't be modified by the
143 * application.
144 *
145 * The application needs a full memory barrier before checking
146 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
147 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000148 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 /*
150 * Number of completion events lost because the queue was full;
151 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800152 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200153 * the completion queue.
154 *
155 * Written by the kernel, shouldn't be modified by the
156 * application (i.e. get number of "new events" by comparing to
157 * cached value).
158 *
159 * As completion events come in out of order this counter is not
160 * ordered with any other data.
161 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000162 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200163 /*
164 * Ring buffer of completion events.
165 *
166 * The kernel writes completion events fresh every time they are
167 * produced, so the application is allowed to modify pending
168 * entries.
169 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000170 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700171};
172
Jens Axboeedafcce2019-01-09 09:16:05 -0700173struct io_mapped_ubuf {
174 u64 ubuf;
175 size_t len;
176 struct bio_vec *bvec;
177 unsigned int nr_bvecs;
178};
179
Jens Axboe65e19f52019-10-26 07:20:21 -0600180struct fixed_file_table {
181 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700182};
183
Jens Axboe05f3fb32019-12-09 11:22:50 -0700184enum {
185 FFD_F_ATOMIC,
186};
187
188struct fixed_file_data {
189 struct fixed_file_table *table;
190 struct io_ring_ctx *ctx;
191
192 struct percpu_ref refs;
193 struct llist_head put_llist;
194 unsigned long state;
195 struct work_struct ref_work;
196 struct completion done;
197};
198
Jens Axboe2b188cc2019-01-07 10:46:33 -0700199struct io_ring_ctx {
200 struct {
201 struct percpu_ref refs;
202 } ____cacheline_aligned_in_smp;
203
204 struct {
205 unsigned int flags;
Jens Axboe69b3e542020-01-08 11:01:46 -0700206 int compat: 1;
207 int account_mem: 1;
208 int cq_overflow_flushed: 1;
209 int drain_next: 1;
Jens Axboef2842ab2020-01-08 11:04:00 -0700210 int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700211
Hristo Venev75b28af2019-08-26 17:23:46 +0000212 /*
213 * Ring buffer of indices into array of io_uring_sqe, which is
214 * mmapped by the application using the IORING_OFF_SQES offset.
215 *
216 * This indirection could e.g. be used to assign fixed
217 * io_uring_sqe entries to operations and only submit them to
218 * the queue when needed.
219 *
220 * The kernel modifies neither the indices array nor the entries
221 * array.
222 */
223 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700224 unsigned cached_sq_head;
225 unsigned sq_entries;
226 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700227 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600228 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700229 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700230 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600231
232 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600233 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700234 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700235
Jens Axboefcb323c2019-10-24 12:39:47 -0600236 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700237 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700238 } ____cacheline_aligned_in_smp;
239
Hristo Venev75b28af2019-08-26 17:23:46 +0000240 struct io_rings *rings;
241
Jens Axboe2b188cc2019-01-07 10:46:33 -0700242 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600243 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700244 struct task_struct *sqo_thread; /* if using sq thread polling */
245 struct mm_struct *sqo_mm;
246 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700247
Jens Axboe6b063142019-01-10 22:13:58 -0700248 /*
249 * If used, fixed file set. Writers must ensure that ->refs is dead,
250 * readers must ensure that ->refs is alive as long as the file* is
251 * used. Only updated through io_uring_register(2).
252 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700253 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700254 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300255 int ring_fd;
256 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700257
Jens Axboeedafcce2019-01-09 09:16:05 -0700258 /* if used, fixed mapped user buffers */
259 unsigned nr_user_bufs;
260 struct io_mapped_ubuf *user_bufs;
261
Jens Axboe2b188cc2019-01-07 10:46:33 -0700262 struct user_struct *user;
263
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700264 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700265
Jens Axboe206aefd2019-11-07 18:27:42 -0700266 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
267 struct completion *completions;
268
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700269 /* if all else fails... */
270 struct io_kiocb *fallback_req;
271
Jens Axboe206aefd2019-11-07 18:27:42 -0700272#if defined(CONFIG_UNIX)
273 struct socket *ring_sock;
274#endif
275
276 struct {
277 unsigned cached_cq_tail;
278 unsigned cq_entries;
279 unsigned cq_mask;
280 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700281 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700282 struct wait_queue_head cq_wait;
283 struct fasync_struct *cq_fasync;
284 struct eventfd_ctx *cq_ev_fd;
285 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700286
287 struct {
288 struct mutex uring_lock;
289 wait_queue_head_t wait;
290 } ____cacheline_aligned_in_smp;
291
292 struct {
293 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700294 struct llist_head poll_llist;
295
Jens Axboedef596e2019-01-09 08:59:42 -0700296 /*
297 * ->poll_list is protected by the ctx->uring_lock for
298 * io_uring instances that don't use IORING_SETUP_SQPOLL.
299 * For SQPOLL, only the single threaded io_sq_thread() will
300 * manipulate the list, hence no extra locking is needed there.
301 */
302 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700303 struct hlist_head *cancel_hash;
304 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700305 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600306
307 spinlock_t inflight_lock;
308 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700309 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700310};
311
Jens Axboe09bb8392019-03-13 12:39:28 -0600312/*
313 * First field must be the file pointer in all the
314 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
315 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700316struct io_poll_iocb {
317 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700318 union {
319 struct wait_queue_head *head;
320 u64 addr;
321 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700322 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600323 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700324 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700325 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700326};
327
Jens Axboeb5dba592019-12-11 14:02:38 -0700328struct io_close {
329 struct file *file;
330 struct file *put_file;
331 int fd;
332};
333
Jens Axboead8a48a2019-11-15 08:49:11 -0700334struct io_timeout_data {
335 struct io_kiocb *req;
336 struct hrtimer timer;
337 struct timespec64 ts;
338 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300339 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700340};
341
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700342struct io_accept {
343 struct file *file;
344 struct sockaddr __user *addr;
345 int __user *addr_len;
346 int flags;
347};
348
349struct io_sync {
350 struct file *file;
351 loff_t len;
352 loff_t off;
353 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700354 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700355};
356
Jens Axboefbf23842019-12-17 18:45:56 -0700357struct io_cancel {
358 struct file *file;
359 u64 addr;
360};
361
Jens Axboeb29472e2019-12-17 18:50:29 -0700362struct io_timeout {
363 struct file *file;
364 u64 addr;
365 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700366 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700367};
368
Jens Axboe9adbd452019-12-20 08:45:55 -0700369struct io_rw {
370 /* NOTE: kiocb has the file as the first member, so don't do it here */
371 struct kiocb kiocb;
372 u64 addr;
373 u64 len;
374};
375
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700376struct io_connect {
377 struct file *file;
378 struct sockaddr __user *addr;
379 int addr_len;
380};
381
Jens Axboee47293f2019-12-20 08:58:21 -0700382struct io_sr_msg {
383 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700384 union {
385 struct user_msghdr __user *msg;
386 void __user *buf;
387 };
Jens Axboee47293f2019-12-20 08:58:21 -0700388 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700389 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700390};
391
Jens Axboe15b71ab2019-12-11 11:20:36 -0700392struct io_open {
393 struct file *file;
394 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700395 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700396 unsigned mask;
397 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700398 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700399 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700400 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700401};
402
Jens Axboe05f3fb32019-12-09 11:22:50 -0700403struct io_files_update {
404 struct file *file;
405 u64 arg;
406 u32 nr_args;
407 u32 offset;
408};
409
Jens Axboe4840e412019-12-25 22:03:45 -0700410struct io_fadvise {
411 struct file *file;
412 u64 offset;
413 u32 len;
414 u32 advice;
415};
416
Jens Axboec1ca7572019-12-25 22:18:28 -0700417struct io_madvise {
418 struct file *file;
419 u64 addr;
420 u32 len;
421 u32 advice;
422};
423
Jens Axboef499a022019-12-02 16:28:46 -0700424struct io_async_connect {
425 struct sockaddr_storage address;
426};
427
Jens Axboe03b12302019-12-02 18:50:25 -0700428struct io_async_msghdr {
429 struct iovec fast_iov[UIO_FASTIOV];
430 struct iovec *iov;
431 struct sockaddr __user *uaddr;
432 struct msghdr msg;
433};
434
Jens Axboef67676d2019-12-02 11:03:47 -0700435struct io_async_rw {
436 struct iovec fast_iov[UIO_FASTIOV];
437 struct iovec *iov;
438 ssize_t nr_segs;
439 ssize_t size;
440};
441
Jens Axboe15b71ab2019-12-11 11:20:36 -0700442struct io_async_open {
443 struct filename *filename;
444};
445
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700446struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700447 union {
448 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700449 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700450 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700451 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700452 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700453 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700454};
455
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300456enum {
457 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
458 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
459 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
460 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
461 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
462
463 REQ_F_LINK_NEXT_BIT,
464 REQ_F_FAIL_LINK_BIT,
465 REQ_F_INFLIGHT_BIT,
466 REQ_F_CUR_POS_BIT,
467 REQ_F_NOWAIT_BIT,
468 REQ_F_IOPOLL_COMPLETED_BIT,
469 REQ_F_LINK_TIMEOUT_BIT,
470 REQ_F_TIMEOUT_BIT,
471 REQ_F_ISREG_BIT,
472 REQ_F_MUST_PUNT_BIT,
473 REQ_F_TIMEOUT_NOSEQ_BIT,
474 REQ_F_COMP_LOCKED_BIT,
475};
476
477enum {
478 /* ctx owns file */
479 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
480 /* drain existing IO first */
481 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
482 /* linked sqes */
483 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
484 /* doesn't sever on completion < 0 */
485 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
486 /* IOSQE_ASYNC */
487 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
488
489 /* already grabbed next link */
490 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
491 /* fail rest of links */
492 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
493 /* on inflight list */
494 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
495 /* read/write uses file position */
496 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
497 /* must not punt to workers */
498 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
499 /* polled IO has completed */
500 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
501 /* has linked timeout */
502 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
503 /* timeout request */
504 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
505 /* regular file */
506 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
507 /* must be punted even for NONBLOCK */
508 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
509 /* no timeout sequence */
510 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
511 /* completion under lock */
512 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
513};
514
Jens Axboe09bb8392019-03-13 12:39:28 -0600515/*
516 * NOTE! Each of the iocb union members has the file pointer
517 * as the first entry in their struct definition. So you can
518 * access the file pointer through any of the sub-structs,
519 * or directly as just 'ki_filp' in this struct.
520 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700521struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700522 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600523 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700524 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700525 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700526 struct io_accept accept;
527 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700528 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700529 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700530 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700531 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700532 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700533 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700534 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700535 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700536 struct io_madvise madvise;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700537 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700538
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700539 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300540 /*
541 * llist_node is only used for poll deferred completions
542 */
543 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300544 bool has_user;
545 bool in_async;
546 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700547 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700548
549 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700550 union {
551 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700552 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700553 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600554 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700555 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700556 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700557 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600558 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600559 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700560
Jens Axboefcb323c2019-10-24 12:39:47 -0600561 struct list_head inflight_entry;
562
Jens Axboe561fb042019-10-24 07:25:42 -0600563 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700564};
565
566#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700567#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700568
Jens Axboe9a56a232019-01-09 09:06:50 -0700569struct io_submit_state {
570 struct blk_plug plug;
571
572 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700573 * io_kiocb alloc cache
574 */
575 void *reqs[IO_IOPOLL_BATCH];
576 unsigned int free_reqs;
577 unsigned int cur_req;
578
579 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700580 * File reference cache
581 */
582 struct file *file;
583 unsigned int fd;
584 unsigned int has_refs;
585 unsigned int used_refs;
586 unsigned int ios_left;
587};
588
Jens Axboed3656342019-12-18 09:50:26 -0700589struct io_op_def {
590 /* needs req->io allocated for deferral/async */
591 unsigned async_ctx : 1;
592 /* needs current->mm setup, does mm access */
593 unsigned needs_mm : 1;
594 /* needs req->file assigned */
595 unsigned needs_file : 1;
596 /* needs req->file assigned IFF fd is >= 0 */
597 unsigned fd_non_neg : 1;
598 /* hash wq insertion if file is a regular file */
599 unsigned hash_reg_file : 1;
600 /* unbound wq insertion if file is a non-regular file */
601 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700602 /* opcode is not supported by this kernel */
603 unsigned not_supported : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700604};
605
606static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300607 [IORING_OP_NOP] = {},
608 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700609 .async_ctx = 1,
610 .needs_mm = 1,
611 .needs_file = 1,
612 .unbound_nonreg_file = 1,
613 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300614 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700615 .async_ctx = 1,
616 .needs_mm = 1,
617 .needs_file = 1,
618 .hash_reg_file = 1,
619 .unbound_nonreg_file = 1,
620 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300621 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700622 .needs_file = 1,
623 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300624 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700625 .needs_file = 1,
626 .unbound_nonreg_file = 1,
627 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300628 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700629 .needs_file = 1,
630 .hash_reg_file = 1,
631 .unbound_nonreg_file = 1,
632 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300633 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700634 .needs_file = 1,
635 .unbound_nonreg_file = 1,
636 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300637 [IORING_OP_POLL_REMOVE] = {},
638 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700639 .needs_file = 1,
640 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300641 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700642 .async_ctx = 1,
643 .needs_mm = 1,
644 .needs_file = 1,
645 .unbound_nonreg_file = 1,
646 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300647 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700648 .async_ctx = 1,
649 .needs_mm = 1,
650 .needs_file = 1,
651 .unbound_nonreg_file = 1,
652 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300653 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700654 .async_ctx = 1,
655 .needs_mm = 1,
656 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300657 [IORING_OP_TIMEOUT_REMOVE] = {},
658 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700659 .needs_mm = 1,
660 .needs_file = 1,
661 .unbound_nonreg_file = 1,
662 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300663 [IORING_OP_ASYNC_CANCEL] = {},
664 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700665 .async_ctx = 1,
666 .needs_mm = 1,
667 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300668 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700669 .async_ctx = 1,
670 .needs_mm = 1,
671 .needs_file = 1,
672 .unbound_nonreg_file = 1,
673 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300674 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700675 .needs_file = 1,
676 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300677 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700678 .needs_file = 1,
679 .fd_non_neg = 1,
680 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300681 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700682 .needs_file = 1,
683 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300684 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700685 .needs_mm = 1,
686 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300687 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700688 .needs_mm = 1,
689 .needs_file = 1,
690 .fd_non_neg = 1,
691 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300692 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700693 .needs_mm = 1,
694 .needs_file = 1,
695 .unbound_nonreg_file = 1,
696 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300697 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700698 .needs_mm = 1,
699 .needs_file = 1,
700 .unbound_nonreg_file = 1,
701 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300702 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700703 .needs_file = 1,
704 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300705 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700706 .needs_mm = 1,
707 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300708 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700709 .needs_mm = 1,
710 .needs_file = 1,
711 .unbound_nonreg_file = 1,
712 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300713 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700714 .needs_mm = 1,
715 .needs_file = 1,
716 .unbound_nonreg_file = 1,
717 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300718 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700719 .needs_file = 1,
720 .fd_non_neg = 1,
721 },
Jens Axboed3656342019-12-18 09:50:26 -0700722};
723
Jens Axboe561fb042019-10-24 07:25:42 -0600724static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700725static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800726static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700727static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700728static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
729static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700730static int __io_sqe_files_update(struct io_ring_ctx *ctx,
731 struct io_uring_files_update *ip,
732 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600733
Jens Axboe2b188cc2019-01-07 10:46:33 -0700734static struct kmem_cache *req_cachep;
735
736static const struct file_operations io_uring_fops;
737
738struct sock *io_uring_get_socket(struct file *file)
739{
740#if defined(CONFIG_UNIX)
741 if (file->f_op == &io_uring_fops) {
742 struct io_ring_ctx *ctx = file->private_data;
743
744 return ctx->ring_sock->sk;
745 }
746#endif
747 return NULL;
748}
749EXPORT_SYMBOL(io_uring_get_socket);
750
751static void io_ring_ctx_ref_free(struct percpu_ref *ref)
752{
753 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
754
Jens Axboe206aefd2019-11-07 18:27:42 -0700755 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700756}
757
758static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
759{
760 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700761 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700762
763 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
764 if (!ctx)
765 return NULL;
766
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700767 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
768 if (!ctx->fallback_req)
769 goto err;
770
Jens Axboe206aefd2019-11-07 18:27:42 -0700771 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
772 if (!ctx->completions)
773 goto err;
774
Jens Axboe78076bb2019-12-04 19:56:40 -0700775 /*
776 * Use 5 bits less than the max cq entries, that should give us around
777 * 32 entries per hash list if totally full and uniformly spread.
778 */
779 hash_bits = ilog2(p->cq_entries);
780 hash_bits -= 5;
781 if (hash_bits <= 0)
782 hash_bits = 1;
783 ctx->cancel_hash_bits = hash_bits;
784 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
785 GFP_KERNEL);
786 if (!ctx->cancel_hash)
787 goto err;
788 __hash_init(ctx->cancel_hash, 1U << hash_bits);
789
Roman Gushchin21482892019-05-07 10:01:48 -0700790 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700791 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
792 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700793
794 ctx->flags = p->flags;
795 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700796 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700797 init_completion(&ctx->completions[0]);
798 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700799 mutex_init(&ctx->uring_lock);
800 init_waitqueue_head(&ctx->wait);
801 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700802 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700803 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600804 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600805 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600806 init_waitqueue_head(&ctx->inflight_wait);
807 spin_lock_init(&ctx->inflight_lock);
808 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700809 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700810err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700811 if (ctx->fallback_req)
812 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700813 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700814 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700815 kfree(ctx);
816 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700817}
818
Bob Liu9d858b22019-11-13 18:06:25 +0800819static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600820{
Jackie Liua197f662019-11-08 08:09:12 -0700821 struct io_ring_ctx *ctx = req->ctx;
822
Jens Axboe498ccd92019-10-25 10:04:25 -0600823 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
824 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600825}
826
Bob Liu9d858b22019-11-13 18:06:25 +0800827static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600828{
Pavel Begunkov87987892020-01-18 01:22:30 +0300829 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800830 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600831
Bob Liu9d858b22019-11-13 18:06:25 +0800832 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600833}
834
835static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600836{
837 struct io_kiocb *req;
838
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600839 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800840 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600841 list_del_init(&req->list);
842 return req;
843 }
844
845 return NULL;
846}
847
Jens Axboe5262f562019-09-17 12:26:57 -0600848static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
849{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600850 struct io_kiocb *req;
851
852 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700853 if (req) {
854 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
855 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800856 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700857 list_del_init(&req->list);
858 return req;
859 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600860 }
861
862 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600863}
864
Jens Axboede0617e2019-04-06 21:51:27 -0600865static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700866{
Hristo Venev75b28af2019-08-26 17:23:46 +0000867 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700868
Pavel Begunkov07910152020-01-17 03:52:46 +0300869 /* order cqe stores with ring update */
870 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700871
Pavel Begunkov07910152020-01-17 03:52:46 +0300872 if (wq_has_sleeper(&ctx->cq_wait)) {
873 wake_up_interruptible(&ctx->cq_wait);
874 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700875 }
876}
877
Jens Axboe94ae5e72019-11-14 19:39:52 -0700878static inline bool io_prep_async_work(struct io_kiocb *req,
879 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600880{
Jens Axboed3656342019-12-18 09:50:26 -0700881 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600882 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600883
Jens Axboed3656342019-12-18 09:50:26 -0700884 if (req->flags & REQ_F_ISREG) {
885 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700886 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700887 } else {
888 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700889 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600890 }
Jens Axboed3656342019-12-18 09:50:26 -0700891 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700892 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600893
Jens Axboe94ae5e72019-11-14 19:39:52 -0700894 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600895 return do_hashed;
896}
897
Jackie Liua197f662019-11-08 08:09:12 -0700898static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600899{
Jackie Liua197f662019-11-08 08:09:12 -0700900 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700901 struct io_kiocb *link;
902 bool do_hashed;
903
904 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600905
906 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
907 req->flags);
908 if (!do_hashed) {
909 io_wq_enqueue(ctx->io_wq, &req->work);
910 } else {
911 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
912 file_inode(req->file));
913 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700914
915 if (link)
916 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600917}
918
Jens Axboe5262f562019-09-17 12:26:57 -0600919static void io_kill_timeout(struct io_kiocb *req)
920{
921 int ret;
922
Jens Axboe2d283902019-12-04 11:08:05 -0700923 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600924 if (ret != -1) {
925 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600926 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700927 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800928 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600929 }
930}
931
932static void io_kill_timeouts(struct io_ring_ctx *ctx)
933{
934 struct io_kiocb *req, *tmp;
935
936 spin_lock_irq(&ctx->completion_lock);
937 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
938 io_kill_timeout(req);
939 spin_unlock_irq(&ctx->completion_lock);
940}
941
Jens Axboede0617e2019-04-06 21:51:27 -0600942static void io_commit_cqring(struct io_ring_ctx *ctx)
943{
944 struct io_kiocb *req;
945
Jens Axboe5262f562019-09-17 12:26:57 -0600946 while ((req = io_get_timeout_req(ctx)) != NULL)
947 io_kill_timeout(req);
948
Jens Axboede0617e2019-04-06 21:51:27 -0600949 __io_commit_cqring(ctx);
950
Pavel Begunkov87987892020-01-18 01:22:30 +0300951 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -0700952 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600953}
954
Jens Axboe2b188cc2019-01-07 10:46:33 -0700955static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
956{
Hristo Venev75b28af2019-08-26 17:23:46 +0000957 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700958 unsigned tail;
959
960 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200961 /*
962 * writes to the cq entry need to come after reading head; the
963 * control dependency is enough as we're using WRITE_ONCE to
964 * fill the cq entry
965 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000966 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700967 return NULL;
968
969 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000970 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700971}
972
Jens Axboef2842ab2020-01-08 11:04:00 -0700973static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
974{
975 if (!ctx->eventfd_async)
976 return true;
977 return io_wq_current_is_worker() || in_interrupt();
978}
979
Jens Axboe8c838782019-03-12 15:48:16 -0600980static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
981{
982 if (waitqueue_active(&ctx->wait))
983 wake_up(&ctx->wait);
984 if (waitqueue_active(&ctx->sqo_wait))
985 wake_up(&ctx->sqo_wait);
Jens Axboef2842ab2020-01-08 11:04:00 -0700986 if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -0600987 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600988}
989
Jens Axboec4a2ed72019-11-21 21:01:26 -0700990/* Returns true if there are no backlogged entries after the flush */
991static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700992{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700993 struct io_rings *rings = ctx->rings;
994 struct io_uring_cqe *cqe;
995 struct io_kiocb *req;
996 unsigned long flags;
997 LIST_HEAD(list);
998
999 if (!force) {
1000 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001001 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001002 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1003 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001004 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001005 }
1006
1007 spin_lock_irqsave(&ctx->completion_lock, flags);
1008
1009 /* if force is set, the ring is going away. always drop after that */
1010 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001011 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001012
Jens Axboec4a2ed72019-11-21 21:01:26 -07001013 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001014 while (!list_empty(&ctx->cq_overflow_list)) {
1015 cqe = io_get_cqring(ctx);
1016 if (!cqe && !force)
1017 break;
1018
1019 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1020 list);
1021 list_move(&req->list, &list);
1022 if (cqe) {
1023 WRITE_ONCE(cqe->user_data, req->user_data);
1024 WRITE_ONCE(cqe->res, req->result);
1025 WRITE_ONCE(cqe->flags, 0);
1026 } else {
1027 WRITE_ONCE(ctx->rings->cq_overflow,
1028 atomic_inc_return(&ctx->cached_cq_overflow));
1029 }
1030 }
1031
1032 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001033 if (cqe) {
1034 clear_bit(0, &ctx->sq_check_overflow);
1035 clear_bit(0, &ctx->cq_check_overflow);
1036 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001037 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1038 io_cqring_ev_posted(ctx);
1039
1040 while (!list_empty(&list)) {
1041 req = list_first_entry(&list, struct io_kiocb, list);
1042 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001043 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001044 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001045
1046 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001047}
1048
Jens Axboe78e19bb2019-11-06 15:21:34 -07001049static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001050{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001051 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001052 struct io_uring_cqe *cqe;
1053
Jens Axboe78e19bb2019-11-06 15:21:34 -07001054 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001055
Jens Axboe2b188cc2019-01-07 10:46:33 -07001056 /*
1057 * If we can't get a cq entry, userspace overflowed the
1058 * submission (by quite a lot). Increment the overflow count in
1059 * the ring.
1060 */
1061 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001062 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001063 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001064 WRITE_ONCE(cqe->res, res);
1065 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001066 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001067 WRITE_ONCE(ctx->rings->cq_overflow,
1068 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001069 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001070 if (list_empty(&ctx->cq_overflow_list)) {
1071 set_bit(0, &ctx->sq_check_overflow);
1072 set_bit(0, &ctx->cq_check_overflow);
1073 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001074 refcount_inc(&req->refs);
1075 req->result = res;
1076 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001077 }
1078}
1079
Jens Axboe78e19bb2019-11-06 15:21:34 -07001080static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001081{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001082 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001083 unsigned long flags;
1084
1085 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001086 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001087 io_commit_cqring(ctx);
1088 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1089
Jens Axboe8c838782019-03-12 15:48:16 -06001090 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001091}
1092
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001093static inline bool io_is_fallback_req(struct io_kiocb *req)
1094{
1095 return req == (struct io_kiocb *)
1096 ((unsigned long) req->ctx->fallback_req & ~1UL);
1097}
1098
1099static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1100{
1101 struct io_kiocb *req;
1102
1103 req = ctx->fallback_req;
1104 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1105 return req;
1106
1107 return NULL;
1108}
1109
Jens Axboe2579f912019-01-09 09:10:43 -07001110static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1111 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001112{
Jens Axboefd6fab22019-03-14 16:30:06 -06001113 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001114 struct io_kiocb *req;
1115
Jens Axboe2579f912019-01-09 09:10:43 -07001116 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001117 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001118 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001119 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001120 } else if (!state->free_reqs) {
1121 size_t sz;
1122 int ret;
1123
1124 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001125 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1126
1127 /*
1128 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1129 * retry single alloc to be on the safe side.
1130 */
1131 if (unlikely(ret <= 0)) {
1132 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1133 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001134 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001135 ret = 1;
1136 }
Jens Axboe2579f912019-01-09 09:10:43 -07001137 state->free_reqs = ret - 1;
1138 state->cur_req = 1;
1139 req = state->reqs[0];
1140 } else {
1141 req = state->reqs[state->cur_req];
1142 state->free_reqs--;
1143 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001144 }
1145
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001146got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001147 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001148 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001149 req->ctx = ctx;
1150 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001151 /* one is dropped after submission, the other at completion */
1152 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001153 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001154 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001155 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001156fallback:
1157 req = io_get_fallback_req(ctx);
1158 if (req)
1159 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001160 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001161 return NULL;
1162}
1163
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001164static void __io_req_do_free(struct io_kiocb *req)
1165{
1166 if (likely(!io_is_fallback_req(req)))
1167 kmem_cache_free(req_cachep, req);
1168 else
1169 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1170}
1171
Jens Axboec6ca97b302019-12-28 12:11:08 -07001172static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001173{
Jens Axboefcb323c2019-10-24 12:39:47 -06001174 struct io_ring_ctx *ctx = req->ctx;
1175
YueHaibing96fd84d2020-01-07 22:22:44 +08001176 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001177 if (req->file) {
1178 if (req->flags & REQ_F_FIXED_FILE)
1179 percpu_ref_put(&ctx->file_data->refs);
1180 else
1181 fput(req->file);
1182 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001183}
1184
1185static void __io_free_req(struct io_kiocb *req)
1186{
1187 __io_req_aux_free(req);
1188
Jens Axboefcb323c2019-10-24 12:39:47 -06001189 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001190 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001191 unsigned long flags;
1192
1193 spin_lock_irqsave(&ctx->inflight_lock, flags);
1194 list_del(&req->inflight_entry);
1195 if (waitqueue_active(&ctx->inflight_wait))
1196 wake_up(&ctx->inflight_wait);
1197 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1198 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001199
1200 percpu_ref_put(&req->ctx->refs);
1201 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001202}
1203
Jens Axboec6ca97b302019-12-28 12:11:08 -07001204struct req_batch {
1205 void *reqs[IO_IOPOLL_BATCH];
1206 int to_free;
1207 int need_iter;
1208};
1209
1210static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1211{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001212 int fixed_refs = rb->to_free;
1213
Jens Axboec6ca97b302019-12-28 12:11:08 -07001214 if (!rb->to_free)
1215 return;
1216 if (rb->need_iter) {
1217 int i, inflight = 0;
1218 unsigned long flags;
1219
Jens Axboe10fef4b2020-01-09 07:52:28 -07001220 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001221 for (i = 0; i < rb->to_free; i++) {
1222 struct io_kiocb *req = rb->reqs[i];
1223
Jens Axboe10fef4b2020-01-09 07:52:28 -07001224 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001225 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001226 fixed_refs++;
1227 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001228 if (req->flags & REQ_F_INFLIGHT)
1229 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001230 __io_req_aux_free(req);
1231 }
1232 if (!inflight)
1233 goto do_free;
1234
1235 spin_lock_irqsave(&ctx->inflight_lock, flags);
1236 for (i = 0; i < rb->to_free; i++) {
1237 struct io_kiocb *req = rb->reqs[i];
1238
Jens Axboe10fef4b2020-01-09 07:52:28 -07001239 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001240 list_del(&req->inflight_entry);
1241 if (!--inflight)
1242 break;
1243 }
1244 }
1245 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1246
1247 if (waitqueue_active(&ctx->inflight_wait))
1248 wake_up(&ctx->inflight_wait);
1249 }
1250do_free:
1251 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001252 if (fixed_refs)
1253 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001254 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001255 rb->to_free = rb->need_iter = 0;
1256}
1257
Jackie Liua197f662019-11-08 08:09:12 -07001258static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001259{
Jackie Liua197f662019-11-08 08:09:12 -07001260 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001261 int ret;
1262
Jens Axboe2d283902019-12-04 11:08:05 -07001263 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001264 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001265 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001266 io_commit_cqring(ctx);
1267 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001268 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001269 return true;
1270 }
1271
1272 return false;
1273}
1274
Jens Axboeba816ad2019-09-28 11:36:45 -06001275static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001276{
Jens Axboe2665abf2019-11-05 12:40:47 -07001277 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001278 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001279
Jens Axboe4d7dd462019-11-20 13:03:52 -07001280 /* Already got next link */
1281 if (req->flags & REQ_F_LINK_NEXT)
1282 return;
1283
Jens Axboe9e645e112019-05-10 16:07:28 -06001284 /*
1285 * The list should never be empty when we are called here. But could
1286 * potentially happen if the chain is messed up, check to be on the
1287 * safe side.
1288 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001289 while (!list_empty(&req->link_list)) {
1290 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1291 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001292
Pavel Begunkov44932332019-12-05 16:16:35 +03001293 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1294 (nxt->flags & REQ_F_TIMEOUT))) {
1295 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001296 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001297 req->flags &= ~REQ_F_LINK_TIMEOUT;
1298 continue;
1299 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001300
Pavel Begunkov44932332019-12-05 16:16:35 +03001301 list_del_init(&req->link_list);
1302 if (!list_empty(&nxt->link_list))
1303 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001304 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001305 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001306 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001307
Jens Axboe4d7dd462019-11-20 13:03:52 -07001308 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001309 if (wake_ev)
1310 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001311}
1312
1313/*
1314 * Called if REQ_F_LINK is set, and we fail the head request
1315 */
1316static void io_fail_links(struct io_kiocb *req)
1317{
Jens Axboe2665abf2019-11-05 12:40:47 -07001318 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001319 unsigned long flags;
1320
1321 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001322
1323 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001324 struct io_kiocb *link = list_first_entry(&req->link_list,
1325 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001326
Pavel Begunkov44932332019-12-05 16:16:35 +03001327 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001328 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001329
1330 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001331 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001332 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001333 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001334 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001335 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001336 }
Jens Axboe5d960722019-11-19 15:31:28 -07001337 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001338 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001339
1340 io_commit_cqring(ctx);
1341 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1342 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001343}
1344
Jens Axboe4d7dd462019-11-20 13:03:52 -07001345static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001346{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001347 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001348 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001349
Jens Axboe9e645e112019-05-10 16:07:28 -06001350 /*
1351 * If LINK is set, we have dependent requests in this chain. If we
1352 * didn't fail this request, queue the first one up, moving any other
1353 * dependencies to the next request. In case of failure, fail the rest
1354 * of the chain.
1355 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001356 if (req->flags & REQ_F_FAIL_LINK) {
1357 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001358 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1359 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001360 struct io_ring_ctx *ctx = req->ctx;
1361 unsigned long flags;
1362
1363 /*
1364 * If this is a timeout link, we could be racing with the
1365 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001366 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001367 */
1368 spin_lock_irqsave(&ctx->completion_lock, flags);
1369 io_req_link_next(req, nxt);
1370 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1371 } else {
1372 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001373 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001374}
Jens Axboe9e645e112019-05-10 16:07:28 -06001375
Jackie Liuc69f8db2019-11-09 11:00:08 +08001376static void io_free_req(struct io_kiocb *req)
1377{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001378 struct io_kiocb *nxt = NULL;
1379
1380 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001381 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001382
1383 if (nxt)
1384 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001385}
1386
Jens Axboeba816ad2019-09-28 11:36:45 -06001387/*
1388 * Drop reference to request, return next in chain (if there is one) if this
1389 * was the last reference to this request.
1390 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001391__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001392static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001393{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001394 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001395
Jens Axboee65ef562019-03-12 10:16:44 -06001396 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001397 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001398}
1399
Jens Axboe2b188cc2019-01-07 10:46:33 -07001400static void io_put_req(struct io_kiocb *req)
1401{
Jens Axboedef596e2019-01-09 08:59:42 -07001402 if (refcount_dec_and_test(&req->refs))
1403 io_free_req(req);
1404}
1405
Jens Axboe978db572019-11-14 22:39:04 -07001406/*
1407 * Must only be used if we don't need to care about links, usually from
1408 * within the completion handling itself.
1409 */
1410static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001411{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001412 /* drop both submit and complete references */
1413 if (refcount_sub_and_test(2, &req->refs))
1414 __io_free_req(req);
1415}
1416
Jens Axboe978db572019-11-14 22:39:04 -07001417static void io_double_put_req(struct io_kiocb *req)
1418{
1419 /* drop both submit and complete references */
1420 if (refcount_sub_and_test(2, &req->refs))
1421 io_free_req(req);
1422}
1423
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001424static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001425{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001426 struct io_rings *rings = ctx->rings;
1427
Jens Axboead3eb2c2019-12-18 17:12:20 -07001428 if (test_bit(0, &ctx->cq_check_overflow)) {
1429 /*
1430 * noflush == true is from the waitqueue handler, just ensure
1431 * we wake up the task, and the next invocation will flush the
1432 * entries. We cannot safely to it from here.
1433 */
1434 if (noflush && !list_empty(&ctx->cq_overflow_list))
1435 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001436
Jens Axboead3eb2c2019-12-18 17:12:20 -07001437 io_cqring_overflow_flush(ctx, false);
1438 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001439
Jens Axboea3a0e432019-08-20 11:03:11 -06001440 /* See comment at the top of this file */
1441 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001442 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001443}
1444
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001445static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1446{
1447 struct io_rings *rings = ctx->rings;
1448
1449 /* make sure SQ entry isn't read before tail */
1450 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1451}
1452
Jens Axboe8237e042019-12-28 10:48:22 -07001453static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001454{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001455 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1456 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001457
Jens Axboec6ca97b302019-12-28 12:11:08 -07001458 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1459 rb->need_iter++;
1460
1461 rb->reqs[rb->to_free++] = req;
1462 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1463 io_free_req_many(req->ctx, rb);
1464 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001465}
1466
Jens Axboedef596e2019-01-09 08:59:42 -07001467/*
1468 * Find and free completed poll iocbs
1469 */
1470static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1471 struct list_head *done)
1472{
Jens Axboe8237e042019-12-28 10:48:22 -07001473 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001474 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001475
Jens Axboec6ca97b302019-12-28 12:11:08 -07001476 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001477 while (!list_empty(done)) {
1478 req = list_first_entry(done, struct io_kiocb, list);
1479 list_del(&req->list);
1480
Jens Axboe78e19bb2019-11-06 15:21:34 -07001481 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001482 (*nr_events)++;
1483
Jens Axboe8237e042019-12-28 10:48:22 -07001484 if (refcount_dec_and_test(&req->refs) &&
1485 !io_req_multi_free(&rb, req))
1486 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001487 }
Jens Axboedef596e2019-01-09 08:59:42 -07001488
Jens Axboe09bb8392019-03-13 12:39:28 -06001489 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001490 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001491}
1492
1493static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1494 long min)
1495{
1496 struct io_kiocb *req, *tmp;
1497 LIST_HEAD(done);
1498 bool spin;
1499 int ret;
1500
1501 /*
1502 * Only spin for completions if we don't have multiple devices hanging
1503 * off our complete list, and we're under the requested amount.
1504 */
1505 spin = !ctx->poll_multi_file && *nr_events < min;
1506
1507 ret = 0;
1508 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001509 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001510
1511 /*
1512 * Move completed entries to our local list. If we find a
1513 * request that requires polling, break out and complete
1514 * the done list first, if we have entries there.
1515 */
1516 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1517 list_move_tail(&req->list, &done);
1518 continue;
1519 }
1520 if (!list_empty(&done))
1521 break;
1522
1523 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1524 if (ret < 0)
1525 break;
1526
1527 if (ret && spin)
1528 spin = false;
1529 ret = 0;
1530 }
1531
1532 if (!list_empty(&done))
1533 io_iopoll_complete(ctx, nr_events, &done);
1534
1535 return ret;
1536}
1537
1538/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001539 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001540 * non-spinning poll check - we'll still enter the driver poll loop, but only
1541 * as a non-spinning completion check.
1542 */
1543static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1544 long min)
1545{
Jens Axboe08f54392019-08-21 22:19:11 -06001546 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001547 int ret;
1548
1549 ret = io_do_iopoll(ctx, nr_events, min);
1550 if (ret < 0)
1551 return ret;
1552 if (!min || *nr_events >= min)
1553 return 0;
1554 }
1555
1556 return 1;
1557}
1558
1559/*
1560 * We can't just wait for polled events to come to us, we have to actively
1561 * find and complete them.
1562 */
1563static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1564{
1565 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1566 return;
1567
1568 mutex_lock(&ctx->uring_lock);
1569 while (!list_empty(&ctx->poll_list)) {
1570 unsigned int nr_events = 0;
1571
1572 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001573
1574 /*
1575 * Ensure we allow local-to-the-cpu processing to take place,
1576 * in this case we need to ensure that we reap all events.
1577 */
1578 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001579 }
1580 mutex_unlock(&ctx->uring_lock);
1581}
1582
Jens Axboe2b2ed972019-10-25 10:06:15 -06001583static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1584 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001585{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001586 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001587
1588 do {
1589 int tmin = 0;
1590
Jens Axboe500f9fb2019-08-19 12:15:59 -06001591 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001592 * Don't enter poll loop if we already have events pending.
1593 * If we do, we can potentially be spinning for commands that
1594 * already triggered a CQE (eg in error).
1595 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001596 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001597 break;
1598
1599 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001600 * If a submit got punted to a workqueue, we can have the
1601 * application entering polling for a command before it gets
1602 * issued. That app will hold the uring_lock for the duration
1603 * of the poll right here, so we need to take a breather every
1604 * now and then to ensure that the issue has a chance to add
1605 * the poll to the issued list. Otherwise we can spin here
1606 * forever, while the workqueue is stuck trying to acquire the
1607 * very same mutex.
1608 */
1609 if (!(++iters & 7)) {
1610 mutex_unlock(&ctx->uring_lock);
1611 mutex_lock(&ctx->uring_lock);
1612 }
1613
Jens Axboedef596e2019-01-09 08:59:42 -07001614 if (*nr_events < min)
1615 tmin = min - *nr_events;
1616
1617 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1618 if (ret <= 0)
1619 break;
1620 ret = 0;
1621 } while (min && !*nr_events && !need_resched());
1622
Jens Axboe2b2ed972019-10-25 10:06:15 -06001623 return ret;
1624}
1625
1626static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1627 long min)
1628{
1629 int ret;
1630
1631 /*
1632 * We disallow the app entering submit/complete with polling, but we
1633 * still need to lock the ring to prevent racing with polled issue
1634 * that got punted to a workqueue.
1635 */
1636 mutex_lock(&ctx->uring_lock);
1637 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001638 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001639 return ret;
1640}
1641
Jens Axboe491381ce2019-10-17 09:20:46 -06001642static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001643{
Jens Axboe491381ce2019-10-17 09:20:46 -06001644 /*
1645 * Tell lockdep we inherited freeze protection from submission
1646 * thread.
1647 */
1648 if (req->flags & REQ_F_ISREG) {
1649 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001650
Jens Axboe491381ce2019-10-17 09:20:46 -06001651 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001652 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001653 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001654}
1655
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001656static inline void req_set_fail_links(struct io_kiocb *req)
1657{
1658 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1659 req->flags |= REQ_F_FAIL_LINK;
1660}
1661
Jens Axboeba816ad2019-09-28 11:36:45 -06001662static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001663{
Jens Axboe9adbd452019-12-20 08:45:55 -07001664 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001665
Jens Axboe491381ce2019-10-17 09:20:46 -06001666 if (kiocb->ki_flags & IOCB_WRITE)
1667 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001668
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001669 if (res != req->result)
1670 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001671 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001672}
1673
1674static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1675{
Jens Axboe9adbd452019-12-20 08:45:55 -07001676 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001677
1678 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001679 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001680}
1681
Jens Axboeba816ad2019-09-28 11:36:45 -06001682static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1683{
Jens Axboe9adbd452019-12-20 08:45:55 -07001684 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001685 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001686
1687 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001688 io_put_req_find_next(req, &nxt);
1689
1690 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001691}
1692
Jens Axboedef596e2019-01-09 08:59:42 -07001693static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1694{
Jens Axboe9adbd452019-12-20 08:45:55 -07001695 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001696
Jens Axboe491381ce2019-10-17 09:20:46 -06001697 if (kiocb->ki_flags & IOCB_WRITE)
1698 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001699
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001700 if (res != req->result)
1701 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001702 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001703 if (res != -EAGAIN)
1704 req->flags |= REQ_F_IOPOLL_COMPLETED;
1705}
1706
1707/*
1708 * After the iocb has been issued, it's safe to be found on the poll list.
1709 * Adding the kiocb to the list AFTER submission ensures that we don't
1710 * find it from a io_iopoll_getevents() thread before the issuer is done
1711 * accessing the kiocb cookie.
1712 */
1713static void io_iopoll_req_issued(struct io_kiocb *req)
1714{
1715 struct io_ring_ctx *ctx = req->ctx;
1716
1717 /*
1718 * Track whether we have multiple files in our lists. This will impact
1719 * how we do polling eventually, not spinning if we're on potentially
1720 * different devices.
1721 */
1722 if (list_empty(&ctx->poll_list)) {
1723 ctx->poll_multi_file = false;
1724 } else if (!ctx->poll_multi_file) {
1725 struct io_kiocb *list_req;
1726
1727 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1728 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001729 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001730 ctx->poll_multi_file = true;
1731 }
1732
1733 /*
1734 * For fast devices, IO may have already completed. If it has, add
1735 * it to the front so we find it first.
1736 */
1737 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1738 list_add(&req->list, &ctx->poll_list);
1739 else
1740 list_add_tail(&req->list, &ctx->poll_list);
1741}
1742
Jens Axboe3d6770f2019-04-13 11:50:54 -06001743static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001744{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001745 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001746 int diff = state->has_refs - state->used_refs;
1747
1748 if (diff)
1749 fput_many(state->file, diff);
1750 state->file = NULL;
1751 }
1752}
1753
1754/*
1755 * Get as many references to a file as we have IOs left in this submission,
1756 * assuming most submissions are for one file, or at least that each file
1757 * has more than one submission.
1758 */
1759static struct file *io_file_get(struct io_submit_state *state, int fd)
1760{
1761 if (!state)
1762 return fget(fd);
1763
1764 if (state->file) {
1765 if (state->fd == fd) {
1766 state->used_refs++;
1767 state->ios_left--;
1768 return state->file;
1769 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001770 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001771 }
1772 state->file = fget_many(fd, state->ios_left);
1773 if (!state->file)
1774 return NULL;
1775
1776 state->fd = fd;
1777 state->has_refs = state->ios_left;
1778 state->used_refs = 1;
1779 state->ios_left--;
1780 return state->file;
1781}
1782
Jens Axboe2b188cc2019-01-07 10:46:33 -07001783/*
1784 * If we tracked the file through the SCM inflight mechanism, we could support
1785 * any file. For now, just ensure that anything potentially problematic is done
1786 * inline.
1787 */
1788static bool io_file_supports_async(struct file *file)
1789{
1790 umode_t mode = file_inode(file)->i_mode;
1791
Jens Axboe10d59342019-12-09 20:16:22 -07001792 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001793 return true;
1794 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1795 return true;
1796
1797 return false;
1798}
1799
Jens Axboe3529d8c2019-12-19 18:24:38 -07001800static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1801 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001802{
Jens Axboedef596e2019-01-09 08:59:42 -07001803 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001804 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001805 unsigned ioprio;
1806 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001807
Jens Axboe09bb8392019-03-13 12:39:28 -06001808 if (!req->file)
1809 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001810
Jens Axboe491381ce2019-10-17 09:20:46 -06001811 if (S_ISREG(file_inode(req->file)->i_mode))
1812 req->flags |= REQ_F_ISREG;
1813
Jens Axboe2b188cc2019-01-07 10:46:33 -07001814 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001815 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1816 req->flags |= REQ_F_CUR_POS;
1817 kiocb->ki_pos = req->file->f_pos;
1818 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001819 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1820 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1821
1822 ioprio = READ_ONCE(sqe->ioprio);
1823 if (ioprio) {
1824 ret = ioprio_check_cap(ioprio);
1825 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001826 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001827
1828 kiocb->ki_ioprio = ioprio;
1829 } else
1830 kiocb->ki_ioprio = get_current_ioprio();
1831
1832 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1833 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001834 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001835
1836 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001837 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1838 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001839 req->flags |= REQ_F_NOWAIT;
1840
1841 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001842 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001843
Jens Axboedef596e2019-01-09 08:59:42 -07001844 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001845 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1846 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001847 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001848
Jens Axboedef596e2019-01-09 08:59:42 -07001849 kiocb->ki_flags |= IOCB_HIPRI;
1850 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001851 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001852 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001853 if (kiocb->ki_flags & IOCB_HIPRI)
1854 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001855 kiocb->ki_complete = io_complete_rw;
1856 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001857
Jens Axboe3529d8c2019-12-19 18:24:38 -07001858 req->rw.addr = READ_ONCE(sqe->addr);
1859 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001860 /* we own ->private, reuse it for the buffer index */
1861 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001862 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001863 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001864}
1865
1866static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1867{
1868 switch (ret) {
1869 case -EIOCBQUEUED:
1870 break;
1871 case -ERESTARTSYS:
1872 case -ERESTARTNOINTR:
1873 case -ERESTARTNOHAND:
1874 case -ERESTART_RESTARTBLOCK:
1875 /*
1876 * We can't just restart the syscall, since previously
1877 * submitted sqes may already be in progress. Just fail this
1878 * IO with EINTR.
1879 */
1880 ret = -EINTR;
1881 /* fall through */
1882 default:
1883 kiocb->ki_complete(kiocb, ret, 0);
1884 }
1885}
1886
Jens Axboeba816ad2019-09-28 11:36:45 -06001887static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1888 bool in_async)
1889{
Jens Axboeba042912019-12-25 16:33:42 -07001890 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1891
1892 if (req->flags & REQ_F_CUR_POS)
1893 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001894 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001895 *nxt = __io_complete_rw(kiocb, ret);
1896 else
1897 io_rw_done(kiocb, ret);
1898}
1899
Jens Axboe9adbd452019-12-20 08:45:55 -07001900static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001901 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001902{
Jens Axboe9adbd452019-12-20 08:45:55 -07001903 struct io_ring_ctx *ctx = req->ctx;
1904 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001905 struct io_mapped_ubuf *imu;
1906 unsigned index, buf_index;
1907 size_t offset;
1908 u64 buf_addr;
1909
1910 /* attempt to use fixed buffers without having provided iovecs */
1911 if (unlikely(!ctx->user_bufs))
1912 return -EFAULT;
1913
Jens Axboe9adbd452019-12-20 08:45:55 -07001914 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001915 if (unlikely(buf_index >= ctx->nr_user_bufs))
1916 return -EFAULT;
1917
1918 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1919 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001920 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001921
1922 /* overflow */
1923 if (buf_addr + len < buf_addr)
1924 return -EFAULT;
1925 /* not inside the mapped region */
1926 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1927 return -EFAULT;
1928
1929 /*
1930 * May not be a start of buffer, set size appropriately
1931 * and advance us to the beginning.
1932 */
1933 offset = buf_addr - imu->ubuf;
1934 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001935
1936 if (offset) {
1937 /*
1938 * Don't use iov_iter_advance() here, as it's really slow for
1939 * using the latter parts of a big fixed buffer - it iterates
1940 * over each segment manually. We can cheat a bit here, because
1941 * we know that:
1942 *
1943 * 1) it's a BVEC iter, we set it up
1944 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1945 * first and last bvec
1946 *
1947 * So just find our index, and adjust the iterator afterwards.
1948 * If the offset is within the first bvec (or the whole first
1949 * bvec, just use iov_iter_advance(). This makes it easier
1950 * since we can just skip the first segment, which may not
1951 * be PAGE_SIZE aligned.
1952 */
1953 const struct bio_vec *bvec = imu->bvec;
1954
1955 if (offset <= bvec->bv_len) {
1956 iov_iter_advance(iter, offset);
1957 } else {
1958 unsigned long seg_skip;
1959
1960 /* skip first vec */
1961 offset -= bvec->bv_len;
1962 seg_skip = 1 + (offset >> PAGE_SHIFT);
1963
1964 iter->bvec = bvec + seg_skip;
1965 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001966 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001967 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001968 }
1969 }
1970
Jens Axboe5e559562019-11-13 16:12:46 -07001971 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001972}
1973
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001974static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1975 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001976{
Jens Axboe9adbd452019-12-20 08:45:55 -07001977 void __user *buf = u64_to_user_ptr(req->rw.addr);
1978 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001979 u8 opcode;
1980
Jens Axboed625c6e2019-12-17 19:53:05 -07001981 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001982 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001983 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001984 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001985 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001986
Jens Axboe9adbd452019-12-20 08:45:55 -07001987 /* buffer index only valid with fixed read/write */
1988 if (req->rw.kiocb.private)
1989 return -EINVAL;
1990
Jens Axboe3a6820f2019-12-22 15:19:35 -07001991 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1992 ssize_t ret;
1993 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1994 *iovec = NULL;
1995 return ret;
1996 }
1997
Jens Axboef67676d2019-12-02 11:03:47 -07001998 if (req->io) {
1999 struct io_async_rw *iorw = &req->io->rw;
2000
2001 *iovec = iorw->iov;
2002 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2003 if (iorw->iov == iorw->fast_iov)
2004 *iovec = NULL;
2005 return iorw->size;
2006 }
2007
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002008 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002009 return -EFAULT;
2010
2011#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002012 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002013 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2014 iovec, iter);
2015#endif
2016
2017 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2018}
2019
Jens Axboe32960612019-09-23 11:05:34 -06002020/*
2021 * For files that don't have ->read_iter() and ->write_iter(), handle them
2022 * by looping over ->read() or ->write() manually.
2023 */
2024static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2025 struct iov_iter *iter)
2026{
2027 ssize_t ret = 0;
2028
2029 /*
2030 * Don't support polled IO through this interface, and we can't
2031 * support non-blocking either. For the latter, this just causes
2032 * the kiocb to be handled from an async context.
2033 */
2034 if (kiocb->ki_flags & IOCB_HIPRI)
2035 return -EOPNOTSUPP;
2036 if (kiocb->ki_flags & IOCB_NOWAIT)
2037 return -EAGAIN;
2038
2039 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002040 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002041 ssize_t nr;
2042
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002043 if (!iov_iter_is_bvec(iter)) {
2044 iovec = iov_iter_iovec(iter);
2045 } else {
2046 /* fixed buffers import bvec */
2047 iovec.iov_base = kmap(iter->bvec->bv_page)
2048 + iter->iov_offset;
2049 iovec.iov_len = min(iter->count,
2050 iter->bvec->bv_len - iter->iov_offset);
2051 }
2052
Jens Axboe32960612019-09-23 11:05:34 -06002053 if (rw == READ) {
2054 nr = file->f_op->read(file, iovec.iov_base,
2055 iovec.iov_len, &kiocb->ki_pos);
2056 } else {
2057 nr = file->f_op->write(file, iovec.iov_base,
2058 iovec.iov_len, &kiocb->ki_pos);
2059 }
2060
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002061 if (iov_iter_is_bvec(iter))
2062 kunmap(iter->bvec->bv_page);
2063
Jens Axboe32960612019-09-23 11:05:34 -06002064 if (nr < 0) {
2065 if (!ret)
2066 ret = nr;
2067 break;
2068 }
2069 ret += nr;
2070 if (nr != iovec.iov_len)
2071 break;
2072 iov_iter_advance(iter, nr);
2073 }
2074
2075 return ret;
2076}
2077
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002078static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002079 struct iovec *iovec, struct iovec *fast_iov,
2080 struct iov_iter *iter)
2081{
2082 req->io->rw.nr_segs = iter->nr_segs;
2083 req->io->rw.size = io_size;
2084 req->io->rw.iov = iovec;
2085 if (!req->io->rw.iov) {
2086 req->io->rw.iov = req->io->rw.fast_iov;
2087 memcpy(req->io->rw.iov, fast_iov,
2088 sizeof(struct iovec) * iter->nr_segs);
2089 }
2090}
2091
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002092static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002093{
Jens Axboed3656342019-12-18 09:50:26 -07002094 if (!io_op_defs[req->opcode].async_ctx)
2095 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002096 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002097 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002098}
2099
2100static void io_rw_async(struct io_wq_work **workptr)
2101{
2102 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2103 struct iovec *iov = NULL;
2104
2105 if (req->io->rw.iov != req->io->rw.fast_iov)
2106 iov = req->io->rw.iov;
2107 io_wq_submit_work(workptr);
2108 kfree(iov);
2109}
2110
2111static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2112 struct iovec *iovec, struct iovec *fast_iov,
2113 struct iov_iter *iter)
2114{
Jens Axboe980ad262020-01-24 23:08:54 -07002115 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002116 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002117 if (!req->io && io_alloc_async_ctx(req))
2118 return -ENOMEM;
2119
2120 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2121 req->work.func = io_rw_async;
2122 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002123}
2124
Jens Axboe3529d8c2019-12-19 18:24:38 -07002125static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2126 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002127{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002128 struct io_async_ctx *io;
2129 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002130 ssize_t ret;
2131
Jens Axboe3529d8c2019-12-19 18:24:38 -07002132 ret = io_prep_rw(req, sqe, force_nonblock);
2133 if (ret)
2134 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002135
Jens Axboe3529d8c2019-12-19 18:24:38 -07002136 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2137 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002138
Jens Axboe3529d8c2019-12-19 18:24:38 -07002139 if (!req->io)
2140 return 0;
2141
2142 io = req->io;
2143 io->rw.iov = io->rw.fast_iov;
2144 req->io = NULL;
2145 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2146 req->io = io;
2147 if (ret < 0)
2148 return ret;
2149
2150 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2151 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002152}
2153
Pavel Begunkov267bc902019-11-07 01:41:08 +03002154static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002155 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002156{
2157 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002158 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002159 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002160 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002161 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002162
Jens Axboe3529d8c2019-12-19 18:24:38 -07002163 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002164 if (ret < 0)
2165 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002166
Jens Axboefd6c2e42019-12-18 12:19:41 -07002167 /* Ensure we clear previously set non-block flag */
2168 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002169 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002170
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002171 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002172 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002173 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002174 req->result = io_size;
2175
2176 /*
2177 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2178 * we know to async punt it even if it was opened O_NONBLOCK
2179 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002180 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002181 req->flags |= REQ_F_MUST_PUNT;
2182 goto copy_iov;
2183 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002184
Jens Axboe31b51512019-01-18 22:56:34 -07002185 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002186 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002187 if (!ret) {
2188 ssize_t ret2;
2189
Jens Axboe9adbd452019-12-20 08:45:55 -07002190 if (req->file->f_op->read_iter)
2191 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002192 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002193 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002194
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002195 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002196 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002197 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002198 } else {
2199copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002200 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002201 inline_vecs, &iter);
2202 if (ret)
2203 goto out_free;
2204 return -EAGAIN;
2205 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002206 }
Jens Axboef67676d2019-12-02 11:03:47 -07002207out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002208 if (!io_wq_current_is_worker())
2209 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002210 return ret;
2211}
2212
Jens Axboe3529d8c2019-12-19 18:24:38 -07002213static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2214 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002215{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002216 struct io_async_ctx *io;
2217 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002218 ssize_t ret;
2219
Jens Axboe3529d8c2019-12-19 18:24:38 -07002220 ret = io_prep_rw(req, sqe, force_nonblock);
2221 if (ret)
2222 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002223
Jens Axboe3529d8c2019-12-19 18:24:38 -07002224 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2225 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002226
Jens Axboe3529d8c2019-12-19 18:24:38 -07002227 if (!req->io)
2228 return 0;
2229
2230 io = req->io;
2231 io->rw.iov = io->rw.fast_iov;
2232 req->io = NULL;
2233 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2234 req->io = io;
2235 if (ret < 0)
2236 return ret;
2237
2238 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2239 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002240}
2241
Pavel Begunkov267bc902019-11-07 01:41:08 +03002242static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002243 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002244{
2245 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002246 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002247 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002248 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002249 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002250
Jens Axboe3529d8c2019-12-19 18:24:38 -07002251 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002252 if (ret < 0)
2253 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002254
Jens Axboefd6c2e42019-12-18 12:19:41 -07002255 /* Ensure we clear previously set non-block flag */
2256 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002257 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002258
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002259 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002260 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002261 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002262 req->result = io_size;
2263
2264 /*
2265 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2266 * we know to async punt it even if it was opened O_NONBLOCK
2267 */
2268 if (force_nonblock && !io_file_supports_async(req->file)) {
2269 req->flags |= REQ_F_MUST_PUNT;
2270 goto copy_iov;
2271 }
2272
Jens Axboe10d59342019-12-09 20:16:22 -07002273 /* file path doesn't support NOWAIT for non-direct_IO */
2274 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2275 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002276 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002277
Jens Axboe31b51512019-01-18 22:56:34 -07002278 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002279 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002280 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002281 ssize_t ret2;
2282
Jens Axboe2b188cc2019-01-07 10:46:33 -07002283 /*
2284 * Open-code file_start_write here to grab freeze protection,
2285 * which will be released by another thread in
2286 * io_complete_rw(). Fool lockdep by telling it the lock got
2287 * released so that it doesn't complain about the held lock when
2288 * we return to userspace.
2289 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002290 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002291 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002292 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002293 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002294 SB_FREEZE_WRITE);
2295 }
2296 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002297
Jens Axboe9adbd452019-12-20 08:45:55 -07002298 if (req->file->f_op->write_iter)
2299 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002300 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002301 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002302 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002303 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002304 } else {
2305copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002306 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002307 inline_vecs, &iter);
2308 if (ret)
2309 goto out_free;
2310 return -EAGAIN;
2311 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002312 }
Jens Axboe31b51512019-01-18 22:56:34 -07002313out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002314 if (!io_wq_current_is_worker())
2315 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002316 return ret;
2317}
2318
2319/*
2320 * IORING_OP_NOP just posts a completion event, nothing else.
2321 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002322static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002323{
2324 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002325
Jens Axboedef596e2019-01-09 08:59:42 -07002326 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2327 return -EINVAL;
2328
Jens Axboe78e19bb2019-11-06 15:21:34 -07002329 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002330 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002331 return 0;
2332}
2333
Jens Axboe3529d8c2019-12-19 18:24:38 -07002334static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002335{
Jens Axboe6b063142019-01-10 22:13:58 -07002336 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002337
Jens Axboe09bb8392019-03-13 12:39:28 -06002338 if (!req->file)
2339 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002340
Jens Axboe6b063142019-01-10 22:13:58 -07002341 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002342 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002343 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002344 return -EINVAL;
2345
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002346 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2347 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2348 return -EINVAL;
2349
2350 req->sync.off = READ_ONCE(sqe->off);
2351 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002352 return 0;
2353}
2354
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002355static bool io_req_cancelled(struct io_kiocb *req)
2356{
2357 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2358 req_set_fail_links(req);
2359 io_cqring_add_event(req, -ECANCELED);
2360 io_put_req(req);
2361 return true;
2362 }
2363
2364 return false;
2365}
2366
Jens Axboe78912932020-01-14 22:09:06 -07002367static void io_link_work_cb(struct io_wq_work **workptr)
2368{
2369 struct io_wq_work *work = *workptr;
2370 struct io_kiocb *link = work->data;
2371
2372 io_queue_linked_timeout(link);
2373 work->func = io_wq_submit_work;
2374}
2375
2376static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2377{
2378 struct io_kiocb *link;
2379
2380 io_prep_async_work(nxt, &link);
2381 *workptr = &nxt->work;
2382 if (link) {
2383 nxt->work.flags |= IO_WQ_WORK_CB;
2384 nxt->work.func = io_link_work_cb;
2385 nxt->work.data = link;
2386 }
2387}
2388
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002389static void io_fsync_finish(struct io_wq_work **workptr)
2390{
2391 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2392 loff_t end = req->sync.off + req->sync.len;
2393 struct io_kiocb *nxt = NULL;
2394 int ret;
2395
2396 if (io_req_cancelled(req))
2397 return;
2398
Jens Axboe9adbd452019-12-20 08:45:55 -07002399 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002400 end > 0 ? end : LLONG_MAX,
2401 req->sync.flags & IORING_FSYNC_DATASYNC);
2402 if (ret < 0)
2403 req_set_fail_links(req);
2404 io_cqring_add_event(req, ret);
2405 io_put_req_find_next(req, &nxt);
2406 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002407 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002408}
2409
Jens Axboefc4df992019-12-10 14:38:45 -07002410static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2411 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002412{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002413 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002414
2415 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002416 if (force_nonblock) {
2417 io_put_req(req);
2418 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002419 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002420 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002421
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002422 work = old_work = &req->work;
2423 io_fsync_finish(&work);
2424 if (work && work != old_work)
2425 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002426 return 0;
2427}
2428
Jens Axboed63d1b52019-12-10 10:38:56 -07002429static void io_fallocate_finish(struct io_wq_work **workptr)
2430{
2431 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2432 struct io_kiocb *nxt = NULL;
2433 int ret;
2434
2435 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2436 req->sync.len);
2437 if (ret < 0)
2438 req_set_fail_links(req);
2439 io_cqring_add_event(req, ret);
2440 io_put_req_find_next(req, &nxt);
2441 if (nxt)
2442 io_wq_assign_next(workptr, nxt);
2443}
2444
2445static int io_fallocate_prep(struct io_kiocb *req,
2446 const struct io_uring_sqe *sqe)
2447{
2448 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2449 return -EINVAL;
2450
2451 req->sync.off = READ_ONCE(sqe->off);
2452 req->sync.len = READ_ONCE(sqe->addr);
2453 req->sync.mode = READ_ONCE(sqe->len);
2454 return 0;
2455}
2456
2457static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2458 bool force_nonblock)
2459{
2460 struct io_wq_work *work, *old_work;
2461
2462 /* fallocate always requiring blocking context */
2463 if (force_nonblock) {
2464 io_put_req(req);
2465 req->work.func = io_fallocate_finish;
2466 return -EAGAIN;
2467 }
2468
2469 work = old_work = &req->work;
2470 io_fallocate_finish(&work);
2471 if (work && work != old_work)
2472 *nxt = container_of(work, struct io_kiocb, work);
2473
2474 return 0;
2475}
2476
Jens Axboe15b71ab2019-12-11 11:20:36 -07002477static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2478{
Jens Axboef8748882020-01-08 17:47:02 -07002479 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002480 int ret;
2481
2482 if (sqe->ioprio || sqe->buf_index)
2483 return -EINVAL;
2484
2485 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002486 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002487 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002488 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002489
Jens Axboef8748882020-01-08 17:47:02 -07002490 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002491 if (IS_ERR(req->open.filename)) {
2492 ret = PTR_ERR(req->open.filename);
2493 req->open.filename = NULL;
2494 return ret;
2495 }
2496
2497 return 0;
2498}
2499
Jens Axboecebdb982020-01-08 17:59:24 -07002500static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2501{
2502 struct open_how __user *how;
2503 const char __user *fname;
2504 size_t len;
2505 int ret;
2506
2507 if (sqe->ioprio || sqe->buf_index)
2508 return -EINVAL;
2509
2510 req->open.dfd = READ_ONCE(sqe->fd);
2511 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2512 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2513 len = READ_ONCE(sqe->len);
2514
2515 if (len < OPEN_HOW_SIZE_VER0)
2516 return -EINVAL;
2517
2518 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2519 len);
2520 if (ret)
2521 return ret;
2522
2523 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2524 req->open.how.flags |= O_LARGEFILE;
2525
2526 req->open.filename = getname(fname);
2527 if (IS_ERR(req->open.filename)) {
2528 ret = PTR_ERR(req->open.filename);
2529 req->open.filename = NULL;
2530 return ret;
2531 }
2532
2533 return 0;
2534}
2535
2536static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2537 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002538{
2539 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002540 struct file *file;
2541 int ret;
2542
2543 if (force_nonblock) {
2544 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2545 return -EAGAIN;
2546 }
2547
Jens Axboecebdb982020-01-08 17:59:24 -07002548 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002549 if (ret)
2550 goto err;
2551
Jens Axboecebdb982020-01-08 17:59:24 -07002552 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002553 if (ret < 0)
2554 goto err;
2555
2556 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2557 if (IS_ERR(file)) {
2558 put_unused_fd(ret);
2559 ret = PTR_ERR(file);
2560 } else {
2561 fsnotify_open(file);
2562 fd_install(ret, file);
2563 }
2564err:
2565 putname(req->open.filename);
2566 if (ret < 0)
2567 req_set_fail_links(req);
2568 io_cqring_add_event(req, ret);
2569 io_put_req_find_next(req, nxt);
2570 return 0;
2571}
2572
Jens Axboecebdb982020-01-08 17:59:24 -07002573static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2574 bool force_nonblock)
2575{
2576 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2577 return io_openat2(req, nxt, force_nonblock);
2578}
2579
Jens Axboec1ca7572019-12-25 22:18:28 -07002580static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2581{
2582#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2583 if (sqe->ioprio || sqe->buf_index || sqe->off)
2584 return -EINVAL;
2585
2586 req->madvise.addr = READ_ONCE(sqe->addr);
2587 req->madvise.len = READ_ONCE(sqe->len);
2588 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2589 return 0;
2590#else
2591 return -EOPNOTSUPP;
2592#endif
2593}
2594
2595static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2596 bool force_nonblock)
2597{
2598#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2599 struct io_madvise *ma = &req->madvise;
2600 int ret;
2601
2602 if (force_nonblock)
2603 return -EAGAIN;
2604
2605 ret = do_madvise(ma->addr, ma->len, ma->advice);
2606 if (ret < 0)
2607 req_set_fail_links(req);
2608 io_cqring_add_event(req, ret);
2609 io_put_req_find_next(req, nxt);
2610 return 0;
2611#else
2612 return -EOPNOTSUPP;
2613#endif
2614}
2615
Jens Axboe4840e412019-12-25 22:03:45 -07002616static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2617{
2618 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2619 return -EINVAL;
2620
2621 req->fadvise.offset = READ_ONCE(sqe->off);
2622 req->fadvise.len = READ_ONCE(sqe->len);
2623 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2624 return 0;
2625}
2626
2627static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2628 bool force_nonblock)
2629{
2630 struct io_fadvise *fa = &req->fadvise;
2631 int ret;
2632
2633 /* DONTNEED may block, others _should_ not */
2634 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2635 return -EAGAIN;
2636
2637 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2638 if (ret < 0)
2639 req_set_fail_links(req);
2640 io_cqring_add_event(req, ret);
2641 io_put_req_find_next(req, nxt);
2642 return 0;
2643}
2644
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002645static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2646{
Jens Axboef8748882020-01-08 17:47:02 -07002647 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002648 unsigned lookup_flags;
2649 int ret;
2650
2651 if (sqe->ioprio || sqe->buf_index)
2652 return -EINVAL;
2653
2654 req->open.dfd = READ_ONCE(sqe->fd);
2655 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002656 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002657 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002658 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002659
Jens Axboec12cedf2020-01-08 17:41:21 -07002660 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002661 return -EINVAL;
2662
Jens Axboef8748882020-01-08 17:47:02 -07002663 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002664 if (IS_ERR(req->open.filename)) {
2665 ret = PTR_ERR(req->open.filename);
2666 req->open.filename = NULL;
2667 return ret;
2668 }
2669
2670 return 0;
2671}
2672
2673static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2674 bool force_nonblock)
2675{
2676 struct io_open *ctx = &req->open;
2677 unsigned lookup_flags;
2678 struct path path;
2679 struct kstat stat;
2680 int ret;
2681
2682 if (force_nonblock)
2683 return -EAGAIN;
2684
Jens Axboec12cedf2020-01-08 17:41:21 -07002685 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002686 return -EINVAL;
2687
2688retry:
2689 /* filename_lookup() drops it, keep a reference */
2690 ctx->filename->refcnt++;
2691
2692 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2693 NULL);
2694 if (ret)
2695 goto err;
2696
Jens Axboec12cedf2020-01-08 17:41:21 -07002697 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002698 path_put(&path);
2699 if (retry_estale(ret, lookup_flags)) {
2700 lookup_flags |= LOOKUP_REVAL;
2701 goto retry;
2702 }
2703 if (!ret)
2704 ret = cp_statx(&stat, ctx->buffer);
2705err:
2706 putname(ctx->filename);
2707 if (ret < 0)
2708 req_set_fail_links(req);
2709 io_cqring_add_event(req, ret);
2710 io_put_req_find_next(req, nxt);
2711 return 0;
2712}
2713
Jens Axboeb5dba592019-12-11 14:02:38 -07002714static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2715{
2716 /*
2717 * If we queue this for async, it must not be cancellable. That would
2718 * leave the 'file' in an undeterminate state.
2719 */
2720 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2721
2722 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2723 sqe->rw_flags || sqe->buf_index)
2724 return -EINVAL;
2725 if (sqe->flags & IOSQE_FIXED_FILE)
2726 return -EINVAL;
2727
2728 req->close.fd = READ_ONCE(sqe->fd);
2729 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002730 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002731 return -EBADF;
2732
2733 return 0;
2734}
2735
2736static void io_close_finish(struct io_wq_work **workptr)
2737{
2738 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2739 struct io_kiocb *nxt = NULL;
2740
2741 /* Invoked with files, we need to do the close */
2742 if (req->work.files) {
2743 int ret;
2744
2745 ret = filp_close(req->close.put_file, req->work.files);
2746 if (ret < 0) {
2747 req_set_fail_links(req);
2748 }
2749 io_cqring_add_event(req, ret);
2750 }
2751
2752 fput(req->close.put_file);
2753
2754 /* we bypassed the re-issue, drop the submission reference */
2755 io_put_req(req);
2756 io_put_req_find_next(req, &nxt);
2757 if (nxt)
2758 io_wq_assign_next(workptr, nxt);
2759}
2760
2761static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2762 bool force_nonblock)
2763{
2764 int ret;
2765
2766 req->close.put_file = NULL;
2767 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2768 if (ret < 0)
2769 return ret;
2770
2771 /* if the file has a flush method, be safe and punt to async */
2772 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2773 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2774 goto eagain;
2775 }
2776
2777 /*
2778 * No ->flush(), safely close from here and just punt the
2779 * fput() to async context.
2780 */
2781 ret = filp_close(req->close.put_file, current->files);
2782
2783 if (ret < 0)
2784 req_set_fail_links(req);
2785 io_cqring_add_event(req, ret);
2786
2787 if (io_wq_current_is_worker()) {
2788 struct io_wq_work *old_work, *work;
2789
2790 old_work = work = &req->work;
2791 io_close_finish(&work);
2792 if (work && work != old_work)
2793 *nxt = container_of(work, struct io_kiocb, work);
2794 return 0;
2795 }
2796
2797eagain:
2798 req->work.func = io_close_finish;
2799 return -EAGAIN;
2800}
2801
Jens Axboe3529d8c2019-12-19 18:24:38 -07002802static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002803{
2804 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002805
2806 if (!req->file)
2807 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002808
2809 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2810 return -EINVAL;
2811 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2812 return -EINVAL;
2813
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002814 req->sync.off = READ_ONCE(sqe->off);
2815 req->sync.len = READ_ONCE(sqe->len);
2816 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002817 return 0;
2818}
2819
2820static void io_sync_file_range_finish(struct io_wq_work **workptr)
2821{
2822 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2823 struct io_kiocb *nxt = NULL;
2824 int ret;
2825
2826 if (io_req_cancelled(req))
2827 return;
2828
Jens Axboe9adbd452019-12-20 08:45:55 -07002829 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002830 req->sync.flags);
2831 if (ret < 0)
2832 req_set_fail_links(req);
2833 io_cqring_add_event(req, ret);
2834 io_put_req_find_next(req, &nxt);
2835 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002836 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002837}
2838
Jens Axboefc4df992019-12-10 14:38:45 -07002839static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002840 bool force_nonblock)
2841{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002842 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002843
2844 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002845 if (force_nonblock) {
2846 io_put_req(req);
2847 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002848 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002849 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002850
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002851 work = old_work = &req->work;
2852 io_sync_file_range_finish(&work);
2853 if (work && work != old_work)
2854 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002855 return 0;
2856}
2857
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002858#if defined(CONFIG_NET)
2859static void io_sendrecv_async(struct io_wq_work **workptr)
2860{
2861 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2862 struct iovec *iov = NULL;
2863
2864 if (req->io->rw.iov != req->io->rw.fast_iov)
2865 iov = req->io->msg.iov;
2866 io_wq_submit_work(workptr);
2867 kfree(iov);
2868}
2869#endif
2870
Jens Axboe3529d8c2019-12-19 18:24:38 -07002871static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002872{
Jens Axboe03b12302019-12-02 18:50:25 -07002873#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002874 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002875 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002876
Jens Axboee47293f2019-12-20 08:58:21 -07002877 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2878 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002879 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002880
Jens Axboefddafac2020-01-04 20:19:44 -07002881 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002882 return 0;
2883
Jens Axboed9688562019-12-09 19:35:20 -07002884 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002885 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002886 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002887#else
Jens Axboee47293f2019-12-20 08:58:21 -07002888 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002889#endif
2890}
2891
Jens Axboefc4df992019-12-10 14:38:45 -07002892static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2893 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002894{
2895#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002896 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002897 struct socket *sock;
2898 int ret;
2899
2900 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2901 return -EINVAL;
2902
2903 sock = sock_from_file(req->file, &ret);
2904 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002905 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002906 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002907 unsigned flags;
2908
Jens Axboe03b12302019-12-02 18:50:25 -07002909 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002910 kmsg = &req->io->msg;
2911 kmsg->msg.msg_name = &addr;
2912 /* if iov is set, it's allocated already */
2913 if (!kmsg->iov)
2914 kmsg->iov = kmsg->fast_iov;
2915 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002916 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002917 struct io_sr_msg *sr = &req->sr_msg;
2918
Jens Axboe0b416c32019-12-15 10:57:46 -07002919 kmsg = &io.msg;
2920 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002921
2922 io.msg.iov = io.msg.fast_iov;
2923 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2924 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002925 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002926 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002927 }
2928
Jens Axboee47293f2019-12-20 08:58:21 -07002929 flags = req->sr_msg.msg_flags;
2930 if (flags & MSG_DONTWAIT)
2931 req->flags |= REQ_F_NOWAIT;
2932 else if (force_nonblock)
2933 flags |= MSG_DONTWAIT;
2934
Jens Axboe0b416c32019-12-15 10:57:46 -07002935 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002936 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002937 if (req->io)
2938 return -EAGAIN;
2939 if (io_alloc_async_ctx(req))
2940 return -ENOMEM;
2941 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2942 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002943 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002944 }
2945 if (ret == -ERESTARTSYS)
2946 ret = -EINTR;
2947 }
2948
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002949 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002950 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002951 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002952 if (ret < 0)
2953 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002954 io_put_req_find_next(req, nxt);
2955 return 0;
2956#else
2957 return -EOPNOTSUPP;
2958#endif
2959}
2960
Jens Axboefddafac2020-01-04 20:19:44 -07002961static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2962 bool force_nonblock)
2963{
2964#if defined(CONFIG_NET)
2965 struct socket *sock;
2966 int ret;
2967
2968 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2969 return -EINVAL;
2970
2971 sock = sock_from_file(req->file, &ret);
2972 if (sock) {
2973 struct io_sr_msg *sr = &req->sr_msg;
2974 struct msghdr msg;
2975 struct iovec iov;
2976 unsigned flags;
2977
2978 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
2979 &msg.msg_iter);
2980 if (ret)
2981 return ret;
2982
2983 msg.msg_name = NULL;
2984 msg.msg_control = NULL;
2985 msg.msg_controllen = 0;
2986 msg.msg_namelen = 0;
2987
2988 flags = req->sr_msg.msg_flags;
2989 if (flags & MSG_DONTWAIT)
2990 req->flags |= REQ_F_NOWAIT;
2991 else if (force_nonblock)
2992 flags |= MSG_DONTWAIT;
2993
2994 ret = __sys_sendmsg_sock(sock, &msg, flags);
2995 if (force_nonblock && ret == -EAGAIN)
2996 return -EAGAIN;
2997 if (ret == -ERESTARTSYS)
2998 ret = -EINTR;
2999 }
3000
3001 io_cqring_add_event(req, ret);
3002 if (ret < 0)
3003 req_set_fail_links(req);
3004 io_put_req_find_next(req, nxt);
3005 return 0;
3006#else
3007 return -EOPNOTSUPP;
3008#endif
3009}
3010
Jens Axboe3529d8c2019-12-19 18:24:38 -07003011static int io_recvmsg_prep(struct io_kiocb *req,
3012 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003013{
3014#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003015 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003016 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07003017
Jens Axboe3529d8c2019-12-19 18:24:38 -07003018 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3019 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
3020
Jens Axboefddafac2020-01-04 20:19:44 -07003021 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003022 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003023
Jens Axboed9688562019-12-09 19:35:20 -07003024 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003025 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003026 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003027#else
Jens Axboee47293f2019-12-20 08:58:21 -07003028 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003029#endif
3030}
3031
Jens Axboefc4df992019-12-10 14:38:45 -07003032static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3033 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003034{
3035#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003036 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003037 struct socket *sock;
3038 int ret;
3039
3040 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3041 return -EINVAL;
3042
3043 sock = sock_from_file(req->file, &ret);
3044 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003045 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003046 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003047 unsigned flags;
3048
Jens Axboe03b12302019-12-02 18:50:25 -07003049 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003050 kmsg = &req->io->msg;
3051 kmsg->msg.msg_name = &addr;
3052 /* if iov is set, it's allocated already */
3053 if (!kmsg->iov)
3054 kmsg->iov = kmsg->fast_iov;
3055 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003056 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003057 struct io_sr_msg *sr = &req->sr_msg;
3058
Jens Axboe0b416c32019-12-15 10:57:46 -07003059 kmsg = &io.msg;
3060 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003061
3062 io.msg.iov = io.msg.fast_iov;
3063 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3064 sr->msg_flags, &io.msg.uaddr,
3065 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003066 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003067 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003068 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003069
Jens Axboee47293f2019-12-20 08:58:21 -07003070 flags = req->sr_msg.msg_flags;
3071 if (flags & MSG_DONTWAIT)
3072 req->flags |= REQ_F_NOWAIT;
3073 else if (force_nonblock)
3074 flags |= MSG_DONTWAIT;
3075
3076 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3077 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003078 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003079 if (req->io)
3080 return -EAGAIN;
3081 if (io_alloc_async_ctx(req))
3082 return -ENOMEM;
3083 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3084 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003085 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003086 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003087 if (ret == -ERESTARTSYS)
3088 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003089 }
3090
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003091 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003092 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003093 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003094 if (ret < 0)
3095 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003096 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003097 return 0;
3098#else
3099 return -EOPNOTSUPP;
3100#endif
3101}
3102
Jens Axboefddafac2020-01-04 20:19:44 -07003103static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3104 bool force_nonblock)
3105{
3106#if defined(CONFIG_NET)
3107 struct socket *sock;
3108 int ret;
3109
3110 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3111 return -EINVAL;
3112
3113 sock = sock_from_file(req->file, &ret);
3114 if (sock) {
3115 struct io_sr_msg *sr = &req->sr_msg;
3116 struct msghdr msg;
3117 struct iovec iov;
3118 unsigned flags;
3119
3120 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3121 &msg.msg_iter);
3122 if (ret)
3123 return ret;
3124
3125 msg.msg_name = NULL;
3126 msg.msg_control = NULL;
3127 msg.msg_controllen = 0;
3128 msg.msg_namelen = 0;
3129 msg.msg_iocb = NULL;
3130 msg.msg_flags = 0;
3131
3132 flags = req->sr_msg.msg_flags;
3133 if (flags & MSG_DONTWAIT)
3134 req->flags |= REQ_F_NOWAIT;
3135 else if (force_nonblock)
3136 flags |= MSG_DONTWAIT;
3137
3138 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3139 if (force_nonblock && ret == -EAGAIN)
3140 return -EAGAIN;
3141 if (ret == -ERESTARTSYS)
3142 ret = -EINTR;
3143 }
3144
3145 io_cqring_add_event(req, ret);
3146 if (ret < 0)
3147 req_set_fail_links(req);
3148 io_put_req_find_next(req, nxt);
3149 return 0;
3150#else
3151 return -EOPNOTSUPP;
3152#endif
3153}
3154
3155
Jens Axboe3529d8c2019-12-19 18:24:38 -07003156static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003157{
3158#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003159 struct io_accept *accept = &req->accept;
3160
Jens Axboe17f2fe32019-10-17 14:42:58 -06003161 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3162 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003163 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003164 return -EINVAL;
3165
Jens Axboed55e5f52019-12-11 16:12:15 -07003166 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3167 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003168 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003169 return 0;
3170#else
3171 return -EOPNOTSUPP;
3172#endif
3173}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003174
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003175#if defined(CONFIG_NET)
3176static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3177 bool force_nonblock)
3178{
3179 struct io_accept *accept = &req->accept;
3180 unsigned file_flags;
3181 int ret;
3182
3183 file_flags = force_nonblock ? O_NONBLOCK : 0;
3184 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3185 accept->addr_len, accept->flags);
3186 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003187 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003188 if (ret == -ERESTARTSYS)
3189 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003190 if (ret < 0)
3191 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003192 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003193 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003194 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003195}
3196
3197static void io_accept_finish(struct io_wq_work **workptr)
3198{
3199 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3200 struct io_kiocb *nxt = NULL;
3201
3202 if (io_req_cancelled(req))
3203 return;
3204 __io_accept(req, &nxt, false);
3205 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003206 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003207}
3208#endif
3209
3210static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3211 bool force_nonblock)
3212{
3213#if defined(CONFIG_NET)
3214 int ret;
3215
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003216 ret = __io_accept(req, nxt, force_nonblock);
3217 if (ret == -EAGAIN && force_nonblock) {
3218 req->work.func = io_accept_finish;
3219 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3220 io_put_req(req);
3221 return -EAGAIN;
3222 }
3223 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003224#else
3225 return -EOPNOTSUPP;
3226#endif
3227}
3228
Jens Axboe3529d8c2019-12-19 18:24:38 -07003229static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003230{
3231#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003232 struct io_connect *conn = &req->connect;
3233 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003234
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003235 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3236 return -EINVAL;
3237 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3238 return -EINVAL;
3239
Jens Axboe3529d8c2019-12-19 18:24:38 -07003240 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3241 conn->addr_len = READ_ONCE(sqe->addr2);
3242
3243 if (!io)
3244 return 0;
3245
3246 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003247 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003248#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003249 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003250#endif
3251}
3252
Jens Axboefc4df992019-12-10 14:38:45 -07003253static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3254 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003255{
3256#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003257 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003258 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003259 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003260
Jens Axboef499a022019-12-02 16:28:46 -07003261 if (req->io) {
3262 io = req->io;
3263 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003264 ret = move_addr_to_kernel(req->connect.addr,
3265 req->connect.addr_len,
3266 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003267 if (ret)
3268 goto out;
3269 io = &__io;
3270 }
3271
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003272 file_flags = force_nonblock ? O_NONBLOCK : 0;
3273
3274 ret = __sys_connect_file(req->file, &io->connect.address,
3275 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003276 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003277 if (req->io)
3278 return -EAGAIN;
3279 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003280 ret = -ENOMEM;
3281 goto out;
3282 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003283 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003284 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003285 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003286 if (ret == -ERESTARTSYS)
3287 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003288out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003289 if (ret < 0)
3290 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003291 io_cqring_add_event(req, ret);
3292 io_put_req_find_next(req, nxt);
3293 return 0;
3294#else
3295 return -EOPNOTSUPP;
3296#endif
3297}
3298
Jens Axboe221c5eb2019-01-17 09:41:58 -07003299static void io_poll_remove_one(struct io_kiocb *req)
3300{
3301 struct io_poll_iocb *poll = &req->poll;
3302
3303 spin_lock(&poll->head->lock);
3304 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003305 if (!list_empty(&poll->wait.entry)) {
3306 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003307 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003308 }
3309 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003310 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003311}
3312
3313static void io_poll_remove_all(struct io_ring_ctx *ctx)
3314{
Jens Axboe78076bb2019-12-04 19:56:40 -07003315 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003316 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003317 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003318
3319 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003320 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3321 struct hlist_head *list;
3322
3323 list = &ctx->cancel_hash[i];
3324 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3325 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003326 }
3327 spin_unlock_irq(&ctx->completion_lock);
3328}
3329
Jens Axboe47f46762019-11-09 17:43:02 -07003330static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3331{
Jens Axboe78076bb2019-12-04 19:56:40 -07003332 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003333 struct io_kiocb *req;
3334
Jens Axboe78076bb2019-12-04 19:56:40 -07003335 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3336 hlist_for_each_entry(req, list, hash_node) {
3337 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003338 io_poll_remove_one(req);
3339 return 0;
3340 }
Jens Axboe47f46762019-11-09 17:43:02 -07003341 }
3342
3343 return -ENOENT;
3344}
3345
Jens Axboe3529d8c2019-12-19 18:24:38 -07003346static int io_poll_remove_prep(struct io_kiocb *req,
3347 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003348{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003349 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3350 return -EINVAL;
3351 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3352 sqe->poll_events)
3353 return -EINVAL;
3354
Jens Axboe0969e782019-12-17 18:40:57 -07003355 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003356 return 0;
3357}
3358
3359/*
3360 * Find a running poll command that matches one specified in sqe->addr,
3361 * and remove it if found.
3362 */
3363static int io_poll_remove(struct io_kiocb *req)
3364{
3365 struct io_ring_ctx *ctx = req->ctx;
3366 u64 addr;
3367 int ret;
3368
Jens Axboe0969e782019-12-17 18:40:57 -07003369 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003370 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003371 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003372 spin_unlock_irq(&ctx->completion_lock);
3373
Jens Axboe78e19bb2019-11-06 15:21:34 -07003374 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003375 if (ret < 0)
3376 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003377 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003378 return 0;
3379}
3380
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003381static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003382{
Jackie Liua197f662019-11-08 08:09:12 -07003383 struct io_ring_ctx *ctx = req->ctx;
3384
Jens Axboe8c838782019-03-12 15:48:16 -06003385 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003386 if (error)
3387 io_cqring_fill_event(req, error);
3388 else
3389 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003390 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003391}
3392
Jens Axboe561fb042019-10-24 07:25:42 -06003393static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003394{
Jens Axboe561fb042019-10-24 07:25:42 -06003395 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003396 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3397 struct io_poll_iocb *poll = &req->poll;
3398 struct poll_table_struct pt = { ._key = poll->events };
3399 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003400 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003401 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003402 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003403
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003404 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003405 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003406 ret = -ECANCELED;
3407 } else if (READ_ONCE(poll->canceled)) {
3408 ret = -ECANCELED;
3409 }
Jens Axboe561fb042019-10-24 07:25:42 -06003410
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003411 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003412 mask = vfs_poll(poll->file, &pt) & poll->events;
3413
3414 /*
3415 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3416 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3417 * synchronize with them. In the cancellation case the list_del_init
3418 * itself is not actually needed, but harmless so we keep it in to
3419 * avoid further branches in the fast path.
3420 */
3421 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003422 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003423 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003424 spin_unlock_irq(&ctx->completion_lock);
3425 return;
3426 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003427 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003428 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003429 spin_unlock_irq(&ctx->completion_lock);
3430
Jens Axboe8c838782019-03-12 15:48:16 -06003431 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003432
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003433 if (ret < 0)
3434 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003435 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003436 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003437 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003438}
3439
Jens Axboee94f1412019-12-19 12:06:02 -07003440static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3441{
Jens Axboee94f1412019-12-19 12:06:02 -07003442 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003443 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003444
Jens Axboec6ca97b302019-12-28 12:11:08 -07003445 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003446 spin_lock_irq(&ctx->completion_lock);
3447 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3448 hash_del(&req->hash_node);
3449 io_poll_complete(req, req->result, 0);
3450
Jens Axboe8237e042019-12-28 10:48:22 -07003451 if (refcount_dec_and_test(&req->refs) &&
3452 !io_req_multi_free(&rb, req)) {
3453 req->flags |= REQ_F_COMP_LOCKED;
3454 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003455 }
3456 }
3457 spin_unlock_irq(&ctx->completion_lock);
3458
3459 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003460 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003461}
3462
3463static void io_poll_flush(struct io_wq_work **workptr)
3464{
3465 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3466 struct llist_node *nodes;
3467
3468 nodes = llist_del_all(&req->ctx->poll_llist);
3469 if (nodes)
3470 __io_poll_flush(req->ctx, nodes);
3471}
3472
Jens Axboe221c5eb2019-01-17 09:41:58 -07003473static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3474 void *key)
3475{
Jens Axboee9444752019-11-26 15:02:04 -07003476 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003477 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3478 struct io_ring_ctx *ctx = req->ctx;
3479 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003480
3481 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003482 if (mask && !(mask & poll->events))
3483 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003484
Jens Axboe392edb42019-12-09 17:52:20 -07003485 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003486
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003487 /*
3488 * Run completion inline if we can. We're using trylock here because
3489 * we are violating the completion_lock -> poll wq lock ordering.
3490 * If we have a link timeout we're going to need the completion_lock
3491 * for finalizing the request, mark us as having grabbed that already.
3492 */
Jens Axboee94f1412019-12-19 12:06:02 -07003493 if (mask) {
3494 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003495
Jens Axboee94f1412019-12-19 12:06:02 -07003496 if (llist_empty(&ctx->poll_llist) &&
3497 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3498 hash_del(&req->hash_node);
3499 io_poll_complete(req, mask, 0);
3500 req->flags |= REQ_F_COMP_LOCKED;
3501 io_put_req(req);
3502 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3503
3504 io_cqring_ev_posted(ctx);
3505 req = NULL;
3506 } else {
3507 req->result = mask;
3508 req->llist_node.next = NULL;
3509 /* if the list wasn't empty, we're done */
3510 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3511 req = NULL;
3512 else
3513 req->work.func = io_poll_flush;
3514 }
Jens Axboe8c838782019-03-12 15:48:16 -06003515 }
Jens Axboee94f1412019-12-19 12:06:02 -07003516 if (req)
3517 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003518
Jens Axboe221c5eb2019-01-17 09:41:58 -07003519 return 1;
3520}
3521
3522struct io_poll_table {
3523 struct poll_table_struct pt;
3524 struct io_kiocb *req;
3525 int error;
3526};
3527
3528static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3529 struct poll_table_struct *p)
3530{
3531 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3532
3533 if (unlikely(pt->req->poll.head)) {
3534 pt->error = -EINVAL;
3535 return;
3536 }
3537
3538 pt->error = 0;
3539 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003540 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003541}
3542
Jens Axboeeac406c2019-11-14 12:09:58 -07003543static void io_poll_req_insert(struct io_kiocb *req)
3544{
3545 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003546 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003547
Jens Axboe78076bb2019-12-04 19:56:40 -07003548 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3549 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003550}
3551
Jens Axboe3529d8c2019-12-19 18:24:38 -07003552static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003553{
3554 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003555 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003556
3557 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3558 return -EINVAL;
3559 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3560 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003561 if (!poll->file)
3562 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003563
Jens Axboe221c5eb2019-01-17 09:41:58 -07003564 events = READ_ONCE(sqe->poll_events);
3565 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003566 return 0;
3567}
3568
3569static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3570{
3571 struct io_poll_iocb *poll = &req->poll;
3572 struct io_ring_ctx *ctx = req->ctx;
3573 struct io_poll_table ipt;
3574 bool cancel = false;
3575 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003576
3577 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003578 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003579
Jens Axboe221c5eb2019-01-17 09:41:58 -07003580 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003581 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003582 poll->canceled = false;
3583
3584 ipt.pt._qproc = io_poll_queue_proc;
3585 ipt.pt._key = poll->events;
3586 ipt.req = req;
3587 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3588
3589 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003590 INIT_LIST_HEAD(&poll->wait.entry);
3591 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3592 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003593
Jens Axboe36703242019-07-25 10:20:18 -06003594 INIT_LIST_HEAD(&req->list);
3595
Jens Axboe221c5eb2019-01-17 09:41:58 -07003596 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003597
3598 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003599 if (likely(poll->head)) {
3600 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003601 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003602 if (ipt.error)
3603 cancel = true;
3604 ipt.error = 0;
3605 mask = 0;
3606 }
3607 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003608 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003609 else if (cancel)
3610 WRITE_ONCE(poll->canceled, true);
3611 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003612 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003613 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003614 }
Jens Axboe8c838782019-03-12 15:48:16 -06003615 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003616 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003617 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003618 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003619 spin_unlock_irq(&ctx->completion_lock);
3620
Jens Axboe8c838782019-03-12 15:48:16 -06003621 if (mask) {
3622 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003623 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003624 }
Jens Axboe8c838782019-03-12 15:48:16 -06003625 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003626}
3627
Jens Axboe5262f562019-09-17 12:26:57 -06003628static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3629{
Jens Axboead8a48a2019-11-15 08:49:11 -07003630 struct io_timeout_data *data = container_of(timer,
3631 struct io_timeout_data, timer);
3632 struct io_kiocb *req = data->req;
3633 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003634 unsigned long flags;
3635
Jens Axboe5262f562019-09-17 12:26:57 -06003636 atomic_inc(&ctx->cq_timeouts);
3637
3638 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003639 /*
Jens Axboe11365042019-10-16 09:08:32 -06003640 * We could be racing with timeout deletion. If the list is empty,
3641 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003642 */
Jens Axboe842f9612019-10-29 12:34:10 -06003643 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003644 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003645
Jens Axboe11365042019-10-16 09:08:32 -06003646 /*
3647 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003648 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003649 * pointer will be increased, otherwise other timeout reqs may
3650 * return in advance without waiting for enough wait_nr.
3651 */
3652 prev = req;
3653 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3654 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003655 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003656 }
Jens Axboe842f9612019-10-29 12:34:10 -06003657
Jens Axboe78e19bb2019-11-06 15:21:34 -07003658 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003659 io_commit_cqring(ctx);
3660 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3661
3662 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003663 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003664 io_put_req(req);
3665 return HRTIMER_NORESTART;
3666}
3667
Jens Axboe47f46762019-11-09 17:43:02 -07003668static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3669{
3670 struct io_kiocb *req;
3671 int ret = -ENOENT;
3672
3673 list_for_each_entry(req, &ctx->timeout_list, list) {
3674 if (user_data == req->user_data) {
3675 list_del_init(&req->list);
3676 ret = 0;
3677 break;
3678 }
3679 }
3680
3681 if (ret == -ENOENT)
3682 return ret;
3683
Jens Axboe2d283902019-12-04 11:08:05 -07003684 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003685 if (ret == -1)
3686 return -EALREADY;
3687
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003688 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003689 io_cqring_fill_event(req, -ECANCELED);
3690 io_put_req(req);
3691 return 0;
3692}
3693
Jens Axboe3529d8c2019-12-19 18:24:38 -07003694static int io_timeout_remove_prep(struct io_kiocb *req,
3695 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003696{
Jens Axboeb29472e2019-12-17 18:50:29 -07003697 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3698 return -EINVAL;
3699 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3700 return -EINVAL;
3701
3702 req->timeout.addr = READ_ONCE(sqe->addr);
3703 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3704 if (req->timeout.flags)
3705 return -EINVAL;
3706
Jens Axboeb29472e2019-12-17 18:50:29 -07003707 return 0;
3708}
3709
Jens Axboe11365042019-10-16 09:08:32 -06003710/*
3711 * Remove or update an existing timeout command
3712 */
Jens Axboefc4df992019-12-10 14:38:45 -07003713static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003714{
3715 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003716 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003717
Jens Axboe11365042019-10-16 09:08:32 -06003718 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003719 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003720
Jens Axboe47f46762019-11-09 17:43:02 -07003721 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003722 io_commit_cqring(ctx);
3723 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003724 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003725 if (ret < 0)
3726 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003727 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003728 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003729}
3730
Jens Axboe3529d8c2019-12-19 18:24:38 -07003731static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003732 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003733{
Jens Axboead8a48a2019-11-15 08:49:11 -07003734 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003735 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003736
Jens Axboead8a48a2019-11-15 08:49:11 -07003737 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003738 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003739 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003740 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003741 if (sqe->off && is_timeout_link)
3742 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003743 flags = READ_ONCE(sqe->timeout_flags);
3744 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003745 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003746
Jens Axboe26a61672019-12-20 09:02:01 -07003747 req->timeout.count = READ_ONCE(sqe->off);
3748
Jens Axboe3529d8c2019-12-19 18:24:38 -07003749 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003750 return -ENOMEM;
3751
3752 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003753 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003754 req->flags |= REQ_F_TIMEOUT;
3755
3756 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003757 return -EFAULT;
3758
Jens Axboe11365042019-10-16 09:08:32 -06003759 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003760 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003761 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003762 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003763
Jens Axboead8a48a2019-11-15 08:49:11 -07003764 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3765 return 0;
3766}
3767
Jens Axboefc4df992019-12-10 14:38:45 -07003768static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003769{
3770 unsigned count;
3771 struct io_ring_ctx *ctx = req->ctx;
3772 struct io_timeout_data *data;
3773 struct list_head *entry;
3774 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003775
Jens Axboe2d283902019-12-04 11:08:05 -07003776 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003777
Jens Axboe5262f562019-09-17 12:26:57 -06003778 /*
3779 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003780 * timeout event to be satisfied. If it isn't set, then this is
3781 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003782 */
Jens Axboe26a61672019-12-20 09:02:01 -07003783 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003784 if (!count) {
3785 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3786 spin_lock_irq(&ctx->completion_lock);
3787 entry = ctx->timeout_list.prev;
3788 goto add;
3789 }
Jens Axboe5262f562019-09-17 12:26:57 -06003790
3791 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003792 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003793
3794 /*
3795 * Insertion sort, ensuring the first entry in the list is always
3796 * the one we need first.
3797 */
Jens Axboe5262f562019-09-17 12:26:57 -06003798 spin_lock_irq(&ctx->completion_lock);
3799 list_for_each_prev(entry, &ctx->timeout_list) {
3800 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003801 unsigned nxt_sq_head;
3802 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003803 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003804
Jens Axboe93bd25b2019-11-11 23:34:31 -07003805 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3806 continue;
3807
yangerkun5da0fb12019-10-15 21:59:29 +08003808 /*
3809 * Since cached_sq_head + count - 1 can overflow, use type long
3810 * long to store it.
3811 */
3812 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003813 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3814 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003815
3816 /*
3817 * cached_sq_head may overflow, and it will never overflow twice
3818 * once there is some timeout req still be valid.
3819 */
3820 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003821 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003822
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003823 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003824 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003825
3826 /*
3827 * Sequence of reqs after the insert one and itself should
3828 * be adjusted because each timeout req consumes a slot.
3829 */
3830 span++;
3831 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003832 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003833 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003834add:
Jens Axboe5262f562019-09-17 12:26:57 -06003835 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003836 data->timer.function = io_timeout_fn;
3837 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003838 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003839 return 0;
3840}
3841
Jens Axboe62755e32019-10-28 21:49:21 -06003842static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003843{
Jens Axboe62755e32019-10-28 21:49:21 -06003844 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003845
Jens Axboe62755e32019-10-28 21:49:21 -06003846 return req->user_data == (unsigned long) data;
3847}
3848
Jens Axboee977d6d2019-11-05 12:39:45 -07003849static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003850{
Jens Axboe62755e32019-10-28 21:49:21 -06003851 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003852 int ret = 0;
3853
Jens Axboe62755e32019-10-28 21:49:21 -06003854 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3855 switch (cancel_ret) {
3856 case IO_WQ_CANCEL_OK:
3857 ret = 0;
3858 break;
3859 case IO_WQ_CANCEL_RUNNING:
3860 ret = -EALREADY;
3861 break;
3862 case IO_WQ_CANCEL_NOTFOUND:
3863 ret = -ENOENT;
3864 break;
3865 }
3866
Jens Axboee977d6d2019-11-05 12:39:45 -07003867 return ret;
3868}
3869
Jens Axboe47f46762019-11-09 17:43:02 -07003870static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3871 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003872 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003873{
3874 unsigned long flags;
3875 int ret;
3876
3877 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3878 if (ret != -ENOENT) {
3879 spin_lock_irqsave(&ctx->completion_lock, flags);
3880 goto done;
3881 }
3882
3883 spin_lock_irqsave(&ctx->completion_lock, flags);
3884 ret = io_timeout_cancel(ctx, sqe_addr);
3885 if (ret != -ENOENT)
3886 goto done;
3887 ret = io_poll_cancel(ctx, sqe_addr);
3888done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003889 if (!ret)
3890 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003891 io_cqring_fill_event(req, ret);
3892 io_commit_cqring(ctx);
3893 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3894 io_cqring_ev_posted(ctx);
3895
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003896 if (ret < 0)
3897 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003898 io_put_req_find_next(req, nxt);
3899}
3900
Jens Axboe3529d8c2019-12-19 18:24:38 -07003901static int io_async_cancel_prep(struct io_kiocb *req,
3902 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003903{
Jens Axboefbf23842019-12-17 18:45:56 -07003904 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003905 return -EINVAL;
3906 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3907 sqe->cancel_flags)
3908 return -EINVAL;
3909
Jens Axboefbf23842019-12-17 18:45:56 -07003910 req->cancel.addr = READ_ONCE(sqe->addr);
3911 return 0;
3912}
3913
3914static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3915{
3916 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003917
3918 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003919 return 0;
3920}
3921
Jens Axboe05f3fb32019-12-09 11:22:50 -07003922static int io_files_update_prep(struct io_kiocb *req,
3923 const struct io_uring_sqe *sqe)
3924{
3925 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3926 return -EINVAL;
3927
3928 req->files_update.offset = READ_ONCE(sqe->off);
3929 req->files_update.nr_args = READ_ONCE(sqe->len);
3930 if (!req->files_update.nr_args)
3931 return -EINVAL;
3932 req->files_update.arg = READ_ONCE(sqe->addr);
3933 return 0;
3934}
3935
3936static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3937{
3938 struct io_ring_ctx *ctx = req->ctx;
3939 struct io_uring_files_update up;
3940 int ret;
3941
3942 if (force_nonblock) {
3943 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3944 return -EAGAIN;
3945 }
3946
3947 up.offset = req->files_update.offset;
3948 up.fds = req->files_update.arg;
3949
3950 mutex_lock(&ctx->uring_lock);
3951 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3952 mutex_unlock(&ctx->uring_lock);
3953
3954 if (ret < 0)
3955 req_set_fail_links(req);
3956 io_cqring_add_event(req, ret);
3957 io_put_req(req);
3958 return 0;
3959}
3960
Jens Axboe3529d8c2019-12-19 18:24:38 -07003961static int io_req_defer_prep(struct io_kiocb *req,
3962 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003963{
Jens Axboee7815732019-12-17 19:45:06 -07003964 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003965
Jens Axboed625c6e2019-12-17 19:53:05 -07003966 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003967 case IORING_OP_NOP:
3968 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003969 case IORING_OP_READV:
3970 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003971 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003972 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003973 break;
3974 case IORING_OP_WRITEV:
3975 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003976 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003977 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003978 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003979 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003980 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003981 break;
3982 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003983 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003984 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003985 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003986 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003987 break;
3988 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003989 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003990 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003991 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003992 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003993 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003994 break;
3995 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003996 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003997 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003998 break;
Jens Axboef499a022019-12-02 16:28:46 -07003999 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004000 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004001 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004002 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004003 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004004 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004005 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004006 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004007 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004008 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004009 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004010 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004011 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004012 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004013 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004014 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004015 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004016 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004017 case IORING_OP_FALLOCATE:
4018 ret = io_fallocate_prep(req, sqe);
4019 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004020 case IORING_OP_OPENAT:
4021 ret = io_openat_prep(req, sqe);
4022 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004023 case IORING_OP_CLOSE:
4024 ret = io_close_prep(req, sqe);
4025 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004026 case IORING_OP_FILES_UPDATE:
4027 ret = io_files_update_prep(req, sqe);
4028 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004029 case IORING_OP_STATX:
4030 ret = io_statx_prep(req, sqe);
4031 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004032 case IORING_OP_FADVISE:
4033 ret = io_fadvise_prep(req, sqe);
4034 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004035 case IORING_OP_MADVISE:
4036 ret = io_madvise_prep(req, sqe);
4037 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004038 case IORING_OP_OPENAT2:
4039 ret = io_openat2_prep(req, sqe);
4040 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004041 default:
Jens Axboee7815732019-12-17 19:45:06 -07004042 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4043 req->opcode);
4044 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004045 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004046 }
4047
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004048 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004049}
4050
Jens Axboe3529d8c2019-12-19 18:24:38 -07004051static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004052{
Jackie Liua197f662019-11-08 08:09:12 -07004053 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004054 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004055
Bob Liu9d858b22019-11-13 18:06:25 +08004056 /* Still need defer if there is pending req in defer list. */
4057 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004058 return 0;
4059
Jens Axboe3529d8c2019-12-19 18:24:38 -07004060 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004061 return -EAGAIN;
4062
Jens Axboe3529d8c2019-12-19 18:24:38 -07004063 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004064 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004065 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004066
Jens Axboede0617e2019-04-06 21:51:27 -06004067 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004068 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004069 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004070 return 0;
4071 }
4072
Jens Axboe915967f2019-11-21 09:01:20 -07004073 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004074 list_add_tail(&req->list, &ctx->defer_list);
4075 spin_unlock_irq(&ctx->completion_lock);
4076 return -EIOCBQUEUED;
4077}
4078
Jens Axboe3529d8c2019-12-19 18:24:38 -07004079static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4080 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004081{
Jackie Liua197f662019-11-08 08:09:12 -07004082 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004083 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004084
Jens Axboed625c6e2019-12-17 19:53:05 -07004085 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004086 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004087 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004088 break;
4089 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004090 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004091 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004092 if (sqe) {
4093 ret = io_read_prep(req, sqe, force_nonblock);
4094 if (ret < 0)
4095 break;
4096 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004097 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004098 break;
4099 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004100 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004101 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004102 if (sqe) {
4103 ret = io_write_prep(req, sqe, force_nonblock);
4104 if (ret < 0)
4105 break;
4106 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004107 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004108 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004109 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004110 if (sqe) {
4111 ret = io_prep_fsync(req, sqe);
4112 if (ret < 0)
4113 break;
4114 }
Jens Axboefc4df992019-12-10 14:38:45 -07004115 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004116 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004117 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004118 if (sqe) {
4119 ret = io_poll_add_prep(req, sqe);
4120 if (ret)
4121 break;
4122 }
Jens Axboefc4df992019-12-10 14:38:45 -07004123 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004124 break;
4125 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004126 if (sqe) {
4127 ret = io_poll_remove_prep(req, sqe);
4128 if (ret < 0)
4129 break;
4130 }
Jens Axboefc4df992019-12-10 14:38:45 -07004131 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004132 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004133 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004134 if (sqe) {
4135 ret = io_prep_sfr(req, sqe);
4136 if (ret < 0)
4137 break;
4138 }
Jens Axboefc4df992019-12-10 14:38:45 -07004139 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004140 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004141 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004142 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004143 if (sqe) {
4144 ret = io_sendmsg_prep(req, sqe);
4145 if (ret < 0)
4146 break;
4147 }
Jens Axboefddafac2020-01-04 20:19:44 -07004148 if (req->opcode == IORING_OP_SENDMSG)
4149 ret = io_sendmsg(req, nxt, force_nonblock);
4150 else
4151 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004152 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004153 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004154 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004155 if (sqe) {
4156 ret = io_recvmsg_prep(req, sqe);
4157 if (ret)
4158 break;
4159 }
Jens Axboefddafac2020-01-04 20:19:44 -07004160 if (req->opcode == IORING_OP_RECVMSG)
4161 ret = io_recvmsg(req, nxt, force_nonblock);
4162 else
4163 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004164 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004165 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004166 if (sqe) {
4167 ret = io_timeout_prep(req, sqe, false);
4168 if (ret)
4169 break;
4170 }
Jens Axboefc4df992019-12-10 14:38:45 -07004171 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004172 break;
Jens Axboe11365042019-10-16 09:08:32 -06004173 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004174 if (sqe) {
4175 ret = io_timeout_remove_prep(req, sqe);
4176 if (ret)
4177 break;
4178 }
Jens Axboefc4df992019-12-10 14:38:45 -07004179 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004180 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004181 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004182 if (sqe) {
4183 ret = io_accept_prep(req, sqe);
4184 if (ret)
4185 break;
4186 }
Jens Axboefc4df992019-12-10 14:38:45 -07004187 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004188 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004189 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004190 if (sqe) {
4191 ret = io_connect_prep(req, sqe);
4192 if (ret)
4193 break;
4194 }
Jens Axboefc4df992019-12-10 14:38:45 -07004195 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004196 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004197 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004198 if (sqe) {
4199 ret = io_async_cancel_prep(req, sqe);
4200 if (ret)
4201 break;
4202 }
Jens Axboefc4df992019-12-10 14:38:45 -07004203 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004204 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004205 case IORING_OP_FALLOCATE:
4206 if (sqe) {
4207 ret = io_fallocate_prep(req, sqe);
4208 if (ret)
4209 break;
4210 }
4211 ret = io_fallocate(req, nxt, force_nonblock);
4212 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004213 case IORING_OP_OPENAT:
4214 if (sqe) {
4215 ret = io_openat_prep(req, sqe);
4216 if (ret)
4217 break;
4218 }
4219 ret = io_openat(req, nxt, force_nonblock);
4220 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004221 case IORING_OP_CLOSE:
4222 if (sqe) {
4223 ret = io_close_prep(req, sqe);
4224 if (ret)
4225 break;
4226 }
4227 ret = io_close(req, nxt, force_nonblock);
4228 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004229 case IORING_OP_FILES_UPDATE:
4230 if (sqe) {
4231 ret = io_files_update_prep(req, sqe);
4232 if (ret)
4233 break;
4234 }
4235 ret = io_files_update(req, force_nonblock);
4236 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004237 case IORING_OP_STATX:
4238 if (sqe) {
4239 ret = io_statx_prep(req, sqe);
4240 if (ret)
4241 break;
4242 }
4243 ret = io_statx(req, nxt, force_nonblock);
4244 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004245 case IORING_OP_FADVISE:
4246 if (sqe) {
4247 ret = io_fadvise_prep(req, sqe);
4248 if (ret)
4249 break;
4250 }
4251 ret = io_fadvise(req, nxt, force_nonblock);
4252 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004253 case IORING_OP_MADVISE:
4254 if (sqe) {
4255 ret = io_madvise_prep(req, sqe);
4256 if (ret)
4257 break;
4258 }
4259 ret = io_madvise(req, nxt, force_nonblock);
4260 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004261 case IORING_OP_OPENAT2:
4262 if (sqe) {
4263 ret = io_openat2_prep(req, sqe);
4264 if (ret)
4265 break;
4266 }
4267 ret = io_openat2(req, nxt, force_nonblock);
4268 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004269 default:
4270 ret = -EINVAL;
4271 break;
4272 }
4273
Jens Axboedef596e2019-01-09 08:59:42 -07004274 if (ret)
4275 return ret;
4276
4277 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004278 const bool in_async = io_wq_current_is_worker();
4279
Jens Axboe9e645e112019-05-10 16:07:28 -06004280 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004281 return -EAGAIN;
4282
Jens Axboe11ba8202020-01-15 21:51:17 -07004283 /* workqueue context doesn't hold uring_lock, grab it now */
4284 if (in_async)
4285 mutex_lock(&ctx->uring_lock);
4286
Jens Axboedef596e2019-01-09 08:59:42 -07004287 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004288
4289 if (in_async)
4290 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004291 }
4292
4293 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004294}
4295
Jens Axboe561fb042019-10-24 07:25:42 -06004296static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004297{
Jens Axboe561fb042019-10-24 07:25:42 -06004298 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004299 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004300 struct io_kiocb *nxt = NULL;
4301 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004302
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004303 /* if NO_CANCEL is set, we must still run the work */
4304 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4305 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004306 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004307 }
Jens Axboe31b51512019-01-18 22:56:34 -07004308
Jens Axboe561fb042019-10-24 07:25:42 -06004309 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004310 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4311 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004312 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004313 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004314 /*
4315 * We can get EAGAIN for polled IO even though we're
4316 * forcing a sync submission from here, since we can't
4317 * wait for request slots on the block side.
4318 */
4319 if (ret != -EAGAIN)
4320 break;
4321 cond_resched();
4322 } while (1);
4323 }
Jens Axboe31b51512019-01-18 22:56:34 -07004324
Jens Axboe561fb042019-10-24 07:25:42 -06004325 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004326 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004327
Jens Axboe561fb042019-10-24 07:25:42 -06004328 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004329 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004330 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004331 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004332 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004333
Jens Axboe561fb042019-10-24 07:25:42 -06004334 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004335 if (!ret && nxt)
4336 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004337}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004338
Jens Axboe15b71ab2019-12-11 11:20:36 -07004339static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004340{
Jens Axboed3656342019-12-18 09:50:26 -07004341 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004342 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004343 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4344 return 0;
4345 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004346}
4347
Jens Axboe65e19f52019-10-26 07:20:21 -06004348static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4349 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004350{
Jens Axboe65e19f52019-10-26 07:20:21 -06004351 struct fixed_file_table *table;
4352
Jens Axboe05f3fb32019-12-09 11:22:50 -07004353 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4354 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004355}
4356
Jens Axboe3529d8c2019-12-19 18:24:38 -07004357static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4358 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004359{
Jackie Liua197f662019-11-08 08:09:12 -07004360 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004361 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004362 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004363
Jens Axboe3529d8c2019-12-19 18:24:38 -07004364 flags = READ_ONCE(sqe->flags);
4365 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004366
Jens Axboed3656342019-12-18 09:50:26 -07004367 if (!io_req_needs_file(req, fd))
4368 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004369
4370 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004371 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004372 (unsigned) fd >= ctx->nr_user_files))
4373 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004374 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004375 req->file = io_file_from_index(ctx, fd);
4376 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004377 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004378 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004379 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004380 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004381 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004382 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004383 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004384 req->file = io_file_get(state, fd);
4385 if (unlikely(!req->file))
4386 return -EBADF;
4387 }
4388
4389 return 0;
4390}
4391
Jackie Liua197f662019-11-08 08:09:12 -07004392static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004393{
Jens Axboefcb323c2019-10-24 12:39:47 -06004394 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004395 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004396
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004397 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004398 return -EBADF;
4399
Jens Axboefcb323c2019-10-24 12:39:47 -06004400 rcu_read_lock();
4401 spin_lock_irq(&ctx->inflight_lock);
4402 /*
4403 * We use the f_ops->flush() handler to ensure that we can flush
4404 * out work accessing these files if the fd is closed. Check if
4405 * the fd has changed since we started down this path, and disallow
4406 * this operation if it has.
4407 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004408 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004409 list_add(&req->inflight_entry, &ctx->inflight_list);
4410 req->flags |= REQ_F_INFLIGHT;
4411 req->work.files = current->files;
4412 ret = 0;
4413 }
4414 spin_unlock_irq(&ctx->inflight_lock);
4415 rcu_read_unlock();
4416
4417 return ret;
4418}
4419
Jens Axboe2665abf2019-11-05 12:40:47 -07004420static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4421{
Jens Axboead8a48a2019-11-15 08:49:11 -07004422 struct io_timeout_data *data = container_of(timer,
4423 struct io_timeout_data, timer);
4424 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004425 struct io_ring_ctx *ctx = req->ctx;
4426 struct io_kiocb *prev = NULL;
4427 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004428
4429 spin_lock_irqsave(&ctx->completion_lock, flags);
4430
4431 /*
4432 * We don't expect the list to be empty, that will only happen if we
4433 * race with the completion of the linked work.
4434 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004435 if (!list_empty(&req->link_list)) {
4436 prev = list_entry(req->link_list.prev, struct io_kiocb,
4437 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004438 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004439 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004440 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4441 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004442 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004443 }
4444
4445 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4446
4447 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004448 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004449 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4450 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004451 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004452 } else {
4453 io_cqring_add_event(req, -ETIME);
4454 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004455 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004456 return HRTIMER_NORESTART;
4457}
4458
Jens Axboead8a48a2019-11-15 08:49:11 -07004459static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004460{
Jens Axboe76a46e02019-11-10 23:34:16 -07004461 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004462
Jens Axboe76a46e02019-11-10 23:34:16 -07004463 /*
4464 * If the list is now empty, then our linked request finished before
4465 * we got a chance to setup the timer
4466 */
4467 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004468 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004469 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004470
Jens Axboead8a48a2019-11-15 08:49:11 -07004471 data->timer.function = io_link_timeout_fn;
4472 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4473 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004474 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004475 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004476
Jens Axboe2665abf2019-11-05 12:40:47 -07004477 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004478 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004479}
4480
Jens Axboead8a48a2019-11-15 08:49:11 -07004481static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004482{
4483 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004484
Jens Axboe2665abf2019-11-05 12:40:47 -07004485 if (!(req->flags & REQ_F_LINK))
4486 return NULL;
4487
Pavel Begunkov44932332019-12-05 16:16:35 +03004488 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4489 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004490 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004491 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004492
Jens Axboe76a46e02019-11-10 23:34:16 -07004493 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004494 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004495}
4496
Jens Axboe3529d8c2019-12-19 18:24:38 -07004497static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004498{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004499 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004500 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004501 int ret;
4502
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004503again:
4504 linked_timeout = io_prep_linked_timeout(req);
4505
Jens Axboe3529d8c2019-12-19 18:24:38 -07004506 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004507
4508 /*
4509 * We async punt it if the file wasn't marked NOWAIT, or if the file
4510 * doesn't support non-blocking read/write attempts
4511 */
4512 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4513 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004514punt:
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004515 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4516 ret = io_grab_files(req);
4517 if (ret)
4518 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004519 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004520
4521 /*
4522 * Queued up for async execution, worker will release
4523 * submit reference when the iocb is actually submitted.
4524 */
4525 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004526 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004527 }
Jens Axboee65ef562019-03-12 10:16:44 -06004528
Jens Axboefcb323c2019-10-24 12:39:47 -06004529err:
Jens Axboee65ef562019-03-12 10:16:44 -06004530 /* drop submission reference */
4531 io_put_req(req);
4532
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004533 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004534 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004535 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004536 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004537 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004538 }
4539
Jens Axboee65ef562019-03-12 10:16:44 -06004540 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004541 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004542 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004543 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004544 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004545 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004546done_req:
4547 if (nxt) {
4548 req = nxt;
4549 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004550
4551 if (req->flags & REQ_F_FORCE_ASYNC)
4552 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004553 goto again;
4554 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004555}
4556
Jens Axboe3529d8c2019-12-19 18:24:38 -07004557static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004558{
4559 int ret;
4560
Jens Axboe3529d8c2019-12-19 18:24:38 -07004561 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004562 if (ret) {
4563 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004564fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004565 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004566 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004567 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004568 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004569 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004570 ret = io_req_defer_prep(req, sqe);
4571 if (unlikely(ret < 0))
4572 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004573 /*
4574 * Never try inline submit of IOSQE_ASYNC is set, go straight
4575 * to async execution.
4576 */
4577 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4578 io_queue_async_work(req);
4579 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004580 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004581 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004582}
4583
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004584static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004585{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004586 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004587 io_cqring_add_event(req, -ECANCELED);
4588 io_double_put_req(req);
4589 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004590 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004591}
4592
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004593#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004594 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004595
Jens Axboe3529d8c2019-12-19 18:24:38 -07004596static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4597 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004598{
Jackie Liua197f662019-11-08 08:09:12 -07004599 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004600 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004601 int ret;
4602
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004603 sqe_flags = READ_ONCE(sqe->flags);
4604
Jens Axboe9e645e112019-05-10 16:07:28 -06004605 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004606 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004607 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004608 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004609 }
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004610 /* same numerical values with corresponding REQ_F_*, safe to copy */
4611 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4612 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004613
Jens Axboe3529d8c2019-12-19 18:24:38 -07004614 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004615 if (unlikely(ret)) {
4616err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004617 io_cqring_add_event(req, ret);
4618 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004619 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004620 }
4621
Jens Axboe9e645e112019-05-10 16:07:28 -06004622 /*
4623 * If we already have a head request, queue this one for async
4624 * submittal once the head completes. If we don't have a head but
4625 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4626 * submitted sync once the chain is complete. If none of those
4627 * conditions are true (normal request), then just queue it.
4628 */
4629 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004630 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004631
Pavel Begunkov711be032020-01-17 03:57:59 +03004632 if (sqe_flags & IOSQE_IO_DRAIN) {
4633 head->flags |= REQ_F_IO_DRAIN;
4634 ctx->drain_next = 1;
4635 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004636 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004637 ret = -EAGAIN;
4638 goto err_req;
4639 }
4640
Jens Axboe3529d8c2019-12-19 18:24:38 -07004641 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004642 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004643 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004644 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004645 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004646 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004647 trace_io_uring_link(ctx, req, head);
4648 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004649
4650 /* last request of a link, enqueue the link */
4651 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4652 io_queue_link_head(head);
4653 *link = NULL;
4654 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004655 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004656 if (unlikely(ctx->drain_next)) {
4657 req->flags |= REQ_F_IO_DRAIN;
4658 req->ctx->drain_next = 0;
4659 }
4660 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4661 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004662 INIT_LIST_HEAD(&req->link_list);
4663 ret = io_req_defer_prep(req, sqe);
4664 if (ret)
4665 req->flags |= REQ_F_FAIL_LINK;
4666 *link = req;
4667 } else {
4668 io_queue_sqe(req, sqe);
4669 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004670 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004671
4672 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004673}
4674
Jens Axboe9a56a232019-01-09 09:06:50 -07004675/*
4676 * Batched submission is done, ensure local IO is flushed out.
4677 */
4678static void io_submit_state_end(struct io_submit_state *state)
4679{
4680 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004681 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004682 if (state->free_reqs)
4683 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4684 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004685}
4686
4687/*
4688 * Start submission side cache.
4689 */
4690static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004691 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004692{
4693 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004694 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004695 state->file = NULL;
4696 state->ios_left = max_ios;
4697}
4698
Jens Axboe2b188cc2019-01-07 10:46:33 -07004699static void io_commit_sqring(struct io_ring_ctx *ctx)
4700{
Hristo Venev75b28af2019-08-26 17:23:46 +00004701 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004702
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004703 /*
4704 * Ensure any loads from the SQEs are done at this point,
4705 * since once we write the new head, the application could
4706 * write new data to them.
4707 */
4708 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004709}
4710
4711/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004712 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004713 * that is mapped by userspace. This means that care needs to be taken to
4714 * ensure that reads are stable, as we cannot rely on userspace always
4715 * being a good citizen. If members of the sqe are validated and then later
4716 * used, it's important that those reads are done through READ_ONCE() to
4717 * prevent a re-load down the line.
4718 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004719static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4720 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004721{
Hristo Venev75b28af2019-08-26 17:23:46 +00004722 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004723 unsigned head;
4724
4725 /*
4726 * The cached sq head (or cq tail) serves two purposes:
4727 *
4728 * 1) allows us to batch the cost of updating the user visible
4729 * head updates.
4730 * 2) allows the kernel side to track the head on its own, even
4731 * though the application is the one updating it.
4732 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004733 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004734 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004735 /*
4736 * All io need record the previous position, if LINK vs DARIN,
4737 * it can be used to mark the position of the first IO in the
4738 * link list.
4739 */
4740 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004741 *sqe_ptr = &ctx->sq_sqes[head];
4742 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4743 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004744 ctx->cached_sq_head++;
4745 return true;
4746 }
4747
4748 /* drop invalid entries */
4749 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004750 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004751 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004752 return false;
4753}
4754
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004755static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004756 struct file *ring_file, int ring_fd,
4757 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004758{
4759 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004760 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004761 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004762 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004763
Jens Axboec4a2ed72019-11-21 21:01:26 -07004764 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004765 if (test_bit(0, &ctx->sq_check_overflow)) {
4766 if (!list_empty(&ctx->cq_overflow_list) &&
4767 !io_cqring_overflow_flush(ctx, false))
4768 return -EBUSY;
4769 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004770
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004771 /* make sure SQ entry isn't read before tail */
4772 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004773
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004774 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4775 return -EAGAIN;
4776
Jens Axboe6c271ce2019-01-10 11:22:30 -07004777 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004778 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004779 statep = &state;
4780 }
4781
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004782 ctx->ring_fd = ring_fd;
4783 ctx->ring_file = ring_file;
4784
Jens Axboe6c271ce2019-01-10 11:22:30 -07004785 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004786 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004787 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004788
Pavel Begunkov196be952019-11-07 01:41:06 +03004789 req = io_get_req(ctx, statep);
4790 if (unlikely(!req)) {
4791 if (!submitted)
4792 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004793 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004794 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004795 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004796 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004797 break;
4798 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004799
Jens Axboed3656342019-12-18 09:50:26 -07004800 /* will complete beyond this point, count as submitted */
4801 submitted++;
4802
4803 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4804 io_cqring_add_event(req, -EINVAL);
4805 io_double_put_req(req);
4806 break;
4807 }
4808
4809 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004810 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4811 if (!mm_fault) {
4812 use_mm(ctx->sqo_mm);
4813 *mm = ctx->sqo_mm;
4814 }
4815 }
4816
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004817 req->has_user = *mm != NULL;
4818 req->in_async = async;
4819 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07004820 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
4821 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004822 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004823 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004824 }
4825
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004826 if (submitted != nr)
4827 percpu_ref_put_many(&ctx->refs, nr - submitted);
Jens Axboe9e645e112019-05-10 16:07:28 -06004828 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004829 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004830 if (statep)
4831 io_submit_state_end(&state);
4832
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004833 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4834 io_commit_sqring(ctx);
4835
Jens Axboe6c271ce2019-01-10 11:22:30 -07004836 return submitted;
4837}
4838
4839static int io_sq_thread(void *data)
4840{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004841 struct io_ring_ctx *ctx = data;
4842 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004843 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004844 mm_segment_t old_fs;
4845 DEFINE_WAIT(wait);
4846 unsigned inflight;
4847 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004848 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004849
Jens Axboe206aefd2019-11-07 18:27:42 -07004850 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004851
Jens Axboe6c271ce2019-01-10 11:22:30 -07004852 old_fs = get_fs();
4853 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004854 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004855
Jens Axboec1edbf52019-11-10 16:56:04 -07004856 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004857 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004858 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004859
4860 if (inflight) {
4861 unsigned nr_events = 0;
4862
4863 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004864 /*
4865 * inflight is the count of the maximum possible
4866 * entries we submitted, but it can be smaller
4867 * if we dropped some of them. If we don't have
4868 * poll entries available, then we know that we
4869 * have nothing left to poll for. Reset the
4870 * inflight count to zero in that case.
4871 */
4872 mutex_lock(&ctx->uring_lock);
4873 if (!list_empty(&ctx->poll_list))
4874 __io_iopoll_check(ctx, &nr_events, 0);
4875 else
4876 inflight = 0;
4877 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004878 } else {
4879 /*
4880 * Normal IO, just pretend everything completed.
4881 * We don't have to poll completions for that.
4882 */
4883 nr_events = inflight;
4884 }
4885
4886 inflight -= nr_events;
4887 if (!inflight)
4888 timeout = jiffies + ctx->sq_thread_idle;
4889 }
4890
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004891 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004892
4893 /*
4894 * If submit got -EBUSY, flag us as needing the application
4895 * to enter the kernel to reap and flush events.
4896 */
4897 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004898 /*
4899 * We're polling. If we're within the defined idle
4900 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004901 * to sleep. The exception is if we got EBUSY doing
4902 * more IO, we should wait for the application to
4903 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004904 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004905 if (inflight ||
4906 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004907 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004908 continue;
4909 }
4910
4911 /*
4912 * Drop cur_mm before scheduling, we can't hold it for
4913 * long periods (or over schedule()). Do this before
4914 * adding ourselves to the waitqueue, as the unuse/drop
4915 * may sleep.
4916 */
4917 if (cur_mm) {
4918 unuse_mm(cur_mm);
4919 mmput(cur_mm);
4920 cur_mm = NULL;
4921 }
4922
4923 prepare_to_wait(&ctx->sqo_wait, &wait,
4924 TASK_INTERRUPTIBLE);
4925
4926 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004927 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004928 /* make sure to read SQ tail after writing flags */
4929 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004930
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004931 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004932 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004933 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004934 finish_wait(&ctx->sqo_wait, &wait);
4935 break;
4936 }
4937 if (signal_pending(current))
4938 flush_signals(current);
4939 schedule();
4940 finish_wait(&ctx->sqo_wait, &wait);
4941
Hristo Venev75b28af2019-08-26 17:23:46 +00004942 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004943 continue;
4944 }
4945 finish_wait(&ctx->sqo_wait, &wait);
4946
Hristo Venev75b28af2019-08-26 17:23:46 +00004947 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004948 }
4949
Jens Axboe8a4955f2019-12-09 14:52:35 -07004950 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004951 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004952 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004953 if (ret > 0)
4954 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004955 }
4956
4957 set_fs(old_fs);
4958 if (cur_mm) {
4959 unuse_mm(cur_mm);
4960 mmput(cur_mm);
4961 }
Jens Axboe181e4482019-11-25 08:52:30 -07004962 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004963
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004964 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004965
Jens Axboe6c271ce2019-01-10 11:22:30 -07004966 return 0;
4967}
4968
Jens Axboebda52162019-09-24 13:47:15 -06004969struct io_wait_queue {
4970 struct wait_queue_entry wq;
4971 struct io_ring_ctx *ctx;
4972 unsigned to_wait;
4973 unsigned nr_timeouts;
4974};
4975
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004976static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004977{
4978 struct io_ring_ctx *ctx = iowq->ctx;
4979
4980 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004981 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004982 * started waiting. For timeouts, we always want to return to userspace,
4983 * regardless of event count.
4984 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004985 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004986 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4987}
4988
4989static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4990 int wake_flags, void *key)
4991{
4992 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4993 wq);
4994
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004995 /* use noflush == true, as we can't safely rely on locking context */
4996 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004997 return -1;
4998
4999 return autoremove_wake_function(curr, mode, wake_flags, key);
5000}
5001
Jens Axboe2b188cc2019-01-07 10:46:33 -07005002/*
5003 * Wait until events become available, if we don't already have some. The
5004 * application must reap them itself, as they reside on the shared cq ring.
5005 */
5006static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5007 const sigset_t __user *sig, size_t sigsz)
5008{
Jens Axboebda52162019-09-24 13:47:15 -06005009 struct io_wait_queue iowq = {
5010 .wq = {
5011 .private = current,
5012 .func = io_wake_function,
5013 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5014 },
5015 .ctx = ctx,
5016 .to_wait = min_events,
5017 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005018 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005019 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005020
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005021 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005022 return 0;
5023
5024 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005025#ifdef CONFIG_COMPAT
5026 if (in_compat_syscall())
5027 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005028 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005029 else
5030#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005031 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005032
Jens Axboe2b188cc2019-01-07 10:46:33 -07005033 if (ret)
5034 return ret;
5035 }
5036
Jens Axboebda52162019-09-24 13:47:15 -06005037 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005038 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005039 do {
5040 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5041 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005042 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005043 break;
5044 schedule();
5045 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005046 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005047 break;
5048 }
5049 } while (1);
5050 finish_wait(&ctx->wait, &iowq.wq);
5051
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005052 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005053
Hristo Venev75b28af2019-08-26 17:23:46 +00005054 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005055}
5056
Jens Axboe6b063142019-01-10 22:13:58 -07005057static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5058{
5059#if defined(CONFIG_UNIX)
5060 if (ctx->ring_sock) {
5061 struct sock *sock = ctx->ring_sock->sk;
5062 struct sk_buff *skb;
5063
5064 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5065 kfree_skb(skb);
5066 }
5067#else
5068 int i;
5069
Jens Axboe65e19f52019-10-26 07:20:21 -06005070 for (i = 0; i < ctx->nr_user_files; i++) {
5071 struct file *file;
5072
5073 file = io_file_from_index(ctx, i);
5074 if (file)
5075 fput(file);
5076 }
Jens Axboe6b063142019-01-10 22:13:58 -07005077#endif
5078}
5079
Jens Axboe05f3fb32019-12-09 11:22:50 -07005080static void io_file_ref_kill(struct percpu_ref *ref)
5081{
5082 struct fixed_file_data *data;
5083
5084 data = container_of(ref, struct fixed_file_data, refs);
5085 complete(&data->done);
5086}
5087
Jens Axboe6b063142019-01-10 22:13:58 -07005088static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5089{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005090 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005091 unsigned nr_tables, i;
5092
Jens Axboe05f3fb32019-12-09 11:22:50 -07005093 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005094 return -ENXIO;
5095
Jens Axboe05f3fb32019-12-09 11:22:50 -07005096 /* protect against inflight atomic switch, which drops the ref */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005097 percpu_ref_get(&data->refs);
Jens Axboee46a7952020-01-17 11:15:34 -07005098 /* wait for existing switches */
5099 flush_work(&data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005100 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5101 wait_for_completion(&data->done);
5102 percpu_ref_put(&data->refs);
Jens Axboee46a7952020-01-17 11:15:34 -07005103 /* flush potential new switch */
5104 flush_work(&data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005105 percpu_ref_exit(&data->refs);
5106
Jens Axboe6b063142019-01-10 22:13:58 -07005107 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005108 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5109 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005110 kfree(data->table[i].files);
5111 kfree(data->table);
5112 kfree(data);
5113 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005114 ctx->nr_user_files = 0;
5115 return 0;
5116}
5117
Jens Axboe6c271ce2019-01-10 11:22:30 -07005118static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5119{
5120 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005121 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005122 /*
5123 * The park is a bit of a work-around, without it we get
5124 * warning spews on shutdown with SQPOLL set and affinity
5125 * set to a single CPU.
5126 */
Jens Axboe06058632019-04-13 09:26:03 -06005127 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005128 kthread_stop(ctx->sqo_thread);
5129 ctx->sqo_thread = NULL;
5130 }
5131}
5132
Jens Axboe6b063142019-01-10 22:13:58 -07005133static void io_finish_async(struct io_ring_ctx *ctx)
5134{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005135 io_sq_thread_stop(ctx);
5136
Jens Axboe561fb042019-10-24 07:25:42 -06005137 if (ctx->io_wq) {
5138 io_wq_destroy(ctx->io_wq);
5139 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005140 }
5141}
5142
5143#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005144/*
5145 * Ensure the UNIX gc is aware of our file set, so we are certain that
5146 * the io_uring can be safely unregistered on process exit, even if we have
5147 * loops in the file referencing.
5148 */
5149static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5150{
5151 struct sock *sk = ctx->ring_sock->sk;
5152 struct scm_fp_list *fpl;
5153 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005154 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005155
5156 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5157 unsigned long inflight = ctx->user->unix_inflight + nr;
5158
5159 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5160 return -EMFILE;
5161 }
5162
5163 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5164 if (!fpl)
5165 return -ENOMEM;
5166
5167 skb = alloc_skb(0, GFP_KERNEL);
5168 if (!skb) {
5169 kfree(fpl);
5170 return -ENOMEM;
5171 }
5172
5173 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005174
Jens Axboe08a45172019-10-03 08:11:03 -06005175 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005176 fpl->user = get_uid(ctx->user);
5177 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005178 struct file *file = io_file_from_index(ctx, i + offset);
5179
5180 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005181 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005182 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005183 unix_inflight(fpl->user, fpl->fp[nr_files]);
5184 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005185 }
5186
Jens Axboe08a45172019-10-03 08:11:03 -06005187 if (nr_files) {
5188 fpl->max = SCM_MAX_FD;
5189 fpl->count = nr_files;
5190 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005191 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005192 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5193 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005194
Jens Axboe08a45172019-10-03 08:11:03 -06005195 for (i = 0; i < nr_files; i++)
5196 fput(fpl->fp[i]);
5197 } else {
5198 kfree_skb(skb);
5199 kfree(fpl);
5200 }
Jens Axboe6b063142019-01-10 22:13:58 -07005201
5202 return 0;
5203}
5204
5205/*
5206 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5207 * causes regular reference counting to break down. We rely on the UNIX
5208 * garbage collection to take care of this problem for us.
5209 */
5210static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5211{
5212 unsigned left, total;
5213 int ret = 0;
5214
5215 total = 0;
5216 left = ctx->nr_user_files;
5217 while (left) {
5218 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005219
5220 ret = __io_sqe_files_scm(ctx, this_files, total);
5221 if (ret)
5222 break;
5223 left -= this_files;
5224 total += this_files;
5225 }
5226
5227 if (!ret)
5228 return 0;
5229
5230 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005231 struct file *file = io_file_from_index(ctx, total);
5232
5233 if (file)
5234 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005235 total++;
5236 }
5237
5238 return ret;
5239}
5240#else
5241static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5242{
5243 return 0;
5244}
5245#endif
5246
Jens Axboe65e19f52019-10-26 07:20:21 -06005247static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5248 unsigned nr_files)
5249{
5250 int i;
5251
5252 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005253 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005254 unsigned this_files;
5255
5256 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5257 table->files = kcalloc(this_files, sizeof(struct file *),
5258 GFP_KERNEL);
5259 if (!table->files)
5260 break;
5261 nr_files -= this_files;
5262 }
5263
5264 if (i == nr_tables)
5265 return 0;
5266
5267 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005268 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005269 kfree(table->files);
5270 }
5271 return 1;
5272}
5273
Jens Axboe05f3fb32019-12-09 11:22:50 -07005274static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005275{
5276#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005277 struct sock *sock = ctx->ring_sock->sk;
5278 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5279 struct sk_buff *skb;
5280 int i;
5281
5282 __skb_queue_head_init(&list);
5283
5284 /*
5285 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5286 * remove this entry and rearrange the file array.
5287 */
5288 skb = skb_dequeue(head);
5289 while (skb) {
5290 struct scm_fp_list *fp;
5291
5292 fp = UNIXCB(skb).fp;
5293 for (i = 0; i < fp->count; i++) {
5294 int left;
5295
5296 if (fp->fp[i] != file)
5297 continue;
5298
5299 unix_notinflight(fp->user, fp->fp[i]);
5300 left = fp->count - 1 - i;
5301 if (left) {
5302 memmove(&fp->fp[i], &fp->fp[i + 1],
5303 left * sizeof(struct file *));
5304 }
5305 fp->count--;
5306 if (!fp->count) {
5307 kfree_skb(skb);
5308 skb = NULL;
5309 } else {
5310 __skb_queue_tail(&list, skb);
5311 }
5312 fput(file);
5313 file = NULL;
5314 break;
5315 }
5316
5317 if (!file)
5318 break;
5319
5320 __skb_queue_tail(&list, skb);
5321
5322 skb = skb_dequeue(head);
5323 }
5324
5325 if (skb_peek(&list)) {
5326 spin_lock_irq(&head->lock);
5327 while ((skb = __skb_dequeue(&list)) != NULL)
5328 __skb_queue_tail(head, skb);
5329 spin_unlock_irq(&head->lock);
5330 }
5331#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005332 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005333#endif
5334}
5335
Jens Axboe05f3fb32019-12-09 11:22:50 -07005336struct io_file_put {
5337 struct llist_node llist;
5338 struct file *file;
5339 struct completion *done;
5340};
5341
5342static void io_ring_file_ref_switch(struct work_struct *work)
5343{
5344 struct io_file_put *pfile, *tmp;
5345 struct fixed_file_data *data;
5346 struct llist_node *node;
5347
5348 data = container_of(work, struct fixed_file_data, ref_work);
5349
5350 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5351 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5352 io_ring_file_put(data->ctx, pfile->file);
5353 if (pfile->done)
5354 complete(pfile->done);
5355 else
5356 kfree(pfile);
5357 }
5358 }
5359
5360 percpu_ref_get(&data->refs);
5361 percpu_ref_switch_to_percpu(&data->refs);
5362}
5363
5364static void io_file_data_ref_zero(struct percpu_ref *ref)
5365{
5366 struct fixed_file_data *data;
5367
5368 data = container_of(ref, struct fixed_file_data, refs);
5369
5370 /* we can't safely switch from inside this context, punt to wq */
5371 queue_work(system_wq, &data->ref_work);
5372}
5373
5374static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5375 unsigned nr_args)
5376{
5377 __s32 __user *fds = (__s32 __user *) arg;
5378 unsigned nr_tables;
5379 struct file *file;
5380 int fd, ret = 0;
5381 unsigned i;
5382
5383 if (ctx->file_data)
5384 return -EBUSY;
5385 if (!nr_args)
5386 return -EINVAL;
5387 if (nr_args > IORING_MAX_FIXED_FILES)
5388 return -EMFILE;
5389
5390 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5391 if (!ctx->file_data)
5392 return -ENOMEM;
5393 ctx->file_data->ctx = ctx;
5394 init_completion(&ctx->file_data->done);
5395
5396 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5397 ctx->file_data->table = kcalloc(nr_tables,
5398 sizeof(struct fixed_file_table),
5399 GFP_KERNEL);
5400 if (!ctx->file_data->table) {
5401 kfree(ctx->file_data);
5402 ctx->file_data = NULL;
5403 return -ENOMEM;
5404 }
5405
5406 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5407 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5408 kfree(ctx->file_data->table);
5409 kfree(ctx->file_data);
5410 ctx->file_data = NULL;
5411 return -ENOMEM;
5412 }
5413 ctx->file_data->put_llist.first = NULL;
5414 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5415
5416 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5417 percpu_ref_exit(&ctx->file_data->refs);
5418 kfree(ctx->file_data->table);
5419 kfree(ctx->file_data);
5420 ctx->file_data = NULL;
5421 return -ENOMEM;
5422 }
5423
5424 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5425 struct fixed_file_table *table;
5426 unsigned index;
5427
5428 ret = -EFAULT;
5429 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5430 break;
5431 /* allow sparse sets */
5432 if (fd == -1) {
5433 ret = 0;
5434 continue;
5435 }
5436
5437 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5438 index = i & IORING_FILE_TABLE_MASK;
5439 file = fget(fd);
5440
5441 ret = -EBADF;
5442 if (!file)
5443 break;
5444
5445 /*
5446 * Don't allow io_uring instances to be registered. If UNIX
5447 * isn't enabled, then this causes a reference cycle and this
5448 * instance can never get freed. If UNIX is enabled we'll
5449 * handle it just fine, but there's still no point in allowing
5450 * a ring fd as it doesn't support regular read/write anyway.
5451 */
5452 if (file->f_op == &io_uring_fops) {
5453 fput(file);
5454 break;
5455 }
5456 ret = 0;
5457 table->files[index] = file;
5458 }
5459
5460 if (ret) {
5461 for (i = 0; i < ctx->nr_user_files; i++) {
5462 file = io_file_from_index(ctx, i);
5463 if (file)
5464 fput(file);
5465 }
5466 for (i = 0; i < nr_tables; i++)
5467 kfree(ctx->file_data->table[i].files);
5468
5469 kfree(ctx->file_data->table);
5470 kfree(ctx->file_data);
5471 ctx->file_data = NULL;
5472 ctx->nr_user_files = 0;
5473 return ret;
5474 }
5475
5476 ret = io_sqe_files_scm(ctx);
5477 if (ret)
5478 io_sqe_files_unregister(ctx);
5479
5480 return ret;
5481}
5482
Jens Axboec3a31e62019-10-03 13:59:56 -06005483static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5484 int index)
5485{
5486#if defined(CONFIG_UNIX)
5487 struct sock *sock = ctx->ring_sock->sk;
5488 struct sk_buff_head *head = &sock->sk_receive_queue;
5489 struct sk_buff *skb;
5490
5491 /*
5492 * See if we can merge this file into an existing skb SCM_RIGHTS
5493 * file set. If there's no room, fall back to allocating a new skb
5494 * and filling it in.
5495 */
5496 spin_lock_irq(&head->lock);
5497 skb = skb_peek(head);
5498 if (skb) {
5499 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5500
5501 if (fpl->count < SCM_MAX_FD) {
5502 __skb_unlink(skb, head);
5503 spin_unlock_irq(&head->lock);
5504 fpl->fp[fpl->count] = get_file(file);
5505 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5506 fpl->count++;
5507 spin_lock_irq(&head->lock);
5508 __skb_queue_head(head, skb);
5509 } else {
5510 skb = NULL;
5511 }
5512 }
5513 spin_unlock_irq(&head->lock);
5514
5515 if (skb) {
5516 fput(file);
5517 return 0;
5518 }
5519
5520 return __io_sqe_files_scm(ctx, 1, index);
5521#else
5522 return 0;
5523#endif
5524}
5525
Jens Axboe05f3fb32019-12-09 11:22:50 -07005526static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005527{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005528 struct fixed_file_data *data;
5529
5530 data = container_of(ref, struct fixed_file_data, refs);
5531 clear_bit(FFD_F_ATOMIC, &data->state);
5532}
5533
5534static bool io_queue_file_removal(struct fixed_file_data *data,
5535 struct file *file)
5536{
5537 struct io_file_put *pfile, pfile_stack;
5538 DECLARE_COMPLETION_ONSTACK(done);
5539
5540 /*
5541 * If we fail allocating the struct we need for doing async reomval
5542 * of this file, just punt to sync and wait for it.
5543 */
5544 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5545 if (!pfile) {
5546 pfile = &pfile_stack;
5547 pfile->done = &done;
5548 }
5549
5550 pfile->file = file;
5551 llist_add(&pfile->llist, &data->put_llist);
5552
5553 if (pfile == &pfile_stack) {
5554 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5555 percpu_ref_put(&data->refs);
5556 percpu_ref_switch_to_atomic(&data->refs,
5557 io_atomic_switch);
5558 }
5559 wait_for_completion(&done);
5560 flush_work(&data->ref_work);
5561 return false;
5562 }
5563
5564 return true;
5565}
5566
5567static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5568 struct io_uring_files_update *up,
5569 unsigned nr_args)
5570{
5571 struct fixed_file_data *data = ctx->file_data;
5572 bool ref_switch = false;
5573 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005574 __s32 __user *fds;
5575 int fd, i, err;
5576 __u32 done;
5577
Jens Axboe05f3fb32019-12-09 11:22:50 -07005578 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005579 return -EOVERFLOW;
5580 if (done > ctx->nr_user_files)
5581 return -EINVAL;
5582
5583 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005584 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005585 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005586 struct fixed_file_table *table;
5587 unsigned index;
5588
Jens Axboec3a31e62019-10-03 13:59:56 -06005589 err = 0;
5590 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5591 err = -EFAULT;
5592 break;
5593 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005594 i = array_index_nospec(up->offset, ctx->nr_user_files);
5595 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005596 index = i & IORING_FILE_TABLE_MASK;
5597 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005598 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005599 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005600 if (io_queue_file_removal(data, file))
5601 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005602 }
5603 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005604 file = fget(fd);
5605 if (!file) {
5606 err = -EBADF;
5607 break;
5608 }
5609 /*
5610 * Don't allow io_uring instances to be registered. If
5611 * UNIX isn't enabled, then this causes a reference
5612 * cycle and this instance can never get freed. If UNIX
5613 * is enabled we'll handle it just fine, but there's
5614 * still no point in allowing a ring fd as it doesn't
5615 * support regular read/write anyway.
5616 */
5617 if (file->f_op == &io_uring_fops) {
5618 fput(file);
5619 err = -EBADF;
5620 break;
5621 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005622 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005623 err = io_sqe_file_register(ctx, file, i);
5624 if (err)
5625 break;
5626 }
5627 nr_args--;
5628 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005629 up->offset++;
5630 }
5631
5632 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5633 percpu_ref_put(&data->refs);
5634 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005635 }
5636
5637 return done ? done : err;
5638}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005639static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5640 unsigned nr_args)
5641{
5642 struct io_uring_files_update up;
5643
5644 if (!ctx->file_data)
5645 return -ENXIO;
5646 if (!nr_args)
5647 return -EINVAL;
5648 if (copy_from_user(&up, arg, sizeof(up)))
5649 return -EFAULT;
5650 if (up.resv)
5651 return -EINVAL;
5652
5653 return __io_sqe_files_update(ctx, &up, nr_args);
5654}
Jens Axboec3a31e62019-10-03 13:59:56 -06005655
Jens Axboe7d723062019-11-12 22:31:31 -07005656static void io_put_work(struct io_wq_work *work)
5657{
5658 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5659
5660 io_put_req(req);
5661}
5662
5663static void io_get_work(struct io_wq_work *work)
5664{
5665 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5666
5667 refcount_inc(&req->refs);
5668}
5669
Jens Axboe6c271ce2019-01-10 11:22:30 -07005670static int io_sq_offload_start(struct io_ring_ctx *ctx,
5671 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005672{
Jens Axboe576a3472019-11-25 08:49:20 -07005673 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005674 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005675 int ret;
5676
Jens Axboe6c271ce2019-01-10 11:22:30 -07005677 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005678 mmgrab(current->mm);
5679 ctx->sqo_mm = current->mm;
5680
Jens Axboe6c271ce2019-01-10 11:22:30 -07005681 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005682 ret = -EPERM;
5683 if (!capable(CAP_SYS_ADMIN))
5684 goto err;
5685
Jens Axboe917257d2019-04-13 09:28:55 -06005686 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5687 if (!ctx->sq_thread_idle)
5688 ctx->sq_thread_idle = HZ;
5689
Jens Axboe6c271ce2019-01-10 11:22:30 -07005690 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005691 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005692
Jens Axboe917257d2019-04-13 09:28:55 -06005693 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005694 if (cpu >= nr_cpu_ids)
5695 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005696 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005697 goto err;
5698
Jens Axboe6c271ce2019-01-10 11:22:30 -07005699 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5700 ctx, cpu,
5701 "io_uring-sq");
5702 } else {
5703 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5704 "io_uring-sq");
5705 }
5706 if (IS_ERR(ctx->sqo_thread)) {
5707 ret = PTR_ERR(ctx->sqo_thread);
5708 ctx->sqo_thread = NULL;
5709 goto err;
5710 }
5711 wake_up_process(ctx->sqo_thread);
5712 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5713 /* Can't have SQ_AFF without SQPOLL */
5714 ret = -EINVAL;
5715 goto err;
5716 }
5717
Jens Axboe576a3472019-11-25 08:49:20 -07005718 data.mm = ctx->sqo_mm;
5719 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005720 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005721 data.get_work = io_get_work;
5722 data.put_work = io_put_work;
5723
Jens Axboe561fb042019-10-24 07:25:42 -06005724 /* Do QD, or 4 * CPUS, whatever is smallest */
5725 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005726 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005727 if (IS_ERR(ctx->io_wq)) {
5728 ret = PTR_ERR(ctx->io_wq);
5729 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005730 goto err;
5731 }
5732
5733 return 0;
5734err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005735 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005736 mmdrop(ctx->sqo_mm);
5737 ctx->sqo_mm = NULL;
5738 return ret;
5739}
5740
5741static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5742{
5743 atomic_long_sub(nr_pages, &user->locked_vm);
5744}
5745
5746static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5747{
5748 unsigned long page_limit, cur_pages, new_pages;
5749
5750 /* Don't allow more pages than we can safely lock */
5751 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5752
5753 do {
5754 cur_pages = atomic_long_read(&user->locked_vm);
5755 new_pages = cur_pages + nr_pages;
5756 if (new_pages > page_limit)
5757 return -ENOMEM;
5758 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5759 new_pages) != cur_pages);
5760
5761 return 0;
5762}
5763
5764static void io_mem_free(void *ptr)
5765{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005766 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005767
Mark Rutland52e04ef2019-04-30 17:30:21 +01005768 if (!ptr)
5769 return;
5770
5771 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005772 if (put_page_testzero(page))
5773 free_compound_page(page);
5774}
5775
5776static void *io_mem_alloc(size_t size)
5777{
5778 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5779 __GFP_NORETRY;
5780
5781 return (void *) __get_free_pages(gfp_flags, get_order(size));
5782}
5783
Hristo Venev75b28af2019-08-26 17:23:46 +00005784static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5785 size_t *sq_offset)
5786{
5787 struct io_rings *rings;
5788 size_t off, sq_array_size;
5789
5790 off = struct_size(rings, cqes, cq_entries);
5791 if (off == SIZE_MAX)
5792 return SIZE_MAX;
5793
5794#ifdef CONFIG_SMP
5795 off = ALIGN(off, SMP_CACHE_BYTES);
5796 if (off == 0)
5797 return SIZE_MAX;
5798#endif
5799
5800 sq_array_size = array_size(sizeof(u32), sq_entries);
5801 if (sq_array_size == SIZE_MAX)
5802 return SIZE_MAX;
5803
5804 if (check_add_overflow(off, sq_array_size, &off))
5805 return SIZE_MAX;
5806
5807 if (sq_offset)
5808 *sq_offset = off;
5809
5810 return off;
5811}
5812
Jens Axboe2b188cc2019-01-07 10:46:33 -07005813static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5814{
Hristo Venev75b28af2019-08-26 17:23:46 +00005815 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005816
Hristo Venev75b28af2019-08-26 17:23:46 +00005817 pages = (size_t)1 << get_order(
5818 rings_size(sq_entries, cq_entries, NULL));
5819 pages += (size_t)1 << get_order(
5820 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005821
Hristo Venev75b28af2019-08-26 17:23:46 +00005822 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005823}
5824
Jens Axboeedafcce2019-01-09 09:16:05 -07005825static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5826{
5827 int i, j;
5828
5829 if (!ctx->user_bufs)
5830 return -ENXIO;
5831
5832 for (i = 0; i < ctx->nr_user_bufs; i++) {
5833 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5834
5835 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005836 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005837
5838 if (ctx->account_mem)
5839 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005840 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005841 imu->nr_bvecs = 0;
5842 }
5843
5844 kfree(ctx->user_bufs);
5845 ctx->user_bufs = NULL;
5846 ctx->nr_user_bufs = 0;
5847 return 0;
5848}
5849
5850static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5851 void __user *arg, unsigned index)
5852{
5853 struct iovec __user *src;
5854
5855#ifdef CONFIG_COMPAT
5856 if (ctx->compat) {
5857 struct compat_iovec __user *ciovs;
5858 struct compat_iovec ciov;
5859
5860 ciovs = (struct compat_iovec __user *) arg;
5861 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5862 return -EFAULT;
5863
Jens Axboed55e5f52019-12-11 16:12:15 -07005864 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005865 dst->iov_len = ciov.iov_len;
5866 return 0;
5867 }
5868#endif
5869 src = (struct iovec __user *) arg;
5870 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5871 return -EFAULT;
5872 return 0;
5873}
5874
5875static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5876 unsigned nr_args)
5877{
5878 struct vm_area_struct **vmas = NULL;
5879 struct page **pages = NULL;
5880 int i, j, got_pages = 0;
5881 int ret = -EINVAL;
5882
5883 if (ctx->user_bufs)
5884 return -EBUSY;
5885 if (!nr_args || nr_args > UIO_MAXIOV)
5886 return -EINVAL;
5887
5888 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5889 GFP_KERNEL);
5890 if (!ctx->user_bufs)
5891 return -ENOMEM;
5892
5893 for (i = 0; i < nr_args; i++) {
5894 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5895 unsigned long off, start, end, ubuf;
5896 int pret, nr_pages;
5897 struct iovec iov;
5898 size_t size;
5899
5900 ret = io_copy_iov(ctx, &iov, arg, i);
5901 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005902 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005903
5904 /*
5905 * Don't impose further limits on the size and buffer
5906 * constraints here, we'll -EINVAL later when IO is
5907 * submitted if they are wrong.
5908 */
5909 ret = -EFAULT;
5910 if (!iov.iov_base || !iov.iov_len)
5911 goto err;
5912
5913 /* arbitrary limit, but we need something */
5914 if (iov.iov_len > SZ_1G)
5915 goto err;
5916
5917 ubuf = (unsigned long) iov.iov_base;
5918 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5919 start = ubuf >> PAGE_SHIFT;
5920 nr_pages = end - start;
5921
5922 if (ctx->account_mem) {
5923 ret = io_account_mem(ctx->user, nr_pages);
5924 if (ret)
5925 goto err;
5926 }
5927
5928 ret = 0;
5929 if (!pages || nr_pages > got_pages) {
5930 kfree(vmas);
5931 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005932 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005933 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005934 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005935 sizeof(struct vm_area_struct *),
5936 GFP_KERNEL);
5937 if (!pages || !vmas) {
5938 ret = -ENOMEM;
5939 if (ctx->account_mem)
5940 io_unaccount_mem(ctx->user, nr_pages);
5941 goto err;
5942 }
5943 got_pages = nr_pages;
5944 }
5945
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005946 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005947 GFP_KERNEL);
5948 ret = -ENOMEM;
5949 if (!imu->bvec) {
5950 if (ctx->account_mem)
5951 io_unaccount_mem(ctx->user, nr_pages);
5952 goto err;
5953 }
5954
5955 ret = 0;
5956 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005957 pret = get_user_pages(ubuf, nr_pages,
5958 FOLL_WRITE | FOLL_LONGTERM,
5959 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005960 if (pret == nr_pages) {
5961 /* don't support file backed memory */
5962 for (j = 0; j < nr_pages; j++) {
5963 struct vm_area_struct *vma = vmas[j];
5964
5965 if (vma->vm_file &&
5966 !is_file_hugepages(vma->vm_file)) {
5967 ret = -EOPNOTSUPP;
5968 break;
5969 }
5970 }
5971 } else {
5972 ret = pret < 0 ? pret : -EFAULT;
5973 }
5974 up_read(&current->mm->mmap_sem);
5975 if (ret) {
5976 /*
5977 * if we did partial map, or found file backed vmas,
5978 * release any pages we did get
5979 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005980 if (pret > 0)
5981 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005982 if (ctx->account_mem)
5983 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005984 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005985 goto err;
5986 }
5987
5988 off = ubuf & ~PAGE_MASK;
5989 size = iov.iov_len;
5990 for (j = 0; j < nr_pages; j++) {
5991 size_t vec_len;
5992
5993 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5994 imu->bvec[j].bv_page = pages[j];
5995 imu->bvec[j].bv_len = vec_len;
5996 imu->bvec[j].bv_offset = off;
5997 off = 0;
5998 size -= vec_len;
5999 }
6000 /* store original address for later verification */
6001 imu->ubuf = ubuf;
6002 imu->len = iov.iov_len;
6003 imu->nr_bvecs = nr_pages;
6004
6005 ctx->nr_user_bufs++;
6006 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006007 kvfree(pages);
6008 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006009 return 0;
6010err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006011 kvfree(pages);
6012 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006013 io_sqe_buffer_unregister(ctx);
6014 return ret;
6015}
6016
Jens Axboe9b402842019-04-11 11:45:41 -06006017static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6018{
6019 __s32 __user *fds = arg;
6020 int fd;
6021
6022 if (ctx->cq_ev_fd)
6023 return -EBUSY;
6024
6025 if (copy_from_user(&fd, fds, sizeof(*fds)))
6026 return -EFAULT;
6027
6028 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6029 if (IS_ERR(ctx->cq_ev_fd)) {
6030 int ret = PTR_ERR(ctx->cq_ev_fd);
6031 ctx->cq_ev_fd = NULL;
6032 return ret;
6033 }
6034
6035 return 0;
6036}
6037
6038static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6039{
6040 if (ctx->cq_ev_fd) {
6041 eventfd_ctx_put(ctx->cq_ev_fd);
6042 ctx->cq_ev_fd = NULL;
6043 return 0;
6044 }
6045
6046 return -ENXIO;
6047}
6048
Jens Axboe2b188cc2019-01-07 10:46:33 -07006049static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6050{
Jens Axboe6b063142019-01-10 22:13:58 -07006051 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006052 if (ctx->sqo_mm)
6053 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006054
6055 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006056 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006057 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006058 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006059
Jens Axboe2b188cc2019-01-07 10:46:33 -07006060#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006061 if (ctx->ring_sock) {
6062 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006063 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006064 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006065#endif
6066
Hristo Venev75b28af2019-08-26 17:23:46 +00006067 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006068 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006069
6070 percpu_ref_exit(&ctx->refs);
6071 if (ctx->account_mem)
6072 io_unaccount_mem(ctx->user,
6073 ring_pages(ctx->sq_entries, ctx->cq_entries));
6074 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006075 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006076 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006077 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006078 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006079 kfree(ctx);
6080}
6081
6082static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6083{
6084 struct io_ring_ctx *ctx = file->private_data;
6085 __poll_t mask = 0;
6086
6087 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006088 /*
6089 * synchronizes with barrier from wq_has_sleeper call in
6090 * io_commit_cqring
6091 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006092 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006093 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6094 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006095 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006096 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006097 mask |= EPOLLIN | EPOLLRDNORM;
6098
6099 return mask;
6100}
6101
6102static int io_uring_fasync(int fd, struct file *file, int on)
6103{
6104 struct io_ring_ctx *ctx = file->private_data;
6105
6106 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6107}
6108
6109static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6110{
6111 mutex_lock(&ctx->uring_lock);
6112 percpu_ref_kill(&ctx->refs);
6113 mutex_unlock(&ctx->uring_lock);
6114
Jens Axboe5262f562019-09-17 12:26:57 -06006115 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006116 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006117
6118 if (ctx->io_wq)
6119 io_wq_cancel_all(ctx->io_wq);
6120
Jens Axboedef596e2019-01-09 08:59:42 -07006121 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006122 /* if we failed setting up the ctx, we might not have any rings */
6123 if (ctx->rings)
6124 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07006125 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006126 io_ring_ctx_free(ctx);
6127}
6128
6129static int io_uring_release(struct inode *inode, struct file *file)
6130{
6131 struct io_ring_ctx *ctx = file->private_data;
6132
6133 file->private_data = NULL;
6134 io_ring_ctx_wait_and_kill(ctx);
6135 return 0;
6136}
6137
Jens Axboefcb323c2019-10-24 12:39:47 -06006138static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6139 struct files_struct *files)
6140{
6141 struct io_kiocb *req;
6142 DEFINE_WAIT(wait);
6143
6144 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006145 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006146
6147 spin_lock_irq(&ctx->inflight_lock);
6148 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006149 if (req->work.files != files)
6150 continue;
6151 /* req is being completed, ignore */
6152 if (!refcount_inc_not_zero(&req->refs))
6153 continue;
6154 cancel_req = req;
6155 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006156 }
Jens Axboe768134d2019-11-10 20:30:53 -07006157 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006158 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006159 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006160 spin_unlock_irq(&ctx->inflight_lock);
6161
Jens Axboe768134d2019-11-10 20:30:53 -07006162 /* We need to keep going until we don't find a matching req */
6163 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006164 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006165
6166 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6167 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006168 schedule();
6169 }
Jens Axboe768134d2019-11-10 20:30:53 -07006170 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006171}
6172
6173static int io_uring_flush(struct file *file, void *data)
6174{
6175 struct io_ring_ctx *ctx = file->private_data;
6176
6177 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006178 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6179 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006180 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006181 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006182 return 0;
6183}
6184
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006185static void *io_uring_validate_mmap_request(struct file *file,
6186 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006187{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006188 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006189 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006190 struct page *page;
6191 void *ptr;
6192
6193 switch (offset) {
6194 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006195 case IORING_OFF_CQ_RING:
6196 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006197 break;
6198 case IORING_OFF_SQES:
6199 ptr = ctx->sq_sqes;
6200 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006201 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006202 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006203 }
6204
6205 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006206 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006207 return ERR_PTR(-EINVAL);
6208
6209 return ptr;
6210}
6211
6212#ifdef CONFIG_MMU
6213
6214static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6215{
6216 size_t sz = vma->vm_end - vma->vm_start;
6217 unsigned long pfn;
6218 void *ptr;
6219
6220 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6221 if (IS_ERR(ptr))
6222 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006223
6224 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6225 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6226}
6227
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006228#else /* !CONFIG_MMU */
6229
6230static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6231{
6232 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6233}
6234
6235static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6236{
6237 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6238}
6239
6240static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6241 unsigned long addr, unsigned long len,
6242 unsigned long pgoff, unsigned long flags)
6243{
6244 void *ptr;
6245
6246 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6247 if (IS_ERR(ptr))
6248 return PTR_ERR(ptr);
6249
6250 return (unsigned long) ptr;
6251}
6252
6253#endif /* !CONFIG_MMU */
6254
Jens Axboe2b188cc2019-01-07 10:46:33 -07006255SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6256 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6257 size_t, sigsz)
6258{
6259 struct io_ring_ctx *ctx;
6260 long ret = -EBADF;
6261 int submitted = 0;
6262 struct fd f;
6263
Jens Axboe6c271ce2019-01-10 11:22:30 -07006264 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006265 return -EINVAL;
6266
6267 f = fdget(fd);
6268 if (!f.file)
6269 return -EBADF;
6270
6271 ret = -EOPNOTSUPP;
6272 if (f.file->f_op != &io_uring_fops)
6273 goto out_fput;
6274
6275 ret = -ENXIO;
6276 ctx = f.file->private_data;
6277 if (!percpu_ref_tryget(&ctx->refs))
6278 goto out_fput;
6279
Jens Axboe6c271ce2019-01-10 11:22:30 -07006280 /*
6281 * For SQ polling, the thread will do all submissions and completions.
6282 * Just return the requested submit count, and wake the thread if
6283 * we were asked to.
6284 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006285 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006286 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006287 if (!list_empty_careful(&ctx->cq_overflow_list))
6288 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006289 if (flags & IORING_ENTER_SQ_WAKEUP)
6290 wake_up(&ctx->sqo_wait);
6291 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006292 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006293 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006294
Jens Axboe44d28272020-01-16 19:00:24 -07006295 if (current->mm != ctx->sqo_mm ||
6296 current_cred() != ctx->creds) {
6297 ret = -EPERM;
6298 goto out;
6299 }
6300
Jens Axboe2b188cc2019-01-07 10:46:33 -07006301 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006302 /* already have mm, so io_submit_sqes() won't try to grab it */
6303 cur_mm = ctx->sqo_mm;
6304 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6305 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006306 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006307
6308 if (submitted != to_submit)
6309 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006310 }
6311 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006312 unsigned nr_events = 0;
6313
Jens Axboe2b188cc2019-01-07 10:46:33 -07006314 min_complete = min(min_complete, ctx->cq_entries);
6315
Jens Axboedef596e2019-01-09 08:59:42 -07006316 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006317 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006318 } else {
6319 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6320 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006321 }
6322
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006323out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006324 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006325out_fput:
6326 fdput(f);
6327 return submitted ? submitted : ret;
6328}
6329
6330static const struct file_operations io_uring_fops = {
6331 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006332 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006333 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006334#ifndef CONFIG_MMU
6335 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6336 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6337#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006338 .poll = io_uring_poll,
6339 .fasync = io_uring_fasync,
6340};
6341
6342static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6343 struct io_uring_params *p)
6344{
Hristo Venev75b28af2019-08-26 17:23:46 +00006345 struct io_rings *rings;
6346 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006347
Hristo Venev75b28af2019-08-26 17:23:46 +00006348 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6349 if (size == SIZE_MAX)
6350 return -EOVERFLOW;
6351
6352 rings = io_mem_alloc(size);
6353 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006354 return -ENOMEM;
6355
Hristo Venev75b28af2019-08-26 17:23:46 +00006356 ctx->rings = rings;
6357 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6358 rings->sq_ring_mask = p->sq_entries - 1;
6359 rings->cq_ring_mask = p->cq_entries - 1;
6360 rings->sq_ring_entries = p->sq_entries;
6361 rings->cq_ring_entries = p->cq_entries;
6362 ctx->sq_mask = rings->sq_ring_mask;
6363 ctx->cq_mask = rings->cq_ring_mask;
6364 ctx->sq_entries = rings->sq_ring_entries;
6365 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006366
6367 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006368 if (size == SIZE_MAX) {
6369 io_mem_free(ctx->rings);
6370 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006371 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006372 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006373
6374 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006375 if (!ctx->sq_sqes) {
6376 io_mem_free(ctx->rings);
6377 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006378 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006379 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006380
Jens Axboe2b188cc2019-01-07 10:46:33 -07006381 return 0;
6382}
6383
6384/*
6385 * Allocate an anonymous fd, this is what constitutes the application
6386 * visible backing of an io_uring instance. The application mmaps this
6387 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6388 * we have to tie this fd to a socket for file garbage collection purposes.
6389 */
6390static int io_uring_get_fd(struct io_ring_ctx *ctx)
6391{
6392 struct file *file;
6393 int ret;
6394
6395#if defined(CONFIG_UNIX)
6396 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6397 &ctx->ring_sock);
6398 if (ret)
6399 return ret;
6400#endif
6401
6402 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6403 if (ret < 0)
6404 goto err;
6405
6406 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6407 O_RDWR | O_CLOEXEC);
6408 if (IS_ERR(file)) {
6409 put_unused_fd(ret);
6410 ret = PTR_ERR(file);
6411 goto err;
6412 }
6413
6414#if defined(CONFIG_UNIX)
6415 ctx->ring_sock->file = file;
6416#endif
6417 fd_install(ret, file);
6418 return ret;
6419err:
6420#if defined(CONFIG_UNIX)
6421 sock_release(ctx->ring_sock);
6422 ctx->ring_sock = NULL;
6423#endif
6424 return ret;
6425}
6426
6427static int io_uring_create(unsigned entries, struct io_uring_params *p)
6428{
6429 struct user_struct *user = NULL;
6430 struct io_ring_ctx *ctx;
6431 bool account_mem;
6432 int ret;
6433
Jens Axboe8110c1a2019-12-28 15:39:54 -07006434 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006435 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006436 if (entries > IORING_MAX_ENTRIES) {
6437 if (!(p->flags & IORING_SETUP_CLAMP))
6438 return -EINVAL;
6439 entries = IORING_MAX_ENTRIES;
6440 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006441
6442 /*
6443 * Use twice as many entries for the CQ ring. It's possible for the
6444 * application to drive a higher depth than the size of the SQ ring,
6445 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006446 * some flexibility in overcommitting a bit. If the application has
6447 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6448 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006449 */
6450 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006451 if (p->flags & IORING_SETUP_CQSIZE) {
6452 /*
6453 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6454 * to a power-of-two, if it isn't already. We do NOT impose
6455 * any cq vs sq ring sizing.
6456 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006457 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006458 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006459 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6460 if (!(p->flags & IORING_SETUP_CLAMP))
6461 return -EINVAL;
6462 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6463 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006464 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6465 } else {
6466 p->cq_entries = 2 * p->sq_entries;
6467 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006468
6469 user = get_uid(current_user());
6470 account_mem = !capable(CAP_IPC_LOCK);
6471
6472 if (account_mem) {
6473 ret = io_account_mem(user,
6474 ring_pages(p->sq_entries, p->cq_entries));
6475 if (ret) {
6476 free_uid(user);
6477 return ret;
6478 }
6479 }
6480
6481 ctx = io_ring_ctx_alloc(p);
6482 if (!ctx) {
6483 if (account_mem)
6484 io_unaccount_mem(user, ring_pages(p->sq_entries,
6485 p->cq_entries));
6486 free_uid(user);
6487 return -ENOMEM;
6488 }
6489 ctx->compat = in_compat_syscall();
6490 ctx->account_mem = account_mem;
6491 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006492 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006493
6494 ret = io_allocate_scq_urings(ctx, p);
6495 if (ret)
6496 goto err;
6497
Jens Axboe6c271ce2019-01-10 11:22:30 -07006498 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006499 if (ret)
6500 goto err;
6501
Jens Axboe2b188cc2019-01-07 10:46:33 -07006502 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006503 p->sq_off.head = offsetof(struct io_rings, sq.head);
6504 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6505 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6506 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6507 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6508 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6509 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006510
6511 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006512 p->cq_off.head = offsetof(struct io_rings, cq.head);
6513 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6514 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6515 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6516 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6517 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006518
Jens Axboe044c1ab2019-10-28 09:15:33 -06006519 /*
6520 * Install ring fd as the very last thing, so we don't risk someone
6521 * having closed it before we finish setup
6522 */
6523 ret = io_uring_get_fd(ctx);
6524 if (ret < 0)
6525 goto err;
6526
Jens Axboeda8c9692019-12-02 18:51:26 -07006527 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboeba042912019-12-25 16:33:42 -07006528 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006529 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006530 return ret;
6531err:
6532 io_ring_ctx_wait_and_kill(ctx);
6533 return ret;
6534}
6535
6536/*
6537 * Sets up an aio uring context, and returns the fd. Applications asks for a
6538 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6539 * params structure passed in.
6540 */
6541static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6542{
6543 struct io_uring_params p;
6544 long ret;
6545 int i;
6546
6547 if (copy_from_user(&p, params, sizeof(p)))
6548 return -EFAULT;
6549 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6550 if (p.resv[i])
6551 return -EINVAL;
6552 }
6553
Jens Axboe6c271ce2019-01-10 11:22:30 -07006554 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006555 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
6556 IORING_SETUP_CLAMP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006557 return -EINVAL;
6558
6559 ret = io_uring_create(entries, &p);
6560 if (ret < 0)
6561 return ret;
6562
6563 if (copy_to_user(params, &p, sizeof(p)))
6564 return -EFAULT;
6565
6566 return ret;
6567}
6568
6569SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6570 struct io_uring_params __user *, params)
6571{
6572 return io_uring_setup(entries, params);
6573}
6574
Jens Axboe66f4af92020-01-16 15:36:52 -07006575static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6576{
6577 struct io_uring_probe *p;
6578 size_t size;
6579 int i, ret;
6580
6581 size = struct_size(p, ops, nr_args);
6582 if (size == SIZE_MAX)
6583 return -EOVERFLOW;
6584 p = kzalloc(size, GFP_KERNEL);
6585 if (!p)
6586 return -ENOMEM;
6587
6588 ret = -EFAULT;
6589 if (copy_from_user(p, arg, size))
6590 goto out;
6591 ret = -EINVAL;
6592 if (memchr_inv(p, 0, size))
6593 goto out;
6594
6595 p->last_op = IORING_OP_LAST - 1;
6596 if (nr_args > IORING_OP_LAST)
6597 nr_args = IORING_OP_LAST;
6598
6599 for (i = 0; i < nr_args; i++) {
6600 p->ops[i].op = i;
6601 if (!io_op_defs[i].not_supported)
6602 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6603 }
6604 p->ops_len = i;
6605
6606 ret = 0;
6607 if (copy_to_user(arg, p, size))
6608 ret = -EFAULT;
6609out:
6610 kfree(p);
6611 return ret;
6612}
6613
Jens Axboeedafcce2019-01-09 09:16:05 -07006614static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6615 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006616 __releases(ctx->uring_lock)
6617 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006618{
6619 int ret;
6620
Jens Axboe35fa71a2019-04-22 10:23:23 -06006621 /*
6622 * We're inside the ring mutex, if the ref is already dying, then
6623 * someone else killed the ctx or is already going through
6624 * io_uring_register().
6625 */
6626 if (percpu_ref_is_dying(&ctx->refs))
6627 return -ENXIO;
6628
Jens Axboe05f3fb32019-12-09 11:22:50 -07006629 if (opcode != IORING_UNREGISTER_FILES &&
Jens Axboe66f4af92020-01-16 15:36:52 -07006630 opcode != IORING_REGISTER_FILES_UPDATE &&
6631 opcode != IORING_REGISTER_PROBE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006632 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006633
Jens Axboe05f3fb32019-12-09 11:22:50 -07006634 /*
6635 * Drop uring mutex before waiting for references to exit. If
6636 * another thread is currently inside io_uring_enter() it might
6637 * need to grab the uring_lock to make progress. If we hold it
6638 * here across the drain wait, then we can deadlock. It's safe
6639 * to drop the mutex here, since no new references will come in
6640 * after we've killed the percpu ref.
6641 */
6642 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006643 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006644 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006645 if (ret) {
6646 percpu_ref_resurrect(&ctx->refs);
6647 ret = -EINTR;
6648 goto out;
6649 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006650 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006651
6652 switch (opcode) {
6653 case IORING_REGISTER_BUFFERS:
6654 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6655 break;
6656 case IORING_UNREGISTER_BUFFERS:
6657 ret = -EINVAL;
6658 if (arg || nr_args)
6659 break;
6660 ret = io_sqe_buffer_unregister(ctx);
6661 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006662 case IORING_REGISTER_FILES:
6663 ret = io_sqe_files_register(ctx, arg, nr_args);
6664 break;
6665 case IORING_UNREGISTER_FILES:
6666 ret = -EINVAL;
6667 if (arg || nr_args)
6668 break;
6669 ret = io_sqe_files_unregister(ctx);
6670 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006671 case IORING_REGISTER_FILES_UPDATE:
6672 ret = io_sqe_files_update(ctx, arg, nr_args);
6673 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006674 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006675 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006676 ret = -EINVAL;
6677 if (nr_args != 1)
6678 break;
6679 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006680 if (ret)
6681 break;
6682 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6683 ctx->eventfd_async = 1;
6684 else
6685 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006686 break;
6687 case IORING_UNREGISTER_EVENTFD:
6688 ret = -EINVAL;
6689 if (arg || nr_args)
6690 break;
6691 ret = io_eventfd_unregister(ctx);
6692 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07006693 case IORING_REGISTER_PROBE:
6694 ret = -EINVAL;
6695 if (!arg || nr_args > 256)
6696 break;
6697 ret = io_probe(ctx, arg, nr_args);
6698 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006699 default:
6700 ret = -EINVAL;
6701 break;
6702 }
6703
Jens Axboe05f3fb32019-12-09 11:22:50 -07006704
6705 if (opcode != IORING_UNREGISTER_FILES &&
Jens Axboe66f4af92020-01-16 15:36:52 -07006706 opcode != IORING_REGISTER_FILES_UPDATE &&
6707 opcode != IORING_REGISTER_PROBE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006708 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006709 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07006710out:
6711 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006712 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006713 return ret;
6714}
6715
6716SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6717 void __user *, arg, unsigned int, nr_args)
6718{
6719 struct io_ring_ctx *ctx;
6720 long ret = -EBADF;
6721 struct fd f;
6722
6723 f = fdget(fd);
6724 if (!f.file)
6725 return -EBADF;
6726
6727 ret = -EOPNOTSUPP;
6728 if (f.file->f_op != &io_uring_fops)
6729 goto out_fput;
6730
6731 ctx = f.file->private_data;
6732
6733 mutex_lock(&ctx->uring_lock);
6734 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6735 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006736 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6737 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006738out_fput:
6739 fdput(f);
6740 return ret;
6741}
6742
Jens Axboe2b188cc2019-01-07 10:46:33 -07006743static int __init io_uring_init(void)
6744{
Jens Axboed3656342019-12-18 09:50:26 -07006745 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006746 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6747 return 0;
6748};
6749__initcall(io_uring_init);