blob: 68843a12300030c5e2101e29ac25ce4b39f28229 [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 */
Jens Axboe5262f562019-09-17 12:26:57 -0600509#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600510#define REQ_F_ISREG 2048 /* regular file */
511#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700512#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800513#define REQ_F_INFLIGHT 16384 /* on inflight list */
514#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700515#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboece35a472019-12-17 08:04:44 -0700516#define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */
Jens Axboeba042912019-12-25 16:33:42 -0700517#define REQ_F_CUR_POS 262144 /* read/write uses file position */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700518 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600519 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600520 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700521
Jens Axboefcb323c2019-10-24 12:39:47 -0600522 struct list_head inflight_entry;
523
Jens Axboe561fb042019-10-24 07:25:42 -0600524 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700525};
526
527#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700528#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700529
Jens Axboe9a56a232019-01-09 09:06:50 -0700530struct io_submit_state {
531 struct blk_plug plug;
532
533 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700534 * io_kiocb alloc cache
535 */
536 void *reqs[IO_IOPOLL_BATCH];
537 unsigned int free_reqs;
538 unsigned int cur_req;
539
540 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700541 * File reference cache
542 */
543 struct file *file;
544 unsigned int fd;
545 unsigned int has_refs;
546 unsigned int used_refs;
547 unsigned int ios_left;
548};
549
Jens Axboed3656342019-12-18 09:50:26 -0700550struct io_op_def {
551 /* needs req->io allocated for deferral/async */
552 unsigned async_ctx : 1;
553 /* needs current->mm setup, does mm access */
554 unsigned needs_mm : 1;
555 /* needs req->file assigned */
556 unsigned needs_file : 1;
557 /* needs req->file assigned IFF fd is >= 0 */
558 unsigned fd_non_neg : 1;
559 /* hash wq insertion if file is a regular file */
560 unsigned hash_reg_file : 1;
561 /* unbound wq insertion if file is a non-regular file */
562 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700563 /* opcode is not supported by this kernel */
564 unsigned not_supported : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700565};
566
567static const struct io_op_def io_op_defs[] = {
568 {
569 /* IORING_OP_NOP */
570 },
571 {
572 /* IORING_OP_READV */
573 .async_ctx = 1,
574 .needs_mm = 1,
575 .needs_file = 1,
576 .unbound_nonreg_file = 1,
577 },
578 {
579 /* IORING_OP_WRITEV */
580 .async_ctx = 1,
581 .needs_mm = 1,
582 .needs_file = 1,
583 .hash_reg_file = 1,
584 .unbound_nonreg_file = 1,
585 },
586 {
587 /* IORING_OP_FSYNC */
588 .needs_file = 1,
589 },
590 {
591 /* IORING_OP_READ_FIXED */
592 .needs_file = 1,
593 .unbound_nonreg_file = 1,
594 },
595 {
596 /* IORING_OP_WRITE_FIXED */
597 .needs_file = 1,
598 .hash_reg_file = 1,
599 .unbound_nonreg_file = 1,
600 },
601 {
602 /* IORING_OP_POLL_ADD */
603 .needs_file = 1,
604 .unbound_nonreg_file = 1,
605 },
606 {
607 /* IORING_OP_POLL_REMOVE */
608 },
609 {
610 /* IORING_OP_SYNC_FILE_RANGE */
611 .needs_file = 1,
612 },
613 {
614 /* IORING_OP_SENDMSG */
615 .async_ctx = 1,
616 .needs_mm = 1,
617 .needs_file = 1,
618 .unbound_nonreg_file = 1,
619 },
620 {
621 /* IORING_OP_RECVMSG */
622 .async_ctx = 1,
623 .needs_mm = 1,
624 .needs_file = 1,
625 .unbound_nonreg_file = 1,
626 },
627 {
628 /* IORING_OP_TIMEOUT */
629 .async_ctx = 1,
630 .needs_mm = 1,
631 },
632 {
633 /* IORING_OP_TIMEOUT_REMOVE */
634 },
635 {
636 /* IORING_OP_ACCEPT */
637 .needs_mm = 1,
638 .needs_file = 1,
639 .unbound_nonreg_file = 1,
640 },
641 {
642 /* IORING_OP_ASYNC_CANCEL */
643 },
644 {
645 /* IORING_OP_LINK_TIMEOUT */
646 .async_ctx = 1,
647 .needs_mm = 1,
648 },
649 {
650 /* IORING_OP_CONNECT */
651 .async_ctx = 1,
652 .needs_mm = 1,
653 .needs_file = 1,
654 .unbound_nonreg_file = 1,
655 },
656 {
657 /* IORING_OP_FALLOCATE */
658 .needs_file = 1,
659 },
660 {
661 /* IORING_OP_OPENAT */
662 .needs_file = 1,
663 .fd_non_neg = 1,
664 },
665 {
666 /* IORING_OP_CLOSE */
667 .needs_file = 1,
668 },
669 {
670 /* IORING_OP_FILES_UPDATE */
671 .needs_mm = 1,
672 },
673 {
674 /* IORING_OP_STATX */
675 .needs_mm = 1,
676 .needs_file = 1,
677 .fd_non_neg = 1,
678 },
Jens Axboe3a6820f2019-12-22 15:19:35 -0700679 {
680 /* IORING_OP_READ */
681 .needs_mm = 1,
682 .needs_file = 1,
683 .unbound_nonreg_file = 1,
684 },
685 {
686 /* IORING_OP_WRITE */
687 .needs_mm = 1,
688 .needs_file = 1,
689 .unbound_nonreg_file = 1,
690 },
Jens Axboe4840e412019-12-25 22:03:45 -0700691 {
692 /* IORING_OP_FADVISE */
693 .needs_file = 1,
694 },
Jens Axboec1ca7572019-12-25 22:18:28 -0700695 {
696 /* IORING_OP_MADVISE */
697 .needs_mm = 1,
698 },
Jens Axboefddafac2020-01-04 20:19:44 -0700699 {
700 /* IORING_OP_SEND */
701 .needs_mm = 1,
702 .needs_file = 1,
703 .unbound_nonreg_file = 1,
704 },
705 {
706 /* IORING_OP_RECV */
707 .needs_mm = 1,
708 .needs_file = 1,
709 .unbound_nonreg_file = 1,
710 },
Jens Axboecebdb982020-01-08 17:59:24 -0700711 {
712 /* IORING_OP_OPENAT2 */
713 .needs_file = 1,
714 .fd_non_neg = 1,
715 },
Jens Axboed3656342019-12-18 09:50:26 -0700716};
717
Jens Axboe561fb042019-10-24 07:25:42 -0600718static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700719static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800720static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700721static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700722static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
723static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700724static int __io_sqe_files_update(struct io_ring_ctx *ctx,
725 struct io_uring_files_update *ip,
726 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600727
Jens Axboe2b188cc2019-01-07 10:46:33 -0700728static struct kmem_cache *req_cachep;
729
730static const struct file_operations io_uring_fops;
731
732struct sock *io_uring_get_socket(struct file *file)
733{
734#if defined(CONFIG_UNIX)
735 if (file->f_op == &io_uring_fops) {
736 struct io_ring_ctx *ctx = file->private_data;
737
738 return ctx->ring_sock->sk;
739 }
740#endif
741 return NULL;
742}
743EXPORT_SYMBOL(io_uring_get_socket);
744
745static void io_ring_ctx_ref_free(struct percpu_ref *ref)
746{
747 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
748
Jens Axboe206aefd2019-11-07 18:27:42 -0700749 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700750}
751
752static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
753{
754 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700755 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700756
757 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
758 if (!ctx)
759 return NULL;
760
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700761 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
762 if (!ctx->fallback_req)
763 goto err;
764
Jens Axboe206aefd2019-11-07 18:27:42 -0700765 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
766 if (!ctx->completions)
767 goto err;
768
Jens Axboe78076bb2019-12-04 19:56:40 -0700769 /*
770 * Use 5 bits less than the max cq entries, that should give us around
771 * 32 entries per hash list if totally full and uniformly spread.
772 */
773 hash_bits = ilog2(p->cq_entries);
774 hash_bits -= 5;
775 if (hash_bits <= 0)
776 hash_bits = 1;
777 ctx->cancel_hash_bits = hash_bits;
778 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
779 GFP_KERNEL);
780 if (!ctx->cancel_hash)
781 goto err;
782 __hash_init(ctx->cancel_hash, 1U << hash_bits);
783
Roman Gushchin21482892019-05-07 10:01:48 -0700784 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700785 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
786 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700787
788 ctx->flags = p->flags;
789 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700790 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700791 init_completion(&ctx->completions[0]);
792 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700793 mutex_init(&ctx->uring_lock);
794 init_waitqueue_head(&ctx->wait);
795 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700796 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700797 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600798 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600799 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600800 init_waitqueue_head(&ctx->inflight_wait);
801 spin_lock_init(&ctx->inflight_lock);
802 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700803 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700804err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700805 if (ctx->fallback_req)
806 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700807 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700808 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700809 kfree(ctx);
810 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700811}
812
Bob Liu9d858b22019-11-13 18:06:25 +0800813static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600814{
Jackie Liua197f662019-11-08 08:09:12 -0700815 struct io_ring_ctx *ctx = req->ctx;
816
Jens Axboe498ccd92019-10-25 10:04:25 -0600817 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
818 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600819}
820
Bob Liu9d858b22019-11-13 18:06:25 +0800821static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600822{
Bob Liu9d858b22019-11-13 18:06:25 +0800823 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
824 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600825
Bob Liu9d858b22019-11-13 18:06:25 +0800826 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600827}
828
829static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600830{
831 struct io_kiocb *req;
832
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600833 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800834 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600835 list_del_init(&req->list);
836 return req;
837 }
838
839 return NULL;
840}
841
Jens Axboe5262f562019-09-17 12:26:57 -0600842static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
843{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600844 struct io_kiocb *req;
845
846 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700847 if (req) {
848 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
849 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800850 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700851 list_del_init(&req->list);
852 return req;
853 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600854 }
855
856 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600857}
858
Jens Axboede0617e2019-04-06 21:51:27 -0600859static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700860{
Hristo Venev75b28af2019-08-26 17:23:46 +0000861 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700862
Hristo Venev75b28af2019-08-26 17:23:46 +0000863 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700864 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000865 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700866
Jens Axboe2b188cc2019-01-07 10:46:33 -0700867 if (wq_has_sleeper(&ctx->cq_wait)) {
868 wake_up_interruptible(&ctx->cq_wait);
869 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
870 }
871 }
872}
873
Jens Axboe94ae5e72019-11-14 19:39:52 -0700874static inline bool io_prep_async_work(struct io_kiocb *req,
875 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600876{
Jens Axboed3656342019-12-18 09:50:26 -0700877 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600878 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600879
Jens Axboed3656342019-12-18 09:50:26 -0700880 if (req->flags & REQ_F_ISREG) {
881 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700882 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700883 } else {
884 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700885 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600886 }
Jens Axboed3656342019-12-18 09:50:26 -0700887 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700888 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600889
Jens Axboe94ae5e72019-11-14 19:39:52 -0700890 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600891 return do_hashed;
892}
893
Jackie Liua197f662019-11-08 08:09:12 -0700894static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600895{
Jackie Liua197f662019-11-08 08:09:12 -0700896 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700897 struct io_kiocb *link;
898 bool do_hashed;
899
900 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600901
902 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
903 req->flags);
904 if (!do_hashed) {
905 io_wq_enqueue(ctx->io_wq, &req->work);
906 } else {
907 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
908 file_inode(req->file));
909 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700910
911 if (link)
912 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600913}
914
Jens Axboe5262f562019-09-17 12:26:57 -0600915static void io_kill_timeout(struct io_kiocb *req)
916{
917 int ret;
918
Jens Axboe2d283902019-12-04 11:08:05 -0700919 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600920 if (ret != -1) {
921 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600922 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700923 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800924 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600925 }
926}
927
928static void io_kill_timeouts(struct io_ring_ctx *ctx)
929{
930 struct io_kiocb *req, *tmp;
931
932 spin_lock_irq(&ctx->completion_lock);
933 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
934 io_kill_timeout(req);
935 spin_unlock_irq(&ctx->completion_lock);
936}
937
Jens Axboede0617e2019-04-06 21:51:27 -0600938static void io_commit_cqring(struct io_ring_ctx *ctx)
939{
940 struct io_kiocb *req;
941
Jens Axboe5262f562019-09-17 12:26:57 -0600942 while ((req = io_get_timeout_req(ctx)) != NULL)
943 io_kill_timeout(req);
944
Jens Axboede0617e2019-04-06 21:51:27 -0600945 __io_commit_cqring(ctx);
946
947 while ((req = io_get_deferred_req(ctx)) != NULL) {
948 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700949 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600950 }
951}
952
Jens Axboe2b188cc2019-01-07 10:46:33 -0700953static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
954{
Hristo Venev75b28af2019-08-26 17:23:46 +0000955 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700956 unsigned tail;
957
958 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200959 /*
960 * writes to the cq entry need to come after reading head; the
961 * control dependency is enough as we're using WRITE_ONCE to
962 * fill the cq entry
963 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000964 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700965 return NULL;
966
967 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000968 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700969}
970
Jens Axboef2842ab2020-01-08 11:04:00 -0700971static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
972{
973 if (!ctx->eventfd_async)
974 return true;
975 return io_wq_current_is_worker() || in_interrupt();
976}
977
Jens Axboe8c838782019-03-12 15:48:16 -0600978static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
979{
980 if (waitqueue_active(&ctx->wait))
981 wake_up(&ctx->wait);
982 if (waitqueue_active(&ctx->sqo_wait))
983 wake_up(&ctx->sqo_wait);
Jens Axboef2842ab2020-01-08 11:04:00 -0700984 if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -0600985 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600986}
987
Jens Axboec4a2ed72019-11-21 21:01:26 -0700988/* Returns true if there are no backlogged entries after the flush */
989static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700990{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700991 struct io_rings *rings = ctx->rings;
992 struct io_uring_cqe *cqe;
993 struct io_kiocb *req;
994 unsigned long flags;
995 LIST_HEAD(list);
996
997 if (!force) {
998 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700999 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001000 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1001 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001002 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001003 }
1004
1005 spin_lock_irqsave(&ctx->completion_lock, flags);
1006
1007 /* if force is set, the ring is going away. always drop after that */
1008 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001009 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001010
Jens Axboec4a2ed72019-11-21 21:01:26 -07001011 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001012 while (!list_empty(&ctx->cq_overflow_list)) {
1013 cqe = io_get_cqring(ctx);
1014 if (!cqe && !force)
1015 break;
1016
1017 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1018 list);
1019 list_move(&req->list, &list);
1020 if (cqe) {
1021 WRITE_ONCE(cqe->user_data, req->user_data);
1022 WRITE_ONCE(cqe->res, req->result);
1023 WRITE_ONCE(cqe->flags, 0);
1024 } else {
1025 WRITE_ONCE(ctx->rings->cq_overflow,
1026 atomic_inc_return(&ctx->cached_cq_overflow));
1027 }
1028 }
1029
1030 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001031 if (cqe) {
1032 clear_bit(0, &ctx->sq_check_overflow);
1033 clear_bit(0, &ctx->cq_check_overflow);
1034 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001035 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1036 io_cqring_ev_posted(ctx);
1037
1038 while (!list_empty(&list)) {
1039 req = list_first_entry(&list, struct io_kiocb, list);
1040 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001041 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001042 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001043
1044 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001045}
1046
Jens Axboe78e19bb2019-11-06 15:21:34 -07001047static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001048{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001049 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001050 struct io_uring_cqe *cqe;
1051
Jens Axboe78e19bb2019-11-06 15:21:34 -07001052 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001053
Jens Axboe2b188cc2019-01-07 10:46:33 -07001054 /*
1055 * If we can't get a cq entry, userspace overflowed the
1056 * submission (by quite a lot). Increment the overflow count in
1057 * the ring.
1058 */
1059 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001060 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001061 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001062 WRITE_ONCE(cqe->res, res);
1063 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001064 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001065 WRITE_ONCE(ctx->rings->cq_overflow,
1066 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001067 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001068 if (list_empty(&ctx->cq_overflow_list)) {
1069 set_bit(0, &ctx->sq_check_overflow);
1070 set_bit(0, &ctx->cq_check_overflow);
1071 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001072 refcount_inc(&req->refs);
1073 req->result = res;
1074 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001075 }
1076}
1077
Jens Axboe78e19bb2019-11-06 15:21:34 -07001078static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001079{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001080 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001081 unsigned long flags;
1082
1083 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001084 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001085 io_commit_cqring(ctx);
1086 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1087
Jens Axboe8c838782019-03-12 15:48:16 -06001088 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001089}
1090
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001091static inline bool io_is_fallback_req(struct io_kiocb *req)
1092{
1093 return req == (struct io_kiocb *)
1094 ((unsigned long) req->ctx->fallback_req & ~1UL);
1095}
1096
1097static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1098{
1099 struct io_kiocb *req;
1100
1101 req = ctx->fallback_req;
1102 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1103 return req;
1104
1105 return NULL;
1106}
1107
Jens Axboe2579f912019-01-09 09:10:43 -07001108static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1109 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001110{
Jens Axboefd6fab22019-03-14 16:30:06 -06001111 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001112 struct io_kiocb *req;
1113
Jens Axboe2579f912019-01-09 09:10:43 -07001114 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001115 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001116 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001117 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001118 } else if (!state->free_reqs) {
1119 size_t sz;
1120 int ret;
1121
1122 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001123 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1124
1125 /*
1126 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1127 * retry single alloc to be on the safe side.
1128 */
1129 if (unlikely(ret <= 0)) {
1130 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1131 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001132 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001133 ret = 1;
1134 }
Jens Axboe2579f912019-01-09 09:10:43 -07001135 state->free_reqs = ret - 1;
1136 state->cur_req = 1;
1137 req = state->reqs[0];
1138 } else {
1139 req = state->reqs[state->cur_req];
1140 state->free_reqs--;
1141 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001142 }
1143
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001144got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001145 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001146 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001147 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001148 req->ctx = ctx;
1149 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001150 /* one is dropped after submission, the other at completion */
1151 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001152 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001153 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001154 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001155fallback:
1156 req = io_get_fallback_req(ctx);
1157 if (req)
1158 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001159 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001160 return NULL;
1161}
1162
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001163static void __io_req_do_free(struct io_kiocb *req)
1164{
1165 if (likely(!io_is_fallback_req(req)))
1166 kmem_cache_free(req_cachep, req);
1167 else
1168 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1169}
1170
Jens Axboec6ca97b302019-12-28 12:11:08 -07001171static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001172{
Jens Axboefcb323c2019-10-24 12:39:47 -06001173 struct io_ring_ctx *ctx = req->ctx;
1174
YueHaibing96fd84d2020-01-07 22:22:44 +08001175 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001176 if (req->file) {
1177 if (req->flags & REQ_F_FIXED_FILE)
1178 percpu_ref_put(&ctx->file_data->refs);
1179 else
1180 fput(req->file);
1181 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001182}
1183
1184static void __io_free_req(struct io_kiocb *req)
1185{
1186 __io_req_aux_free(req);
1187
Jens Axboefcb323c2019-10-24 12:39:47 -06001188 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001189 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001190 unsigned long flags;
1191
1192 spin_lock_irqsave(&ctx->inflight_lock, flags);
1193 list_del(&req->inflight_entry);
1194 if (waitqueue_active(&ctx->inflight_wait))
1195 wake_up(&ctx->inflight_wait);
1196 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1197 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001198
1199 percpu_ref_put(&req->ctx->refs);
1200 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001201}
1202
Jens Axboec6ca97b302019-12-28 12:11:08 -07001203struct req_batch {
1204 void *reqs[IO_IOPOLL_BATCH];
1205 int to_free;
1206 int need_iter;
1207};
1208
1209static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1210{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001211 int fixed_refs = rb->to_free;
1212
Jens Axboec6ca97b302019-12-28 12:11:08 -07001213 if (!rb->to_free)
1214 return;
1215 if (rb->need_iter) {
1216 int i, inflight = 0;
1217 unsigned long flags;
1218
Jens Axboe10fef4b2020-01-09 07:52:28 -07001219 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001220 for (i = 0; i < rb->to_free; i++) {
1221 struct io_kiocb *req = rb->reqs[i];
1222
Jens Axboe10fef4b2020-01-09 07:52:28 -07001223 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001224 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001225 fixed_refs++;
1226 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001227 if (req->flags & REQ_F_INFLIGHT)
1228 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001229 __io_req_aux_free(req);
1230 }
1231 if (!inflight)
1232 goto do_free;
1233
1234 spin_lock_irqsave(&ctx->inflight_lock, flags);
1235 for (i = 0; i < rb->to_free; i++) {
1236 struct io_kiocb *req = rb->reqs[i];
1237
Jens Axboe10fef4b2020-01-09 07:52:28 -07001238 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001239 list_del(&req->inflight_entry);
1240 if (!--inflight)
1241 break;
1242 }
1243 }
1244 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1245
1246 if (waitqueue_active(&ctx->inflight_wait))
1247 wake_up(&ctx->inflight_wait);
1248 }
1249do_free:
1250 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001251 if (fixed_refs)
1252 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001253 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001254 rb->to_free = rb->need_iter = 0;
1255}
1256
Jackie Liua197f662019-11-08 08:09:12 -07001257static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001258{
Jackie Liua197f662019-11-08 08:09:12 -07001259 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001260 int ret;
1261
Jens Axboe2d283902019-12-04 11:08:05 -07001262 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001263 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001264 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001265 io_commit_cqring(ctx);
1266 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001267 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001268 return true;
1269 }
1270
1271 return false;
1272}
1273
Jens Axboeba816ad2019-09-28 11:36:45 -06001274static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001275{
Jens Axboe2665abf2019-11-05 12:40:47 -07001276 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001277 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001278
Jens Axboe4d7dd462019-11-20 13:03:52 -07001279 /* Already got next link */
1280 if (req->flags & REQ_F_LINK_NEXT)
1281 return;
1282
Jens Axboe9e645e112019-05-10 16:07:28 -06001283 /*
1284 * The list should never be empty when we are called here. But could
1285 * potentially happen if the chain is messed up, check to be on the
1286 * safe side.
1287 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001288 while (!list_empty(&req->link_list)) {
1289 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1290 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001291
Pavel Begunkov44932332019-12-05 16:16:35 +03001292 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1293 (nxt->flags & REQ_F_TIMEOUT))) {
1294 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001295 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001296 req->flags &= ~REQ_F_LINK_TIMEOUT;
1297 continue;
1298 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001299
Pavel Begunkov44932332019-12-05 16:16:35 +03001300 list_del_init(&req->link_list);
1301 if (!list_empty(&nxt->link_list))
1302 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001303 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001304 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001305 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001306
Jens Axboe4d7dd462019-11-20 13:03:52 -07001307 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001308 if (wake_ev)
1309 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001310}
1311
1312/*
1313 * Called if REQ_F_LINK is set, and we fail the head request
1314 */
1315static void io_fail_links(struct io_kiocb *req)
1316{
Jens Axboe2665abf2019-11-05 12:40:47 -07001317 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001318 unsigned long flags;
1319
1320 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001321
1322 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001323 struct io_kiocb *link = list_first_entry(&req->link_list,
1324 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001325
Pavel Begunkov44932332019-12-05 16:16:35 +03001326 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001327 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001328
1329 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001330 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001331 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001332 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001333 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001334 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001335 }
Jens Axboe5d960722019-11-19 15:31:28 -07001336 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001337 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001338
1339 io_commit_cqring(ctx);
1340 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1341 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001342}
1343
Jens Axboe4d7dd462019-11-20 13:03:52 -07001344static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001345{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001346 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001347 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001348
Jens Axboe9e645e112019-05-10 16:07:28 -06001349 /*
1350 * If LINK is set, we have dependent requests in this chain. If we
1351 * didn't fail this request, queue the first one up, moving any other
1352 * dependencies to the next request. In case of failure, fail the rest
1353 * of the chain.
1354 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001355 if (req->flags & REQ_F_FAIL_LINK) {
1356 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001357 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1358 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001359 struct io_ring_ctx *ctx = req->ctx;
1360 unsigned long flags;
1361
1362 /*
1363 * If this is a timeout link, we could be racing with the
1364 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001365 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001366 */
1367 spin_lock_irqsave(&ctx->completion_lock, flags);
1368 io_req_link_next(req, nxt);
1369 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1370 } else {
1371 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001372 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001373}
Jens Axboe9e645e112019-05-10 16:07:28 -06001374
Jackie Liuc69f8db2019-11-09 11:00:08 +08001375static void io_free_req(struct io_kiocb *req)
1376{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001377 struct io_kiocb *nxt = NULL;
1378
1379 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001380 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001381
1382 if (nxt)
1383 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001384}
1385
Jens Axboeba816ad2019-09-28 11:36:45 -06001386/*
1387 * Drop reference to request, return next in chain (if there is one) if this
1388 * was the last reference to this request.
1389 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001390__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001391static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001392{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001393 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001394
Jens Axboee65ef562019-03-12 10:16:44 -06001395 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001396 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001397}
1398
Jens Axboe2b188cc2019-01-07 10:46:33 -07001399static void io_put_req(struct io_kiocb *req)
1400{
Jens Axboedef596e2019-01-09 08:59:42 -07001401 if (refcount_dec_and_test(&req->refs))
1402 io_free_req(req);
1403}
1404
Jens Axboe978db572019-11-14 22:39:04 -07001405/*
1406 * Must only be used if we don't need to care about links, usually from
1407 * within the completion handling itself.
1408 */
1409static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001410{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001411 /* drop both submit and complete references */
1412 if (refcount_sub_and_test(2, &req->refs))
1413 __io_free_req(req);
1414}
1415
Jens Axboe978db572019-11-14 22:39:04 -07001416static void io_double_put_req(struct io_kiocb *req)
1417{
1418 /* drop both submit and complete references */
1419 if (refcount_sub_and_test(2, &req->refs))
1420 io_free_req(req);
1421}
1422
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001423static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001424{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001425 struct io_rings *rings = ctx->rings;
1426
Jens Axboead3eb2c2019-12-18 17:12:20 -07001427 if (test_bit(0, &ctx->cq_check_overflow)) {
1428 /*
1429 * noflush == true is from the waitqueue handler, just ensure
1430 * we wake up the task, and the next invocation will flush the
1431 * entries. We cannot safely to it from here.
1432 */
1433 if (noflush && !list_empty(&ctx->cq_overflow_list))
1434 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001435
Jens Axboead3eb2c2019-12-18 17:12:20 -07001436 io_cqring_overflow_flush(ctx, false);
1437 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001438
Jens Axboea3a0e432019-08-20 11:03:11 -06001439 /* See comment at the top of this file */
1440 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001441 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001442}
1443
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001444static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1445{
1446 struct io_rings *rings = ctx->rings;
1447
1448 /* make sure SQ entry isn't read before tail */
1449 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1450}
1451
Jens Axboe8237e042019-12-28 10:48:22 -07001452static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001453{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001454 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1455 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001456
Jens Axboec6ca97b302019-12-28 12:11:08 -07001457 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1458 rb->need_iter++;
1459
1460 rb->reqs[rb->to_free++] = req;
1461 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1462 io_free_req_many(req->ctx, rb);
1463 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001464}
1465
Jens Axboedef596e2019-01-09 08:59:42 -07001466/*
1467 * Find and free completed poll iocbs
1468 */
1469static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1470 struct list_head *done)
1471{
Jens Axboe8237e042019-12-28 10:48:22 -07001472 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001473 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001474
Jens Axboec6ca97b302019-12-28 12:11:08 -07001475 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001476 while (!list_empty(done)) {
1477 req = list_first_entry(done, struct io_kiocb, list);
1478 list_del(&req->list);
1479
Jens Axboe78e19bb2019-11-06 15:21:34 -07001480 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001481 (*nr_events)++;
1482
Jens Axboe8237e042019-12-28 10:48:22 -07001483 if (refcount_dec_and_test(&req->refs) &&
1484 !io_req_multi_free(&rb, req))
1485 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001486 }
Jens Axboedef596e2019-01-09 08:59:42 -07001487
Jens Axboe09bb8392019-03-13 12:39:28 -06001488 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001489 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001490}
1491
1492static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1493 long min)
1494{
1495 struct io_kiocb *req, *tmp;
1496 LIST_HEAD(done);
1497 bool spin;
1498 int ret;
1499
1500 /*
1501 * Only spin for completions if we don't have multiple devices hanging
1502 * off our complete list, and we're under the requested amount.
1503 */
1504 spin = !ctx->poll_multi_file && *nr_events < min;
1505
1506 ret = 0;
1507 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001508 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001509
1510 /*
1511 * Move completed entries to our local list. If we find a
1512 * request that requires polling, break out and complete
1513 * the done list first, if we have entries there.
1514 */
1515 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1516 list_move_tail(&req->list, &done);
1517 continue;
1518 }
1519 if (!list_empty(&done))
1520 break;
1521
1522 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1523 if (ret < 0)
1524 break;
1525
1526 if (ret && spin)
1527 spin = false;
1528 ret = 0;
1529 }
1530
1531 if (!list_empty(&done))
1532 io_iopoll_complete(ctx, nr_events, &done);
1533
1534 return ret;
1535}
1536
1537/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001538 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001539 * non-spinning poll check - we'll still enter the driver poll loop, but only
1540 * as a non-spinning completion check.
1541 */
1542static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1543 long min)
1544{
Jens Axboe08f54392019-08-21 22:19:11 -06001545 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001546 int ret;
1547
1548 ret = io_do_iopoll(ctx, nr_events, min);
1549 if (ret < 0)
1550 return ret;
1551 if (!min || *nr_events >= min)
1552 return 0;
1553 }
1554
1555 return 1;
1556}
1557
1558/*
1559 * We can't just wait for polled events to come to us, we have to actively
1560 * find and complete them.
1561 */
1562static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1563{
1564 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1565 return;
1566
1567 mutex_lock(&ctx->uring_lock);
1568 while (!list_empty(&ctx->poll_list)) {
1569 unsigned int nr_events = 0;
1570
1571 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001572
1573 /*
1574 * Ensure we allow local-to-the-cpu processing to take place,
1575 * in this case we need to ensure that we reap all events.
1576 */
1577 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001578 }
1579 mutex_unlock(&ctx->uring_lock);
1580}
1581
Jens Axboe2b2ed972019-10-25 10:06:15 -06001582static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1583 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001584{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001585 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001586
1587 do {
1588 int tmin = 0;
1589
Jens Axboe500f9fb2019-08-19 12:15:59 -06001590 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001591 * Don't enter poll loop if we already have events pending.
1592 * If we do, we can potentially be spinning for commands that
1593 * already triggered a CQE (eg in error).
1594 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001595 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001596 break;
1597
1598 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001599 * If a submit got punted to a workqueue, we can have the
1600 * application entering polling for a command before it gets
1601 * issued. That app will hold the uring_lock for the duration
1602 * of the poll right here, so we need to take a breather every
1603 * now and then to ensure that the issue has a chance to add
1604 * the poll to the issued list. Otherwise we can spin here
1605 * forever, while the workqueue is stuck trying to acquire the
1606 * very same mutex.
1607 */
1608 if (!(++iters & 7)) {
1609 mutex_unlock(&ctx->uring_lock);
1610 mutex_lock(&ctx->uring_lock);
1611 }
1612
Jens Axboedef596e2019-01-09 08:59:42 -07001613 if (*nr_events < min)
1614 tmin = min - *nr_events;
1615
1616 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1617 if (ret <= 0)
1618 break;
1619 ret = 0;
1620 } while (min && !*nr_events && !need_resched());
1621
Jens Axboe2b2ed972019-10-25 10:06:15 -06001622 return ret;
1623}
1624
1625static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1626 long min)
1627{
1628 int ret;
1629
1630 /*
1631 * We disallow the app entering submit/complete with polling, but we
1632 * still need to lock the ring to prevent racing with polled issue
1633 * that got punted to a workqueue.
1634 */
1635 mutex_lock(&ctx->uring_lock);
1636 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001637 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001638 return ret;
1639}
1640
Jens Axboe491381ce2019-10-17 09:20:46 -06001641static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001642{
Jens Axboe491381ce2019-10-17 09:20:46 -06001643 /*
1644 * Tell lockdep we inherited freeze protection from submission
1645 * thread.
1646 */
1647 if (req->flags & REQ_F_ISREG) {
1648 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001649
Jens Axboe491381ce2019-10-17 09:20:46 -06001650 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001651 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001652 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001653}
1654
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001655static inline void req_set_fail_links(struct io_kiocb *req)
1656{
1657 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1658 req->flags |= REQ_F_FAIL_LINK;
1659}
1660
Jens Axboeba816ad2019-09-28 11:36:45 -06001661static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001662{
Jens Axboe9adbd452019-12-20 08:45:55 -07001663 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001664
Jens Axboe491381ce2019-10-17 09:20:46 -06001665 if (kiocb->ki_flags & IOCB_WRITE)
1666 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001667
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001668 if (res != req->result)
1669 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001670 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001671}
1672
1673static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1674{
Jens Axboe9adbd452019-12-20 08:45:55 -07001675 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001676
1677 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001678 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001679}
1680
Jens Axboeba816ad2019-09-28 11:36:45 -06001681static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1682{
Jens Axboe9adbd452019-12-20 08:45:55 -07001683 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001684 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001685
1686 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001687 io_put_req_find_next(req, &nxt);
1688
1689 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001690}
1691
Jens Axboedef596e2019-01-09 08:59:42 -07001692static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1693{
Jens Axboe9adbd452019-12-20 08:45:55 -07001694 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001695
Jens Axboe491381ce2019-10-17 09:20:46 -06001696 if (kiocb->ki_flags & IOCB_WRITE)
1697 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001698
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001699 if (res != req->result)
1700 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001701 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001702 if (res != -EAGAIN)
1703 req->flags |= REQ_F_IOPOLL_COMPLETED;
1704}
1705
1706/*
1707 * After the iocb has been issued, it's safe to be found on the poll list.
1708 * Adding the kiocb to the list AFTER submission ensures that we don't
1709 * find it from a io_iopoll_getevents() thread before the issuer is done
1710 * accessing the kiocb cookie.
1711 */
1712static void io_iopoll_req_issued(struct io_kiocb *req)
1713{
1714 struct io_ring_ctx *ctx = req->ctx;
1715
1716 /*
1717 * Track whether we have multiple files in our lists. This will impact
1718 * how we do polling eventually, not spinning if we're on potentially
1719 * different devices.
1720 */
1721 if (list_empty(&ctx->poll_list)) {
1722 ctx->poll_multi_file = false;
1723 } else if (!ctx->poll_multi_file) {
1724 struct io_kiocb *list_req;
1725
1726 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1727 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001728 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001729 ctx->poll_multi_file = true;
1730 }
1731
1732 /*
1733 * For fast devices, IO may have already completed. If it has, add
1734 * it to the front so we find it first.
1735 */
1736 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1737 list_add(&req->list, &ctx->poll_list);
1738 else
1739 list_add_tail(&req->list, &ctx->poll_list);
1740}
1741
Jens Axboe3d6770f2019-04-13 11:50:54 -06001742static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001743{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001744 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001745 int diff = state->has_refs - state->used_refs;
1746
1747 if (diff)
1748 fput_many(state->file, diff);
1749 state->file = NULL;
1750 }
1751}
1752
1753/*
1754 * Get as many references to a file as we have IOs left in this submission,
1755 * assuming most submissions are for one file, or at least that each file
1756 * has more than one submission.
1757 */
1758static struct file *io_file_get(struct io_submit_state *state, int fd)
1759{
1760 if (!state)
1761 return fget(fd);
1762
1763 if (state->file) {
1764 if (state->fd == fd) {
1765 state->used_refs++;
1766 state->ios_left--;
1767 return state->file;
1768 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001769 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001770 }
1771 state->file = fget_many(fd, state->ios_left);
1772 if (!state->file)
1773 return NULL;
1774
1775 state->fd = fd;
1776 state->has_refs = state->ios_left;
1777 state->used_refs = 1;
1778 state->ios_left--;
1779 return state->file;
1780}
1781
Jens Axboe2b188cc2019-01-07 10:46:33 -07001782/*
1783 * If we tracked the file through the SCM inflight mechanism, we could support
1784 * any file. For now, just ensure that anything potentially problematic is done
1785 * inline.
1786 */
1787static bool io_file_supports_async(struct file *file)
1788{
1789 umode_t mode = file_inode(file)->i_mode;
1790
Jens Axboe10d59342019-12-09 20:16:22 -07001791 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001792 return true;
1793 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1794 return true;
1795
1796 return false;
1797}
1798
Jens Axboe3529d8c2019-12-19 18:24:38 -07001799static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1800 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001801{
Jens Axboedef596e2019-01-09 08:59:42 -07001802 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001803 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001804 unsigned ioprio;
1805 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001806
Jens Axboe09bb8392019-03-13 12:39:28 -06001807 if (!req->file)
1808 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001809
Jens Axboe491381ce2019-10-17 09:20:46 -06001810 if (S_ISREG(file_inode(req->file)->i_mode))
1811 req->flags |= REQ_F_ISREG;
1812
Jens Axboe2b188cc2019-01-07 10:46:33 -07001813 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001814 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1815 req->flags |= REQ_F_CUR_POS;
1816 kiocb->ki_pos = req->file->f_pos;
1817 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001818 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1819 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1820
1821 ioprio = READ_ONCE(sqe->ioprio);
1822 if (ioprio) {
1823 ret = ioprio_check_cap(ioprio);
1824 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001825 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001826
1827 kiocb->ki_ioprio = ioprio;
1828 } else
1829 kiocb->ki_ioprio = get_current_ioprio();
1830
1831 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1832 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001833 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001834
1835 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001836 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1837 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001838 req->flags |= REQ_F_NOWAIT;
1839
1840 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001841 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001842
Jens Axboedef596e2019-01-09 08:59:42 -07001843 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001844 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1845 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001846 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001847
Jens Axboedef596e2019-01-09 08:59:42 -07001848 kiocb->ki_flags |= IOCB_HIPRI;
1849 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001850 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001851 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001852 if (kiocb->ki_flags & IOCB_HIPRI)
1853 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001854 kiocb->ki_complete = io_complete_rw;
1855 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001856
Jens Axboe3529d8c2019-12-19 18:24:38 -07001857 req->rw.addr = READ_ONCE(sqe->addr);
1858 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001859 /* we own ->private, reuse it for the buffer index */
1860 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001861 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001862 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001863}
1864
1865static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1866{
1867 switch (ret) {
1868 case -EIOCBQUEUED:
1869 break;
1870 case -ERESTARTSYS:
1871 case -ERESTARTNOINTR:
1872 case -ERESTARTNOHAND:
1873 case -ERESTART_RESTARTBLOCK:
1874 /*
1875 * We can't just restart the syscall, since previously
1876 * submitted sqes may already be in progress. Just fail this
1877 * IO with EINTR.
1878 */
1879 ret = -EINTR;
1880 /* fall through */
1881 default:
1882 kiocb->ki_complete(kiocb, ret, 0);
1883 }
1884}
1885
Jens Axboeba816ad2019-09-28 11:36:45 -06001886static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1887 bool in_async)
1888{
Jens Axboeba042912019-12-25 16:33:42 -07001889 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1890
1891 if (req->flags & REQ_F_CUR_POS)
1892 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001893 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001894 *nxt = __io_complete_rw(kiocb, ret);
1895 else
1896 io_rw_done(kiocb, ret);
1897}
1898
Jens Axboe9adbd452019-12-20 08:45:55 -07001899static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001900 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001901{
Jens Axboe9adbd452019-12-20 08:45:55 -07001902 struct io_ring_ctx *ctx = req->ctx;
1903 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001904 struct io_mapped_ubuf *imu;
1905 unsigned index, buf_index;
1906 size_t offset;
1907 u64 buf_addr;
1908
1909 /* attempt to use fixed buffers without having provided iovecs */
1910 if (unlikely(!ctx->user_bufs))
1911 return -EFAULT;
1912
Jens Axboe9adbd452019-12-20 08:45:55 -07001913 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001914 if (unlikely(buf_index >= ctx->nr_user_bufs))
1915 return -EFAULT;
1916
1917 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1918 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001919 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001920
1921 /* overflow */
1922 if (buf_addr + len < buf_addr)
1923 return -EFAULT;
1924 /* not inside the mapped region */
1925 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1926 return -EFAULT;
1927
1928 /*
1929 * May not be a start of buffer, set size appropriately
1930 * and advance us to the beginning.
1931 */
1932 offset = buf_addr - imu->ubuf;
1933 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001934
1935 if (offset) {
1936 /*
1937 * Don't use iov_iter_advance() here, as it's really slow for
1938 * using the latter parts of a big fixed buffer - it iterates
1939 * over each segment manually. We can cheat a bit here, because
1940 * we know that:
1941 *
1942 * 1) it's a BVEC iter, we set it up
1943 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1944 * first and last bvec
1945 *
1946 * So just find our index, and adjust the iterator afterwards.
1947 * If the offset is within the first bvec (or the whole first
1948 * bvec, just use iov_iter_advance(). This makes it easier
1949 * since we can just skip the first segment, which may not
1950 * be PAGE_SIZE aligned.
1951 */
1952 const struct bio_vec *bvec = imu->bvec;
1953
1954 if (offset <= bvec->bv_len) {
1955 iov_iter_advance(iter, offset);
1956 } else {
1957 unsigned long seg_skip;
1958
1959 /* skip first vec */
1960 offset -= bvec->bv_len;
1961 seg_skip = 1 + (offset >> PAGE_SHIFT);
1962
1963 iter->bvec = bvec + seg_skip;
1964 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001965 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001966 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001967 }
1968 }
1969
Jens Axboe5e559562019-11-13 16:12:46 -07001970 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001971}
1972
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001973static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1974 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001975{
Jens Axboe9adbd452019-12-20 08:45:55 -07001976 void __user *buf = u64_to_user_ptr(req->rw.addr);
1977 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001978 u8 opcode;
1979
Jens Axboed625c6e2019-12-17 19:53:05 -07001980 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001981 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001982 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001983 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001984 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001985
Jens Axboe9adbd452019-12-20 08:45:55 -07001986 /* buffer index only valid with fixed read/write */
1987 if (req->rw.kiocb.private)
1988 return -EINVAL;
1989
Jens Axboe3a6820f2019-12-22 15:19:35 -07001990 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1991 ssize_t ret;
1992 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1993 *iovec = NULL;
1994 return ret;
1995 }
1996
Jens Axboef67676d2019-12-02 11:03:47 -07001997 if (req->io) {
1998 struct io_async_rw *iorw = &req->io->rw;
1999
2000 *iovec = iorw->iov;
2001 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2002 if (iorw->iov == iorw->fast_iov)
2003 *iovec = NULL;
2004 return iorw->size;
2005 }
2006
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002007 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002008 return -EFAULT;
2009
2010#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002011 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002012 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2013 iovec, iter);
2014#endif
2015
2016 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2017}
2018
Jens Axboe32960612019-09-23 11:05:34 -06002019/*
2020 * For files that don't have ->read_iter() and ->write_iter(), handle them
2021 * by looping over ->read() or ->write() manually.
2022 */
2023static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2024 struct iov_iter *iter)
2025{
2026 ssize_t ret = 0;
2027
2028 /*
2029 * Don't support polled IO through this interface, and we can't
2030 * support non-blocking either. For the latter, this just causes
2031 * the kiocb to be handled from an async context.
2032 */
2033 if (kiocb->ki_flags & IOCB_HIPRI)
2034 return -EOPNOTSUPP;
2035 if (kiocb->ki_flags & IOCB_NOWAIT)
2036 return -EAGAIN;
2037
2038 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002039 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002040 ssize_t nr;
2041
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002042 if (!iov_iter_is_bvec(iter)) {
2043 iovec = iov_iter_iovec(iter);
2044 } else {
2045 /* fixed buffers import bvec */
2046 iovec.iov_base = kmap(iter->bvec->bv_page)
2047 + iter->iov_offset;
2048 iovec.iov_len = min(iter->count,
2049 iter->bvec->bv_len - iter->iov_offset);
2050 }
2051
Jens Axboe32960612019-09-23 11:05:34 -06002052 if (rw == READ) {
2053 nr = file->f_op->read(file, iovec.iov_base,
2054 iovec.iov_len, &kiocb->ki_pos);
2055 } else {
2056 nr = file->f_op->write(file, iovec.iov_base,
2057 iovec.iov_len, &kiocb->ki_pos);
2058 }
2059
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002060 if (iov_iter_is_bvec(iter))
2061 kunmap(iter->bvec->bv_page);
2062
Jens Axboe32960612019-09-23 11:05:34 -06002063 if (nr < 0) {
2064 if (!ret)
2065 ret = nr;
2066 break;
2067 }
2068 ret += nr;
2069 if (nr != iovec.iov_len)
2070 break;
2071 iov_iter_advance(iter, nr);
2072 }
2073
2074 return ret;
2075}
2076
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002077static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002078 struct iovec *iovec, struct iovec *fast_iov,
2079 struct iov_iter *iter)
2080{
2081 req->io->rw.nr_segs = iter->nr_segs;
2082 req->io->rw.size = io_size;
2083 req->io->rw.iov = iovec;
2084 if (!req->io->rw.iov) {
2085 req->io->rw.iov = req->io->rw.fast_iov;
2086 memcpy(req->io->rw.iov, fast_iov,
2087 sizeof(struct iovec) * iter->nr_segs);
2088 }
2089}
2090
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002091static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002092{
Jens Axboed3656342019-12-18 09:50:26 -07002093 if (!io_op_defs[req->opcode].async_ctx)
2094 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002095 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002096 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002097}
2098
2099static void io_rw_async(struct io_wq_work **workptr)
2100{
2101 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2102 struct iovec *iov = NULL;
2103
2104 if (req->io->rw.iov != req->io->rw.fast_iov)
2105 iov = req->io->rw.iov;
2106 io_wq_submit_work(workptr);
2107 kfree(iov);
2108}
2109
2110static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2111 struct iovec *iovec, struct iovec *fast_iov,
2112 struct iov_iter *iter)
2113{
Jens Axboe74566df2020-01-13 19:23:24 -07002114 if (req->opcode == IORING_OP_READ_FIXED ||
2115 req->opcode == IORING_OP_WRITE_FIXED)
2116 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002117 if (!req->io && io_alloc_async_ctx(req))
2118 return -ENOMEM;
2119
2120 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2121 req->work.func = io_rw_async;
2122 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002123}
2124
Jens Axboe3529d8c2019-12-19 18:24:38 -07002125static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2126 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002127{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002128 struct io_async_ctx *io;
2129 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002130 ssize_t ret;
2131
Jens Axboe3529d8c2019-12-19 18:24:38 -07002132 ret = io_prep_rw(req, sqe, force_nonblock);
2133 if (ret)
2134 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002135
Jens Axboe3529d8c2019-12-19 18:24:38 -07002136 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2137 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002138
Jens Axboe3529d8c2019-12-19 18:24:38 -07002139 if (!req->io)
2140 return 0;
2141
2142 io = req->io;
2143 io->rw.iov = io->rw.fast_iov;
2144 req->io = NULL;
2145 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2146 req->io = io;
2147 if (ret < 0)
2148 return ret;
2149
2150 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2151 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002152}
2153
Pavel Begunkov267bc902019-11-07 01:41:08 +03002154static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002155 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002156{
2157 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002158 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002159 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002160 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002161 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002162
Jens Axboe3529d8c2019-12-19 18:24:38 -07002163 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002164 if (ret < 0)
2165 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002166
Jens Axboefd6c2e42019-12-18 12:19:41 -07002167 /* Ensure we clear previously set non-block flag */
2168 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002169 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002170
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002171 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002172 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002173 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002174 req->result = io_size;
2175
2176 /*
2177 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2178 * we know to async punt it even if it was opened O_NONBLOCK
2179 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002180 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002181 req->flags |= REQ_F_MUST_PUNT;
2182 goto copy_iov;
2183 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002184
Jens Axboe31b51512019-01-18 22:56:34 -07002185 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002186 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002187 if (!ret) {
2188 ssize_t ret2;
2189
Jens Axboe9adbd452019-12-20 08:45:55 -07002190 if (req->file->f_op->read_iter)
2191 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002192 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002193 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002194
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002195 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002196 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002197 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002198 } else {
2199copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002200 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002201 inline_vecs, &iter);
2202 if (ret)
2203 goto out_free;
2204 return -EAGAIN;
2205 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002206 }
Jens Axboef67676d2019-12-02 11:03:47 -07002207out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002208 if (!io_wq_current_is_worker())
2209 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002210 return ret;
2211}
2212
Jens Axboe3529d8c2019-12-19 18:24:38 -07002213static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2214 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002215{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002216 struct io_async_ctx *io;
2217 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002218 ssize_t ret;
2219
Jens Axboe3529d8c2019-12-19 18:24:38 -07002220 ret = io_prep_rw(req, sqe, force_nonblock);
2221 if (ret)
2222 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002223
Jens Axboe3529d8c2019-12-19 18:24:38 -07002224 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2225 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002226
Jens Axboe3529d8c2019-12-19 18:24:38 -07002227 if (!req->io)
2228 return 0;
2229
2230 io = req->io;
2231 io->rw.iov = io->rw.fast_iov;
2232 req->io = NULL;
2233 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2234 req->io = io;
2235 if (ret < 0)
2236 return ret;
2237
2238 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2239 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002240}
2241
Pavel Begunkov267bc902019-11-07 01:41:08 +03002242static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002243 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002244{
2245 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002246 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002247 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002248 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002249 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002250
Jens Axboe3529d8c2019-12-19 18:24:38 -07002251 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002252 if (ret < 0)
2253 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002254
Jens Axboefd6c2e42019-12-18 12:19:41 -07002255 /* Ensure we clear previously set non-block flag */
2256 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002257 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002258
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002259 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002260 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002261 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002262 req->result = io_size;
2263
2264 /*
2265 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2266 * we know to async punt it even if it was opened O_NONBLOCK
2267 */
2268 if (force_nonblock && !io_file_supports_async(req->file)) {
2269 req->flags |= REQ_F_MUST_PUNT;
2270 goto copy_iov;
2271 }
2272
Jens Axboe10d59342019-12-09 20:16:22 -07002273 /* file path doesn't support NOWAIT for non-direct_IO */
2274 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2275 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002276 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002277
Jens Axboe31b51512019-01-18 22:56:34 -07002278 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002279 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002280 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002281 ssize_t ret2;
2282
Jens Axboe2b188cc2019-01-07 10:46:33 -07002283 /*
2284 * Open-code file_start_write here to grab freeze protection,
2285 * which will be released by another thread in
2286 * io_complete_rw(). Fool lockdep by telling it the lock got
2287 * released so that it doesn't complain about the held lock when
2288 * we return to userspace.
2289 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002290 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002291 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002292 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002293 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002294 SB_FREEZE_WRITE);
2295 }
2296 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002297
Jens Axboe9adbd452019-12-20 08:45:55 -07002298 if (req->file->f_op->write_iter)
2299 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002300 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002301 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002302 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002303 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002304 } else {
2305copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002306 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002307 inline_vecs, &iter);
2308 if (ret)
2309 goto out_free;
2310 return -EAGAIN;
2311 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002312 }
Jens Axboe31b51512019-01-18 22:56:34 -07002313out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002314 if (!io_wq_current_is_worker())
2315 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002316 return ret;
2317}
2318
2319/*
2320 * IORING_OP_NOP just posts a completion event, nothing else.
2321 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002322static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002323{
2324 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002325
Jens Axboedef596e2019-01-09 08:59:42 -07002326 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2327 return -EINVAL;
2328
Jens Axboe78e19bb2019-11-06 15:21:34 -07002329 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002330 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002331 return 0;
2332}
2333
Jens Axboe3529d8c2019-12-19 18:24:38 -07002334static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002335{
Jens Axboe6b063142019-01-10 22:13:58 -07002336 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002337
Jens Axboe09bb8392019-03-13 12:39:28 -06002338 if (!req->file)
2339 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002340
Jens Axboe6b063142019-01-10 22:13:58 -07002341 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002342 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002343 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002344 return -EINVAL;
2345
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002346 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2347 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2348 return -EINVAL;
2349
2350 req->sync.off = READ_ONCE(sqe->off);
2351 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002352 return 0;
2353}
2354
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002355static bool io_req_cancelled(struct io_kiocb *req)
2356{
2357 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2358 req_set_fail_links(req);
2359 io_cqring_add_event(req, -ECANCELED);
2360 io_put_req(req);
2361 return true;
2362 }
2363
2364 return false;
2365}
2366
Jens Axboe78912932020-01-14 22:09:06 -07002367static void io_link_work_cb(struct io_wq_work **workptr)
2368{
2369 struct io_wq_work *work = *workptr;
2370 struct io_kiocb *link = work->data;
2371
2372 io_queue_linked_timeout(link);
2373 work->func = io_wq_submit_work;
2374}
2375
2376static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2377{
2378 struct io_kiocb *link;
2379
2380 io_prep_async_work(nxt, &link);
2381 *workptr = &nxt->work;
2382 if (link) {
2383 nxt->work.flags |= IO_WQ_WORK_CB;
2384 nxt->work.func = io_link_work_cb;
2385 nxt->work.data = link;
2386 }
2387}
2388
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002389static void io_fsync_finish(struct io_wq_work **workptr)
2390{
2391 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2392 loff_t end = req->sync.off + req->sync.len;
2393 struct io_kiocb *nxt = NULL;
2394 int ret;
2395
2396 if (io_req_cancelled(req))
2397 return;
2398
Jens Axboe9adbd452019-12-20 08:45:55 -07002399 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002400 end > 0 ? end : LLONG_MAX,
2401 req->sync.flags & IORING_FSYNC_DATASYNC);
2402 if (ret < 0)
2403 req_set_fail_links(req);
2404 io_cqring_add_event(req, ret);
2405 io_put_req_find_next(req, &nxt);
2406 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002407 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002408}
2409
Jens Axboefc4df992019-12-10 14:38:45 -07002410static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2411 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002412{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002413 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002414
2415 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002416 if (force_nonblock) {
2417 io_put_req(req);
2418 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002419 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002420 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002421
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002422 work = old_work = &req->work;
2423 io_fsync_finish(&work);
2424 if (work && work != old_work)
2425 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002426 return 0;
2427}
2428
Jens Axboed63d1b52019-12-10 10:38:56 -07002429static void io_fallocate_finish(struct io_wq_work **workptr)
2430{
2431 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2432 struct io_kiocb *nxt = NULL;
2433 int ret;
2434
2435 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2436 req->sync.len);
2437 if (ret < 0)
2438 req_set_fail_links(req);
2439 io_cqring_add_event(req, ret);
2440 io_put_req_find_next(req, &nxt);
2441 if (nxt)
2442 io_wq_assign_next(workptr, nxt);
2443}
2444
2445static int io_fallocate_prep(struct io_kiocb *req,
2446 const struct io_uring_sqe *sqe)
2447{
2448 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2449 return -EINVAL;
2450
2451 req->sync.off = READ_ONCE(sqe->off);
2452 req->sync.len = READ_ONCE(sqe->addr);
2453 req->sync.mode = READ_ONCE(sqe->len);
2454 return 0;
2455}
2456
2457static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2458 bool force_nonblock)
2459{
2460 struct io_wq_work *work, *old_work;
2461
2462 /* fallocate always requiring blocking context */
2463 if (force_nonblock) {
2464 io_put_req(req);
2465 req->work.func = io_fallocate_finish;
2466 return -EAGAIN;
2467 }
2468
2469 work = old_work = &req->work;
2470 io_fallocate_finish(&work);
2471 if (work && work != old_work)
2472 *nxt = container_of(work, struct io_kiocb, work);
2473
2474 return 0;
2475}
2476
Jens Axboe15b71ab2019-12-11 11:20:36 -07002477static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2478{
Jens Axboef8748882020-01-08 17:47:02 -07002479 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002480 int ret;
2481
2482 if (sqe->ioprio || sqe->buf_index)
2483 return -EINVAL;
2484
2485 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002486 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002487 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002488 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002489
Jens Axboef8748882020-01-08 17:47:02 -07002490 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002491 if (IS_ERR(req->open.filename)) {
2492 ret = PTR_ERR(req->open.filename);
2493 req->open.filename = NULL;
2494 return ret;
2495 }
2496
2497 return 0;
2498}
2499
Jens Axboecebdb982020-01-08 17:59:24 -07002500static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2501{
2502 struct open_how __user *how;
2503 const char __user *fname;
2504 size_t len;
2505 int ret;
2506
2507 if (sqe->ioprio || sqe->buf_index)
2508 return -EINVAL;
2509
2510 req->open.dfd = READ_ONCE(sqe->fd);
2511 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2512 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2513 len = READ_ONCE(sqe->len);
2514
2515 if (len < OPEN_HOW_SIZE_VER0)
2516 return -EINVAL;
2517
2518 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2519 len);
2520 if (ret)
2521 return ret;
2522
2523 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2524 req->open.how.flags |= O_LARGEFILE;
2525
2526 req->open.filename = getname(fname);
2527 if (IS_ERR(req->open.filename)) {
2528 ret = PTR_ERR(req->open.filename);
2529 req->open.filename = NULL;
2530 return ret;
2531 }
2532
2533 return 0;
2534}
2535
2536static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2537 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002538{
2539 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002540 struct file *file;
2541 int ret;
2542
2543 if (force_nonblock) {
2544 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2545 return -EAGAIN;
2546 }
2547
Jens Axboecebdb982020-01-08 17:59:24 -07002548 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002549 if (ret)
2550 goto err;
2551
Jens Axboecebdb982020-01-08 17:59:24 -07002552 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002553 if (ret < 0)
2554 goto err;
2555
2556 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2557 if (IS_ERR(file)) {
2558 put_unused_fd(ret);
2559 ret = PTR_ERR(file);
2560 } else {
2561 fsnotify_open(file);
2562 fd_install(ret, file);
2563 }
2564err:
2565 putname(req->open.filename);
2566 if (ret < 0)
2567 req_set_fail_links(req);
2568 io_cqring_add_event(req, ret);
2569 io_put_req_find_next(req, nxt);
2570 return 0;
2571}
2572
Jens Axboecebdb982020-01-08 17:59:24 -07002573static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2574 bool force_nonblock)
2575{
2576 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2577 return io_openat2(req, nxt, force_nonblock);
2578}
2579
Jens Axboec1ca7572019-12-25 22:18:28 -07002580static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2581{
2582#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2583 if (sqe->ioprio || sqe->buf_index || sqe->off)
2584 return -EINVAL;
2585
2586 req->madvise.addr = READ_ONCE(sqe->addr);
2587 req->madvise.len = READ_ONCE(sqe->len);
2588 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2589 return 0;
2590#else
2591 return -EOPNOTSUPP;
2592#endif
2593}
2594
2595static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2596 bool force_nonblock)
2597{
2598#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2599 struct io_madvise *ma = &req->madvise;
2600 int ret;
2601
2602 if (force_nonblock)
2603 return -EAGAIN;
2604
2605 ret = do_madvise(ma->addr, ma->len, ma->advice);
2606 if (ret < 0)
2607 req_set_fail_links(req);
2608 io_cqring_add_event(req, ret);
2609 io_put_req_find_next(req, nxt);
2610 return 0;
2611#else
2612 return -EOPNOTSUPP;
2613#endif
2614}
2615
Jens Axboe4840e412019-12-25 22:03:45 -07002616static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2617{
2618 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2619 return -EINVAL;
2620
2621 req->fadvise.offset = READ_ONCE(sqe->off);
2622 req->fadvise.len = READ_ONCE(sqe->len);
2623 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2624 return 0;
2625}
2626
2627static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2628 bool force_nonblock)
2629{
2630 struct io_fadvise *fa = &req->fadvise;
2631 int ret;
2632
2633 /* DONTNEED may block, others _should_ not */
2634 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2635 return -EAGAIN;
2636
2637 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2638 if (ret < 0)
2639 req_set_fail_links(req);
2640 io_cqring_add_event(req, ret);
2641 io_put_req_find_next(req, nxt);
2642 return 0;
2643}
2644
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002645static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2646{
Jens Axboef8748882020-01-08 17:47:02 -07002647 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002648 unsigned lookup_flags;
2649 int ret;
2650
2651 if (sqe->ioprio || sqe->buf_index)
2652 return -EINVAL;
2653
2654 req->open.dfd = READ_ONCE(sqe->fd);
2655 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002656 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002657 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002658 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002659
Jens Axboec12cedf2020-01-08 17:41:21 -07002660 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002661 return -EINVAL;
2662
Jens Axboef8748882020-01-08 17:47:02 -07002663 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002664 if (IS_ERR(req->open.filename)) {
2665 ret = PTR_ERR(req->open.filename);
2666 req->open.filename = NULL;
2667 return ret;
2668 }
2669
2670 return 0;
2671}
2672
2673static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2674 bool force_nonblock)
2675{
2676 struct io_open *ctx = &req->open;
2677 unsigned lookup_flags;
2678 struct path path;
2679 struct kstat stat;
2680 int ret;
2681
2682 if (force_nonblock)
2683 return -EAGAIN;
2684
Jens Axboec12cedf2020-01-08 17:41:21 -07002685 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002686 return -EINVAL;
2687
2688retry:
2689 /* filename_lookup() drops it, keep a reference */
2690 ctx->filename->refcnt++;
2691
2692 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2693 NULL);
2694 if (ret)
2695 goto err;
2696
Jens Axboec12cedf2020-01-08 17:41:21 -07002697 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002698 path_put(&path);
2699 if (retry_estale(ret, lookup_flags)) {
2700 lookup_flags |= LOOKUP_REVAL;
2701 goto retry;
2702 }
2703 if (!ret)
2704 ret = cp_statx(&stat, ctx->buffer);
2705err:
2706 putname(ctx->filename);
2707 if (ret < 0)
2708 req_set_fail_links(req);
2709 io_cqring_add_event(req, ret);
2710 io_put_req_find_next(req, nxt);
2711 return 0;
2712}
2713
Jens Axboeb5dba592019-12-11 14:02:38 -07002714static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2715{
2716 /*
2717 * If we queue this for async, it must not be cancellable. That would
2718 * leave the 'file' in an undeterminate state.
2719 */
2720 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2721
2722 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2723 sqe->rw_flags || sqe->buf_index)
2724 return -EINVAL;
2725 if (sqe->flags & IOSQE_FIXED_FILE)
2726 return -EINVAL;
2727
2728 req->close.fd = READ_ONCE(sqe->fd);
2729 if (req->file->f_op == &io_uring_fops ||
2730 req->close.fd == req->ring_fd)
2731 return -EBADF;
2732
2733 return 0;
2734}
2735
2736static void io_close_finish(struct io_wq_work **workptr)
2737{
2738 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2739 struct io_kiocb *nxt = NULL;
2740
2741 /* Invoked with files, we need to do the close */
2742 if (req->work.files) {
2743 int ret;
2744
2745 ret = filp_close(req->close.put_file, req->work.files);
2746 if (ret < 0) {
2747 req_set_fail_links(req);
2748 }
2749 io_cqring_add_event(req, ret);
2750 }
2751
2752 fput(req->close.put_file);
2753
2754 /* we bypassed the re-issue, drop the submission reference */
2755 io_put_req(req);
2756 io_put_req_find_next(req, &nxt);
2757 if (nxt)
2758 io_wq_assign_next(workptr, nxt);
2759}
2760
2761static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2762 bool force_nonblock)
2763{
2764 int ret;
2765
2766 req->close.put_file = NULL;
2767 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2768 if (ret < 0)
2769 return ret;
2770
2771 /* if the file has a flush method, be safe and punt to async */
2772 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2773 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2774 goto eagain;
2775 }
2776
2777 /*
2778 * No ->flush(), safely close from here and just punt the
2779 * fput() to async context.
2780 */
2781 ret = filp_close(req->close.put_file, current->files);
2782
2783 if (ret < 0)
2784 req_set_fail_links(req);
2785 io_cqring_add_event(req, ret);
2786
2787 if (io_wq_current_is_worker()) {
2788 struct io_wq_work *old_work, *work;
2789
2790 old_work = work = &req->work;
2791 io_close_finish(&work);
2792 if (work && work != old_work)
2793 *nxt = container_of(work, struct io_kiocb, work);
2794 return 0;
2795 }
2796
2797eagain:
2798 req->work.func = io_close_finish;
2799 return -EAGAIN;
2800}
2801
Jens Axboe3529d8c2019-12-19 18:24:38 -07002802static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002803{
2804 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002805
2806 if (!req->file)
2807 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002808
2809 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2810 return -EINVAL;
2811 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2812 return -EINVAL;
2813
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002814 req->sync.off = READ_ONCE(sqe->off);
2815 req->sync.len = READ_ONCE(sqe->len);
2816 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002817 return 0;
2818}
2819
2820static void io_sync_file_range_finish(struct io_wq_work **workptr)
2821{
2822 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2823 struct io_kiocb *nxt = NULL;
2824 int ret;
2825
2826 if (io_req_cancelled(req))
2827 return;
2828
Jens Axboe9adbd452019-12-20 08:45:55 -07002829 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002830 req->sync.flags);
2831 if (ret < 0)
2832 req_set_fail_links(req);
2833 io_cqring_add_event(req, ret);
2834 io_put_req_find_next(req, &nxt);
2835 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002836 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002837}
2838
Jens Axboefc4df992019-12-10 14:38:45 -07002839static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002840 bool force_nonblock)
2841{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002842 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002843
2844 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002845 if (force_nonblock) {
2846 io_put_req(req);
2847 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002848 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002849 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002850
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002851 work = old_work = &req->work;
2852 io_sync_file_range_finish(&work);
2853 if (work && work != old_work)
2854 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002855 return 0;
2856}
2857
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002858#if defined(CONFIG_NET)
2859static void io_sendrecv_async(struct io_wq_work **workptr)
2860{
2861 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2862 struct iovec *iov = NULL;
2863
2864 if (req->io->rw.iov != req->io->rw.fast_iov)
2865 iov = req->io->msg.iov;
2866 io_wq_submit_work(workptr);
2867 kfree(iov);
2868}
2869#endif
2870
Jens Axboe3529d8c2019-12-19 18:24:38 -07002871static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002872{
Jens Axboe03b12302019-12-02 18:50:25 -07002873#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002874 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002875 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002876
Jens Axboee47293f2019-12-20 08:58:21 -07002877 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2878 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002879 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002880
Jens Axboefddafac2020-01-04 20:19:44 -07002881 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002882 return 0;
2883
Jens Axboed9688562019-12-09 19:35:20 -07002884 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002885 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002886 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002887#else
Jens Axboee47293f2019-12-20 08:58:21 -07002888 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002889#endif
2890}
2891
Jens Axboefc4df992019-12-10 14:38:45 -07002892static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2893 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002894{
2895#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002896 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002897 struct socket *sock;
2898 int ret;
2899
2900 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2901 return -EINVAL;
2902
2903 sock = sock_from_file(req->file, &ret);
2904 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002905 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002906 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002907 unsigned flags;
2908
Jens Axboe03b12302019-12-02 18:50:25 -07002909 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002910 kmsg = &req->io->msg;
2911 kmsg->msg.msg_name = &addr;
2912 /* if iov is set, it's allocated already */
2913 if (!kmsg->iov)
2914 kmsg->iov = kmsg->fast_iov;
2915 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002916 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002917 struct io_sr_msg *sr = &req->sr_msg;
2918
Jens Axboe0b416c32019-12-15 10:57:46 -07002919 kmsg = &io.msg;
2920 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002921
2922 io.msg.iov = io.msg.fast_iov;
2923 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2924 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002925 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002926 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002927 }
2928
Jens Axboee47293f2019-12-20 08:58:21 -07002929 flags = req->sr_msg.msg_flags;
2930 if (flags & MSG_DONTWAIT)
2931 req->flags |= REQ_F_NOWAIT;
2932 else if (force_nonblock)
2933 flags |= MSG_DONTWAIT;
2934
Jens Axboe0b416c32019-12-15 10:57:46 -07002935 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002936 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002937 if (req->io)
2938 return -EAGAIN;
2939 if (io_alloc_async_ctx(req))
2940 return -ENOMEM;
2941 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2942 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002943 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002944 }
2945 if (ret == -ERESTARTSYS)
2946 ret = -EINTR;
2947 }
2948
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002949 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002950 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002951 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002952 if (ret < 0)
2953 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002954 io_put_req_find_next(req, nxt);
2955 return 0;
2956#else
2957 return -EOPNOTSUPP;
2958#endif
2959}
2960
Jens Axboefddafac2020-01-04 20:19:44 -07002961static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2962 bool force_nonblock)
2963{
2964#if defined(CONFIG_NET)
2965 struct socket *sock;
2966 int ret;
2967
2968 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2969 return -EINVAL;
2970
2971 sock = sock_from_file(req->file, &ret);
2972 if (sock) {
2973 struct io_sr_msg *sr = &req->sr_msg;
2974 struct msghdr msg;
2975 struct iovec iov;
2976 unsigned flags;
2977
2978 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
2979 &msg.msg_iter);
2980 if (ret)
2981 return ret;
2982
2983 msg.msg_name = NULL;
2984 msg.msg_control = NULL;
2985 msg.msg_controllen = 0;
2986 msg.msg_namelen = 0;
2987
2988 flags = req->sr_msg.msg_flags;
2989 if (flags & MSG_DONTWAIT)
2990 req->flags |= REQ_F_NOWAIT;
2991 else if (force_nonblock)
2992 flags |= MSG_DONTWAIT;
2993
2994 ret = __sys_sendmsg_sock(sock, &msg, flags);
2995 if (force_nonblock && ret == -EAGAIN)
2996 return -EAGAIN;
2997 if (ret == -ERESTARTSYS)
2998 ret = -EINTR;
2999 }
3000
3001 io_cqring_add_event(req, ret);
3002 if (ret < 0)
3003 req_set_fail_links(req);
3004 io_put_req_find_next(req, nxt);
3005 return 0;
3006#else
3007 return -EOPNOTSUPP;
3008#endif
3009}
3010
Jens Axboe3529d8c2019-12-19 18:24:38 -07003011static int io_recvmsg_prep(struct io_kiocb *req,
3012 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003013{
3014#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003015 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003016 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07003017
Jens Axboe3529d8c2019-12-19 18:24:38 -07003018 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3019 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
3020
Jens Axboefddafac2020-01-04 20:19:44 -07003021 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003022 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003023
Jens Axboed9688562019-12-09 19:35:20 -07003024 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003025 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003026 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003027#else
Jens Axboee47293f2019-12-20 08:58:21 -07003028 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003029#endif
3030}
3031
Jens Axboefc4df992019-12-10 14:38:45 -07003032static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3033 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003034{
3035#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003036 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003037 struct socket *sock;
3038 int ret;
3039
3040 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3041 return -EINVAL;
3042
3043 sock = sock_from_file(req->file, &ret);
3044 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003045 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003046 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003047 unsigned flags;
3048
Jens Axboe03b12302019-12-02 18:50:25 -07003049 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003050 kmsg = &req->io->msg;
3051 kmsg->msg.msg_name = &addr;
3052 /* if iov is set, it's allocated already */
3053 if (!kmsg->iov)
3054 kmsg->iov = kmsg->fast_iov;
3055 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003056 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003057 struct io_sr_msg *sr = &req->sr_msg;
3058
Jens Axboe0b416c32019-12-15 10:57:46 -07003059 kmsg = &io.msg;
3060 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003061
3062 io.msg.iov = io.msg.fast_iov;
3063 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3064 sr->msg_flags, &io.msg.uaddr,
3065 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003066 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003067 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003068 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003069
Jens Axboee47293f2019-12-20 08:58:21 -07003070 flags = req->sr_msg.msg_flags;
3071 if (flags & MSG_DONTWAIT)
3072 req->flags |= REQ_F_NOWAIT;
3073 else if (force_nonblock)
3074 flags |= MSG_DONTWAIT;
3075
3076 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3077 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003078 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003079 if (req->io)
3080 return -EAGAIN;
3081 if (io_alloc_async_ctx(req))
3082 return -ENOMEM;
3083 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3084 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003085 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003086 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003087 if (ret == -ERESTARTSYS)
3088 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003089 }
3090
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003091 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003092 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003093 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003094 if (ret < 0)
3095 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003096 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003097 return 0;
3098#else
3099 return -EOPNOTSUPP;
3100#endif
3101}
3102
Jens Axboefddafac2020-01-04 20:19:44 -07003103static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3104 bool force_nonblock)
3105{
3106#if defined(CONFIG_NET)
3107 struct socket *sock;
3108 int ret;
3109
3110 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3111 return -EINVAL;
3112
3113 sock = sock_from_file(req->file, &ret);
3114 if (sock) {
3115 struct io_sr_msg *sr = &req->sr_msg;
3116 struct msghdr msg;
3117 struct iovec iov;
3118 unsigned flags;
3119
3120 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3121 &msg.msg_iter);
3122 if (ret)
3123 return ret;
3124
3125 msg.msg_name = NULL;
3126 msg.msg_control = NULL;
3127 msg.msg_controllen = 0;
3128 msg.msg_namelen = 0;
3129 msg.msg_iocb = NULL;
3130 msg.msg_flags = 0;
3131
3132 flags = req->sr_msg.msg_flags;
3133 if (flags & MSG_DONTWAIT)
3134 req->flags |= REQ_F_NOWAIT;
3135 else if (force_nonblock)
3136 flags |= MSG_DONTWAIT;
3137
3138 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3139 if (force_nonblock && ret == -EAGAIN)
3140 return -EAGAIN;
3141 if (ret == -ERESTARTSYS)
3142 ret = -EINTR;
3143 }
3144
3145 io_cqring_add_event(req, ret);
3146 if (ret < 0)
3147 req_set_fail_links(req);
3148 io_put_req_find_next(req, nxt);
3149 return 0;
3150#else
3151 return -EOPNOTSUPP;
3152#endif
3153}
3154
3155
Jens Axboe3529d8c2019-12-19 18:24:38 -07003156static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003157{
3158#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003159 struct io_accept *accept = &req->accept;
3160
Jens Axboe17f2fe32019-10-17 14:42:58 -06003161 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3162 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003163 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003164 return -EINVAL;
3165
Jens Axboed55e5f52019-12-11 16:12:15 -07003166 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3167 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003168 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003169 return 0;
3170#else
3171 return -EOPNOTSUPP;
3172#endif
3173}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003174
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003175#if defined(CONFIG_NET)
3176static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3177 bool force_nonblock)
3178{
3179 struct io_accept *accept = &req->accept;
3180 unsigned file_flags;
3181 int ret;
3182
3183 file_flags = force_nonblock ? O_NONBLOCK : 0;
3184 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3185 accept->addr_len, accept->flags);
3186 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003187 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003188 if (ret == -ERESTARTSYS)
3189 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003190 if (ret < 0)
3191 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003192 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003193 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003194 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003195}
3196
3197static void io_accept_finish(struct io_wq_work **workptr)
3198{
3199 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3200 struct io_kiocb *nxt = NULL;
3201
3202 if (io_req_cancelled(req))
3203 return;
3204 __io_accept(req, &nxt, false);
3205 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003206 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003207}
3208#endif
3209
3210static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3211 bool force_nonblock)
3212{
3213#if defined(CONFIG_NET)
3214 int ret;
3215
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003216 ret = __io_accept(req, nxt, force_nonblock);
3217 if (ret == -EAGAIN && force_nonblock) {
3218 req->work.func = io_accept_finish;
3219 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3220 io_put_req(req);
3221 return -EAGAIN;
3222 }
3223 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003224#else
3225 return -EOPNOTSUPP;
3226#endif
3227}
3228
Jens Axboe3529d8c2019-12-19 18:24:38 -07003229static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003230{
3231#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003232 struct io_connect *conn = &req->connect;
3233 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003234
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003235 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3236 return -EINVAL;
3237 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3238 return -EINVAL;
3239
Jens Axboe3529d8c2019-12-19 18:24:38 -07003240 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3241 conn->addr_len = READ_ONCE(sqe->addr2);
3242
3243 if (!io)
3244 return 0;
3245
3246 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003247 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003248#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003249 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003250#endif
3251}
3252
Jens Axboefc4df992019-12-10 14:38:45 -07003253static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3254 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003255{
3256#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003257 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003258 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003259 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003260
Jens Axboef499a022019-12-02 16:28:46 -07003261 if (req->io) {
3262 io = req->io;
3263 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003264 ret = move_addr_to_kernel(req->connect.addr,
3265 req->connect.addr_len,
3266 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003267 if (ret)
3268 goto out;
3269 io = &__io;
3270 }
3271
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003272 file_flags = force_nonblock ? O_NONBLOCK : 0;
3273
3274 ret = __sys_connect_file(req->file, &io->connect.address,
3275 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003276 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003277 if (req->io)
3278 return -EAGAIN;
3279 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003280 ret = -ENOMEM;
3281 goto out;
3282 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003283 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003284 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003285 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003286 if (ret == -ERESTARTSYS)
3287 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003288out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003289 if (ret < 0)
3290 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003291 io_cqring_add_event(req, ret);
3292 io_put_req_find_next(req, nxt);
3293 return 0;
3294#else
3295 return -EOPNOTSUPP;
3296#endif
3297}
3298
Jens Axboe221c5eb2019-01-17 09:41:58 -07003299static void io_poll_remove_one(struct io_kiocb *req)
3300{
3301 struct io_poll_iocb *poll = &req->poll;
3302
3303 spin_lock(&poll->head->lock);
3304 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003305 if (!list_empty(&poll->wait.entry)) {
3306 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003307 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003308 }
3309 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003310 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003311}
3312
3313static void io_poll_remove_all(struct io_ring_ctx *ctx)
3314{
Jens Axboe78076bb2019-12-04 19:56:40 -07003315 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003316 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003317 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003318
3319 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003320 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3321 struct hlist_head *list;
3322
3323 list = &ctx->cancel_hash[i];
3324 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3325 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003326 }
3327 spin_unlock_irq(&ctx->completion_lock);
3328}
3329
Jens Axboe47f46762019-11-09 17:43:02 -07003330static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3331{
Jens Axboe78076bb2019-12-04 19:56:40 -07003332 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003333 struct io_kiocb *req;
3334
Jens Axboe78076bb2019-12-04 19:56:40 -07003335 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3336 hlist_for_each_entry(req, list, hash_node) {
3337 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003338 io_poll_remove_one(req);
3339 return 0;
3340 }
Jens Axboe47f46762019-11-09 17:43:02 -07003341 }
3342
3343 return -ENOENT;
3344}
3345
Jens Axboe3529d8c2019-12-19 18:24:38 -07003346static int io_poll_remove_prep(struct io_kiocb *req,
3347 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003348{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003349 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3350 return -EINVAL;
3351 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3352 sqe->poll_events)
3353 return -EINVAL;
3354
Jens Axboe0969e782019-12-17 18:40:57 -07003355 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003356 return 0;
3357}
3358
3359/*
3360 * Find a running poll command that matches one specified in sqe->addr,
3361 * and remove it if found.
3362 */
3363static int io_poll_remove(struct io_kiocb *req)
3364{
3365 struct io_ring_ctx *ctx = req->ctx;
3366 u64 addr;
3367 int ret;
3368
Jens Axboe0969e782019-12-17 18:40:57 -07003369 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003370 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003371 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003372 spin_unlock_irq(&ctx->completion_lock);
3373
Jens Axboe78e19bb2019-11-06 15:21:34 -07003374 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003375 if (ret < 0)
3376 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003377 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003378 return 0;
3379}
3380
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003381static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003382{
Jackie Liua197f662019-11-08 08:09:12 -07003383 struct io_ring_ctx *ctx = req->ctx;
3384
Jens Axboe8c838782019-03-12 15:48:16 -06003385 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003386 if (error)
3387 io_cqring_fill_event(req, error);
3388 else
3389 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003390 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003391}
3392
Jens Axboe561fb042019-10-24 07:25:42 -06003393static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003394{
Jens Axboe561fb042019-10-24 07:25:42 -06003395 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003396 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3397 struct io_poll_iocb *poll = &req->poll;
3398 struct poll_table_struct pt = { ._key = poll->events };
3399 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003400 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003401 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003402 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003403
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003404 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003405 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003406 ret = -ECANCELED;
3407 } else if (READ_ONCE(poll->canceled)) {
3408 ret = -ECANCELED;
3409 }
Jens Axboe561fb042019-10-24 07:25:42 -06003410
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003411 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003412 mask = vfs_poll(poll->file, &pt) & poll->events;
3413
3414 /*
3415 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3416 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3417 * synchronize with them. In the cancellation case the list_del_init
3418 * itself is not actually needed, but harmless so we keep it in to
3419 * avoid further branches in the fast path.
3420 */
3421 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003422 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003423 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003424 spin_unlock_irq(&ctx->completion_lock);
3425 return;
3426 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003427 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003428 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003429 spin_unlock_irq(&ctx->completion_lock);
3430
Jens Axboe8c838782019-03-12 15:48:16 -06003431 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003432
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003433 if (ret < 0)
3434 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003435 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003436 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003437 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003438}
3439
Jens Axboee94f1412019-12-19 12:06:02 -07003440static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3441{
Jens Axboee94f1412019-12-19 12:06:02 -07003442 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003443 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003444
Jens Axboec6ca97b302019-12-28 12:11:08 -07003445 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003446 spin_lock_irq(&ctx->completion_lock);
3447 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3448 hash_del(&req->hash_node);
3449 io_poll_complete(req, req->result, 0);
3450
Jens Axboe8237e042019-12-28 10:48:22 -07003451 if (refcount_dec_and_test(&req->refs) &&
3452 !io_req_multi_free(&rb, req)) {
3453 req->flags |= REQ_F_COMP_LOCKED;
3454 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003455 }
3456 }
3457 spin_unlock_irq(&ctx->completion_lock);
3458
3459 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003460 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003461}
3462
3463static void io_poll_flush(struct io_wq_work **workptr)
3464{
3465 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3466 struct llist_node *nodes;
3467
3468 nodes = llist_del_all(&req->ctx->poll_llist);
3469 if (nodes)
3470 __io_poll_flush(req->ctx, nodes);
3471}
3472
Jens Axboe221c5eb2019-01-17 09:41:58 -07003473static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3474 void *key)
3475{
Jens Axboee9444752019-11-26 15:02:04 -07003476 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003477 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3478 struct io_ring_ctx *ctx = req->ctx;
3479 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003480
3481 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003482 if (mask && !(mask & poll->events))
3483 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003484
Jens Axboe392edb42019-12-09 17:52:20 -07003485 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003486
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003487 /*
3488 * Run completion inline if we can. We're using trylock here because
3489 * we are violating the completion_lock -> poll wq lock ordering.
3490 * If we have a link timeout we're going to need the completion_lock
3491 * for finalizing the request, mark us as having grabbed that already.
3492 */
Jens Axboee94f1412019-12-19 12:06:02 -07003493 if (mask) {
3494 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003495
Jens Axboee94f1412019-12-19 12:06:02 -07003496 if (llist_empty(&ctx->poll_llist) &&
3497 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3498 hash_del(&req->hash_node);
3499 io_poll_complete(req, mask, 0);
3500 req->flags |= REQ_F_COMP_LOCKED;
3501 io_put_req(req);
3502 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3503
3504 io_cqring_ev_posted(ctx);
3505 req = NULL;
3506 } else {
3507 req->result = mask;
3508 req->llist_node.next = NULL;
3509 /* if the list wasn't empty, we're done */
3510 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3511 req = NULL;
3512 else
3513 req->work.func = io_poll_flush;
3514 }
Jens Axboe8c838782019-03-12 15:48:16 -06003515 }
Jens Axboee94f1412019-12-19 12:06:02 -07003516 if (req)
3517 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003518
Jens Axboe221c5eb2019-01-17 09:41:58 -07003519 return 1;
3520}
3521
3522struct io_poll_table {
3523 struct poll_table_struct pt;
3524 struct io_kiocb *req;
3525 int error;
3526};
3527
3528static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3529 struct poll_table_struct *p)
3530{
3531 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3532
3533 if (unlikely(pt->req->poll.head)) {
3534 pt->error = -EINVAL;
3535 return;
3536 }
3537
3538 pt->error = 0;
3539 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003540 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003541}
3542
Jens Axboeeac406c2019-11-14 12:09:58 -07003543static void io_poll_req_insert(struct io_kiocb *req)
3544{
3545 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003546 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003547
Jens Axboe78076bb2019-12-04 19:56:40 -07003548 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3549 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003550}
3551
Jens Axboe3529d8c2019-12-19 18:24:38 -07003552static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003553{
3554 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003555 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003556
3557 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3558 return -EINVAL;
3559 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3560 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003561 if (!poll->file)
3562 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003563
Jens Axboe221c5eb2019-01-17 09:41:58 -07003564 events = READ_ONCE(sqe->poll_events);
3565 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003566 return 0;
3567}
3568
3569static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3570{
3571 struct io_poll_iocb *poll = &req->poll;
3572 struct io_ring_ctx *ctx = req->ctx;
3573 struct io_poll_table ipt;
3574 bool cancel = false;
3575 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003576
3577 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003578 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003579
Jens Axboe221c5eb2019-01-17 09:41:58 -07003580 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003581 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003582 poll->canceled = false;
3583
3584 ipt.pt._qproc = io_poll_queue_proc;
3585 ipt.pt._key = poll->events;
3586 ipt.req = req;
3587 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3588
3589 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003590 INIT_LIST_HEAD(&poll->wait.entry);
3591 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3592 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003593
Jens Axboe36703242019-07-25 10:20:18 -06003594 INIT_LIST_HEAD(&req->list);
3595
Jens Axboe221c5eb2019-01-17 09:41:58 -07003596 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003597
3598 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003599 if (likely(poll->head)) {
3600 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003601 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003602 if (ipt.error)
3603 cancel = true;
3604 ipt.error = 0;
3605 mask = 0;
3606 }
3607 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003608 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003609 else if (cancel)
3610 WRITE_ONCE(poll->canceled, true);
3611 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003612 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003613 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003614 }
Jens Axboe8c838782019-03-12 15:48:16 -06003615 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003616 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003617 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003618 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003619 spin_unlock_irq(&ctx->completion_lock);
3620
Jens Axboe8c838782019-03-12 15:48:16 -06003621 if (mask) {
3622 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003623 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003624 }
Jens Axboe8c838782019-03-12 15:48:16 -06003625 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003626}
3627
Jens Axboe5262f562019-09-17 12:26:57 -06003628static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3629{
Jens Axboead8a48a2019-11-15 08:49:11 -07003630 struct io_timeout_data *data = container_of(timer,
3631 struct io_timeout_data, timer);
3632 struct io_kiocb *req = data->req;
3633 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003634 unsigned long flags;
3635
Jens Axboe5262f562019-09-17 12:26:57 -06003636 atomic_inc(&ctx->cq_timeouts);
3637
3638 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003639 /*
Jens Axboe11365042019-10-16 09:08:32 -06003640 * We could be racing with timeout deletion. If the list is empty,
3641 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003642 */
Jens Axboe842f9612019-10-29 12:34:10 -06003643 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003644 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003645
Jens Axboe11365042019-10-16 09:08:32 -06003646 /*
3647 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003648 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003649 * pointer will be increased, otherwise other timeout reqs may
3650 * return in advance without waiting for enough wait_nr.
3651 */
3652 prev = req;
3653 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3654 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003655 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003656 }
Jens Axboe842f9612019-10-29 12:34:10 -06003657
Jens Axboe78e19bb2019-11-06 15:21:34 -07003658 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003659 io_commit_cqring(ctx);
3660 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3661
3662 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003663 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003664 io_put_req(req);
3665 return HRTIMER_NORESTART;
3666}
3667
Jens Axboe47f46762019-11-09 17:43:02 -07003668static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3669{
3670 struct io_kiocb *req;
3671 int ret = -ENOENT;
3672
3673 list_for_each_entry(req, &ctx->timeout_list, list) {
3674 if (user_data == req->user_data) {
3675 list_del_init(&req->list);
3676 ret = 0;
3677 break;
3678 }
3679 }
3680
3681 if (ret == -ENOENT)
3682 return ret;
3683
Jens Axboe2d283902019-12-04 11:08:05 -07003684 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003685 if (ret == -1)
3686 return -EALREADY;
3687
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003688 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003689 io_cqring_fill_event(req, -ECANCELED);
3690 io_put_req(req);
3691 return 0;
3692}
3693
Jens Axboe3529d8c2019-12-19 18:24:38 -07003694static int io_timeout_remove_prep(struct io_kiocb *req,
3695 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003696{
Jens Axboeb29472e2019-12-17 18:50:29 -07003697 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3698 return -EINVAL;
3699 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3700 return -EINVAL;
3701
3702 req->timeout.addr = READ_ONCE(sqe->addr);
3703 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3704 if (req->timeout.flags)
3705 return -EINVAL;
3706
Jens Axboeb29472e2019-12-17 18:50:29 -07003707 return 0;
3708}
3709
Jens Axboe11365042019-10-16 09:08:32 -06003710/*
3711 * Remove or update an existing timeout command
3712 */
Jens Axboefc4df992019-12-10 14:38:45 -07003713static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003714{
3715 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003716 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003717
Jens Axboe11365042019-10-16 09:08:32 -06003718 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003719 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003720
Jens Axboe47f46762019-11-09 17:43:02 -07003721 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003722 io_commit_cqring(ctx);
3723 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003724 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003725 if (ret < 0)
3726 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003727 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003728 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003729}
3730
Jens Axboe3529d8c2019-12-19 18:24:38 -07003731static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003732 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003733{
Jens Axboead8a48a2019-11-15 08:49:11 -07003734 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003735 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003736
Jens Axboead8a48a2019-11-15 08:49:11 -07003737 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003738 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003739 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003740 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003741 if (sqe->off && is_timeout_link)
3742 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003743 flags = READ_ONCE(sqe->timeout_flags);
3744 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003745 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003746
Jens Axboe26a61672019-12-20 09:02:01 -07003747 req->timeout.count = READ_ONCE(sqe->off);
3748
Jens Axboe3529d8c2019-12-19 18:24:38 -07003749 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003750 return -ENOMEM;
3751
3752 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003753 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003754 req->flags |= REQ_F_TIMEOUT;
3755
3756 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003757 return -EFAULT;
3758
Jens Axboe11365042019-10-16 09:08:32 -06003759 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003760 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003761 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003762 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003763
Jens Axboead8a48a2019-11-15 08:49:11 -07003764 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3765 return 0;
3766}
3767
Jens Axboefc4df992019-12-10 14:38:45 -07003768static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003769{
3770 unsigned count;
3771 struct io_ring_ctx *ctx = req->ctx;
3772 struct io_timeout_data *data;
3773 struct list_head *entry;
3774 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003775
Jens Axboe2d283902019-12-04 11:08:05 -07003776 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003777
Jens Axboe5262f562019-09-17 12:26:57 -06003778 /*
3779 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003780 * timeout event to be satisfied. If it isn't set, then this is
3781 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003782 */
Jens Axboe26a61672019-12-20 09:02:01 -07003783 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003784 if (!count) {
3785 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3786 spin_lock_irq(&ctx->completion_lock);
3787 entry = ctx->timeout_list.prev;
3788 goto add;
3789 }
Jens Axboe5262f562019-09-17 12:26:57 -06003790
3791 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003792 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003793
3794 /*
3795 * Insertion sort, ensuring the first entry in the list is always
3796 * the one we need first.
3797 */
Jens Axboe5262f562019-09-17 12:26:57 -06003798 spin_lock_irq(&ctx->completion_lock);
3799 list_for_each_prev(entry, &ctx->timeout_list) {
3800 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003801 unsigned nxt_sq_head;
3802 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003803 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003804
Jens Axboe93bd25b2019-11-11 23:34:31 -07003805 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3806 continue;
3807
yangerkun5da0fb12019-10-15 21:59:29 +08003808 /*
3809 * Since cached_sq_head + count - 1 can overflow, use type long
3810 * long to store it.
3811 */
3812 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003813 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3814 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003815
3816 /*
3817 * cached_sq_head may overflow, and it will never overflow twice
3818 * once there is some timeout req still be valid.
3819 */
3820 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003821 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003822
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003823 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003824 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003825
3826 /*
3827 * Sequence of reqs after the insert one and itself should
3828 * be adjusted because each timeout req consumes a slot.
3829 */
3830 span++;
3831 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003832 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003833 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003834add:
Jens Axboe5262f562019-09-17 12:26:57 -06003835 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003836 data->timer.function = io_timeout_fn;
3837 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003838 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003839 return 0;
3840}
3841
Jens Axboe62755e32019-10-28 21:49:21 -06003842static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003843{
Jens Axboe62755e32019-10-28 21:49:21 -06003844 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003845
Jens Axboe62755e32019-10-28 21:49:21 -06003846 return req->user_data == (unsigned long) data;
3847}
3848
Jens Axboee977d6d2019-11-05 12:39:45 -07003849static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003850{
Jens Axboe62755e32019-10-28 21:49:21 -06003851 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003852 int ret = 0;
3853
Jens Axboe62755e32019-10-28 21:49:21 -06003854 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3855 switch (cancel_ret) {
3856 case IO_WQ_CANCEL_OK:
3857 ret = 0;
3858 break;
3859 case IO_WQ_CANCEL_RUNNING:
3860 ret = -EALREADY;
3861 break;
3862 case IO_WQ_CANCEL_NOTFOUND:
3863 ret = -ENOENT;
3864 break;
3865 }
3866
Jens Axboee977d6d2019-11-05 12:39:45 -07003867 return ret;
3868}
3869
Jens Axboe47f46762019-11-09 17:43:02 -07003870static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3871 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003872 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003873{
3874 unsigned long flags;
3875 int ret;
3876
3877 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3878 if (ret != -ENOENT) {
3879 spin_lock_irqsave(&ctx->completion_lock, flags);
3880 goto done;
3881 }
3882
3883 spin_lock_irqsave(&ctx->completion_lock, flags);
3884 ret = io_timeout_cancel(ctx, sqe_addr);
3885 if (ret != -ENOENT)
3886 goto done;
3887 ret = io_poll_cancel(ctx, sqe_addr);
3888done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003889 if (!ret)
3890 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003891 io_cqring_fill_event(req, ret);
3892 io_commit_cqring(ctx);
3893 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3894 io_cqring_ev_posted(ctx);
3895
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003896 if (ret < 0)
3897 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003898 io_put_req_find_next(req, nxt);
3899}
3900
Jens Axboe3529d8c2019-12-19 18:24:38 -07003901static int io_async_cancel_prep(struct io_kiocb *req,
3902 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003903{
Jens Axboefbf23842019-12-17 18:45:56 -07003904 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003905 return -EINVAL;
3906 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3907 sqe->cancel_flags)
3908 return -EINVAL;
3909
Jens Axboefbf23842019-12-17 18:45:56 -07003910 req->cancel.addr = READ_ONCE(sqe->addr);
3911 return 0;
3912}
3913
3914static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3915{
3916 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003917
3918 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003919 return 0;
3920}
3921
Jens Axboe05f3fb32019-12-09 11:22:50 -07003922static int io_files_update_prep(struct io_kiocb *req,
3923 const struct io_uring_sqe *sqe)
3924{
3925 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3926 return -EINVAL;
3927
3928 req->files_update.offset = READ_ONCE(sqe->off);
3929 req->files_update.nr_args = READ_ONCE(sqe->len);
3930 if (!req->files_update.nr_args)
3931 return -EINVAL;
3932 req->files_update.arg = READ_ONCE(sqe->addr);
3933 return 0;
3934}
3935
3936static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3937{
3938 struct io_ring_ctx *ctx = req->ctx;
3939 struct io_uring_files_update up;
3940 int ret;
3941
3942 if (force_nonblock) {
3943 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3944 return -EAGAIN;
3945 }
3946
3947 up.offset = req->files_update.offset;
3948 up.fds = req->files_update.arg;
3949
3950 mutex_lock(&ctx->uring_lock);
3951 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3952 mutex_unlock(&ctx->uring_lock);
3953
3954 if (ret < 0)
3955 req_set_fail_links(req);
3956 io_cqring_add_event(req, ret);
3957 io_put_req(req);
3958 return 0;
3959}
3960
Jens Axboe3529d8c2019-12-19 18:24:38 -07003961static int io_req_defer_prep(struct io_kiocb *req,
3962 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003963{
Jens Axboee7815732019-12-17 19:45:06 -07003964 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003965
Jens Axboed625c6e2019-12-17 19:53:05 -07003966 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003967 case IORING_OP_NOP:
3968 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003969 case IORING_OP_READV:
3970 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003971 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003972 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003973 break;
3974 case IORING_OP_WRITEV:
3975 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003976 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003977 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003978 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003979 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003980 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003981 break;
3982 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003983 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003984 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003985 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003986 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003987 break;
3988 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003989 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003990 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003991 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003992 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003993 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003994 break;
3995 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003996 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003997 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003998 break;
Jens Axboef499a022019-12-02 16:28:46 -07003999 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004000 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004001 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004002 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004003 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004004 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004005 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004006 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004007 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004008 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004009 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004010 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004011 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004012 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004013 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004014 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004015 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004016 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004017 case IORING_OP_FALLOCATE:
4018 ret = io_fallocate_prep(req, sqe);
4019 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004020 case IORING_OP_OPENAT:
4021 ret = io_openat_prep(req, sqe);
4022 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004023 case IORING_OP_CLOSE:
4024 ret = io_close_prep(req, sqe);
4025 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004026 case IORING_OP_FILES_UPDATE:
4027 ret = io_files_update_prep(req, sqe);
4028 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004029 case IORING_OP_STATX:
4030 ret = io_statx_prep(req, sqe);
4031 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004032 case IORING_OP_FADVISE:
4033 ret = io_fadvise_prep(req, sqe);
4034 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004035 case IORING_OP_MADVISE:
4036 ret = io_madvise_prep(req, sqe);
4037 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004038 case IORING_OP_OPENAT2:
4039 ret = io_openat2_prep(req, sqe);
4040 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004041 default:
Jens Axboee7815732019-12-17 19:45:06 -07004042 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4043 req->opcode);
4044 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004045 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004046 }
4047
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004048 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004049}
4050
Jens Axboe3529d8c2019-12-19 18:24:38 -07004051static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004052{
Jackie Liua197f662019-11-08 08:09:12 -07004053 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004054 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004055
Bob Liu9d858b22019-11-13 18:06:25 +08004056 /* Still need defer if there is pending req in defer list. */
4057 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004058 return 0;
4059
Jens Axboe3529d8c2019-12-19 18:24:38 -07004060 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004061 return -EAGAIN;
4062
Jens Axboe3529d8c2019-12-19 18:24:38 -07004063 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004064 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004065 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004066
Jens Axboede0617e2019-04-06 21:51:27 -06004067 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004068 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004069 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004070 return 0;
4071 }
4072
Jens Axboe915967f2019-11-21 09:01:20 -07004073 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004074 list_add_tail(&req->list, &ctx->defer_list);
4075 spin_unlock_irq(&ctx->completion_lock);
4076 return -EIOCBQUEUED;
4077}
4078
Jens Axboe3529d8c2019-12-19 18:24:38 -07004079static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4080 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004081{
Jackie Liua197f662019-11-08 08:09:12 -07004082 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004083 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004084
Jens Axboed625c6e2019-12-17 19:53:05 -07004085 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004086 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004087 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004088 break;
4089 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004090 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004091 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004092 if (sqe) {
4093 ret = io_read_prep(req, sqe, force_nonblock);
4094 if (ret < 0)
4095 break;
4096 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004097 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004098 break;
4099 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004100 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004101 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004102 if (sqe) {
4103 ret = io_write_prep(req, sqe, force_nonblock);
4104 if (ret < 0)
4105 break;
4106 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004107 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004108 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004109 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004110 if (sqe) {
4111 ret = io_prep_fsync(req, sqe);
4112 if (ret < 0)
4113 break;
4114 }
Jens Axboefc4df992019-12-10 14:38:45 -07004115 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004116 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004117 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004118 if (sqe) {
4119 ret = io_poll_add_prep(req, sqe);
4120 if (ret)
4121 break;
4122 }
Jens Axboefc4df992019-12-10 14:38:45 -07004123 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004124 break;
4125 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004126 if (sqe) {
4127 ret = io_poll_remove_prep(req, sqe);
4128 if (ret < 0)
4129 break;
4130 }
Jens Axboefc4df992019-12-10 14:38:45 -07004131 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004132 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004133 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004134 if (sqe) {
4135 ret = io_prep_sfr(req, sqe);
4136 if (ret < 0)
4137 break;
4138 }
Jens Axboefc4df992019-12-10 14:38:45 -07004139 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004140 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004141 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004142 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004143 if (sqe) {
4144 ret = io_sendmsg_prep(req, sqe);
4145 if (ret < 0)
4146 break;
4147 }
Jens Axboefddafac2020-01-04 20:19:44 -07004148 if (req->opcode == IORING_OP_SENDMSG)
4149 ret = io_sendmsg(req, nxt, force_nonblock);
4150 else
4151 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004152 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004153 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004154 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004155 if (sqe) {
4156 ret = io_recvmsg_prep(req, sqe);
4157 if (ret)
4158 break;
4159 }
Jens Axboefddafac2020-01-04 20:19:44 -07004160 if (req->opcode == IORING_OP_RECVMSG)
4161 ret = io_recvmsg(req, nxt, force_nonblock);
4162 else
4163 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004164 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004165 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004166 if (sqe) {
4167 ret = io_timeout_prep(req, sqe, false);
4168 if (ret)
4169 break;
4170 }
Jens Axboefc4df992019-12-10 14:38:45 -07004171 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004172 break;
Jens Axboe11365042019-10-16 09:08:32 -06004173 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004174 if (sqe) {
4175 ret = io_timeout_remove_prep(req, sqe);
4176 if (ret)
4177 break;
4178 }
Jens Axboefc4df992019-12-10 14:38:45 -07004179 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004180 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004181 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004182 if (sqe) {
4183 ret = io_accept_prep(req, sqe);
4184 if (ret)
4185 break;
4186 }
Jens Axboefc4df992019-12-10 14:38:45 -07004187 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004188 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004189 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004190 if (sqe) {
4191 ret = io_connect_prep(req, sqe);
4192 if (ret)
4193 break;
4194 }
Jens Axboefc4df992019-12-10 14:38:45 -07004195 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004196 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004197 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004198 if (sqe) {
4199 ret = io_async_cancel_prep(req, sqe);
4200 if (ret)
4201 break;
4202 }
Jens Axboefc4df992019-12-10 14:38:45 -07004203 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004204 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004205 case IORING_OP_FALLOCATE:
4206 if (sqe) {
4207 ret = io_fallocate_prep(req, sqe);
4208 if (ret)
4209 break;
4210 }
4211 ret = io_fallocate(req, nxt, force_nonblock);
4212 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004213 case IORING_OP_OPENAT:
4214 if (sqe) {
4215 ret = io_openat_prep(req, sqe);
4216 if (ret)
4217 break;
4218 }
4219 ret = io_openat(req, nxt, force_nonblock);
4220 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004221 case IORING_OP_CLOSE:
4222 if (sqe) {
4223 ret = io_close_prep(req, sqe);
4224 if (ret)
4225 break;
4226 }
4227 ret = io_close(req, nxt, force_nonblock);
4228 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004229 case IORING_OP_FILES_UPDATE:
4230 if (sqe) {
4231 ret = io_files_update_prep(req, sqe);
4232 if (ret)
4233 break;
4234 }
4235 ret = io_files_update(req, force_nonblock);
4236 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004237 case IORING_OP_STATX:
4238 if (sqe) {
4239 ret = io_statx_prep(req, sqe);
4240 if (ret)
4241 break;
4242 }
4243 ret = io_statx(req, nxt, force_nonblock);
4244 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004245 case IORING_OP_FADVISE:
4246 if (sqe) {
4247 ret = io_fadvise_prep(req, sqe);
4248 if (ret)
4249 break;
4250 }
4251 ret = io_fadvise(req, nxt, force_nonblock);
4252 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004253 case IORING_OP_MADVISE:
4254 if (sqe) {
4255 ret = io_madvise_prep(req, sqe);
4256 if (ret)
4257 break;
4258 }
4259 ret = io_madvise(req, nxt, force_nonblock);
4260 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004261 case IORING_OP_OPENAT2:
4262 if (sqe) {
4263 ret = io_openat2_prep(req, sqe);
4264 if (ret)
4265 break;
4266 }
4267 ret = io_openat2(req, nxt, force_nonblock);
4268 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004269 default:
4270 ret = -EINVAL;
4271 break;
4272 }
4273
Jens Axboedef596e2019-01-09 08:59:42 -07004274 if (ret)
4275 return ret;
4276
4277 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004278 const bool in_async = io_wq_current_is_worker();
4279
Jens Axboe9e645e112019-05-10 16:07:28 -06004280 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004281 return -EAGAIN;
4282
Jens Axboe11ba8202020-01-15 21:51:17 -07004283 /* workqueue context doesn't hold uring_lock, grab it now */
4284 if (in_async)
4285 mutex_lock(&ctx->uring_lock);
4286
Jens Axboedef596e2019-01-09 08:59:42 -07004287 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004288
4289 if (in_async)
4290 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004291 }
4292
4293 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004294}
4295
Jens Axboe561fb042019-10-24 07:25:42 -06004296static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004297{
Jens Axboe561fb042019-10-24 07:25:42 -06004298 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004299 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004300 struct io_kiocb *nxt = NULL;
4301 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004302
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004303 /* if NO_CANCEL is set, we must still run the work */
4304 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4305 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004306 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004307 }
Jens Axboe31b51512019-01-18 22:56:34 -07004308
Jens Axboe561fb042019-10-24 07:25:42 -06004309 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004310 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4311 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004312 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004313 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004314 /*
4315 * We can get EAGAIN for polled IO even though we're
4316 * forcing a sync submission from here, since we can't
4317 * wait for request slots on the block side.
4318 */
4319 if (ret != -EAGAIN)
4320 break;
4321 cond_resched();
4322 } while (1);
4323 }
Jens Axboe31b51512019-01-18 22:56:34 -07004324
Jens Axboe561fb042019-10-24 07:25:42 -06004325 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004326 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004327
Jens Axboe561fb042019-10-24 07:25:42 -06004328 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004329 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004330 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004331 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004332 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004333
Jens Axboe561fb042019-10-24 07:25:42 -06004334 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004335 if (!ret && nxt)
4336 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004337}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004338
Jens Axboe15b71ab2019-12-11 11:20:36 -07004339static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004340{
Jens Axboed3656342019-12-18 09:50:26 -07004341 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004342 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004343 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4344 return 0;
4345 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004346}
4347
Jens Axboe65e19f52019-10-26 07:20:21 -06004348static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4349 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004350{
Jens Axboe65e19f52019-10-26 07:20:21 -06004351 struct fixed_file_table *table;
4352
Jens Axboe05f3fb32019-12-09 11:22:50 -07004353 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4354 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004355}
4356
Jens Axboe3529d8c2019-12-19 18:24:38 -07004357static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4358 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004359{
Jackie Liua197f662019-11-08 08:09:12 -07004360 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004361 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004362 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004363
Jens Axboe3529d8c2019-12-19 18:24:38 -07004364 flags = READ_ONCE(sqe->flags);
4365 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004366
Jackie Liu4fe2c962019-09-09 20:50:40 +08004367 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06004368 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06004369
Jens Axboed3656342019-12-18 09:50:26 -07004370 if (!io_req_needs_file(req, fd))
4371 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004372
4373 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004374 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004375 (unsigned) fd >= ctx->nr_user_files))
4376 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004377 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004378 req->file = io_file_from_index(ctx, fd);
4379 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004380 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004381 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004382 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004383 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004384 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004385 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004386 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004387 req->file = io_file_get(state, fd);
4388 if (unlikely(!req->file))
4389 return -EBADF;
4390 }
4391
4392 return 0;
4393}
4394
Jackie Liua197f662019-11-08 08:09:12 -07004395static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004396{
Jens Axboefcb323c2019-10-24 12:39:47 -06004397 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004398 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004399
Jens Axboeb5dba592019-12-11 14:02:38 -07004400 if (!req->ring_file)
4401 return -EBADF;
4402
Jens Axboefcb323c2019-10-24 12:39:47 -06004403 rcu_read_lock();
4404 spin_lock_irq(&ctx->inflight_lock);
4405 /*
4406 * We use the f_ops->flush() handler to ensure that we can flush
4407 * out work accessing these files if the fd is closed. Check if
4408 * the fd has changed since we started down this path, and disallow
4409 * this operation if it has.
4410 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004411 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004412 list_add(&req->inflight_entry, &ctx->inflight_list);
4413 req->flags |= REQ_F_INFLIGHT;
4414 req->work.files = current->files;
4415 ret = 0;
4416 }
4417 spin_unlock_irq(&ctx->inflight_lock);
4418 rcu_read_unlock();
4419
4420 return ret;
4421}
4422
Jens Axboe2665abf2019-11-05 12:40:47 -07004423static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4424{
Jens Axboead8a48a2019-11-15 08:49:11 -07004425 struct io_timeout_data *data = container_of(timer,
4426 struct io_timeout_data, timer);
4427 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004428 struct io_ring_ctx *ctx = req->ctx;
4429 struct io_kiocb *prev = NULL;
4430 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004431
4432 spin_lock_irqsave(&ctx->completion_lock, flags);
4433
4434 /*
4435 * We don't expect the list to be empty, that will only happen if we
4436 * race with the completion of the linked work.
4437 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004438 if (!list_empty(&req->link_list)) {
4439 prev = list_entry(req->link_list.prev, struct io_kiocb,
4440 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004441 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004442 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004443 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4444 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004445 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004446 }
4447
4448 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4449
4450 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004451 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004452 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4453 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004454 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004455 } else {
4456 io_cqring_add_event(req, -ETIME);
4457 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004458 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004459 return HRTIMER_NORESTART;
4460}
4461
Jens Axboead8a48a2019-11-15 08:49:11 -07004462static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004463{
Jens Axboe76a46e02019-11-10 23:34:16 -07004464 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004465
Jens Axboe76a46e02019-11-10 23:34:16 -07004466 /*
4467 * If the list is now empty, then our linked request finished before
4468 * we got a chance to setup the timer
4469 */
4470 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004471 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004472 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004473
Jens Axboead8a48a2019-11-15 08:49:11 -07004474 data->timer.function = io_link_timeout_fn;
4475 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4476 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004477 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004478 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004479
Jens Axboe2665abf2019-11-05 12:40:47 -07004480 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004481 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004482}
4483
Jens Axboead8a48a2019-11-15 08:49:11 -07004484static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004485{
4486 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004487
Jens Axboe2665abf2019-11-05 12:40:47 -07004488 if (!(req->flags & REQ_F_LINK))
4489 return NULL;
4490
Pavel Begunkov44932332019-12-05 16:16:35 +03004491 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4492 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004493 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004494 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004495
Jens Axboe76a46e02019-11-10 23:34:16 -07004496 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004497 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004498}
4499
Jens Axboe3529d8c2019-12-19 18:24:38 -07004500static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004501{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004502 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004503 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004504 int ret;
4505
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004506again:
4507 linked_timeout = io_prep_linked_timeout(req);
4508
Jens Axboe3529d8c2019-12-19 18:24:38 -07004509 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004510
4511 /*
4512 * We async punt it if the file wasn't marked NOWAIT, or if the file
4513 * doesn't support non-blocking read/write attempts
4514 */
4515 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4516 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004517 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4518 ret = io_grab_files(req);
4519 if (ret)
4520 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004521 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004522
4523 /*
4524 * Queued up for async execution, worker will release
4525 * submit reference when the iocb is actually submitted.
4526 */
4527 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004528 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004529 }
Jens Axboee65ef562019-03-12 10:16:44 -06004530
Jens Axboefcb323c2019-10-24 12:39:47 -06004531err:
Jens Axboee65ef562019-03-12 10:16:44 -06004532 /* drop submission reference */
4533 io_put_req(req);
4534
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004535 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004536 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004537 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004538 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004539 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004540 }
4541
Jens Axboee65ef562019-03-12 10:16:44 -06004542 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004543 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004544 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004545 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004546 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004547 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004548done_req:
4549 if (nxt) {
4550 req = nxt;
4551 nxt = NULL;
4552 goto again;
4553 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004554}
4555
Jens Axboe3529d8c2019-12-19 18:24:38 -07004556static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004557{
4558 int ret;
4559
Jens Axboe3529d8c2019-12-19 18:24:38 -07004560 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004561 if (ret) {
4562 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004563 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004564 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004565 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004566 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004567 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboece35a472019-12-17 08:04:44 -07004568 /*
4569 * Never try inline submit of IOSQE_ASYNC is set, go straight
4570 * to async execution.
4571 */
4572 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4573 io_queue_async_work(req);
4574 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004575 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004576 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004577}
4578
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004579static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004580{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004581 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004582 io_cqring_add_event(req, -ECANCELED);
4583 io_double_put_req(req);
4584 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004585 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004586}
4587
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004588#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004589 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004590
Jens Axboe3529d8c2019-12-19 18:24:38 -07004591static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4592 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004593{
Jackie Liua197f662019-11-08 08:09:12 -07004594 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004595 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004596 int ret;
4597
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004598 sqe_flags = READ_ONCE(sqe->flags);
4599
Jens Axboe9e645e112019-05-10 16:07:28 -06004600 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004601 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004602 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004603 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004604 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004605 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004606 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004607
Jens Axboe3529d8c2019-12-19 18:24:38 -07004608 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004609 if (unlikely(ret)) {
4610err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004611 io_cqring_add_event(req, ret);
4612 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004613 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004614 }
4615
Jens Axboe9e645e112019-05-10 16:07:28 -06004616 /*
4617 * If we already have a head request, queue this one for async
4618 * submittal once the head completes. If we don't have a head but
4619 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4620 * submitted sync once the chain is complete. If none of those
4621 * conditions are true (normal request), then just queue it.
4622 */
4623 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004624 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004625
Pavel Begunkov711be032020-01-17 03:57:59 +03004626 if (sqe_flags & IOSQE_IO_DRAIN) {
4627 head->flags |= REQ_F_IO_DRAIN;
4628 ctx->drain_next = 1;
4629 }
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004630
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004631 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004632 req->flags |= REQ_F_HARDLINK;
4633
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004634 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004635 ret = -EAGAIN;
4636 goto err_req;
4637 }
4638
Jens Axboe3529d8c2019-12-19 18:24:38 -07004639 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004640 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004641 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004642 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004643 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004644 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004645 trace_io_uring_link(ctx, req, head);
4646 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004647
4648 /* last request of a link, enqueue the link */
4649 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4650 io_queue_link_head(head);
4651 *link = NULL;
4652 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004653 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004654 if (unlikely(ctx->drain_next)) {
4655 req->flags |= REQ_F_IO_DRAIN;
4656 req->ctx->drain_next = 0;
4657 }
4658 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4659 req->flags |= REQ_F_LINK;
4660 if (sqe_flags & IOSQE_IO_HARDLINK)
4661 req->flags |= REQ_F_HARDLINK;
4662
4663 INIT_LIST_HEAD(&req->link_list);
4664 ret = io_req_defer_prep(req, sqe);
4665 if (ret)
4666 req->flags |= REQ_F_FAIL_LINK;
4667 *link = req;
4668 } else {
4669 io_queue_sqe(req, sqe);
4670 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004671 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004672
4673 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004674}
4675
Jens Axboe9a56a232019-01-09 09:06:50 -07004676/*
4677 * Batched submission is done, ensure local IO is flushed out.
4678 */
4679static void io_submit_state_end(struct io_submit_state *state)
4680{
4681 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004682 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004683 if (state->free_reqs)
4684 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4685 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004686}
4687
4688/*
4689 * Start submission side cache.
4690 */
4691static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004692 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004693{
4694 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004695 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004696 state->file = NULL;
4697 state->ios_left = max_ios;
4698}
4699
Jens Axboe2b188cc2019-01-07 10:46:33 -07004700static void io_commit_sqring(struct io_ring_ctx *ctx)
4701{
Hristo Venev75b28af2019-08-26 17:23:46 +00004702 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004703
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004704 /*
4705 * Ensure any loads from the SQEs are done at this point,
4706 * since once we write the new head, the application could
4707 * write new data to them.
4708 */
4709 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004710}
4711
4712/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004713 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004714 * that is mapped by userspace. This means that care needs to be taken to
4715 * ensure that reads are stable, as we cannot rely on userspace always
4716 * being a good citizen. If members of the sqe are validated and then later
4717 * used, it's important that those reads are done through READ_ONCE() to
4718 * prevent a re-load down the line.
4719 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004720static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4721 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004722{
Hristo Venev75b28af2019-08-26 17:23:46 +00004723 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004724 unsigned head;
4725
4726 /*
4727 * The cached sq head (or cq tail) serves two purposes:
4728 *
4729 * 1) allows us to batch the cost of updating the user visible
4730 * head updates.
4731 * 2) allows the kernel side to track the head on its own, even
4732 * though the application is the one updating it.
4733 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004734 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004735 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004736 /*
4737 * All io need record the previous position, if LINK vs DARIN,
4738 * it can be used to mark the position of the first IO in the
4739 * link list.
4740 */
4741 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004742 *sqe_ptr = &ctx->sq_sqes[head];
4743 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4744 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004745 ctx->cached_sq_head++;
4746 return true;
4747 }
4748
4749 /* drop invalid entries */
4750 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004751 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004752 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004753 return false;
4754}
4755
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004756static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004757 struct file *ring_file, int ring_fd,
4758 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004759{
4760 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004761 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004762 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004763 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004764
Jens Axboec4a2ed72019-11-21 21:01:26 -07004765 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004766 if (test_bit(0, &ctx->sq_check_overflow)) {
4767 if (!list_empty(&ctx->cq_overflow_list) &&
4768 !io_cqring_overflow_flush(ctx, false))
4769 return -EBUSY;
4770 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004771
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004772 /* make sure SQ entry isn't read before tail */
4773 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004774
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004775 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4776 return -EAGAIN;
4777
Jens Axboe6c271ce2019-01-10 11:22:30 -07004778 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004779 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004780 statep = &state;
4781 }
4782
4783 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004784 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004785 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004786
Pavel Begunkov196be952019-11-07 01:41:06 +03004787 req = io_get_req(ctx, statep);
4788 if (unlikely(!req)) {
4789 if (!submitted)
4790 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004791 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004792 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004793 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004794 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004795 break;
4796 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004797
Jens Axboed3656342019-12-18 09:50:26 -07004798 /* will complete beyond this point, count as submitted */
4799 submitted++;
4800
4801 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4802 io_cqring_add_event(req, -EINVAL);
4803 io_double_put_req(req);
4804 break;
4805 }
4806
4807 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004808 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4809 if (!mm_fault) {
4810 use_mm(ctx->sqo_mm);
4811 *mm = ctx->sqo_mm;
4812 }
4813 }
4814
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004815 req->ring_file = ring_file;
4816 req->ring_fd = ring_fd;
4817 req->has_user = *mm != NULL;
4818 req->in_async = async;
4819 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07004820 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
4821 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004822 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004823 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004824 }
4825
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004826 if (submitted != nr)
4827 percpu_ref_put_many(&ctx->refs, nr - submitted);
Jens Axboe9e645e112019-05-10 16:07:28 -06004828 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004829 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004830 if (statep)
4831 io_submit_state_end(&state);
4832
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004833 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4834 io_commit_sqring(ctx);
4835
Jens Axboe6c271ce2019-01-10 11:22:30 -07004836 return submitted;
4837}
4838
4839static int io_sq_thread(void *data)
4840{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004841 struct io_ring_ctx *ctx = data;
4842 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004843 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004844 mm_segment_t old_fs;
4845 DEFINE_WAIT(wait);
4846 unsigned inflight;
4847 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004848 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004849
Jens Axboe206aefd2019-11-07 18:27:42 -07004850 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004851
Jens Axboe6c271ce2019-01-10 11:22:30 -07004852 old_fs = get_fs();
4853 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004854 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004855
Jens Axboec1edbf52019-11-10 16:56:04 -07004856 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004857 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004858 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004859
4860 if (inflight) {
4861 unsigned nr_events = 0;
4862
4863 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004864 /*
4865 * inflight is the count of the maximum possible
4866 * entries we submitted, but it can be smaller
4867 * if we dropped some of them. If we don't have
4868 * poll entries available, then we know that we
4869 * have nothing left to poll for. Reset the
4870 * inflight count to zero in that case.
4871 */
4872 mutex_lock(&ctx->uring_lock);
4873 if (!list_empty(&ctx->poll_list))
4874 __io_iopoll_check(ctx, &nr_events, 0);
4875 else
4876 inflight = 0;
4877 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004878 } else {
4879 /*
4880 * Normal IO, just pretend everything completed.
4881 * We don't have to poll completions for that.
4882 */
4883 nr_events = inflight;
4884 }
4885
4886 inflight -= nr_events;
4887 if (!inflight)
4888 timeout = jiffies + ctx->sq_thread_idle;
4889 }
4890
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004891 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004892
4893 /*
4894 * If submit got -EBUSY, flag us as needing the application
4895 * to enter the kernel to reap and flush events.
4896 */
4897 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004898 /*
4899 * We're polling. If we're within the defined idle
4900 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004901 * to sleep. The exception is if we got EBUSY doing
4902 * more IO, we should wait for the application to
4903 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004904 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004905 if (inflight ||
4906 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004907 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004908 continue;
4909 }
4910
4911 /*
4912 * Drop cur_mm before scheduling, we can't hold it for
4913 * long periods (or over schedule()). Do this before
4914 * adding ourselves to the waitqueue, as the unuse/drop
4915 * may sleep.
4916 */
4917 if (cur_mm) {
4918 unuse_mm(cur_mm);
4919 mmput(cur_mm);
4920 cur_mm = NULL;
4921 }
4922
4923 prepare_to_wait(&ctx->sqo_wait, &wait,
4924 TASK_INTERRUPTIBLE);
4925
4926 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004927 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004928 /* make sure to read SQ tail after writing flags */
4929 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004930
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004931 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004932 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004933 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004934 finish_wait(&ctx->sqo_wait, &wait);
4935 break;
4936 }
4937 if (signal_pending(current))
4938 flush_signals(current);
4939 schedule();
4940 finish_wait(&ctx->sqo_wait, &wait);
4941
Hristo Venev75b28af2019-08-26 17:23:46 +00004942 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004943 continue;
4944 }
4945 finish_wait(&ctx->sqo_wait, &wait);
4946
Hristo Venev75b28af2019-08-26 17:23:46 +00004947 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004948 }
4949
Jens Axboe8a4955f2019-12-09 14:52:35 -07004950 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004951 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004952 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004953 if (ret > 0)
4954 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004955 }
4956
4957 set_fs(old_fs);
4958 if (cur_mm) {
4959 unuse_mm(cur_mm);
4960 mmput(cur_mm);
4961 }
Jens Axboe181e4482019-11-25 08:52:30 -07004962 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004963
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004964 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004965
Jens Axboe6c271ce2019-01-10 11:22:30 -07004966 return 0;
4967}
4968
Jens Axboebda52162019-09-24 13:47:15 -06004969struct io_wait_queue {
4970 struct wait_queue_entry wq;
4971 struct io_ring_ctx *ctx;
4972 unsigned to_wait;
4973 unsigned nr_timeouts;
4974};
4975
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004976static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004977{
4978 struct io_ring_ctx *ctx = iowq->ctx;
4979
4980 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004981 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004982 * started waiting. For timeouts, we always want to return to userspace,
4983 * regardless of event count.
4984 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004985 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004986 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4987}
4988
4989static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4990 int wake_flags, void *key)
4991{
4992 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4993 wq);
4994
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004995 /* use noflush == true, as we can't safely rely on locking context */
4996 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004997 return -1;
4998
4999 return autoremove_wake_function(curr, mode, wake_flags, key);
5000}
5001
Jens Axboe2b188cc2019-01-07 10:46:33 -07005002/*
5003 * Wait until events become available, if we don't already have some. The
5004 * application must reap them itself, as they reside on the shared cq ring.
5005 */
5006static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5007 const sigset_t __user *sig, size_t sigsz)
5008{
Jens Axboebda52162019-09-24 13:47:15 -06005009 struct io_wait_queue iowq = {
5010 .wq = {
5011 .private = current,
5012 .func = io_wake_function,
5013 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5014 },
5015 .ctx = ctx,
5016 .to_wait = min_events,
5017 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005018 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005019 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005020
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005021 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005022 return 0;
5023
5024 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005025#ifdef CONFIG_COMPAT
5026 if (in_compat_syscall())
5027 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005028 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005029 else
5030#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005031 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005032
Jens Axboe2b188cc2019-01-07 10:46:33 -07005033 if (ret)
5034 return ret;
5035 }
5036
Jens Axboebda52162019-09-24 13:47:15 -06005037 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005038 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005039 do {
5040 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5041 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005042 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005043 break;
5044 schedule();
5045 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005046 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005047 break;
5048 }
5049 } while (1);
5050 finish_wait(&ctx->wait, &iowq.wq);
5051
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005052 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005053
Hristo Venev75b28af2019-08-26 17:23:46 +00005054 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005055}
5056
Jens Axboe6b063142019-01-10 22:13:58 -07005057static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5058{
5059#if defined(CONFIG_UNIX)
5060 if (ctx->ring_sock) {
5061 struct sock *sock = ctx->ring_sock->sk;
5062 struct sk_buff *skb;
5063
5064 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5065 kfree_skb(skb);
5066 }
5067#else
5068 int i;
5069
Jens Axboe65e19f52019-10-26 07:20:21 -06005070 for (i = 0; i < ctx->nr_user_files; i++) {
5071 struct file *file;
5072
5073 file = io_file_from_index(ctx, i);
5074 if (file)
5075 fput(file);
5076 }
Jens Axboe6b063142019-01-10 22:13:58 -07005077#endif
5078}
5079
Jens Axboe05f3fb32019-12-09 11:22:50 -07005080static void io_file_ref_kill(struct percpu_ref *ref)
5081{
5082 struct fixed_file_data *data;
5083
5084 data = container_of(ref, struct fixed_file_data, refs);
5085 complete(&data->done);
5086}
5087
Jens Axboe6b063142019-01-10 22:13:58 -07005088static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5089{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005090 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005091 unsigned nr_tables, i;
5092
Jens Axboe05f3fb32019-12-09 11:22:50 -07005093 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005094 return -ENXIO;
5095
Jens Axboe05f3fb32019-12-09 11:22:50 -07005096 /* protect against inflight atomic switch, which drops the ref */
5097 flush_work(&data->ref_work);
5098 percpu_ref_get(&data->refs);
5099 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5100 wait_for_completion(&data->done);
5101 percpu_ref_put(&data->refs);
5102 percpu_ref_exit(&data->refs);
5103
Jens Axboe6b063142019-01-10 22:13:58 -07005104 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005105 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5106 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005107 kfree(data->table[i].files);
5108 kfree(data->table);
5109 kfree(data);
5110 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005111 ctx->nr_user_files = 0;
5112 return 0;
5113}
5114
Jens Axboe6c271ce2019-01-10 11:22:30 -07005115static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5116{
5117 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005118 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005119 /*
5120 * The park is a bit of a work-around, without it we get
5121 * warning spews on shutdown with SQPOLL set and affinity
5122 * set to a single CPU.
5123 */
Jens Axboe06058632019-04-13 09:26:03 -06005124 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005125 kthread_stop(ctx->sqo_thread);
5126 ctx->sqo_thread = NULL;
5127 }
5128}
5129
Jens Axboe6b063142019-01-10 22:13:58 -07005130static void io_finish_async(struct io_ring_ctx *ctx)
5131{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005132 io_sq_thread_stop(ctx);
5133
Jens Axboe561fb042019-10-24 07:25:42 -06005134 if (ctx->io_wq) {
5135 io_wq_destroy(ctx->io_wq);
5136 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005137 }
5138}
5139
5140#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005141/*
5142 * Ensure the UNIX gc is aware of our file set, so we are certain that
5143 * the io_uring can be safely unregistered on process exit, even if we have
5144 * loops in the file referencing.
5145 */
5146static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5147{
5148 struct sock *sk = ctx->ring_sock->sk;
5149 struct scm_fp_list *fpl;
5150 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005151 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005152
5153 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5154 unsigned long inflight = ctx->user->unix_inflight + nr;
5155
5156 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5157 return -EMFILE;
5158 }
5159
5160 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5161 if (!fpl)
5162 return -ENOMEM;
5163
5164 skb = alloc_skb(0, GFP_KERNEL);
5165 if (!skb) {
5166 kfree(fpl);
5167 return -ENOMEM;
5168 }
5169
5170 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005171
Jens Axboe08a45172019-10-03 08:11:03 -06005172 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005173 fpl->user = get_uid(ctx->user);
5174 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005175 struct file *file = io_file_from_index(ctx, i + offset);
5176
5177 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005178 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005179 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005180 unix_inflight(fpl->user, fpl->fp[nr_files]);
5181 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005182 }
5183
Jens Axboe08a45172019-10-03 08:11:03 -06005184 if (nr_files) {
5185 fpl->max = SCM_MAX_FD;
5186 fpl->count = nr_files;
5187 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005188 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005189 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5190 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005191
Jens Axboe08a45172019-10-03 08:11:03 -06005192 for (i = 0; i < nr_files; i++)
5193 fput(fpl->fp[i]);
5194 } else {
5195 kfree_skb(skb);
5196 kfree(fpl);
5197 }
Jens Axboe6b063142019-01-10 22:13:58 -07005198
5199 return 0;
5200}
5201
5202/*
5203 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5204 * causes regular reference counting to break down. We rely on the UNIX
5205 * garbage collection to take care of this problem for us.
5206 */
5207static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5208{
5209 unsigned left, total;
5210 int ret = 0;
5211
5212 total = 0;
5213 left = ctx->nr_user_files;
5214 while (left) {
5215 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005216
5217 ret = __io_sqe_files_scm(ctx, this_files, total);
5218 if (ret)
5219 break;
5220 left -= this_files;
5221 total += this_files;
5222 }
5223
5224 if (!ret)
5225 return 0;
5226
5227 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005228 struct file *file = io_file_from_index(ctx, total);
5229
5230 if (file)
5231 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005232 total++;
5233 }
5234
5235 return ret;
5236}
5237#else
5238static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5239{
5240 return 0;
5241}
5242#endif
5243
Jens Axboe65e19f52019-10-26 07:20:21 -06005244static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5245 unsigned nr_files)
5246{
5247 int i;
5248
5249 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005250 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005251 unsigned this_files;
5252
5253 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5254 table->files = kcalloc(this_files, sizeof(struct file *),
5255 GFP_KERNEL);
5256 if (!table->files)
5257 break;
5258 nr_files -= this_files;
5259 }
5260
5261 if (i == nr_tables)
5262 return 0;
5263
5264 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005265 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005266 kfree(table->files);
5267 }
5268 return 1;
5269}
5270
Jens Axboe05f3fb32019-12-09 11:22:50 -07005271static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005272{
5273#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005274 struct sock *sock = ctx->ring_sock->sk;
5275 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5276 struct sk_buff *skb;
5277 int i;
5278
5279 __skb_queue_head_init(&list);
5280
5281 /*
5282 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5283 * remove this entry and rearrange the file array.
5284 */
5285 skb = skb_dequeue(head);
5286 while (skb) {
5287 struct scm_fp_list *fp;
5288
5289 fp = UNIXCB(skb).fp;
5290 for (i = 0; i < fp->count; i++) {
5291 int left;
5292
5293 if (fp->fp[i] != file)
5294 continue;
5295
5296 unix_notinflight(fp->user, fp->fp[i]);
5297 left = fp->count - 1 - i;
5298 if (left) {
5299 memmove(&fp->fp[i], &fp->fp[i + 1],
5300 left * sizeof(struct file *));
5301 }
5302 fp->count--;
5303 if (!fp->count) {
5304 kfree_skb(skb);
5305 skb = NULL;
5306 } else {
5307 __skb_queue_tail(&list, skb);
5308 }
5309 fput(file);
5310 file = NULL;
5311 break;
5312 }
5313
5314 if (!file)
5315 break;
5316
5317 __skb_queue_tail(&list, skb);
5318
5319 skb = skb_dequeue(head);
5320 }
5321
5322 if (skb_peek(&list)) {
5323 spin_lock_irq(&head->lock);
5324 while ((skb = __skb_dequeue(&list)) != NULL)
5325 __skb_queue_tail(head, skb);
5326 spin_unlock_irq(&head->lock);
5327 }
5328#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005329 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005330#endif
5331}
5332
Jens Axboe05f3fb32019-12-09 11:22:50 -07005333struct io_file_put {
5334 struct llist_node llist;
5335 struct file *file;
5336 struct completion *done;
5337};
5338
5339static void io_ring_file_ref_switch(struct work_struct *work)
5340{
5341 struct io_file_put *pfile, *tmp;
5342 struct fixed_file_data *data;
5343 struct llist_node *node;
5344
5345 data = container_of(work, struct fixed_file_data, ref_work);
5346
5347 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5348 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5349 io_ring_file_put(data->ctx, pfile->file);
5350 if (pfile->done)
5351 complete(pfile->done);
5352 else
5353 kfree(pfile);
5354 }
5355 }
5356
5357 percpu_ref_get(&data->refs);
5358 percpu_ref_switch_to_percpu(&data->refs);
5359}
5360
5361static void io_file_data_ref_zero(struct percpu_ref *ref)
5362{
5363 struct fixed_file_data *data;
5364
5365 data = container_of(ref, struct fixed_file_data, refs);
5366
5367 /* we can't safely switch from inside this context, punt to wq */
5368 queue_work(system_wq, &data->ref_work);
5369}
5370
5371static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5372 unsigned nr_args)
5373{
5374 __s32 __user *fds = (__s32 __user *) arg;
5375 unsigned nr_tables;
5376 struct file *file;
5377 int fd, ret = 0;
5378 unsigned i;
5379
5380 if (ctx->file_data)
5381 return -EBUSY;
5382 if (!nr_args)
5383 return -EINVAL;
5384 if (nr_args > IORING_MAX_FIXED_FILES)
5385 return -EMFILE;
5386
5387 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5388 if (!ctx->file_data)
5389 return -ENOMEM;
5390 ctx->file_data->ctx = ctx;
5391 init_completion(&ctx->file_data->done);
5392
5393 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5394 ctx->file_data->table = kcalloc(nr_tables,
5395 sizeof(struct fixed_file_table),
5396 GFP_KERNEL);
5397 if (!ctx->file_data->table) {
5398 kfree(ctx->file_data);
5399 ctx->file_data = NULL;
5400 return -ENOMEM;
5401 }
5402
5403 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5404 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5405 kfree(ctx->file_data->table);
5406 kfree(ctx->file_data);
5407 ctx->file_data = NULL;
5408 return -ENOMEM;
5409 }
5410 ctx->file_data->put_llist.first = NULL;
5411 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5412
5413 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5414 percpu_ref_exit(&ctx->file_data->refs);
5415 kfree(ctx->file_data->table);
5416 kfree(ctx->file_data);
5417 ctx->file_data = NULL;
5418 return -ENOMEM;
5419 }
5420
5421 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5422 struct fixed_file_table *table;
5423 unsigned index;
5424
5425 ret = -EFAULT;
5426 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5427 break;
5428 /* allow sparse sets */
5429 if (fd == -1) {
5430 ret = 0;
5431 continue;
5432 }
5433
5434 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5435 index = i & IORING_FILE_TABLE_MASK;
5436 file = fget(fd);
5437
5438 ret = -EBADF;
5439 if (!file)
5440 break;
5441
5442 /*
5443 * Don't allow io_uring instances to be registered. If UNIX
5444 * isn't enabled, then this causes a reference cycle and this
5445 * instance can never get freed. If UNIX is enabled we'll
5446 * handle it just fine, but there's still no point in allowing
5447 * a ring fd as it doesn't support regular read/write anyway.
5448 */
5449 if (file->f_op == &io_uring_fops) {
5450 fput(file);
5451 break;
5452 }
5453 ret = 0;
5454 table->files[index] = file;
5455 }
5456
5457 if (ret) {
5458 for (i = 0; i < ctx->nr_user_files; i++) {
5459 file = io_file_from_index(ctx, i);
5460 if (file)
5461 fput(file);
5462 }
5463 for (i = 0; i < nr_tables; i++)
5464 kfree(ctx->file_data->table[i].files);
5465
5466 kfree(ctx->file_data->table);
5467 kfree(ctx->file_data);
5468 ctx->file_data = NULL;
5469 ctx->nr_user_files = 0;
5470 return ret;
5471 }
5472
5473 ret = io_sqe_files_scm(ctx);
5474 if (ret)
5475 io_sqe_files_unregister(ctx);
5476
5477 return ret;
5478}
5479
Jens Axboec3a31e62019-10-03 13:59:56 -06005480static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5481 int index)
5482{
5483#if defined(CONFIG_UNIX)
5484 struct sock *sock = ctx->ring_sock->sk;
5485 struct sk_buff_head *head = &sock->sk_receive_queue;
5486 struct sk_buff *skb;
5487
5488 /*
5489 * See if we can merge this file into an existing skb SCM_RIGHTS
5490 * file set. If there's no room, fall back to allocating a new skb
5491 * and filling it in.
5492 */
5493 spin_lock_irq(&head->lock);
5494 skb = skb_peek(head);
5495 if (skb) {
5496 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5497
5498 if (fpl->count < SCM_MAX_FD) {
5499 __skb_unlink(skb, head);
5500 spin_unlock_irq(&head->lock);
5501 fpl->fp[fpl->count] = get_file(file);
5502 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5503 fpl->count++;
5504 spin_lock_irq(&head->lock);
5505 __skb_queue_head(head, skb);
5506 } else {
5507 skb = NULL;
5508 }
5509 }
5510 spin_unlock_irq(&head->lock);
5511
5512 if (skb) {
5513 fput(file);
5514 return 0;
5515 }
5516
5517 return __io_sqe_files_scm(ctx, 1, index);
5518#else
5519 return 0;
5520#endif
5521}
5522
Jens Axboe05f3fb32019-12-09 11:22:50 -07005523static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005524{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005525 struct fixed_file_data *data;
5526
5527 data = container_of(ref, struct fixed_file_data, refs);
5528 clear_bit(FFD_F_ATOMIC, &data->state);
5529}
5530
5531static bool io_queue_file_removal(struct fixed_file_data *data,
5532 struct file *file)
5533{
5534 struct io_file_put *pfile, pfile_stack;
5535 DECLARE_COMPLETION_ONSTACK(done);
5536
5537 /*
5538 * If we fail allocating the struct we need for doing async reomval
5539 * of this file, just punt to sync and wait for it.
5540 */
5541 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5542 if (!pfile) {
5543 pfile = &pfile_stack;
5544 pfile->done = &done;
5545 }
5546
5547 pfile->file = file;
5548 llist_add(&pfile->llist, &data->put_llist);
5549
5550 if (pfile == &pfile_stack) {
5551 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5552 percpu_ref_put(&data->refs);
5553 percpu_ref_switch_to_atomic(&data->refs,
5554 io_atomic_switch);
5555 }
5556 wait_for_completion(&done);
5557 flush_work(&data->ref_work);
5558 return false;
5559 }
5560
5561 return true;
5562}
5563
5564static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5565 struct io_uring_files_update *up,
5566 unsigned nr_args)
5567{
5568 struct fixed_file_data *data = ctx->file_data;
5569 bool ref_switch = false;
5570 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005571 __s32 __user *fds;
5572 int fd, i, err;
5573 __u32 done;
5574
Jens Axboe05f3fb32019-12-09 11:22:50 -07005575 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005576 return -EOVERFLOW;
5577 if (done > ctx->nr_user_files)
5578 return -EINVAL;
5579
5580 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005581 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005582 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005583 struct fixed_file_table *table;
5584 unsigned index;
5585
Jens Axboec3a31e62019-10-03 13:59:56 -06005586 err = 0;
5587 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5588 err = -EFAULT;
5589 break;
5590 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005591 i = array_index_nospec(up->offset, ctx->nr_user_files);
5592 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005593 index = i & IORING_FILE_TABLE_MASK;
5594 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005595 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005596 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005597 if (io_queue_file_removal(data, file))
5598 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005599 }
5600 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005601 file = fget(fd);
5602 if (!file) {
5603 err = -EBADF;
5604 break;
5605 }
5606 /*
5607 * Don't allow io_uring instances to be registered. If
5608 * UNIX isn't enabled, then this causes a reference
5609 * cycle and this instance can never get freed. If UNIX
5610 * is enabled we'll handle it just fine, but there's
5611 * still no point in allowing a ring fd as it doesn't
5612 * support regular read/write anyway.
5613 */
5614 if (file->f_op == &io_uring_fops) {
5615 fput(file);
5616 err = -EBADF;
5617 break;
5618 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005619 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005620 err = io_sqe_file_register(ctx, file, i);
5621 if (err)
5622 break;
5623 }
5624 nr_args--;
5625 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005626 up->offset++;
5627 }
5628
5629 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5630 percpu_ref_put(&data->refs);
5631 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005632 }
5633
5634 return done ? done : err;
5635}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005636static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5637 unsigned nr_args)
5638{
5639 struct io_uring_files_update up;
5640
5641 if (!ctx->file_data)
5642 return -ENXIO;
5643 if (!nr_args)
5644 return -EINVAL;
5645 if (copy_from_user(&up, arg, sizeof(up)))
5646 return -EFAULT;
5647 if (up.resv)
5648 return -EINVAL;
5649
5650 return __io_sqe_files_update(ctx, &up, nr_args);
5651}
Jens Axboec3a31e62019-10-03 13:59:56 -06005652
Jens Axboe7d723062019-11-12 22:31:31 -07005653static void io_put_work(struct io_wq_work *work)
5654{
5655 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5656
5657 io_put_req(req);
5658}
5659
5660static void io_get_work(struct io_wq_work *work)
5661{
5662 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5663
5664 refcount_inc(&req->refs);
5665}
5666
Jens Axboe6c271ce2019-01-10 11:22:30 -07005667static int io_sq_offload_start(struct io_ring_ctx *ctx,
5668 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005669{
Jens Axboe576a3472019-11-25 08:49:20 -07005670 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005671 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005672 int ret;
5673
Jens Axboe6c271ce2019-01-10 11:22:30 -07005674 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005675 mmgrab(current->mm);
5676 ctx->sqo_mm = current->mm;
5677
Jens Axboe6c271ce2019-01-10 11:22:30 -07005678 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005679 ret = -EPERM;
5680 if (!capable(CAP_SYS_ADMIN))
5681 goto err;
5682
Jens Axboe917257d2019-04-13 09:28:55 -06005683 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5684 if (!ctx->sq_thread_idle)
5685 ctx->sq_thread_idle = HZ;
5686
Jens Axboe6c271ce2019-01-10 11:22:30 -07005687 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005688 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005689
Jens Axboe917257d2019-04-13 09:28:55 -06005690 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005691 if (cpu >= nr_cpu_ids)
5692 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005693 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005694 goto err;
5695
Jens Axboe6c271ce2019-01-10 11:22:30 -07005696 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5697 ctx, cpu,
5698 "io_uring-sq");
5699 } else {
5700 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5701 "io_uring-sq");
5702 }
5703 if (IS_ERR(ctx->sqo_thread)) {
5704 ret = PTR_ERR(ctx->sqo_thread);
5705 ctx->sqo_thread = NULL;
5706 goto err;
5707 }
5708 wake_up_process(ctx->sqo_thread);
5709 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5710 /* Can't have SQ_AFF without SQPOLL */
5711 ret = -EINVAL;
5712 goto err;
5713 }
5714
Jens Axboe576a3472019-11-25 08:49:20 -07005715 data.mm = ctx->sqo_mm;
5716 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005717 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005718 data.get_work = io_get_work;
5719 data.put_work = io_put_work;
5720
Jens Axboe561fb042019-10-24 07:25:42 -06005721 /* Do QD, or 4 * CPUS, whatever is smallest */
5722 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005723 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005724 if (IS_ERR(ctx->io_wq)) {
5725 ret = PTR_ERR(ctx->io_wq);
5726 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005727 goto err;
5728 }
5729
5730 return 0;
5731err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005732 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005733 mmdrop(ctx->sqo_mm);
5734 ctx->sqo_mm = NULL;
5735 return ret;
5736}
5737
5738static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5739{
5740 atomic_long_sub(nr_pages, &user->locked_vm);
5741}
5742
5743static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5744{
5745 unsigned long page_limit, cur_pages, new_pages;
5746
5747 /* Don't allow more pages than we can safely lock */
5748 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5749
5750 do {
5751 cur_pages = atomic_long_read(&user->locked_vm);
5752 new_pages = cur_pages + nr_pages;
5753 if (new_pages > page_limit)
5754 return -ENOMEM;
5755 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5756 new_pages) != cur_pages);
5757
5758 return 0;
5759}
5760
5761static void io_mem_free(void *ptr)
5762{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005763 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005764
Mark Rutland52e04ef2019-04-30 17:30:21 +01005765 if (!ptr)
5766 return;
5767
5768 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005769 if (put_page_testzero(page))
5770 free_compound_page(page);
5771}
5772
5773static void *io_mem_alloc(size_t size)
5774{
5775 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5776 __GFP_NORETRY;
5777
5778 return (void *) __get_free_pages(gfp_flags, get_order(size));
5779}
5780
Hristo Venev75b28af2019-08-26 17:23:46 +00005781static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5782 size_t *sq_offset)
5783{
5784 struct io_rings *rings;
5785 size_t off, sq_array_size;
5786
5787 off = struct_size(rings, cqes, cq_entries);
5788 if (off == SIZE_MAX)
5789 return SIZE_MAX;
5790
5791#ifdef CONFIG_SMP
5792 off = ALIGN(off, SMP_CACHE_BYTES);
5793 if (off == 0)
5794 return SIZE_MAX;
5795#endif
5796
5797 sq_array_size = array_size(sizeof(u32), sq_entries);
5798 if (sq_array_size == SIZE_MAX)
5799 return SIZE_MAX;
5800
5801 if (check_add_overflow(off, sq_array_size, &off))
5802 return SIZE_MAX;
5803
5804 if (sq_offset)
5805 *sq_offset = off;
5806
5807 return off;
5808}
5809
Jens Axboe2b188cc2019-01-07 10:46:33 -07005810static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5811{
Hristo Venev75b28af2019-08-26 17:23:46 +00005812 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005813
Hristo Venev75b28af2019-08-26 17:23:46 +00005814 pages = (size_t)1 << get_order(
5815 rings_size(sq_entries, cq_entries, NULL));
5816 pages += (size_t)1 << get_order(
5817 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005818
Hristo Venev75b28af2019-08-26 17:23:46 +00005819 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005820}
5821
Jens Axboeedafcce2019-01-09 09:16:05 -07005822static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5823{
5824 int i, j;
5825
5826 if (!ctx->user_bufs)
5827 return -ENXIO;
5828
5829 for (i = 0; i < ctx->nr_user_bufs; i++) {
5830 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5831
5832 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005833 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005834
5835 if (ctx->account_mem)
5836 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005837 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005838 imu->nr_bvecs = 0;
5839 }
5840
5841 kfree(ctx->user_bufs);
5842 ctx->user_bufs = NULL;
5843 ctx->nr_user_bufs = 0;
5844 return 0;
5845}
5846
5847static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5848 void __user *arg, unsigned index)
5849{
5850 struct iovec __user *src;
5851
5852#ifdef CONFIG_COMPAT
5853 if (ctx->compat) {
5854 struct compat_iovec __user *ciovs;
5855 struct compat_iovec ciov;
5856
5857 ciovs = (struct compat_iovec __user *) arg;
5858 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5859 return -EFAULT;
5860
Jens Axboed55e5f52019-12-11 16:12:15 -07005861 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005862 dst->iov_len = ciov.iov_len;
5863 return 0;
5864 }
5865#endif
5866 src = (struct iovec __user *) arg;
5867 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5868 return -EFAULT;
5869 return 0;
5870}
5871
5872static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5873 unsigned nr_args)
5874{
5875 struct vm_area_struct **vmas = NULL;
5876 struct page **pages = NULL;
5877 int i, j, got_pages = 0;
5878 int ret = -EINVAL;
5879
5880 if (ctx->user_bufs)
5881 return -EBUSY;
5882 if (!nr_args || nr_args > UIO_MAXIOV)
5883 return -EINVAL;
5884
5885 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5886 GFP_KERNEL);
5887 if (!ctx->user_bufs)
5888 return -ENOMEM;
5889
5890 for (i = 0; i < nr_args; i++) {
5891 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5892 unsigned long off, start, end, ubuf;
5893 int pret, nr_pages;
5894 struct iovec iov;
5895 size_t size;
5896
5897 ret = io_copy_iov(ctx, &iov, arg, i);
5898 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005899 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005900
5901 /*
5902 * Don't impose further limits on the size and buffer
5903 * constraints here, we'll -EINVAL later when IO is
5904 * submitted if they are wrong.
5905 */
5906 ret = -EFAULT;
5907 if (!iov.iov_base || !iov.iov_len)
5908 goto err;
5909
5910 /* arbitrary limit, but we need something */
5911 if (iov.iov_len > SZ_1G)
5912 goto err;
5913
5914 ubuf = (unsigned long) iov.iov_base;
5915 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5916 start = ubuf >> PAGE_SHIFT;
5917 nr_pages = end - start;
5918
5919 if (ctx->account_mem) {
5920 ret = io_account_mem(ctx->user, nr_pages);
5921 if (ret)
5922 goto err;
5923 }
5924
5925 ret = 0;
5926 if (!pages || nr_pages > got_pages) {
5927 kfree(vmas);
5928 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005929 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005930 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005931 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005932 sizeof(struct vm_area_struct *),
5933 GFP_KERNEL);
5934 if (!pages || !vmas) {
5935 ret = -ENOMEM;
5936 if (ctx->account_mem)
5937 io_unaccount_mem(ctx->user, nr_pages);
5938 goto err;
5939 }
5940 got_pages = nr_pages;
5941 }
5942
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005943 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005944 GFP_KERNEL);
5945 ret = -ENOMEM;
5946 if (!imu->bvec) {
5947 if (ctx->account_mem)
5948 io_unaccount_mem(ctx->user, nr_pages);
5949 goto err;
5950 }
5951
5952 ret = 0;
5953 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005954 pret = get_user_pages(ubuf, nr_pages,
5955 FOLL_WRITE | FOLL_LONGTERM,
5956 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005957 if (pret == nr_pages) {
5958 /* don't support file backed memory */
5959 for (j = 0; j < nr_pages; j++) {
5960 struct vm_area_struct *vma = vmas[j];
5961
5962 if (vma->vm_file &&
5963 !is_file_hugepages(vma->vm_file)) {
5964 ret = -EOPNOTSUPP;
5965 break;
5966 }
5967 }
5968 } else {
5969 ret = pret < 0 ? pret : -EFAULT;
5970 }
5971 up_read(&current->mm->mmap_sem);
5972 if (ret) {
5973 /*
5974 * if we did partial map, or found file backed vmas,
5975 * release any pages we did get
5976 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005977 if (pret > 0)
5978 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005979 if (ctx->account_mem)
5980 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005981 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005982 goto err;
5983 }
5984
5985 off = ubuf & ~PAGE_MASK;
5986 size = iov.iov_len;
5987 for (j = 0; j < nr_pages; j++) {
5988 size_t vec_len;
5989
5990 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5991 imu->bvec[j].bv_page = pages[j];
5992 imu->bvec[j].bv_len = vec_len;
5993 imu->bvec[j].bv_offset = off;
5994 off = 0;
5995 size -= vec_len;
5996 }
5997 /* store original address for later verification */
5998 imu->ubuf = ubuf;
5999 imu->len = iov.iov_len;
6000 imu->nr_bvecs = nr_pages;
6001
6002 ctx->nr_user_bufs++;
6003 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006004 kvfree(pages);
6005 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006006 return 0;
6007err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006008 kvfree(pages);
6009 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006010 io_sqe_buffer_unregister(ctx);
6011 return ret;
6012}
6013
Jens Axboe9b402842019-04-11 11:45:41 -06006014static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6015{
6016 __s32 __user *fds = arg;
6017 int fd;
6018
6019 if (ctx->cq_ev_fd)
6020 return -EBUSY;
6021
6022 if (copy_from_user(&fd, fds, sizeof(*fds)))
6023 return -EFAULT;
6024
6025 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6026 if (IS_ERR(ctx->cq_ev_fd)) {
6027 int ret = PTR_ERR(ctx->cq_ev_fd);
6028 ctx->cq_ev_fd = NULL;
6029 return ret;
6030 }
6031
6032 return 0;
6033}
6034
6035static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6036{
6037 if (ctx->cq_ev_fd) {
6038 eventfd_ctx_put(ctx->cq_ev_fd);
6039 ctx->cq_ev_fd = NULL;
6040 return 0;
6041 }
6042
6043 return -ENXIO;
6044}
6045
Jens Axboe2b188cc2019-01-07 10:46:33 -07006046static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6047{
Jens Axboe6b063142019-01-10 22:13:58 -07006048 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006049 if (ctx->sqo_mm)
6050 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006051
6052 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006053 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006054 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006055 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006056
Jens Axboe2b188cc2019-01-07 10:46:33 -07006057#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006058 if (ctx->ring_sock) {
6059 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006060 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006061 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006062#endif
6063
Hristo Venev75b28af2019-08-26 17:23:46 +00006064 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006065 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006066
6067 percpu_ref_exit(&ctx->refs);
6068 if (ctx->account_mem)
6069 io_unaccount_mem(ctx->user,
6070 ring_pages(ctx->sq_entries, ctx->cq_entries));
6071 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006072 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006073 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006074 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006075 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006076 kfree(ctx);
6077}
6078
6079static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6080{
6081 struct io_ring_ctx *ctx = file->private_data;
6082 __poll_t mask = 0;
6083
6084 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006085 /*
6086 * synchronizes with barrier from wq_has_sleeper call in
6087 * io_commit_cqring
6088 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006089 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006090 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6091 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006092 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006093 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006094 mask |= EPOLLIN | EPOLLRDNORM;
6095
6096 return mask;
6097}
6098
6099static int io_uring_fasync(int fd, struct file *file, int on)
6100{
6101 struct io_ring_ctx *ctx = file->private_data;
6102
6103 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6104}
6105
6106static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6107{
6108 mutex_lock(&ctx->uring_lock);
6109 percpu_ref_kill(&ctx->refs);
6110 mutex_unlock(&ctx->uring_lock);
6111
Jens Axboe5262f562019-09-17 12:26:57 -06006112 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006113 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006114
6115 if (ctx->io_wq)
6116 io_wq_cancel_all(ctx->io_wq);
6117
Jens Axboedef596e2019-01-09 08:59:42 -07006118 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006119 /* if we failed setting up the ctx, we might not have any rings */
6120 if (ctx->rings)
6121 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07006122 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006123 io_ring_ctx_free(ctx);
6124}
6125
6126static int io_uring_release(struct inode *inode, struct file *file)
6127{
6128 struct io_ring_ctx *ctx = file->private_data;
6129
6130 file->private_data = NULL;
6131 io_ring_ctx_wait_and_kill(ctx);
6132 return 0;
6133}
6134
Jens Axboefcb323c2019-10-24 12:39:47 -06006135static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6136 struct files_struct *files)
6137{
6138 struct io_kiocb *req;
6139 DEFINE_WAIT(wait);
6140
6141 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006142 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006143
6144 spin_lock_irq(&ctx->inflight_lock);
6145 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006146 if (req->work.files != files)
6147 continue;
6148 /* req is being completed, ignore */
6149 if (!refcount_inc_not_zero(&req->refs))
6150 continue;
6151 cancel_req = req;
6152 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006153 }
Jens Axboe768134d2019-11-10 20:30:53 -07006154 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006155 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006156 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006157 spin_unlock_irq(&ctx->inflight_lock);
6158
Jens Axboe768134d2019-11-10 20:30:53 -07006159 /* We need to keep going until we don't find a matching req */
6160 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006161 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006162
6163 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6164 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006165 schedule();
6166 }
Jens Axboe768134d2019-11-10 20:30:53 -07006167 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006168}
6169
6170static int io_uring_flush(struct file *file, void *data)
6171{
6172 struct io_ring_ctx *ctx = file->private_data;
6173
6174 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006175 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6176 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006177 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006178 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006179 return 0;
6180}
6181
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006182static void *io_uring_validate_mmap_request(struct file *file,
6183 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006184{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006185 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006186 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006187 struct page *page;
6188 void *ptr;
6189
6190 switch (offset) {
6191 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006192 case IORING_OFF_CQ_RING:
6193 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006194 break;
6195 case IORING_OFF_SQES:
6196 ptr = ctx->sq_sqes;
6197 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006198 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006199 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006200 }
6201
6202 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006203 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006204 return ERR_PTR(-EINVAL);
6205
6206 return ptr;
6207}
6208
6209#ifdef CONFIG_MMU
6210
6211static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6212{
6213 size_t sz = vma->vm_end - vma->vm_start;
6214 unsigned long pfn;
6215 void *ptr;
6216
6217 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6218 if (IS_ERR(ptr))
6219 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006220
6221 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6222 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6223}
6224
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006225#else /* !CONFIG_MMU */
6226
6227static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6228{
6229 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6230}
6231
6232static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6233{
6234 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6235}
6236
6237static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6238 unsigned long addr, unsigned long len,
6239 unsigned long pgoff, unsigned long flags)
6240{
6241 void *ptr;
6242
6243 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6244 if (IS_ERR(ptr))
6245 return PTR_ERR(ptr);
6246
6247 return (unsigned long) ptr;
6248}
6249
6250#endif /* !CONFIG_MMU */
6251
Jens Axboe2b188cc2019-01-07 10:46:33 -07006252SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6253 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6254 size_t, sigsz)
6255{
6256 struct io_ring_ctx *ctx;
6257 long ret = -EBADF;
6258 int submitted = 0;
6259 struct fd f;
6260
Jens Axboe6c271ce2019-01-10 11:22:30 -07006261 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006262 return -EINVAL;
6263
6264 f = fdget(fd);
6265 if (!f.file)
6266 return -EBADF;
6267
6268 ret = -EOPNOTSUPP;
6269 if (f.file->f_op != &io_uring_fops)
6270 goto out_fput;
6271
6272 ret = -ENXIO;
6273 ctx = f.file->private_data;
6274 if (!percpu_ref_tryget(&ctx->refs))
6275 goto out_fput;
6276
Jens Axboe6c271ce2019-01-10 11:22:30 -07006277 /*
6278 * For SQ polling, the thread will do all submissions and completions.
6279 * Just return the requested submit count, and wake the thread if
6280 * we were asked to.
6281 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006282 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006283 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006284 if (!list_empty_careful(&ctx->cq_overflow_list))
6285 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006286 if (flags & IORING_ENTER_SQ_WAKEUP)
6287 wake_up(&ctx->sqo_wait);
6288 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006289 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006290 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006291
Jens Axboe44d28272020-01-16 19:00:24 -07006292 if (current->mm != ctx->sqo_mm ||
6293 current_cred() != ctx->creds) {
6294 ret = -EPERM;
6295 goto out;
6296 }
6297
Jens Axboe2b188cc2019-01-07 10:46:33 -07006298 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006299 /* already have mm, so io_submit_sqes() won't try to grab it */
6300 cur_mm = ctx->sqo_mm;
6301 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6302 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006303 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006304
6305 if (submitted != to_submit)
6306 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006307 }
6308 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006309 unsigned nr_events = 0;
6310
Jens Axboe2b188cc2019-01-07 10:46:33 -07006311 min_complete = min(min_complete, ctx->cq_entries);
6312
Jens Axboedef596e2019-01-09 08:59:42 -07006313 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006314 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006315 } else {
6316 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6317 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006318 }
6319
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006320out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006321 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006322out_fput:
6323 fdput(f);
6324 return submitted ? submitted : ret;
6325}
6326
6327static const struct file_operations io_uring_fops = {
6328 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006329 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006330 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006331#ifndef CONFIG_MMU
6332 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6333 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6334#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006335 .poll = io_uring_poll,
6336 .fasync = io_uring_fasync,
6337};
6338
6339static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6340 struct io_uring_params *p)
6341{
Hristo Venev75b28af2019-08-26 17:23:46 +00006342 struct io_rings *rings;
6343 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006344
Hristo Venev75b28af2019-08-26 17:23:46 +00006345 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6346 if (size == SIZE_MAX)
6347 return -EOVERFLOW;
6348
6349 rings = io_mem_alloc(size);
6350 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006351 return -ENOMEM;
6352
Hristo Venev75b28af2019-08-26 17:23:46 +00006353 ctx->rings = rings;
6354 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6355 rings->sq_ring_mask = p->sq_entries - 1;
6356 rings->cq_ring_mask = p->cq_entries - 1;
6357 rings->sq_ring_entries = p->sq_entries;
6358 rings->cq_ring_entries = p->cq_entries;
6359 ctx->sq_mask = rings->sq_ring_mask;
6360 ctx->cq_mask = rings->cq_ring_mask;
6361 ctx->sq_entries = rings->sq_ring_entries;
6362 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006363
6364 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006365 if (size == SIZE_MAX) {
6366 io_mem_free(ctx->rings);
6367 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006368 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006369 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006370
6371 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006372 if (!ctx->sq_sqes) {
6373 io_mem_free(ctx->rings);
6374 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006375 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006376 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006377
Jens Axboe2b188cc2019-01-07 10:46:33 -07006378 return 0;
6379}
6380
6381/*
6382 * Allocate an anonymous fd, this is what constitutes the application
6383 * visible backing of an io_uring instance. The application mmaps this
6384 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6385 * we have to tie this fd to a socket for file garbage collection purposes.
6386 */
6387static int io_uring_get_fd(struct io_ring_ctx *ctx)
6388{
6389 struct file *file;
6390 int ret;
6391
6392#if defined(CONFIG_UNIX)
6393 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6394 &ctx->ring_sock);
6395 if (ret)
6396 return ret;
6397#endif
6398
6399 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6400 if (ret < 0)
6401 goto err;
6402
6403 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6404 O_RDWR | O_CLOEXEC);
6405 if (IS_ERR(file)) {
6406 put_unused_fd(ret);
6407 ret = PTR_ERR(file);
6408 goto err;
6409 }
6410
6411#if defined(CONFIG_UNIX)
6412 ctx->ring_sock->file = file;
6413#endif
6414 fd_install(ret, file);
6415 return ret;
6416err:
6417#if defined(CONFIG_UNIX)
6418 sock_release(ctx->ring_sock);
6419 ctx->ring_sock = NULL;
6420#endif
6421 return ret;
6422}
6423
6424static int io_uring_create(unsigned entries, struct io_uring_params *p)
6425{
6426 struct user_struct *user = NULL;
6427 struct io_ring_ctx *ctx;
6428 bool account_mem;
6429 int ret;
6430
Jens Axboe8110c1a2019-12-28 15:39:54 -07006431 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006432 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006433 if (entries > IORING_MAX_ENTRIES) {
6434 if (!(p->flags & IORING_SETUP_CLAMP))
6435 return -EINVAL;
6436 entries = IORING_MAX_ENTRIES;
6437 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006438
6439 /*
6440 * Use twice as many entries for the CQ ring. It's possible for the
6441 * application to drive a higher depth than the size of the SQ ring,
6442 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006443 * some flexibility in overcommitting a bit. If the application has
6444 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6445 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006446 */
6447 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006448 if (p->flags & IORING_SETUP_CQSIZE) {
6449 /*
6450 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6451 * to a power-of-two, if it isn't already. We do NOT impose
6452 * any cq vs sq ring sizing.
6453 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006454 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006455 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006456 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6457 if (!(p->flags & IORING_SETUP_CLAMP))
6458 return -EINVAL;
6459 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6460 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006461 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6462 } else {
6463 p->cq_entries = 2 * p->sq_entries;
6464 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006465
6466 user = get_uid(current_user());
6467 account_mem = !capable(CAP_IPC_LOCK);
6468
6469 if (account_mem) {
6470 ret = io_account_mem(user,
6471 ring_pages(p->sq_entries, p->cq_entries));
6472 if (ret) {
6473 free_uid(user);
6474 return ret;
6475 }
6476 }
6477
6478 ctx = io_ring_ctx_alloc(p);
6479 if (!ctx) {
6480 if (account_mem)
6481 io_unaccount_mem(user, ring_pages(p->sq_entries,
6482 p->cq_entries));
6483 free_uid(user);
6484 return -ENOMEM;
6485 }
6486 ctx->compat = in_compat_syscall();
6487 ctx->account_mem = account_mem;
6488 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006489 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006490
6491 ret = io_allocate_scq_urings(ctx, p);
6492 if (ret)
6493 goto err;
6494
Jens Axboe6c271ce2019-01-10 11:22:30 -07006495 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006496 if (ret)
6497 goto err;
6498
Jens Axboe2b188cc2019-01-07 10:46:33 -07006499 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006500 p->sq_off.head = offsetof(struct io_rings, sq.head);
6501 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6502 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6503 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6504 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6505 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6506 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006507
6508 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006509 p->cq_off.head = offsetof(struct io_rings, cq.head);
6510 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6511 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6512 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6513 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6514 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006515
Jens Axboe044c1ab2019-10-28 09:15:33 -06006516 /*
6517 * Install ring fd as the very last thing, so we don't risk someone
6518 * having closed it before we finish setup
6519 */
6520 ret = io_uring_get_fd(ctx);
6521 if (ret < 0)
6522 goto err;
6523
Jens Axboeda8c9692019-12-02 18:51:26 -07006524 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboeba042912019-12-25 16:33:42 -07006525 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006526 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006527 return ret;
6528err:
6529 io_ring_ctx_wait_and_kill(ctx);
6530 return ret;
6531}
6532
6533/*
6534 * Sets up an aio uring context, and returns the fd. Applications asks for a
6535 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6536 * params structure passed in.
6537 */
6538static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6539{
6540 struct io_uring_params p;
6541 long ret;
6542 int i;
6543
6544 if (copy_from_user(&p, params, sizeof(p)))
6545 return -EFAULT;
6546 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6547 if (p.resv[i])
6548 return -EINVAL;
6549 }
6550
Jens Axboe6c271ce2019-01-10 11:22:30 -07006551 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006552 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
6553 IORING_SETUP_CLAMP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006554 return -EINVAL;
6555
6556 ret = io_uring_create(entries, &p);
6557 if (ret < 0)
6558 return ret;
6559
6560 if (copy_to_user(params, &p, sizeof(p)))
6561 return -EFAULT;
6562
6563 return ret;
6564}
6565
6566SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6567 struct io_uring_params __user *, params)
6568{
6569 return io_uring_setup(entries, params);
6570}
6571
Jens Axboe66f4af92020-01-16 15:36:52 -07006572static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6573{
6574 struct io_uring_probe *p;
6575 size_t size;
6576 int i, ret;
6577
6578 size = struct_size(p, ops, nr_args);
6579 if (size == SIZE_MAX)
6580 return -EOVERFLOW;
6581 p = kzalloc(size, GFP_KERNEL);
6582 if (!p)
6583 return -ENOMEM;
6584
6585 ret = -EFAULT;
6586 if (copy_from_user(p, arg, size))
6587 goto out;
6588 ret = -EINVAL;
6589 if (memchr_inv(p, 0, size))
6590 goto out;
6591
6592 p->last_op = IORING_OP_LAST - 1;
6593 if (nr_args > IORING_OP_LAST)
6594 nr_args = IORING_OP_LAST;
6595
6596 for (i = 0; i < nr_args; i++) {
6597 p->ops[i].op = i;
6598 if (!io_op_defs[i].not_supported)
6599 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6600 }
6601 p->ops_len = i;
6602
6603 ret = 0;
6604 if (copy_to_user(arg, p, size))
6605 ret = -EFAULT;
6606out:
6607 kfree(p);
6608 return ret;
6609}
6610
Jens Axboeedafcce2019-01-09 09:16:05 -07006611static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6612 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006613 __releases(ctx->uring_lock)
6614 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006615{
6616 int ret;
6617
Jens Axboe35fa71a2019-04-22 10:23:23 -06006618 /*
6619 * We're inside the ring mutex, if the ref is already dying, then
6620 * someone else killed the ctx or is already going through
6621 * io_uring_register().
6622 */
6623 if (percpu_ref_is_dying(&ctx->refs))
6624 return -ENXIO;
6625
Jens Axboe05f3fb32019-12-09 11:22:50 -07006626 if (opcode != IORING_UNREGISTER_FILES &&
Jens Axboe66f4af92020-01-16 15:36:52 -07006627 opcode != IORING_REGISTER_FILES_UPDATE &&
6628 opcode != IORING_REGISTER_PROBE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006629 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006630
Jens Axboe05f3fb32019-12-09 11:22:50 -07006631 /*
6632 * Drop uring mutex before waiting for references to exit. If
6633 * another thread is currently inside io_uring_enter() it might
6634 * need to grab the uring_lock to make progress. If we hold it
6635 * here across the drain wait, then we can deadlock. It's safe
6636 * to drop the mutex here, since no new references will come in
6637 * after we've killed the percpu ref.
6638 */
6639 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006640 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006641 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006642 if (ret) {
6643 percpu_ref_resurrect(&ctx->refs);
6644 ret = -EINTR;
6645 goto out;
6646 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006647 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006648
6649 switch (opcode) {
6650 case IORING_REGISTER_BUFFERS:
6651 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6652 break;
6653 case IORING_UNREGISTER_BUFFERS:
6654 ret = -EINVAL;
6655 if (arg || nr_args)
6656 break;
6657 ret = io_sqe_buffer_unregister(ctx);
6658 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006659 case IORING_REGISTER_FILES:
6660 ret = io_sqe_files_register(ctx, arg, nr_args);
6661 break;
6662 case IORING_UNREGISTER_FILES:
6663 ret = -EINVAL;
6664 if (arg || nr_args)
6665 break;
6666 ret = io_sqe_files_unregister(ctx);
6667 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006668 case IORING_REGISTER_FILES_UPDATE:
6669 ret = io_sqe_files_update(ctx, arg, nr_args);
6670 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006671 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006672 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006673 ret = -EINVAL;
6674 if (nr_args != 1)
6675 break;
6676 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006677 if (ret)
6678 break;
6679 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6680 ctx->eventfd_async = 1;
6681 else
6682 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006683 break;
6684 case IORING_UNREGISTER_EVENTFD:
6685 ret = -EINVAL;
6686 if (arg || nr_args)
6687 break;
6688 ret = io_eventfd_unregister(ctx);
6689 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07006690 case IORING_REGISTER_PROBE:
6691 ret = -EINVAL;
6692 if (!arg || nr_args > 256)
6693 break;
6694 ret = io_probe(ctx, arg, nr_args);
6695 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006696 default:
6697 ret = -EINVAL;
6698 break;
6699 }
6700
Jens Axboe05f3fb32019-12-09 11:22:50 -07006701
6702 if (opcode != IORING_UNREGISTER_FILES &&
Jens Axboe66f4af92020-01-16 15:36:52 -07006703 opcode != IORING_REGISTER_FILES_UPDATE &&
6704 opcode != IORING_REGISTER_PROBE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006705 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006706 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07006707out:
6708 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006709 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006710 return ret;
6711}
6712
6713SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6714 void __user *, arg, unsigned int, nr_args)
6715{
6716 struct io_ring_ctx *ctx;
6717 long ret = -EBADF;
6718 struct fd f;
6719
6720 f = fdget(fd);
6721 if (!f.file)
6722 return -EBADF;
6723
6724 ret = -EOPNOTSUPP;
6725 if (f.file->f_op != &io_uring_fops)
6726 goto out_fput;
6727
6728 ctx = f.file->private_data;
6729
6730 mutex_lock(&ctx->uring_lock);
6731 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6732 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006733 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6734 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006735out_fput:
6736 fdput(f);
6737 return ret;
6738}
6739
Jens Axboe2b188cc2019-01-07 10:46:33 -07006740static int __init io_uring_init(void)
6741{
Jens Axboed3656342019-12-18 09:50:26 -07006742 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006743 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6744 return 0;
6745};
6746__initcall(io_uring_init);