blob: 8a645a37b4c761cc4fadaeec778b4bc041e0f37a [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070059#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070073#include <linux/namei.h>
74#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070075#include <linux/fadvise.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070076
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020077#define CREATE_TRACE_POINTS
78#include <trace/events/io_uring.h>
79
Jens Axboe2b188cc2019-01-07 10:46:33 -070080#include <uapi/linux/io_uring.h>
81
82#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060083#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
Daniel Xu5277dea2019-09-14 14:23:45 -070085#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060086#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060087
88/*
89 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
90 */
91#define IORING_FILE_TABLE_SHIFT 9
92#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
93#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
94#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070095
96struct io_uring {
97 u32 head ____cacheline_aligned_in_smp;
98 u32 tail ____cacheline_aligned_in_smp;
99};
100
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000102 * This data is shared with the application through the mmap at offsets
103 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200104 *
105 * The offsets to the member fields are published through struct
106 * io_sqring_offsets when calling io_uring_setup.
107 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000108struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200109 /*
110 * Head and tail offsets into the ring; the offsets need to be
111 * masked to get valid indices.
112 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 * The kernel controls head of the sq ring and the tail of the cq ring,
114 * and the application controls tail of the sq ring and the head of the
115 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200116 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000117 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120 * ring_entries - 1)
121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 u32 sq_ring_mask, cq_ring_mask;
123 /* Ring sizes (constant, power of 2) */
124 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 /*
126 * Number of invalid entries dropped by the kernel due to
127 * invalid index stored in array
128 *
129 * Written by the kernel, shouldn't be modified by the
130 * application (i.e. get number of "new events" by comparing to
131 * cached value).
132 *
133 * After a new SQ head value was read by the application this
134 * counter includes all submissions that were dropped reaching
135 * the new SQ head (and possibly more).
136 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000137 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200138 /*
139 * Runtime flags
140 *
141 * Written by the kernel, shouldn't be modified by the
142 * application.
143 *
144 * The application needs a full memory barrier before checking
145 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
146 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000147 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200148 /*
149 * Number of completion events lost because the queue was full;
150 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800151 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200152 * the completion queue.
153 *
154 * Written by the kernel, shouldn't be modified by the
155 * application (i.e. get number of "new events" by comparing to
156 * cached value).
157 *
158 * As completion events come in out of order this counter is not
159 * ordered with any other data.
160 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000161 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 /*
163 * Ring buffer of completion events.
164 *
165 * The kernel writes completion events fresh every time they are
166 * produced, so the application is allowed to modify pending
167 * entries.
168 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000169 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700170};
171
Jens Axboeedafcce2019-01-09 09:16:05 -0700172struct io_mapped_ubuf {
173 u64 ubuf;
174 size_t len;
175 struct bio_vec *bvec;
176 unsigned int nr_bvecs;
177};
178
Jens Axboe65e19f52019-10-26 07:20:21 -0600179struct fixed_file_table {
180 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700181};
182
Jens Axboe05f3fb32019-12-09 11:22:50 -0700183enum {
184 FFD_F_ATOMIC,
185};
186
187struct fixed_file_data {
188 struct fixed_file_table *table;
189 struct io_ring_ctx *ctx;
190
191 struct percpu_ref refs;
192 struct llist_head put_llist;
193 unsigned long state;
194 struct work_struct ref_work;
195 struct completion done;
196};
197
Jens Axboe2b188cc2019-01-07 10:46:33 -0700198struct io_ring_ctx {
199 struct {
200 struct percpu_ref refs;
201 } ____cacheline_aligned_in_smp;
202
203 struct {
204 unsigned int flags;
Jens Axboe69b3e542020-01-08 11:01:46 -0700205 int compat: 1;
206 int account_mem: 1;
207 int cq_overflow_flushed: 1;
208 int drain_next: 1;
Jens Axboef2842ab2020-01-08 11:04:00 -0700209 int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700210
Hristo Venev75b28af2019-08-26 17:23:46 +0000211 /*
212 * Ring buffer of indices into array of io_uring_sqe, which is
213 * mmapped by the application using the IORING_OFF_SQES offset.
214 *
215 * This indirection could e.g. be used to assign fixed
216 * io_uring_sqe entries to operations and only submit them to
217 * the queue when needed.
218 *
219 * The kernel modifies neither the indices array nor the entries
220 * array.
221 */
222 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700223 unsigned cached_sq_head;
224 unsigned sq_entries;
225 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700226 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600227 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700228 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700229 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600230
231 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600232 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700233 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234
Jens Axboefcb323c2019-10-24 12:39:47 -0600235 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700236 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700237 } ____cacheline_aligned_in_smp;
238
Hristo Venev75b28af2019-08-26 17:23:46 +0000239 struct io_rings *rings;
240
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600242 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700243 struct task_struct *sqo_thread; /* if using sq thread polling */
244 struct mm_struct *sqo_mm;
245 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246
Jens Axboe6b063142019-01-10 22:13:58 -0700247 /*
248 * If used, fixed file set. Writers must ensure that ->refs is dead,
249 * readers must ensure that ->refs is alive as long as the file* is
250 * used. Only updated through io_uring_register(2).
251 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700252 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700253 unsigned nr_user_files;
254
Jens Axboeedafcce2019-01-09 09:16:05 -0700255 /* if used, fixed mapped user buffers */
256 unsigned nr_user_bufs;
257 struct io_mapped_ubuf *user_bufs;
258
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259 struct user_struct *user;
260
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700261 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700262
Jens Axboe206aefd2019-11-07 18:27:42 -0700263 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
264 struct completion *completions;
265
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700266 /* if all else fails... */
267 struct io_kiocb *fallback_req;
268
Jens Axboe206aefd2019-11-07 18:27:42 -0700269#if defined(CONFIG_UNIX)
270 struct socket *ring_sock;
271#endif
272
273 struct {
274 unsigned cached_cq_tail;
275 unsigned cq_entries;
276 unsigned cq_mask;
277 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700278 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700279 struct wait_queue_head cq_wait;
280 struct fasync_struct *cq_fasync;
281 struct eventfd_ctx *cq_ev_fd;
282 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283
284 struct {
285 struct mutex uring_lock;
286 wait_queue_head_t wait;
287 } ____cacheline_aligned_in_smp;
288
289 struct {
290 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700291 struct llist_head poll_llist;
292
Jens Axboedef596e2019-01-09 08:59:42 -0700293 /*
294 * ->poll_list is protected by the ctx->uring_lock for
295 * io_uring instances that don't use IORING_SETUP_SQPOLL.
296 * For SQPOLL, only the single threaded io_sq_thread() will
297 * manipulate the list, hence no extra locking is needed there.
298 */
299 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700300 struct hlist_head *cancel_hash;
301 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700302 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600303
304 spinlock_t inflight_lock;
305 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700306 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700307};
308
Jens Axboe09bb8392019-03-13 12:39:28 -0600309/*
310 * First field must be the file pointer in all the
311 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
312 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700313struct io_poll_iocb {
314 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700315 union {
316 struct wait_queue_head *head;
317 u64 addr;
318 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700319 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600320 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700321 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700322 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700323};
324
Jens Axboeb5dba592019-12-11 14:02:38 -0700325struct io_close {
326 struct file *file;
327 struct file *put_file;
328 int fd;
329};
330
Jens Axboead8a48a2019-11-15 08:49:11 -0700331struct io_timeout_data {
332 struct io_kiocb *req;
333 struct hrtimer timer;
334 struct timespec64 ts;
335 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300336 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700337};
338
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700339struct io_accept {
340 struct file *file;
341 struct sockaddr __user *addr;
342 int __user *addr_len;
343 int flags;
344};
345
346struct io_sync {
347 struct file *file;
348 loff_t len;
349 loff_t off;
350 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700351 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700352};
353
Jens Axboefbf23842019-12-17 18:45:56 -0700354struct io_cancel {
355 struct file *file;
356 u64 addr;
357};
358
Jens Axboeb29472e2019-12-17 18:50:29 -0700359struct io_timeout {
360 struct file *file;
361 u64 addr;
362 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700363 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700364};
365
Jens Axboe9adbd452019-12-20 08:45:55 -0700366struct io_rw {
367 /* NOTE: kiocb has the file as the first member, so don't do it here */
368 struct kiocb kiocb;
369 u64 addr;
370 u64 len;
371};
372
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700373struct io_connect {
374 struct file *file;
375 struct sockaddr __user *addr;
376 int addr_len;
377};
378
Jens Axboee47293f2019-12-20 08:58:21 -0700379struct io_sr_msg {
380 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700381 union {
382 struct user_msghdr __user *msg;
383 void __user *buf;
384 };
Jens Axboee47293f2019-12-20 08:58:21 -0700385 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700386 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700387};
388
Jens Axboe15b71ab2019-12-11 11:20:36 -0700389struct io_open {
390 struct file *file;
391 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700392 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700393 unsigned mask;
394 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700395 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700396 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700397 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700398};
399
Jens Axboe05f3fb32019-12-09 11:22:50 -0700400struct io_files_update {
401 struct file *file;
402 u64 arg;
403 u32 nr_args;
404 u32 offset;
405};
406
Jens Axboe4840e412019-12-25 22:03:45 -0700407struct io_fadvise {
408 struct file *file;
409 u64 offset;
410 u32 len;
411 u32 advice;
412};
413
Jens Axboec1ca7572019-12-25 22:18:28 -0700414struct io_madvise {
415 struct file *file;
416 u64 addr;
417 u32 len;
418 u32 advice;
419};
420
Jens Axboef499a022019-12-02 16:28:46 -0700421struct io_async_connect {
422 struct sockaddr_storage address;
423};
424
Jens Axboe03b12302019-12-02 18:50:25 -0700425struct io_async_msghdr {
426 struct iovec fast_iov[UIO_FASTIOV];
427 struct iovec *iov;
428 struct sockaddr __user *uaddr;
429 struct msghdr msg;
430};
431
Jens Axboef67676d2019-12-02 11:03:47 -0700432struct io_async_rw {
433 struct iovec fast_iov[UIO_FASTIOV];
434 struct iovec *iov;
435 ssize_t nr_segs;
436 ssize_t size;
437};
438
Jens Axboe15b71ab2019-12-11 11:20:36 -0700439struct io_async_open {
440 struct filename *filename;
441};
442
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700443struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700444 union {
445 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700446 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700447 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700448 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700449 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700450 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700451};
452
Jens Axboe09bb8392019-03-13 12:39:28 -0600453/*
454 * NOTE! Each of the iocb union members has the file pointer
455 * as the first entry in their struct definition. So you can
456 * access the file pointer through any of the sub-structs,
457 * or directly as just 'ki_filp' in this struct.
458 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700459struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700460 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600461 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700462 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700463 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700464 struct io_accept accept;
465 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700466 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700467 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700468 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700469 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700470 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700471 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700472 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700473 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700474 struct io_madvise madvise;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700475 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700476
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700477 struct io_async_ctx *io;
Jens Axboee94f1412019-12-19 12:06:02 -0700478 union {
479 /*
480 * ring_file is only used in the submission path, and
481 * llist_node is only used for poll deferred completions
482 */
483 struct file *ring_file;
484 struct llist_node llist_node;
485 };
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300486 int ring_fd;
487 bool has_user;
488 bool in_async;
489 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700490 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700491
492 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700493 union {
494 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700495 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700496 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600497 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700498 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700499 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200500#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700501#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700502#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700503#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200504#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
505#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600506#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700507#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800508#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300509#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600510#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600511#define REQ_F_ISREG 2048 /* regular file */
512#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700513#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800514#define REQ_F_INFLIGHT 16384 /* on inflight list */
515#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700516#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboece35a472019-12-17 08:04:44 -0700517#define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */
Jens Axboeba042912019-12-25 16:33:42 -0700518#define REQ_F_CUR_POS 262144 /* read/write uses file position */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700519 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600520 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600521 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700522
Jens Axboefcb323c2019-10-24 12:39:47 -0600523 struct list_head inflight_entry;
524
Jens Axboe561fb042019-10-24 07:25:42 -0600525 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700526};
527
528#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700529#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700530
Jens Axboe9a56a232019-01-09 09:06:50 -0700531struct io_submit_state {
532 struct blk_plug plug;
533
534 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700535 * io_kiocb alloc cache
536 */
537 void *reqs[IO_IOPOLL_BATCH];
538 unsigned int free_reqs;
539 unsigned int cur_req;
540
541 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700542 * File reference cache
543 */
544 struct file *file;
545 unsigned int fd;
546 unsigned int has_refs;
547 unsigned int used_refs;
548 unsigned int ios_left;
549};
550
Jens Axboed3656342019-12-18 09:50:26 -0700551struct io_op_def {
552 /* needs req->io allocated for deferral/async */
553 unsigned async_ctx : 1;
554 /* needs current->mm setup, does mm access */
555 unsigned needs_mm : 1;
556 /* needs req->file assigned */
557 unsigned needs_file : 1;
558 /* needs req->file assigned IFF fd is >= 0 */
559 unsigned fd_non_neg : 1;
560 /* hash wq insertion if file is a regular file */
561 unsigned hash_reg_file : 1;
562 /* unbound wq insertion if file is a non-regular file */
563 unsigned unbound_nonreg_file : 1;
564};
565
566static const struct io_op_def io_op_defs[] = {
567 {
568 /* IORING_OP_NOP */
569 },
570 {
571 /* IORING_OP_READV */
572 .async_ctx = 1,
573 .needs_mm = 1,
574 .needs_file = 1,
575 .unbound_nonreg_file = 1,
576 },
577 {
578 /* IORING_OP_WRITEV */
579 .async_ctx = 1,
580 .needs_mm = 1,
581 .needs_file = 1,
582 .hash_reg_file = 1,
583 .unbound_nonreg_file = 1,
584 },
585 {
586 /* IORING_OP_FSYNC */
587 .needs_file = 1,
588 },
589 {
590 /* IORING_OP_READ_FIXED */
591 .needs_file = 1,
592 .unbound_nonreg_file = 1,
593 },
594 {
595 /* IORING_OP_WRITE_FIXED */
596 .needs_file = 1,
597 .hash_reg_file = 1,
598 .unbound_nonreg_file = 1,
599 },
600 {
601 /* IORING_OP_POLL_ADD */
602 .needs_file = 1,
603 .unbound_nonreg_file = 1,
604 },
605 {
606 /* IORING_OP_POLL_REMOVE */
607 },
608 {
609 /* IORING_OP_SYNC_FILE_RANGE */
610 .needs_file = 1,
611 },
612 {
613 /* IORING_OP_SENDMSG */
614 .async_ctx = 1,
615 .needs_mm = 1,
616 .needs_file = 1,
617 .unbound_nonreg_file = 1,
618 },
619 {
620 /* IORING_OP_RECVMSG */
621 .async_ctx = 1,
622 .needs_mm = 1,
623 .needs_file = 1,
624 .unbound_nonreg_file = 1,
625 },
626 {
627 /* IORING_OP_TIMEOUT */
628 .async_ctx = 1,
629 .needs_mm = 1,
630 },
631 {
632 /* IORING_OP_TIMEOUT_REMOVE */
633 },
634 {
635 /* IORING_OP_ACCEPT */
636 .needs_mm = 1,
637 .needs_file = 1,
638 .unbound_nonreg_file = 1,
639 },
640 {
641 /* IORING_OP_ASYNC_CANCEL */
642 },
643 {
644 /* IORING_OP_LINK_TIMEOUT */
645 .async_ctx = 1,
646 .needs_mm = 1,
647 },
648 {
649 /* IORING_OP_CONNECT */
650 .async_ctx = 1,
651 .needs_mm = 1,
652 .needs_file = 1,
653 .unbound_nonreg_file = 1,
654 },
655 {
656 /* IORING_OP_FALLOCATE */
657 .needs_file = 1,
658 },
659 {
660 /* IORING_OP_OPENAT */
661 .needs_file = 1,
662 .fd_non_neg = 1,
663 },
664 {
665 /* IORING_OP_CLOSE */
666 .needs_file = 1,
667 },
668 {
669 /* IORING_OP_FILES_UPDATE */
670 .needs_mm = 1,
671 },
672 {
673 /* IORING_OP_STATX */
674 .needs_mm = 1,
675 .needs_file = 1,
676 .fd_non_neg = 1,
677 },
Jens Axboe3a6820f2019-12-22 15:19:35 -0700678 {
679 /* IORING_OP_READ */
680 .needs_mm = 1,
681 .needs_file = 1,
682 .unbound_nonreg_file = 1,
683 },
684 {
685 /* IORING_OP_WRITE */
686 .needs_mm = 1,
687 .needs_file = 1,
688 .unbound_nonreg_file = 1,
689 },
Jens Axboe4840e412019-12-25 22:03:45 -0700690 {
691 /* IORING_OP_FADVISE */
692 .needs_file = 1,
693 },
Jens Axboec1ca7572019-12-25 22:18:28 -0700694 {
695 /* IORING_OP_MADVISE */
696 .needs_mm = 1,
697 },
Jens Axboefddafac2020-01-04 20:19:44 -0700698 {
699 /* IORING_OP_SEND */
700 .needs_mm = 1,
701 .needs_file = 1,
702 .unbound_nonreg_file = 1,
703 },
704 {
705 /* IORING_OP_RECV */
706 .needs_mm = 1,
707 .needs_file = 1,
708 .unbound_nonreg_file = 1,
709 },
Jens Axboecebdb982020-01-08 17:59:24 -0700710 {
711 /* IORING_OP_OPENAT2 */
712 .needs_file = 1,
713 .fd_non_neg = 1,
714 },
Jens Axboed3656342019-12-18 09:50:26 -0700715};
716
Jens Axboe561fb042019-10-24 07:25:42 -0600717static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700718static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800719static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700720static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700721static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
722static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700723static int __io_sqe_files_update(struct io_ring_ctx *ctx,
724 struct io_uring_files_update *ip,
725 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600726
Jens Axboe2b188cc2019-01-07 10:46:33 -0700727static struct kmem_cache *req_cachep;
728
729static const struct file_operations io_uring_fops;
730
731struct sock *io_uring_get_socket(struct file *file)
732{
733#if defined(CONFIG_UNIX)
734 if (file->f_op == &io_uring_fops) {
735 struct io_ring_ctx *ctx = file->private_data;
736
737 return ctx->ring_sock->sk;
738 }
739#endif
740 return NULL;
741}
742EXPORT_SYMBOL(io_uring_get_socket);
743
744static void io_ring_ctx_ref_free(struct percpu_ref *ref)
745{
746 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
747
Jens Axboe206aefd2019-11-07 18:27:42 -0700748 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700749}
750
751static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
752{
753 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700754 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700755
756 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
757 if (!ctx)
758 return NULL;
759
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700760 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
761 if (!ctx->fallback_req)
762 goto err;
763
Jens Axboe206aefd2019-11-07 18:27:42 -0700764 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
765 if (!ctx->completions)
766 goto err;
767
Jens Axboe78076bb2019-12-04 19:56:40 -0700768 /*
769 * Use 5 bits less than the max cq entries, that should give us around
770 * 32 entries per hash list if totally full and uniformly spread.
771 */
772 hash_bits = ilog2(p->cq_entries);
773 hash_bits -= 5;
774 if (hash_bits <= 0)
775 hash_bits = 1;
776 ctx->cancel_hash_bits = hash_bits;
777 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
778 GFP_KERNEL);
779 if (!ctx->cancel_hash)
780 goto err;
781 __hash_init(ctx->cancel_hash, 1U << hash_bits);
782
Roman Gushchin21482892019-05-07 10:01:48 -0700783 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700784 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
785 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700786
787 ctx->flags = p->flags;
788 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700789 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700790 init_completion(&ctx->completions[0]);
791 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700792 mutex_init(&ctx->uring_lock);
793 init_waitqueue_head(&ctx->wait);
794 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700795 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700796 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600797 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600798 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600799 init_waitqueue_head(&ctx->inflight_wait);
800 spin_lock_init(&ctx->inflight_lock);
801 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700802 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700803err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700804 if (ctx->fallback_req)
805 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700806 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700807 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700808 kfree(ctx);
809 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700810}
811
Bob Liu9d858b22019-11-13 18:06:25 +0800812static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600813{
Jackie Liua197f662019-11-08 08:09:12 -0700814 struct io_ring_ctx *ctx = req->ctx;
815
Jens Axboe498ccd92019-10-25 10:04:25 -0600816 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
817 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600818}
819
Bob Liu9d858b22019-11-13 18:06:25 +0800820static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600821{
Bob Liu9d858b22019-11-13 18:06:25 +0800822 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
823 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600824
Bob Liu9d858b22019-11-13 18:06:25 +0800825 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600826}
827
828static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600829{
830 struct io_kiocb *req;
831
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600832 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800833 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600834 list_del_init(&req->list);
835 return req;
836 }
837
838 return NULL;
839}
840
Jens Axboe5262f562019-09-17 12:26:57 -0600841static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
842{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600843 struct io_kiocb *req;
844
845 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700846 if (req) {
847 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
848 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800849 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700850 list_del_init(&req->list);
851 return req;
852 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600853 }
854
855 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600856}
857
Jens Axboede0617e2019-04-06 21:51:27 -0600858static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700859{
Hristo Venev75b28af2019-08-26 17:23:46 +0000860 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700861
Hristo Venev75b28af2019-08-26 17:23:46 +0000862 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700863 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000864 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700865
Jens Axboe2b188cc2019-01-07 10:46:33 -0700866 if (wq_has_sleeper(&ctx->cq_wait)) {
867 wake_up_interruptible(&ctx->cq_wait);
868 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
869 }
870 }
871}
872
Jens Axboe94ae5e72019-11-14 19:39:52 -0700873static inline bool io_prep_async_work(struct io_kiocb *req,
874 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600875{
Jens Axboed3656342019-12-18 09:50:26 -0700876 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600877 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600878
Jens Axboed3656342019-12-18 09:50:26 -0700879 if (req->flags & REQ_F_ISREG) {
880 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700881 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700882 } else {
883 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700884 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600885 }
Jens Axboed3656342019-12-18 09:50:26 -0700886 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700887 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600888
Jens Axboe94ae5e72019-11-14 19:39:52 -0700889 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600890 return do_hashed;
891}
892
Jackie Liua197f662019-11-08 08:09:12 -0700893static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600894{
Jackie Liua197f662019-11-08 08:09:12 -0700895 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700896 struct io_kiocb *link;
897 bool do_hashed;
898
899 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600900
901 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
902 req->flags);
903 if (!do_hashed) {
904 io_wq_enqueue(ctx->io_wq, &req->work);
905 } else {
906 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
907 file_inode(req->file));
908 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700909
910 if (link)
911 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600912}
913
Jens Axboe5262f562019-09-17 12:26:57 -0600914static void io_kill_timeout(struct io_kiocb *req)
915{
916 int ret;
917
Jens Axboe2d283902019-12-04 11:08:05 -0700918 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600919 if (ret != -1) {
920 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600921 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700922 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800923 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600924 }
925}
926
927static void io_kill_timeouts(struct io_ring_ctx *ctx)
928{
929 struct io_kiocb *req, *tmp;
930
931 spin_lock_irq(&ctx->completion_lock);
932 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
933 io_kill_timeout(req);
934 spin_unlock_irq(&ctx->completion_lock);
935}
936
Jens Axboede0617e2019-04-06 21:51:27 -0600937static void io_commit_cqring(struct io_ring_ctx *ctx)
938{
939 struct io_kiocb *req;
940
Jens Axboe5262f562019-09-17 12:26:57 -0600941 while ((req = io_get_timeout_req(ctx)) != NULL)
942 io_kill_timeout(req);
943
Jens Axboede0617e2019-04-06 21:51:27 -0600944 __io_commit_cqring(ctx);
945
946 while ((req = io_get_deferred_req(ctx)) != NULL) {
947 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700948 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600949 }
950}
951
Jens Axboe2b188cc2019-01-07 10:46:33 -0700952static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
953{
Hristo Venev75b28af2019-08-26 17:23:46 +0000954 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700955 unsigned tail;
956
957 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200958 /*
959 * writes to the cq entry need to come after reading head; the
960 * control dependency is enough as we're using WRITE_ONCE to
961 * fill the cq entry
962 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000963 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700964 return NULL;
965
966 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000967 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700968}
969
Jens Axboef2842ab2020-01-08 11:04:00 -0700970static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
971{
972 if (!ctx->eventfd_async)
973 return true;
974 return io_wq_current_is_worker() || in_interrupt();
975}
976
Jens Axboe8c838782019-03-12 15:48:16 -0600977static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
978{
979 if (waitqueue_active(&ctx->wait))
980 wake_up(&ctx->wait);
981 if (waitqueue_active(&ctx->sqo_wait))
982 wake_up(&ctx->sqo_wait);
Jens Axboef2842ab2020-01-08 11:04:00 -0700983 if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -0600984 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600985}
986
Jens Axboec4a2ed72019-11-21 21:01:26 -0700987/* Returns true if there are no backlogged entries after the flush */
988static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700989{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700990 struct io_rings *rings = ctx->rings;
991 struct io_uring_cqe *cqe;
992 struct io_kiocb *req;
993 unsigned long flags;
994 LIST_HEAD(list);
995
996 if (!force) {
997 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700998 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700999 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1000 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001001 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001002 }
1003
1004 spin_lock_irqsave(&ctx->completion_lock, flags);
1005
1006 /* if force is set, the ring is going away. always drop after that */
1007 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001008 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001009
Jens Axboec4a2ed72019-11-21 21:01:26 -07001010 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001011 while (!list_empty(&ctx->cq_overflow_list)) {
1012 cqe = io_get_cqring(ctx);
1013 if (!cqe && !force)
1014 break;
1015
1016 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1017 list);
1018 list_move(&req->list, &list);
1019 if (cqe) {
1020 WRITE_ONCE(cqe->user_data, req->user_data);
1021 WRITE_ONCE(cqe->res, req->result);
1022 WRITE_ONCE(cqe->flags, 0);
1023 } else {
1024 WRITE_ONCE(ctx->rings->cq_overflow,
1025 atomic_inc_return(&ctx->cached_cq_overflow));
1026 }
1027 }
1028
1029 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001030 if (cqe) {
1031 clear_bit(0, &ctx->sq_check_overflow);
1032 clear_bit(0, &ctx->cq_check_overflow);
1033 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001034 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1035 io_cqring_ev_posted(ctx);
1036
1037 while (!list_empty(&list)) {
1038 req = list_first_entry(&list, struct io_kiocb, list);
1039 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001040 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001041 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001042
1043 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001044}
1045
Jens Axboe78e19bb2019-11-06 15:21:34 -07001046static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001048 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049 struct io_uring_cqe *cqe;
1050
Jens Axboe78e19bb2019-11-06 15:21:34 -07001051 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001052
Jens Axboe2b188cc2019-01-07 10:46:33 -07001053 /*
1054 * If we can't get a cq entry, userspace overflowed the
1055 * submission (by quite a lot). Increment the overflow count in
1056 * the ring.
1057 */
1058 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001059 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001060 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001061 WRITE_ONCE(cqe->res, res);
1062 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001063 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001064 WRITE_ONCE(ctx->rings->cq_overflow,
1065 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001066 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001067 if (list_empty(&ctx->cq_overflow_list)) {
1068 set_bit(0, &ctx->sq_check_overflow);
1069 set_bit(0, &ctx->cq_check_overflow);
1070 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001071 refcount_inc(&req->refs);
1072 req->result = res;
1073 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074 }
1075}
1076
Jens Axboe78e19bb2019-11-06 15:21:34 -07001077static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001078{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001079 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001080 unsigned long flags;
1081
1082 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001083 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084 io_commit_cqring(ctx);
1085 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1086
Jens Axboe8c838782019-03-12 15:48:16 -06001087 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001088}
1089
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001090static inline bool io_is_fallback_req(struct io_kiocb *req)
1091{
1092 return req == (struct io_kiocb *)
1093 ((unsigned long) req->ctx->fallback_req & ~1UL);
1094}
1095
1096static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1097{
1098 struct io_kiocb *req;
1099
1100 req = ctx->fallback_req;
1101 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1102 return req;
1103
1104 return NULL;
1105}
1106
Jens Axboe2579f912019-01-09 09:10:43 -07001107static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1108 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001109{
Jens Axboefd6fab22019-03-14 16:30:06 -06001110 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001111 struct io_kiocb *req;
1112
Jens Axboe2579f912019-01-09 09:10:43 -07001113 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001114 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001115 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001116 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001117 } else if (!state->free_reqs) {
1118 size_t sz;
1119 int ret;
1120
1121 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001122 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1123
1124 /*
1125 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1126 * retry single alloc to be on the safe side.
1127 */
1128 if (unlikely(ret <= 0)) {
1129 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1130 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001131 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001132 ret = 1;
1133 }
Jens Axboe2579f912019-01-09 09:10:43 -07001134 state->free_reqs = ret - 1;
1135 state->cur_req = 1;
1136 req = state->reqs[0];
1137 } else {
1138 req = state->reqs[state->cur_req];
1139 state->free_reqs--;
1140 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001141 }
1142
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001143got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001144 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001145 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001146 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001147 req->ctx = ctx;
1148 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001149 /* one is dropped after submission, the other at completion */
1150 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001151 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001152 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001153 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001154fallback:
1155 req = io_get_fallback_req(ctx);
1156 if (req)
1157 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001158 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001159 return NULL;
1160}
1161
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001162static void __io_req_do_free(struct io_kiocb *req)
1163{
1164 if (likely(!io_is_fallback_req(req)))
1165 kmem_cache_free(req_cachep, req);
1166 else
1167 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1168}
1169
Jens Axboec6ca97b302019-12-28 12:11:08 -07001170static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001171{
Jens Axboefcb323c2019-10-24 12:39:47 -06001172 struct io_ring_ctx *ctx = req->ctx;
1173
YueHaibing96fd84d2020-01-07 22:22:44 +08001174 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001175 if (req->file) {
1176 if (req->flags & REQ_F_FIXED_FILE)
1177 percpu_ref_put(&ctx->file_data->refs);
1178 else
1179 fput(req->file);
1180 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001181}
1182
1183static void __io_free_req(struct io_kiocb *req)
1184{
1185 __io_req_aux_free(req);
1186
Jens Axboefcb323c2019-10-24 12:39:47 -06001187 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001188 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001189 unsigned long flags;
1190
1191 spin_lock_irqsave(&ctx->inflight_lock, flags);
1192 list_del(&req->inflight_entry);
1193 if (waitqueue_active(&ctx->inflight_wait))
1194 wake_up(&ctx->inflight_wait);
1195 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1196 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001197
1198 percpu_ref_put(&req->ctx->refs);
1199 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001200}
1201
Jens Axboec6ca97b302019-12-28 12:11:08 -07001202struct req_batch {
1203 void *reqs[IO_IOPOLL_BATCH];
1204 int to_free;
1205 int need_iter;
1206};
1207
1208static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1209{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001210 int fixed_refs = rb->to_free;
1211
Jens Axboec6ca97b302019-12-28 12:11:08 -07001212 if (!rb->to_free)
1213 return;
1214 if (rb->need_iter) {
1215 int i, inflight = 0;
1216 unsigned long flags;
1217
Jens Axboe10fef4b2020-01-09 07:52:28 -07001218 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001219 for (i = 0; i < rb->to_free; i++) {
1220 struct io_kiocb *req = rb->reqs[i];
1221
Jens Axboe10fef4b2020-01-09 07:52:28 -07001222 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001223 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001224 fixed_refs++;
1225 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001226 if (req->flags & REQ_F_INFLIGHT)
1227 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001228 __io_req_aux_free(req);
1229 }
1230 if (!inflight)
1231 goto do_free;
1232
1233 spin_lock_irqsave(&ctx->inflight_lock, flags);
1234 for (i = 0; i < rb->to_free; i++) {
1235 struct io_kiocb *req = rb->reqs[i];
1236
Jens Axboe10fef4b2020-01-09 07:52:28 -07001237 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001238 list_del(&req->inflight_entry);
1239 if (!--inflight)
1240 break;
1241 }
1242 }
1243 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1244
1245 if (waitqueue_active(&ctx->inflight_wait))
1246 wake_up(&ctx->inflight_wait);
1247 }
1248do_free:
1249 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001250 if (fixed_refs)
1251 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001252 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001253 rb->to_free = rb->need_iter = 0;
1254}
1255
Jackie Liua197f662019-11-08 08:09:12 -07001256static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001257{
Jackie Liua197f662019-11-08 08:09:12 -07001258 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001259 int ret;
1260
Jens Axboe2d283902019-12-04 11:08:05 -07001261 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001262 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001263 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001264 io_commit_cqring(ctx);
1265 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001266 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001267 return true;
1268 }
1269
1270 return false;
1271}
1272
Jens Axboeba816ad2019-09-28 11:36:45 -06001273static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001274{
Jens Axboe2665abf2019-11-05 12:40:47 -07001275 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001276 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001277
Jens Axboe4d7dd462019-11-20 13:03:52 -07001278 /* Already got next link */
1279 if (req->flags & REQ_F_LINK_NEXT)
1280 return;
1281
Jens Axboe9e645e112019-05-10 16:07:28 -06001282 /*
1283 * The list should never be empty when we are called here. But could
1284 * potentially happen if the chain is messed up, check to be on the
1285 * safe side.
1286 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001287 while (!list_empty(&req->link_list)) {
1288 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1289 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001290
Pavel Begunkov44932332019-12-05 16:16:35 +03001291 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1292 (nxt->flags & REQ_F_TIMEOUT))) {
1293 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001294 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001295 req->flags &= ~REQ_F_LINK_TIMEOUT;
1296 continue;
1297 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001298
Pavel Begunkov44932332019-12-05 16:16:35 +03001299 list_del_init(&req->link_list);
1300 if (!list_empty(&nxt->link_list))
1301 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001302 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001303 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001304 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001305
Jens Axboe4d7dd462019-11-20 13:03:52 -07001306 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001307 if (wake_ev)
1308 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001309}
1310
1311/*
1312 * Called if REQ_F_LINK is set, and we fail the head request
1313 */
1314static void io_fail_links(struct io_kiocb *req)
1315{
Jens Axboe2665abf2019-11-05 12:40:47 -07001316 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001317 unsigned long flags;
1318
1319 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001320
1321 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001322 struct io_kiocb *link = list_first_entry(&req->link_list,
1323 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001324
Pavel Begunkov44932332019-12-05 16:16:35 +03001325 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001326 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001327
1328 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001329 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001330 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001331 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001332 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001333 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001334 }
Jens Axboe5d960722019-11-19 15:31:28 -07001335 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001336 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001337
1338 io_commit_cqring(ctx);
1339 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1340 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001341}
1342
Jens Axboe4d7dd462019-11-20 13:03:52 -07001343static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001344{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001345 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001346 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001347
Jens Axboe9e645e112019-05-10 16:07:28 -06001348 /*
1349 * If LINK is set, we have dependent requests in this chain. If we
1350 * didn't fail this request, queue the first one up, moving any other
1351 * dependencies to the next request. In case of failure, fail the rest
1352 * of the chain.
1353 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001354 if (req->flags & REQ_F_FAIL_LINK) {
1355 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001356 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1357 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001358 struct io_ring_ctx *ctx = req->ctx;
1359 unsigned long flags;
1360
1361 /*
1362 * If this is a timeout link, we could be racing with the
1363 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001364 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001365 */
1366 spin_lock_irqsave(&ctx->completion_lock, flags);
1367 io_req_link_next(req, nxt);
1368 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1369 } else {
1370 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001371 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001372}
Jens Axboe9e645e112019-05-10 16:07:28 -06001373
Jackie Liuc69f8db2019-11-09 11:00:08 +08001374static void io_free_req(struct io_kiocb *req)
1375{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001376 struct io_kiocb *nxt = NULL;
1377
1378 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001379 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001380
1381 if (nxt)
1382 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001383}
1384
Jens Axboeba816ad2019-09-28 11:36:45 -06001385/*
1386 * Drop reference to request, return next in chain (if there is one) if this
1387 * was the last reference to this request.
1388 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001389__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001390static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001391{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001392 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001393
Jens Axboee65ef562019-03-12 10:16:44 -06001394 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001395 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001396}
1397
Jens Axboe2b188cc2019-01-07 10:46:33 -07001398static void io_put_req(struct io_kiocb *req)
1399{
Jens Axboedef596e2019-01-09 08:59:42 -07001400 if (refcount_dec_and_test(&req->refs))
1401 io_free_req(req);
1402}
1403
Jens Axboe978db572019-11-14 22:39:04 -07001404/*
1405 * Must only be used if we don't need to care about links, usually from
1406 * within the completion handling itself.
1407 */
1408static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001409{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001410 /* drop both submit and complete references */
1411 if (refcount_sub_and_test(2, &req->refs))
1412 __io_free_req(req);
1413}
1414
Jens Axboe978db572019-11-14 22:39:04 -07001415static void io_double_put_req(struct io_kiocb *req)
1416{
1417 /* drop both submit and complete references */
1418 if (refcount_sub_and_test(2, &req->refs))
1419 io_free_req(req);
1420}
1421
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001422static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001423{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001424 struct io_rings *rings = ctx->rings;
1425
Jens Axboead3eb2c2019-12-18 17:12:20 -07001426 if (test_bit(0, &ctx->cq_check_overflow)) {
1427 /*
1428 * noflush == true is from the waitqueue handler, just ensure
1429 * we wake up the task, and the next invocation will flush the
1430 * entries. We cannot safely to it from here.
1431 */
1432 if (noflush && !list_empty(&ctx->cq_overflow_list))
1433 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001434
Jens Axboead3eb2c2019-12-18 17:12:20 -07001435 io_cqring_overflow_flush(ctx, false);
1436 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001437
Jens Axboea3a0e432019-08-20 11:03:11 -06001438 /* See comment at the top of this file */
1439 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001440 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001441}
1442
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001443static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1444{
1445 struct io_rings *rings = ctx->rings;
1446
1447 /* make sure SQ entry isn't read before tail */
1448 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1449}
1450
Jens Axboe8237e042019-12-28 10:48:22 -07001451static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001452{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001453 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1454 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001455
Jens Axboec6ca97b302019-12-28 12:11:08 -07001456 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1457 rb->need_iter++;
1458
1459 rb->reqs[rb->to_free++] = req;
1460 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1461 io_free_req_many(req->ctx, rb);
1462 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001463}
1464
Jens Axboedef596e2019-01-09 08:59:42 -07001465/*
1466 * Find and free completed poll iocbs
1467 */
1468static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1469 struct list_head *done)
1470{
Jens Axboe8237e042019-12-28 10:48:22 -07001471 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001472 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001473
Jens Axboec6ca97b302019-12-28 12:11:08 -07001474 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001475 while (!list_empty(done)) {
1476 req = list_first_entry(done, struct io_kiocb, list);
1477 list_del(&req->list);
1478
Jens Axboe78e19bb2019-11-06 15:21:34 -07001479 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001480 (*nr_events)++;
1481
Jens Axboe8237e042019-12-28 10:48:22 -07001482 if (refcount_dec_and_test(&req->refs) &&
1483 !io_req_multi_free(&rb, req))
1484 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001485 }
Jens Axboedef596e2019-01-09 08:59:42 -07001486
Jens Axboe09bb8392019-03-13 12:39:28 -06001487 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001488 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001489}
1490
1491static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1492 long min)
1493{
1494 struct io_kiocb *req, *tmp;
1495 LIST_HEAD(done);
1496 bool spin;
1497 int ret;
1498
1499 /*
1500 * Only spin for completions if we don't have multiple devices hanging
1501 * off our complete list, and we're under the requested amount.
1502 */
1503 spin = !ctx->poll_multi_file && *nr_events < min;
1504
1505 ret = 0;
1506 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001507 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001508
1509 /*
1510 * Move completed entries to our local list. If we find a
1511 * request that requires polling, break out and complete
1512 * the done list first, if we have entries there.
1513 */
1514 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1515 list_move_tail(&req->list, &done);
1516 continue;
1517 }
1518 if (!list_empty(&done))
1519 break;
1520
1521 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1522 if (ret < 0)
1523 break;
1524
1525 if (ret && spin)
1526 spin = false;
1527 ret = 0;
1528 }
1529
1530 if (!list_empty(&done))
1531 io_iopoll_complete(ctx, nr_events, &done);
1532
1533 return ret;
1534}
1535
1536/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001537 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001538 * non-spinning poll check - we'll still enter the driver poll loop, but only
1539 * as a non-spinning completion check.
1540 */
1541static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1542 long min)
1543{
Jens Axboe08f54392019-08-21 22:19:11 -06001544 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001545 int ret;
1546
1547 ret = io_do_iopoll(ctx, nr_events, min);
1548 if (ret < 0)
1549 return ret;
1550 if (!min || *nr_events >= min)
1551 return 0;
1552 }
1553
1554 return 1;
1555}
1556
1557/*
1558 * We can't just wait for polled events to come to us, we have to actively
1559 * find and complete them.
1560 */
1561static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1562{
1563 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1564 return;
1565
1566 mutex_lock(&ctx->uring_lock);
1567 while (!list_empty(&ctx->poll_list)) {
1568 unsigned int nr_events = 0;
1569
1570 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001571
1572 /*
1573 * Ensure we allow local-to-the-cpu processing to take place,
1574 * in this case we need to ensure that we reap all events.
1575 */
1576 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001577 }
1578 mutex_unlock(&ctx->uring_lock);
1579}
1580
Jens Axboe2b2ed972019-10-25 10:06:15 -06001581static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1582 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001583{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001584 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001585
1586 do {
1587 int tmin = 0;
1588
Jens Axboe500f9fb2019-08-19 12:15:59 -06001589 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001590 * Don't enter poll loop if we already have events pending.
1591 * If we do, we can potentially be spinning for commands that
1592 * already triggered a CQE (eg in error).
1593 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001594 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001595 break;
1596
1597 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001598 * If a submit got punted to a workqueue, we can have the
1599 * application entering polling for a command before it gets
1600 * issued. That app will hold the uring_lock for the duration
1601 * of the poll right here, so we need to take a breather every
1602 * now and then to ensure that the issue has a chance to add
1603 * the poll to the issued list. Otherwise we can spin here
1604 * forever, while the workqueue is stuck trying to acquire the
1605 * very same mutex.
1606 */
1607 if (!(++iters & 7)) {
1608 mutex_unlock(&ctx->uring_lock);
1609 mutex_lock(&ctx->uring_lock);
1610 }
1611
Jens Axboedef596e2019-01-09 08:59:42 -07001612 if (*nr_events < min)
1613 tmin = min - *nr_events;
1614
1615 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1616 if (ret <= 0)
1617 break;
1618 ret = 0;
1619 } while (min && !*nr_events && !need_resched());
1620
Jens Axboe2b2ed972019-10-25 10:06:15 -06001621 return ret;
1622}
1623
1624static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1625 long min)
1626{
1627 int ret;
1628
1629 /*
1630 * We disallow the app entering submit/complete with polling, but we
1631 * still need to lock the ring to prevent racing with polled issue
1632 * that got punted to a workqueue.
1633 */
1634 mutex_lock(&ctx->uring_lock);
1635 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001636 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001637 return ret;
1638}
1639
Jens Axboe491381ce2019-10-17 09:20:46 -06001640static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001641{
Jens Axboe491381ce2019-10-17 09:20:46 -06001642 /*
1643 * Tell lockdep we inherited freeze protection from submission
1644 * thread.
1645 */
1646 if (req->flags & REQ_F_ISREG) {
1647 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001648
Jens Axboe491381ce2019-10-17 09:20:46 -06001649 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001650 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001651 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001652}
1653
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001654static inline void req_set_fail_links(struct io_kiocb *req)
1655{
1656 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1657 req->flags |= REQ_F_FAIL_LINK;
1658}
1659
Jens Axboeba816ad2019-09-28 11:36:45 -06001660static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001661{
Jens Axboe9adbd452019-12-20 08:45:55 -07001662 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001663
Jens Axboe491381ce2019-10-17 09:20:46 -06001664 if (kiocb->ki_flags & IOCB_WRITE)
1665 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001666
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001667 if (res != req->result)
1668 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001669 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001670}
1671
1672static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1673{
Jens Axboe9adbd452019-12-20 08:45:55 -07001674 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001675
1676 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001677 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001678}
1679
Jens Axboeba816ad2019-09-28 11:36:45 -06001680static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1681{
Jens Axboe9adbd452019-12-20 08:45:55 -07001682 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001683 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001684
1685 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001686 io_put_req_find_next(req, &nxt);
1687
1688 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001689}
1690
Jens Axboedef596e2019-01-09 08:59:42 -07001691static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1692{
Jens Axboe9adbd452019-12-20 08:45:55 -07001693 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001694
Jens Axboe491381ce2019-10-17 09:20:46 -06001695 if (kiocb->ki_flags & IOCB_WRITE)
1696 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001697
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001698 if (res != req->result)
1699 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001700 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001701 if (res != -EAGAIN)
1702 req->flags |= REQ_F_IOPOLL_COMPLETED;
1703}
1704
1705/*
1706 * After the iocb has been issued, it's safe to be found on the poll list.
1707 * Adding the kiocb to the list AFTER submission ensures that we don't
1708 * find it from a io_iopoll_getevents() thread before the issuer is done
1709 * accessing the kiocb cookie.
1710 */
1711static void io_iopoll_req_issued(struct io_kiocb *req)
1712{
1713 struct io_ring_ctx *ctx = req->ctx;
1714
1715 /*
1716 * Track whether we have multiple files in our lists. This will impact
1717 * how we do polling eventually, not spinning if we're on potentially
1718 * different devices.
1719 */
1720 if (list_empty(&ctx->poll_list)) {
1721 ctx->poll_multi_file = false;
1722 } else if (!ctx->poll_multi_file) {
1723 struct io_kiocb *list_req;
1724
1725 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1726 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001727 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001728 ctx->poll_multi_file = true;
1729 }
1730
1731 /*
1732 * For fast devices, IO may have already completed. If it has, add
1733 * it to the front so we find it first.
1734 */
1735 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1736 list_add(&req->list, &ctx->poll_list);
1737 else
1738 list_add_tail(&req->list, &ctx->poll_list);
1739}
1740
Jens Axboe3d6770f2019-04-13 11:50:54 -06001741static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001742{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001743 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001744 int diff = state->has_refs - state->used_refs;
1745
1746 if (diff)
1747 fput_many(state->file, diff);
1748 state->file = NULL;
1749 }
1750}
1751
1752/*
1753 * Get as many references to a file as we have IOs left in this submission,
1754 * assuming most submissions are for one file, or at least that each file
1755 * has more than one submission.
1756 */
1757static struct file *io_file_get(struct io_submit_state *state, int fd)
1758{
1759 if (!state)
1760 return fget(fd);
1761
1762 if (state->file) {
1763 if (state->fd == fd) {
1764 state->used_refs++;
1765 state->ios_left--;
1766 return state->file;
1767 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001768 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001769 }
1770 state->file = fget_many(fd, state->ios_left);
1771 if (!state->file)
1772 return NULL;
1773
1774 state->fd = fd;
1775 state->has_refs = state->ios_left;
1776 state->used_refs = 1;
1777 state->ios_left--;
1778 return state->file;
1779}
1780
Jens Axboe2b188cc2019-01-07 10:46:33 -07001781/*
1782 * If we tracked the file through the SCM inflight mechanism, we could support
1783 * any file. For now, just ensure that anything potentially problematic is done
1784 * inline.
1785 */
1786static bool io_file_supports_async(struct file *file)
1787{
1788 umode_t mode = file_inode(file)->i_mode;
1789
Jens Axboe10d59342019-12-09 20:16:22 -07001790 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001791 return true;
1792 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1793 return true;
1794
1795 return false;
1796}
1797
Jens Axboe3529d8c2019-12-19 18:24:38 -07001798static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1799 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001800{
Jens Axboedef596e2019-01-09 08:59:42 -07001801 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001802 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001803 unsigned ioprio;
1804 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001805
Jens Axboe09bb8392019-03-13 12:39:28 -06001806 if (!req->file)
1807 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001808
Jens Axboe491381ce2019-10-17 09:20:46 -06001809 if (S_ISREG(file_inode(req->file)->i_mode))
1810 req->flags |= REQ_F_ISREG;
1811
Jens Axboe2b188cc2019-01-07 10:46:33 -07001812 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001813 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1814 req->flags |= REQ_F_CUR_POS;
1815 kiocb->ki_pos = req->file->f_pos;
1816 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001817 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1818 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1819
1820 ioprio = READ_ONCE(sqe->ioprio);
1821 if (ioprio) {
1822 ret = ioprio_check_cap(ioprio);
1823 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001824 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001825
1826 kiocb->ki_ioprio = ioprio;
1827 } else
1828 kiocb->ki_ioprio = get_current_ioprio();
1829
1830 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1831 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001832 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001833
1834 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001835 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1836 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001837 req->flags |= REQ_F_NOWAIT;
1838
1839 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001840 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001841
Jens Axboedef596e2019-01-09 08:59:42 -07001842 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001843 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1844 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001845 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001846
Jens Axboedef596e2019-01-09 08:59:42 -07001847 kiocb->ki_flags |= IOCB_HIPRI;
1848 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001849 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001850 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001851 if (kiocb->ki_flags & IOCB_HIPRI)
1852 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001853 kiocb->ki_complete = io_complete_rw;
1854 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001855
Jens Axboe3529d8c2019-12-19 18:24:38 -07001856 req->rw.addr = READ_ONCE(sqe->addr);
1857 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001858 /* we own ->private, reuse it for the buffer index */
1859 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001860 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001861 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001862}
1863
1864static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1865{
1866 switch (ret) {
1867 case -EIOCBQUEUED:
1868 break;
1869 case -ERESTARTSYS:
1870 case -ERESTARTNOINTR:
1871 case -ERESTARTNOHAND:
1872 case -ERESTART_RESTARTBLOCK:
1873 /*
1874 * We can't just restart the syscall, since previously
1875 * submitted sqes may already be in progress. Just fail this
1876 * IO with EINTR.
1877 */
1878 ret = -EINTR;
1879 /* fall through */
1880 default:
1881 kiocb->ki_complete(kiocb, ret, 0);
1882 }
1883}
1884
Jens Axboeba816ad2019-09-28 11:36:45 -06001885static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1886 bool in_async)
1887{
Jens Axboeba042912019-12-25 16:33:42 -07001888 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1889
1890 if (req->flags & REQ_F_CUR_POS)
1891 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001892 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001893 *nxt = __io_complete_rw(kiocb, ret);
1894 else
1895 io_rw_done(kiocb, ret);
1896}
1897
Jens Axboe9adbd452019-12-20 08:45:55 -07001898static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001899 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001900{
Jens Axboe9adbd452019-12-20 08:45:55 -07001901 struct io_ring_ctx *ctx = req->ctx;
1902 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001903 struct io_mapped_ubuf *imu;
1904 unsigned index, buf_index;
1905 size_t offset;
1906 u64 buf_addr;
1907
1908 /* attempt to use fixed buffers without having provided iovecs */
1909 if (unlikely(!ctx->user_bufs))
1910 return -EFAULT;
1911
Jens Axboe9adbd452019-12-20 08:45:55 -07001912 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001913 if (unlikely(buf_index >= ctx->nr_user_bufs))
1914 return -EFAULT;
1915
1916 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1917 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001918 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001919
1920 /* overflow */
1921 if (buf_addr + len < buf_addr)
1922 return -EFAULT;
1923 /* not inside the mapped region */
1924 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1925 return -EFAULT;
1926
1927 /*
1928 * May not be a start of buffer, set size appropriately
1929 * and advance us to the beginning.
1930 */
1931 offset = buf_addr - imu->ubuf;
1932 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001933
1934 if (offset) {
1935 /*
1936 * Don't use iov_iter_advance() here, as it's really slow for
1937 * using the latter parts of a big fixed buffer - it iterates
1938 * over each segment manually. We can cheat a bit here, because
1939 * we know that:
1940 *
1941 * 1) it's a BVEC iter, we set it up
1942 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1943 * first and last bvec
1944 *
1945 * So just find our index, and adjust the iterator afterwards.
1946 * If the offset is within the first bvec (or the whole first
1947 * bvec, just use iov_iter_advance(). This makes it easier
1948 * since we can just skip the first segment, which may not
1949 * be PAGE_SIZE aligned.
1950 */
1951 const struct bio_vec *bvec = imu->bvec;
1952
1953 if (offset <= bvec->bv_len) {
1954 iov_iter_advance(iter, offset);
1955 } else {
1956 unsigned long seg_skip;
1957
1958 /* skip first vec */
1959 offset -= bvec->bv_len;
1960 seg_skip = 1 + (offset >> PAGE_SHIFT);
1961
1962 iter->bvec = bvec + seg_skip;
1963 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001964 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001965 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001966 }
1967 }
1968
Jens Axboe5e559562019-11-13 16:12:46 -07001969 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001970}
1971
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001972static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1973 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001974{
Jens Axboe9adbd452019-12-20 08:45:55 -07001975 void __user *buf = u64_to_user_ptr(req->rw.addr);
1976 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001977 u8 opcode;
1978
Jens Axboed625c6e2019-12-17 19:53:05 -07001979 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001980 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001981 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001982 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001983 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001984
Jens Axboe9adbd452019-12-20 08:45:55 -07001985 /* buffer index only valid with fixed read/write */
1986 if (req->rw.kiocb.private)
1987 return -EINVAL;
1988
Jens Axboe3a6820f2019-12-22 15:19:35 -07001989 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1990 ssize_t ret;
1991 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1992 *iovec = NULL;
1993 return ret;
1994 }
1995
Jens Axboef67676d2019-12-02 11:03:47 -07001996 if (req->io) {
1997 struct io_async_rw *iorw = &req->io->rw;
1998
1999 *iovec = iorw->iov;
2000 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2001 if (iorw->iov == iorw->fast_iov)
2002 *iovec = NULL;
2003 return iorw->size;
2004 }
2005
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002006 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002007 return -EFAULT;
2008
2009#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002010 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002011 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2012 iovec, iter);
2013#endif
2014
2015 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2016}
2017
Jens Axboe32960612019-09-23 11:05:34 -06002018/*
2019 * For files that don't have ->read_iter() and ->write_iter(), handle them
2020 * by looping over ->read() or ->write() manually.
2021 */
2022static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2023 struct iov_iter *iter)
2024{
2025 ssize_t ret = 0;
2026
2027 /*
2028 * Don't support polled IO through this interface, and we can't
2029 * support non-blocking either. For the latter, this just causes
2030 * the kiocb to be handled from an async context.
2031 */
2032 if (kiocb->ki_flags & IOCB_HIPRI)
2033 return -EOPNOTSUPP;
2034 if (kiocb->ki_flags & IOCB_NOWAIT)
2035 return -EAGAIN;
2036
2037 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002038 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002039 ssize_t nr;
2040
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002041 if (!iov_iter_is_bvec(iter)) {
2042 iovec = iov_iter_iovec(iter);
2043 } else {
2044 /* fixed buffers import bvec */
2045 iovec.iov_base = kmap(iter->bvec->bv_page)
2046 + iter->iov_offset;
2047 iovec.iov_len = min(iter->count,
2048 iter->bvec->bv_len - iter->iov_offset);
2049 }
2050
Jens Axboe32960612019-09-23 11:05:34 -06002051 if (rw == READ) {
2052 nr = file->f_op->read(file, iovec.iov_base,
2053 iovec.iov_len, &kiocb->ki_pos);
2054 } else {
2055 nr = file->f_op->write(file, iovec.iov_base,
2056 iovec.iov_len, &kiocb->ki_pos);
2057 }
2058
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002059 if (iov_iter_is_bvec(iter))
2060 kunmap(iter->bvec->bv_page);
2061
Jens Axboe32960612019-09-23 11:05:34 -06002062 if (nr < 0) {
2063 if (!ret)
2064 ret = nr;
2065 break;
2066 }
2067 ret += nr;
2068 if (nr != iovec.iov_len)
2069 break;
2070 iov_iter_advance(iter, nr);
2071 }
2072
2073 return ret;
2074}
2075
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002076static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002077 struct iovec *iovec, struct iovec *fast_iov,
2078 struct iov_iter *iter)
2079{
2080 req->io->rw.nr_segs = iter->nr_segs;
2081 req->io->rw.size = io_size;
2082 req->io->rw.iov = iovec;
2083 if (!req->io->rw.iov) {
2084 req->io->rw.iov = req->io->rw.fast_iov;
2085 memcpy(req->io->rw.iov, fast_iov,
2086 sizeof(struct iovec) * iter->nr_segs);
2087 }
2088}
2089
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002090static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002091{
Jens Axboed3656342019-12-18 09:50:26 -07002092 if (!io_op_defs[req->opcode].async_ctx)
2093 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002094 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002095 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002096}
2097
2098static void io_rw_async(struct io_wq_work **workptr)
2099{
2100 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2101 struct iovec *iov = NULL;
2102
2103 if (req->io->rw.iov != req->io->rw.fast_iov)
2104 iov = req->io->rw.iov;
2105 io_wq_submit_work(workptr);
2106 kfree(iov);
2107}
2108
2109static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2110 struct iovec *iovec, struct iovec *fast_iov,
2111 struct iov_iter *iter)
2112{
Jens Axboe74566df2020-01-13 19:23:24 -07002113 if (req->opcode == IORING_OP_READ_FIXED ||
2114 req->opcode == IORING_OP_WRITE_FIXED)
2115 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002116 if (!req->io && io_alloc_async_ctx(req))
2117 return -ENOMEM;
2118
2119 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2120 req->work.func = io_rw_async;
2121 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002122}
2123
Jens Axboe3529d8c2019-12-19 18:24:38 -07002124static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2125 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002126{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002127 struct io_async_ctx *io;
2128 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002129 ssize_t ret;
2130
Jens Axboe3529d8c2019-12-19 18:24:38 -07002131 ret = io_prep_rw(req, sqe, force_nonblock);
2132 if (ret)
2133 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002134
Jens Axboe3529d8c2019-12-19 18:24:38 -07002135 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2136 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002137
Jens Axboe3529d8c2019-12-19 18:24:38 -07002138 if (!req->io)
2139 return 0;
2140
2141 io = req->io;
2142 io->rw.iov = io->rw.fast_iov;
2143 req->io = NULL;
2144 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2145 req->io = io;
2146 if (ret < 0)
2147 return ret;
2148
2149 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2150 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002151}
2152
Pavel Begunkov267bc902019-11-07 01:41:08 +03002153static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002154 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002155{
2156 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002157 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002158 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002159 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002160 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002161
Jens Axboe3529d8c2019-12-19 18:24:38 -07002162 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002163 if (ret < 0)
2164 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002165
Jens Axboefd6c2e42019-12-18 12:19:41 -07002166 /* Ensure we clear previously set non-block flag */
2167 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002168 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002169
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002170 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002171 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002172 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002173 req->result = io_size;
2174
2175 /*
2176 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2177 * we know to async punt it even if it was opened O_NONBLOCK
2178 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002179 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002180 req->flags |= REQ_F_MUST_PUNT;
2181 goto copy_iov;
2182 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002183
Jens Axboe31b51512019-01-18 22:56:34 -07002184 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002185 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002186 if (!ret) {
2187 ssize_t ret2;
2188
Jens Axboe9adbd452019-12-20 08:45:55 -07002189 if (req->file->f_op->read_iter)
2190 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002191 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002192 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002193
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002194 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002195 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002196 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002197 } else {
2198copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002199 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002200 inline_vecs, &iter);
2201 if (ret)
2202 goto out_free;
2203 return -EAGAIN;
2204 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002205 }
Jens Axboef67676d2019-12-02 11:03:47 -07002206out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002207 if (!io_wq_current_is_worker())
2208 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002209 return ret;
2210}
2211
Jens Axboe3529d8c2019-12-19 18:24:38 -07002212static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2213 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002214{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002215 struct io_async_ctx *io;
2216 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002217 ssize_t ret;
2218
Jens Axboe3529d8c2019-12-19 18:24:38 -07002219 ret = io_prep_rw(req, sqe, force_nonblock);
2220 if (ret)
2221 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002222
Jens Axboe3529d8c2019-12-19 18:24:38 -07002223 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2224 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002225
Jens Axboe3529d8c2019-12-19 18:24:38 -07002226 if (!req->io)
2227 return 0;
2228
2229 io = req->io;
2230 io->rw.iov = io->rw.fast_iov;
2231 req->io = NULL;
2232 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2233 req->io = io;
2234 if (ret < 0)
2235 return ret;
2236
2237 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2238 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002239}
2240
Pavel Begunkov267bc902019-11-07 01:41:08 +03002241static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002242 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002243{
2244 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002245 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002246 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002247 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002248 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002249
Jens Axboe3529d8c2019-12-19 18:24:38 -07002250 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002251 if (ret < 0)
2252 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002253
Jens Axboefd6c2e42019-12-18 12:19:41 -07002254 /* Ensure we clear previously set non-block flag */
2255 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002256 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002257
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002258 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002259 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002260 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002261 req->result = io_size;
2262
2263 /*
2264 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2265 * we know to async punt it even if it was opened O_NONBLOCK
2266 */
2267 if (force_nonblock && !io_file_supports_async(req->file)) {
2268 req->flags |= REQ_F_MUST_PUNT;
2269 goto copy_iov;
2270 }
2271
Jens Axboe10d59342019-12-09 20:16:22 -07002272 /* file path doesn't support NOWAIT for non-direct_IO */
2273 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2274 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002275 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002276
Jens Axboe31b51512019-01-18 22:56:34 -07002277 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002278 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002279 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002280 ssize_t ret2;
2281
Jens Axboe2b188cc2019-01-07 10:46:33 -07002282 /*
2283 * Open-code file_start_write here to grab freeze protection,
2284 * which will be released by another thread in
2285 * io_complete_rw(). Fool lockdep by telling it the lock got
2286 * released so that it doesn't complain about the held lock when
2287 * we return to userspace.
2288 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002289 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002290 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002291 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002292 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002293 SB_FREEZE_WRITE);
2294 }
2295 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002296
Jens Axboe9adbd452019-12-20 08:45:55 -07002297 if (req->file->f_op->write_iter)
2298 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002299 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002300 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002301 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002302 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002303 } else {
2304copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002305 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002306 inline_vecs, &iter);
2307 if (ret)
2308 goto out_free;
2309 return -EAGAIN;
2310 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002311 }
Jens Axboe31b51512019-01-18 22:56:34 -07002312out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002313 if (!io_wq_current_is_worker())
2314 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002315 return ret;
2316}
2317
2318/*
2319 * IORING_OP_NOP just posts a completion event, nothing else.
2320 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002321static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002322{
2323 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002324
Jens Axboedef596e2019-01-09 08:59:42 -07002325 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2326 return -EINVAL;
2327
Jens Axboe78e19bb2019-11-06 15:21:34 -07002328 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002329 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002330 return 0;
2331}
2332
Jens Axboe3529d8c2019-12-19 18:24:38 -07002333static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002334{
Jens Axboe6b063142019-01-10 22:13:58 -07002335 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002336
Jens Axboe09bb8392019-03-13 12:39:28 -06002337 if (!req->file)
2338 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002339
Jens Axboe6b063142019-01-10 22:13:58 -07002340 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002341 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002342 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002343 return -EINVAL;
2344
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002345 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2346 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2347 return -EINVAL;
2348
2349 req->sync.off = READ_ONCE(sqe->off);
2350 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002351 return 0;
2352}
2353
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002354static bool io_req_cancelled(struct io_kiocb *req)
2355{
2356 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2357 req_set_fail_links(req);
2358 io_cqring_add_event(req, -ECANCELED);
2359 io_put_req(req);
2360 return true;
2361 }
2362
2363 return false;
2364}
2365
Jens Axboe78912932020-01-14 22:09:06 -07002366static void io_link_work_cb(struct io_wq_work **workptr)
2367{
2368 struct io_wq_work *work = *workptr;
2369 struct io_kiocb *link = work->data;
2370
2371 io_queue_linked_timeout(link);
2372 work->func = io_wq_submit_work;
2373}
2374
2375static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2376{
2377 struct io_kiocb *link;
2378
2379 io_prep_async_work(nxt, &link);
2380 *workptr = &nxt->work;
2381 if (link) {
2382 nxt->work.flags |= IO_WQ_WORK_CB;
2383 nxt->work.func = io_link_work_cb;
2384 nxt->work.data = link;
2385 }
2386}
2387
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002388static void io_fsync_finish(struct io_wq_work **workptr)
2389{
2390 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2391 loff_t end = req->sync.off + req->sync.len;
2392 struct io_kiocb *nxt = NULL;
2393 int ret;
2394
2395 if (io_req_cancelled(req))
2396 return;
2397
Jens Axboe9adbd452019-12-20 08:45:55 -07002398 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002399 end > 0 ? end : LLONG_MAX,
2400 req->sync.flags & IORING_FSYNC_DATASYNC);
2401 if (ret < 0)
2402 req_set_fail_links(req);
2403 io_cqring_add_event(req, ret);
2404 io_put_req_find_next(req, &nxt);
2405 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002406 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002407}
2408
Jens Axboefc4df992019-12-10 14:38:45 -07002409static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2410 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002411{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002412 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002413
2414 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002415 if (force_nonblock) {
2416 io_put_req(req);
2417 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002418 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002419 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002420
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002421 work = old_work = &req->work;
2422 io_fsync_finish(&work);
2423 if (work && work != old_work)
2424 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002425 return 0;
2426}
2427
Jens Axboed63d1b52019-12-10 10:38:56 -07002428static void io_fallocate_finish(struct io_wq_work **workptr)
2429{
2430 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2431 struct io_kiocb *nxt = NULL;
2432 int ret;
2433
2434 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2435 req->sync.len);
2436 if (ret < 0)
2437 req_set_fail_links(req);
2438 io_cqring_add_event(req, ret);
2439 io_put_req_find_next(req, &nxt);
2440 if (nxt)
2441 io_wq_assign_next(workptr, nxt);
2442}
2443
2444static int io_fallocate_prep(struct io_kiocb *req,
2445 const struct io_uring_sqe *sqe)
2446{
2447 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2448 return -EINVAL;
2449
2450 req->sync.off = READ_ONCE(sqe->off);
2451 req->sync.len = READ_ONCE(sqe->addr);
2452 req->sync.mode = READ_ONCE(sqe->len);
2453 return 0;
2454}
2455
2456static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2457 bool force_nonblock)
2458{
2459 struct io_wq_work *work, *old_work;
2460
2461 /* fallocate always requiring blocking context */
2462 if (force_nonblock) {
2463 io_put_req(req);
2464 req->work.func = io_fallocate_finish;
2465 return -EAGAIN;
2466 }
2467
2468 work = old_work = &req->work;
2469 io_fallocate_finish(&work);
2470 if (work && work != old_work)
2471 *nxt = container_of(work, struct io_kiocb, work);
2472
2473 return 0;
2474}
2475
Jens Axboe15b71ab2019-12-11 11:20:36 -07002476static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2477{
Jens Axboef8748882020-01-08 17:47:02 -07002478 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002479 int ret;
2480
2481 if (sqe->ioprio || sqe->buf_index)
2482 return -EINVAL;
2483
2484 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002485 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002486 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002487 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002488
Jens Axboef8748882020-01-08 17:47:02 -07002489 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002490 if (IS_ERR(req->open.filename)) {
2491 ret = PTR_ERR(req->open.filename);
2492 req->open.filename = NULL;
2493 return ret;
2494 }
2495
2496 return 0;
2497}
2498
Jens Axboecebdb982020-01-08 17:59:24 -07002499static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2500{
2501 struct open_how __user *how;
2502 const char __user *fname;
2503 size_t len;
2504 int ret;
2505
2506 if (sqe->ioprio || sqe->buf_index)
2507 return -EINVAL;
2508
2509 req->open.dfd = READ_ONCE(sqe->fd);
2510 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2511 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2512 len = READ_ONCE(sqe->len);
2513
2514 if (len < OPEN_HOW_SIZE_VER0)
2515 return -EINVAL;
2516
2517 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2518 len);
2519 if (ret)
2520 return ret;
2521
2522 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2523 req->open.how.flags |= O_LARGEFILE;
2524
2525 req->open.filename = getname(fname);
2526 if (IS_ERR(req->open.filename)) {
2527 ret = PTR_ERR(req->open.filename);
2528 req->open.filename = NULL;
2529 return ret;
2530 }
2531
2532 return 0;
2533}
2534
2535static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2536 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002537{
2538 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002539 struct file *file;
2540 int ret;
2541
2542 if (force_nonblock) {
2543 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2544 return -EAGAIN;
2545 }
2546
Jens Axboecebdb982020-01-08 17:59:24 -07002547 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002548 if (ret)
2549 goto err;
2550
Jens Axboecebdb982020-01-08 17:59:24 -07002551 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002552 if (ret < 0)
2553 goto err;
2554
2555 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2556 if (IS_ERR(file)) {
2557 put_unused_fd(ret);
2558 ret = PTR_ERR(file);
2559 } else {
2560 fsnotify_open(file);
2561 fd_install(ret, file);
2562 }
2563err:
2564 putname(req->open.filename);
2565 if (ret < 0)
2566 req_set_fail_links(req);
2567 io_cqring_add_event(req, ret);
2568 io_put_req_find_next(req, nxt);
2569 return 0;
2570}
2571
Jens Axboecebdb982020-01-08 17:59:24 -07002572static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2573 bool force_nonblock)
2574{
2575 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2576 return io_openat2(req, nxt, force_nonblock);
2577}
2578
Jens Axboec1ca7572019-12-25 22:18:28 -07002579static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2580{
2581#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2582 if (sqe->ioprio || sqe->buf_index || sqe->off)
2583 return -EINVAL;
2584
2585 req->madvise.addr = READ_ONCE(sqe->addr);
2586 req->madvise.len = READ_ONCE(sqe->len);
2587 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2588 return 0;
2589#else
2590 return -EOPNOTSUPP;
2591#endif
2592}
2593
2594static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2595 bool force_nonblock)
2596{
2597#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2598 struct io_madvise *ma = &req->madvise;
2599 int ret;
2600
2601 if (force_nonblock)
2602 return -EAGAIN;
2603
2604 ret = do_madvise(ma->addr, ma->len, ma->advice);
2605 if (ret < 0)
2606 req_set_fail_links(req);
2607 io_cqring_add_event(req, ret);
2608 io_put_req_find_next(req, nxt);
2609 return 0;
2610#else
2611 return -EOPNOTSUPP;
2612#endif
2613}
2614
Jens Axboe4840e412019-12-25 22:03:45 -07002615static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2616{
2617 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2618 return -EINVAL;
2619
2620 req->fadvise.offset = READ_ONCE(sqe->off);
2621 req->fadvise.len = READ_ONCE(sqe->len);
2622 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2623 return 0;
2624}
2625
2626static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2627 bool force_nonblock)
2628{
2629 struct io_fadvise *fa = &req->fadvise;
2630 int ret;
2631
2632 /* DONTNEED may block, others _should_ not */
2633 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2634 return -EAGAIN;
2635
2636 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2637 if (ret < 0)
2638 req_set_fail_links(req);
2639 io_cqring_add_event(req, ret);
2640 io_put_req_find_next(req, nxt);
2641 return 0;
2642}
2643
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002644static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2645{
Jens Axboef8748882020-01-08 17:47:02 -07002646 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002647 unsigned lookup_flags;
2648 int ret;
2649
2650 if (sqe->ioprio || sqe->buf_index)
2651 return -EINVAL;
2652
2653 req->open.dfd = READ_ONCE(sqe->fd);
2654 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002655 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002656 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002657 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002658
Jens Axboec12cedf2020-01-08 17:41:21 -07002659 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002660 return -EINVAL;
2661
Jens Axboef8748882020-01-08 17:47:02 -07002662 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002663 if (IS_ERR(req->open.filename)) {
2664 ret = PTR_ERR(req->open.filename);
2665 req->open.filename = NULL;
2666 return ret;
2667 }
2668
2669 return 0;
2670}
2671
2672static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2673 bool force_nonblock)
2674{
2675 struct io_open *ctx = &req->open;
2676 unsigned lookup_flags;
2677 struct path path;
2678 struct kstat stat;
2679 int ret;
2680
2681 if (force_nonblock)
2682 return -EAGAIN;
2683
Jens Axboec12cedf2020-01-08 17:41:21 -07002684 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002685 return -EINVAL;
2686
2687retry:
2688 /* filename_lookup() drops it, keep a reference */
2689 ctx->filename->refcnt++;
2690
2691 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2692 NULL);
2693 if (ret)
2694 goto err;
2695
Jens Axboec12cedf2020-01-08 17:41:21 -07002696 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002697 path_put(&path);
2698 if (retry_estale(ret, lookup_flags)) {
2699 lookup_flags |= LOOKUP_REVAL;
2700 goto retry;
2701 }
2702 if (!ret)
2703 ret = cp_statx(&stat, ctx->buffer);
2704err:
2705 putname(ctx->filename);
2706 if (ret < 0)
2707 req_set_fail_links(req);
2708 io_cqring_add_event(req, ret);
2709 io_put_req_find_next(req, nxt);
2710 return 0;
2711}
2712
Jens Axboeb5dba592019-12-11 14:02:38 -07002713static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2714{
2715 /*
2716 * If we queue this for async, it must not be cancellable. That would
2717 * leave the 'file' in an undeterminate state.
2718 */
2719 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2720
2721 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2722 sqe->rw_flags || sqe->buf_index)
2723 return -EINVAL;
2724 if (sqe->flags & IOSQE_FIXED_FILE)
2725 return -EINVAL;
2726
2727 req->close.fd = READ_ONCE(sqe->fd);
2728 if (req->file->f_op == &io_uring_fops ||
2729 req->close.fd == req->ring_fd)
2730 return -EBADF;
2731
2732 return 0;
2733}
2734
2735static void io_close_finish(struct io_wq_work **workptr)
2736{
2737 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2738 struct io_kiocb *nxt = NULL;
2739
2740 /* Invoked with files, we need to do the close */
2741 if (req->work.files) {
2742 int ret;
2743
2744 ret = filp_close(req->close.put_file, req->work.files);
2745 if (ret < 0) {
2746 req_set_fail_links(req);
2747 }
2748 io_cqring_add_event(req, ret);
2749 }
2750
2751 fput(req->close.put_file);
2752
2753 /* we bypassed the re-issue, drop the submission reference */
2754 io_put_req(req);
2755 io_put_req_find_next(req, &nxt);
2756 if (nxt)
2757 io_wq_assign_next(workptr, nxt);
2758}
2759
2760static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2761 bool force_nonblock)
2762{
2763 int ret;
2764
2765 req->close.put_file = NULL;
2766 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2767 if (ret < 0)
2768 return ret;
2769
2770 /* if the file has a flush method, be safe and punt to async */
2771 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2772 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2773 goto eagain;
2774 }
2775
2776 /*
2777 * No ->flush(), safely close from here and just punt the
2778 * fput() to async context.
2779 */
2780 ret = filp_close(req->close.put_file, current->files);
2781
2782 if (ret < 0)
2783 req_set_fail_links(req);
2784 io_cqring_add_event(req, ret);
2785
2786 if (io_wq_current_is_worker()) {
2787 struct io_wq_work *old_work, *work;
2788
2789 old_work = work = &req->work;
2790 io_close_finish(&work);
2791 if (work && work != old_work)
2792 *nxt = container_of(work, struct io_kiocb, work);
2793 return 0;
2794 }
2795
2796eagain:
2797 req->work.func = io_close_finish;
2798 return -EAGAIN;
2799}
2800
Jens Axboe3529d8c2019-12-19 18:24:38 -07002801static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002802{
2803 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002804
2805 if (!req->file)
2806 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002807
2808 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2809 return -EINVAL;
2810 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2811 return -EINVAL;
2812
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002813 req->sync.off = READ_ONCE(sqe->off);
2814 req->sync.len = READ_ONCE(sqe->len);
2815 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002816 return 0;
2817}
2818
2819static void io_sync_file_range_finish(struct io_wq_work **workptr)
2820{
2821 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2822 struct io_kiocb *nxt = NULL;
2823 int ret;
2824
2825 if (io_req_cancelled(req))
2826 return;
2827
Jens Axboe9adbd452019-12-20 08:45:55 -07002828 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002829 req->sync.flags);
2830 if (ret < 0)
2831 req_set_fail_links(req);
2832 io_cqring_add_event(req, ret);
2833 io_put_req_find_next(req, &nxt);
2834 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002835 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002836}
2837
Jens Axboefc4df992019-12-10 14:38:45 -07002838static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002839 bool force_nonblock)
2840{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002841 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002842
2843 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002844 if (force_nonblock) {
2845 io_put_req(req);
2846 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002847 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002848 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002849
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002850 work = old_work = &req->work;
2851 io_sync_file_range_finish(&work);
2852 if (work && work != old_work)
2853 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002854 return 0;
2855}
2856
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002857#if defined(CONFIG_NET)
2858static void io_sendrecv_async(struct io_wq_work **workptr)
2859{
2860 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2861 struct iovec *iov = NULL;
2862
2863 if (req->io->rw.iov != req->io->rw.fast_iov)
2864 iov = req->io->msg.iov;
2865 io_wq_submit_work(workptr);
2866 kfree(iov);
2867}
2868#endif
2869
Jens Axboe3529d8c2019-12-19 18:24:38 -07002870static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002871{
Jens Axboe03b12302019-12-02 18:50:25 -07002872#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002873 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002874 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002875
Jens Axboee47293f2019-12-20 08:58:21 -07002876 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2877 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002878 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002879
Jens Axboefddafac2020-01-04 20:19:44 -07002880 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002881 return 0;
2882
Jens Axboed9688562019-12-09 19:35:20 -07002883 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002884 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002885 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002886#else
Jens Axboee47293f2019-12-20 08:58:21 -07002887 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002888#endif
2889}
2890
Jens Axboefc4df992019-12-10 14:38:45 -07002891static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2892 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002893{
2894#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002895 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002896 struct socket *sock;
2897 int ret;
2898
2899 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2900 return -EINVAL;
2901
2902 sock = sock_from_file(req->file, &ret);
2903 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002904 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002905 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002906 unsigned flags;
2907
Jens Axboe03b12302019-12-02 18:50:25 -07002908 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002909 kmsg = &req->io->msg;
2910 kmsg->msg.msg_name = &addr;
2911 /* if iov is set, it's allocated already */
2912 if (!kmsg->iov)
2913 kmsg->iov = kmsg->fast_iov;
2914 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002915 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002916 struct io_sr_msg *sr = &req->sr_msg;
2917
Jens Axboe0b416c32019-12-15 10:57:46 -07002918 kmsg = &io.msg;
2919 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002920
2921 io.msg.iov = io.msg.fast_iov;
2922 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2923 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002924 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002925 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002926 }
2927
Jens Axboee47293f2019-12-20 08:58:21 -07002928 flags = req->sr_msg.msg_flags;
2929 if (flags & MSG_DONTWAIT)
2930 req->flags |= REQ_F_NOWAIT;
2931 else if (force_nonblock)
2932 flags |= MSG_DONTWAIT;
2933
Jens Axboe0b416c32019-12-15 10:57:46 -07002934 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002935 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002936 if (req->io)
2937 return -EAGAIN;
2938 if (io_alloc_async_ctx(req))
2939 return -ENOMEM;
2940 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2941 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002942 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002943 }
2944 if (ret == -ERESTARTSYS)
2945 ret = -EINTR;
2946 }
2947
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002948 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002949 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002950 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002951 if (ret < 0)
2952 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002953 io_put_req_find_next(req, nxt);
2954 return 0;
2955#else
2956 return -EOPNOTSUPP;
2957#endif
2958}
2959
Jens Axboefddafac2020-01-04 20:19:44 -07002960static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2961 bool force_nonblock)
2962{
2963#if defined(CONFIG_NET)
2964 struct socket *sock;
2965 int ret;
2966
2967 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2968 return -EINVAL;
2969
2970 sock = sock_from_file(req->file, &ret);
2971 if (sock) {
2972 struct io_sr_msg *sr = &req->sr_msg;
2973 struct msghdr msg;
2974 struct iovec iov;
2975 unsigned flags;
2976
2977 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
2978 &msg.msg_iter);
2979 if (ret)
2980 return ret;
2981
2982 msg.msg_name = NULL;
2983 msg.msg_control = NULL;
2984 msg.msg_controllen = 0;
2985 msg.msg_namelen = 0;
2986
2987 flags = req->sr_msg.msg_flags;
2988 if (flags & MSG_DONTWAIT)
2989 req->flags |= REQ_F_NOWAIT;
2990 else if (force_nonblock)
2991 flags |= MSG_DONTWAIT;
2992
2993 ret = __sys_sendmsg_sock(sock, &msg, flags);
2994 if (force_nonblock && ret == -EAGAIN)
2995 return -EAGAIN;
2996 if (ret == -ERESTARTSYS)
2997 ret = -EINTR;
2998 }
2999
3000 io_cqring_add_event(req, ret);
3001 if (ret < 0)
3002 req_set_fail_links(req);
3003 io_put_req_find_next(req, nxt);
3004 return 0;
3005#else
3006 return -EOPNOTSUPP;
3007#endif
3008}
3009
Jens Axboe3529d8c2019-12-19 18:24:38 -07003010static int io_recvmsg_prep(struct io_kiocb *req,
3011 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003012{
3013#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003014 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003015 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07003016
Jens Axboe3529d8c2019-12-19 18:24:38 -07003017 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3018 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
3019
Jens Axboefddafac2020-01-04 20:19:44 -07003020 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003021 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003022
Jens Axboed9688562019-12-09 19:35:20 -07003023 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003024 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003025 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003026#else
Jens Axboee47293f2019-12-20 08:58:21 -07003027 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003028#endif
3029}
3030
Jens Axboefc4df992019-12-10 14:38:45 -07003031static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3032 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003033{
3034#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003035 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003036 struct socket *sock;
3037 int ret;
3038
3039 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3040 return -EINVAL;
3041
3042 sock = sock_from_file(req->file, &ret);
3043 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003044 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003045 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003046 unsigned flags;
3047
Jens Axboe03b12302019-12-02 18:50:25 -07003048 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003049 kmsg = &req->io->msg;
3050 kmsg->msg.msg_name = &addr;
3051 /* if iov is set, it's allocated already */
3052 if (!kmsg->iov)
3053 kmsg->iov = kmsg->fast_iov;
3054 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003055 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003056 struct io_sr_msg *sr = &req->sr_msg;
3057
Jens Axboe0b416c32019-12-15 10:57:46 -07003058 kmsg = &io.msg;
3059 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003060
3061 io.msg.iov = io.msg.fast_iov;
3062 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3063 sr->msg_flags, &io.msg.uaddr,
3064 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003065 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003066 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003067 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003068
Jens Axboee47293f2019-12-20 08:58:21 -07003069 flags = req->sr_msg.msg_flags;
3070 if (flags & MSG_DONTWAIT)
3071 req->flags |= REQ_F_NOWAIT;
3072 else if (force_nonblock)
3073 flags |= MSG_DONTWAIT;
3074
3075 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3076 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003077 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003078 if (req->io)
3079 return -EAGAIN;
3080 if (io_alloc_async_ctx(req))
3081 return -ENOMEM;
3082 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3083 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003084 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003085 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003086 if (ret == -ERESTARTSYS)
3087 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003088 }
3089
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003090 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003091 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003092 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003093 if (ret < 0)
3094 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003095 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003096 return 0;
3097#else
3098 return -EOPNOTSUPP;
3099#endif
3100}
3101
Jens Axboefddafac2020-01-04 20:19:44 -07003102static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3103 bool force_nonblock)
3104{
3105#if defined(CONFIG_NET)
3106 struct socket *sock;
3107 int ret;
3108
3109 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3110 return -EINVAL;
3111
3112 sock = sock_from_file(req->file, &ret);
3113 if (sock) {
3114 struct io_sr_msg *sr = &req->sr_msg;
3115 struct msghdr msg;
3116 struct iovec iov;
3117 unsigned flags;
3118
3119 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3120 &msg.msg_iter);
3121 if (ret)
3122 return ret;
3123
3124 msg.msg_name = NULL;
3125 msg.msg_control = NULL;
3126 msg.msg_controllen = 0;
3127 msg.msg_namelen = 0;
3128 msg.msg_iocb = NULL;
3129 msg.msg_flags = 0;
3130
3131 flags = req->sr_msg.msg_flags;
3132 if (flags & MSG_DONTWAIT)
3133 req->flags |= REQ_F_NOWAIT;
3134 else if (force_nonblock)
3135 flags |= MSG_DONTWAIT;
3136
3137 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3138 if (force_nonblock && ret == -EAGAIN)
3139 return -EAGAIN;
3140 if (ret == -ERESTARTSYS)
3141 ret = -EINTR;
3142 }
3143
3144 io_cqring_add_event(req, ret);
3145 if (ret < 0)
3146 req_set_fail_links(req);
3147 io_put_req_find_next(req, nxt);
3148 return 0;
3149#else
3150 return -EOPNOTSUPP;
3151#endif
3152}
3153
3154
Jens Axboe3529d8c2019-12-19 18:24:38 -07003155static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003156{
3157#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003158 struct io_accept *accept = &req->accept;
3159
Jens Axboe17f2fe32019-10-17 14:42:58 -06003160 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3161 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003162 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003163 return -EINVAL;
3164
Jens Axboed55e5f52019-12-11 16:12:15 -07003165 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3166 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003167 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003168 return 0;
3169#else
3170 return -EOPNOTSUPP;
3171#endif
3172}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003173
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003174#if defined(CONFIG_NET)
3175static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3176 bool force_nonblock)
3177{
3178 struct io_accept *accept = &req->accept;
3179 unsigned file_flags;
3180 int ret;
3181
3182 file_flags = force_nonblock ? O_NONBLOCK : 0;
3183 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3184 accept->addr_len, accept->flags);
3185 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003186 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003187 if (ret == -ERESTARTSYS)
3188 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003189 if (ret < 0)
3190 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003191 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003192 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003193 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003194}
3195
3196static void io_accept_finish(struct io_wq_work **workptr)
3197{
3198 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3199 struct io_kiocb *nxt = NULL;
3200
3201 if (io_req_cancelled(req))
3202 return;
3203 __io_accept(req, &nxt, false);
3204 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003205 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003206}
3207#endif
3208
3209static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3210 bool force_nonblock)
3211{
3212#if defined(CONFIG_NET)
3213 int ret;
3214
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003215 ret = __io_accept(req, nxt, force_nonblock);
3216 if (ret == -EAGAIN && force_nonblock) {
3217 req->work.func = io_accept_finish;
3218 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3219 io_put_req(req);
3220 return -EAGAIN;
3221 }
3222 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003223#else
3224 return -EOPNOTSUPP;
3225#endif
3226}
3227
Jens Axboe3529d8c2019-12-19 18:24:38 -07003228static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003229{
3230#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003231 struct io_connect *conn = &req->connect;
3232 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003233
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003234 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3235 return -EINVAL;
3236 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3237 return -EINVAL;
3238
Jens Axboe3529d8c2019-12-19 18:24:38 -07003239 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3240 conn->addr_len = READ_ONCE(sqe->addr2);
3241
3242 if (!io)
3243 return 0;
3244
3245 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003246 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003247#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003248 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003249#endif
3250}
3251
Jens Axboefc4df992019-12-10 14:38:45 -07003252static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3253 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003254{
3255#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003256 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003257 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003258 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003259
Jens Axboef499a022019-12-02 16:28:46 -07003260 if (req->io) {
3261 io = req->io;
3262 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003263 ret = move_addr_to_kernel(req->connect.addr,
3264 req->connect.addr_len,
3265 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003266 if (ret)
3267 goto out;
3268 io = &__io;
3269 }
3270
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003271 file_flags = force_nonblock ? O_NONBLOCK : 0;
3272
3273 ret = __sys_connect_file(req->file, &io->connect.address,
3274 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003275 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003276 if (req->io)
3277 return -EAGAIN;
3278 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003279 ret = -ENOMEM;
3280 goto out;
3281 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003282 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003283 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003284 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003285 if (ret == -ERESTARTSYS)
3286 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003287out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003288 if (ret < 0)
3289 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003290 io_cqring_add_event(req, ret);
3291 io_put_req_find_next(req, nxt);
3292 return 0;
3293#else
3294 return -EOPNOTSUPP;
3295#endif
3296}
3297
Jens Axboe221c5eb2019-01-17 09:41:58 -07003298static void io_poll_remove_one(struct io_kiocb *req)
3299{
3300 struct io_poll_iocb *poll = &req->poll;
3301
3302 spin_lock(&poll->head->lock);
3303 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003304 if (!list_empty(&poll->wait.entry)) {
3305 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003306 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003307 }
3308 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003309 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003310}
3311
3312static void io_poll_remove_all(struct io_ring_ctx *ctx)
3313{
Jens Axboe78076bb2019-12-04 19:56:40 -07003314 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003315 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003316 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003317
3318 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003319 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3320 struct hlist_head *list;
3321
3322 list = &ctx->cancel_hash[i];
3323 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3324 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003325 }
3326 spin_unlock_irq(&ctx->completion_lock);
3327}
3328
Jens Axboe47f46762019-11-09 17:43:02 -07003329static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3330{
Jens Axboe78076bb2019-12-04 19:56:40 -07003331 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003332 struct io_kiocb *req;
3333
Jens Axboe78076bb2019-12-04 19:56:40 -07003334 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3335 hlist_for_each_entry(req, list, hash_node) {
3336 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003337 io_poll_remove_one(req);
3338 return 0;
3339 }
Jens Axboe47f46762019-11-09 17:43:02 -07003340 }
3341
3342 return -ENOENT;
3343}
3344
Jens Axboe3529d8c2019-12-19 18:24:38 -07003345static int io_poll_remove_prep(struct io_kiocb *req,
3346 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003347{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003348 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3349 return -EINVAL;
3350 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3351 sqe->poll_events)
3352 return -EINVAL;
3353
Jens Axboe0969e782019-12-17 18:40:57 -07003354 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003355 return 0;
3356}
3357
3358/*
3359 * Find a running poll command that matches one specified in sqe->addr,
3360 * and remove it if found.
3361 */
3362static int io_poll_remove(struct io_kiocb *req)
3363{
3364 struct io_ring_ctx *ctx = req->ctx;
3365 u64 addr;
3366 int ret;
3367
Jens Axboe0969e782019-12-17 18:40:57 -07003368 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003369 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003370 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003371 spin_unlock_irq(&ctx->completion_lock);
3372
Jens Axboe78e19bb2019-11-06 15:21:34 -07003373 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003374 if (ret < 0)
3375 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003376 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003377 return 0;
3378}
3379
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003380static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003381{
Jackie Liua197f662019-11-08 08:09:12 -07003382 struct io_ring_ctx *ctx = req->ctx;
3383
Jens Axboe8c838782019-03-12 15:48:16 -06003384 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003385 if (error)
3386 io_cqring_fill_event(req, error);
3387 else
3388 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003389 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003390}
3391
Jens Axboe561fb042019-10-24 07:25:42 -06003392static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003393{
Jens Axboe561fb042019-10-24 07:25:42 -06003394 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003395 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3396 struct io_poll_iocb *poll = &req->poll;
3397 struct poll_table_struct pt = { ._key = poll->events };
3398 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003399 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003400 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003401 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003402
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003403 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003404 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003405 ret = -ECANCELED;
3406 } else if (READ_ONCE(poll->canceled)) {
3407 ret = -ECANCELED;
3408 }
Jens Axboe561fb042019-10-24 07:25:42 -06003409
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003410 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003411 mask = vfs_poll(poll->file, &pt) & poll->events;
3412
3413 /*
3414 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3415 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3416 * synchronize with them. In the cancellation case the list_del_init
3417 * itself is not actually needed, but harmless so we keep it in to
3418 * avoid further branches in the fast path.
3419 */
3420 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003421 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003422 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003423 spin_unlock_irq(&ctx->completion_lock);
3424 return;
3425 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003426 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003427 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003428 spin_unlock_irq(&ctx->completion_lock);
3429
Jens Axboe8c838782019-03-12 15:48:16 -06003430 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003431
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003432 if (ret < 0)
3433 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003434 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003435 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003436 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003437}
3438
Jens Axboee94f1412019-12-19 12:06:02 -07003439static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3440{
Jens Axboee94f1412019-12-19 12:06:02 -07003441 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003442 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003443
Jens Axboec6ca97b302019-12-28 12:11:08 -07003444 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003445 spin_lock_irq(&ctx->completion_lock);
3446 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3447 hash_del(&req->hash_node);
3448 io_poll_complete(req, req->result, 0);
3449
Jens Axboe8237e042019-12-28 10:48:22 -07003450 if (refcount_dec_and_test(&req->refs) &&
3451 !io_req_multi_free(&rb, req)) {
3452 req->flags |= REQ_F_COMP_LOCKED;
3453 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003454 }
3455 }
3456 spin_unlock_irq(&ctx->completion_lock);
3457
3458 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003459 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003460}
3461
3462static void io_poll_flush(struct io_wq_work **workptr)
3463{
3464 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3465 struct llist_node *nodes;
3466
3467 nodes = llist_del_all(&req->ctx->poll_llist);
3468 if (nodes)
3469 __io_poll_flush(req->ctx, nodes);
3470}
3471
Jens Axboe221c5eb2019-01-17 09:41:58 -07003472static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3473 void *key)
3474{
Jens Axboee9444752019-11-26 15:02:04 -07003475 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003476 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3477 struct io_ring_ctx *ctx = req->ctx;
3478 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003479
3480 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003481 if (mask && !(mask & poll->events))
3482 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003483
Jens Axboe392edb42019-12-09 17:52:20 -07003484 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003485
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003486 /*
3487 * Run completion inline if we can. We're using trylock here because
3488 * we are violating the completion_lock -> poll wq lock ordering.
3489 * If we have a link timeout we're going to need the completion_lock
3490 * for finalizing the request, mark us as having grabbed that already.
3491 */
Jens Axboee94f1412019-12-19 12:06:02 -07003492 if (mask) {
3493 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003494
Jens Axboee94f1412019-12-19 12:06:02 -07003495 if (llist_empty(&ctx->poll_llist) &&
3496 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3497 hash_del(&req->hash_node);
3498 io_poll_complete(req, mask, 0);
3499 req->flags |= REQ_F_COMP_LOCKED;
3500 io_put_req(req);
3501 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3502
3503 io_cqring_ev_posted(ctx);
3504 req = NULL;
3505 } else {
3506 req->result = mask;
3507 req->llist_node.next = NULL;
3508 /* if the list wasn't empty, we're done */
3509 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3510 req = NULL;
3511 else
3512 req->work.func = io_poll_flush;
3513 }
Jens Axboe8c838782019-03-12 15:48:16 -06003514 }
Jens Axboee94f1412019-12-19 12:06:02 -07003515 if (req)
3516 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003517
Jens Axboe221c5eb2019-01-17 09:41:58 -07003518 return 1;
3519}
3520
3521struct io_poll_table {
3522 struct poll_table_struct pt;
3523 struct io_kiocb *req;
3524 int error;
3525};
3526
3527static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3528 struct poll_table_struct *p)
3529{
3530 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3531
3532 if (unlikely(pt->req->poll.head)) {
3533 pt->error = -EINVAL;
3534 return;
3535 }
3536
3537 pt->error = 0;
3538 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003539 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003540}
3541
Jens Axboeeac406c2019-11-14 12:09:58 -07003542static void io_poll_req_insert(struct io_kiocb *req)
3543{
3544 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003545 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003546
Jens Axboe78076bb2019-12-04 19:56:40 -07003547 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3548 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003549}
3550
Jens Axboe3529d8c2019-12-19 18:24:38 -07003551static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003552{
3553 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003554 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003555
3556 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3557 return -EINVAL;
3558 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3559 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003560 if (!poll->file)
3561 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003562
Jens Axboe221c5eb2019-01-17 09:41:58 -07003563 events = READ_ONCE(sqe->poll_events);
3564 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003565 return 0;
3566}
3567
3568static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3569{
3570 struct io_poll_iocb *poll = &req->poll;
3571 struct io_ring_ctx *ctx = req->ctx;
3572 struct io_poll_table ipt;
3573 bool cancel = false;
3574 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003575
3576 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003577 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003578
Jens Axboe221c5eb2019-01-17 09:41:58 -07003579 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003580 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003581 poll->canceled = false;
3582
3583 ipt.pt._qproc = io_poll_queue_proc;
3584 ipt.pt._key = poll->events;
3585 ipt.req = req;
3586 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3587
3588 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003589 INIT_LIST_HEAD(&poll->wait.entry);
3590 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3591 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003592
Jens Axboe36703242019-07-25 10:20:18 -06003593 INIT_LIST_HEAD(&req->list);
3594
Jens Axboe221c5eb2019-01-17 09:41:58 -07003595 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003596
3597 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003598 if (likely(poll->head)) {
3599 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003600 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003601 if (ipt.error)
3602 cancel = true;
3603 ipt.error = 0;
3604 mask = 0;
3605 }
3606 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003607 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003608 else if (cancel)
3609 WRITE_ONCE(poll->canceled, true);
3610 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003611 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003612 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003613 }
Jens Axboe8c838782019-03-12 15:48:16 -06003614 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003615 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003616 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003617 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003618 spin_unlock_irq(&ctx->completion_lock);
3619
Jens Axboe8c838782019-03-12 15:48:16 -06003620 if (mask) {
3621 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003622 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003623 }
Jens Axboe8c838782019-03-12 15:48:16 -06003624 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003625}
3626
Jens Axboe5262f562019-09-17 12:26:57 -06003627static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3628{
Jens Axboead8a48a2019-11-15 08:49:11 -07003629 struct io_timeout_data *data = container_of(timer,
3630 struct io_timeout_data, timer);
3631 struct io_kiocb *req = data->req;
3632 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003633 unsigned long flags;
3634
Jens Axboe5262f562019-09-17 12:26:57 -06003635 atomic_inc(&ctx->cq_timeouts);
3636
3637 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003638 /*
Jens Axboe11365042019-10-16 09:08:32 -06003639 * We could be racing with timeout deletion. If the list is empty,
3640 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003641 */
Jens Axboe842f9612019-10-29 12:34:10 -06003642 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003643 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003644
Jens Axboe11365042019-10-16 09:08:32 -06003645 /*
3646 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003647 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003648 * pointer will be increased, otherwise other timeout reqs may
3649 * return in advance without waiting for enough wait_nr.
3650 */
3651 prev = req;
3652 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3653 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003654 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003655 }
Jens Axboe842f9612019-10-29 12:34:10 -06003656
Jens Axboe78e19bb2019-11-06 15:21:34 -07003657 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003658 io_commit_cqring(ctx);
3659 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3660
3661 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003662 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003663 io_put_req(req);
3664 return HRTIMER_NORESTART;
3665}
3666
Jens Axboe47f46762019-11-09 17:43:02 -07003667static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3668{
3669 struct io_kiocb *req;
3670 int ret = -ENOENT;
3671
3672 list_for_each_entry(req, &ctx->timeout_list, list) {
3673 if (user_data == req->user_data) {
3674 list_del_init(&req->list);
3675 ret = 0;
3676 break;
3677 }
3678 }
3679
3680 if (ret == -ENOENT)
3681 return ret;
3682
Jens Axboe2d283902019-12-04 11:08:05 -07003683 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003684 if (ret == -1)
3685 return -EALREADY;
3686
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003687 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003688 io_cqring_fill_event(req, -ECANCELED);
3689 io_put_req(req);
3690 return 0;
3691}
3692
Jens Axboe3529d8c2019-12-19 18:24:38 -07003693static int io_timeout_remove_prep(struct io_kiocb *req,
3694 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003695{
Jens Axboeb29472e2019-12-17 18:50:29 -07003696 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3697 return -EINVAL;
3698 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3699 return -EINVAL;
3700
3701 req->timeout.addr = READ_ONCE(sqe->addr);
3702 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3703 if (req->timeout.flags)
3704 return -EINVAL;
3705
Jens Axboeb29472e2019-12-17 18:50:29 -07003706 return 0;
3707}
3708
Jens Axboe11365042019-10-16 09:08:32 -06003709/*
3710 * Remove or update an existing timeout command
3711 */
Jens Axboefc4df992019-12-10 14:38:45 -07003712static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003713{
3714 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003715 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003716
Jens Axboe11365042019-10-16 09:08:32 -06003717 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003718 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003719
Jens Axboe47f46762019-11-09 17:43:02 -07003720 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003721 io_commit_cqring(ctx);
3722 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003723 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003724 if (ret < 0)
3725 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003726 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003727 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003728}
3729
Jens Axboe3529d8c2019-12-19 18:24:38 -07003730static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003731 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003732{
Jens Axboead8a48a2019-11-15 08:49:11 -07003733 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003734 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003735
Jens Axboead8a48a2019-11-15 08:49:11 -07003736 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003737 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003738 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003739 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003740 if (sqe->off && is_timeout_link)
3741 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003742 flags = READ_ONCE(sqe->timeout_flags);
3743 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003744 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003745
Jens Axboe26a61672019-12-20 09:02:01 -07003746 req->timeout.count = READ_ONCE(sqe->off);
3747
Jens Axboe3529d8c2019-12-19 18:24:38 -07003748 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003749 return -ENOMEM;
3750
3751 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003752 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003753 req->flags |= REQ_F_TIMEOUT;
3754
3755 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003756 return -EFAULT;
3757
Jens Axboe11365042019-10-16 09:08:32 -06003758 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003759 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003760 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003761 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003762
Jens Axboead8a48a2019-11-15 08:49:11 -07003763 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3764 return 0;
3765}
3766
Jens Axboefc4df992019-12-10 14:38:45 -07003767static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003768{
3769 unsigned count;
3770 struct io_ring_ctx *ctx = req->ctx;
3771 struct io_timeout_data *data;
3772 struct list_head *entry;
3773 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003774
Jens Axboe2d283902019-12-04 11:08:05 -07003775 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003776
Jens Axboe5262f562019-09-17 12:26:57 -06003777 /*
3778 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003779 * timeout event to be satisfied. If it isn't set, then this is
3780 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003781 */
Jens Axboe26a61672019-12-20 09:02:01 -07003782 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003783 if (!count) {
3784 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3785 spin_lock_irq(&ctx->completion_lock);
3786 entry = ctx->timeout_list.prev;
3787 goto add;
3788 }
Jens Axboe5262f562019-09-17 12:26:57 -06003789
3790 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003791 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003792
3793 /*
3794 * Insertion sort, ensuring the first entry in the list is always
3795 * the one we need first.
3796 */
Jens Axboe5262f562019-09-17 12:26:57 -06003797 spin_lock_irq(&ctx->completion_lock);
3798 list_for_each_prev(entry, &ctx->timeout_list) {
3799 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003800 unsigned nxt_sq_head;
3801 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003802 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003803
Jens Axboe93bd25b2019-11-11 23:34:31 -07003804 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3805 continue;
3806
yangerkun5da0fb12019-10-15 21:59:29 +08003807 /*
3808 * Since cached_sq_head + count - 1 can overflow, use type long
3809 * long to store it.
3810 */
3811 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003812 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3813 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003814
3815 /*
3816 * cached_sq_head may overflow, and it will never overflow twice
3817 * once there is some timeout req still be valid.
3818 */
3819 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003820 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003821
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003822 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003823 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003824
3825 /*
3826 * Sequence of reqs after the insert one and itself should
3827 * be adjusted because each timeout req consumes a slot.
3828 */
3829 span++;
3830 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003831 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003832 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003833add:
Jens Axboe5262f562019-09-17 12:26:57 -06003834 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003835 data->timer.function = io_timeout_fn;
3836 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003837 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003838 return 0;
3839}
3840
Jens Axboe62755e32019-10-28 21:49:21 -06003841static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003842{
Jens Axboe62755e32019-10-28 21:49:21 -06003843 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003844
Jens Axboe62755e32019-10-28 21:49:21 -06003845 return req->user_data == (unsigned long) data;
3846}
3847
Jens Axboee977d6d2019-11-05 12:39:45 -07003848static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003849{
Jens Axboe62755e32019-10-28 21:49:21 -06003850 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003851 int ret = 0;
3852
Jens Axboe62755e32019-10-28 21:49:21 -06003853 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3854 switch (cancel_ret) {
3855 case IO_WQ_CANCEL_OK:
3856 ret = 0;
3857 break;
3858 case IO_WQ_CANCEL_RUNNING:
3859 ret = -EALREADY;
3860 break;
3861 case IO_WQ_CANCEL_NOTFOUND:
3862 ret = -ENOENT;
3863 break;
3864 }
3865
Jens Axboee977d6d2019-11-05 12:39:45 -07003866 return ret;
3867}
3868
Jens Axboe47f46762019-11-09 17:43:02 -07003869static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3870 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003871 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003872{
3873 unsigned long flags;
3874 int ret;
3875
3876 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3877 if (ret != -ENOENT) {
3878 spin_lock_irqsave(&ctx->completion_lock, flags);
3879 goto done;
3880 }
3881
3882 spin_lock_irqsave(&ctx->completion_lock, flags);
3883 ret = io_timeout_cancel(ctx, sqe_addr);
3884 if (ret != -ENOENT)
3885 goto done;
3886 ret = io_poll_cancel(ctx, sqe_addr);
3887done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003888 if (!ret)
3889 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003890 io_cqring_fill_event(req, ret);
3891 io_commit_cqring(ctx);
3892 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3893 io_cqring_ev_posted(ctx);
3894
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003895 if (ret < 0)
3896 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003897 io_put_req_find_next(req, nxt);
3898}
3899
Jens Axboe3529d8c2019-12-19 18:24:38 -07003900static int io_async_cancel_prep(struct io_kiocb *req,
3901 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003902{
Jens Axboefbf23842019-12-17 18:45:56 -07003903 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003904 return -EINVAL;
3905 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3906 sqe->cancel_flags)
3907 return -EINVAL;
3908
Jens Axboefbf23842019-12-17 18:45:56 -07003909 req->cancel.addr = READ_ONCE(sqe->addr);
3910 return 0;
3911}
3912
3913static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3914{
3915 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003916
3917 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003918 return 0;
3919}
3920
Jens Axboe05f3fb32019-12-09 11:22:50 -07003921static int io_files_update_prep(struct io_kiocb *req,
3922 const struct io_uring_sqe *sqe)
3923{
3924 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3925 return -EINVAL;
3926
3927 req->files_update.offset = READ_ONCE(sqe->off);
3928 req->files_update.nr_args = READ_ONCE(sqe->len);
3929 if (!req->files_update.nr_args)
3930 return -EINVAL;
3931 req->files_update.arg = READ_ONCE(sqe->addr);
3932 return 0;
3933}
3934
3935static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3936{
3937 struct io_ring_ctx *ctx = req->ctx;
3938 struct io_uring_files_update up;
3939 int ret;
3940
3941 if (force_nonblock) {
3942 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3943 return -EAGAIN;
3944 }
3945
3946 up.offset = req->files_update.offset;
3947 up.fds = req->files_update.arg;
3948
3949 mutex_lock(&ctx->uring_lock);
3950 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3951 mutex_unlock(&ctx->uring_lock);
3952
3953 if (ret < 0)
3954 req_set_fail_links(req);
3955 io_cqring_add_event(req, ret);
3956 io_put_req(req);
3957 return 0;
3958}
3959
Jens Axboe3529d8c2019-12-19 18:24:38 -07003960static int io_req_defer_prep(struct io_kiocb *req,
3961 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003962{
Jens Axboee7815732019-12-17 19:45:06 -07003963 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003964
Jens Axboed625c6e2019-12-17 19:53:05 -07003965 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003966 case IORING_OP_NOP:
3967 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003968 case IORING_OP_READV:
3969 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003970 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003971 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003972 break;
3973 case IORING_OP_WRITEV:
3974 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003975 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003976 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003977 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003978 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003979 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003980 break;
3981 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003982 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003983 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003984 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003985 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003986 break;
3987 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003988 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003989 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003990 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003991 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003992 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003993 break;
3994 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003995 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003996 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003997 break;
Jens Axboef499a022019-12-02 16:28:46 -07003998 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003999 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004000 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004001 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004002 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004003 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004004 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004005 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004006 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004007 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004008 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004009 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004010 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004011 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004012 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004013 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004014 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004015 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004016 case IORING_OP_FALLOCATE:
4017 ret = io_fallocate_prep(req, sqe);
4018 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004019 case IORING_OP_OPENAT:
4020 ret = io_openat_prep(req, sqe);
4021 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004022 case IORING_OP_CLOSE:
4023 ret = io_close_prep(req, sqe);
4024 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004025 case IORING_OP_FILES_UPDATE:
4026 ret = io_files_update_prep(req, sqe);
4027 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004028 case IORING_OP_STATX:
4029 ret = io_statx_prep(req, sqe);
4030 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004031 case IORING_OP_FADVISE:
4032 ret = io_fadvise_prep(req, sqe);
4033 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004034 case IORING_OP_MADVISE:
4035 ret = io_madvise_prep(req, sqe);
4036 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004037 case IORING_OP_OPENAT2:
4038 ret = io_openat2_prep(req, sqe);
4039 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004040 default:
Jens Axboee7815732019-12-17 19:45:06 -07004041 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4042 req->opcode);
4043 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004044 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004045 }
4046
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004047 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004048}
4049
Jens Axboe3529d8c2019-12-19 18:24:38 -07004050static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004051{
Jackie Liua197f662019-11-08 08:09:12 -07004052 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004053 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004054
Bob Liu9d858b22019-11-13 18:06:25 +08004055 /* Still need defer if there is pending req in defer list. */
4056 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004057 return 0;
4058
Jens Axboe3529d8c2019-12-19 18:24:38 -07004059 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004060 return -EAGAIN;
4061
Jens Axboe3529d8c2019-12-19 18:24:38 -07004062 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004063 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004064 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004065
Jens Axboede0617e2019-04-06 21:51:27 -06004066 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004067 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004068 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004069 return 0;
4070 }
4071
Jens Axboe915967f2019-11-21 09:01:20 -07004072 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004073 list_add_tail(&req->list, &ctx->defer_list);
4074 spin_unlock_irq(&ctx->completion_lock);
4075 return -EIOCBQUEUED;
4076}
4077
Jens Axboe3529d8c2019-12-19 18:24:38 -07004078static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4079 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004080{
Jackie Liua197f662019-11-08 08:09:12 -07004081 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004082 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004083
Jens Axboed625c6e2019-12-17 19:53:05 -07004084 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004085 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004086 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004087 break;
4088 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004089 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004090 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004091 if (sqe) {
4092 ret = io_read_prep(req, sqe, force_nonblock);
4093 if (ret < 0)
4094 break;
4095 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004096 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004097 break;
4098 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004099 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004100 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004101 if (sqe) {
4102 ret = io_write_prep(req, sqe, force_nonblock);
4103 if (ret < 0)
4104 break;
4105 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004106 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004107 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004108 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004109 if (sqe) {
4110 ret = io_prep_fsync(req, sqe);
4111 if (ret < 0)
4112 break;
4113 }
Jens Axboefc4df992019-12-10 14:38:45 -07004114 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004115 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004116 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004117 if (sqe) {
4118 ret = io_poll_add_prep(req, sqe);
4119 if (ret)
4120 break;
4121 }
Jens Axboefc4df992019-12-10 14:38:45 -07004122 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004123 break;
4124 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004125 if (sqe) {
4126 ret = io_poll_remove_prep(req, sqe);
4127 if (ret < 0)
4128 break;
4129 }
Jens Axboefc4df992019-12-10 14:38:45 -07004130 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004131 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004132 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004133 if (sqe) {
4134 ret = io_prep_sfr(req, sqe);
4135 if (ret < 0)
4136 break;
4137 }
Jens Axboefc4df992019-12-10 14:38:45 -07004138 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004139 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004140 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004141 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004142 if (sqe) {
4143 ret = io_sendmsg_prep(req, sqe);
4144 if (ret < 0)
4145 break;
4146 }
Jens Axboefddafac2020-01-04 20:19:44 -07004147 if (req->opcode == IORING_OP_SENDMSG)
4148 ret = io_sendmsg(req, nxt, force_nonblock);
4149 else
4150 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004151 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004152 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004153 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004154 if (sqe) {
4155 ret = io_recvmsg_prep(req, sqe);
4156 if (ret)
4157 break;
4158 }
Jens Axboefddafac2020-01-04 20:19:44 -07004159 if (req->opcode == IORING_OP_RECVMSG)
4160 ret = io_recvmsg(req, nxt, force_nonblock);
4161 else
4162 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004163 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004164 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004165 if (sqe) {
4166 ret = io_timeout_prep(req, sqe, false);
4167 if (ret)
4168 break;
4169 }
Jens Axboefc4df992019-12-10 14:38:45 -07004170 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004171 break;
Jens Axboe11365042019-10-16 09:08:32 -06004172 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004173 if (sqe) {
4174 ret = io_timeout_remove_prep(req, sqe);
4175 if (ret)
4176 break;
4177 }
Jens Axboefc4df992019-12-10 14:38:45 -07004178 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004179 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004180 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004181 if (sqe) {
4182 ret = io_accept_prep(req, sqe);
4183 if (ret)
4184 break;
4185 }
Jens Axboefc4df992019-12-10 14:38:45 -07004186 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004187 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004188 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004189 if (sqe) {
4190 ret = io_connect_prep(req, sqe);
4191 if (ret)
4192 break;
4193 }
Jens Axboefc4df992019-12-10 14:38:45 -07004194 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004195 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004196 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004197 if (sqe) {
4198 ret = io_async_cancel_prep(req, sqe);
4199 if (ret)
4200 break;
4201 }
Jens Axboefc4df992019-12-10 14:38:45 -07004202 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004203 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004204 case IORING_OP_FALLOCATE:
4205 if (sqe) {
4206 ret = io_fallocate_prep(req, sqe);
4207 if (ret)
4208 break;
4209 }
4210 ret = io_fallocate(req, nxt, force_nonblock);
4211 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004212 case IORING_OP_OPENAT:
4213 if (sqe) {
4214 ret = io_openat_prep(req, sqe);
4215 if (ret)
4216 break;
4217 }
4218 ret = io_openat(req, nxt, force_nonblock);
4219 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004220 case IORING_OP_CLOSE:
4221 if (sqe) {
4222 ret = io_close_prep(req, sqe);
4223 if (ret)
4224 break;
4225 }
4226 ret = io_close(req, nxt, force_nonblock);
4227 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004228 case IORING_OP_FILES_UPDATE:
4229 if (sqe) {
4230 ret = io_files_update_prep(req, sqe);
4231 if (ret)
4232 break;
4233 }
4234 ret = io_files_update(req, force_nonblock);
4235 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004236 case IORING_OP_STATX:
4237 if (sqe) {
4238 ret = io_statx_prep(req, sqe);
4239 if (ret)
4240 break;
4241 }
4242 ret = io_statx(req, nxt, force_nonblock);
4243 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004244 case IORING_OP_FADVISE:
4245 if (sqe) {
4246 ret = io_fadvise_prep(req, sqe);
4247 if (ret)
4248 break;
4249 }
4250 ret = io_fadvise(req, nxt, force_nonblock);
4251 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004252 case IORING_OP_MADVISE:
4253 if (sqe) {
4254 ret = io_madvise_prep(req, sqe);
4255 if (ret)
4256 break;
4257 }
4258 ret = io_madvise(req, nxt, force_nonblock);
4259 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004260 case IORING_OP_OPENAT2:
4261 if (sqe) {
4262 ret = io_openat2_prep(req, sqe);
4263 if (ret)
4264 break;
4265 }
4266 ret = io_openat2(req, nxt, force_nonblock);
4267 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004268 default:
4269 ret = -EINVAL;
4270 break;
4271 }
4272
Jens Axboedef596e2019-01-09 08:59:42 -07004273 if (ret)
4274 return ret;
4275
4276 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004277 const bool in_async = io_wq_current_is_worker();
4278
Jens Axboe9e645e112019-05-10 16:07:28 -06004279 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004280 return -EAGAIN;
4281
Jens Axboe11ba8202020-01-15 21:51:17 -07004282 /* workqueue context doesn't hold uring_lock, grab it now */
4283 if (in_async)
4284 mutex_lock(&ctx->uring_lock);
4285
Jens Axboedef596e2019-01-09 08:59:42 -07004286 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004287
4288 if (in_async)
4289 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004290 }
4291
4292 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004293}
4294
Jens Axboe561fb042019-10-24 07:25:42 -06004295static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004296{
Jens Axboe561fb042019-10-24 07:25:42 -06004297 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004298 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004299 struct io_kiocb *nxt = NULL;
4300 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004301
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004302 /* if NO_CANCEL is set, we must still run the work */
4303 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4304 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004305 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004306 }
Jens Axboe31b51512019-01-18 22:56:34 -07004307
Jens Axboe561fb042019-10-24 07:25:42 -06004308 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004309 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4310 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004311 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004312 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004313 /*
4314 * We can get EAGAIN for polled IO even though we're
4315 * forcing a sync submission from here, since we can't
4316 * wait for request slots on the block side.
4317 */
4318 if (ret != -EAGAIN)
4319 break;
4320 cond_resched();
4321 } while (1);
4322 }
Jens Axboe31b51512019-01-18 22:56:34 -07004323
Jens Axboe561fb042019-10-24 07:25:42 -06004324 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004325 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004326
Jens Axboe561fb042019-10-24 07:25:42 -06004327 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004328 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004329 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004330 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004331 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004332
Jens Axboe561fb042019-10-24 07:25:42 -06004333 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004334 if (!ret && nxt)
4335 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004336}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004337
Jens Axboe15b71ab2019-12-11 11:20:36 -07004338static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004339{
Jens Axboed3656342019-12-18 09:50:26 -07004340 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004341 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004342 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4343 return 0;
4344 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004345}
4346
Jens Axboe65e19f52019-10-26 07:20:21 -06004347static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4348 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004349{
Jens Axboe65e19f52019-10-26 07:20:21 -06004350 struct fixed_file_table *table;
4351
Jens Axboe05f3fb32019-12-09 11:22:50 -07004352 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4353 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004354}
4355
Jens Axboe3529d8c2019-12-19 18:24:38 -07004356static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4357 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004358{
Jackie Liua197f662019-11-08 08:09:12 -07004359 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004360 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004361 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004362
Jens Axboe3529d8c2019-12-19 18:24:38 -07004363 flags = READ_ONCE(sqe->flags);
4364 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004365
Jackie Liu4fe2c962019-09-09 20:50:40 +08004366 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06004367 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06004368
Jens Axboed3656342019-12-18 09:50:26 -07004369 if (!io_req_needs_file(req, fd))
4370 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004371
4372 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004373 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004374 (unsigned) fd >= ctx->nr_user_files))
4375 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004376 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004377 req->file = io_file_from_index(ctx, fd);
4378 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004379 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004380 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004381 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004382 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004383 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004384 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004385 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004386 req->file = io_file_get(state, fd);
4387 if (unlikely(!req->file))
4388 return -EBADF;
4389 }
4390
4391 return 0;
4392}
4393
Jackie Liua197f662019-11-08 08:09:12 -07004394static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004395{
Jens Axboefcb323c2019-10-24 12:39:47 -06004396 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004397 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004398
Jens Axboeb5dba592019-12-11 14:02:38 -07004399 if (!req->ring_file)
4400 return -EBADF;
4401
Jens Axboefcb323c2019-10-24 12:39:47 -06004402 rcu_read_lock();
4403 spin_lock_irq(&ctx->inflight_lock);
4404 /*
4405 * We use the f_ops->flush() handler to ensure that we can flush
4406 * out work accessing these files if the fd is closed. Check if
4407 * the fd has changed since we started down this path, and disallow
4408 * this operation if it has.
4409 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004410 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004411 list_add(&req->inflight_entry, &ctx->inflight_list);
4412 req->flags |= REQ_F_INFLIGHT;
4413 req->work.files = current->files;
4414 ret = 0;
4415 }
4416 spin_unlock_irq(&ctx->inflight_lock);
4417 rcu_read_unlock();
4418
4419 return ret;
4420}
4421
Jens Axboe2665abf2019-11-05 12:40:47 -07004422static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4423{
Jens Axboead8a48a2019-11-15 08:49:11 -07004424 struct io_timeout_data *data = container_of(timer,
4425 struct io_timeout_data, timer);
4426 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004427 struct io_ring_ctx *ctx = req->ctx;
4428 struct io_kiocb *prev = NULL;
4429 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004430
4431 spin_lock_irqsave(&ctx->completion_lock, flags);
4432
4433 /*
4434 * We don't expect the list to be empty, that will only happen if we
4435 * race with the completion of the linked work.
4436 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004437 if (!list_empty(&req->link_list)) {
4438 prev = list_entry(req->link_list.prev, struct io_kiocb,
4439 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004440 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004441 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004442 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4443 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004444 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004445 }
4446
4447 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4448
4449 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004450 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004451 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4452 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004453 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004454 } else {
4455 io_cqring_add_event(req, -ETIME);
4456 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004457 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004458 return HRTIMER_NORESTART;
4459}
4460
Jens Axboead8a48a2019-11-15 08:49:11 -07004461static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004462{
Jens Axboe76a46e02019-11-10 23:34:16 -07004463 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004464
Jens Axboe76a46e02019-11-10 23:34:16 -07004465 /*
4466 * If the list is now empty, then our linked request finished before
4467 * we got a chance to setup the timer
4468 */
4469 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004470 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004471 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004472
Jens Axboead8a48a2019-11-15 08:49:11 -07004473 data->timer.function = io_link_timeout_fn;
4474 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4475 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004476 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004477 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004478
Jens Axboe2665abf2019-11-05 12:40:47 -07004479 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004480 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004481}
4482
Jens Axboead8a48a2019-11-15 08:49:11 -07004483static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004484{
4485 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004486
Jens Axboe2665abf2019-11-05 12:40:47 -07004487 if (!(req->flags & REQ_F_LINK))
4488 return NULL;
4489
Pavel Begunkov44932332019-12-05 16:16:35 +03004490 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4491 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004492 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004493 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004494
Jens Axboe76a46e02019-11-10 23:34:16 -07004495 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004496 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004497}
4498
Jens Axboe3529d8c2019-12-19 18:24:38 -07004499static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004500{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004501 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004502 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004503 int ret;
4504
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004505again:
4506 linked_timeout = io_prep_linked_timeout(req);
4507
Jens Axboe3529d8c2019-12-19 18:24:38 -07004508 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004509
4510 /*
4511 * We async punt it if the file wasn't marked NOWAIT, or if the file
4512 * doesn't support non-blocking read/write attempts
4513 */
4514 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4515 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004516 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4517 ret = io_grab_files(req);
4518 if (ret)
4519 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004520 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004521
4522 /*
4523 * Queued up for async execution, worker will release
4524 * submit reference when the iocb is actually submitted.
4525 */
4526 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004527 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004528 }
Jens Axboee65ef562019-03-12 10:16:44 -06004529
Jens Axboefcb323c2019-10-24 12:39:47 -06004530err:
Jens Axboee65ef562019-03-12 10:16:44 -06004531 /* drop submission reference */
4532 io_put_req(req);
4533
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004534 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004535 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004536 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004537 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004538 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004539 }
4540
Jens Axboee65ef562019-03-12 10:16:44 -06004541 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004542 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004543 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004544 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004545 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004546 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004547done_req:
4548 if (nxt) {
4549 req = nxt;
4550 nxt = NULL;
4551 goto again;
4552 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004553}
4554
Jens Axboe3529d8c2019-12-19 18:24:38 -07004555static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004556{
4557 int ret;
4558
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004559 if (unlikely(req->ctx->drain_next)) {
4560 req->flags |= REQ_F_IO_DRAIN;
Jens Axboe69b3e542020-01-08 11:01:46 -07004561 req->ctx->drain_next = 0;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004562 }
Jens Axboe69b3e542020-01-08 11:01:46 -07004563 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK) != 0;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004564
Jens Axboe3529d8c2019-12-19 18:24:38 -07004565 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004566 if (ret) {
4567 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004568 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004569 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004570 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004571 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004572 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboece35a472019-12-17 08:04:44 -07004573 /*
4574 * Never try inline submit of IOSQE_ASYNC is set, go straight
4575 * to async execution.
4576 */
4577 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4578 io_queue_async_work(req);
4579 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004580 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004581 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004582}
4583
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004584static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004585{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004586 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004587 io_cqring_add_event(req, -ECANCELED);
4588 io_double_put_req(req);
4589 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004590 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004591}
4592
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004593#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004594 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004595
Jens Axboe3529d8c2019-12-19 18:24:38 -07004596static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4597 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004598{
Jackie Liua197f662019-11-08 08:09:12 -07004599 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004600 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004601 int ret;
4602
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004603 sqe_flags = READ_ONCE(sqe->flags);
4604
Jens Axboe9e645e112019-05-10 16:07:28 -06004605 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004606 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004607 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004608 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004609 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004610 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004611 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004612
Jens Axboe3529d8c2019-12-19 18:24:38 -07004613 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004614 if (unlikely(ret)) {
4615err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004616 io_cqring_add_event(req, ret);
4617 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004618 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004619 }
4620
Jens Axboe9e645e112019-05-10 16:07:28 -06004621 /*
4622 * If we already have a head request, queue this one for async
4623 * submittal once the head completes. If we don't have a head but
4624 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4625 * submitted sync once the chain is complete. If none of those
4626 * conditions are true (normal request), then just queue it.
4627 */
4628 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004629 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004630
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004631 if (sqe_flags & IOSQE_IO_DRAIN)
Pavel Begunkov9d763772019-12-17 02:22:07 +03004632 head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004633
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004634 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004635 req->flags |= REQ_F_HARDLINK;
4636
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004637 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004638 ret = -EAGAIN;
4639 goto err_req;
4640 }
4641
Jens Axboe3529d8c2019-12-19 18:24:38 -07004642 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004643 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004644 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004645 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004646 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004647 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004648 trace_io_uring_link(ctx, req, head);
4649 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004650
4651 /* last request of a link, enqueue the link */
4652 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4653 io_queue_link_head(head);
4654 *link = NULL;
4655 }
4656 } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004657 req->flags |= REQ_F_LINK;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004658 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004659 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004660
Jens Axboe9e645e112019-05-10 16:07:28 -06004661 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004662 ret = io_req_defer_prep(req, sqe);
4663 if (ret)
4664 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004665 *link = req;
4666 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004667 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004668 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004669
4670 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004671}
4672
Jens Axboe9a56a232019-01-09 09:06:50 -07004673/*
4674 * Batched submission is done, ensure local IO is flushed out.
4675 */
4676static void io_submit_state_end(struct io_submit_state *state)
4677{
4678 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004679 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004680 if (state->free_reqs)
4681 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4682 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004683}
4684
4685/*
4686 * Start submission side cache.
4687 */
4688static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004689 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004690{
4691 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004692 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004693 state->file = NULL;
4694 state->ios_left = max_ios;
4695}
4696
Jens Axboe2b188cc2019-01-07 10:46:33 -07004697static void io_commit_sqring(struct io_ring_ctx *ctx)
4698{
Hristo Venev75b28af2019-08-26 17:23:46 +00004699 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004700
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004701 /*
4702 * Ensure any loads from the SQEs are done at this point,
4703 * since once we write the new head, the application could
4704 * write new data to them.
4705 */
4706 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004707}
4708
4709/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004710 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004711 * that is mapped by userspace. This means that care needs to be taken to
4712 * ensure that reads are stable, as we cannot rely on userspace always
4713 * being a good citizen. If members of the sqe are validated and then later
4714 * used, it's important that those reads are done through READ_ONCE() to
4715 * prevent a re-load down the line.
4716 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004717static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4718 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004719{
Hristo Venev75b28af2019-08-26 17:23:46 +00004720 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004721 unsigned head;
4722
4723 /*
4724 * The cached sq head (or cq tail) serves two purposes:
4725 *
4726 * 1) allows us to batch the cost of updating the user visible
4727 * head updates.
4728 * 2) allows the kernel side to track the head on its own, even
4729 * though the application is the one updating it.
4730 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004731 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004732 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004733 /*
4734 * All io need record the previous position, if LINK vs DARIN,
4735 * it can be used to mark the position of the first IO in the
4736 * link list.
4737 */
4738 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004739 *sqe_ptr = &ctx->sq_sqes[head];
4740 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4741 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004742 ctx->cached_sq_head++;
4743 return true;
4744 }
4745
4746 /* drop invalid entries */
4747 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004748 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004749 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004750 return false;
4751}
4752
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004753static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004754 struct file *ring_file, int ring_fd,
4755 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004756{
4757 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004758 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004759 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004760 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004761
Jens Axboec4a2ed72019-11-21 21:01:26 -07004762 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004763 if (test_bit(0, &ctx->sq_check_overflow)) {
4764 if (!list_empty(&ctx->cq_overflow_list) &&
4765 !io_cqring_overflow_flush(ctx, false))
4766 return -EBUSY;
4767 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004768
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004769 /* make sure SQ entry isn't read before tail */
4770 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004771
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004772 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4773 return -EAGAIN;
4774
Jens Axboe6c271ce2019-01-10 11:22:30 -07004775 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004776 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004777 statep = &state;
4778 }
4779
4780 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004781 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004782 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004783
Pavel Begunkov196be952019-11-07 01:41:06 +03004784 req = io_get_req(ctx, statep);
4785 if (unlikely(!req)) {
4786 if (!submitted)
4787 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004788 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004789 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004790 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004791 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004792 break;
4793 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004794
Jens Axboed3656342019-12-18 09:50:26 -07004795 /* will complete beyond this point, count as submitted */
4796 submitted++;
4797
4798 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4799 io_cqring_add_event(req, -EINVAL);
4800 io_double_put_req(req);
4801 break;
4802 }
4803
4804 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004805 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4806 if (!mm_fault) {
4807 use_mm(ctx->sqo_mm);
4808 *mm = ctx->sqo_mm;
4809 }
4810 }
4811
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004812 req->ring_file = ring_file;
4813 req->ring_fd = ring_fd;
4814 req->has_user = *mm != NULL;
4815 req->in_async = async;
4816 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07004817 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
4818 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004819 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004820 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004821 }
4822
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004823 if (submitted != nr)
4824 percpu_ref_put_many(&ctx->refs, nr - submitted);
Jens Axboe9e645e112019-05-10 16:07:28 -06004825 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004826 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004827 if (statep)
4828 io_submit_state_end(&state);
4829
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004830 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4831 io_commit_sqring(ctx);
4832
Jens Axboe6c271ce2019-01-10 11:22:30 -07004833 return submitted;
4834}
4835
4836static int io_sq_thread(void *data)
4837{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004838 struct io_ring_ctx *ctx = data;
4839 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004840 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004841 mm_segment_t old_fs;
4842 DEFINE_WAIT(wait);
4843 unsigned inflight;
4844 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004845 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004846
Jens Axboe206aefd2019-11-07 18:27:42 -07004847 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004848
Jens Axboe6c271ce2019-01-10 11:22:30 -07004849 old_fs = get_fs();
4850 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004851 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004852
Jens Axboec1edbf52019-11-10 16:56:04 -07004853 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004854 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004855 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004856
4857 if (inflight) {
4858 unsigned nr_events = 0;
4859
4860 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004861 /*
4862 * inflight is the count of the maximum possible
4863 * entries we submitted, but it can be smaller
4864 * if we dropped some of them. If we don't have
4865 * poll entries available, then we know that we
4866 * have nothing left to poll for. Reset the
4867 * inflight count to zero in that case.
4868 */
4869 mutex_lock(&ctx->uring_lock);
4870 if (!list_empty(&ctx->poll_list))
4871 __io_iopoll_check(ctx, &nr_events, 0);
4872 else
4873 inflight = 0;
4874 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004875 } else {
4876 /*
4877 * Normal IO, just pretend everything completed.
4878 * We don't have to poll completions for that.
4879 */
4880 nr_events = inflight;
4881 }
4882
4883 inflight -= nr_events;
4884 if (!inflight)
4885 timeout = jiffies + ctx->sq_thread_idle;
4886 }
4887
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004888 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004889
4890 /*
4891 * If submit got -EBUSY, flag us as needing the application
4892 * to enter the kernel to reap and flush events.
4893 */
4894 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004895 /*
4896 * We're polling. If we're within the defined idle
4897 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004898 * to sleep. The exception is if we got EBUSY doing
4899 * more IO, we should wait for the application to
4900 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004901 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004902 if (inflight ||
4903 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004904 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004905 continue;
4906 }
4907
4908 /*
4909 * Drop cur_mm before scheduling, we can't hold it for
4910 * long periods (or over schedule()). Do this before
4911 * adding ourselves to the waitqueue, as the unuse/drop
4912 * may sleep.
4913 */
4914 if (cur_mm) {
4915 unuse_mm(cur_mm);
4916 mmput(cur_mm);
4917 cur_mm = NULL;
4918 }
4919
4920 prepare_to_wait(&ctx->sqo_wait, &wait,
4921 TASK_INTERRUPTIBLE);
4922
4923 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004924 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004925 /* make sure to read SQ tail after writing flags */
4926 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004927
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004928 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004929 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004930 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004931 finish_wait(&ctx->sqo_wait, &wait);
4932 break;
4933 }
4934 if (signal_pending(current))
4935 flush_signals(current);
4936 schedule();
4937 finish_wait(&ctx->sqo_wait, &wait);
4938
Hristo Venev75b28af2019-08-26 17:23:46 +00004939 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004940 continue;
4941 }
4942 finish_wait(&ctx->sqo_wait, &wait);
4943
Hristo Venev75b28af2019-08-26 17:23:46 +00004944 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004945 }
4946
Jens Axboe8a4955f2019-12-09 14:52:35 -07004947 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004948 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004949 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004950 if (ret > 0)
4951 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004952 }
4953
4954 set_fs(old_fs);
4955 if (cur_mm) {
4956 unuse_mm(cur_mm);
4957 mmput(cur_mm);
4958 }
Jens Axboe181e4482019-11-25 08:52:30 -07004959 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004960
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004961 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004962
Jens Axboe6c271ce2019-01-10 11:22:30 -07004963 return 0;
4964}
4965
Jens Axboebda52162019-09-24 13:47:15 -06004966struct io_wait_queue {
4967 struct wait_queue_entry wq;
4968 struct io_ring_ctx *ctx;
4969 unsigned to_wait;
4970 unsigned nr_timeouts;
4971};
4972
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004973static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004974{
4975 struct io_ring_ctx *ctx = iowq->ctx;
4976
4977 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004978 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004979 * started waiting. For timeouts, we always want to return to userspace,
4980 * regardless of event count.
4981 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004982 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004983 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4984}
4985
4986static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4987 int wake_flags, void *key)
4988{
4989 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4990 wq);
4991
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004992 /* use noflush == true, as we can't safely rely on locking context */
4993 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004994 return -1;
4995
4996 return autoremove_wake_function(curr, mode, wake_flags, key);
4997}
4998
Jens Axboe2b188cc2019-01-07 10:46:33 -07004999/*
5000 * Wait until events become available, if we don't already have some. The
5001 * application must reap them itself, as they reside on the shared cq ring.
5002 */
5003static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5004 const sigset_t __user *sig, size_t sigsz)
5005{
Jens Axboebda52162019-09-24 13:47:15 -06005006 struct io_wait_queue iowq = {
5007 .wq = {
5008 .private = current,
5009 .func = io_wake_function,
5010 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5011 },
5012 .ctx = ctx,
5013 .to_wait = min_events,
5014 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005015 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005016 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005017
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005018 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005019 return 0;
5020
5021 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005022#ifdef CONFIG_COMPAT
5023 if (in_compat_syscall())
5024 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005025 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005026 else
5027#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005028 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005029
Jens Axboe2b188cc2019-01-07 10:46:33 -07005030 if (ret)
5031 return ret;
5032 }
5033
Jens Axboebda52162019-09-24 13:47:15 -06005034 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005035 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005036 do {
5037 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5038 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005039 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005040 break;
5041 schedule();
5042 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005043 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005044 break;
5045 }
5046 } while (1);
5047 finish_wait(&ctx->wait, &iowq.wq);
5048
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005049 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005050
Hristo Venev75b28af2019-08-26 17:23:46 +00005051 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005052}
5053
Jens Axboe6b063142019-01-10 22:13:58 -07005054static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5055{
5056#if defined(CONFIG_UNIX)
5057 if (ctx->ring_sock) {
5058 struct sock *sock = ctx->ring_sock->sk;
5059 struct sk_buff *skb;
5060
5061 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5062 kfree_skb(skb);
5063 }
5064#else
5065 int i;
5066
Jens Axboe65e19f52019-10-26 07:20:21 -06005067 for (i = 0; i < ctx->nr_user_files; i++) {
5068 struct file *file;
5069
5070 file = io_file_from_index(ctx, i);
5071 if (file)
5072 fput(file);
5073 }
Jens Axboe6b063142019-01-10 22:13:58 -07005074#endif
5075}
5076
Jens Axboe05f3fb32019-12-09 11:22:50 -07005077static void io_file_ref_kill(struct percpu_ref *ref)
5078{
5079 struct fixed_file_data *data;
5080
5081 data = container_of(ref, struct fixed_file_data, refs);
5082 complete(&data->done);
5083}
5084
Jens Axboe6b063142019-01-10 22:13:58 -07005085static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5086{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005087 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005088 unsigned nr_tables, i;
5089
Jens Axboe05f3fb32019-12-09 11:22:50 -07005090 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005091 return -ENXIO;
5092
Jens Axboe05f3fb32019-12-09 11:22:50 -07005093 /* protect against inflight atomic switch, which drops the ref */
5094 flush_work(&data->ref_work);
5095 percpu_ref_get(&data->refs);
5096 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5097 wait_for_completion(&data->done);
5098 percpu_ref_put(&data->refs);
5099 percpu_ref_exit(&data->refs);
5100
Jens Axboe6b063142019-01-10 22:13:58 -07005101 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005102 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5103 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005104 kfree(data->table[i].files);
5105 kfree(data->table);
5106 kfree(data);
5107 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005108 ctx->nr_user_files = 0;
5109 return 0;
5110}
5111
Jens Axboe6c271ce2019-01-10 11:22:30 -07005112static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5113{
5114 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005115 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005116 /*
5117 * The park is a bit of a work-around, without it we get
5118 * warning spews on shutdown with SQPOLL set and affinity
5119 * set to a single CPU.
5120 */
Jens Axboe06058632019-04-13 09:26:03 -06005121 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005122 kthread_stop(ctx->sqo_thread);
5123 ctx->sqo_thread = NULL;
5124 }
5125}
5126
Jens Axboe6b063142019-01-10 22:13:58 -07005127static void io_finish_async(struct io_ring_ctx *ctx)
5128{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005129 io_sq_thread_stop(ctx);
5130
Jens Axboe561fb042019-10-24 07:25:42 -06005131 if (ctx->io_wq) {
5132 io_wq_destroy(ctx->io_wq);
5133 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005134 }
5135}
5136
5137#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005138/*
5139 * Ensure the UNIX gc is aware of our file set, so we are certain that
5140 * the io_uring can be safely unregistered on process exit, even if we have
5141 * loops in the file referencing.
5142 */
5143static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5144{
5145 struct sock *sk = ctx->ring_sock->sk;
5146 struct scm_fp_list *fpl;
5147 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005148 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005149
5150 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5151 unsigned long inflight = ctx->user->unix_inflight + nr;
5152
5153 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5154 return -EMFILE;
5155 }
5156
5157 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5158 if (!fpl)
5159 return -ENOMEM;
5160
5161 skb = alloc_skb(0, GFP_KERNEL);
5162 if (!skb) {
5163 kfree(fpl);
5164 return -ENOMEM;
5165 }
5166
5167 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005168
Jens Axboe08a45172019-10-03 08:11:03 -06005169 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005170 fpl->user = get_uid(ctx->user);
5171 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005172 struct file *file = io_file_from_index(ctx, i + offset);
5173
5174 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005175 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005176 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005177 unix_inflight(fpl->user, fpl->fp[nr_files]);
5178 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005179 }
5180
Jens Axboe08a45172019-10-03 08:11:03 -06005181 if (nr_files) {
5182 fpl->max = SCM_MAX_FD;
5183 fpl->count = nr_files;
5184 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005185 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005186 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5187 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005188
Jens Axboe08a45172019-10-03 08:11:03 -06005189 for (i = 0; i < nr_files; i++)
5190 fput(fpl->fp[i]);
5191 } else {
5192 kfree_skb(skb);
5193 kfree(fpl);
5194 }
Jens Axboe6b063142019-01-10 22:13:58 -07005195
5196 return 0;
5197}
5198
5199/*
5200 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5201 * causes regular reference counting to break down. We rely on the UNIX
5202 * garbage collection to take care of this problem for us.
5203 */
5204static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5205{
5206 unsigned left, total;
5207 int ret = 0;
5208
5209 total = 0;
5210 left = ctx->nr_user_files;
5211 while (left) {
5212 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005213
5214 ret = __io_sqe_files_scm(ctx, this_files, total);
5215 if (ret)
5216 break;
5217 left -= this_files;
5218 total += this_files;
5219 }
5220
5221 if (!ret)
5222 return 0;
5223
5224 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005225 struct file *file = io_file_from_index(ctx, total);
5226
5227 if (file)
5228 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005229 total++;
5230 }
5231
5232 return ret;
5233}
5234#else
5235static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5236{
5237 return 0;
5238}
5239#endif
5240
Jens Axboe65e19f52019-10-26 07:20:21 -06005241static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5242 unsigned nr_files)
5243{
5244 int i;
5245
5246 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005247 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005248 unsigned this_files;
5249
5250 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5251 table->files = kcalloc(this_files, sizeof(struct file *),
5252 GFP_KERNEL);
5253 if (!table->files)
5254 break;
5255 nr_files -= this_files;
5256 }
5257
5258 if (i == nr_tables)
5259 return 0;
5260
5261 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005262 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005263 kfree(table->files);
5264 }
5265 return 1;
5266}
5267
Jens Axboe05f3fb32019-12-09 11:22:50 -07005268static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005269{
5270#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005271 struct sock *sock = ctx->ring_sock->sk;
5272 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5273 struct sk_buff *skb;
5274 int i;
5275
5276 __skb_queue_head_init(&list);
5277
5278 /*
5279 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5280 * remove this entry and rearrange the file array.
5281 */
5282 skb = skb_dequeue(head);
5283 while (skb) {
5284 struct scm_fp_list *fp;
5285
5286 fp = UNIXCB(skb).fp;
5287 for (i = 0; i < fp->count; i++) {
5288 int left;
5289
5290 if (fp->fp[i] != file)
5291 continue;
5292
5293 unix_notinflight(fp->user, fp->fp[i]);
5294 left = fp->count - 1 - i;
5295 if (left) {
5296 memmove(&fp->fp[i], &fp->fp[i + 1],
5297 left * sizeof(struct file *));
5298 }
5299 fp->count--;
5300 if (!fp->count) {
5301 kfree_skb(skb);
5302 skb = NULL;
5303 } else {
5304 __skb_queue_tail(&list, skb);
5305 }
5306 fput(file);
5307 file = NULL;
5308 break;
5309 }
5310
5311 if (!file)
5312 break;
5313
5314 __skb_queue_tail(&list, skb);
5315
5316 skb = skb_dequeue(head);
5317 }
5318
5319 if (skb_peek(&list)) {
5320 spin_lock_irq(&head->lock);
5321 while ((skb = __skb_dequeue(&list)) != NULL)
5322 __skb_queue_tail(head, skb);
5323 spin_unlock_irq(&head->lock);
5324 }
5325#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005326 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005327#endif
5328}
5329
Jens Axboe05f3fb32019-12-09 11:22:50 -07005330struct io_file_put {
5331 struct llist_node llist;
5332 struct file *file;
5333 struct completion *done;
5334};
5335
5336static void io_ring_file_ref_switch(struct work_struct *work)
5337{
5338 struct io_file_put *pfile, *tmp;
5339 struct fixed_file_data *data;
5340 struct llist_node *node;
5341
5342 data = container_of(work, struct fixed_file_data, ref_work);
5343
5344 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5345 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5346 io_ring_file_put(data->ctx, pfile->file);
5347 if (pfile->done)
5348 complete(pfile->done);
5349 else
5350 kfree(pfile);
5351 }
5352 }
5353
5354 percpu_ref_get(&data->refs);
5355 percpu_ref_switch_to_percpu(&data->refs);
5356}
5357
5358static void io_file_data_ref_zero(struct percpu_ref *ref)
5359{
5360 struct fixed_file_data *data;
5361
5362 data = container_of(ref, struct fixed_file_data, refs);
5363
5364 /* we can't safely switch from inside this context, punt to wq */
5365 queue_work(system_wq, &data->ref_work);
5366}
5367
5368static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5369 unsigned nr_args)
5370{
5371 __s32 __user *fds = (__s32 __user *) arg;
5372 unsigned nr_tables;
5373 struct file *file;
5374 int fd, ret = 0;
5375 unsigned i;
5376
5377 if (ctx->file_data)
5378 return -EBUSY;
5379 if (!nr_args)
5380 return -EINVAL;
5381 if (nr_args > IORING_MAX_FIXED_FILES)
5382 return -EMFILE;
5383
5384 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5385 if (!ctx->file_data)
5386 return -ENOMEM;
5387 ctx->file_data->ctx = ctx;
5388 init_completion(&ctx->file_data->done);
5389
5390 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5391 ctx->file_data->table = kcalloc(nr_tables,
5392 sizeof(struct fixed_file_table),
5393 GFP_KERNEL);
5394 if (!ctx->file_data->table) {
5395 kfree(ctx->file_data);
5396 ctx->file_data = NULL;
5397 return -ENOMEM;
5398 }
5399
5400 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5401 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5402 kfree(ctx->file_data->table);
5403 kfree(ctx->file_data);
5404 ctx->file_data = NULL;
5405 return -ENOMEM;
5406 }
5407 ctx->file_data->put_llist.first = NULL;
5408 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5409
5410 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5411 percpu_ref_exit(&ctx->file_data->refs);
5412 kfree(ctx->file_data->table);
5413 kfree(ctx->file_data);
5414 ctx->file_data = NULL;
5415 return -ENOMEM;
5416 }
5417
5418 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5419 struct fixed_file_table *table;
5420 unsigned index;
5421
5422 ret = -EFAULT;
5423 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5424 break;
5425 /* allow sparse sets */
5426 if (fd == -1) {
5427 ret = 0;
5428 continue;
5429 }
5430
5431 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5432 index = i & IORING_FILE_TABLE_MASK;
5433 file = fget(fd);
5434
5435 ret = -EBADF;
5436 if (!file)
5437 break;
5438
5439 /*
5440 * Don't allow io_uring instances to be registered. If UNIX
5441 * isn't enabled, then this causes a reference cycle and this
5442 * instance can never get freed. If UNIX is enabled we'll
5443 * handle it just fine, but there's still no point in allowing
5444 * a ring fd as it doesn't support regular read/write anyway.
5445 */
5446 if (file->f_op == &io_uring_fops) {
5447 fput(file);
5448 break;
5449 }
5450 ret = 0;
5451 table->files[index] = file;
5452 }
5453
5454 if (ret) {
5455 for (i = 0; i < ctx->nr_user_files; i++) {
5456 file = io_file_from_index(ctx, i);
5457 if (file)
5458 fput(file);
5459 }
5460 for (i = 0; i < nr_tables; i++)
5461 kfree(ctx->file_data->table[i].files);
5462
5463 kfree(ctx->file_data->table);
5464 kfree(ctx->file_data);
5465 ctx->file_data = NULL;
5466 ctx->nr_user_files = 0;
5467 return ret;
5468 }
5469
5470 ret = io_sqe_files_scm(ctx);
5471 if (ret)
5472 io_sqe_files_unregister(ctx);
5473
5474 return ret;
5475}
5476
Jens Axboec3a31e62019-10-03 13:59:56 -06005477static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5478 int index)
5479{
5480#if defined(CONFIG_UNIX)
5481 struct sock *sock = ctx->ring_sock->sk;
5482 struct sk_buff_head *head = &sock->sk_receive_queue;
5483 struct sk_buff *skb;
5484
5485 /*
5486 * See if we can merge this file into an existing skb SCM_RIGHTS
5487 * file set. If there's no room, fall back to allocating a new skb
5488 * and filling it in.
5489 */
5490 spin_lock_irq(&head->lock);
5491 skb = skb_peek(head);
5492 if (skb) {
5493 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5494
5495 if (fpl->count < SCM_MAX_FD) {
5496 __skb_unlink(skb, head);
5497 spin_unlock_irq(&head->lock);
5498 fpl->fp[fpl->count] = get_file(file);
5499 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5500 fpl->count++;
5501 spin_lock_irq(&head->lock);
5502 __skb_queue_head(head, skb);
5503 } else {
5504 skb = NULL;
5505 }
5506 }
5507 spin_unlock_irq(&head->lock);
5508
5509 if (skb) {
5510 fput(file);
5511 return 0;
5512 }
5513
5514 return __io_sqe_files_scm(ctx, 1, index);
5515#else
5516 return 0;
5517#endif
5518}
5519
Jens Axboe05f3fb32019-12-09 11:22:50 -07005520static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005521{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005522 struct fixed_file_data *data;
5523
5524 data = container_of(ref, struct fixed_file_data, refs);
5525 clear_bit(FFD_F_ATOMIC, &data->state);
5526}
5527
5528static bool io_queue_file_removal(struct fixed_file_data *data,
5529 struct file *file)
5530{
5531 struct io_file_put *pfile, pfile_stack;
5532 DECLARE_COMPLETION_ONSTACK(done);
5533
5534 /*
5535 * If we fail allocating the struct we need for doing async reomval
5536 * of this file, just punt to sync and wait for it.
5537 */
5538 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5539 if (!pfile) {
5540 pfile = &pfile_stack;
5541 pfile->done = &done;
5542 }
5543
5544 pfile->file = file;
5545 llist_add(&pfile->llist, &data->put_llist);
5546
5547 if (pfile == &pfile_stack) {
5548 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5549 percpu_ref_put(&data->refs);
5550 percpu_ref_switch_to_atomic(&data->refs,
5551 io_atomic_switch);
5552 }
5553 wait_for_completion(&done);
5554 flush_work(&data->ref_work);
5555 return false;
5556 }
5557
5558 return true;
5559}
5560
5561static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5562 struct io_uring_files_update *up,
5563 unsigned nr_args)
5564{
5565 struct fixed_file_data *data = ctx->file_data;
5566 bool ref_switch = false;
5567 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005568 __s32 __user *fds;
5569 int fd, i, err;
5570 __u32 done;
5571
Jens Axboe05f3fb32019-12-09 11:22:50 -07005572 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005573 return -EOVERFLOW;
5574 if (done > ctx->nr_user_files)
5575 return -EINVAL;
5576
5577 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005578 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005579 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005580 struct fixed_file_table *table;
5581 unsigned index;
5582
Jens Axboec3a31e62019-10-03 13:59:56 -06005583 err = 0;
5584 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5585 err = -EFAULT;
5586 break;
5587 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005588 i = array_index_nospec(up->offset, ctx->nr_user_files);
5589 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005590 index = i & IORING_FILE_TABLE_MASK;
5591 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005592 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005593 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005594 if (io_queue_file_removal(data, file))
5595 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005596 }
5597 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005598 file = fget(fd);
5599 if (!file) {
5600 err = -EBADF;
5601 break;
5602 }
5603 /*
5604 * Don't allow io_uring instances to be registered. If
5605 * UNIX isn't enabled, then this causes a reference
5606 * cycle and this instance can never get freed. If UNIX
5607 * is enabled we'll handle it just fine, but there's
5608 * still no point in allowing a ring fd as it doesn't
5609 * support regular read/write anyway.
5610 */
5611 if (file->f_op == &io_uring_fops) {
5612 fput(file);
5613 err = -EBADF;
5614 break;
5615 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005616 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005617 err = io_sqe_file_register(ctx, file, i);
5618 if (err)
5619 break;
5620 }
5621 nr_args--;
5622 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005623 up->offset++;
5624 }
5625
5626 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5627 percpu_ref_put(&data->refs);
5628 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005629 }
5630
5631 return done ? done : err;
5632}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005633static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5634 unsigned nr_args)
5635{
5636 struct io_uring_files_update up;
5637
5638 if (!ctx->file_data)
5639 return -ENXIO;
5640 if (!nr_args)
5641 return -EINVAL;
5642 if (copy_from_user(&up, arg, sizeof(up)))
5643 return -EFAULT;
5644 if (up.resv)
5645 return -EINVAL;
5646
5647 return __io_sqe_files_update(ctx, &up, nr_args);
5648}
Jens Axboec3a31e62019-10-03 13:59:56 -06005649
Jens Axboe7d723062019-11-12 22:31:31 -07005650static void io_put_work(struct io_wq_work *work)
5651{
5652 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5653
5654 io_put_req(req);
5655}
5656
5657static void io_get_work(struct io_wq_work *work)
5658{
5659 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5660
5661 refcount_inc(&req->refs);
5662}
5663
Jens Axboe6c271ce2019-01-10 11:22:30 -07005664static int io_sq_offload_start(struct io_ring_ctx *ctx,
5665 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005666{
Jens Axboe576a3472019-11-25 08:49:20 -07005667 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005668 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005669 int ret;
5670
Jens Axboe6c271ce2019-01-10 11:22:30 -07005671 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005672 mmgrab(current->mm);
5673 ctx->sqo_mm = current->mm;
5674
Jens Axboe6c271ce2019-01-10 11:22:30 -07005675 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005676 ret = -EPERM;
5677 if (!capable(CAP_SYS_ADMIN))
5678 goto err;
5679
Jens Axboe917257d2019-04-13 09:28:55 -06005680 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5681 if (!ctx->sq_thread_idle)
5682 ctx->sq_thread_idle = HZ;
5683
Jens Axboe6c271ce2019-01-10 11:22:30 -07005684 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005685 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005686
Jens Axboe917257d2019-04-13 09:28:55 -06005687 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005688 if (cpu >= nr_cpu_ids)
5689 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005690 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005691 goto err;
5692
Jens Axboe6c271ce2019-01-10 11:22:30 -07005693 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5694 ctx, cpu,
5695 "io_uring-sq");
5696 } else {
5697 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5698 "io_uring-sq");
5699 }
5700 if (IS_ERR(ctx->sqo_thread)) {
5701 ret = PTR_ERR(ctx->sqo_thread);
5702 ctx->sqo_thread = NULL;
5703 goto err;
5704 }
5705 wake_up_process(ctx->sqo_thread);
5706 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5707 /* Can't have SQ_AFF without SQPOLL */
5708 ret = -EINVAL;
5709 goto err;
5710 }
5711
Jens Axboe576a3472019-11-25 08:49:20 -07005712 data.mm = ctx->sqo_mm;
5713 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005714 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005715 data.get_work = io_get_work;
5716 data.put_work = io_put_work;
5717
Jens Axboe561fb042019-10-24 07:25:42 -06005718 /* Do QD, or 4 * CPUS, whatever is smallest */
5719 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005720 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005721 if (IS_ERR(ctx->io_wq)) {
5722 ret = PTR_ERR(ctx->io_wq);
5723 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005724 goto err;
5725 }
5726
5727 return 0;
5728err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005729 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005730 mmdrop(ctx->sqo_mm);
5731 ctx->sqo_mm = NULL;
5732 return ret;
5733}
5734
5735static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5736{
5737 atomic_long_sub(nr_pages, &user->locked_vm);
5738}
5739
5740static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5741{
5742 unsigned long page_limit, cur_pages, new_pages;
5743
5744 /* Don't allow more pages than we can safely lock */
5745 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5746
5747 do {
5748 cur_pages = atomic_long_read(&user->locked_vm);
5749 new_pages = cur_pages + nr_pages;
5750 if (new_pages > page_limit)
5751 return -ENOMEM;
5752 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5753 new_pages) != cur_pages);
5754
5755 return 0;
5756}
5757
5758static void io_mem_free(void *ptr)
5759{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005760 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005761
Mark Rutland52e04ef2019-04-30 17:30:21 +01005762 if (!ptr)
5763 return;
5764
5765 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005766 if (put_page_testzero(page))
5767 free_compound_page(page);
5768}
5769
5770static void *io_mem_alloc(size_t size)
5771{
5772 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5773 __GFP_NORETRY;
5774
5775 return (void *) __get_free_pages(gfp_flags, get_order(size));
5776}
5777
Hristo Venev75b28af2019-08-26 17:23:46 +00005778static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5779 size_t *sq_offset)
5780{
5781 struct io_rings *rings;
5782 size_t off, sq_array_size;
5783
5784 off = struct_size(rings, cqes, cq_entries);
5785 if (off == SIZE_MAX)
5786 return SIZE_MAX;
5787
5788#ifdef CONFIG_SMP
5789 off = ALIGN(off, SMP_CACHE_BYTES);
5790 if (off == 0)
5791 return SIZE_MAX;
5792#endif
5793
5794 sq_array_size = array_size(sizeof(u32), sq_entries);
5795 if (sq_array_size == SIZE_MAX)
5796 return SIZE_MAX;
5797
5798 if (check_add_overflow(off, sq_array_size, &off))
5799 return SIZE_MAX;
5800
5801 if (sq_offset)
5802 *sq_offset = off;
5803
5804 return off;
5805}
5806
Jens Axboe2b188cc2019-01-07 10:46:33 -07005807static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5808{
Hristo Venev75b28af2019-08-26 17:23:46 +00005809 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005810
Hristo Venev75b28af2019-08-26 17:23:46 +00005811 pages = (size_t)1 << get_order(
5812 rings_size(sq_entries, cq_entries, NULL));
5813 pages += (size_t)1 << get_order(
5814 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005815
Hristo Venev75b28af2019-08-26 17:23:46 +00005816 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005817}
5818
Jens Axboeedafcce2019-01-09 09:16:05 -07005819static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5820{
5821 int i, j;
5822
5823 if (!ctx->user_bufs)
5824 return -ENXIO;
5825
5826 for (i = 0; i < ctx->nr_user_bufs; i++) {
5827 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5828
5829 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005830 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005831
5832 if (ctx->account_mem)
5833 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005834 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005835 imu->nr_bvecs = 0;
5836 }
5837
5838 kfree(ctx->user_bufs);
5839 ctx->user_bufs = NULL;
5840 ctx->nr_user_bufs = 0;
5841 return 0;
5842}
5843
5844static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5845 void __user *arg, unsigned index)
5846{
5847 struct iovec __user *src;
5848
5849#ifdef CONFIG_COMPAT
5850 if (ctx->compat) {
5851 struct compat_iovec __user *ciovs;
5852 struct compat_iovec ciov;
5853
5854 ciovs = (struct compat_iovec __user *) arg;
5855 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5856 return -EFAULT;
5857
Jens Axboed55e5f52019-12-11 16:12:15 -07005858 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005859 dst->iov_len = ciov.iov_len;
5860 return 0;
5861 }
5862#endif
5863 src = (struct iovec __user *) arg;
5864 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5865 return -EFAULT;
5866 return 0;
5867}
5868
5869static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5870 unsigned nr_args)
5871{
5872 struct vm_area_struct **vmas = NULL;
5873 struct page **pages = NULL;
5874 int i, j, got_pages = 0;
5875 int ret = -EINVAL;
5876
5877 if (ctx->user_bufs)
5878 return -EBUSY;
5879 if (!nr_args || nr_args > UIO_MAXIOV)
5880 return -EINVAL;
5881
5882 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5883 GFP_KERNEL);
5884 if (!ctx->user_bufs)
5885 return -ENOMEM;
5886
5887 for (i = 0; i < nr_args; i++) {
5888 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5889 unsigned long off, start, end, ubuf;
5890 int pret, nr_pages;
5891 struct iovec iov;
5892 size_t size;
5893
5894 ret = io_copy_iov(ctx, &iov, arg, i);
5895 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005896 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005897
5898 /*
5899 * Don't impose further limits on the size and buffer
5900 * constraints here, we'll -EINVAL later when IO is
5901 * submitted if they are wrong.
5902 */
5903 ret = -EFAULT;
5904 if (!iov.iov_base || !iov.iov_len)
5905 goto err;
5906
5907 /* arbitrary limit, but we need something */
5908 if (iov.iov_len > SZ_1G)
5909 goto err;
5910
5911 ubuf = (unsigned long) iov.iov_base;
5912 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5913 start = ubuf >> PAGE_SHIFT;
5914 nr_pages = end - start;
5915
5916 if (ctx->account_mem) {
5917 ret = io_account_mem(ctx->user, nr_pages);
5918 if (ret)
5919 goto err;
5920 }
5921
5922 ret = 0;
5923 if (!pages || nr_pages > got_pages) {
5924 kfree(vmas);
5925 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005926 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005927 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005928 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005929 sizeof(struct vm_area_struct *),
5930 GFP_KERNEL);
5931 if (!pages || !vmas) {
5932 ret = -ENOMEM;
5933 if (ctx->account_mem)
5934 io_unaccount_mem(ctx->user, nr_pages);
5935 goto err;
5936 }
5937 got_pages = nr_pages;
5938 }
5939
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005940 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005941 GFP_KERNEL);
5942 ret = -ENOMEM;
5943 if (!imu->bvec) {
5944 if (ctx->account_mem)
5945 io_unaccount_mem(ctx->user, nr_pages);
5946 goto err;
5947 }
5948
5949 ret = 0;
5950 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005951 pret = get_user_pages(ubuf, nr_pages,
5952 FOLL_WRITE | FOLL_LONGTERM,
5953 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005954 if (pret == nr_pages) {
5955 /* don't support file backed memory */
5956 for (j = 0; j < nr_pages; j++) {
5957 struct vm_area_struct *vma = vmas[j];
5958
5959 if (vma->vm_file &&
5960 !is_file_hugepages(vma->vm_file)) {
5961 ret = -EOPNOTSUPP;
5962 break;
5963 }
5964 }
5965 } else {
5966 ret = pret < 0 ? pret : -EFAULT;
5967 }
5968 up_read(&current->mm->mmap_sem);
5969 if (ret) {
5970 /*
5971 * if we did partial map, or found file backed vmas,
5972 * release any pages we did get
5973 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005974 if (pret > 0)
5975 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005976 if (ctx->account_mem)
5977 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005978 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005979 goto err;
5980 }
5981
5982 off = ubuf & ~PAGE_MASK;
5983 size = iov.iov_len;
5984 for (j = 0; j < nr_pages; j++) {
5985 size_t vec_len;
5986
5987 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5988 imu->bvec[j].bv_page = pages[j];
5989 imu->bvec[j].bv_len = vec_len;
5990 imu->bvec[j].bv_offset = off;
5991 off = 0;
5992 size -= vec_len;
5993 }
5994 /* store original address for later verification */
5995 imu->ubuf = ubuf;
5996 imu->len = iov.iov_len;
5997 imu->nr_bvecs = nr_pages;
5998
5999 ctx->nr_user_bufs++;
6000 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006001 kvfree(pages);
6002 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006003 return 0;
6004err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006005 kvfree(pages);
6006 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006007 io_sqe_buffer_unregister(ctx);
6008 return ret;
6009}
6010
Jens Axboe9b402842019-04-11 11:45:41 -06006011static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6012{
6013 __s32 __user *fds = arg;
6014 int fd;
6015
6016 if (ctx->cq_ev_fd)
6017 return -EBUSY;
6018
6019 if (copy_from_user(&fd, fds, sizeof(*fds)))
6020 return -EFAULT;
6021
6022 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6023 if (IS_ERR(ctx->cq_ev_fd)) {
6024 int ret = PTR_ERR(ctx->cq_ev_fd);
6025 ctx->cq_ev_fd = NULL;
6026 return ret;
6027 }
6028
6029 return 0;
6030}
6031
6032static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6033{
6034 if (ctx->cq_ev_fd) {
6035 eventfd_ctx_put(ctx->cq_ev_fd);
6036 ctx->cq_ev_fd = NULL;
6037 return 0;
6038 }
6039
6040 return -ENXIO;
6041}
6042
Jens Axboe2b188cc2019-01-07 10:46:33 -07006043static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6044{
Jens Axboe6b063142019-01-10 22:13:58 -07006045 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006046 if (ctx->sqo_mm)
6047 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006048
6049 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006050 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006051 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006052 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006053
Jens Axboe2b188cc2019-01-07 10:46:33 -07006054#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006055 if (ctx->ring_sock) {
6056 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006057 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006058 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006059#endif
6060
Hristo Venev75b28af2019-08-26 17:23:46 +00006061 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006062 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006063
6064 percpu_ref_exit(&ctx->refs);
6065 if (ctx->account_mem)
6066 io_unaccount_mem(ctx->user,
6067 ring_pages(ctx->sq_entries, ctx->cq_entries));
6068 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006069 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006070 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006071 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006072 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006073 kfree(ctx);
6074}
6075
6076static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6077{
6078 struct io_ring_ctx *ctx = file->private_data;
6079 __poll_t mask = 0;
6080
6081 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006082 /*
6083 * synchronizes with barrier from wq_has_sleeper call in
6084 * io_commit_cqring
6085 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006086 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006087 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6088 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006089 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006090 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006091 mask |= EPOLLIN | EPOLLRDNORM;
6092
6093 return mask;
6094}
6095
6096static int io_uring_fasync(int fd, struct file *file, int on)
6097{
6098 struct io_ring_ctx *ctx = file->private_data;
6099
6100 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6101}
6102
6103static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6104{
6105 mutex_lock(&ctx->uring_lock);
6106 percpu_ref_kill(&ctx->refs);
6107 mutex_unlock(&ctx->uring_lock);
6108
Jens Axboe5262f562019-09-17 12:26:57 -06006109 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006110 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006111
6112 if (ctx->io_wq)
6113 io_wq_cancel_all(ctx->io_wq);
6114
Jens Axboedef596e2019-01-09 08:59:42 -07006115 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006116 /* if we failed setting up the ctx, we might not have any rings */
6117 if (ctx->rings)
6118 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07006119 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006120 io_ring_ctx_free(ctx);
6121}
6122
6123static int io_uring_release(struct inode *inode, struct file *file)
6124{
6125 struct io_ring_ctx *ctx = file->private_data;
6126
6127 file->private_data = NULL;
6128 io_ring_ctx_wait_and_kill(ctx);
6129 return 0;
6130}
6131
Jens Axboefcb323c2019-10-24 12:39:47 -06006132static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6133 struct files_struct *files)
6134{
6135 struct io_kiocb *req;
6136 DEFINE_WAIT(wait);
6137
6138 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006139 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006140
6141 spin_lock_irq(&ctx->inflight_lock);
6142 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006143 if (req->work.files != files)
6144 continue;
6145 /* req is being completed, ignore */
6146 if (!refcount_inc_not_zero(&req->refs))
6147 continue;
6148 cancel_req = req;
6149 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006150 }
Jens Axboe768134d2019-11-10 20:30:53 -07006151 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006152 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006153 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006154 spin_unlock_irq(&ctx->inflight_lock);
6155
Jens Axboe768134d2019-11-10 20:30:53 -07006156 /* We need to keep going until we don't find a matching req */
6157 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006158 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006159
6160 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6161 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006162 schedule();
6163 }
Jens Axboe768134d2019-11-10 20:30:53 -07006164 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006165}
6166
6167static int io_uring_flush(struct file *file, void *data)
6168{
6169 struct io_ring_ctx *ctx = file->private_data;
6170
6171 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006172 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6173 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006174 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006175 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006176 return 0;
6177}
6178
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006179static void *io_uring_validate_mmap_request(struct file *file,
6180 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006181{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006182 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006183 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006184 struct page *page;
6185 void *ptr;
6186
6187 switch (offset) {
6188 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006189 case IORING_OFF_CQ_RING:
6190 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006191 break;
6192 case IORING_OFF_SQES:
6193 ptr = ctx->sq_sqes;
6194 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006195 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006196 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006197 }
6198
6199 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006200 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006201 return ERR_PTR(-EINVAL);
6202
6203 return ptr;
6204}
6205
6206#ifdef CONFIG_MMU
6207
6208static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6209{
6210 size_t sz = vma->vm_end - vma->vm_start;
6211 unsigned long pfn;
6212 void *ptr;
6213
6214 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6215 if (IS_ERR(ptr))
6216 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006217
6218 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6219 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6220}
6221
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006222#else /* !CONFIG_MMU */
6223
6224static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6225{
6226 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6227}
6228
6229static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6230{
6231 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6232}
6233
6234static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6235 unsigned long addr, unsigned long len,
6236 unsigned long pgoff, unsigned long flags)
6237{
6238 void *ptr;
6239
6240 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6241 if (IS_ERR(ptr))
6242 return PTR_ERR(ptr);
6243
6244 return (unsigned long) ptr;
6245}
6246
6247#endif /* !CONFIG_MMU */
6248
Jens Axboe2b188cc2019-01-07 10:46:33 -07006249SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6250 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6251 size_t, sigsz)
6252{
6253 struct io_ring_ctx *ctx;
6254 long ret = -EBADF;
6255 int submitted = 0;
6256 struct fd f;
6257
Jens Axboe6c271ce2019-01-10 11:22:30 -07006258 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006259 return -EINVAL;
6260
6261 f = fdget(fd);
6262 if (!f.file)
6263 return -EBADF;
6264
6265 ret = -EOPNOTSUPP;
6266 if (f.file->f_op != &io_uring_fops)
6267 goto out_fput;
6268
6269 ret = -ENXIO;
6270 ctx = f.file->private_data;
6271 if (!percpu_ref_tryget(&ctx->refs))
6272 goto out_fput;
6273
Jens Axboe6c271ce2019-01-10 11:22:30 -07006274 /*
6275 * For SQ polling, the thread will do all submissions and completions.
6276 * Just return the requested submit count, and wake the thread if
6277 * we were asked to.
6278 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006279 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006280 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006281 if (!list_empty_careful(&ctx->cq_overflow_list))
6282 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006283 if (flags & IORING_ENTER_SQ_WAKEUP)
6284 wake_up(&ctx->sqo_wait);
6285 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006286 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006287 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006288
Jens Axboe44d28272020-01-16 19:00:24 -07006289 if (current->mm != ctx->sqo_mm ||
6290 current_cred() != ctx->creds) {
6291 ret = -EPERM;
6292 goto out;
6293 }
6294
Jens Axboe2b188cc2019-01-07 10:46:33 -07006295 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006296 /* already have mm, so io_submit_sqes() won't try to grab it */
6297 cur_mm = ctx->sqo_mm;
6298 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6299 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006300 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006301
6302 if (submitted != to_submit)
6303 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006304 }
6305 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006306 unsigned nr_events = 0;
6307
Jens Axboe2b188cc2019-01-07 10:46:33 -07006308 min_complete = min(min_complete, ctx->cq_entries);
6309
Jens Axboedef596e2019-01-09 08:59:42 -07006310 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006311 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006312 } else {
6313 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6314 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006315 }
6316
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006317out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006318 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006319out_fput:
6320 fdput(f);
6321 return submitted ? submitted : ret;
6322}
6323
6324static const struct file_operations io_uring_fops = {
6325 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006326 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006327 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006328#ifndef CONFIG_MMU
6329 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6330 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6331#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006332 .poll = io_uring_poll,
6333 .fasync = io_uring_fasync,
6334};
6335
6336static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6337 struct io_uring_params *p)
6338{
Hristo Venev75b28af2019-08-26 17:23:46 +00006339 struct io_rings *rings;
6340 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006341
Hristo Venev75b28af2019-08-26 17:23:46 +00006342 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6343 if (size == SIZE_MAX)
6344 return -EOVERFLOW;
6345
6346 rings = io_mem_alloc(size);
6347 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006348 return -ENOMEM;
6349
Hristo Venev75b28af2019-08-26 17:23:46 +00006350 ctx->rings = rings;
6351 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6352 rings->sq_ring_mask = p->sq_entries - 1;
6353 rings->cq_ring_mask = p->cq_entries - 1;
6354 rings->sq_ring_entries = p->sq_entries;
6355 rings->cq_ring_entries = p->cq_entries;
6356 ctx->sq_mask = rings->sq_ring_mask;
6357 ctx->cq_mask = rings->cq_ring_mask;
6358 ctx->sq_entries = rings->sq_ring_entries;
6359 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006360
6361 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006362 if (size == SIZE_MAX) {
6363 io_mem_free(ctx->rings);
6364 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006365 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006366 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006367
6368 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006369 if (!ctx->sq_sqes) {
6370 io_mem_free(ctx->rings);
6371 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006372 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006373 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006374
Jens Axboe2b188cc2019-01-07 10:46:33 -07006375 return 0;
6376}
6377
6378/*
6379 * Allocate an anonymous fd, this is what constitutes the application
6380 * visible backing of an io_uring instance. The application mmaps this
6381 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6382 * we have to tie this fd to a socket for file garbage collection purposes.
6383 */
6384static int io_uring_get_fd(struct io_ring_ctx *ctx)
6385{
6386 struct file *file;
6387 int ret;
6388
6389#if defined(CONFIG_UNIX)
6390 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6391 &ctx->ring_sock);
6392 if (ret)
6393 return ret;
6394#endif
6395
6396 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6397 if (ret < 0)
6398 goto err;
6399
6400 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6401 O_RDWR | O_CLOEXEC);
6402 if (IS_ERR(file)) {
6403 put_unused_fd(ret);
6404 ret = PTR_ERR(file);
6405 goto err;
6406 }
6407
6408#if defined(CONFIG_UNIX)
6409 ctx->ring_sock->file = file;
6410#endif
6411 fd_install(ret, file);
6412 return ret;
6413err:
6414#if defined(CONFIG_UNIX)
6415 sock_release(ctx->ring_sock);
6416 ctx->ring_sock = NULL;
6417#endif
6418 return ret;
6419}
6420
6421static int io_uring_create(unsigned entries, struct io_uring_params *p)
6422{
6423 struct user_struct *user = NULL;
6424 struct io_ring_ctx *ctx;
6425 bool account_mem;
6426 int ret;
6427
Jens Axboe8110c1a2019-12-28 15:39:54 -07006428 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006429 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006430 if (entries > IORING_MAX_ENTRIES) {
6431 if (!(p->flags & IORING_SETUP_CLAMP))
6432 return -EINVAL;
6433 entries = IORING_MAX_ENTRIES;
6434 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006435
6436 /*
6437 * Use twice as many entries for the CQ ring. It's possible for the
6438 * application to drive a higher depth than the size of the SQ ring,
6439 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006440 * some flexibility in overcommitting a bit. If the application has
6441 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6442 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006443 */
6444 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006445 if (p->flags & IORING_SETUP_CQSIZE) {
6446 /*
6447 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6448 * to a power-of-two, if it isn't already. We do NOT impose
6449 * any cq vs sq ring sizing.
6450 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006451 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006452 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006453 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6454 if (!(p->flags & IORING_SETUP_CLAMP))
6455 return -EINVAL;
6456 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6457 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006458 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6459 } else {
6460 p->cq_entries = 2 * p->sq_entries;
6461 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006462
6463 user = get_uid(current_user());
6464 account_mem = !capable(CAP_IPC_LOCK);
6465
6466 if (account_mem) {
6467 ret = io_account_mem(user,
6468 ring_pages(p->sq_entries, p->cq_entries));
6469 if (ret) {
6470 free_uid(user);
6471 return ret;
6472 }
6473 }
6474
6475 ctx = io_ring_ctx_alloc(p);
6476 if (!ctx) {
6477 if (account_mem)
6478 io_unaccount_mem(user, ring_pages(p->sq_entries,
6479 p->cq_entries));
6480 free_uid(user);
6481 return -ENOMEM;
6482 }
6483 ctx->compat = in_compat_syscall();
6484 ctx->account_mem = account_mem;
6485 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006486 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006487
6488 ret = io_allocate_scq_urings(ctx, p);
6489 if (ret)
6490 goto err;
6491
Jens Axboe6c271ce2019-01-10 11:22:30 -07006492 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006493 if (ret)
6494 goto err;
6495
Jens Axboe2b188cc2019-01-07 10:46:33 -07006496 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006497 p->sq_off.head = offsetof(struct io_rings, sq.head);
6498 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6499 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6500 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6501 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6502 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6503 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006504
6505 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006506 p->cq_off.head = offsetof(struct io_rings, cq.head);
6507 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6508 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6509 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6510 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6511 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006512
Jens Axboe044c1ab2019-10-28 09:15:33 -06006513 /*
6514 * Install ring fd as the very last thing, so we don't risk someone
6515 * having closed it before we finish setup
6516 */
6517 ret = io_uring_get_fd(ctx);
6518 if (ret < 0)
6519 goto err;
6520
Jens Axboeda8c9692019-12-02 18:51:26 -07006521 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboeba042912019-12-25 16:33:42 -07006522 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006523 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006524 return ret;
6525err:
6526 io_ring_ctx_wait_and_kill(ctx);
6527 return ret;
6528}
6529
6530/*
6531 * Sets up an aio uring context, and returns the fd. Applications asks for a
6532 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6533 * params structure passed in.
6534 */
6535static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6536{
6537 struct io_uring_params p;
6538 long ret;
6539 int i;
6540
6541 if (copy_from_user(&p, params, sizeof(p)))
6542 return -EFAULT;
6543 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6544 if (p.resv[i])
6545 return -EINVAL;
6546 }
6547
Jens Axboe6c271ce2019-01-10 11:22:30 -07006548 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006549 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
6550 IORING_SETUP_CLAMP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006551 return -EINVAL;
6552
6553 ret = io_uring_create(entries, &p);
6554 if (ret < 0)
6555 return ret;
6556
6557 if (copy_to_user(params, &p, sizeof(p)))
6558 return -EFAULT;
6559
6560 return ret;
6561}
6562
6563SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6564 struct io_uring_params __user *, params)
6565{
6566 return io_uring_setup(entries, params);
6567}
6568
Jens Axboeedafcce2019-01-09 09:16:05 -07006569static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6570 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006571 __releases(ctx->uring_lock)
6572 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006573{
6574 int ret;
6575
Jens Axboe35fa71a2019-04-22 10:23:23 -06006576 /*
6577 * We're inside the ring mutex, if the ref is already dying, then
6578 * someone else killed the ctx or is already going through
6579 * io_uring_register().
6580 */
6581 if (percpu_ref_is_dying(&ctx->refs))
6582 return -ENXIO;
6583
Jens Axboe05f3fb32019-12-09 11:22:50 -07006584 if (opcode != IORING_UNREGISTER_FILES &&
6585 opcode != IORING_REGISTER_FILES_UPDATE) {
6586 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006587
Jens Axboe05f3fb32019-12-09 11:22:50 -07006588 /*
6589 * Drop uring mutex before waiting for references to exit. If
6590 * another thread is currently inside io_uring_enter() it might
6591 * need to grab the uring_lock to make progress. If we hold it
6592 * here across the drain wait, then we can deadlock. It's safe
6593 * to drop the mutex here, since no new references will come in
6594 * after we've killed the percpu ref.
6595 */
6596 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006597 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006598 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006599 if (ret) {
6600 percpu_ref_resurrect(&ctx->refs);
6601 ret = -EINTR;
6602 goto out;
6603 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006604 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006605
6606 switch (opcode) {
6607 case IORING_REGISTER_BUFFERS:
6608 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6609 break;
6610 case IORING_UNREGISTER_BUFFERS:
6611 ret = -EINVAL;
6612 if (arg || nr_args)
6613 break;
6614 ret = io_sqe_buffer_unregister(ctx);
6615 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006616 case IORING_REGISTER_FILES:
6617 ret = io_sqe_files_register(ctx, arg, nr_args);
6618 break;
6619 case IORING_UNREGISTER_FILES:
6620 ret = -EINVAL;
6621 if (arg || nr_args)
6622 break;
6623 ret = io_sqe_files_unregister(ctx);
6624 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006625 case IORING_REGISTER_FILES_UPDATE:
6626 ret = io_sqe_files_update(ctx, arg, nr_args);
6627 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006628 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006629 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006630 ret = -EINVAL;
6631 if (nr_args != 1)
6632 break;
6633 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006634 if (ret)
6635 break;
6636 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6637 ctx->eventfd_async = 1;
6638 else
6639 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006640 break;
6641 case IORING_UNREGISTER_EVENTFD:
6642 ret = -EINVAL;
6643 if (arg || nr_args)
6644 break;
6645 ret = io_eventfd_unregister(ctx);
6646 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006647 default:
6648 ret = -EINVAL;
6649 break;
6650 }
6651
Jens Axboe05f3fb32019-12-09 11:22:50 -07006652
6653 if (opcode != IORING_UNREGISTER_FILES &&
6654 opcode != IORING_REGISTER_FILES_UPDATE) {
6655 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006656 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07006657out:
6658 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006659 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006660 return ret;
6661}
6662
6663SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6664 void __user *, arg, unsigned int, nr_args)
6665{
6666 struct io_ring_ctx *ctx;
6667 long ret = -EBADF;
6668 struct fd f;
6669
6670 f = fdget(fd);
6671 if (!f.file)
6672 return -EBADF;
6673
6674 ret = -EOPNOTSUPP;
6675 if (f.file->f_op != &io_uring_fops)
6676 goto out_fput;
6677
6678 ctx = f.file->private_data;
6679
6680 mutex_lock(&ctx->uring_lock);
6681 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6682 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006683 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6684 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006685out_fput:
6686 fdput(f);
6687 return ret;
6688}
6689
Jens Axboe2b188cc2019-01-07 10:46:33 -07006690static int __init io_uring_init(void)
6691{
Jens Axboed3656342019-12-18 09:50:26 -07006692 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006693 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6694 return 0;
6695};
6696__initcall(io_uring_init);