blob: 50233efd9445910531edc2d9d14f98e118b0cb2f [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;
564};
565
566static const struct io_op_def io_op_defs[] = {
567 {
568 /* IORING_OP_NOP */
569 },
570 {
571 /* IORING_OP_READV */
572 .async_ctx = 1,
573 .needs_mm = 1,
574 .needs_file = 1,
575 .unbound_nonreg_file = 1,
576 },
577 {
578 /* IORING_OP_WRITEV */
579 .async_ctx = 1,
580 .needs_mm = 1,
581 .needs_file = 1,
582 .hash_reg_file = 1,
583 .unbound_nonreg_file = 1,
584 },
585 {
586 /* IORING_OP_FSYNC */
587 .needs_file = 1,
588 },
589 {
590 /* IORING_OP_READ_FIXED */
591 .needs_file = 1,
592 .unbound_nonreg_file = 1,
593 },
594 {
595 /* IORING_OP_WRITE_FIXED */
596 .needs_file = 1,
597 .hash_reg_file = 1,
598 .unbound_nonreg_file = 1,
599 },
600 {
601 /* IORING_OP_POLL_ADD */
602 .needs_file = 1,
603 .unbound_nonreg_file = 1,
604 },
605 {
606 /* IORING_OP_POLL_REMOVE */
607 },
608 {
609 /* IORING_OP_SYNC_FILE_RANGE */
610 .needs_file = 1,
611 },
612 {
613 /* IORING_OP_SENDMSG */
614 .async_ctx = 1,
615 .needs_mm = 1,
616 .needs_file = 1,
617 .unbound_nonreg_file = 1,
618 },
619 {
620 /* IORING_OP_RECVMSG */
621 .async_ctx = 1,
622 .needs_mm = 1,
623 .needs_file = 1,
624 .unbound_nonreg_file = 1,
625 },
626 {
627 /* IORING_OP_TIMEOUT */
628 .async_ctx = 1,
629 .needs_mm = 1,
630 },
631 {
632 /* IORING_OP_TIMEOUT_REMOVE */
633 },
634 {
635 /* IORING_OP_ACCEPT */
636 .needs_mm = 1,
637 .needs_file = 1,
638 .unbound_nonreg_file = 1,
639 },
640 {
641 /* IORING_OP_ASYNC_CANCEL */
642 },
643 {
644 /* IORING_OP_LINK_TIMEOUT */
645 .async_ctx = 1,
646 .needs_mm = 1,
647 },
648 {
649 /* IORING_OP_CONNECT */
650 .async_ctx = 1,
651 .needs_mm = 1,
652 .needs_file = 1,
653 .unbound_nonreg_file = 1,
654 },
655 {
656 /* IORING_OP_FALLOCATE */
657 .needs_file = 1,
658 },
659 {
660 /* IORING_OP_OPENAT */
661 .needs_file = 1,
662 .fd_non_neg = 1,
663 },
664 {
665 /* IORING_OP_CLOSE */
666 .needs_file = 1,
667 },
668 {
669 /* IORING_OP_FILES_UPDATE */
670 .needs_mm = 1,
671 },
672 {
673 /* IORING_OP_STATX */
674 .needs_mm = 1,
675 .needs_file = 1,
676 .fd_non_neg = 1,
677 },
Jens Axboe3a6820f2019-12-22 15:19:35 -0700678 {
679 /* IORING_OP_READ */
680 .needs_mm = 1,
681 .needs_file = 1,
682 .unbound_nonreg_file = 1,
683 },
684 {
685 /* IORING_OP_WRITE */
686 .needs_mm = 1,
687 .needs_file = 1,
688 .unbound_nonreg_file = 1,
689 },
Jens Axboe4840e412019-12-25 22:03:45 -0700690 {
691 /* IORING_OP_FADVISE */
692 .needs_file = 1,
693 },
Jens Axboec1ca7572019-12-25 22:18:28 -0700694 {
695 /* IORING_OP_MADVISE */
696 .needs_mm = 1,
697 },
Jens Axboefddafac2020-01-04 20:19:44 -0700698 {
699 /* IORING_OP_SEND */
700 .needs_mm = 1,
701 .needs_file = 1,
702 .unbound_nonreg_file = 1,
703 },
704 {
705 /* IORING_OP_RECV */
706 .needs_mm = 1,
707 .needs_file = 1,
708 .unbound_nonreg_file = 1,
709 },
Jens Axboecebdb982020-01-08 17:59:24 -0700710 {
711 /* IORING_OP_OPENAT2 */
712 .needs_file = 1,
713 .fd_non_neg = 1,
714 },
Jens Axboed3656342019-12-18 09:50:26 -0700715};
716
Jens Axboe561fb042019-10-24 07:25:42 -0600717static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700718static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800719static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700720static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700721static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
722static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700723static int __io_sqe_files_update(struct io_ring_ctx *ctx,
724 struct io_uring_files_update *ip,
725 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600726
Jens Axboe2b188cc2019-01-07 10:46:33 -0700727static struct kmem_cache *req_cachep;
728
729static const struct file_operations io_uring_fops;
730
731struct sock *io_uring_get_socket(struct file *file)
732{
733#if defined(CONFIG_UNIX)
734 if (file->f_op == &io_uring_fops) {
735 struct io_ring_ctx *ctx = file->private_data;
736
737 return ctx->ring_sock->sk;
738 }
739#endif
740 return NULL;
741}
742EXPORT_SYMBOL(io_uring_get_socket);
743
744static void io_ring_ctx_ref_free(struct percpu_ref *ref)
745{
746 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
747
Jens Axboe206aefd2019-11-07 18:27:42 -0700748 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700749}
750
751static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
752{
753 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700754 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700755
756 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
757 if (!ctx)
758 return NULL;
759
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700760 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
761 if (!ctx->fallback_req)
762 goto err;
763
Jens Axboe206aefd2019-11-07 18:27:42 -0700764 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
765 if (!ctx->completions)
766 goto err;
767
Jens Axboe78076bb2019-12-04 19:56:40 -0700768 /*
769 * Use 5 bits less than the max cq entries, that should give us around
770 * 32 entries per hash list if totally full and uniformly spread.
771 */
772 hash_bits = ilog2(p->cq_entries);
773 hash_bits -= 5;
774 if (hash_bits <= 0)
775 hash_bits = 1;
776 ctx->cancel_hash_bits = hash_bits;
777 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
778 GFP_KERNEL);
779 if (!ctx->cancel_hash)
780 goto err;
781 __hash_init(ctx->cancel_hash, 1U << hash_bits);
782
Roman Gushchin21482892019-05-07 10:01:48 -0700783 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700784 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
785 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700786
787 ctx->flags = p->flags;
788 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700789 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700790 init_completion(&ctx->completions[0]);
791 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700792 mutex_init(&ctx->uring_lock);
793 init_waitqueue_head(&ctx->wait);
794 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700795 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700796 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600797 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600798 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600799 init_waitqueue_head(&ctx->inflight_wait);
800 spin_lock_init(&ctx->inflight_lock);
801 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700802 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700803err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700804 if (ctx->fallback_req)
805 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700806 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700807 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700808 kfree(ctx);
809 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700810}
811
Bob Liu9d858b22019-11-13 18:06:25 +0800812static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600813{
Jackie Liua197f662019-11-08 08:09:12 -0700814 struct io_ring_ctx *ctx = req->ctx;
815
Jens Axboe498ccd92019-10-25 10:04:25 -0600816 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
817 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600818}
819
Bob Liu9d858b22019-11-13 18:06:25 +0800820static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600821{
Bob Liu9d858b22019-11-13 18:06:25 +0800822 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
823 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600824
Bob Liu9d858b22019-11-13 18:06:25 +0800825 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600826}
827
828static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600829{
830 struct io_kiocb *req;
831
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600832 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800833 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600834 list_del_init(&req->list);
835 return req;
836 }
837
838 return NULL;
839}
840
Jens Axboe5262f562019-09-17 12:26:57 -0600841static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
842{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600843 struct io_kiocb *req;
844
845 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700846 if (req) {
847 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
848 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800849 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700850 list_del_init(&req->list);
851 return req;
852 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600853 }
854
855 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600856}
857
Jens Axboede0617e2019-04-06 21:51:27 -0600858static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700859{
Hristo Venev75b28af2019-08-26 17:23:46 +0000860 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700861
Hristo Venev75b28af2019-08-26 17:23:46 +0000862 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700863 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000864 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700865
Jens Axboe2b188cc2019-01-07 10:46:33 -0700866 if (wq_has_sleeper(&ctx->cq_wait)) {
867 wake_up_interruptible(&ctx->cq_wait);
868 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
869 }
870 }
871}
872
Jens Axboe94ae5e72019-11-14 19:39:52 -0700873static inline bool io_prep_async_work(struct io_kiocb *req,
874 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600875{
Jens Axboed3656342019-12-18 09:50:26 -0700876 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600877 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600878
Jens Axboed3656342019-12-18 09:50:26 -0700879 if (req->flags & REQ_F_ISREG) {
880 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700881 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700882 } else {
883 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700884 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600885 }
Jens Axboed3656342019-12-18 09:50:26 -0700886 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700887 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600888
Jens Axboe94ae5e72019-11-14 19:39:52 -0700889 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600890 return do_hashed;
891}
892
Jackie Liua197f662019-11-08 08:09:12 -0700893static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600894{
Jackie Liua197f662019-11-08 08:09:12 -0700895 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700896 struct io_kiocb *link;
897 bool do_hashed;
898
899 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600900
901 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
902 req->flags);
903 if (!do_hashed) {
904 io_wq_enqueue(ctx->io_wq, &req->work);
905 } else {
906 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
907 file_inode(req->file));
908 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700909
910 if (link)
911 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600912}
913
Jens Axboe5262f562019-09-17 12:26:57 -0600914static void io_kill_timeout(struct io_kiocb *req)
915{
916 int ret;
917
Jens Axboe2d283902019-12-04 11:08:05 -0700918 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600919 if (ret != -1) {
920 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600921 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700922 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800923 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600924 }
925}
926
927static void io_kill_timeouts(struct io_ring_ctx *ctx)
928{
929 struct io_kiocb *req, *tmp;
930
931 spin_lock_irq(&ctx->completion_lock);
932 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
933 io_kill_timeout(req);
934 spin_unlock_irq(&ctx->completion_lock);
935}
936
Jens Axboede0617e2019-04-06 21:51:27 -0600937static void io_commit_cqring(struct io_ring_ctx *ctx)
938{
939 struct io_kiocb *req;
940
Jens Axboe5262f562019-09-17 12:26:57 -0600941 while ((req = io_get_timeout_req(ctx)) != NULL)
942 io_kill_timeout(req);
943
Jens Axboede0617e2019-04-06 21:51:27 -0600944 __io_commit_cqring(ctx);
945
946 while ((req = io_get_deferred_req(ctx)) != NULL) {
947 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700948 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600949 }
950}
951
Jens Axboe2b188cc2019-01-07 10:46:33 -0700952static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
953{
Hristo Venev75b28af2019-08-26 17:23:46 +0000954 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700955 unsigned tail;
956
957 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200958 /*
959 * writes to the cq entry need to come after reading head; the
960 * control dependency is enough as we're using WRITE_ONCE to
961 * fill the cq entry
962 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000963 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700964 return NULL;
965
966 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000967 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700968}
969
Jens Axboef2842ab2020-01-08 11:04:00 -0700970static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
971{
972 if (!ctx->eventfd_async)
973 return true;
974 return io_wq_current_is_worker() || in_interrupt();
975}
976
Jens Axboe8c838782019-03-12 15:48:16 -0600977static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
978{
979 if (waitqueue_active(&ctx->wait))
980 wake_up(&ctx->wait);
981 if (waitqueue_active(&ctx->sqo_wait))
982 wake_up(&ctx->sqo_wait);
Jens Axboef2842ab2020-01-08 11:04:00 -0700983 if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -0600984 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600985}
986
Jens Axboec4a2ed72019-11-21 21:01:26 -0700987/* Returns true if there are no backlogged entries after the flush */
988static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700989{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700990 struct io_rings *rings = ctx->rings;
991 struct io_uring_cqe *cqe;
992 struct io_kiocb *req;
993 unsigned long flags;
994 LIST_HEAD(list);
995
996 if (!force) {
997 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700998 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700999 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1000 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001001 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001002 }
1003
1004 spin_lock_irqsave(&ctx->completion_lock, flags);
1005
1006 /* if force is set, the ring is going away. always drop after that */
1007 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001008 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001009
Jens Axboec4a2ed72019-11-21 21:01:26 -07001010 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001011 while (!list_empty(&ctx->cq_overflow_list)) {
1012 cqe = io_get_cqring(ctx);
1013 if (!cqe && !force)
1014 break;
1015
1016 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1017 list);
1018 list_move(&req->list, &list);
1019 if (cqe) {
1020 WRITE_ONCE(cqe->user_data, req->user_data);
1021 WRITE_ONCE(cqe->res, req->result);
1022 WRITE_ONCE(cqe->flags, 0);
1023 } else {
1024 WRITE_ONCE(ctx->rings->cq_overflow,
1025 atomic_inc_return(&ctx->cached_cq_overflow));
1026 }
1027 }
1028
1029 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001030 if (cqe) {
1031 clear_bit(0, &ctx->sq_check_overflow);
1032 clear_bit(0, &ctx->cq_check_overflow);
1033 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001034 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1035 io_cqring_ev_posted(ctx);
1036
1037 while (!list_empty(&list)) {
1038 req = list_first_entry(&list, struct io_kiocb, list);
1039 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001040 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001041 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001042
1043 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001044}
1045
Jens Axboe78e19bb2019-11-06 15:21:34 -07001046static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001048 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049 struct io_uring_cqe *cqe;
1050
Jens Axboe78e19bb2019-11-06 15:21:34 -07001051 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001052
Jens Axboe2b188cc2019-01-07 10:46:33 -07001053 /*
1054 * If we can't get a cq entry, userspace overflowed the
1055 * submission (by quite a lot). Increment the overflow count in
1056 * the ring.
1057 */
1058 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001059 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001060 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001061 WRITE_ONCE(cqe->res, res);
1062 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001063 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001064 WRITE_ONCE(ctx->rings->cq_overflow,
1065 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001066 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001067 if (list_empty(&ctx->cq_overflow_list)) {
1068 set_bit(0, &ctx->sq_check_overflow);
1069 set_bit(0, &ctx->cq_check_overflow);
1070 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001071 refcount_inc(&req->refs);
1072 req->result = res;
1073 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074 }
1075}
1076
Jens Axboe78e19bb2019-11-06 15:21:34 -07001077static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001078{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001079 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001080 unsigned long flags;
1081
1082 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001083 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084 io_commit_cqring(ctx);
1085 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1086
Jens Axboe8c838782019-03-12 15:48:16 -06001087 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001088}
1089
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001090static inline bool io_is_fallback_req(struct io_kiocb *req)
1091{
1092 return req == (struct io_kiocb *)
1093 ((unsigned long) req->ctx->fallback_req & ~1UL);
1094}
1095
1096static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1097{
1098 struct io_kiocb *req;
1099
1100 req = ctx->fallback_req;
1101 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1102 return req;
1103
1104 return NULL;
1105}
1106
Jens Axboe2579f912019-01-09 09:10:43 -07001107static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1108 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001109{
Jens Axboefd6fab22019-03-14 16:30:06 -06001110 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001111 struct io_kiocb *req;
1112
Jens Axboe2579f912019-01-09 09:10:43 -07001113 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001114 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001115 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001116 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001117 } else if (!state->free_reqs) {
1118 size_t sz;
1119 int ret;
1120
1121 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001122 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1123
1124 /*
1125 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1126 * retry single alloc to be on the safe side.
1127 */
1128 if (unlikely(ret <= 0)) {
1129 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1130 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001131 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001132 ret = 1;
1133 }
Jens Axboe2579f912019-01-09 09:10:43 -07001134 state->free_reqs = ret - 1;
1135 state->cur_req = 1;
1136 req = state->reqs[0];
1137 } else {
1138 req = state->reqs[state->cur_req];
1139 state->free_reqs--;
1140 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001141 }
1142
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001143got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001144 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001145 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001146 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001147 req->ctx = ctx;
1148 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001149 /* one is dropped after submission, the other at completion */
1150 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001151 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001152 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001153 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001154fallback:
1155 req = io_get_fallback_req(ctx);
1156 if (req)
1157 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001158 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001159 return NULL;
1160}
1161
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001162static void __io_req_do_free(struct io_kiocb *req)
1163{
1164 if (likely(!io_is_fallback_req(req)))
1165 kmem_cache_free(req_cachep, req);
1166 else
1167 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1168}
1169
Jens Axboec6ca97b302019-12-28 12:11:08 -07001170static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001171{
Jens Axboefcb323c2019-10-24 12:39:47 -06001172 struct io_ring_ctx *ctx = req->ctx;
1173
YueHaibing96fd84d2020-01-07 22:22:44 +08001174 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001175 if (req->file) {
1176 if (req->flags & REQ_F_FIXED_FILE)
1177 percpu_ref_put(&ctx->file_data->refs);
1178 else
1179 fput(req->file);
1180 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001181}
1182
1183static void __io_free_req(struct io_kiocb *req)
1184{
1185 __io_req_aux_free(req);
1186
Jens Axboefcb323c2019-10-24 12:39:47 -06001187 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001188 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001189 unsigned long flags;
1190
1191 spin_lock_irqsave(&ctx->inflight_lock, flags);
1192 list_del(&req->inflight_entry);
1193 if (waitqueue_active(&ctx->inflight_wait))
1194 wake_up(&ctx->inflight_wait);
1195 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1196 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001197
1198 percpu_ref_put(&req->ctx->refs);
1199 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001200}
1201
Jens Axboec6ca97b302019-12-28 12:11:08 -07001202struct req_batch {
1203 void *reqs[IO_IOPOLL_BATCH];
1204 int to_free;
1205 int need_iter;
1206};
1207
1208static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1209{
1210 if (!rb->to_free)
1211 return;
1212 if (rb->need_iter) {
1213 int i, inflight = 0;
1214 unsigned long flags;
1215
1216 for (i = 0; i < rb->to_free; i++) {
1217 struct io_kiocb *req = rb->reqs[i];
1218
1219 if (req->flags & REQ_F_FIXED_FILE)
1220 req->file = NULL;
1221 if (req->flags & REQ_F_INFLIGHT)
1222 inflight++;
1223 else
1224 rb->reqs[i] = NULL;
1225 __io_req_aux_free(req);
1226 }
1227 if (!inflight)
1228 goto do_free;
1229
1230 spin_lock_irqsave(&ctx->inflight_lock, flags);
1231 for (i = 0; i < rb->to_free; i++) {
1232 struct io_kiocb *req = rb->reqs[i];
1233
1234 if (req) {
1235 list_del(&req->inflight_entry);
1236 if (!--inflight)
1237 break;
1238 }
1239 }
1240 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1241
1242 if (waitqueue_active(&ctx->inflight_wait))
1243 wake_up(&ctx->inflight_wait);
1244 }
1245do_free:
1246 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1247 percpu_ref_put_many(&ctx->refs, rb->to_free);
1248 percpu_ref_put_many(&ctx->file_data->refs, rb->to_free);
1249 rb->to_free = rb->need_iter = 0;
1250}
1251
Jackie Liua197f662019-11-08 08:09:12 -07001252static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001253{
Jackie Liua197f662019-11-08 08:09:12 -07001254 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001255 int ret;
1256
Jens Axboe2d283902019-12-04 11:08:05 -07001257 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001258 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001259 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001260 io_commit_cqring(ctx);
1261 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001262 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001263 return true;
1264 }
1265
1266 return false;
1267}
1268
Jens Axboeba816ad2019-09-28 11:36:45 -06001269static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001270{
Jens Axboe2665abf2019-11-05 12:40:47 -07001271 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001272 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001273
Jens Axboe4d7dd462019-11-20 13:03:52 -07001274 /* Already got next link */
1275 if (req->flags & REQ_F_LINK_NEXT)
1276 return;
1277
Jens Axboe9e645e112019-05-10 16:07:28 -06001278 /*
1279 * The list should never be empty when we are called here. But could
1280 * potentially happen if the chain is messed up, check to be on the
1281 * safe side.
1282 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001283 while (!list_empty(&req->link_list)) {
1284 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1285 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001286
Pavel Begunkov44932332019-12-05 16:16:35 +03001287 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1288 (nxt->flags & REQ_F_TIMEOUT))) {
1289 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001290 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001291 req->flags &= ~REQ_F_LINK_TIMEOUT;
1292 continue;
1293 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001294
Pavel Begunkov44932332019-12-05 16:16:35 +03001295 list_del_init(&req->link_list);
1296 if (!list_empty(&nxt->link_list))
1297 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001298 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001299 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001300 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001301
Jens Axboe4d7dd462019-11-20 13:03:52 -07001302 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001303 if (wake_ev)
1304 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001305}
1306
1307/*
1308 * Called if REQ_F_LINK is set, and we fail the head request
1309 */
1310static void io_fail_links(struct io_kiocb *req)
1311{
Jens Axboe2665abf2019-11-05 12:40:47 -07001312 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001313 unsigned long flags;
1314
1315 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001316
1317 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001318 struct io_kiocb *link = list_first_entry(&req->link_list,
1319 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001320
Pavel Begunkov44932332019-12-05 16:16:35 +03001321 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001322 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001323
1324 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001325 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001326 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001327 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001328 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001329 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001330 }
Jens Axboe5d960722019-11-19 15:31:28 -07001331 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001332 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001333
1334 io_commit_cqring(ctx);
1335 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1336 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001337}
1338
Jens Axboe4d7dd462019-11-20 13:03:52 -07001339static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001340{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001341 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001342 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001343
Jens Axboe9e645e112019-05-10 16:07:28 -06001344 /*
1345 * If LINK is set, we have dependent requests in this chain. If we
1346 * didn't fail this request, queue the first one up, moving any other
1347 * dependencies to the next request. In case of failure, fail the rest
1348 * of the chain.
1349 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001350 if (req->flags & REQ_F_FAIL_LINK) {
1351 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001352 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1353 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001354 struct io_ring_ctx *ctx = req->ctx;
1355 unsigned long flags;
1356
1357 /*
1358 * If this is a timeout link, we could be racing with the
1359 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001360 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001361 */
1362 spin_lock_irqsave(&ctx->completion_lock, flags);
1363 io_req_link_next(req, nxt);
1364 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1365 } else {
1366 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001367 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001368}
Jens Axboe9e645e112019-05-10 16:07:28 -06001369
Jackie Liuc69f8db2019-11-09 11:00:08 +08001370static void io_free_req(struct io_kiocb *req)
1371{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001372 struct io_kiocb *nxt = NULL;
1373
1374 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001375 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001376
1377 if (nxt)
1378 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001379}
1380
Jens Axboeba816ad2019-09-28 11:36:45 -06001381/*
1382 * Drop reference to request, return next in chain (if there is one) if this
1383 * was the last reference to this request.
1384 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001385__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001386static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001387{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001388 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001389
Jens Axboee65ef562019-03-12 10:16:44 -06001390 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001391 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001392}
1393
Jens Axboe2b188cc2019-01-07 10:46:33 -07001394static void io_put_req(struct io_kiocb *req)
1395{
Jens Axboedef596e2019-01-09 08:59:42 -07001396 if (refcount_dec_and_test(&req->refs))
1397 io_free_req(req);
1398}
1399
Jens Axboe978db572019-11-14 22:39:04 -07001400/*
1401 * Must only be used if we don't need to care about links, usually from
1402 * within the completion handling itself.
1403 */
1404static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001405{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001406 /* drop both submit and complete references */
1407 if (refcount_sub_and_test(2, &req->refs))
1408 __io_free_req(req);
1409}
1410
Jens Axboe978db572019-11-14 22:39:04 -07001411static void io_double_put_req(struct io_kiocb *req)
1412{
1413 /* drop both submit and complete references */
1414 if (refcount_sub_and_test(2, &req->refs))
1415 io_free_req(req);
1416}
1417
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001418static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001419{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001420 struct io_rings *rings = ctx->rings;
1421
Jens Axboead3eb2c2019-12-18 17:12:20 -07001422 if (test_bit(0, &ctx->cq_check_overflow)) {
1423 /*
1424 * noflush == true is from the waitqueue handler, just ensure
1425 * we wake up the task, and the next invocation will flush the
1426 * entries. We cannot safely to it from here.
1427 */
1428 if (noflush && !list_empty(&ctx->cq_overflow_list))
1429 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001430
Jens Axboead3eb2c2019-12-18 17:12:20 -07001431 io_cqring_overflow_flush(ctx, false);
1432 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001433
Jens Axboea3a0e432019-08-20 11:03:11 -06001434 /* See comment at the top of this file */
1435 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001436 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001437}
1438
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001439static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1440{
1441 struct io_rings *rings = ctx->rings;
1442
1443 /* make sure SQ entry isn't read before tail */
1444 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1445}
1446
Jens Axboe8237e042019-12-28 10:48:22 -07001447static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001448{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001449 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1450 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001451
Jens Axboec6ca97b302019-12-28 12:11:08 -07001452 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1453 rb->need_iter++;
1454
1455 rb->reqs[rb->to_free++] = req;
1456 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1457 io_free_req_many(req->ctx, rb);
1458 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001459}
1460
Jens Axboedef596e2019-01-09 08:59:42 -07001461/*
1462 * Find and free completed poll iocbs
1463 */
1464static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1465 struct list_head *done)
1466{
Jens Axboe8237e042019-12-28 10:48:22 -07001467 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001468 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001469
Jens Axboec6ca97b302019-12-28 12:11:08 -07001470 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001471 while (!list_empty(done)) {
1472 req = list_first_entry(done, struct io_kiocb, list);
1473 list_del(&req->list);
1474
Jens Axboe78e19bb2019-11-06 15:21:34 -07001475 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001476 (*nr_events)++;
1477
Jens Axboe8237e042019-12-28 10:48:22 -07001478 if (refcount_dec_and_test(&req->refs) &&
1479 !io_req_multi_free(&rb, req))
1480 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001481 }
Jens Axboedef596e2019-01-09 08:59:42 -07001482
Jens Axboe09bb8392019-03-13 12:39:28 -06001483 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001484 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001485}
1486
1487static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1488 long min)
1489{
1490 struct io_kiocb *req, *tmp;
1491 LIST_HEAD(done);
1492 bool spin;
1493 int ret;
1494
1495 /*
1496 * Only spin for completions if we don't have multiple devices hanging
1497 * off our complete list, and we're under the requested amount.
1498 */
1499 spin = !ctx->poll_multi_file && *nr_events < min;
1500
1501 ret = 0;
1502 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001503 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001504
1505 /*
1506 * Move completed entries to our local list. If we find a
1507 * request that requires polling, break out and complete
1508 * the done list first, if we have entries there.
1509 */
1510 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1511 list_move_tail(&req->list, &done);
1512 continue;
1513 }
1514 if (!list_empty(&done))
1515 break;
1516
1517 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1518 if (ret < 0)
1519 break;
1520
1521 if (ret && spin)
1522 spin = false;
1523 ret = 0;
1524 }
1525
1526 if (!list_empty(&done))
1527 io_iopoll_complete(ctx, nr_events, &done);
1528
1529 return ret;
1530}
1531
1532/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001533 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001534 * non-spinning poll check - we'll still enter the driver poll loop, but only
1535 * as a non-spinning completion check.
1536 */
1537static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1538 long min)
1539{
Jens Axboe08f54392019-08-21 22:19:11 -06001540 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001541 int ret;
1542
1543 ret = io_do_iopoll(ctx, nr_events, min);
1544 if (ret < 0)
1545 return ret;
1546 if (!min || *nr_events >= min)
1547 return 0;
1548 }
1549
1550 return 1;
1551}
1552
1553/*
1554 * We can't just wait for polled events to come to us, we have to actively
1555 * find and complete them.
1556 */
1557static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1558{
1559 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1560 return;
1561
1562 mutex_lock(&ctx->uring_lock);
1563 while (!list_empty(&ctx->poll_list)) {
1564 unsigned int nr_events = 0;
1565
1566 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001567
1568 /*
1569 * Ensure we allow local-to-the-cpu processing to take place,
1570 * in this case we need to ensure that we reap all events.
1571 */
1572 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001573 }
1574 mutex_unlock(&ctx->uring_lock);
1575}
1576
Jens Axboe2b2ed972019-10-25 10:06:15 -06001577static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1578 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001579{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001580 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001581
1582 do {
1583 int tmin = 0;
1584
Jens Axboe500f9fb2019-08-19 12:15:59 -06001585 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001586 * Don't enter poll loop if we already have events pending.
1587 * If we do, we can potentially be spinning for commands that
1588 * already triggered a CQE (eg in error).
1589 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001590 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001591 break;
1592
1593 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001594 * If a submit got punted to a workqueue, we can have the
1595 * application entering polling for a command before it gets
1596 * issued. That app will hold the uring_lock for the duration
1597 * of the poll right here, so we need to take a breather every
1598 * now and then to ensure that the issue has a chance to add
1599 * the poll to the issued list. Otherwise we can spin here
1600 * forever, while the workqueue is stuck trying to acquire the
1601 * very same mutex.
1602 */
1603 if (!(++iters & 7)) {
1604 mutex_unlock(&ctx->uring_lock);
1605 mutex_lock(&ctx->uring_lock);
1606 }
1607
Jens Axboedef596e2019-01-09 08:59:42 -07001608 if (*nr_events < min)
1609 tmin = min - *nr_events;
1610
1611 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1612 if (ret <= 0)
1613 break;
1614 ret = 0;
1615 } while (min && !*nr_events && !need_resched());
1616
Jens Axboe2b2ed972019-10-25 10:06:15 -06001617 return ret;
1618}
1619
1620static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1621 long min)
1622{
1623 int ret;
1624
1625 /*
1626 * We disallow the app entering submit/complete with polling, but we
1627 * still need to lock the ring to prevent racing with polled issue
1628 * that got punted to a workqueue.
1629 */
1630 mutex_lock(&ctx->uring_lock);
1631 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001632 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001633 return ret;
1634}
1635
Jens Axboe491381ce2019-10-17 09:20:46 -06001636static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001637{
Jens Axboe491381ce2019-10-17 09:20:46 -06001638 /*
1639 * Tell lockdep we inherited freeze protection from submission
1640 * thread.
1641 */
1642 if (req->flags & REQ_F_ISREG) {
1643 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001644
Jens Axboe491381ce2019-10-17 09:20:46 -06001645 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001646 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001647 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001648}
1649
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001650static inline void req_set_fail_links(struct io_kiocb *req)
1651{
1652 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1653 req->flags |= REQ_F_FAIL_LINK;
1654}
1655
Jens Axboeba816ad2019-09-28 11:36:45 -06001656static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001657{
Jens Axboe9adbd452019-12-20 08:45:55 -07001658 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001659
Jens Axboe491381ce2019-10-17 09:20:46 -06001660 if (kiocb->ki_flags & IOCB_WRITE)
1661 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001662
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001663 if (res != req->result)
1664 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001665 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001666}
1667
1668static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1669{
Jens Axboe9adbd452019-12-20 08:45:55 -07001670 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001671
1672 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001673 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674}
1675
Jens Axboeba816ad2019-09-28 11:36:45 -06001676static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1677{
Jens Axboe9adbd452019-12-20 08:45:55 -07001678 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001679 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001680
1681 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001682 io_put_req_find_next(req, &nxt);
1683
1684 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001685}
1686
Jens Axboedef596e2019-01-09 08:59:42 -07001687static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1688{
Jens Axboe9adbd452019-12-20 08:45:55 -07001689 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001690
Jens Axboe491381ce2019-10-17 09:20:46 -06001691 if (kiocb->ki_flags & IOCB_WRITE)
1692 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001693
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001694 if (res != req->result)
1695 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001696 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001697 if (res != -EAGAIN)
1698 req->flags |= REQ_F_IOPOLL_COMPLETED;
1699}
1700
1701/*
1702 * After the iocb has been issued, it's safe to be found on the poll list.
1703 * Adding the kiocb to the list AFTER submission ensures that we don't
1704 * find it from a io_iopoll_getevents() thread before the issuer is done
1705 * accessing the kiocb cookie.
1706 */
1707static void io_iopoll_req_issued(struct io_kiocb *req)
1708{
1709 struct io_ring_ctx *ctx = req->ctx;
1710
1711 /*
1712 * Track whether we have multiple files in our lists. This will impact
1713 * how we do polling eventually, not spinning if we're on potentially
1714 * different devices.
1715 */
1716 if (list_empty(&ctx->poll_list)) {
1717 ctx->poll_multi_file = false;
1718 } else if (!ctx->poll_multi_file) {
1719 struct io_kiocb *list_req;
1720
1721 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1722 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001723 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001724 ctx->poll_multi_file = true;
1725 }
1726
1727 /*
1728 * For fast devices, IO may have already completed. If it has, add
1729 * it to the front so we find it first.
1730 */
1731 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1732 list_add(&req->list, &ctx->poll_list);
1733 else
1734 list_add_tail(&req->list, &ctx->poll_list);
1735}
1736
Jens Axboe3d6770f2019-04-13 11:50:54 -06001737static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001738{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001739 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001740 int diff = state->has_refs - state->used_refs;
1741
1742 if (diff)
1743 fput_many(state->file, diff);
1744 state->file = NULL;
1745 }
1746}
1747
1748/*
1749 * Get as many references to a file as we have IOs left in this submission,
1750 * assuming most submissions are for one file, or at least that each file
1751 * has more than one submission.
1752 */
1753static struct file *io_file_get(struct io_submit_state *state, int fd)
1754{
1755 if (!state)
1756 return fget(fd);
1757
1758 if (state->file) {
1759 if (state->fd == fd) {
1760 state->used_refs++;
1761 state->ios_left--;
1762 return state->file;
1763 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001764 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001765 }
1766 state->file = fget_many(fd, state->ios_left);
1767 if (!state->file)
1768 return NULL;
1769
1770 state->fd = fd;
1771 state->has_refs = state->ios_left;
1772 state->used_refs = 1;
1773 state->ios_left--;
1774 return state->file;
1775}
1776
Jens Axboe2b188cc2019-01-07 10:46:33 -07001777/*
1778 * If we tracked the file through the SCM inflight mechanism, we could support
1779 * any file. For now, just ensure that anything potentially problematic is done
1780 * inline.
1781 */
1782static bool io_file_supports_async(struct file *file)
1783{
1784 umode_t mode = file_inode(file)->i_mode;
1785
Jens Axboe10d59342019-12-09 20:16:22 -07001786 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001787 return true;
1788 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1789 return true;
1790
1791 return false;
1792}
1793
Jens Axboe3529d8c2019-12-19 18:24:38 -07001794static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1795 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001796{
Jens Axboedef596e2019-01-09 08:59:42 -07001797 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001798 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001799 unsigned ioprio;
1800 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001801
Jens Axboe09bb8392019-03-13 12:39:28 -06001802 if (!req->file)
1803 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001804
Jens Axboe491381ce2019-10-17 09:20:46 -06001805 if (S_ISREG(file_inode(req->file)->i_mode))
1806 req->flags |= REQ_F_ISREG;
1807
Jens Axboe2b188cc2019-01-07 10:46:33 -07001808 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001809 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1810 req->flags |= REQ_F_CUR_POS;
1811 kiocb->ki_pos = req->file->f_pos;
1812 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001813 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1814 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1815
1816 ioprio = READ_ONCE(sqe->ioprio);
1817 if (ioprio) {
1818 ret = ioprio_check_cap(ioprio);
1819 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001820 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001821
1822 kiocb->ki_ioprio = ioprio;
1823 } else
1824 kiocb->ki_ioprio = get_current_ioprio();
1825
1826 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1827 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001828 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001829
1830 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001831 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1832 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001833 req->flags |= REQ_F_NOWAIT;
1834
1835 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001836 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001837
Jens Axboedef596e2019-01-09 08:59:42 -07001838 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001839 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1840 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001841 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001842
Jens Axboedef596e2019-01-09 08:59:42 -07001843 kiocb->ki_flags |= IOCB_HIPRI;
1844 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001845 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001846 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001847 if (kiocb->ki_flags & IOCB_HIPRI)
1848 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001849 kiocb->ki_complete = io_complete_rw;
1850 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001851
Jens Axboe3529d8c2019-12-19 18:24:38 -07001852 req->rw.addr = READ_ONCE(sqe->addr);
1853 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001854 /* we own ->private, reuse it for the buffer index */
1855 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001856 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001857 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001858}
1859
1860static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1861{
1862 switch (ret) {
1863 case -EIOCBQUEUED:
1864 break;
1865 case -ERESTARTSYS:
1866 case -ERESTARTNOINTR:
1867 case -ERESTARTNOHAND:
1868 case -ERESTART_RESTARTBLOCK:
1869 /*
1870 * We can't just restart the syscall, since previously
1871 * submitted sqes may already be in progress. Just fail this
1872 * IO with EINTR.
1873 */
1874 ret = -EINTR;
1875 /* fall through */
1876 default:
1877 kiocb->ki_complete(kiocb, ret, 0);
1878 }
1879}
1880
Jens Axboeba816ad2019-09-28 11:36:45 -06001881static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1882 bool in_async)
1883{
Jens Axboeba042912019-12-25 16:33:42 -07001884 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1885
1886 if (req->flags & REQ_F_CUR_POS)
1887 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001888 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001889 *nxt = __io_complete_rw(kiocb, ret);
1890 else
1891 io_rw_done(kiocb, ret);
1892}
1893
Jens Axboe9adbd452019-12-20 08:45:55 -07001894static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001895 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001896{
Jens Axboe9adbd452019-12-20 08:45:55 -07001897 struct io_ring_ctx *ctx = req->ctx;
1898 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001899 struct io_mapped_ubuf *imu;
1900 unsigned index, buf_index;
1901 size_t offset;
1902 u64 buf_addr;
1903
1904 /* attempt to use fixed buffers without having provided iovecs */
1905 if (unlikely(!ctx->user_bufs))
1906 return -EFAULT;
1907
Jens Axboe9adbd452019-12-20 08:45:55 -07001908 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001909 if (unlikely(buf_index >= ctx->nr_user_bufs))
1910 return -EFAULT;
1911
1912 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1913 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001914 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001915
1916 /* overflow */
1917 if (buf_addr + len < buf_addr)
1918 return -EFAULT;
1919 /* not inside the mapped region */
1920 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1921 return -EFAULT;
1922
1923 /*
1924 * May not be a start of buffer, set size appropriately
1925 * and advance us to the beginning.
1926 */
1927 offset = buf_addr - imu->ubuf;
1928 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001929
1930 if (offset) {
1931 /*
1932 * Don't use iov_iter_advance() here, as it's really slow for
1933 * using the latter parts of a big fixed buffer - it iterates
1934 * over each segment manually. We can cheat a bit here, because
1935 * we know that:
1936 *
1937 * 1) it's a BVEC iter, we set it up
1938 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1939 * first and last bvec
1940 *
1941 * So just find our index, and adjust the iterator afterwards.
1942 * If the offset is within the first bvec (or the whole first
1943 * bvec, just use iov_iter_advance(). This makes it easier
1944 * since we can just skip the first segment, which may not
1945 * be PAGE_SIZE aligned.
1946 */
1947 const struct bio_vec *bvec = imu->bvec;
1948
1949 if (offset <= bvec->bv_len) {
1950 iov_iter_advance(iter, offset);
1951 } else {
1952 unsigned long seg_skip;
1953
1954 /* skip first vec */
1955 offset -= bvec->bv_len;
1956 seg_skip = 1 + (offset >> PAGE_SHIFT);
1957
1958 iter->bvec = bvec + seg_skip;
1959 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001960 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001961 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001962 }
1963 }
1964
Jens Axboe5e559562019-11-13 16:12:46 -07001965 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001966}
1967
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001968static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1969 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001970{
Jens Axboe9adbd452019-12-20 08:45:55 -07001971 void __user *buf = u64_to_user_ptr(req->rw.addr);
1972 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001973 u8 opcode;
1974
Jens Axboed625c6e2019-12-17 19:53:05 -07001975 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001976 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001977 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001978 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001979 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001980
Jens Axboe9adbd452019-12-20 08:45:55 -07001981 /* buffer index only valid with fixed read/write */
1982 if (req->rw.kiocb.private)
1983 return -EINVAL;
1984
Jens Axboe3a6820f2019-12-22 15:19:35 -07001985 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1986 ssize_t ret;
1987 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1988 *iovec = NULL;
1989 return ret;
1990 }
1991
Jens Axboef67676d2019-12-02 11:03:47 -07001992 if (req->io) {
1993 struct io_async_rw *iorw = &req->io->rw;
1994
1995 *iovec = iorw->iov;
1996 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1997 if (iorw->iov == iorw->fast_iov)
1998 *iovec = NULL;
1999 return iorw->size;
2000 }
2001
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002002 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002003 return -EFAULT;
2004
2005#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002006 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002007 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2008 iovec, iter);
2009#endif
2010
2011 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2012}
2013
Jens Axboe32960612019-09-23 11:05:34 -06002014/*
2015 * For files that don't have ->read_iter() and ->write_iter(), handle them
2016 * by looping over ->read() or ->write() manually.
2017 */
2018static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2019 struct iov_iter *iter)
2020{
2021 ssize_t ret = 0;
2022
2023 /*
2024 * Don't support polled IO through this interface, and we can't
2025 * support non-blocking either. For the latter, this just causes
2026 * the kiocb to be handled from an async context.
2027 */
2028 if (kiocb->ki_flags & IOCB_HIPRI)
2029 return -EOPNOTSUPP;
2030 if (kiocb->ki_flags & IOCB_NOWAIT)
2031 return -EAGAIN;
2032
2033 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002034 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002035 ssize_t nr;
2036
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002037 if (!iov_iter_is_bvec(iter)) {
2038 iovec = iov_iter_iovec(iter);
2039 } else {
2040 /* fixed buffers import bvec */
2041 iovec.iov_base = kmap(iter->bvec->bv_page)
2042 + iter->iov_offset;
2043 iovec.iov_len = min(iter->count,
2044 iter->bvec->bv_len - iter->iov_offset);
2045 }
2046
Jens Axboe32960612019-09-23 11:05:34 -06002047 if (rw == READ) {
2048 nr = file->f_op->read(file, iovec.iov_base,
2049 iovec.iov_len, &kiocb->ki_pos);
2050 } else {
2051 nr = file->f_op->write(file, iovec.iov_base,
2052 iovec.iov_len, &kiocb->ki_pos);
2053 }
2054
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002055 if (iov_iter_is_bvec(iter))
2056 kunmap(iter->bvec->bv_page);
2057
Jens Axboe32960612019-09-23 11:05:34 -06002058 if (nr < 0) {
2059 if (!ret)
2060 ret = nr;
2061 break;
2062 }
2063 ret += nr;
2064 if (nr != iovec.iov_len)
2065 break;
2066 iov_iter_advance(iter, nr);
2067 }
2068
2069 return ret;
2070}
2071
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002072static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002073 struct iovec *iovec, struct iovec *fast_iov,
2074 struct iov_iter *iter)
2075{
2076 req->io->rw.nr_segs = iter->nr_segs;
2077 req->io->rw.size = io_size;
2078 req->io->rw.iov = iovec;
2079 if (!req->io->rw.iov) {
2080 req->io->rw.iov = req->io->rw.fast_iov;
2081 memcpy(req->io->rw.iov, fast_iov,
2082 sizeof(struct iovec) * iter->nr_segs);
2083 }
2084}
2085
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002086static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002087{
Jens Axboed3656342019-12-18 09:50:26 -07002088 if (!io_op_defs[req->opcode].async_ctx)
2089 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002090 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002091 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002092}
2093
2094static void io_rw_async(struct io_wq_work **workptr)
2095{
2096 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2097 struct iovec *iov = NULL;
2098
2099 if (req->io->rw.iov != req->io->rw.fast_iov)
2100 iov = req->io->rw.iov;
2101 io_wq_submit_work(workptr);
2102 kfree(iov);
2103}
2104
2105static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2106 struct iovec *iovec, struct iovec *fast_iov,
2107 struct iov_iter *iter)
2108{
Jens Axboe74566df2020-01-13 19:23:24 -07002109 if (req->opcode == IORING_OP_READ_FIXED ||
2110 req->opcode == IORING_OP_WRITE_FIXED)
2111 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002112 if (!req->io && io_alloc_async_ctx(req))
2113 return -ENOMEM;
2114
2115 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2116 req->work.func = io_rw_async;
2117 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002118}
2119
Jens Axboe3529d8c2019-12-19 18:24:38 -07002120static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2121 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002122{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002123 struct io_async_ctx *io;
2124 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002125 ssize_t ret;
2126
Jens Axboe3529d8c2019-12-19 18:24:38 -07002127 ret = io_prep_rw(req, sqe, force_nonblock);
2128 if (ret)
2129 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002130
Jens Axboe3529d8c2019-12-19 18:24:38 -07002131 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2132 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002133
Jens Axboe3529d8c2019-12-19 18:24:38 -07002134 if (!req->io)
2135 return 0;
2136
2137 io = req->io;
2138 io->rw.iov = io->rw.fast_iov;
2139 req->io = NULL;
2140 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2141 req->io = io;
2142 if (ret < 0)
2143 return ret;
2144
2145 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2146 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002147}
2148
Pavel Begunkov267bc902019-11-07 01:41:08 +03002149static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002150 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002151{
2152 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002153 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002154 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002155 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002156 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002157
Jens Axboe3529d8c2019-12-19 18:24:38 -07002158 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002159 if (ret < 0)
2160 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002161
Jens Axboefd6c2e42019-12-18 12:19:41 -07002162 /* Ensure we clear previously set non-block flag */
2163 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002164 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002165
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002166 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002167 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002168 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002169 req->result = io_size;
2170
2171 /*
2172 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2173 * we know to async punt it even if it was opened O_NONBLOCK
2174 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002175 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002176 req->flags |= REQ_F_MUST_PUNT;
2177 goto copy_iov;
2178 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002179
Jens Axboe31b51512019-01-18 22:56:34 -07002180 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002181 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002182 if (!ret) {
2183 ssize_t ret2;
2184
Jens Axboe9adbd452019-12-20 08:45:55 -07002185 if (req->file->f_op->read_iter)
2186 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002187 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002188 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002189
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002190 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002191 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002192 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002193 } else {
2194copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002195 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002196 inline_vecs, &iter);
2197 if (ret)
2198 goto out_free;
2199 return -EAGAIN;
2200 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002201 }
Jens Axboef67676d2019-12-02 11:03:47 -07002202out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002203 if (!io_wq_current_is_worker())
2204 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002205 return ret;
2206}
2207
Jens Axboe3529d8c2019-12-19 18:24:38 -07002208static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2209 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002210{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002211 struct io_async_ctx *io;
2212 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002213 ssize_t ret;
2214
Jens Axboe3529d8c2019-12-19 18:24:38 -07002215 ret = io_prep_rw(req, sqe, force_nonblock);
2216 if (ret)
2217 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002218
Jens Axboe3529d8c2019-12-19 18:24:38 -07002219 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2220 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002221
Jens Axboe3529d8c2019-12-19 18:24:38 -07002222 if (!req->io)
2223 return 0;
2224
2225 io = req->io;
2226 io->rw.iov = io->rw.fast_iov;
2227 req->io = NULL;
2228 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2229 req->io = io;
2230 if (ret < 0)
2231 return ret;
2232
2233 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2234 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002235}
2236
Pavel Begunkov267bc902019-11-07 01:41:08 +03002237static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002238 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002239{
2240 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002241 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002242 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002243 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002244 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002245
Jens Axboe3529d8c2019-12-19 18:24:38 -07002246 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002247 if (ret < 0)
2248 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002249
Jens Axboefd6c2e42019-12-18 12:19:41 -07002250 /* Ensure we clear previously set non-block flag */
2251 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002252 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002253
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002254 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002255 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002256 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002257 req->result = io_size;
2258
2259 /*
2260 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2261 * we know to async punt it even if it was opened O_NONBLOCK
2262 */
2263 if (force_nonblock && !io_file_supports_async(req->file)) {
2264 req->flags |= REQ_F_MUST_PUNT;
2265 goto copy_iov;
2266 }
2267
Jens Axboe10d59342019-12-09 20:16:22 -07002268 /* file path doesn't support NOWAIT for non-direct_IO */
2269 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2270 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002271 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002272
Jens Axboe31b51512019-01-18 22:56:34 -07002273 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002274 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002275 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002276 ssize_t ret2;
2277
Jens Axboe2b188cc2019-01-07 10:46:33 -07002278 /*
2279 * Open-code file_start_write here to grab freeze protection,
2280 * which will be released by another thread in
2281 * io_complete_rw(). Fool lockdep by telling it the lock got
2282 * released so that it doesn't complain about the held lock when
2283 * we return to userspace.
2284 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002285 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002286 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002287 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002288 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002289 SB_FREEZE_WRITE);
2290 }
2291 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002292
Jens Axboe9adbd452019-12-20 08:45:55 -07002293 if (req->file->f_op->write_iter)
2294 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002295 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002296 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002297 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002298 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002299 } else {
2300copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002301 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002302 inline_vecs, &iter);
2303 if (ret)
2304 goto out_free;
2305 return -EAGAIN;
2306 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002307 }
Jens Axboe31b51512019-01-18 22:56:34 -07002308out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002309 if (!io_wq_current_is_worker())
2310 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002311 return ret;
2312}
2313
2314/*
2315 * IORING_OP_NOP just posts a completion event, nothing else.
2316 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002317static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002318{
2319 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002320
Jens Axboedef596e2019-01-09 08:59:42 -07002321 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2322 return -EINVAL;
2323
Jens Axboe78e19bb2019-11-06 15:21:34 -07002324 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002325 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002326 return 0;
2327}
2328
Jens Axboe3529d8c2019-12-19 18:24:38 -07002329static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002330{
Jens Axboe6b063142019-01-10 22:13:58 -07002331 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002332
Jens Axboe09bb8392019-03-13 12:39:28 -06002333 if (!req->file)
2334 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002335
Jens Axboe6b063142019-01-10 22:13:58 -07002336 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002337 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002338 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002339 return -EINVAL;
2340
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002341 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2342 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2343 return -EINVAL;
2344
2345 req->sync.off = READ_ONCE(sqe->off);
2346 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002347 return 0;
2348}
2349
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002350static bool io_req_cancelled(struct io_kiocb *req)
2351{
2352 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2353 req_set_fail_links(req);
2354 io_cqring_add_event(req, -ECANCELED);
2355 io_put_req(req);
2356 return true;
2357 }
2358
2359 return false;
2360}
2361
Jens Axboe78912932020-01-14 22:09:06 -07002362static void io_link_work_cb(struct io_wq_work **workptr)
2363{
2364 struct io_wq_work *work = *workptr;
2365 struct io_kiocb *link = work->data;
2366
2367 io_queue_linked_timeout(link);
2368 work->func = io_wq_submit_work;
2369}
2370
2371static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2372{
2373 struct io_kiocb *link;
2374
2375 io_prep_async_work(nxt, &link);
2376 *workptr = &nxt->work;
2377 if (link) {
2378 nxt->work.flags |= IO_WQ_WORK_CB;
2379 nxt->work.func = io_link_work_cb;
2380 nxt->work.data = link;
2381 }
2382}
2383
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002384static void io_fsync_finish(struct io_wq_work **workptr)
2385{
2386 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2387 loff_t end = req->sync.off + req->sync.len;
2388 struct io_kiocb *nxt = NULL;
2389 int ret;
2390
2391 if (io_req_cancelled(req))
2392 return;
2393
Jens Axboe9adbd452019-12-20 08:45:55 -07002394 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002395 end > 0 ? end : LLONG_MAX,
2396 req->sync.flags & IORING_FSYNC_DATASYNC);
2397 if (ret < 0)
2398 req_set_fail_links(req);
2399 io_cqring_add_event(req, ret);
2400 io_put_req_find_next(req, &nxt);
2401 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002402 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002403}
2404
Jens Axboefc4df992019-12-10 14:38:45 -07002405static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2406 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002407{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002408 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002409
2410 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002411 if (force_nonblock) {
2412 io_put_req(req);
2413 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002414 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002415 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002416
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002417 work = old_work = &req->work;
2418 io_fsync_finish(&work);
2419 if (work && work != old_work)
2420 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002421 return 0;
2422}
2423
Jens Axboed63d1b52019-12-10 10:38:56 -07002424static void io_fallocate_finish(struct io_wq_work **workptr)
2425{
2426 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2427 struct io_kiocb *nxt = NULL;
2428 int ret;
2429
2430 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2431 req->sync.len);
2432 if (ret < 0)
2433 req_set_fail_links(req);
2434 io_cqring_add_event(req, ret);
2435 io_put_req_find_next(req, &nxt);
2436 if (nxt)
2437 io_wq_assign_next(workptr, nxt);
2438}
2439
2440static int io_fallocate_prep(struct io_kiocb *req,
2441 const struct io_uring_sqe *sqe)
2442{
2443 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2444 return -EINVAL;
2445
2446 req->sync.off = READ_ONCE(sqe->off);
2447 req->sync.len = READ_ONCE(sqe->addr);
2448 req->sync.mode = READ_ONCE(sqe->len);
2449 return 0;
2450}
2451
2452static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2453 bool force_nonblock)
2454{
2455 struct io_wq_work *work, *old_work;
2456
2457 /* fallocate always requiring blocking context */
2458 if (force_nonblock) {
2459 io_put_req(req);
2460 req->work.func = io_fallocate_finish;
2461 return -EAGAIN;
2462 }
2463
2464 work = old_work = &req->work;
2465 io_fallocate_finish(&work);
2466 if (work && work != old_work)
2467 *nxt = container_of(work, struct io_kiocb, work);
2468
2469 return 0;
2470}
2471
Jens Axboe15b71ab2019-12-11 11:20:36 -07002472static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2473{
Jens Axboef8748882020-01-08 17:47:02 -07002474 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002475 int ret;
2476
2477 if (sqe->ioprio || sqe->buf_index)
2478 return -EINVAL;
2479
2480 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002481 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002482 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002483 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002484
Jens Axboef8748882020-01-08 17:47:02 -07002485 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002486 if (IS_ERR(req->open.filename)) {
2487 ret = PTR_ERR(req->open.filename);
2488 req->open.filename = NULL;
2489 return ret;
2490 }
2491
2492 return 0;
2493}
2494
Jens Axboecebdb982020-01-08 17:59:24 -07002495static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2496{
2497 struct open_how __user *how;
2498 const char __user *fname;
2499 size_t len;
2500 int ret;
2501
2502 if (sqe->ioprio || sqe->buf_index)
2503 return -EINVAL;
2504
2505 req->open.dfd = READ_ONCE(sqe->fd);
2506 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2507 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2508 len = READ_ONCE(sqe->len);
2509
2510 if (len < OPEN_HOW_SIZE_VER0)
2511 return -EINVAL;
2512
2513 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2514 len);
2515 if (ret)
2516 return ret;
2517
2518 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2519 req->open.how.flags |= O_LARGEFILE;
2520
2521 req->open.filename = getname(fname);
2522 if (IS_ERR(req->open.filename)) {
2523 ret = PTR_ERR(req->open.filename);
2524 req->open.filename = NULL;
2525 return ret;
2526 }
2527
2528 return 0;
2529}
2530
2531static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2532 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002533{
2534 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002535 struct file *file;
2536 int ret;
2537
2538 if (force_nonblock) {
2539 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2540 return -EAGAIN;
2541 }
2542
Jens Axboecebdb982020-01-08 17:59:24 -07002543 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002544 if (ret)
2545 goto err;
2546
Jens Axboecebdb982020-01-08 17:59:24 -07002547 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002548 if (ret < 0)
2549 goto err;
2550
2551 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2552 if (IS_ERR(file)) {
2553 put_unused_fd(ret);
2554 ret = PTR_ERR(file);
2555 } else {
2556 fsnotify_open(file);
2557 fd_install(ret, file);
2558 }
2559err:
2560 putname(req->open.filename);
2561 if (ret < 0)
2562 req_set_fail_links(req);
2563 io_cqring_add_event(req, ret);
2564 io_put_req_find_next(req, nxt);
2565 return 0;
2566}
2567
Jens Axboecebdb982020-01-08 17:59:24 -07002568static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2569 bool force_nonblock)
2570{
2571 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2572 return io_openat2(req, nxt, force_nonblock);
2573}
2574
Jens Axboec1ca7572019-12-25 22:18:28 -07002575static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2576{
2577#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2578 if (sqe->ioprio || sqe->buf_index || sqe->off)
2579 return -EINVAL;
2580
2581 req->madvise.addr = READ_ONCE(sqe->addr);
2582 req->madvise.len = READ_ONCE(sqe->len);
2583 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2584 return 0;
2585#else
2586 return -EOPNOTSUPP;
2587#endif
2588}
2589
2590static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2591 bool force_nonblock)
2592{
2593#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2594 struct io_madvise *ma = &req->madvise;
2595 int ret;
2596
2597 if (force_nonblock)
2598 return -EAGAIN;
2599
2600 ret = do_madvise(ma->addr, ma->len, ma->advice);
2601 if (ret < 0)
2602 req_set_fail_links(req);
2603 io_cqring_add_event(req, ret);
2604 io_put_req_find_next(req, nxt);
2605 return 0;
2606#else
2607 return -EOPNOTSUPP;
2608#endif
2609}
2610
Jens Axboe4840e412019-12-25 22:03:45 -07002611static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2612{
2613 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2614 return -EINVAL;
2615
2616 req->fadvise.offset = READ_ONCE(sqe->off);
2617 req->fadvise.len = READ_ONCE(sqe->len);
2618 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2619 return 0;
2620}
2621
2622static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2623 bool force_nonblock)
2624{
2625 struct io_fadvise *fa = &req->fadvise;
2626 int ret;
2627
2628 /* DONTNEED may block, others _should_ not */
2629 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2630 return -EAGAIN;
2631
2632 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2633 if (ret < 0)
2634 req_set_fail_links(req);
2635 io_cqring_add_event(req, ret);
2636 io_put_req_find_next(req, nxt);
2637 return 0;
2638}
2639
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002640static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2641{
Jens Axboef8748882020-01-08 17:47:02 -07002642 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002643 unsigned lookup_flags;
2644 int ret;
2645
2646 if (sqe->ioprio || sqe->buf_index)
2647 return -EINVAL;
2648
2649 req->open.dfd = READ_ONCE(sqe->fd);
2650 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002651 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002652 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002653 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002654
Jens Axboec12cedf2020-01-08 17:41:21 -07002655 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002656 return -EINVAL;
2657
Jens Axboef8748882020-01-08 17:47:02 -07002658 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002659 if (IS_ERR(req->open.filename)) {
2660 ret = PTR_ERR(req->open.filename);
2661 req->open.filename = NULL;
2662 return ret;
2663 }
2664
2665 return 0;
2666}
2667
2668static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2669 bool force_nonblock)
2670{
2671 struct io_open *ctx = &req->open;
2672 unsigned lookup_flags;
2673 struct path path;
2674 struct kstat stat;
2675 int ret;
2676
2677 if (force_nonblock)
2678 return -EAGAIN;
2679
Jens Axboec12cedf2020-01-08 17:41:21 -07002680 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002681 return -EINVAL;
2682
2683retry:
2684 /* filename_lookup() drops it, keep a reference */
2685 ctx->filename->refcnt++;
2686
2687 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2688 NULL);
2689 if (ret)
2690 goto err;
2691
Jens Axboec12cedf2020-01-08 17:41:21 -07002692 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002693 path_put(&path);
2694 if (retry_estale(ret, lookup_flags)) {
2695 lookup_flags |= LOOKUP_REVAL;
2696 goto retry;
2697 }
2698 if (!ret)
2699 ret = cp_statx(&stat, ctx->buffer);
2700err:
2701 putname(ctx->filename);
2702 if (ret < 0)
2703 req_set_fail_links(req);
2704 io_cqring_add_event(req, ret);
2705 io_put_req_find_next(req, nxt);
2706 return 0;
2707}
2708
Jens Axboeb5dba592019-12-11 14:02:38 -07002709static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2710{
2711 /*
2712 * If we queue this for async, it must not be cancellable. That would
2713 * leave the 'file' in an undeterminate state.
2714 */
2715 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2716
2717 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2718 sqe->rw_flags || sqe->buf_index)
2719 return -EINVAL;
2720 if (sqe->flags & IOSQE_FIXED_FILE)
2721 return -EINVAL;
2722
2723 req->close.fd = READ_ONCE(sqe->fd);
2724 if (req->file->f_op == &io_uring_fops ||
2725 req->close.fd == req->ring_fd)
2726 return -EBADF;
2727
2728 return 0;
2729}
2730
2731static void io_close_finish(struct io_wq_work **workptr)
2732{
2733 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2734 struct io_kiocb *nxt = NULL;
2735
2736 /* Invoked with files, we need to do the close */
2737 if (req->work.files) {
2738 int ret;
2739
2740 ret = filp_close(req->close.put_file, req->work.files);
2741 if (ret < 0) {
2742 req_set_fail_links(req);
2743 }
2744 io_cqring_add_event(req, ret);
2745 }
2746
2747 fput(req->close.put_file);
2748
2749 /* we bypassed the re-issue, drop the submission reference */
2750 io_put_req(req);
2751 io_put_req_find_next(req, &nxt);
2752 if (nxt)
2753 io_wq_assign_next(workptr, nxt);
2754}
2755
2756static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2757 bool force_nonblock)
2758{
2759 int ret;
2760
2761 req->close.put_file = NULL;
2762 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2763 if (ret < 0)
2764 return ret;
2765
2766 /* if the file has a flush method, be safe and punt to async */
2767 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2768 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2769 goto eagain;
2770 }
2771
2772 /*
2773 * No ->flush(), safely close from here and just punt the
2774 * fput() to async context.
2775 */
2776 ret = filp_close(req->close.put_file, current->files);
2777
2778 if (ret < 0)
2779 req_set_fail_links(req);
2780 io_cqring_add_event(req, ret);
2781
2782 if (io_wq_current_is_worker()) {
2783 struct io_wq_work *old_work, *work;
2784
2785 old_work = work = &req->work;
2786 io_close_finish(&work);
2787 if (work && work != old_work)
2788 *nxt = container_of(work, struct io_kiocb, work);
2789 return 0;
2790 }
2791
2792eagain:
2793 req->work.func = io_close_finish;
2794 return -EAGAIN;
2795}
2796
Jens Axboe3529d8c2019-12-19 18:24:38 -07002797static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002798{
2799 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002800
2801 if (!req->file)
2802 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002803
2804 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2805 return -EINVAL;
2806 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2807 return -EINVAL;
2808
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002809 req->sync.off = READ_ONCE(sqe->off);
2810 req->sync.len = READ_ONCE(sqe->len);
2811 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002812 return 0;
2813}
2814
2815static void io_sync_file_range_finish(struct io_wq_work **workptr)
2816{
2817 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2818 struct io_kiocb *nxt = NULL;
2819 int ret;
2820
2821 if (io_req_cancelled(req))
2822 return;
2823
Jens Axboe9adbd452019-12-20 08:45:55 -07002824 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002825 req->sync.flags);
2826 if (ret < 0)
2827 req_set_fail_links(req);
2828 io_cqring_add_event(req, ret);
2829 io_put_req_find_next(req, &nxt);
2830 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002831 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002832}
2833
Jens Axboefc4df992019-12-10 14:38:45 -07002834static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002835 bool force_nonblock)
2836{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002837 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002838
2839 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002840 if (force_nonblock) {
2841 io_put_req(req);
2842 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002843 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002844 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002845
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002846 work = old_work = &req->work;
2847 io_sync_file_range_finish(&work);
2848 if (work && work != old_work)
2849 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002850 return 0;
2851}
2852
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002853#if defined(CONFIG_NET)
2854static void io_sendrecv_async(struct io_wq_work **workptr)
2855{
2856 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2857 struct iovec *iov = NULL;
2858
2859 if (req->io->rw.iov != req->io->rw.fast_iov)
2860 iov = req->io->msg.iov;
2861 io_wq_submit_work(workptr);
2862 kfree(iov);
2863}
2864#endif
2865
Jens Axboe3529d8c2019-12-19 18:24:38 -07002866static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002867{
Jens Axboe03b12302019-12-02 18:50:25 -07002868#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002869 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002870 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002871
Jens Axboee47293f2019-12-20 08:58:21 -07002872 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2873 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002874 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002875
Jens Axboefddafac2020-01-04 20:19:44 -07002876 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002877 return 0;
2878
Jens Axboed9688562019-12-09 19:35:20 -07002879 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002880 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002881 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002882#else
Jens Axboee47293f2019-12-20 08:58:21 -07002883 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002884#endif
2885}
2886
Jens Axboefc4df992019-12-10 14:38:45 -07002887static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2888 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002889{
2890#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002891 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002892 struct socket *sock;
2893 int ret;
2894
2895 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2896 return -EINVAL;
2897
2898 sock = sock_from_file(req->file, &ret);
2899 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002900 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002901 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002902 unsigned flags;
2903
Jens Axboe03b12302019-12-02 18:50:25 -07002904 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002905 kmsg = &req->io->msg;
2906 kmsg->msg.msg_name = &addr;
2907 /* if iov is set, it's allocated already */
2908 if (!kmsg->iov)
2909 kmsg->iov = kmsg->fast_iov;
2910 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002911 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002912 struct io_sr_msg *sr = &req->sr_msg;
2913
Jens Axboe0b416c32019-12-15 10:57:46 -07002914 kmsg = &io.msg;
2915 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002916
2917 io.msg.iov = io.msg.fast_iov;
2918 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2919 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002920 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002921 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002922 }
2923
Jens Axboee47293f2019-12-20 08:58:21 -07002924 flags = req->sr_msg.msg_flags;
2925 if (flags & MSG_DONTWAIT)
2926 req->flags |= REQ_F_NOWAIT;
2927 else if (force_nonblock)
2928 flags |= MSG_DONTWAIT;
2929
Jens Axboe0b416c32019-12-15 10:57:46 -07002930 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002931 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002932 if (req->io)
2933 return -EAGAIN;
2934 if (io_alloc_async_ctx(req))
2935 return -ENOMEM;
2936 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2937 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002938 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002939 }
2940 if (ret == -ERESTARTSYS)
2941 ret = -EINTR;
2942 }
2943
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002944 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002945 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002946 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002947 if (ret < 0)
2948 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002949 io_put_req_find_next(req, nxt);
2950 return 0;
2951#else
2952 return -EOPNOTSUPP;
2953#endif
2954}
2955
Jens Axboefddafac2020-01-04 20:19:44 -07002956static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2957 bool force_nonblock)
2958{
2959#if defined(CONFIG_NET)
2960 struct socket *sock;
2961 int ret;
2962
2963 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2964 return -EINVAL;
2965
2966 sock = sock_from_file(req->file, &ret);
2967 if (sock) {
2968 struct io_sr_msg *sr = &req->sr_msg;
2969 struct msghdr msg;
2970 struct iovec iov;
2971 unsigned flags;
2972
2973 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
2974 &msg.msg_iter);
2975 if (ret)
2976 return ret;
2977
2978 msg.msg_name = NULL;
2979 msg.msg_control = NULL;
2980 msg.msg_controllen = 0;
2981 msg.msg_namelen = 0;
2982
2983 flags = req->sr_msg.msg_flags;
2984 if (flags & MSG_DONTWAIT)
2985 req->flags |= REQ_F_NOWAIT;
2986 else if (force_nonblock)
2987 flags |= MSG_DONTWAIT;
2988
2989 ret = __sys_sendmsg_sock(sock, &msg, flags);
2990 if (force_nonblock && ret == -EAGAIN)
2991 return -EAGAIN;
2992 if (ret == -ERESTARTSYS)
2993 ret = -EINTR;
2994 }
2995
2996 io_cqring_add_event(req, ret);
2997 if (ret < 0)
2998 req_set_fail_links(req);
2999 io_put_req_find_next(req, nxt);
3000 return 0;
3001#else
3002 return -EOPNOTSUPP;
3003#endif
3004}
3005
Jens Axboe3529d8c2019-12-19 18:24:38 -07003006static int io_recvmsg_prep(struct io_kiocb *req,
3007 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003008{
3009#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003010 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003011 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07003012
Jens Axboe3529d8c2019-12-19 18:24:38 -07003013 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3014 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
3015
Jens Axboefddafac2020-01-04 20:19:44 -07003016 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003017 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003018
Jens Axboed9688562019-12-09 19:35:20 -07003019 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003020 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003021 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003022#else
Jens Axboee47293f2019-12-20 08:58:21 -07003023 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003024#endif
3025}
3026
Jens Axboefc4df992019-12-10 14:38:45 -07003027static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3028 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003029{
3030#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003031 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003032 struct socket *sock;
3033 int ret;
3034
3035 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3036 return -EINVAL;
3037
3038 sock = sock_from_file(req->file, &ret);
3039 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003040 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003041 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003042 unsigned flags;
3043
Jens Axboe03b12302019-12-02 18:50:25 -07003044 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003045 kmsg = &req->io->msg;
3046 kmsg->msg.msg_name = &addr;
3047 /* if iov is set, it's allocated already */
3048 if (!kmsg->iov)
3049 kmsg->iov = kmsg->fast_iov;
3050 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003051 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003052 struct io_sr_msg *sr = &req->sr_msg;
3053
Jens Axboe0b416c32019-12-15 10:57:46 -07003054 kmsg = &io.msg;
3055 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003056
3057 io.msg.iov = io.msg.fast_iov;
3058 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3059 sr->msg_flags, &io.msg.uaddr,
3060 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003061 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003062 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003063 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003064
Jens Axboee47293f2019-12-20 08:58:21 -07003065 flags = req->sr_msg.msg_flags;
3066 if (flags & MSG_DONTWAIT)
3067 req->flags |= REQ_F_NOWAIT;
3068 else if (force_nonblock)
3069 flags |= MSG_DONTWAIT;
3070
3071 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3072 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003073 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003074 if (req->io)
3075 return -EAGAIN;
3076 if (io_alloc_async_ctx(req))
3077 return -ENOMEM;
3078 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3079 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003080 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003081 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003082 if (ret == -ERESTARTSYS)
3083 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003084 }
3085
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003086 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003087 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003088 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003089 if (ret < 0)
3090 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003091 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003092 return 0;
3093#else
3094 return -EOPNOTSUPP;
3095#endif
3096}
3097
Jens Axboefddafac2020-01-04 20:19:44 -07003098static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3099 bool force_nonblock)
3100{
3101#if defined(CONFIG_NET)
3102 struct socket *sock;
3103 int ret;
3104
3105 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3106 return -EINVAL;
3107
3108 sock = sock_from_file(req->file, &ret);
3109 if (sock) {
3110 struct io_sr_msg *sr = &req->sr_msg;
3111 struct msghdr msg;
3112 struct iovec iov;
3113 unsigned flags;
3114
3115 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3116 &msg.msg_iter);
3117 if (ret)
3118 return ret;
3119
3120 msg.msg_name = NULL;
3121 msg.msg_control = NULL;
3122 msg.msg_controllen = 0;
3123 msg.msg_namelen = 0;
3124 msg.msg_iocb = NULL;
3125 msg.msg_flags = 0;
3126
3127 flags = req->sr_msg.msg_flags;
3128 if (flags & MSG_DONTWAIT)
3129 req->flags |= REQ_F_NOWAIT;
3130 else if (force_nonblock)
3131 flags |= MSG_DONTWAIT;
3132
3133 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3134 if (force_nonblock && ret == -EAGAIN)
3135 return -EAGAIN;
3136 if (ret == -ERESTARTSYS)
3137 ret = -EINTR;
3138 }
3139
3140 io_cqring_add_event(req, ret);
3141 if (ret < 0)
3142 req_set_fail_links(req);
3143 io_put_req_find_next(req, nxt);
3144 return 0;
3145#else
3146 return -EOPNOTSUPP;
3147#endif
3148}
3149
3150
Jens Axboe3529d8c2019-12-19 18:24:38 -07003151static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003152{
3153#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003154 struct io_accept *accept = &req->accept;
3155
Jens Axboe17f2fe32019-10-17 14:42:58 -06003156 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3157 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003158 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003159 return -EINVAL;
3160
Jens Axboed55e5f52019-12-11 16:12:15 -07003161 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3162 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003163 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003164 return 0;
3165#else
3166 return -EOPNOTSUPP;
3167#endif
3168}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003169
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003170#if defined(CONFIG_NET)
3171static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3172 bool force_nonblock)
3173{
3174 struct io_accept *accept = &req->accept;
3175 unsigned file_flags;
3176 int ret;
3177
3178 file_flags = force_nonblock ? O_NONBLOCK : 0;
3179 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3180 accept->addr_len, accept->flags);
3181 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003182 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003183 if (ret == -ERESTARTSYS)
3184 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003185 if (ret < 0)
3186 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003187 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003188 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003189 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003190}
3191
3192static void io_accept_finish(struct io_wq_work **workptr)
3193{
3194 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3195 struct io_kiocb *nxt = NULL;
3196
3197 if (io_req_cancelled(req))
3198 return;
3199 __io_accept(req, &nxt, false);
3200 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003201 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003202}
3203#endif
3204
3205static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3206 bool force_nonblock)
3207{
3208#if defined(CONFIG_NET)
3209 int ret;
3210
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003211 ret = __io_accept(req, nxt, force_nonblock);
3212 if (ret == -EAGAIN && force_nonblock) {
3213 req->work.func = io_accept_finish;
3214 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3215 io_put_req(req);
3216 return -EAGAIN;
3217 }
3218 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003219#else
3220 return -EOPNOTSUPP;
3221#endif
3222}
3223
Jens Axboe3529d8c2019-12-19 18:24:38 -07003224static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003225{
3226#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003227 struct io_connect *conn = &req->connect;
3228 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003229
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003230 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3231 return -EINVAL;
3232 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3233 return -EINVAL;
3234
Jens Axboe3529d8c2019-12-19 18:24:38 -07003235 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3236 conn->addr_len = READ_ONCE(sqe->addr2);
3237
3238 if (!io)
3239 return 0;
3240
3241 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003242 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003243#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003244 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003245#endif
3246}
3247
Jens Axboefc4df992019-12-10 14:38:45 -07003248static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3249 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003250{
3251#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003252 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003253 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003254 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003255
Jens Axboef499a022019-12-02 16:28:46 -07003256 if (req->io) {
3257 io = req->io;
3258 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003259 ret = move_addr_to_kernel(req->connect.addr,
3260 req->connect.addr_len,
3261 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003262 if (ret)
3263 goto out;
3264 io = &__io;
3265 }
3266
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003267 file_flags = force_nonblock ? O_NONBLOCK : 0;
3268
3269 ret = __sys_connect_file(req->file, &io->connect.address,
3270 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003271 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003272 if (req->io)
3273 return -EAGAIN;
3274 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003275 ret = -ENOMEM;
3276 goto out;
3277 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003278 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003279 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003280 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003281 if (ret == -ERESTARTSYS)
3282 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003283out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003284 if (ret < 0)
3285 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003286 io_cqring_add_event(req, ret);
3287 io_put_req_find_next(req, nxt);
3288 return 0;
3289#else
3290 return -EOPNOTSUPP;
3291#endif
3292}
3293
Jens Axboe221c5eb2019-01-17 09:41:58 -07003294static void io_poll_remove_one(struct io_kiocb *req)
3295{
3296 struct io_poll_iocb *poll = &req->poll;
3297
3298 spin_lock(&poll->head->lock);
3299 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003300 if (!list_empty(&poll->wait.entry)) {
3301 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003302 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003303 }
3304 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003305 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003306}
3307
3308static void io_poll_remove_all(struct io_ring_ctx *ctx)
3309{
Jens Axboe78076bb2019-12-04 19:56:40 -07003310 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003311 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003312 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003313
3314 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003315 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3316 struct hlist_head *list;
3317
3318 list = &ctx->cancel_hash[i];
3319 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3320 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003321 }
3322 spin_unlock_irq(&ctx->completion_lock);
3323}
3324
Jens Axboe47f46762019-11-09 17:43:02 -07003325static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3326{
Jens Axboe78076bb2019-12-04 19:56:40 -07003327 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003328 struct io_kiocb *req;
3329
Jens Axboe78076bb2019-12-04 19:56:40 -07003330 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3331 hlist_for_each_entry(req, list, hash_node) {
3332 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003333 io_poll_remove_one(req);
3334 return 0;
3335 }
Jens Axboe47f46762019-11-09 17:43:02 -07003336 }
3337
3338 return -ENOENT;
3339}
3340
Jens Axboe3529d8c2019-12-19 18:24:38 -07003341static int io_poll_remove_prep(struct io_kiocb *req,
3342 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003343{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003344 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3345 return -EINVAL;
3346 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3347 sqe->poll_events)
3348 return -EINVAL;
3349
Jens Axboe0969e782019-12-17 18:40:57 -07003350 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003351 return 0;
3352}
3353
3354/*
3355 * Find a running poll command that matches one specified in sqe->addr,
3356 * and remove it if found.
3357 */
3358static int io_poll_remove(struct io_kiocb *req)
3359{
3360 struct io_ring_ctx *ctx = req->ctx;
3361 u64 addr;
3362 int ret;
3363
Jens Axboe0969e782019-12-17 18:40:57 -07003364 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003365 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003366 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003367 spin_unlock_irq(&ctx->completion_lock);
3368
Jens Axboe78e19bb2019-11-06 15:21:34 -07003369 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003370 if (ret < 0)
3371 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003372 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003373 return 0;
3374}
3375
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003376static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003377{
Jackie Liua197f662019-11-08 08:09:12 -07003378 struct io_ring_ctx *ctx = req->ctx;
3379
Jens Axboe8c838782019-03-12 15:48:16 -06003380 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003381 if (error)
3382 io_cqring_fill_event(req, error);
3383 else
3384 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003385 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003386}
3387
Jens Axboe561fb042019-10-24 07:25:42 -06003388static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003389{
Jens Axboe561fb042019-10-24 07:25:42 -06003390 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003391 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3392 struct io_poll_iocb *poll = &req->poll;
3393 struct poll_table_struct pt = { ._key = poll->events };
3394 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003395 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003396 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003397 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003398
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003399 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003400 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003401 ret = -ECANCELED;
3402 } else if (READ_ONCE(poll->canceled)) {
3403 ret = -ECANCELED;
3404 }
Jens Axboe561fb042019-10-24 07:25:42 -06003405
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003406 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003407 mask = vfs_poll(poll->file, &pt) & poll->events;
3408
3409 /*
3410 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3411 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3412 * synchronize with them. In the cancellation case the list_del_init
3413 * itself is not actually needed, but harmless so we keep it in to
3414 * avoid further branches in the fast path.
3415 */
3416 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003417 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003418 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003419 spin_unlock_irq(&ctx->completion_lock);
3420 return;
3421 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003422 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003423 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003424 spin_unlock_irq(&ctx->completion_lock);
3425
Jens Axboe8c838782019-03-12 15:48:16 -06003426 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003427
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003428 if (ret < 0)
3429 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003430 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003431 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003432 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003433}
3434
Jens Axboee94f1412019-12-19 12:06:02 -07003435static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3436{
Jens Axboee94f1412019-12-19 12:06:02 -07003437 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003438 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003439
Jens Axboec6ca97b302019-12-28 12:11:08 -07003440 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003441 spin_lock_irq(&ctx->completion_lock);
3442 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3443 hash_del(&req->hash_node);
3444 io_poll_complete(req, req->result, 0);
3445
Jens Axboe8237e042019-12-28 10:48:22 -07003446 if (refcount_dec_and_test(&req->refs) &&
3447 !io_req_multi_free(&rb, req)) {
3448 req->flags |= REQ_F_COMP_LOCKED;
3449 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003450 }
3451 }
3452 spin_unlock_irq(&ctx->completion_lock);
3453
3454 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003455 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003456}
3457
3458static void io_poll_flush(struct io_wq_work **workptr)
3459{
3460 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3461 struct llist_node *nodes;
3462
3463 nodes = llist_del_all(&req->ctx->poll_llist);
3464 if (nodes)
3465 __io_poll_flush(req->ctx, nodes);
3466}
3467
Jens Axboe221c5eb2019-01-17 09:41:58 -07003468static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3469 void *key)
3470{
Jens Axboee9444752019-11-26 15:02:04 -07003471 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003472 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3473 struct io_ring_ctx *ctx = req->ctx;
3474 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003475
3476 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003477 if (mask && !(mask & poll->events))
3478 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003479
Jens Axboe392edb42019-12-09 17:52:20 -07003480 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003481
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003482 /*
3483 * Run completion inline if we can. We're using trylock here because
3484 * we are violating the completion_lock -> poll wq lock ordering.
3485 * If we have a link timeout we're going to need the completion_lock
3486 * for finalizing the request, mark us as having grabbed that already.
3487 */
Jens Axboee94f1412019-12-19 12:06:02 -07003488 if (mask) {
3489 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003490
Jens Axboee94f1412019-12-19 12:06:02 -07003491 if (llist_empty(&ctx->poll_llist) &&
3492 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3493 hash_del(&req->hash_node);
3494 io_poll_complete(req, mask, 0);
3495 req->flags |= REQ_F_COMP_LOCKED;
3496 io_put_req(req);
3497 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3498
3499 io_cqring_ev_posted(ctx);
3500 req = NULL;
3501 } else {
3502 req->result = mask;
3503 req->llist_node.next = NULL;
3504 /* if the list wasn't empty, we're done */
3505 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3506 req = NULL;
3507 else
3508 req->work.func = io_poll_flush;
3509 }
Jens Axboe8c838782019-03-12 15:48:16 -06003510 }
Jens Axboee94f1412019-12-19 12:06:02 -07003511 if (req)
3512 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003513
Jens Axboe221c5eb2019-01-17 09:41:58 -07003514 return 1;
3515}
3516
3517struct io_poll_table {
3518 struct poll_table_struct pt;
3519 struct io_kiocb *req;
3520 int error;
3521};
3522
3523static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3524 struct poll_table_struct *p)
3525{
3526 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3527
3528 if (unlikely(pt->req->poll.head)) {
3529 pt->error = -EINVAL;
3530 return;
3531 }
3532
3533 pt->error = 0;
3534 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003535 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003536}
3537
Jens Axboeeac406c2019-11-14 12:09:58 -07003538static void io_poll_req_insert(struct io_kiocb *req)
3539{
3540 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003541 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003542
Jens Axboe78076bb2019-12-04 19:56:40 -07003543 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3544 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003545}
3546
Jens Axboe3529d8c2019-12-19 18:24:38 -07003547static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003548{
3549 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003550 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003551
3552 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3553 return -EINVAL;
3554 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3555 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003556 if (!poll->file)
3557 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003558
Jens Axboe221c5eb2019-01-17 09:41:58 -07003559 events = READ_ONCE(sqe->poll_events);
3560 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003561 return 0;
3562}
3563
3564static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3565{
3566 struct io_poll_iocb *poll = &req->poll;
3567 struct io_ring_ctx *ctx = req->ctx;
3568 struct io_poll_table ipt;
3569 bool cancel = false;
3570 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003571
3572 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003573 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003574
Jens Axboe221c5eb2019-01-17 09:41:58 -07003575 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003576 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003577 poll->canceled = false;
3578
3579 ipt.pt._qproc = io_poll_queue_proc;
3580 ipt.pt._key = poll->events;
3581 ipt.req = req;
3582 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3583
3584 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003585 INIT_LIST_HEAD(&poll->wait.entry);
3586 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3587 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003588
Jens Axboe36703242019-07-25 10:20:18 -06003589 INIT_LIST_HEAD(&req->list);
3590
Jens Axboe221c5eb2019-01-17 09:41:58 -07003591 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003592
3593 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003594 if (likely(poll->head)) {
3595 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003596 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003597 if (ipt.error)
3598 cancel = true;
3599 ipt.error = 0;
3600 mask = 0;
3601 }
3602 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003603 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003604 else if (cancel)
3605 WRITE_ONCE(poll->canceled, true);
3606 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003607 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003608 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003609 }
Jens Axboe8c838782019-03-12 15:48:16 -06003610 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003611 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003612 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003613 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003614 spin_unlock_irq(&ctx->completion_lock);
3615
Jens Axboe8c838782019-03-12 15:48:16 -06003616 if (mask) {
3617 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003618 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003619 }
Jens Axboe8c838782019-03-12 15:48:16 -06003620 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003621}
3622
Jens Axboe5262f562019-09-17 12:26:57 -06003623static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3624{
Jens Axboead8a48a2019-11-15 08:49:11 -07003625 struct io_timeout_data *data = container_of(timer,
3626 struct io_timeout_data, timer);
3627 struct io_kiocb *req = data->req;
3628 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003629 unsigned long flags;
3630
Jens Axboe5262f562019-09-17 12:26:57 -06003631 atomic_inc(&ctx->cq_timeouts);
3632
3633 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003634 /*
Jens Axboe11365042019-10-16 09:08:32 -06003635 * We could be racing with timeout deletion. If the list is empty,
3636 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003637 */
Jens Axboe842f9612019-10-29 12:34:10 -06003638 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003639 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003640
Jens Axboe11365042019-10-16 09:08:32 -06003641 /*
3642 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003643 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003644 * pointer will be increased, otherwise other timeout reqs may
3645 * return in advance without waiting for enough wait_nr.
3646 */
3647 prev = req;
3648 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3649 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003650 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003651 }
Jens Axboe842f9612019-10-29 12:34:10 -06003652
Jens Axboe78e19bb2019-11-06 15:21:34 -07003653 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003654 io_commit_cqring(ctx);
3655 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3656
3657 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003658 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003659 io_put_req(req);
3660 return HRTIMER_NORESTART;
3661}
3662
Jens Axboe47f46762019-11-09 17:43:02 -07003663static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3664{
3665 struct io_kiocb *req;
3666 int ret = -ENOENT;
3667
3668 list_for_each_entry(req, &ctx->timeout_list, list) {
3669 if (user_data == req->user_data) {
3670 list_del_init(&req->list);
3671 ret = 0;
3672 break;
3673 }
3674 }
3675
3676 if (ret == -ENOENT)
3677 return ret;
3678
Jens Axboe2d283902019-12-04 11:08:05 -07003679 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003680 if (ret == -1)
3681 return -EALREADY;
3682
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003683 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003684 io_cqring_fill_event(req, -ECANCELED);
3685 io_put_req(req);
3686 return 0;
3687}
3688
Jens Axboe3529d8c2019-12-19 18:24:38 -07003689static int io_timeout_remove_prep(struct io_kiocb *req,
3690 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003691{
Jens Axboeb29472e2019-12-17 18:50:29 -07003692 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3693 return -EINVAL;
3694 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3695 return -EINVAL;
3696
3697 req->timeout.addr = READ_ONCE(sqe->addr);
3698 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3699 if (req->timeout.flags)
3700 return -EINVAL;
3701
Jens Axboeb29472e2019-12-17 18:50:29 -07003702 return 0;
3703}
3704
Jens Axboe11365042019-10-16 09:08:32 -06003705/*
3706 * Remove or update an existing timeout command
3707 */
Jens Axboefc4df992019-12-10 14:38:45 -07003708static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003709{
3710 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003711 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003712
Jens Axboe11365042019-10-16 09:08:32 -06003713 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003714 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003715
Jens Axboe47f46762019-11-09 17:43:02 -07003716 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003717 io_commit_cqring(ctx);
3718 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003719 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003720 if (ret < 0)
3721 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003722 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003723 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003724}
3725
Jens Axboe3529d8c2019-12-19 18:24:38 -07003726static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003727 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003728{
Jens Axboead8a48a2019-11-15 08:49:11 -07003729 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003730 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003731
Jens Axboead8a48a2019-11-15 08:49:11 -07003732 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003733 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003734 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003735 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003736 if (sqe->off && is_timeout_link)
3737 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003738 flags = READ_ONCE(sqe->timeout_flags);
3739 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003740 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003741
Jens Axboe26a61672019-12-20 09:02:01 -07003742 req->timeout.count = READ_ONCE(sqe->off);
3743
Jens Axboe3529d8c2019-12-19 18:24:38 -07003744 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003745 return -ENOMEM;
3746
3747 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003748 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003749 req->flags |= REQ_F_TIMEOUT;
3750
3751 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003752 return -EFAULT;
3753
Jens Axboe11365042019-10-16 09:08:32 -06003754 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003755 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003756 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003757 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003758
Jens Axboead8a48a2019-11-15 08:49:11 -07003759 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3760 return 0;
3761}
3762
Jens Axboefc4df992019-12-10 14:38:45 -07003763static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003764{
3765 unsigned count;
3766 struct io_ring_ctx *ctx = req->ctx;
3767 struct io_timeout_data *data;
3768 struct list_head *entry;
3769 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003770
Jens Axboe2d283902019-12-04 11:08:05 -07003771 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003772
Jens Axboe5262f562019-09-17 12:26:57 -06003773 /*
3774 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003775 * timeout event to be satisfied. If it isn't set, then this is
3776 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003777 */
Jens Axboe26a61672019-12-20 09:02:01 -07003778 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003779 if (!count) {
3780 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3781 spin_lock_irq(&ctx->completion_lock);
3782 entry = ctx->timeout_list.prev;
3783 goto add;
3784 }
Jens Axboe5262f562019-09-17 12:26:57 -06003785
3786 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003787 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003788
3789 /*
3790 * Insertion sort, ensuring the first entry in the list is always
3791 * the one we need first.
3792 */
Jens Axboe5262f562019-09-17 12:26:57 -06003793 spin_lock_irq(&ctx->completion_lock);
3794 list_for_each_prev(entry, &ctx->timeout_list) {
3795 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003796 unsigned nxt_sq_head;
3797 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003798 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003799
Jens Axboe93bd25b2019-11-11 23:34:31 -07003800 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3801 continue;
3802
yangerkun5da0fb12019-10-15 21:59:29 +08003803 /*
3804 * Since cached_sq_head + count - 1 can overflow, use type long
3805 * long to store it.
3806 */
3807 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003808 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3809 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003810
3811 /*
3812 * cached_sq_head may overflow, and it will never overflow twice
3813 * once there is some timeout req still be valid.
3814 */
3815 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003816 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003817
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003818 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003819 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003820
3821 /*
3822 * Sequence of reqs after the insert one and itself should
3823 * be adjusted because each timeout req consumes a slot.
3824 */
3825 span++;
3826 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003827 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003828 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003829add:
Jens Axboe5262f562019-09-17 12:26:57 -06003830 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003831 data->timer.function = io_timeout_fn;
3832 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003833 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003834 return 0;
3835}
3836
Jens Axboe62755e32019-10-28 21:49:21 -06003837static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003838{
Jens Axboe62755e32019-10-28 21:49:21 -06003839 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003840
Jens Axboe62755e32019-10-28 21:49:21 -06003841 return req->user_data == (unsigned long) data;
3842}
3843
Jens Axboee977d6d2019-11-05 12:39:45 -07003844static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003845{
Jens Axboe62755e32019-10-28 21:49:21 -06003846 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003847 int ret = 0;
3848
Jens Axboe62755e32019-10-28 21:49:21 -06003849 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3850 switch (cancel_ret) {
3851 case IO_WQ_CANCEL_OK:
3852 ret = 0;
3853 break;
3854 case IO_WQ_CANCEL_RUNNING:
3855 ret = -EALREADY;
3856 break;
3857 case IO_WQ_CANCEL_NOTFOUND:
3858 ret = -ENOENT;
3859 break;
3860 }
3861
Jens Axboee977d6d2019-11-05 12:39:45 -07003862 return ret;
3863}
3864
Jens Axboe47f46762019-11-09 17:43:02 -07003865static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3866 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003867 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003868{
3869 unsigned long flags;
3870 int ret;
3871
3872 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3873 if (ret != -ENOENT) {
3874 spin_lock_irqsave(&ctx->completion_lock, flags);
3875 goto done;
3876 }
3877
3878 spin_lock_irqsave(&ctx->completion_lock, flags);
3879 ret = io_timeout_cancel(ctx, sqe_addr);
3880 if (ret != -ENOENT)
3881 goto done;
3882 ret = io_poll_cancel(ctx, sqe_addr);
3883done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003884 if (!ret)
3885 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003886 io_cqring_fill_event(req, ret);
3887 io_commit_cqring(ctx);
3888 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3889 io_cqring_ev_posted(ctx);
3890
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003891 if (ret < 0)
3892 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003893 io_put_req_find_next(req, nxt);
3894}
3895
Jens Axboe3529d8c2019-12-19 18:24:38 -07003896static int io_async_cancel_prep(struct io_kiocb *req,
3897 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003898{
Jens Axboefbf23842019-12-17 18:45:56 -07003899 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003900 return -EINVAL;
3901 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3902 sqe->cancel_flags)
3903 return -EINVAL;
3904
Jens Axboefbf23842019-12-17 18:45:56 -07003905 req->cancel.addr = READ_ONCE(sqe->addr);
3906 return 0;
3907}
3908
3909static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3910{
3911 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003912
3913 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003914 return 0;
3915}
3916
Jens Axboe05f3fb32019-12-09 11:22:50 -07003917static int io_files_update_prep(struct io_kiocb *req,
3918 const struct io_uring_sqe *sqe)
3919{
3920 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3921 return -EINVAL;
3922
3923 req->files_update.offset = READ_ONCE(sqe->off);
3924 req->files_update.nr_args = READ_ONCE(sqe->len);
3925 if (!req->files_update.nr_args)
3926 return -EINVAL;
3927 req->files_update.arg = READ_ONCE(sqe->addr);
3928 return 0;
3929}
3930
3931static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3932{
3933 struct io_ring_ctx *ctx = req->ctx;
3934 struct io_uring_files_update up;
3935 int ret;
3936
3937 if (force_nonblock) {
3938 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3939 return -EAGAIN;
3940 }
3941
3942 up.offset = req->files_update.offset;
3943 up.fds = req->files_update.arg;
3944
3945 mutex_lock(&ctx->uring_lock);
3946 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3947 mutex_unlock(&ctx->uring_lock);
3948
3949 if (ret < 0)
3950 req_set_fail_links(req);
3951 io_cqring_add_event(req, ret);
3952 io_put_req(req);
3953 return 0;
3954}
3955
Jens Axboe3529d8c2019-12-19 18:24:38 -07003956static int io_req_defer_prep(struct io_kiocb *req,
3957 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003958{
Jens Axboee7815732019-12-17 19:45:06 -07003959 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003960
Jens Axboed625c6e2019-12-17 19:53:05 -07003961 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003962 case IORING_OP_NOP:
3963 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003964 case IORING_OP_READV:
3965 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003966 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003967 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003968 break;
3969 case IORING_OP_WRITEV:
3970 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003971 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003972 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003973 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003974 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003975 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003976 break;
3977 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003978 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003979 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003980 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003981 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003982 break;
3983 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003984 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003985 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003986 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003987 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003988 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003989 break;
3990 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003991 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003992 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003993 break;
Jens Axboef499a022019-12-02 16:28:46 -07003994 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003995 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003996 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003997 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003998 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003999 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004000 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004001 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004002 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004003 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004004 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004005 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004006 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004007 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004008 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004009 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004010 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004011 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004012 case IORING_OP_FALLOCATE:
4013 ret = io_fallocate_prep(req, sqe);
4014 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004015 case IORING_OP_OPENAT:
4016 ret = io_openat_prep(req, sqe);
4017 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004018 case IORING_OP_CLOSE:
4019 ret = io_close_prep(req, sqe);
4020 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004021 case IORING_OP_FILES_UPDATE:
4022 ret = io_files_update_prep(req, sqe);
4023 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004024 case IORING_OP_STATX:
4025 ret = io_statx_prep(req, sqe);
4026 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004027 case IORING_OP_FADVISE:
4028 ret = io_fadvise_prep(req, sqe);
4029 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004030 case IORING_OP_MADVISE:
4031 ret = io_madvise_prep(req, sqe);
4032 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004033 case IORING_OP_OPENAT2:
4034 ret = io_openat2_prep(req, sqe);
4035 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004036 default:
Jens Axboee7815732019-12-17 19:45:06 -07004037 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4038 req->opcode);
4039 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004040 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004041 }
4042
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004043 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004044}
4045
Jens Axboe3529d8c2019-12-19 18:24:38 -07004046static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004047{
Jackie Liua197f662019-11-08 08:09:12 -07004048 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004049 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004050
Bob Liu9d858b22019-11-13 18:06:25 +08004051 /* Still need defer if there is pending req in defer list. */
4052 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004053 return 0;
4054
Jens Axboe3529d8c2019-12-19 18:24:38 -07004055 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004056 return -EAGAIN;
4057
Jens Axboe3529d8c2019-12-19 18:24:38 -07004058 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004059 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004060 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004061
Jens Axboede0617e2019-04-06 21:51:27 -06004062 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004063 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004064 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004065 return 0;
4066 }
4067
Jens Axboe915967f2019-11-21 09:01:20 -07004068 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004069 list_add_tail(&req->list, &ctx->defer_list);
4070 spin_unlock_irq(&ctx->completion_lock);
4071 return -EIOCBQUEUED;
4072}
4073
Jens Axboe3529d8c2019-12-19 18:24:38 -07004074static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4075 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004076{
Jackie Liua197f662019-11-08 08:09:12 -07004077 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004078 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004079
Jens Axboed625c6e2019-12-17 19:53:05 -07004080 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004081 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004082 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004083 break;
4084 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004085 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004086 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004087 if (sqe) {
4088 ret = io_read_prep(req, sqe, force_nonblock);
4089 if (ret < 0)
4090 break;
4091 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004092 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004093 break;
4094 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004095 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004096 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004097 if (sqe) {
4098 ret = io_write_prep(req, sqe, force_nonblock);
4099 if (ret < 0)
4100 break;
4101 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004102 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004103 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004104 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004105 if (sqe) {
4106 ret = io_prep_fsync(req, sqe);
4107 if (ret < 0)
4108 break;
4109 }
Jens Axboefc4df992019-12-10 14:38:45 -07004110 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004111 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004112 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004113 if (sqe) {
4114 ret = io_poll_add_prep(req, sqe);
4115 if (ret)
4116 break;
4117 }
Jens Axboefc4df992019-12-10 14:38:45 -07004118 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004119 break;
4120 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004121 if (sqe) {
4122 ret = io_poll_remove_prep(req, sqe);
4123 if (ret < 0)
4124 break;
4125 }
Jens Axboefc4df992019-12-10 14:38:45 -07004126 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004127 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004128 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004129 if (sqe) {
4130 ret = io_prep_sfr(req, sqe);
4131 if (ret < 0)
4132 break;
4133 }
Jens Axboefc4df992019-12-10 14:38:45 -07004134 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004135 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004136 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004137 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004138 if (sqe) {
4139 ret = io_sendmsg_prep(req, sqe);
4140 if (ret < 0)
4141 break;
4142 }
Jens Axboefddafac2020-01-04 20:19:44 -07004143 if (req->opcode == IORING_OP_SENDMSG)
4144 ret = io_sendmsg(req, nxt, force_nonblock);
4145 else
4146 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004147 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004148 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004149 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004150 if (sqe) {
4151 ret = io_recvmsg_prep(req, sqe);
4152 if (ret)
4153 break;
4154 }
Jens Axboefddafac2020-01-04 20:19:44 -07004155 if (req->opcode == IORING_OP_RECVMSG)
4156 ret = io_recvmsg(req, nxt, force_nonblock);
4157 else
4158 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004159 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004160 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004161 if (sqe) {
4162 ret = io_timeout_prep(req, sqe, false);
4163 if (ret)
4164 break;
4165 }
Jens Axboefc4df992019-12-10 14:38:45 -07004166 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004167 break;
Jens Axboe11365042019-10-16 09:08:32 -06004168 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004169 if (sqe) {
4170 ret = io_timeout_remove_prep(req, sqe);
4171 if (ret)
4172 break;
4173 }
Jens Axboefc4df992019-12-10 14:38:45 -07004174 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004175 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004176 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004177 if (sqe) {
4178 ret = io_accept_prep(req, sqe);
4179 if (ret)
4180 break;
4181 }
Jens Axboefc4df992019-12-10 14:38:45 -07004182 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004183 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004184 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004185 if (sqe) {
4186 ret = io_connect_prep(req, sqe);
4187 if (ret)
4188 break;
4189 }
Jens Axboefc4df992019-12-10 14:38:45 -07004190 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004191 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004192 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004193 if (sqe) {
4194 ret = io_async_cancel_prep(req, sqe);
4195 if (ret)
4196 break;
4197 }
Jens Axboefc4df992019-12-10 14:38:45 -07004198 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004199 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004200 case IORING_OP_FALLOCATE:
4201 if (sqe) {
4202 ret = io_fallocate_prep(req, sqe);
4203 if (ret)
4204 break;
4205 }
4206 ret = io_fallocate(req, nxt, force_nonblock);
4207 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004208 case IORING_OP_OPENAT:
4209 if (sqe) {
4210 ret = io_openat_prep(req, sqe);
4211 if (ret)
4212 break;
4213 }
4214 ret = io_openat(req, nxt, force_nonblock);
4215 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004216 case IORING_OP_CLOSE:
4217 if (sqe) {
4218 ret = io_close_prep(req, sqe);
4219 if (ret)
4220 break;
4221 }
4222 ret = io_close(req, nxt, force_nonblock);
4223 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004224 case IORING_OP_FILES_UPDATE:
4225 if (sqe) {
4226 ret = io_files_update_prep(req, sqe);
4227 if (ret)
4228 break;
4229 }
4230 ret = io_files_update(req, force_nonblock);
4231 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004232 case IORING_OP_STATX:
4233 if (sqe) {
4234 ret = io_statx_prep(req, sqe);
4235 if (ret)
4236 break;
4237 }
4238 ret = io_statx(req, nxt, force_nonblock);
4239 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004240 case IORING_OP_FADVISE:
4241 if (sqe) {
4242 ret = io_fadvise_prep(req, sqe);
4243 if (ret)
4244 break;
4245 }
4246 ret = io_fadvise(req, nxt, force_nonblock);
4247 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004248 case IORING_OP_MADVISE:
4249 if (sqe) {
4250 ret = io_madvise_prep(req, sqe);
4251 if (ret)
4252 break;
4253 }
4254 ret = io_madvise(req, nxt, force_nonblock);
4255 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004256 case IORING_OP_OPENAT2:
4257 if (sqe) {
4258 ret = io_openat2_prep(req, sqe);
4259 if (ret)
4260 break;
4261 }
4262 ret = io_openat2(req, nxt, force_nonblock);
4263 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004264 default:
4265 ret = -EINVAL;
4266 break;
4267 }
4268
Jens Axboedef596e2019-01-09 08:59:42 -07004269 if (ret)
4270 return ret;
4271
4272 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004273 const bool in_async = io_wq_current_is_worker();
4274
Jens Axboe9e645e112019-05-10 16:07:28 -06004275 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004276 return -EAGAIN;
4277
Jens Axboe11ba8202020-01-15 21:51:17 -07004278 /* workqueue context doesn't hold uring_lock, grab it now */
4279 if (in_async)
4280 mutex_lock(&ctx->uring_lock);
4281
Jens Axboedef596e2019-01-09 08:59:42 -07004282 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004283
4284 if (in_async)
4285 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004286 }
4287
4288 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004289}
4290
Jens Axboe561fb042019-10-24 07:25:42 -06004291static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004292{
Jens Axboe561fb042019-10-24 07:25:42 -06004293 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004294 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004295 struct io_kiocb *nxt = NULL;
4296 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004297
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004298 /* if NO_CANCEL is set, we must still run the work */
4299 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4300 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004301 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004302 }
Jens Axboe31b51512019-01-18 22:56:34 -07004303
Jens Axboe561fb042019-10-24 07:25:42 -06004304 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004305 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4306 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004307 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004308 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004309 /*
4310 * We can get EAGAIN for polled IO even though we're
4311 * forcing a sync submission from here, since we can't
4312 * wait for request slots on the block side.
4313 */
4314 if (ret != -EAGAIN)
4315 break;
4316 cond_resched();
4317 } while (1);
4318 }
Jens Axboe31b51512019-01-18 22:56:34 -07004319
Jens Axboe561fb042019-10-24 07:25:42 -06004320 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004321 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004322
Jens Axboe561fb042019-10-24 07:25:42 -06004323 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004324 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004325 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004326 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004327 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004328
Jens Axboe561fb042019-10-24 07:25:42 -06004329 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004330 if (!ret && nxt)
4331 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004332}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004333
Jens Axboe15b71ab2019-12-11 11:20:36 -07004334static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004335{
Jens Axboed3656342019-12-18 09:50:26 -07004336 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004337 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004338 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4339 return 0;
4340 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004341}
4342
Jens Axboe65e19f52019-10-26 07:20:21 -06004343static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4344 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004345{
Jens Axboe65e19f52019-10-26 07:20:21 -06004346 struct fixed_file_table *table;
4347
Jens Axboe05f3fb32019-12-09 11:22:50 -07004348 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4349 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004350}
4351
Jens Axboe3529d8c2019-12-19 18:24:38 -07004352static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4353 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004354{
Jackie Liua197f662019-11-08 08:09:12 -07004355 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004356 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004357 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004358
Jens Axboe3529d8c2019-12-19 18:24:38 -07004359 flags = READ_ONCE(sqe->flags);
4360 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004361
Jackie Liu4fe2c962019-09-09 20:50:40 +08004362 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06004363 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06004364
Jens Axboed3656342019-12-18 09:50:26 -07004365 if (!io_req_needs_file(req, fd))
4366 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004367
4368 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004369 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004370 (unsigned) fd >= ctx->nr_user_files))
4371 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004372 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004373 req->file = io_file_from_index(ctx, fd);
4374 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004375 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004376 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004377 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004378 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004379 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004380 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004381 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004382 req->file = io_file_get(state, fd);
4383 if (unlikely(!req->file))
4384 return -EBADF;
4385 }
4386
4387 return 0;
4388}
4389
Jackie Liua197f662019-11-08 08:09:12 -07004390static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004391{
Jens Axboefcb323c2019-10-24 12:39:47 -06004392 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004393 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004394
Jens Axboeb5dba592019-12-11 14:02:38 -07004395 if (!req->ring_file)
4396 return -EBADF;
4397
Jens Axboefcb323c2019-10-24 12:39:47 -06004398 rcu_read_lock();
4399 spin_lock_irq(&ctx->inflight_lock);
4400 /*
4401 * We use the f_ops->flush() handler to ensure that we can flush
4402 * out work accessing these files if the fd is closed. Check if
4403 * the fd has changed since we started down this path, and disallow
4404 * this operation if it has.
4405 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004406 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004407 list_add(&req->inflight_entry, &ctx->inflight_list);
4408 req->flags |= REQ_F_INFLIGHT;
4409 req->work.files = current->files;
4410 ret = 0;
4411 }
4412 spin_unlock_irq(&ctx->inflight_lock);
4413 rcu_read_unlock();
4414
4415 return ret;
4416}
4417
Jens Axboe2665abf2019-11-05 12:40:47 -07004418static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4419{
Jens Axboead8a48a2019-11-15 08:49:11 -07004420 struct io_timeout_data *data = container_of(timer,
4421 struct io_timeout_data, timer);
4422 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004423 struct io_ring_ctx *ctx = req->ctx;
4424 struct io_kiocb *prev = NULL;
4425 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004426
4427 spin_lock_irqsave(&ctx->completion_lock, flags);
4428
4429 /*
4430 * We don't expect the list to be empty, that will only happen if we
4431 * race with the completion of the linked work.
4432 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004433 if (!list_empty(&req->link_list)) {
4434 prev = list_entry(req->link_list.prev, struct io_kiocb,
4435 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004436 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004437 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004438 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4439 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004440 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004441 }
4442
4443 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4444
4445 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004446 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004447 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4448 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004449 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004450 } else {
4451 io_cqring_add_event(req, -ETIME);
4452 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004453 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004454 return HRTIMER_NORESTART;
4455}
4456
Jens Axboead8a48a2019-11-15 08:49:11 -07004457static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004458{
Jens Axboe76a46e02019-11-10 23:34:16 -07004459 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004460
Jens Axboe76a46e02019-11-10 23:34:16 -07004461 /*
4462 * If the list is now empty, then our linked request finished before
4463 * we got a chance to setup the timer
4464 */
4465 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004466 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004467 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004468
Jens Axboead8a48a2019-11-15 08:49:11 -07004469 data->timer.function = io_link_timeout_fn;
4470 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4471 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004472 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004473 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004474
Jens Axboe2665abf2019-11-05 12:40:47 -07004475 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004476 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004477}
4478
Jens Axboead8a48a2019-11-15 08:49:11 -07004479static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004480{
4481 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004482
Jens Axboe2665abf2019-11-05 12:40:47 -07004483 if (!(req->flags & REQ_F_LINK))
4484 return NULL;
4485
Pavel Begunkov44932332019-12-05 16:16:35 +03004486 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4487 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004488 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004489 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004490
Jens Axboe76a46e02019-11-10 23:34:16 -07004491 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004492 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004493}
4494
Jens Axboe3529d8c2019-12-19 18:24:38 -07004495static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004496{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004497 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004498 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004499 int ret;
4500
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004501again:
4502 linked_timeout = io_prep_linked_timeout(req);
4503
Jens Axboe3529d8c2019-12-19 18:24:38 -07004504 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004505
4506 /*
4507 * We async punt it if the file wasn't marked NOWAIT, or if the file
4508 * doesn't support non-blocking read/write attempts
4509 */
4510 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4511 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004512 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4513 ret = io_grab_files(req);
4514 if (ret)
4515 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004516 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004517
4518 /*
4519 * Queued up for async execution, worker will release
4520 * submit reference when the iocb is actually submitted.
4521 */
4522 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004523 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004524 }
Jens Axboee65ef562019-03-12 10:16:44 -06004525
Jens Axboefcb323c2019-10-24 12:39:47 -06004526err:
Jens Axboee65ef562019-03-12 10:16:44 -06004527 /* drop submission reference */
4528 io_put_req(req);
4529
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004530 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004531 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004532 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004533 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004534 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004535 }
4536
Jens Axboee65ef562019-03-12 10:16:44 -06004537 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004538 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004539 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004540 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004541 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004542 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004543done_req:
4544 if (nxt) {
4545 req = nxt;
4546 nxt = NULL;
4547 goto again;
4548 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004549}
4550
Jens Axboe3529d8c2019-12-19 18:24:38 -07004551static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004552{
4553 int ret;
4554
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004555 if (unlikely(req->ctx->drain_next)) {
4556 req->flags |= REQ_F_IO_DRAIN;
Jens Axboe69b3e542020-01-08 11:01:46 -07004557 req->ctx->drain_next = 0;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004558 }
Jens Axboe69b3e542020-01-08 11:01:46 -07004559 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK) != 0;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004560
Jens Axboe3529d8c2019-12-19 18:24:38 -07004561 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004562 if (ret) {
4563 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004564 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004565 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004566 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004567 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004568 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboece35a472019-12-17 08:04:44 -07004569 /*
4570 * Never try inline submit of IOSQE_ASYNC is set, go straight
4571 * to async execution.
4572 */
4573 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4574 io_queue_async_work(req);
4575 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004576 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004577 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004578}
4579
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004580static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004581{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004582 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004583 io_cqring_add_event(req, -ECANCELED);
4584 io_double_put_req(req);
4585 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004586 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004587}
4588
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004589#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004590 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004591
Jens Axboe3529d8c2019-12-19 18:24:38 -07004592static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4593 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004594{
Jackie Liua197f662019-11-08 08:09:12 -07004595 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004596 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004597 int ret;
4598
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004599 sqe_flags = READ_ONCE(sqe->flags);
4600
Jens Axboe9e645e112019-05-10 16:07:28 -06004601 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004602 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004603 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004604 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004605 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004606 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004607 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004608
Jens Axboe3529d8c2019-12-19 18:24:38 -07004609 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004610 if (unlikely(ret)) {
4611err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004612 io_cqring_add_event(req, ret);
4613 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004614 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004615 }
4616
Jens Axboe9e645e112019-05-10 16:07:28 -06004617 /*
4618 * If we already have a head request, queue this one for async
4619 * submittal once the head completes. If we don't have a head but
4620 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4621 * submitted sync once the chain is complete. If none of those
4622 * conditions are true (normal request), then just queue it.
4623 */
4624 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004625 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004626
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004627 if (sqe_flags & IOSQE_IO_DRAIN)
Pavel Begunkov9d763772019-12-17 02:22:07 +03004628 head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004629
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004630 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004631 req->flags |= REQ_F_HARDLINK;
4632
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004633 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004634 ret = -EAGAIN;
4635 goto err_req;
4636 }
4637
Jens Axboe3529d8c2019-12-19 18:24:38 -07004638 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004639 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004640 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004641 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004642 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004643 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004644 trace_io_uring_link(ctx, req, head);
4645 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004646
4647 /* last request of a link, enqueue the link */
4648 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4649 io_queue_link_head(head);
4650 *link = NULL;
4651 }
4652 } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004653 req->flags |= REQ_F_LINK;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004654 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004655 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004656
Jens Axboe9e645e112019-05-10 16:07:28 -06004657 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004658 ret = io_req_defer_prep(req, sqe);
4659 if (ret)
4660 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004661 *link = req;
4662 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004663 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004664 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004665
4666 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004667}
4668
Jens Axboe9a56a232019-01-09 09:06:50 -07004669/*
4670 * Batched submission is done, ensure local IO is flushed out.
4671 */
4672static void io_submit_state_end(struct io_submit_state *state)
4673{
4674 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004675 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004676 if (state->free_reqs)
4677 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4678 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004679}
4680
4681/*
4682 * Start submission side cache.
4683 */
4684static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004685 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004686{
4687 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004688 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004689 state->file = NULL;
4690 state->ios_left = max_ios;
4691}
4692
Jens Axboe2b188cc2019-01-07 10:46:33 -07004693static void io_commit_sqring(struct io_ring_ctx *ctx)
4694{
Hristo Venev75b28af2019-08-26 17:23:46 +00004695 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004696
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004697 /*
4698 * Ensure any loads from the SQEs are done at this point,
4699 * since once we write the new head, the application could
4700 * write new data to them.
4701 */
4702 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004703}
4704
4705/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004706 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004707 * that is mapped by userspace. This means that care needs to be taken to
4708 * ensure that reads are stable, as we cannot rely on userspace always
4709 * being a good citizen. If members of the sqe are validated and then later
4710 * used, it's important that those reads are done through READ_ONCE() to
4711 * prevent a re-load down the line.
4712 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004713static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4714 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004715{
Hristo Venev75b28af2019-08-26 17:23:46 +00004716 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004717 unsigned head;
4718
4719 /*
4720 * The cached sq head (or cq tail) serves two purposes:
4721 *
4722 * 1) allows us to batch the cost of updating the user visible
4723 * head updates.
4724 * 2) allows the kernel side to track the head on its own, even
4725 * though the application is the one updating it.
4726 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004727 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004728 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004729 /*
4730 * All io need record the previous position, if LINK vs DARIN,
4731 * it can be used to mark the position of the first IO in the
4732 * link list.
4733 */
4734 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004735 *sqe_ptr = &ctx->sq_sqes[head];
4736 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4737 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004738 ctx->cached_sq_head++;
4739 return true;
4740 }
4741
4742 /* drop invalid entries */
4743 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004744 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004745 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004746 return false;
4747}
4748
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004749static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004750 struct file *ring_file, int ring_fd,
4751 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004752{
4753 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004754 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004755 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004756 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004757
Jens Axboec4a2ed72019-11-21 21:01:26 -07004758 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004759 if (test_bit(0, &ctx->sq_check_overflow)) {
4760 if (!list_empty(&ctx->cq_overflow_list) &&
4761 !io_cqring_overflow_flush(ctx, false))
4762 return -EBUSY;
4763 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004764
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004765 /* make sure SQ entry isn't read before tail */
4766 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004767
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004768 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4769 return -EAGAIN;
4770
Jens Axboe6c271ce2019-01-10 11:22:30 -07004771 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004772 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004773 statep = &state;
4774 }
4775
4776 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004777 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004778 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004779
Pavel Begunkov196be952019-11-07 01:41:06 +03004780 req = io_get_req(ctx, statep);
4781 if (unlikely(!req)) {
4782 if (!submitted)
4783 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004784 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004785 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004786 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004787 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004788 break;
4789 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004790
Jens Axboed3656342019-12-18 09:50:26 -07004791 /* will complete beyond this point, count as submitted */
4792 submitted++;
4793
4794 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4795 io_cqring_add_event(req, -EINVAL);
4796 io_double_put_req(req);
4797 break;
4798 }
4799
4800 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004801 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4802 if (!mm_fault) {
4803 use_mm(ctx->sqo_mm);
4804 *mm = ctx->sqo_mm;
4805 }
4806 }
4807
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004808 req->ring_file = ring_file;
4809 req->ring_fd = ring_fd;
4810 req->has_user = *mm != NULL;
4811 req->in_async = async;
4812 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07004813 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
4814 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004815 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004816 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004817 }
4818
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004819 if (submitted != nr)
4820 percpu_ref_put_many(&ctx->refs, nr - submitted);
Jens Axboe9e645e112019-05-10 16:07:28 -06004821 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004822 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004823 if (statep)
4824 io_submit_state_end(&state);
4825
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004826 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4827 io_commit_sqring(ctx);
4828
Jens Axboe6c271ce2019-01-10 11:22:30 -07004829 return submitted;
4830}
4831
4832static int io_sq_thread(void *data)
4833{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004834 struct io_ring_ctx *ctx = data;
4835 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004836 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004837 mm_segment_t old_fs;
4838 DEFINE_WAIT(wait);
4839 unsigned inflight;
4840 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004841 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004842
Jens Axboe206aefd2019-11-07 18:27:42 -07004843 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004844
Jens Axboe6c271ce2019-01-10 11:22:30 -07004845 old_fs = get_fs();
4846 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004847 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004848
Jens Axboec1edbf52019-11-10 16:56:04 -07004849 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004850 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004851 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004852
4853 if (inflight) {
4854 unsigned nr_events = 0;
4855
4856 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004857 /*
4858 * inflight is the count of the maximum possible
4859 * entries we submitted, but it can be smaller
4860 * if we dropped some of them. If we don't have
4861 * poll entries available, then we know that we
4862 * have nothing left to poll for. Reset the
4863 * inflight count to zero in that case.
4864 */
4865 mutex_lock(&ctx->uring_lock);
4866 if (!list_empty(&ctx->poll_list))
4867 __io_iopoll_check(ctx, &nr_events, 0);
4868 else
4869 inflight = 0;
4870 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004871 } else {
4872 /*
4873 * Normal IO, just pretend everything completed.
4874 * We don't have to poll completions for that.
4875 */
4876 nr_events = inflight;
4877 }
4878
4879 inflight -= nr_events;
4880 if (!inflight)
4881 timeout = jiffies + ctx->sq_thread_idle;
4882 }
4883
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004884 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004885
4886 /*
4887 * If submit got -EBUSY, flag us as needing the application
4888 * to enter the kernel to reap and flush events.
4889 */
4890 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004891 /*
4892 * We're polling. If we're within the defined idle
4893 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004894 * to sleep. The exception is if we got EBUSY doing
4895 * more IO, we should wait for the application to
4896 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004897 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004898 if (inflight ||
4899 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004900 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004901 continue;
4902 }
4903
4904 /*
4905 * Drop cur_mm before scheduling, we can't hold it for
4906 * long periods (or over schedule()). Do this before
4907 * adding ourselves to the waitqueue, as the unuse/drop
4908 * may sleep.
4909 */
4910 if (cur_mm) {
4911 unuse_mm(cur_mm);
4912 mmput(cur_mm);
4913 cur_mm = NULL;
4914 }
4915
4916 prepare_to_wait(&ctx->sqo_wait, &wait,
4917 TASK_INTERRUPTIBLE);
4918
4919 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004920 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004921 /* make sure to read SQ tail after writing flags */
4922 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004923
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004924 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004925 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004926 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004927 finish_wait(&ctx->sqo_wait, &wait);
4928 break;
4929 }
4930 if (signal_pending(current))
4931 flush_signals(current);
4932 schedule();
4933 finish_wait(&ctx->sqo_wait, &wait);
4934
Hristo Venev75b28af2019-08-26 17:23:46 +00004935 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004936 continue;
4937 }
4938 finish_wait(&ctx->sqo_wait, &wait);
4939
Hristo Venev75b28af2019-08-26 17:23:46 +00004940 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004941 }
4942
Jens Axboe8a4955f2019-12-09 14:52:35 -07004943 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004944 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004945 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004946 if (ret > 0)
4947 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004948 }
4949
4950 set_fs(old_fs);
4951 if (cur_mm) {
4952 unuse_mm(cur_mm);
4953 mmput(cur_mm);
4954 }
Jens Axboe181e4482019-11-25 08:52:30 -07004955 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004956
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004957 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004958
Jens Axboe6c271ce2019-01-10 11:22:30 -07004959 return 0;
4960}
4961
Jens Axboebda52162019-09-24 13:47:15 -06004962struct io_wait_queue {
4963 struct wait_queue_entry wq;
4964 struct io_ring_ctx *ctx;
4965 unsigned to_wait;
4966 unsigned nr_timeouts;
4967};
4968
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004969static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004970{
4971 struct io_ring_ctx *ctx = iowq->ctx;
4972
4973 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004974 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004975 * started waiting. For timeouts, we always want to return to userspace,
4976 * regardless of event count.
4977 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004978 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004979 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4980}
4981
4982static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4983 int wake_flags, void *key)
4984{
4985 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4986 wq);
4987
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004988 /* use noflush == true, as we can't safely rely on locking context */
4989 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004990 return -1;
4991
4992 return autoremove_wake_function(curr, mode, wake_flags, key);
4993}
4994
Jens Axboe2b188cc2019-01-07 10:46:33 -07004995/*
4996 * Wait until events become available, if we don't already have some. The
4997 * application must reap them itself, as they reside on the shared cq ring.
4998 */
4999static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5000 const sigset_t __user *sig, size_t sigsz)
5001{
Jens Axboebda52162019-09-24 13:47:15 -06005002 struct io_wait_queue iowq = {
5003 .wq = {
5004 .private = current,
5005 .func = io_wake_function,
5006 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5007 },
5008 .ctx = ctx,
5009 .to_wait = min_events,
5010 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005011 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005012 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005013
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005014 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005015 return 0;
5016
5017 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005018#ifdef CONFIG_COMPAT
5019 if (in_compat_syscall())
5020 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005021 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005022 else
5023#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005024 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005025
Jens Axboe2b188cc2019-01-07 10:46:33 -07005026 if (ret)
5027 return ret;
5028 }
5029
Jens Axboebda52162019-09-24 13:47:15 -06005030 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005031 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005032 do {
5033 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5034 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005035 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005036 break;
5037 schedule();
5038 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005039 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005040 break;
5041 }
5042 } while (1);
5043 finish_wait(&ctx->wait, &iowq.wq);
5044
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005045 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005046
Hristo Venev75b28af2019-08-26 17:23:46 +00005047 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005048}
5049
Jens Axboe6b063142019-01-10 22:13:58 -07005050static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5051{
5052#if defined(CONFIG_UNIX)
5053 if (ctx->ring_sock) {
5054 struct sock *sock = ctx->ring_sock->sk;
5055 struct sk_buff *skb;
5056
5057 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5058 kfree_skb(skb);
5059 }
5060#else
5061 int i;
5062
Jens Axboe65e19f52019-10-26 07:20:21 -06005063 for (i = 0; i < ctx->nr_user_files; i++) {
5064 struct file *file;
5065
5066 file = io_file_from_index(ctx, i);
5067 if (file)
5068 fput(file);
5069 }
Jens Axboe6b063142019-01-10 22:13:58 -07005070#endif
5071}
5072
Jens Axboe05f3fb32019-12-09 11:22:50 -07005073static void io_file_ref_kill(struct percpu_ref *ref)
5074{
5075 struct fixed_file_data *data;
5076
5077 data = container_of(ref, struct fixed_file_data, refs);
5078 complete(&data->done);
5079}
5080
Jens Axboe6b063142019-01-10 22:13:58 -07005081static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5082{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005083 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005084 unsigned nr_tables, i;
5085
Jens Axboe05f3fb32019-12-09 11:22:50 -07005086 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005087 return -ENXIO;
5088
Jens Axboe05f3fb32019-12-09 11:22:50 -07005089 /* protect against inflight atomic switch, which drops the ref */
5090 flush_work(&data->ref_work);
5091 percpu_ref_get(&data->refs);
5092 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5093 wait_for_completion(&data->done);
5094 percpu_ref_put(&data->refs);
5095 percpu_ref_exit(&data->refs);
5096
Jens Axboe6b063142019-01-10 22:13:58 -07005097 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005098 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5099 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005100 kfree(data->table[i].files);
5101 kfree(data->table);
5102 kfree(data);
5103 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005104 ctx->nr_user_files = 0;
5105 return 0;
5106}
5107
Jens Axboe6c271ce2019-01-10 11:22:30 -07005108static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5109{
5110 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005111 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005112 /*
5113 * The park is a bit of a work-around, without it we get
5114 * warning spews on shutdown with SQPOLL set and affinity
5115 * set to a single CPU.
5116 */
Jens Axboe06058632019-04-13 09:26:03 -06005117 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005118 kthread_stop(ctx->sqo_thread);
5119 ctx->sqo_thread = NULL;
5120 }
5121}
5122
Jens Axboe6b063142019-01-10 22:13:58 -07005123static void io_finish_async(struct io_ring_ctx *ctx)
5124{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005125 io_sq_thread_stop(ctx);
5126
Jens Axboe561fb042019-10-24 07:25:42 -06005127 if (ctx->io_wq) {
5128 io_wq_destroy(ctx->io_wq);
5129 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005130 }
5131}
5132
5133#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005134/*
5135 * Ensure the UNIX gc is aware of our file set, so we are certain that
5136 * the io_uring can be safely unregistered on process exit, even if we have
5137 * loops in the file referencing.
5138 */
5139static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5140{
5141 struct sock *sk = ctx->ring_sock->sk;
5142 struct scm_fp_list *fpl;
5143 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005144 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005145
5146 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5147 unsigned long inflight = ctx->user->unix_inflight + nr;
5148
5149 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5150 return -EMFILE;
5151 }
5152
5153 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5154 if (!fpl)
5155 return -ENOMEM;
5156
5157 skb = alloc_skb(0, GFP_KERNEL);
5158 if (!skb) {
5159 kfree(fpl);
5160 return -ENOMEM;
5161 }
5162
5163 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005164
Jens Axboe08a45172019-10-03 08:11:03 -06005165 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005166 fpl->user = get_uid(ctx->user);
5167 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005168 struct file *file = io_file_from_index(ctx, i + offset);
5169
5170 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005171 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005172 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005173 unix_inflight(fpl->user, fpl->fp[nr_files]);
5174 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005175 }
5176
Jens Axboe08a45172019-10-03 08:11:03 -06005177 if (nr_files) {
5178 fpl->max = SCM_MAX_FD;
5179 fpl->count = nr_files;
5180 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005181 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005182 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5183 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005184
Jens Axboe08a45172019-10-03 08:11:03 -06005185 for (i = 0; i < nr_files; i++)
5186 fput(fpl->fp[i]);
5187 } else {
5188 kfree_skb(skb);
5189 kfree(fpl);
5190 }
Jens Axboe6b063142019-01-10 22:13:58 -07005191
5192 return 0;
5193}
5194
5195/*
5196 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5197 * causes regular reference counting to break down. We rely on the UNIX
5198 * garbage collection to take care of this problem for us.
5199 */
5200static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5201{
5202 unsigned left, total;
5203 int ret = 0;
5204
5205 total = 0;
5206 left = ctx->nr_user_files;
5207 while (left) {
5208 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005209
5210 ret = __io_sqe_files_scm(ctx, this_files, total);
5211 if (ret)
5212 break;
5213 left -= this_files;
5214 total += this_files;
5215 }
5216
5217 if (!ret)
5218 return 0;
5219
5220 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005221 struct file *file = io_file_from_index(ctx, total);
5222
5223 if (file)
5224 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005225 total++;
5226 }
5227
5228 return ret;
5229}
5230#else
5231static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5232{
5233 return 0;
5234}
5235#endif
5236
Jens Axboe65e19f52019-10-26 07:20:21 -06005237static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5238 unsigned nr_files)
5239{
5240 int i;
5241
5242 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005243 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005244 unsigned this_files;
5245
5246 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5247 table->files = kcalloc(this_files, sizeof(struct file *),
5248 GFP_KERNEL);
5249 if (!table->files)
5250 break;
5251 nr_files -= this_files;
5252 }
5253
5254 if (i == nr_tables)
5255 return 0;
5256
5257 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005258 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005259 kfree(table->files);
5260 }
5261 return 1;
5262}
5263
Jens Axboe05f3fb32019-12-09 11:22:50 -07005264static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005265{
5266#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005267 struct sock *sock = ctx->ring_sock->sk;
5268 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5269 struct sk_buff *skb;
5270 int i;
5271
5272 __skb_queue_head_init(&list);
5273
5274 /*
5275 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5276 * remove this entry and rearrange the file array.
5277 */
5278 skb = skb_dequeue(head);
5279 while (skb) {
5280 struct scm_fp_list *fp;
5281
5282 fp = UNIXCB(skb).fp;
5283 for (i = 0; i < fp->count; i++) {
5284 int left;
5285
5286 if (fp->fp[i] != file)
5287 continue;
5288
5289 unix_notinflight(fp->user, fp->fp[i]);
5290 left = fp->count - 1 - i;
5291 if (left) {
5292 memmove(&fp->fp[i], &fp->fp[i + 1],
5293 left * sizeof(struct file *));
5294 }
5295 fp->count--;
5296 if (!fp->count) {
5297 kfree_skb(skb);
5298 skb = NULL;
5299 } else {
5300 __skb_queue_tail(&list, skb);
5301 }
5302 fput(file);
5303 file = NULL;
5304 break;
5305 }
5306
5307 if (!file)
5308 break;
5309
5310 __skb_queue_tail(&list, skb);
5311
5312 skb = skb_dequeue(head);
5313 }
5314
5315 if (skb_peek(&list)) {
5316 spin_lock_irq(&head->lock);
5317 while ((skb = __skb_dequeue(&list)) != NULL)
5318 __skb_queue_tail(head, skb);
5319 spin_unlock_irq(&head->lock);
5320 }
5321#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005322 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005323#endif
5324}
5325
Jens Axboe05f3fb32019-12-09 11:22:50 -07005326struct io_file_put {
5327 struct llist_node llist;
5328 struct file *file;
5329 struct completion *done;
5330};
5331
5332static void io_ring_file_ref_switch(struct work_struct *work)
5333{
5334 struct io_file_put *pfile, *tmp;
5335 struct fixed_file_data *data;
5336 struct llist_node *node;
5337
5338 data = container_of(work, struct fixed_file_data, ref_work);
5339
5340 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5341 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5342 io_ring_file_put(data->ctx, pfile->file);
5343 if (pfile->done)
5344 complete(pfile->done);
5345 else
5346 kfree(pfile);
5347 }
5348 }
5349
5350 percpu_ref_get(&data->refs);
5351 percpu_ref_switch_to_percpu(&data->refs);
5352}
5353
5354static void io_file_data_ref_zero(struct percpu_ref *ref)
5355{
5356 struct fixed_file_data *data;
5357
5358 data = container_of(ref, struct fixed_file_data, refs);
5359
5360 /* we can't safely switch from inside this context, punt to wq */
5361 queue_work(system_wq, &data->ref_work);
5362}
5363
5364static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5365 unsigned nr_args)
5366{
5367 __s32 __user *fds = (__s32 __user *) arg;
5368 unsigned nr_tables;
5369 struct file *file;
5370 int fd, ret = 0;
5371 unsigned i;
5372
5373 if (ctx->file_data)
5374 return -EBUSY;
5375 if (!nr_args)
5376 return -EINVAL;
5377 if (nr_args > IORING_MAX_FIXED_FILES)
5378 return -EMFILE;
5379
5380 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5381 if (!ctx->file_data)
5382 return -ENOMEM;
5383 ctx->file_data->ctx = ctx;
5384 init_completion(&ctx->file_data->done);
5385
5386 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5387 ctx->file_data->table = kcalloc(nr_tables,
5388 sizeof(struct fixed_file_table),
5389 GFP_KERNEL);
5390 if (!ctx->file_data->table) {
5391 kfree(ctx->file_data);
5392 ctx->file_data = NULL;
5393 return -ENOMEM;
5394 }
5395
5396 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5397 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5398 kfree(ctx->file_data->table);
5399 kfree(ctx->file_data);
5400 ctx->file_data = NULL;
5401 return -ENOMEM;
5402 }
5403 ctx->file_data->put_llist.first = NULL;
5404 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5405
5406 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5407 percpu_ref_exit(&ctx->file_data->refs);
5408 kfree(ctx->file_data->table);
5409 kfree(ctx->file_data);
5410 ctx->file_data = NULL;
5411 return -ENOMEM;
5412 }
5413
5414 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5415 struct fixed_file_table *table;
5416 unsigned index;
5417
5418 ret = -EFAULT;
5419 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5420 break;
5421 /* allow sparse sets */
5422 if (fd == -1) {
5423 ret = 0;
5424 continue;
5425 }
5426
5427 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5428 index = i & IORING_FILE_TABLE_MASK;
5429 file = fget(fd);
5430
5431 ret = -EBADF;
5432 if (!file)
5433 break;
5434
5435 /*
5436 * Don't allow io_uring instances to be registered. If UNIX
5437 * isn't enabled, then this causes a reference cycle and this
5438 * instance can never get freed. If UNIX is enabled we'll
5439 * handle it just fine, but there's still no point in allowing
5440 * a ring fd as it doesn't support regular read/write anyway.
5441 */
5442 if (file->f_op == &io_uring_fops) {
5443 fput(file);
5444 break;
5445 }
5446 ret = 0;
5447 table->files[index] = file;
5448 }
5449
5450 if (ret) {
5451 for (i = 0; i < ctx->nr_user_files; i++) {
5452 file = io_file_from_index(ctx, i);
5453 if (file)
5454 fput(file);
5455 }
5456 for (i = 0; i < nr_tables; i++)
5457 kfree(ctx->file_data->table[i].files);
5458
5459 kfree(ctx->file_data->table);
5460 kfree(ctx->file_data);
5461 ctx->file_data = NULL;
5462 ctx->nr_user_files = 0;
5463 return ret;
5464 }
5465
5466 ret = io_sqe_files_scm(ctx);
5467 if (ret)
5468 io_sqe_files_unregister(ctx);
5469
5470 return ret;
5471}
5472
Jens Axboec3a31e62019-10-03 13:59:56 -06005473static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5474 int index)
5475{
5476#if defined(CONFIG_UNIX)
5477 struct sock *sock = ctx->ring_sock->sk;
5478 struct sk_buff_head *head = &sock->sk_receive_queue;
5479 struct sk_buff *skb;
5480
5481 /*
5482 * See if we can merge this file into an existing skb SCM_RIGHTS
5483 * file set. If there's no room, fall back to allocating a new skb
5484 * and filling it in.
5485 */
5486 spin_lock_irq(&head->lock);
5487 skb = skb_peek(head);
5488 if (skb) {
5489 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5490
5491 if (fpl->count < SCM_MAX_FD) {
5492 __skb_unlink(skb, head);
5493 spin_unlock_irq(&head->lock);
5494 fpl->fp[fpl->count] = get_file(file);
5495 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5496 fpl->count++;
5497 spin_lock_irq(&head->lock);
5498 __skb_queue_head(head, skb);
5499 } else {
5500 skb = NULL;
5501 }
5502 }
5503 spin_unlock_irq(&head->lock);
5504
5505 if (skb) {
5506 fput(file);
5507 return 0;
5508 }
5509
5510 return __io_sqe_files_scm(ctx, 1, index);
5511#else
5512 return 0;
5513#endif
5514}
5515
Jens Axboe05f3fb32019-12-09 11:22:50 -07005516static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005517{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005518 struct fixed_file_data *data;
5519
5520 data = container_of(ref, struct fixed_file_data, refs);
5521 clear_bit(FFD_F_ATOMIC, &data->state);
5522}
5523
5524static bool io_queue_file_removal(struct fixed_file_data *data,
5525 struct file *file)
5526{
5527 struct io_file_put *pfile, pfile_stack;
5528 DECLARE_COMPLETION_ONSTACK(done);
5529
5530 /*
5531 * If we fail allocating the struct we need for doing async reomval
5532 * of this file, just punt to sync and wait for it.
5533 */
5534 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5535 if (!pfile) {
5536 pfile = &pfile_stack;
5537 pfile->done = &done;
5538 }
5539
5540 pfile->file = file;
5541 llist_add(&pfile->llist, &data->put_llist);
5542
5543 if (pfile == &pfile_stack) {
5544 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5545 percpu_ref_put(&data->refs);
5546 percpu_ref_switch_to_atomic(&data->refs,
5547 io_atomic_switch);
5548 }
5549 wait_for_completion(&done);
5550 flush_work(&data->ref_work);
5551 return false;
5552 }
5553
5554 return true;
5555}
5556
5557static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5558 struct io_uring_files_update *up,
5559 unsigned nr_args)
5560{
5561 struct fixed_file_data *data = ctx->file_data;
5562 bool ref_switch = false;
5563 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005564 __s32 __user *fds;
5565 int fd, i, err;
5566 __u32 done;
5567
Jens Axboe05f3fb32019-12-09 11:22:50 -07005568 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005569 return -EOVERFLOW;
5570 if (done > ctx->nr_user_files)
5571 return -EINVAL;
5572
5573 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005574 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005575 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005576 struct fixed_file_table *table;
5577 unsigned index;
5578
Jens Axboec3a31e62019-10-03 13:59:56 -06005579 err = 0;
5580 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5581 err = -EFAULT;
5582 break;
5583 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005584 i = array_index_nospec(up->offset, ctx->nr_user_files);
5585 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005586 index = i & IORING_FILE_TABLE_MASK;
5587 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005588 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005589 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005590 if (io_queue_file_removal(data, file))
5591 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005592 }
5593 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005594 file = fget(fd);
5595 if (!file) {
5596 err = -EBADF;
5597 break;
5598 }
5599 /*
5600 * Don't allow io_uring instances to be registered. If
5601 * UNIX isn't enabled, then this causes a reference
5602 * cycle and this instance can never get freed. If UNIX
5603 * is enabled we'll handle it just fine, but there's
5604 * still no point in allowing a ring fd as it doesn't
5605 * support regular read/write anyway.
5606 */
5607 if (file->f_op == &io_uring_fops) {
5608 fput(file);
5609 err = -EBADF;
5610 break;
5611 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005612 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005613 err = io_sqe_file_register(ctx, file, i);
5614 if (err)
5615 break;
5616 }
5617 nr_args--;
5618 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005619 up->offset++;
5620 }
5621
5622 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5623 percpu_ref_put(&data->refs);
5624 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005625 }
5626
5627 return done ? done : err;
5628}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005629static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5630 unsigned nr_args)
5631{
5632 struct io_uring_files_update up;
5633
5634 if (!ctx->file_data)
5635 return -ENXIO;
5636 if (!nr_args)
5637 return -EINVAL;
5638 if (copy_from_user(&up, arg, sizeof(up)))
5639 return -EFAULT;
5640 if (up.resv)
5641 return -EINVAL;
5642
5643 return __io_sqe_files_update(ctx, &up, nr_args);
5644}
Jens Axboec3a31e62019-10-03 13:59:56 -06005645
Jens Axboe7d723062019-11-12 22:31:31 -07005646static void io_put_work(struct io_wq_work *work)
5647{
5648 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5649
5650 io_put_req(req);
5651}
5652
5653static void io_get_work(struct io_wq_work *work)
5654{
5655 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5656
5657 refcount_inc(&req->refs);
5658}
5659
Jens Axboe6c271ce2019-01-10 11:22:30 -07005660static int io_sq_offload_start(struct io_ring_ctx *ctx,
5661 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005662{
Jens Axboe576a3472019-11-25 08:49:20 -07005663 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005664 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005665 int ret;
5666
Jens Axboe6c271ce2019-01-10 11:22:30 -07005667 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005668 mmgrab(current->mm);
5669 ctx->sqo_mm = current->mm;
5670
Jens Axboe6c271ce2019-01-10 11:22:30 -07005671 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005672 ret = -EPERM;
5673 if (!capable(CAP_SYS_ADMIN))
5674 goto err;
5675
Jens Axboe917257d2019-04-13 09:28:55 -06005676 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5677 if (!ctx->sq_thread_idle)
5678 ctx->sq_thread_idle = HZ;
5679
Jens Axboe6c271ce2019-01-10 11:22:30 -07005680 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005681 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005682
Jens Axboe917257d2019-04-13 09:28:55 -06005683 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005684 if (cpu >= nr_cpu_ids)
5685 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005686 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005687 goto err;
5688
Jens Axboe6c271ce2019-01-10 11:22:30 -07005689 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5690 ctx, cpu,
5691 "io_uring-sq");
5692 } else {
5693 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5694 "io_uring-sq");
5695 }
5696 if (IS_ERR(ctx->sqo_thread)) {
5697 ret = PTR_ERR(ctx->sqo_thread);
5698 ctx->sqo_thread = NULL;
5699 goto err;
5700 }
5701 wake_up_process(ctx->sqo_thread);
5702 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5703 /* Can't have SQ_AFF without SQPOLL */
5704 ret = -EINVAL;
5705 goto err;
5706 }
5707
Jens Axboe576a3472019-11-25 08:49:20 -07005708 data.mm = ctx->sqo_mm;
5709 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005710 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005711 data.get_work = io_get_work;
5712 data.put_work = io_put_work;
5713
Jens Axboe561fb042019-10-24 07:25:42 -06005714 /* Do QD, or 4 * CPUS, whatever is smallest */
5715 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005716 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005717 if (IS_ERR(ctx->io_wq)) {
5718 ret = PTR_ERR(ctx->io_wq);
5719 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005720 goto err;
5721 }
5722
5723 return 0;
5724err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005725 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005726 mmdrop(ctx->sqo_mm);
5727 ctx->sqo_mm = NULL;
5728 return ret;
5729}
5730
5731static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5732{
5733 atomic_long_sub(nr_pages, &user->locked_vm);
5734}
5735
5736static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5737{
5738 unsigned long page_limit, cur_pages, new_pages;
5739
5740 /* Don't allow more pages than we can safely lock */
5741 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5742
5743 do {
5744 cur_pages = atomic_long_read(&user->locked_vm);
5745 new_pages = cur_pages + nr_pages;
5746 if (new_pages > page_limit)
5747 return -ENOMEM;
5748 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5749 new_pages) != cur_pages);
5750
5751 return 0;
5752}
5753
5754static void io_mem_free(void *ptr)
5755{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005756 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005757
Mark Rutland52e04ef2019-04-30 17:30:21 +01005758 if (!ptr)
5759 return;
5760
5761 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005762 if (put_page_testzero(page))
5763 free_compound_page(page);
5764}
5765
5766static void *io_mem_alloc(size_t size)
5767{
5768 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5769 __GFP_NORETRY;
5770
5771 return (void *) __get_free_pages(gfp_flags, get_order(size));
5772}
5773
Hristo Venev75b28af2019-08-26 17:23:46 +00005774static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5775 size_t *sq_offset)
5776{
5777 struct io_rings *rings;
5778 size_t off, sq_array_size;
5779
5780 off = struct_size(rings, cqes, cq_entries);
5781 if (off == SIZE_MAX)
5782 return SIZE_MAX;
5783
5784#ifdef CONFIG_SMP
5785 off = ALIGN(off, SMP_CACHE_BYTES);
5786 if (off == 0)
5787 return SIZE_MAX;
5788#endif
5789
5790 sq_array_size = array_size(sizeof(u32), sq_entries);
5791 if (sq_array_size == SIZE_MAX)
5792 return SIZE_MAX;
5793
5794 if (check_add_overflow(off, sq_array_size, &off))
5795 return SIZE_MAX;
5796
5797 if (sq_offset)
5798 *sq_offset = off;
5799
5800 return off;
5801}
5802
Jens Axboe2b188cc2019-01-07 10:46:33 -07005803static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5804{
Hristo Venev75b28af2019-08-26 17:23:46 +00005805 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005806
Hristo Venev75b28af2019-08-26 17:23:46 +00005807 pages = (size_t)1 << get_order(
5808 rings_size(sq_entries, cq_entries, NULL));
5809 pages += (size_t)1 << get_order(
5810 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005811
Hristo Venev75b28af2019-08-26 17:23:46 +00005812 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005813}
5814
Jens Axboeedafcce2019-01-09 09:16:05 -07005815static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5816{
5817 int i, j;
5818
5819 if (!ctx->user_bufs)
5820 return -ENXIO;
5821
5822 for (i = 0; i < ctx->nr_user_bufs; i++) {
5823 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5824
5825 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005826 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005827
5828 if (ctx->account_mem)
5829 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005830 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005831 imu->nr_bvecs = 0;
5832 }
5833
5834 kfree(ctx->user_bufs);
5835 ctx->user_bufs = NULL;
5836 ctx->nr_user_bufs = 0;
5837 return 0;
5838}
5839
5840static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5841 void __user *arg, unsigned index)
5842{
5843 struct iovec __user *src;
5844
5845#ifdef CONFIG_COMPAT
5846 if (ctx->compat) {
5847 struct compat_iovec __user *ciovs;
5848 struct compat_iovec ciov;
5849
5850 ciovs = (struct compat_iovec __user *) arg;
5851 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5852 return -EFAULT;
5853
Jens Axboed55e5f52019-12-11 16:12:15 -07005854 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005855 dst->iov_len = ciov.iov_len;
5856 return 0;
5857 }
5858#endif
5859 src = (struct iovec __user *) arg;
5860 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5861 return -EFAULT;
5862 return 0;
5863}
5864
5865static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5866 unsigned nr_args)
5867{
5868 struct vm_area_struct **vmas = NULL;
5869 struct page **pages = NULL;
5870 int i, j, got_pages = 0;
5871 int ret = -EINVAL;
5872
5873 if (ctx->user_bufs)
5874 return -EBUSY;
5875 if (!nr_args || nr_args > UIO_MAXIOV)
5876 return -EINVAL;
5877
5878 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5879 GFP_KERNEL);
5880 if (!ctx->user_bufs)
5881 return -ENOMEM;
5882
5883 for (i = 0; i < nr_args; i++) {
5884 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5885 unsigned long off, start, end, ubuf;
5886 int pret, nr_pages;
5887 struct iovec iov;
5888 size_t size;
5889
5890 ret = io_copy_iov(ctx, &iov, arg, i);
5891 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005892 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005893
5894 /*
5895 * Don't impose further limits on the size and buffer
5896 * constraints here, we'll -EINVAL later when IO is
5897 * submitted if they are wrong.
5898 */
5899 ret = -EFAULT;
5900 if (!iov.iov_base || !iov.iov_len)
5901 goto err;
5902
5903 /* arbitrary limit, but we need something */
5904 if (iov.iov_len > SZ_1G)
5905 goto err;
5906
5907 ubuf = (unsigned long) iov.iov_base;
5908 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5909 start = ubuf >> PAGE_SHIFT;
5910 nr_pages = end - start;
5911
5912 if (ctx->account_mem) {
5913 ret = io_account_mem(ctx->user, nr_pages);
5914 if (ret)
5915 goto err;
5916 }
5917
5918 ret = 0;
5919 if (!pages || nr_pages > got_pages) {
5920 kfree(vmas);
5921 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005922 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005923 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005924 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005925 sizeof(struct vm_area_struct *),
5926 GFP_KERNEL);
5927 if (!pages || !vmas) {
5928 ret = -ENOMEM;
5929 if (ctx->account_mem)
5930 io_unaccount_mem(ctx->user, nr_pages);
5931 goto err;
5932 }
5933 got_pages = nr_pages;
5934 }
5935
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005936 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005937 GFP_KERNEL);
5938 ret = -ENOMEM;
5939 if (!imu->bvec) {
5940 if (ctx->account_mem)
5941 io_unaccount_mem(ctx->user, nr_pages);
5942 goto err;
5943 }
5944
5945 ret = 0;
5946 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005947 pret = get_user_pages(ubuf, nr_pages,
5948 FOLL_WRITE | FOLL_LONGTERM,
5949 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005950 if (pret == nr_pages) {
5951 /* don't support file backed memory */
5952 for (j = 0; j < nr_pages; j++) {
5953 struct vm_area_struct *vma = vmas[j];
5954
5955 if (vma->vm_file &&
5956 !is_file_hugepages(vma->vm_file)) {
5957 ret = -EOPNOTSUPP;
5958 break;
5959 }
5960 }
5961 } else {
5962 ret = pret < 0 ? pret : -EFAULT;
5963 }
5964 up_read(&current->mm->mmap_sem);
5965 if (ret) {
5966 /*
5967 * if we did partial map, or found file backed vmas,
5968 * release any pages we did get
5969 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005970 if (pret > 0)
5971 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005972 if (ctx->account_mem)
5973 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005974 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005975 goto err;
5976 }
5977
5978 off = ubuf & ~PAGE_MASK;
5979 size = iov.iov_len;
5980 for (j = 0; j < nr_pages; j++) {
5981 size_t vec_len;
5982
5983 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5984 imu->bvec[j].bv_page = pages[j];
5985 imu->bvec[j].bv_len = vec_len;
5986 imu->bvec[j].bv_offset = off;
5987 off = 0;
5988 size -= vec_len;
5989 }
5990 /* store original address for later verification */
5991 imu->ubuf = ubuf;
5992 imu->len = iov.iov_len;
5993 imu->nr_bvecs = nr_pages;
5994
5995 ctx->nr_user_bufs++;
5996 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005997 kvfree(pages);
5998 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005999 return 0;
6000err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006001 kvfree(pages);
6002 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006003 io_sqe_buffer_unregister(ctx);
6004 return ret;
6005}
6006
Jens Axboe9b402842019-04-11 11:45:41 -06006007static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6008{
6009 __s32 __user *fds = arg;
6010 int fd;
6011
6012 if (ctx->cq_ev_fd)
6013 return -EBUSY;
6014
6015 if (copy_from_user(&fd, fds, sizeof(*fds)))
6016 return -EFAULT;
6017
6018 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6019 if (IS_ERR(ctx->cq_ev_fd)) {
6020 int ret = PTR_ERR(ctx->cq_ev_fd);
6021 ctx->cq_ev_fd = NULL;
6022 return ret;
6023 }
6024
6025 return 0;
6026}
6027
6028static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6029{
6030 if (ctx->cq_ev_fd) {
6031 eventfd_ctx_put(ctx->cq_ev_fd);
6032 ctx->cq_ev_fd = NULL;
6033 return 0;
6034 }
6035
6036 return -ENXIO;
6037}
6038
Jens Axboe2b188cc2019-01-07 10:46:33 -07006039static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6040{
Jens Axboe6b063142019-01-10 22:13:58 -07006041 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006042 if (ctx->sqo_mm)
6043 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006044
6045 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006046 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006047 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006048 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006049
Jens Axboe2b188cc2019-01-07 10:46:33 -07006050#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006051 if (ctx->ring_sock) {
6052 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006053 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006054 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006055#endif
6056
Hristo Venev75b28af2019-08-26 17:23:46 +00006057 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006058 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006059
6060 percpu_ref_exit(&ctx->refs);
6061 if (ctx->account_mem)
6062 io_unaccount_mem(ctx->user,
6063 ring_pages(ctx->sq_entries, ctx->cq_entries));
6064 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006065 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006066 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006067 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006068 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006069 kfree(ctx);
6070}
6071
6072static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6073{
6074 struct io_ring_ctx *ctx = file->private_data;
6075 __poll_t mask = 0;
6076
6077 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006078 /*
6079 * synchronizes with barrier from wq_has_sleeper call in
6080 * io_commit_cqring
6081 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006082 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006083 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6084 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006085 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006086 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006087 mask |= EPOLLIN | EPOLLRDNORM;
6088
6089 return mask;
6090}
6091
6092static int io_uring_fasync(int fd, struct file *file, int on)
6093{
6094 struct io_ring_ctx *ctx = file->private_data;
6095
6096 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6097}
6098
6099static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6100{
6101 mutex_lock(&ctx->uring_lock);
6102 percpu_ref_kill(&ctx->refs);
6103 mutex_unlock(&ctx->uring_lock);
6104
Jens Axboe5262f562019-09-17 12:26:57 -06006105 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006106 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006107
6108 if (ctx->io_wq)
6109 io_wq_cancel_all(ctx->io_wq);
6110
Jens Axboedef596e2019-01-09 08:59:42 -07006111 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006112 /* if we failed setting up the ctx, we might not have any rings */
6113 if (ctx->rings)
6114 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07006115 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006116 io_ring_ctx_free(ctx);
6117}
6118
6119static int io_uring_release(struct inode *inode, struct file *file)
6120{
6121 struct io_ring_ctx *ctx = file->private_data;
6122
6123 file->private_data = NULL;
6124 io_ring_ctx_wait_and_kill(ctx);
6125 return 0;
6126}
6127
Jens Axboefcb323c2019-10-24 12:39:47 -06006128static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6129 struct files_struct *files)
6130{
6131 struct io_kiocb *req;
6132 DEFINE_WAIT(wait);
6133
6134 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006135 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006136
6137 spin_lock_irq(&ctx->inflight_lock);
6138 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006139 if (req->work.files != files)
6140 continue;
6141 /* req is being completed, ignore */
6142 if (!refcount_inc_not_zero(&req->refs))
6143 continue;
6144 cancel_req = req;
6145 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006146 }
Jens Axboe768134d2019-11-10 20:30:53 -07006147 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006148 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006149 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006150 spin_unlock_irq(&ctx->inflight_lock);
6151
Jens Axboe768134d2019-11-10 20:30:53 -07006152 /* We need to keep going until we don't find a matching req */
6153 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006154 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006155
6156 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6157 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006158 schedule();
6159 }
Jens Axboe768134d2019-11-10 20:30:53 -07006160 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006161}
6162
6163static int io_uring_flush(struct file *file, void *data)
6164{
6165 struct io_ring_ctx *ctx = file->private_data;
6166
6167 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006168 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6169 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006170 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006171 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006172 return 0;
6173}
6174
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006175static void *io_uring_validate_mmap_request(struct file *file,
6176 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006177{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006178 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006179 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006180 struct page *page;
6181 void *ptr;
6182
6183 switch (offset) {
6184 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006185 case IORING_OFF_CQ_RING:
6186 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006187 break;
6188 case IORING_OFF_SQES:
6189 ptr = ctx->sq_sqes;
6190 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006191 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006192 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006193 }
6194
6195 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006196 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006197 return ERR_PTR(-EINVAL);
6198
6199 return ptr;
6200}
6201
6202#ifdef CONFIG_MMU
6203
6204static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6205{
6206 size_t sz = vma->vm_end - vma->vm_start;
6207 unsigned long pfn;
6208 void *ptr;
6209
6210 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6211 if (IS_ERR(ptr))
6212 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006213
6214 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6215 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6216}
6217
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006218#else /* !CONFIG_MMU */
6219
6220static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6221{
6222 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6223}
6224
6225static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6226{
6227 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6228}
6229
6230static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6231 unsigned long addr, unsigned long len,
6232 unsigned long pgoff, unsigned long flags)
6233{
6234 void *ptr;
6235
6236 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6237 if (IS_ERR(ptr))
6238 return PTR_ERR(ptr);
6239
6240 return (unsigned long) ptr;
6241}
6242
6243#endif /* !CONFIG_MMU */
6244
Jens Axboe2b188cc2019-01-07 10:46:33 -07006245SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6246 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6247 size_t, sigsz)
6248{
6249 struct io_ring_ctx *ctx;
6250 long ret = -EBADF;
6251 int submitted = 0;
6252 struct fd f;
6253
Jens Axboe6c271ce2019-01-10 11:22:30 -07006254 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006255 return -EINVAL;
6256
6257 f = fdget(fd);
6258 if (!f.file)
6259 return -EBADF;
6260
6261 ret = -EOPNOTSUPP;
6262 if (f.file->f_op != &io_uring_fops)
6263 goto out_fput;
6264
6265 ret = -ENXIO;
6266 ctx = f.file->private_data;
6267 if (!percpu_ref_tryget(&ctx->refs))
6268 goto out_fput;
6269
Jens Axboe6c271ce2019-01-10 11:22:30 -07006270 /*
6271 * For SQ polling, the thread will do all submissions and completions.
6272 * Just return the requested submit count, and wake the thread if
6273 * we were asked to.
6274 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006275 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006276 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006277 if (!list_empty_careful(&ctx->cq_overflow_list))
6278 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006279 if (flags & IORING_ENTER_SQ_WAKEUP)
6280 wake_up(&ctx->sqo_wait);
6281 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006282 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006283 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006284
Jens Axboe44d28272020-01-16 19:00:24 -07006285 if (current->mm != ctx->sqo_mm ||
6286 current_cred() != ctx->creds) {
6287 ret = -EPERM;
6288 goto out;
6289 }
6290
Jens Axboe2b188cc2019-01-07 10:46:33 -07006291 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006292 /* already have mm, so io_submit_sqes() won't try to grab it */
6293 cur_mm = ctx->sqo_mm;
6294 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6295 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006296 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006297
6298 if (submitted != to_submit)
6299 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006300 }
6301 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006302 unsigned nr_events = 0;
6303
Jens Axboe2b188cc2019-01-07 10:46:33 -07006304 min_complete = min(min_complete, ctx->cq_entries);
6305
Jens Axboedef596e2019-01-09 08:59:42 -07006306 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006307 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006308 } else {
6309 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6310 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006311 }
6312
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006313out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006314 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006315out_fput:
6316 fdput(f);
6317 return submitted ? submitted : ret;
6318}
6319
6320static const struct file_operations io_uring_fops = {
6321 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006322 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006323 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006324#ifndef CONFIG_MMU
6325 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6326 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6327#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006328 .poll = io_uring_poll,
6329 .fasync = io_uring_fasync,
6330};
6331
6332static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6333 struct io_uring_params *p)
6334{
Hristo Venev75b28af2019-08-26 17:23:46 +00006335 struct io_rings *rings;
6336 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006337
Hristo Venev75b28af2019-08-26 17:23:46 +00006338 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6339 if (size == SIZE_MAX)
6340 return -EOVERFLOW;
6341
6342 rings = io_mem_alloc(size);
6343 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006344 return -ENOMEM;
6345
Hristo Venev75b28af2019-08-26 17:23:46 +00006346 ctx->rings = rings;
6347 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6348 rings->sq_ring_mask = p->sq_entries - 1;
6349 rings->cq_ring_mask = p->cq_entries - 1;
6350 rings->sq_ring_entries = p->sq_entries;
6351 rings->cq_ring_entries = p->cq_entries;
6352 ctx->sq_mask = rings->sq_ring_mask;
6353 ctx->cq_mask = rings->cq_ring_mask;
6354 ctx->sq_entries = rings->sq_ring_entries;
6355 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006356
6357 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006358 if (size == SIZE_MAX) {
6359 io_mem_free(ctx->rings);
6360 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006361 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006362 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006363
6364 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006365 if (!ctx->sq_sqes) {
6366 io_mem_free(ctx->rings);
6367 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006368 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006369 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006370
Jens Axboe2b188cc2019-01-07 10:46:33 -07006371 return 0;
6372}
6373
6374/*
6375 * Allocate an anonymous fd, this is what constitutes the application
6376 * visible backing of an io_uring instance. The application mmaps this
6377 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6378 * we have to tie this fd to a socket for file garbage collection purposes.
6379 */
6380static int io_uring_get_fd(struct io_ring_ctx *ctx)
6381{
6382 struct file *file;
6383 int ret;
6384
6385#if defined(CONFIG_UNIX)
6386 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6387 &ctx->ring_sock);
6388 if (ret)
6389 return ret;
6390#endif
6391
6392 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6393 if (ret < 0)
6394 goto err;
6395
6396 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6397 O_RDWR | O_CLOEXEC);
6398 if (IS_ERR(file)) {
6399 put_unused_fd(ret);
6400 ret = PTR_ERR(file);
6401 goto err;
6402 }
6403
6404#if defined(CONFIG_UNIX)
6405 ctx->ring_sock->file = file;
6406#endif
6407 fd_install(ret, file);
6408 return ret;
6409err:
6410#if defined(CONFIG_UNIX)
6411 sock_release(ctx->ring_sock);
6412 ctx->ring_sock = NULL;
6413#endif
6414 return ret;
6415}
6416
6417static int io_uring_create(unsigned entries, struct io_uring_params *p)
6418{
6419 struct user_struct *user = NULL;
6420 struct io_ring_ctx *ctx;
6421 bool account_mem;
6422 int ret;
6423
Jens Axboe8110c1a2019-12-28 15:39:54 -07006424 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006425 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006426 if (entries > IORING_MAX_ENTRIES) {
6427 if (!(p->flags & IORING_SETUP_CLAMP))
6428 return -EINVAL;
6429 entries = IORING_MAX_ENTRIES;
6430 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006431
6432 /*
6433 * Use twice as many entries for the CQ ring. It's possible for the
6434 * application to drive a higher depth than the size of the SQ ring,
6435 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006436 * some flexibility in overcommitting a bit. If the application has
6437 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6438 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006439 */
6440 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006441 if (p->flags & IORING_SETUP_CQSIZE) {
6442 /*
6443 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6444 * to a power-of-two, if it isn't already. We do NOT impose
6445 * any cq vs sq ring sizing.
6446 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006447 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006448 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006449 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6450 if (!(p->flags & IORING_SETUP_CLAMP))
6451 return -EINVAL;
6452 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6453 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006454 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6455 } else {
6456 p->cq_entries = 2 * p->sq_entries;
6457 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006458
6459 user = get_uid(current_user());
6460 account_mem = !capable(CAP_IPC_LOCK);
6461
6462 if (account_mem) {
6463 ret = io_account_mem(user,
6464 ring_pages(p->sq_entries, p->cq_entries));
6465 if (ret) {
6466 free_uid(user);
6467 return ret;
6468 }
6469 }
6470
6471 ctx = io_ring_ctx_alloc(p);
6472 if (!ctx) {
6473 if (account_mem)
6474 io_unaccount_mem(user, ring_pages(p->sq_entries,
6475 p->cq_entries));
6476 free_uid(user);
6477 return -ENOMEM;
6478 }
6479 ctx->compat = in_compat_syscall();
6480 ctx->account_mem = account_mem;
6481 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006482 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006483
6484 ret = io_allocate_scq_urings(ctx, p);
6485 if (ret)
6486 goto err;
6487
Jens Axboe6c271ce2019-01-10 11:22:30 -07006488 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006489 if (ret)
6490 goto err;
6491
Jens Axboe2b188cc2019-01-07 10:46:33 -07006492 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006493 p->sq_off.head = offsetof(struct io_rings, sq.head);
6494 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6495 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6496 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6497 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6498 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6499 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006500
6501 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006502 p->cq_off.head = offsetof(struct io_rings, cq.head);
6503 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6504 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6505 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6506 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6507 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006508
Jens Axboe044c1ab2019-10-28 09:15:33 -06006509 /*
6510 * Install ring fd as the very last thing, so we don't risk someone
6511 * having closed it before we finish setup
6512 */
6513 ret = io_uring_get_fd(ctx);
6514 if (ret < 0)
6515 goto err;
6516
Jens Axboeda8c9692019-12-02 18:51:26 -07006517 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboeba042912019-12-25 16:33:42 -07006518 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006519 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006520 return ret;
6521err:
6522 io_ring_ctx_wait_and_kill(ctx);
6523 return ret;
6524}
6525
6526/*
6527 * Sets up an aio uring context, and returns the fd. Applications asks for a
6528 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6529 * params structure passed in.
6530 */
6531static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6532{
6533 struct io_uring_params p;
6534 long ret;
6535 int i;
6536
6537 if (copy_from_user(&p, params, sizeof(p)))
6538 return -EFAULT;
6539 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6540 if (p.resv[i])
6541 return -EINVAL;
6542 }
6543
Jens Axboe6c271ce2019-01-10 11:22:30 -07006544 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006545 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
6546 IORING_SETUP_CLAMP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006547 return -EINVAL;
6548
6549 ret = io_uring_create(entries, &p);
6550 if (ret < 0)
6551 return ret;
6552
6553 if (copy_to_user(params, &p, sizeof(p)))
6554 return -EFAULT;
6555
6556 return ret;
6557}
6558
6559SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6560 struct io_uring_params __user *, params)
6561{
6562 return io_uring_setup(entries, params);
6563}
6564
Jens Axboeedafcce2019-01-09 09:16:05 -07006565static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6566 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006567 __releases(ctx->uring_lock)
6568 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006569{
6570 int ret;
6571
Jens Axboe35fa71a2019-04-22 10:23:23 -06006572 /*
6573 * We're inside the ring mutex, if the ref is already dying, then
6574 * someone else killed the ctx or is already going through
6575 * io_uring_register().
6576 */
6577 if (percpu_ref_is_dying(&ctx->refs))
6578 return -ENXIO;
6579
Jens Axboe05f3fb32019-12-09 11:22:50 -07006580 if (opcode != IORING_UNREGISTER_FILES &&
6581 opcode != IORING_REGISTER_FILES_UPDATE) {
6582 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006583
Jens Axboe05f3fb32019-12-09 11:22:50 -07006584 /*
6585 * Drop uring mutex before waiting for references to exit. If
6586 * another thread is currently inside io_uring_enter() it might
6587 * need to grab the uring_lock to make progress. If we hold it
6588 * here across the drain wait, then we can deadlock. It's safe
6589 * to drop the mutex here, since no new references will come in
6590 * after we've killed the percpu ref.
6591 */
6592 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006593 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006594 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006595 if (ret) {
6596 percpu_ref_resurrect(&ctx->refs);
6597 ret = -EINTR;
6598 goto out;
6599 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006600 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006601
6602 switch (opcode) {
6603 case IORING_REGISTER_BUFFERS:
6604 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6605 break;
6606 case IORING_UNREGISTER_BUFFERS:
6607 ret = -EINVAL;
6608 if (arg || nr_args)
6609 break;
6610 ret = io_sqe_buffer_unregister(ctx);
6611 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006612 case IORING_REGISTER_FILES:
6613 ret = io_sqe_files_register(ctx, arg, nr_args);
6614 break;
6615 case IORING_UNREGISTER_FILES:
6616 ret = -EINVAL;
6617 if (arg || nr_args)
6618 break;
6619 ret = io_sqe_files_unregister(ctx);
6620 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006621 case IORING_REGISTER_FILES_UPDATE:
6622 ret = io_sqe_files_update(ctx, arg, nr_args);
6623 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006624 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006625 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006626 ret = -EINVAL;
6627 if (nr_args != 1)
6628 break;
6629 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006630 if (ret)
6631 break;
6632 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6633 ctx->eventfd_async = 1;
6634 else
6635 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006636 break;
6637 case IORING_UNREGISTER_EVENTFD:
6638 ret = -EINVAL;
6639 if (arg || nr_args)
6640 break;
6641 ret = io_eventfd_unregister(ctx);
6642 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006643 default:
6644 ret = -EINVAL;
6645 break;
6646 }
6647
Jens Axboe05f3fb32019-12-09 11:22:50 -07006648
6649 if (opcode != IORING_UNREGISTER_FILES &&
6650 opcode != IORING_REGISTER_FILES_UPDATE) {
6651 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006652 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07006653out:
6654 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006655 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006656 return ret;
6657}
6658
6659SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6660 void __user *, arg, unsigned int, nr_args)
6661{
6662 struct io_ring_ctx *ctx;
6663 long ret = -EBADF;
6664 struct fd f;
6665
6666 f = fdget(fd);
6667 if (!f.file)
6668 return -EBADF;
6669
6670 ret = -EOPNOTSUPP;
6671 if (f.file->f_op != &io_uring_fops)
6672 goto out_fput;
6673
6674 ctx = f.file->private_data;
6675
6676 mutex_lock(&ctx->uring_lock);
6677 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6678 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006679 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6680 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006681out_fput:
6682 fdput(f);
6683 return ret;
6684}
6685
Jens Axboe2b188cc2019-01-07 10:46:33 -07006686static int __init io_uring_init(void)
6687{
Jens Axboed3656342019-12-18 09:50:26 -07006688 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006689 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6690 return 0;
6691};
6692__initcall(io_uring_init);