blob: 7715a729271a80724217c653db670a9dcd900601 [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>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070059#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070073#include <linux/namei.h>
74#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070075#include <linux/fadvise.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070076
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020077#define CREATE_TRACE_POINTS
78#include <trace/events/io_uring.h>
79
Jens Axboe2b188cc2019-01-07 10:46:33 -070080#include <uapi/linux/io_uring.h>
81
82#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060083#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
Daniel Xu5277dea2019-09-14 14:23:45 -070085#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060086#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060087
88/*
89 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
90 */
91#define IORING_FILE_TABLE_SHIFT 9
92#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
93#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
94#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070095
96struct io_uring {
97 u32 head ____cacheline_aligned_in_smp;
98 u32 tail ____cacheline_aligned_in_smp;
99};
100
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000102 * This data is shared with the application through the mmap at offsets
103 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200104 *
105 * The offsets to the member fields are published through struct
106 * io_sqring_offsets when calling io_uring_setup.
107 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000108struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200109 /*
110 * Head and tail offsets into the ring; the offsets need to be
111 * masked to get valid indices.
112 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 * The kernel controls head of the sq ring and the tail of the cq ring,
114 * and the application controls tail of the sq ring and the head of the
115 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200116 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000117 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120 * ring_entries - 1)
121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 u32 sq_ring_mask, cq_ring_mask;
123 /* Ring sizes (constant, power of 2) */
124 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 /*
126 * Number of invalid entries dropped by the kernel due to
127 * invalid index stored in array
128 *
129 * Written by the kernel, shouldn't be modified by the
130 * application (i.e. get number of "new events" by comparing to
131 * cached value).
132 *
133 * After a new SQ head value was read by the application this
134 * counter includes all submissions that were dropped reaching
135 * the new SQ head (and possibly more).
136 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000137 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200138 /*
139 * Runtime flags
140 *
141 * Written by the kernel, shouldn't be modified by the
142 * application.
143 *
144 * The application needs a full memory barrier before checking
145 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
146 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000147 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200148 /*
149 * Number of completion events lost because the queue was full;
150 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800151 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200152 * the completion queue.
153 *
154 * Written by the kernel, shouldn't be modified by the
155 * application (i.e. get number of "new events" by comparing to
156 * cached value).
157 *
158 * As completion events come in out of order this counter is not
159 * ordered with any other data.
160 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000161 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 /*
163 * Ring buffer of completion events.
164 *
165 * The kernel writes completion events fresh every time they are
166 * produced, so the application is allowed to modify pending
167 * entries.
168 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000169 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700170};
171
Jens Axboeedafcce2019-01-09 09:16:05 -0700172struct io_mapped_ubuf {
173 u64 ubuf;
174 size_t len;
175 struct bio_vec *bvec;
176 unsigned int nr_bvecs;
177};
178
Jens Axboe65e19f52019-10-26 07:20:21 -0600179struct fixed_file_table {
180 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700181};
182
Jens Axboe05f3fb32019-12-09 11:22:50 -0700183enum {
184 FFD_F_ATOMIC,
185};
186
187struct fixed_file_data {
188 struct fixed_file_table *table;
189 struct io_ring_ctx *ctx;
190
191 struct percpu_ref refs;
192 struct llist_head put_llist;
193 unsigned long state;
194 struct work_struct ref_work;
195 struct completion done;
196};
197
Jens Axboe2b188cc2019-01-07 10:46:33 -0700198struct io_ring_ctx {
199 struct {
200 struct percpu_ref refs;
201 } ____cacheline_aligned_in_smp;
202
203 struct {
204 unsigned int flags;
Jens Axboe69b3e542020-01-08 11:01:46 -0700205 int compat: 1;
206 int account_mem: 1;
207 int cq_overflow_flushed: 1;
208 int drain_next: 1;
Jens Axboef2842ab2020-01-08 11:04:00 -0700209 int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700210
Hristo Venev75b28af2019-08-26 17:23:46 +0000211 /*
212 * Ring buffer of indices into array of io_uring_sqe, which is
213 * mmapped by the application using the IORING_OFF_SQES offset.
214 *
215 * This indirection could e.g. be used to assign fixed
216 * io_uring_sqe entries to operations and only submit them to
217 * the queue when needed.
218 *
219 * The kernel modifies neither the indices array nor the entries
220 * array.
221 */
222 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700223 unsigned cached_sq_head;
224 unsigned sq_entries;
225 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700226 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600227 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700228 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700229 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600230
231 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600232 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700233 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234
Jens Axboefcb323c2019-10-24 12:39:47 -0600235 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700236 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700237 } ____cacheline_aligned_in_smp;
238
Hristo Venev75b28af2019-08-26 17:23:46 +0000239 struct io_rings *rings;
240
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600242 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700243 struct task_struct *sqo_thread; /* if using sq thread polling */
244 struct mm_struct *sqo_mm;
245 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246
Jens Axboe6b063142019-01-10 22:13:58 -0700247 /*
248 * If used, fixed file set. Writers must ensure that ->refs is dead,
249 * readers must ensure that ->refs is alive as long as the file* is
250 * used. Only updated through io_uring_register(2).
251 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700252 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700253 unsigned nr_user_files;
254
Jens Axboeedafcce2019-01-09 09:16:05 -0700255 /* if used, fixed mapped user buffers */
256 unsigned nr_user_bufs;
257 struct io_mapped_ubuf *user_bufs;
258
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259 struct user_struct *user;
260
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700261 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700262
Jens Axboe206aefd2019-11-07 18:27:42 -0700263 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
264 struct completion *completions;
265
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700266 /* if all else fails... */
267 struct io_kiocb *fallback_req;
268
Jens Axboe206aefd2019-11-07 18:27:42 -0700269#if defined(CONFIG_UNIX)
270 struct socket *ring_sock;
271#endif
272
273 struct {
274 unsigned cached_cq_tail;
275 unsigned cq_entries;
276 unsigned cq_mask;
277 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700278 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700279 struct wait_queue_head cq_wait;
280 struct fasync_struct *cq_fasync;
281 struct eventfd_ctx *cq_ev_fd;
282 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283
284 struct {
285 struct mutex uring_lock;
286 wait_queue_head_t wait;
287 } ____cacheline_aligned_in_smp;
288
289 struct {
290 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700291 struct llist_head poll_llist;
292
Jens Axboedef596e2019-01-09 08:59:42 -0700293 /*
294 * ->poll_list is protected by the ctx->uring_lock for
295 * io_uring instances that don't use IORING_SETUP_SQPOLL.
296 * For SQPOLL, only the single threaded io_sq_thread() will
297 * manipulate the list, hence no extra locking is needed there.
298 */
299 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700300 struct hlist_head *cancel_hash;
301 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700302 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600303
304 spinlock_t inflight_lock;
305 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700306 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700307};
308
Jens Axboe09bb8392019-03-13 12:39:28 -0600309/*
310 * First field must be the file pointer in all the
311 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
312 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700313struct io_poll_iocb {
314 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700315 union {
316 struct wait_queue_head *head;
317 u64 addr;
318 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700319 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600320 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700321 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700322 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700323};
324
Jens Axboeb5dba592019-12-11 14:02:38 -0700325struct io_close {
326 struct file *file;
327 struct file *put_file;
328 int fd;
329};
330
Jens Axboead8a48a2019-11-15 08:49:11 -0700331struct io_timeout_data {
332 struct io_kiocb *req;
333 struct hrtimer timer;
334 struct timespec64 ts;
335 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300336 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700337};
338
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700339struct io_accept {
340 struct file *file;
341 struct sockaddr __user *addr;
342 int __user *addr_len;
343 int flags;
344};
345
346struct io_sync {
347 struct file *file;
348 loff_t len;
349 loff_t off;
350 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700351 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700352};
353
Jens Axboefbf23842019-12-17 18:45:56 -0700354struct io_cancel {
355 struct file *file;
356 u64 addr;
357};
358
Jens Axboeb29472e2019-12-17 18:50:29 -0700359struct io_timeout {
360 struct file *file;
361 u64 addr;
362 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700363 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700364};
365
Jens Axboe9adbd452019-12-20 08:45:55 -0700366struct io_rw {
367 /* NOTE: kiocb has the file as the first member, so don't do it here */
368 struct kiocb kiocb;
369 u64 addr;
370 u64 len;
371};
372
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700373struct io_connect {
374 struct file *file;
375 struct sockaddr __user *addr;
376 int addr_len;
377};
378
Jens Axboee47293f2019-12-20 08:58:21 -0700379struct io_sr_msg {
380 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700381 union {
382 struct user_msghdr __user *msg;
383 void __user *buf;
384 };
Jens Axboee47293f2019-12-20 08:58:21 -0700385 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700386 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700387};
388
Jens Axboe15b71ab2019-12-11 11:20:36 -0700389struct io_open {
390 struct file *file;
391 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700392 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700393 unsigned mask;
394 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700395 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700396 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700397 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700398};
399
Jens Axboe05f3fb32019-12-09 11:22:50 -0700400struct io_files_update {
401 struct file *file;
402 u64 arg;
403 u32 nr_args;
404 u32 offset;
405};
406
Jens Axboe4840e412019-12-25 22:03:45 -0700407struct io_fadvise {
408 struct file *file;
409 u64 offset;
410 u32 len;
411 u32 advice;
412};
413
Jens Axboec1ca7572019-12-25 22:18:28 -0700414struct io_madvise {
415 struct file *file;
416 u64 addr;
417 u32 len;
418 u32 advice;
419};
420
Jens Axboef499a022019-12-02 16:28:46 -0700421struct io_async_connect {
422 struct sockaddr_storage address;
423};
424
Jens Axboe03b12302019-12-02 18:50:25 -0700425struct io_async_msghdr {
426 struct iovec fast_iov[UIO_FASTIOV];
427 struct iovec *iov;
428 struct sockaddr __user *uaddr;
429 struct msghdr msg;
430};
431
Jens Axboef67676d2019-12-02 11:03:47 -0700432struct io_async_rw {
433 struct iovec fast_iov[UIO_FASTIOV];
434 struct iovec *iov;
435 ssize_t nr_segs;
436 ssize_t size;
437};
438
Jens Axboe15b71ab2019-12-11 11:20:36 -0700439struct io_async_open {
440 struct filename *filename;
441};
442
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700443struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700444 union {
445 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700446 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700447 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700448 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700449 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700450 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700451};
452
Jens Axboe09bb8392019-03-13 12:39:28 -0600453/*
454 * NOTE! Each of the iocb union members has the file pointer
455 * as the first entry in their struct definition. So you can
456 * access the file pointer through any of the sub-structs,
457 * or directly as just 'ki_filp' in this struct.
458 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700459struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700460 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600461 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700462 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700463 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700464 struct io_accept accept;
465 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700466 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700467 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700468 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700469 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700470 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700471 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700472 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700473 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700474 struct io_madvise madvise;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700475 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700476
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700477 struct io_async_ctx *io;
Jens Axboee94f1412019-12-19 12:06:02 -0700478 union {
479 /*
480 * ring_file is only used in the submission path, and
481 * llist_node is only used for poll deferred completions
482 */
483 struct file *ring_file;
484 struct llist_node llist_node;
485 };
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300486 int ring_fd;
487 bool has_user;
488 bool in_async;
489 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700490 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700491
492 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700493 union {
494 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700495 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700496 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600497 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700498 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700499 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200500#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700501#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700502#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700503#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200504#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
505#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600506#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700507#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800508#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300509#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600510#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600511#define REQ_F_ISREG 2048 /* regular file */
512#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700513#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800514#define REQ_F_INFLIGHT 16384 /* on inflight list */
515#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700516#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboece35a472019-12-17 08:04:44 -0700517#define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */
Jens Axboeba042912019-12-25 16:33:42 -0700518#define REQ_F_CUR_POS 262144 /* read/write uses file position */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700519 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600520 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600521 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700522
Jens Axboefcb323c2019-10-24 12:39:47 -0600523 struct list_head inflight_entry;
524
Jens Axboe561fb042019-10-24 07:25:42 -0600525 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700526};
527
528#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700529#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700530
Jens Axboe9a56a232019-01-09 09:06:50 -0700531struct io_submit_state {
532 struct blk_plug plug;
533
534 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700535 * io_kiocb alloc cache
536 */
537 void *reqs[IO_IOPOLL_BATCH];
538 unsigned int free_reqs;
539 unsigned int cur_req;
540
541 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700542 * File reference cache
543 */
544 struct file *file;
545 unsigned int fd;
546 unsigned int has_refs;
547 unsigned int used_refs;
548 unsigned int ios_left;
549};
550
Jens Axboed3656342019-12-18 09:50:26 -0700551struct io_op_def {
552 /* needs req->io allocated for deferral/async */
553 unsigned async_ctx : 1;
554 /* needs current->mm setup, does mm access */
555 unsigned needs_mm : 1;
556 /* needs req->file assigned */
557 unsigned needs_file : 1;
558 /* needs req->file assigned IFF fd is >= 0 */
559 unsigned fd_non_neg : 1;
560 /* hash wq insertion if file is a regular file */
561 unsigned hash_reg_file : 1;
562 /* unbound wq insertion if file is a non-regular file */
563 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700564 /* opcode is not supported by this kernel */
565 unsigned not_supported : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700566};
567
568static const struct io_op_def io_op_defs[] = {
569 {
570 /* IORING_OP_NOP */
571 },
572 {
573 /* IORING_OP_READV */
574 .async_ctx = 1,
575 .needs_mm = 1,
576 .needs_file = 1,
577 .unbound_nonreg_file = 1,
578 },
579 {
580 /* IORING_OP_WRITEV */
581 .async_ctx = 1,
582 .needs_mm = 1,
583 .needs_file = 1,
584 .hash_reg_file = 1,
585 .unbound_nonreg_file = 1,
586 },
587 {
588 /* IORING_OP_FSYNC */
589 .needs_file = 1,
590 },
591 {
592 /* IORING_OP_READ_FIXED */
593 .needs_file = 1,
594 .unbound_nonreg_file = 1,
595 },
596 {
597 /* IORING_OP_WRITE_FIXED */
598 .needs_file = 1,
599 .hash_reg_file = 1,
600 .unbound_nonreg_file = 1,
601 },
602 {
603 /* IORING_OP_POLL_ADD */
604 .needs_file = 1,
605 .unbound_nonreg_file = 1,
606 },
607 {
608 /* IORING_OP_POLL_REMOVE */
609 },
610 {
611 /* IORING_OP_SYNC_FILE_RANGE */
612 .needs_file = 1,
613 },
614 {
615 /* IORING_OP_SENDMSG */
616 .async_ctx = 1,
617 .needs_mm = 1,
618 .needs_file = 1,
619 .unbound_nonreg_file = 1,
620 },
621 {
622 /* IORING_OP_RECVMSG */
623 .async_ctx = 1,
624 .needs_mm = 1,
625 .needs_file = 1,
626 .unbound_nonreg_file = 1,
627 },
628 {
629 /* IORING_OP_TIMEOUT */
630 .async_ctx = 1,
631 .needs_mm = 1,
632 },
633 {
634 /* IORING_OP_TIMEOUT_REMOVE */
635 },
636 {
637 /* IORING_OP_ACCEPT */
638 .needs_mm = 1,
639 .needs_file = 1,
640 .unbound_nonreg_file = 1,
641 },
642 {
643 /* IORING_OP_ASYNC_CANCEL */
644 },
645 {
646 /* IORING_OP_LINK_TIMEOUT */
647 .async_ctx = 1,
648 .needs_mm = 1,
649 },
650 {
651 /* IORING_OP_CONNECT */
652 .async_ctx = 1,
653 .needs_mm = 1,
654 .needs_file = 1,
655 .unbound_nonreg_file = 1,
656 },
657 {
658 /* IORING_OP_FALLOCATE */
659 .needs_file = 1,
660 },
661 {
662 /* IORING_OP_OPENAT */
663 .needs_file = 1,
664 .fd_non_neg = 1,
665 },
666 {
667 /* IORING_OP_CLOSE */
668 .needs_file = 1,
669 },
670 {
671 /* IORING_OP_FILES_UPDATE */
672 .needs_mm = 1,
673 },
674 {
675 /* IORING_OP_STATX */
676 .needs_mm = 1,
677 .needs_file = 1,
678 .fd_non_neg = 1,
679 },
Jens Axboe3a6820f2019-12-22 15:19:35 -0700680 {
681 /* IORING_OP_READ */
682 .needs_mm = 1,
683 .needs_file = 1,
684 .unbound_nonreg_file = 1,
685 },
686 {
687 /* IORING_OP_WRITE */
688 .needs_mm = 1,
689 .needs_file = 1,
690 .unbound_nonreg_file = 1,
691 },
Jens Axboe4840e412019-12-25 22:03:45 -0700692 {
693 /* IORING_OP_FADVISE */
694 .needs_file = 1,
695 },
Jens Axboec1ca7572019-12-25 22:18:28 -0700696 {
697 /* IORING_OP_MADVISE */
698 .needs_mm = 1,
699 },
Jens Axboefddafac2020-01-04 20:19:44 -0700700 {
701 /* IORING_OP_SEND */
702 .needs_mm = 1,
703 .needs_file = 1,
704 .unbound_nonreg_file = 1,
705 },
706 {
707 /* IORING_OP_RECV */
708 .needs_mm = 1,
709 .needs_file = 1,
710 .unbound_nonreg_file = 1,
711 },
Jens Axboecebdb982020-01-08 17:59:24 -0700712 {
713 /* IORING_OP_OPENAT2 */
714 .needs_file = 1,
715 .fd_non_neg = 1,
716 },
Jens Axboed3656342019-12-18 09:50:26 -0700717};
718
Jens Axboe561fb042019-10-24 07:25:42 -0600719static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700720static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800721static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700722static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700723static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
724static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700725static int __io_sqe_files_update(struct io_ring_ctx *ctx,
726 struct io_uring_files_update *ip,
727 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600728
Jens Axboe2b188cc2019-01-07 10:46:33 -0700729static struct kmem_cache *req_cachep;
730
731static const struct file_operations io_uring_fops;
732
733struct sock *io_uring_get_socket(struct file *file)
734{
735#if defined(CONFIG_UNIX)
736 if (file->f_op == &io_uring_fops) {
737 struct io_ring_ctx *ctx = file->private_data;
738
739 return ctx->ring_sock->sk;
740 }
741#endif
742 return NULL;
743}
744EXPORT_SYMBOL(io_uring_get_socket);
745
746static void io_ring_ctx_ref_free(struct percpu_ref *ref)
747{
748 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
749
Jens Axboe206aefd2019-11-07 18:27:42 -0700750 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700751}
752
753static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
754{
755 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700756 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700757
758 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
759 if (!ctx)
760 return NULL;
761
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700762 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
763 if (!ctx->fallback_req)
764 goto err;
765
Jens Axboe206aefd2019-11-07 18:27:42 -0700766 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
767 if (!ctx->completions)
768 goto err;
769
Jens Axboe78076bb2019-12-04 19:56:40 -0700770 /*
771 * Use 5 bits less than the max cq entries, that should give us around
772 * 32 entries per hash list if totally full and uniformly spread.
773 */
774 hash_bits = ilog2(p->cq_entries);
775 hash_bits -= 5;
776 if (hash_bits <= 0)
777 hash_bits = 1;
778 ctx->cancel_hash_bits = hash_bits;
779 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
780 GFP_KERNEL);
781 if (!ctx->cancel_hash)
782 goto err;
783 __hash_init(ctx->cancel_hash, 1U << hash_bits);
784
Roman Gushchin21482892019-05-07 10:01:48 -0700785 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700786 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
787 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700788
789 ctx->flags = p->flags;
790 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700791 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700792 init_completion(&ctx->completions[0]);
793 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700794 mutex_init(&ctx->uring_lock);
795 init_waitqueue_head(&ctx->wait);
796 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700797 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700798 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600799 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600800 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600801 init_waitqueue_head(&ctx->inflight_wait);
802 spin_lock_init(&ctx->inflight_lock);
803 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700804 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700805err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700806 if (ctx->fallback_req)
807 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700808 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700809 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700810 kfree(ctx);
811 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700812}
813
Bob Liu9d858b22019-11-13 18:06:25 +0800814static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600815{
Jackie Liua197f662019-11-08 08:09:12 -0700816 struct io_ring_ctx *ctx = req->ctx;
817
Jens Axboe498ccd92019-10-25 10:04:25 -0600818 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
819 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600820}
821
Bob Liu9d858b22019-11-13 18:06:25 +0800822static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600823{
Bob Liu9d858b22019-11-13 18:06:25 +0800824 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
825 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600826
Bob Liu9d858b22019-11-13 18:06:25 +0800827 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600828}
829
830static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600831{
832 struct io_kiocb *req;
833
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600834 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800835 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600836 list_del_init(&req->list);
837 return req;
838 }
839
840 return NULL;
841}
842
Jens Axboe5262f562019-09-17 12:26:57 -0600843static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
844{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600845 struct io_kiocb *req;
846
847 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700848 if (req) {
849 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
850 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800851 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700852 list_del_init(&req->list);
853 return req;
854 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600855 }
856
857 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600858}
859
Jens Axboede0617e2019-04-06 21:51:27 -0600860static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700861{
Hristo Venev75b28af2019-08-26 17:23:46 +0000862 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700863
Hristo Venev75b28af2019-08-26 17:23:46 +0000864 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700865 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000866 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700867
Jens Axboe2b188cc2019-01-07 10:46:33 -0700868 if (wq_has_sleeper(&ctx->cq_wait)) {
869 wake_up_interruptible(&ctx->cq_wait);
870 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
871 }
872 }
873}
874
Jens Axboe94ae5e72019-11-14 19:39:52 -0700875static inline bool io_prep_async_work(struct io_kiocb *req,
876 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600877{
Jens Axboed3656342019-12-18 09:50:26 -0700878 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600879 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600880
Jens Axboed3656342019-12-18 09:50:26 -0700881 if (req->flags & REQ_F_ISREG) {
882 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700883 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700884 } else {
885 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700886 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600887 }
Jens Axboed3656342019-12-18 09:50:26 -0700888 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700889 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600890
Jens Axboe94ae5e72019-11-14 19:39:52 -0700891 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600892 return do_hashed;
893}
894
Jackie Liua197f662019-11-08 08:09:12 -0700895static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600896{
Jackie Liua197f662019-11-08 08:09:12 -0700897 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700898 struct io_kiocb *link;
899 bool do_hashed;
900
901 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600902
903 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
904 req->flags);
905 if (!do_hashed) {
906 io_wq_enqueue(ctx->io_wq, &req->work);
907 } else {
908 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
909 file_inode(req->file));
910 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700911
912 if (link)
913 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600914}
915
Jens Axboe5262f562019-09-17 12:26:57 -0600916static void io_kill_timeout(struct io_kiocb *req)
917{
918 int ret;
919
Jens Axboe2d283902019-12-04 11:08:05 -0700920 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600921 if (ret != -1) {
922 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600923 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700924 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800925 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600926 }
927}
928
929static void io_kill_timeouts(struct io_ring_ctx *ctx)
930{
931 struct io_kiocb *req, *tmp;
932
933 spin_lock_irq(&ctx->completion_lock);
934 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
935 io_kill_timeout(req);
936 spin_unlock_irq(&ctx->completion_lock);
937}
938
Jens Axboede0617e2019-04-06 21:51:27 -0600939static void io_commit_cqring(struct io_ring_ctx *ctx)
940{
941 struct io_kiocb *req;
942
Jens Axboe5262f562019-09-17 12:26:57 -0600943 while ((req = io_get_timeout_req(ctx)) != NULL)
944 io_kill_timeout(req);
945
Jens Axboede0617e2019-04-06 21:51:27 -0600946 __io_commit_cqring(ctx);
947
948 while ((req = io_get_deferred_req(ctx)) != NULL) {
949 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700950 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600951 }
952}
953
Jens Axboe2b188cc2019-01-07 10:46:33 -0700954static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
955{
Hristo Venev75b28af2019-08-26 17:23:46 +0000956 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700957 unsigned tail;
958
959 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200960 /*
961 * writes to the cq entry need to come after reading head; the
962 * control dependency is enough as we're using WRITE_ONCE to
963 * fill the cq entry
964 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000965 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700966 return NULL;
967
968 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000969 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700970}
971
Jens Axboef2842ab2020-01-08 11:04:00 -0700972static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
973{
974 if (!ctx->eventfd_async)
975 return true;
976 return io_wq_current_is_worker() || in_interrupt();
977}
978
Jens Axboe8c838782019-03-12 15:48:16 -0600979static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
980{
981 if (waitqueue_active(&ctx->wait))
982 wake_up(&ctx->wait);
983 if (waitqueue_active(&ctx->sqo_wait))
984 wake_up(&ctx->sqo_wait);
Jens Axboef2842ab2020-01-08 11:04:00 -0700985 if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -0600986 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600987}
988
Jens Axboec4a2ed72019-11-21 21:01:26 -0700989/* Returns true if there are no backlogged entries after the flush */
990static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700991{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700992 struct io_rings *rings = ctx->rings;
993 struct io_uring_cqe *cqe;
994 struct io_kiocb *req;
995 unsigned long flags;
996 LIST_HEAD(list);
997
998 if (!force) {
999 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001000 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001001 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1002 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001003 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001004 }
1005
1006 spin_lock_irqsave(&ctx->completion_lock, flags);
1007
1008 /* if force is set, the ring is going away. always drop after that */
1009 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001010 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001011
Jens Axboec4a2ed72019-11-21 21:01:26 -07001012 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001013 while (!list_empty(&ctx->cq_overflow_list)) {
1014 cqe = io_get_cqring(ctx);
1015 if (!cqe && !force)
1016 break;
1017
1018 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1019 list);
1020 list_move(&req->list, &list);
1021 if (cqe) {
1022 WRITE_ONCE(cqe->user_data, req->user_data);
1023 WRITE_ONCE(cqe->res, req->result);
1024 WRITE_ONCE(cqe->flags, 0);
1025 } else {
1026 WRITE_ONCE(ctx->rings->cq_overflow,
1027 atomic_inc_return(&ctx->cached_cq_overflow));
1028 }
1029 }
1030
1031 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001032 if (cqe) {
1033 clear_bit(0, &ctx->sq_check_overflow);
1034 clear_bit(0, &ctx->cq_check_overflow);
1035 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001036 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1037 io_cqring_ev_posted(ctx);
1038
1039 while (!list_empty(&list)) {
1040 req = list_first_entry(&list, struct io_kiocb, list);
1041 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001042 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001043 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001044
1045 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001046}
1047
Jens Axboe78e19bb2019-11-06 15:21:34 -07001048static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001050 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001051 struct io_uring_cqe *cqe;
1052
Jens Axboe78e19bb2019-11-06 15:21:34 -07001053 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001054
Jens Axboe2b188cc2019-01-07 10:46:33 -07001055 /*
1056 * If we can't get a cq entry, userspace overflowed the
1057 * submission (by quite a lot). Increment the overflow count in
1058 * the ring.
1059 */
1060 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001061 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001062 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001063 WRITE_ONCE(cqe->res, res);
1064 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001065 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001066 WRITE_ONCE(ctx->rings->cq_overflow,
1067 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001068 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001069 if (list_empty(&ctx->cq_overflow_list)) {
1070 set_bit(0, &ctx->sq_check_overflow);
1071 set_bit(0, &ctx->cq_check_overflow);
1072 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001073 refcount_inc(&req->refs);
1074 req->result = res;
1075 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001076 }
1077}
1078
Jens Axboe78e19bb2019-11-06 15:21:34 -07001079static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001080{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001081 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001082 unsigned long flags;
1083
1084 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001085 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001086 io_commit_cqring(ctx);
1087 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1088
Jens Axboe8c838782019-03-12 15:48:16 -06001089 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001090}
1091
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001092static inline bool io_is_fallback_req(struct io_kiocb *req)
1093{
1094 return req == (struct io_kiocb *)
1095 ((unsigned long) req->ctx->fallback_req & ~1UL);
1096}
1097
1098static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1099{
1100 struct io_kiocb *req;
1101
1102 req = ctx->fallback_req;
1103 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1104 return req;
1105
1106 return NULL;
1107}
1108
Jens Axboe2579f912019-01-09 09:10:43 -07001109static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1110 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001111{
Jens Axboefd6fab22019-03-14 16:30:06 -06001112 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001113 struct io_kiocb *req;
1114
Jens Axboe2579f912019-01-09 09:10:43 -07001115 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001116 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001117 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001118 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001119 } else if (!state->free_reqs) {
1120 size_t sz;
1121 int ret;
1122
1123 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001124 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1125
1126 /*
1127 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1128 * retry single alloc to be on the safe side.
1129 */
1130 if (unlikely(ret <= 0)) {
1131 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1132 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001133 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001134 ret = 1;
1135 }
Jens Axboe2579f912019-01-09 09:10:43 -07001136 state->free_reqs = ret - 1;
1137 state->cur_req = 1;
1138 req = state->reqs[0];
1139 } else {
1140 req = state->reqs[state->cur_req];
1141 state->free_reqs--;
1142 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001143 }
1144
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001145got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001146 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001147 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001148 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001149 req->ctx = ctx;
1150 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001151 /* one is dropped after submission, the other at completion */
1152 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001153 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001154 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001155 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001156fallback:
1157 req = io_get_fallback_req(ctx);
1158 if (req)
1159 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001160 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001161 return NULL;
1162}
1163
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001164static void __io_req_do_free(struct io_kiocb *req)
1165{
1166 if (likely(!io_is_fallback_req(req)))
1167 kmem_cache_free(req_cachep, req);
1168 else
1169 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1170}
1171
Jens Axboec6ca97b302019-12-28 12:11:08 -07001172static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001173{
Jens Axboefcb323c2019-10-24 12:39:47 -06001174 struct io_ring_ctx *ctx = req->ctx;
1175
YueHaibing96fd84d2020-01-07 22:22:44 +08001176 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001177 if (req->file) {
1178 if (req->flags & REQ_F_FIXED_FILE)
1179 percpu_ref_put(&ctx->file_data->refs);
1180 else
1181 fput(req->file);
1182 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001183}
1184
1185static void __io_free_req(struct io_kiocb *req)
1186{
1187 __io_req_aux_free(req);
1188
Jens Axboefcb323c2019-10-24 12:39:47 -06001189 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001190 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001191 unsigned long flags;
1192
1193 spin_lock_irqsave(&ctx->inflight_lock, flags);
1194 list_del(&req->inflight_entry);
1195 if (waitqueue_active(&ctx->inflight_wait))
1196 wake_up(&ctx->inflight_wait);
1197 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1198 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001199
1200 percpu_ref_put(&req->ctx->refs);
1201 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001202}
1203
Jens Axboec6ca97b302019-12-28 12:11:08 -07001204struct req_batch {
1205 void *reqs[IO_IOPOLL_BATCH];
1206 int to_free;
1207 int need_iter;
1208};
1209
1210static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1211{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001212 int fixed_refs = rb->to_free;
1213
Jens Axboec6ca97b302019-12-28 12:11:08 -07001214 if (!rb->to_free)
1215 return;
1216 if (rb->need_iter) {
1217 int i, inflight = 0;
1218 unsigned long flags;
1219
Jens Axboe10fef4b2020-01-09 07:52:28 -07001220 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001221 for (i = 0; i < rb->to_free; i++) {
1222 struct io_kiocb *req = rb->reqs[i];
1223
Jens Axboe10fef4b2020-01-09 07:52:28 -07001224 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001225 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001226 fixed_refs++;
1227 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001228 if (req->flags & REQ_F_INFLIGHT)
1229 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001230 __io_req_aux_free(req);
1231 }
1232 if (!inflight)
1233 goto do_free;
1234
1235 spin_lock_irqsave(&ctx->inflight_lock, flags);
1236 for (i = 0; i < rb->to_free; i++) {
1237 struct io_kiocb *req = rb->reqs[i];
1238
Jens Axboe10fef4b2020-01-09 07:52:28 -07001239 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001240 list_del(&req->inflight_entry);
1241 if (!--inflight)
1242 break;
1243 }
1244 }
1245 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1246
1247 if (waitqueue_active(&ctx->inflight_wait))
1248 wake_up(&ctx->inflight_wait);
1249 }
1250do_free:
1251 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001252 if (fixed_refs)
1253 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001254 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001255 rb->to_free = rb->need_iter = 0;
1256}
1257
Jackie Liua197f662019-11-08 08:09:12 -07001258static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001259{
Jackie Liua197f662019-11-08 08:09:12 -07001260 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001261 int ret;
1262
Jens Axboe2d283902019-12-04 11:08:05 -07001263 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001264 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001265 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001266 io_commit_cqring(ctx);
1267 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001268 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001269 return true;
1270 }
1271
1272 return false;
1273}
1274
Jens Axboeba816ad2019-09-28 11:36:45 -06001275static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001276{
Jens Axboe2665abf2019-11-05 12:40:47 -07001277 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001278 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001279
Jens Axboe4d7dd462019-11-20 13:03:52 -07001280 /* Already got next link */
1281 if (req->flags & REQ_F_LINK_NEXT)
1282 return;
1283
Jens Axboe9e645e112019-05-10 16:07:28 -06001284 /*
1285 * The list should never be empty when we are called here. But could
1286 * potentially happen if the chain is messed up, check to be on the
1287 * safe side.
1288 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001289 while (!list_empty(&req->link_list)) {
1290 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1291 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001292
Pavel Begunkov44932332019-12-05 16:16:35 +03001293 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1294 (nxt->flags & REQ_F_TIMEOUT))) {
1295 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001296 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001297 req->flags &= ~REQ_F_LINK_TIMEOUT;
1298 continue;
1299 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001300
Pavel Begunkov44932332019-12-05 16:16:35 +03001301 list_del_init(&req->link_list);
1302 if (!list_empty(&nxt->link_list))
1303 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001304 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001305 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001306 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001307
Jens Axboe4d7dd462019-11-20 13:03:52 -07001308 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001309 if (wake_ev)
1310 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001311}
1312
1313/*
1314 * Called if REQ_F_LINK is set, and we fail the head request
1315 */
1316static void io_fail_links(struct io_kiocb *req)
1317{
Jens Axboe2665abf2019-11-05 12:40:47 -07001318 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001319 unsigned long flags;
1320
1321 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001322
1323 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001324 struct io_kiocb *link = list_first_entry(&req->link_list,
1325 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001326
Pavel Begunkov44932332019-12-05 16:16:35 +03001327 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001328 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001329
1330 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001331 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001332 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001333 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001334 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001335 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001336 }
Jens Axboe5d960722019-11-19 15:31:28 -07001337 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001338 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001339
1340 io_commit_cqring(ctx);
1341 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1342 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001343}
1344
Jens Axboe4d7dd462019-11-20 13:03:52 -07001345static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001346{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001347 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001348 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001349
Jens Axboe9e645e112019-05-10 16:07:28 -06001350 /*
1351 * If LINK is set, we have dependent requests in this chain. If we
1352 * didn't fail this request, queue the first one up, moving any other
1353 * dependencies to the next request. In case of failure, fail the rest
1354 * of the chain.
1355 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001356 if (req->flags & REQ_F_FAIL_LINK) {
1357 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001358 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1359 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001360 struct io_ring_ctx *ctx = req->ctx;
1361 unsigned long flags;
1362
1363 /*
1364 * If this is a timeout link, we could be racing with the
1365 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001366 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001367 */
1368 spin_lock_irqsave(&ctx->completion_lock, flags);
1369 io_req_link_next(req, nxt);
1370 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1371 } else {
1372 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001373 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001374}
Jens Axboe9e645e112019-05-10 16:07:28 -06001375
Jackie Liuc69f8db2019-11-09 11:00:08 +08001376static void io_free_req(struct io_kiocb *req)
1377{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001378 struct io_kiocb *nxt = NULL;
1379
1380 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001381 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001382
1383 if (nxt)
1384 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001385}
1386
Jens Axboeba816ad2019-09-28 11:36:45 -06001387/*
1388 * Drop reference to request, return next in chain (if there is one) if this
1389 * was the last reference to this request.
1390 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001391__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001392static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001393{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001394 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001395
Jens Axboee65ef562019-03-12 10:16:44 -06001396 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001397 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001398}
1399
Jens Axboe2b188cc2019-01-07 10:46:33 -07001400static void io_put_req(struct io_kiocb *req)
1401{
Jens Axboedef596e2019-01-09 08:59:42 -07001402 if (refcount_dec_and_test(&req->refs))
1403 io_free_req(req);
1404}
1405
Jens Axboe978db572019-11-14 22:39:04 -07001406/*
1407 * Must only be used if we don't need to care about links, usually from
1408 * within the completion handling itself.
1409 */
1410static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001411{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001412 /* drop both submit and complete references */
1413 if (refcount_sub_and_test(2, &req->refs))
1414 __io_free_req(req);
1415}
1416
Jens Axboe978db572019-11-14 22:39:04 -07001417static void io_double_put_req(struct io_kiocb *req)
1418{
1419 /* drop both submit and complete references */
1420 if (refcount_sub_and_test(2, &req->refs))
1421 io_free_req(req);
1422}
1423
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001424static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001425{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001426 struct io_rings *rings = ctx->rings;
1427
Jens Axboead3eb2c2019-12-18 17:12:20 -07001428 if (test_bit(0, &ctx->cq_check_overflow)) {
1429 /*
1430 * noflush == true is from the waitqueue handler, just ensure
1431 * we wake up the task, and the next invocation will flush the
1432 * entries. We cannot safely to it from here.
1433 */
1434 if (noflush && !list_empty(&ctx->cq_overflow_list))
1435 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001436
Jens Axboead3eb2c2019-12-18 17:12:20 -07001437 io_cqring_overflow_flush(ctx, false);
1438 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001439
Jens Axboea3a0e432019-08-20 11:03:11 -06001440 /* See comment at the top of this file */
1441 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001442 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001443}
1444
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001445static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1446{
1447 struct io_rings *rings = ctx->rings;
1448
1449 /* make sure SQ entry isn't read before tail */
1450 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1451}
1452
Jens Axboe8237e042019-12-28 10:48:22 -07001453static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001454{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001455 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1456 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001457
Jens Axboec6ca97b302019-12-28 12:11:08 -07001458 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1459 rb->need_iter++;
1460
1461 rb->reqs[rb->to_free++] = req;
1462 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1463 io_free_req_many(req->ctx, rb);
1464 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001465}
1466
Jens Axboedef596e2019-01-09 08:59:42 -07001467/*
1468 * Find and free completed poll iocbs
1469 */
1470static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1471 struct list_head *done)
1472{
Jens Axboe8237e042019-12-28 10:48:22 -07001473 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001474 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001475
Jens Axboec6ca97b302019-12-28 12:11:08 -07001476 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001477 while (!list_empty(done)) {
1478 req = list_first_entry(done, struct io_kiocb, list);
1479 list_del(&req->list);
1480
Jens Axboe78e19bb2019-11-06 15:21:34 -07001481 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001482 (*nr_events)++;
1483
Jens Axboe8237e042019-12-28 10:48:22 -07001484 if (refcount_dec_and_test(&req->refs) &&
1485 !io_req_multi_free(&rb, req))
1486 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001487 }
Jens Axboedef596e2019-01-09 08:59:42 -07001488
Jens Axboe09bb8392019-03-13 12:39:28 -06001489 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001490 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001491}
1492
1493static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1494 long min)
1495{
1496 struct io_kiocb *req, *tmp;
1497 LIST_HEAD(done);
1498 bool spin;
1499 int ret;
1500
1501 /*
1502 * Only spin for completions if we don't have multiple devices hanging
1503 * off our complete list, and we're under the requested amount.
1504 */
1505 spin = !ctx->poll_multi_file && *nr_events < min;
1506
1507 ret = 0;
1508 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001509 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001510
1511 /*
1512 * Move completed entries to our local list. If we find a
1513 * request that requires polling, break out and complete
1514 * the done list first, if we have entries there.
1515 */
1516 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1517 list_move_tail(&req->list, &done);
1518 continue;
1519 }
1520 if (!list_empty(&done))
1521 break;
1522
1523 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1524 if (ret < 0)
1525 break;
1526
1527 if (ret && spin)
1528 spin = false;
1529 ret = 0;
1530 }
1531
1532 if (!list_empty(&done))
1533 io_iopoll_complete(ctx, nr_events, &done);
1534
1535 return ret;
1536}
1537
1538/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001539 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001540 * non-spinning poll check - we'll still enter the driver poll loop, but only
1541 * as a non-spinning completion check.
1542 */
1543static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1544 long min)
1545{
Jens Axboe08f54392019-08-21 22:19:11 -06001546 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001547 int ret;
1548
1549 ret = io_do_iopoll(ctx, nr_events, min);
1550 if (ret < 0)
1551 return ret;
1552 if (!min || *nr_events >= min)
1553 return 0;
1554 }
1555
1556 return 1;
1557}
1558
1559/*
1560 * We can't just wait for polled events to come to us, we have to actively
1561 * find and complete them.
1562 */
1563static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1564{
1565 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1566 return;
1567
1568 mutex_lock(&ctx->uring_lock);
1569 while (!list_empty(&ctx->poll_list)) {
1570 unsigned int nr_events = 0;
1571
1572 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001573
1574 /*
1575 * Ensure we allow local-to-the-cpu processing to take place,
1576 * in this case we need to ensure that we reap all events.
1577 */
1578 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001579 }
1580 mutex_unlock(&ctx->uring_lock);
1581}
1582
Jens Axboe2b2ed972019-10-25 10:06:15 -06001583static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1584 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001585{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001586 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001587
1588 do {
1589 int tmin = 0;
1590
Jens Axboe500f9fb2019-08-19 12:15:59 -06001591 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001592 * Don't enter poll loop if we already have events pending.
1593 * If we do, we can potentially be spinning for commands that
1594 * already triggered a CQE (eg in error).
1595 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001596 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001597 break;
1598
1599 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001600 * If a submit got punted to a workqueue, we can have the
1601 * application entering polling for a command before it gets
1602 * issued. That app will hold the uring_lock for the duration
1603 * of the poll right here, so we need to take a breather every
1604 * now and then to ensure that the issue has a chance to add
1605 * the poll to the issued list. Otherwise we can spin here
1606 * forever, while the workqueue is stuck trying to acquire the
1607 * very same mutex.
1608 */
1609 if (!(++iters & 7)) {
1610 mutex_unlock(&ctx->uring_lock);
1611 mutex_lock(&ctx->uring_lock);
1612 }
1613
Jens Axboedef596e2019-01-09 08:59:42 -07001614 if (*nr_events < min)
1615 tmin = min - *nr_events;
1616
1617 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1618 if (ret <= 0)
1619 break;
1620 ret = 0;
1621 } while (min && !*nr_events && !need_resched());
1622
Jens Axboe2b2ed972019-10-25 10:06:15 -06001623 return ret;
1624}
1625
1626static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1627 long min)
1628{
1629 int ret;
1630
1631 /*
1632 * We disallow the app entering submit/complete with polling, but we
1633 * still need to lock the ring to prevent racing with polled issue
1634 * that got punted to a workqueue.
1635 */
1636 mutex_lock(&ctx->uring_lock);
1637 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001638 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001639 return ret;
1640}
1641
Jens Axboe491381ce2019-10-17 09:20:46 -06001642static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001643{
Jens Axboe491381ce2019-10-17 09:20:46 -06001644 /*
1645 * Tell lockdep we inherited freeze protection from submission
1646 * thread.
1647 */
1648 if (req->flags & REQ_F_ISREG) {
1649 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001650
Jens Axboe491381ce2019-10-17 09:20:46 -06001651 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001652 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001653 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001654}
1655
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001656static inline void req_set_fail_links(struct io_kiocb *req)
1657{
1658 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1659 req->flags |= REQ_F_FAIL_LINK;
1660}
1661
Jens Axboeba816ad2019-09-28 11:36:45 -06001662static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001663{
Jens Axboe9adbd452019-12-20 08:45:55 -07001664 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001665
Jens Axboe491381ce2019-10-17 09:20:46 -06001666 if (kiocb->ki_flags & IOCB_WRITE)
1667 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001668
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001669 if (res != req->result)
1670 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001671 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001672}
1673
1674static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1675{
Jens Axboe9adbd452019-12-20 08:45:55 -07001676 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001677
1678 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001679 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001680}
1681
Jens Axboeba816ad2019-09-28 11:36:45 -06001682static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1683{
Jens Axboe9adbd452019-12-20 08:45:55 -07001684 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001685 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001686
1687 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001688 io_put_req_find_next(req, &nxt);
1689
1690 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001691}
1692
Jens Axboedef596e2019-01-09 08:59:42 -07001693static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1694{
Jens Axboe9adbd452019-12-20 08:45:55 -07001695 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001696
Jens Axboe491381ce2019-10-17 09:20:46 -06001697 if (kiocb->ki_flags & IOCB_WRITE)
1698 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001699
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001700 if (res != req->result)
1701 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001702 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001703 if (res != -EAGAIN)
1704 req->flags |= REQ_F_IOPOLL_COMPLETED;
1705}
1706
1707/*
1708 * After the iocb has been issued, it's safe to be found on the poll list.
1709 * Adding the kiocb to the list AFTER submission ensures that we don't
1710 * find it from a io_iopoll_getevents() thread before the issuer is done
1711 * accessing the kiocb cookie.
1712 */
1713static void io_iopoll_req_issued(struct io_kiocb *req)
1714{
1715 struct io_ring_ctx *ctx = req->ctx;
1716
1717 /*
1718 * Track whether we have multiple files in our lists. This will impact
1719 * how we do polling eventually, not spinning if we're on potentially
1720 * different devices.
1721 */
1722 if (list_empty(&ctx->poll_list)) {
1723 ctx->poll_multi_file = false;
1724 } else if (!ctx->poll_multi_file) {
1725 struct io_kiocb *list_req;
1726
1727 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1728 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001729 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001730 ctx->poll_multi_file = true;
1731 }
1732
1733 /*
1734 * For fast devices, IO may have already completed. If it has, add
1735 * it to the front so we find it first.
1736 */
1737 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1738 list_add(&req->list, &ctx->poll_list);
1739 else
1740 list_add_tail(&req->list, &ctx->poll_list);
1741}
1742
Jens Axboe3d6770f2019-04-13 11:50:54 -06001743static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001744{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001745 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001746 int diff = state->has_refs - state->used_refs;
1747
1748 if (diff)
1749 fput_many(state->file, diff);
1750 state->file = NULL;
1751 }
1752}
1753
1754/*
1755 * Get as many references to a file as we have IOs left in this submission,
1756 * assuming most submissions are for one file, or at least that each file
1757 * has more than one submission.
1758 */
1759static struct file *io_file_get(struct io_submit_state *state, int fd)
1760{
1761 if (!state)
1762 return fget(fd);
1763
1764 if (state->file) {
1765 if (state->fd == fd) {
1766 state->used_refs++;
1767 state->ios_left--;
1768 return state->file;
1769 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001770 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001771 }
1772 state->file = fget_many(fd, state->ios_left);
1773 if (!state->file)
1774 return NULL;
1775
1776 state->fd = fd;
1777 state->has_refs = state->ios_left;
1778 state->used_refs = 1;
1779 state->ios_left--;
1780 return state->file;
1781}
1782
Jens Axboe2b188cc2019-01-07 10:46:33 -07001783/*
1784 * If we tracked the file through the SCM inflight mechanism, we could support
1785 * any file. For now, just ensure that anything potentially problematic is done
1786 * inline.
1787 */
1788static bool io_file_supports_async(struct file *file)
1789{
1790 umode_t mode = file_inode(file)->i_mode;
1791
Jens Axboe10d59342019-12-09 20:16:22 -07001792 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001793 return true;
1794 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1795 return true;
1796
1797 return false;
1798}
1799
Jens Axboe3529d8c2019-12-19 18:24:38 -07001800static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1801 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001802{
Jens Axboedef596e2019-01-09 08:59:42 -07001803 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001804 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001805 unsigned ioprio;
1806 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001807
Jens Axboe09bb8392019-03-13 12:39:28 -06001808 if (!req->file)
1809 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001810
Jens Axboe491381ce2019-10-17 09:20:46 -06001811 if (S_ISREG(file_inode(req->file)->i_mode))
1812 req->flags |= REQ_F_ISREG;
1813
Jens Axboe2b188cc2019-01-07 10:46:33 -07001814 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001815 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1816 req->flags |= REQ_F_CUR_POS;
1817 kiocb->ki_pos = req->file->f_pos;
1818 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001819 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1820 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1821
1822 ioprio = READ_ONCE(sqe->ioprio);
1823 if (ioprio) {
1824 ret = ioprio_check_cap(ioprio);
1825 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001826 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001827
1828 kiocb->ki_ioprio = ioprio;
1829 } else
1830 kiocb->ki_ioprio = get_current_ioprio();
1831
1832 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1833 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001834 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001835
1836 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001837 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1838 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001839 req->flags |= REQ_F_NOWAIT;
1840
1841 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001842 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001843
Jens Axboedef596e2019-01-09 08:59:42 -07001844 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001845 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1846 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001847 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001848
Jens Axboedef596e2019-01-09 08:59:42 -07001849 kiocb->ki_flags |= IOCB_HIPRI;
1850 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001851 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001852 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001853 if (kiocb->ki_flags & IOCB_HIPRI)
1854 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001855 kiocb->ki_complete = io_complete_rw;
1856 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001857
Jens Axboe3529d8c2019-12-19 18:24:38 -07001858 req->rw.addr = READ_ONCE(sqe->addr);
1859 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001860 /* we own ->private, reuse it for the buffer index */
1861 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001862 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001863 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001864}
1865
1866static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1867{
1868 switch (ret) {
1869 case -EIOCBQUEUED:
1870 break;
1871 case -ERESTARTSYS:
1872 case -ERESTARTNOINTR:
1873 case -ERESTARTNOHAND:
1874 case -ERESTART_RESTARTBLOCK:
1875 /*
1876 * We can't just restart the syscall, since previously
1877 * submitted sqes may already be in progress. Just fail this
1878 * IO with EINTR.
1879 */
1880 ret = -EINTR;
1881 /* fall through */
1882 default:
1883 kiocb->ki_complete(kiocb, ret, 0);
1884 }
1885}
1886
Jens Axboeba816ad2019-09-28 11:36:45 -06001887static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1888 bool in_async)
1889{
Jens Axboeba042912019-12-25 16:33:42 -07001890 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1891
1892 if (req->flags & REQ_F_CUR_POS)
1893 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001894 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001895 *nxt = __io_complete_rw(kiocb, ret);
1896 else
1897 io_rw_done(kiocb, ret);
1898}
1899
Jens Axboe9adbd452019-12-20 08:45:55 -07001900static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001901 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001902{
Jens Axboe9adbd452019-12-20 08:45:55 -07001903 struct io_ring_ctx *ctx = req->ctx;
1904 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001905 struct io_mapped_ubuf *imu;
1906 unsigned index, buf_index;
1907 size_t offset;
1908 u64 buf_addr;
1909
1910 /* attempt to use fixed buffers without having provided iovecs */
1911 if (unlikely(!ctx->user_bufs))
1912 return -EFAULT;
1913
Jens Axboe9adbd452019-12-20 08:45:55 -07001914 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001915 if (unlikely(buf_index >= ctx->nr_user_bufs))
1916 return -EFAULT;
1917
1918 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1919 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001920 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001921
1922 /* overflow */
1923 if (buf_addr + len < buf_addr)
1924 return -EFAULT;
1925 /* not inside the mapped region */
1926 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1927 return -EFAULT;
1928
1929 /*
1930 * May not be a start of buffer, set size appropriately
1931 * and advance us to the beginning.
1932 */
1933 offset = buf_addr - imu->ubuf;
1934 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001935
1936 if (offset) {
1937 /*
1938 * Don't use iov_iter_advance() here, as it's really slow for
1939 * using the latter parts of a big fixed buffer - it iterates
1940 * over each segment manually. We can cheat a bit here, because
1941 * we know that:
1942 *
1943 * 1) it's a BVEC iter, we set it up
1944 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1945 * first and last bvec
1946 *
1947 * So just find our index, and adjust the iterator afterwards.
1948 * If the offset is within the first bvec (or the whole first
1949 * bvec, just use iov_iter_advance(). This makes it easier
1950 * since we can just skip the first segment, which may not
1951 * be PAGE_SIZE aligned.
1952 */
1953 const struct bio_vec *bvec = imu->bvec;
1954
1955 if (offset <= bvec->bv_len) {
1956 iov_iter_advance(iter, offset);
1957 } else {
1958 unsigned long seg_skip;
1959
1960 /* skip first vec */
1961 offset -= bvec->bv_len;
1962 seg_skip = 1 + (offset >> PAGE_SHIFT);
1963
1964 iter->bvec = bvec + seg_skip;
1965 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001966 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001967 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001968 }
1969 }
1970
Jens Axboe5e559562019-11-13 16:12:46 -07001971 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001972}
1973
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001974static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1975 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001976{
Jens Axboe9adbd452019-12-20 08:45:55 -07001977 void __user *buf = u64_to_user_ptr(req->rw.addr);
1978 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001979 u8 opcode;
1980
Jens Axboed625c6e2019-12-17 19:53:05 -07001981 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001982 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001983 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001984 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001985 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001986
Jens Axboe9adbd452019-12-20 08:45:55 -07001987 /* buffer index only valid with fixed read/write */
1988 if (req->rw.kiocb.private)
1989 return -EINVAL;
1990
Jens Axboe3a6820f2019-12-22 15:19:35 -07001991 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1992 ssize_t ret;
1993 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1994 *iovec = NULL;
1995 return ret;
1996 }
1997
Jens Axboef67676d2019-12-02 11:03:47 -07001998 if (req->io) {
1999 struct io_async_rw *iorw = &req->io->rw;
2000
2001 *iovec = iorw->iov;
2002 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2003 if (iorw->iov == iorw->fast_iov)
2004 *iovec = NULL;
2005 return iorw->size;
2006 }
2007
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002008 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002009 return -EFAULT;
2010
2011#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002012 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002013 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2014 iovec, iter);
2015#endif
2016
2017 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2018}
2019
Jens Axboe32960612019-09-23 11:05:34 -06002020/*
2021 * For files that don't have ->read_iter() and ->write_iter(), handle them
2022 * by looping over ->read() or ->write() manually.
2023 */
2024static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2025 struct iov_iter *iter)
2026{
2027 ssize_t ret = 0;
2028
2029 /*
2030 * Don't support polled IO through this interface, and we can't
2031 * support non-blocking either. For the latter, this just causes
2032 * the kiocb to be handled from an async context.
2033 */
2034 if (kiocb->ki_flags & IOCB_HIPRI)
2035 return -EOPNOTSUPP;
2036 if (kiocb->ki_flags & IOCB_NOWAIT)
2037 return -EAGAIN;
2038
2039 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002040 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002041 ssize_t nr;
2042
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002043 if (!iov_iter_is_bvec(iter)) {
2044 iovec = iov_iter_iovec(iter);
2045 } else {
2046 /* fixed buffers import bvec */
2047 iovec.iov_base = kmap(iter->bvec->bv_page)
2048 + iter->iov_offset;
2049 iovec.iov_len = min(iter->count,
2050 iter->bvec->bv_len - iter->iov_offset);
2051 }
2052
Jens Axboe32960612019-09-23 11:05:34 -06002053 if (rw == READ) {
2054 nr = file->f_op->read(file, iovec.iov_base,
2055 iovec.iov_len, &kiocb->ki_pos);
2056 } else {
2057 nr = file->f_op->write(file, iovec.iov_base,
2058 iovec.iov_len, &kiocb->ki_pos);
2059 }
2060
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002061 if (iov_iter_is_bvec(iter))
2062 kunmap(iter->bvec->bv_page);
2063
Jens Axboe32960612019-09-23 11:05:34 -06002064 if (nr < 0) {
2065 if (!ret)
2066 ret = nr;
2067 break;
2068 }
2069 ret += nr;
2070 if (nr != iovec.iov_len)
2071 break;
2072 iov_iter_advance(iter, nr);
2073 }
2074
2075 return ret;
2076}
2077
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002078static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002079 struct iovec *iovec, struct iovec *fast_iov,
2080 struct iov_iter *iter)
2081{
2082 req->io->rw.nr_segs = iter->nr_segs;
2083 req->io->rw.size = io_size;
2084 req->io->rw.iov = iovec;
2085 if (!req->io->rw.iov) {
2086 req->io->rw.iov = req->io->rw.fast_iov;
2087 memcpy(req->io->rw.iov, fast_iov,
2088 sizeof(struct iovec) * iter->nr_segs);
2089 }
2090}
2091
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002092static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002093{
Jens Axboed3656342019-12-18 09:50:26 -07002094 if (!io_op_defs[req->opcode].async_ctx)
2095 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002096 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002097 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002098}
2099
2100static void io_rw_async(struct io_wq_work **workptr)
2101{
2102 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2103 struct iovec *iov = NULL;
2104
2105 if (req->io->rw.iov != req->io->rw.fast_iov)
2106 iov = req->io->rw.iov;
2107 io_wq_submit_work(workptr);
2108 kfree(iov);
2109}
2110
2111static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2112 struct iovec *iovec, struct iovec *fast_iov,
2113 struct iov_iter *iter)
2114{
Jens Axboe74566df2020-01-13 19:23:24 -07002115 if (req->opcode == IORING_OP_READ_FIXED ||
2116 req->opcode == IORING_OP_WRITE_FIXED)
2117 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002118 if (!req->io && io_alloc_async_ctx(req))
2119 return -ENOMEM;
2120
2121 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2122 req->work.func = io_rw_async;
2123 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002124}
2125
Jens Axboe3529d8c2019-12-19 18:24:38 -07002126static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2127 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002128{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002129 struct io_async_ctx *io;
2130 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002131 ssize_t ret;
2132
Jens Axboe3529d8c2019-12-19 18:24:38 -07002133 ret = io_prep_rw(req, sqe, force_nonblock);
2134 if (ret)
2135 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002136
Jens Axboe3529d8c2019-12-19 18:24:38 -07002137 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2138 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002139
Jens Axboe3529d8c2019-12-19 18:24:38 -07002140 if (!req->io)
2141 return 0;
2142
2143 io = req->io;
2144 io->rw.iov = io->rw.fast_iov;
2145 req->io = NULL;
2146 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2147 req->io = io;
2148 if (ret < 0)
2149 return ret;
2150
2151 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2152 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002153}
2154
Pavel Begunkov267bc902019-11-07 01:41:08 +03002155static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002156 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002157{
2158 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002159 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002160 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002161 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002162 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002163
Jens Axboe3529d8c2019-12-19 18:24:38 -07002164 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002165 if (ret < 0)
2166 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002167
Jens Axboefd6c2e42019-12-18 12:19:41 -07002168 /* Ensure we clear previously set non-block flag */
2169 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002170 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002171
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002172 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002173 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002174 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002175 req->result = io_size;
2176
2177 /*
2178 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2179 * we know to async punt it even if it was opened O_NONBLOCK
2180 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002181 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002182 req->flags |= REQ_F_MUST_PUNT;
2183 goto copy_iov;
2184 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002185
Jens Axboe31b51512019-01-18 22:56:34 -07002186 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002187 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002188 if (!ret) {
2189 ssize_t ret2;
2190
Jens Axboe9adbd452019-12-20 08:45:55 -07002191 if (req->file->f_op->read_iter)
2192 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002193 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002194 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002195
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002196 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002197 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002198 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002199 } else {
2200copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002201 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002202 inline_vecs, &iter);
2203 if (ret)
2204 goto out_free;
2205 return -EAGAIN;
2206 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002207 }
Jens Axboef67676d2019-12-02 11:03:47 -07002208out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002209 if (!io_wq_current_is_worker())
2210 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002211 return ret;
2212}
2213
Jens Axboe3529d8c2019-12-19 18:24:38 -07002214static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2215 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002216{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002217 struct io_async_ctx *io;
2218 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002219 ssize_t ret;
2220
Jens Axboe3529d8c2019-12-19 18:24:38 -07002221 ret = io_prep_rw(req, sqe, force_nonblock);
2222 if (ret)
2223 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002224
Jens Axboe3529d8c2019-12-19 18:24:38 -07002225 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2226 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002227
Jens Axboe3529d8c2019-12-19 18:24:38 -07002228 if (!req->io)
2229 return 0;
2230
2231 io = req->io;
2232 io->rw.iov = io->rw.fast_iov;
2233 req->io = NULL;
2234 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2235 req->io = io;
2236 if (ret < 0)
2237 return ret;
2238
2239 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2240 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002241}
2242
Pavel Begunkov267bc902019-11-07 01:41:08 +03002243static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002244 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002245{
2246 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002247 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002248 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002249 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002250 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002251
Jens Axboe3529d8c2019-12-19 18:24:38 -07002252 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002253 if (ret < 0)
2254 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002255
Jens Axboefd6c2e42019-12-18 12:19:41 -07002256 /* Ensure we clear previously set non-block flag */
2257 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002258 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002259
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002260 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002261 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002262 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002263 req->result = io_size;
2264
2265 /*
2266 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2267 * we know to async punt it even if it was opened O_NONBLOCK
2268 */
2269 if (force_nonblock && !io_file_supports_async(req->file)) {
2270 req->flags |= REQ_F_MUST_PUNT;
2271 goto copy_iov;
2272 }
2273
Jens Axboe10d59342019-12-09 20:16:22 -07002274 /* file path doesn't support NOWAIT for non-direct_IO */
2275 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2276 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002277 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002278
Jens Axboe31b51512019-01-18 22:56:34 -07002279 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002280 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002281 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002282 ssize_t ret2;
2283
Jens Axboe2b188cc2019-01-07 10:46:33 -07002284 /*
2285 * Open-code file_start_write here to grab freeze protection,
2286 * which will be released by another thread in
2287 * io_complete_rw(). Fool lockdep by telling it the lock got
2288 * released so that it doesn't complain about the held lock when
2289 * we return to userspace.
2290 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002291 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002292 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002293 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002294 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002295 SB_FREEZE_WRITE);
2296 }
2297 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002298
Jens Axboe9adbd452019-12-20 08:45:55 -07002299 if (req->file->f_op->write_iter)
2300 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002301 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002302 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002303 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002304 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002305 } else {
2306copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002307 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002308 inline_vecs, &iter);
2309 if (ret)
2310 goto out_free;
2311 return -EAGAIN;
2312 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002313 }
Jens Axboe31b51512019-01-18 22:56:34 -07002314out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002315 if (!io_wq_current_is_worker())
2316 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002317 return ret;
2318}
2319
2320/*
2321 * IORING_OP_NOP just posts a completion event, nothing else.
2322 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002323static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002324{
2325 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002326
Jens Axboedef596e2019-01-09 08:59:42 -07002327 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2328 return -EINVAL;
2329
Jens Axboe78e19bb2019-11-06 15:21:34 -07002330 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002331 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002332 return 0;
2333}
2334
Jens Axboe3529d8c2019-12-19 18:24:38 -07002335static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002336{
Jens Axboe6b063142019-01-10 22:13:58 -07002337 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002338
Jens Axboe09bb8392019-03-13 12:39:28 -06002339 if (!req->file)
2340 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002341
Jens Axboe6b063142019-01-10 22:13:58 -07002342 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002343 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002344 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002345 return -EINVAL;
2346
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002347 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2348 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2349 return -EINVAL;
2350
2351 req->sync.off = READ_ONCE(sqe->off);
2352 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002353 return 0;
2354}
2355
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002356static bool io_req_cancelled(struct io_kiocb *req)
2357{
2358 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2359 req_set_fail_links(req);
2360 io_cqring_add_event(req, -ECANCELED);
2361 io_put_req(req);
2362 return true;
2363 }
2364
2365 return false;
2366}
2367
Jens Axboe78912932020-01-14 22:09:06 -07002368static void io_link_work_cb(struct io_wq_work **workptr)
2369{
2370 struct io_wq_work *work = *workptr;
2371 struct io_kiocb *link = work->data;
2372
2373 io_queue_linked_timeout(link);
2374 work->func = io_wq_submit_work;
2375}
2376
2377static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2378{
2379 struct io_kiocb *link;
2380
2381 io_prep_async_work(nxt, &link);
2382 *workptr = &nxt->work;
2383 if (link) {
2384 nxt->work.flags |= IO_WQ_WORK_CB;
2385 nxt->work.func = io_link_work_cb;
2386 nxt->work.data = link;
2387 }
2388}
2389
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002390static void io_fsync_finish(struct io_wq_work **workptr)
2391{
2392 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2393 loff_t end = req->sync.off + req->sync.len;
2394 struct io_kiocb *nxt = NULL;
2395 int ret;
2396
2397 if (io_req_cancelled(req))
2398 return;
2399
Jens Axboe9adbd452019-12-20 08:45:55 -07002400 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002401 end > 0 ? end : LLONG_MAX,
2402 req->sync.flags & IORING_FSYNC_DATASYNC);
2403 if (ret < 0)
2404 req_set_fail_links(req);
2405 io_cqring_add_event(req, ret);
2406 io_put_req_find_next(req, &nxt);
2407 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002408 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002409}
2410
Jens Axboefc4df992019-12-10 14:38:45 -07002411static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2412 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002413{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002414 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002415
2416 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002417 if (force_nonblock) {
2418 io_put_req(req);
2419 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002420 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002421 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002422
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002423 work = old_work = &req->work;
2424 io_fsync_finish(&work);
2425 if (work && work != old_work)
2426 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002427 return 0;
2428}
2429
Jens Axboed63d1b52019-12-10 10:38:56 -07002430static void io_fallocate_finish(struct io_wq_work **workptr)
2431{
2432 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2433 struct io_kiocb *nxt = NULL;
2434 int ret;
2435
2436 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2437 req->sync.len);
2438 if (ret < 0)
2439 req_set_fail_links(req);
2440 io_cqring_add_event(req, ret);
2441 io_put_req_find_next(req, &nxt);
2442 if (nxt)
2443 io_wq_assign_next(workptr, nxt);
2444}
2445
2446static int io_fallocate_prep(struct io_kiocb *req,
2447 const struct io_uring_sqe *sqe)
2448{
2449 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2450 return -EINVAL;
2451
2452 req->sync.off = READ_ONCE(sqe->off);
2453 req->sync.len = READ_ONCE(sqe->addr);
2454 req->sync.mode = READ_ONCE(sqe->len);
2455 return 0;
2456}
2457
2458static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2459 bool force_nonblock)
2460{
2461 struct io_wq_work *work, *old_work;
2462
2463 /* fallocate always requiring blocking context */
2464 if (force_nonblock) {
2465 io_put_req(req);
2466 req->work.func = io_fallocate_finish;
2467 return -EAGAIN;
2468 }
2469
2470 work = old_work = &req->work;
2471 io_fallocate_finish(&work);
2472 if (work && work != old_work)
2473 *nxt = container_of(work, struct io_kiocb, work);
2474
2475 return 0;
2476}
2477
Jens Axboe15b71ab2019-12-11 11:20:36 -07002478static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2479{
Jens Axboef8748882020-01-08 17:47:02 -07002480 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002481 int ret;
2482
2483 if (sqe->ioprio || sqe->buf_index)
2484 return -EINVAL;
2485
2486 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002487 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002488 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002489 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002490
Jens Axboef8748882020-01-08 17:47:02 -07002491 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002492 if (IS_ERR(req->open.filename)) {
2493 ret = PTR_ERR(req->open.filename);
2494 req->open.filename = NULL;
2495 return ret;
2496 }
2497
2498 return 0;
2499}
2500
Jens Axboecebdb982020-01-08 17:59:24 -07002501static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2502{
2503 struct open_how __user *how;
2504 const char __user *fname;
2505 size_t len;
2506 int ret;
2507
2508 if (sqe->ioprio || sqe->buf_index)
2509 return -EINVAL;
2510
2511 req->open.dfd = READ_ONCE(sqe->fd);
2512 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2513 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2514 len = READ_ONCE(sqe->len);
2515
2516 if (len < OPEN_HOW_SIZE_VER0)
2517 return -EINVAL;
2518
2519 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2520 len);
2521 if (ret)
2522 return ret;
2523
2524 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2525 req->open.how.flags |= O_LARGEFILE;
2526
2527 req->open.filename = getname(fname);
2528 if (IS_ERR(req->open.filename)) {
2529 ret = PTR_ERR(req->open.filename);
2530 req->open.filename = NULL;
2531 return ret;
2532 }
2533
2534 return 0;
2535}
2536
2537static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2538 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002539{
2540 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002541 struct file *file;
2542 int ret;
2543
2544 if (force_nonblock) {
2545 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2546 return -EAGAIN;
2547 }
2548
Jens Axboecebdb982020-01-08 17:59:24 -07002549 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002550 if (ret)
2551 goto err;
2552
Jens Axboecebdb982020-01-08 17:59:24 -07002553 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002554 if (ret < 0)
2555 goto err;
2556
2557 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2558 if (IS_ERR(file)) {
2559 put_unused_fd(ret);
2560 ret = PTR_ERR(file);
2561 } else {
2562 fsnotify_open(file);
2563 fd_install(ret, file);
2564 }
2565err:
2566 putname(req->open.filename);
2567 if (ret < 0)
2568 req_set_fail_links(req);
2569 io_cqring_add_event(req, ret);
2570 io_put_req_find_next(req, nxt);
2571 return 0;
2572}
2573
Jens Axboecebdb982020-01-08 17:59:24 -07002574static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2575 bool force_nonblock)
2576{
2577 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2578 return io_openat2(req, nxt, force_nonblock);
2579}
2580
Jens Axboec1ca7572019-12-25 22:18:28 -07002581static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2582{
2583#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2584 if (sqe->ioprio || sqe->buf_index || sqe->off)
2585 return -EINVAL;
2586
2587 req->madvise.addr = READ_ONCE(sqe->addr);
2588 req->madvise.len = READ_ONCE(sqe->len);
2589 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2590 return 0;
2591#else
2592 return -EOPNOTSUPP;
2593#endif
2594}
2595
2596static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2597 bool force_nonblock)
2598{
2599#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2600 struct io_madvise *ma = &req->madvise;
2601 int ret;
2602
2603 if (force_nonblock)
2604 return -EAGAIN;
2605
2606 ret = do_madvise(ma->addr, ma->len, ma->advice);
2607 if (ret < 0)
2608 req_set_fail_links(req);
2609 io_cqring_add_event(req, ret);
2610 io_put_req_find_next(req, nxt);
2611 return 0;
2612#else
2613 return -EOPNOTSUPP;
2614#endif
2615}
2616
Jens Axboe4840e412019-12-25 22:03:45 -07002617static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2618{
2619 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2620 return -EINVAL;
2621
2622 req->fadvise.offset = READ_ONCE(sqe->off);
2623 req->fadvise.len = READ_ONCE(sqe->len);
2624 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2625 return 0;
2626}
2627
2628static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2629 bool force_nonblock)
2630{
2631 struct io_fadvise *fa = &req->fadvise;
2632 int ret;
2633
2634 /* DONTNEED may block, others _should_ not */
2635 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2636 return -EAGAIN;
2637
2638 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2639 if (ret < 0)
2640 req_set_fail_links(req);
2641 io_cqring_add_event(req, ret);
2642 io_put_req_find_next(req, nxt);
2643 return 0;
2644}
2645
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002646static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2647{
Jens Axboef8748882020-01-08 17:47:02 -07002648 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002649 unsigned lookup_flags;
2650 int ret;
2651
2652 if (sqe->ioprio || sqe->buf_index)
2653 return -EINVAL;
2654
2655 req->open.dfd = READ_ONCE(sqe->fd);
2656 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002657 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002658 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002659 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002660
Jens Axboec12cedf2020-01-08 17:41:21 -07002661 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002662 return -EINVAL;
2663
Jens Axboef8748882020-01-08 17:47:02 -07002664 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002665 if (IS_ERR(req->open.filename)) {
2666 ret = PTR_ERR(req->open.filename);
2667 req->open.filename = NULL;
2668 return ret;
2669 }
2670
2671 return 0;
2672}
2673
2674static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2675 bool force_nonblock)
2676{
2677 struct io_open *ctx = &req->open;
2678 unsigned lookup_flags;
2679 struct path path;
2680 struct kstat stat;
2681 int ret;
2682
2683 if (force_nonblock)
2684 return -EAGAIN;
2685
Jens Axboec12cedf2020-01-08 17:41:21 -07002686 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002687 return -EINVAL;
2688
2689retry:
2690 /* filename_lookup() drops it, keep a reference */
2691 ctx->filename->refcnt++;
2692
2693 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2694 NULL);
2695 if (ret)
2696 goto err;
2697
Jens Axboec12cedf2020-01-08 17:41:21 -07002698 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002699 path_put(&path);
2700 if (retry_estale(ret, lookup_flags)) {
2701 lookup_flags |= LOOKUP_REVAL;
2702 goto retry;
2703 }
2704 if (!ret)
2705 ret = cp_statx(&stat, ctx->buffer);
2706err:
2707 putname(ctx->filename);
2708 if (ret < 0)
2709 req_set_fail_links(req);
2710 io_cqring_add_event(req, ret);
2711 io_put_req_find_next(req, nxt);
2712 return 0;
2713}
2714
Jens Axboeb5dba592019-12-11 14:02:38 -07002715static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2716{
2717 /*
2718 * If we queue this for async, it must not be cancellable. That would
2719 * leave the 'file' in an undeterminate state.
2720 */
2721 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2722
2723 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2724 sqe->rw_flags || sqe->buf_index)
2725 return -EINVAL;
2726 if (sqe->flags & IOSQE_FIXED_FILE)
2727 return -EINVAL;
2728
2729 req->close.fd = READ_ONCE(sqe->fd);
2730 if (req->file->f_op == &io_uring_fops ||
2731 req->close.fd == req->ring_fd)
2732 return -EBADF;
2733
2734 return 0;
2735}
2736
2737static void io_close_finish(struct io_wq_work **workptr)
2738{
2739 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2740 struct io_kiocb *nxt = NULL;
2741
2742 /* Invoked with files, we need to do the close */
2743 if (req->work.files) {
2744 int ret;
2745
2746 ret = filp_close(req->close.put_file, req->work.files);
2747 if (ret < 0) {
2748 req_set_fail_links(req);
2749 }
2750 io_cqring_add_event(req, ret);
2751 }
2752
2753 fput(req->close.put_file);
2754
2755 /* we bypassed the re-issue, drop the submission reference */
2756 io_put_req(req);
2757 io_put_req_find_next(req, &nxt);
2758 if (nxt)
2759 io_wq_assign_next(workptr, nxt);
2760}
2761
2762static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2763 bool force_nonblock)
2764{
2765 int ret;
2766
2767 req->close.put_file = NULL;
2768 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2769 if (ret < 0)
2770 return ret;
2771
2772 /* if the file has a flush method, be safe and punt to async */
2773 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2774 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2775 goto eagain;
2776 }
2777
2778 /*
2779 * No ->flush(), safely close from here and just punt the
2780 * fput() to async context.
2781 */
2782 ret = filp_close(req->close.put_file, current->files);
2783
2784 if (ret < 0)
2785 req_set_fail_links(req);
2786 io_cqring_add_event(req, ret);
2787
2788 if (io_wq_current_is_worker()) {
2789 struct io_wq_work *old_work, *work;
2790
2791 old_work = work = &req->work;
2792 io_close_finish(&work);
2793 if (work && work != old_work)
2794 *nxt = container_of(work, struct io_kiocb, work);
2795 return 0;
2796 }
2797
2798eagain:
2799 req->work.func = io_close_finish;
2800 return -EAGAIN;
2801}
2802
Jens Axboe3529d8c2019-12-19 18:24:38 -07002803static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002804{
2805 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002806
2807 if (!req->file)
2808 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002809
2810 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2811 return -EINVAL;
2812 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2813 return -EINVAL;
2814
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002815 req->sync.off = READ_ONCE(sqe->off);
2816 req->sync.len = READ_ONCE(sqe->len);
2817 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002818 return 0;
2819}
2820
2821static void io_sync_file_range_finish(struct io_wq_work **workptr)
2822{
2823 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2824 struct io_kiocb *nxt = NULL;
2825 int ret;
2826
2827 if (io_req_cancelled(req))
2828 return;
2829
Jens Axboe9adbd452019-12-20 08:45:55 -07002830 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002831 req->sync.flags);
2832 if (ret < 0)
2833 req_set_fail_links(req);
2834 io_cqring_add_event(req, ret);
2835 io_put_req_find_next(req, &nxt);
2836 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002837 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002838}
2839
Jens Axboefc4df992019-12-10 14:38:45 -07002840static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002841 bool force_nonblock)
2842{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002843 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002844
2845 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002846 if (force_nonblock) {
2847 io_put_req(req);
2848 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002849 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002850 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002851
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002852 work = old_work = &req->work;
2853 io_sync_file_range_finish(&work);
2854 if (work && work != old_work)
2855 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002856 return 0;
2857}
2858
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002859#if defined(CONFIG_NET)
2860static void io_sendrecv_async(struct io_wq_work **workptr)
2861{
2862 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2863 struct iovec *iov = NULL;
2864
2865 if (req->io->rw.iov != req->io->rw.fast_iov)
2866 iov = req->io->msg.iov;
2867 io_wq_submit_work(workptr);
2868 kfree(iov);
2869}
2870#endif
2871
Jens Axboe3529d8c2019-12-19 18:24:38 -07002872static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002873{
Jens Axboe03b12302019-12-02 18:50:25 -07002874#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002875 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002876 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002877
Jens Axboee47293f2019-12-20 08:58:21 -07002878 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2879 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002880 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002881
Jens Axboefddafac2020-01-04 20:19:44 -07002882 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002883 return 0;
2884
Jens Axboed9688562019-12-09 19:35:20 -07002885 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002886 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002887 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002888#else
Jens Axboee47293f2019-12-20 08:58:21 -07002889 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002890#endif
2891}
2892
Jens Axboefc4df992019-12-10 14:38:45 -07002893static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2894 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002895{
2896#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002897 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002898 struct socket *sock;
2899 int ret;
2900
2901 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2902 return -EINVAL;
2903
2904 sock = sock_from_file(req->file, &ret);
2905 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002906 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002907 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002908 unsigned flags;
2909
Jens Axboe03b12302019-12-02 18:50:25 -07002910 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002911 kmsg = &req->io->msg;
2912 kmsg->msg.msg_name = &addr;
2913 /* if iov is set, it's allocated already */
2914 if (!kmsg->iov)
2915 kmsg->iov = kmsg->fast_iov;
2916 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002917 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002918 struct io_sr_msg *sr = &req->sr_msg;
2919
Jens Axboe0b416c32019-12-15 10:57:46 -07002920 kmsg = &io.msg;
2921 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002922
2923 io.msg.iov = io.msg.fast_iov;
2924 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2925 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002926 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002927 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002928 }
2929
Jens Axboee47293f2019-12-20 08:58:21 -07002930 flags = req->sr_msg.msg_flags;
2931 if (flags & MSG_DONTWAIT)
2932 req->flags |= REQ_F_NOWAIT;
2933 else if (force_nonblock)
2934 flags |= MSG_DONTWAIT;
2935
Jens Axboe0b416c32019-12-15 10:57:46 -07002936 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002937 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002938 if (req->io)
2939 return -EAGAIN;
2940 if (io_alloc_async_ctx(req))
2941 return -ENOMEM;
2942 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2943 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002944 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002945 }
2946 if (ret == -ERESTARTSYS)
2947 ret = -EINTR;
2948 }
2949
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002950 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002951 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002952 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002953 if (ret < 0)
2954 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002955 io_put_req_find_next(req, nxt);
2956 return 0;
2957#else
2958 return -EOPNOTSUPP;
2959#endif
2960}
2961
Jens Axboefddafac2020-01-04 20:19:44 -07002962static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2963 bool force_nonblock)
2964{
2965#if defined(CONFIG_NET)
2966 struct socket *sock;
2967 int ret;
2968
2969 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2970 return -EINVAL;
2971
2972 sock = sock_from_file(req->file, &ret);
2973 if (sock) {
2974 struct io_sr_msg *sr = &req->sr_msg;
2975 struct msghdr msg;
2976 struct iovec iov;
2977 unsigned flags;
2978
2979 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
2980 &msg.msg_iter);
2981 if (ret)
2982 return ret;
2983
2984 msg.msg_name = NULL;
2985 msg.msg_control = NULL;
2986 msg.msg_controllen = 0;
2987 msg.msg_namelen = 0;
2988
2989 flags = req->sr_msg.msg_flags;
2990 if (flags & MSG_DONTWAIT)
2991 req->flags |= REQ_F_NOWAIT;
2992 else if (force_nonblock)
2993 flags |= MSG_DONTWAIT;
2994
2995 ret = __sys_sendmsg_sock(sock, &msg, flags);
2996 if (force_nonblock && ret == -EAGAIN)
2997 return -EAGAIN;
2998 if (ret == -ERESTARTSYS)
2999 ret = -EINTR;
3000 }
3001
3002 io_cqring_add_event(req, ret);
3003 if (ret < 0)
3004 req_set_fail_links(req);
3005 io_put_req_find_next(req, nxt);
3006 return 0;
3007#else
3008 return -EOPNOTSUPP;
3009#endif
3010}
3011
Jens Axboe3529d8c2019-12-19 18:24:38 -07003012static int io_recvmsg_prep(struct io_kiocb *req,
3013 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003014{
3015#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003016 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003017 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07003018
Jens Axboe3529d8c2019-12-19 18:24:38 -07003019 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3020 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
3021
Jens Axboefddafac2020-01-04 20:19:44 -07003022 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003023 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003024
Jens Axboed9688562019-12-09 19:35:20 -07003025 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003026 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003027 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003028#else
Jens Axboee47293f2019-12-20 08:58:21 -07003029 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003030#endif
3031}
3032
Jens Axboefc4df992019-12-10 14:38:45 -07003033static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3034 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003035{
3036#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003037 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003038 struct socket *sock;
3039 int ret;
3040
3041 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3042 return -EINVAL;
3043
3044 sock = sock_from_file(req->file, &ret);
3045 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003046 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003047 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003048 unsigned flags;
3049
Jens Axboe03b12302019-12-02 18:50:25 -07003050 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003051 kmsg = &req->io->msg;
3052 kmsg->msg.msg_name = &addr;
3053 /* if iov is set, it's allocated already */
3054 if (!kmsg->iov)
3055 kmsg->iov = kmsg->fast_iov;
3056 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003057 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003058 struct io_sr_msg *sr = &req->sr_msg;
3059
Jens Axboe0b416c32019-12-15 10:57:46 -07003060 kmsg = &io.msg;
3061 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003062
3063 io.msg.iov = io.msg.fast_iov;
3064 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3065 sr->msg_flags, &io.msg.uaddr,
3066 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003067 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003068 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003069 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003070
Jens Axboee47293f2019-12-20 08:58:21 -07003071 flags = req->sr_msg.msg_flags;
3072 if (flags & MSG_DONTWAIT)
3073 req->flags |= REQ_F_NOWAIT;
3074 else if (force_nonblock)
3075 flags |= MSG_DONTWAIT;
3076
3077 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3078 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003079 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003080 if (req->io)
3081 return -EAGAIN;
3082 if (io_alloc_async_ctx(req))
3083 return -ENOMEM;
3084 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3085 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003086 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003087 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003088 if (ret == -ERESTARTSYS)
3089 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003090 }
3091
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003092 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003093 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003094 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003095 if (ret < 0)
3096 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003097 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003098 return 0;
3099#else
3100 return -EOPNOTSUPP;
3101#endif
3102}
3103
Jens Axboefddafac2020-01-04 20:19:44 -07003104static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3105 bool force_nonblock)
3106{
3107#if defined(CONFIG_NET)
3108 struct socket *sock;
3109 int ret;
3110
3111 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3112 return -EINVAL;
3113
3114 sock = sock_from_file(req->file, &ret);
3115 if (sock) {
3116 struct io_sr_msg *sr = &req->sr_msg;
3117 struct msghdr msg;
3118 struct iovec iov;
3119 unsigned flags;
3120
3121 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3122 &msg.msg_iter);
3123 if (ret)
3124 return ret;
3125
3126 msg.msg_name = NULL;
3127 msg.msg_control = NULL;
3128 msg.msg_controllen = 0;
3129 msg.msg_namelen = 0;
3130 msg.msg_iocb = NULL;
3131 msg.msg_flags = 0;
3132
3133 flags = req->sr_msg.msg_flags;
3134 if (flags & MSG_DONTWAIT)
3135 req->flags |= REQ_F_NOWAIT;
3136 else if (force_nonblock)
3137 flags |= MSG_DONTWAIT;
3138
3139 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3140 if (force_nonblock && ret == -EAGAIN)
3141 return -EAGAIN;
3142 if (ret == -ERESTARTSYS)
3143 ret = -EINTR;
3144 }
3145
3146 io_cqring_add_event(req, ret);
3147 if (ret < 0)
3148 req_set_fail_links(req);
3149 io_put_req_find_next(req, nxt);
3150 return 0;
3151#else
3152 return -EOPNOTSUPP;
3153#endif
3154}
3155
3156
Jens Axboe3529d8c2019-12-19 18:24:38 -07003157static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003158{
3159#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003160 struct io_accept *accept = &req->accept;
3161
Jens Axboe17f2fe32019-10-17 14:42:58 -06003162 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3163 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003164 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003165 return -EINVAL;
3166
Jens Axboed55e5f52019-12-11 16:12:15 -07003167 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3168 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003169 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003170 return 0;
3171#else
3172 return -EOPNOTSUPP;
3173#endif
3174}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003175
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003176#if defined(CONFIG_NET)
3177static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3178 bool force_nonblock)
3179{
3180 struct io_accept *accept = &req->accept;
3181 unsigned file_flags;
3182 int ret;
3183
3184 file_flags = force_nonblock ? O_NONBLOCK : 0;
3185 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3186 accept->addr_len, accept->flags);
3187 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003188 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003189 if (ret == -ERESTARTSYS)
3190 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003191 if (ret < 0)
3192 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003193 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003194 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003195 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003196}
3197
3198static void io_accept_finish(struct io_wq_work **workptr)
3199{
3200 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3201 struct io_kiocb *nxt = NULL;
3202
3203 if (io_req_cancelled(req))
3204 return;
3205 __io_accept(req, &nxt, false);
3206 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003207 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003208}
3209#endif
3210
3211static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3212 bool force_nonblock)
3213{
3214#if defined(CONFIG_NET)
3215 int ret;
3216
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003217 ret = __io_accept(req, nxt, force_nonblock);
3218 if (ret == -EAGAIN && force_nonblock) {
3219 req->work.func = io_accept_finish;
3220 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3221 io_put_req(req);
3222 return -EAGAIN;
3223 }
3224 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003225#else
3226 return -EOPNOTSUPP;
3227#endif
3228}
3229
Jens Axboe3529d8c2019-12-19 18:24:38 -07003230static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003231{
3232#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003233 struct io_connect *conn = &req->connect;
3234 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003235
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003236 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3237 return -EINVAL;
3238 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3239 return -EINVAL;
3240
Jens Axboe3529d8c2019-12-19 18:24:38 -07003241 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3242 conn->addr_len = READ_ONCE(sqe->addr2);
3243
3244 if (!io)
3245 return 0;
3246
3247 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003248 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003249#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003250 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003251#endif
3252}
3253
Jens Axboefc4df992019-12-10 14:38:45 -07003254static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3255 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003256{
3257#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003258 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003259 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003260 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003261
Jens Axboef499a022019-12-02 16:28:46 -07003262 if (req->io) {
3263 io = req->io;
3264 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003265 ret = move_addr_to_kernel(req->connect.addr,
3266 req->connect.addr_len,
3267 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003268 if (ret)
3269 goto out;
3270 io = &__io;
3271 }
3272
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003273 file_flags = force_nonblock ? O_NONBLOCK : 0;
3274
3275 ret = __sys_connect_file(req->file, &io->connect.address,
3276 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003277 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003278 if (req->io)
3279 return -EAGAIN;
3280 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003281 ret = -ENOMEM;
3282 goto out;
3283 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003284 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003285 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003286 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003287 if (ret == -ERESTARTSYS)
3288 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003289out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003290 if (ret < 0)
3291 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003292 io_cqring_add_event(req, ret);
3293 io_put_req_find_next(req, nxt);
3294 return 0;
3295#else
3296 return -EOPNOTSUPP;
3297#endif
3298}
3299
Jens Axboe221c5eb2019-01-17 09:41:58 -07003300static void io_poll_remove_one(struct io_kiocb *req)
3301{
3302 struct io_poll_iocb *poll = &req->poll;
3303
3304 spin_lock(&poll->head->lock);
3305 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003306 if (!list_empty(&poll->wait.entry)) {
3307 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003308 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003309 }
3310 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003311 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003312}
3313
3314static void io_poll_remove_all(struct io_ring_ctx *ctx)
3315{
Jens Axboe78076bb2019-12-04 19:56:40 -07003316 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003317 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003318 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003319
3320 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003321 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3322 struct hlist_head *list;
3323
3324 list = &ctx->cancel_hash[i];
3325 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3326 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003327 }
3328 spin_unlock_irq(&ctx->completion_lock);
3329}
3330
Jens Axboe47f46762019-11-09 17:43:02 -07003331static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3332{
Jens Axboe78076bb2019-12-04 19:56:40 -07003333 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003334 struct io_kiocb *req;
3335
Jens Axboe78076bb2019-12-04 19:56:40 -07003336 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3337 hlist_for_each_entry(req, list, hash_node) {
3338 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003339 io_poll_remove_one(req);
3340 return 0;
3341 }
Jens Axboe47f46762019-11-09 17:43:02 -07003342 }
3343
3344 return -ENOENT;
3345}
3346
Jens Axboe3529d8c2019-12-19 18:24:38 -07003347static int io_poll_remove_prep(struct io_kiocb *req,
3348 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003349{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003350 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3351 return -EINVAL;
3352 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3353 sqe->poll_events)
3354 return -EINVAL;
3355
Jens Axboe0969e782019-12-17 18:40:57 -07003356 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003357 return 0;
3358}
3359
3360/*
3361 * Find a running poll command that matches one specified in sqe->addr,
3362 * and remove it if found.
3363 */
3364static int io_poll_remove(struct io_kiocb *req)
3365{
3366 struct io_ring_ctx *ctx = req->ctx;
3367 u64 addr;
3368 int ret;
3369
Jens Axboe0969e782019-12-17 18:40:57 -07003370 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003371 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003372 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003373 spin_unlock_irq(&ctx->completion_lock);
3374
Jens Axboe78e19bb2019-11-06 15:21:34 -07003375 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003376 if (ret < 0)
3377 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003378 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003379 return 0;
3380}
3381
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003382static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003383{
Jackie Liua197f662019-11-08 08:09:12 -07003384 struct io_ring_ctx *ctx = req->ctx;
3385
Jens Axboe8c838782019-03-12 15:48:16 -06003386 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003387 if (error)
3388 io_cqring_fill_event(req, error);
3389 else
3390 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003391 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003392}
3393
Jens Axboe561fb042019-10-24 07:25:42 -06003394static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003395{
Jens Axboe561fb042019-10-24 07:25:42 -06003396 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003397 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3398 struct io_poll_iocb *poll = &req->poll;
3399 struct poll_table_struct pt = { ._key = poll->events };
3400 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003401 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003402 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003403 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003404
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003405 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003406 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003407 ret = -ECANCELED;
3408 } else if (READ_ONCE(poll->canceled)) {
3409 ret = -ECANCELED;
3410 }
Jens Axboe561fb042019-10-24 07:25:42 -06003411
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003412 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003413 mask = vfs_poll(poll->file, &pt) & poll->events;
3414
3415 /*
3416 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3417 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3418 * synchronize with them. In the cancellation case the list_del_init
3419 * itself is not actually needed, but harmless so we keep it in to
3420 * avoid further branches in the fast path.
3421 */
3422 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003423 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003424 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003425 spin_unlock_irq(&ctx->completion_lock);
3426 return;
3427 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003428 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003429 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003430 spin_unlock_irq(&ctx->completion_lock);
3431
Jens Axboe8c838782019-03-12 15:48:16 -06003432 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003433
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003434 if (ret < 0)
3435 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003436 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003437 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003438 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003439}
3440
Jens Axboee94f1412019-12-19 12:06:02 -07003441static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3442{
Jens Axboee94f1412019-12-19 12:06:02 -07003443 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003444 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003445
Jens Axboec6ca97b302019-12-28 12:11:08 -07003446 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003447 spin_lock_irq(&ctx->completion_lock);
3448 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3449 hash_del(&req->hash_node);
3450 io_poll_complete(req, req->result, 0);
3451
Jens Axboe8237e042019-12-28 10:48:22 -07003452 if (refcount_dec_and_test(&req->refs) &&
3453 !io_req_multi_free(&rb, req)) {
3454 req->flags |= REQ_F_COMP_LOCKED;
3455 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003456 }
3457 }
3458 spin_unlock_irq(&ctx->completion_lock);
3459
3460 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003461 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003462}
3463
3464static void io_poll_flush(struct io_wq_work **workptr)
3465{
3466 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3467 struct llist_node *nodes;
3468
3469 nodes = llist_del_all(&req->ctx->poll_llist);
3470 if (nodes)
3471 __io_poll_flush(req->ctx, nodes);
3472}
3473
Jens Axboe221c5eb2019-01-17 09:41:58 -07003474static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3475 void *key)
3476{
Jens Axboee9444752019-11-26 15:02:04 -07003477 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003478 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3479 struct io_ring_ctx *ctx = req->ctx;
3480 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003481
3482 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003483 if (mask && !(mask & poll->events))
3484 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003485
Jens Axboe392edb42019-12-09 17:52:20 -07003486 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003487
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003488 /*
3489 * Run completion inline if we can. We're using trylock here because
3490 * we are violating the completion_lock -> poll wq lock ordering.
3491 * If we have a link timeout we're going to need the completion_lock
3492 * for finalizing the request, mark us as having grabbed that already.
3493 */
Jens Axboee94f1412019-12-19 12:06:02 -07003494 if (mask) {
3495 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003496
Jens Axboee94f1412019-12-19 12:06:02 -07003497 if (llist_empty(&ctx->poll_llist) &&
3498 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3499 hash_del(&req->hash_node);
3500 io_poll_complete(req, mask, 0);
3501 req->flags |= REQ_F_COMP_LOCKED;
3502 io_put_req(req);
3503 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3504
3505 io_cqring_ev_posted(ctx);
3506 req = NULL;
3507 } else {
3508 req->result = mask;
3509 req->llist_node.next = NULL;
3510 /* if the list wasn't empty, we're done */
3511 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3512 req = NULL;
3513 else
3514 req->work.func = io_poll_flush;
3515 }
Jens Axboe8c838782019-03-12 15:48:16 -06003516 }
Jens Axboee94f1412019-12-19 12:06:02 -07003517 if (req)
3518 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003519
Jens Axboe221c5eb2019-01-17 09:41:58 -07003520 return 1;
3521}
3522
3523struct io_poll_table {
3524 struct poll_table_struct pt;
3525 struct io_kiocb *req;
3526 int error;
3527};
3528
3529static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3530 struct poll_table_struct *p)
3531{
3532 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3533
3534 if (unlikely(pt->req->poll.head)) {
3535 pt->error = -EINVAL;
3536 return;
3537 }
3538
3539 pt->error = 0;
3540 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003541 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003542}
3543
Jens Axboeeac406c2019-11-14 12:09:58 -07003544static void io_poll_req_insert(struct io_kiocb *req)
3545{
3546 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003547 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003548
Jens Axboe78076bb2019-12-04 19:56:40 -07003549 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3550 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003551}
3552
Jens Axboe3529d8c2019-12-19 18:24:38 -07003553static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003554{
3555 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003556 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003557
3558 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3559 return -EINVAL;
3560 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3561 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003562 if (!poll->file)
3563 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003564
Jens Axboe221c5eb2019-01-17 09:41:58 -07003565 events = READ_ONCE(sqe->poll_events);
3566 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003567 return 0;
3568}
3569
3570static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3571{
3572 struct io_poll_iocb *poll = &req->poll;
3573 struct io_ring_ctx *ctx = req->ctx;
3574 struct io_poll_table ipt;
3575 bool cancel = false;
3576 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003577
3578 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003579 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003580
Jens Axboe221c5eb2019-01-17 09:41:58 -07003581 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003582 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003583 poll->canceled = false;
3584
3585 ipt.pt._qproc = io_poll_queue_proc;
3586 ipt.pt._key = poll->events;
3587 ipt.req = req;
3588 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3589
3590 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003591 INIT_LIST_HEAD(&poll->wait.entry);
3592 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3593 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003594
Jens Axboe36703242019-07-25 10:20:18 -06003595 INIT_LIST_HEAD(&req->list);
3596
Jens Axboe221c5eb2019-01-17 09:41:58 -07003597 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003598
3599 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003600 if (likely(poll->head)) {
3601 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003602 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003603 if (ipt.error)
3604 cancel = true;
3605 ipt.error = 0;
3606 mask = 0;
3607 }
3608 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003609 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003610 else if (cancel)
3611 WRITE_ONCE(poll->canceled, true);
3612 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003613 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003614 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003615 }
Jens Axboe8c838782019-03-12 15:48:16 -06003616 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003617 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003618 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003619 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003620 spin_unlock_irq(&ctx->completion_lock);
3621
Jens Axboe8c838782019-03-12 15:48:16 -06003622 if (mask) {
3623 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003624 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003625 }
Jens Axboe8c838782019-03-12 15:48:16 -06003626 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003627}
3628
Jens Axboe5262f562019-09-17 12:26:57 -06003629static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3630{
Jens Axboead8a48a2019-11-15 08:49:11 -07003631 struct io_timeout_data *data = container_of(timer,
3632 struct io_timeout_data, timer);
3633 struct io_kiocb *req = data->req;
3634 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003635 unsigned long flags;
3636
Jens Axboe5262f562019-09-17 12:26:57 -06003637 atomic_inc(&ctx->cq_timeouts);
3638
3639 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003640 /*
Jens Axboe11365042019-10-16 09:08:32 -06003641 * We could be racing with timeout deletion. If the list is empty,
3642 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003643 */
Jens Axboe842f9612019-10-29 12:34:10 -06003644 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003645 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003646
Jens Axboe11365042019-10-16 09:08:32 -06003647 /*
3648 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003649 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003650 * pointer will be increased, otherwise other timeout reqs may
3651 * return in advance without waiting for enough wait_nr.
3652 */
3653 prev = req;
3654 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3655 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003656 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003657 }
Jens Axboe842f9612019-10-29 12:34:10 -06003658
Jens Axboe78e19bb2019-11-06 15:21:34 -07003659 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003660 io_commit_cqring(ctx);
3661 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3662
3663 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003664 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003665 io_put_req(req);
3666 return HRTIMER_NORESTART;
3667}
3668
Jens Axboe47f46762019-11-09 17:43:02 -07003669static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3670{
3671 struct io_kiocb *req;
3672 int ret = -ENOENT;
3673
3674 list_for_each_entry(req, &ctx->timeout_list, list) {
3675 if (user_data == req->user_data) {
3676 list_del_init(&req->list);
3677 ret = 0;
3678 break;
3679 }
3680 }
3681
3682 if (ret == -ENOENT)
3683 return ret;
3684
Jens Axboe2d283902019-12-04 11:08:05 -07003685 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003686 if (ret == -1)
3687 return -EALREADY;
3688
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003689 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003690 io_cqring_fill_event(req, -ECANCELED);
3691 io_put_req(req);
3692 return 0;
3693}
3694
Jens Axboe3529d8c2019-12-19 18:24:38 -07003695static int io_timeout_remove_prep(struct io_kiocb *req,
3696 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003697{
Jens Axboeb29472e2019-12-17 18:50:29 -07003698 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3699 return -EINVAL;
3700 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3701 return -EINVAL;
3702
3703 req->timeout.addr = READ_ONCE(sqe->addr);
3704 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3705 if (req->timeout.flags)
3706 return -EINVAL;
3707
Jens Axboeb29472e2019-12-17 18:50:29 -07003708 return 0;
3709}
3710
Jens Axboe11365042019-10-16 09:08:32 -06003711/*
3712 * Remove or update an existing timeout command
3713 */
Jens Axboefc4df992019-12-10 14:38:45 -07003714static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003715{
3716 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003717 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003718
Jens Axboe11365042019-10-16 09:08:32 -06003719 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003720 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003721
Jens Axboe47f46762019-11-09 17:43:02 -07003722 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003723 io_commit_cqring(ctx);
3724 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003725 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003726 if (ret < 0)
3727 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003728 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003729 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003730}
3731
Jens Axboe3529d8c2019-12-19 18:24:38 -07003732static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003733 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003734{
Jens Axboead8a48a2019-11-15 08:49:11 -07003735 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003736 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003737
Jens Axboead8a48a2019-11-15 08:49:11 -07003738 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003739 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003740 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003741 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003742 if (sqe->off && is_timeout_link)
3743 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003744 flags = READ_ONCE(sqe->timeout_flags);
3745 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003746 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003747
Jens Axboe26a61672019-12-20 09:02:01 -07003748 req->timeout.count = READ_ONCE(sqe->off);
3749
Jens Axboe3529d8c2019-12-19 18:24:38 -07003750 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003751 return -ENOMEM;
3752
3753 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003754 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003755 req->flags |= REQ_F_TIMEOUT;
3756
3757 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003758 return -EFAULT;
3759
Jens Axboe11365042019-10-16 09:08:32 -06003760 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003761 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003762 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003763 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003764
Jens Axboead8a48a2019-11-15 08:49:11 -07003765 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3766 return 0;
3767}
3768
Jens Axboefc4df992019-12-10 14:38:45 -07003769static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003770{
3771 unsigned count;
3772 struct io_ring_ctx *ctx = req->ctx;
3773 struct io_timeout_data *data;
3774 struct list_head *entry;
3775 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003776
Jens Axboe2d283902019-12-04 11:08:05 -07003777 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003778
Jens Axboe5262f562019-09-17 12:26:57 -06003779 /*
3780 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003781 * timeout event to be satisfied. If it isn't set, then this is
3782 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003783 */
Jens Axboe26a61672019-12-20 09:02:01 -07003784 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003785 if (!count) {
3786 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3787 spin_lock_irq(&ctx->completion_lock);
3788 entry = ctx->timeout_list.prev;
3789 goto add;
3790 }
Jens Axboe5262f562019-09-17 12:26:57 -06003791
3792 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003793 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003794
3795 /*
3796 * Insertion sort, ensuring the first entry in the list is always
3797 * the one we need first.
3798 */
Jens Axboe5262f562019-09-17 12:26:57 -06003799 spin_lock_irq(&ctx->completion_lock);
3800 list_for_each_prev(entry, &ctx->timeout_list) {
3801 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003802 unsigned nxt_sq_head;
3803 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003804 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003805
Jens Axboe93bd25b2019-11-11 23:34:31 -07003806 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3807 continue;
3808
yangerkun5da0fb12019-10-15 21:59:29 +08003809 /*
3810 * Since cached_sq_head + count - 1 can overflow, use type long
3811 * long to store it.
3812 */
3813 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003814 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3815 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003816
3817 /*
3818 * cached_sq_head may overflow, and it will never overflow twice
3819 * once there is some timeout req still be valid.
3820 */
3821 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003822 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003823
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003824 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003825 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003826
3827 /*
3828 * Sequence of reqs after the insert one and itself should
3829 * be adjusted because each timeout req consumes a slot.
3830 */
3831 span++;
3832 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003833 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003834 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003835add:
Jens Axboe5262f562019-09-17 12:26:57 -06003836 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003837 data->timer.function = io_timeout_fn;
3838 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003839 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003840 return 0;
3841}
3842
Jens Axboe62755e32019-10-28 21:49:21 -06003843static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003844{
Jens Axboe62755e32019-10-28 21:49:21 -06003845 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003846
Jens Axboe62755e32019-10-28 21:49:21 -06003847 return req->user_data == (unsigned long) data;
3848}
3849
Jens Axboee977d6d2019-11-05 12:39:45 -07003850static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003851{
Jens Axboe62755e32019-10-28 21:49:21 -06003852 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003853 int ret = 0;
3854
Jens Axboe62755e32019-10-28 21:49:21 -06003855 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3856 switch (cancel_ret) {
3857 case IO_WQ_CANCEL_OK:
3858 ret = 0;
3859 break;
3860 case IO_WQ_CANCEL_RUNNING:
3861 ret = -EALREADY;
3862 break;
3863 case IO_WQ_CANCEL_NOTFOUND:
3864 ret = -ENOENT;
3865 break;
3866 }
3867
Jens Axboee977d6d2019-11-05 12:39:45 -07003868 return ret;
3869}
3870
Jens Axboe47f46762019-11-09 17:43:02 -07003871static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3872 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003873 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003874{
3875 unsigned long flags;
3876 int ret;
3877
3878 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3879 if (ret != -ENOENT) {
3880 spin_lock_irqsave(&ctx->completion_lock, flags);
3881 goto done;
3882 }
3883
3884 spin_lock_irqsave(&ctx->completion_lock, flags);
3885 ret = io_timeout_cancel(ctx, sqe_addr);
3886 if (ret != -ENOENT)
3887 goto done;
3888 ret = io_poll_cancel(ctx, sqe_addr);
3889done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003890 if (!ret)
3891 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003892 io_cqring_fill_event(req, ret);
3893 io_commit_cqring(ctx);
3894 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3895 io_cqring_ev_posted(ctx);
3896
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003897 if (ret < 0)
3898 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003899 io_put_req_find_next(req, nxt);
3900}
3901
Jens Axboe3529d8c2019-12-19 18:24:38 -07003902static int io_async_cancel_prep(struct io_kiocb *req,
3903 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003904{
Jens Axboefbf23842019-12-17 18:45:56 -07003905 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003906 return -EINVAL;
3907 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3908 sqe->cancel_flags)
3909 return -EINVAL;
3910
Jens Axboefbf23842019-12-17 18:45:56 -07003911 req->cancel.addr = READ_ONCE(sqe->addr);
3912 return 0;
3913}
3914
3915static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3916{
3917 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003918
3919 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003920 return 0;
3921}
3922
Jens Axboe05f3fb32019-12-09 11:22:50 -07003923static int io_files_update_prep(struct io_kiocb *req,
3924 const struct io_uring_sqe *sqe)
3925{
3926 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3927 return -EINVAL;
3928
3929 req->files_update.offset = READ_ONCE(sqe->off);
3930 req->files_update.nr_args = READ_ONCE(sqe->len);
3931 if (!req->files_update.nr_args)
3932 return -EINVAL;
3933 req->files_update.arg = READ_ONCE(sqe->addr);
3934 return 0;
3935}
3936
3937static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3938{
3939 struct io_ring_ctx *ctx = req->ctx;
3940 struct io_uring_files_update up;
3941 int ret;
3942
3943 if (force_nonblock) {
3944 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3945 return -EAGAIN;
3946 }
3947
3948 up.offset = req->files_update.offset;
3949 up.fds = req->files_update.arg;
3950
3951 mutex_lock(&ctx->uring_lock);
3952 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3953 mutex_unlock(&ctx->uring_lock);
3954
3955 if (ret < 0)
3956 req_set_fail_links(req);
3957 io_cqring_add_event(req, ret);
3958 io_put_req(req);
3959 return 0;
3960}
3961
Jens Axboe3529d8c2019-12-19 18:24:38 -07003962static int io_req_defer_prep(struct io_kiocb *req,
3963 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003964{
Jens Axboee7815732019-12-17 19:45:06 -07003965 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003966
Jens Axboed625c6e2019-12-17 19:53:05 -07003967 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003968 case IORING_OP_NOP:
3969 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003970 case IORING_OP_READV:
3971 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003972 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003973 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003974 break;
3975 case IORING_OP_WRITEV:
3976 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003977 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003978 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003979 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003980 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003981 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003982 break;
3983 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003984 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003985 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003986 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003987 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003988 break;
3989 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003990 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003991 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003992 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003993 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003994 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003995 break;
3996 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003997 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003998 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003999 break;
Jens Axboef499a022019-12-02 16:28:46 -07004000 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004001 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004002 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004003 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004004 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004005 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004006 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004007 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004008 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004009 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004010 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004011 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004012 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004013 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004014 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004015 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004016 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004017 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004018 case IORING_OP_FALLOCATE:
4019 ret = io_fallocate_prep(req, sqe);
4020 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004021 case IORING_OP_OPENAT:
4022 ret = io_openat_prep(req, sqe);
4023 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004024 case IORING_OP_CLOSE:
4025 ret = io_close_prep(req, sqe);
4026 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004027 case IORING_OP_FILES_UPDATE:
4028 ret = io_files_update_prep(req, sqe);
4029 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004030 case IORING_OP_STATX:
4031 ret = io_statx_prep(req, sqe);
4032 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004033 case IORING_OP_FADVISE:
4034 ret = io_fadvise_prep(req, sqe);
4035 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004036 case IORING_OP_MADVISE:
4037 ret = io_madvise_prep(req, sqe);
4038 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004039 case IORING_OP_OPENAT2:
4040 ret = io_openat2_prep(req, sqe);
4041 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004042 default:
Jens Axboee7815732019-12-17 19:45:06 -07004043 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4044 req->opcode);
4045 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004046 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004047 }
4048
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004049 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004050}
4051
Jens Axboe3529d8c2019-12-19 18:24:38 -07004052static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004053{
Jackie Liua197f662019-11-08 08:09:12 -07004054 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004055 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004056
Bob Liu9d858b22019-11-13 18:06:25 +08004057 /* Still need defer if there is pending req in defer list. */
4058 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004059 return 0;
4060
Jens Axboe3529d8c2019-12-19 18:24:38 -07004061 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004062 return -EAGAIN;
4063
Jens Axboe3529d8c2019-12-19 18:24:38 -07004064 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004065 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004066 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004067
Jens Axboede0617e2019-04-06 21:51:27 -06004068 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004069 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004070 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004071 return 0;
4072 }
4073
Jens Axboe915967f2019-11-21 09:01:20 -07004074 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004075 list_add_tail(&req->list, &ctx->defer_list);
4076 spin_unlock_irq(&ctx->completion_lock);
4077 return -EIOCBQUEUED;
4078}
4079
Jens Axboe3529d8c2019-12-19 18:24:38 -07004080static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4081 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004082{
Jackie Liua197f662019-11-08 08:09:12 -07004083 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004084 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004085
Jens Axboed625c6e2019-12-17 19:53:05 -07004086 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004087 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004088 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004089 break;
4090 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004091 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004092 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004093 if (sqe) {
4094 ret = io_read_prep(req, sqe, force_nonblock);
4095 if (ret < 0)
4096 break;
4097 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004098 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004099 break;
4100 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004101 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004102 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004103 if (sqe) {
4104 ret = io_write_prep(req, sqe, force_nonblock);
4105 if (ret < 0)
4106 break;
4107 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004108 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004109 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004110 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004111 if (sqe) {
4112 ret = io_prep_fsync(req, sqe);
4113 if (ret < 0)
4114 break;
4115 }
Jens Axboefc4df992019-12-10 14:38:45 -07004116 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004117 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004118 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004119 if (sqe) {
4120 ret = io_poll_add_prep(req, sqe);
4121 if (ret)
4122 break;
4123 }
Jens Axboefc4df992019-12-10 14:38:45 -07004124 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004125 break;
4126 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004127 if (sqe) {
4128 ret = io_poll_remove_prep(req, sqe);
4129 if (ret < 0)
4130 break;
4131 }
Jens Axboefc4df992019-12-10 14:38:45 -07004132 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004133 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004134 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004135 if (sqe) {
4136 ret = io_prep_sfr(req, sqe);
4137 if (ret < 0)
4138 break;
4139 }
Jens Axboefc4df992019-12-10 14:38:45 -07004140 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004141 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004142 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004143 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004144 if (sqe) {
4145 ret = io_sendmsg_prep(req, sqe);
4146 if (ret < 0)
4147 break;
4148 }
Jens Axboefddafac2020-01-04 20:19:44 -07004149 if (req->opcode == IORING_OP_SENDMSG)
4150 ret = io_sendmsg(req, nxt, force_nonblock);
4151 else
4152 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004153 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004154 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004155 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004156 if (sqe) {
4157 ret = io_recvmsg_prep(req, sqe);
4158 if (ret)
4159 break;
4160 }
Jens Axboefddafac2020-01-04 20:19:44 -07004161 if (req->opcode == IORING_OP_RECVMSG)
4162 ret = io_recvmsg(req, nxt, force_nonblock);
4163 else
4164 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004165 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004166 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004167 if (sqe) {
4168 ret = io_timeout_prep(req, sqe, false);
4169 if (ret)
4170 break;
4171 }
Jens Axboefc4df992019-12-10 14:38:45 -07004172 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004173 break;
Jens Axboe11365042019-10-16 09:08:32 -06004174 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004175 if (sqe) {
4176 ret = io_timeout_remove_prep(req, sqe);
4177 if (ret)
4178 break;
4179 }
Jens Axboefc4df992019-12-10 14:38:45 -07004180 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004181 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004182 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004183 if (sqe) {
4184 ret = io_accept_prep(req, sqe);
4185 if (ret)
4186 break;
4187 }
Jens Axboefc4df992019-12-10 14:38:45 -07004188 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004189 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004190 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004191 if (sqe) {
4192 ret = io_connect_prep(req, sqe);
4193 if (ret)
4194 break;
4195 }
Jens Axboefc4df992019-12-10 14:38:45 -07004196 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004197 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004198 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004199 if (sqe) {
4200 ret = io_async_cancel_prep(req, sqe);
4201 if (ret)
4202 break;
4203 }
Jens Axboefc4df992019-12-10 14:38:45 -07004204 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004205 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004206 case IORING_OP_FALLOCATE:
4207 if (sqe) {
4208 ret = io_fallocate_prep(req, sqe);
4209 if (ret)
4210 break;
4211 }
4212 ret = io_fallocate(req, nxt, force_nonblock);
4213 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004214 case IORING_OP_OPENAT:
4215 if (sqe) {
4216 ret = io_openat_prep(req, sqe);
4217 if (ret)
4218 break;
4219 }
4220 ret = io_openat(req, nxt, force_nonblock);
4221 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004222 case IORING_OP_CLOSE:
4223 if (sqe) {
4224 ret = io_close_prep(req, sqe);
4225 if (ret)
4226 break;
4227 }
4228 ret = io_close(req, nxt, force_nonblock);
4229 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004230 case IORING_OP_FILES_UPDATE:
4231 if (sqe) {
4232 ret = io_files_update_prep(req, sqe);
4233 if (ret)
4234 break;
4235 }
4236 ret = io_files_update(req, force_nonblock);
4237 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004238 case IORING_OP_STATX:
4239 if (sqe) {
4240 ret = io_statx_prep(req, sqe);
4241 if (ret)
4242 break;
4243 }
4244 ret = io_statx(req, nxt, force_nonblock);
4245 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004246 case IORING_OP_FADVISE:
4247 if (sqe) {
4248 ret = io_fadvise_prep(req, sqe);
4249 if (ret)
4250 break;
4251 }
4252 ret = io_fadvise(req, nxt, force_nonblock);
4253 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004254 case IORING_OP_MADVISE:
4255 if (sqe) {
4256 ret = io_madvise_prep(req, sqe);
4257 if (ret)
4258 break;
4259 }
4260 ret = io_madvise(req, nxt, force_nonblock);
4261 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004262 case IORING_OP_OPENAT2:
4263 if (sqe) {
4264 ret = io_openat2_prep(req, sqe);
4265 if (ret)
4266 break;
4267 }
4268 ret = io_openat2(req, nxt, force_nonblock);
4269 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004270 default:
4271 ret = -EINVAL;
4272 break;
4273 }
4274
Jens Axboedef596e2019-01-09 08:59:42 -07004275 if (ret)
4276 return ret;
4277
4278 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004279 const bool in_async = io_wq_current_is_worker();
4280
Jens Axboe9e645e112019-05-10 16:07:28 -06004281 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004282 return -EAGAIN;
4283
Jens Axboe11ba8202020-01-15 21:51:17 -07004284 /* workqueue context doesn't hold uring_lock, grab it now */
4285 if (in_async)
4286 mutex_lock(&ctx->uring_lock);
4287
Jens Axboedef596e2019-01-09 08:59:42 -07004288 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004289
4290 if (in_async)
4291 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004292 }
4293
4294 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004295}
4296
Jens Axboe561fb042019-10-24 07:25:42 -06004297static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004298{
Jens Axboe561fb042019-10-24 07:25:42 -06004299 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004300 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004301 struct io_kiocb *nxt = NULL;
4302 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004303
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004304 /* if NO_CANCEL is set, we must still run the work */
4305 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4306 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004307 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004308 }
Jens Axboe31b51512019-01-18 22:56:34 -07004309
Jens Axboe561fb042019-10-24 07:25:42 -06004310 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004311 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4312 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004313 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004314 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004315 /*
4316 * We can get EAGAIN for polled IO even though we're
4317 * forcing a sync submission from here, since we can't
4318 * wait for request slots on the block side.
4319 */
4320 if (ret != -EAGAIN)
4321 break;
4322 cond_resched();
4323 } while (1);
4324 }
Jens Axboe31b51512019-01-18 22:56:34 -07004325
Jens Axboe561fb042019-10-24 07:25:42 -06004326 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004327 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004328
Jens Axboe561fb042019-10-24 07:25:42 -06004329 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004330 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004331 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004332 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004333 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004334
Jens Axboe561fb042019-10-24 07:25:42 -06004335 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004336 if (!ret && nxt)
4337 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004338}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004339
Jens Axboe15b71ab2019-12-11 11:20:36 -07004340static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004341{
Jens Axboed3656342019-12-18 09:50:26 -07004342 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004343 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004344 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4345 return 0;
4346 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004347}
4348
Jens Axboe65e19f52019-10-26 07:20:21 -06004349static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4350 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004351{
Jens Axboe65e19f52019-10-26 07:20:21 -06004352 struct fixed_file_table *table;
4353
Jens Axboe05f3fb32019-12-09 11:22:50 -07004354 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4355 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004356}
4357
Jens Axboe3529d8c2019-12-19 18:24:38 -07004358static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4359 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004360{
Jackie Liua197f662019-11-08 08:09:12 -07004361 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004362 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004363 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004364
Jens Axboe3529d8c2019-12-19 18:24:38 -07004365 flags = READ_ONCE(sqe->flags);
4366 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004367
Jackie Liu4fe2c962019-09-09 20:50:40 +08004368 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06004369 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06004370
Jens Axboed3656342019-12-18 09:50:26 -07004371 if (!io_req_needs_file(req, fd))
4372 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004373
4374 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004375 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004376 (unsigned) fd >= ctx->nr_user_files))
4377 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004378 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004379 req->file = io_file_from_index(ctx, fd);
4380 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004381 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004382 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004383 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004384 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004385 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004386 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004387 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004388 req->file = io_file_get(state, fd);
4389 if (unlikely(!req->file))
4390 return -EBADF;
4391 }
4392
4393 return 0;
4394}
4395
Jackie Liua197f662019-11-08 08:09:12 -07004396static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004397{
Jens Axboefcb323c2019-10-24 12:39:47 -06004398 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004399 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004400
Jens Axboeb5dba592019-12-11 14:02:38 -07004401 if (!req->ring_file)
4402 return -EBADF;
4403
Jens Axboefcb323c2019-10-24 12:39:47 -06004404 rcu_read_lock();
4405 spin_lock_irq(&ctx->inflight_lock);
4406 /*
4407 * We use the f_ops->flush() handler to ensure that we can flush
4408 * out work accessing these files if the fd is closed. Check if
4409 * the fd has changed since we started down this path, and disallow
4410 * this operation if it has.
4411 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004412 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004413 list_add(&req->inflight_entry, &ctx->inflight_list);
4414 req->flags |= REQ_F_INFLIGHT;
4415 req->work.files = current->files;
4416 ret = 0;
4417 }
4418 spin_unlock_irq(&ctx->inflight_lock);
4419 rcu_read_unlock();
4420
4421 return ret;
4422}
4423
Jens Axboe2665abf2019-11-05 12:40:47 -07004424static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4425{
Jens Axboead8a48a2019-11-15 08:49:11 -07004426 struct io_timeout_data *data = container_of(timer,
4427 struct io_timeout_data, timer);
4428 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004429 struct io_ring_ctx *ctx = req->ctx;
4430 struct io_kiocb *prev = NULL;
4431 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004432
4433 spin_lock_irqsave(&ctx->completion_lock, flags);
4434
4435 /*
4436 * We don't expect the list to be empty, that will only happen if we
4437 * race with the completion of the linked work.
4438 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004439 if (!list_empty(&req->link_list)) {
4440 prev = list_entry(req->link_list.prev, struct io_kiocb,
4441 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004442 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004443 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004444 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4445 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004446 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004447 }
4448
4449 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4450
4451 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004452 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004453 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4454 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004455 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004456 } else {
4457 io_cqring_add_event(req, -ETIME);
4458 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004459 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004460 return HRTIMER_NORESTART;
4461}
4462
Jens Axboead8a48a2019-11-15 08:49:11 -07004463static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004464{
Jens Axboe76a46e02019-11-10 23:34:16 -07004465 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004466
Jens Axboe76a46e02019-11-10 23:34:16 -07004467 /*
4468 * If the list is now empty, then our linked request finished before
4469 * we got a chance to setup the timer
4470 */
4471 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004472 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004473 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004474
Jens Axboead8a48a2019-11-15 08:49:11 -07004475 data->timer.function = io_link_timeout_fn;
4476 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4477 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004478 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004479 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004480
Jens Axboe2665abf2019-11-05 12:40:47 -07004481 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004482 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004483}
4484
Jens Axboead8a48a2019-11-15 08:49:11 -07004485static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004486{
4487 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004488
Jens Axboe2665abf2019-11-05 12:40:47 -07004489 if (!(req->flags & REQ_F_LINK))
4490 return NULL;
4491
Pavel Begunkov44932332019-12-05 16:16:35 +03004492 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4493 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004494 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004495 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004496
Jens Axboe76a46e02019-11-10 23:34:16 -07004497 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004498 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004499}
4500
Jens Axboe3529d8c2019-12-19 18:24:38 -07004501static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004502{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004503 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004504 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004505 int ret;
4506
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004507again:
4508 linked_timeout = io_prep_linked_timeout(req);
4509
Jens Axboe3529d8c2019-12-19 18:24:38 -07004510 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004511
4512 /*
4513 * We async punt it if the file wasn't marked NOWAIT, or if the file
4514 * doesn't support non-blocking read/write attempts
4515 */
4516 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4517 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004518 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4519 ret = io_grab_files(req);
4520 if (ret)
4521 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004522 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004523
4524 /*
4525 * Queued up for async execution, worker will release
4526 * submit reference when the iocb is actually submitted.
4527 */
4528 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004529 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004530 }
Jens Axboee65ef562019-03-12 10:16:44 -06004531
Jens Axboefcb323c2019-10-24 12:39:47 -06004532err:
Jens Axboee65ef562019-03-12 10:16:44 -06004533 /* drop submission reference */
4534 io_put_req(req);
4535
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004536 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004537 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004538 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004539 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004540 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004541 }
4542
Jens Axboee65ef562019-03-12 10:16:44 -06004543 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004544 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004545 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004546 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004547 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004548 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004549done_req:
4550 if (nxt) {
4551 req = nxt;
4552 nxt = NULL;
4553 goto again;
4554 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004555}
4556
Jens Axboe3529d8c2019-12-19 18:24:38 -07004557static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004558{
4559 int ret;
4560
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004561 if (unlikely(req->ctx->drain_next)) {
4562 req->flags |= REQ_F_IO_DRAIN;
Jens Axboe69b3e542020-01-08 11:01:46 -07004563 req->ctx->drain_next = 0;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004564 }
Jens Axboe69b3e542020-01-08 11:01:46 -07004565 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK) != 0;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004566
Jens Axboe3529d8c2019-12-19 18:24:38 -07004567 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004568 if (ret) {
4569 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004570 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004571 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004572 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004573 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004574 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboece35a472019-12-17 08:04:44 -07004575 /*
4576 * Never try inline submit of IOSQE_ASYNC is set, go straight
4577 * to async execution.
4578 */
4579 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4580 io_queue_async_work(req);
4581 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004582 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004583 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004584}
4585
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004586static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004587{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004588 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004589 io_cqring_add_event(req, -ECANCELED);
4590 io_double_put_req(req);
4591 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004592 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004593}
4594
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004595#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004596 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004597
Jens Axboe3529d8c2019-12-19 18:24:38 -07004598static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4599 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004600{
Jackie Liua197f662019-11-08 08:09:12 -07004601 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004602 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004603 int ret;
4604
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004605 sqe_flags = READ_ONCE(sqe->flags);
4606
Jens Axboe9e645e112019-05-10 16:07:28 -06004607 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004608 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004609 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004610 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004611 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004612 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004613 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004614
Jens Axboe3529d8c2019-12-19 18:24:38 -07004615 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004616 if (unlikely(ret)) {
4617err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004618 io_cqring_add_event(req, ret);
4619 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004620 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004621 }
4622
Jens Axboe9e645e112019-05-10 16:07:28 -06004623 /*
4624 * If we already have a head request, queue this one for async
4625 * submittal once the head completes. If we don't have a head but
4626 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4627 * submitted sync once the chain is complete. If none of those
4628 * conditions are true (normal request), then just queue it.
4629 */
4630 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004631 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004632
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004633 if (sqe_flags & IOSQE_IO_DRAIN)
Pavel Begunkov9d763772019-12-17 02:22:07 +03004634 head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004635
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004636 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004637 req->flags |= REQ_F_HARDLINK;
4638
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004639 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004640 ret = -EAGAIN;
4641 goto err_req;
4642 }
4643
Jens Axboe3529d8c2019-12-19 18:24:38 -07004644 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004645 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004646 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004647 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004648 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004649 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004650 trace_io_uring_link(ctx, req, head);
4651 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004652
4653 /* last request of a link, enqueue the link */
4654 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4655 io_queue_link_head(head);
4656 *link = NULL;
4657 }
4658 } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004659 req->flags |= REQ_F_LINK;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004660 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004661 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004662
Jens Axboe9e645e112019-05-10 16:07:28 -06004663 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004664 ret = io_req_defer_prep(req, sqe);
4665 if (ret)
4666 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004667 *link = req;
4668 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004669 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004670 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004671
4672 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004673}
4674
Jens Axboe9a56a232019-01-09 09:06:50 -07004675/*
4676 * Batched submission is done, ensure local IO is flushed out.
4677 */
4678static void io_submit_state_end(struct io_submit_state *state)
4679{
4680 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004681 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004682 if (state->free_reqs)
4683 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4684 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004685}
4686
4687/*
4688 * Start submission side cache.
4689 */
4690static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004691 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004692{
4693 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004694 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004695 state->file = NULL;
4696 state->ios_left = max_ios;
4697}
4698
Jens Axboe2b188cc2019-01-07 10:46:33 -07004699static void io_commit_sqring(struct io_ring_ctx *ctx)
4700{
Hristo Venev75b28af2019-08-26 17:23:46 +00004701 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004702
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004703 /*
4704 * Ensure any loads from the SQEs are done at this point,
4705 * since once we write the new head, the application could
4706 * write new data to them.
4707 */
4708 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004709}
4710
4711/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004712 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004713 * that is mapped by userspace. This means that care needs to be taken to
4714 * ensure that reads are stable, as we cannot rely on userspace always
4715 * being a good citizen. If members of the sqe are validated and then later
4716 * used, it's important that those reads are done through READ_ONCE() to
4717 * prevent a re-load down the line.
4718 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004719static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4720 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004721{
Hristo Venev75b28af2019-08-26 17:23:46 +00004722 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004723 unsigned head;
4724
4725 /*
4726 * The cached sq head (or cq tail) serves two purposes:
4727 *
4728 * 1) allows us to batch the cost of updating the user visible
4729 * head updates.
4730 * 2) allows the kernel side to track the head on its own, even
4731 * though the application is the one updating it.
4732 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004733 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004734 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004735 /*
4736 * All io need record the previous position, if LINK vs DARIN,
4737 * it can be used to mark the position of the first IO in the
4738 * link list.
4739 */
4740 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004741 *sqe_ptr = &ctx->sq_sqes[head];
4742 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4743 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004744 ctx->cached_sq_head++;
4745 return true;
4746 }
4747
4748 /* drop invalid entries */
4749 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004750 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004751 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004752 return false;
4753}
4754
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004755static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004756 struct file *ring_file, int ring_fd,
4757 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004758{
4759 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004760 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004761 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004762 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004763
Jens Axboec4a2ed72019-11-21 21:01:26 -07004764 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004765 if (test_bit(0, &ctx->sq_check_overflow)) {
4766 if (!list_empty(&ctx->cq_overflow_list) &&
4767 !io_cqring_overflow_flush(ctx, false))
4768 return -EBUSY;
4769 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004770
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004771 /* make sure SQ entry isn't read before tail */
4772 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004773
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004774 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4775 return -EAGAIN;
4776
Jens Axboe6c271ce2019-01-10 11:22:30 -07004777 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004778 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004779 statep = &state;
4780 }
4781
4782 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004783 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004784 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004785
Pavel Begunkov196be952019-11-07 01:41:06 +03004786 req = io_get_req(ctx, statep);
4787 if (unlikely(!req)) {
4788 if (!submitted)
4789 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004790 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004791 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004792 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004793 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004794 break;
4795 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004796
Jens Axboed3656342019-12-18 09:50:26 -07004797 /* will complete beyond this point, count as submitted */
4798 submitted++;
4799
4800 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4801 io_cqring_add_event(req, -EINVAL);
4802 io_double_put_req(req);
4803 break;
4804 }
4805
4806 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004807 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4808 if (!mm_fault) {
4809 use_mm(ctx->sqo_mm);
4810 *mm = ctx->sqo_mm;
4811 }
4812 }
4813
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004814 req->ring_file = ring_file;
4815 req->ring_fd = ring_fd;
4816 req->has_user = *mm != NULL;
4817 req->in_async = async;
4818 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07004819 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
4820 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004821 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004822 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004823 }
4824
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004825 if (submitted != nr)
4826 percpu_ref_put_many(&ctx->refs, nr - submitted);
Jens Axboe9e645e112019-05-10 16:07:28 -06004827 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004828 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004829 if (statep)
4830 io_submit_state_end(&state);
4831
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004832 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4833 io_commit_sqring(ctx);
4834
Jens Axboe6c271ce2019-01-10 11:22:30 -07004835 return submitted;
4836}
4837
4838static int io_sq_thread(void *data)
4839{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004840 struct io_ring_ctx *ctx = data;
4841 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004842 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004843 mm_segment_t old_fs;
4844 DEFINE_WAIT(wait);
4845 unsigned inflight;
4846 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004847 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004848
Jens Axboe206aefd2019-11-07 18:27:42 -07004849 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004850
Jens Axboe6c271ce2019-01-10 11:22:30 -07004851 old_fs = get_fs();
4852 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004853 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004854
Jens Axboec1edbf52019-11-10 16:56:04 -07004855 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004856 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004857 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004858
4859 if (inflight) {
4860 unsigned nr_events = 0;
4861
4862 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004863 /*
4864 * inflight is the count of the maximum possible
4865 * entries we submitted, but it can be smaller
4866 * if we dropped some of them. If we don't have
4867 * poll entries available, then we know that we
4868 * have nothing left to poll for. Reset the
4869 * inflight count to zero in that case.
4870 */
4871 mutex_lock(&ctx->uring_lock);
4872 if (!list_empty(&ctx->poll_list))
4873 __io_iopoll_check(ctx, &nr_events, 0);
4874 else
4875 inflight = 0;
4876 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004877 } else {
4878 /*
4879 * Normal IO, just pretend everything completed.
4880 * We don't have to poll completions for that.
4881 */
4882 nr_events = inflight;
4883 }
4884
4885 inflight -= nr_events;
4886 if (!inflight)
4887 timeout = jiffies + ctx->sq_thread_idle;
4888 }
4889
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004890 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004891
4892 /*
4893 * If submit got -EBUSY, flag us as needing the application
4894 * to enter the kernel to reap and flush events.
4895 */
4896 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004897 /*
4898 * We're polling. If we're within the defined idle
4899 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004900 * to sleep. The exception is if we got EBUSY doing
4901 * more IO, we should wait for the application to
4902 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004903 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004904 if (inflight ||
4905 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004906 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004907 continue;
4908 }
4909
4910 /*
4911 * Drop cur_mm before scheduling, we can't hold it for
4912 * long periods (or over schedule()). Do this before
4913 * adding ourselves to the waitqueue, as the unuse/drop
4914 * may sleep.
4915 */
4916 if (cur_mm) {
4917 unuse_mm(cur_mm);
4918 mmput(cur_mm);
4919 cur_mm = NULL;
4920 }
4921
4922 prepare_to_wait(&ctx->sqo_wait, &wait,
4923 TASK_INTERRUPTIBLE);
4924
4925 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004926 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004927 /* make sure to read SQ tail after writing flags */
4928 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004929
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004930 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004931 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004932 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004933 finish_wait(&ctx->sqo_wait, &wait);
4934 break;
4935 }
4936 if (signal_pending(current))
4937 flush_signals(current);
4938 schedule();
4939 finish_wait(&ctx->sqo_wait, &wait);
4940
Hristo Venev75b28af2019-08-26 17:23:46 +00004941 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004942 continue;
4943 }
4944 finish_wait(&ctx->sqo_wait, &wait);
4945
Hristo Venev75b28af2019-08-26 17:23:46 +00004946 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004947 }
4948
Jens Axboe8a4955f2019-12-09 14:52:35 -07004949 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004950 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004951 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004952 if (ret > 0)
4953 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004954 }
4955
4956 set_fs(old_fs);
4957 if (cur_mm) {
4958 unuse_mm(cur_mm);
4959 mmput(cur_mm);
4960 }
Jens Axboe181e4482019-11-25 08:52:30 -07004961 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004962
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004963 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004964
Jens Axboe6c271ce2019-01-10 11:22:30 -07004965 return 0;
4966}
4967
Jens Axboebda52162019-09-24 13:47:15 -06004968struct io_wait_queue {
4969 struct wait_queue_entry wq;
4970 struct io_ring_ctx *ctx;
4971 unsigned to_wait;
4972 unsigned nr_timeouts;
4973};
4974
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004975static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004976{
4977 struct io_ring_ctx *ctx = iowq->ctx;
4978
4979 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004980 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004981 * started waiting. For timeouts, we always want to return to userspace,
4982 * regardless of event count.
4983 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004984 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004985 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4986}
4987
4988static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4989 int wake_flags, void *key)
4990{
4991 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4992 wq);
4993
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004994 /* use noflush == true, as we can't safely rely on locking context */
4995 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004996 return -1;
4997
4998 return autoremove_wake_function(curr, mode, wake_flags, key);
4999}
5000
Jens Axboe2b188cc2019-01-07 10:46:33 -07005001/*
5002 * Wait until events become available, if we don't already have some. The
5003 * application must reap them itself, as they reside on the shared cq ring.
5004 */
5005static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5006 const sigset_t __user *sig, size_t sigsz)
5007{
Jens Axboebda52162019-09-24 13:47:15 -06005008 struct io_wait_queue iowq = {
5009 .wq = {
5010 .private = current,
5011 .func = io_wake_function,
5012 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5013 },
5014 .ctx = ctx,
5015 .to_wait = min_events,
5016 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005017 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005018 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005019
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005020 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005021 return 0;
5022
5023 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005024#ifdef CONFIG_COMPAT
5025 if (in_compat_syscall())
5026 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005027 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005028 else
5029#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005030 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005031
Jens Axboe2b188cc2019-01-07 10:46:33 -07005032 if (ret)
5033 return ret;
5034 }
5035
Jens Axboebda52162019-09-24 13:47:15 -06005036 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005037 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005038 do {
5039 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5040 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005041 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005042 break;
5043 schedule();
5044 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005045 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005046 break;
5047 }
5048 } while (1);
5049 finish_wait(&ctx->wait, &iowq.wq);
5050
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005051 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005052
Hristo Venev75b28af2019-08-26 17:23:46 +00005053 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005054}
5055
Jens Axboe6b063142019-01-10 22:13:58 -07005056static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5057{
5058#if defined(CONFIG_UNIX)
5059 if (ctx->ring_sock) {
5060 struct sock *sock = ctx->ring_sock->sk;
5061 struct sk_buff *skb;
5062
5063 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5064 kfree_skb(skb);
5065 }
5066#else
5067 int i;
5068
Jens Axboe65e19f52019-10-26 07:20:21 -06005069 for (i = 0; i < ctx->nr_user_files; i++) {
5070 struct file *file;
5071
5072 file = io_file_from_index(ctx, i);
5073 if (file)
5074 fput(file);
5075 }
Jens Axboe6b063142019-01-10 22:13:58 -07005076#endif
5077}
5078
Jens Axboe05f3fb32019-12-09 11:22:50 -07005079static void io_file_ref_kill(struct percpu_ref *ref)
5080{
5081 struct fixed_file_data *data;
5082
5083 data = container_of(ref, struct fixed_file_data, refs);
5084 complete(&data->done);
5085}
5086
Jens Axboe6b063142019-01-10 22:13:58 -07005087static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5088{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005089 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005090 unsigned nr_tables, i;
5091
Jens Axboe05f3fb32019-12-09 11:22:50 -07005092 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005093 return -ENXIO;
5094
Jens Axboe05f3fb32019-12-09 11:22:50 -07005095 /* protect against inflight atomic switch, which drops the ref */
5096 flush_work(&data->ref_work);
5097 percpu_ref_get(&data->refs);
5098 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5099 wait_for_completion(&data->done);
5100 percpu_ref_put(&data->refs);
5101 percpu_ref_exit(&data->refs);
5102
Jens Axboe6b063142019-01-10 22:13:58 -07005103 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005104 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5105 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005106 kfree(data->table[i].files);
5107 kfree(data->table);
5108 kfree(data);
5109 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005110 ctx->nr_user_files = 0;
5111 return 0;
5112}
5113
Jens Axboe6c271ce2019-01-10 11:22:30 -07005114static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5115{
5116 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005117 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005118 /*
5119 * The park is a bit of a work-around, without it we get
5120 * warning spews on shutdown with SQPOLL set and affinity
5121 * set to a single CPU.
5122 */
Jens Axboe06058632019-04-13 09:26:03 -06005123 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005124 kthread_stop(ctx->sqo_thread);
5125 ctx->sqo_thread = NULL;
5126 }
5127}
5128
Jens Axboe6b063142019-01-10 22:13:58 -07005129static void io_finish_async(struct io_ring_ctx *ctx)
5130{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005131 io_sq_thread_stop(ctx);
5132
Jens Axboe561fb042019-10-24 07:25:42 -06005133 if (ctx->io_wq) {
5134 io_wq_destroy(ctx->io_wq);
5135 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005136 }
5137}
5138
5139#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005140/*
5141 * Ensure the UNIX gc is aware of our file set, so we are certain that
5142 * the io_uring can be safely unregistered on process exit, even if we have
5143 * loops in the file referencing.
5144 */
5145static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5146{
5147 struct sock *sk = ctx->ring_sock->sk;
5148 struct scm_fp_list *fpl;
5149 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005150 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005151
5152 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5153 unsigned long inflight = ctx->user->unix_inflight + nr;
5154
5155 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5156 return -EMFILE;
5157 }
5158
5159 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5160 if (!fpl)
5161 return -ENOMEM;
5162
5163 skb = alloc_skb(0, GFP_KERNEL);
5164 if (!skb) {
5165 kfree(fpl);
5166 return -ENOMEM;
5167 }
5168
5169 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005170
Jens Axboe08a45172019-10-03 08:11:03 -06005171 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005172 fpl->user = get_uid(ctx->user);
5173 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005174 struct file *file = io_file_from_index(ctx, i + offset);
5175
5176 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005177 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005178 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005179 unix_inflight(fpl->user, fpl->fp[nr_files]);
5180 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005181 }
5182
Jens Axboe08a45172019-10-03 08:11:03 -06005183 if (nr_files) {
5184 fpl->max = SCM_MAX_FD;
5185 fpl->count = nr_files;
5186 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005187 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005188 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5189 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005190
Jens Axboe08a45172019-10-03 08:11:03 -06005191 for (i = 0; i < nr_files; i++)
5192 fput(fpl->fp[i]);
5193 } else {
5194 kfree_skb(skb);
5195 kfree(fpl);
5196 }
Jens Axboe6b063142019-01-10 22:13:58 -07005197
5198 return 0;
5199}
5200
5201/*
5202 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5203 * causes regular reference counting to break down. We rely on the UNIX
5204 * garbage collection to take care of this problem for us.
5205 */
5206static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5207{
5208 unsigned left, total;
5209 int ret = 0;
5210
5211 total = 0;
5212 left = ctx->nr_user_files;
5213 while (left) {
5214 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005215
5216 ret = __io_sqe_files_scm(ctx, this_files, total);
5217 if (ret)
5218 break;
5219 left -= this_files;
5220 total += this_files;
5221 }
5222
5223 if (!ret)
5224 return 0;
5225
5226 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005227 struct file *file = io_file_from_index(ctx, total);
5228
5229 if (file)
5230 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005231 total++;
5232 }
5233
5234 return ret;
5235}
5236#else
5237static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5238{
5239 return 0;
5240}
5241#endif
5242
Jens Axboe65e19f52019-10-26 07:20:21 -06005243static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5244 unsigned nr_files)
5245{
5246 int i;
5247
5248 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005249 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005250 unsigned this_files;
5251
5252 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5253 table->files = kcalloc(this_files, sizeof(struct file *),
5254 GFP_KERNEL);
5255 if (!table->files)
5256 break;
5257 nr_files -= this_files;
5258 }
5259
5260 if (i == nr_tables)
5261 return 0;
5262
5263 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005264 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005265 kfree(table->files);
5266 }
5267 return 1;
5268}
5269
Jens Axboe05f3fb32019-12-09 11:22:50 -07005270static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005271{
5272#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005273 struct sock *sock = ctx->ring_sock->sk;
5274 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5275 struct sk_buff *skb;
5276 int i;
5277
5278 __skb_queue_head_init(&list);
5279
5280 /*
5281 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5282 * remove this entry and rearrange the file array.
5283 */
5284 skb = skb_dequeue(head);
5285 while (skb) {
5286 struct scm_fp_list *fp;
5287
5288 fp = UNIXCB(skb).fp;
5289 for (i = 0; i < fp->count; i++) {
5290 int left;
5291
5292 if (fp->fp[i] != file)
5293 continue;
5294
5295 unix_notinflight(fp->user, fp->fp[i]);
5296 left = fp->count - 1 - i;
5297 if (left) {
5298 memmove(&fp->fp[i], &fp->fp[i + 1],
5299 left * sizeof(struct file *));
5300 }
5301 fp->count--;
5302 if (!fp->count) {
5303 kfree_skb(skb);
5304 skb = NULL;
5305 } else {
5306 __skb_queue_tail(&list, skb);
5307 }
5308 fput(file);
5309 file = NULL;
5310 break;
5311 }
5312
5313 if (!file)
5314 break;
5315
5316 __skb_queue_tail(&list, skb);
5317
5318 skb = skb_dequeue(head);
5319 }
5320
5321 if (skb_peek(&list)) {
5322 spin_lock_irq(&head->lock);
5323 while ((skb = __skb_dequeue(&list)) != NULL)
5324 __skb_queue_tail(head, skb);
5325 spin_unlock_irq(&head->lock);
5326 }
5327#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005328 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005329#endif
5330}
5331
Jens Axboe05f3fb32019-12-09 11:22:50 -07005332struct io_file_put {
5333 struct llist_node llist;
5334 struct file *file;
5335 struct completion *done;
5336};
5337
5338static void io_ring_file_ref_switch(struct work_struct *work)
5339{
5340 struct io_file_put *pfile, *tmp;
5341 struct fixed_file_data *data;
5342 struct llist_node *node;
5343
5344 data = container_of(work, struct fixed_file_data, ref_work);
5345
5346 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5347 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5348 io_ring_file_put(data->ctx, pfile->file);
5349 if (pfile->done)
5350 complete(pfile->done);
5351 else
5352 kfree(pfile);
5353 }
5354 }
5355
5356 percpu_ref_get(&data->refs);
5357 percpu_ref_switch_to_percpu(&data->refs);
5358}
5359
5360static void io_file_data_ref_zero(struct percpu_ref *ref)
5361{
5362 struct fixed_file_data *data;
5363
5364 data = container_of(ref, struct fixed_file_data, refs);
5365
5366 /* we can't safely switch from inside this context, punt to wq */
5367 queue_work(system_wq, &data->ref_work);
5368}
5369
5370static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5371 unsigned nr_args)
5372{
5373 __s32 __user *fds = (__s32 __user *) arg;
5374 unsigned nr_tables;
5375 struct file *file;
5376 int fd, ret = 0;
5377 unsigned i;
5378
5379 if (ctx->file_data)
5380 return -EBUSY;
5381 if (!nr_args)
5382 return -EINVAL;
5383 if (nr_args > IORING_MAX_FIXED_FILES)
5384 return -EMFILE;
5385
5386 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5387 if (!ctx->file_data)
5388 return -ENOMEM;
5389 ctx->file_data->ctx = ctx;
5390 init_completion(&ctx->file_data->done);
5391
5392 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5393 ctx->file_data->table = kcalloc(nr_tables,
5394 sizeof(struct fixed_file_table),
5395 GFP_KERNEL);
5396 if (!ctx->file_data->table) {
5397 kfree(ctx->file_data);
5398 ctx->file_data = NULL;
5399 return -ENOMEM;
5400 }
5401
5402 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5403 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5404 kfree(ctx->file_data->table);
5405 kfree(ctx->file_data);
5406 ctx->file_data = NULL;
5407 return -ENOMEM;
5408 }
5409 ctx->file_data->put_llist.first = NULL;
5410 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5411
5412 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5413 percpu_ref_exit(&ctx->file_data->refs);
5414 kfree(ctx->file_data->table);
5415 kfree(ctx->file_data);
5416 ctx->file_data = NULL;
5417 return -ENOMEM;
5418 }
5419
5420 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5421 struct fixed_file_table *table;
5422 unsigned index;
5423
5424 ret = -EFAULT;
5425 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5426 break;
5427 /* allow sparse sets */
5428 if (fd == -1) {
5429 ret = 0;
5430 continue;
5431 }
5432
5433 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5434 index = i & IORING_FILE_TABLE_MASK;
5435 file = fget(fd);
5436
5437 ret = -EBADF;
5438 if (!file)
5439 break;
5440
5441 /*
5442 * Don't allow io_uring instances to be registered. If UNIX
5443 * isn't enabled, then this causes a reference cycle and this
5444 * instance can never get freed. If UNIX is enabled we'll
5445 * handle it just fine, but there's still no point in allowing
5446 * a ring fd as it doesn't support regular read/write anyway.
5447 */
5448 if (file->f_op == &io_uring_fops) {
5449 fput(file);
5450 break;
5451 }
5452 ret = 0;
5453 table->files[index] = file;
5454 }
5455
5456 if (ret) {
5457 for (i = 0; i < ctx->nr_user_files; i++) {
5458 file = io_file_from_index(ctx, i);
5459 if (file)
5460 fput(file);
5461 }
5462 for (i = 0; i < nr_tables; i++)
5463 kfree(ctx->file_data->table[i].files);
5464
5465 kfree(ctx->file_data->table);
5466 kfree(ctx->file_data);
5467 ctx->file_data = NULL;
5468 ctx->nr_user_files = 0;
5469 return ret;
5470 }
5471
5472 ret = io_sqe_files_scm(ctx);
5473 if (ret)
5474 io_sqe_files_unregister(ctx);
5475
5476 return ret;
5477}
5478
Jens Axboec3a31e62019-10-03 13:59:56 -06005479static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5480 int index)
5481{
5482#if defined(CONFIG_UNIX)
5483 struct sock *sock = ctx->ring_sock->sk;
5484 struct sk_buff_head *head = &sock->sk_receive_queue;
5485 struct sk_buff *skb;
5486
5487 /*
5488 * See if we can merge this file into an existing skb SCM_RIGHTS
5489 * file set. If there's no room, fall back to allocating a new skb
5490 * and filling it in.
5491 */
5492 spin_lock_irq(&head->lock);
5493 skb = skb_peek(head);
5494 if (skb) {
5495 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5496
5497 if (fpl->count < SCM_MAX_FD) {
5498 __skb_unlink(skb, head);
5499 spin_unlock_irq(&head->lock);
5500 fpl->fp[fpl->count] = get_file(file);
5501 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5502 fpl->count++;
5503 spin_lock_irq(&head->lock);
5504 __skb_queue_head(head, skb);
5505 } else {
5506 skb = NULL;
5507 }
5508 }
5509 spin_unlock_irq(&head->lock);
5510
5511 if (skb) {
5512 fput(file);
5513 return 0;
5514 }
5515
5516 return __io_sqe_files_scm(ctx, 1, index);
5517#else
5518 return 0;
5519#endif
5520}
5521
Jens Axboe05f3fb32019-12-09 11:22:50 -07005522static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005523{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005524 struct fixed_file_data *data;
5525
5526 data = container_of(ref, struct fixed_file_data, refs);
5527 clear_bit(FFD_F_ATOMIC, &data->state);
5528}
5529
5530static bool io_queue_file_removal(struct fixed_file_data *data,
5531 struct file *file)
5532{
5533 struct io_file_put *pfile, pfile_stack;
5534 DECLARE_COMPLETION_ONSTACK(done);
5535
5536 /*
5537 * If we fail allocating the struct we need for doing async reomval
5538 * of this file, just punt to sync and wait for it.
5539 */
5540 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5541 if (!pfile) {
5542 pfile = &pfile_stack;
5543 pfile->done = &done;
5544 }
5545
5546 pfile->file = file;
5547 llist_add(&pfile->llist, &data->put_llist);
5548
5549 if (pfile == &pfile_stack) {
5550 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5551 percpu_ref_put(&data->refs);
5552 percpu_ref_switch_to_atomic(&data->refs,
5553 io_atomic_switch);
5554 }
5555 wait_for_completion(&done);
5556 flush_work(&data->ref_work);
5557 return false;
5558 }
5559
5560 return true;
5561}
5562
5563static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5564 struct io_uring_files_update *up,
5565 unsigned nr_args)
5566{
5567 struct fixed_file_data *data = ctx->file_data;
5568 bool ref_switch = false;
5569 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005570 __s32 __user *fds;
5571 int fd, i, err;
5572 __u32 done;
5573
Jens Axboe05f3fb32019-12-09 11:22:50 -07005574 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005575 return -EOVERFLOW;
5576 if (done > ctx->nr_user_files)
5577 return -EINVAL;
5578
5579 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005580 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005581 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005582 struct fixed_file_table *table;
5583 unsigned index;
5584
Jens Axboec3a31e62019-10-03 13:59:56 -06005585 err = 0;
5586 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5587 err = -EFAULT;
5588 break;
5589 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005590 i = array_index_nospec(up->offset, ctx->nr_user_files);
5591 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005592 index = i & IORING_FILE_TABLE_MASK;
5593 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005594 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005595 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005596 if (io_queue_file_removal(data, file))
5597 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005598 }
5599 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005600 file = fget(fd);
5601 if (!file) {
5602 err = -EBADF;
5603 break;
5604 }
5605 /*
5606 * Don't allow io_uring instances to be registered. If
5607 * UNIX isn't enabled, then this causes a reference
5608 * cycle and this instance can never get freed. If UNIX
5609 * is enabled we'll handle it just fine, but there's
5610 * still no point in allowing a ring fd as it doesn't
5611 * support regular read/write anyway.
5612 */
5613 if (file->f_op == &io_uring_fops) {
5614 fput(file);
5615 err = -EBADF;
5616 break;
5617 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005618 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005619 err = io_sqe_file_register(ctx, file, i);
5620 if (err)
5621 break;
5622 }
5623 nr_args--;
5624 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005625 up->offset++;
5626 }
5627
5628 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5629 percpu_ref_put(&data->refs);
5630 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005631 }
5632
5633 return done ? done : err;
5634}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005635static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5636 unsigned nr_args)
5637{
5638 struct io_uring_files_update up;
5639
5640 if (!ctx->file_data)
5641 return -ENXIO;
5642 if (!nr_args)
5643 return -EINVAL;
5644 if (copy_from_user(&up, arg, sizeof(up)))
5645 return -EFAULT;
5646 if (up.resv)
5647 return -EINVAL;
5648
5649 return __io_sqe_files_update(ctx, &up, nr_args);
5650}
Jens Axboec3a31e62019-10-03 13:59:56 -06005651
Jens Axboe7d723062019-11-12 22:31:31 -07005652static void io_put_work(struct io_wq_work *work)
5653{
5654 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5655
5656 io_put_req(req);
5657}
5658
5659static void io_get_work(struct io_wq_work *work)
5660{
5661 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5662
5663 refcount_inc(&req->refs);
5664}
5665
Jens Axboe6c271ce2019-01-10 11:22:30 -07005666static int io_sq_offload_start(struct io_ring_ctx *ctx,
5667 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005668{
Jens Axboe576a3472019-11-25 08:49:20 -07005669 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005670 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005671 int ret;
5672
Jens Axboe6c271ce2019-01-10 11:22:30 -07005673 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005674 mmgrab(current->mm);
5675 ctx->sqo_mm = current->mm;
5676
Jens Axboe6c271ce2019-01-10 11:22:30 -07005677 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005678 ret = -EPERM;
5679 if (!capable(CAP_SYS_ADMIN))
5680 goto err;
5681
Jens Axboe917257d2019-04-13 09:28:55 -06005682 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5683 if (!ctx->sq_thread_idle)
5684 ctx->sq_thread_idle = HZ;
5685
Jens Axboe6c271ce2019-01-10 11:22:30 -07005686 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005687 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005688
Jens Axboe917257d2019-04-13 09:28:55 -06005689 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005690 if (cpu >= nr_cpu_ids)
5691 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005692 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005693 goto err;
5694
Jens Axboe6c271ce2019-01-10 11:22:30 -07005695 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5696 ctx, cpu,
5697 "io_uring-sq");
5698 } else {
5699 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5700 "io_uring-sq");
5701 }
5702 if (IS_ERR(ctx->sqo_thread)) {
5703 ret = PTR_ERR(ctx->sqo_thread);
5704 ctx->sqo_thread = NULL;
5705 goto err;
5706 }
5707 wake_up_process(ctx->sqo_thread);
5708 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5709 /* Can't have SQ_AFF without SQPOLL */
5710 ret = -EINVAL;
5711 goto err;
5712 }
5713
Jens Axboe576a3472019-11-25 08:49:20 -07005714 data.mm = ctx->sqo_mm;
5715 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005716 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005717 data.get_work = io_get_work;
5718 data.put_work = io_put_work;
5719
Jens Axboe561fb042019-10-24 07:25:42 -06005720 /* Do QD, or 4 * CPUS, whatever is smallest */
5721 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005722 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005723 if (IS_ERR(ctx->io_wq)) {
5724 ret = PTR_ERR(ctx->io_wq);
5725 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005726 goto err;
5727 }
5728
5729 return 0;
5730err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005731 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005732 mmdrop(ctx->sqo_mm);
5733 ctx->sqo_mm = NULL;
5734 return ret;
5735}
5736
5737static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5738{
5739 atomic_long_sub(nr_pages, &user->locked_vm);
5740}
5741
5742static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5743{
5744 unsigned long page_limit, cur_pages, new_pages;
5745
5746 /* Don't allow more pages than we can safely lock */
5747 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5748
5749 do {
5750 cur_pages = atomic_long_read(&user->locked_vm);
5751 new_pages = cur_pages + nr_pages;
5752 if (new_pages > page_limit)
5753 return -ENOMEM;
5754 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5755 new_pages) != cur_pages);
5756
5757 return 0;
5758}
5759
5760static void io_mem_free(void *ptr)
5761{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005762 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005763
Mark Rutland52e04ef2019-04-30 17:30:21 +01005764 if (!ptr)
5765 return;
5766
5767 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005768 if (put_page_testzero(page))
5769 free_compound_page(page);
5770}
5771
5772static void *io_mem_alloc(size_t size)
5773{
5774 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5775 __GFP_NORETRY;
5776
5777 return (void *) __get_free_pages(gfp_flags, get_order(size));
5778}
5779
Hristo Venev75b28af2019-08-26 17:23:46 +00005780static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5781 size_t *sq_offset)
5782{
5783 struct io_rings *rings;
5784 size_t off, sq_array_size;
5785
5786 off = struct_size(rings, cqes, cq_entries);
5787 if (off == SIZE_MAX)
5788 return SIZE_MAX;
5789
5790#ifdef CONFIG_SMP
5791 off = ALIGN(off, SMP_CACHE_BYTES);
5792 if (off == 0)
5793 return SIZE_MAX;
5794#endif
5795
5796 sq_array_size = array_size(sizeof(u32), sq_entries);
5797 if (sq_array_size == SIZE_MAX)
5798 return SIZE_MAX;
5799
5800 if (check_add_overflow(off, sq_array_size, &off))
5801 return SIZE_MAX;
5802
5803 if (sq_offset)
5804 *sq_offset = off;
5805
5806 return off;
5807}
5808
Jens Axboe2b188cc2019-01-07 10:46:33 -07005809static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5810{
Hristo Venev75b28af2019-08-26 17:23:46 +00005811 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005812
Hristo Venev75b28af2019-08-26 17:23:46 +00005813 pages = (size_t)1 << get_order(
5814 rings_size(sq_entries, cq_entries, NULL));
5815 pages += (size_t)1 << get_order(
5816 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005817
Hristo Venev75b28af2019-08-26 17:23:46 +00005818 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005819}
5820
Jens Axboeedafcce2019-01-09 09:16:05 -07005821static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5822{
5823 int i, j;
5824
5825 if (!ctx->user_bufs)
5826 return -ENXIO;
5827
5828 for (i = 0; i < ctx->nr_user_bufs; i++) {
5829 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5830
5831 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005832 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005833
5834 if (ctx->account_mem)
5835 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005836 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005837 imu->nr_bvecs = 0;
5838 }
5839
5840 kfree(ctx->user_bufs);
5841 ctx->user_bufs = NULL;
5842 ctx->nr_user_bufs = 0;
5843 return 0;
5844}
5845
5846static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5847 void __user *arg, unsigned index)
5848{
5849 struct iovec __user *src;
5850
5851#ifdef CONFIG_COMPAT
5852 if (ctx->compat) {
5853 struct compat_iovec __user *ciovs;
5854 struct compat_iovec ciov;
5855
5856 ciovs = (struct compat_iovec __user *) arg;
5857 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5858 return -EFAULT;
5859
Jens Axboed55e5f52019-12-11 16:12:15 -07005860 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005861 dst->iov_len = ciov.iov_len;
5862 return 0;
5863 }
5864#endif
5865 src = (struct iovec __user *) arg;
5866 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5867 return -EFAULT;
5868 return 0;
5869}
5870
5871static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5872 unsigned nr_args)
5873{
5874 struct vm_area_struct **vmas = NULL;
5875 struct page **pages = NULL;
5876 int i, j, got_pages = 0;
5877 int ret = -EINVAL;
5878
5879 if (ctx->user_bufs)
5880 return -EBUSY;
5881 if (!nr_args || nr_args > UIO_MAXIOV)
5882 return -EINVAL;
5883
5884 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5885 GFP_KERNEL);
5886 if (!ctx->user_bufs)
5887 return -ENOMEM;
5888
5889 for (i = 0; i < nr_args; i++) {
5890 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5891 unsigned long off, start, end, ubuf;
5892 int pret, nr_pages;
5893 struct iovec iov;
5894 size_t size;
5895
5896 ret = io_copy_iov(ctx, &iov, arg, i);
5897 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005898 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005899
5900 /*
5901 * Don't impose further limits on the size and buffer
5902 * constraints here, we'll -EINVAL later when IO is
5903 * submitted if they are wrong.
5904 */
5905 ret = -EFAULT;
5906 if (!iov.iov_base || !iov.iov_len)
5907 goto err;
5908
5909 /* arbitrary limit, but we need something */
5910 if (iov.iov_len > SZ_1G)
5911 goto err;
5912
5913 ubuf = (unsigned long) iov.iov_base;
5914 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5915 start = ubuf >> PAGE_SHIFT;
5916 nr_pages = end - start;
5917
5918 if (ctx->account_mem) {
5919 ret = io_account_mem(ctx->user, nr_pages);
5920 if (ret)
5921 goto err;
5922 }
5923
5924 ret = 0;
5925 if (!pages || nr_pages > got_pages) {
5926 kfree(vmas);
5927 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005928 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005929 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005930 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005931 sizeof(struct vm_area_struct *),
5932 GFP_KERNEL);
5933 if (!pages || !vmas) {
5934 ret = -ENOMEM;
5935 if (ctx->account_mem)
5936 io_unaccount_mem(ctx->user, nr_pages);
5937 goto err;
5938 }
5939 got_pages = nr_pages;
5940 }
5941
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005942 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005943 GFP_KERNEL);
5944 ret = -ENOMEM;
5945 if (!imu->bvec) {
5946 if (ctx->account_mem)
5947 io_unaccount_mem(ctx->user, nr_pages);
5948 goto err;
5949 }
5950
5951 ret = 0;
5952 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005953 pret = get_user_pages(ubuf, nr_pages,
5954 FOLL_WRITE | FOLL_LONGTERM,
5955 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005956 if (pret == nr_pages) {
5957 /* don't support file backed memory */
5958 for (j = 0; j < nr_pages; j++) {
5959 struct vm_area_struct *vma = vmas[j];
5960
5961 if (vma->vm_file &&
5962 !is_file_hugepages(vma->vm_file)) {
5963 ret = -EOPNOTSUPP;
5964 break;
5965 }
5966 }
5967 } else {
5968 ret = pret < 0 ? pret : -EFAULT;
5969 }
5970 up_read(&current->mm->mmap_sem);
5971 if (ret) {
5972 /*
5973 * if we did partial map, or found file backed vmas,
5974 * release any pages we did get
5975 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005976 if (pret > 0)
5977 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005978 if (ctx->account_mem)
5979 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005980 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005981 goto err;
5982 }
5983
5984 off = ubuf & ~PAGE_MASK;
5985 size = iov.iov_len;
5986 for (j = 0; j < nr_pages; j++) {
5987 size_t vec_len;
5988
5989 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5990 imu->bvec[j].bv_page = pages[j];
5991 imu->bvec[j].bv_len = vec_len;
5992 imu->bvec[j].bv_offset = off;
5993 off = 0;
5994 size -= vec_len;
5995 }
5996 /* store original address for later verification */
5997 imu->ubuf = ubuf;
5998 imu->len = iov.iov_len;
5999 imu->nr_bvecs = nr_pages;
6000
6001 ctx->nr_user_bufs++;
6002 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006003 kvfree(pages);
6004 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006005 return 0;
6006err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006007 kvfree(pages);
6008 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006009 io_sqe_buffer_unregister(ctx);
6010 return ret;
6011}
6012
Jens Axboe9b402842019-04-11 11:45:41 -06006013static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6014{
6015 __s32 __user *fds = arg;
6016 int fd;
6017
6018 if (ctx->cq_ev_fd)
6019 return -EBUSY;
6020
6021 if (copy_from_user(&fd, fds, sizeof(*fds)))
6022 return -EFAULT;
6023
6024 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6025 if (IS_ERR(ctx->cq_ev_fd)) {
6026 int ret = PTR_ERR(ctx->cq_ev_fd);
6027 ctx->cq_ev_fd = NULL;
6028 return ret;
6029 }
6030
6031 return 0;
6032}
6033
6034static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6035{
6036 if (ctx->cq_ev_fd) {
6037 eventfd_ctx_put(ctx->cq_ev_fd);
6038 ctx->cq_ev_fd = NULL;
6039 return 0;
6040 }
6041
6042 return -ENXIO;
6043}
6044
Jens Axboe2b188cc2019-01-07 10:46:33 -07006045static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6046{
Jens Axboe6b063142019-01-10 22:13:58 -07006047 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006048 if (ctx->sqo_mm)
6049 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006050
6051 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006052 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006053 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006054 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006055
Jens Axboe2b188cc2019-01-07 10:46:33 -07006056#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006057 if (ctx->ring_sock) {
6058 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006059 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006060 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006061#endif
6062
Hristo Venev75b28af2019-08-26 17:23:46 +00006063 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006064 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006065
6066 percpu_ref_exit(&ctx->refs);
6067 if (ctx->account_mem)
6068 io_unaccount_mem(ctx->user,
6069 ring_pages(ctx->sq_entries, ctx->cq_entries));
6070 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006071 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006072 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006073 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006074 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006075 kfree(ctx);
6076}
6077
6078static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6079{
6080 struct io_ring_ctx *ctx = file->private_data;
6081 __poll_t mask = 0;
6082
6083 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006084 /*
6085 * synchronizes with barrier from wq_has_sleeper call in
6086 * io_commit_cqring
6087 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006088 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006089 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6090 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006091 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006092 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006093 mask |= EPOLLIN | EPOLLRDNORM;
6094
6095 return mask;
6096}
6097
6098static int io_uring_fasync(int fd, struct file *file, int on)
6099{
6100 struct io_ring_ctx *ctx = file->private_data;
6101
6102 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6103}
6104
6105static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6106{
6107 mutex_lock(&ctx->uring_lock);
6108 percpu_ref_kill(&ctx->refs);
6109 mutex_unlock(&ctx->uring_lock);
6110
Jens Axboe5262f562019-09-17 12:26:57 -06006111 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006112 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006113
6114 if (ctx->io_wq)
6115 io_wq_cancel_all(ctx->io_wq);
6116
Jens Axboedef596e2019-01-09 08:59:42 -07006117 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006118 /* if we failed setting up the ctx, we might not have any rings */
6119 if (ctx->rings)
6120 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07006121 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006122 io_ring_ctx_free(ctx);
6123}
6124
6125static int io_uring_release(struct inode *inode, struct file *file)
6126{
6127 struct io_ring_ctx *ctx = file->private_data;
6128
6129 file->private_data = NULL;
6130 io_ring_ctx_wait_and_kill(ctx);
6131 return 0;
6132}
6133
Jens Axboefcb323c2019-10-24 12:39:47 -06006134static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6135 struct files_struct *files)
6136{
6137 struct io_kiocb *req;
6138 DEFINE_WAIT(wait);
6139
6140 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006141 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006142
6143 spin_lock_irq(&ctx->inflight_lock);
6144 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006145 if (req->work.files != files)
6146 continue;
6147 /* req is being completed, ignore */
6148 if (!refcount_inc_not_zero(&req->refs))
6149 continue;
6150 cancel_req = req;
6151 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006152 }
Jens Axboe768134d2019-11-10 20:30:53 -07006153 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006154 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006155 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006156 spin_unlock_irq(&ctx->inflight_lock);
6157
Jens Axboe768134d2019-11-10 20:30:53 -07006158 /* We need to keep going until we don't find a matching req */
6159 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006160 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006161
6162 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6163 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006164 schedule();
6165 }
Jens Axboe768134d2019-11-10 20:30:53 -07006166 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006167}
6168
6169static int io_uring_flush(struct file *file, void *data)
6170{
6171 struct io_ring_ctx *ctx = file->private_data;
6172
6173 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006174 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6175 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006176 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006177 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006178 return 0;
6179}
6180
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006181static void *io_uring_validate_mmap_request(struct file *file,
6182 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006183{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006184 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006185 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006186 struct page *page;
6187 void *ptr;
6188
6189 switch (offset) {
6190 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006191 case IORING_OFF_CQ_RING:
6192 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006193 break;
6194 case IORING_OFF_SQES:
6195 ptr = ctx->sq_sqes;
6196 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006197 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006198 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006199 }
6200
6201 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006202 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006203 return ERR_PTR(-EINVAL);
6204
6205 return ptr;
6206}
6207
6208#ifdef CONFIG_MMU
6209
6210static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6211{
6212 size_t sz = vma->vm_end - vma->vm_start;
6213 unsigned long pfn;
6214 void *ptr;
6215
6216 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6217 if (IS_ERR(ptr))
6218 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006219
6220 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6221 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6222}
6223
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006224#else /* !CONFIG_MMU */
6225
6226static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6227{
6228 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6229}
6230
6231static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6232{
6233 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6234}
6235
6236static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6237 unsigned long addr, unsigned long len,
6238 unsigned long pgoff, unsigned long flags)
6239{
6240 void *ptr;
6241
6242 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6243 if (IS_ERR(ptr))
6244 return PTR_ERR(ptr);
6245
6246 return (unsigned long) ptr;
6247}
6248
6249#endif /* !CONFIG_MMU */
6250
Jens Axboe2b188cc2019-01-07 10:46:33 -07006251SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6252 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6253 size_t, sigsz)
6254{
6255 struct io_ring_ctx *ctx;
6256 long ret = -EBADF;
6257 int submitted = 0;
6258 struct fd f;
6259
Jens Axboe6c271ce2019-01-10 11:22:30 -07006260 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006261 return -EINVAL;
6262
6263 f = fdget(fd);
6264 if (!f.file)
6265 return -EBADF;
6266
6267 ret = -EOPNOTSUPP;
6268 if (f.file->f_op != &io_uring_fops)
6269 goto out_fput;
6270
6271 ret = -ENXIO;
6272 ctx = f.file->private_data;
6273 if (!percpu_ref_tryget(&ctx->refs))
6274 goto out_fput;
6275
Jens Axboe6c271ce2019-01-10 11:22:30 -07006276 /*
6277 * For SQ polling, the thread will do all submissions and completions.
6278 * Just return the requested submit count, and wake the thread if
6279 * we were asked to.
6280 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006281 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006282 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006283 if (!list_empty_careful(&ctx->cq_overflow_list))
6284 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006285 if (flags & IORING_ENTER_SQ_WAKEUP)
6286 wake_up(&ctx->sqo_wait);
6287 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006288 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006289 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006290
Jens Axboe44d28272020-01-16 19:00:24 -07006291 if (current->mm != ctx->sqo_mm ||
6292 current_cred() != ctx->creds) {
6293 ret = -EPERM;
6294 goto out;
6295 }
6296
Jens Axboe2b188cc2019-01-07 10:46:33 -07006297 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006298 /* already have mm, so io_submit_sqes() won't try to grab it */
6299 cur_mm = ctx->sqo_mm;
6300 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6301 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006302 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006303
6304 if (submitted != to_submit)
6305 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006306 }
6307 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006308 unsigned nr_events = 0;
6309
Jens Axboe2b188cc2019-01-07 10:46:33 -07006310 min_complete = min(min_complete, ctx->cq_entries);
6311
Jens Axboedef596e2019-01-09 08:59:42 -07006312 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006313 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006314 } else {
6315 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6316 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006317 }
6318
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006319out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006320 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006321out_fput:
6322 fdput(f);
6323 return submitted ? submitted : ret;
6324}
6325
6326static const struct file_operations io_uring_fops = {
6327 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006328 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006329 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006330#ifndef CONFIG_MMU
6331 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6332 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6333#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006334 .poll = io_uring_poll,
6335 .fasync = io_uring_fasync,
6336};
6337
6338static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6339 struct io_uring_params *p)
6340{
Hristo Venev75b28af2019-08-26 17:23:46 +00006341 struct io_rings *rings;
6342 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006343
Hristo Venev75b28af2019-08-26 17:23:46 +00006344 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6345 if (size == SIZE_MAX)
6346 return -EOVERFLOW;
6347
6348 rings = io_mem_alloc(size);
6349 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006350 return -ENOMEM;
6351
Hristo Venev75b28af2019-08-26 17:23:46 +00006352 ctx->rings = rings;
6353 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6354 rings->sq_ring_mask = p->sq_entries - 1;
6355 rings->cq_ring_mask = p->cq_entries - 1;
6356 rings->sq_ring_entries = p->sq_entries;
6357 rings->cq_ring_entries = p->cq_entries;
6358 ctx->sq_mask = rings->sq_ring_mask;
6359 ctx->cq_mask = rings->cq_ring_mask;
6360 ctx->sq_entries = rings->sq_ring_entries;
6361 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006362
6363 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006364 if (size == SIZE_MAX) {
6365 io_mem_free(ctx->rings);
6366 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006367 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006368 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006369
6370 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006371 if (!ctx->sq_sqes) {
6372 io_mem_free(ctx->rings);
6373 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006374 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006375 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006376
Jens Axboe2b188cc2019-01-07 10:46:33 -07006377 return 0;
6378}
6379
6380/*
6381 * Allocate an anonymous fd, this is what constitutes the application
6382 * visible backing of an io_uring instance. The application mmaps this
6383 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6384 * we have to tie this fd to a socket for file garbage collection purposes.
6385 */
6386static int io_uring_get_fd(struct io_ring_ctx *ctx)
6387{
6388 struct file *file;
6389 int ret;
6390
6391#if defined(CONFIG_UNIX)
6392 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6393 &ctx->ring_sock);
6394 if (ret)
6395 return ret;
6396#endif
6397
6398 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6399 if (ret < 0)
6400 goto err;
6401
6402 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6403 O_RDWR | O_CLOEXEC);
6404 if (IS_ERR(file)) {
6405 put_unused_fd(ret);
6406 ret = PTR_ERR(file);
6407 goto err;
6408 }
6409
6410#if defined(CONFIG_UNIX)
6411 ctx->ring_sock->file = file;
6412#endif
6413 fd_install(ret, file);
6414 return ret;
6415err:
6416#if defined(CONFIG_UNIX)
6417 sock_release(ctx->ring_sock);
6418 ctx->ring_sock = NULL;
6419#endif
6420 return ret;
6421}
6422
6423static int io_uring_create(unsigned entries, struct io_uring_params *p)
6424{
6425 struct user_struct *user = NULL;
6426 struct io_ring_ctx *ctx;
6427 bool account_mem;
6428 int ret;
6429
Jens Axboe8110c1a2019-12-28 15:39:54 -07006430 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006431 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006432 if (entries > IORING_MAX_ENTRIES) {
6433 if (!(p->flags & IORING_SETUP_CLAMP))
6434 return -EINVAL;
6435 entries = IORING_MAX_ENTRIES;
6436 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006437
6438 /*
6439 * Use twice as many entries for the CQ ring. It's possible for the
6440 * application to drive a higher depth than the size of the SQ ring,
6441 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006442 * some flexibility in overcommitting a bit. If the application has
6443 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6444 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006445 */
6446 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006447 if (p->flags & IORING_SETUP_CQSIZE) {
6448 /*
6449 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6450 * to a power-of-two, if it isn't already. We do NOT impose
6451 * any cq vs sq ring sizing.
6452 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006453 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006454 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006455 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6456 if (!(p->flags & IORING_SETUP_CLAMP))
6457 return -EINVAL;
6458 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6459 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006460 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6461 } else {
6462 p->cq_entries = 2 * p->sq_entries;
6463 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006464
6465 user = get_uid(current_user());
6466 account_mem = !capable(CAP_IPC_LOCK);
6467
6468 if (account_mem) {
6469 ret = io_account_mem(user,
6470 ring_pages(p->sq_entries, p->cq_entries));
6471 if (ret) {
6472 free_uid(user);
6473 return ret;
6474 }
6475 }
6476
6477 ctx = io_ring_ctx_alloc(p);
6478 if (!ctx) {
6479 if (account_mem)
6480 io_unaccount_mem(user, ring_pages(p->sq_entries,
6481 p->cq_entries));
6482 free_uid(user);
6483 return -ENOMEM;
6484 }
6485 ctx->compat = in_compat_syscall();
6486 ctx->account_mem = account_mem;
6487 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006488 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006489
6490 ret = io_allocate_scq_urings(ctx, p);
6491 if (ret)
6492 goto err;
6493
Jens Axboe6c271ce2019-01-10 11:22:30 -07006494 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006495 if (ret)
6496 goto err;
6497
Jens Axboe2b188cc2019-01-07 10:46:33 -07006498 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006499 p->sq_off.head = offsetof(struct io_rings, sq.head);
6500 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6501 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6502 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6503 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6504 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6505 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006506
6507 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006508 p->cq_off.head = offsetof(struct io_rings, cq.head);
6509 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6510 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6511 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6512 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6513 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006514
Jens Axboe044c1ab2019-10-28 09:15:33 -06006515 /*
6516 * Install ring fd as the very last thing, so we don't risk someone
6517 * having closed it before we finish setup
6518 */
6519 ret = io_uring_get_fd(ctx);
6520 if (ret < 0)
6521 goto err;
6522
Jens Axboeda8c9692019-12-02 18:51:26 -07006523 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboeba042912019-12-25 16:33:42 -07006524 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006525 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006526 return ret;
6527err:
6528 io_ring_ctx_wait_and_kill(ctx);
6529 return ret;
6530}
6531
6532/*
6533 * Sets up an aio uring context, and returns the fd. Applications asks for a
6534 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6535 * params structure passed in.
6536 */
6537static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6538{
6539 struct io_uring_params p;
6540 long ret;
6541 int i;
6542
6543 if (copy_from_user(&p, params, sizeof(p)))
6544 return -EFAULT;
6545 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6546 if (p.resv[i])
6547 return -EINVAL;
6548 }
6549
Jens Axboe6c271ce2019-01-10 11:22:30 -07006550 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006551 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
6552 IORING_SETUP_CLAMP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006553 return -EINVAL;
6554
6555 ret = io_uring_create(entries, &p);
6556 if (ret < 0)
6557 return ret;
6558
6559 if (copy_to_user(params, &p, sizeof(p)))
6560 return -EFAULT;
6561
6562 return ret;
6563}
6564
6565SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6566 struct io_uring_params __user *, params)
6567{
6568 return io_uring_setup(entries, params);
6569}
6570
Jens Axboe66f4af92020-01-16 15:36:52 -07006571static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6572{
6573 struct io_uring_probe *p;
6574 size_t size;
6575 int i, ret;
6576
6577 size = struct_size(p, ops, nr_args);
6578 if (size == SIZE_MAX)
6579 return -EOVERFLOW;
6580 p = kzalloc(size, GFP_KERNEL);
6581 if (!p)
6582 return -ENOMEM;
6583
6584 ret = -EFAULT;
6585 if (copy_from_user(p, arg, size))
6586 goto out;
6587 ret = -EINVAL;
6588 if (memchr_inv(p, 0, size))
6589 goto out;
6590
6591 p->last_op = IORING_OP_LAST - 1;
6592 if (nr_args > IORING_OP_LAST)
6593 nr_args = IORING_OP_LAST;
6594
6595 for (i = 0; i < nr_args; i++) {
6596 p->ops[i].op = i;
6597 if (!io_op_defs[i].not_supported)
6598 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6599 }
6600 p->ops_len = i;
6601
6602 ret = 0;
6603 if (copy_to_user(arg, p, size))
6604 ret = -EFAULT;
6605out:
6606 kfree(p);
6607 return ret;
6608}
6609
Jens Axboeedafcce2019-01-09 09:16:05 -07006610static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6611 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006612 __releases(ctx->uring_lock)
6613 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006614{
6615 int ret;
6616
Jens Axboe35fa71a2019-04-22 10:23:23 -06006617 /*
6618 * We're inside the ring mutex, if the ref is already dying, then
6619 * someone else killed the ctx or is already going through
6620 * io_uring_register().
6621 */
6622 if (percpu_ref_is_dying(&ctx->refs))
6623 return -ENXIO;
6624
Jens Axboe05f3fb32019-12-09 11:22:50 -07006625 if (opcode != IORING_UNREGISTER_FILES &&
Jens Axboe66f4af92020-01-16 15:36:52 -07006626 opcode != IORING_REGISTER_FILES_UPDATE &&
6627 opcode != IORING_REGISTER_PROBE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006628 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006629
Jens Axboe05f3fb32019-12-09 11:22:50 -07006630 /*
6631 * Drop uring mutex before waiting for references to exit. If
6632 * another thread is currently inside io_uring_enter() it might
6633 * need to grab the uring_lock to make progress. If we hold it
6634 * here across the drain wait, then we can deadlock. It's safe
6635 * to drop the mutex here, since no new references will come in
6636 * after we've killed the percpu ref.
6637 */
6638 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006639 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006640 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006641 if (ret) {
6642 percpu_ref_resurrect(&ctx->refs);
6643 ret = -EINTR;
6644 goto out;
6645 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006646 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006647
6648 switch (opcode) {
6649 case IORING_REGISTER_BUFFERS:
6650 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6651 break;
6652 case IORING_UNREGISTER_BUFFERS:
6653 ret = -EINVAL;
6654 if (arg || nr_args)
6655 break;
6656 ret = io_sqe_buffer_unregister(ctx);
6657 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006658 case IORING_REGISTER_FILES:
6659 ret = io_sqe_files_register(ctx, arg, nr_args);
6660 break;
6661 case IORING_UNREGISTER_FILES:
6662 ret = -EINVAL;
6663 if (arg || nr_args)
6664 break;
6665 ret = io_sqe_files_unregister(ctx);
6666 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006667 case IORING_REGISTER_FILES_UPDATE:
6668 ret = io_sqe_files_update(ctx, arg, nr_args);
6669 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006670 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006671 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006672 ret = -EINVAL;
6673 if (nr_args != 1)
6674 break;
6675 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006676 if (ret)
6677 break;
6678 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6679 ctx->eventfd_async = 1;
6680 else
6681 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006682 break;
6683 case IORING_UNREGISTER_EVENTFD:
6684 ret = -EINVAL;
6685 if (arg || nr_args)
6686 break;
6687 ret = io_eventfd_unregister(ctx);
6688 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07006689 case IORING_REGISTER_PROBE:
6690 ret = -EINVAL;
6691 if (!arg || nr_args > 256)
6692 break;
6693 ret = io_probe(ctx, arg, nr_args);
6694 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006695 default:
6696 ret = -EINVAL;
6697 break;
6698 }
6699
Jens Axboe05f3fb32019-12-09 11:22:50 -07006700
6701 if (opcode != IORING_UNREGISTER_FILES &&
Jens Axboe66f4af92020-01-16 15:36:52 -07006702 opcode != IORING_REGISTER_FILES_UPDATE &&
6703 opcode != IORING_REGISTER_PROBE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006704 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006705 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07006706out:
6707 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006708 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006709 return ret;
6710}
6711
6712SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6713 void __user *, arg, unsigned int, nr_args)
6714{
6715 struct io_ring_ctx *ctx;
6716 long ret = -EBADF;
6717 struct fd f;
6718
6719 f = fdget(fd);
6720 if (!f.file)
6721 return -EBADF;
6722
6723 ret = -EOPNOTSUPP;
6724 if (f.file->f_op != &io_uring_fops)
6725 goto out_fput;
6726
6727 ctx = f.file->private_data;
6728
6729 mutex_lock(&ctx->uring_lock);
6730 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6731 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006732 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6733 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006734out_fput:
6735 fdput(f);
6736 return ret;
6737}
6738
Jens Axboe2b188cc2019-01-07 10:46:33 -07006739static int __init io_uring_init(void)
6740{
Jens Axboed3656342019-12-18 09:50:26 -07006741 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006742 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6743 return 0;
6744};
6745__initcall(io_uring_init);