blob: 771954bb4c38a291f75c51fb27a3196e3481a895 [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
Pavel Begunkov07910152020-01-17 03:52:46 +0300863 /* order cqe stores with ring update */
864 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700865
Pavel Begunkov07910152020-01-17 03:52:46 +0300866 if (wq_has_sleeper(&ctx->cq_wait)) {
867 wake_up_interruptible(&ctx->cq_wait);
868 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700869 }
870}
871
Jens Axboe94ae5e72019-11-14 19:39:52 -0700872static inline bool io_prep_async_work(struct io_kiocb *req,
873 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600874{
Jens Axboed3656342019-12-18 09:50:26 -0700875 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600876 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600877
Jens Axboed3656342019-12-18 09:50:26 -0700878 if (req->flags & REQ_F_ISREG) {
879 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700880 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700881 } else {
882 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700883 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600884 }
Jens Axboed3656342019-12-18 09:50:26 -0700885 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700886 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600887
Jens Axboe94ae5e72019-11-14 19:39:52 -0700888 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600889 return do_hashed;
890}
891
Jackie Liua197f662019-11-08 08:09:12 -0700892static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600893{
Jackie Liua197f662019-11-08 08:09:12 -0700894 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700895 struct io_kiocb *link;
896 bool do_hashed;
897
898 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600899
900 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
901 req->flags);
902 if (!do_hashed) {
903 io_wq_enqueue(ctx->io_wq, &req->work);
904 } else {
905 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
906 file_inode(req->file));
907 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700908
909 if (link)
910 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600911}
912
Jens Axboe5262f562019-09-17 12:26:57 -0600913static void io_kill_timeout(struct io_kiocb *req)
914{
915 int ret;
916
Jens Axboe2d283902019-12-04 11:08:05 -0700917 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600918 if (ret != -1) {
919 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600920 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700921 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800922 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600923 }
924}
925
926static void io_kill_timeouts(struct io_ring_ctx *ctx)
927{
928 struct io_kiocb *req, *tmp;
929
930 spin_lock_irq(&ctx->completion_lock);
931 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
932 io_kill_timeout(req);
933 spin_unlock_irq(&ctx->completion_lock);
934}
935
Jens Axboede0617e2019-04-06 21:51:27 -0600936static void io_commit_cqring(struct io_ring_ctx *ctx)
937{
938 struct io_kiocb *req;
939
Jens Axboe5262f562019-09-17 12:26:57 -0600940 while ((req = io_get_timeout_req(ctx)) != NULL)
941 io_kill_timeout(req);
942
Jens Axboede0617e2019-04-06 21:51:27 -0600943 __io_commit_cqring(ctx);
944
945 while ((req = io_get_deferred_req(ctx)) != NULL) {
946 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700947 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600948 }
949}
950
Jens Axboe2b188cc2019-01-07 10:46:33 -0700951static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
952{
Hristo Venev75b28af2019-08-26 17:23:46 +0000953 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700954 unsigned tail;
955
956 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200957 /*
958 * writes to the cq entry need to come after reading head; the
959 * control dependency is enough as we're using WRITE_ONCE to
960 * fill the cq entry
961 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000962 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700963 return NULL;
964
965 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000966 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700967}
968
Jens Axboef2842ab2020-01-08 11:04:00 -0700969static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
970{
971 if (!ctx->eventfd_async)
972 return true;
973 return io_wq_current_is_worker() || in_interrupt();
974}
975
Jens Axboe8c838782019-03-12 15:48:16 -0600976static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
977{
978 if (waitqueue_active(&ctx->wait))
979 wake_up(&ctx->wait);
980 if (waitqueue_active(&ctx->sqo_wait))
981 wake_up(&ctx->sqo_wait);
Jens Axboef2842ab2020-01-08 11:04:00 -0700982 if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -0600983 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600984}
985
Jens Axboec4a2ed72019-11-21 21:01:26 -0700986/* Returns true if there are no backlogged entries after the flush */
987static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700988{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700989 struct io_rings *rings = ctx->rings;
990 struct io_uring_cqe *cqe;
991 struct io_kiocb *req;
992 unsigned long flags;
993 LIST_HEAD(list);
994
995 if (!force) {
996 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700997 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700998 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
999 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001000 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001001 }
1002
1003 spin_lock_irqsave(&ctx->completion_lock, flags);
1004
1005 /* if force is set, the ring is going away. always drop after that */
1006 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001007 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001008
Jens Axboec4a2ed72019-11-21 21:01:26 -07001009 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001010 while (!list_empty(&ctx->cq_overflow_list)) {
1011 cqe = io_get_cqring(ctx);
1012 if (!cqe && !force)
1013 break;
1014
1015 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1016 list);
1017 list_move(&req->list, &list);
1018 if (cqe) {
1019 WRITE_ONCE(cqe->user_data, req->user_data);
1020 WRITE_ONCE(cqe->res, req->result);
1021 WRITE_ONCE(cqe->flags, 0);
1022 } else {
1023 WRITE_ONCE(ctx->rings->cq_overflow,
1024 atomic_inc_return(&ctx->cached_cq_overflow));
1025 }
1026 }
1027
1028 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001029 if (cqe) {
1030 clear_bit(0, &ctx->sq_check_overflow);
1031 clear_bit(0, &ctx->cq_check_overflow);
1032 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001033 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1034 io_cqring_ev_posted(ctx);
1035
1036 while (!list_empty(&list)) {
1037 req = list_first_entry(&list, struct io_kiocb, list);
1038 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001039 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001040 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001041
1042 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001043}
1044
Jens Axboe78e19bb2019-11-06 15:21:34 -07001045static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001046{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001047 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001048 struct io_uring_cqe *cqe;
1049
Jens Axboe78e19bb2019-11-06 15:21:34 -07001050 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001051
Jens Axboe2b188cc2019-01-07 10:46:33 -07001052 /*
1053 * If we can't get a cq entry, userspace overflowed the
1054 * submission (by quite a lot). Increment the overflow count in
1055 * the ring.
1056 */
1057 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001058 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001059 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001060 WRITE_ONCE(cqe->res, res);
1061 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001062 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001063 WRITE_ONCE(ctx->rings->cq_overflow,
1064 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001065 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001066 if (list_empty(&ctx->cq_overflow_list)) {
1067 set_bit(0, &ctx->sq_check_overflow);
1068 set_bit(0, &ctx->cq_check_overflow);
1069 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001070 refcount_inc(&req->refs);
1071 req->result = res;
1072 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001073 }
1074}
1075
Jens Axboe78e19bb2019-11-06 15:21:34 -07001076static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001077{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001078 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001079 unsigned long flags;
1080
1081 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001082 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001083 io_commit_cqring(ctx);
1084 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1085
Jens Axboe8c838782019-03-12 15:48:16 -06001086 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001087}
1088
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001089static inline bool io_is_fallback_req(struct io_kiocb *req)
1090{
1091 return req == (struct io_kiocb *)
1092 ((unsigned long) req->ctx->fallback_req & ~1UL);
1093}
1094
1095static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1096{
1097 struct io_kiocb *req;
1098
1099 req = ctx->fallback_req;
1100 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1101 return req;
1102
1103 return NULL;
1104}
1105
Jens Axboe2579f912019-01-09 09:10:43 -07001106static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1107 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001108{
Jens Axboefd6fab22019-03-14 16:30:06 -06001109 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001110 struct io_kiocb *req;
1111
Jens Axboe2579f912019-01-09 09:10:43 -07001112 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001113 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001114 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001115 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001116 } else if (!state->free_reqs) {
1117 size_t sz;
1118 int ret;
1119
1120 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001121 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1122
1123 /*
1124 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1125 * retry single alloc to be on the safe side.
1126 */
1127 if (unlikely(ret <= 0)) {
1128 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1129 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001130 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001131 ret = 1;
1132 }
Jens Axboe2579f912019-01-09 09:10:43 -07001133 state->free_reqs = ret - 1;
1134 state->cur_req = 1;
1135 req = state->reqs[0];
1136 } else {
1137 req = state->reqs[state->cur_req];
1138 state->free_reqs--;
1139 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001140 }
1141
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001142got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001143 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001144 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001145 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001146 req->ctx = ctx;
1147 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001148 /* one is dropped after submission, the other at completion */
1149 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001150 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001151 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001152 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001153fallback:
1154 req = io_get_fallback_req(ctx);
1155 if (req)
1156 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001157 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001158 return NULL;
1159}
1160
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001161static void __io_req_do_free(struct io_kiocb *req)
1162{
1163 if (likely(!io_is_fallback_req(req)))
1164 kmem_cache_free(req_cachep, req);
1165 else
1166 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1167}
1168
Jens Axboec6ca97b302019-12-28 12:11:08 -07001169static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001170{
Jens Axboefcb323c2019-10-24 12:39:47 -06001171 struct io_ring_ctx *ctx = req->ctx;
1172
YueHaibing96fd84d2020-01-07 22:22:44 +08001173 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001174 if (req->file) {
1175 if (req->flags & REQ_F_FIXED_FILE)
1176 percpu_ref_put(&ctx->file_data->refs);
1177 else
1178 fput(req->file);
1179 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001180}
1181
1182static void __io_free_req(struct io_kiocb *req)
1183{
1184 __io_req_aux_free(req);
1185
Jens Axboefcb323c2019-10-24 12:39:47 -06001186 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001187 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001188 unsigned long flags;
1189
1190 spin_lock_irqsave(&ctx->inflight_lock, flags);
1191 list_del(&req->inflight_entry);
1192 if (waitqueue_active(&ctx->inflight_wait))
1193 wake_up(&ctx->inflight_wait);
1194 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1195 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001196
1197 percpu_ref_put(&req->ctx->refs);
1198 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001199}
1200
Jens Axboec6ca97b302019-12-28 12:11:08 -07001201struct req_batch {
1202 void *reqs[IO_IOPOLL_BATCH];
1203 int to_free;
1204 int need_iter;
1205};
1206
1207static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1208{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001209 int fixed_refs = rb->to_free;
1210
Jens Axboec6ca97b302019-12-28 12:11:08 -07001211 if (!rb->to_free)
1212 return;
1213 if (rb->need_iter) {
1214 int i, inflight = 0;
1215 unsigned long flags;
1216
Jens Axboe10fef4b2020-01-09 07:52:28 -07001217 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001218 for (i = 0; i < rb->to_free; i++) {
1219 struct io_kiocb *req = rb->reqs[i];
1220
Jens Axboe10fef4b2020-01-09 07:52:28 -07001221 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001222 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001223 fixed_refs++;
1224 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001225 if (req->flags & REQ_F_INFLIGHT)
1226 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001227 __io_req_aux_free(req);
1228 }
1229 if (!inflight)
1230 goto do_free;
1231
1232 spin_lock_irqsave(&ctx->inflight_lock, flags);
1233 for (i = 0; i < rb->to_free; i++) {
1234 struct io_kiocb *req = rb->reqs[i];
1235
Jens Axboe10fef4b2020-01-09 07:52:28 -07001236 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001237 list_del(&req->inflight_entry);
1238 if (!--inflight)
1239 break;
1240 }
1241 }
1242 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1243
1244 if (waitqueue_active(&ctx->inflight_wait))
1245 wake_up(&ctx->inflight_wait);
1246 }
1247do_free:
1248 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001249 if (fixed_refs)
1250 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001251 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001252 rb->to_free = rb->need_iter = 0;
1253}
1254
Jackie Liua197f662019-11-08 08:09:12 -07001255static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001256{
Jackie Liua197f662019-11-08 08:09:12 -07001257 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001258 int ret;
1259
Jens Axboe2d283902019-12-04 11:08:05 -07001260 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001261 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001262 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001263 io_commit_cqring(ctx);
1264 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001265 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001266 return true;
1267 }
1268
1269 return false;
1270}
1271
Jens Axboeba816ad2019-09-28 11:36:45 -06001272static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001273{
Jens Axboe2665abf2019-11-05 12:40:47 -07001274 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001275 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001276
Jens Axboe4d7dd462019-11-20 13:03:52 -07001277 /* Already got next link */
1278 if (req->flags & REQ_F_LINK_NEXT)
1279 return;
1280
Jens Axboe9e645e112019-05-10 16:07:28 -06001281 /*
1282 * The list should never be empty when we are called here. But could
1283 * potentially happen if the chain is messed up, check to be on the
1284 * safe side.
1285 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001286 while (!list_empty(&req->link_list)) {
1287 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1288 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001289
Pavel Begunkov44932332019-12-05 16:16:35 +03001290 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1291 (nxt->flags & REQ_F_TIMEOUT))) {
1292 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001293 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001294 req->flags &= ~REQ_F_LINK_TIMEOUT;
1295 continue;
1296 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001297
Pavel Begunkov44932332019-12-05 16:16:35 +03001298 list_del_init(&req->link_list);
1299 if (!list_empty(&nxt->link_list))
1300 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001301 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001302 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001303 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001304
Jens Axboe4d7dd462019-11-20 13:03:52 -07001305 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001306 if (wake_ev)
1307 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001308}
1309
1310/*
1311 * Called if REQ_F_LINK is set, and we fail the head request
1312 */
1313static void io_fail_links(struct io_kiocb *req)
1314{
Jens Axboe2665abf2019-11-05 12:40:47 -07001315 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001316 unsigned long flags;
1317
1318 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001319
1320 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001321 struct io_kiocb *link = list_first_entry(&req->link_list,
1322 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001323
Pavel Begunkov44932332019-12-05 16:16:35 +03001324 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001325 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001326
1327 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001328 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001329 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001330 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001331 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001332 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001333 }
Jens Axboe5d960722019-11-19 15:31:28 -07001334 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001335 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001336
1337 io_commit_cqring(ctx);
1338 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1339 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001340}
1341
Jens Axboe4d7dd462019-11-20 13:03:52 -07001342static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001343{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001344 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001345 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001346
Jens Axboe9e645e112019-05-10 16:07:28 -06001347 /*
1348 * If LINK is set, we have dependent requests in this chain. If we
1349 * didn't fail this request, queue the first one up, moving any other
1350 * dependencies to the next request. In case of failure, fail the rest
1351 * of the chain.
1352 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001353 if (req->flags & REQ_F_FAIL_LINK) {
1354 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001355 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1356 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001357 struct io_ring_ctx *ctx = req->ctx;
1358 unsigned long flags;
1359
1360 /*
1361 * If this is a timeout link, we could be racing with the
1362 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001363 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001364 */
1365 spin_lock_irqsave(&ctx->completion_lock, flags);
1366 io_req_link_next(req, nxt);
1367 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1368 } else {
1369 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001370 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001371}
Jens Axboe9e645e112019-05-10 16:07:28 -06001372
Jackie Liuc69f8db2019-11-09 11:00:08 +08001373static void io_free_req(struct io_kiocb *req)
1374{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001375 struct io_kiocb *nxt = NULL;
1376
1377 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001378 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001379
1380 if (nxt)
1381 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001382}
1383
Jens Axboeba816ad2019-09-28 11:36:45 -06001384/*
1385 * Drop reference to request, return next in chain (if there is one) if this
1386 * was the last reference to this request.
1387 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001388__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001389static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001390{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001391 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001392
Jens Axboee65ef562019-03-12 10:16:44 -06001393 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001394 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001395}
1396
Jens Axboe2b188cc2019-01-07 10:46:33 -07001397static void io_put_req(struct io_kiocb *req)
1398{
Jens Axboedef596e2019-01-09 08:59:42 -07001399 if (refcount_dec_and_test(&req->refs))
1400 io_free_req(req);
1401}
1402
Jens Axboe978db572019-11-14 22:39:04 -07001403/*
1404 * Must only be used if we don't need to care about links, usually from
1405 * within the completion handling itself.
1406 */
1407static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001408{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001409 /* drop both submit and complete references */
1410 if (refcount_sub_and_test(2, &req->refs))
1411 __io_free_req(req);
1412}
1413
Jens Axboe978db572019-11-14 22:39:04 -07001414static void io_double_put_req(struct io_kiocb *req)
1415{
1416 /* drop both submit and complete references */
1417 if (refcount_sub_and_test(2, &req->refs))
1418 io_free_req(req);
1419}
1420
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001421static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001422{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001423 struct io_rings *rings = ctx->rings;
1424
Jens Axboead3eb2c2019-12-18 17:12:20 -07001425 if (test_bit(0, &ctx->cq_check_overflow)) {
1426 /*
1427 * noflush == true is from the waitqueue handler, just ensure
1428 * we wake up the task, and the next invocation will flush the
1429 * entries. We cannot safely to it from here.
1430 */
1431 if (noflush && !list_empty(&ctx->cq_overflow_list))
1432 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001433
Jens Axboead3eb2c2019-12-18 17:12:20 -07001434 io_cqring_overflow_flush(ctx, false);
1435 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001436
Jens Axboea3a0e432019-08-20 11:03:11 -06001437 /* See comment at the top of this file */
1438 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001439 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001440}
1441
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001442static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1443{
1444 struct io_rings *rings = ctx->rings;
1445
1446 /* make sure SQ entry isn't read before tail */
1447 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1448}
1449
Jens Axboe8237e042019-12-28 10:48:22 -07001450static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001451{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001452 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1453 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001454
Jens Axboec6ca97b302019-12-28 12:11:08 -07001455 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1456 rb->need_iter++;
1457
1458 rb->reqs[rb->to_free++] = req;
1459 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1460 io_free_req_many(req->ctx, rb);
1461 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001462}
1463
Jens Axboedef596e2019-01-09 08:59:42 -07001464/*
1465 * Find and free completed poll iocbs
1466 */
1467static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1468 struct list_head *done)
1469{
Jens Axboe8237e042019-12-28 10:48:22 -07001470 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001471 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001472
Jens Axboec6ca97b302019-12-28 12:11:08 -07001473 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001474 while (!list_empty(done)) {
1475 req = list_first_entry(done, struct io_kiocb, list);
1476 list_del(&req->list);
1477
Jens Axboe78e19bb2019-11-06 15:21:34 -07001478 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001479 (*nr_events)++;
1480
Jens Axboe8237e042019-12-28 10:48:22 -07001481 if (refcount_dec_and_test(&req->refs) &&
1482 !io_req_multi_free(&rb, req))
1483 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001484 }
Jens Axboedef596e2019-01-09 08:59:42 -07001485
Jens Axboe09bb8392019-03-13 12:39:28 -06001486 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001487 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001488}
1489
1490static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1491 long min)
1492{
1493 struct io_kiocb *req, *tmp;
1494 LIST_HEAD(done);
1495 bool spin;
1496 int ret;
1497
1498 /*
1499 * Only spin for completions if we don't have multiple devices hanging
1500 * off our complete list, and we're under the requested amount.
1501 */
1502 spin = !ctx->poll_multi_file && *nr_events < min;
1503
1504 ret = 0;
1505 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001506 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001507
1508 /*
1509 * Move completed entries to our local list. If we find a
1510 * request that requires polling, break out and complete
1511 * the done list first, if we have entries there.
1512 */
1513 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1514 list_move_tail(&req->list, &done);
1515 continue;
1516 }
1517 if (!list_empty(&done))
1518 break;
1519
1520 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1521 if (ret < 0)
1522 break;
1523
1524 if (ret && spin)
1525 spin = false;
1526 ret = 0;
1527 }
1528
1529 if (!list_empty(&done))
1530 io_iopoll_complete(ctx, nr_events, &done);
1531
1532 return ret;
1533}
1534
1535/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001536 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001537 * non-spinning poll check - we'll still enter the driver poll loop, but only
1538 * as a non-spinning completion check.
1539 */
1540static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1541 long min)
1542{
Jens Axboe08f54392019-08-21 22:19:11 -06001543 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001544 int ret;
1545
1546 ret = io_do_iopoll(ctx, nr_events, min);
1547 if (ret < 0)
1548 return ret;
1549 if (!min || *nr_events >= min)
1550 return 0;
1551 }
1552
1553 return 1;
1554}
1555
1556/*
1557 * We can't just wait for polled events to come to us, we have to actively
1558 * find and complete them.
1559 */
1560static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1561{
1562 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1563 return;
1564
1565 mutex_lock(&ctx->uring_lock);
1566 while (!list_empty(&ctx->poll_list)) {
1567 unsigned int nr_events = 0;
1568
1569 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001570
1571 /*
1572 * Ensure we allow local-to-the-cpu processing to take place,
1573 * in this case we need to ensure that we reap all events.
1574 */
1575 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001576 }
1577 mutex_unlock(&ctx->uring_lock);
1578}
1579
Jens Axboe2b2ed972019-10-25 10:06:15 -06001580static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1581 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001582{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001583 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001584
1585 do {
1586 int tmin = 0;
1587
Jens Axboe500f9fb2019-08-19 12:15:59 -06001588 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001589 * Don't enter poll loop if we already have events pending.
1590 * If we do, we can potentially be spinning for commands that
1591 * already triggered a CQE (eg in error).
1592 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001593 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001594 break;
1595
1596 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001597 * If a submit got punted to a workqueue, we can have the
1598 * application entering polling for a command before it gets
1599 * issued. That app will hold the uring_lock for the duration
1600 * of the poll right here, so we need to take a breather every
1601 * now and then to ensure that the issue has a chance to add
1602 * the poll to the issued list. Otherwise we can spin here
1603 * forever, while the workqueue is stuck trying to acquire the
1604 * very same mutex.
1605 */
1606 if (!(++iters & 7)) {
1607 mutex_unlock(&ctx->uring_lock);
1608 mutex_lock(&ctx->uring_lock);
1609 }
1610
Jens Axboedef596e2019-01-09 08:59:42 -07001611 if (*nr_events < min)
1612 tmin = min - *nr_events;
1613
1614 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1615 if (ret <= 0)
1616 break;
1617 ret = 0;
1618 } while (min && !*nr_events && !need_resched());
1619
Jens Axboe2b2ed972019-10-25 10:06:15 -06001620 return ret;
1621}
1622
1623static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1624 long min)
1625{
1626 int ret;
1627
1628 /*
1629 * We disallow the app entering submit/complete with polling, but we
1630 * still need to lock the ring to prevent racing with polled issue
1631 * that got punted to a workqueue.
1632 */
1633 mutex_lock(&ctx->uring_lock);
1634 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001635 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001636 return ret;
1637}
1638
Jens Axboe491381ce2019-10-17 09:20:46 -06001639static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001640{
Jens Axboe491381ce2019-10-17 09:20:46 -06001641 /*
1642 * Tell lockdep we inherited freeze protection from submission
1643 * thread.
1644 */
1645 if (req->flags & REQ_F_ISREG) {
1646 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001647
Jens Axboe491381ce2019-10-17 09:20:46 -06001648 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001649 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001650 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001651}
1652
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001653static inline void req_set_fail_links(struct io_kiocb *req)
1654{
1655 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1656 req->flags |= REQ_F_FAIL_LINK;
1657}
1658
Jens Axboeba816ad2019-09-28 11:36:45 -06001659static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001660{
Jens Axboe9adbd452019-12-20 08:45:55 -07001661 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001662
Jens Axboe491381ce2019-10-17 09:20:46 -06001663 if (kiocb->ki_flags & IOCB_WRITE)
1664 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001665
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001666 if (res != req->result)
1667 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001668 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001669}
1670
1671static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1672{
Jens Axboe9adbd452019-12-20 08:45:55 -07001673 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001674
1675 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001676 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001677}
1678
Jens Axboeba816ad2019-09-28 11:36:45 -06001679static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1680{
Jens Axboe9adbd452019-12-20 08:45:55 -07001681 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001682 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001683
1684 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001685 io_put_req_find_next(req, &nxt);
1686
1687 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001688}
1689
Jens Axboedef596e2019-01-09 08:59:42 -07001690static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1691{
Jens Axboe9adbd452019-12-20 08:45:55 -07001692 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001693
Jens Axboe491381ce2019-10-17 09:20:46 -06001694 if (kiocb->ki_flags & IOCB_WRITE)
1695 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001696
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001697 if (res != req->result)
1698 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001699 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001700 if (res != -EAGAIN)
1701 req->flags |= REQ_F_IOPOLL_COMPLETED;
1702}
1703
1704/*
1705 * After the iocb has been issued, it's safe to be found on the poll list.
1706 * Adding the kiocb to the list AFTER submission ensures that we don't
1707 * find it from a io_iopoll_getevents() thread before the issuer is done
1708 * accessing the kiocb cookie.
1709 */
1710static void io_iopoll_req_issued(struct io_kiocb *req)
1711{
1712 struct io_ring_ctx *ctx = req->ctx;
1713
1714 /*
1715 * Track whether we have multiple files in our lists. This will impact
1716 * how we do polling eventually, not spinning if we're on potentially
1717 * different devices.
1718 */
1719 if (list_empty(&ctx->poll_list)) {
1720 ctx->poll_multi_file = false;
1721 } else if (!ctx->poll_multi_file) {
1722 struct io_kiocb *list_req;
1723
1724 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1725 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001726 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001727 ctx->poll_multi_file = true;
1728 }
1729
1730 /*
1731 * For fast devices, IO may have already completed. If it has, add
1732 * it to the front so we find it first.
1733 */
1734 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1735 list_add(&req->list, &ctx->poll_list);
1736 else
1737 list_add_tail(&req->list, &ctx->poll_list);
1738}
1739
Jens Axboe3d6770f2019-04-13 11:50:54 -06001740static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001741{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001742 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001743 int diff = state->has_refs - state->used_refs;
1744
1745 if (diff)
1746 fput_many(state->file, diff);
1747 state->file = NULL;
1748 }
1749}
1750
1751/*
1752 * Get as many references to a file as we have IOs left in this submission,
1753 * assuming most submissions are for one file, or at least that each file
1754 * has more than one submission.
1755 */
1756static struct file *io_file_get(struct io_submit_state *state, int fd)
1757{
1758 if (!state)
1759 return fget(fd);
1760
1761 if (state->file) {
1762 if (state->fd == fd) {
1763 state->used_refs++;
1764 state->ios_left--;
1765 return state->file;
1766 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001767 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001768 }
1769 state->file = fget_many(fd, state->ios_left);
1770 if (!state->file)
1771 return NULL;
1772
1773 state->fd = fd;
1774 state->has_refs = state->ios_left;
1775 state->used_refs = 1;
1776 state->ios_left--;
1777 return state->file;
1778}
1779
Jens Axboe2b188cc2019-01-07 10:46:33 -07001780/*
1781 * If we tracked the file through the SCM inflight mechanism, we could support
1782 * any file. For now, just ensure that anything potentially problematic is done
1783 * inline.
1784 */
1785static bool io_file_supports_async(struct file *file)
1786{
1787 umode_t mode = file_inode(file)->i_mode;
1788
Jens Axboe10d59342019-12-09 20:16:22 -07001789 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001790 return true;
1791 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1792 return true;
1793
1794 return false;
1795}
1796
Jens Axboe3529d8c2019-12-19 18:24:38 -07001797static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1798 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001799{
Jens Axboedef596e2019-01-09 08:59:42 -07001800 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001801 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001802 unsigned ioprio;
1803 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001804
Jens Axboe09bb8392019-03-13 12:39:28 -06001805 if (!req->file)
1806 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001807
Jens Axboe491381ce2019-10-17 09:20:46 -06001808 if (S_ISREG(file_inode(req->file)->i_mode))
1809 req->flags |= REQ_F_ISREG;
1810
Jens Axboe2b188cc2019-01-07 10:46:33 -07001811 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001812 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1813 req->flags |= REQ_F_CUR_POS;
1814 kiocb->ki_pos = req->file->f_pos;
1815 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001816 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1817 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1818
1819 ioprio = READ_ONCE(sqe->ioprio);
1820 if (ioprio) {
1821 ret = ioprio_check_cap(ioprio);
1822 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001823 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001824
1825 kiocb->ki_ioprio = ioprio;
1826 } else
1827 kiocb->ki_ioprio = get_current_ioprio();
1828
1829 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1830 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001831 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001832
1833 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001834 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1835 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001836 req->flags |= REQ_F_NOWAIT;
1837
1838 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001839 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001840
Jens Axboedef596e2019-01-09 08:59:42 -07001841 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001842 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1843 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001844 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001845
Jens Axboedef596e2019-01-09 08:59:42 -07001846 kiocb->ki_flags |= IOCB_HIPRI;
1847 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001848 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001849 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001850 if (kiocb->ki_flags & IOCB_HIPRI)
1851 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001852 kiocb->ki_complete = io_complete_rw;
1853 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001854
Jens Axboe3529d8c2019-12-19 18:24:38 -07001855 req->rw.addr = READ_ONCE(sqe->addr);
1856 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001857 /* we own ->private, reuse it for the buffer index */
1858 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001859 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001860 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001861}
1862
1863static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1864{
1865 switch (ret) {
1866 case -EIOCBQUEUED:
1867 break;
1868 case -ERESTARTSYS:
1869 case -ERESTARTNOINTR:
1870 case -ERESTARTNOHAND:
1871 case -ERESTART_RESTARTBLOCK:
1872 /*
1873 * We can't just restart the syscall, since previously
1874 * submitted sqes may already be in progress. Just fail this
1875 * IO with EINTR.
1876 */
1877 ret = -EINTR;
1878 /* fall through */
1879 default:
1880 kiocb->ki_complete(kiocb, ret, 0);
1881 }
1882}
1883
Jens Axboeba816ad2019-09-28 11:36:45 -06001884static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1885 bool in_async)
1886{
Jens Axboeba042912019-12-25 16:33:42 -07001887 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1888
1889 if (req->flags & REQ_F_CUR_POS)
1890 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001891 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001892 *nxt = __io_complete_rw(kiocb, ret);
1893 else
1894 io_rw_done(kiocb, ret);
1895}
1896
Jens Axboe9adbd452019-12-20 08:45:55 -07001897static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001898 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001899{
Jens Axboe9adbd452019-12-20 08:45:55 -07001900 struct io_ring_ctx *ctx = req->ctx;
1901 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001902 struct io_mapped_ubuf *imu;
1903 unsigned index, buf_index;
1904 size_t offset;
1905 u64 buf_addr;
1906
1907 /* attempt to use fixed buffers without having provided iovecs */
1908 if (unlikely(!ctx->user_bufs))
1909 return -EFAULT;
1910
Jens Axboe9adbd452019-12-20 08:45:55 -07001911 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001912 if (unlikely(buf_index >= ctx->nr_user_bufs))
1913 return -EFAULT;
1914
1915 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1916 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001917 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001918
1919 /* overflow */
1920 if (buf_addr + len < buf_addr)
1921 return -EFAULT;
1922 /* not inside the mapped region */
1923 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1924 return -EFAULT;
1925
1926 /*
1927 * May not be a start of buffer, set size appropriately
1928 * and advance us to the beginning.
1929 */
1930 offset = buf_addr - imu->ubuf;
1931 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001932
1933 if (offset) {
1934 /*
1935 * Don't use iov_iter_advance() here, as it's really slow for
1936 * using the latter parts of a big fixed buffer - it iterates
1937 * over each segment manually. We can cheat a bit here, because
1938 * we know that:
1939 *
1940 * 1) it's a BVEC iter, we set it up
1941 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1942 * first and last bvec
1943 *
1944 * So just find our index, and adjust the iterator afterwards.
1945 * If the offset is within the first bvec (or the whole first
1946 * bvec, just use iov_iter_advance(). This makes it easier
1947 * since we can just skip the first segment, which may not
1948 * be PAGE_SIZE aligned.
1949 */
1950 const struct bio_vec *bvec = imu->bvec;
1951
1952 if (offset <= bvec->bv_len) {
1953 iov_iter_advance(iter, offset);
1954 } else {
1955 unsigned long seg_skip;
1956
1957 /* skip first vec */
1958 offset -= bvec->bv_len;
1959 seg_skip = 1 + (offset >> PAGE_SHIFT);
1960
1961 iter->bvec = bvec + seg_skip;
1962 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001963 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001964 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001965 }
1966 }
1967
Jens Axboe5e559562019-11-13 16:12:46 -07001968 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001969}
1970
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001971static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1972 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001973{
Jens Axboe9adbd452019-12-20 08:45:55 -07001974 void __user *buf = u64_to_user_ptr(req->rw.addr);
1975 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001976 u8 opcode;
1977
Jens Axboed625c6e2019-12-17 19:53:05 -07001978 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001979 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001980 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001981 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001982 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001983
Jens Axboe9adbd452019-12-20 08:45:55 -07001984 /* buffer index only valid with fixed read/write */
1985 if (req->rw.kiocb.private)
1986 return -EINVAL;
1987
Jens Axboe3a6820f2019-12-22 15:19:35 -07001988 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1989 ssize_t ret;
1990 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1991 *iovec = NULL;
1992 return ret;
1993 }
1994
Jens Axboef67676d2019-12-02 11:03:47 -07001995 if (req->io) {
1996 struct io_async_rw *iorw = &req->io->rw;
1997
1998 *iovec = iorw->iov;
1999 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2000 if (iorw->iov == iorw->fast_iov)
2001 *iovec = NULL;
2002 return iorw->size;
2003 }
2004
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002005 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002006 return -EFAULT;
2007
2008#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002009 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002010 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2011 iovec, iter);
2012#endif
2013
2014 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2015}
2016
Jens Axboe32960612019-09-23 11:05:34 -06002017/*
2018 * For files that don't have ->read_iter() and ->write_iter(), handle them
2019 * by looping over ->read() or ->write() manually.
2020 */
2021static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2022 struct iov_iter *iter)
2023{
2024 ssize_t ret = 0;
2025
2026 /*
2027 * Don't support polled IO through this interface, and we can't
2028 * support non-blocking either. For the latter, this just causes
2029 * the kiocb to be handled from an async context.
2030 */
2031 if (kiocb->ki_flags & IOCB_HIPRI)
2032 return -EOPNOTSUPP;
2033 if (kiocb->ki_flags & IOCB_NOWAIT)
2034 return -EAGAIN;
2035
2036 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002037 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002038 ssize_t nr;
2039
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002040 if (!iov_iter_is_bvec(iter)) {
2041 iovec = iov_iter_iovec(iter);
2042 } else {
2043 /* fixed buffers import bvec */
2044 iovec.iov_base = kmap(iter->bvec->bv_page)
2045 + iter->iov_offset;
2046 iovec.iov_len = min(iter->count,
2047 iter->bvec->bv_len - iter->iov_offset);
2048 }
2049
Jens Axboe32960612019-09-23 11:05:34 -06002050 if (rw == READ) {
2051 nr = file->f_op->read(file, iovec.iov_base,
2052 iovec.iov_len, &kiocb->ki_pos);
2053 } else {
2054 nr = file->f_op->write(file, iovec.iov_base,
2055 iovec.iov_len, &kiocb->ki_pos);
2056 }
2057
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002058 if (iov_iter_is_bvec(iter))
2059 kunmap(iter->bvec->bv_page);
2060
Jens Axboe32960612019-09-23 11:05:34 -06002061 if (nr < 0) {
2062 if (!ret)
2063 ret = nr;
2064 break;
2065 }
2066 ret += nr;
2067 if (nr != iovec.iov_len)
2068 break;
2069 iov_iter_advance(iter, nr);
2070 }
2071
2072 return ret;
2073}
2074
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002075static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002076 struct iovec *iovec, struct iovec *fast_iov,
2077 struct iov_iter *iter)
2078{
2079 req->io->rw.nr_segs = iter->nr_segs;
2080 req->io->rw.size = io_size;
2081 req->io->rw.iov = iovec;
2082 if (!req->io->rw.iov) {
2083 req->io->rw.iov = req->io->rw.fast_iov;
2084 memcpy(req->io->rw.iov, fast_iov,
2085 sizeof(struct iovec) * iter->nr_segs);
2086 }
2087}
2088
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002089static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002090{
Jens Axboed3656342019-12-18 09:50:26 -07002091 if (!io_op_defs[req->opcode].async_ctx)
2092 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002093 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002094 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002095}
2096
2097static void io_rw_async(struct io_wq_work **workptr)
2098{
2099 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2100 struct iovec *iov = NULL;
2101
2102 if (req->io->rw.iov != req->io->rw.fast_iov)
2103 iov = req->io->rw.iov;
2104 io_wq_submit_work(workptr);
2105 kfree(iov);
2106}
2107
2108static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2109 struct iovec *iovec, struct iovec *fast_iov,
2110 struct iov_iter *iter)
2111{
Jens Axboe74566df2020-01-13 19:23:24 -07002112 if (req->opcode == IORING_OP_READ_FIXED ||
2113 req->opcode == IORING_OP_WRITE_FIXED)
2114 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002115 if (!req->io && io_alloc_async_ctx(req))
2116 return -ENOMEM;
2117
2118 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2119 req->work.func = io_rw_async;
2120 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002121}
2122
Jens Axboe3529d8c2019-12-19 18:24:38 -07002123static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2124 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002125{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002126 struct io_async_ctx *io;
2127 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002128 ssize_t ret;
2129
Jens Axboe3529d8c2019-12-19 18:24:38 -07002130 ret = io_prep_rw(req, sqe, force_nonblock);
2131 if (ret)
2132 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002133
Jens Axboe3529d8c2019-12-19 18:24:38 -07002134 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2135 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002136
Jens Axboe3529d8c2019-12-19 18:24:38 -07002137 if (!req->io)
2138 return 0;
2139
2140 io = req->io;
2141 io->rw.iov = io->rw.fast_iov;
2142 req->io = NULL;
2143 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2144 req->io = io;
2145 if (ret < 0)
2146 return ret;
2147
2148 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2149 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002150}
2151
Pavel Begunkov267bc902019-11-07 01:41:08 +03002152static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002153 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002154{
2155 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002156 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002157 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002158 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002159 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002160
Jens Axboe3529d8c2019-12-19 18:24:38 -07002161 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002162 if (ret < 0)
2163 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002164
Jens Axboefd6c2e42019-12-18 12:19:41 -07002165 /* Ensure we clear previously set non-block flag */
2166 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002167 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002168
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002169 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002170 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002171 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002172 req->result = io_size;
2173
2174 /*
2175 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2176 * we know to async punt it even if it was opened O_NONBLOCK
2177 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002178 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002179 req->flags |= REQ_F_MUST_PUNT;
2180 goto copy_iov;
2181 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002182
Jens Axboe31b51512019-01-18 22:56:34 -07002183 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002184 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002185 if (!ret) {
2186 ssize_t ret2;
2187
Jens Axboe9adbd452019-12-20 08:45:55 -07002188 if (req->file->f_op->read_iter)
2189 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002190 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002191 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002192
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002193 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002194 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002195 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002196 } else {
2197copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002198 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002199 inline_vecs, &iter);
2200 if (ret)
2201 goto out_free;
2202 return -EAGAIN;
2203 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002204 }
Jens Axboef67676d2019-12-02 11:03:47 -07002205out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002206 if (!io_wq_current_is_worker())
2207 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002208 return ret;
2209}
2210
Jens Axboe3529d8c2019-12-19 18:24:38 -07002211static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2212 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002213{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002214 struct io_async_ctx *io;
2215 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002216 ssize_t ret;
2217
Jens Axboe3529d8c2019-12-19 18:24:38 -07002218 ret = io_prep_rw(req, sqe, force_nonblock);
2219 if (ret)
2220 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002221
Jens Axboe3529d8c2019-12-19 18:24:38 -07002222 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2223 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002224
Jens Axboe3529d8c2019-12-19 18:24:38 -07002225 if (!req->io)
2226 return 0;
2227
2228 io = req->io;
2229 io->rw.iov = io->rw.fast_iov;
2230 req->io = NULL;
2231 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2232 req->io = io;
2233 if (ret < 0)
2234 return ret;
2235
2236 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2237 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002238}
2239
Pavel Begunkov267bc902019-11-07 01:41:08 +03002240static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002241 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002242{
2243 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002244 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002245 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002246 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002247 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002248
Jens Axboe3529d8c2019-12-19 18:24:38 -07002249 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002250 if (ret < 0)
2251 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002252
Jens Axboefd6c2e42019-12-18 12:19:41 -07002253 /* Ensure we clear previously set non-block flag */
2254 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002255 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002256
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002257 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002258 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002259 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002260 req->result = io_size;
2261
2262 /*
2263 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2264 * we know to async punt it even if it was opened O_NONBLOCK
2265 */
2266 if (force_nonblock && !io_file_supports_async(req->file)) {
2267 req->flags |= REQ_F_MUST_PUNT;
2268 goto copy_iov;
2269 }
2270
Jens Axboe10d59342019-12-09 20:16:22 -07002271 /* file path doesn't support NOWAIT for non-direct_IO */
2272 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2273 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002274 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002275
Jens Axboe31b51512019-01-18 22:56:34 -07002276 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002277 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002278 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002279 ssize_t ret2;
2280
Jens Axboe2b188cc2019-01-07 10:46:33 -07002281 /*
2282 * Open-code file_start_write here to grab freeze protection,
2283 * which will be released by another thread in
2284 * io_complete_rw(). Fool lockdep by telling it the lock got
2285 * released so that it doesn't complain about the held lock when
2286 * we return to userspace.
2287 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002288 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002289 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002290 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002291 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002292 SB_FREEZE_WRITE);
2293 }
2294 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002295
Jens Axboe9adbd452019-12-20 08:45:55 -07002296 if (req->file->f_op->write_iter)
2297 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002298 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002299 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002300 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002301 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002302 } else {
2303copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002304 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002305 inline_vecs, &iter);
2306 if (ret)
2307 goto out_free;
2308 return -EAGAIN;
2309 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002310 }
Jens Axboe31b51512019-01-18 22:56:34 -07002311out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002312 if (!io_wq_current_is_worker())
2313 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002314 return ret;
2315}
2316
2317/*
2318 * IORING_OP_NOP just posts a completion event, nothing else.
2319 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002320static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321{
2322 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002323
Jens Axboedef596e2019-01-09 08:59:42 -07002324 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2325 return -EINVAL;
2326
Jens Axboe78e19bb2019-11-06 15:21:34 -07002327 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002328 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002329 return 0;
2330}
2331
Jens Axboe3529d8c2019-12-19 18:24:38 -07002332static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002333{
Jens Axboe6b063142019-01-10 22:13:58 -07002334 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002335
Jens Axboe09bb8392019-03-13 12:39:28 -06002336 if (!req->file)
2337 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002338
Jens Axboe6b063142019-01-10 22:13:58 -07002339 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002340 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002341 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002342 return -EINVAL;
2343
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002344 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2345 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2346 return -EINVAL;
2347
2348 req->sync.off = READ_ONCE(sqe->off);
2349 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002350 return 0;
2351}
2352
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002353static bool io_req_cancelled(struct io_kiocb *req)
2354{
2355 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2356 req_set_fail_links(req);
2357 io_cqring_add_event(req, -ECANCELED);
2358 io_put_req(req);
2359 return true;
2360 }
2361
2362 return false;
2363}
2364
Jens Axboe78912932020-01-14 22:09:06 -07002365static void io_link_work_cb(struct io_wq_work **workptr)
2366{
2367 struct io_wq_work *work = *workptr;
2368 struct io_kiocb *link = work->data;
2369
2370 io_queue_linked_timeout(link);
2371 work->func = io_wq_submit_work;
2372}
2373
2374static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2375{
2376 struct io_kiocb *link;
2377
2378 io_prep_async_work(nxt, &link);
2379 *workptr = &nxt->work;
2380 if (link) {
2381 nxt->work.flags |= IO_WQ_WORK_CB;
2382 nxt->work.func = io_link_work_cb;
2383 nxt->work.data = link;
2384 }
2385}
2386
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002387static void io_fsync_finish(struct io_wq_work **workptr)
2388{
2389 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2390 loff_t end = req->sync.off + req->sync.len;
2391 struct io_kiocb *nxt = NULL;
2392 int ret;
2393
2394 if (io_req_cancelled(req))
2395 return;
2396
Jens Axboe9adbd452019-12-20 08:45:55 -07002397 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002398 end > 0 ? end : LLONG_MAX,
2399 req->sync.flags & IORING_FSYNC_DATASYNC);
2400 if (ret < 0)
2401 req_set_fail_links(req);
2402 io_cqring_add_event(req, ret);
2403 io_put_req_find_next(req, &nxt);
2404 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002405 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002406}
2407
Jens Axboefc4df992019-12-10 14:38:45 -07002408static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2409 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002410{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002411 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002412
2413 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002414 if (force_nonblock) {
2415 io_put_req(req);
2416 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002417 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002418 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002419
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002420 work = old_work = &req->work;
2421 io_fsync_finish(&work);
2422 if (work && work != old_work)
2423 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002424 return 0;
2425}
2426
Jens Axboed63d1b52019-12-10 10:38:56 -07002427static void io_fallocate_finish(struct io_wq_work **workptr)
2428{
2429 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2430 struct io_kiocb *nxt = NULL;
2431 int ret;
2432
2433 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2434 req->sync.len);
2435 if (ret < 0)
2436 req_set_fail_links(req);
2437 io_cqring_add_event(req, ret);
2438 io_put_req_find_next(req, &nxt);
2439 if (nxt)
2440 io_wq_assign_next(workptr, nxt);
2441}
2442
2443static int io_fallocate_prep(struct io_kiocb *req,
2444 const struct io_uring_sqe *sqe)
2445{
2446 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2447 return -EINVAL;
2448
2449 req->sync.off = READ_ONCE(sqe->off);
2450 req->sync.len = READ_ONCE(sqe->addr);
2451 req->sync.mode = READ_ONCE(sqe->len);
2452 return 0;
2453}
2454
2455static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2456 bool force_nonblock)
2457{
2458 struct io_wq_work *work, *old_work;
2459
2460 /* fallocate always requiring blocking context */
2461 if (force_nonblock) {
2462 io_put_req(req);
2463 req->work.func = io_fallocate_finish;
2464 return -EAGAIN;
2465 }
2466
2467 work = old_work = &req->work;
2468 io_fallocate_finish(&work);
2469 if (work && work != old_work)
2470 *nxt = container_of(work, struct io_kiocb, work);
2471
2472 return 0;
2473}
2474
Jens Axboe15b71ab2019-12-11 11:20:36 -07002475static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2476{
Jens Axboef8748882020-01-08 17:47:02 -07002477 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002478 int ret;
2479
2480 if (sqe->ioprio || sqe->buf_index)
2481 return -EINVAL;
2482
2483 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002484 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002485 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002486 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002487
Jens Axboef8748882020-01-08 17:47:02 -07002488 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002489 if (IS_ERR(req->open.filename)) {
2490 ret = PTR_ERR(req->open.filename);
2491 req->open.filename = NULL;
2492 return ret;
2493 }
2494
2495 return 0;
2496}
2497
Jens Axboecebdb982020-01-08 17:59:24 -07002498static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2499{
2500 struct open_how __user *how;
2501 const char __user *fname;
2502 size_t len;
2503 int ret;
2504
2505 if (sqe->ioprio || sqe->buf_index)
2506 return -EINVAL;
2507
2508 req->open.dfd = READ_ONCE(sqe->fd);
2509 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2510 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2511 len = READ_ONCE(sqe->len);
2512
2513 if (len < OPEN_HOW_SIZE_VER0)
2514 return -EINVAL;
2515
2516 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2517 len);
2518 if (ret)
2519 return ret;
2520
2521 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2522 req->open.how.flags |= O_LARGEFILE;
2523
2524 req->open.filename = getname(fname);
2525 if (IS_ERR(req->open.filename)) {
2526 ret = PTR_ERR(req->open.filename);
2527 req->open.filename = NULL;
2528 return ret;
2529 }
2530
2531 return 0;
2532}
2533
2534static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2535 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002536{
2537 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002538 struct file *file;
2539 int ret;
2540
2541 if (force_nonblock) {
2542 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2543 return -EAGAIN;
2544 }
2545
Jens Axboecebdb982020-01-08 17:59:24 -07002546 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002547 if (ret)
2548 goto err;
2549
Jens Axboecebdb982020-01-08 17:59:24 -07002550 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002551 if (ret < 0)
2552 goto err;
2553
2554 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2555 if (IS_ERR(file)) {
2556 put_unused_fd(ret);
2557 ret = PTR_ERR(file);
2558 } else {
2559 fsnotify_open(file);
2560 fd_install(ret, file);
2561 }
2562err:
2563 putname(req->open.filename);
2564 if (ret < 0)
2565 req_set_fail_links(req);
2566 io_cqring_add_event(req, ret);
2567 io_put_req_find_next(req, nxt);
2568 return 0;
2569}
2570
Jens Axboecebdb982020-01-08 17:59:24 -07002571static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2572 bool force_nonblock)
2573{
2574 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2575 return io_openat2(req, nxt, force_nonblock);
2576}
2577
Jens Axboec1ca7572019-12-25 22:18:28 -07002578static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2579{
2580#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2581 if (sqe->ioprio || sqe->buf_index || sqe->off)
2582 return -EINVAL;
2583
2584 req->madvise.addr = READ_ONCE(sqe->addr);
2585 req->madvise.len = READ_ONCE(sqe->len);
2586 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2587 return 0;
2588#else
2589 return -EOPNOTSUPP;
2590#endif
2591}
2592
2593static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2594 bool force_nonblock)
2595{
2596#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2597 struct io_madvise *ma = &req->madvise;
2598 int ret;
2599
2600 if (force_nonblock)
2601 return -EAGAIN;
2602
2603 ret = do_madvise(ma->addr, ma->len, ma->advice);
2604 if (ret < 0)
2605 req_set_fail_links(req);
2606 io_cqring_add_event(req, ret);
2607 io_put_req_find_next(req, nxt);
2608 return 0;
2609#else
2610 return -EOPNOTSUPP;
2611#endif
2612}
2613
Jens Axboe4840e412019-12-25 22:03:45 -07002614static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2615{
2616 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2617 return -EINVAL;
2618
2619 req->fadvise.offset = READ_ONCE(sqe->off);
2620 req->fadvise.len = READ_ONCE(sqe->len);
2621 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2622 return 0;
2623}
2624
2625static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2626 bool force_nonblock)
2627{
2628 struct io_fadvise *fa = &req->fadvise;
2629 int ret;
2630
2631 /* DONTNEED may block, others _should_ not */
2632 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2633 return -EAGAIN;
2634
2635 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2636 if (ret < 0)
2637 req_set_fail_links(req);
2638 io_cqring_add_event(req, ret);
2639 io_put_req_find_next(req, nxt);
2640 return 0;
2641}
2642
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002643static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2644{
Jens Axboef8748882020-01-08 17:47:02 -07002645 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002646 unsigned lookup_flags;
2647 int ret;
2648
2649 if (sqe->ioprio || sqe->buf_index)
2650 return -EINVAL;
2651
2652 req->open.dfd = READ_ONCE(sqe->fd);
2653 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002654 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002655 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002656 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002657
Jens Axboec12cedf2020-01-08 17:41:21 -07002658 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002659 return -EINVAL;
2660
Jens Axboef8748882020-01-08 17:47:02 -07002661 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002662 if (IS_ERR(req->open.filename)) {
2663 ret = PTR_ERR(req->open.filename);
2664 req->open.filename = NULL;
2665 return ret;
2666 }
2667
2668 return 0;
2669}
2670
2671static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2672 bool force_nonblock)
2673{
2674 struct io_open *ctx = &req->open;
2675 unsigned lookup_flags;
2676 struct path path;
2677 struct kstat stat;
2678 int ret;
2679
2680 if (force_nonblock)
2681 return -EAGAIN;
2682
Jens Axboec12cedf2020-01-08 17:41:21 -07002683 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002684 return -EINVAL;
2685
2686retry:
2687 /* filename_lookup() drops it, keep a reference */
2688 ctx->filename->refcnt++;
2689
2690 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2691 NULL);
2692 if (ret)
2693 goto err;
2694
Jens Axboec12cedf2020-01-08 17:41:21 -07002695 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002696 path_put(&path);
2697 if (retry_estale(ret, lookup_flags)) {
2698 lookup_flags |= LOOKUP_REVAL;
2699 goto retry;
2700 }
2701 if (!ret)
2702 ret = cp_statx(&stat, ctx->buffer);
2703err:
2704 putname(ctx->filename);
2705 if (ret < 0)
2706 req_set_fail_links(req);
2707 io_cqring_add_event(req, ret);
2708 io_put_req_find_next(req, nxt);
2709 return 0;
2710}
2711
Jens Axboeb5dba592019-12-11 14:02:38 -07002712static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2713{
2714 /*
2715 * If we queue this for async, it must not be cancellable. That would
2716 * leave the 'file' in an undeterminate state.
2717 */
2718 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2719
2720 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2721 sqe->rw_flags || sqe->buf_index)
2722 return -EINVAL;
2723 if (sqe->flags & IOSQE_FIXED_FILE)
2724 return -EINVAL;
2725
2726 req->close.fd = READ_ONCE(sqe->fd);
2727 if (req->file->f_op == &io_uring_fops ||
2728 req->close.fd == req->ring_fd)
2729 return -EBADF;
2730
2731 return 0;
2732}
2733
2734static void io_close_finish(struct io_wq_work **workptr)
2735{
2736 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2737 struct io_kiocb *nxt = NULL;
2738
2739 /* Invoked with files, we need to do the close */
2740 if (req->work.files) {
2741 int ret;
2742
2743 ret = filp_close(req->close.put_file, req->work.files);
2744 if (ret < 0) {
2745 req_set_fail_links(req);
2746 }
2747 io_cqring_add_event(req, ret);
2748 }
2749
2750 fput(req->close.put_file);
2751
2752 /* we bypassed the re-issue, drop the submission reference */
2753 io_put_req(req);
2754 io_put_req_find_next(req, &nxt);
2755 if (nxt)
2756 io_wq_assign_next(workptr, nxt);
2757}
2758
2759static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2760 bool force_nonblock)
2761{
2762 int ret;
2763
2764 req->close.put_file = NULL;
2765 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2766 if (ret < 0)
2767 return ret;
2768
2769 /* if the file has a flush method, be safe and punt to async */
2770 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2771 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2772 goto eagain;
2773 }
2774
2775 /*
2776 * No ->flush(), safely close from here and just punt the
2777 * fput() to async context.
2778 */
2779 ret = filp_close(req->close.put_file, current->files);
2780
2781 if (ret < 0)
2782 req_set_fail_links(req);
2783 io_cqring_add_event(req, ret);
2784
2785 if (io_wq_current_is_worker()) {
2786 struct io_wq_work *old_work, *work;
2787
2788 old_work = work = &req->work;
2789 io_close_finish(&work);
2790 if (work && work != old_work)
2791 *nxt = container_of(work, struct io_kiocb, work);
2792 return 0;
2793 }
2794
2795eagain:
2796 req->work.func = io_close_finish;
2797 return -EAGAIN;
2798}
2799
Jens Axboe3529d8c2019-12-19 18:24:38 -07002800static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002801{
2802 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002803
2804 if (!req->file)
2805 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002806
2807 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2808 return -EINVAL;
2809 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2810 return -EINVAL;
2811
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002812 req->sync.off = READ_ONCE(sqe->off);
2813 req->sync.len = READ_ONCE(sqe->len);
2814 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002815 return 0;
2816}
2817
2818static void io_sync_file_range_finish(struct io_wq_work **workptr)
2819{
2820 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2821 struct io_kiocb *nxt = NULL;
2822 int ret;
2823
2824 if (io_req_cancelled(req))
2825 return;
2826
Jens Axboe9adbd452019-12-20 08:45:55 -07002827 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002828 req->sync.flags);
2829 if (ret < 0)
2830 req_set_fail_links(req);
2831 io_cqring_add_event(req, ret);
2832 io_put_req_find_next(req, &nxt);
2833 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002834 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002835}
2836
Jens Axboefc4df992019-12-10 14:38:45 -07002837static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002838 bool force_nonblock)
2839{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002840 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002841
2842 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002843 if (force_nonblock) {
2844 io_put_req(req);
2845 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002846 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002847 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002848
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002849 work = old_work = &req->work;
2850 io_sync_file_range_finish(&work);
2851 if (work && work != old_work)
2852 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002853 return 0;
2854}
2855
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002856#if defined(CONFIG_NET)
2857static void io_sendrecv_async(struct io_wq_work **workptr)
2858{
2859 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2860 struct iovec *iov = NULL;
2861
2862 if (req->io->rw.iov != req->io->rw.fast_iov)
2863 iov = req->io->msg.iov;
2864 io_wq_submit_work(workptr);
2865 kfree(iov);
2866}
2867#endif
2868
Jens Axboe3529d8c2019-12-19 18:24:38 -07002869static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002870{
Jens Axboe03b12302019-12-02 18:50:25 -07002871#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002872 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002873 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002874
Jens Axboee47293f2019-12-20 08:58:21 -07002875 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2876 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002877 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002878
Jens Axboefddafac2020-01-04 20:19:44 -07002879 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002880 return 0;
2881
Jens Axboed9688562019-12-09 19:35:20 -07002882 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002883 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002884 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002885#else
Jens Axboee47293f2019-12-20 08:58:21 -07002886 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002887#endif
2888}
2889
Jens Axboefc4df992019-12-10 14:38:45 -07002890static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2891 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002892{
2893#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002894 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002895 struct socket *sock;
2896 int ret;
2897
2898 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2899 return -EINVAL;
2900
2901 sock = sock_from_file(req->file, &ret);
2902 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002903 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002904 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002905 unsigned flags;
2906
Jens Axboe03b12302019-12-02 18:50:25 -07002907 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002908 kmsg = &req->io->msg;
2909 kmsg->msg.msg_name = &addr;
2910 /* if iov is set, it's allocated already */
2911 if (!kmsg->iov)
2912 kmsg->iov = kmsg->fast_iov;
2913 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002914 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002915 struct io_sr_msg *sr = &req->sr_msg;
2916
Jens Axboe0b416c32019-12-15 10:57:46 -07002917 kmsg = &io.msg;
2918 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002919
2920 io.msg.iov = io.msg.fast_iov;
2921 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2922 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002923 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002924 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002925 }
2926
Jens Axboee47293f2019-12-20 08:58:21 -07002927 flags = req->sr_msg.msg_flags;
2928 if (flags & MSG_DONTWAIT)
2929 req->flags |= REQ_F_NOWAIT;
2930 else if (force_nonblock)
2931 flags |= MSG_DONTWAIT;
2932
Jens Axboe0b416c32019-12-15 10:57:46 -07002933 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002934 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002935 if (req->io)
2936 return -EAGAIN;
2937 if (io_alloc_async_ctx(req))
2938 return -ENOMEM;
2939 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2940 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002941 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002942 }
2943 if (ret == -ERESTARTSYS)
2944 ret = -EINTR;
2945 }
2946
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002947 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002948 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002949 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002950 if (ret < 0)
2951 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002952 io_put_req_find_next(req, nxt);
2953 return 0;
2954#else
2955 return -EOPNOTSUPP;
2956#endif
2957}
2958
Jens Axboefddafac2020-01-04 20:19:44 -07002959static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2960 bool force_nonblock)
2961{
2962#if defined(CONFIG_NET)
2963 struct socket *sock;
2964 int ret;
2965
2966 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2967 return -EINVAL;
2968
2969 sock = sock_from_file(req->file, &ret);
2970 if (sock) {
2971 struct io_sr_msg *sr = &req->sr_msg;
2972 struct msghdr msg;
2973 struct iovec iov;
2974 unsigned flags;
2975
2976 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
2977 &msg.msg_iter);
2978 if (ret)
2979 return ret;
2980
2981 msg.msg_name = NULL;
2982 msg.msg_control = NULL;
2983 msg.msg_controllen = 0;
2984 msg.msg_namelen = 0;
2985
2986 flags = req->sr_msg.msg_flags;
2987 if (flags & MSG_DONTWAIT)
2988 req->flags |= REQ_F_NOWAIT;
2989 else if (force_nonblock)
2990 flags |= MSG_DONTWAIT;
2991
2992 ret = __sys_sendmsg_sock(sock, &msg, flags);
2993 if (force_nonblock && ret == -EAGAIN)
2994 return -EAGAIN;
2995 if (ret == -ERESTARTSYS)
2996 ret = -EINTR;
2997 }
2998
2999 io_cqring_add_event(req, ret);
3000 if (ret < 0)
3001 req_set_fail_links(req);
3002 io_put_req_find_next(req, nxt);
3003 return 0;
3004#else
3005 return -EOPNOTSUPP;
3006#endif
3007}
3008
Jens Axboe3529d8c2019-12-19 18:24:38 -07003009static int io_recvmsg_prep(struct io_kiocb *req,
3010 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003011{
3012#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003013 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003014 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07003015
Jens Axboe3529d8c2019-12-19 18:24:38 -07003016 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3017 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
3018
Jens Axboefddafac2020-01-04 20:19:44 -07003019 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003020 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003021
Jens Axboed9688562019-12-09 19:35:20 -07003022 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003023 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003024 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003025#else
Jens Axboee47293f2019-12-20 08:58:21 -07003026 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003027#endif
3028}
3029
Jens Axboefc4df992019-12-10 14:38:45 -07003030static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3031 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003032{
3033#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003034 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003035 struct socket *sock;
3036 int ret;
3037
3038 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3039 return -EINVAL;
3040
3041 sock = sock_from_file(req->file, &ret);
3042 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003043 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003044 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003045 unsigned flags;
3046
Jens Axboe03b12302019-12-02 18:50:25 -07003047 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003048 kmsg = &req->io->msg;
3049 kmsg->msg.msg_name = &addr;
3050 /* if iov is set, it's allocated already */
3051 if (!kmsg->iov)
3052 kmsg->iov = kmsg->fast_iov;
3053 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003054 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003055 struct io_sr_msg *sr = &req->sr_msg;
3056
Jens Axboe0b416c32019-12-15 10:57:46 -07003057 kmsg = &io.msg;
3058 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003059
3060 io.msg.iov = io.msg.fast_iov;
3061 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3062 sr->msg_flags, &io.msg.uaddr,
3063 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003064 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003065 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003066 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003067
Jens Axboee47293f2019-12-20 08:58:21 -07003068 flags = req->sr_msg.msg_flags;
3069 if (flags & MSG_DONTWAIT)
3070 req->flags |= REQ_F_NOWAIT;
3071 else if (force_nonblock)
3072 flags |= MSG_DONTWAIT;
3073
3074 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3075 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003076 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003077 if (req->io)
3078 return -EAGAIN;
3079 if (io_alloc_async_ctx(req))
3080 return -ENOMEM;
3081 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3082 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003083 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003084 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003085 if (ret == -ERESTARTSYS)
3086 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003087 }
3088
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003089 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003090 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003091 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003092 if (ret < 0)
3093 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003094 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003095 return 0;
3096#else
3097 return -EOPNOTSUPP;
3098#endif
3099}
3100
Jens Axboefddafac2020-01-04 20:19:44 -07003101static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3102 bool force_nonblock)
3103{
3104#if defined(CONFIG_NET)
3105 struct socket *sock;
3106 int ret;
3107
3108 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3109 return -EINVAL;
3110
3111 sock = sock_from_file(req->file, &ret);
3112 if (sock) {
3113 struct io_sr_msg *sr = &req->sr_msg;
3114 struct msghdr msg;
3115 struct iovec iov;
3116 unsigned flags;
3117
3118 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3119 &msg.msg_iter);
3120 if (ret)
3121 return ret;
3122
3123 msg.msg_name = NULL;
3124 msg.msg_control = NULL;
3125 msg.msg_controllen = 0;
3126 msg.msg_namelen = 0;
3127 msg.msg_iocb = NULL;
3128 msg.msg_flags = 0;
3129
3130 flags = req->sr_msg.msg_flags;
3131 if (flags & MSG_DONTWAIT)
3132 req->flags |= REQ_F_NOWAIT;
3133 else if (force_nonblock)
3134 flags |= MSG_DONTWAIT;
3135
3136 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3137 if (force_nonblock && ret == -EAGAIN)
3138 return -EAGAIN;
3139 if (ret == -ERESTARTSYS)
3140 ret = -EINTR;
3141 }
3142
3143 io_cqring_add_event(req, ret);
3144 if (ret < 0)
3145 req_set_fail_links(req);
3146 io_put_req_find_next(req, nxt);
3147 return 0;
3148#else
3149 return -EOPNOTSUPP;
3150#endif
3151}
3152
3153
Jens Axboe3529d8c2019-12-19 18:24:38 -07003154static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003155{
3156#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003157 struct io_accept *accept = &req->accept;
3158
Jens Axboe17f2fe32019-10-17 14:42:58 -06003159 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3160 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003161 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003162 return -EINVAL;
3163
Jens Axboed55e5f52019-12-11 16:12:15 -07003164 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3165 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003166 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003167 return 0;
3168#else
3169 return -EOPNOTSUPP;
3170#endif
3171}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003172
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003173#if defined(CONFIG_NET)
3174static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3175 bool force_nonblock)
3176{
3177 struct io_accept *accept = &req->accept;
3178 unsigned file_flags;
3179 int ret;
3180
3181 file_flags = force_nonblock ? O_NONBLOCK : 0;
3182 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3183 accept->addr_len, accept->flags);
3184 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003185 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003186 if (ret == -ERESTARTSYS)
3187 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003188 if (ret < 0)
3189 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003190 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003191 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003192 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003193}
3194
3195static void io_accept_finish(struct io_wq_work **workptr)
3196{
3197 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3198 struct io_kiocb *nxt = NULL;
3199
3200 if (io_req_cancelled(req))
3201 return;
3202 __io_accept(req, &nxt, false);
3203 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003204 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003205}
3206#endif
3207
3208static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3209 bool force_nonblock)
3210{
3211#if defined(CONFIG_NET)
3212 int ret;
3213
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003214 ret = __io_accept(req, nxt, force_nonblock);
3215 if (ret == -EAGAIN && force_nonblock) {
3216 req->work.func = io_accept_finish;
3217 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3218 io_put_req(req);
3219 return -EAGAIN;
3220 }
3221 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003222#else
3223 return -EOPNOTSUPP;
3224#endif
3225}
3226
Jens Axboe3529d8c2019-12-19 18:24:38 -07003227static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003228{
3229#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003230 struct io_connect *conn = &req->connect;
3231 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003232
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003233 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3234 return -EINVAL;
3235 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3236 return -EINVAL;
3237
Jens Axboe3529d8c2019-12-19 18:24:38 -07003238 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3239 conn->addr_len = READ_ONCE(sqe->addr2);
3240
3241 if (!io)
3242 return 0;
3243
3244 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003245 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003246#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003247 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003248#endif
3249}
3250
Jens Axboefc4df992019-12-10 14:38:45 -07003251static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3252 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003253{
3254#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003255 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003256 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003257 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003258
Jens Axboef499a022019-12-02 16:28:46 -07003259 if (req->io) {
3260 io = req->io;
3261 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003262 ret = move_addr_to_kernel(req->connect.addr,
3263 req->connect.addr_len,
3264 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003265 if (ret)
3266 goto out;
3267 io = &__io;
3268 }
3269
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003270 file_flags = force_nonblock ? O_NONBLOCK : 0;
3271
3272 ret = __sys_connect_file(req->file, &io->connect.address,
3273 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003274 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003275 if (req->io)
3276 return -EAGAIN;
3277 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003278 ret = -ENOMEM;
3279 goto out;
3280 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003281 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003282 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003283 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003284 if (ret == -ERESTARTSYS)
3285 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003286out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003287 if (ret < 0)
3288 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003289 io_cqring_add_event(req, ret);
3290 io_put_req_find_next(req, nxt);
3291 return 0;
3292#else
3293 return -EOPNOTSUPP;
3294#endif
3295}
3296
Jens Axboe221c5eb2019-01-17 09:41:58 -07003297static void io_poll_remove_one(struct io_kiocb *req)
3298{
3299 struct io_poll_iocb *poll = &req->poll;
3300
3301 spin_lock(&poll->head->lock);
3302 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003303 if (!list_empty(&poll->wait.entry)) {
3304 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003305 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003306 }
3307 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003308 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003309}
3310
3311static void io_poll_remove_all(struct io_ring_ctx *ctx)
3312{
Jens Axboe78076bb2019-12-04 19:56:40 -07003313 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003314 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003315 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003316
3317 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003318 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3319 struct hlist_head *list;
3320
3321 list = &ctx->cancel_hash[i];
3322 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3323 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003324 }
3325 spin_unlock_irq(&ctx->completion_lock);
3326}
3327
Jens Axboe47f46762019-11-09 17:43:02 -07003328static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3329{
Jens Axboe78076bb2019-12-04 19:56:40 -07003330 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003331 struct io_kiocb *req;
3332
Jens Axboe78076bb2019-12-04 19:56:40 -07003333 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3334 hlist_for_each_entry(req, list, hash_node) {
3335 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003336 io_poll_remove_one(req);
3337 return 0;
3338 }
Jens Axboe47f46762019-11-09 17:43:02 -07003339 }
3340
3341 return -ENOENT;
3342}
3343
Jens Axboe3529d8c2019-12-19 18:24:38 -07003344static int io_poll_remove_prep(struct io_kiocb *req,
3345 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003346{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003347 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3348 return -EINVAL;
3349 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3350 sqe->poll_events)
3351 return -EINVAL;
3352
Jens Axboe0969e782019-12-17 18:40:57 -07003353 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003354 return 0;
3355}
3356
3357/*
3358 * Find a running poll command that matches one specified in sqe->addr,
3359 * and remove it if found.
3360 */
3361static int io_poll_remove(struct io_kiocb *req)
3362{
3363 struct io_ring_ctx *ctx = req->ctx;
3364 u64 addr;
3365 int ret;
3366
Jens Axboe0969e782019-12-17 18:40:57 -07003367 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003368 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003369 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003370 spin_unlock_irq(&ctx->completion_lock);
3371
Jens Axboe78e19bb2019-11-06 15:21:34 -07003372 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003373 if (ret < 0)
3374 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003375 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003376 return 0;
3377}
3378
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003379static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003380{
Jackie Liua197f662019-11-08 08:09:12 -07003381 struct io_ring_ctx *ctx = req->ctx;
3382
Jens Axboe8c838782019-03-12 15:48:16 -06003383 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003384 if (error)
3385 io_cqring_fill_event(req, error);
3386 else
3387 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003388 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003389}
3390
Jens Axboe561fb042019-10-24 07:25:42 -06003391static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003392{
Jens Axboe561fb042019-10-24 07:25:42 -06003393 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003394 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3395 struct io_poll_iocb *poll = &req->poll;
3396 struct poll_table_struct pt = { ._key = poll->events };
3397 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003398 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003399 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003400 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003401
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003402 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003403 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003404 ret = -ECANCELED;
3405 } else if (READ_ONCE(poll->canceled)) {
3406 ret = -ECANCELED;
3407 }
Jens Axboe561fb042019-10-24 07:25:42 -06003408
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003409 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003410 mask = vfs_poll(poll->file, &pt) & poll->events;
3411
3412 /*
3413 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3414 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3415 * synchronize with them. In the cancellation case the list_del_init
3416 * itself is not actually needed, but harmless so we keep it in to
3417 * avoid further branches in the fast path.
3418 */
3419 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003420 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003421 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003422 spin_unlock_irq(&ctx->completion_lock);
3423 return;
3424 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003425 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003426 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003427 spin_unlock_irq(&ctx->completion_lock);
3428
Jens Axboe8c838782019-03-12 15:48:16 -06003429 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003430
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003431 if (ret < 0)
3432 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003433 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003434 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003435 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003436}
3437
Jens Axboee94f1412019-12-19 12:06:02 -07003438static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3439{
Jens Axboee94f1412019-12-19 12:06:02 -07003440 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003441 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003442
Jens Axboec6ca97b302019-12-28 12:11:08 -07003443 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003444 spin_lock_irq(&ctx->completion_lock);
3445 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3446 hash_del(&req->hash_node);
3447 io_poll_complete(req, req->result, 0);
3448
Jens Axboe8237e042019-12-28 10:48:22 -07003449 if (refcount_dec_and_test(&req->refs) &&
3450 !io_req_multi_free(&rb, req)) {
3451 req->flags |= REQ_F_COMP_LOCKED;
3452 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003453 }
3454 }
3455 spin_unlock_irq(&ctx->completion_lock);
3456
3457 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003458 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003459}
3460
3461static void io_poll_flush(struct io_wq_work **workptr)
3462{
3463 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3464 struct llist_node *nodes;
3465
3466 nodes = llist_del_all(&req->ctx->poll_llist);
3467 if (nodes)
3468 __io_poll_flush(req->ctx, nodes);
3469}
3470
Jens Axboe221c5eb2019-01-17 09:41:58 -07003471static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3472 void *key)
3473{
Jens Axboee9444752019-11-26 15:02:04 -07003474 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003475 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3476 struct io_ring_ctx *ctx = req->ctx;
3477 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003478
3479 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003480 if (mask && !(mask & poll->events))
3481 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003482
Jens Axboe392edb42019-12-09 17:52:20 -07003483 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003484
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003485 /*
3486 * Run completion inline if we can. We're using trylock here because
3487 * we are violating the completion_lock -> poll wq lock ordering.
3488 * If we have a link timeout we're going to need the completion_lock
3489 * for finalizing the request, mark us as having grabbed that already.
3490 */
Jens Axboee94f1412019-12-19 12:06:02 -07003491 if (mask) {
3492 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003493
Jens Axboee94f1412019-12-19 12:06:02 -07003494 if (llist_empty(&ctx->poll_llist) &&
3495 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3496 hash_del(&req->hash_node);
3497 io_poll_complete(req, mask, 0);
3498 req->flags |= REQ_F_COMP_LOCKED;
3499 io_put_req(req);
3500 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3501
3502 io_cqring_ev_posted(ctx);
3503 req = NULL;
3504 } else {
3505 req->result = mask;
3506 req->llist_node.next = NULL;
3507 /* if the list wasn't empty, we're done */
3508 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3509 req = NULL;
3510 else
3511 req->work.func = io_poll_flush;
3512 }
Jens Axboe8c838782019-03-12 15:48:16 -06003513 }
Jens Axboee94f1412019-12-19 12:06:02 -07003514 if (req)
3515 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003516
Jens Axboe221c5eb2019-01-17 09:41:58 -07003517 return 1;
3518}
3519
3520struct io_poll_table {
3521 struct poll_table_struct pt;
3522 struct io_kiocb *req;
3523 int error;
3524};
3525
3526static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3527 struct poll_table_struct *p)
3528{
3529 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3530
3531 if (unlikely(pt->req->poll.head)) {
3532 pt->error = -EINVAL;
3533 return;
3534 }
3535
3536 pt->error = 0;
3537 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003538 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003539}
3540
Jens Axboeeac406c2019-11-14 12:09:58 -07003541static void io_poll_req_insert(struct io_kiocb *req)
3542{
3543 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003544 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003545
Jens Axboe78076bb2019-12-04 19:56:40 -07003546 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3547 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003548}
3549
Jens Axboe3529d8c2019-12-19 18:24:38 -07003550static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003551{
3552 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003553 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003554
3555 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3556 return -EINVAL;
3557 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3558 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003559 if (!poll->file)
3560 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003561
Jens Axboe221c5eb2019-01-17 09:41:58 -07003562 events = READ_ONCE(sqe->poll_events);
3563 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003564 return 0;
3565}
3566
3567static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3568{
3569 struct io_poll_iocb *poll = &req->poll;
3570 struct io_ring_ctx *ctx = req->ctx;
3571 struct io_poll_table ipt;
3572 bool cancel = false;
3573 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003574
3575 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003576 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003577
Jens Axboe221c5eb2019-01-17 09:41:58 -07003578 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003579 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003580 poll->canceled = false;
3581
3582 ipt.pt._qproc = io_poll_queue_proc;
3583 ipt.pt._key = poll->events;
3584 ipt.req = req;
3585 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3586
3587 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003588 INIT_LIST_HEAD(&poll->wait.entry);
3589 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3590 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003591
Jens Axboe36703242019-07-25 10:20:18 -06003592 INIT_LIST_HEAD(&req->list);
3593
Jens Axboe221c5eb2019-01-17 09:41:58 -07003594 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003595
3596 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003597 if (likely(poll->head)) {
3598 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003599 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003600 if (ipt.error)
3601 cancel = true;
3602 ipt.error = 0;
3603 mask = 0;
3604 }
3605 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003606 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003607 else if (cancel)
3608 WRITE_ONCE(poll->canceled, true);
3609 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003610 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003611 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003612 }
Jens Axboe8c838782019-03-12 15:48:16 -06003613 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003614 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003615 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003616 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003617 spin_unlock_irq(&ctx->completion_lock);
3618
Jens Axboe8c838782019-03-12 15:48:16 -06003619 if (mask) {
3620 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003621 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003622 }
Jens Axboe8c838782019-03-12 15:48:16 -06003623 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003624}
3625
Jens Axboe5262f562019-09-17 12:26:57 -06003626static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3627{
Jens Axboead8a48a2019-11-15 08:49:11 -07003628 struct io_timeout_data *data = container_of(timer,
3629 struct io_timeout_data, timer);
3630 struct io_kiocb *req = data->req;
3631 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003632 unsigned long flags;
3633
Jens Axboe5262f562019-09-17 12:26:57 -06003634 atomic_inc(&ctx->cq_timeouts);
3635
3636 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003637 /*
Jens Axboe11365042019-10-16 09:08:32 -06003638 * We could be racing with timeout deletion. If the list is empty,
3639 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003640 */
Jens Axboe842f9612019-10-29 12:34:10 -06003641 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003642 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003643
Jens Axboe11365042019-10-16 09:08:32 -06003644 /*
3645 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003646 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003647 * pointer will be increased, otherwise other timeout reqs may
3648 * return in advance without waiting for enough wait_nr.
3649 */
3650 prev = req;
3651 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3652 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003653 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003654 }
Jens Axboe842f9612019-10-29 12:34:10 -06003655
Jens Axboe78e19bb2019-11-06 15:21:34 -07003656 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003657 io_commit_cqring(ctx);
3658 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3659
3660 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003661 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003662 io_put_req(req);
3663 return HRTIMER_NORESTART;
3664}
3665
Jens Axboe47f46762019-11-09 17:43:02 -07003666static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3667{
3668 struct io_kiocb *req;
3669 int ret = -ENOENT;
3670
3671 list_for_each_entry(req, &ctx->timeout_list, list) {
3672 if (user_data == req->user_data) {
3673 list_del_init(&req->list);
3674 ret = 0;
3675 break;
3676 }
3677 }
3678
3679 if (ret == -ENOENT)
3680 return ret;
3681
Jens Axboe2d283902019-12-04 11:08:05 -07003682 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003683 if (ret == -1)
3684 return -EALREADY;
3685
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003686 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003687 io_cqring_fill_event(req, -ECANCELED);
3688 io_put_req(req);
3689 return 0;
3690}
3691
Jens Axboe3529d8c2019-12-19 18:24:38 -07003692static int io_timeout_remove_prep(struct io_kiocb *req,
3693 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003694{
Jens Axboeb29472e2019-12-17 18:50:29 -07003695 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3696 return -EINVAL;
3697 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3698 return -EINVAL;
3699
3700 req->timeout.addr = READ_ONCE(sqe->addr);
3701 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3702 if (req->timeout.flags)
3703 return -EINVAL;
3704
Jens Axboeb29472e2019-12-17 18:50:29 -07003705 return 0;
3706}
3707
Jens Axboe11365042019-10-16 09:08:32 -06003708/*
3709 * Remove or update an existing timeout command
3710 */
Jens Axboefc4df992019-12-10 14:38:45 -07003711static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003712{
3713 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003714 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003715
Jens Axboe11365042019-10-16 09:08:32 -06003716 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003717 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003718
Jens Axboe47f46762019-11-09 17:43:02 -07003719 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003720 io_commit_cqring(ctx);
3721 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003722 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003723 if (ret < 0)
3724 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003725 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003726 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003727}
3728
Jens Axboe3529d8c2019-12-19 18:24:38 -07003729static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003730 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003731{
Jens Axboead8a48a2019-11-15 08:49:11 -07003732 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003733 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003734
Jens Axboead8a48a2019-11-15 08:49:11 -07003735 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003736 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003737 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003738 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003739 if (sqe->off && is_timeout_link)
3740 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003741 flags = READ_ONCE(sqe->timeout_flags);
3742 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003743 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003744
Jens Axboe26a61672019-12-20 09:02:01 -07003745 req->timeout.count = READ_ONCE(sqe->off);
3746
Jens Axboe3529d8c2019-12-19 18:24:38 -07003747 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003748 return -ENOMEM;
3749
3750 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003751 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003752 req->flags |= REQ_F_TIMEOUT;
3753
3754 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003755 return -EFAULT;
3756
Jens Axboe11365042019-10-16 09:08:32 -06003757 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003758 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003759 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003760 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003761
Jens Axboead8a48a2019-11-15 08:49:11 -07003762 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3763 return 0;
3764}
3765
Jens Axboefc4df992019-12-10 14:38:45 -07003766static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003767{
3768 unsigned count;
3769 struct io_ring_ctx *ctx = req->ctx;
3770 struct io_timeout_data *data;
3771 struct list_head *entry;
3772 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003773
Jens Axboe2d283902019-12-04 11:08:05 -07003774 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003775
Jens Axboe5262f562019-09-17 12:26:57 -06003776 /*
3777 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003778 * timeout event to be satisfied. If it isn't set, then this is
3779 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003780 */
Jens Axboe26a61672019-12-20 09:02:01 -07003781 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003782 if (!count) {
3783 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3784 spin_lock_irq(&ctx->completion_lock);
3785 entry = ctx->timeout_list.prev;
3786 goto add;
3787 }
Jens Axboe5262f562019-09-17 12:26:57 -06003788
3789 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003790 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003791
3792 /*
3793 * Insertion sort, ensuring the first entry in the list is always
3794 * the one we need first.
3795 */
Jens Axboe5262f562019-09-17 12:26:57 -06003796 spin_lock_irq(&ctx->completion_lock);
3797 list_for_each_prev(entry, &ctx->timeout_list) {
3798 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003799 unsigned nxt_sq_head;
3800 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003801 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003802
Jens Axboe93bd25b2019-11-11 23:34:31 -07003803 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3804 continue;
3805
yangerkun5da0fb12019-10-15 21:59:29 +08003806 /*
3807 * Since cached_sq_head + count - 1 can overflow, use type long
3808 * long to store it.
3809 */
3810 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003811 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3812 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003813
3814 /*
3815 * cached_sq_head may overflow, and it will never overflow twice
3816 * once there is some timeout req still be valid.
3817 */
3818 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003819 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003820
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003821 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003822 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003823
3824 /*
3825 * Sequence of reqs after the insert one and itself should
3826 * be adjusted because each timeout req consumes a slot.
3827 */
3828 span++;
3829 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003830 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003831 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003832add:
Jens Axboe5262f562019-09-17 12:26:57 -06003833 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003834 data->timer.function = io_timeout_fn;
3835 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003836 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003837 return 0;
3838}
3839
Jens Axboe62755e32019-10-28 21:49:21 -06003840static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003841{
Jens Axboe62755e32019-10-28 21:49:21 -06003842 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003843
Jens Axboe62755e32019-10-28 21:49:21 -06003844 return req->user_data == (unsigned long) data;
3845}
3846
Jens Axboee977d6d2019-11-05 12:39:45 -07003847static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003848{
Jens Axboe62755e32019-10-28 21:49:21 -06003849 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003850 int ret = 0;
3851
Jens Axboe62755e32019-10-28 21:49:21 -06003852 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3853 switch (cancel_ret) {
3854 case IO_WQ_CANCEL_OK:
3855 ret = 0;
3856 break;
3857 case IO_WQ_CANCEL_RUNNING:
3858 ret = -EALREADY;
3859 break;
3860 case IO_WQ_CANCEL_NOTFOUND:
3861 ret = -ENOENT;
3862 break;
3863 }
3864
Jens Axboee977d6d2019-11-05 12:39:45 -07003865 return ret;
3866}
3867
Jens Axboe47f46762019-11-09 17:43:02 -07003868static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3869 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003870 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003871{
3872 unsigned long flags;
3873 int ret;
3874
3875 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3876 if (ret != -ENOENT) {
3877 spin_lock_irqsave(&ctx->completion_lock, flags);
3878 goto done;
3879 }
3880
3881 spin_lock_irqsave(&ctx->completion_lock, flags);
3882 ret = io_timeout_cancel(ctx, sqe_addr);
3883 if (ret != -ENOENT)
3884 goto done;
3885 ret = io_poll_cancel(ctx, sqe_addr);
3886done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003887 if (!ret)
3888 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003889 io_cqring_fill_event(req, ret);
3890 io_commit_cqring(ctx);
3891 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3892 io_cqring_ev_posted(ctx);
3893
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003894 if (ret < 0)
3895 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003896 io_put_req_find_next(req, nxt);
3897}
3898
Jens Axboe3529d8c2019-12-19 18:24:38 -07003899static int io_async_cancel_prep(struct io_kiocb *req,
3900 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003901{
Jens Axboefbf23842019-12-17 18:45:56 -07003902 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003903 return -EINVAL;
3904 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3905 sqe->cancel_flags)
3906 return -EINVAL;
3907
Jens Axboefbf23842019-12-17 18:45:56 -07003908 req->cancel.addr = READ_ONCE(sqe->addr);
3909 return 0;
3910}
3911
3912static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3913{
3914 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003915
3916 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003917 return 0;
3918}
3919
Jens Axboe05f3fb32019-12-09 11:22:50 -07003920static int io_files_update_prep(struct io_kiocb *req,
3921 const struct io_uring_sqe *sqe)
3922{
3923 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3924 return -EINVAL;
3925
3926 req->files_update.offset = READ_ONCE(sqe->off);
3927 req->files_update.nr_args = READ_ONCE(sqe->len);
3928 if (!req->files_update.nr_args)
3929 return -EINVAL;
3930 req->files_update.arg = READ_ONCE(sqe->addr);
3931 return 0;
3932}
3933
3934static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3935{
3936 struct io_ring_ctx *ctx = req->ctx;
3937 struct io_uring_files_update up;
3938 int ret;
3939
3940 if (force_nonblock) {
3941 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3942 return -EAGAIN;
3943 }
3944
3945 up.offset = req->files_update.offset;
3946 up.fds = req->files_update.arg;
3947
3948 mutex_lock(&ctx->uring_lock);
3949 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3950 mutex_unlock(&ctx->uring_lock);
3951
3952 if (ret < 0)
3953 req_set_fail_links(req);
3954 io_cqring_add_event(req, ret);
3955 io_put_req(req);
3956 return 0;
3957}
3958
Jens Axboe3529d8c2019-12-19 18:24:38 -07003959static int io_req_defer_prep(struct io_kiocb *req,
3960 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003961{
Jens Axboee7815732019-12-17 19:45:06 -07003962 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003963
Jens Axboed625c6e2019-12-17 19:53:05 -07003964 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003965 case IORING_OP_NOP:
3966 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003967 case IORING_OP_READV:
3968 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003969 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003970 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003971 break;
3972 case IORING_OP_WRITEV:
3973 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003974 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003975 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003976 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003977 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003978 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003979 break;
3980 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003981 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003982 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003983 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003984 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003985 break;
3986 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003987 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003988 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003989 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003990 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003991 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003992 break;
3993 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003994 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003995 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003996 break;
Jens Axboef499a022019-12-02 16:28:46 -07003997 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003998 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003999 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004000 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004001 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004002 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004003 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004004 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004005 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004006 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004007 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004008 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004009 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004010 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004011 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004012 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004013 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004014 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004015 case IORING_OP_FALLOCATE:
4016 ret = io_fallocate_prep(req, sqe);
4017 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004018 case IORING_OP_OPENAT:
4019 ret = io_openat_prep(req, sqe);
4020 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004021 case IORING_OP_CLOSE:
4022 ret = io_close_prep(req, sqe);
4023 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004024 case IORING_OP_FILES_UPDATE:
4025 ret = io_files_update_prep(req, sqe);
4026 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004027 case IORING_OP_STATX:
4028 ret = io_statx_prep(req, sqe);
4029 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004030 case IORING_OP_FADVISE:
4031 ret = io_fadvise_prep(req, sqe);
4032 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004033 case IORING_OP_MADVISE:
4034 ret = io_madvise_prep(req, sqe);
4035 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004036 case IORING_OP_OPENAT2:
4037 ret = io_openat2_prep(req, sqe);
4038 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004039 default:
Jens Axboee7815732019-12-17 19:45:06 -07004040 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4041 req->opcode);
4042 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004043 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004044 }
4045
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004046 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004047}
4048
Jens Axboe3529d8c2019-12-19 18:24:38 -07004049static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004050{
Jackie Liua197f662019-11-08 08:09:12 -07004051 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004052 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004053
Bob Liu9d858b22019-11-13 18:06:25 +08004054 /* Still need defer if there is pending req in defer list. */
4055 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004056 return 0;
4057
Jens Axboe3529d8c2019-12-19 18:24:38 -07004058 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004059 return -EAGAIN;
4060
Jens Axboe3529d8c2019-12-19 18:24:38 -07004061 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004062 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004063 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004064
Jens Axboede0617e2019-04-06 21:51:27 -06004065 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004066 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004067 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004068 return 0;
4069 }
4070
Jens Axboe915967f2019-11-21 09:01:20 -07004071 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004072 list_add_tail(&req->list, &ctx->defer_list);
4073 spin_unlock_irq(&ctx->completion_lock);
4074 return -EIOCBQUEUED;
4075}
4076
Jens Axboe3529d8c2019-12-19 18:24:38 -07004077static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4078 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004079{
Jackie Liua197f662019-11-08 08:09:12 -07004080 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004081 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004082
Jens Axboed625c6e2019-12-17 19:53:05 -07004083 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004084 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004085 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004086 break;
4087 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004088 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004089 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004090 if (sqe) {
4091 ret = io_read_prep(req, sqe, force_nonblock);
4092 if (ret < 0)
4093 break;
4094 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004095 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004096 break;
4097 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004098 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004099 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004100 if (sqe) {
4101 ret = io_write_prep(req, sqe, force_nonblock);
4102 if (ret < 0)
4103 break;
4104 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004105 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004106 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004107 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004108 if (sqe) {
4109 ret = io_prep_fsync(req, sqe);
4110 if (ret < 0)
4111 break;
4112 }
Jens Axboefc4df992019-12-10 14:38:45 -07004113 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004114 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004115 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004116 if (sqe) {
4117 ret = io_poll_add_prep(req, sqe);
4118 if (ret)
4119 break;
4120 }
Jens Axboefc4df992019-12-10 14:38:45 -07004121 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004122 break;
4123 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004124 if (sqe) {
4125 ret = io_poll_remove_prep(req, sqe);
4126 if (ret < 0)
4127 break;
4128 }
Jens Axboefc4df992019-12-10 14:38:45 -07004129 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004130 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004131 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004132 if (sqe) {
4133 ret = io_prep_sfr(req, sqe);
4134 if (ret < 0)
4135 break;
4136 }
Jens Axboefc4df992019-12-10 14:38:45 -07004137 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004138 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004139 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004140 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004141 if (sqe) {
4142 ret = io_sendmsg_prep(req, sqe);
4143 if (ret < 0)
4144 break;
4145 }
Jens Axboefddafac2020-01-04 20:19:44 -07004146 if (req->opcode == IORING_OP_SENDMSG)
4147 ret = io_sendmsg(req, nxt, force_nonblock);
4148 else
4149 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004150 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004151 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004152 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004153 if (sqe) {
4154 ret = io_recvmsg_prep(req, sqe);
4155 if (ret)
4156 break;
4157 }
Jens Axboefddafac2020-01-04 20:19:44 -07004158 if (req->opcode == IORING_OP_RECVMSG)
4159 ret = io_recvmsg(req, nxt, force_nonblock);
4160 else
4161 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004162 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004163 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004164 if (sqe) {
4165 ret = io_timeout_prep(req, sqe, false);
4166 if (ret)
4167 break;
4168 }
Jens Axboefc4df992019-12-10 14:38:45 -07004169 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004170 break;
Jens Axboe11365042019-10-16 09:08:32 -06004171 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004172 if (sqe) {
4173 ret = io_timeout_remove_prep(req, sqe);
4174 if (ret)
4175 break;
4176 }
Jens Axboefc4df992019-12-10 14:38:45 -07004177 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004178 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004179 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004180 if (sqe) {
4181 ret = io_accept_prep(req, sqe);
4182 if (ret)
4183 break;
4184 }
Jens Axboefc4df992019-12-10 14:38:45 -07004185 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004186 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004187 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004188 if (sqe) {
4189 ret = io_connect_prep(req, sqe);
4190 if (ret)
4191 break;
4192 }
Jens Axboefc4df992019-12-10 14:38:45 -07004193 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004194 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004195 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004196 if (sqe) {
4197 ret = io_async_cancel_prep(req, sqe);
4198 if (ret)
4199 break;
4200 }
Jens Axboefc4df992019-12-10 14:38:45 -07004201 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004202 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004203 case IORING_OP_FALLOCATE:
4204 if (sqe) {
4205 ret = io_fallocate_prep(req, sqe);
4206 if (ret)
4207 break;
4208 }
4209 ret = io_fallocate(req, nxt, force_nonblock);
4210 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004211 case IORING_OP_OPENAT:
4212 if (sqe) {
4213 ret = io_openat_prep(req, sqe);
4214 if (ret)
4215 break;
4216 }
4217 ret = io_openat(req, nxt, force_nonblock);
4218 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004219 case IORING_OP_CLOSE:
4220 if (sqe) {
4221 ret = io_close_prep(req, sqe);
4222 if (ret)
4223 break;
4224 }
4225 ret = io_close(req, nxt, force_nonblock);
4226 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004227 case IORING_OP_FILES_UPDATE:
4228 if (sqe) {
4229 ret = io_files_update_prep(req, sqe);
4230 if (ret)
4231 break;
4232 }
4233 ret = io_files_update(req, force_nonblock);
4234 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004235 case IORING_OP_STATX:
4236 if (sqe) {
4237 ret = io_statx_prep(req, sqe);
4238 if (ret)
4239 break;
4240 }
4241 ret = io_statx(req, nxt, force_nonblock);
4242 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004243 case IORING_OP_FADVISE:
4244 if (sqe) {
4245 ret = io_fadvise_prep(req, sqe);
4246 if (ret)
4247 break;
4248 }
4249 ret = io_fadvise(req, nxt, force_nonblock);
4250 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004251 case IORING_OP_MADVISE:
4252 if (sqe) {
4253 ret = io_madvise_prep(req, sqe);
4254 if (ret)
4255 break;
4256 }
4257 ret = io_madvise(req, nxt, force_nonblock);
4258 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004259 case IORING_OP_OPENAT2:
4260 if (sqe) {
4261 ret = io_openat2_prep(req, sqe);
4262 if (ret)
4263 break;
4264 }
4265 ret = io_openat2(req, nxt, force_nonblock);
4266 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004267 default:
4268 ret = -EINVAL;
4269 break;
4270 }
4271
Jens Axboedef596e2019-01-09 08:59:42 -07004272 if (ret)
4273 return ret;
4274
4275 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004276 const bool in_async = io_wq_current_is_worker();
4277
Jens Axboe9e645e112019-05-10 16:07:28 -06004278 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004279 return -EAGAIN;
4280
Jens Axboe11ba8202020-01-15 21:51:17 -07004281 /* workqueue context doesn't hold uring_lock, grab it now */
4282 if (in_async)
4283 mutex_lock(&ctx->uring_lock);
4284
Jens Axboedef596e2019-01-09 08:59:42 -07004285 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004286
4287 if (in_async)
4288 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004289 }
4290
4291 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004292}
4293
Jens Axboe561fb042019-10-24 07:25:42 -06004294static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004295{
Jens Axboe561fb042019-10-24 07:25:42 -06004296 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004297 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004298 struct io_kiocb *nxt = NULL;
4299 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004300
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004301 /* if NO_CANCEL is set, we must still run the work */
4302 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4303 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004304 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004305 }
Jens Axboe31b51512019-01-18 22:56:34 -07004306
Jens Axboe561fb042019-10-24 07:25:42 -06004307 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004308 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4309 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004310 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004311 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004312 /*
4313 * We can get EAGAIN for polled IO even though we're
4314 * forcing a sync submission from here, since we can't
4315 * wait for request slots on the block side.
4316 */
4317 if (ret != -EAGAIN)
4318 break;
4319 cond_resched();
4320 } while (1);
4321 }
Jens Axboe31b51512019-01-18 22:56:34 -07004322
Jens Axboe561fb042019-10-24 07:25:42 -06004323 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004324 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004325
Jens Axboe561fb042019-10-24 07:25:42 -06004326 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004327 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004328 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004329 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004330 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004331
Jens Axboe561fb042019-10-24 07:25:42 -06004332 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004333 if (!ret && nxt)
4334 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004335}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004336
Jens Axboe15b71ab2019-12-11 11:20:36 -07004337static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004338{
Jens Axboed3656342019-12-18 09:50:26 -07004339 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004340 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004341 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4342 return 0;
4343 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004344}
4345
Jens Axboe65e19f52019-10-26 07:20:21 -06004346static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4347 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004348{
Jens Axboe65e19f52019-10-26 07:20:21 -06004349 struct fixed_file_table *table;
4350
Jens Axboe05f3fb32019-12-09 11:22:50 -07004351 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4352 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004353}
4354
Jens Axboe3529d8c2019-12-19 18:24:38 -07004355static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4356 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004357{
Jackie Liua197f662019-11-08 08:09:12 -07004358 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004359 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004360 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004361
Jens Axboe3529d8c2019-12-19 18:24:38 -07004362 flags = READ_ONCE(sqe->flags);
4363 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004364
Jackie Liu4fe2c962019-09-09 20:50:40 +08004365 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06004366 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06004367
Jens Axboed3656342019-12-18 09:50:26 -07004368 if (!io_req_needs_file(req, fd))
4369 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004370
4371 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004372 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004373 (unsigned) fd >= ctx->nr_user_files))
4374 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004375 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004376 req->file = io_file_from_index(ctx, fd);
4377 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004378 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004379 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004380 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004381 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004382 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004383 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004384 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004385 req->file = io_file_get(state, fd);
4386 if (unlikely(!req->file))
4387 return -EBADF;
4388 }
4389
4390 return 0;
4391}
4392
Jackie Liua197f662019-11-08 08:09:12 -07004393static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004394{
Jens Axboefcb323c2019-10-24 12:39:47 -06004395 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004396 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004397
Jens Axboeb5dba592019-12-11 14:02:38 -07004398 if (!req->ring_file)
4399 return -EBADF;
4400
Jens Axboefcb323c2019-10-24 12:39:47 -06004401 rcu_read_lock();
4402 spin_lock_irq(&ctx->inflight_lock);
4403 /*
4404 * We use the f_ops->flush() handler to ensure that we can flush
4405 * out work accessing these files if the fd is closed. Check if
4406 * the fd has changed since we started down this path, and disallow
4407 * this operation if it has.
4408 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004409 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004410 list_add(&req->inflight_entry, &ctx->inflight_list);
4411 req->flags |= REQ_F_INFLIGHT;
4412 req->work.files = current->files;
4413 ret = 0;
4414 }
4415 spin_unlock_irq(&ctx->inflight_lock);
4416 rcu_read_unlock();
4417
4418 return ret;
4419}
4420
Jens Axboe2665abf2019-11-05 12:40:47 -07004421static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4422{
Jens Axboead8a48a2019-11-15 08:49:11 -07004423 struct io_timeout_data *data = container_of(timer,
4424 struct io_timeout_data, timer);
4425 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004426 struct io_ring_ctx *ctx = req->ctx;
4427 struct io_kiocb *prev = NULL;
4428 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004429
4430 spin_lock_irqsave(&ctx->completion_lock, flags);
4431
4432 /*
4433 * We don't expect the list to be empty, that will only happen if we
4434 * race with the completion of the linked work.
4435 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004436 if (!list_empty(&req->link_list)) {
4437 prev = list_entry(req->link_list.prev, struct io_kiocb,
4438 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004439 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004440 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004441 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4442 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004443 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004444 }
4445
4446 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4447
4448 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004449 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004450 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4451 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004452 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004453 } else {
4454 io_cqring_add_event(req, -ETIME);
4455 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004456 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004457 return HRTIMER_NORESTART;
4458}
4459
Jens Axboead8a48a2019-11-15 08:49:11 -07004460static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004461{
Jens Axboe76a46e02019-11-10 23:34:16 -07004462 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004463
Jens Axboe76a46e02019-11-10 23:34:16 -07004464 /*
4465 * If the list is now empty, then our linked request finished before
4466 * we got a chance to setup the timer
4467 */
4468 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004469 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004470 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004471
Jens Axboead8a48a2019-11-15 08:49:11 -07004472 data->timer.function = io_link_timeout_fn;
4473 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4474 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004475 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004476 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004477
Jens Axboe2665abf2019-11-05 12:40:47 -07004478 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004479 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004480}
4481
Jens Axboead8a48a2019-11-15 08:49:11 -07004482static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004483{
4484 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004485
Jens Axboe2665abf2019-11-05 12:40:47 -07004486 if (!(req->flags & REQ_F_LINK))
4487 return NULL;
4488
Pavel Begunkov44932332019-12-05 16:16:35 +03004489 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4490 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004491 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004492 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004493
Jens Axboe76a46e02019-11-10 23:34:16 -07004494 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004495 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004496}
4497
Jens Axboe3529d8c2019-12-19 18:24:38 -07004498static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004499{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004500 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004501 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004502 int ret;
4503
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004504again:
4505 linked_timeout = io_prep_linked_timeout(req);
4506
Jens Axboe3529d8c2019-12-19 18:24:38 -07004507 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004508
4509 /*
4510 * We async punt it if the file wasn't marked NOWAIT, or if the file
4511 * doesn't support non-blocking read/write attempts
4512 */
4513 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4514 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004515 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4516 ret = io_grab_files(req);
4517 if (ret)
4518 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004519 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004520
4521 /*
4522 * Queued up for async execution, worker will release
4523 * submit reference when the iocb is actually submitted.
4524 */
4525 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004526 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004527 }
Jens Axboee65ef562019-03-12 10:16:44 -06004528
Jens Axboefcb323c2019-10-24 12:39:47 -06004529err:
Jens Axboee65ef562019-03-12 10:16:44 -06004530 /* drop submission reference */
4531 io_put_req(req);
4532
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004533 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004534 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004535 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004536 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004537 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004538 }
4539
Jens Axboee65ef562019-03-12 10:16:44 -06004540 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004541 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004542 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004543 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004544 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004545 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004546done_req:
4547 if (nxt) {
4548 req = nxt;
4549 nxt = NULL;
4550 goto again;
4551 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004552}
4553
Jens Axboe3529d8c2019-12-19 18:24:38 -07004554static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004555{
4556 int ret;
4557
Jens Axboe3529d8c2019-12-19 18:24:38 -07004558 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004559 if (ret) {
4560 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004561 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004562 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004563 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004564 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004565 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboece35a472019-12-17 08:04:44 -07004566 /*
4567 * Never try inline submit of IOSQE_ASYNC is set, go straight
4568 * to async execution.
4569 */
4570 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4571 io_queue_async_work(req);
4572 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004573 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004574 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004575}
4576
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004577static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004578{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004579 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004580 io_cqring_add_event(req, -ECANCELED);
4581 io_double_put_req(req);
4582 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004583 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004584}
4585
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004586#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004587 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004588
Jens Axboe3529d8c2019-12-19 18:24:38 -07004589static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4590 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004591{
Jackie Liua197f662019-11-08 08:09:12 -07004592 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004593 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004594 int ret;
4595
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004596 sqe_flags = READ_ONCE(sqe->flags);
4597
Jens Axboe9e645e112019-05-10 16:07:28 -06004598 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004599 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004600 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004601 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004602 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004603 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004604 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004605
Jens Axboe3529d8c2019-12-19 18:24:38 -07004606 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004607 if (unlikely(ret)) {
4608err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004609 io_cqring_add_event(req, ret);
4610 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004611 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004612 }
4613
Jens Axboe9e645e112019-05-10 16:07:28 -06004614 /*
4615 * If we already have a head request, queue this one for async
4616 * submittal once the head completes. If we don't have a head but
4617 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4618 * submitted sync once the chain is complete. If none of those
4619 * conditions are true (normal request), then just queue it.
4620 */
4621 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004622 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004623
Pavel Begunkov711be032020-01-17 03:57:59 +03004624 if (sqe_flags & IOSQE_IO_DRAIN) {
4625 head->flags |= REQ_F_IO_DRAIN;
4626 ctx->drain_next = 1;
4627 }
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004628
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004629 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004630 req->flags |= REQ_F_HARDLINK;
4631
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004632 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004633 ret = -EAGAIN;
4634 goto err_req;
4635 }
4636
Jens Axboe3529d8c2019-12-19 18:24:38 -07004637 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004638 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004639 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004640 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004641 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004642 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004643 trace_io_uring_link(ctx, req, head);
4644 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004645
4646 /* last request of a link, enqueue the link */
4647 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4648 io_queue_link_head(head);
4649 *link = NULL;
4650 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004651 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004652 if (unlikely(ctx->drain_next)) {
4653 req->flags |= REQ_F_IO_DRAIN;
4654 req->ctx->drain_next = 0;
4655 }
4656 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4657 req->flags |= REQ_F_LINK;
4658 if (sqe_flags & IOSQE_IO_HARDLINK)
4659 req->flags |= REQ_F_HARDLINK;
4660
4661 INIT_LIST_HEAD(&req->link_list);
4662 ret = io_req_defer_prep(req, sqe);
4663 if (ret)
4664 req->flags |= REQ_F_FAIL_LINK;
4665 *link = req;
4666 } else {
4667 io_queue_sqe(req, sqe);
4668 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004669 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004670
4671 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004672}
4673
Jens Axboe9a56a232019-01-09 09:06:50 -07004674/*
4675 * Batched submission is done, ensure local IO is flushed out.
4676 */
4677static void io_submit_state_end(struct io_submit_state *state)
4678{
4679 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004680 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004681 if (state->free_reqs)
4682 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4683 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004684}
4685
4686/*
4687 * Start submission side cache.
4688 */
4689static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004690 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004691{
4692 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004693 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004694 state->file = NULL;
4695 state->ios_left = max_ios;
4696}
4697
Jens Axboe2b188cc2019-01-07 10:46:33 -07004698static void io_commit_sqring(struct io_ring_ctx *ctx)
4699{
Hristo Venev75b28af2019-08-26 17:23:46 +00004700 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004701
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004702 /*
4703 * Ensure any loads from the SQEs are done at this point,
4704 * since once we write the new head, the application could
4705 * write new data to them.
4706 */
4707 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004708}
4709
4710/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004711 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004712 * that is mapped by userspace. This means that care needs to be taken to
4713 * ensure that reads are stable, as we cannot rely on userspace always
4714 * being a good citizen. If members of the sqe are validated and then later
4715 * used, it's important that those reads are done through READ_ONCE() to
4716 * prevent a re-load down the line.
4717 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004718static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4719 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004720{
Hristo Venev75b28af2019-08-26 17:23:46 +00004721 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004722 unsigned head;
4723
4724 /*
4725 * The cached sq head (or cq tail) serves two purposes:
4726 *
4727 * 1) allows us to batch the cost of updating the user visible
4728 * head updates.
4729 * 2) allows the kernel side to track the head on its own, even
4730 * though the application is the one updating it.
4731 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004732 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004733 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004734 /*
4735 * All io need record the previous position, if LINK vs DARIN,
4736 * it can be used to mark the position of the first IO in the
4737 * link list.
4738 */
4739 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004740 *sqe_ptr = &ctx->sq_sqes[head];
4741 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4742 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004743 ctx->cached_sq_head++;
4744 return true;
4745 }
4746
4747 /* drop invalid entries */
4748 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004749 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004750 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004751 return false;
4752}
4753
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004754static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004755 struct file *ring_file, int ring_fd,
4756 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004757{
4758 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004759 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004760 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004761 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004762
Jens Axboec4a2ed72019-11-21 21:01:26 -07004763 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004764 if (test_bit(0, &ctx->sq_check_overflow)) {
4765 if (!list_empty(&ctx->cq_overflow_list) &&
4766 !io_cqring_overflow_flush(ctx, false))
4767 return -EBUSY;
4768 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004769
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004770 /* make sure SQ entry isn't read before tail */
4771 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004772
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004773 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4774 return -EAGAIN;
4775
Jens Axboe6c271ce2019-01-10 11:22:30 -07004776 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004777 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004778 statep = &state;
4779 }
4780
4781 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004782 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004783 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004784
Pavel Begunkov196be952019-11-07 01:41:06 +03004785 req = io_get_req(ctx, statep);
4786 if (unlikely(!req)) {
4787 if (!submitted)
4788 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004789 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004790 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004791 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004792 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004793 break;
4794 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004795
Jens Axboed3656342019-12-18 09:50:26 -07004796 /* will complete beyond this point, count as submitted */
4797 submitted++;
4798
4799 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4800 io_cqring_add_event(req, -EINVAL);
4801 io_double_put_req(req);
4802 break;
4803 }
4804
4805 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004806 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4807 if (!mm_fault) {
4808 use_mm(ctx->sqo_mm);
4809 *mm = ctx->sqo_mm;
4810 }
4811 }
4812
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004813 req->ring_file = ring_file;
4814 req->ring_fd = ring_fd;
4815 req->has_user = *mm != NULL;
4816 req->in_async = async;
4817 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07004818 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
4819 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004820 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004821 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004822 }
4823
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004824 if (submitted != nr)
4825 percpu_ref_put_many(&ctx->refs, nr - submitted);
Jens Axboe9e645e112019-05-10 16:07:28 -06004826 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004827 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004828 if (statep)
4829 io_submit_state_end(&state);
4830
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004831 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4832 io_commit_sqring(ctx);
4833
Jens Axboe6c271ce2019-01-10 11:22:30 -07004834 return submitted;
4835}
4836
4837static int io_sq_thread(void *data)
4838{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004839 struct io_ring_ctx *ctx = data;
4840 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004841 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004842 mm_segment_t old_fs;
4843 DEFINE_WAIT(wait);
4844 unsigned inflight;
4845 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004846 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004847
Jens Axboe206aefd2019-11-07 18:27:42 -07004848 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004849
Jens Axboe6c271ce2019-01-10 11:22:30 -07004850 old_fs = get_fs();
4851 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004852 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004853
Jens Axboec1edbf52019-11-10 16:56:04 -07004854 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004855 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004856 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004857
4858 if (inflight) {
4859 unsigned nr_events = 0;
4860
4861 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004862 /*
4863 * inflight is the count of the maximum possible
4864 * entries we submitted, but it can be smaller
4865 * if we dropped some of them. If we don't have
4866 * poll entries available, then we know that we
4867 * have nothing left to poll for. Reset the
4868 * inflight count to zero in that case.
4869 */
4870 mutex_lock(&ctx->uring_lock);
4871 if (!list_empty(&ctx->poll_list))
4872 __io_iopoll_check(ctx, &nr_events, 0);
4873 else
4874 inflight = 0;
4875 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004876 } else {
4877 /*
4878 * Normal IO, just pretend everything completed.
4879 * We don't have to poll completions for that.
4880 */
4881 nr_events = inflight;
4882 }
4883
4884 inflight -= nr_events;
4885 if (!inflight)
4886 timeout = jiffies + ctx->sq_thread_idle;
4887 }
4888
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004889 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004890
4891 /*
4892 * If submit got -EBUSY, flag us as needing the application
4893 * to enter the kernel to reap and flush events.
4894 */
4895 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004896 /*
4897 * We're polling. If we're within the defined idle
4898 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004899 * to sleep. The exception is if we got EBUSY doing
4900 * more IO, we should wait for the application to
4901 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004902 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004903 if (inflight ||
4904 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004905 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004906 continue;
4907 }
4908
4909 /*
4910 * Drop cur_mm before scheduling, we can't hold it for
4911 * long periods (or over schedule()). Do this before
4912 * adding ourselves to the waitqueue, as the unuse/drop
4913 * may sleep.
4914 */
4915 if (cur_mm) {
4916 unuse_mm(cur_mm);
4917 mmput(cur_mm);
4918 cur_mm = NULL;
4919 }
4920
4921 prepare_to_wait(&ctx->sqo_wait, &wait,
4922 TASK_INTERRUPTIBLE);
4923
4924 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004925 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004926 /* make sure to read SQ tail after writing flags */
4927 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004928
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004929 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004930 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004931 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004932 finish_wait(&ctx->sqo_wait, &wait);
4933 break;
4934 }
4935 if (signal_pending(current))
4936 flush_signals(current);
4937 schedule();
4938 finish_wait(&ctx->sqo_wait, &wait);
4939
Hristo Venev75b28af2019-08-26 17:23:46 +00004940 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004941 continue;
4942 }
4943 finish_wait(&ctx->sqo_wait, &wait);
4944
Hristo Venev75b28af2019-08-26 17:23:46 +00004945 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004946 }
4947
Jens Axboe8a4955f2019-12-09 14:52:35 -07004948 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004949 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004950 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004951 if (ret > 0)
4952 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004953 }
4954
4955 set_fs(old_fs);
4956 if (cur_mm) {
4957 unuse_mm(cur_mm);
4958 mmput(cur_mm);
4959 }
Jens Axboe181e4482019-11-25 08:52:30 -07004960 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004961
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004962 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004963
Jens Axboe6c271ce2019-01-10 11:22:30 -07004964 return 0;
4965}
4966
Jens Axboebda52162019-09-24 13:47:15 -06004967struct io_wait_queue {
4968 struct wait_queue_entry wq;
4969 struct io_ring_ctx *ctx;
4970 unsigned to_wait;
4971 unsigned nr_timeouts;
4972};
4973
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004974static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004975{
4976 struct io_ring_ctx *ctx = iowq->ctx;
4977
4978 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004979 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004980 * started waiting. For timeouts, we always want to return to userspace,
4981 * regardless of event count.
4982 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004983 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004984 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4985}
4986
4987static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4988 int wake_flags, void *key)
4989{
4990 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4991 wq);
4992
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004993 /* use noflush == true, as we can't safely rely on locking context */
4994 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004995 return -1;
4996
4997 return autoremove_wake_function(curr, mode, wake_flags, key);
4998}
4999
Jens Axboe2b188cc2019-01-07 10:46:33 -07005000/*
5001 * Wait until events become available, if we don't already have some. The
5002 * application must reap them itself, as they reside on the shared cq ring.
5003 */
5004static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5005 const sigset_t __user *sig, size_t sigsz)
5006{
Jens Axboebda52162019-09-24 13:47:15 -06005007 struct io_wait_queue iowq = {
5008 .wq = {
5009 .private = current,
5010 .func = io_wake_function,
5011 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5012 },
5013 .ctx = ctx,
5014 .to_wait = min_events,
5015 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005016 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005017 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005018
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005019 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005020 return 0;
5021
5022 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005023#ifdef CONFIG_COMPAT
5024 if (in_compat_syscall())
5025 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005026 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005027 else
5028#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005029 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005030
Jens Axboe2b188cc2019-01-07 10:46:33 -07005031 if (ret)
5032 return ret;
5033 }
5034
Jens Axboebda52162019-09-24 13:47:15 -06005035 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005036 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005037 do {
5038 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5039 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005040 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005041 break;
5042 schedule();
5043 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005044 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005045 break;
5046 }
5047 } while (1);
5048 finish_wait(&ctx->wait, &iowq.wq);
5049
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005050 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005051
Hristo Venev75b28af2019-08-26 17:23:46 +00005052 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005053}
5054
Jens Axboe6b063142019-01-10 22:13:58 -07005055static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5056{
5057#if defined(CONFIG_UNIX)
5058 if (ctx->ring_sock) {
5059 struct sock *sock = ctx->ring_sock->sk;
5060 struct sk_buff *skb;
5061
5062 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5063 kfree_skb(skb);
5064 }
5065#else
5066 int i;
5067
Jens Axboe65e19f52019-10-26 07:20:21 -06005068 for (i = 0; i < ctx->nr_user_files; i++) {
5069 struct file *file;
5070
5071 file = io_file_from_index(ctx, i);
5072 if (file)
5073 fput(file);
5074 }
Jens Axboe6b063142019-01-10 22:13:58 -07005075#endif
5076}
5077
Jens Axboe05f3fb32019-12-09 11:22:50 -07005078static void io_file_ref_kill(struct percpu_ref *ref)
5079{
5080 struct fixed_file_data *data;
5081
5082 data = container_of(ref, struct fixed_file_data, refs);
5083 complete(&data->done);
5084}
5085
Jens Axboe6b063142019-01-10 22:13:58 -07005086static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5087{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005088 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005089 unsigned nr_tables, i;
5090
Jens Axboe05f3fb32019-12-09 11:22:50 -07005091 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005092 return -ENXIO;
5093
Jens Axboe05f3fb32019-12-09 11:22:50 -07005094 /* protect against inflight atomic switch, which drops the ref */
5095 flush_work(&data->ref_work);
5096 percpu_ref_get(&data->refs);
5097 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5098 wait_for_completion(&data->done);
5099 percpu_ref_put(&data->refs);
5100 percpu_ref_exit(&data->refs);
5101
Jens Axboe6b063142019-01-10 22:13:58 -07005102 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005103 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5104 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005105 kfree(data->table[i].files);
5106 kfree(data->table);
5107 kfree(data);
5108 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005109 ctx->nr_user_files = 0;
5110 return 0;
5111}
5112
Jens Axboe6c271ce2019-01-10 11:22:30 -07005113static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5114{
5115 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005116 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005117 /*
5118 * The park is a bit of a work-around, without it we get
5119 * warning spews on shutdown with SQPOLL set and affinity
5120 * set to a single CPU.
5121 */
Jens Axboe06058632019-04-13 09:26:03 -06005122 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005123 kthread_stop(ctx->sqo_thread);
5124 ctx->sqo_thread = NULL;
5125 }
5126}
5127
Jens Axboe6b063142019-01-10 22:13:58 -07005128static void io_finish_async(struct io_ring_ctx *ctx)
5129{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005130 io_sq_thread_stop(ctx);
5131
Jens Axboe561fb042019-10-24 07:25:42 -06005132 if (ctx->io_wq) {
5133 io_wq_destroy(ctx->io_wq);
5134 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005135 }
5136}
5137
5138#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005139/*
5140 * Ensure the UNIX gc is aware of our file set, so we are certain that
5141 * the io_uring can be safely unregistered on process exit, even if we have
5142 * loops in the file referencing.
5143 */
5144static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5145{
5146 struct sock *sk = ctx->ring_sock->sk;
5147 struct scm_fp_list *fpl;
5148 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005149 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005150
5151 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5152 unsigned long inflight = ctx->user->unix_inflight + nr;
5153
5154 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5155 return -EMFILE;
5156 }
5157
5158 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5159 if (!fpl)
5160 return -ENOMEM;
5161
5162 skb = alloc_skb(0, GFP_KERNEL);
5163 if (!skb) {
5164 kfree(fpl);
5165 return -ENOMEM;
5166 }
5167
5168 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005169
Jens Axboe08a45172019-10-03 08:11:03 -06005170 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005171 fpl->user = get_uid(ctx->user);
5172 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005173 struct file *file = io_file_from_index(ctx, i + offset);
5174
5175 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005176 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005177 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005178 unix_inflight(fpl->user, fpl->fp[nr_files]);
5179 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005180 }
5181
Jens Axboe08a45172019-10-03 08:11:03 -06005182 if (nr_files) {
5183 fpl->max = SCM_MAX_FD;
5184 fpl->count = nr_files;
5185 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005186 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005187 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5188 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005189
Jens Axboe08a45172019-10-03 08:11:03 -06005190 for (i = 0; i < nr_files; i++)
5191 fput(fpl->fp[i]);
5192 } else {
5193 kfree_skb(skb);
5194 kfree(fpl);
5195 }
Jens Axboe6b063142019-01-10 22:13:58 -07005196
5197 return 0;
5198}
5199
5200/*
5201 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5202 * causes regular reference counting to break down. We rely on the UNIX
5203 * garbage collection to take care of this problem for us.
5204 */
5205static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5206{
5207 unsigned left, total;
5208 int ret = 0;
5209
5210 total = 0;
5211 left = ctx->nr_user_files;
5212 while (left) {
5213 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005214
5215 ret = __io_sqe_files_scm(ctx, this_files, total);
5216 if (ret)
5217 break;
5218 left -= this_files;
5219 total += this_files;
5220 }
5221
5222 if (!ret)
5223 return 0;
5224
5225 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005226 struct file *file = io_file_from_index(ctx, total);
5227
5228 if (file)
5229 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005230 total++;
5231 }
5232
5233 return ret;
5234}
5235#else
5236static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5237{
5238 return 0;
5239}
5240#endif
5241
Jens Axboe65e19f52019-10-26 07:20:21 -06005242static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5243 unsigned nr_files)
5244{
5245 int i;
5246
5247 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005248 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005249 unsigned this_files;
5250
5251 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5252 table->files = kcalloc(this_files, sizeof(struct file *),
5253 GFP_KERNEL);
5254 if (!table->files)
5255 break;
5256 nr_files -= this_files;
5257 }
5258
5259 if (i == nr_tables)
5260 return 0;
5261
5262 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005263 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005264 kfree(table->files);
5265 }
5266 return 1;
5267}
5268
Jens Axboe05f3fb32019-12-09 11:22:50 -07005269static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005270{
5271#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005272 struct sock *sock = ctx->ring_sock->sk;
5273 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5274 struct sk_buff *skb;
5275 int i;
5276
5277 __skb_queue_head_init(&list);
5278
5279 /*
5280 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5281 * remove this entry and rearrange the file array.
5282 */
5283 skb = skb_dequeue(head);
5284 while (skb) {
5285 struct scm_fp_list *fp;
5286
5287 fp = UNIXCB(skb).fp;
5288 for (i = 0; i < fp->count; i++) {
5289 int left;
5290
5291 if (fp->fp[i] != file)
5292 continue;
5293
5294 unix_notinflight(fp->user, fp->fp[i]);
5295 left = fp->count - 1 - i;
5296 if (left) {
5297 memmove(&fp->fp[i], &fp->fp[i + 1],
5298 left * sizeof(struct file *));
5299 }
5300 fp->count--;
5301 if (!fp->count) {
5302 kfree_skb(skb);
5303 skb = NULL;
5304 } else {
5305 __skb_queue_tail(&list, skb);
5306 }
5307 fput(file);
5308 file = NULL;
5309 break;
5310 }
5311
5312 if (!file)
5313 break;
5314
5315 __skb_queue_tail(&list, skb);
5316
5317 skb = skb_dequeue(head);
5318 }
5319
5320 if (skb_peek(&list)) {
5321 spin_lock_irq(&head->lock);
5322 while ((skb = __skb_dequeue(&list)) != NULL)
5323 __skb_queue_tail(head, skb);
5324 spin_unlock_irq(&head->lock);
5325 }
5326#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005327 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005328#endif
5329}
5330
Jens Axboe05f3fb32019-12-09 11:22:50 -07005331struct io_file_put {
5332 struct llist_node llist;
5333 struct file *file;
5334 struct completion *done;
5335};
5336
5337static void io_ring_file_ref_switch(struct work_struct *work)
5338{
5339 struct io_file_put *pfile, *tmp;
5340 struct fixed_file_data *data;
5341 struct llist_node *node;
5342
5343 data = container_of(work, struct fixed_file_data, ref_work);
5344
5345 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5346 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5347 io_ring_file_put(data->ctx, pfile->file);
5348 if (pfile->done)
5349 complete(pfile->done);
5350 else
5351 kfree(pfile);
5352 }
5353 }
5354
5355 percpu_ref_get(&data->refs);
5356 percpu_ref_switch_to_percpu(&data->refs);
5357}
5358
5359static void io_file_data_ref_zero(struct percpu_ref *ref)
5360{
5361 struct fixed_file_data *data;
5362
5363 data = container_of(ref, struct fixed_file_data, refs);
5364
5365 /* we can't safely switch from inside this context, punt to wq */
5366 queue_work(system_wq, &data->ref_work);
5367}
5368
5369static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5370 unsigned nr_args)
5371{
5372 __s32 __user *fds = (__s32 __user *) arg;
5373 unsigned nr_tables;
5374 struct file *file;
5375 int fd, ret = 0;
5376 unsigned i;
5377
5378 if (ctx->file_data)
5379 return -EBUSY;
5380 if (!nr_args)
5381 return -EINVAL;
5382 if (nr_args > IORING_MAX_FIXED_FILES)
5383 return -EMFILE;
5384
5385 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5386 if (!ctx->file_data)
5387 return -ENOMEM;
5388 ctx->file_data->ctx = ctx;
5389 init_completion(&ctx->file_data->done);
5390
5391 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5392 ctx->file_data->table = kcalloc(nr_tables,
5393 sizeof(struct fixed_file_table),
5394 GFP_KERNEL);
5395 if (!ctx->file_data->table) {
5396 kfree(ctx->file_data);
5397 ctx->file_data = NULL;
5398 return -ENOMEM;
5399 }
5400
5401 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5402 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5403 kfree(ctx->file_data->table);
5404 kfree(ctx->file_data);
5405 ctx->file_data = NULL;
5406 return -ENOMEM;
5407 }
5408 ctx->file_data->put_llist.first = NULL;
5409 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5410
5411 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5412 percpu_ref_exit(&ctx->file_data->refs);
5413 kfree(ctx->file_data->table);
5414 kfree(ctx->file_data);
5415 ctx->file_data = NULL;
5416 return -ENOMEM;
5417 }
5418
5419 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5420 struct fixed_file_table *table;
5421 unsigned index;
5422
5423 ret = -EFAULT;
5424 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5425 break;
5426 /* allow sparse sets */
5427 if (fd == -1) {
5428 ret = 0;
5429 continue;
5430 }
5431
5432 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5433 index = i & IORING_FILE_TABLE_MASK;
5434 file = fget(fd);
5435
5436 ret = -EBADF;
5437 if (!file)
5438 break;
5439
5440 /*
5441 * Don't allow io_uring instances to be registered. If UNIX
5442 * isn't enabled, then this causes a reference cycle and this
5443 * instance can never get freed. If UNIX is enabled we'll
5444 * handle it just fine, but there's still no point in allowing
5445 * a ring fd as it doesn't support regular read/write anyway.
5446 */
5447 if (file->f_op == &io_uring_fops) {
5448 fput(file);
5449 break;
5450 }
5451 ret = 0;
5452 table->files[index] = file;
5453 }
5454
5455 if (ret) {
5456 for (i = 0; i < ctx->nr_user_files; i++) {
5457 file = io_file_from_index(ctx, i);
5458 if (file)
5459 fput(file);
5460 }
5461 for (i = 0; i < nr_tables; i++)
5462 kfree(ctx->file_data->table[i].files);
5463
5464 kfree(ctx->file_data->table);
5465 kfree(ctx->file_data);
5466 ctx->file_data = NULL;
5467 ctx->nr_user_files = 0;
5468 return ret;
5469 }
5470
5471 ret = io_sqe_files_scm(ctx);
5472 if (ret)
5473 io_sqe_files_unregister(ctx);
5474
5475 return ret;
5476}
5477
Jens Axboec3a31e62019-10-03 13:59:56 -06005478static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5479 int index)
5480{
5481#if defined(CONFIG_UNIX)
5482 struct sock *sock = ctx->ring_sock->sk;
5483 struct sk_buff_head *head = &sock->sk_receive_queue;
5484 struct sk_buff *skb;
5485
5486 /*
5487 * See if we can merge this file into an existing skb SCM_RIGHTS
5488 * file set. If there's no room, fall back to allocating a new skb
5489 * and filling it in.
5490 */
5491 spin_lock_irq(&head->lock);
5492 skb = skb_peek(head);
5493 if (skb) {
5494 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5495
5496 if (fpl->count < SCM_MAX_FD) {
5497 __skb_unlink(skb, head);
5498 spin_unlock_irq(&head->lock);
5499 fpl->fp[fpl->count] = get_file(file);
5500 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5501 fpl->count++;
5502 spin_lock_irq(&head->lock);
5503 __skb_queue_head(head, skb);
5504 } else {
5505 skb = NULL;
5506 }
5507 }
5508 spin_unlock_irq(&head->lock);
5509
5510 if (skb) {
5511 fput(file);
5512 return 0;
5513 }
5514
5515 return __io_sqe_files_scm(ctx, 1, index);
5516#else
5517 return 0;
5518#endif
5519}
5520
Jens Axboe05f3fb32019-12-09 11:22:50 -07005521static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005522{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005523 struct fixed_file_data *data;
5524
5525 data = container_of(ref, struct fixed_file_data, refs);
5526 clear_bit(FFD_F_ATOMIC, &data->state);
5527}
5528
5529static bool io_queue_file_removal(struct fixed_file_data *data,
5530 struct file *file)
5531{
5532 struct io_file_put *pfile, pfile_stack;
5533 DECLARE_COMPLETION_ONSTACK(done);
5534
5535 /*
5536 * If we fail allocating the struct we need for doing async reomval
5537 * of this file, just punt to sync and wait for it.
5538 */
5539 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5540 if (!pfile) {
5541 pfile = &pfile_stack;
5542 pfile->done = &done;
5543 }
5544
5545 pfile->file = file;
5546 llist_add(&pfile->llist, &data->put_llist);
5547
5548 if (pfile == &pfile_stack) {
5549 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5550 percpu_ref_put(&data->refs);
5551 percpu_ref_switch_to_atomic(&data->refs,
5552 io_atomic_switch);
5553 }
5554 wait_for_completion(&done);
5555 flush_work(&data->ref_work);
5556 return false;
5557 }
5558
5559 return true;
5560}
5561
5562static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5563 struct io_uring_files_update *up,
5564 unsigned nr_args)
5565{
5566 struct fixed_file_data *data = ctx->file_data;
5567 bool ref_switch = false;
5568 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005569 __s32 __user *fds;
5570 int fd, i, err;
5571 __u32 done;
5572
Jens Axboe05f3fb32019-12-09 11:22:50 -07005573 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005574 return -EOVERFLOW;
5575 if (done > ctx->nr_user_files)
5576 return -EINVAL;
5577
5578 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005579 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005580 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005581 struct fixed_file_table *table;
5582 unsigned index;
5583
Jens Axboec3a31e62019-10-03 13:59:56 -06005584 err = 0;
5585 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5586 err = -EFAULT;
5587 break;
5588 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005589 i = array_index_nospec(up->offset, ctx->nr_user_files);
5590 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005591 index = i & IORING_FILE_TABLE_MASK;
5592 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005593 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005594 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005595 if (io_queue_file_removal(data, file))
5596 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005597 }
5598 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005599 file = fget(fd);
5600 if (!file) {
5601 err = -EBADF;
5602 break;
5603 }
5604 /*
5605 * Don't allow io_uring instances to be registered. If
5606 * UNIX isn't enabled, then this causes a reference
5607 * cycle and this instance can never get freed. If UNIX
5608 * is enabled we'll handle it just fine, but there's
5609 * still no point in allowing a ring fd as it doesn't
5610 * support regular read/write anyway.
5611 */
5612 if (file->f_op == &io_uring_fops) {
5613 fput(file);
5614 err = -EBADF;
5615 break;
5616 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005617 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005618 err = io_sqe_file_register(ctx, file, i);
5619 if (err)
5620 break;
5621 }
5622 nr_args--;
5623 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005624 up->offset++;
5625 }
5626
5627 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5628 percpu_ref_put(&data->refs);
5629 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005630 }
5631
5632 return done ? done : err;
5633}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005634static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5635 unsigned nr_args)
5636{
5637 struct io_uring_files_update up;
5638
5639 if (!ctx->file_data)
5640 return -ENXIO;
5641 if (!nr_args)
5642 return -EINVAL;
5643 if (copy_from_user(&up, arg, sizeof(up)))
5644 return -EFAULT;
5645 if (up.resv)
5646 return -EINVAL;
5647
5648 return __io_sqe_files_update(ctx, &up, nr_args);
5649}
Jens Axboec3a31e62019-10-03 13:59:56 -06005650
Jens Axboe7d723062019-11-12 22:31:31 -07005651static void io_put_work(struct io_wq_work *work)
5652{
5653 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5654
5655 io_put_req(req);
5656}
5657
5658static void io_get_work(struct io_wq_work *work)
5659{
5660 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5661
5662 refcount_inc(&req->refs);
5663}
5664
Jens Axboe6c271ce2019-01-10 11:22:30 -07005665static int io_sq_offload_start(struct io_ring_ctx *ctx,
5666 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005667{
Jens Axboe576a3472019-11-25 08:49:20 -07005668 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005669 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005670 int ret;
5671
Jens Axboe6c271ce2019-01-10 11:22:30 -07005672 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005673 mmgrab(current->mm);
5674 ctx->sqo_mm = current->mm;
5675
Jens Axboe6c271ce2019-01-10 11:22:30 -07005676 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005677 ret = -EPERM;
5678 if (!capable(CAP_SYS_ADMIN))
5679 goto err;
5680
Jens Axboe917257d2019-04-13 09:28:55 -06005681 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5682 if (!ctx->sq_thread_idle)
5683 ctx->sq_thread_idle = HZ;
5684
Jens Axboe6c271ce2019-01-10 11:22:30 -07005685 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005686 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005687
Jens Axboe917257d2019-04-13 09:28:55 -06005688 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005689 if (cpu >= nr_cpu_ids)
5690 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005691 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005692 goto err;
5693
Jens Axboe6c271ce2019-01-10 11:22:30 -07005694 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5695 ctx, cpu,
5696 "io_uring-sq");
5697 } else {
5698 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5699 "io_uring-sq");
5700 }
5701 if (IS_ERR(ctx->sqo_thread)) {
5702 ret = PTR_ERR(ctx->sqo_thread);
5703 ctx->sqo_thread = NULL;
5704 goto err;
5705 }
5706 wake_up_process(ctx->sqo_thread);
5707 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5708 /* Can't have SQ_AFF without SQPOLL */
5709 ret = -EINVAL;
5710 goto err;
5711 }
5712
Jens Axboe576a3472019-11-25 08:49:20 -07005713 data.mm = ctx->sqo_mm;
5714 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005715 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005716 data.get_work = io_get_work;
5717 data.put_work = io_put_work;
5718
Jens Axboe561fb042019-10-24 07:25:42 -06005719 /* Do QD, or 4 * CPUS, whatever is smallest */
5720 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005721 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005722 if (IS_ERR(ctx->io_wq)) {
5723 ret = PTR_ERR(ctx->io_wq);
5724 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005725 goto err;
5726 }
5727
5728 return 0;
5729err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005730 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005731 mmdrop(ctx->sqo_mm);
5732 ctx->sqo_mm = NULL;
5733 return ret;
5734}
5735
5736static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5737{
5738 atomic_long_sub(nr_pages, &user->locked_vm);
5739}
5740
5741static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5742{
5743 unsigned long page_limit, cur_pages, new_pages;
5744
5745 /* Don't allow more pages than we can safely lock */
5746 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5747
5748 do {
5749 cur_pages = atomic_long_read(&user->locked_vm);
5750 new_pages = cur_pages + nr_pages;
5751 if (new_pages > page_limit)
5752 return -ENOMEM;
5753 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5754 new_pages) != cur_pages);
5755
5756 return 0;
5757}
5758
5759static void io_mem_free(void *ptr)
5760{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005761 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005762
Mark Rutland52e04ef2019-04-30 17:30:21 +01005763 if (!ptr)
5764 return;
5765
5766 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005767 if (put_page_testzero(page))
5768 free_compound_page(page);
5769}
5770
5771static void *io_mem_alloc(size_t size)
5772{
5773 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5774 __GFP_NORETRY;
5775
5776 return (void *) __get_free_pages(gfp_flags, get_order(size));
5777}
5778
Hristo Venev75b28af2019-08-26 17:23:46 +00005779static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5780 size_t *sq_offset)
5781{
5782 struct io_rings *rings;
5783 size_t off, sq_array_size;
5784
5785 off = struct_size(rings, cqes, cq_entries);
5786 if (off == SIZE_MAX)
5787 return SIZE_MAX;
5788
5789#ifdef CONFIG_SMP
5790 off = ALIGN(off, SMP_CACHE_BYTES);
5791 if (off == 0)
5792 return SIZE_MAX;
5793#endif
5794
5795 sq_array_size = array_size(sizeof(u32), sq_entries);
5796 if (sq_array_size == SIZE_MAX)
5797 return SIZE_MAX;
5798
5799 if (check_add_overflow(off, sq_array_size, &off))
5800 return SIZE_MAX;
5801
5802 if (sq_offset)
5803 *sq_offset = off;
5804
5805 return off;
5806}
5807
Jens Axboe2b188cc2019-01-07 10:46:33 -07005808static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5809{
Hristo Venev75b28af2019-08-26 17:23:46 +00005810 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005811
Hristo Venev75b28af2019-08-26 17:23:46 +00005812 pages = (size_t)1 << get_order(
5813 rings_size(sq_entries, cq_entries, NULL));
5814 pages += (size_t)1 << get_order(
5815 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005816
Hristo Venev75b28af2019-08-26 17:23:46 +00005817 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005818}
5819
Jens Axboeedafcce2019-01-09 09:16:05 -07005820static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5821{
5822 int i, j;
5823
5824 if (!ctx->user_bufs)
5825 return -ENXIO;
5826
5827 for (i = 0; i < ctx->nr_user_bufs; i++) {
5828 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5829
5830 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005831 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005832
5833 if (ctx->account_mem)
5834 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005835 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005836 imu->nr_bvecs = 0;
5837 }
5838
5839 kfree(ctx->user_bufs);
5840 ctx->user_bufs = NULL;
5841 ctx->nr_user_bufs = 0;
5842 return 0;
5843}
5844
5845static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5846 void __user *arg, unsigned index)
5847{
5848 struct iovec __user *src;
5849
5850#ifdef CONFIG_COMPAT
5851 if (ctx->compat) {
5852 struct compat_iovec __user *ciovs;
5853 struct compat_iovec ciov;
5854
5855 ciovs = (struct compat_iovec __user *) arg;
5856 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5857 return -EFAULT;
5858
Jens Axboed55e5f52019-12-11 16:12:15 -07005859 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005860 dst->iov_len = ciov.iov_len;
5861 return 0;
5862 }
5863#endif
5864 src = (struct iovec __user *) arg;
5865 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5866 return -EFAULT;
5867 return 0;
5868}
5869
5870static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5871 unsigned nr_args)
5872{
5873 struct vm_area_struct **vmas = NULL;
5874 struct page **pages = NULL;
5875 int i, j, got_pages = 0;
5876 int ret = -EINVAL;
5877
5878 if (ctx->user_bufs)
5879 return -EBUSY;
5880 if (!nr_args || nr_args > UIO_MAXIOV)
5881 return -EINVAL;
5882
5883 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5884 GFP_KERNEL);
5885 if (!ctx->user_bufs)
5886 return -ENOMEM;
5887
5888 for (i = 0; i < nr_args; i++) {
5889 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5890 unsigned long off, start, end, ubuf;
5891 int pret, nr_pages;
5892 struct iovec iov;
5893 size_t size;
5894
5895 ret = io_copy_iov(ctx, &iov, arg, i);
5896 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005897 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005898
5899 /*
5900 * Don't impose further limits on the size and buffer
5901 * constraints here, we'll -EINVAL later when IO is
5902 * submitted if they are wrong.
5903 */
5904 ret = -EFAULT;
5905 if (!iov.iov_base || !iov.iov_len)
5906 goto err;
5907
5908 /* arbitrary limit, but we need something */
5909 if (iov.iov_len > SZ_1G)
5910 goto err;
5911
5912 ubuf = (unsigned long) iov.iov_base;
5913 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5914 start = ubuf >> PAGE_SHIFT;
5915 nr_pages = end - start;
5916
5917 if (ctx->account_mem) {
5918 ret = io_account_mem(ctx->user, nr_pages);
5919 if (ret)
5920 goto err;
5921 }
5922
5923 ret = 0;
5924 if (!pages || nr_pages > got_pages) {
5925 kfree(vmas);
5926 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005927 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005928 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005929 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005930 sizeof(struct vm_area_struct *),
5931 GFP_KERNEL);
5932 if (!pages || !vmas) {
5933 ret = -ENOMEM;
5934 if (ctx->account_mem)
5935 io_unaccount_mem(ctx->user, nr_pages);
5936 goto err;
5937 }
5938 got_pages = nr_pages;
5939 }
5940
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005941 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005942 GFP_KERNEL);
5943 ret = -ENOMEM;
5944 if (!imu->bvec) {
5945 if (ctx->account_mem)
5946 io_unaccount_mem(ctx->user, nr_pages);
5947 goto err;
5948 }
5949
5950 ret = 0;
5951 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005952 pret = get_user_pages(ubuf, nr_pages,
5953 FOLL_WRITE | FOLL_LONGTERM,
5954 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005955 if (pret == nr_pages) {
5956 /* don't support file backed memory */
5957 for (j = 0; j < nr_pages; j++) {
5958 struct vm_area_struct *vma = vmas[j];
5959
5960 if (vma->vm_file &&
5961 !is_file_hugepages(vma->vm_file)) {
5962 ret = -EOPNOTSUPP;
5963 break;
5964 }
5965 }
5966 } else {
5967 ret = pret < 0 ? pret : -EFAULT;
5968 }
5969 up_read(&current->mm->mmap_sem);
5970 if (ret) {
5971 /*
5972 * if we did partial map, or found file backed vmas,
5973 * release any pages we did get
5974 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005975 if (pret > 0)
5976 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005977 if (ctx->account_mem)
5978 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005979 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005980 goto err;
5981 }
5982
5983 off = ubuf & ~PAGE_MASK;
5984 size = iov.iov_len;
5985 for (j = 0; j < nr_pages; j++) {
5986 size_t vec_len;
5987
5988 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5989 imu->bvec[j].bv_page = pages[j];
5990 imu->bvec[j].bv_len = vec_len;
5991 imu->bvec[j].bv_offset = off;
5992 off = 0;
5993 size -= vec_len;
5994 }
5995 /* store original address for later verification */
5996 imu->ubuf = ubuf;
5997 imu->len = iov.iov_len;
5998 imu->nr_bvecs = nr_pages;
5999
6000 ctx->nr_user_bufs++;
6001 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006002 kvfree(pages);
6003 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006004 return 0;
6005err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006006 kvfree(pages);
6007 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006008 io_sqe_buffer_unregister(ctx);
6009 return ret;
6010}
6011
Jens Axboe9b402842019-04-11 11:45:41 -06006012static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6013{
6014 __s32 __user *fds = arg;
6015 int fd;
6016
6017 if (ctx->cq_ev_fd)
6018 return -EBUSY;
6019
6020 if (copy_from_user(&fd, fds, sizeof(*fds)))
6021 return -EFAULT;
6022
6023 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6024 if (IS_ERR(ctx->cq_ev_fd)) {
6025 int ret = PTR_ERR(ctx->cq_ev_fd);
6026 ctx->cq_ev_fd = NULL;
6027 return ret;
6028 }
6029
6030 return 0;
6031}
6032
6033static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6034{
6035 if (ctx->cq_ev_fd) {
6036 eventfd_ctx_put(ctx->cq_ev_fd);
6037 ctx->cq_ev_fd = NULL;
6038 return 0;
6039 }
6040
6041 return -ENXIO;
6042}
6043
Jens Axboe2b188cc2019-01-07 10:46:33 -07006044static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6045{
Jens Axboe6b063142019-01-10 22:13:58 -07006046 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006047 if (ctx->sqo_mm)
6048 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006049
6050 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006051 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006052 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006053 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006054
Jens Axboe2b188cc2019-01-07 10:46:33 -07006055#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006056 if (ctx->ring_sock) {
6057 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006058 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006059 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006060#endif
6061
Hristo Venev75b28af2019-08-26 17:23:46 +00006062 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006063 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006064
6065 percpu_ref_exit(&ctx->refs);
6066 if (ctx->account_mem)
6067 io_unaccount_mem(ctx->user,
6068 ring_pages(ctx->sq_entries, ctx->cq_entries));
6069 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006070 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006071 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006072 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006073 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006074 kfree(ctx);
6075}
6076
6077static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6078{
6079 struct io_ring_ctx *ctx = file->private_data;
6080 __poll_t mask = 0;
6081
6082 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006083 /*
6084 * synchronizes with barrier from wq_has_sleeper call in
6085 * io_commit_cqring
6086 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006087 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006088 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6089 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006090 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006091 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006092 mask |= EPOLLIN | EPOLLRDNORM;
6093
6094 return mask;
6095}
6096
6097static int io_uring_fasync(int fd, struct file *file, int on)
6098{
6099 struct io_ring_ctx *ctx = file->private_data;
6100
6101 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6102}
6103
6104static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6105{
6106 mutex_lock(&ctx->uring_lock);
6107 percpu_ref_kill(&ctx->refs);
6108 mutex_unlock(&ctx->uring_lock);
6109
Jens Axboe5262f562019-09-17 12:26:57 -06006110 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006111 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006112
6113 if (ctx->io_wq)
6114 io_wq_cancel_all(ctx->io_wq);
6115
Jens Axboedef596e2019-01-09 08:59:42 -07006116 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006117 /* if we failed setting up the ctx, we might not have any rings */
6118 if (ctx->rings)
6119 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07006120 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006121 io_ring_ctx_free(ctx);
6122}
6123
6124static int io_uring_release(struct inode *inode, struct file *file)
6125{
6126 struct io_ring_ctx *ctx = file->private_data;
6127
6128 file->private_data = NULL;
6129 io_ring_ctx_wait_and_kill(ctx);
6130 return 0;
6131}
6132
Jens Axboefcb323c2019-10-24 12:39:47 -06006133static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6134 struct files_struct *files)
6135{
6136 struct io_kiocb *req;
6137 DEFINE_WAIT(wait);
6138
6139 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006140 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006141
6142 spin_lock_irq(&ctx->inflight_lock);
6143 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006144 if (req->work.files != files)
6145 continue;
6146 /* req is being completed, ignore */
6147 if (!refcount_inc_not_zero(&req->refs))
6148 continue;
6149 cancel_req = req;
6150 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006151 }
Jens Axboe768134d2019-11-10 20:30:53 -07006152 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006153 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006154 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006155 spin_unlock_irq(&ctx->inflight_lock);
6156
Jens Axboe768134d2019-11-10 20:30:53 -07006157 /* We need to keep going until we don't find a matching req */
6158 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006159 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006160
6161 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6162 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006163 schedule();
6164 }
Jens Axboe768134d2019-11-10 20:30:53 -07006165 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006166}
6167
6168static int io_uring_flush(struct file *file, void *data)
6169{
6170 struct io_ring_ctx *ctx = file->private_data;
6171
6172 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006173 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6174 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006175 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006176 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006177 return 0;
6178}
6179
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006180static void *io_uring_validate_mmap_request(struct file *file,
6181 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006182{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006183 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006184 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006185 struct page *page;
6186 void *ptr;
6187
6188 switch (offset) {
6189 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006190 case IORING_OFF_CQ_RING:
6191 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006192 break;
6193 case IORING_OFF_SQES:
6194 ptr = ctx->sq_sqes;
6195 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006196 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006197 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006198 }
6199
6200 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006201 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006202 return ERR_PTR(-EINVAL);
6203
6204 return ptr;
6205}
6206
6207#ifdef CONFIG_MMU
6208
6209static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6210{
6211 size_t sz = vma->vm_end - vma->vm_start;
6212 unsigned long pfn;
6213 void *ptr;
6214
6215 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6216 if (IS_ERR(ptr))
6217 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006218
6219 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6220 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6221}
6222
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006223#else /* !CONFIG_MMU */
6224
6225static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6226{
6227 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6228}
6229
6230static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6231{
6232 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6233}
6234
6235static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6236 unsigned long addr, unsigned long len,
6237 unsigned long pgoff, unsigned long flags)
6238{
6239 void *ptr;
6240
6241 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6242 if (IS_ERR(ptr))
6243 return PTR_ERR(ptr);
6244
6245 return (unsigned long) ptr;
6246}
6247
6248#endif /* !CONFIG_MMU */
6249
Jens Axboe2b188cc2019-01-07 10:46:33 -07006250SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6251 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6252 size_t, sigsz)
6253{
6254 struct io_ring_ctx *ctx;
6255 long ret = -EBADF;
6256 int submitted = 0;
6257 struct fd f;
6258
Jens Axboe6c271ce2019-01-10 11:22:30 -07006259 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006260 return -EINVAL;
6261
6262 f = fdget(fd);
6263 if (!f.file)
6264 return -EBADF;
6265
6266 ret = -EOPNOTSUPP;
6267 if (f.file->f_op != &io_uring_fops)
6268 goto out_fput;
6269
6270 ret = -ENXIO;
6271 ctx = f.file->private_data;
6272 if (!percpu_ref_tryget(&ctx->refs))
6273 goto out_fput;
6274
Jens Axboe6c271ce2019-01-10 11:22:30 -07006275 /*
6276 * For SQ polling, the thread will do all submissions and completions.
6277 * Just return the requested submit count, and wake the thread if
6278 * we were asked to.
6279 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006280 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006281 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006282 if (!list_empty_careful(&ctx->cq_overflow_list))
6283 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006284 if (flags & IORING_ENTER_SQ_WAKEUP)
6285 wake_up(&ctx->sqo_wait);
6286 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006287 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006288 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006289
Jens Axboe44d28272020-01-16 19:00:24 -07006290 if (current->mm != ctx->sqo_mm ||
6291 current_cred() != ctx->creds) {
6292 ret = -EPERM;
6293 goto out;
6294 }
6295
Jens Axboe2b188cc2019-01-07 10:46:33 -07006296 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006297 /* already have mm, so io_submit_sqes() won't try to grab it */
6298 cur_mm = ctx->sqo_mm;
6299 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6300 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006301 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006302
6303 if (submitted != to_submit)
6304 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006305 }
6306 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006307 unsigned nr_events = 0;
6308
Jens Axboe2b188cc2019-01-07 10:46:33 -07006309 min_complete = min(min_complete, ctx->cq_entries);
6310
Jens Axboedef596e2019-01-09 08:59:42 -07006311 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006312 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006313 } else {
6314 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6315 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006316 }
6317
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006318out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006319 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006320out_fput:
6321 fdput(f);
6322 return submitted ? submitted : ret;
6323}
6324
6325static const struct file_operations io_uring_fops = {
6326 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006327 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006328 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006329#ifndef CONFIG_MMU
6330 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6331 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6332#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006333 .poll = io_uring_poll,
6334 .fasync = io_uring_fasync,
6335};
6336
6337static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6338 struct io_uring_params *p)
6339{
Hristo Venev75b28af2019-08-26 17:23:46 +00006340 struct io_rings *rings;
6341 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006342
Hristo Venev75b28af2019-08-26 17:23:46 +00006343 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6344 if (size == SIZE_MAX)
6345 return -EOVERFLOW;
6346
6347 rings = io_mem_alloc(size);
6348 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006349 return -ENOMEM;
6350
Hristo Venev75b28af2019-08-26 17:23:46 +00006351 ctx->rings = rings;
6352 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6353 rings->sq_ring_mask = p->sq_entries - 1;
6354 rings->cq_ring_mask = p->cq_entries - 1;
6355 rings->sq_ring_entries = p->sq_entries;
6356 rings->cq_ring_entries = p->cq_entries;
6357 ctx->sq_mask = rings->sq_ring_mask;
6358 ctx->cq_mask = rings->cq_ring_mask;
6359 ctx->sq_entries = rings->sq_ring_entries;
6360 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006361
6362 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006363 if (size == SIZE_MAX) {
6364 io_mem_free(ctx->rings);
6365 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006366 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006367 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006368
6369 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006370 if (!ctx->sq_sqes) {
6371 io_mem_free(ctx->rings);
6372 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006373 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006374 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006375
Jens Axboe2b188cc2019-01-07 10:46:33 -07006376 return 0;
6377}
6378
6379/*
6380 * Allocate an anonymous fd, this is what constitutes the application
6381 * visible backing of an io_uring instance. The application mmaps this
6382 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6383 * we have to tie this fd to a socket for file garbage collection purposes.
6384 */
6385static int io_uring_get_fd(struct io_ring_ctx *ctx)
6386{
6387 struct file *file;
6388 int ret;
6389
6390#if defined(CONFIG_UNIX)
6391 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6392 &ctx->ring_sock);
6393 if (ret)
6394 return ret;
6395#endif
6396
6397 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6398 if (ret < 0)
6399 goto err;
6400
6401 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6402 O_RDWR | O_CLOEXEC);
6403 if (IS_ERR(file)) {
6404 put_unused_fd(ret);
6405 ret = PTR_ERR(file);
6406 goto err;
6407 }
6408
6409#if defined(CONFIG_UNIX)
6410 ctx->ring_sock->file = file;
6411#endif
6412 fd_install(ret, file);
6413 return ret;
6414err:
6415#if defined(CONFIG_UNIX)
6416 sock_release(ctx->ring_sock);
6417 ctx->ring_sock = NULL;
6418#endif
6419 return ret;
6420}
6421
6422static int io_uring_create(unsigned entries, struct io_uring_params *p)
6423{
6424 struct user_struct *user = NULL;
6425 struct io_ring_ctx *ctx;
6426 bool account_mem;
6427 int ret;
6428
Jens Axboe8110c1a2019-12-28 15:39:54 -07006429 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006430 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006431 if (entries > IORING_MAX_ENTRIES) {
6432 if (!(p->flags & IORING_SETUP_CLAMP))
6433 return -EINVAL;
6434 entries = IORING_MAX_ENTRIES;
6435 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006436
6437 /*
6438 * Use twice as many entries for the CQ ring. It's possible for the
6439 * application to drive a higher depth than the size of the SQ ring,
6440 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006441 * some flexibility in overcommitting a bit. If the application has
6442 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6443 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006444 */
6445 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006446 if (p->flags & IORING_SETUP_CQSIZE) {
6447 /*
6448 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6449 * to a power-of-two, if it isn't already. We do NOT impose
6450 * any cq vs sq ring sizing.
6451 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006452 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006453 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006454 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6455 if (!(p->flags & IORING_SETUP_CLAMP))
6456 return -EINVAL;
6457 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6458 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006459 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6460 } else {
6461 p->cq_entries = 2 * p->sq_entries;
6462 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006463
6464 user = get_uid(current_user());
6465 account_mem = !capable(CAP_IPC_LOCK);
6466
6467 if (account_mem) {
6468 ret = io_account_mem(user,
6469 ring_pages(p->sq_entries, p->cq_entries));
6470 if (ret) {
6471 free_uid(user);
6472 return ret;
6473 }
6474 }
6475
6476 ctx = io_ring_ctx_alloc(p);
6477 if (!ctx) {
6478 if (account_mem)
6479 io_unaccount_mem(user, ring_pages(p->sq_entries,
6480 p->cq_entries));
6481 free_uid(user);
6482 return -ENOMEM;
6483 }
6484 ctx->compat = in_compat_syscall();
6485 ctx->account_mem = account_mem;
6486 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006487 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006488
6489 ret = io_allocate_scq_urings(ctx, p);
6490 if (ret)
6491 goto err;
6492
Jens Axboe6c271ce2019-01-10 11:22:30 -07006493 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006494 if (ret)
6495 goto err;
6496
Jens Axboe2b188cc2019-01-07 10:46:33 -07006497 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006498 p->sq_off.head = offsetof(struct io_rings, sq.head);
6499 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6500 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6501 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6502 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6503 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6504 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006505
6506 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006507 p->cq_off.head = offsetof(struct io_rings, cq.head);
6508 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6509 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6510 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6511 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6512 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006513
Jens Axboe044c1ab2019-10-28 09:15:33 -06006514 /*
6515 * Install ring fd as the very last thing, so we don't risk someone
6516 * having closed it before we finish setup
6517 */
6518 ret = io_uring_get_fd(ctx);
6519 if (ret < 0)
6520 goto err;
6521
Jens Axboeda8c9692019-12-02 18:51:26 -07006522 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboeba042912019-12-25 16:33:42 -07006523 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006524 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006525 return ret;
6526err:
6527 io_ring_ctx_wait_and_kill(ctx);
6528 return ret;
6529}
6530
6531/*
6532 * Sets up an aio uring context, and returns the fd. Applications asks for a
6533 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6534 * params structure passed in.
6535 */
6536static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6537{
6538 struct io_uring_params p;
6539 long ret;
6540 int i;
6541
6542 if (copy_from_user(&p, params, sizeof(p)))
6543 return -EFAULT;
6544 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6545 if (p.resv[i])
6546 return -EINVAL;
6547 }
6548
Jens Axboe6c271ce2019-01-10 11:22:30 -07006549 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006550 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
6551 IORING_SETUP_CLAMP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006552 return -EINVAL;
6553
6554 ret = io_uring_create(entries, &p);
6555 if (ret < 0)
6556 return ret;
6557
6558 if (copy_to_user(params, &p, sizeof(p)))
6559 return -EFAULT;
6560
6561 return ret;
6562}
6563
6564SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6565 struct io_uring_params __user *, params)
6566{
6567 return io_uring_setup(entries, params);
6568}
6569
Jens Axboe66f4af92020-01-16 15:36:52 -07006570static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6571{
6572 struct io_uring_probe *p;
6573 size_t size;
6574 int i, ret;
6575
6576 size = struct_size(p, ops, nr_args);
6577 if (size == SIZE_MAX)
6578 return -EOVERFLOW;
6579 p = kzalloc(size, GFP_KERNEL);
6580 if (!p)
6581 return -ENOMEM;
6582
6583 ret = -EFAULT;
6584 if (copy_from_user(p, arg, size))
6585 goto out;
6586 ret = -EINVAL;
6587 if (memchr_inv(p, 0, size))
6588 goto out;
6589
6590 p->last_op = IORING_OP_LAST - 1;
6591 if (nr_args > IORING_OP_LAST)
6592 nr_args = IORING_OP_LAST;
6593
6594 for (i = 0; i < nr_args; i++) {
6595 p->ops[i].op = i;
6596 if (!io_op_defs[i].not_supported)
6597 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6598 }
6599 p->ops_len = i;
6600
6601 ret = 0;
6602 if (copy_to_user(arg, p, size))
6603 ret = -EFAULT;
6604out:
6605 kfree(p);
6606 return ret;
6607}
6608
Jens Axboeedafcce2019-01-09 09:16:05 -07006609static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6610 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006611 __releases(ctx->uring_lock)
6612 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006613{
6614 int ret;
6615
Jens Axboe35fa71a2019-04-22 10:23:23 -06006616 /*
6617 * We're inside the ring mutex, if the ref is already dying, then
6618 * someone else killed the ctx or is already going through
6619 * io_uring_register().
6620 */
6621 if (percpu_ref_is_dying(&ctx->refs))
6622 return -ENXIO;
6623
Jens Axboe05f3fb32019-12-09 11:22:50 -07006624 if (opcode != IORING_UNREGISTER_FILES &&
Jens Axboe66f4af92020-01-16 15:36:52 -07006625 opcode != IORING_REGISTER_FILES_UPDATE &&
6626 opcode != IORING_REGISTER_PROBE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006627 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006628
Jens Axboe05f3fb32019-12-09 11:22:50 -07006629 /*
6630 * Drop uring mutex before waiting for references to exit. If
6631 * another thread is currently inside io_uring_enter() it might
6632 * need to grab the uring_lock to make progress. If we hold it
6633 * here across the drain wait, then we can deadlock. It's safe
6634 * to drop the mutex here, since no new references will come in
6635 * after we've killed the percpu ref.
6636 */
6637 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006638 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006639 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006640 if (ret) {
6641 percpu_ref_resurrect(&ctx->refs);
6642 ret = -EINTR;
6643 goto out;
6644 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006645 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006646
6647 switch (opcode) {
6648 case IORING_REGISTER_BUFFERS:
6649 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6650 break;
6651 case IORING_UNREGISTER_BUFFERS:
6652 ret = -EINVAL;
6653 if (arg || nr_args)
6654 break;
6655 ret = io_sqe_buffer_unregister(ctx);
6656 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006657 case IORING_REGISTER_FILES:
6658 ret = io_sqe_files_register(ctx, arg, nr_args);
6659 break;
6660 case IORING_UNREGISTER_FILES:
6661 ret = -EINVAL;
6662 if (arg || nr_args)
6663 break;
6664 ret = io_sqe_files_unregister(ctx);
6665 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006666 case IORING_REGISTER_FILES_UPDATE:
6667 ret = io_sqe_files_update(ctx, arg, nr_args);
6668 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006669 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006670 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006671 ret = -EINVAL;
6672 if (nr_args != 1)
6673 break;
6674 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006675 if (ret)
6676 break;
6677 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6678 ctx->eventfd_async = 1;
6679 else
6680 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006681 break;
6682 case IORING_UNREGISTER_EVENTFD:
6683 ret = -EINVAL;
6684 if (arg || nr_args)
6685 break;
6686 ret = io_eventfd_unregister(ctx);
6687 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07006688 case IORING_REGISTER_PROBE:
6689 ret = -EINVAL;
6690 if (!arg || nr_args > 256)
6691 break;
6692 ret = io_probe(ctx, arg, nr_args);
6693 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006694 default:
6695 ret = -EINVAL;
6696 break;
6697 }
6698
Jens Axboe05f3fb32019-12-09 11:22:50 -07006699
6700 if (opcode != IORING_UNREGISTER_FILES &&
Jens Axboe66f4af92020-01-16 15:36:52 -07006701 opcode != IORING_REGISTER_FILES_UPDATE &&
6702 opcode != IORING_REGISTER_PROBE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006703 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006704 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07006705out:
6706 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006707 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006708 return ret;
6709}
6710
6711SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6712 void __user *, arg, unsigned int, nr_args)
6713{
6714 struct io_ring_ctx *ctx;
6715 long ret = -EBADF;
6716 struct fd f;
6717
6718 f = fdget(fd);
6719 if (!f.file)
6720 return -EBADF;
6721
6722 ret = -EOPNOTSUPP;
6723 if (f.file->f_op != &io_uring_fops)
6724 goto out_fput;
6725
6726 ctx = f.file->private_data;
6727
6728 mutex_lock(&ctx->uring_lock);
6729 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6730 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006731 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6732 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006733out_fput:
6734 fdput(f);
6735 return ret;
6736}
6737
Jens Axboe2b188cc2019-01-07 10:46:33 -07006738static int __init io_uring_init(void)
6739{
Jens Axboed3656342019-12-18 09:50:26 -07006740 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006741 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6742 return 0;
6743};
6744__initcall(io_uring_init);