blob: 9f73586dcfb87787907e2fccfcba539698838154 [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 Axboe74566df2020-01-13 19:23:24 -07002115 if (req->opcode == IORING_OP_READ_FIXED ||
2116 req->opcode == IORING_OP_WRITE_FIXED)
2117 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002118 if (!req->io && io_alloc_async_ctx(req))
2119 return -ENOMEM;
2120
2121 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2122 req->work.func = io_rw_async;
2123 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002124}
2125
Jens Axboe3529d8c2019-12-19 18:24:38 -07002126static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2127 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002128{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002129 struct io_async_ctx *io;
2130 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002131 ssize_t ret;
2132
Jens Axboe3529d8c2019-12-19 18:24:38 -07002133 ret = io_prep_rw(req, sqe, force_nonblock);
2134 if (ret)
2135 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002136
Jens Axboe3529d8c2019-12-19 18:24:38 -07002137 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2138 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002139
Jens Axboe3529d8c2019-12-19 18:24:38 -07002140 if (!req->io)
2141 return 0;
2142
2143 io = req->io;
2144 io->rw.iov = io->rw.fast_iov;
2145 req->io = NULL;
2146 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2147 req->io = io;
2148 if (ret < 0)
2149 return ret;
2150
2151 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2152 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002153}
2154
Pavel Begunkov267bc902019-11-07 01:41:08 +03002155static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002156 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002157{
2158 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002159 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002160 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002161 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002162 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002163
Jens Axboe3529d8c2019-12-19 18:24:38 -07002164 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002165 if (ret < 0)
2166 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002167
Jens Axboefd6c2e42019-12-18 12:19:41 -07002168 /* Ensure we clear previously set non-block flag */
2169 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002170 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002171
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002172 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002173 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002174 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002175 req->result = io_size;
2176
2177 /*
2178 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2179 * we know to async punt it even if it was opened O_NONBLOCK
2180 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002181 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002182 req->flags |= REQ_F_MUST_PUNT;
2183 goto copy_iov;
2184 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002185
Jens Axboe31b51512019-01-18 22:56:34 -07002186 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002187 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002188 if (!ret) {
2189 ssize_t ret2;
2190
Jens Axboe9adbd452019-12-20 08:45:55 -07002191 if (req->file->f_op->read_iter)
2192 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002193 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002194 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002195
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002196 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002197 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002198 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002199 } else {
2200copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002201 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002202 inline_vecs, &iter);
2203 if (ret)
2204 goto out_free;
2205 return -EAGAIN;
2206 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002207 }
Jens Axboef67676d2019-12-02 11:03:47 -07002208out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002209 if (!io_wq_current_is_worker())
2210 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002211 return ret;
2212}
2213
Jens Axboe3529d8c2019-12-19 18:24:38 -07002214static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2215 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002216{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002217 struct io_async_ctx *io;
2218 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002219 ssize_t ret;
2220
Jens Axboe3529d8c2019-12-19 18:24:38 -07002221 ret = io_prep_rw(req, sqe, force_nonblock);
2222 if (ret)
2223 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002224
Jens Axboe3529d8c2019-12-19 18:24:38 -07002225 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2226 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002227
Jens Axboe3529d8c2019-12-19 18:24:38 -07002228 if (!req->io)
2229 return 0;
2230
2231 io = req->io;
2232 io->rw.iov = io->rw.fast_iov;
2233 req->io = NULL;
2234 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2235 req->io = io;
2236 if (ret < 0)
2237 return ret;
2238
2239 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2240 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002241}
2242
Pavel Begunkov267bc902019-11-07 01:41:08 +03002243static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002244 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002245{
2246 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002247 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002248 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002249 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002250 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002251
Jens Axboe3529d8c2019-12-19 18:24:38 -07002252 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002253 if (ret < 0)
2254 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002255
Jens Axboefd6c2e42019-12-18 12:19:41 -07002256 /* Ensure we clear previously set non-block flag */
2257 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002258 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002259
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002260 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002261 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002262 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002263 req->result = io_size;
2264
2265 /*
2266 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2267 * we know to async punt it even if it was opened O_NONBLOCK
2268 */
2269 if (force_nonblock && !io_file_supports_async(req->file)) {
2270 req->flags |= REQ_F_MUST_PUNT;
2271 goto copy_iov;
2272 }
2273
Jens Axboe10d59342019-12-09 20:16:22 -07002274 /* file path doesn't support NOWAIT for non-direct_IO */
2275 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2276 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002277 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002278
Jens Axboe31b51512019-01-18 22:56:34 -07002279 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002280 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002281 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002282 ssize_t ret2;
2283
Jens Axboe2b188cc2019-01-07 10:46:33 -07002284 /*
2285 * Open-code file_start_write here to grab freeze protection,
2286 * which will be released by another thread in
2287 * io_complete_rw(). Fool lockdep by telling it the lock got
2288 * released so that it doesn't complain about the held lock when
2289 * we return to userspace.
2290 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002291 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002292 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002293 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002294 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002295 SB_FREEZE_WRITE);
2296 }
2297 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002298
Jens Axboe9adbd452019-12-20 08:45:55 -07002299 if (req->file->f_op->write_iter)
2300 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002301 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002302 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002303 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002304 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002305 } else {
2306copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002307 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002308 inline_vecs, &iter);
2309 if (ret)
2310 goto out_free;
2311 return -EAGAIN;
2312 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002313 }
Jens Axboe31b51512019-01-18 22:56:34 -07002314out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002315 if (!io_wq_current_is_worker())
2316 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002317 return ret;
2318}
2319
2320/*
2321 * IORING_OP_NOP just posts a completion event, nothing else.
2322 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002323static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002324{
2325 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002326
Jens Axboedef596e2019-01-09 08:59:42 -07002327 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2328 return -EINVAL;
2329
Jens Axboe78e19bb2019-11-06 15:21:34 -07002330 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002331 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002332 return 0;
2333}
2334
Jens Axboe3529d8c2019-12-19 18:24:38 -07002335static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002336{
Jens Axboe6b063142019-01-10 22:13:58 -07002337 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002338
Jens Axboe09bb8392019-03-13 12:39:28 -06002339 if (!req->file)
2340 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002341
Jens Axboe6b063142019-01-10 22:13:58 -07002342 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002343 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002344 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002345 return -EINVAL;
2346
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002347 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2348 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2349 return -EINVAL;
2350
2351 req->sync.off = READ_ONCE(sqe->off);
2352 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002353 return 0;
2354}
2355
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002356static bool io_req_cancelled(struct io_kiocb *req)
2357{
2358 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2359 req_set_fail_links(req);
2360 io_cqring_add_event(req, -ECANCELED);
2361 io_put_req(req);
2362 return true;
2363 }
2364
2365 return false;
2366}
2367
Jens Axboe78912932020-01-14 22:09:06 -07002368static void io_link_work_cb(struct io_wq_work **workptr)
2369{
2370 struct io_wq_work *work = *workptr;
2371 struct io_kiocb *link = work->data;
2372
2373 io_queue_linked_timeout(link);
2374 work->func = io_wq_submit_work;
2375}
2376
2377static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2378{
2379 struct io_kiocb *link;
2380
2381 io_prep_async_work(nxt, &link);
2382 *workptr = &nxt->work;
2383 if (link) {
2384 nxt->work.flags |= IO_WQ_WORK_CB;
2385 nxt->work.func = io_link_work_cb;
2386 nxt->work.data = link;
2387 }
2388}
2389
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002390static void io_fsync_finish(struct io_wq_work **workptr)
2391{
2392 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2393 loff_t end = req->sync.off + req->sync.len;
2394 struct io_kiocb *nxt = NULL;
2395 int ret;
2396
2397 if (io_req_cancelled(req))
2398 return;
2399
Jens Axboe9adbd452019-12-20 08:45:55 -07002400 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002401 end > 0 ? end : LLONG_MAX,
2402 req->sync.flags & IORING_FSYNC_DATASYNC);
2403 if (ret < 0)
2404 req_set_fail_links(req);
2405 io_cqring_add_event(req, ret);
2406 io_put_req_find_next(req, &nxt);
2407 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002408 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002409}
2410
Jens Axboefc4df992019-12-10 14:38:45 -07002411static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2412 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002413{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002414 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002415
2416 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002417 if (force_nonblock) {
2418 io_put_req(req);
2419 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002420 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002421 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002422
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002423 work = old_work = &req->work;
2424 io_fsync_finish(&work);
2425 if (work && work != old_work)
2426 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002427 return 0;
2428}
2429
Jens Axboed63d1b52019-12-10 10:38:56 -07002430static void io_fallocate_finish(struct io_wq_work **workptr)
2431{
2432 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2433 struct io_kiocb *nxt = NULL;
2434 int ret;
2435
2436 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2437 req->sync.len);
2438 if (ret < 0)
2439 req_set_fail_links(req);
2440 io_cqring_add_event(req, ret);
2441 io_put_req_find_next(req, &nxt);
2442 if (nxt)
2443 io_wq_assign_next(workptr, nxt);
2444}
2445
2446static int io_fallocate_prep(struct io_kiocb *req,
2447 const struct io_uring_sqe *sqe)
2448{
2449 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2450 return -EINVAL;
2451
2452 req->sync.off = READ_ONCE(sqe->off);
2453 req->sync.len = READ_ONCE(sqe->addr);
2454 req->sync.mode = READ_ONCE(sqe->len);
2455 return 0;
2456}
2457
2458static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2459 bool force_nonblock)
2460{
2461 struct io_wq_work *work, *old_work;
2462
2463 /* fallocate always requiring blocking context */
2464 if (force_nonblock) {
2465 io_put_req(req);
2466 req->work.func = io_fallocate_finish;
2467 return -EAGAIN;
2468 }
2469
2470 work = old_work = &req->work;
2471 io_fallocate_finish(&work);
2472 if (work && work != old_work)
2473 *nxt = container_of(work, struct io_kiocb, work);
2474
2475 return 0;
2476}
2477
Jens Axboe15b71ab2019-12-11 11:20:36 -07002478static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2479{
Jens Axboef8748882020-01-08 17:47:02 -07002480 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002481 int ret;
2482
2483 if (sqe->ioprio || sqe->buf_index)
2484 return -EINVAL;
2485
2486 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002487 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002488 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002489 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002490
Jens Axboef8748882020-01-08 17:47:02 -07002491 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002492 if (IS_ERR(req->open.filename)) {
2493 ret = PTR_ERR(req->open.filename);
2494 req->open.filename = NULL;
2495 return ret;
2496 }
2497
2498 return 0;
2499}
2500
Jens Axboecebdb982020-01-08 17:59:24 -07002501static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2502{
2503 struct open_how __user *how;
2504 const char __user *fname;
2505 size_t len;
2506 int ret;
2507
2508 if (sqe->ioprio || sqe->buf_index)
2509 return -EINVAL;
2510
2511 req->open.dfd = READ_ONCE(sqe->fd);
2512 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2513 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2514 len = READ_ONCE(sqe->len);
2515
2516 if (len < OPEN_HOW_SIZE_VER0)
2517 return -EINVAL;
2518
2519 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2520 len);
2521 if (ret)
2522 return ret;
2523
2524 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2525 req->open.how.flags |= O_LARGEFILE;
2526
2527 req->open.filename = getname(fname);
2528 if (IS_ERR(req->open.filename)) {
2529 ret = PTR_ERR(req->open.filename);
2530 req->open.filename = NULL;
2531 return ret;
2532 }
2533
2534 return 0;
2535}
2536
2537static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2538 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002539{
2540 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002541 struct file *file;
2542 int ret;
2543
2544 if (force_nonblock) {
2545 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2546 return -EAGAIN;
2547 }
2548
Jens Axboecebdb982020-01-08 17:59:24 -07002549 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002550 if (ret)
2551 goto err;
2552
Jens Axboecebdb982020-01-08 17:59:24 -07002553 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002554 if (ret < 0)
2555 goto err;
2556
2557 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2558 if (IS_ERR(file)) {
2559 put_unused_fd(ret);
2560 ret = PTR_ERR(file);
2561 } else {
2562 fsnotify_open(file);
2563 fd_install(ret, file);
2564 }
2565err:
2566 putname(req->open.filename);
2567 if (ret < 0)
2568 req_set_fail_links(req);
2569 io_cqring_add_event(req, ret);
2570 io_put_req_find_next(req, nxt);
2571 return 0;
2572}
2573
Jens Axboecebdb982020-01-08 17:59:24 -07002574static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2575 bool force_nonblock)
2576{
2577 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2578 return io_openat2(req, nxt, force_nonblock);
2579}
2580
Jens Axboec1ca7572019-12-25 22:18:28 -07002581static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2582{
2583#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2584 if (sqe->ioprio || sqe->buf_index || sqe->off)
2585 return -EINVAL;
2586
2587 req->madvise.addr = READ_ONCE(sqe->addr);
2588 req->madvise.len = READ_ONCE(sqe->len);
2589 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2590 return 0;
2591#else
2592 return -EOPNOTSUPP;
2593#endif
2594}
2595
2596static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2597 bool force_nonblock)
2598{
2599#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2600 struct io_madvise *ma = &req->madvise;
2601 int ret;
2602
2603 if (force_nonblock)
2604 return -EAGAIN;
2605
2606 ret = do_madvise(ma->addr, ma->len, ma->advice);
2607 if (ret < 0)
2608 req_set_fail_links(req);
2609 io_cqring_add_event(req, ret);
2610 io_put_req_find_next(req, nxt);
2611 return 0;
2612#else
2613 return -EOPNOTSUPP;
2614#endif
2615}
2616
Jens Axboe4840e412019-12-25 22:03:45 -07002617static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2618{
2619 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2620 return -EINVAL;
2621
2622 req->fadvise.offset = READ_ONCE(sqe->off);
2623 req->fadvise.len = READ_ONCE(sqe->len);
2624 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2625 return 0;
2626}
2627
2628static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2629 bool force_nonblock)
2630{
2631 struct io_fadvise *fa = &req->fadvise;
2632 int ret;
2633
2634 /* DONTNEED may block, others _should_ not */
2635 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2636 return -EAGAIN;
2637
2638 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2639 if (ret < 0)
2640 req_set_fail_links(req);
2641 io_cqring_add_event(req, ret);
2642 io_put_req_find_next(req, nxt);
2643 return 0;
2644}
2645
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002646static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2647{
Jens Axboef8748882020-01-08 17:47:02 -07002648 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002649 unsigned lookup_flags;
2650 int ret;
2651
2652 if (sqe->ioprio || sqe->buf_index)
2653 return -EINVAL;
2654
2655 req->open.dfd = READ_ONCE(sqe->fd);
2656 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002657 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002658 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002659 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002660
Jens Axboec12cedf2020-01-08 17:41:21 -07002661 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002662 return -EINVAL;
2663
Jens Axboef8748882020-01-08 17:47:02 -07002664 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002665 if (IS_ERR(req->open.filename)) {
2666 ret = PTR_ERR(req->open.filename);
2667 req->open.filename = NULL;
2668 return ret;
2669 }
2670
2671 return 0;
2672}
2673
2674static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2675 bool force_nonblock)
2676{
2677 struct io_open *ctx = &req->open;
2678 unsigned lookup_flags;
2679 struct path path;
2680 struct kstat stat;
2681 int ret;
2682
2683 if (force_nonblock)
2684 return -EAGAIN;
2685
Jens Axboec12cedf2020-01-08 17:41:21 -07002686 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002687 return -EINVAL;
2688
2689retry:
2690 /* filename_lookup() drops it, keep a reference */
2691 ctx->filename->refcnt++;
2692
2693 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2694 NULL);
2695 if (ret)
2696 goto err;
2697
Jens Axboec12cedf2020-01-08 17:41:21 -07002698 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002699 path_put(&path);
2700 if (retry_estale(ret, lookup_flags)) {
2701 lookup_flags |= LOOKUP_REVAL;
2702 goto retry;
2703 }
2704 if (!ret)
2705 ret = cp_statx(&stat, ctx->buffer);
2706err:
2707 putname(ctx->filename);
2708 if (ret < 0)
2709 req_set_fail_links(req);
2710 io_cqring_add_event(req, ret);
2711 io_put_req_find_next(req, nxt);
2712 return 0;
2713}
2714
Jens Axboeb5dba592019-12-11 14:02:38 -07002715static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2716{
2717 /*
2718 * If we queue this for async, it must not be cancellable. That would
2719 * leave the 'file' in an undeterminate state.
2720 */
2721 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2722
2723 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2724 sqe->rw_flags || sqe->buf_index)
2725 return -EINVAL;
2726 if (sqe->flags & IOSQE_FIXED_FILE)
2727 return -EINVAL;
2728
2729 req->close.fd = READ_ONCE(sqe->fd);
2730 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002731 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002732 return -EBADF;
2733
2734 return 0;
2735}
2736
2737static void io_close_finish(struct io_wq_work **workptr)
2738{
2739 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2740 struct io_kiocb *nxt = NULL;
2741
2742 /* Invoked with files, we need to do the close */
2743 if (req->work.files) {
2744 int ret;
2745
2746 ret = filp_close(req->close.put_file, req->work.files);
2747 if (ret < 0) {
2748 req_set_fail_links(req);
2749 }
2750 io_cqring_add_event(req, ret);
2751 }
2752
2753 fput(req->close.put_file);
2754
2755 /* we bypassed the re-issue, drop the submission reference */
2756 io_put_req(req);
2757 io_put_req_find_next(req, &nxt);
2758 if (nxt)
2759 io_wq_assign_next(workptr, nxt);
2760}
2761
2762static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2763 bool force_nonblock)
2764{
2765 int ret;
2766
2767 req->close.put_file = NULL;
2768 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2769 if (ret < 0)
2770 return ret;
2771
2772 /* if the file has a flush method, be safe and punt to async */
2773 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2774 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2775 goto eagain;
2776 }
2777
2778 /*
2779 * No ->flush(), safely close from here and just punt the
2780 * fput() to async context.
2781 */
2782 ret = filp_close(req->close.put_file, current->files);
2783
2784 if (ret < 0)
2785 req_set_fail_links(req);
2786 io_cqring_add_event(req, ret);
2787
2788 if (io_wq_current_is_worker()) {
2789 struct io_wq_work *old_work, *work;
2790
2791 old_work = work = &req->work;
2792 io_close_finish(&work);
2793 if (work && work != old_work)
2794 *nxt = container_of(work, struct io_kiocb, work);
2795 return 0;
2796 }
2797
2798eagain:
2799 req->work.func = io_close_finish;
2800 return -EAGAIN;
2801}
2802
Jens Axboe3529d8c2019-12-19 18:24:38 -07002803static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002804{
2805 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002806
2807 if (!req->file)
2808 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002809
2810 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2811 return -EINVAL;
2812 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2813 return -EINVAL;
2814
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002815 req->sync.off = READ_ONCE(sqe->off);
2816 req->sync.len = READ_ONCE(sqe->len);
2817 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002818 return 0;
2819}
2820
2821static void io_sync_file_range_finish(struct io_wq_work **workptr)
2822{
2823 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2824 struct io_kiocb *nxt = NULL;
2825 int ret;
2826
2827 if (io_req_cancelled(req))
2828 return;
2829
Jens Axboe9adbd452019-12-20 08:45:55 -07002830 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002831 req->sync.flags);
2832 if (ret < 0)
2833 req_set_fail_links(req);
2834 io_cqring_add_event(req, ret);
2835 io_put_req_find_next(req, &nxt);
2836 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002837 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002838}
2839
Jens Axboefc4df992019-12-10 14:38:45 -07002840static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002841 bool force_nonblock)
2842{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002843 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002844
2845 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002846 if (force_nonblock) {
2847 io_put_req(req);
2848 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002849 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002850 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002851
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002852 work = old_work = &req->work;
2853 io_sync_file_range_finish(&work);
2854 if (work && work != old_work)
2855 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002856 return 0;
2857}
2858
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002859#if defined(CONFIG_NET)
2860static void io_sendrecv_async(struct io_wq_work **workptr)
2861{
2862 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2863 struct iovec *iov = NULL;
2864
2865 if (req->io->rw.iov != req->io->rw.fast_iov)
2866 iov = req->io->msg.iov;
2867 io_wq_submit_work(workptr);
2868 kfree(iov);
2869}
2870#endif
2871
Jens Axboe3529d8c2019-12-19 18:24:38 -07002872static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002873{
Jens Axboe03b12302019-12-02 18:50:25 -07002874#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002875 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002876 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002877
Jens Axboee47293f2019-12-20 08:58:21 -07002878 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2879 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002880 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002881
Jens Axboefddafac2020-01-04 20:19:44 -07002882 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002883 return 0;
2884
Jens Axboed9688562019-12-09 19:35:20 -07002885 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002886 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002887 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002888#else
Jens Axboee47293f2019-12-20 08:58:21 -07002889 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002890#endif
2891}
2892
Jens Axboefc4df992019-12-10 14:38:45 -07002893static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2894 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002895{
2896#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002897 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002898 struct socket *sock;
2899 int ret;
2900
2901 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2902 return -EINVAL;
2903
2904 sock = sock_from_file(req->file, &ret);
2905 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002906 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002907 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002908 unsigned flags;
2909
Jens Axboe03b12302019-12-02 18:50:25 -07002910 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002911 kmsg = &req->io->msg;
2912 kmsg->msg.msg_name = &addr;
2913 /* if iov is set, it's allocated already */
2914 if (!kmsg->iov)
2915 kmsg->iov = kmsg->fast_iov;
2916 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002917 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002918 struct io_sr_msg *sr = &req->sr_msg;
2919
Jens Axboe0b416c32019-12-15 10:57:46 -07002920 kmsg = &io.msg;
2921 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002922
2923 io.msg.iov = io.msg.fast_iov;
2924 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2925 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002926 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002927 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002928 }
2929
Jens Axboee47293f2019-12-20 08:58:21 -07002930 flags = req->sr_msg.msg_flags;
2931 if (flags & MSG_DONTWAIT)
2932 req->flags |= REQ_F_NOWAIT;
2933 else if (force_nonblock)
2934 flags |= MSG_DONTWAIT;
2935
Jens Axboe0b416c32019-12-15 10:57:46 -07002936 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002937 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002938 if (req->io)
2939 return -EAGAIN;
2940 if (io_alloc_async_ctx(req))
2941 return -ENOMEM;
2942 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2943 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002944 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002945 }
2946 if (ret == -ERESTARTSYS)
2947 ret = -EINTR;
2948 }
2949
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002950 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002951 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002952 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002953 if (ret < 0)
2954 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002955 io_put_req_find_next(req, nxt);
2956 return 0;
2957#else
2958 return -EOPNOTSUPP;
2959#endif
2960}
2961
Jens Axboefddafac2020-01-04 20:19:44 -07002962static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2963 bool force_nonblock)
2964{
2965#if defined(CONFIG_NET)
2966 struct socket *sock;
2967 int ret;
2968
2969 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2970 return -EINVAL;
2971
2972 sock = sock_from_file(req->file, &ret);
2973 if (sock) {
2974 struct io_sr_msg *sr = &req->sr_msg;
2975 struct msghdr msg;
2976 struct iovec iov;
2977 unsigned flags;
2978
2979 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
2980 &msg.msg_iter);
2981 if (ret)
2982 return ret;
2983
2984 msg.msg_name = NULL;
2985 msg.msg_control = NULL;
2986 msg.msg_controllen = 0;
2987 msg.msg_namelen = 0;
2988
2989 flags = req->sr_msg.msg_flags;
2990 if (flags & MSG_DONTWAIT)
2991 req->flags |= REQ_F_NOWAIT;
2992 else if (force_nonblock)
2993 flags |= MSG_DONTWAIT;
2994
2995 ret = __sys_sendmsg_sock(sock, &msg, flags);
2996 if (force_nonblock && ret == -EAGAIN)
2997 return -EAGAIN;
2998 if (ret == -ERESTARTSYS)
2999 ret = -EINTR;
3000 }
3001
3002 io_cqring_add_event(req, ret);
3003 if (ret < 0)
3004 req_set_fail_links(req);
3005 io_put_req_find_next(req, nxt);
3006 return 0;
3007#else
3008 return -EOPNOTSUPP;
3009#endif
3010}
3011
Jens Axboe3529d8c2019-12-19 18:24:38 -07003012static int io_recvmsg_prep(struct io_kiocb *req,
3013 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003014{
3015#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003016 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003017 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07003018
Jens Axboe3529d8c2019-12-19 18:24:38 -07003019 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3020 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
3021
Jens Axboefddafac2020-01-04 20:19:44 -07003022 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003023 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003024
Jens Axboed9688562019-12-09 19:35:20 -07003025 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003026 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003027 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003028#else
Jens Axboee47293f2019-12-20 08:58:21 -07003029 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003030#endif
3031}
3032
Jens Axboefc4df992019-12-10 14:38:45 -07003033static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3034 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003035{
3036#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003037 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003038 struct socket *sock;
3039 int ret;
3040
3041 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3042 return -EINVAL;
3043
3044 sock = sock_from_file(req->file, &ret);
3045 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003046 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003047 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003048 unsigned flags;
3049
Jens Axboe03b12302019-12-02 18:50:25 -07003050 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003051 kmsg = &req->io->msg;
3052 kmsg->msg.msg_name = &addr;
3053 /* if iov is set, it's allocated already */
3054 if (!kmsg->iov)
3055 kmsg->iov = kmsg->fast_iov;
3056 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003057 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003058 struct io_sr_msg *sr = &req->sr_msg;
3059
Jens Axboe0b416c32019-12-15 10:57:46 -07003060 kmsg = &io.msg;
3061 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003062
3063 io.msg.iov = io.msg.fast_iov;
3064 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3065 sr->msg_flags, &io.msg.uaddr,
3066 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003067 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003068 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003069 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003070
Jens Axboee47293f2019-12-20 08:58:21 -07003071 flags = req->sr_msg.msg_flags;
3072 if (flags & MSG_DONTWAIT)
3073 req->flags |= REQ_F_NOWAIT;
3074 else if (force_nonblock)
3075 flags |= MSG_DONTWAIT;
3076
3077 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3078 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003079 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003080 if (req->io)
3081 return -EAGAIN;
3082 if (io_alloc_async_ctx(req))
3083 return -ENOMEM;
3084 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3085 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003086 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003087 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003088 if (ret == -ERESTARTSYS)
3089 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003090 }
3091
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003092 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003093 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003094 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003095 if (ret < 0)
3096 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003097 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003098 return 0;
3099#else
3100 return -EOPNOTSUPP;
3101#endif
3102}
3103
Jens Axboefddafac2020-01-04 20:19:44 -07003104static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3105 bool force_nonblock)
3106{
3107#if defined(CONFIG_NET)
3108 struct socket *sock;
3109 int ret;
3110
3111 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3112 return -EINVAL;
3113
3114 sock = sock_from_file(req->file, &ret);
3115 if (sock) {
3116 struct io_sr_msg *sr = &req->sr_msg;
3117 struct msghdr msg;
3118 struct iovec iov;
3119 unsigned flags;
3120
3121 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3122 &msg.msg_iter);
3123 if (ret)
3124 return ret;
3125
3126 msg.msg_name = NULL;
3127 msg.msg_control = NULL;
3128 msg.msg_controllen = 0;
3129 msg.msg_namelen = 0;
3130 msg.msg_iocb = NULL;
3131 msg.msg_flags = 0;
3132
3133 flags = req->sr_msg.msg_flags;
3134 if (flags & MSG_DONTWAIT)
3135 req->flags |= REQ_F_NOWAIT;
3136 else if (force_nonblock)
3137 flags |= MSG_DONTWAIT;
3138
3139 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3140 if (force_nonblock && ret == -EAGAIN)
3141 return -EAGAIN;
3142 if (ret == -ERESTARTSYS)
3143 ret = -EINTR;
3144 }
3145
3146 io_cqring_add_event(req, ret);
3147 if (ret < 0)
3148 req_set_fail_links(req);
3149 io_put_req_find_next(req, nxt);
3150 return 0;
3151#else
3152 return -EOPNOTSUPP;
3153#endif
3154}
3155
3156
Jens Axboe3529d8c2019-12-19 18:24:38 -07003157static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003158{
3159#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003160 struct io_accept *accept = &req->accept;
3161
Jens Axboe17f2fe32019-10-17 14:42:58 -06003162 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3163 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003164 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003165 return -EINVAL;
3166
Jens Axboed55e5f52019-12-11 16:12:15 -07003167 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3168 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003169 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003170 return 0;
3171#else
3172 return -EOPNOTSUPP;
3173#endif
3174}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003175
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003176#if defined(CONFIG_NET)
3177static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3178 bool force_nonblock)
3179{
3180 struct io_accept *accept = &req->accept;
3181 unsigned file_flags;
3182 int ret;
3183
3184 file_flags = force_nonblock ? O_NONBLOCK : 0;
3185 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3186 accept->addr_len, accept->flags);
3187 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003188 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003189 if (ret == -ERESTARTSYS)
3190 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003191 if (ret < 0)
3192 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003193 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003194 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003195 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003196}
3197
3198static void io_accept_finish(struct io_wq_work **workptr)
3199{
3200 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3201 struct io_kiocb *nxt = NULL;
3202
3203 if (io_req_cancelled(req))
3204 return;
3205 __io_accept(req, &nxt, false);
3206 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003207 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003208}
3209#endif
3210
3211static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3212 bool force_nonblock)
3213{
3214#if defined(CONFIG_NET)
3215 int ret;
3216
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003217 ret = __io_accept(req, nxt, force_nonblock);
3218 if (ret == -EAGAIN && force_nonblock) {
3219 req->work.func = io_accept_finish;
3220 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3221 io_put_req(req);
3222 return -EAGAIN;
3223 }
3224 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003225#else
3226 return -EOPNOTSUPP;
3227#endif
3228}
3229
Jens Axboe3529d8c2019-12-19 18:24:38 -07003230static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003231{
3232#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003233 struct io_connect *conn = &req->connect;
3234 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003235
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003236 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3237 return -EINVAL;
3238 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3239 return -EINVAL;
3240
Jens Axboe3529d8c2019-12-19 18:24:38 -07003241 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3242 conn->addr_len = READ_ONCE(sqe->addr2);
3243
3244 if (!io)
3245 return 0;
3246
3247 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003248 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003249#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003250 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003251#endif
3252}
3253
Jens Axboefc4df992019-12-10 14:38:45 -07003254static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3255 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003256{
3257#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003258 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003259 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003260 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003261
Jens Axboef499a022019-12-02 16:28:46 -07003262 if (req->io) {
3263 io = req->io;
3264 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003265 ret = move_addr_to_kernel(req->connect.addr,
3266 req->connect.addr_len,
3267 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003268 if (ret)
3269 goto out;
3270 io = &__io;
3271 }
3272
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003273 file_flags = force_nonblock ? O_NONBLOCK : 0;
3274
3275 ret = __sys_connect_file(req->file, &io->connect.address,
3276 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003277 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003278 if (req->io)
3279 return -EAGAIN;
3280 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003281 ret = -ENOMEM;
3282 goto out;
3283 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003284 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003285 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003286 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003287 if (ret == -ERESTARTSYS)
3288 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003289out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003290 if (ret < 0)
3291 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003292 io_cqring_add_event(req, ret);
3293 io_put_req_find_next(req, nxt);
3294 return 0;
3295#else
3296 return -EOPNOTSUPP;
3297#endif
3298}
3299
Jens Axboe221c5eb2019-01-17 09:41:58 -07003300static void io_poll_remove_one(struct io_kiocb *req)
3301{
3302 struct io_poll_iocb *poll = &req->poll;
3303
3304 spin_lock(&poll->head->lock);
3305 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003306 if (!list_empty(&poll->wait.entry)) {
3307 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003308 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003309 }
3310 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003311 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003312}
3313
3314static void io_poll_remove_all(struct io_ring_ctx *ctx)
3315{
Jens Axboe78076bb2019-12-04 19:56:40 -07003316 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003317 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003318 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003319
3320 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003321 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3322 struct hlist_head *list;
3323
3324 list = &ctx->cancel_hash[i];
3325 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3326 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003327 }
3328 spin_unlock_irq(&ctx->completion_lock);
3329}
3330
Jens Axboe47f46762019-11-09 17:43:02 -07003331static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3332{
Jens Axboe78076bb2019-12-04 19:56:40 -07003333 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003334 struct io_kiocb *req;
3335
Jens Axboe78076bb2019-12-04 19:56:40 -07003336 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3337 hlist_for_each_entry(req, list, hash_node) {
3338 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003339 io_poll_remove_one(req);
3340 return 0;
3341 }
Jens Axboe47f46762019-11-09 17:43:02 -07003342 }
3343
3344 return -ENOENT;
3345}
3346
Jens Axboe3529d8c2019-12-19 18:24:38 -07003347static int io_poll_remove_prep(struct io_kiocb *req,
3348 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003349{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003350 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3351 return -EINVAL;
3352 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3353 sqe->poll_events)
3354 return -EINVAL;
3355
Jens Axboe0969e782019-12-17 18:40:57 -07003356 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003357 return 0;
3358}
3359
3360/*
3361 * Find a running poll command that matches one specified in sqe->addr,
3362 * and remove it if found.
3363 */
3364static int io_poll_remove(struct io_kiocb *req)
3365{
3366 struct io_ring_ctx *ctx = req->ctx;
3367 u64 addr;
3368 int ret;
3369
Jens Axboe0969e782019-12-17 18:40:57 -07003370 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003371 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003372 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003373 spin_unlock_irq(&ctx->completion_lock);
3374
Jens Axboe78e19bb2019-11-06 15:21:34 -07003375 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003376 if (ret < 0)
3377 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003378 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003379 return 0;
3380}
3381
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003382static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003383{
Jackie Liua197f662019-11-08 08:09:12 -07003384 struct io_ring_ctx *ctx = req->ctx;
3385
Jens Axboe8c838782019-03-12 15:48:16 -06003386 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003387 if (error)
3388 io_cqring_fill_event(req, error);
3389 else
3390 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003391 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003392}
3393
Jens Axboe561fb042019-10-24 07:25:42 -06003394static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003395{
Jens Axboe561fb042019-10-24 07:25:42 -06003396 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003397 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3398 struct io_poll_iocb *poll = &req->poll;
3399 struct poll_table_struct pt = { ._key = poll->events };
3400 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003401 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003402 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003403 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003404
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003405 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003406 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003407 ret = -ECANCELED;
3408 } else if (READ_ONCE(poll->canceled)) {
3409 ret = -ECANCELED;
3410 }
Jens Axboe561fb042019-10-24 07:25:42 -06003411
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003412 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003413 mask = vfs_poll(poll->file, &pt) & poll->events;
3414
3415 /*
3416 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3417 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3418 * synchronize with them. In the cancellation case the list_del_init
3419 * itself is not actually needed, but harmless so we keep it in to
3420 * avoid further branches in the fast path.
3421 */
3422 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003423 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003424 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003425 spin_unlock_irq(&ctx->completion_lock);
3426 return;
3427 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003428 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003429 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003430 spin_unlock_irq(&ctx->completion_lock);
3431
Jens Axboe8c838782019-03-12 15:48:16 -06003432 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003433
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003434 if (ret < 0)
3435 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003436 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003437 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003438 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003439}
3440
Jens Axboee94f1412019-12-19 12:06:02 -07003441static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3442{
Jens Axboee94f1412019-12-19 12:06:02 -07003443 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003444 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003445
Jens Axboec6ca97b302019-12-28 12:11:08 -07003446 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003447 spin_lock_irq(&ctx->completion_lock);
3448 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3449 hash_del(&req->hash_node);
3450 io_poll_complete(req, req->result, 0);
3451
Jens Axboe8237e042019-12-28 10:48:22 -07003452 if (refcount_dec_and_test(&req->refs) &&
3453 !io_req_multi_free(&rb, req)) {
3454 req->flags |= REQ_F_COMP_LOCKED;
3455 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003456 }
3457 }
3458 spin_unlock_irq(&ctx->completion_lock);
3459
3460 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003461 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003462}
3463
3464static void io_poll_flush(struct io_wq_work **workptr)
3465{
3466 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3467 struct llist_node *nodes;
3468
3469 nodes = llist_del_all(&req->ctx->poll_llist);
3470 if (nodes)
3471 __io_poll_flush(req->ctx, nodes);
3472}
3473
Jens Axboe221c5eb2019-01-17 09:41:58 -07003474static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3475 void *key)
3476{
Jens Axboee9444752019-11-26 15:02:04 -07003477 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003478 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3479 struct io_ring_ctx *ctx = req->ctx;
3480 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003481
3482 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003483 if (mask && !(mask & poll->events))
3484 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003485
Jens Axboe392edb42019-12-09 17:52:20 -07003486 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003487
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003488 /*
3489 * Run completion inline if we can. We're using trylock here because
3490 * we are violating the completion_lock -> poll wq lock ordering.
3491 * If we have a link timeout we're going to need the completion_lock
3492 * for finalizing the request, mark us as having grabbed that already.
3493 */
Jens Axboee94f1412019-12-19 12:06:02 -07003494 if (mask) {
3495 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003496
Jens Axboee94f1412019-12-19 12:06:02 -07003497 if (llist_empty(&ctx->poll_llist) &&
3498 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3499 hash_del(&req->hash_node);
3500 io_poll_complete(req, mask, 0);
3501 req->flags |= REQ_F_COMP_LOCKED;
3502 io_put_req(req);
3503 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3504
3505 io_cqring_ev_posted(ctx);
3506 req = NULL;
3507 } else {
3508 req->result = mask;
3509 req->llist_node.next = NULL;
3510 /* if the list wasn't empty, we're done */
3511 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3512 req = NULL;
3513 else
3514 req->work.func = io_poll_flush;
3515 }
Jens Axboe8c838782019-03-12 15:48:16 -06003516 }
Jens Axboee94f1412019-12-19 12:06:02 -07003517 if (req)
3518 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003519
Jens Axboe221c5eb2019-01-17 09:41:58 -07003520 return 1;
3521}
3522
3523struct io_poll_table {
3524 struct poll_table_struct pt;
3525 struct io_kiocb *req;
3526 int error;
3527};
3528
3529static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3530 struct poll_table_struct *p)
3531{
3532 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3533
3534 if (unlikely(pt->req->poll.head)) {
3535 pt->error = -EINVAL;
3536 return;
3537 }
3538
3539 pt->error = 0;
3540 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003541 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003542}
3543
Jens Axboeeac406c2019-11-14 12:09:58 -07003544static void io_poll_req_insert(struct io_kiocb *req)
3545{
3546 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003547 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003548
Jens Axboe78076bb2019-12-04 19:56:40 -07003549 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3550 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003551}
3552
Jens Axboe3529d8c2019-12-19 18:24:38 -07003553static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003554{
3555 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003556 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003557
3558 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3559 return -EINVAL;
3560 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3561 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003562 if (!poll->file)
3563 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003564
Jens Axboe221c5eb2019-01-17 09:41:58 -07003565 events = READ_ONCE(sqe->poll_events);
3566 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003567 return 0;
3568}
3569
3570static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3571{
3572 struct io_poll_iocb *poll = &req->poll;
3573 struct io_ring_ctx *ctx = req->ctx;
3574 struct io_poll_table ipt;
3575 bool cancel = false;
3576 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003577
3578 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003579 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003580
Jens Axboe221c5eb2019-01-17 09:41:58 -07003581 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003582 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003583 poll->canceled = false;
3584
3585 ipt.pt._qproc = io_poll_queue_proc;
3586 ipt.pt._key = poll->events;
3587 ipt.req = req;
3588 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3589
3590 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003591 INIT_LIST_HEAD(&poll->wait.entry);
3592 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3593 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003594
Jens Axboe36703242019-07-25 10:20:18 -06003595 INIT_LIST_HEAD(&req->list);
3596
Jens Axboe221c5eb2019-01-17 09:41:58 -07003597 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003598
3599 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003600 if (likely(poll->head)) {
3601 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003602 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003603 if (ipt.error)
3604 cancel = true;
3605 ipt.error = 0;
3606 mask = 0;
3607 }
3608 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003609 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003610 else if (cancel)
3611 WRITE_ONCE(poll->canceled, true);
3612 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003613 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003614 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003615 }
Jens Axboe8c838782019-03-12 15:48:16 -06003616 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003617 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003618 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003619 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003620 spin_unlock_irq(&ctx->completion_lock);
3621
Jens Axboe8c838782019-03-12 15:48:16 -06003622 if (mask) {
3623 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003624 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003625 }
Jens Axboe8c838782019-03-12 15:48:16 -06003626 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003627}
3628
Jens Axboe5262f562019-09-17 12:26:57 -06003629static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3630{
Jens Axboead8a48a2019-11-15 08:49:11 -07003631 struct io_timeout_data *data = container_of(timer,
3632 struct io_timeout_data, timer);
3633 struct io_kiocb *req = data->req;
3634 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003635 unsigned long flags;
3636
Jens Axboe5262f562019-09-17 12:26:57 -06003637 atomic_inc(&ctx->cq_timeouts);
3638
3639 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003640 /*
Jens Axboe11365042019-10-16 09:08:32 -06003641 * We could be racing with timeout deletion. If the list is empty,
3642 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003643 */
Jens Axboe842f9612019-10-29 12:34:10 -06003644 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003645 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003646
Jens Axboe11365042019-10-16 09:08:32 -06003647 /*
3648 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003649 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003650 * pointer will be increased, otherwise other timeout reqs may
3651 * return in advance without waiting for enough wait_nr.
3652 */
3653 prev = req;
3654 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3655 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003656 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003657 }
Jens Axboe842f9612019-10-29 12:34:10 -06003658
Jens Axboe78e19bb2019-11-06 15:21:34 -07003659 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003660 io_commit_cqring(ctx);
3661 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3662
3663 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003664 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003665 io_put_req(req);
3666 return HRTIMER_NORESTART;
3667}
3668
Jens Axboe47f46762019-11-09 17:43:02 -07003669static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3670{
3671 struct io_kiocb *req;
3672 int ret = -ENOENT;
3673
3674 list_for_each_entry(req, &ctx->timeout_list, list) {
3675 if (user_data == req->user_data) {
3676 list_del_init(&req->list);
3677 ret = 0;
3678 break;
3679 }
3680 }
3681
3682 if (ret == -ENOENT)
3683 return ret;
3684
Jens Axboe2d283902019-12-04 11:08:05 -07003685 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003686 if (ret == -1)
3687 return -EALREADY;
3688
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003689 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003690 io_cqring_fill_event(req, -ECANCELED);
3691 io_put_req(req);
3692 return 0;
3693}
3694
Jens Axboe3529d8c2019-12-19 18:24:38 -07003695static int io_timeout_remove_prep(struct io_kiocb *req,
3696 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003697{
Jens Axboeb29472e2019-12-17 18:50:29 -07003698 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3699 return -EINVAL;
3700 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3701 return -EINVAL;
3702
3703 req->timeout.addr = READ_ONCE(sqe->addr);
3704 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3705 if (req->timeout.flags)
3706 return -EINVAL;
3707
Jens Axboeb29472e2019-12-17 18:50:29 -07003708 return 0;
3709}
3710
Jens Axboe11365042019-10-16 09:08:32 -06003711/*
3712 * Remove or update an existing timeout command
3713 */
Jens Axboefc4df992019-12-10 14:38:45 -07003714static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003715{
3716 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003717 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003718
Jens Axboe11365042019-10-16 09:08:32 -06003719 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003720 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003721
Jens Axboe47f46762019-11-09 17:43:02 -07003722 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003723 io_commit_cqring(ctx);
3724 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003725 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003726 if (ret < 0)
3727 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003728 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003729 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003730}
3731
Jens Axboe3529d8c2019-12-19 18:24:38 -07003732static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003733 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003734{
Jens Axboead8a48a2019-11-15 08:49:11 -07003735 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003736 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003737
Jens Axboead8a48a2019-11-15 08:49:11 -07003738 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003739 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003740 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003741 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003742 if (sqe->off && is_timeout_link)
3743 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003744 flags = READ_ONCE(sqe->timeout_flags);
3745 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003746 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003747
Jens Axboe26a61672019-12-20 09:02:01 -07003748 req->timeout.count = READ_ONCE(sqe->off);
3749
Jens Axboe3529d8c2019-12-19 18:24:38 -07003750 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003751 return -ENOMEM;
3752
3753 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003754 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003755 req->flags |= REQ_F_TIMEOUT;
3756
3757 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003758 return -EFAULT;
3759
Jens Axboe11365042019-10-16 09:08:32 -06003760 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003761 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003762 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003763 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003764
Jens Axboead8a48a2019-11-15 08:49:11 -07003765 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3766 return 0;
3767}
3768
Jens Axboefc4df992019-12-10 14:38:45 -07003769static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003770{
3771 unsigned count;
3772 struct io_ring_ctx *ctx = req->ctx;
3773 struct io_timeout_data *data;
3774 struct list_head *entry;
3775 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003776
Jens Axboe2d283902019-12-04 11:08:05 -07003777 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003778
Jens Axboe5262f562019-09-17 12:26:57 -06003779 /*
3780 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003781 * timeout event to be satisfied. If it isn't set, then this is
3782 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003783 */
Jens Axboe26a61672019-12-20 09:02:01 -07003784 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003785 if (!count) {
3786 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3787 spin_lock_irq(&ctx->completion_lock);
3788 entry = ctx->timeout_list.prev;
3789 goto add;
3790 }
Jens Axboe5262f562019-09-17 12:26:57 -06003791
3792 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003793 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003794
3795 /*
3796 * Insertion sort, ensuring the first entry in the list is always
3797 * the one we need first.
3798 */
Jens Axboe5262f562019-09-17 12:26:57 -06003799 spin_lock_irq(&ctx->completion_lock);
3800 list_for_each_prev(entry, &ctx->timeout_list) {
3801 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003802 unsigned nxt_sq_head;
3803 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003804 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003805
Jens Axboe93bd25b2019-11-11 23:34:31 -07003806 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3807 continue;
3808
yangerkun5da0fb12019-10-15 21:59:29 +08003809 /*
3810 * Since cached_sq_head + count - 1 can overflow, use type long
3811 * long to store it.
3812 */
3813 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003814 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3815 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003816
3817 /*
3818 * cached_sq_head may overflow, and it will never overflow twice
3819 * once there is some timeout req still be valid.
3820 */
3821 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003822 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003823
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003824 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003825 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003826
3827 /*
3828 * Sequence of reqs after the insert one and itself should
3829 * be adjusted because each timeout req consumes a slot.
3830 */
3831 span++;
3832 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003833 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003834 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003835add:
Jens Axboe5262f562019-09-17 12:26:57 -06003836 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003837 data->timer.function = io_timeout_fn;
3838 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003839 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003840 return 0;
3841}
3842
Jens Axboe62755e32019-10-28 21:49:21 -06003843static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003844{
Jens Axboe62755e32019-10-28 21:49:21 -06003845 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003846
Jens Axboe62755e32019-10-28 21:49:21 -06003847 return req->user_data == (unsigned long) data;
3848}
3849
Jens Axboee977d6d2019-11-05 12:39:45 -07003850static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003851{
Jens Axboe62755e32019-10-28 21:49:21 -06003852 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003853 int ret = 0;
3854
Jens Axboe62755e32019-10-28 21:49:21 -06003855 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3856 switch (cancel_ret) {
3857 case IO_WQ_CANCEL_OK:
3858 ret = 0;
3859 break;
3860 case IO_WQ_CANCEL_RUNNING:
3861 ret = -EALREADY;
3862 break;
3863 case IO_WQ_CANCEL_NOTFOUND:
3864 ret = -ENOENT;
3865 break;
3866 }
3867
Jens Axboee977d6d2019-11-05 12:39:45 -07003868 return ret;
3869}
3870
Jens Axboe47f46762019-11-09 17:43:02 -07003871static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3872 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003873 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003874{
3875 unsigned long flags;
3876 int ret;
3877
3878 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3879 if (ret != -ENOENT) {
3880 spin_lock_irqsave(&ctx->completion_lock, flags);
3881 goto done;
3882 }
3883
3884 spin_lock_irqsave(&ctx->completion_lock, flags);
3885 ret = io_timeout_cancel(ctx, sqe_addr);
3886 if (ret != -ENOENT)
3887 goto done;
3888 ret = io_poll_cancel(ctx, sqe_addr);
3889done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003890 if (!ret)
3891 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003892 io_cqring_fill_event(req, ret);
3893 io_commit_cqring(ctx);
3894 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3895 io_cqring_ev_posted(ctx);
3896
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003897 if (ret < 0)
3898 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003899 io_put_req_find_next(req, nxt);
3900}
3901
Jens Axboe3529d8c2019-12-19 18:24:38 -07003902static int io_async_cancel_prep(struct io_kiocb *req,
3903 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003904{
Jens Axboefbf23842019-12-17 18:45:56 -07003905 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003906 return -EINVAL;
3907 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3908 sqe->cancel_flags)
3909 return -EINVAL;
3910
Jens Axboefbf23842019-12-17 18:45:56 -07003911 req->cancel.addr = READ_ONCE(sqe->addr);
3912 return 0;
3913}
3914
3915static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3916{
3917 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003918
3919 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003920 return 0;
3921}
3922
Jens Axboe05f3fb32019-12-09 11:22:50 -07003923static int io_files_update_prep(struct io_kiocb *req,
3924 const struct io_uring_sqe *sqe)
3925{
3926 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3927 return -EINVAL;
3928
3929 req->files_update.offset = READ_ONCE(sqe->off);
3930 req->files_update.nr_args = READ_ONCE(sqe->len);
3931 if (!req->files_update.nr_args)
3932 return -EINVAL;
3933 req->files_update.arg = READ_ONCE(sqe->addr);
3934 return 0;
3935}
3936
3937static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3938{
3939 struct io_ring_ctx *ctx = req->ctx;
3940 struct io_uring_files_update up;
3941 int ret;
3942
3943 if (force_nonblock) {
3944 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3945 return -EAGAIN;
3946 }
3947
3948 up.offset = req->files_update.offset;
3949 up.fds = req->files_update.arg;
3950
3951 mutex_lock(&ctx->uring_lock);
3952 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3953 mutex_unlock(&ctx->uring_lock);
3954
3955 if (ret < 0)
3956 req_set_fail_links(req);
3957 io_cqring_add_event(req, ret);
3958 io_put_req(req);
3959 return 0;
3960}
3961
Jens Axboe3529d8c2019-12-19 18:24:38 -07003962static int io_req_defer_prep(struct io_kiocb *req,
3963 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003964{
Jens Axboee7815732019-12-17 19:45:06 -07003965 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003966
Jens Axboed625c6e2019-12-17 19:53:05 -07003967 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003968 case IORING_OP_NOP:
3969 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003970 case IORING_OP_READV:
3971 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003972 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003973 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003974 break;
3975 case IORING_OP_WRITEV:
3976 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003977 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003978 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003979 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003980 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003981 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003982 break;
3983 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003984 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003985 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003986 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003987 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003988 break;
3989 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003990 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003991 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003992 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003993 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003994 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003995 break;
3996 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003997 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003998 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003999 break;
Jens Axboef499a022019-12-02 16:28:46 -07004000 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004001 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004002 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004003 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004004 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004005 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004006 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004007 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004008 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004009 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004010 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004011 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004012 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004013 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004014 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004015 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004016 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004017 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004018 case IORING_OP_FALLOCATE:
4019 ret = io_fallocate_prep(req, sqe);
4020 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004021 case IORING_OP_OPENAT:
4022 ret = io_openat_prep(req, sqe);
4023 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004024 case IORING_OP_CLOSE:
4025 ret = io_close_prep(req, sqe);
4026 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004027 case IORING_OP_FILES_UPDATE:
4028 ret = io_files_update_prep(req, sqe);
4029 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004030 case IORING_OP_STATX:
4031 ret = io_statx_prep(req, sqe);
4032 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004033 case IORING_OP_FADVISE:
4034 ret = io_fadvise_prep(req, sqe);
4035 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004036 case IORING_OP_MADVISE:
4037 ret = io_madvise_prep(req, sqe);
4038 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004039 case IORING_OP_OPENAT2:
4040 ret = io_openat2_prep(req, sqe);
4041 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004042 default:
Jens Axboee7815732019-12-17 19:45:06 -07004043 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4044 req->opcode);
4045 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004046 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004047 }
4048
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004049 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004050}
4051
Jens Axboe3529d8c2019-12-19 18:24:38 -07004052static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004053{
Jackie Liua197f662019-11-08 08:09:12 -07004054 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004055 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004056
Bob Liu9d858b22019-11-13 18:06:25 +08004057 /* Still need defer if there is pending req in defer list. */
4058 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004059 return 0;
4060
Jens Axboe3529d8c2019-12-19 18:24:38 -07004061 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004062 return -EAGAIN;
4063
Jens Axboe3529d8c2019-12-19 18:24:38 -07004064 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004065 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004066 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004067
Jens Axboede0617e2019-04-06 21:51:27 -06004068 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004069 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004070 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004071 return 0;
4072 }
4073
Jens Axboe915967f2019-11-21 09:01:20 -07004074 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004075 list_add_tail(&req->list, &ctx->defer_list);
4076 spin_unlock_irq(&ctx->completion_lock);
4077 return -EIOCBQUEUED;
4078}
4079
Jens Axboe3529d8c2019-12-19 18:24:38 -07004080static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4081 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004082{
Jackie Liua197f662019-11-08 08:09:12 -07004083 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004084 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004085
Jens Axboed625c6e2019-12-17 19:53:05 -07004086 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004087 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004088 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004089 break;
4090 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004091 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004092 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004093 if (sqe) {
4094 ret = io_read_prep(req, sqe, force_nonblock);
4095 if (ret < 0)
4096 break;
4097 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004098 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004099 break;
4100 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004101 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004102 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004103 if (sqe) {
4104 ret = io_write_prep(req, sqe, force_nonblock);
4105 if (ret < 0)
4106 break;
4107 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004108 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004109 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004110 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004111 if (sqe) {
4112 ret = io_prep_fsync(req, sqe);
4113 if (ret < 0)
4114 break;
4115 }
Jens Axboefc4df992019-12-10 14:38:45 -07004116 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004117 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004118 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004119 if (sqe) {
4120 ret = io_poll_add_prep(req, sqe);
4121 if (ret)
4122 break;
4123 }
Jens Axboefc4df992019-12-10 14:38:45 -07004124 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004125 break;
4126 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004127 if (sqe) {
4128 ret = io_poll_remove_prep(req, sqe);
4129 if (ret < 0)
4130 break;
4131 }
Jens Axboefc4df992019-12-10 14:38:45 -07004132 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004133 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004134 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004135 if (sqe) {
4136 ret = io_prep_sfr(req, sqe);
4137 if (ret < 0)
4138 break;
4139 }
Jens Axboefc4df992019-12-10 14:38:45 -07004140 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004141 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004142 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004143 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004144 if (sqe) {
4145 ret = io_sendmsg_prep(req, sqe);
4146 if (ret < 0)
4147 break;
4148 }
Jens Axboefddafac2020-01-04 20:19:44 -07004149 if (req->opcode == IORING_OP_SENDMSG)
4150 ret = io_sendmsg(req, nxt, force_nonblock);
4151 else
4152 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004153 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004154 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004155 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004156 if (sqe) {
4157 ret = io_recvmsg_prep(req, sqe);
4158 if (ret)
4159 break;
4160 }
Jens Axboefddafac2020-01-04 20:19:44 -07004161 if (req->opcode == IORING_OP_RECVMSG)
4162 ret = io_recvmsg(req, nxt, force_nonblock);
4163 else
4164 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004165 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004166 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004167 if (sqe) {
4168 ret = io_timeout_prep(req, sqe, false);
4169 if (ret)
4170 break;
4171 }
Jens Axboefc4df992019-12-10 14:38:45 -07004172 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004173 break;
Jens Axboe11365042019-10-16 09:08:32 -06004174 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004175 if (sqe) {
4176 ret = io_timeout_remove_prep(req, sqe);
4177 if (ret)
4178 break;
4179 }
Jens Axboefc4df992019-12-10 14:38:45 -07004180 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004181 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004182 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004183 if (sqe) {
4184 ret = io_accept_prep(req, sqe);
4185 if (ret)
4186 break;
4187 }
Jens Axboefc4df992019-12-10 14:38:45 -07004188 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004189 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004190 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004191 if (sqe) {
4192 ret = io_connect_prep(req, sqe);
4193 if (ret)
4194 break;
4195 }
Jens Axboefc4df992019-12-10 14:38:45 -07004196 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004197 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004198 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004199 if (sqe) {
4200 ret = io_async_cancel_prep(req, sqe);
4201 if (ret)
4202 break;
4203 }
Jens Axboefc4df992019-12-10 14:38:45 -07004204 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004205 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004206 case IORING_OP_FALLOCATE:
4207 if (sqe) {
4208 ret = io_fallocate_prep(req, sqe);
4209 if (ret)
4210 break;
4211 }
4212 ret = io_fallocate(req, nxt, force_nonblock);
4213 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004214 case IORING_OP_OPENAT:
4215 if (sqe) {
4216 ret = io_openat_prep(req, sqe);
4217 if (ret)
4218 break;
4219 }
4220 ret = io_openat(req, nxt, force_nonblock);
4221 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004222 case IORING_OP_CLOSE:
4223 if (sqe) {
4224 ret = io_close_prep(req, sqe);
4225 if (ret)
4226 break;
4227 }
4228 ret = io_close(req, nxt, force_nonblock);
4229 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004230 case IORING_OP_FILES_UPDATE:
4231 if (sqe) {
4232 ret = io_files_update_prep(req, sqe);
4233 if (ret)
4234 break;
4235 }
4236 ret = io_files_update(req, force_nonblock);
4237 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004238 case IORING_OP_STATX:
4239 if (sqe) {
4240 ret = io_statx_prep(req, sqe);
4241 if (ret)
4242 break;
4243 }
4244 ret = io_statx(req, nxt, force_nonblock);
4245 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004246 case IORING_OP_FADVISE:
4247 if (sqe) {
4248 ret = io_fadvise_prep(req, sqe);
4249 if (ret)
4250 break;
4251 }
4252 ret = io_fadvise(req, nxt, force_nonblock);
4253 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004254 case IORING_OP_MADVISE:
4255 if (sqe) {
4256 ret = io_madvise_prep(req, sqe);
4257 if (ret)
4258 break;
4259 }
4260 ret = io_madvise(req, nxt, force_nonblock);
4261 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004262 case IORING_OP_OPENAT2:
4263 if (sqe) {
4264 ret = io_openat2_prep(req, sqe);
4265 if (ret)
4266 break;
4267 }
4268 ret = io_openat2(req, nxt, force_nonblock);
4269 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004270 default:
4271 ret = -EINVAL;
4272 break;
4273 }
4274
Jens Axboedef596e2019-01-09 08:59:42 -07004275 if (ret)
4276 return ret;
4277
4278 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004279 const bool in_async = io_wq_current_is_worker();
4280
Jens Axboe9e645e112019-05-10 16:07:28 -06004281 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004282 return -EAGAIN;
4283
Jens Axboe11ba8202020-01-15 21:51:17 -07004284 /* workqueue context doesn't hold uring_lock, grab it now */
4285 if (in_async)
4286 mutex_lock(&ctx->uring_lock);
4287
Jens Axboedef596e2019-01-09 08:59:42 -07004288 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004289
4290 if (in_async)
4291 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004292 }
4293
4294 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004295}
4296
Jens Axboe561fb042019-10-24 07:25:42 -06004297static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004298{
Jens Axboe561fb042019-10-24 07:25:42 -06004299 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004300 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004301 struct io_kiocb *nxt = NULL;
4302 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004303
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004304 /* if NO_CANCEL is set, we must still run the work */
4305 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4306 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004307 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004308 }
Jens Axboe31b51512019-01-18 22:56:34 -07004309
Jens Axboe561fb042019-10-24 07:25:42 -06004310 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004311 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4312 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004313 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004314 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004315 /*
4316 * We can get EAGAIN for polled IO even though we're
4317 * forcing a sync submission from here, since we can't
4318 * wait for request slots on the block side.
4319 */
4320 if (ret != -EAGAIN)
4321 break;
4322 cond_resched();
4323 } while (1);
4324 }
Jens Axboe31b51512019-01-18 22:56:34 -07004325
Jens Axboe561fb042019-10-24 07:25:42 -06004326 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004327 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004328
Jens Axboe561fb042019-10-24 07:25:42 -06004329 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004330 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004331 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004332 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004333 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004334
Jens Axboe561fb042019-10-24 07:25:42 -06004335 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004336 if (!ret && nxt)
4337 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004338}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004339
Jens Axboe15b71ab2019-12-11 11:20:36 -07004340static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004341{
Jens Axboed3656342019-12-18 09:50:26 -07004342 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004343 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004344 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4345 return 0;
4346 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004347}
4348
Jens Axboe65e19f52019-10-26 07:20:21 -06004349static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4350 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004351{
Jens Axboe65e19f52019-10-26 07:20:21 -06004352 struct fixed_file_table *table;
4353
Jens Axboe05f3fb32019-12-09 11:22:50 -07004354 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4355 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004356}
4357
Jens Axboe3529d8c2019-12-19 18:24:38 -07004358static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4359 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004360{
Jackie Liua197f662019-11-08 08:09:12 -07004361 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004362 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004363 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004364
Jens Axboe3529d8c2019-12-19 18:24:38 -07004365 flags = READ_ONCE(sqe->flags);
4366 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004367
Jens Axboed3656342019-12-18 09:50:26 -07004368 if (!io_req_needs_file(req, fd))
4369 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004370
4371 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004372 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004373 (unsigned) fd >= ctx->nr_user_files))
4374 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004375 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004376 req->file = io_file_from_index(ctx, fd);
4377 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004378 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004379 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004380 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004381 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004382 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004383 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004384 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004385 req->file = io_file_get(state, fd);
4386 if (unlikely(!req->file))
4387 return -EBADF;
4388 }
4389
4390 return 0;
4391}
4392
Jackie Liua197f662019-11-08 08:09:12 -07004393static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004394{
Jens Axboefcb323c2019-10-24 12:39:47 -06004395 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004396 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004397
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004398 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004399 return -EBADF;
4400
Jens Axboefcb323c2019-10-24 12:39:47 -06004401 rcu_read_lock();
4402 spin_lock_irq(&ctx->inflight_lock);
4403 /*
4404 * We use the f_ops->flush() handler to ensure that we can flush
4405 * out work accessing these files if the fd is closed. Check if
4406 * the fd has changed since we started down this path, and disallow
4407 * this operation if it has.
4408 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004409 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004410 list_add(&req->inflight_entry, &ctx->inflight_list);
4411 req->flags |= REQ_F_INFLIGHT;
4412 req->work.files = current->files;
4413 ret = 0;
4414 }
4415 spin_unlock_irq(&ctx->inflight_lock);
4416 rcu_read_unlock();
4417
4418 return ret;
4419}
4420
Jens Axboe2665abf2019-11-05 12:40:47 -07004421static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4422{
Jens Axboead8a48a2019-11-15 08:49:11 -07004423 struct io_timeout_data *data = container_of(timer,
4424 struct io_timeout_data, timer);
4425 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004426 struct io_ring_ctx *ctx = req->ctx;
4427 struct io_kiocb *prev = NULL;
4428 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004429
4430 spin_lock_irqsave(&ctx->completion_lock, flags);
4431
4432 /*
4433 * We don't expect the list to be empty, that will only happen if we
4434 * race with the completion of the linked work.
4435 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004436 if (!list_empty(&req->link_list)) {
4437 prev = list_entry(req->link_list.prev, struct io_kiocb,
4438 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004439 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004440 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004441 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4442 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004443 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004444 }
4445
4446 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4447
4448 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004449 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004450 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4451 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004452 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004453 } else {
4454 io_cqring_add_event(req, -ETIME);
4455 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004456 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004457 return HRTIMER_NORESTART;
4458}
4459
Jens Axboead8a48a2019-11-15 08:49:11 -07004460static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004461{
Jens Axboe76a46e02019-11-10 23:34:16 -07004462 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004463
Jens Axboe76a46e02019-11-10 23:34:16 -07004464 /*
4465 * If the list is now empty, then our linked request finished before
4466 * we got a chance to setup the timer
4467 */
4468 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004469 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004470 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004471
Jens Axboead8a48a2019-11-15 08:49:11 -07004472 data->timer.function = io_link_timeout_fn;
4473 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4474 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004475 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004476 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004477
Jens Axboe2665abf2019-11-05 12:40:47 -07004478 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004479 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004480}
4481
Jens Axboead8a48a2019-11-15 08:49:11 -07004482static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004483{
4484 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004485
Jens Axboe2665abf2019-11-05 12:40:47 -07004486 if (!(req->flags & REQ_F_LINK))
4487 return NULL;
4488
Pavel Begunkov44932332019-12-05 16:16:35 +03004489 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4490 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004491 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004492 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004493
Jens Axboe76a46e02019-11-10 23:34:16 -07004494 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004495 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004496}
4497
Jens Axboe3529d8c2019-12-19 18:24:38 -07004498static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004499{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004500 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004501 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004502 int ret;
4503
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004504again:
4505 linked_timeout = io_prep_linked_timeout(req);
4506
Jens Axboe3529d8c2019-12-19 18:24:38 -07004507 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004508
4509 /*
4510 * We async punt it if the file wasn't marked NOWAIT, or if the file
4511 * doesn't support non-blocking read/write attempts
4512 */
4513 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4514 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004515punt:
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004516 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4517 ret = io_grab_files(req);
4518 if (ret)
4519 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004520 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004521
4522 /*
4523 * Queued up for async execution, worker will release
4524 * submit reference when the iocb is actually submitted.
4525 */
4526 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004527 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004528 }
Jens Axboee65ef562019-03-12 10:16:44 -06004529
Jens Axboefcb323c2019-10-24 12:39:47 -06004530err:
Jens Axboee65ef562019-03-12 10:16:44 -06004531 /* drop submission reference */
4532 io_put_req(req);
4533
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004534 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004535 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004536 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004537 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004538 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004539 }
4540
Jens Axboee65ef562019-03-12 10:16:44 -06004541 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004542 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004543 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004544 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004545 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004546 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004547done_req:
4548 if (nxt) {
4549 req = nxt;
4550 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004551
4552 if (req->flags & REQ_F_FORCE_ASYNC)
4553 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004554 goto again;
4555 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004556}
4557
Jens Axboe3529d8c2019-12-19 18:24:38 -07004558static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004559{
4560 int ret;
4561
Jens Axboe3529d8c2019-12-19 18:24:38 -07004562 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004563 if (ret) {
4564 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004565fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004566 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004567 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004568 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004569 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004570 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004571 ret = io_req_defer_prep(req, sqe);
4572 if (unlikely(ret < 0))
4573 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004574 /*
4575 * Never try inline submit of IOSQE_ASYNC is set, go straight
4576 * to async execution.
4577 */
4578 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4579 io_queue_async_work(req);
4580 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004581 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004582 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004583}
4584
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004585static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004586{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004587 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004588 io_cqring_add_event(req, -ECANCELED);
4589 io_double_put_req(req);
4590 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004591 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004592}
4593
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004594#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004595 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004596
Jens Axboe3529d8c2019-12-19 18:24:38 -07004597static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4598 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004599{
Jackie Liua197f662019-11-08 08:09:12 -07004600 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004601 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004602 int ret;
4603
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004604 sqe_flags = READ_ONCE(sqe->flags);
4605
Jens Axboe9e645e112019-05-10 16:07:28 -06004606 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004607 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004608 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004609 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004610 }
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004611 /* same numerical values with corresponding REQ_F_*, safe to copy */
4612 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4613 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004614
Jens Axboe3529d8c2019-12-19 18:24:38 -07004615 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004616 if (unlikely(ret)) {
4617err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004618 io_cqring_add_event(req, ret);
4619 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004620 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004621 }
4622
Jens Axboe9e645e112019-05-10 16:07:28 -06004623 /*
4624 * If we already have a head request, queue this one for async
4625 * submittal once the head completes. If we don't have a head but
4626 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4627 * submitted sync once the chain is complete. If none of those
4628 * conditions are true (normal request), then just queue it.
4629 */
4630 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004631 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004632
Pavel Begunkov711be032020-01-17 03:57:59 +03004633 if (sqe_flags & IOSQE_IO_DRAIN) {
4634 head->flags |= REQ_F_IO_DRAIN;
4635 ctx->drain_next = 1;
4636 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004637 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004638 ret = -EAGAIN;
4639 goto err_req;
4640 }
4641
Jens Axboe3529d8c2019-12-19 18:24:38 -07004642 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004643 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004644 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004645 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004646 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004647 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004648 trace_io_uring_link(ctx, req, head);
4649 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004650
4651 /* last request of a link, enqueue the link */
4652 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4653 io_queue_link_head(head);
4654 *link = NULL;
4655 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004656 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004657 if (unlikely(ctx->drain_next)) {
4658 req->flags |= REQ_F_IO_DRAIN;
4659 req->ctx->drain_next = 0;
4660 }
4661 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4662 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004663 INIT_LIST_HEAD(&req->link_list);
4664 ret = io_req_defer_prep(req, sqe);
4665 if (ret)
4666 req->flags |= REQ_F_FAIL_LINK;
4667 *link = req;
4668 } else {
4669 io_queue_sqe(req, sqe);
4670 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004671 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004672
4673 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004674}
4675
Jens Axboe9a56a232019-01-09 09:06:50 -07004676/*
4677 * Batched submission is done, ensure local IO is flushed out.
4678 */
4679static void io_submit_state_end(struct io_submit_state *state)
4680{
4681 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004682 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004683 if (state->free_reqs)
4684 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4685 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004686}
4687
4688/*
4689 * Start submission side cache.
4690 */
4691static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004692 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004693{
4694 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004695 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004696 state->file = NULL;
4697 state->ios_left = max_ios;
4698}
4699
Jens Axboe2b188cc2019-01-07 10:46:33 -07004700static void io_commit_sqring(struct io_ring_ctx *ctx)
4701{
Hristo Venev75b28af2019-08-26 17:23:46 +00004702 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004703
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004704 /*
4705 * Ensure any loads from the SQEs are done at this point,
4706 * since once we write the new head, the application could
4707 * write new data to them.
4708 */
4709 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004710}
4711
4712/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004713 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004714 * that is mapped by userspace. This means that care needs to be taken to
4715 * ensure that reads are stable, as we cannot rely on userspace always
4716 * being a good citizen. If members of the sqe are validated and then later
4717 * used, it's important that those reads are done through READ_ONCE() to
4718 * prevent a re-load down the line.
4719 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004720static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4721 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004722{
Hristo Venev75b28af2019-08-26 17:23:46 +00004723 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004724 unsigned head;
4725
4726 /*
4727 * The cached sq head (or cq tail) serves two purposes:
4728 *
4729 * 1) allows us to batch the cost of updating the user visible
4730 * head updates.
4731 * 2) allows the kernel side to track the head on its own, even
4732 * though the application is the one updating it.
4733 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004734 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004735 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004736 /*
4737 * All io need record the previous position, if LINK vs DARIN,
4738 * it can be used to mark the position of the first IO in the
4739 * link list.
4740 */
4741 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004742 *sqe_ptr = &ctx->sq_sqes[head];
4743 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4744 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004745 ctx->cached_sq_head++;
4746 return true;
4747 }
4748
4749 /* drop invalid entries */
4750 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004751 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004752 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004753 return false;
4754}
4755
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004756static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004757 struct file *ring_file, int ring_fd,
4758 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004759{
4760 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004761 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004762 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004763 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004764
Jens Axboec4a2ed72019-11-21 21:01:26 -07004765 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004766 if (test_bit(0, &ctx->sq_check_overflow)) {
4767 if (!list_empty(&ctx->cq_overflow_list) &&
4768 !io_cqring_overflow_flush(ctx, false))
4769 return -EBUSY;
4770 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004771
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004772 /* make sure SQ entry isn't read before tail */
4773 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004774
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004775 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4776 return -EAGAIN;
4777
Jens Axboe6c271ce2019-01-10 11:22:30 -07004778 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004779 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004780 statep = &state;
4781 }
4782
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004783 ctx->ring_fd = ring_fd;
4784 ctx->ring_file = ring_file;
4785
Jens Axboe6c271ce2019-01-10 11:22:30 -07004786 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004787 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004788 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004789
Pavel Begunkov196be952019-11-07 01:41:06 +03004790 req = io_get_req(ctx, statep);
4791 if (unlikely(!req)) {
4792 if (!submitted)
4793 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004794 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004795 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004796 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004797 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004798 break;
4799 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004800
Jens Axboed3656342019-12-18 09:50:26 -07004801 /* will complete beyond this point, count as submitted */
4802 submitted++;
4803
4804 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4805 io_cqring_add_event(req, -EINVAL);
4806 io_double_put_req(req);
4807 break;
4808 }
4809
4810 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004811 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4812 if (!mm_fault) {
4813 use_mm(ctx->sqo_mm);
4814 *mm = ctx->sqo_mm;
4815 }
4816 }
4817
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004818 req->has_user = *mm != NULL;
4819 req->in_async = async;
4820 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07004821 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
4822 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004823 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004824 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004825 }
4826
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004827 if (submitted != nr)
4828 percpu_ref_put_many(&ctx->refs, nr - submitted);
Jens Axboe9e645e112019-05-10 16:07:28 -06004829 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004830 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004831 if (statep)
4832 io_submit_state_end(&state);
4833
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004834 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4835 io_commit_sqring(ctx);
4836
Jens Axboe6c271ce2019-01-10 11:22:30 -07004837 return submitted;
4838}
4839
4840static int io_sq_thread(void *data)
4841{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004842 struct io_ring_ctx *ctx = data;
4843 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004844 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004845 mm_segment_t old_fs;
4846 DEFINE_WAIT(wait);
4847 unsigned inflight;
4848 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004849 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004850
Jens Axboe206aefd2019-11-07 18:27:42 -07004851 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004852
Jens Axboe6c271ce2019-01-10 11:22:30 -07004853 old_fs = get_fs();
4854 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004855 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004856
Jens Axboec1edbf52019-11-10 16:56:04 -07004857 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004858 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004859 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004860
4861 if (inflight) {
4862 unsigned nr_events = 0;
4863
4864 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004865 /*
4866 * inflight is the count of the maximum possible
4867 * entries we submitted, but it can be smaller
4868 * if we dropped some of them. If we don't have
4869 * poll entries available, then we know that we
4870 * have nothing left to poll for. Reset the
4871 * inflight count to zero in that case.
4872 */
4873 mutex_lock(&ctx->uring_lock);
4874 if (!list_empty(&ctx->poll_list))
4875 __io_iopoll_check(ctx, &nr_events, 0);
4876 else
4877 inflight = 0;
4878 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004879 } else {
4880 /*
4881 * Normal IO, just pretend everything completed.
4882 * We don't have to poll completions for that.
4883 */
4884 nr_events = inflight;
4885 }
4886
4887 inflight -= nr_events;
4888 if (!inflight)
4889 timeout = jiffies + ctx->sq_thread_idle;
4890 }
4891
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004892 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004893
4894 /*
4895 * If submit got -EBUSY, flag us as needing the application
4896 * to enter the kernel to reap and flush events.
4897 */
4898 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004899 /*
4900 * We're polling. If we're within the defined idle
4901 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004902 * to sleep. The exception is if we got EBUSY doing
4903 * more IO, we should wait for the application to
4904 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004905 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004906 if (inflight ||
4907 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004908 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004909 continue;
4910 }
4911
4912 /*
4913 * Drop cur_mm before scheduling, we can't hold it for
4914 * long periods (or over schedule()). Do this before
4915 * adding ourselves to the waitqueue, as the unuse/drop
4916 * may sleep.
4917 */
4918 if (cur_mm) {
4919 unuse_mm(cur_mm);
4920 mmput(cur_mm);
4921 cur_mm = NULL;
4922 }
4923
4924 prepare_to_wait(&ctx->sqo_wait, &wait,
4925 TASK_INTERRUPTIBLE);
4926
4927 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004928 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004929 /* make sure to read SQ tail after writing flags */
4930 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004931
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004932 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004933 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004934 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004935 finish_wait(&ctx->sqo_wait, &wait);
4936 break;
4937 }
4938 if (signal_pending(current))
4939 flush_signals(current);
4940 schedule();
4941 finish_wait(&ctx->sqo_wait, &wait);
4942
Hristo Venev75b28af2019-08-26 17:23:46 +00004943 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004944 continue;
4945 }
4946 finish_wait(&ctx->sqo_wait, &wait);
4947
Hristo Venev75b28af2019-08-26 17:23:46 +00004948 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004949 }
4950
Jens Axboe8a4955f2019-12-09 14:52:35 -07004951 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004952 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004953 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004954 if (ret > 0)
4955 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004956 }
4957
4958 set_fs(old_fs);
4959 if (cur_mm) {
4960 unuse_mm(cur_mm);
4961 mmput(cur_mm);
4962 }
Jens Axboe181e4482019-11-25 08:52:30 -07004963 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004964
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004965 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004966
Jens Axboe6c271ce2019-01-10 11:22:30 -07004967 return 0;
4968}
4969
Jens Axboebda52162019-09-24 13:47:15 -06004970struct io_wait_queue {
4971 struct wait_queue_entry wq;
4972 struct io_ring_ctx *ctx;
4973 unsigned to_wait;
4974 unsigned nr_timeouts;
4975};
4976
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004977static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004978{
4979 struct io_ring_ctx *ctx = iowq->ctx;
4980
4981 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004982 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004983 * started waiting. For timeouts, we always want to return to userspace,
4984 * regardless of event count.
4985 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004986 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004987 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4988}
4989
4990static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4991 int wake_flags, void *key)
4992{
4993 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4994 wq);
4995
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004996 /* use noflush == true, as we can't safely rely on locking context */
4997 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004998 return -1;
4999
5000 return autoremove_wake_function(curr, mode, wake_flags, key);
5001}
5002
Jens Axboe2b188cc2019-01-07 10:46:33 -07005003/*
5004 * Wait until events become available, if we don't already have some. The
5005 * application must reap them itself, as they reside on the shared cq ring.
5006 */
5007static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5008 const sigset_t __user *sig, size_t sigsz)
5009{
Jens Axboebda52162019-09-24 13:47:15 -06005010 struct io_wait_queue iowq = {
5011 .wq = {
5012 .private = current,
5013 .func = io_wake_function,
5014 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5015 },
5016 .ctx = ctx,
5017 .to_wait = min_events,
5018 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005019 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005020 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005021
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005022 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005023 return 0;
5024
5025 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005026#ifdef CONFIG_COMPAT
5027 if (in_compat_syscall())
5028 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005029 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005030 else
5031#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005032 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005033
Jens Axboe2b188cc2019-01-07 10:46:33 -07005034 if (ret)
5035 return ret;
5036 }
5037
Jens Axboebda52162019-09-24 13:47:15 -06005038 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005039 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005040 do {
5041 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5042 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005043 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005044 break;
5045 schedule();
5046 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005047 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005048 break;
5049 }
5050 } while (1);
5051 finish_wait(&ctx->wait, &iowq.wq);
5052
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005053 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005054
Hristo Venev75b28af2019-08-26 17:23:46 +00005055 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005056}
5057
Jens Axboe6b063142019-01-10 22:13:58 -07005058static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5059{
5060#if defined(CONFIG_UNIX)
5061 if (ctx->ring_sock) {
5062 struct sock *sock = ctx->ring_sock->sk;
5063 struct sk_buff *skb;
5064
5065 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5066 kfree_skb(skb);
5067 }
5068#else
5069 int i;
5070
Jens Axboe65e19f52019-10-26 07:20:21 -06005071 for (i = 0; i < ctx->nr_user_files; i++) {
5072 struct file *file;
5073
5074 file = io_file_from_index(ctx, i);
5075 if (file)
5076 fput(file);
5077 }
Jens Axboe6b063142019-01-10 22:13:58 -07005078#endif
5079}
5080
Jens Axboe05f3fb32019-12-09 11:22:50 -07005081static void io_file_ref_kill(struct percpu_ref *ref)
5082{
5083 struct fixed_file_data *data;
5084
5085 data = container_of(ref, struct fixed_file_data, refs);
5086 complete(&data->done);
5087}
5088
Jens Axboe6b063142019-01-10 22:13:58 -07005089static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5090{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005091 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005092 unsigned nr_tables, i;
5093
Jens Axboe05f3fb32019-12-09 11:22:50 -07005094 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005095 return -ENXIO;
5096
Jens Axboe05f3fb32019-12-09 11:22:50 -07005097 /* protect against inflight atomic switch, which drops the ref */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005098 percpu_ref_get(&data->refs);
Jens Axboee46a7952020-01-17 11:15:34 -07005099 /* wait for existing switches */
5100 flush_work(&data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005101 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5102 wait_for_completion(&data->done);
5103 percpu_ref_put(&data->refs);
Jens Axboee46a7952020-01-17 11:15:34 -07005104 /* flush potential new switch */
5105 flush_work(&data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005106 percpu_ref_exit(&data->refs);
5107
Jens Axboe6b063142019-01-10 22:13:58 -07005108 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005109 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5110 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005111 kfree(data->table[i].files);
5112 kfree(data->table);
5113 kfree(data);
5114 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005115 ctx->nr_user_files = 0;
5116 return 0;
5117}
5118
Jens Axboe6c271ce2019-01-10 11:22:30 -07005119static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5120{
5121 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005122 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005123 /*
5124 * The park is a bit of a work-around, without it we get
5125 * warning spews on shutdown with SQPOLL set and affinity
5126 * set to a single CPU.
5127 */
Jens Axboe06058632019-04-13 09:26:03 -06005128 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005129 kthread_stop(ctx->sqo_thread);
5130 ctx->sqo_thread = NULL;
5131 }
5132}
5133
Jens Axboe6b063142019-01-10 22:13:58 -07005134static void io_finish_async(struct io_ring_ctx *ctx)
5135{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005136 io_sq_thread_stop(ctx);
5137
Jens Axboe561fb042019-10-24 07:25:42 -06005138 if (ctx->io_wq) {
5139 io_wq_destroy(ctx->io_wq);
5140 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005141 }
5142}
5143
5144#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005145/*
5146 * Ensure the UNIX gc is aware of our file set, so we are certain that
5147 * the io_uring can be safely unregistered on process exit, even if we have
5148 * loops in the file referencing.
5149 */
5150static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5151{
5152 struct sock *sk = ctx->ring_sock->sk;
5153 struct scm_fp_list *fpl;
5154 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005155 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005156
5157 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5158 unsigned long inflight = ctx->user->unix_inflight + nr;
5159
5160 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5161 return -EMFILE;
5162 }
5163
5164 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5165 if (!fpl)
5166 return -ENOMEM;
5167
5168 skb = alloc_skb(0, GFP_KERNEL);
5169 if (!skb) {
5170 kfree(fpl);
5171 return -ENOMEM;
5172 }
5173
5174 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005175
Jens Axboe08a45172019-10-03 08:11:03 -06005176 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005177 fpl->user = get_uid(ctx->user);
5178 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005179 struct file *file = io_file_from_index(ctx, i + offset);
5180
5181 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005182 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005183 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005184 unix_inflight(fpl->user, fpl->fp[nr_files]);
5185 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005186 }
5187
Jens Axboe08a45172019-10-03 08:11:03 -06005188 if (nr_files) {
5189 fpl->max = SCM_MAX_FD;
5190 fpl->count = nr_files;
5191 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005192 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005193 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5194 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005195
Jens Axboe08a45172019-10-03 08:11:03 -06005196 for (i = 0; i < nr_files; i++)
5197 fput(fpl->fp[i]);
5198 } else {
5199 kfree_skb(skb);
5200 kfree(fpl);
5201 }
Jens Axboe6b063142019-01-10 22:13:58 -07005202
5203 return 0;
5204}
5205
5206/*
5207 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5208 * causes regular reference counting to break down. We rely on the UNIX
5209 * garbage collection to take care of this problem for us.
5210 */
5211static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5212{
5213 unsigned left, total;
5214 int ret = 0;
5215
5216 total = 0;
5217 left = ctx->nr_user_files;
5218 while (left) {
5219 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005220
5221 ret = __io_sqe_files_scm(ctx, this_files, total);
5222 if (ret)
5223 break;
5224 left -= this_files;
5225 total += this_files;
5226 }
5227
5228 if (!ret)
5229 return 0;
5230
5231 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005232 struct file *file = io_file_from_index(ctx, total);
5233
5234 if (file)
5235 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005236 total++;
5237 }
5238
5239 return ret;
5240}
5241#else
5242static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5243{
5244 return 0;
5245}
5246#endif
5247
Jens Axboe65e19f52019-10-26 07:20:21 -06005248static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5249 unsigned nr_files)
5250{
5251 int i;
5252
5253 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005254 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005255 unsigned this_files;
5256
5257 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5258 table->files = kcalloc(this_files, sizeof(struct file *),
5259 GFP_KERNEL);
5260 if (!table->files)
5261 break;
5262 nr_files -= this_files;
5263 }
5264
5265 if (i == nr_tables)
5266 return 0;
5267
5268 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005269 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005270 kfree(table->files);
5271 }
5272 return 1;
5273}
5274
Jens Axboe05f3fb32019-12-09 11:22:50 -07005275static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005276{
5277#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005278 struct sock *sock = ctx->ring_sock->sk;
5279 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5280 struct sk_buff *skb;
5281 int i;
5282
5283 __skb_queue_head_init(&list);
5284
5285 /*
5286 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5287 * remove this entry and rearrange the file array.
5288 */
5289 skb = skb_dequeue(head);
5290 while (skb) {
5291 struct scm_fp_list *fp;
5292
5293 fp = UNIXCB(skb).fp;
5294 for (i = 0; i < fp->count; i++) {
5295 int left;
5296
5297 if (fp->fp[i] != file)
5298 continue;
5299
5300 unix_notinflight(fp->user, fp->fp[i]);
5301 left = fp->count - 1 - i;
5302 if (left) {
5303 memmove(&fp->fp[i], &fp->fp[i + 1],
5304 left * sizeof(struct file *));
5305 }
5306 fp->count--;
5307 if (!fp->count) {
5308 kfree_skb(skb);
5309 skb = NULL;
5310 } else {
5311 __skb_queue_tail(&list, skb);
5312 }
5313 fput(file);
5314 file = NULL;
5315 break;
5316 }
5317
5318 if (!file)
5319 break;
5320
5321 __skb_queue_tail(&list, skb);
5322
5323 skb = skb_dequeue(head);
5324 }
5325
5326 if (skb_peek(&list)) {
5327 spin_lock_irq(&head->lock);
5328 while ((skb = __skb_dequeue(&list)) != NULL)
5329 __skb_queue_tail(head, skb);
5330 spin_unlock_irq(&head->lock);
5331 }
5332#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005333 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005334#endif
5335}
5336
Jens Axboe05f3fb32019-12-09 11:22:50 -07005337struct io_file_put {
5338 struct llist_node llist;
5339 struct file *file;
5340 struct completion *done;
5341};
5342
5343static void io_ring_file_ref_switch(struct work_struct *work)
5344{
5345 struct io_file_put *pfile, *tmp;
5346 struct fixed_file_data *data;
5347 struct llist_node *node;
5348
5349 data = container_of(work, struct fixed_file_data, ref_work);
5350
5351 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5352 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5353 io_ring_file_put(data->ctx, pfile->file);
5354 if (pfile->done)
5355 complete(pfile->done);
5356 else
5357 kfree(pfile);
5358 }
5359 }
5360
5361 percpu_ref_get(&data->refs);
5362 percpu_ref_switch_to_percpu(&data->refs);
5363}
5364
5365static void io_file_data_ref_zero(struct percpu_ref *ref)
5366{
5367 struct fixed_file_data *data;
5368
5369 data = container_of(ref, struct fixed_file_data, refs);
5370
5371 /* we can't safely switch from inside this context, punt to wq */
5372 queue_work(system_wq, &data->ref_work);
5373}
5374
5375static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5376 unsigned nr_args)
5377{
5378 __s32 __user *fds = (__s32 __user *) arg;
5379 unsigned nr_tables;
5380 struct file *file;
5381 int fd, ret = 0;
5382 unsigned i;
5383
5384 if (ctx->file_data)
5385 return -EBUSY;
5386 if (!nr_args)
5387 return -EINVAL;
5388 if (nr_args > IORING_MAX_FIXED_FILES)
5389 return -EMFILE;
5390
5391 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5392 if (!ctx->file_data)
5393 return -ENOMEM;
5394 ctx->file_data->ctx = ctx;
5395 init_completion(&ctx->file_data->done);
5396
5397 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5398 ctx->file_data->table = kcalloc(nr_tables,
5399 sizeof(struct fixed_file_table),
5400 GFP_KERNEL);
5401 if (!ctx->file_data->table) {
5402 kfree(ctx->file_data);
5403 ctx->file_data = NULL;
5404 return -ENOMEM;
5405 }
5406
5407 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5408 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5409 kfree(ctx->file_data->table);
5410 kfree(ctx->file_data);
5411 ctx->file_data = NULL;
5412 return -ENOMEM;
5413 }
5414 ctx->file_data->put_llist.first = NULL;
5415 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5416
5417 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5418 percpu_ref_exit(&ctx->file_data->refs);
5419 kfree(ctx->file_data->table);
5420 kfree(ctx->file_data);
5421 ctx->file_data = NULL;
5422 return -ENOMEM;
5423 }
5424
5425 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5426 struct fixed_file_table *table;
5427 unsigned index;
5428
5429 ret = -EFAULT;
5430 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5431 break;
5432 /* allow sparse sets */
5433 if (fd == -1) {
5434 ret = 0;
5435 continue;
5436 }
5437
5438 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5439 index = i & IORING_FILE_TABLE_MASK;
5440 file = fget(fd);
5441
5442 ret = -EBADF;
5443 if (!file)
5444 break;
5445
5446 /*
5447 * Don't allow io_uring instances to be registered. If UNIX
5448 * isn't enabled, then this causes a reference cycle and this
5449 * instance can never get freed. If UNIX is enabled we'll
5450 * handle it just fine, but there's still no point in allowing
5451 * a ring fd as it doesn't support regular read/write anyway.
5452 */
5453 if (file->f_op == &io_uring_fops) {
5454 fput(file);
5455 break;
5456 }
5457 ret = 0;
5458 table->files[index] = file;
5459 }
5460
5461 if (ret) {
5462 for (i = 0; i < ctx->nr_user_files; i++) {
5463 file = io_file_from_index(ctx, i);
5464 if (file)
5465 fput(file);
5466 }
5467 for (i = 0; i < nr_tables; i++)
5468 kfree(ctx->file_data->table[i].files);
5469
5470 kfree(ctx->file_data->table);
5471 kfree(ctx->file_data);
5472 ctx->file_data = NULL;
5473 ctx->nr_user_files = 0;
5474 return ret;
5475 }
5476
5477 ret = io_sqe_files_scm(ctx);
5478 if (ret)
5479 io_sqe_files_unregister(ctx);
5480
5481 return ret;
5482}
5483
Jens Axboec3a31e62019-10-03 13:59:56 -06005484static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5485 int index)
5486{
5487#if defined(CONFIG_UNIX)
5488 struct sock *sock = ctx->ring_sock->sk;
5489 struct sk_buff_head *head = &sock->sk_receive_queue;
5490 struct sk_buff *skb;
5491
5492 /*
5493 * See if we can merge this file into an existing skb SCM_RIGHTS
5494 * file set. If there's no room, fall back to allocating a new skb
5495 * and filling it in.
5496 */
5497 spin_lock_irq(&head->lock);
5498 skb = skb_peek(head);
5499 if (skb) {
5500 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5501
5502 if (fpl->count < SCM_MAX_FD) {
5503 __skb_unlink(skb, head);
5504 spin_unlock_irq(&head->lock);
5505 fpl->fp[fpl->count] = get_file(file);
5506 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5507 fpl->count++;
5508 spin_lock_irq(&head->lock);
5509 __skb_queue_head(head, skb);
5510 } else {
5511 skb = NULL;
5512 }
5513 }
5514 spin_unlock_irq(&head->lock);
5515
5516 if (skb) {
5517 fput(file);
5518 return 0;
5519 }
5520
5521 return __io_sqe_files_scm(ctx, 1, index);
5522#else
5523 return 0;
5524#endif
5525}
5526
Jens Axboe05f3fb32019-12-09 11:22:50 -07005527static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005528{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005529 struct fixed_file_data *data;
5530
5531 data = container_of(ref, struct fixed_file_data, refs);
5532 clear_bit(FFD_F_ATOMIC, &data->state);
5533}
5534
5535static bool io_queue_file_removal(struct fixed_file_data *data,
5536 struct file *file)
5537{
5538 struct io_file_put *pfile, pfile_stack;
5539 DECLARE_COMPLETION_ONSTACK(done);
5540
5541 /*
5542 * If we fail allocating the struct we need for doing async reomval
5543 * of this file, just punt to sync and wait for it.
5544 */
5545 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5546 if (!pfile) {
5547 pfile = &pfile_stack;
5548 pfile->done = &done;
5549 }
5550
5551 pfile->file = file;
5552 llist_add(&pfile->llist, &data->put_llist);
5553
5554 if (pfile == &pfile_stack) {
5555 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5556 percpu_ref_put(&data->refs);
5557 percpu_ref_switch_to_atomic(&data->refs,
5558 io_atomic_switch);
5559 }
5560 wait_for_completion(&done);
5561 flush_work(&data->ref_work);
5562 return false;
5563 }
5564
5565 return true;
5566}
5567
5568static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5569 struct io_uring_files_update *up,
5570 unsigned nr_args)
5571{
5572 struct fixed_file_data *data = ctx->file_data;
5573 bool ref_switch = false;
5574 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005575 __s32 __user *fds;
5576 int fd, i, err;
5577 __u32 done;
5578
Jens Axboe05f3fb32019-12-09 11:22:50 -07005579 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005580 return -EOVERFLOW;
5581 if (done > ctx->nr_user_files)
5582 return -EINVAL;
5583
5584 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005585 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005586 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005587 struct fixed_file_table *table;
5588 unsigned index;
5589
Jens Axboec3a31e62019-10-03 13:59:56 -06005590 err = 0;
5591 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5592 err = -EFAULT;
5593 break;
5594 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005595 i = array_index_nospec(up->offset, ctx->nr_user_files);
5596 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005597 index = i & IORING_FILE_TABLE_MASK;
5598 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005599 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005600 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005601 if (io_queue_file_removal(data, file))
5602 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005603 }
5604 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005605 file = fget(fd);
5606 if (!file) {
5607 err = -EBADF;
5608 break;
5609 }
5610 /*
5611 * Don't allow io_uring instances to be registered. If
5612 * UNIX isn't enabled, then this causes a reference
5613 * cycle and this instance can never get freed. If UNIX
5614 * is enabled we'll handle it just fine, but there's
5615 * still no point in allowing a ring fd as it doesn't
5616 * support regular read/write anyway.
5617 */
5618 if (file->f_op == &io_uring_fops) {
5619 fput(file);
5620 err = -EBADF;
5621 break;
5622 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005623 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005624 err = io_sqe_file_register(ctx, file, i);
5625 if (err)
5626 break;
5627 }
5628 nr_args--;
5629 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005630 up->offset++;
5631 }
5632
5633 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5634 percpu_ref_put(&data->refs);
5635 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005636 }
5637
5638 return done ? done : err;
5639}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005640static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5641 unsigned nr_args)
5642{
5643 struct io_uring_files_update up;
5644
5645 if (!ctx->file_data)
5646 return -ENXIO;
5647 if (!nr_args)
5648 return -EINVAL;
5649 if (copy_from_user(&up, arg, sizeof(up)))
5650 return -EFAULT;
5651 if (up.resv)
5652 return -EINVAL;
5653
5654 return __io_sqe_files_update(ctx, &up, nr_args);
5655}
Jens Axboec3a31e62019-10-03 13:59:56 -06005656
Jens Axboe7d723062019-11-12 22:31:31 -07005657static void io_put_work(struct io_wq_work *work)
5658{
5659 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5660
5661 io_put_req(req);
5662}
5663
5664static void io_get_work(struct io_wq_work *work)
5665{
5666 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5667
5668 refcount_inc(&req->refs);
5669}
5670
Jens Axboe6c271ce2019-01-10 11:22:30 -07005671static int io_sq_offload_start(struct io_ring_ctx *ctx,
5672 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005673{
Jens Axboe576a3472019-11-25 08:49:20 -07005674 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005675 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005676 int ret;
5677
Jens Axboe6c271ce2019-01-10 11:22:30 -07005678 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005679 mmgrab(current->mm);
5680 ctx->sqo_mm = current->mm;
5681
Jens Axboe6c271ce2019-01-10 11:22:30 -07005682 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005683 ret = -EPERM;
5684 if (!capable(CAP_SYS_ADMIN))
5685 goto err;
5686
Jens Axboe917257d2019-04-13 09:28:55 -06005687 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5688 if (!ctx->sq_thread_idle)
5689 ctx->sq_thread_idle = HZ;
5690
Jens Axboe6c271ce2019-01-10 11:22:30 -07005691 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005692 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005693
Jens Axboe917257d2019-04-13 09:28:55 -06005694 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005695 if (cpu >= nr_cpu_ids)
5696 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005697 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005698 goto err;
5699
Jens Axboe6c271ce2019-01-10 11:22:30 -07005700 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5701 ctx, cpu,
5702 "io_uring-sq");
5703 } else {
5704 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5705 "io_uring-sq");
5706 }
5707 if (IS_ERR(ctx->sqo_thread)) {
5708 ret = PTR_ERR(ctx->sqo_thread);
5709 ctx->sqo_thread = NULL;
5710 goto err;
5711 }
5712 wake_up_process(ctx->sqo_thread);
5713 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5714 /* Can't have SQ_AFF without SQPOLL */
5715 ret = -EINVAL;
5716 goto err;
5717 }
5718
Jens Axboe576a3472019-11-25 08:49:20 -07005719 data.mm = ctx->sqo_mm;
5720 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005721 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005722 data.get_work = io_get_work;
5723 data.put_work = io_put_work;
5724
Jens Axboe561fb042019-10-24 07:25:42 -06005725 /* Do QD, or 4 * CPUS, whatever is smallest */
5726 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005727 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005728 if (IS_ERR(ctx->io_wq)) {
5729 ret = PTR_ERR(ctx->io_wq);
5730 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005731 goto err;
5732 }
5733
5734 return 0;
5735err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005736 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005737 mmdrop(ctx->sqo_mm);
5738 ctx->sqo_mm = NULL;
5739 return ret;
5740}
5741
5742static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5743{
5744 atomic_long_sub(nr_pages, &user->locked_vm);
5745}
5746
5747static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5748{
5749 unsigned long page_limit, cur_pages, new_pages;
5750
5751 /* Don't allow more pages than we can safely lock */
5752 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5753
5754 do {
5755 cur_pages = atomic_long_read(&user->locked_vm);
5756 new_pages = cur_pages + nr_pages;
5757 if (new_pages > page_limit)
5758 return -ENOMEM;
5759 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5760 new_pages) != cur_pages);
5761
5762 return 0;
5763}
5764
5765static void io_mem_free(void *ptr)
5766{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005767 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005768
Mark Rutland52e04ef2019-04-30 17:30:21 +01005769 if (!ptr)
5770 return;
5771
5772 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005773 if (put_page_testzero(page))
5774 free_compound_page(page);
5775}
5776
5777static void *io_mem_alloc(size_t size)
5778{
5779 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5780 __GFP_NORETRY;
5781
5782 return (void *) __get_free_pages(gfp_flags, get_order(size));
5783}
5784
Hristo Venev75b28af2019-08-26 17:23:46 +00005785static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5786 size_t *sq_offset)
5787{
5788 struct io_rings *rings;
5789 size_t off, sq_array_size;
5790
5791 off = struct_size(rings, cqes, cq_entries);
5792 if (off == SIZE_MAX)
5793 return SIZE_MAX;
5794
5795#ifdef CONFIG_SMP
5796 off = ALIGN(off, SMP_CACHE_BYTES);
5797 if (off == 0)
5798 return SIZE_MAX;
5799#endif
5800
5801 sq_array_size = array_size(sizeof(u32), sq_entries);
5802 if (sq_array_size == SIZE_MAX)
5803 return SIZE_MAX;
5804
5805 if (check_add_overflow(off, sq_array_size, &off))
5806 return SIZE_MAX;
5807
5808 if (sq_offset)
5809 *sq_offset = off;
5810
5811 return off;
5812}
5813
Jens Axboe2b188cc2019-01-07 10:46:33 -07005814static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5815{
Hristo Venev75b28af2019-08-26 17:23:46 +00005816 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005817
Hristo Venev75b28af2019-08-26 17:23:46 +00005818 pages = (size_t)1 << get_order(
5819 rings_size(sq_entries, cq_entries, NULL));
5820 pages += (size_t)1 << get_order(
5821 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005822
Hristo Venev75b28af2019-08-26 17:23:46 +00005823 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005824}
5825
Jens Axboeedafcce2019-01-09 09:16:05 -07005826static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5827{
5828 int i, j;
5829
5830 if (!ctx->user_bufs)
5831 return -ENXIO;
5832
5833 for (i = 0; i < ctx->nr_user_bufs; i++) {
5834 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5835
5836 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005837 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005838
5839 if (ctx->account_mem)
5840 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005841 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005842 imu->nr_bvecs = 0;
5843 }
5844
5845 kfree(ctx->user_bufs);
5846 ctx->user_bufs = NULL;
5847 ctx->nr_user_bufs = 0;
5848 return 0;
5849}
5850
5851static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5852 void __user *arg, unsigned index)
5853{
5854 struct iovec __user *src;
5855
5856#ifdef CONFIG_COMPAT
5857 if (ctx->compat) {
5858 struct compat_iovec __user *ciovs;
5859 struct compat_iovec ciov;
5860
5861 ciovs = (struct compat_iovec __user *) arg;
5862 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5863 return -EFAULT;
5864
Jens Axboed55e5f52019-12-11 16:12:15 -07005865 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005866 dst->iov_len = ciov.iov_len;
5867 return 0;
5868 }
5869#endif
5870 src = (struct iovec __user *) arg;
5871 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5872 return -EFAULT;
5873 return 0;
5874}
5875
5876static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5877 unsigned nr_args)
5878{
5879 struct vm_area_struct **vmas = NULL;
5880 struct page **pages = NULL;
5881 int i, j, got_pages = 0;
5882 int ret = -EINVAL;
5883
5884 if (ctx->user_bufs)
5885 return -EBUSY;
5886 if (!nr_args || nr_args > UIO_MAXIOV)
5887 return -EINVAL;
5888
5889 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5890 GFP_KERNEL);
5891 if (!ctx->user_bufs)
5892 return -ENOMEM;
5893
5894 for (i = 0; i < nr_args; i++) {
5895 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5896 unsigned long off, start, end, ubuf;
5897 int pret, nr_pages;
5898 struct iovec iov;
5899 size_t size;
5900
5901 ret = io_copy_iov(ctx, &iov, arg, i);
5902 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005903 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005904
5905 /*
5906 * Don't impose further limits on the size and buffer
5907 * constraints here, we'll -EINVAL later when IO is
5908 * submitted if they are wrong.
5909 */
5910 ret = -EFAULT;
5911 if (!iov.iov_base || !iov.iov_len)
5912 goto err;
5913
5914 /* arbitrary limit, but we need something */
5915 if (iov.iov_len > SZ_1G)
5916 goto err;
5917
5918 ubuf = (unsigned long) iov.iov_base;
5919 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5920 start = ubuf >> PAGE_SHIFT;
5921 nr_pages = end - start;
5922
5923 if (ctx->account_mem) {
5924 ret = io_account_mem(ctx->user, nr_pages);
5925 if (ret)
5926 goto err;
5927 }
5928
5929 ret = 0;
5930 if (!pages || nr_pages > got_pages) {
5931 kfree(vmas);
5932 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005933 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005934 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005935 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005936 sizeof(struct vm_area_struct *),
5937 GFP_KERNEL);
5938 if (!pages || !vmas) {
5939 ret = -ENOMEM;
5940 if (ctx->account_mem)
5941 io_unaccount_mem(ctx->user, nr_pages);
5942 goto err;
5943 }
5944 got_pages = nr_pages;
5945 }
5946
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005947 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005948 GFP_KERNEL);
5949 ret = -ENOMEM;
5950 if (!imu->bvec) {
5951 if (ctx->account_mem)
5952 io_unaccount_mem(ctx->user, nr_pages);
5953 goto err;
5954 }
5955
5956 ret = 0;
5957 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005958 pret = get_user_pages(ubuf, nr_pages,
5959 FOLL_WRITE | FOLL_LONGTERM,
5960 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005961 if (pret == nr_pages) {
5962 /* don't support file backed memory */
5963 for (j = 0; j < nr_pages; j++) {
5964 struct vm_area_struct *vma = vmas[j];
5965
5966 if (vma->vm_file &&
5967 !is_file_hugepages(vma->vm_file)) {
5968 ret = -EOPNOTSUPP;
5969 break;
5970 }
5971 }
5972 } else {
5973 ret = pret < 0 ? pret : -EFAULT;
5974 }
5975 up_read(&current->mm->mmap_sem);
5976 if (ret) {
5977 /*
5978 * if we did partial map, or found file backed vmas,
5979 * release any pages we did get
5980 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005981 if (pret > 0)
5982 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005983 if (ctx->account_mem)
5984 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005985 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005986 goto err;
5987 }
5988
5989 off = ubuf & ~PAGE_MASK;
5990 size = iov.iov_len;
5991 for (j = 0; j < nr_pages; j++) {
5992 size_t vec_len;
5993
5994 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5995 imu->bvec[j].bv_page = pages[j];
5996 imu->bvec[j].bv_len = vec_len;
5997 imu->bvec[j].bv_offset = off;
5998 off = 0;
5999 size -= vec_len;
6000 }
6001 /* store original address for later verification */
6002 imu->ubuf = ubuf;
6003 imu->len = iov.iov_len;
6004 imu->nr_bvecs = nr_pages;
6005
6006 ctx->nr_user_bufs++;
6007 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006008 kvfree(pages);
6009 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006010 return 0;
6011err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006012 kvfree(pages);
6013 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006014 io_sqe_buffer_unregister(ctx);
6015 return ret;
6016}
6017
Jens Axboe9b402842019-04-11 11:45:41 -06006018static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6019{
6020 __s32 __user *fds = arg;
6021 int fd;
6022
6023 if (ctx->cq_ev_fd)
6024 return -EBUSY;
6025
6026 if (copy_from_user(&fd, fds, sizeof(*fds)))
6027 return -EFAULT;
6028
6029 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6030 if (IS_ERR(ctx->cq_ev_fd)) {
6031 int ret = PTR_ERR(ctx->cq_ev_fd);
6032 ctx->cq_ev_fd = NULL;
6033 return ret;
6034 }
6035
6036 return 0;
6037}
6038
6039static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6040{
6041 if (ctx->cq_ev_fd) {
6042 eventfd_ctx_put(ctx->cq_ev_fd);
6043 ctx->cq_ev_fd = NULL;
6044 return 0;
6045 }
6046
6047 return -ENXIO;
6048}
6049
Jens Axboe2b188cc2019-01-07 10:46:33 -07006050static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6051{
Jens Axboe6b063142019-01-10 22:13:58 -07006052 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006053 if (ctx->sqo_mm)
6054 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006055
6056 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006057 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006058 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006059 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006060
Jens Axboe2b188cc2019-01-07 10:46:33 -07006061#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006062 if (ctx->ring_sock) {
6063 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006064 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006065 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006066#endif
6067
Hristo Venev75b28af2019-08-26 17:23:46 +00006068 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006069 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006070
6071 percpu_ref_exit(&ctx->refs);
6072 if (ctx->account_mem)
6073 io_unaccount_mem(ctx->user,
6074 ring_pages(ctx->sq_entries, ctx->cq_entries));
6075 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006076 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006077 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006078 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006079 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006080 kfree(ctx);
6081}
6082
6083static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6084{
6085 struct io_ring_ctx *ctx = file->private_data;
6086 __poll_t mask = 0;
6087
6088 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006089 /*
6090 * synchronizes with barrier from wq_has_sleeper call in
6091 * io_commit_cqring
6092 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006093 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006094 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6095 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006096 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006097 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006098 mask |= EPOLLIN | EPOLLRDNORM;
6099
6100 return mask;
6101}
6102
6103static int io_uring_fasync(int fd, struct file *file, int on)
6104{
6105 struct io_ring_ctx *ctx = file->private_data;
6106
6107 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6108}
6109
6110static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6111{
6112 mutex_lock(&ctx->uring_lock);
6113 percpu_ref_kill(&ctx->refs);
6114 mutex_unlock(&ctx->uring_lock);
6115
Jens Axboe5262f562019-09-17 12:26:57 -06006116 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006117 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006118
6119 if (ctx->io_wq)
6120 io_wq_cancel_all(ctx->io_wq);
6121
Jens Axboedef596e2019-01-09 08:59:42 -07006122 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006123 /* if we failed setting up the ctx, we might not have any rings */
6124 if (ctx->rings)
6125 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07006126 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006127 io_ring_ctx_free(ctx);
6128}
6129
6130static int io_uring_release(struct inode *inode, struct file *file)
6131{
6132 struct io_ring_ctx *ctx = file->private_data;
6133
6134 file->private_data = NULL;
6135 io_ring_ctx_wait_and_kill(ctx);
6136 return 0;
6137}
6138
Jens Axboefcb323c2019-10-24 12:39:47 -06006139static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6140 struct files_struct *files)
6141{
6142 struct io_kiocb *req;
6143 DEFINE_WAIT(wait);
6144
6145 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006146 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006147
6148 spin_lock_irq(&ctx->inflight_lock);
6149 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006150 if (req->work.files != files)
6151 continue;
6152 /* req is being completed, ignore */
6153 if (!refcount_inc_not_zero(&req->refs))
6154 continue;
6155 cancel_req = req;
6156 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006157 }
Jens Axboe768134d2019-11-10 20:30:53 -07006158 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006159 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006160 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006161 spin_unlock_irq(&ctx->inflight_lock);
6162
Jens Axboe768134d2019-11-10 20:30:53 -07006163 /* We need to keep going until we don't find a matching req */
6164 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006165 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006166
6167 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6168 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006169 schedule();
6170 }
Jens Axboe768134d2019-11-10 20:30:53 -07006171 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006172}
6173
6174static int io_uring_flush(struct file *file, void *data)
6175{
6176 struct io_ring_ctx *ctx = file->private_data;
6177
6178 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006179 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6180 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006181 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006182 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006183 return 0;
6184}
6185
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006186static void *io_uring_validate_mmap_request(struct file *file,
6187 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006188{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006189 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006190 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006191 struct page *page;
6192 void *ptr;
6193
6194 switch (offset) {
6195 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006196 case IORING_OFF_CQ_RING:
6197 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006198 break;
6199 case IORING_OFF_SQES:
6200 ptr = ctx->sq_sqes;
6201 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006202 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006203 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006204 }
6205
6206 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006207 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006208 return ERR_PTR(-EINVAL);
6209
6210 return ptr;
6211}
6212
6213#ifdef CONFIG_MMU
6214
6215static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6216{
6217 size_t sz = vma->vm_end - vma->vm_start;
6218 unsigned long pfn;
6219 void *ptr;
6220
6221 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6222 if (IS_ERR(ptr))
6223 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006224
6225 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6226 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6227}
6228
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006229#else /* !CONFIG_MMU */
6230
6231static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6232{
6233 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6234}
6235
6236static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6237{
6238 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6239}
6240
6241static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6242 unsigned long addr, unsigned long len,
6243 unsigned long pgoff, unsigned long flags)
6244{
6245 void *ptr;
6246
6247 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6248 if (IS_ERR(ptr))
6249 return PTR_ERR(ptr);
6250
6251 return (unsigned long) ptr;
6252}
6253
6254#endif /* !CONFIG_MMU */
6255
Jens Axboe2b188cc2019-01-07 10:46:33 -07006256SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6257 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6258 size_t, sigsz)
6259{
6260 struct io_ring_ctx *ctx;
6261 long ret = -EBADF;
6262 int submitted = 0;
6263 struct fd f;
6264
Jens Axboe6c271ce2019-01-10 11:22:30 -07006265 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006266 return -EINVAL;
6267
6268 f = fdget(fd);
6269 if (!f.file)
6270 return -EBADF;
6271
6272 ret = -EOPNOTSUPP;
6273 if (f.file->f_op != &io_uring_fops)
6274 goto out_fput;
6275
6276 ret = -ENXIO;
6277 ctx = f.file->private_data;
6278 if (!percpu_ref_tryget(&ctx->refs))
6279 goto out_fput;
6280
Jens Axboe6c271ce2019-01-10 11:22:30 -07006281 /*
6282 * For SQ polling, the thread will do all submissions and completions.
6283 * Just return the requested submit count, and wake the thread if
6284 * we were asked to.
6285 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006286 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006287 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006288 if (!list_empty_careful(&ctx->cq_overflow_list))
6289 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006290 if (flags & IORING_ENTER_SQ_WAKEUP)
6291 wake_up(&ctx->sqo_wait);
6292 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006293 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006294 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006295
Jens Axboe44d28272020-01-16 19:00:24 -07006296 if (current->mm != ctx->sqo_mm ||
6297 current_cred() != ctx->creds) {
6298 ret = -EPERM;
6299 goto out;
6300 }
6301
Jens Axboe2b188cc2019-01-07 10:46:33 -07006302 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006303 /* already have mm, so io_submit_sqes() won't try to grab it */
6304 cur_mm = ctx->sqo_mm;
6305 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6306 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006307 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006308
6309 if (submitted != to_submit)
6310 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006311 }
6312 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006313 unsigned nr_events = 0;
6314
Jens Axboe2b188cc2019-01-07 10:46:33 -07006315 min_complete = min(min_complete, ctx->cq_entries);
6316
Jens Axboedef596e2019-01-09 08:59:42 -07006317 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006318 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006319 } else {
6320 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6321 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006322 }
6323
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006324out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006325 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006326out_fput:
6327 fdput(f);
6328 return submitted ? submitted : ret;
6329}
6330
6331static const struct file_operations io_uring_fops = {
6332 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006333 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006334 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006335#ifndef CONFIG_MMU
6336 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6337 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6338#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006339 .poll = io_uring_poll,
6340 .fasync = io_uring_fasync,
6341};
6342
6343static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6344 struct io_uring_params *p)
6345{
Hristo Venev75b28af2019-08-26 17:23:46 +00006346 struct io_rings *rings;
6347 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006348
Hristo Venev75b28af2019-08-26 17:23:46 +00006349 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6350 if (size == SIZE_MAX)
6351 return -EOVERFLOW;
6352
6353 rings = io_mem_alloc(size);
6354 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006355 return -ENOMEM;
6356
Hristo Venev75b28af2019-08-26 17:23:46 +00006357 ctx->rings = rings;
6358 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6359 rings->sq_ring_mask = p->sq_entries - 1;
6360 rings->cq_ring_mask = p->cq_entries - 1;
6361 rings->sq_ring_entries = p->sq_entries;
6362 rings->cq_ring_entries = p->cq_entries;
6363 ctx->sq_mask = rings->sq_ring_mask;
6364 ctx->cq_mask = rings->cq_ring_mask;
6365 ctx->sq_entries = rings->sq_ring_entries;
6366 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006367
6368 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006369 if (size == SIZE_MAX) {
6370 io_mem_free(ctx->rings);
6371 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006372 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006373 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006374
6375 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006376 if (!ctx->sq_sqes) {
6377 io_mem_free(ctx->rings);
6378 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006379 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006380 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006381
Jens Axboe2b188cc2019-01-07 10:46:33 -07006382 return 0;
6383}
6384
6385/*
6386 * Allocate an anonymous fd, this is what constitutes the application
6387 * visible backing of an io_uring instance. The application mmaps this
6388 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6389 * we have to tie this fd to a socket for file garbage collection purposes.
6390 */
6391static int io_uring_get_fd(struct io_ring_ctx *ctx)
6392{
6393 struct file *file;
6394 int ret;
6395
6396#if defined(CONFIG_UNIX)
6397 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6398 &ctx->ring_sock);
6399 if (ret)
6400 return ret;
6401#endif
6402
6403 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6404 if (ret < 0)
6405 goto err;
6406
6407 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6408 O_RDWR | O_CLOEXEC);
6409 if (IS_ERR(file)) {
6410 put_unused_fd(ret);
6411 ret = PTR_ERR(file);
6412 goto err;
6413 }
6414
6415#if defined(CONFIG_UNIX)
6416 ctx->ring_sock->file = file;
6417#endif
6418 fd_install(ret, file);
6419 return ret;
6420err:
6421#if defined(CONFIG_UNIX)
6422 sock_release(ctx->ring_sock);
6423 ctx->ring_sock = NULL;
6424#endif
6425 return ret;
6426}
6427
6428static int io_uring_create(unsigned entries, struct io_uring_params *p)
6429{
6430 struct user_struct *user = NULL;
6431 struct io_ring_ctx *ctx;
6432 bool account_mem;
6433 int ret;
6434
Jens Axboe8110c1a2019-12-28 15:39:54 -07006435 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006436 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006437 if (entries > IORING_MAX_ENTRIES) {
6438 if (!(p->flags & IORING_SETUP_CLAMP))
6439 return -EINVAL;
6440 entries = IORING_MAX_ENTRIES;
6441 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006442
6443 /*
6444 * Use twice as many entries for the CQ ring. It's possible for the
6445 * application to drive a higher depth than the size of the SQ ring,
6446 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006447 * some flexibility in overcommitting a bit. If the application has
6448 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6449 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006450 */
6451 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006452 if (p->flags & IORING_SETUP_CQSIZE) {
6453 /*
6454 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6455 * to a power-of-two, if it isn't already. We do NOT impose
6456 * any cq vs sq ring sizing.
6457 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006458 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006459 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006460 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6461 if (!(p->flags & IORING_SETUP_CLAMP))
6462 return -EINVAL;
6463 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6464 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006465 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6466 } else {
6467 p->cq_entries = 2 * p->sq_entries;
6468 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006469
6470 user = get_uid(current_user());
6471 account_mem = !capable(CAP_IPC_LOCK);
6472
6473 if (account_mem) {
6474 ret = io_account_mem(user,
6475 ring_pages(p->sq_entries, p->cq_entries));
6476 if (ret) {
6477 free_uid(user);
6478 return ret;
6479 }
6480 }
6481
6482 ctx = io_ring_ctx_alloc(p);
6483 if (!ctx) {
6484 if (account_mem)
6485 io_unaccount_mem(user, ring_pages(p->sq_entries,
6486 p->cq_entries));
6487 free_uid(user);
6488 return -ENOMEM;
6489 }
6490 ctx->compat = in_compat_syscall();
6491 ctx->account_mem = account_mem;
6492 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006493 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006494
6495 ret = io_allocate_scq_urings(ctx, p);
6496 if (ret)
6497 goto err;
6498
Jens Axboe6c271ce2019-01-10 11:22:30 -07006499 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006500 if (ret)
6501 goto err;
6502
Jens Axboe2b188cc2019-01-07 10:46:33 -07006503 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006504 p->sq_off.head = offsetof(struct io_rings, sq.head);
6505 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6506 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6507 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6508 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6509 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6510 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006511
6512 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006513 p->cq_off.head = offsetof(struct io_rings, cq.head);
6514 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6515 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6516 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6517 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6518 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006519
Jens Axboe044c1ab2019-10-28 09:15:33 -06006520 /*
6521 * Install ring fd as the very last thing, so we don't risk someone
6522 * having closed it before we finish setup
6523 */
6524 ret = io_uring_get_fd(ctx);
6525 if (ret < 0)
6526 goto err;
6527
Jens Axboeda8c9692019-12-02 18:51:26 -07006528 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboeba042912019-12-25 16:33:42 -07006529 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006530 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006531 return ret;
6532err:
6533 io_ring_ctx_wait_and_kill(ctx);
6534 return ret;
6535}
6536
6537/*
6538 * Sets up an aio uring context, and returns the fd. Applications asks for a
6539 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6540 * params structure passed in.
6541 */
6542static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6543{
6544 struct io_uring_params p;
6545 long ret;
6546 int i;
6547
6548 if (copy_from_user(&p, params, sizeof(p)))
6549 return -EFAULT;
6550 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6551 if (p.resv[i])
6552 return -EINVAL;
6553 }
6554
Jens Axboe6c271ce2019-01-10 11:22:30 -07006555 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006556 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
6557 IORING_SETUP_CLAMP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006558 return -EINVAL;
6559
6560 ret = io_uring_create(entries, &p);
6561 if (ret < 0)
6562 return ret;
6563
6564 if (copy_to_user(params, &p, sizeof(p)))
6565 return -EFAULT;
6566
6567 return ret;
6568}
6569
6570SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6571 struct io_uring_params __user *, params)
6572{
6573 return io_uring_setup(entries, params);
6574}
6575
Jens Axboe66f4af92020-01-16 15:36:52 -07006576static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6577{
6578 struct io_uring_probe *p;
6579 size_t size;
6580 int i, ret;
6581
6582 size = struct_size(p, ops, nr_args);
6583 if (size == SIZE_MAX)
6584 return -EOVERFLOW;
6585 p = kzalloc(size, GFP_KERNEL);
6586 if (!p)
6587 return -ENOMEM;
6588
6589 ret = -EFAULT;
6590 if (copy_from_user(p, arg, size))
6591 goto out;
6592 ret = -EINVAL;
6593 if (memchr_inv(p, 0, size))
6594 goto out;
6595
6596 p->last_op = IORING_OP_LAST - 1;
6597 if (nr_args > IORING_OP_LAST)
6598 nr_args = IORING_OP_LAST;
6599
6600 for (i = 0; i < nr_args; i++) {
6601 p->ops[i].op = i;
6602 if (!io_op_defs[i].not_supported)
6603 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6604 }
6605 p->ops_len = i;
6606
6607 ret = 0;
6608 if (copy_to_user(arg, p, size))
6609 ret = -EFAULT;
6610out:
6611 kfree(p);
6612 return ret;
6613}
6614
Jens Axboeedafcce2019-01-09 09:16:05 -07006615static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6616 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006617 __releases(ctx->uring_lock)
6618 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006619{
6620 int ret;
6621
Jens Axboe35fa71a2019-04-22 10:23:23 -06006622 /*
6623 * We're inside the ring mutex, if the ref is already dying, then
6624 * someone else killed the ctx or is already going through
6625 * io_uring_register().
6626 */
6627 if (percpu_ref_is_dying(&ctx->refs))
6628 return -ENXIO;
6629
Jens Axboe05f3fb32019-12-09 11:22:50 -07006630 if (opcode != IORING_UNREGISTER_FILES &&
Jens Axboe66f4af92020-01-16 15:36:52 -07006631 opcode != IORING_REGISTER_FILES_UPDATE &&
6632 opcode != IORING_REGISTER_PROBE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006633 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006634
Jens Axboe05f3fb32019-12-09 11:22:50 -07006635 /*
6636 * Drop uring mutex before waiting for references to exit. If
6637 * another thread is currently inside io_uring_enter() it might
6638 * need to grab the uring_lock to make progress. If we hold it
6639 * here across the drain wait, then we can deadlock. It's safe
6640 * to drop the mutex here, since no new references will come in
6641 * after we've killed the percpu ref.
6642 */
6643 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006644 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006645 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006646 if (ret) {
6647 percpu_ref_resurrect(&ctx->refs);
6648 ret = -EINTR;
6649 goto out;
6650 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006651 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006652
6653 switch (opcode) {
6654 case IORING_REGISTER_BUFFERS:
6655 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6656 break;
6657 case IORING_UNREGISTER_BUFFERS:
6658 ret = -EINVAL;
6659 if (arg || nr_args)
6660 break;
6661 ret = io_sqe_buffer_unregister(ctx);
6662 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006663 case IORING_REGISTER_FILES:
6664 ret = io_sqe_files_register(ctx, arg, nr_args);
6665 break;
6666 case IORING_UNREGISTER_FILES:
6667 ret = -EINVAL;
6668 if (arg || nr_args)
6669 break;
6670 ret = io_sqe_files_unregister(ctx);
6671 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006672 case IORING_REGISTER_FILES_UPDATE:
6673 ret = io_sqe_files_update(ctx, arg, nr_args);
6674 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006675 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006676 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006677 ret = -EINVAL;
6678 if (nr_args != 1)
6679 break;
6680 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006681 if (ret)
6682 break;
6683 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6684 ctx->eventfd_async = 1;
6685 else
6686 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006687 break;
6688 case IORING_UNREGISTER_EVENTFD:
6689 ret = -EINVAL;
6690 if (arg || nr_args)
6691 break;
6692 ret = io_eventfd_unregister(ctx);
6693 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07006694 case IORING_REGISTER_PROBE:
6695 ret = -EINVAL;
6696 if (!arg || nr_args > 256)
6697 break;
6698 ret = io_probe(ctx, arg, nr_args);
6699 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006700 default:
6701 ret = -EINVAL;
6702 break;
6703 }
6704
Jens Axboe05f3fb32019-12-09 11:22:50 -07006705
6706 if (opcode != IORING_UNREGISTER_FILES &&
Jens Axboe66f4af92020-01-16 15:36:52 -07006707 opcode != IORING_REGISTER_FILES_UPDATE &&
6708 opcode != IORING_REGISTER_PROBE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006709 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006710 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07006711out:
6712 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006713 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006714 return ret;
6715}
6716
6717SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6718 void __user *, arg, unsigned int, nr_args)
6719{
6720 struct io_ring_ctx *ctx;
6721 long ret = -EBADF;
6722 struct fd f;
6723
6724 f = fdget(fd);
6725 if (!f.file)
6726 return -EBADF;
6727
6728 ret = -EOPNOTSUPP;
6729 if (f.file->f_op != &io_uring_fops)
6730 goto out_fput;
6731
6732 ctx = f.file->private_data;
6733
6734 mutex_lock(&ctx->uring_lock);
6735 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6736 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006737 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6738 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006739out_fput:
6740 fdput(f);
6741 return ret;
6742}
6743
Jens Axboe2b188cc2019-01-07 10:46:33 -07006744static int __init io_uring_init(void)
6745{
Jens Axboed3656342019-12-18 09:50:26 -07006746 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006747 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6748 return 0;
6749};
6750__initcall(io_uring_init);