blob: 0ea36911745ddb378cddcc1866c99b2fa8325854 [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 Axboecccf0ee2020-01-27 16:34:48 -0700878static inline void io_req_work_grab_env(struct io_kiocb *req,
879 const struct io_op_def *def)
880{
881 if (!req->work.mm && def->needs_mm) {
882 mmgrab(current->mm);
883 req->work.mm = current->mm;
884 }
885 if (!req->work.creds)
886 req->work.creds = get_current_cred();
887}
888
889static inline void io_req_work_drop_env(struct io_kiocb *req)
890{
891 if (req->work.mm) {
892 mmdrop(req->work.mm);
893 req->work.mm = NULL;
894 }
895 if (req->work.creds) {
896 put_cred(req->work.creds);
897 req->work.creds = NULL;
898 }
899}
900
Jens Axboe94ae5e72019-11-14 19:39:52 -0700901static inline bool io_prep_async_work(struct io_kiocb *req,
902 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600903{
Jens Axboed3656342019-12-18 09:50:26 -0700904 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600905 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600906
Jens Axboed3656342019-12-18 09:50:26 -0700907 if (req->flags & REQ_F_ISREG) {
908 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700909 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700910 } else {
911 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700912 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600913 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700914
915 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600916
Jens Axboe94ae5e72019-11-14 19:39:52 -0700917 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600918 return do_hashed;
919}
920
Jackie Liua197f662019-11-08 08:09:12 -0700921static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600922{
Jackie Liua197f662019-11-08 08:09:12 -0700923 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700924 struct io_kiocb *link;
925 bool do_hashed;
926
927 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600928
929 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
930 req->flags);
931 if (!do_hashed) {
932 io_wq_enqueue(ctx->io_wq, &req->work);
933 } else {
934 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
935 file_inode(req->file));
936 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700937
938 if (link)
939 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600940}
941
Jens Axboe5262f562019-09-17 12:26:57 -0600942static void io_kill_timeout(struct io_kiocb *req)
943{
944 int ret;
945
Jens Axboe2d283902019-12-04 11:08:05 -0700946 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600947 if (ret != -1) {
948 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600949 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700950 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800951 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600952 }
953}
954
955static void io_kill_timeouts(struct io_ring_ctx *ctx)
956{
957 struct io_kiocb *req, *tmp;
958
959 spin_lock_irq(&ctx->completion_lock);
960 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
961 io_kill_timeout(req);
962 spin_unlock_irq(&ctx->completion_lock);
963}
964
Jens Axboede0617e2019-04-06 21:51:27 -0600965static void io_commit_cqring(struct io_ring_ctx *ctx)
966{
967 struct io_kiocb *req;
968
Jens Axboe5262f562019-09-17 12:26:57 -0600969 while ((req = io_get_timeout_req(ctx)) != NULL)
970 io_kill_timeout(req);
971
Jens Axboede0617e2019-04-06 21:51:27 -0600972 __io_commit_cqring(ctx);
973
Pavel Begunkov87987892020-01-18 01:22:30 +0300974 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -0700975 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600976}
977
Jens Axboe2b188cc2019-01-07 10:46:33 -0700978static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
979{
Hristo Venev75b28af2019-08-26 17:23:46 +0000980 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700981 unsigned tail;
982
983 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200984 /*
985 * writes to the cq entry need to come after reading head; the
986 * control dependency is enough as we're using WRITE_ONCE to
987 * fill the cq entry
988 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000989 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700990 return NULL;
991
992 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000993 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700994}
995
Jens Axboef2842ab2020-01-08 11:04:00 -0700996static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
997{
998 if (!ctx->eventfd_async)
999 return true;
1000 return io_wq_current_is_worker() || in_interrupt();
1001}
1002
Jens Axboe8c838782019-03-12 15:48:16 -06001003static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1004{
1005 if (waitqueue_active(&ctx->wait))
1006 wake_up(&ctx->wait);
1007 if (waitqueue_active(&ctx->sqo_wait))
1008 wake_up(&ctx->sqo_wait);
Jens Axboef2842ab2020-01-08 11:04:00 -07001009 if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -06001010 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001011}
1012
Jens Axboec4a2ed72019-11-21 21:01:26 -07001013/* Returns true if there are no backlogged entries after the flush */
1014static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001015{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001016 struct io_rings *rings = ctx->rings;
1017 struct io_uring_cqe *cqe;
1018 struct io_kiocb *req;
1019 unsigned long flags;
1020 LIST_HEAD(list);
1021
1022 if (!force) {
1023 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001024 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001025 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1026 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001027 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001028 }
1029
1030 spin_lock_irqsave(&ctx->completion_lock, flags);
1031
1032 /* if force is set, the ring is going away. always drop after that */
1033 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001034 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001035
Jens Axboec4a2ed72019-11-21 21:01:26 -07001036 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001037 while (!list_empty(&ctx->cq_overflow_list)) {
1038 cqe = io_get_cqring(ctx);
1039 if (!cqe && !force)
1040 break;
1041
1042 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1043 list);
1044 list_move(&req->list, &list);
1045 if (cqe) {
1046 WRITE_ONCE(cqe->user_data, req->user_data);
1047 WRITE_ONCE(cqe->res, req->result);
1048 WRITE_ONCE(cqe->flags, 0);
1049 } else {
1050 WRITE_ONCE(ctx->rings->cq_overflow,
1051 atomic_inc_return(&ctx->cached_cq_overflow));
1052 }
1053 }
1054
1055 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001056 if (cqe) {
1057 clear_bit(0, &ctx->sq_check_overflow);
1058 clear_bit(0, &ctx->cq_check_overflow);
1059 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001060 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1061 io_cqring_ev_posted(ctx);
1062
1063 while (!list_empty(&list)) {
1064 req = list_first_entry(&list, struct io_kiocb, list);
1065 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001066 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001067 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001068
1069 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001070}
1071
Jens Axboe78e19bb2019-11-06 15:21:34 -07001072static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001073{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001074 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001075 struct io_uring_cqe *cqe;
1076
Jens Axboe78e19bb2019-11-06 15:21:34 -07001077 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001078
Jens Axboe2b188cc2019-01-07 10:46:33 -07001079 /*
1080 * If we can't get a cq entry, userspace overflowed the
1081 * submission (by quite a lot). Increment the overflow count in
1082 * the ring.
1083 */
1084 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001085 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001086 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001087 WRITE_ONCE(cqe->res, res);
1088 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001089 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001090 WRITE_ONCE(ctx->rings->cq_overflow,
1091 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001092 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001093 if (list_empty(&ctx->cq_overflow_list)) {
1094 set_bit(0, &ctx->sq_check_overflow);
1095 set_bit(0, &ctx->cq_check_overflow);
1096 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001097 refcount_inc(&req->refs);
1098 req->result = res;
1099 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001100 }
1101}
1102
Jens Axboe78e19bb2019-11-06 15:21:34 -07001103static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001104{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001105 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001106 unsigned long flags;
1107
1108 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001109 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001110 io_commit_cqring(ctx);
1111 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1112
Jens Axboe8c838782019-03-12 15:48:16 -06001113 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001114}
1115
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001116static inline bool io_is_fallback_req(struct io_kiocb *req)
1117{
1118 return req == (struct io_kiocb *)
1119 ((unsigned long) req->ctx->fallback_req & ~1UL);
1120}
1121
1122static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1123{
1124 struct io_kiocb *req;
1125
1126 req = ctx->fallback_req;
1127 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1128 return req;
1129
1130 return NULL;
1131}
1132
Jens Axboe2579f912019-01-09 09:10:43 -07001133static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1134 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001135{
Jens Axboefd6fab22019-03-14 16:30:06 -06001136 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001137 struct io_kiocb *req;
1138
Jens Axboe2579f912019-01-09 09:10:43 -07001139 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001140 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001141 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001142 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001143 } else if (!state->free_reqs) {
1144 size_t sz;
1145 int ret;
1146
1147 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001148 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1149
1150 /*
1151 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1152 * retry single alloc to be on the safe side.
1153 */
1154 if (unlikely(ret <= 0)) {
1155 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1156 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001157 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001158 ret = 1;
1159 }
Jens Axboe2579f912019-01-09 09:10:43 -07001160 state->free_reqs = ret - 1;
1161 state->cur_req = 1;
1162 req = state->reqs[0];
1163 } else {
1164 req = state->reqs[state->cur_req];
1165 state->free_reqs--;
1166 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001167 }
1168
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001169got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001170 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001171 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001172 req->ctx = ctx;
1173 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001174 /* one is dropped after submission, the other at completion */
1175 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001176 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001177 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001178 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001179fallback:
1180 req = io_get_fallback_req(ctx);
1181 if (req)
1182 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001183 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001184 return NULL;
1185}
1186
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001187static void __io_req_do_free(struct io_kiocb *req)
1188{
1189 if (likely(!io_is_fallback_req(req)))
1190 kmem_cache_free(req_cachep, req);
1191 else
1192 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1193}
1194
Jens Axboec6ca97b302019-12-28 12:11:08 -07001195static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001196{
Jens Axboefcb323c2019-10-24 12:39:47 -06001197 struct io_ring_ctx *ctx = req->ctx;
1198
YueHaibing96fd84d2020-01-07 22:22:44 +08001199 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001200 if (req->file) {
1201 if (req->flags & REQ_F_FIXED_FILE)
1202 percpu_ref_put(&ctx->file_data->refs);
1203 else
1204 fput(req->file);
1205 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001206
1207 io_req_work_drop_env(req);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001208}
1209
1210static void __io_free_req(struct io_kiocb *req)
1211{
1212 __io_req_aux_free(req);
1213
Jens Axboefcb323c2019-10-24 12:39:47 -06001214 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001215 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001216 unsigned long flags;
1217
1218 spin_lock_irqsave(&ctx->inflight_lock, flags);
1219 list_del(&req->inflight_entry);
1220 if (waitqueue_active(&ctx->inflight_wait))
1221 wake_up(&ctx->inflight_wait);
1222 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1223 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001224
1225 percpu_ref_put(&req->ctx->refs);
1226 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001227}
1228
Jens Axboec6ca97b302019-12-28 12:11:08 -07001229struct req_batch {
1230 void *reqs[IO_IOPOLL_BATCH];
1231 int to_free;
1232 int need_iter;
1233};
1234
1235static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1236{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001237 int fixed_refs = rb->to_free;
1238
Jens Axboec6ca97b302019-12-28 12:11:08 -07001239 if (!rb->to_free)
1240 return;
1241 if (rb->need_iter) {
1242 int i, inflight = 0;
1243 unsigned long flags;
1244
Jens Axboe10fef4b2020-01-09 07:52:28 -07001245 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001246 for (i = 0; i < rb->to_free; i++) {
1247 struct io_kiocb *req = rb->reqs[i];
1248
Jens Axboe10fef4b2020-01-09 07:52:28 -07001249 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001250 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001251 fixed_refs++;
1252 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001253 if (req->flags & REQ_F_INFLIGHT)
1254 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001255 __io_req_aux_free(req);
1256 }
1257 if (!inflight)
1258 goto do_free;
1259
1260 spin_lock_irqsave(&ctx->inflight_lock, flags);
1261 for (i = 0; i < rb->to_free; i++) {
1262 struct io_kiocb *req = rb->reqs[i];
1263
Jens Axboe10fef4b2020-01-09 07:52:28 -07001264 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001265 list_del(&req->inflight_entry);
1266 if (!--inflight)
1267 break;
1268 }
1269 }
1270 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1271
1272 if (waitqueue_active(&ctx->inflight_wait))
1273 wake_up(&ctx->inflight_wait);
1274 }
1275do_free:
1276 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001277 if (fixed_refs)
1278 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001279 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001280 rb->to_free = rb->need_iter = 0;
1281}
1282
Jackie Liua197f662019-11-08 08:09:12 -07001283static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001284{
Jackie Liua197f662019-11-08 08:09:12 -07001285 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001286 int ret;
1287
Jens Axboe2d283902019-12-04 11:08:05 -07001288 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001289 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001290 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001291 io_commit_cqring(ctx);
1292 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001293 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001294 return true;
1295 }
1296
1297 return false;
1298}
1299
Jens Axboeba816ad2019-09-28 11:36:45 -06001300static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001301{
Jens Axboe2665abf2019-11-05 12:40:47 -07001302 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001303 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001304
Jens Axboe4d7dd462019-11-20 13:03:52 -07001305 /* Already got next link */
1306 if (req->flags & REQ_F_LINK_NEXT)
1307 return;
1308
Jens Axboe9e645e112019-05-10 16:07:28 -06001309 /*
1310 * The list should never be empty when we are called here. But could
1311 * potentially happen if the chain is messed up, check to be on the
1312 * safe side.
1313 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001314 while (!list_empty(&req->link_list)) {
1315 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1316 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001317
Pavel Begunkov44932332019-12-05 16:16:35 +03001318 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1319 (nxt->flags & REQ_F_TIMEOUT))) {
1320 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001321 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001322 req->flags &= ~REQ_F_LINK_TIMEOUT;
1323 continue;
1324 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001325
Pavel Begunkov44932332019-12-05 16:16:35 +03001326 list_del_init(&req->link_list);
1327 if (!list_empty(&nxt->link_list))
1328 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001329 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001330 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001331 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001332
Jens Axboe4d7dd462019-11-20 13:03:52 -07001333 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001334 if (wake_ev)
1335 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001336}
1337
1338/*
1339 * Called if REQ_F_LINK is set, and we fail the head request
1340 */
1341static void io_fail_links(struct io_kiocb *req)
1342{
Jens Axboe2665abf2019-11-05 12:40:47 -07001343 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001344 unsigned long flags;
1345
1346 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001347
1348 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001349 struct io_kiocb *link = list_first_entry(&req->link_list,
1350 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001351
Pavel Begunkov44932332019-12-05 16:16:35 +03001352 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001353 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001354
1355 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001356 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001357 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001358 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001359 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001360 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001361 }
Jens Axboe5d960722019-11-19 15:31:28 -07001362 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001363 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001364
1365 io_commit_cqring(ctx);
1366 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1367 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001368}
1369
Jens Axboe4d7dd462019-11-20 13:03:52 -07001370static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001371{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001372 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001373 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001374
Jens Axboe9e645e112019-05-10 16:07:28 -06001375 /*
1376 * If LINK is set, we have dependent requests in this chain. If we
1377 * didn't fail this request, queue the first one up, moving any other
1378 * dependencies to the next request. In case of failure, fail the rest
1379 * of the chain.
1380 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001381 if (req->flags & REQ_F_FAIL_LINK) {
1382 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001383 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1384 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001385 struct io_ring_ctx *ctx = req->ctx;
1386 unsigned long flags;
1387
1388 /*
1389 * If this is a timeout link, we could be racing with the
1390 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001391 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001392 */
1393 spin_lock_irqsave(&ctx->completion_lock, flags);
1394 io_req_link_next(req, nxt);
1395 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1396 } else {
1397 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001398 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001399}
Jens Axboe9e645e112019-05-10 16:07:28 -06001400
Jackie Liuc69f8db2019-11-09 11:00:08 +08001401static void io_free_req(struct io_kiocb *req)
1402{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001403 struct io_kiocb *nxt = NULL;
1404
1405 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001406 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001407
1408 if (nxt)
1409 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001410}
1411
Jens Axboeba816ad2019-09-28 11:36:45 -06001412/*
1413 * Drop reference to request, return next in chain (if there is one) if this
1414 * was the last reference to this request.
1415 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001416__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001417static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001418{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001419 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001420
Jens Axboee65ef562019-03-12 10:16:44 -06001421 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001422 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001423}
1424
Jens Axboe2b188cc2019-01-07 10:46:33 -07001425static void io_put_req(struct io_kiocb *req)
1426{
Jens Axboedef596e2019-01-09 08:59:42 -07001427 if (refcount_dec_and_test(&req->refs))
1428 io_free_req(req);
1429}
1430
Jens Axboe978db572019-11-14 22:39:04 -07001431/*
1432 * Must only be used if we don't need to care about links, usually from
1433 * within the completion handling itself.
1434 */
1435static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001436{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001437 /* drop both submit and complete references */
1438 if (refcount_sub_and_test(2, &req->refs))
1439 __io_free_req(req);
1440}
1441
Jens Axboe978db572019-11-14 22:39:04 -07001442static void io_double_put_req(struct io_kiocb *req)
1443{
1444 /* drop both submit and complete references */
1445 if (refcount_sub_and_test(2, &req->refs))
1446 io_free_req(req);
1447}
1448
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001449static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001450{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001451 struct io_rings *rings = ctx->rings;
1452
Jens Axboead3eb2c2019-12-18 17:12:20 -07001453 if (test_bit(0, &ctx->cq_check_overflow)) {
1454 /*
1455 * noflush == true is from the waitqueue handler, just ensure
1456 * we wake up the task, and the next invocation will flush the
1457 * entries. We cannot safely to it from here.
1458 */
1459 if (noflush && !list_empty(&ctx->cq_overflow_list))
1460 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001461
Jens Axboead3eb2c2019-12-18 17:12:20 -07001462 io_cqring_overflow_flush(ctx, false);
1463 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001464
Jens Axboea3a0e432019-08-20 11:03:11 -06001465 /* See comment at the top of this file */
1466 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001467 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001468}
1469
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001470static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1471{
1472 struct io_rings *rings = ctx->rings;
1473
1474 /* make sure SQ entry isn't read before tail */
1475 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1476}
1477
Jens Axboe8237e042019-12-28 10:48:22 -07001478static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001479{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001480 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1481 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001482
Jens Axboec6ca97b302019-12-28 12:11:08 -07001483 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1484 rb->need_iter++;
1485
1486 rb->reqs[rb->to_free++] = req;
1487 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1488 io_free_req_many(req->ctx, rb);
1489 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001490}
1491
Jens Axboedef596e2019-01-09 08:59:42 -07001492/*
1493 * Find and free completed poll iocbs
1494 */
1495static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1496 struct list_head *done)
1497{
Jens Axboe8237e042019-12-28 10:48:22 -07001498 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001499 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001500
Jens Axboec6ca97b302019-12-28 12:11:08 -07001501 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001502 while (!list_empty(done)) {
1503 req = list_first_entry(done, struct io_kiocb, list);
1504 list_del(&req->list);
1505
Jens Axboe78e19bb2019-11-06 15:21:34 -07001506 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001507 (*nr_events)++;
1508
Jens Axboe8237e042019-12-28 10:48:22 -07001509 if (refcount_dec_and_test(&req->refs) &&
1510 !io_req_multi_free(&rb, req))
1511 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001512 }
Jens Axboedef596e2019-01-09 08:59:42 -07001513
Jens Axboe09bb8392019-03-13 12:39:28 -06001514 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001515 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001516}
1517
1518static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1519 long min)
1520{
1521 struct io_kiocb *req, *tmp;
1522 LIST_HEAD(done);
1523 bool spin;
1524 int ret;
1525
1526 /*
1527 * Only spin for completions if we don't have multiple devices hanging
1528 * off our complete list, and we're under the requested amount.
1529 */
1530 spin = !ctx->poll_multi_file && *nr_events < min;
1531
1532 ret = 0;
1533 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001534 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001535
1536 /*
1537 * Move completed entries to our local list. If we find a
1538 * request that requires polling, break out and complete
1539 * the done list first, if we have entries there.
1540 */
1541 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1542 list_move_tail(&req->list, &done);
1543 continue;
1544 }
1545 if (!list_empty(&done))
1546 break;
1547
1548 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1549 if (ret < 0)
1550 break;
1551
1552 if (ret && spin)
1553 spin = false;
1554 ret = 0;
1555 }
1556
1557 if (!list_empty(&done))
1558 io_iopoll_complete(ctx, nr_events, &done);
1559
1560 return ret;
1561}
1562
1563/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001564 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001565 * non-spinning poll check - we'll still enter the driver poll loop, but only
1566 * as a non-spinning completion check.
1567 */
1568static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1569 long min)
1570{
Jens Axboe08f54392019-08-21 22:19:11 -06001571 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001572 int ret;
1573
1574 ret = io_do_iopoll(ctx, nr_events, min);
1575 if (ret < 0)
1576 return ret;
1577 if (!min || *nr_events >= min)
1578 return 0;
1579 }
1580
1581 return 1;
1582}
1583
1584/*
1585 * We can't just wait for polled events to come to us, we have to actively
1586 * find and complete them.
1587 */
1588static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1589{
1590 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1591 return;
1592
1593 mutex_lock(&ctx->uring_lock);
1594 while (!list_empty(&ctx->poll_list)) {
1595 unsigned int nr_events = 0;
1596
1597 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001598
1599 /*
1600 * Ensure we allow local-to-the-cpu processing to take place,
1601 * in this case we need to ensure that we reap all events.
1602 */
1603 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001604 }
1605 mutex_unlock(&ctx->uring_lock);
1606}
1607
Jens Axboe2b2ed972019-10-25 10:06:15 -06001608static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1609 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001610{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001611 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001612
1613 do {
1614 int tmin = 0;
1615
Jens Axboe500f9fb2019-08-19 12:15:59 -06001616 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001617 * Don't enter poll loop if we already have events pending.
1618 * If we do, we can potentially be spinning for commands that
1619 * already triggered a CQE (eg in error).
1620 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001621 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001622 break;
1623
1624 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001625 * If a submit got punted to a workqueue, we can have the
1626 * application entering polling for a command before it gets
1627 * issued. That app will hold the uring_lock for the duration
1628 * of the poll right here, so we need to take a breather every
1629 * now and then to ensure that the issue has a chance to add
1630 * the poll to the issued list. Otherwise we can spin here
1631 * forever, while the workqueue is stuck trying to acquire the
1632 * very same mutex.
1633 */
1634 if (!(++iters & 7)) {
1635 mutex_unlock(&ctx->uring_lock);
1636 mutex_lock(&ctx->uring_lock);
1637 }
1638
Jens Axboedef596e2019-01-09 08:59:42 -07001639 if (*nr_events < min)
1640 tmin = min - *nr_events;
1641
1642 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1643 if (ret <= 0)
1644 break;
1645 ret = 0;
1646 } while (min && !*nr_events && !need_resched());
1647
Jens Axboe2b2ed972019-10-25 10:06:15 -06001648 return ret;
1649}
1650
1651static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1652 long min)
1653{
1654 int ret;
1655
1656 /*
1657 * We disallow the app entering submit/complete with polling, but we
1658 * still need to lock the ring to prevent racing with polled issue
1659 * that got punted to a workqueue.
1660 */
1661 mutex_lock(&ctx->uring_lock);
1662 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001663 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001664 return ret;
1665}
1666
Jens Axboe491381ce2019-10-17 09:20:46 -06001667static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001668{
Jens Axboe491381ce2019-10-17 09:20:46 -06001669 /*
1670 * Tell lockdep we inherited freeze protection from submission
1671 * thread.
1672 */
1673 if (req->flags & REQ_F_ISREG) {
1674 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001675
Jens Axboe491381ce2019-10-17 09:20:46 -06001676 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001677 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001678 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001679}
1680
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001681static inline void req_set_fail_links(struct io_kiocb *req)
1682{
1683 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1684 req->flags |= REQ_F_FAIL_LINK;
1685}
1686
Jens Axboeba816ad2019-09-28 11:36:45 -06001687static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001688{
Jens Axboe9adbd452019-12-20 08:45:55 -07001689 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001690
Jens Axboe491381ce2019-10-17 09:20:46 -06001691 if (kiocb->ki_flags & IOCB_WRITE)
1692 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001693
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001694 if (res != req->result)
1695 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001696 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001697}
1698
1699static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1700{
Jens Axboe9adbd452019-12-20 08:45:55 -07001701 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001702
1703 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001704 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001705}
1706
Jens Axboeba816ad2019-09-28 11:36:45 -06001707static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1708{
Jens Axboe9adbd452019-12-20 08:45:55 -07001709 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001710 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001711
1712 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001713 io_put_req_find_next(req, &nxt);
1714
1715 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001716}
1717
Jens Axboedef596e2019-01-09 08:59:42 -07001718static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1719{
Jens Axboe9adbd452019-12-20 08:45:55 -07001720 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001721
Jens Axboe491381ce2019-10-17 09:20:46 -06001722 if (kiocb->ki_flags & IOCB_WRITE)
1723 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001724
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001725 if (res != req->result)
1726 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001727 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001728 if (res != -EAGAIN)
1729 req->flags |= REQ_F_IOPOLL_COMPLETED;
1730}
1731
1732/*
1733 * After the iocb has been issued, it's safe to be found on the poll list.
1734 * Adding the kiocb to the list AFTER submission ensures that we don't
1735 * find it from a io_iopoll_getevents() thread before the issuer is done
1736 * accessing the kiocb cookie.
1737 */
1738static void io_iopoll_req_issued(struct io_kiocb *req)
1739{
1740 struct io_ring_ctx *ctx = req->ctx;
1741
1742 /*
1743 * Track whether we have multiple files in our lists. This will impact
1744 * how we do polling eventually, not spinning if we're on potentially
1745 * different devices.
1746 */
1747 if (list_empty(&ctx->poll_list)) {
1748 ctx->poll_multi_file = false;
1749 } else if (!ctx->poll_multi_file) {
1750 struct io_kiocb *list_req;
1751
1752 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1753 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001754 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001755 ctx->poll_multi_file = true;
1756 }
1757
1758 /*
1759 * For fast devices, IO may have already completed. If it has, add
1760 * it to the front so we find it first.
1761 */
1762 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1763 list_add(&req->list, &ctx->poll_list);
1764 else
1765 list_add_tail(&req->list, &ctx->poll_list);
1766}
1767
Jens Axboe3d6770f2019-04-13 11:50:54 -06001768static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001769{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001770 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001771 int diff = state->has_refs - state->used_refs;
1772
1773 if (diff)
1774 fput_many(state->file, diff);
1775 state->file = NULL;
1776 }
1777}
1778
1779/*
1780 * Get as many references to a file as we have IOs left in this submission,
1781 * assuming most submissions are for one file, or at least that each file
1782 * has more than one submission.
1783 */
1784static struct file *io_file_get(struct io_submit_state *state, int fd)
1785{
1786 if (!state)
1787 return fget(fd);
1788
1789 if (state->file) {
1790 if (state->fd == fd) {
1791 state->used_refs++;
1792 state->ios_left--;
1793 return state->file;
1794 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001795 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001796 }
1797 state->file = fget_many(fd, state->ios_left);
1798 if (!state->file)
1799 return NULL;
1800
1801 state->fd = fd;
1802 state->has_refs = state->ios_left;
1803 state->used_refs = 1;
1804 state->ios_left--;
1805 return state->file;
1806}
1807
Jens Axboe2b188cc2019-01-07 10:46:33 -07001808/*
1809 * If we tracked the file through the SCM inflight mechanism, we could support
1810 * any file. For now, just ensure that anything potentially problematic is done
1811 * inline.
1812 */
1813static bool io_file_supports_async(struct file *file)
1814{
1815 umode_t mode = file_inode(file)->i_mode;
1816
Jens Axboe10d59342019-12-09 20:16:22 -07001817 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001818 return true;
1819 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1820 return true;
1821
1822 return false;
1823}
1824
Jens Axboe3529d8c2019-12-19 18:24:38 -07001825static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1826 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001827{
Jens Axboedef596e2019-01-09 08:59:42 -07001828 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001829 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001830 unsigned ioprio;
1831 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001832
Jens Axboe09bb8392019-03-13 12:39:28 -06001833 if (!req->file)
1834 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001835
Jens Axboe491381ce2019-10-17 09:20:46 -06001836 if (S_ISREG(file_inode(req->file)->i_mode))
1837 req->flags |= REQ_F_ISREG;
1838
Jens Axboe2b188cc2019-01-07 10:46:33 -07001839 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001840 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1841 req->flags |= REQ_F_CUR_POS;
1842 kiocb->ki_pos = req->file->f_pos;
1843 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001844 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1845 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1846
1847 ioprio = READ_ONCE(sqe->ioprio);
1848 if (ioprio) {
1849 ret = ioprio_check_cap(ioprio);
1850 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001851 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001852
1853 kiocb->ki_ioprio = ioprio;
1854 } else
1855 kiocb->ki_ioprio = get_current_ioprio();
1856
1857 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1858 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001859 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001860
1861 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001862 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1863 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001864 req->flags |= REQ_F_NOWAIT;
1865
1866 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001867 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001868
Jens Axboedef596e2019-01-09 08:59:42 -07001869 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001870 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1871 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001872 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001873
Jens Axboedef596e2019-01-09 08:59:42 -07001874 kiocb->ki_flags |= IOCB_HIPRI;
1875 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001876 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001877 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001878 if (kiocb->ki_flags & IOCB_HIPRI)
1879 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001880 kiocb->ki_complete = io_complete_rw;
1881 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001882
Jens Axboe3529d8c2019-12-19 18:24:38 -07001883 req->rw.addr = READ_ONCE(sqe->addr);
1884 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001885 /* we own ->private, reuse it for the buffer index */
1886 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001887 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001888 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001889}
1890
1891static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1892{
1893 switch (ret) {
1894 case -EIOCBQUEUED:
1895 break;
1896 case -ERESTARTSYS:
1897 case -ERESTARTNOINTR:
1898 case -ERESTARTNOHAND:
1899 case -ERESTART_RESTARTBLOCK:
1900 /*
1901 * We can't just restart the syscall, since previously
1902 * submitted sqes may already be in progress. Just fail this
1903 * IO with EINTR.
1904 */
1905 ret = -EINTR;
1906 /* fall through */
1907 default:
1908 kiocb->ki_complete(kiocb, ret, 0);
1909 }
1910}
1911
Jens Axboeba816ad2019-09-28 11:36:45 -06001912static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1913 bool in_async)
1914{
Jens Axboeba042912019-12-25 16:33:42 -07001915 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1916
1917 if (req->flags & REQ_F_CUR_POS)
1918 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001919 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001920 *nxt = __io_complete_rw(kiocb, ret);
1921 else
1922 io_rw_done(kiocb, ret);
1923}
1924
Jens Axboe9adbd452019-12-20 08:45:55 -07001925static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001926 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001927{
Jens Axboe9adbd452019-12-20 08:45:55 -07001928 struct io_ring_ctx *ctx = req->ctx;
1929 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001930 struct io_mapped_ubuf *imu;
1931 unsigned index, buf_index;
1932 size_t offset;
1933 u64 buf_addr;
1934
1935 /* attempt to use fixed buffers without having provided iovecs */
1936 if (unlikely(!ctx->user_bufs))
1937 return -EFAULT;
1938
Jens Axboe9adbd452019-12-20 08:45:55 -07001939 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001940 if (unlikely(buf_index >= ctx->nr_user_bufs))
1941 return -EFAULT;
1942
1943 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1944 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001945 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001946
1947 /* overflow */
1948 if (buf_addr + len < buf_addr)
1949 return -EFAULT;
1950 /* not inside the mapped region */
1951 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1952 return -EFAULT;
1953
1954 /*
1955 * May not be a start of buffer, set size appropriately
1956 * and advance us to the beginning.
1957 */
1958 offset = buf_addr - imu->ubuf;
1959 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001960
1961 if (offset) {
1962 /*
1963 * Don't use iov_iter_advance() here, as it's really slow for
1964 * using the latter parts of a big fixed buffer - it iterates
1965 * over each segment manually. We can cheat a bit here, because
1966 * we know that:
1967 *
1968 * 1) it's a BVEC iter, we set it up
1969 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1970 * first and last bvec
1971 *
1972 * So just find our index, and adjust the iterator afterwards.
1973 * If the offset is within the first bvec (or the whole first
1974 * bvec, just use iov_iter_advance(). This makes it easier
1975 * since we can just skip the first segment, which may not
1976 * be PAGE_SIZE aligned.
1977 */
1978 const struct bio_vec *bvec = imu->bvec;
1979
1980 if (offset <= bvec->bv_len) {
1981 iov_iter_advance(iter, offset);
1982 } else {
1983 unsigned long seg_skip;
1984
1985 /* skip first vec */
1986 offset -= bvec->bv_len;
1987 seg_skip = 1 + (offset >> PAGE_SHIFT);
1988
1989 iter->bvec = bvec + seg_skip;
1990 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001991 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001992 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001993 }
1994 }
1995
Jens Axboe5e559562019-11-13 16:12:46 -07001996 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001997}
1998
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001999static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2000 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002001{
Jens Axboe9adbd452019-12-20 08:45:55 -07002002 void __user *buf = u64_to_user_ptr(req->rw.addr);
2003 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002004 u8 opcode;
2005
Jens Axboed625c6e2019-12-17 19:53:05 -07002006 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002007 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002008 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002009 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002010 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002011
Jens Axboe9adbd452019-12-20 08:45:55 -07002012 /* buffer index only valid with fixed read/write */
2013 if (req->rw.kiocb.private)
2014 return -EINVAL;
2015
Jens Axboe3a6820f2019-12-22 15:19:35 -07002016 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2017 ssize_t ret;
2018 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2019 *iovec = NULL;
2020 return ret;
2021 }
2022
Jens Axboef67676d2019-12-02 11:03:47 -07002023 if (req->io) {
2024 struct io_async_rw *iorw = &req->io->rw;
2025
2026 *iovec = iorw->iov;
2027 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2028 if (iorw->iov == iorw->fast_iov)
2029 *iovec = NULL;
2030 return iorw->size;
2031 }
2032
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002033 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002034 return -EFAULT;
2035
2036#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002037 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002038 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2039 iovec, iter);
2040#endif
2041
2042 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2043}
2044
Jens Axboe32960612019-09-23 11:05:34 -06002045/*
2046 * For files that don't have ->read_iter() and ->write_iter(), handle them
2047 * by looping over ->read() or ->write() manually.
2048 */
2049static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2050 struct iov_iter *iter)
2051{
2052 ssize_t ret = 0;
2053
2054 /*
2055 * Don't support polled IO through this interface, and we can't
2056 * support non-blocking either. For the latter, this just causes
2057 * the kiocb to be handled from an async context.
2058 */
2059 if (kiocb->ki_flags & IOCB_HIPRI)
2060 return -EOPNOTSUPP;
2061 if (kiocb->ki_flags & IOCB_NOWAIT)
2062 return -EAGAIN;
2063
2064 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002065 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002066 ssize_t nr;
2067
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002068 if (!iov_iter_is_bvec(iter)) {
2069 iovec = iov_iter_iovec(iter);
2070 } else {
2071 /* fixed buffers import bvec */
2072 iovec.iov_base = kmap(iter->bvec->bv_page)
2073 + iter->iov_offset;
2074 iovec.iov_len = min(iter->count,
2075 iter->bvec->bv_len - iter->iov_offset);
2076 }
2077
Jens Axboe32960612019-09-23 11:05:34 -06002078 if (rw == READ) {
2079 nr = file->f_op->read(file, iovec.iov_base,
2080 iovec.iov_len, &kiocb->ki_pos);
2081 } else {
2082 nr = file->f_op->write(file, iovec.iov_base,
2083 iovec.iov_len, &kiocb->ki_pos);
2084 }
2085
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002086 if (iov_iter_is_bvec(iter))
2087 kunmap(iter->bvec->bv_page);
2088
Jens Axboe32960612019-09-23 11:05:34 -06002089 if (nr < 0) {
2090 if (!ret)
2091 ret = nr;
2092 break;
2093 }
2094 ret += nr;
2095 if (nr != iovec.iov_len)
2096 break;
2097 iov_iter_advance(iter, nr);
2098 }
2099
2100 return ret;
2101}
2102
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002103static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002104 struct iovec *iovec, struct iovec *fast_iov,
2105 struct iov_iter *iter)
2106{
2107 req->io->rw.nr_segs = iter->nr_segs;
2108 req->io->rw.size = io_size;
2109 req->io->rw.iov = iovec;
2110 if (!req->io->rw.iov) {
2111 req->io->rw.iov = req->io->rw.fast_iov;
2112 memcpy(req->io->rw.iov, fast_iov,
2113 sizeof(struct iovec) * iter->nr_segs);
2114 }
2115}
2116
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002117static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002118{
Jens Axboed3656342019-12-18 09:50:26 -07002119 if (!io_op_defs[req->opcode].async_ctx)
2120 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002121 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002122 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002123}
2124
2125static void io_rw_async(struct io_wq_work **workptr)
2126{
2127 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2128 struct iovec *iov = NULL;
2129
2130 if (req->io->rw.iov != req->io->rw.fast_iov)
2131 iov = req->io->rw.iov;
2132 io_wq_submit_work(workptr);
2133 kfree(iov);
2134}
2135
2136static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2137 struct iovec *iovec, struct iovec *fast_iov,
2138 struct iov_iter *iter)
2139{
Jens Axboe980ad262020-01-24 23:08:54 -07002140 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002141 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002142 if (!req->io && io_alloc_async_ctx(req))
2143 return -ENOMEM;
2144
2145 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2146 req->work.func = io_rw_async;
2147 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002148}
2149
Jens Axboe3529d8c2019-12-19 18:24:38 -07002150static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2151 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002152{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002153 struct io_async_ctx *io;
2154 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002155 ssize_t ret;
2156
Jens Axboe3529d8c2019-12-19 18:24:38 -07002157 ret = io_prep_rw(req, sqe, force_nonblock);
2158 if (ret)
2159 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002160
Jens Axboe3529d8c2019-12-19 18:24:38 -07002161 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2162 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002163
Jens Axboe3529d8c2019-12-19 18:24:38 -07002164 if (!req->io)
2165 return 0;
2166
2167 io = req->io;
2168 io->rw.iov = io->rw.fast_iov;
2169 req->io = NULL;
2170 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2171 req->io = io;
2172 if (ret < 0)
2173 return ret;
2174
2175 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2176 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002177}
2178
Pavel Begunkov267bc902019-11-07 01:41:08 +03002179static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002180 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002181{
2182 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002183 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002184 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002185 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002186 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002187
Jens Axboe3529d8c2019-12-19 18:24:38 -07002188 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002189 if (ret < 0)
2190 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002191
Jens Axboefd6c2e42019-12-18 12:19:41 -07002192 /* Ensure we clear previously set non-block flag */
2193 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002194 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002195
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002196 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002197 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002198 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002199 req->result = io_size;
2200
2201 /*
2202 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2203 * we know to async punt it even if it was opened O_NONBLOCK
2204 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002205 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002206 req->flags |= REQ_F_MUST_PUNT;
2207 goto copy_iov;
2208 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002209
Jens Axboe31b51512019-01-18 22:56:34 -07002210 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002211 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002212 if (!ret) {
2213 ssize_t ret2;
2214
Jens Axboe9adbd452019-12-20 08:45:55 -07002215 if (req->file->f_op->read_iter)
2216 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002217 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002218 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002219
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002220 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002221 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002222 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002223 } else {
2224copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002225 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002226 inline_vecs, &iter);
2227 if (ret)
2228 goto out_free;
2229 return -EAGAIN;
2230 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002231 }
Jens Axboef67676d2019-12-02 11:03:47 -07002232out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002233 if (!io_wq_current_is_worker())
2234 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002235 return ret;
2236}
2237
Jens Axboe3529d8c2019-12-19 18:24:38 -07002238static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2239 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002240{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002241 struct io_async_ctx *io;
2242 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002243 ssize_t ret;
2244
Jens Axboe3529d8c2019-12-19 18:24:38 -07002245 ret = io_prep_rw(req, sqe, force_nonblock);
2246 if (ret)
2247 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002248
Jens Axboe3529d8c2019-12-19 18:24:38 -07002249 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2250 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002251
Jens Axboe3529d8c2019-12-19 18:24:38 -07002252 if (!req->io)
2253 return 0;
2254
2255 io = req->io;
2256 io->rw.iov = io->rw.fast_iov;
2257 req->io = NULL;
2258 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2259 req->io = io;
2260 if (ret < 0)
2261 return ret;
2262
2263 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2264 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002265}
2266
Pavel Begunkov267bc902019-11-07 01:41:08 +03002267static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002268 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002269{
2270 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002271 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002272 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002273 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002274 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002275
Jens Axboe3529d8c2019-12-19 18:24:38 -07002276 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002277 if (ret < 0)
2278 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002279
Jens Axboefd6c2e42019-12-18 12:19:41 -07002280 /* Ensure we clear previously set non-block flag */
2281 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002282 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002283
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002284 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002285 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002286 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002287 req->result = io_size;
2288
2289 /*
2290 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2291 * we know to async punt it even if it was opened O_NONBLOCK
2292 */
2293 if (force_nonblock && !io_file_supports_async(req->file)) {
2294 req->flags |= REQ_F_MUST_PUNT;
2295 goto copy_iov;
2296 }
2297
Jens Axboe10d59342019-12-09 20:16:22 -07002298 /* file path doesn't support NOWAIT for non-direct_IO */
2299 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2300 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002301 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002302
Jens Axboe31b51512019-01-18 22:56:34 -07002303 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002304 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002305 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002306 ssize_t ret2;
2307
Jens Axboe2b188cc2019-01-07 10:46:33 -07002308 /*
2309 * Open-code file_start_write here to grab freeze protection,
2310 * which will be released by another thread in
2311 * io_complete_rw(). Fool lockdep by telling it the lock got
2312 * released so that it doesn't complain about the held lock when
2313 * we return to userspace.
2314 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002315 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002316 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002317 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002318 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002319 SB_FREEZE_WRITE);
2320 }
2321 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002322
Jens Axboe9adbd452019-12-20 08:45:55 -07002323 if (req->file->f_op->write_iter)
2324 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002325 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002326 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002327 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002328 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002329 } else {
2330copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002331 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002332 inline_vecs, &iter);
2333 if (ret)
2334 goto out_free;
2335 return -EAGAIN;
2336 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002337 }
Jens Axboe31b51512019-01-18 22:56:34 -07002338out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002339 if (!io_wq_current_is_worker())
2340 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002341 return ret;
2342}
2343
2344/*
2345 * IORING_OP_NOP just posts a completion event, nothing else.
2346 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002347static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002348{
2349 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002350
Jens Axboedef596e2019-01-09 08:59:42 -07002351 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2352 return -EINVAL;
2353
Jens Axboe78e19bb2019-11-06 15:21:34 -07002354 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002355 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002356 return 0;
2357}
2358
Jens Axboe3529d8c2019-12-19 18:24:38 -07002359static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002360{
Jens Axboe6b063142019-01-10 22:13:58 -07002361 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002362
Jens Axboe09bb8392019-03-13 12:39:28 -06002363 if (!req->file)
2364 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002365
Jens Axboe6b063142019-01-10 22:13:58 -07002366 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002367 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002368 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002369 return -EINVAL;
2370
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002371 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2372 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2373 return -EINVAL;
2374
2375 req->sync.off = READ_ONCE(sqe->off);
2376 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002377 return 0;
2378}
2379
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002380static bool io_req_cancelled(struct io_kiocb *req)
2381{
2382 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2383 req_set_fail_links(req);
2384 io_cqring_add_event(req, -ECANCELED);
2385 io_put_req(req);
2386 return true;
2387 }
2388
2389 return false;
2390}
2391
Jens Axboe78912932020-01-14 22:09:06 -07002392static void io_link_work_cb(struct io_wq_work **workptr)
2393{
2394 struct io_wq_work *work = *workptr;
2395 struct io_kiocb *link = work->data;
2396
2397 io_queue_linked_timeout(link);
2398 work->func = io_wq_submit_work;
2399}
2400
2401static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2402{
2403 struct io_kiocb *link;
2404
2405 io_prep_async_work(nxt, &link);
2406 *workptr = &nxt->work;
2407 if (link) {
2408 nxt->work.flags |= IO_WQ_WORK_CB;
2409 nxt->work.func = io_link_work_cb;
2410 nxt->work.data = link;
2411 }
2412}
2413
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002414static void io_fsync_finish(struct io_wq_work **workptr)
2415{
2416 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2417 loff_t end = req->sync.off + req->sync.len;
2418 struct io_kiocb *nxt = NULL;
2419 int ret;
2420
2421 if (io_req_cancelled(req))
2422 return;
2423
Jens Axboe9adbd452019-12-20 08:45:55 -07002424 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002425 end > 0 ? end : LLONG_MAX,
2426 req->sync.flags & IORING_FSYNC_DATASYNC);
2427 if (ret < 0)
2428 req_set_fail_links(req);
2429 io_cqring_add_event(req, ret);
2430 io_put_req_find_next(req, &nxt);
2431 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002432 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002433}
2434
Jens Axboefc4df992019-12-10 14:38:45 -07002435static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2436 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002437{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002438 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002439
2440 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002441 if (force_nonblock) {
2442 io_put_req(req);
2443 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002444 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002445 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002446
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002447 work = old_work = &req->work;
2448 io_fsync_finish(&work);
2449 if (work && work != old_work)
2450 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002451 return 0;
2452}
2453
Jens Axboed63d1b52019-12-10 10:38:56 -07002454static void io_fallocate_finish(struct io_wq_work **workptr)
2455{
2456 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2457 struct io_kiocb *nxt = NULL;
2458 int ret;
2459
2460 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2461 req->sync.len);
2462 if (ret < 0)
2463 req_set_fail_links(req);
2464 io_cqring_add_event(req, ret);
2465 io_put_req_find_next(req, &nxt);
2466 if (nxt)
2467 io_wq_assign_next(workptr, nxt);
2468}
2469
2470static int io_fallocate_prep(struct io_kiocb *req,
2471 const struct io_uring_sqe *sqe)
2472{
2473 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2474 return -EINVAL;
2475
2476 req->sync.off = READ_ONCE(sqe->off);
2477 req->sync.len = READ_ONCE(sqe->addr);
2478 req->sync.mode = READ_ONCE(sqe->len);
2479 return 0;
2480}
2481
2482static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2483 bool force_nonblock)
2484{
2485 struct io_wq_work *work, *old_work;
2486
2487 /* fallocate always requiring blocking context */
2488 if (force_nonblock) {
2489 io_put_req(req);
2490 req->work.func = io_fallocate_finish;
2491 return -EAGAIN;
2492 }
2493
2494 work = old_work = &req->work;
2495 io_fallocate_finish(&work);
2496 if (work && work != old_work)
2497 *nxt = container_of(work, struct io_kiocb, work);
2498
2499 return 0;
2500}
2501
Jens Axboe15b71ab2019-12-11 11:20:36 -07002502static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2503{
Jens Axboef8748882020-01-08 17:47:02 -07002504 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002505 int ret;
2506
2507 if (sqe->ioprio || sqe->buf_index)
2508 return -EINVAL;
2509
2510 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002511 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002512 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002513 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002514
Jens Axboef8748882020-01-08 17:47:02 -07002515 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002516 if (IS_ERR(req->open.filename)) {
2517 ret = PTR_ERR(req->open.filename);
2518 req->open.filename = NULL;
2519 return ret;
2520 }
2521
2522 return 0;
2523}
2524
Jens Axboecebdb982020-01-08 17:59:24 -07002525static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2526{
2527 struct open_how __user *how;
2528 const char __user *fname;
2529 size_t len;
2530 int ret;
2531
2532 if (sqe->ioprio || sqe->buf_index)
2533 return -EINVAL;
2534
2535 req->open.dfd = READ_ONCE(sqe->fd);
2536 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2537 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2538 len = READ_ONCE(sqe->len);
2539
2540 if (len < OPEN_HOW_SIZE_VER0)
2541 return -EINVAL;
2542
2543 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2544 len);
2545 if (ret)
2546 return ret;
2547
2548 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2549 req->open.how.flags |= O_LARGEFILE;
2550
2551 req->open.filename = getname(fname);
2552 if (IS_ERR(req->open.filename)) {
2553 ret = PTR_ERR(req->open.filename);
2554 req->open.filename = NULL;
2555 return ret;
2556 }
2557
2558 return 0;
2559}
2560
2561static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2562 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002563{
2564 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002565 struct file *file;
2566 int ret;
2567
2568 if (force_nonblock) {
2569 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2570 return -EAGAIN;
2571 }
2572
Jens Axboecebdb982020-01-08 17:59:24 -07002573 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002574 if (ret)
2575 goto err;
2576
Jens Axboecebdb982020-01-08 17:59:24 -07002577 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002578 if (ret < 0)
2579 goto err;
2580
2581 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2582 if (IS_ERR(file)) {
2583 put_unused_fd(ret);
2584 ret = PTR_ERR(file);
2585 } else {
2586 fsnotify_open(file);
2587 fd_install(ret, file);
2588 }
2589err:
2590 putname(req->open.filename);
2591 if (ret < 0)
2592 req_set_fail_links(req);
2593 io_cqring_add_event(req, ret);
2594 io_put_req_find_next(req, nxt);
2595 return 0;
2596}
2597
Jens Axboecebdb982020-01-08 17:59:24 -07002598static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2599 bool force_nonblock)
2600{
2601 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2602 return io_openat2(req, nxt, force_nonblock);
2603}
2604
Jens Axboec1ca7572019-12-25 22:18:28 -07002605static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2606{
2607#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2608 if (sqe->ioprio || sqe->buf_index || sqe->off)
2609 return -EINVAL;
2610
2611 req->madvise.addr = READ_ONCE(sqe->addr);
2612 req->madvise.len = READ_ONCE(sqe->len);
2613 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2614 return 0;
2615#else
2616 return -EOPNOTSUPP;
2617#endif
2618}
2619
2620static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2621 bool force_nonblock)
2622{
2623#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2624 struct io_madvise *ma = &req->madvise;
2625 int ret;
2626
2627 if (force_nonblock)
2628 return -EAGAIN;
2629
2630 ret = do_madvise(ma->addr, ma->len, ma->advice);
2631 if (ret < 0)
2632 req_set_fail_links(req);
2633 io_cqring_add_event(req, ret);
2634 io_put_req_find_next(req, nxt);
2635 return 0;
2636#else
2637 return -EOPNOTSUPP;
2638#endif
2639}
2640
Jens Axboe4840e412019-12-25 22:03:45 -07002641static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2642{
2643 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2644 return -EINVAL;
2645
2646 req->fadvise.offset = READ_ONCE(sqe->off);
2647 req->fadvise.len = READ_ONCE(sqe->len);
2648 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2649 return 0;
2650}
2651
2652static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2653 bool force_nonblock)
2654{
2655 struct io_fadvise *fa = &req->fadvise;
2656 int ret;
2657
2658 /* DONTNEED may block, others _should_ not */
2659 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2660 return -EAGAIN;
2661
2662 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2663 if (ret < 0)
2664 req_set_fail_links(req);
2665 io_cqring_add_event(req, ret);
2666 io_put_req_find_next(req, nxt);
2667 return 0;
2668}
2669
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002670static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2671{
Jens Axboef8748882020-01-08 17:47:02 -07002672 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002673 unsigned lookup_flags;
2674 int ret;
2675
2676 if (sqe->ioprio || sqe->buf_index)
2677 return -EINVAL;
2678
2679 req->open.dfd = READ_ONCE(sqe->fd);
2680 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002681 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002682 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002683 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002684
Jens Axboec12cedf2020-01-08 17:41:21 -07002685 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002686 return -EINVAL;
2687
Jens Axboef8748882020-01-08 17:47:02 -07002688 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002689 if (IS_ERR(req->open.filename)) {
2690 ret = PTR_ERR(req->open.filename);
2691 req->open.filename = NULL;
2692 return ret;
2693 }
2694
2695 return 0;
2696}
2697
2698static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2699 bool force_nonblock)
2700{
2701 struct io_open *ctx = &req->open;
2702 unsigned lookup_flags;
2703 struct path path;
2704 struct kstat stat;
2705 int ret;
2706
2707 if (force_nonblock)
2708 return -EAGAIN;
2709
Jens Axboec12cedf2020-01-08 17:41:21 -07002710 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002711 return -EINVAL;
2712
2713retry:
2714 /* filename_lookup() drops it, keep a reference */
2715 ctx->filename->refcnt++;
2716
2717 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2718 NULL);
2719 if (ret)
2720 goto err;
2721
Jens Axboec12cedf2020-01-08 17:41:21 -07002722 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002723 path_put(&path);
2724 if (retry_estale(ret, lookup_flags)) {
2725 lookup_flags |= LOOKUP_REVAL;
2726 goto retry;
2727 }
2728 if (!ret)
2729 ret = cp_statx(&stat, ctx->buffer);
2730err:
2731 putname(ctx->filename);
2732 if (ret < 0)
2733 req_set_fail_links(req);
2734 io_cqring_add_event(req, ret);
2735 io_put_req_find_next(req, nxt);
2736 return 0;
2737}
2738
Jens Axboeb5dba592019-12-11 14:02:38 -07002739static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2740{
2741 /*
2742 * If we queue this for async, it must not be cancellable. That would
2743 * leave the 'file' in an undeterminate state.
2744 */
2745 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2746
2747 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2748 sqe->rw_flags || sqe->buf_index)
2749 return -EINVAL;
2750 if (sqe->flags & IOSQE_FIXED_FILE)
2751 return -EINVAL;
2752
2753 req->close.fd = READ_ONCE(sqe->fd);
2754 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002755 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002756 return -EBADF;
2757
2758 return 0;
2759}
2760
2761static void io_close_finish(struct io_wq_work **workptr)
2762{
2763 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2764 struct io_kiocb *nxt = NULL;
2765
2766 /* Invoked with files, we need to do the close */
2767 if (req->work.files) {
2768 int ret;
2769
2770 ret = filp_close(req->close.put_file, req->work.files);
2771 if (ret < 0) {
2772 req_set_fail_links(req);
2773 }
2774 io_cqring_add_event(req, ret);
2775 }
2776
2777 fput(req->close.put_file);
2778
2779 /* we bypassed the re-issue, drop the submission reference */
2780 io_put_req(req);
2781 io_put_req_find_next(req, &nxt);
2782 if (nxt)
2783 io_wq_assign_next(workptr, nxt);
2784}
2785
2786static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2787 bool force_nonblock)
2788{
2789 int ret;
2790
2791 req->close.put_file = NULL;
2792 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2793 if (ret < 0)
2794 return ret;
2795
2796 /* if the file has a flush method, be safe and punt to async */
2797 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2798 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2799 goto eagain;
2800 }
2801
2802 /*
2803 * No ->flush(), safely close from here and just punt the
2804 * fput() to async context.
2805 */
2806 ret = filp_close(req->close.put_file, current->files);
2807
2808 if (ret < 0)
2809 req_set_fail_links(req);
2810 io_cqring_add_event(req, ret);
2811
2812 if (io_wq_current_is_worker()) {
2813 struct io_wq_work *old_work, *work;
2814
2815 old_work = work = &req->work;
2816 io_close_finish(&work);
2817 if (work && work != old_work)
2818 *nxt = container_of(work, struct io_kiocb, work);
2819 return 0;
2820 }
2821
2822eagain:
2823 req->work.func = io_close_finish;
2824 return -EAGAIN;
2825}
2826
Jens Axboe3529d8c2019-12-19 18:24:38 -07002827static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002828{
2829 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002830
2831 if (!req->file)
2832 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002833
2834 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2835 return -EINVAL;
2836 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2837 return -EINVAL;
2838
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002839 req->sync.off = READ_ONCE(sqe->off);
2840 req->sync.len = READ_ONCE(sqe->len);
2841 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002842 return 0;
2843}
2844
2845static void io_sync_file_range_finish(struct io_wq_work **workptr)
2846{
2847 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2848 struct io_kiocb *nxt = NULL;
2849 int ret;
2850
2851 if (io_req_cancelled(req))
2852 return;
2853
Jens Axboe9adbd452019-12-20 08:45:55 -07002854 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002855 req->sync.flags);
2856 if (ret < 0)
2857 req_set_fail_links(req);
2858 io_cqring_add_event(req, ret);
2859 io_put_req_find_next(req, &nxt);
2860 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002861 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002862}
2863
Jens Axboefc4df992019-12-10 14:38:45 -07002864static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002865 bool force_nonblock)
2866{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002867 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002868
2869 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002870 if (force_nonblock) {
2871 io_put_req(req);
2872 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002873 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002874 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002875
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002876 work = old_work = &req->work;
2877 io_sync_file_range_finish(&work);
2878 if (work && work != old_work)
2879 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002880 return 0;
2881}
2882
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002883#if defined(CONFIG_NET)
2884static void io_sendrecv_async(struct io_wq_work **workptr)
2885{
2886 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2887 struct iovec *iov = NULL;
2888
2889 if (req->io->rw.iov != req->io->rw.fast_iov)
2890 iov = req->io->msg.iov;
2891 io_wq_submit_work(workptr);
2892 kfree(iov);
2893}
2894#endif
2895
Jens Axboe3529d8c2019-12-19 18:24:38 -07002896static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002897{
Jens Axboe03b12302019-12-02 18:50:25 -07002898#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002899 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002900 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002901
Jens Axboee47293f2019-12-20 08:58:21 -07002902 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2903 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002904 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002905
Jens Axboefddafac2020-01-04 20:19:44 -07002906 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002907 return 0;
2908
Jens Axboed9688562019-12-09 19:35:20 -07002909 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002910 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002911 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002912#else
Jens Axboee47293f2019-12-20 08:58:21 -07002913 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002914#endif
2915}
2916
Jens Axboefc4df992019-12-10 14:38:45 -07002917static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2918 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002919{
2920#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002921 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002922 struct socket *sock;
2923 int ret;
2924
2925 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2926 return -EINVAL;
2927
2928 sock = sock_from_file(req->file, &ret);
2929 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002930 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002931 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002932 unsigned flags;
2933
Jens Axboe03b12302019-12-02 18:50:25 -07002934 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002935 kmsg = &req->io->msg;
2936 kmsg->msg.msg_name = &addr;
2937 /* if iov is set, it's allocated already */
2938 if (!kmsg->iov)
2939 kmsg->iov = kmsg->fast_iov;
2940 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002941 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002942 struct io_sr_msg *sr = &req->sr_msg;
2943
Jens Axboe0b416c32019-12-15 10:57:46 -07002944 kmsg = &io.msg;
2945 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002946
2947 io.msg.iov = io.msg.fast_iov;
2948 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2949 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002950 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002951 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002952 }
2953
Jens Axboee47293f2019-12-20 08:58:21 -07002954 flags = req->sr_msg.msg_flags;
2955 if (flags & MSG_DONTWAIT)
2956 req->flags |= REQ_F_NOWAIT;
2957 else if (force_nonblock)
2958 flags |= MSG_DONTWAIT;
2959
Jens Axboe0b416c32019-12-15 10:57:46 -07002960 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002961 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002962 if (req->io)
2963 return -EAGAIN;
2964 if (io_alloc_async_ctx(req))
2965 return -ENOMEM;
2966 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2967 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002968 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002969 }
2970 if (ret == -ERESTARTSYS)
2971 ret = -EINTR;
2972 }
2973
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002974 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002975 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002976 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002977 if (ret < 0)
2978 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002979 io_put_req_find_next(req, nxt);
2980 return 0;
2981#else
2982 return -EOPNOTSUPP;
2983#endif
2984}
2985
Jens Axboefddafac2020-01-04 20:19:44 -07002986static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2987 bool force_nonblock)
2988{
2989#if defined(CONFIG_NET)
2990 struct socket *sock;
2991 int ret;
2992
2993 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2994 return -EINVAL;
2995
2996 sock = sock_from_file(req->file, &ret);
2997 if (sock) {
2998 struct io_sr_msg *sr = &req->sr_msg;
2999 struct msghdr msg;
3000 struct iovec iov;
3001 unsigned flags;
3002
3003 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3004 &msg.msg_iter);
3005 if (ret)
3006 return ret;
3007
3008 msg.msg_name = NULL;
3009 msg.msg_control = NULL;
3010 msg.msg_controllen = 0;
3011 msg.msg_namelen = 0;
3012
3013 flags = req->sr_msg.msg_flags;
3014 if (flags & MSG_DONTWAIT)
3015 req->flags |= REQ_F_NOWAIT;
3016 else if (force_nonblock)
3017 flags |= MSG_DONTWAIT;
3018
3019 ret = __sys_sendmsg_sock(sock, &msg, flags);
3020 if (force_nonblock && ret == -EAGAIN)
3021 return -EAGAIN;
3022 if (ret == -ERESTARTSYS)
3023 ret = -EINTR;
3024 }
3025
3026 io_cqring_add_event(req, ret);
3027 if (ret < 0)
3028 req_set_fail_links(req);
3029 io_put_req_find_next(req, nxt);
3030 return 0;
3031#else
3032 return -EOPNOTSUPP;
3033#endif
3034}
3035
Jens Axboe3529d8c2019-12-19 18:24:38 -07003036static int io_recvmsg_prep(struct io_kiocb *req,
3037 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003038{
3039#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003040 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003041 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07003042
Jens Axboe3529d8c2019-12-19 18:24:38 -07003043 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3044 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
3045
Jens Axboefddafac2020-01-04 20:19:44 -07003046 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003047 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003048
Jens Axboed9688562019-12-09 19:35:20 -07003049 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003050 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003051 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003052#else
Jens Axboee47293f2019-12-20 08:58:21 -07003053 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003054#endif
3055}
3056
Jens Axboefc4df992019-12-10 14:38:45 -07003057static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3058 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003059{
3060#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003061 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003062 struct socket *sock;
3063 int ret;
3064
3065 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3066 return -EINVAL;
3067
3068 sock = sock_from_file(req->file, &ret);
3069 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003070 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003071 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003072 unsigned flags;
3073
Jens Axboe03b12302019-12-02 18:50:25 -07003074 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003075 kmsg = &req->io->msg;
3076 kmsg->msg.msg_name = &addr;
3077 /* if iov is set, it's allocated already */
3078 if (!kmsg->iov)
3079 kmsg->iov = kmsg->fast_iov;
3080 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003081 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003082 struct io_sr_msg *sr = &req->sr_msg;
3083
Jens Axboe0b416c32019-12-15 10:57:46 -07003084 kmsg = &io.msg;
3085 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003086
3087 io.msg.iov = io.msg.fast_iov;
3088 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3089 sr->msg_flags, &io.msg.uaddr,
3090 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003091 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003092 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003093 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003094
Jens Axboee47293f2019-12-20 08:58:21 -07003095 flags = req->sr_msg.msg_flags;
3096 if (flags & MSG_DONTWAIT)
3097 req->flags |= REQ_F_NOWAIT;
3098 else if (force_nonblock)
3099 flags |= MSG_DONTWAIT;
3100
3101 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3102 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003103 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003104 if (req->io)
3105 return -EAGAIN;
3106 if (io_alloc_async_ctx(req))
3107 return -ENOMEM;
3108 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3109 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003110 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003111 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003112 if (ret == -ERESTARTSYS)
3113 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003114 }
3115
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003116 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003117 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003118 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003119 if (ret < 0)
3120 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003121 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003122 return 0;
3123#else
3124 return -EOPNOTSUPP;
3125#endif
3126}
3127
Jens Axboefddafac2020-01-04 20:19:44 -07003128static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3129 bool force_nonblock)
3130{
3131#if defined(CONFIG_NET)
3132 struct socket *sock;
3133 int ret;
3134
3135 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3136 return -EINVAL;
3137
3138 sock = sock_from_file(req->file, &ret);
3139 if (sock) {
3140 struct io_sr_msg *sr = &req->sr_msg;
3141 struct msghdr msg;
3142 struct iovec iov;
3143 unsigned flags;
3144
3145 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3146 &msg.msg_iter);
3147 if (ret)
3148 return ret;
3149
3150 msg.msg_name = NULL;
3151 msg.msg_control = NULL;
3152 msg.msg_controllen = 0;
3153 msg.msg_namelen = 0;
3154 msg.msg_iocb = NULL;
3155 msg.msg_flags = 0;
3156
3157 flags = req->sr_msg.msg_flags;
3158 if (flags & MSG_DONTWAIT)
3159 req->flags |= REQ_F_NOWAIT;
3160 else if (force_nonblock)
3161 flags |= MSG_DONTWAIT;
3162
3163 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3164 if (force_nonblock && ret == -EAGAIN)
3165 return -EAGAIN;
3166 if (ret == -ERESTARTSYS)
3167 ret = -EINTR;
3168 }
3169
3170 io_cqring_add_event(req, ret);
3171 if (ret < 0)
3172 req_set_fail_links(req);
3173 io_put_req_find_next(req, nxt);
3174 return 0;
3175#else
3176 return -EOPNOTSUPP;
3177#endif
3178}
3179
3180
Jens Axboe3529d8c2019-12-19 18:24:38 -07003181static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003182{
3183#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003184 struct io_accept *accept = &req->accept;
3185
Jens Axboe17f2fe32019-10-17 14:42:58 -06003186 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3187 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003188 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003189 return -EINVAL;
3190
Jens Axboed55e5f52019-12-11 16:12:15 -07003191 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3192 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003193 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003194 return 0;
3195#else
3196 return -EOPNOTSUPP;
3197#endif
3198}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003199
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003200#if defined(CONFIG_NET)
3201static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3202 bool force_nonblock)
3203{
3204 struct io_accept *accept = &req->accept;
3205 unsigned file_flags;
3206 int ret;
3207
3208 file_flags = force_nonblock ? O_NONBLOCK : 0;
3209 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3210 accept->addr_len, accept->flags);
3211 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003212 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003213 if (ret == -ERESTARTSYS)
3214 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003215 if (ret < 0)
3216 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003217 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003218 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003219 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003220}
3221
3222static void io_accept_finish(struct io_wq_work **workptr)
3223{
3224 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3225 struct io_kiocb *nxt = NULL;
3226
3227 if (io_req_cancelled(req))
3228 return;
3229 __io_accept(req, &nxt, false);
3230 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003231 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003232}
3233#endif
3234
3235static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3236 bool force_nonblock)
3237{
3238#if defined(CONFIG_NET)
3239 int ret;
3240
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003241 ret = __io_accept(req, nxt, force_nonblock);
3242 if (ret == -EAGAIN && force_nonblock) {
3243 req->work.func = io_accept_finish;
3244 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3245 io_put_req(req);
3246 return -EAGAIN;
3247 }
3248 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003249#else
3250 return -EOPNOTSUPP;
3251#endif
3252}
3253
Jens Axboe3529d8c2019-12-19 18:24:38 -07003254static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003255{
3256#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003257 struct io_connect *conn = &req->connect;
3258 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003259
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003260 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3261 return -EINVAL;
3262 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3263 return -EINVAL;
3264
Jens Axboe3529d8c2019-12-19 18:24:38 -07003265 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3266 conn->addr_len = READ_ONCE(sqe->addr2);
3267
3268 if (!io)
3269 return 0;
3270
3271 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003272 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003273#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003274 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003275#endif
3276}
3277
Jens Axboefc4df992019-12-10 14:38:45 -07003278static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3279 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003280{
3281#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003282 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003283 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003284 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003285
Jens Axboef499a022019-12-02 16:28:46 -07003286 if (req->io) {
3287 io = req->io;
3288 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003289 ret = move_addr_to_kernel(req->connect.addr,
3290 req->connect.addr_len,
3291 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003292 if (ret)
3293 goto out;
3294 io = &__io;
3295 }
3296
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003297 file_flags = force_nonblock ? O_NONBLOCK : 0;
3298
3299 ret = __sys_connect_file(req->file, &io->connect.address,
3300 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003301 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003302 if (req->io)
3303 return -EAGAIN;
3304 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003305 ret = -ENOMEM;
3306 goto out;
3307 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003308 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003309 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003310 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003311 if (ret == -ERESTARTSYS)
3312 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003313out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003314 if (ret < 0)
3315 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003316 io_cqring_add_event(req, ret);
3317 io_put_req_find_next(req, nxt);
3318 return 0;
3319#else
3320 return -EOPNOTSUPP;
3321#endif
3322}
3323
Jens Axboe221c5eb2019-01-17 09:41:58 -07003324static void io_poll_remove_one(struct io_kiocb *req)
3325{
3326 struct io_poll_iocb *poll = &req->poll;
3327
3328 spin_lock(&poll->head->lock);
3329 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003330 if (!list_empty(&poll->wait.entry)) {
3331 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003332 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003333 }
3334 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003335 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003336}
3337
3338static void io_poll_remove_all(struct io_ring_ctx *ctx)
3339{
Jens Axboe78076bb2019-12-04 19:56:40 -07003340 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003341 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003342 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003343
3344 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003345 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3346 struct hlist_head *list;
3347
3348 list = &ctx->cancel_hash[i];
3349 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3350 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003351 }
3352 spin_unlock_irq(&ctx->completion_lock);
3353}
3354
Jens Axboe47f46762019-11-09 17:43:02 -07003355static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3356{
Jens Axboe78076bb2019-12-04 19:56:40 -07003357 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003358 struct io_kiocb *req;
3359
Jens Axboe78076bb2019-12-04 19:56:40 -07003360 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3361 hlist_for_each_entry(req, list, hash_node) {
3362 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003363 io_poll_remove_one(req);
3364 return 0;
3365 }
Jens Axboe47f46762019-11-09 17:43:02 -07003366 }
3367
3368 return -ENOENT;
3369}
3370
Jens Axboe3529d8c2019-12-19 18:24:38 -07003371static int io_poll_remove_prep(struct io_kiocb *req,
3372 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003373{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003374 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3375 return -EINVAL;
3376 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3377 sqe->poll_events)
3378 return -EINVAL;
3379
Jens Axboe0969e782019-12-17 18:40:57 -07003380 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003381 return 0;
3382}
3383
3384/*
3385 * Find a running poll command that matches one specified in sqe->addr,
3386 * and remove it if found.
3387 */
3388static int io_poll_remove(struct io_kiocb *req)
3389{
3390 struct io_ring_ctx *ctx = req->ctx;
3391 u64 addr;
3392 int ret;
3393
Jens Axboe0969e782019-12-17 18:40:57 -07003394 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003395 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003396 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003397 spin_unlock_irq(&ctx->completion_lock);
3398
Jens Axboe78e19bb2019-11-06 15:21:34 -07003399 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003400 if (ret < 0)
3401 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003402 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003403 return 0;
3404}
3405
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003406static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003407{
Jackie Liua197f662019-11-08 08:09:12 -07003408 struct io_ring_ctx *ctx = req->ctx;
3409
Jens Axboe8c838782019-03-12 15:48:16 -06003410 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003411 if (error)
3412 io_cqring_fill_event(req, error);
3413 else
3414 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003415 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003416}
3417
Jens Axboe561fb042019-10-24 07:25:42 -06003418static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003419{
Jens Axboe561fb042019-10-24 07:25:42 -06003420 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003421 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3422 struct io_poll_iocb *poll = &req->poll;
3423 struct poll_table_struct pt = { ._key = poll->events };
3424 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003425 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003426 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003427 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003428
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003429 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003430 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003431 ret = -ECANCELED;
3432 } else if (READ_ONCE(poll->canceled)) {
3433 ret = -ECANCELED;
3434 }
Jens Axboe561fb042019-10-24 07:25:42 -06003435
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003436 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003437 mask = vfs_poll(poll->file, &pt) & poll->events;
3438
3439 /*
3440 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3441 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3442 * synchronize with them. In the cancellation case the list_del_init
3443 * itself is not actually needed, but harmless so we keep it in to
3444 * avoid further branches in the fast path.
3445 */
3446 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003447 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003448 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003449 spin_unlock_irq(&ctx->completion_lock);
3450 return;
3451 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003452 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003453 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003454 spin_unlock_irq(&ctx->completion_lock);
3455
Jens Axboe8c838782019-03-12 15:48:16 -06003456 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003457
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003458 if (ret < 0)
3459 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003460 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003461 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003462 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003463}
3464
Jens Axboee94f1412019-12-19 12:06:02 -07003465static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3466{
Jens Axboee94f1412019-12-19 12:06:02 -07003467 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003468 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003469
Jens Axboec6ca97b302019-12-28 12:11:08 -07003470 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003471 spin_lock_irq(&ctx->completion_lock);
3472 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3473 hash_del(&req->hash_node);
3474 io_poll_complete(req, req->result, 0);
3475
Jens Axboe8237e042019-12-28 10:48:22 -07003476 if (refcount_dec_and_test(&req->refs) &&
3477 !io_req_multi_free(&rb, req)) {
3478 req->flags |= REQ_F_COMP_LOCKED;
3479 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003480 }
3481 }
3482 spin_unlock_irq(&ctx->completion_lock);
3483
3484 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003485 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003486}
3487
3488static void io_poll_flush(struct io_wq_work **workptr)
3489{
3490 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3491 struct llist_node *nodes;
3492
3493 nodes = llist_del_all(&req->ctx->poll_llist);
3494 if (nodes)
3495 __io_poll_flush(req->ctx, nodes);
3496}
3497
Jens Axboe221c5eb2019-01-17 09:41:58 -07003498static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3499 void *key)
3500{
Jens Axboee9444752019-11-26 15:02:04 -07003501 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003502 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3503 struct io_ring_ctx *ctx = req->ctx;
3504 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003505
3506 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003507 if (mask && !(mask & poll->events))
3508 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003509
Jens Axboe392edb42019-12-09 17:52:20 -07003510 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003511
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003512 /*
3513 * Run completion inline if we can. We're using trylock here because
3514 * we are violating the completion_lock -> poll wq lock ordering.
3515 * If we have a link timeout we're going to need the completion_lock
3516 * for finalizing the request, mark us as having grabbed that already.
3517 */
Jens Axboee94f1412019-12-19 12:06:02 -07003518 if (mask) {
3519 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003520
Jens Axboee94f1412019-12-19 12:06:02 -07003521 if (llist_empty(&ctx->poll_llist) &&
3522 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3523 hash_del(&req->hash_node);
3524 io_poll_complete(req, mask, 0);
3525 req->flags |= REQ_F_COMP_LOCKED;
3526 io_put_req(req);
3527 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3528
3529 io_cqring_ev_posted(ctx);
3530 req = NULL;
3531 } else {
3532 req->result = mask;
3533 req->llist_node.next = NULL;
3534 /* if the list wasn't empty, we're done */
3535 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3536 req = NULL;
3537 else
3538 req->work.func = io_poll_flush;
3539 }
Jens Axboe8c838782019-03-12 15:48:16 -06003540 }
Jens Axboee94f1412019-12-19 12:06:02 -07003541 if (req)
3542 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003543
Jens Axboe221c5eb2019-01-17 09:41:58 -07003544 return 1;
3545}
3546
3547struct io_poll_table {
3548 struct poll_table_struct pt;
3549 struct io_kiocb *req;
3550 int error;
3551};
3552
3553static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3554 struct poll_table_struct *p)
3555{
3556 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3557
3558 if (unlikely(pt->req->poll.head)) {
3559 pt->error = -EINVAL;
3560 return;
3561 }
3562
3563 pt->error = 0;
3564 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003565 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003566}
3567
Jens Axboeeac406c2019-11-14 12:09:58 -07003568static void io_poll_req_insert(struct io_kiocb *req)
3569{
3570 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003571 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003572
Jens Axboe78076bb2019-12-04 19:56:40 -07003573 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3574 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003575}
3576
Jens Axboe3529d8c2019-12-19 18:24:38 -07003577static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003578{
3579 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003580 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003581
3582 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3583 return -EINVAL;
3584 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3585 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003586 if (!poll->file)
3587 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003588
Jens Axboe221c5eb2019-01-17 09:41:58 -07003589 events = READ_ONCE(sqe->poll_events);
3590 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003591 return 0;
3592}
3593
3594static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3595{
3596 struct io_poll_iocb *poll = &req->poll;
3597 struct io_ring_ctx *ctx = req->ctx;
3598 struct io_poll_table ipt;
3599 bool cancel = false;
3600 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003601
3602 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003603 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003604
Jens Axboe221c5eb2019-01-17 09:41:58 -07003605 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003606 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003607 poll->canceled = false;
3608
3609 ipt.pt._qproc = io_poll_queue_proc;
3610 ipt.pt._key = poll->events;
3611 ipt.req = req;
3612 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3613
3614 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003615 INIT_LIST_HEAD(&poll->wait.entry);
3616 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3617 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003618
Jens Axboe36703242019-07-25 10:20:18 -06003619 INIT_LIST_HEAD(&req->list);
3620
Jens Axboe221c5eb2019-01-17 09:41:58 -07003621 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003622
3623 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003624 if (likely(poll->head)) {
3625 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003626 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003627 if (ipt.error)
3628 cancel = true;
3629 ipt.error = 0;
3630 mask = 0;
3631 }
3632 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003633 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003634 else if (cancel)
3635 WRITE_ONCE(poll->canceled, true);
3636 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003637 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003638 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003639 }
Jens Axboe8c838782019-03-12 15:48:16 -06003640 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003641 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003642 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003643 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003644 spin_unlock_irq(&ctx->completion_lock);
3645
Jens Axboe8c838782019-03-12 15:48:16 -06003646 if (mask) {
3647 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003648 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003649 }
Jens Axboe8c838782019-03-12 15:48:16 -06003650 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003651}
3652
Jens Axboe5262f562019-09-17 12:26:57 -06003653static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3654{
Jens Axboead8a48a2019-11-15 08:49:11 -07003655 struct io_timeout_data *data = container_of(timer,
3656 struct io_timeout_data, timer);
3657 struct io_kiocb *req = data->req;
3658 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003659 unsigned long flags;
3660
Jens Axboe5262f562019-09-17 12:26:57 -06003661 atomic_inc(&ctx->cq_timeouts);
3662
3663 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003664 /*
Jens Axboe11365042019-10-16 09:08:32 -06003665 * We could be racing with timeout deletion. If the list is empty,
3666 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003667 */
Jens Axboe842f9612019-10-29 12:34:10 -06003668 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003669 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003670
Jens Axboe11365042019-10-16 09:08:32 -06003671 /*
3672 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003673 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003674 * pointer will be increased, otherwise other timeout reqs may
3675 * return in advance without waiting for enough wait_nr.
3676 */
3677 prev = req;
3678 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3679 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003680 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003681 }
Jens Axboe842f9612019-10-29 12:34:10 -06003682
Jens Axboe78e19bb2019-11-06 15:21:34 -07003683 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003684 io_commit_cqring(ctx);
3685 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3686
3687 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003688 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003689 io_put_req(req);
3690 return HRTIMER_NORESTART;
3691}
3692
Jens Axboe47f46762019-11-09 17:43:02 -07003693static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3694{
3695 struct io_kiocb *req;
3696 int ret = -ENOENT;
3697
3698 list_for_each_entry(req, &ctx->timeout_list, list) {
3699 if (user_data == req->user_data) {
3700 list_del_init(&req->list);
3701 ret = 0;
3702 break;
3703 }
3704 }
3705
3706 if (ret == -ENOENT)
3707 return ret;
3708
Jens Axboe2d283902019-12-04 11:08:05 -07003709 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003710 if (ret == -1)
3711 return -EALREADY;
3712
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003713 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003714 io_cqring_fill_event(req, -ECANCELED);
3715 io_put_req(req);
3716 return 0;
3717}
3718
Jens Axboe3529d8c2019-12-19 18:24:38 -07003719static int io_timeout_remove_prep(struct io_kiocb *req,
3720 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003721{
Jens Axboeb29472e2019-12-17 18:50:29 -07003722 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3723 return -EINVAL;
3724 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3725 return -EINVAL;
3726
3727 req->timeout.addr = READ_ONCE(sqe->addr);
3728 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3729 if (req->timeout.flags)
3730 return -EINVAL;
3731
Jens Axboeb29472e2019-12-17 18:50:29 -07003732 return 0;
3733}
3734
Jens Axboe11365042019-10-16 09:08:32 -06003735/*
3736 * Remove or update an existing timeout command
3737 */
Jens Axboefc4df992019-12-10 14:38:45 -07003738static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003739{
3740 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003741 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003742
Jens Axboe11365042019-10-16 09:08:32 -06003743 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003744 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003745
Jens Axboe47f46762019-11-09 17:43:02 -07003746 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003747 io_commit_cqring(ctx);
3748 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003749 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003750 if (ret < 0)
3751 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003752 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003753 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003754}
3755
Jens Axboe3529d8c2019-12-19 18:24:38 -07003756static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003757 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003758{
Jens Axboead8a48a2019-11-15 08:49:11 -07003759 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003760 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003761
Jens Axboead8a48a2019-11-15 08:49:11 -07003762 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003763 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003764 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003765 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003766 if (sqe->off && is_timeout_link)
3767 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003768 flags = READ_ONCE(sqe->timeout_flags);
3769 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003770 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003771
Jens Axboe26a61672019-12-20 09:02:01 -07003772 req->timeout.count = READ_ONCE(sqe->off);
3773
Jens Axboe3529d8c2019-12-19 18:24:38 -07003774 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003775 return -ENOMEM;
3776
3777 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003778 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003779 req->flags |= REQ_F_TIMEOUT;
3780
3781 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003782 return -EFAULT;
3783
Jens Axboe11365042019-10-16 09:08:32 -06003784 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003785 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003786 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003787 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003788
Jens Axboead8a48a2019-11-15 08:49:11 -07003789 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3790 return 0;
3791}
3792
Jens Axboefc4df992019-12-10 14:38:45 -07003793static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003794{
3795 unsigned count;
3796 struct io_ring_ctx *ctx = req->ctx;
3797 struct io_timeout_data *data;
3798 struct list_head *entry;
3799 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003800
Jens Axboe2d283902019-12-04 11:08:05 -07003801 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003802
Jens Axboe5262f562019-09-17 12:26:57 -06003803 /*
3804 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003805 * timeout event to be satisfied. If it isn't set, then this is
3806 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003807 */
Jens Axboe26a61672019-12-20 09:02:01 -07003808 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003809 if (!count) {
3810 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3811 spin_lock_irq(&ctx->completion_lock);
3812 entry = ctx->timeout_list.prev;
3813 goto add;
3814 }
Jens Axboe5262f562019-09-17 12:26:57 -06003815
3816 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003817 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003818
3819 /*
3820 * Insertion sort, ensuring the first entry in the list is always
3821 * the one we need first.
3822 */
Jens Axboe5262f562019-09-17 12:26:57 -06003823 spin_lock_irq(&ctx->completion_lock);
3824 list_for_each_prev(entry, &ctx->timeout_list) {
3825 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003826 unsigned nxt_sq_head;
3827 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003828 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003829
Jens Axboe93bd25b2019-11-11 23:34:31 -07003830 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3831 continue;
3832
yangerkun5da0fb12019-10-15 21:59:29 +08003833 /*
3834 * Since cached_sq_head + count - 1 can overflow, use type long
3835 * long to store it.
3836 */
3837 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003838 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3839 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003840
3841 /*
3842 * cached_sq_head may overflow, and it will never overflow twice
3843 * once there is some timeout req still be valid.
3844 */
3845 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003846 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003847
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003848 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003849 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003850
3851 /*
3852 * Sequence of reqs after the insert one and itself should
3853 * be adjusted because each timeout req consumes a slot.
3854 */
3855 span++;
3856 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003857 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003858 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003859add:
Jens Axboe5262f562019-09-17 12:26:57 -06003860 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003861 data->timer.function = io_timeout_fn;
3862 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003863 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003864 return 0;
3865}
3866
Jens Axboe62755e32019-10-28 21:49:21 -06003867static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003868{
Jens Axboe62755e32019-10-28 21:49:21 -06003869 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003870
Jens Axboe62755e32019-10-28 21:49:21 -06003871 return req->user_data == (unsigned long) data;
3872}
3873
Jens Axboee977d6d2019-11-05 12:39:45 -07003874static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003875{
Jens Axboe62755e32019-10-28 21:49:21 -06003876 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003877 int ret = 0;
3878
Jens Axboe62755e32019-10-28 21:49:21 -06003879 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3880 switch (cancel_ret) {
3881 case IO_WQ_CANCEL_OK:
3882 ret = 0;
3883 break;
3884 case IO_WQ_CANCEL_RUNNING:
3885 ret = -EALREADY;
3886 break;
3887 case IO_WQ_CANCEL_NOTFOUND:
3888 ret = -ENOENT;
3889 break;
3890 }
3891
Jens Axboee977d6d2019-11-05 12:39:45 -07003892 return ret;
3893}
3894
Jens Axboe47f46762019-11-09 17:43:02 -07003895static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3896 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003897 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003898{
3899 unsigned long flags;
3900 int ret;
3901
3902 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3903 if (ret != -ENOENT) {
3904 spin_lock_irqsave(&ctx->completion_lock, flags);
3905 goto done;
3906 }
3907
3908 spin_lock_irqsave(&ctx->completion_lock, flags);
3909 ret = io_timeout_cancel(ctx, sqe_addr);
3910 if (ret != -ENOENT)
3911 goto done;
3912 ret = io_poll_cancel(ctx, sqe_addr);
3913done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003914 if (!ret)
3915 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003916 io_cqring_fill_event(req, ret);
3917 io_commit_cqring(ctx);
3918 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3919 io_cqring_ev_posted(ctx);
3920
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003921 if (ret < 0)
3922 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003923 io_put_req_find_next(req, nxt);
3924}
3925
Jens Axboe3529d8c2019-12-19 18:24:38 -07003926static int io_async_cancel_prep(struct io_kiocb *req,
3927 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003928{
Jens Axboefbf23842019-12-17 18:45:56 -07003929 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003930 return -EINVAL;
3931 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3932 sqe->cancel_flags)
3933 return -EINVAL;
3934
Jens Axboefbf23842019-12-17 18:45:56 -07003935 req->cancel.addr = READ_ONCE(sqe->addr);
3936 return 0;
3937}
3938
3939static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3940{
3941 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003942
3943 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003944 return 0;
3945}
3946
Jens Axboe05f3fb32019-12-09 11:22:50 -07003947static int io_files_update_prep(struct io_kiocb *req,
3948 const struct io_uring_sqe *sqe)
3949{
3950 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3951 return -EINVAL;
3952
3953 req->files_update.offset = READ_ONCE(sqe->off);
3954 req->files_update.nr_args = READ_ONCE(sqe->len);
3955 if (!req->files_update.nr_args)
3956 return -EINVAL;
3957 req->files_update.arg = READ_ONCE(sqe->addr);
3958 return 0;
3959}
3960
3961static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3962{
3963 struct io_ring_ctx *ctx = req->ctx;
3964 struct io_uring_files_update up;
3965 int ret;
3966
3967 if (force_nonblock) {
3968 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3969 return -EAGAIN;
3970 }
3971
3972 up.offset = req->files_update.offset;
3973 up.fds = req->files_update.arg;
3974
3975 mutex_lock(&ctx->uring_lock);
3976 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3977 mutex_unlock(&ctx->uring_lock);
3978
3979 if (ret < 0)
3980 req_set_fail_links(req);
3981 io_cqring_add_event(req, ret);
3982 io_put_req(req);
3983 return 0;
3984}
3985
Jens Axboe3529d8c2019-12-19 18:24:38 -07003986static int io_req_defer_prep(struct io_kiocb *req,
3987 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003988{
Jens Axboee7815732019-12-17 19:45:06 -07003989 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003990
Jens Axboecccf0ee2020-01-27 16:34:48 -07003991 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
3992
Jens Axboed625c6e2019-12-17 19:53:05 -07003993 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003994 case IORING_OP_NOP:
3995 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003996 case IORING_OP_READV:
3997 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003998 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003999 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004000 break;
4001 case IORING_OP_WRITEV:
4002 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004003 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004004 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004005 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004006 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004007 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004008 break;
4009 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004010 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004011 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004012 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004013 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004014 break;
4015 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004016 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004017 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004018 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004019 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004020 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004021 break;
4022 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004023 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004024 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004025 break;
Jens Axboef499a022019-12-02 16:28:46 -07004026 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004027 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004028 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004029 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004030 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004031 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004032 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004033 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004034 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004035 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004036 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004037 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004038 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004039 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004040 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004041 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004042 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004043 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004044 case IORING_OP_FALLOCATE:
4045 ret = io_fallocate_prep(req, sqe);
4046 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004047 case IORING_OP_OPENAT:
4048 ret = io_openat_prep(req, sqe);
4049 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004050 case IORING_OP_CLOSE:
4051 ret = io_close_prep(req, sqe);
4052 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004053 case IORING_OP_FILES_UPDATE:
4054 ret = io_files_update_prep(req, sqe);
4055 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004056 case IORING_OP_STATX:
4057 ret = io_statx_prep(req, sqe);
4058 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004059 case IORING_OP_FADVISE:
4060 ret = io_fadvise_prep(req, sqe);
4061 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004062 case IORING_OP_MADVISE:
4063 ret = io_madvise_prep(req, sqe);
4064 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004065 case IORING_OP_OPENAT2:
4066 ret = io_openat2_prep(req, sqe);
4067 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004068 default:
Jens Axboee7815732019-12-17 19:45:06 -07004069 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4070 req->opcode);
4071 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004072 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004073 }
4074
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004075 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004076}
4077
Jens Axboe3529d8c2019-12-19 18:24:38 -07004078static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004079{
Jackie Liua197f662019-11-08 08:09:12 -07004080 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004081 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004082
Bob Liu9d858b22019-11-13 18:06:25 +08004083 /* Still need defer if there is pending req in defer list. */
4084 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004085 return 0;
4086
Jens Axboe3529d8c2019-12-19 18:24:38 -07004087 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004088 return -EAGAIN;
4089
Jens Axboe3529d8c2019-12-19 18:24:38 -07004090 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004091 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004092 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004093
Jens Axboede0617e2019-04-06 21:51:27 -06004094 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004095 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004096 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004097 return 0;
4098 }
4099
Jens Axboe915967f2019-11-21 09:01:20 -07004100 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004101 list_add_tail(&req->list, &ctx->defer_list);
4102 spin_unlock_irq(&ctx->completion_lock);
4103 return -EIOCBQUEUED;
4104}
4105
Jens Axboe3529d8c2019-12-19 18:24:38 -07004106static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4107 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004108{
Jackie Liua197f662019-11-08 08:09:12 -07004109 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004110 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004111
Jens Axboed625c6e2019-12-17 19:53:05 -07004112 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004113 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004114 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004115 break;
4116 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004117 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004118 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004119 if (sqe) {
4120 ret = io_read_prep(req, sqe, force_nonblock);
4121 if (ret < 0)
4122 break;
4123 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004124 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004125 break;
4126 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004127 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004128 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004129 if (sqe) {
4130 ret = io_write_prep(req, sqe, force_nonblock);
4131 if (ret < 0)
4132 break;
4133 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004134 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004135 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004136 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004137 if (sqe) {
4138 ret = io_prep_fsync(req, sqe);
4139 if (ret < 0)
4140 break;
4141 }
Jens Axboefc4df992019-12-10 14:38:45 -07004142 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004143 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004144 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004145 if (sqe) {
4146 ret = io_poll_add_prep(req, sqe);
4147 if (ret)
4148 break;
4149 }
Jens Axboefc4df992019-12-10 14:38:45 -07004150 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004151 break;
4152 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004153 if (sqe) {
4154 ret = io_poll_remove_prep(req, sqe);
4155 if (ret < 0)
4156 break;
4157 }
Jens Axboefc4df992019-12-10 14:38:45 -07004158 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004159 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004160 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004161 if (sqe) {
4162 ret = io_prep_sfr(req, sqe);
4163 if (ret < 0)
4164 break;
4165 }
Jens Axboefc4df992019-12-10 14:38:45 -07004166 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004167 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004168 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004169 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004170 if (sqe) {
4171 ret = io_sendmsg_prep(req, sqe);
4172 if (ret < 0)
4173 break;
4174 }
Jens Axboefddafac2020-01-04 20:19:44 -07004175 if (req->opcode == IORING_OP_SENDMSG)
4176 ret = io_sendmsg(req, nxt, force_nonblock);
4177 else
4178 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004179 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004180 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004181 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004182 if (sqe) {
4183 ret = io_recvmsg_prep(req, sqe);
4184 if (ret)
4185 break;
4186 }
Jens Axboefddafac2020-01-04 20:19:44 -07004187 if (req->opcode == IORING_OP_RECVMSG)
4188 ret = io_recvmsg(req, nxt, force_nonblock);
4189 else
4190 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004191 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004192 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004193 if (sqe) {
4194 ret = io_timeout_prep(req, sqe, false);
4195 if (ret)
4196 break;
4197 }
Jens Axboefc4df992019-12-10 14:38:45 -07004198 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004199 break;
Jens Axboe11365042019-10-16 09:08:32 -06004200 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004201 if (sqe) {
4202 ret = io_timeout_remove_prep(req, sqe);
4203 if (ret)
4204 break;
4205 }
Jens Axboefc4df992019-12-10 14:38:45 -07004206 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004207 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004208 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004209 if (sqe) {
4210 ret = io_accept_prep(req, sqe);
4211 if (ret)
4212 break;
4213 }
Jens Axboefc4df992019-12-10 14:38:45 -07004214 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004215 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004216 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004217 if (sqe) {
4218 ret = io_connect_prep(req, sqe);
4219 if (ret)
4220 break;
4221 }
Jens Axboefc4df992019-12-10 14:38:45 -07004222 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004223 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004224 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004225 if (sqe) {
4226 ret = io_async_cancel_prep(req, sqe);
4227 if (ret)
4228 break;
4229 }
Jens Axboefc4df992019-12-10 14:38:45 -07004230 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004231 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004232 case IORING_OP_FALLOCATE:
4233 if (sqe) {
4234 ret = io_fallocate_prep(req, sqe);
4235 if (ret)
4236 break;
4237 }
4238 ret = io_fallocate(req, nxt, force_nonblock);
4239 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004240 case IORING_OP_OPENAT:
4241 if (sqe) {
4242 ret = io_openat_prep(req, sqe);
4243 if (ret)
4244 break;
4245 }
4246 ret = io_openat(req, nxt, force_nonblock);
4247 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004248 case IORING_OP_CLOSE:
4249 if (sqe) {
4250 ret = io_close_prep(req, sqe);
4251 if (ret)
4252 break;
4253 }
4254 ret = io_close(req, nxt, force_nonblock);
4255 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004256 case IORING_OP_FILES_UPDATE:
4257 if (sqe) {
4258 ret = io_files_update_prep(req, sqe);
4259 if (ret)
4260 break;
4261 }
4262 ret = io_files_update(req, force_nonblock);
4263 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004264 case IORING_OP_STATX:
4265 if (sqe) {
4266 ret = io_statx_prep(req, sqe);
4267 if (ret)
4268 break;
4269 }
4270 ret = io_statx(req, nxt, force_nonblock);
4271 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004272 case IORING_OP_FADVISE:
4273 if (sqe) {
4274 ret = io_fadvise_prep(req, sqe);
4275 if (ret)
4276 break;
4277 }
4278 ret = io_fadvise(req, nxt, force_nonblock);
4279 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004280 case IORING_OP_MADVISE:
4281 if (sqe) {
4282 ret = io_madvise_prep(req, sqe);
4283 if (ret)
4284 break;
4285 }
4286 ret = io_madvise(req, nxt, force_nonblock);
4287 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004288 case IORING_OP_OPENAT2:
4289 if (sqe) {
4290 ret = io_openat2_prep(req, sqe);
4291 if (ret)
4292 break;
4293 }
4294 ret = io_openat2(req, nxt, force_nonblock);
4295 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004296 default:
4297 ret = -EINVAL;
4298 break;
4299 }
4300
Jens Axboedef596e2019-01-09 08:59:42 -07004301 if (ret)
4302 return ret;
4303
4304 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004305 const bool in_async = io_wq_current_is_worker();
4306
Jens Axboe9e645e112019-05-10 16:07:28 -06004307 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004308 return -EAGAIN;
4309
Jens Axboe11ba8202020-01-15 21:51:17 -07004310 /* workqueue context doesn't hold uring_lock, grab it now */
4311 if (in_async)
4312 mutex_lock(&ctx->uring_lock);
4313
Jens Axboedef596e2019-01-09 08:59:42 -07004314 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004315
4316 if (in_async)
4317 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004318 }
4319
4320 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004321}
4322
Jens Axboe561fb042019-10-24 07:25:42 -06004323static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004324{
Jens Axboe561fb042019-10-24 07:25:42 -06004325 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004326 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004327 struct io_kiocb *nxt = NULL;
4328 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004329
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004330 /* if NO_CANCEL is set, we must still run the work */
4331 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4332 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004333 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004334 }
Jens Axboe31b51512019-01-18 22:56:34 -07004335
Jens Axboe561fb042019-10-24 07:25:42 -06004336 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004337 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4338 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004339 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004340 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004341 /*
4342 * We can get EAGAIN for polled IO even though we're
4343 * forcing a sync submission from here, since we can't
4344 * wait for request slots on the block side.
4345 */
4346 if (ret != -EAGAIN)
4347 break;
4348 cond_resched();
4349 } while (1);
4350 }
Jens Axboe31b51512019-01-18 22:56:34 -07004351
Jens Axboe561fb042019-10-24 07:25:42 -06004352 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004353 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004354
Jens Axboe561fb042019-10-24 07:25:42 -06004355 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004356 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004357 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004358 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004359 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004360
Jens Axboe561fb042019-10-24 07:25:42 -06004361 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004362 if (!ret && nxt)
4363 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004364}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004365
Jens Axboe15b71ab2019-12-11 11:20:36 -07004366static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004367{
Jens Axboed3656342019-12-18 09:50:26 -07004368 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004369 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004370 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4371 return 0;
4372 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004373}
4374
Jens Axboe65e19f52019-10-26 07:20:21 -06004375static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4376 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004377{
Jens Axboe65e19f52019-10-26 07:20:21 -06004378 struct fixed_file_table *table;
4379
Jens Axboe05f3fb32019-12-09 11:22:50 -07004380 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4381 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004382}
4383
Jens Axboe3529d8c2019-12-19 18:24:38 -07004384static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4385 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004386{
Jackie Liua197f662019-11-08 08:09:12 -07004387 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004388 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004389 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004390
Jens Axboe3529d8c2019-12-19 18:24:38 -07004391 flags = READ_ONCE(sqe->flags);
4392 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004393
Jens Axboed3656342019-12-18 09:50:26 -07004394 if (!io_req_needs_file(req, fd))
4395 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004396
4397 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004398 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004399 (unsigned) fd >= ctx->nr_user_files))
4400 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004401 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004402 req->file = io_file_from_index(ctx, fd);
4403 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004404 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004405 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004406 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004407 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004408 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004409 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004410 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004411 req->file = io_file_get(state, fd);
4412 if (unlikely(!req->file))
4413 return -EBADF;
4414 }
4415
4416 return 0;
4417}
4418
Jackie Liua197f662019-11-08 08:09:12 -07004419static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004420{
Jens Axboefcb323c2019-10-24 12:39:47 -06004421 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004422 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004423
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004424 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004425 return -EBADF;
4426
Jens Axboefcb323c2019-10-24 12:39:47 -06004427 rcu_read_lock();
4428 spin_lock_irq(&ctx->inflight_lock);
4429 /*
4430 * We use the f_ops->flush() handler to ensure that we can flush
4431 * out work accessing these files if the fd is closed. Check if
4432 * the fd has changed since we started down this path, and disallow
4433 * this operation if it has.
4434 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004435 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004436 list_add(&req->inflight_entry, &ctx->inflight_list);
4437 req->flags |= REQ_F_INFLIGHT;
4438 req->work.files = current->files;
4439 ret = 0;
4440 }
4441 spin_unlock_irq(&ctx->inflight_lock);
4442 rcu_read_unlock();
4443
4444 return ret;
4445}
4446
Jens Axboe2665abf2019-11-05 12:40:47 -07004447static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4448{
Jens Axboead8a48a2019-11-15 08:49:11 -07004449 struct io_timeout_data *data = container_of(timer,
4450 struct io_timeout_data, timer);
4451 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004452 struct io_ring_ctx *ctx = req->ctx;
4453 struct io_kiocb *prev = NULL;
4454 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004455
4456 spin_lock_irqsave(&ctx->completion_lock, flags);
4457
4458 /*
4459 * We don't expect the list to be empty, that will only happen if we
4460 * race with the completion of the linked work.
4461 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004462 if (!list_empty(&req->link_list)) {
4463 prev = list_entry(req->link_list.prev, struct io_kiocb,
4464 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004465 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004466 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004467 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4468 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004469 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004470 }
4471
4472 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4473
4474 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004475 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004476 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4477 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004478 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004479 } else {
4480 io_cqring_add_event(req, -ETIME);
4481 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004482 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004483 return HRTIMER_NORESTART;
4484}
4485
Jens Axboead8a48a2019-11-15 08:49:11 -07004486static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004487{
Jens Axboe76a46e02019-11-10 23:34:16 -07004488 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004489
Jens Axboe76a46e02019-11-10 23:34:16 -07004490 /*
4491 * If the list is now empty, then our linked request finished before
4492 * we got a chance to setup the timer
4493 */
4494 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004495 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004496 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004497
Jens Axboead8a48a2019-11-15 08:49:11 -07004498 data->timer.function = io_link_timeout_fn;
4499 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4500 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004501 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004502 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004503
Jens Axboe2665abf2019-11-05 12:40:47 -07004504 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004505 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004506}
4507
Jens Axboead8a48a2019-11-15 08:49:11 -07004508static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004509{
4510 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004511
Jens Axboe2665abf2019-11-05 12:40:47 -07004512 if (!(req->flags & REQ_F_LINK))
4513 return NULL;
4514
Pavel Begunkov44932332019-12-05 16:16:35 +03004515 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4516 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004517 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004518 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004519
Jens Axboe76a46e02019-11-10 23:34:16 -07004520 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004521 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004522}
4523
Jens Axboe3529d8c2019-12-19 18:24:38 -07004524static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004525{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004526 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004527 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004528 int ret;
4529
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004530again:
4531 linked_timeout = io_prep_linked_timeout(req);
4532
Jens Axboe3529d8c2019-12-19 18:24:38 -07004533 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004534
4535 /*
4536 * We async punt it if the file wasn't marked NOWAIT, or if the file
4537 * doesn't support non-blocking read/write attempts
4538 */
4539 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4540 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004541punt:
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004542 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4543 ret = io_grab_files(req);
4544 if (ret)
4545 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004546 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004547
4548 /*
4549 * Queued up for async execution, worker will release
4550 * submit reference when the iocb is actually submitted.
4551 */
4552 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004553 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004554 }
Jens Axboee65ef562019-03-12 10:16:44 -06004555
Jens Axboefcb323c2019-10-24 12:39:47 -06004556err:
Jens Axboee65ef562019-03-12 10:16:44 -06004557 /* drop submission reference */
4558 io_put_req(req);
4559
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004560 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004561 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004562 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004563 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004564 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004565 }
4566
Jens Axboee65ef562019-03-12 10:16:44 -06004567 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004568 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004569 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004570 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004571 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004572 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004573done_req:
4574 if (nxt) {
4575 req = nxt;
4576 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004577
4578 if (req->flags & REQ_F_FORCE_ASYNC)
4579 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004580 goto again;
4581 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004582}
4583
Jens Axboe3529d8c2019-12-19 18:24:38 -07004584static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004585{
4586 int ret;
4587
Jens Axboe3529d8c2019-12-19 18:24:38 -07004588 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004589 if (ret) {
4590 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004591fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004592 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004593 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004594 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004595 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004596 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004597 ret = io_req_defer_prep(req, sqe);
4598 if (unlikely(ret < 0))
4599 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004600 /*
4601 * Never try inline submit of IOSQE_ASYNC is set, go straight
4602 * to async execution.
4603 */
4604 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4605 io_queue_async_work(req);
4606 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004607 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004608 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004609}
4610
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004611static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004612{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004613 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004614 io_cqring_add_event(req, -ECANCELED);
4615 io_double_put_req(req);
4616 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004617 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004618}
4619
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004620#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004621 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004622
Jens Axboe3529d8c2019-12-19 18:24:38 -07004623static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4624 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004625{
Jackie Liua197f662019-11-08 08:09:12 -07004626 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004627 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004628 int ret;
4629
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004630 sqe_flags = READ_ONCE(sqe->flags);
4631
Jens Axboe9e645e112019-05-10 16:07:28 -06004632 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004633 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004634 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004635 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004636 }
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004637 /* same numerical values with corresponding REQ_F_*, safe to copy */
4638 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4639 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004640
Jens Axboe3529d8c2019-12-19 18:24:38 -07004641 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004642 if (unlikely(ret)) {
4643err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004644 io_cqring_add_event(req, ret);
4645 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004646 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004647 }
4648
Jens Axboe9e645e112019-05-10 16:07:28 -06004649 /*
4650 * If we already have a head request, queue this one for async
4651 * submittal once the head completes. If we don't have a head but
4652 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4653 * submitted sync once the chain is complete. If none of those
4654 * conditions are true (normal request), then just queue it.
4655 */
4656 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004657 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004658
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004659 /*
4660 * Taking sequential execution of a link, draining both sides
4661 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4662 * requests in the link. So, it drains the head and the
4663 * next after the link request. The last one is done via
4664 * drain_next flag to persist the effect across calls.
4665 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004666 if (sqe_flags & IOSQE_IO_DRAIN) {
4667 head->flags |= REQ_F_IO_DRAIN;
4668 ctx->drain_next = 1;
4669 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004670 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004671 ret = -EAGAIN;
4672 goto err_req;
4673 }
4674
Jens Axboe3529d8c2019-12-19 18:24:38 -07004675 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004676 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004677 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004678 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004679 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004680 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004681 trace_io_uring_link(ctx, req, head);
4682 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004683
4684 /* last request of a link, enqueue the link */
4685 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4686 io_queue_link_head(head);
4687 *link = NULL;
4688 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004689 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004690 if (unlikely(ctx->drain_next)) {
4691 req->flags |= REQ_F_IO_DRAIN;
4692 req->ctx->drain_next = 0;
4693 }
4694 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4695 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004696 INIT_LIST_HEAD(&req->link_list);
4697 ret = io_req_defer_prep(req, sqe);
4698 if (ret)
4699 req->flags |= REQ_F_FAIL_LINK;
4700 *link = req;
4701 } else {
4702 io_queue_sqe(req, sqe);
4703 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004704 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004705
4706 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004707}
4708
Jens Axboe9a56a232019-01-09 09:06:50 -07004709/*
4710 * Batched submission is done, ensure local IO is flushed out.
4711 */
4712static void io_submit_state_end(struct io_submit_state *state)
4713{
4714 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004715 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004716 if (state->free_reqs)
4717 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4718 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004719}
4720
4721/*
4722 * Start submission side cache.
4723 */
4724static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004725 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004726{
4727 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004728 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004729 state->file = NULL;
4730 state->ios_left = max_ios;
4731}
4732
Jens Axboe2b188cc2019-01-07 10:46:33 -07004733static void io_commit_sqring(struct io_ring_ctx *ctx)
4734{
Hristo Venev75b28af2019-08-26 17:23:46 +00004735 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004736
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004737 /*
4738 * Ensure any loads from the SQEs are done at this point,
4739 * since once we write the new head, the application could
4740 * write new data to them.
4741 */
4742 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004743}
4744
4745/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004746 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004747 * that is mapped by userspace. This means that care needs to be taken to
4748 * ensure that reads are stable, as we cannot rely on userspace always
4749 * being a good citizen. If members of the sqe are validated and then later
4750 * used, it's important that those reads are done through READ_ONCE() to
4751 * prevent a re-load down the line.
4752 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004753static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4754 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004755{
Hristo Venev75b28af2019-08-26 17:23:46 +00004756 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004757 unsigned head;
4758
4759 /*
4760 * The cached sq head (or cq tail) serves two purposes:
4761 *
4762 * 1) allows us to batch the cost of updating the user visible
4763 * head updates.
4764 * 2) allows the kernel side to track the head on its own, even
4765 * though the application is the one updating it.
4766 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004767 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004768 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004769 /*
4770 * All io need record the previous position, if LINK vs DARIN,
4771 * it can be used to mark the position of the first IO in the
4772 * link list.
4773 */
4774 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004775 *sqe_ptr = &ctx->sq_sqes[head];
4776 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4777 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004778 ctx->cached_sq_head++;
4779 return true;
4780 }
4781
4782 /* drop invalid entries */
4783 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004784 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004785 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004786 return false;
4787}
4788
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004789static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004790 struct file *ring_file, int ring_fd,
4791 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004792{
4793 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004794 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004795 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004796 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004797
Jens Axboec4a2ed72019-11-21 21:01:26 -07004798 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004799 if (test_bit(0, &ctx->sq_check_overflow)) {
4800 if (!list_empty(&ctx->cq_overflow_list) &&
4801 !io_cqring_overflow_flush(ctx, false))
4802 return -EBUSY;
4803 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004804
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004805 /* make sure SQ entry isn't read before tail */
4806 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004807
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004808 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4809 return -EAGAIN;
4810
Jens Axboe6c271ce2019-01-10 11:22:30 -07004811 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004812 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004813 statep = &state;
4814 }
4815
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004816 ctx->ring_fd = ring_fd;
4817 ctx->ring_file = ring_file;
4818
Jens Axboe6c271ce2019-01-10 11:22:30 -07004819 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004820 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004821 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004822
Pavel Begunkov196be952019-11-07 01:41:06 +03004823 req = io_get_req(ctx, statep);
4824 if (unlikely(!req)) {
4825 if (!submitted)
4826 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004827 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004828 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004829 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004830 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004831 break;
4832 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004833
Jens Axboed3656342019-12-18 09:50:26 -07004834 /* will complete beyond this point, count as submitted */
4835 submitted++;
4836
4837 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4838 io_cqring_add_event(req, -EINVAL);
4839 io_double_put_req(req);
4840 break;
4841 }
4842
4843 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004844 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4845 if (!mm_fault) {
4846 use_mm(ctx->sqo_mm);
4847 *mm = ctx->sqo_mm;
4848 }
4849 }
4850
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004851 req->has_user = *mm != NULL;
4852 req->in_async = async;
4853 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07004854 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
4855 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004856 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004857 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004858 }
4859
Pavel Begunkov9466f432020-01-25 22:34:01 +03004860 if (unlikely(submitted != nr)) {
4861 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
4862
4863 percpu_ref_put_many(&ctx->refs, nr - ref_used);
4864 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004865 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004866 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004867 if (statep)
4868 io_submit_state_end(&state);
4869
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004870 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4871 io_commit_sqring(ctx);
4872
Jens Axboe6c271ce2019-01-10 11:22:30 -07004873 return submitted;
4874}
4875
4876static int io_sq_thread(void *data)
4877{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004878 struct io_ring_ctx *ctx = data;
4879 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004880 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004881 mm_segment_t old_fs;
4882 DEFINE_WAIT(wait);
4883 unsigned inflight;
4884 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004885 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004886
Jens Axboe206aefd2019-11-07 18:27:42 -07004887 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004888
Jens Axboe6c271ce2019-01-10 11:22:30 -07004889 old_fs = get_fs();
4890 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004891 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004892
Jens Axboec1edbf52019-11-10 16:56:04 -07004893 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004894 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004895 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004896
4897 if (inflight) {
4898 unsigned nr_events = 0;
4899
4900 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004901 /*
4902 * inflight is the count of the maximum possible
4903 * entries we submitted, but it can be smaller
4904 * if we dropped some of them. If we don't have
4905 * poll entries available, then we know that we
4906 * have nothing left to poll for. Reset the
4907 * inflight count to zero in that case.
4908 */
4909 mutex_lock(&ctx->uring_lock);
4910 if (!list_empty(&ctx->poll_list))
4911 __io_iopoll_check(ctx, &nr_events, 0);
4912 else
4913 inflight = 0;
4914 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004915 } else {
4916 /*
4917 * Normal IO, just pretend everything completed.
4918 * We don't have to poll completions for that.
4919 */
4920 nr_events = inflight;
4921 }
4922
4923 inflight -= nr_events;
4924 if (!inflight)
4925 timeout = jiffies + ctx->sq_thread_idle;
4926 }
4927
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004928 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004929
4930 /*
4931 * If submit got -EBUSY, flag us as needing the application
4932 * to enter the kernel to reap and flush events.
4933 */
4934 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004935 /*
4936 * We're polling. If we're within the defined idle
4937 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004938 * to sleep. The exception is if we got EBUSY doing
4939 * more IO, we should wait for the application to
4940 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004941 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004942 if (inflight ||
4943 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004944 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004945 continue;
4946 }
4947
4948 /*
4949 * Drop cur_mm before scheduling, we can't hold it for
4950 * long periods (or over schedule()). Do this before
4951 * adding ourselves to the waitqueue, as the unuse/drop
4952 * may sleep.
4953 */
4954 if (cur_mm) {
4955 unuse_mm(cur_mm);
4956 mmput(cur_mm);
4957 cur_mm = NULL;
4958 }
4959
4960 prepare_to_wait(&ctx->sqo_wait, &wait,
4961 TASK_INTERRUPTIBLE);
4962
4963 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004964 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004965 /* make sure to read SQ tail after writing flags */
4966 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004967
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004968 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004969 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004970 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004971 finish_wait(&ctx->sqo_wait, &wait);
4972 break;
4973 }
4974 if (signal_pending(current))
4975 flush_signals(current);
4976 schedule();
4977 finish_wait(&ctx->sqo_wait, &wait);
4978
Hristo Venev75b28af2019-08-26 17:23:46 +00004979 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004980 continue;
4981 }
4982 finish_wait(&ctx->sqo_wait, &wait);
4983
Hristo Venev75b28af2019-08-26 17:23:46 +00004984 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004985 }
4986
Jens Axboe8a4955f2019-12-09 14:52:35 -07004987 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004988 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004989 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004990 if (ret > 0)
4991 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004992 }
4993
4994 set_fs(old_fs);
4995 if (cur_mm) {
4996 unuse_mm(cur_mm);
4997 mmput(cur_mm);
4998 }
Jens Axboe181e4482019-11-25 08:52:30 -07004999 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005000
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005001 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005002
Jens Axboe6c271ce2019-01-10 11:22:30 -07005003 return 0;
5004}
5005
Jens Axboebda52162019-09-24 13:47:15 -06005006struct io_wait_queue {
5007 struct wait_queue_entry wq;
5008 struct io_ring_ctx *ctx;
5009 unsigned to_wait;
5010 unsigned nr_timeouts;
5011};
5012
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005013static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005014{
5015 struct io_ring_ctx *ctx = iowq->ctx;
5016
5017 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005018 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005019 * started waiting. For timeouts, we always want to return to userspace,
5020 * regardless of event count.
5021 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005022 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005023 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5024}
5025
5026static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5027 int wake_flags, void *key)
5028{
5029 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5030 wq);
5031
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005032 /* use noflush == true, as we can't safely rely on locking context */
5033 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005034 return -1;
5035
5036 return autoremove_wake_function(curr, mode, wake_flags, key);
5037}
5038
Jens Axboe2b188cc2019-01-07 10:46:33 -07005039/*
5040 * Wait until events become available, if we don't already have some. The
5041 * application must reap them itself, as they reside on the shared cq ring.
5042 */
5043static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5044 const sigset_t __user *sig, size_t sigsz)
5045{
Jens Axboebda52162019-09-24 13:47:15 -06005046 struct io_wait_queue iowq = {
5047 .wq = {
5048 .private = current,
5049 .func = io_wake_function,
5050 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5051 },
5052 .ctx = ctx,
5053 .to_wait = min_events,
5054 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005055 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005056 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005057
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005058 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005059 return 0;
5060
5061 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005062#ifdef CONFIG_COMPAT
5063 if (in_compat_syscall())
5064 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005065 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005066 else
5067#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005068 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005069
Jens Axboe2b188cc2019-01-07 10:46:33 -07005070 if (ret)
5071 return ret;
5072 }
5073
Jens Axboebda52162019-09-24 13:47:15 -06005074 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005075 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005076 do {
5077 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5078 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005079 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005080 break;
5081 schedule();
5082 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005083 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005084 break;
5085 }
5086 } while (1);
5087 finish_wait(&ctx->wait, &iowq.wq);
5088
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005089 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005090
Hristo Venev75b28af2019-08-26 17:23:46 +00005091 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005092}
5093
Jens Axboe6b063142019-01-10 22:13:58 -07005094static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5095{
5096#if defined(CONFIG_UNIX)
5097 if (ctx->ring_sock) {
5098 struct sock *sock = ctx->ring_sock->sk;
5099 struct sk_buff *skb;
5100
5101 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5102 kfree_skb(skb);
5103 }
5104#else
5105 int i;
5106
Jens Axboe65e19f52019-10-26 07:20:21 -06005107 for (i = 0; i < ctx->nr_user_files; i++) {
5108 struct file *file;
5109
5110 file = io_file_from_index(ctx, i);
5111 if (file)
5112 fput(file);
5113 }
Jens Axboe6b063142019-01-10 22:13:58 -07005114#endif
5115}
5116
Jens Axboe05f3fb32019-12-09 11:22:50 -07005117static void io_file_ref_kill(struct percpu_ref *ref)
5118{
5119 struct fixed_file_data *data;
5120
5121 data = container_of(ref, struct fixed_file_data, refs);
5122 complete(&data->done);
5123}
5124
Jens Axboe6b063142019-01-10 22:13:58 -07005125static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5126{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005127 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005128 unsigned nr_tables, i;
5129
Jens Axboe05f3fb32019-12-09 11:22:50 -07005130 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005131 return -ENXIO;
5132
Jens Axboe05f3fb32019-12-09 11:22:50 -07005133 /* protect against inflight atomic switch, which drops the ref */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005134 percpu_ref_get(&data->refs);
Jens Axboee46a7952020-01-17 11:15:34 -07005135 /* wait for existing switches */
5136 flush_work(&data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005137 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5138 wait_for_completion(&data->done);
5139 percpu_ref_put(&data->refs);
Jens Axboee46a7952020-01-17 11:15:34 -07005140 /* flush potential new switch */
5141 flush_work(&data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005142 percpu_ref_exit(&data->refs);
5143
Jens Axboe6b063142019-01-10 22:13:58 -07005144 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005145 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5146 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005147 kfree(data->table[i].files);
5148 kfree(data->table);
5149 kfree(data);
5150 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005151 ctx->nr_user_files = 0;
5152 return 0;
5153}
5154
Jens Axboe6c271ce2019-01-10 11:22:30 -07005155static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5156{
5157 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005158 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005159 /*
5160 * The park is a bit of a work-around, without it we get
5161 * warning spews on shutdown with SQPOLL set and affinity
5162 * set to a single CPU.
5163 */
Jens Axboe06058632019-04-13 09:26:03 -06005164 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005165 kthread_stop(ctx->sqo_thread);
5166 ctx->sqo_thread = NULL;
5167 }
5168}
5169
Jens Axboe6b063142019-01-10 22:13:58 -07005170static void io_finish_async(struct io_ring_ctx *ctx)
5171{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005172 io_sq_thread_stop(ctx);
5173
Jens Axboe561fb042019-10-24 07:25:42 -06005174 if (ctx->io_wq) {
5175 io_wq_destroy(ctx->io_wq);
5176 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005177 }
5178}
5179
5180#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005181/*
5182 * Ensure the UNIX gc is aware of our file set, so we are certain that
5183 * the io_uring can be safely unregistered on process exit, even if we have
5184 * loops in the file referencing.
5185 */
5186static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5187{
5188 struct sock *sk = ctx->ring_sock->sk;
5189 struct scm_fp_list *fpl;
5190 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005191 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005192
5193 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5194 unsigned long inflight = ctx->user->unix_inflight + nr;
5195
5196 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5197 return -EMFILE;
5198 }
5199
5200 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5201 if (!fpl)
5202 return -ENOMEM;
5203
5204 skb = alloc_skb(0, GFP_KERNEL);
5205 if (!skb) {
5206 kfree(fpl);
5207 return -ENOMEM;
5208 }
5209
5210 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005211
Jens Axboe08a45172019-10-03 08:11:03 -06005212 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005213 fpl->user = get_uid(ctx->user);
5214 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005215 struct file *file = io_file_from_index(ctx, i + offset);
5216
5217 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005218 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005219 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005220 unix_inflight(fpl->user, fpl->fp[nr_files]);
5221 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005222 }
5223
Jens Axboe08a45172019-10-03 08:11:03 -06005224 if (nr_files) {
5225 fpl->max = SCM_MAX_FD;
5226 fpl->count = nr_files;
5227 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005228 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005229 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5230 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005231
Jens Axboe08a45172019-10-03 08:11:03 -06005232 for (i = 0; i < nr_files; i++)
5233 fput(fpl->fp[i]);
5234 } else {
5235 kfree_skb(skb);
5236 kfree(fpl);
5237 }
Jens Axboe6b063142019-01-10 22:13:58 -07005238
5239 return 0;
5240}
5241
5242/*
5243 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5244 * causes regular reference counting to break down. We rely on the UNIX
5245 * garbage collection to take care of this problem for us.
5246 */
5247static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5248{
5249 unsigned left, total;
5250 int ret = 0;
5251
5252 total = 0;
5253 left = ctx->nr_user_files;
5254 while (left) {
5255 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005256
5257 ret = __io_sqe_files_scm(ctx, this_files, total);
5258 if (ret)
5259 break;
5260 left -= this_files;
5261 total += this_files;
5262 }
5263
5264 if (!ret)
5265 return 0;
5266
5267 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005268 struct file *file = io_file_from_index(ctx, total);
5269
5270 if (file)
5271 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005272 total++;
5273 }
5274
5275 return ret;
5276}
5277#else
5278static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5279{
5280 return 0;
5281}
5282#endif
5283
Jens Axboe65e19f52019-10-26 07:20:21 -06005284static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5285 unsigned nr_files)
5286{
5287 int i;
5288
5289 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005290 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005291 unsigned this_files;
5292
5293 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5294 table->files = kcalloc(this_files, sizeof(struct file *),
5295 GFP_KERNEL);
5296 if (!table->files)
5297 break;
5298 nr_files -= this_files;
5299 }
5300
5301 if (i == nr_tables)
5302 return 0;
5303
5304 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005305 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005306 kfree(table->files);
5307 }
5308 return 1;
5309}
5310
Jens Axboe05f3fb32019-12-09 11:22:50 -07005311static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005312{
5313#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005314 struct sock *sock = ctx->ring_sock->sk;
5315 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5316 struct sk_buff *skb;
5317 int i;
5318
5319 __skb_queue_head_init(&list);
5320
5321 /*
5322 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5323 * remove this entry and rearrange the file array.
5324 */
5325 skb = skb_dequeue(head);
5326 while (skb) {
5327 struct scm_fp_list *fp;
5328
5329 fp = UNIXCB(skb).fp;
5330 for (i = 0; i < fp->count; i++) {
5331 int left;
5332
5333 if (fp->fp[i] != file)
5334 continue;
5335
5336 unix_notinflight(fp->user, fp->fp[i]);
5337 left = fp->count - 1 - i;
5338 if (left) {
5339 memmove(&fp->fp[i], &fp->fp[i + 1],
5340 left * sizeof(struct file *));
5341 }
5342 fp->count--;
5343 if (!fp->count) {
5344 kfree_skb(skb);
5345 skb = NULL;
5346 } else {
5347 __skb_queue_tail(&list, skb);
5348 }
5349 fput(file);
5350 file = NULL;
5351 break;
5352 }
5353
5354 if (!file)
5355 break;
5356
5357 __skb_queue_tail(&list, skb);
5358
5359 skb = skb_dequeue(head);
5360 }
5361
5362 if (skb_peek(&list)) {
5363 spin_lock_irq(&head->lock);
5364 while ((skb = __skb_dequeue(&list)) != NULL)
5365 __skb_queue_tail(head, skb);
5366 spin_unlock_irq(&head->lock);
5367 }
5368#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005369 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005370#endif
5371}
5372
Jens Axboe05f3fb32019-12-09 11:22:50 -07005373struct io_file_put {
5374 struct llist_node llist;
5375 struct file *file;
5376 struct completion *done;
5377};
5378
5379static void io_ring_file_ref_switch(struct work_struct *work)
5380{
5381 struct io_file_put *pfile, *tmp;
5382 struct fixed_file_data *data;
5383 struct llist_node *node;
5384
5385 data = container_of(work, struct fixed_file_data, ref_work);
5386
5387 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5388 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5389 io_ring_file_put(data->ctx, pfile->file);
5390 if (pfile->done)
5391 complete(pfile->done);
5392 else
5393 kfree(pfile);
5394 }
5395 }
5396
5397 percpu_ref_get(&data->refs);
5398 percpu_ref_switch_to_percpu(&data->refs);
5399}
5400
5401static void io_file_data_ref_zero(struct percpu_ref *ref)
5402{
5403 struct fixed_file_data *data;
5404
5405 data = container_of(ref, struct fixed_file_data, refs);
5406
5407 /* we can't safely switch from inside this context, punt to wq */
5408 queue_work(system_wq, &data->ref_work);
5409}
5410
5411static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5412 unsigned nr_args)
5413{
5414 __s32 __user *fds = (__s32 __user *) arg;
5415 unsigned nr_tables;
5416 struct file *file;
5417 int fd, ret = 0;
5418 unsigned i;
5419
5420 if (ctx->file_data)
5421 return -EBUSY;
5422 if (!nr_args)
5423 return -EINVAL;
5424 if (nr_args > IORING_MAX_FIXED_FILES)
5425 return -EMFILE;
5426
5427 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5428 if (!ctx->file_data)
5429 return -ENOMEM;
5430 ctx->file_data->ctx = ctx;
5431 init_completion(&ctx->file_data->done);
5432
5433 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5434 ctx->file_data->table = kcalloc(nr_tables,
5435 sizeof(struct fixed_file_table),
5436 GFP_KERNEL);
5437 if (!ctx->file_data->table) {
5438 kfree(ctx->file_data);
5439 ctx->file_data = NULL;
5440 return -ENOMEM;
5441 }
5442
5443 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5444 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5445 kfree(ctx->file_data->table);
5446 kfree(ctx->file_data);
5447 ctx->file_data = NULL;
5448 return -ENOMEM;
5449 }
5450 ctx->file_data->put_llist.first = NULL;
5451 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5452
5453 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5454 percpu_ref_exit(&ctx->file_data->refs);
5455 kfree(ctx->file_data->table);
5456 kfree(ctx->file_data);
5457 ctx->file_data = NULL;
5458 return -ENOMEM;
5459 }
5460
5461 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5462 struct fixed_file_table *table;
5463 unsigned index;
5464
5465 ret = -EFAULT;
5466 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5467 break;
5468 /* allow sparse sets */
5469 if (fd == -1) {
5470 ret = 0;
5471 continue;
5472 }
5473
5474 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5475 index = i & IORING_FILE_TABLE_MASK;
5476 file = fget(fd);
5477
5478 ret = -EBADF;
5479 if (!file)
5480 break;
5481
5482 /*
5483 * Don't allow io_uring instances to be registered. If UNIX
5484 * isn't enabled, then this causes a reference cycle and this
5485 * instance can never get freed. If UNIX is enabled we'll
5486 * handle it just fine, but there's still no point in allowing
5487 * a ring fd as it doesn't support regular read/write anyway.
5488 */
5489 if (file->f_op == &io_uring_fops) {
5490 fput(file);
5491 break;
5492 }
5493 ret = 0;
5494 table->files[index] = file;
5495 }
5496
5497 if (ret) {
5498 for (i = 0; i < ctx->nr_user_files; i++) {
5499 file = io_file_from_index(ctx, i);
5500 if (file)
5501 fput(file);
5502 }
5503 for (i = 0; i < nr_tables; i++)
5504 kfree(ctx->file_data->table[i].files);
5505
5506 kfree(ctx->file_data->table);
5507 kfree(ctx->file_data);
5508 ctx->file_data = NULL;
5509 ctx->nr_user_files = 0;
5510 return ret;
5511 }
5512
5513 ret = io_sqe_files_scm(ctx);
5514 if (ret)
5515 io_sqe_files_unregister(ctx);
5516
5517 return ret;
5518}
5519
Jens Axboec3a31e62019-10-03 13:59:56 -06005520static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5521 int index)
5522{
5523#if defined(CONFIG_UNIX)
5524 struct sock *sock = ctx->ring_sock->sk;
5525 struct sk_buff_head *head = &sock->sk_receive_queue;
5526 struct sk_buff *skb;
5527
5528 /*
5529 * See if we can merge this file into an existing skb SCM_RIGHTS
5530 * file set. If there's no room, fall back to allocating a new skb
5531 * and filling it in.
5532 */
5533 spin_lock_irq(&head->lock);
5534 skb = skb_peek(head);
5535 if (skb) {
5536 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5537
5538 if (fpl->count < SCM_MAX_FD) {
5539 __skb_unlink(skb, head);
5540 spin_unlock_irq(&head->lock);
5541 fpl->fp[fpl->count] = get_file(file);
5542 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5543 fpl->count++;
5544 spin_lock_irq(&head->lock);
5545 __skb_queue_head(head, skb);
5546 } else {
5547 skb = NULL;
5548 }
5549 }
5550 spin_unlock_irq(&head->lock);
5551
5552 if (skb) {
5553 fput(file);
5554 return 0;
5555 }
5556
5557 return __io_sqe_files_scm(ctx, 1, index);
5558#else
5559 return 0;
5560#endif
5561}
5562
Jens Axboe05f3fb32019-12-09 11:22:50 -07005563static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005564{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005565 struct fixed_file_data *data;
5566
5567 data = container_of(ref, struct fixed_file_data, refs);
5568 clear_bit(FFD_F_ATOMIC, &data->state);
5569}
5570
5571static bool io_queue_file_removal(struct fixed_file_data *data,
5572 struct file *file)
5573{
5574 struct io_file_put *pfile, pfile_stack;
5575 DECLARE_COMPLETION_ONSTACK(done);
5576
5577 /*
5578 * If we fail allocating the struct we need for doing async reomval
5579 * of this file, just punt to sync and wait for it.
5580 */
5581 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5582 if (!pfile) {
5583 pfile = &pfile_stack;
5584 pfile->done = &done;
5585 }
5586
5587 pfile->file = file;
5588 llist_add(&pfile->llist, &data->put_llist);
5589
5590 if (pfile == &pfile_stack) {
5591 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5592 percpu_ref_put(&data->refs);
5593 percpu_ref_switch_to_atomic(&data->refs,
5594 io_atomic_switch);
5595 }
5596 wait_for_completion(&done);
5597 flush_work(&data->ref_work);
5598 return false;
5599 }
5600
5601 return true;
5602}
5603
5604static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5605 struct io_uring_files_update *up,
5606 unsigned nr_args)
5607{
5608 struct fixed_file_data *data = ctx->file_data;
5609 bool ref_switch = false;
5610 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005611 __s32 __user *fds;
5612 int fd, i, err;
5613 __u32 done;
5614
Jens Axboe05f3fb32019-12-09 11:22:50 -07005615 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005616 return -EOVERFLOW;
5617 if (done > ctx->nr_user_files)
5618 return -EINVAL;
5619
5620 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005621 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005622 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005623 struct fixed_file_table *table;
5624 unsigned index;
5625
Jens Axboec3a31e62019-10-03 13:59:56 -06005626 err = 0;
5627 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5628 err = -EFAULT;
5629 break;
5630 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005631 i = array_index_nospec(up->offset, ctx->nr_user_files);
5632 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005633 index = i & IORING_FILE_TABLE_MASK;
5634 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005635 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005636 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005637 if (io_queue_file_removal(data, file))
5638 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005639 }
5640 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005641 file = fget(fd);
5642 if (!file) {
5643 err = -EBADF;
5644 break;
5645 }
5646 /*
5647 * Don't allow io_uring instances to be registered. If
5648 * UNIX isn't enabled, then this causes a reference
5649 * cycle and this instance can never get freed. If UNIX
5650 * is enabled we'll handle it just fine, but there's
5651 * still no point in allowing a ring fd as it doesn't
5652 * support regular read/write anyway.
5653 */
5654 if (file->f_op == &io_uring_fops) {
5655 fput(file);
5656 err = -EBADF;
5657 break;
5658 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005659 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005660 err = io_sqe_file_register(ctx, file, i);
5661 if (err)
5662 break;
5663 }
5664 nr_args--;
5665 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005666 up->offset++;
5667 }
5668
5669 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5670 percpu_ref_put(&data->refs);
5671 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005672 }
5673
5674 return done ? done : err;
5675}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005676static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5677 unsigned nr_args)
5678{
5679 struct io_uring_files_update up;
5680
5681 if (!ctx->file_data)
5682 return -ENXIO;
5683 if (!nr_args)
5684 return -EINVAL;
5685 if (copy_from_user(&up, arg, sizeof(up)))
5686 return -EFAULT;
5687 if (up.resv)
5688 return -EINVAL;
5689
5690 return __io_sqe_files_update(ctx, &up, nr_args);
5691}
Jens Axboec3a31e62019-10-03 13:59:56 -06005692
Jens Axboe7d723062019-11-12 22:31:31 -07005693static void io_put_work(struct io_wq_work *work)
5694{
5695 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5696
5697 io_put_req(req);
5698}
5699
5700static void io_get_work(struct io_wq_work *work)
5701{
5702 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5703
5704 refcount_inc(&req->refs);
5705}
5706
Jens Axboe6c271ce2019-01-10 11:22:30 -07005707static int io_sq_offload_start(struct io_ring_ctx *ctx,
5708 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005709{
Jens Axboe576a3472019-11-25 08:49:20 -07005710 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005711 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005712 int ret;
5713
Jens Axboe6c271ce2019-01-10 11:22:30 -07005714 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005715 mmgrab(current->mm);
5716 ctx->sqo_mm = current->mm;
5717
Jens Axboe6c271ce2019-01-10 11:22:30 -07005718 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005719 ret = -EPERM;
5720 if (!capable(CAP_SYS_ADMIN))
5721 goto err;
5722
Jens Axboe917257d2019-04-13 09:28:55 -06005723 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5724 if (!ctx->sq_thread_idle)
5725 ctx->sq_thread_idle = HZ;
5726
Jens Axboe6c271ce2019-01-10 11:22:30 -07005727 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005728 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005729
Jens Axboe917257d2019-04-13 09:28:55 -06005730 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005731 if (cpu >= nr_cpu_ids)
5732 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005733 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005734 goto err;
5735
Jens Axboe6c271ce2019-01-10 11:22:30 -07005736 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5737 ctx, cpu,
5738 "io_uring-sq");
5739 } else {
5740 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5741 "io_uring-sq");
5742 }
5743 if (IS_ERR(ctx->sqo_thread)) {
5744 ret = PTR_ERR(ctx->sqo_thread);
5745 ctx->sqo_thread = NULL;
5746 goto err;
5747 }
5748 wake_up_process(ctx->sqo_thread);
5749 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5750 /* Can't have SQ_AFF without SQPOLL */
5751 ret = -EINVAL;
5752 goto err;
5753 }
5754
Jens Axboe576a3472019-11-25 08:49:20 -07005755 data.user = ctx->user;
5756 data.get_work = io_get_work;
5757 data.put_work = io_put_work;
5758
Jens Axboe561fb042019-10-24 07:25:42 -06005759 /* Do QD, or 4 * CPUS, whatever is smallest */
5760 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005761 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005762 if (IS_ERR(ctx->io_wq)) {
5763 ret = PTR_ERR(ctx->io_wq);
5764 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005765 goto err;
5766 }
5767
5768 return 0;
5769err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005770 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005771 mmdrop(ctx->sqo_mm);
5772 ctx->sqo_mm = NULL;
5773 return ret;
5774}
5775
5776static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5777{
5778 atomic_long_sub(nr_pages, &user->locked_vm);
5779}
5780
5781static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5782{
5783 unsigned long page_limit, cur_pages, new_pages;
5784
5785 /* Don't allow more pages than we can safely lock */
5786 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5787
5788 do {
5789 cur_pages = atomic_long_read(&user->locked_vm);
5790 new_pages = cur_pages + nr_pages;
5791 if (new_pages > page_limit)
5792 return -ENOMEM;
5793 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5794 new_pages) != cur_pages);
5795
5796 return 0;
5797}
5798
5799static void io_mem_free(void *ptr)
5800{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005801 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005802
Mark Rutland52e04ef2019-04-30 17:30:21 +01005803 if (!ptr)
5804 return;
5805
5806 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005807 if (put_page_testzero(page))
5808 free_compound_page(page);
5809}
5810
5811static void *io_mem_alloc(size_t size)
5812{
5813 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5814 __GFP_NORETRY;
5815
5816 return (void *) __get_free_pages(gfp_flags, get_order(size));
5817}
5818
Hristo Venev75b28af2019-08-26 17:23:46 +00005819static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5820 size_t *sq_offset)
5821{
5822 struct io_rings *rings;
5823 size_t off, sq_array_size;
5824
5825 off = struct_size(rings, cqes, cq_entries);
5826 if (off == SIZE_MAX)
5827 return SIZE_MAX;
5828
5829#ifdef CONFIG_SMP
5830 off = ALIGN(off, SMP_CACHE_BYTES);
5831 if (off == 0)
5832 return SIZE_MAX;
5833#endif
5834
5835 sq_array_size = array_size(sizeof(u32), sq_entries);
5836 if (sq_array_size == SIZE_MAX)
5837 return SIZE_MAX;
5838
5839 if (check_add_overflow(off, sq_array_size, &off))
5840 return SIZE_MAX;
5841
5842 if (sq_offset)
5843 *sq_offset = off;
5844
5845 return off;
5846}
5847
Jens Axboe2b188cc2019-01-07 10:46:33 -07005848static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5849{
Hristo Venev75b28af2019-08-26 17:23:46 +00005850 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005851
Hristo Venev75b28af2019-08-26 17:23:46 +00005852 pages = (size_t)1 << get_order(
5853 rings_size(sq_entries, cq_entries, NULL));
5854 pages += (size_t)1 << get_order(
5855 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005856
Hristo Venev75b28af2019-08-26 17:23:46 +00005857 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005858}
5859
Jens Axboeedafcce2019-01-09 09:16:05 -07005860static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5861{
5862 int i, j;
5863
5864 if (!ctx->user_bufs)
5865 return -ENXIO;
5866
5867 for (i = 0; i < ctx->nr_user_bufs; i++) {
5868 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5869
5870 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005871 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005872
5873 if (ctx->account_mem)
5874 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005875 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005876 imu->nr_bvecs = 0;
5877 }
5878
5879 kfree(ctx->user_bufs);
5880 ctx->user_bufs = NULL;
5881 ctx->nr_user_bufs = 0;
5882 return 0;
5883}
5884
5885static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5886 void __user *arg, unsigned index)
5887{
5888 struct iovec __user *src;
5889
5890#ifdef CONFIG_COMPAT
5891 if (ctx->compat) {
5892 struct compat_iovec __user *ciovs;
5893 struct compat_iovec ciov;
5894
5895 ciovs = (struct compat_iovec __user *) arg;
5896 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5897 return -EFAULT;
5898
Jens Axboed55e5f52019-12-11 16:12:15 -07005899 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005900 dst->iov_len = ciov.iov_len;
5901 return 0;
5902 }
5903#endif
5904 src = (struct iovec __user *) arg;
5905 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5906 return -EFAULT;
5907 return 0;
5908}
5909
5910static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5911 unsigned nr_args)
5912{
5913 struct vm_area_struct **vmas = NULL;
5914 struct page **pages = NULL;
5915 int i, j, got_pages = 0;
5916 int ret = -EINVAL;
5917
5918 if (ctx->user_bufs)
5919 return -EBUSY;
5920 if (!nr_args || nr_args > UIO_MAXIOV)
5921 return -EINVAL;
5922
5923 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5924 GFP_KERNEL);
5925 if (!ctx->user_bufs)
5926 return -ENOMEM;
5927
5928 for (i = 0; i < nr_args; i++) {
5929 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5930 unsigned long off, start, end, ubuf;
5931 int pret, nr_pages;
5932 struct iovec iov;
5933 size_t size;
5934
5935 ret = io_copy_iov(ctx, &iov, arg, i);
5936 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005937 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005938
5939 /*
5940 * Don't impose further limits on the size and buffer
5941 * constraints here, we'll -EINVAL later when IO is
5942 * submitted if they are wrong.
5943 */
5944 ret = -EFAULT;
5945 if (!iov.iov_base || !iov.iov_len)
5946 goto err;
5947
5948 /* arbitrary limit, but we need something */
5949 if (iov.iov_len > SZ_1G)
5950 goto err;
5951
5952 ubuf = (unsigned long) iov.iov_base;
5953 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5954 start = ubuf >> PAGE_SHIFT;
5955 nr_pages = end - start;
5956
5957 if (ctx->account_mem) {
5958 ret = io_account_mem(ctx->user, nr_pages);
5959 if (ret)
5960 goto err;
5961 }
5962
5963 ret = 0;
5964 if (!pages || nr_pages > got_pages) {
5965 kfree(vmas);
5966 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005967 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005968 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005969 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005970 sizeof(struct vm_area_struct *),
5971 GFP_KERNEL);
5972 if (!pages || !vmas) {
5973 ret = -ENOMEM;
5974 if (ctx->account_mem)
5975 io_unaccount_mem(ctx->user, nr_pages);
5976 goto err;
5977 }
5978 got_pages = nr_pages;
5979 }
5980
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005981 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005982 GFP_KERNEL);
5983 ret = -ENOMEM;
5984 if (!imu->bvec) {
5985 if (ctx->account_mem)
5986 io_unaccount_mem(ctx->user, nr_pages);
5987 goto err;
5988 }
5989
5990 ret = 0;
5991 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005992 pret = get_user_pages(ubuf, nr_pages,
5993 FOLL_WRITE | FOLL_LONGTERM,
5994 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005995 if (pret == nr_pages) {
5996 /* don't support file backed memory */
5997 for (j = 0; j < nr_pages; j++) {
5998 struct vm_area_struct *vma = vmas[j];
5999
6000 if (vma->vm_file &&
6001 !is_file_hugepages(vma->vm_file)) {
6002 ret = -EOPNOTSUPP;
6003 break;
6004 }
6005 }
6006 } else {
6007 ret = pret < 0 ? pret : -EFAULT;
6008 }
6009 up_read(&current->mm->mmap_sem);
6010 if (ret) {
6011 /*
6012 * if we did partial map, or found file backed vmas,
6013 * release any pages we did get
6014 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006015 if (pret > 0)
6016 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006017 if (ctx->account_mem)
6018 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006019 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006020 goto err;
6021 }
6022
6023 off = ubuf & ~PAGE_MASK;
6024 size = iov.iov_len;
6025 for (j = 0; j < nr_pages; j++) {
6026 size_t vec_len;
6027
6028 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6029 imu->bvec[j].bv_page = pages[j];
6030 imu->bvec[j].bv_len = vec_len;
6031 imu->bvec[j].bv_offset = off;
6032 off = 0;
6033 size -= vec_len;
6034 }
6035 /* store original address for later verification */
6036 imu->ubuf = ubuf;
6037 imu->len = iov.iov_len;
6038 imu->nr_bvecs = nr_pages;
6039
6040 ctx->nr_user_bufs++;
6041 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006042 kvfree(pages);
6043 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006044 return 0;
6045err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006046 kvfree(pages);
6047 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006048 io_sqe_buffer_unregister(ctx);
6049 return ret;
6050}
6051
Jens Axboe9b402842019-04-11 11:45:41 -06006052static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6053{
6054 __s32 __user *fds = arg;
6055 int fd;
6056
6057 if (ctx->cq_ev_fd)
6058 return -EBUSY;
6059
6060 if (copy_from_user(&fd, fds, sizeof(*fds)))
6061 return -EFAULT;
6062
6063 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6064 if (IS_ERR(ctx->cq_ev_fd)) {
6065 int ret = PTR_ERR(ctx->cq_ev_fd);
6066 ctx->cq_ev_fd = NULL;
6067 return ret;
6068 }
6069
6070 return 0;
6071}
6072
6073static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6074{
6075 if (ctx->cq_ev_fd) {
6076 eventfd_ctx_put(ctx->cq_ev_fd);
6077 ctx->cq_ev_fd = NULL;
6078 return 0;
6079 }
6080
6081 return -ENXIO;
6082}
6083
Jens Axboe2b188cc2019-01-07 10:46:33 -07006084static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6085{
Jens Axboe6b063142019-01-10 22:13:58 -07006086 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006087 if (ctx->sqo_mm)
6088 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006089
6090 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006091 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006092 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006093 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006094
Jens Axboe2b188cc2019-01-07 10:46:33 -07006095#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006096 if (ctx->ring_sock) {
6097 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006098 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006099 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006100#endif
6101
Hristo Venev75b28af2019-08-26 17:23:46 +00006102 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006103 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006104
6105 percpu_ref_exit(&ctx->refs);
6106 if (ctx->account_mem)
6107 io_unaccount_mem(ctx->user,
6108 ring_pages(ctx->sq_entries, ctx->cq_entries));
6109 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006110 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006111 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006112 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006113 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006114 kfree(ctx);
6115}
6116
6117static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6118{
6119 struct io_ring_ctx *ctx = file->private_data;
6120 __poll_t mask = 0;
6121
6122 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006123 /*
6124 * synchronizes with barrier from wq_has_sleeper call in
6125 * io_commit_cqring
6126 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006127 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006128 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6129 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006130 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006131 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006132 mask |= EPOLLIN | EPOLLRDNORM;
6133
6134 return mask;
6135}
6136
6137static int io_uring_fasync(int fd, struct file *file, int on)
6138{
6139 struct io_ring_ctx *ctx = file->private_data;
6140
6141 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6142}
6143
6144static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6145{
6146 mutex_lock(&ctx->uring_lock);
6147 percpu_ref_kill(&ctx->refs);
6148 mutex_unlock(&ctx->uring_lock);
6149
Jens Axboe5262f562019-09-17 12:26:57 -06006150 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006151 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006152
6153 if (ctx->io_wq)
6154 io_wq_cancel_all(ctx->io_wq);
6155
Jens Axboedef596e2019-01-09 08:59:42 -07006156 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006157 /* if we failed setting up the ctx, we might not have any rings */
6158 if (ctx->rings)
6159 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07006160 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006161 io_ring_ctx_free(ctx);
6162}
6163
6164static int io_uring_release(struct inode *inode, struct file *file)
6165{
6166 struct io_ring_ctx *ctx = file->private_data;
6167
6168 file->private_data = NULL;
6169 io_ring_ctx_wait_and_kill(ctx);
6170 return 0;
6171}
6172
Jens Axboefcb323c2019-10-24 12:39:47 -06006173static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6174 struct files_struct *files)
6175{
6176 struct io_kiocb *req;
6177 DEFINE_WAIT(wait);
6178
6179 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006180 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006181
6182 spin_lock_irq(&ctx->inflight_lock);
6183 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006184 if (req->work.files != files)
6185 continue;
6186 /* req is being completed, ignore */
6187 if (!refcount_inc_not_zero(&req->refs))
6188 continue;
6189 cancel_req = req;
6190 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006191 }
Jens Axboe768134d2019-11-10 20:30:53 -07006192 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006193 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006194 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006195 spin_unlock_irq(&ctx->inflight_lock);
6196
Jens Axboe768134d2019-11-10 20:30:53 -07006197 /* We need to keep going until we don't find a matching req */
6198 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006199 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006200
6201 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6202 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006203 schedule();
6204 }
Jens Axboe768134d2019-11-10 20:30:53 -07006205 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006206}
6207
6208static int io_uring_flush(struct file *file, void *data)
6209{
6210 struct io_ring_ctx *ctx = file->private_data;
6211
6212 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006213 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6214 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006215 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006216 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006217 return 0;
6218}
6219
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006220static void *io_uring_validate_mmap_request(struct file *file,
6221 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006222{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006223 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006224 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006225 struct page *page;
6226 void *ptr;
6227
6228 switch (offset) {
6229 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006230 case IORING_OFF_CQ_RING:
6231 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006232 break;
6233 case IORING_OFF_SQES:
6234 ptr = ctx->sq_sqes;
6235 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006236 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006237 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006238 }
6239
6240 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006241 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006242 return ERR_PTR(-EINVAL);
6243
6244 return ptr;
6245}
6246
6247#ifdef CONFIG_MMU
6248
6249static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6250{
6251 size_t sz = vma->vm_end - vma->vm_start;
6252 unsigned long pfn;
6253 void *ptr;
6254
6255 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6256 if (IS_ERR(ptr))
6257 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006258
6259 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6260 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6261}
6262
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006263#else /* !CONFIG_MMU */
6264
6265static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6266{
6267 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6268}
6269
6270static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6271{
6272 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6273}
6274
6275static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6276 unsigned long addr, unsigned long len,
6277 unsigned long pgoff, unsigned long flags)
6278{
6279 void *ptr;
6280
6281 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6282 if (IS_ERR(ptr))
6283 return PTR_ERR(ptr);
6284
6285 return (unsigned long) ptr;
6286}
6287
6288#endif /* !CONFIG_MMU */
6289
Jens Axboe2b188cc2019-01-07 10:46:33 -07006290SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6291 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6292 size_t, sigsz)
6293{
6294 struct io_ring_ctx *ctx;
6295 long ret = -EBADF;
6296 int submitted = 0;
6297 struct fd f;
6298
Jens Axboe6c271ce2019-01-10 11:22:30 -07006299 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006300 return -EINVAL;
6301
6302 f = fdget(fd);
6303 if (!f.file)
6304 return -EBADF;
6305
6306 ret = -EOPNOTSUPP;
6307 if (f.file->f_op != &io_uring_fops)
6308 goto out_fput;
6309
6310 ret = -ENXIO;
6311 ctx = f.file->private_data;
6312 if (!percpu_ref_tryget(&ctx->refs))
6313 goto out_fput;
6314
Jens Axboe6c271ce2019-01-10 11:22:30 -07006315 /*
6316 * For SQ polling, the thread will do all submissions and completions.
6317 * Just return the requested submit count, and wake the thread if
6318 * we were asked to.
6319 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006320 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006321 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006322 if (!list_empty_careful(&ctx->cq_overflow_list))
6323 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006324 if (flags & IORING_ENTER_SQ_WAKEUP)
6325 wake_up(&ctx->sqo_wait);
6326 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006327 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006328 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006329
Jens Axboe44d28272020-01-16 19:00:24 -07006330 if (current->mm != ctx->sqo_mm ||
6331 current_cred() != ctx->creds) {
6332 ret = -EPERM;
6333 goto out;
6334 }
6335
Jens Axboe2b188cc2019-01-07 10:46:33 -07006336 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006337 /* already have mm, so io_submit_sqes() won't try to grab it */
6338 cur_mm = ctx->sqo_mm;
6339 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6340 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006341 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006342
6343 if (submitted != to_submit)
6344 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006345 }
6346 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006347 unsigned nr_events = 0;
6348
Jens Axboe2b188cc2019-01-07 10:46:33 -07006349 min_complete = min(min_complete, ctx->cq_entries);
6350
Jens Axboedef596e2019-01-09 08:59:42 -07006351 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006352 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006353 } else {
6354 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6355 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006356 }
6357
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006358out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006359 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006360out_fput:
6361 fdput(f);
6362 return submitted ? submitted : ret;
6363}
6364
6365static const struct file_operations io_uring_fops = {
6366 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006367 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006368 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006369#ifndef CONFIG_MMU
6370 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6371 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6372#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006373 .poll = io_uring_poll,
6374 .fasync = io_uring_fasync,
6375};
6376
6377static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6378 struct io_uring_params *p)
6379{
Hristo Venev75b28af2019-08-26 17:23:46 +00006380 struct io_rings *rings;
6381 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006382
Hristo Venev75b28af2019-08-26 17:23:46 +00006383 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6384 if (size == SIZE_MAX)
6385 return -EOVERFLOW;
6386
6387 rings = io_mem_alloc(size);
6388 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006389 return -ENOMEM;
6390
Hristo Venev75b28af2019-08-26 17:23:46 +00006391 ctx->rings = rings;
6392 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6393 rings->sq_ring_mask = p->sq_entries - 1;
6394 rings->cq_ring_mask = p->cq_entries - 1;
6395 rings->sq_ring_entries = p->sq_entries;
6396 rings->cq_ring_entries = p->cq_entries;
6397 ctx->sq_mask = rings->sq_ring_mask;
6398 ctx->cq_mask = rings->cq_ring_mask;
6399 ctx->sq_entries = rings->sq_ring_entries;
6400 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006401
6402 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006403 if (size == SIZE_MAX) {
6404 io_mem_free(ctx->rings);
6405 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006406 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006407 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006408
6409 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006410 if (!ctx->sq_sqes) {
6411 io_mem_free(ctx->rings);
6412 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006413 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006414 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006415
Jens Axboe2b188cc2019-01-07 10:46:33 -07006416 return 0;
6417}
6418
6419/*
6420 * Allocate an anonymous fd, this is what constitutes the application
6421 * visible backing of an io_uring instance. The application mmaps this
6422 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6423 * we have to tie this fd to a socket for file garbage collection purposes.
6424 */
6425static int io_uring_get_fd(struct io_ring_ctx *ctx)
6426{
6427 struct file *file;
6428 int ret;
6429
6430#if defined(CONFIG_UNIX)
6431 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6432 &ctx->ring_sock);
6433 if (ret)
6434 return ret;
6435#endif
6436
6437 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6438 if (ret < 0)
6439 goto err;
6440
6441 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6442 O_RDWR | O_CLOEXEC);
6443 if (IS_ERR(file)) {
6444 put_unused_fd(ret);
6445 ret = PTR_ERR(file);
6446 goto err;
6447 }
6448
6449#if defined(CONFIG_UNIX)
6450 ctx->ring_sock->file = file;
6451#endif
6452 fd_install(ret, file);
6453 return ret;
6454err:
6455#if defined(CONFIG_UNIX)
6456 sock_release(ctx->ring_sock);
6457 ctx->ring_sock = NULL;
6458#endif
6459 return ret;
6460}
6461
6462static int io_uring_create(unsigned entries, struct io_uring_params *p)
6463{
6464 struct user_struct *user = NULL;
6465 struct io_ring_ctx *ctx;
6466 bool account_mem;
6467 int ret;
6468
Jens Axboe8110c1a2019-12-28 15:39:54 -07006469 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006470 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006471 if (entries > IORING_MAX_ENTRIES) {
6472 if (!(p->flags & IORING_SETUP_CLAMP))
6473 return -EINVAL;
6474 entries = IORING_MAX_ENTRIES;
6475 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006476
6477 /*
6478 * Use twice as many entries for the CQ ring. It's possible for the
6479 * application to drive a higher depth than the size of the SQ ring,
6480 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006481 * some flexibility in overcommitting a bit. If the application has
6482 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6483 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006484 */
6485 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006486 if (p->flags & IORING_SETUP_CQSIZE) {
6487 /*
6488 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6489 * to a power-of-two, if it isn't already. We do NOT impose
6490 * any cq vs sq ring sizing.
6491 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006492 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006493 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006494 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6495 if (!(p->flags & IORING_SETUP_CLAMP))
6496 return -EINVAL;
6497 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6498 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006499 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6500 } else {
6501 p->cq_entries = 2 * p->sq_entries;
6502 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006503
6504 user = get_uid(current_user());
6505 account_mem = !capable(CAP_IPC_LOCK);
6506
6507 if (account_mem) {
6508 ret = io_account_mem(user,
6509 ring_pages(p->sq_entries, p->cq_entries));
6510 if (ret) {
6511 free_uid(user);
6512 return ret;
6513 }
6514 }
6515
6516 ctx = io_ring_ctx_alloc(p);
6517 if (!ctx) {
6518 if (account_mem)
6519 io_unaccount_mem(user, ring_pages(p->sq_entries,
6520 p->cq_entries));
6521 free_uid(user);
6522 return -ENOMEM;
6523 }
6524 ctx->compat = in_compat_syscall();
6525 ctx->account_mem = account_mem;
6526 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006527 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006528
6529 ret = io_allocate_scq_urings(ctx, p);
6530 if (ret)
6531 goto err;
6532
Jens Axboe6c271ce2019-01-10 11:22:30 -07006533 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006534 if (ret)
6535 goto err;
6536
Jens Axboe2b188cc2019-01-07 10:46:33 -07006537 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006538 p->sq_off.head = offsetof(struct io_rings, sq.head);
6539 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6540 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6541 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6542 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6543 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6544 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006545
6546 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006547 p->cq_off.head = offsetof(struct io_rings, cq.head);
6548 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6549 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6550 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6551 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6552 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006553
Jens Axboe044c1ab2019-10-28 09:15:33 -06006554 /*
6555 * Install ring fd as the very last thing, so we don't risk someone
6556 * having closed it before we finish setup
6557 */
6558 ret = io_uring_get_fd(ctx);
6559 if (ret < 0)
6560 goto err;
6561
Jens Axboeda8c9692019-12-02 18:51:26 -07006562 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006563 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6564 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006565 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006566 return ret;
6567err:
6568 io_ring_ctx_wait_and_kill(ctx);
6569 return ret;
6570}
6571
6572/*
6573 * Sets up an aio uring context, and returns the fd. Applications asks for a
6574 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6575 * params structure passed in.
6576 */
6577static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6578{
6579 struct io_uring_params p;
6580 long ret;
6581 int i;
6582
6583 if (copy_from_user(&p, params, sizeof(p)))
6584 return -EFAULT;
6585 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6586 if (p.resv[i])
6587 return -EINVAL;
6588 }
6589
Jens Axboe6c271ce2019-01-10 11:22:30 -07006590 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006591 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
6592 IORING_SETUP_CLAMP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006593 return -EINVAL;
6594
6595 ret = io_uring_create(entries, &p);
6596 if (ret < 0)
6597 return ret;
6598
6599 if (copy_to_user(params, &p, sizeof(p)))
6600 return -EFAULT;
6601
6602 return ret;
6603}
6604
6605SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6606 struct io_uring_params __user *, params)
6607{
6608 return io_uring_setup(entries, params);
6609}
6610
Jens Axboe66f4af92020-01-16 15:36:52 -07006611static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6612{
6613 struct io_uring_probe *p;
6614 size_t size;
6615 int i, ret;
6616
6617 size = struct_size(p, ops, nr_args);
6618 if (size == SIZE_MAX)
6619 return -EOVERFLOW;
6620 p = kzalloc(size, GFP_KERNEL);
6621 if (!p)
6622 return -ENOMEM;
6623
6624 ret = -EFAULT;
6625 if (copy_from_user(p, arg, size))
6626 goto out;
6627 ret = -EINVAL;
6628 if (memchr_inv(p, 0, size))
6629 goto out;
6630
6631 p->last_op = IORING_OP_LAST - 1;
6632 if (nr_args > IORING_OP_LAST)
6633 nr_args = IORING_OP_LAST;
6634
6635 for (i = 0; i < nr_args; i++) {
6636 p->ops[i].op = i;
6637 if (!io_op_defs[i].not_supported)
6638 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6639 }
6640 p->ops_len = i;
6641
6642 ret = 0;
6643 if (copy_to_user(arg, p, size))
6644 ret = -EFAULT;
6645out:
6646 kfree(p);
6647 return ret;
6648}
6649
Jens Axboeedafcce2019-01-09 09:16:05 -07006650static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6651 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006652 __releases(ctx->uring_lock)
6653 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006654{
6655 int ret;
6656
Jens Axboe35fa71a2019-04-22 10:23:23 -06006657 /*
6658 * We're inside the ring mutex, if the ref is already dying, then
6659 * someone else killed the ctx or is already going through
6660 * io_uring_register().
6661 */
6662 if (percpu_ref_is_dying(&ctx->refs))
6663 return -ENXIO;
6664
Jens Axboe05f3fb32019-12-09 11:22:50 -07006665 if (opcode != IORING_UNREGISTER_FILES &&
Jens Axboe66f4af92020-01-16 15:36:52 -07006666 opcode != IORING_REGISTER_FILES_UPDATE &&
6667 opcode != IORING_REGISTER_PROBE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006668 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006669
Jens Axboe05f3fb32019-12-09 11:22:50 -07006670 /*
6671 * Drop uring mutex before waiting for references to exit. If
6672 * another thread is currently inside io_uring_enter() it might
6673 * need to grab the uring_lock to make progress. If we hold it
6674 * here across the drain wait, then we can deadlock. It's safe
6675 * to drop the mutex here, since no new references will come in
6676 * after we've killed the percpu ref.
6677 */
6678 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006679 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006680 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006681 if (ret) {
6682 percpu_ref_resurrect(&ctx->refs);
6683 ret = -EINTR;
6684 goto out;
6685 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006686 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006687
6688 switch (opcode) {
6689 case IORING_REGISTER_BUFFERS:
6690 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6691 break;
6692 case IORING_UNREGISTER_BUFFERS:
6693 ret = -EINVAL;
6694 if (arg || nr_args)
6695 break;
6696 ret = io_sqe_buffer_unregister(ctx);
6697 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006698 case IORING_REGISTER_FILES:
6699 ret = io_sqe_files_register(ctx, arg, nr_args);
6700 break;
6701 case IORING_UNREGISTER_FILES:
6702 ret = -EINVAL;
6703 if (arg || nr_args)
6704 break;
6705 ret = io_sqe_files_unregister(ctx);
6706 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006707 case IORING_REGISTER_FILES_UPDATE:
6708 ret = io_sqe_files_update(ctx, arg, nr_args);
6709 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006710 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006711 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006712 ret = -EINVAL;
6713 if (nr_args != 1)
6714 break;
6715 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006716 if (ret)
6717 break;
6718 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6719 ctx->eventfd_async = 1;
6720 else
6721 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006722 break;
6723 case IORING_UNREGISTER_EVENTFD:
6724 ret = -EINVAL;
6725 if (arg || nr_args)
6726 break;
6727 ret = io_eventfd_unregister(ctx);
6728 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07006729 case IORING_REGISTER_PROBE:
6730 ret = -EINVAL;
6731 if (!arg || nr_args > 256)
6732 break;
6733 ret = io_probe(ctx, arg, nr_args);
6734 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006735 default:
6736 ret = -EINVAL;
6737 break;
6738 }
6739
Jens Axboe05f3fb32019-12-09 11:22:50 -07006740
6741 if (opcode != IORING_UNREGISTER_FILES &&
Jens Axboe66f4af92020-01-16 15:36:52 -07006742 opcode != IORING_REGISTER_FILES_UPDATE &&
6743 opcode != IORING_REGISTER_PROBE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006744 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006745 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07006746out:
6747 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006748 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006749 return ret;
6750}
6751
6752SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6753 void __user *, arg, unsigned int, nr_args)
6754{
6755 struct io_ring_ctx *ctx;
6756 long ret = -EBADF;
6757 struct fd f;
6758
6759 f = fdget(fd);
6760 if (!f.file)
6761 return -EBADF;
6762
6763 ret = -EOPNOTSUPP;
6764 if (f.file->f_op != &io_uring_fops)
6765 goto out_fput;
6766
6767 ctx = f.file->private_data;
6768
6769 mutex_lock(&ctx->uring_lock);
6770 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6771 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006772 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6773 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006774out_fput:
6775 fdput(f);
6776 return ret;
6777}
6778
Jens Axboe2b188cc2019-01-07 10:46:33 -07006779static int __init io_uring_init(void)
6780{
Jens Axboed3656342019-12-18 09:50:26 -07006781 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006782 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6783 return 0;
6784};
6785__initcall(io_uring_init);