blob: 1ef20a2af10b03fae7fefb3e309d23197f4b73c2 [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>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030049#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070050
51#include <linux/sched/signal.h>
52#include <linux/fs.h>
53#include <linux/file.h>
54#include <linux/fdtable.h>
55#include <linux/mm.h>
56#include <linux/mman.h>
57#include <linux/mmu_context.h>
58#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Pavel Begunkov7d67af22020-02-24 11:32:45 +030079#include <linux/splice.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070080
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020081#define CREATE_TRACE_POINTS
82#include <trace/events/io_uring.h>
83
Jens Axboe2b188cc2019-01-07 10:46:33 -070084#include <uapi/linux/io_uring.h>
85
86#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060087#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070088
Daniel Xu5277dea2019-09-14 14:23:45 -070089#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060090#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060091
92/*
93 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
94 */
95#define IORING_FILE_TABLE_SHIFT 9
96#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
97#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
98#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070099
100struct io_uring {
101 u32 head ____cacheline_aligned_in_smp;
102 u32 tail ____cacheline_aligned_in_smp;
103};
104
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000106 * This data is shared with the application through the mmap at offsets
107 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200108 *
109 * The offsets to the member fields are published through struct
110 * io_sqring_offsets when calling io_uring_setup.
111 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000112struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200113 /*
114 * Head and tail offsets into the ring; the offsets need to be
115 * masked to get valid indices.
116 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000117 * The kernel controls head of the sq ring and the tail of the cq ring,
118 * and the application controls tail of the sq ring and the head of the
119 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000121 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000123 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 * ring_entries - 1)
125 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000126 u32 sq_ring_mask, cq_ring_mask;
127 /* Ring sizes (constant, power of 2) */
128 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200129 /*
130 * Number of invalid entries dropped by the kernel due to
131 * invalid index stored in array
132 *
133 * Written by the kernel, shouldn't be modified by the
134 * application (i.e. get number of "new events" by comparing to
135 * cached value).
136 *
137 * After a new SQ head value was read by the application this
138 * counter includes all submissions that were dropped reaching
139 * the new SQ head (and possibly more).
140 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000141 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200142 /*
143 * Runtime flags
144 *
145 * Written by the kernel, shouldn't be modified by the
146 * application.
147 *
148 * The application needs a full memory barrier before checking
149 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
150 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000151 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200152 /*
153 * Number of completion events lost because the queue was full;
154 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800155 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200156 * the completion queue.
157 *
158 * Written by the kernel, shouldn't be modified by the
159 * application (i.e. get number of "new events" by comparing to
160 * cached value).
161 *
162 * As completion events come in out of order this counter is not
163 * ordered with any other data.
164 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000165 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200166 /*
167 * Ring buffer of completion events.
168 *
169 * The kernel writes completion events fresh every time they are
170 * produced, so the application is allowed to modify pending
171 * entries.
172 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000173 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700174};
175
Jens Axboeedafcce2019-01-09 09:16:05 -0700176struct io_mapped_ubuf {
177 u64 ubuf;
178 size_t len;
179 struct bio_vec *bvec;
180 unsigned int nr_bvecs;
181};
182
Jens Axboe65e19f52019-10-26 07:20:21 -0600183struct fixed_file_table {
184 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700185};
186
Jens Axboe05f3fb32019-12-09 11:22:50 -0700187struct 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;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700193 struct work_struct ref_work;
194 struct completion done;
195};
196
Jens Axboe2b188cc2019-01-07 10:46:33 -0700197struct io_ring_ctx {
198 struct {
199 struct percpu_ref refs;
200 } ____cacheline_aligned_in_smp;
201
202 struct {
203 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800204 unsigned int compat: 1;
205 unsigned int account_mem: 1;
206 unsigned int cq_overflow_flushed: 1;
207 unsigned int drain_next: 1;
208 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700209
Hristo Venev75b28af2019-08-26 17:23:46 +0000210 /*
211 * Ring buffer of indices into array of io_uring_sqe, which is
212 * mmapped by the application using the IORING_OFF_SQES offset.
213 *
214 * This indirection could e.g. be used to assign fixed
215 * io_uring_sqe entries to operations and only submit them to
216 * the queue when needed.
217 *
218 * The kernel modifies neither the indices array nor the entries
219 * array.
220 */
221 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222 unsigned cached_sq_head;
223 unsigned sq_entries;
224 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700225 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600226 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700227 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700228 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600229
230 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600231 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700232 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700233
Jens Axboefcb323c2019-10-24 12:39:47 -0600234 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700235 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700236 } ____cacheline_aligned_in_smp;
237
Hristo Venev75b28af2019-08-26 17:23:46 +0000238 struct io_rings *rings;
239
Jens Axboe2b188cc2019-01-07 10:46:33 -0700240 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600241 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700242 struct task_struct *sqo_thread; /* if using sq thread polling */
243 struct mm_struct *sqo_mm;
244 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700245
Jens Axboe6b063142019-01-10 22:13:58 -0700246 /*
247 * If used, fixed file set. Writers must ensure that ->refs is dead,
248 * readers must ensure that ->refs is alive as long as the file* is
249 * used. Only updated through io_uring_register(2).
250 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700251 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700252 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300253 int ring_fd;
254 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700255
Jens Axboeedafcce2019-01-09 09:16:05 -0700256 /* if used, fixed mapped user buffers */
257 unsigned nr_user_bufs;
258 struct io_mapped_ubuf *user_bufs;
259
Jens Axboe2b188cc2019-01-07 10:46:33 -0700260 struct user_struct *user;
261
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700262 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700263
Jens Axboe206aefd2019-11-07 18:27:42 -0700264 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
265 struct completion *completions;
266
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700267 /* if all else fails... */
268 struct io_kiocb *fallback_req;
269
Jens Axboe206aefd2019-11-07 18:27:42 -0700270#if defined(CONFIG_UNIX)
271 struct socket *ring_sock;
272#endif
273
Jens Axboe071698e2020-01-28 10:04:42 -0700274 struct idr personality_idr;
275
Jens Axboe206aefd2019-11-07 18:27:42 -0700276 struct {
277 unsigned cached_cq_tail;
278 unsigned cq_entries;
279 unsigned cq_mask;
280 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700281 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700282 struct wait_queue_head cq_wait;
283 struct fasync_struct *cq_fasync;
284 struct eventfd_ctx *cq_ev_fd;
285 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700286
287 struct {
288 struct mutex uring_lock;
289 wait_queue_head_t wait;
290 } ____cacheline_aligned_in_smp;
291
292 struct {
293 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700294 struct llist_head poll_llist;
295
Jens Axboedef596e2019-01-09 08:59:42 -0700296 /*
297 * ->poll_list is protected by the ctx->uring_lock for
298 * io_uring instances that don't use IORING_SETUP_SQPOLL.
299 * For SQPOLL, only the single threaded io_sq_thread() will
300 * manipulate the list, hence no extra locking is needed there.
301 */
302 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700303 struct hlist_head *cancel_hash;
304 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700305 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600306
307 spinlock_t inflight_lock;
308 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700309 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700310};
311
Jens Axboe09bb8392019-03-13 12:39:28 -0600312/*
313 * First field must be the file pointer in all the
314 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
315 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700316struct io_poll_iocb {
317 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700318 union {
319 struct wait_queue_head *head;
320 u64 addr;
321 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700322 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600323 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700324 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700325 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700326};
327
Jens Axboeb5dba592019-12-11 14:02:38 -0700328struct io_close {
329 struct file *file;
330 struct file *put_file;
331 int fd;
332};
333
Jens Axboead8a48a2019-11-15 08:49:11 -0700334struct io_timeout_data {
335 struct io_kiocb *req;
336 struct hrtimer timer;
337 struct timespec64 ts;
338 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300339 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700340};
341
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700342struct io_accept {
343 struct file *file;
344 struct sockaddr __user *addr;
345 int __user *addr_len;
346 int flags;
347};
348
349struct io_sync {
350 struct file *file;
351 loff_t len;
352 loff_t off;
353 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700354 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700355};
356
Jens Axboefbf23842019-12-17 18:45:56 -0700357struct io_cancel {
358 struct file *file;
359 u64 addr;
360};
361
Jens Axboeb29472e2019-12-17 18:50:29 -0700362struct io_timeout {
363 struct file *file;
364 u64 addr;
365 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700366 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700367};
368
Jens Axboe9adbd452019-12-20 08:45:55 -0700369struct io_rw {
370 /* NOTE: kiocb has the file as the first member, so don't do it here */
371 struct kiocb kiocb;
372 u64 addr;
373 u64 len;
374};
375
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700376struct io_connect {
377 struct file *file;
378 struct sockaddr __user *addr;
379 int addr_len;
380};
381
Jens Axboee47293f2019-12-20 08:58:21 -0700382struct io_sr_msg {
383 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700384 union {
385 struct user_msghdr __user *msg;
386 void __user *buf;
387 };
Jens Axboee47293f2019-12-20 08:58:21 -0700388 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700389 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700390};
391
Jens Axboe15b71ab2019-12-11 11:20:36 -0700392struct io_open {
393 struct file *file;
394 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700395 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700396 unsigned mask;
397 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700398 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700399 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700400 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700401};
402
Jens Axboe05f3fb32019-12-09 11:22:50 -0700403struct io_files_update {
404 struct file *file;
405 u64 arg;
406 u32 nr_args;
407 u32 offset;
408};
409
Jens Axboe4840e412019-12-25 22:03:45 -0700410struct io_fadvise {
411 struct file *file;
412 u64 offset;
413 u32 len;
414 u32 advice;
415};
416
Jens Axboec1ca7572019-12-25 22:18:28 -0700417struct io_madvise {
418 struct file *file;
419 u64 addr;
420 u32 len;
421 u32 advice;
422};
423
Jens Axboe3e4827b2020-01-08 15:18:09 -0700424struct io_epoll {
425 struct file *file;
426 int epfd;
427 int op;
428 int fd;
429 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700430};
431
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300432struct io_splice {
433 struct file *file_out;
434 struct file *file_in;
435 loff_t off_out;
436 loff_t off_in;
437 u64 len;
438 unsigned int flags;
439};
440
Jens Axboef499a022019-12-02 16:28:46 -0700441struct io_async_connect {
442 struct sockaddr_storage address;
443};
444
Jens Axboe03b12302019-12-02 18:50:25 -0700445struct io_async_msghdr {
446 struct iovec fast_iov[UIO_FASTIOV];
447 struct iovec *iov;
448 struct sockaddr __user *uaddr;
449 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700450 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700451};
452
Jens Axboef67676d2019-12-02 11:03:47 -0700453struct io_async_rw {
454 struct iovec fast_iov[UIO_FASTIOV];
455 struct iovec *iov;
456 ssize_t nr_segs;
457 ssize_t size;
458};
459
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700460struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700461 union {
462 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700463 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700464 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700465 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700466 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700467};
468
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300469enum {
470 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
471 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
472 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
473 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
474 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
475
476 REQ_F_LINK_NEXT_BIT,
477 REQ_F_FAIL_LINK_BIT,
478 REQ_F_INFLIGHT_BIT,
479 REQ_F_CUR_POS_BIT,
480 REQ_F_NOWAIT_BIT,
481 REQ_F_IOPOLL_COMPLETED_BIT,
482 REQ_F_LINK_TIMEOUT_BIT,
483 REQ_F_TIMEOUT_BIT,
484 REQ_F_ISREG_BIT,
485 REQ_F_MUST_PUNT_BIT,
486 REQ_F_TIMEOUT_NOSEQ_BIT,
487 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300488 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700489 REQ_F_OVERFLOW_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300490};
491
492enum {
493 /* ctx owns file */
494 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
495 /* drain existing IO first */
496 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
497 /* linked sqes */
498 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
499 /* doesn't sever on completion < 0 */
500 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
501 /* IOSQE_ASYNC */
502 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
503
504 /* already grabbed next link */
505 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
506 /* fail rest of links */
507 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
508 /* on inflight list */
509 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
510 /* read/write uses file position */
511 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
512 /* must not punt to workers */
513 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
514 /* polled IO has completed */
515 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
516 /* has linked timeout */
517 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
518 /* timeout request */
519 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
520 /* regular file */
521 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
522 /* must be punted even for NONBLOCK */
523 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
524 /* no timeout sequence */
525 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
526 /* completion under lock */
527 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300528 /* needs cleanup */
529 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700530 /* in overflow list */
531 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300532};
533
Jens Axboe09bb8392019-03-13 12:39:28 -0600534/*
535 * NOTE! Each of the iocb union members has the file pointer
536 * as the first entry in their struct definition. So you can
537 * access the file pointer through any of the sub-structs,
538 * or directly as just 'ki_filp' in this struct.
539 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700540struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700541 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600542 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700543 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700544 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700545 struct io_accept accept;
546 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700547 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700548 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700549 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700550 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700551 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700552 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700553 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700554 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700555 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700556 struct io_epoll epoll;
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300557 struct io_splice splice;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700558 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700559
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700560 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300561 /*
562 * llist_node is only used for poll deferred completions
563 */
564 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300565 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700566 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700567
568 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700569 union {
570 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700571 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700572 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600573 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700574 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700575 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700576 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600577 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600578 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700579
Jens Axboefcb323c2019-10-24 12:39:47 -0600580 struct list_head inflight_entry;
581
Jens Axboe561fb042019-10-24 07:25:42 -0600582 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700583};
584
585#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700586#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700587
Jens Axboe9a56a232019-01-09 09:06:50 -0700588struct io_submit_state {
589 struct blk_plug plug;
590
591 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700592 * io_kiocb alloc cache
593 */
594 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300595 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700596
597 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700598 * File reference cache
599 */
600 struct file *file;
601 unsigned int fd;
602 unsigned int has_refs;
603 unsigned int used_refs;
604 unsigned int ios_left;
605};
606
Jens Axboed3656342019-12-18 09:50:26 -0700607struct io_op_def {
608 /* needs req->io allocated for deferral/async */
609 unsigned async_ctx : 1;
610 /* needs current->mm setup, does mm access */
611 unsigned needs_mm : 1;
612 /* needs req->file assigned */
613 unsigned needs_file : 1;
614 /* needs req->file assigned IFF fd is >= 0 */
615 unsigned fd_non_neg : 1;
616 /* hash wq insertion if file is a regular file */
617 unsigned hash_reg_file : 1;
618 /* unbound wq insertion if file is a non-regular file */
619 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700620 /* opcode is not supported by this kernel */
621 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700622 /* needs file table */
623 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700624 /* needs ->fs */
625 unsigned needs_fs : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700626};
627
628static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300629 [IORING_OP_NOP] = {},
630 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700631 .async_ctx = 1,
632 .needs_mm = 1,
633 .needs_file = 1,
634 .unbound_nonreg_file = 1,
635 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300636 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700637 .async_ctx = 1,
638 .needs_mm = 1,
639 .needs_file = 1,
640 .hash_reg_file = 1,
641 .unbound_nonreg_file = 1,
642 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300643 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700644 .needs_file = 1,
645 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300646 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700647 .needs_file = 1,
648 .unbound_nonreg_file = 1,
649 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300650 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700651 .needs_file = 1,
652 .hash_reg_file = 1,
653 .unbound_nonreg_file = 1,
654 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300655 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700656 .needs_file = 1,
657 .unbound_nonreg_file = 1,
658 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300659 [IORING_OP_POLL_REMOVE] = {},
660 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700661 .needs_file = 1,
662 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300663 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700664 .async_ctx = 1,
665 .needs_mm = 1,
666 .needs_file = 1,
667 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700668 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700669 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300670 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700671 .async_ctx = 1,
672 .needs_mm = 1,
673 .needs_file = 1,
674 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700675 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700676 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300677 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700678 .async_ctx = 1,
679 .needs_mm = 1,
680 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300681 [IORING_OP_TIMEOUT_REMOVE] = {},
682 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700683 .needs_mm = 1,
684 .needs_file = 1,
685 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700686 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700687 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300688 [IORING_OP_ASYNC_CANCEL] = {},
689 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700690 .async_ctx = 1,
691 .needs_mm = 1,
692 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300693 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700694 .async_ctx = 1,
695 .needs_mm = 1,
696 .needs_file = 1,
697 .unbound_nonreg_file = 1,
698 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300699 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700700 .needs_file = 1,
701 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300702 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700703 .needs_file = 1,
704 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700705 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700706 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700707 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300708 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700709 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700710 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700711 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300712 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700713 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700714 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700715 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300716 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700717 .needs_mm = 1,
718 .needs_file = 1,
719 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700720 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700721 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300722 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700723 .needs_mm = 1,
724 .needs_file = 1,
725 .unbound_nonreg_file = 1,
726 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300727 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700728 .needs_mm = 1,
729 .needs_file = 1,
730 .unbound_nonreg_file = 1,
731 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300732 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700733 .needs_file = 1,
734 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300735 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700736 .needs_mm = 1,
737 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300738 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700739 .needs_mm = 1,
740 .needs_file = 1,
741 .unbound_nonreg_file = 1,
742 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300743 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700744 .needs_mm = 1,
745 .needs_file = 1,
746 .unbound_nonreg_file = 1,
747 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300748 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700749 .needs_file = 1,
750 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700751 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700752 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700753 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700754 [IORING_OP_EPOLL_CTL] = {
755 .unbound_nonreg_file = 1,
756 .file_table = 1,
757 },
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300758 [IORING_OP_SPLICE] = {
759 .needs_file = 1,
760 .hash_reg_file = 1,
761 .unbound_nonreg_file = 1,
762 }
Jens Axboed3656342019-12-18 09:50:26 -0700763};
764
Jens Axboe561fb042019-10-24 07:25:42 -0600765static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700766static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800767static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700768static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700769static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
770static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700771static int __io_sqe_files_update(struct io_ring_ctx *ctx,
772 struct io_uring_files_update *ip,
773 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700774static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700775static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300776static void io_cleanup_req(struct io_kiocb *req);
Pavel Begunkov7d67af22020-02-24 11:32:45 +0300777static int io_file_get(struct io_submit_state *state,
778 struct io_kiocb *req,
779 int fd, struct file **out_file,
780 bool fixed);
Jens Axboede0617e2019-04-06 21:51:27 -0600781
Jens Axboe2b188cc2019-01-07 10:46:33 -0700782static struct kmem_cache *req_cachep;
783
784static const struct file_operations io_uring_fops;
785
786struct sock *io_uring_get_socket(struct file *file)
787{
788#if defined(CONFIG_UNIX)
789 if (file->f_op == &io_uring_fops) {
790 struct io_ring_ctx *ctx = file->private_data;
791
792 return ctx->ring_sock->sk;
793 }
794#endif
795 return NULL;
796}
797EXPORT_SYMBOL(io_uring_get_socket);
798
799static void io_ring_ctx_ref_free(struct percpu_ref *ref)
800{
801 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
802
Jens Axboe206aefd2019-11-07 18:27:42 -0700803 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700804}
805
806static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
807{
808 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700809 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700810
811 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
812 if (!ctx)
813 return NULL;
814
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700815 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
816 if (!ctx->fallback_req)
817 goto err;
818
Jens Axboe206aefd2019-11-07 18:27:42 -0700819 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
820 if (!ctx->completions)
821 goto err;
822
Jens Axboe78076bb2019-12-04 19:56:40 -0700823 /*
824 * Use 5 bits less than the max cq entries, that should give us around
825 * 32 entries per hash list if totally full and uniformly spread.
826 */
827 hash_bits = ilog2(p->cq_entries);
828 hash_bits -= 5;
829 if (hash_bits <= 0)
830 hash_bits = 1;
831 ctx->cancel_hash_bits = hash_bits;
832 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
833 GFP_KERNEL);
834 if (!ctx->cancel_hash)
835 goto err;
836 __hash_init(ctx->cancel_hash, 1U << hash_bits);
837
Roman Gushchin21482892019-05-07 10:01:48 -0700838 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700839 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
840 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700841
842 ctx->flags = p->flags;
843 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700844 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700845 init_completion(&ctx->completions[0]);
846 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700847 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700848 mutex_init(&ctx->uring_lock);
849 init_waitqueue_head(&ctx->wait);
850 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700851 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700852 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600853 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600854 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600855 init_waitqueue_head(&ctx->inflight_wait);
856 spin_lock_init(&ctx->inflight_lock);
857 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700858 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700859err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700860 if (ctx->fallback_req)
861 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700862 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700863 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700864 kfree(ctx);
865 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700866}
867
Bob Liu9d858b22019-11-13 18:06:25 +0800868static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600869{
Jackie Liua197f662019-11-08 08:09:12 -0700870 struct io_ring_ctx *ctx = req->ctx;
871
Jens Axboe498ccd92019-10-25 10:04:25 -0600872 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
873 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600874}
875
Bob Liu9d858b22019-11-13 18:06:25 +0800876static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600877{
Pavel Begunkov87987892020-01-18 01:22:30 +0300878 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800879 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600880
Bob Liu9d858b22019-11-13 18:06:25 +0800881 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600882}
883
884static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600885{
886 struct io_kiocb *req;
887
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600888 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800889 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600890 list_del_init(&req->list);
891 return req;
892 }
893
894 return NULL;
895}
896
Jens Axboe5262f562019-09-17 12:26:57 -0600897static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
898{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600899 struct io_kiocb *req;
900
901 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700902 if (req) {
903 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
904 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800905 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700906 list_del_init(&req->list);
907 return req;
908 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600909 }
910
911 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600912}
913
Jens Axboede0617e2019-04-06 21:51:27 -0600914static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700915{
Hristo Venev75b28af2019-08-26 17:23:46 +0000916 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700917
Pavel Begunkov07910152020-01-17 03:52:46 +0300918 /* order cqe stores with ring update */
919 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700920
Pavel Begunkov07910152020-01-17 03:52:46 +0300921 if (wq_has_sleeper(&ctx->cq_wait)) {
922 wake_up_interruptible(&ctx->cq_wait);
923 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700924 }
925}
926
Jens Axboecccf0ee2020-01-27 16:34:48 -0700927static inline void io_req_work_grab_env(struct io_kiocb *req,
928 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600929{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700930 if (!req->work.mm && def->needs_mm) {
931 mmgrab(current->mm);
932 req->work.mm = current->mm;
933 }
934 if (!req->work.creds)
935 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700936 if (!req->work.fs && def->needs_fs) {
937 spin_lock(&current->fs->lock);
938 if (!current->fs->in_exec) {
939 req->work.fs = current->fs;
940 req->work.fs->users++;
941 } else {
942 req->work.flags |= IO_WQ_WORK_CANCEL;
943 }
944 spin_unlock(&current->fs->lock);
945 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700946 if (!req->work.task_pid)
947 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700948}
949
950static inline void io_req_work_drop_env(struct io_kiocb *req)
951{
952 if (req->work.mm) {
953 mmdrop(req->work.mm);
954 req->work.mm = NULL;
955 }
956 if (req->work.creds) {
957 put_cred(req->work.creds);
958 req->work.creds = NULL;
959 }
Jens Axboeff002b32020-02-07 16:05:21 -0700960 if (req->work.fs) {
961 struct fs_struct *fs = req->work.fs;
962
963 spin_lock(&req->work.fs->lock);
964 if (--fs->users)
965 fs = NULL;
966 spin_unlock(&req->work.fs->lock);
967 if (fs)
968 free_fs_struct(fs);
969 }
Jens Axboe561fb042019-10-24 07:25:42 -0600970}
971
Pavel Begunkovdeb6dc02020-02-24 11:30:17 +0300972static inline void io_prep_next_work(struct io_kiocb *req,
973 struct io_kiocb **link)
974{
975 const struct io_op_def *def = &io_op_defs[req->opcode];
976
977 if (!(req->flags & REQ_F_ISREG) && def->unbound_nonreg_file)
978 req->work.flags |= IO_WQ_WORK_UNBOUND;
979
980 *link = io_prep_linked_timeout(req);
981}
982
Jens Axboe94ae5e72019-11-14 19:39:52 -0700983static inline bool io_prep_async_work(struct io_kiocb *req,
984 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600985{
Jens Axboed3656342019-12-18 09:50:26 -0700986 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600987 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600988
Jens Axboed3656342019-12-18 09:50:26 -0700989 if (req->flags & REQ_F_ISREG) {
990 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700991 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700992 } else {
993 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700994 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600995 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700996
997 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600998
Jens Axboe94ae5e72019-11-14 19:39:52 -0700999 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -06001000 return do_hashed;
1001}
1002
Jackie Liua197f662019-11-08 08:09:12 -07001003static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -06001004{
Jackie Liua197f662019-11-08 08:09:12 -07001005 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001006 struct io_kiocb *link;
1007 bool do_hashed;
1008
1009 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06001010
1011 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
1012 req->flags);
1013 if (!do_hashed) {
1014 io_wq_enqueue(ctx->io_wq, &req->work);
1015 } else {
1016 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
1017 file_inode(req->file));
1018 }
Jens Axboe94ae5e72019-11-14 19:39:52 -07001019
1020 if (link)
1021 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001022}
1023
Jens Axboe5262f562019-09-17 12:26:57 -06001024static void io_kill_timeout(struct io_kiocb *req)
1025{
1026 int ret;
1027
Jens Axboe2d283902019-12-04 11:08:05 -07001028 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001029 if (ret != -1) {
1030 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001031 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001032 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001033 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001034 }
1035}
1036
1037static void io_kill_timeouts(struct io_ring_ctx *ctx)
1038{
1039 struct io_kiocb *req, *tmp;
1040
1041 spin_lock_irq(&ctx->completion_lock);
1042 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1043 io_kill_timeout(req);
1044 spin_unlock_irq(&ctx->completion_lock);
1045}
1046
Jens Axboede0617e2019-04-06 21:51:27 -06001047static void io_commit_cqring(struct io_ring_ctx *ctx)
1048{
1049 struct io_kiocb *req;
1050
Jens Axboe5262f562019-09-17 12:26:57 -06001051 while ((req = io_get_timeout_req(ctx)) != NULL)
1052 io_kill_timeout(req);
1053
Jens Axboede0617e2019-04-06 21:51:27 -06001054 __io_commit_cqring(ctx);
1055
Pavel Begunkov87987892020-01-18 01:22:30 +03001056 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001057 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001058}
1059
Jens Axboe2b188cc2019-01-07 10:46:33 -07001060static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1061{
Hristo Venev75b28af2019-08-26 17:23:46 +00001062 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001063 unsigned tail;
1064
1065 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001066 /*
1067 * writes to the cq entry need to come after reading head; the
1068 * control dependency is enough as we're using WRITE_ONCE to
1069 * fill the cq entry
1070 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001071 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001072 return NULL;
1073
1074 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001075 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001076}
1077
Jens Axboef2842ab2020-01-08 11:04:00 -07001078static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1079{
Jens Axboef0b493e2020-02-01 21:30:11 -07001080 if (!ctx->cq_ev_fd)
1081 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001082 if (!ctx->eventfd_async)
1083 return true;
1084 return io_wq_current_is_worker() || in_interrupt();
1085}
1086
Jens Axboef0b493e2020-02-01 21:30:11 -07001087static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001088{
1089 if (waitqueue_active(&ctx->wait))
1090 wake_up(&ctx->wait);
1091 if (waitqueue_active(&ctx->sqo_wait))
1092 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001093 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001094 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001095}
1096
Jens Axboef0b493e2020-02-01 21:30:11 -07001097static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1098{
1099 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1100}
1101
Jens Axboec4a2ed72019-11-21 21:01:26 -07001102/* Returns true if there are no backlogged entries after the flush */
1103static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001104{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001105 struct io_rings *rings = ctx->rings;
1106 struct io_uring_cqe *cqe;
1107 struct io_kiocb *req;
1108 unsigned long flags;
1109 LIST_HEAD(list);
1110
1111 if (!force) {
1112 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001113 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001114 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1115 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001116 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001117 }
1118
1119 spin_lock_irqsave(&ctx->completion_lock, flags);
1120
1121 /* if force is set, the ring is going away. always drop after that */
1122 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001123 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001124
Jens Axboec4a2ed72019-11-21 21:01:26 -07001125 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001126 while (!list_empty(&ctx->cq_overflow_list)) {
1127 cqe = io_get_cqring(ctx);
1128 if (!cqe && !force)
1129 break;
1130
1131 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1132 list);
1133 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001134 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001135 if (cqe) {
1136 WRITE_ONCE(cqe->user_data, req->user_data);
1137 WRITE_ONCE(cqe->res, req->result);
1138 WRITE_ONCE(cqe->flags, 0);
1139 } else {
1140 WRITE_ONCE(ctx->rings->cq_overflow,
1141 atomic_inc_return(&ctx->cached_cq_overflow));
1142 }
1143 }
1144
1145 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001146 if (cqe) {
1147 clear_bit(0, &ctx->sq_check_overflow);
1148 clear_bit(0, &ctx->cq_check_overflow);
1149 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001150 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1151 io_cqring_ev_posted(ctx);
1152
1153 while (!list_empty(&list)) {
1154 req = list_first_entry(&list, struct io_kiocb, list);
1155 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001156 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001157 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001158
1159 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001160}
1161
Jens Axboe78e19bb2019-11-06 15:21:34 -07001162static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001163{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001164 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001165 struct io_uring_cqe *cqe;
1166
Jens Axboe78e19bb2019-11-06 15:21:34 -07001167 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001168
Jens Axboe2b188cc2019-01-07 10:46:33 -07001169 /*
1170 * If we can't get a cq entry, userspace overflowed the
1171 * submission (by quite a lot). Increment the overflow count in
1172 * the ring.
1173 */
1174 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001175 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001176 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177 WRITE_ONCE(cqe->res, res);
1178 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001179 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001180 WRITE_ONCE(ctx->rings->cq_overflow,
1181 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001182 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001183 if (list_empty(&ctx->cq_overflow_list)) {
1184 set_bit(0, &ctx->sq_check_overflow);
1185 set_bit(0, &ctx->cq_check_overflow);
1186 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001187 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001188 refcount_inc(&req->refs);
1189 req->result = res;
1190 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001191 }
1192}
1193
Jens Axboe78e19bb2019-11-06 15:21:34 -07001194static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001195{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001196 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001197 unsigned long flags;
1198
1199 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001200 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001201 io_commit_cqring(ctx);
1202 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1203
Jens Axboe8c838782019-03-12 15:48:16 -06001204 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001205}
1206
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001207static inline bool io_is_fallback_req(struct io_kiocb *req)
1208{
1209 return req == (struct io_kiocb *)
1210 ((unsigned long) req->ctx->fallback_req & ~1UL);
1211}
1212
1213static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1214{
1215 struct io_kiocb *req;
1216
1217 req = ctx->fallback_req;
1218 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1219 return req;
1220
1221 return NULL;
1222}
1223
Jens Axboe2579f912019-01-09 09:10:43 -07001224static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1225 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001226{
Jens Axboefd6fab22019-03-14 16:30:06 -06001227 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001228 struct io_kiocb *req;
1229
Jens Axboe2579f912019-01-09 09:10:43 -07001230 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001231 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001232 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001233 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001234 } else if (!state->free_reqs) {
1235 size_t sz;
1236 int ret;
1237
1238 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001239 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1240
1241 /*
1242 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1243 * retry single alloc to be on the safe side.
1244 */
1245 if (unlikely(ret <= 0)) {
1246 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1247 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001248 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001249 ret = 1;
1250 }
Jens Axboe2579f912019-01-09 09:10:43 -07001251 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001252 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001253 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001254 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001255 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001256 }
1257
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001258got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001259 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001260 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001261 req->ctx = ctx;
1262 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001263 /* one is dropped after submission, the other at completion */
1264 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001265 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001266 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001267 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001268fallback:
1269 req = io_get_fallback_req(ctx);
1270 if (req)
1271 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001272 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001273 return NULL;
1274}
1275
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001276static inline void io_put_file(struct io_kiocb *req, struct file *file,
1277 bool fixed)
1278{
1279 if (fixed)
1280 percpu_ref_put(&req->ctx->file_data->refs);
1281 else
1282 fput(file);
1283}
1284
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001285static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001286{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001287 if (likely(!io_is_fallback_req(req)))
1288 kmem_cache_free(req_cachep, req);
1289 else
1290 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1291}
1292
Jens Axboec6ca97b302019-12-28 12:11:08 -07001293static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001294{
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001295 if (req->flags & REQ_F_NEED_CLEANUP)
1296 io_cleanup_req(req);
1297
YueHaibing96fd84d2020-01-07 22:22:44 +08001298 kfree(req->io);
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001299 if (req->file)
1300 io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
Jens Axboecccf0ee2020-01-27 16:34:48 -07001301
1302 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001303}
1304
1305static void __io_free_req(struct io_kiocb *req)
1306{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001307 __io_req_aux_free(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06001308
Jens Axboefcb323c2019-10-24 12:39:47 -06001309 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001310 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001311 unsigned long flags;
1312
1313 spin_lock_irqsave(&ctx->inflight_lock, flags);
1314 list_del(&req->inflight_entry);
1315 if (waitqueue_active(&ctx->inflight_wait))
1316 wake_up(&ctx->inflight_wait);
1317 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1318 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001319
1320 percpu_ref_put(&req->ctx->refs);
1321 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001322}
1323
Jens Axboec6ca97b302019-12-28 12:11:08 -07001324struct req_batch {
1325 void *reqs[IO_IOPOLL_BATCH];
1326 int to_free;
1327 int need_iter;
1328};
1329
1330static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1331{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001332 int fixed_refs = rb->to_free;
1333
Jens Axboec6ca97b302019-12-28 12:11:08 -07001334 if (!rb->to_free)
1335 return;
1336 if (rb->need_iter) {
1337 int i, inflight = 0;
1338 unsigned long flags;
1339
Jens Axboe10fef4b2020-01-09 07:52:28 -07001340 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001341 for (i = 0; i < rb->to_free; i++) {
1342 struct io_kiocb *req = rb->reqs[i];
1343
Jens Axboe10fef4b2020-01-09 07:52:28 -07001344 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001345 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001346 fixed_refs++;
1347 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001348 if (req->flags & REQ_F_INFLIGHT)
1349 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001350 __io_req_aux_free(req);
1351 }
1352 if (!inflight)
1353 goto do_free;
1354
1355 spin_lock_irqsave(&ctx->inflight_lock, flags);
1356 for (i = 0; i < rb->to_free; i++) {
1357 struct io_kiocb *req = rb->reqs[i];
1358
Jens Axboe10fef4b2020-01-09 07:52:28 -07001359 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001360 list_del(&req->inflight_entry);
1361 if (!--inflight)
1362 break;
1363 }
1364 }
1365 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1366
1367 if (waitqueue_active(&ctx->inflight_wait))
1368 wake_up(&ctx->inflight_wait);
1369 }
1370do_free:
1371 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001372 if (fixed_refs)
1373 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001374 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001375 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001376}
1377
Jackie Liua197f662019-11-08 08:09:12 -07001378static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001379{
Jackie Liua197f662019-11-08 08:09:12 -07001380 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001381 int ret;
1382
Jens Axboe2d283902019-12-04 11:08:05 -07001383 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001384 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001385 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001386 io_commit_cqring(ctx);
1387 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001388 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001389 return true;
1390 }
1391
1392 return false;
1393}
1394
Jens Axboeba816ad2019-09-28 11:36:45 -06001395static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001396{
Jens Axboe2665abf2019-11-05 12:40:47 -07001397 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001398 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001399
Jens Axboe4d7dd462019-11-20 13:03:52 -07001400 /* Already got next link */
1401 if (req->flags & REQ_F_LINK_NEXT)
1402 return;
1403
Jens Axboe9e645e112019-05-10 16:07:28 -06001404 /*
1405 * The list should never be empty when we are called here. But could
1406 * potentially happen if the chain is messed up, check to be on the
1407 * safe side.
1408 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001409 while (!list_empty(&req->link_list)) {
1410 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1411 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001412
Pavel Begunkov44932332019-12-05 16:16:35 +03001413 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1414 (nxt->flags & REQ_F_TIMEOUT))) {
1415 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001416 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001417 req->flags &= ~REQ_F_LINK_TIMEOUT;
1418 continue;
1419 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001420
Pavel Begunkov44932332019-12-05 16:16:35 +03001421 list_del_init(&req->link_list);
1422 if (!list_empty(&nxt->link_list))
1423 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001424 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001425 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001426 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001427
Jens Axboe4d7dd462019-11-20 13:03:52 -07001428 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001429 if (wake_ev)
1430 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001431}
1432
1433/*
1434 * Called if REQ_F_LINK is set, and we fail the head request
1435 */
1436static void io_fail_links(struct io_kiocb *req)
1437{
Jens Axboe2665abf2019-11-05 12:40:47 -07001438 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001439 unsigned long flags;
1440
1441 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001442
1443 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001444 struct io_kiocb *link = list_first_entry(&req->link_list,
1445 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001446
Pavel Begunkov44932332019-12-05 16:16:35 +03001447 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001448 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001449
1450 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001451 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001452 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001453 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001454 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001455 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001456 }
Jens Axboe5d960722019-11-19 15:31:28 -07001457 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001458 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001459
1460 io_commit_cqring(ctx);
1461 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1462 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001463}
1464
Jens Axboe4d7dd462019-11-20 13:03:52 -07001465static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001466{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001467 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001468 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001469
Jens Axboe9e645e112019-05-10 16:07:28 -06001470 /*
1471 * If LINK is set, we have dependent requests in this chain. If we
1472 * didn't fail this request, queue the first one up, moving any other
1473 * dependencies to the next request. In case of failure, fail the rest
1474 * of the chain.
1475 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001476 if (req->flags & REQ_F_FAIL_LINK) {
1477 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001478 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1479 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001480 struct io_ring_ctx *ctx = req->ctx;
1481 unsigned long flags;
1482
1483 /*
1484 * If this is a timeout link, we could be racing with the
1485 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001486 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001487 */
1488 spin_lock_irqsave(&ctx->completion_lock, flags);
1489 io_req_link_next(req, nxt);
1490 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1491 } else {
1492 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001493 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001494}
Jens Axboe9e645e112019-05-10 16:07:28 -06001495
Jackie Liuc69f8db2019-11-09 11:00:08 +08001496static void io_free_req(struct io_kiocb *req)
1497{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001498 struct io_kiocb *nxt = NULL;
1499
1500 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001501 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001502
1503 if (nxt)
1504 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001505}
1506
Jens Axboeba816ad2019-09-28 11:36:45 -06001507/*
1508 * Drop reference to request, return next in chain (if there is one) if this
1509 * was the last reference to this request.
1510 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001511__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001512static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001513{
Jens Axboe2a44f462020-02-25 13:25:41 -07001514 if (refcount_dec_and_test(&req->refs)) {
1515 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001516 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001517 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001518}
1519
Jens Axboe2b188cc2019-01-07 10:46:33 -07001520static void io_put_req(struct io_kiocb *req)
1521{
Jens Axboedef596e2019-01-09 08:59:42 -07001522 if (refcount_dec_and_test(&req->refs))
1523 io_free_req(req);
1524}
1525
Jens Axboe978db572019-11-14 22:39:04 -07001526/*
1527 * Must only be used if we don't need to care about links, usually from
1528 * within the completion handling itself.
1529 */
1530static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001531{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001532 /* drop both submit and complete references */
1533 if (refcount_sub_and_test(2, &req->refs))
1534 __io_free_req(req);
1535}
1536
Jens Axboe978db572019-11-14 22:39:04 -07001537static void io_double_put_req(struct io_kiocb *req)
1538{
1539 /* drop both submit and complete references */
1540 if (refcount_sub_and_test(2, &req->refs))
1541 io_free_req(req);
1542}
1543
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001544static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001545{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001546 struct io_rings *rings = ctx->rings;
1547
Jens Axboead3eb2c2019-12-18 17:12:20 -07001548 if (test_bit(0, &ctx->cq_check_overflow)) {
1549 /*
1550 * noflush == true is from the waitqueue handler, just ensure
1551 * we wake up the task, and the next invocation will flush the
1552 * entries. We cannot safely to it from here.
1553 */
1554 if (noflush && !list_empty(&ctx->cq_overflow_list))
1555 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001556
Jens Axboead3eb2c2019-12-18 17:12:20 -07001557 io_cqring_overflow_flush(ctx, false);
1558 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001559
Jens Axboea3a0e432019-08-20 11:03:11 -06001560 /* See comment at the top of this file */
1561 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001562 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001563}
1564
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001565static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1566{
1567 struct io_rings *rings = ctx->rings;
1568
1569 /* make sure SQ entry isn't read before tail */
1570 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1571}
1572
Jens Axboe8237e042019-12-28 10:48:22 -07001573static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001574{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001575 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1576 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001577
Jens Axboec6ca97b302019-12-28 12:11:08 -07001578 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1579 rb->need_iter++;
1580
1581 rb->reqs[rb->to_free++] = req;
1582 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1583 io_free_req_many(req->ctx, rb);
1584 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001585}
1586
Jens Axboedef596e2019-01-09 08:59:42 -07001587/*
1588 * Find and free completed poll iocbs
1589 */
1590static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1591 struct list_head *done)
1592{
Jens Axboe8237e042019-12-28 10:48:22 -07001593 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001594 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001595
Jens Axboec6ca97b302019-12-28 12:11:08 -07001596 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001597 while (!list_empty(done)) {
1598 req = list_first_entry(done, struct io_kiocb, list);
1599 list_del(&req->list);
1600
Jens Axboe78e19bb2019-11-06 15:21:34 -07001601 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001602 (*nr_events)++;
1603
Jens Axboe8237e042019-12-28 10:48:22 -07001604 if (refcount_dec_and_test(&req->refs) &&
1605 !io_req_multi_free(&rb, req))
1606 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001607 }
Jens Axboedef596e2019-01-09 08:59:42 -07001608
Jens Axboe09bb8392019-03-13 12:39:28 -06001609 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001610 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001611}
1612
1613static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1614 long min)
1615{
1616 struct io_kiocb *req, *tmp;
1617 LIST_HEAD(done);
1618 bool spin;
1619 int ret;
1620
1621 /*
1622 * Only spin for completions if we don't have multiple devices hanging
1623 * off our complete list, and we're under the requested amount.
1624 */
1625 spin = !ctx->poll_multi_file && *nr_events < min;
1626
1627 ret = 0;
1628 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001629 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001630
1631 /*
1632 * Move completed entries to our local list. If we find a
1633 * request that requires polling, break out and complete
1634 * the done list first, if we have entries there.
1635 */
1636 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1637 list_move_tail(&req->list, &done);
1638 continue;
1639 }
1640 if (!list_empty(&done))
1641 break;
1642
1643 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1644 if (ret < 0)
1645 break;
1646
1647 if (ret && spin)
1648 spin = false;
1649 ret = 0;
1650 }
1651
1652 if (!list_empty(&done))
1653 io_iopoll_complete(ctx, nr_events, &done);
1654
1655 return ret;
1656}
1657
1658/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001659 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001660 * non-spinning poll check - we'll still enter the driver poll loop, but only
1661 * as a non-spinning completion check.
1662 */
1663static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1664 long min)
1665{
Jens Axboe08f54392019-08-21 22:19:11 -06001666 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001667 int ret;
1668
1669 ret = io_do_iopoll(ctx, nr_events, min);
1670 if (ret < 0)
1671 return ret;
1672 if (!min || *nr_events >= min)
1673 return 0;
1674 }
1675
1676 return 1;
1677}
1678
1679/*
1680 * We can't just wait for polled events to come to us, we have to actively
1681 * find and complete them.
1682 */
1683static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1684{
1685 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1686 return;
1687
1688 mutex_lock(&ctx->uring_lock);
1689 while (!list_empty(&ctx->poll_list)) {
1690 unsigned int nr_events = 0;
1691
1692 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001693
1694 /*
1695 * Ensure we allow local-to-the-cpu processing to take place,
1696 * in this case we need to ensure that we reap all events.
1697 */
1698 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001699 }
1700 mutex_unlock(&ctx->uring_lock);
1701}
1702
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001703static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1704 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001705{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001706 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001707
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001708 /*
1709 * We disallow the app entering submit/complete with polling, but we
1710 * still need to lock the ring to prevent racing with polled issue
1711 * that got punted to a workqueue.
1712 */
1713 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001714 do {
1715 int tmin = 0;
1716
Jens Axboe500f9fb2019-08-19 12:15:59 -06001717 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001718 * Don't enter poll loop if we already have events pending.
1719 * If we do, we can potentially be spinning for commands that
1720 * already triggered a CQE (eg in error).
1721 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001722 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001723 break;
1724
1725 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001726 * If a submit got punted to a workqueue, we can have the
1727 * application entering polling for a command before it gets
1728 * issued. That app will hold the uring_lock for the duration
1729 * of the poll right here, so we need to take a breather every
1730 * now and then to ensure that the issue has a chance to add
1731 * the poll to the issued list. Otherwise we can spin here
1732 * forever, while the workqueue is stuck trying to acquire the
1733 * very same mutex.
1734 */
1735 if (!(++iters & 7)) {
1736 mutex_unlock(&ctx->uring_lock);
1737 mutex_lock(&ctx->uring_lock);
1738 }
1739
Jens Axboedef596e2019-01-09 08:59:42 -07001740 if (*nr_events < min)
1741 tmin = min - *nr_events;
1742
1743 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1744 if (ret <= 0)
1745 break;
1746 ret = 0;
1747 } while (min && !*nr_events && !need_resched());
1748
Jens Axboe500f9fb2019-08-19 12:15:59 -06001749 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001750 return ret;
1751}
1752
Jens Axboe491381ce2019-10-17 09:20:46 -06001753static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001754{
Jens Axboe491381ce2019-10-17 09:20:46 -06001755 /*
1756 * Tell lockdep we inherited freeze protection from submission
1757 * thread.
1758 */
1759 if (req->flags & REQ_F_ISREG) {
1760 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001761
Jens Axboe491381ce2019-10-17 09:20:46 -06001762 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001763 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001764 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001765}
1766
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001767static inline void req_set_fail_links(struct io_kiocb *req)
1768{
1769 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1770 req->flags |= REQ_F_FAIL_LINK;
1771}
1772
Jens Axboeba816ad2019-09-28 11:36:45 -06001773static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001774{
Jens Axboe9adbd452019-12-20 08:45:55 -07001775 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001776
Jens Axboe491381ce2019-10-17 09:20:46 -06001777 if (kiocb->ki_flags & IOCB_WRITE)
1778 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001779
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001780 if (res != req->result)
1781 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001782 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001783}
1784
1785static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1786{
Jens Axboe9adbd452019-12-20 08:45:55 -07001787 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001788
1789 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001790 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001791}
1792
Jens Axboeba816ad2019-09-28 11:36:45 -06001793static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1794{
Jens Axboe9adbd452019-12-20 08:45:55 -07001795 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001796 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001797
1798 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001799 io_put_req_find_next(req, &nxt);
1800
1801 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001802}
1803
Jens Axboedef596e2019-01-09 08:59:42 -07001804static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1805{
Jens Axboe9adbd452019-12-20 08:45:55 -07001806 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001807
Jens Axboe491381ce2019-10-17 09:20:46 -06001808 if (kiocb->ki_flags & IOCB_WRITE)
1809 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001810
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001811 if (res != req->result)
1812 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001813 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001814 if (res != -EAGAIN)
1815 req->flags |= REQ_F_IOPOLL_COMPLETED;
1816}
1817
1818/*
1819 * After the iocb has been issued, it's safe to be found on the poll list.
1820 * Adding the kiocb to the list AFTER submission ensures that we don't
1821 * find it from a io_iopoll_getevents() thread before the issuer is done
1822 * accessing the kiocb cookie.
1823 */
1824static void io_iopoll_req_issued(struct io_kiocb *req)
1825{
1826 struct io_ring_ctx *ctx = req->ctx;
1827
1828 /*
1829 * Track whether we have multiple files in our lists. This will impact
1830 * how we do polling eventually, not spinning if we're on potentially
1831 * different devices.
1832 */
1833 if (list_empty(&ctx->poll_list)) {
1834 ctx->poll_multi_file = false;
1835 } else if (!ctx->poll_multi_file) {
1836 struct io_kiocb *list_req;
1837
1838 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1839 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001840 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001841 ctx->poll_multi_file = true;
1842 }
1843
1844 /*
1845 * For fast devices, IO may have already completed. If it has, add
1846 * it to the front so we find it first.
1847 */
1848 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1849 list_add(&req->list, &ctx->poll_list);
1850 else
1851 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001852
1853 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1854 wq_has_sleeper(&ctx->sqo_wait))
1855 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001856}
1857
Jens Axboe3d6770f2019-04-13 11:50:54 -06001858static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001859{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001860 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001861 int diff = state->has_refs - state->used_refs;
1862
1863 if (diff)
1864 fput_many(state->file, diff);
1865 state->file = NULL;
1866 }
1867}
1868
1869/*
1870 * Get as many references to a file as we have IOs left in this submission,
1871 * assuming most submissions are for one file, or at least that each file
1872 * has more than one submission.
1873 */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03001874static struct file *__io_file_get(struct io_submit_state *state, int fd)
Jens Axboe9a56a232019-01-09 09:06:50 -07001875{
1876 if (!state)
1877 return fget(fd);
1878
1879 if (state->file) {
1880 if (state->fd == fd) {
1881 state->used_refs++;
1882 state->ios_left--;
1883 return state->file;
1884 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001885 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001886 }
1887 state->file = fget_many(fd, state->ios_left);
1888 if (!state->file)
1889 return NULL;
1890
1891 state->fd = fd;
1892 state->has_refs = state->ios_left;
1893 state->used_refs = 1;
1894 state->ios_left--;
1895 return state->file;
1896}
1897
Jens Axboe2b188cc2019-01-07 10:46:33 -07001898/*
1899 * If we tracked the file through the SCM inflight mechanism, we could support
1900 * any file. For now, just ensure that anything potentially problematic is done
1901 * inline.
1902 */
1903static bool io_file_supports_async(struct file *file)
1904{
1905 umode_t mode = file_inode(file)->i_mode;
1906
Jens Axboe10d59342019-12-09 20:16:22 -07001907 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001908 return true;
1909 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1910 return true;
1911
1912 return false;
1913}
1914
Jens Axboe3529d8c2019-12-19 18:24:38 -07001915static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1916 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001917{
Jens Axboedef596e2019-01-09 08:59:42 -07001918 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001919 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001920 unsigned ioprio;
1921 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001922
Jens Axboe491381ce2019-10-17 09:20:46 -06001923 if (S_ISREG(file_inode(req->file)->i_mode))
1924 req->flags |= REQ_F_ISREG;
1925
Jens Axboe2b188cc2019-01-07 10:46:33 -07001926 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001927 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1928 req->flags |= REQ_F_CUR_POS;
1929 kiocb->ki_pos = req->file->f_pos;
1930 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001931 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001932 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1933 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1934 if (unlikely(ret))
1935 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001936
1937 ioprio = READ_ONCE(sqe->ioprio);
1938 if (ioprio) {
1939 ret = ioprio_check_cap(ioprio);
1940 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001941 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001942
1943 kiocb->ki_ioprio = ioprio;
1944 } else
1945 kiocb->ki_ioprio = get_current_ioprio();
1946
Stefan Bühler8449eed2019-04-27 20:34:19 +02001947 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001948 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1949 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001950 req->flags |= REQ_F_NOWAIT;
1951
1952 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001953 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001954
Jens Axboedef596e2019-01-09 08:59:42 -07001955 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001956 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1957 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001958 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001959
Jens Axboedef596e2019-01-09 08:59:42 -07001960 kiocb->ki_flags |= IOCB_HIPRI;
1961 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001962 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001963 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001964 if (kiocb->ki_flags & IOCB_HIPRI)
1965 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001966 kiocb->ki_complete = io_complete_rw;
1967 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001968
Jens Axboe3529d8c2019-12-19 18:24:38 -07001969 req->rw.addr = READ_ONCE(sqe->addr);
1970 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001971 /* we own ->private, reuse it for the buffer index */
1972 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001973 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001974 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001975}
1976
1977static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1978{
1979 switch (ret) {
1980 case -EIOCBQUEUED:
1981 break;
1982 case -ERESTARTSYS:
1983 case -ERESTARTNOINTR:
1984 case -ERESTARTNOHAND:
1985 case -ERESTART_RESTARTBLOCK:
1986 /*
1987 * We can't just restart the syscall, since previously
1988 * submitted sqes may already be in progress. Just fail this
1989 * IO with EINTR.
1990 */
1991 ret = -EINTR;
1992 /* fall through */
1993 default:
1994 kiocb->ki_complete(kiocb, ret, 0);
1995 }
1996}
1997
Pavel Begunkovbcaec082020-02-24 11:30:18 +03001998static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt)
Jens Axboeba816ad2019-09-28 11:36:45 -06001999{
Jens Axboeba042912019-12-25 16:33:42 -07002000 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
2001
2002 if (req->flags & REQ_F_CUR_POS)
2003 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002004 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06002005 *nxt = __io_complete_rw(kiocb, ret);
2006 else
2007 io_rw_done(kiocb, ret);
2008}
2009
Jens Axboe9adbd452019-12-20 08:45:55 -07002010static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03002011 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07002012{
Jens Axboe9adbd452019-12-20 08:45:55 -07002013 struct io_ring_ctx *ctx = req->ctx;
2014 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002015 struct io_mapped_ubuf *imu;
2016 unsigned index, buf_index;
2017 size_t offset;
2018 u64 buf_addr;
2019
2020 /* attempt to use fixed buffers without having provided iovecs */
2021 if (unlikely(!ctx->user_bufs))
2022 return -EFAULT;
2023
Jens Axboe9adbd452019-12-20 08:45:55 -07002024 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002025 if (unlikely(buf_index >= ctx->nr_user_bufs))
2026 return -EFAULT;
2027
2028 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2029 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002030 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002031
2032 /* overflow */
2033 if (buf_addr + len < buf_addr)
2034 return -EFAULT;
2035 /* not inside the mapped region */
2036 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2037 return -EFAULT;
2038
2039 /*
2040 * May not be a start of buffer, set size appropriately
2041 * and advance us to the beginning.
2042 */
2043 offset = buf_addr - imu->ubuf;
2044 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002045
2046 if (offset) {
2047 /*
2048 * Don't use iov_iter_advance() here, as it's really slow for
2049 * using the latter parts of a big fixed buffer - it iterates
2050 * over each segment manually. We can cheat a bit here, because
2051 * we know that:
2052 *
2053 * 1) it's a BVEC iter, we set it up
2054 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2055 * first and last bvec
2056 *
2057 * So just find our index, and adjust the iterator afterwards.
2058 * If the offset is within the first bvec (or the whole first
2059 * bvec, just use iov_iter_advance(). This makes it easier
2060 * since we can just skip the first segment, which may not
2061 * be PAGE_SIZE aligned.
2062 */
2063 const struct bio_vec *bvec = imu->bvec;
2064
2065 if (offset <= bvec->bv_len) {
2066 iov_iter_advance(iter, offset);
2067 } else {
2068 unsigned long seg_skip;
2069
2070 /* skip first vec */
2071 offset -= bvec->bv_len;
2072 seg_skip = 1 + (offset >> PAGE_SHIFT);
2073
2074 iter->bvec = bvec + seg_skip;
2075 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002076 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002077 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002078 }
2079 }
2080
Jens Axboe5e559562019-11-13 16:12:46 -07002081 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002082}
2083
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002084static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2085 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002086{
Jens Axboe9adbd452019-12-20 08:45:55 -07002087 void __user *buf = u64_to_user_ptr(req->rw.addr);
2088 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002089 u8 opcode;
2090
Jens Axboed625c6e2019-12-17 19:53:05 -07002091 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002092 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002093 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002094 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002095 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002096
Jens Axboe9adbd452019-12-20 08:45:55 -07002097 /* buffer index only valid with fixed read/write */
2098 if (req->rw.kiocb.private)
2099 return -EINVAL;
2100
Jens Axboe3a6820f2019-12-22 15:19:35 -07002101 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2102 ssize_t ret;
2103 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2104 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002105 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002106 }
2107
Jens Axboef67676d2019-12-02 11:03:47 -07002108 if (req->io) {
2109 struct io_async_rw *iorw = &req->io->rw;
2110
2111 *iovec = iorw->iov;
2112 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2113 if (iorw->iov == iorw->fast_iov)
2114 *iovec = NULL;
2115 return iorw->size;
2116 }
2117
Jens Axboe2b188cc2019-01-07 10:46:33 -07002118#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002119 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002120 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2121 iovec, iter);
2122#endif
2123
2124 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2125}
2126
Jens Axboe32960612019-09-23 11:05:34 -06002127/*
2128 * For files that don't have ->read_iter() and ->write_iter(), handle them
2129 * by looping over ->read() or ->write() manually.
2130 */
2131static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2132 struct iov_iter *iter)
2133{
2134 ssize_t ret = 0;
2135
2136 /*
2137 * Don't support polled IO through this interface, and we can't
2138 * support non-blocking either. For the latter, this just causes
2139 * the kiocb to be handled from an async context.
2140 */
2141 if (kiocb->ki_flags & IOCB_HIPRI)
2142 return -EOPNOTSUPP;
2143 if (kiocb->ki_flags & IOCB_NOWAIT)
2144 return -EAGAIN;
2145
2146 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002147 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002148 ssize_t nr;
2149
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002150 if (!iov_iter_is_bvec(iter)) {
2151 iovec = iov_iter_iovec(iter);
2152 } else {
2153 /* fixed buffers import bvec */
2154 iovec.iov_base = kmap(iter->bvec->bv_page)
2155 + iter->iov_offset;
2156 iovec.iov_len = min(iter->count,
2157 iter->bvec->bv_len - iter->iov_offset);
2158 }
2159
Jens Axboe32960612019-09-23 11:05:34 -06002160 if (rw == READ) {
2161 nr = file->f_op->read(file, iovec.iov_base,
2162 iovec.iov_len, &kiocb->ki_pos);
2163 } else {
2164 nr = file->f_op->write(file, iovec.iov_base,
2165 iovec.iov_len, &kiocb->ki_pos);
2166 }
2167
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002168 if (iov_iter_is_bvec(iter))
2169 kunmap(iter->bvec->bv_page);
2170
Jens Axboe32960612019-09-23 11:05:34 -06002171 if (nr < 0) {
2172 if (!ret)
2173 ret = nr;
2174 break;
2175 }
2176 ret += nr;
2177 if (nr != iovec.iov_len)
2178 break;
2179 iov_iter_advance(iter, nr);
2180 }
2181
2182 return ret;
2183}
2184
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002185static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002186 struct iovec *iovec, struct iovec *fast_iov,
2187 struct iov_iter *iter)
2188{
2189 req->io->rw.nr_segs = iter->nr_segs;
2190 req->io->rw.size = io_size;
2191 req->io->rw.iov = iovec;
2192 if (!req->io->rw.iov) {
2193 req->io->rw.iov = req->io->rw.fast_iov;
2194 memcpy(req->io->rw.iov, fast_iov,
2195 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002196 } else {
2197 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002198 }
2199}
2200
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002201static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002202{
Jens Axboed3656342019-12-18 09:50:26 -07002203 if (!io_op_defs[req->opcode].async_ctx)
2204 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002205 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002206 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002207}
2208
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002209static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2210 struct iovec *iovec, struct iovec *fast_iov,
2211 struct iov_iter *iter)
2212{
Jens Axboe980ad262020-01-24 23:08:54 -07002213 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002214 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002215 if (!req->io) {
2216 if (io_alloc_async_ctx(req))
2217 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002218
Jens Axboe5d204bc2020-01-31 12:06:52 -07002219 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2220 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002221 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002222}
2223
Jens Axboe3529d8c2019-12-19 18:24:38 -07002224static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2225 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002226{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002227 struct io_async_ctx *io;
2228 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002229 ssize_t ret;
2230
Jens Axboe3529d8c2019-12-19 18:24:38 -07002231 ret = io_prep_rw(req, sqe, force_nonblock);
2232 if (ret)
2233 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002234
Jens Axboe3529d8c2019-12-19 18:24:38 -07002235 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2236 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002237
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002238 /* either don't need iovec imported or already have it */
2239 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002240 return 0;
2241
2242 io = req->io;
2243 io->rw.iov = io->rw.fast_iov;
2244 req->io = NULL;
2245 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2246 req->io = io;
2247 if (ret < 0)
2248 return ret;
2249
2250 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2251 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002252}
2253
Pavel Begunkov267bc902019-11-07 01:41:08 +03002254static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002255 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002256{
2257 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002258 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002259 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002260 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002261 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002262
Jens Axboe3529d8c2019-12-19 18:24:38 -07002263 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002264 if (ret < 0)
2265 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002266
Jens Axboefd6c2e42019-12-18 12:19:41 -07002267 /* Ensure we clear previously set non-block flag */
2268 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002269 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002270
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002271 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002272 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002273 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002274 req->result = io_size;
2275
2276 /*
2277 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2278 * we know to async punt it even if it was opened O_NONBLOCK
2279 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002280 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002281 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002282
Jens Axboe31b51512019-01-18 22:56:34 -07002283 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002284 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002285 if (!ret) {
2286 ssize_t ret2;
2287
Jens Axboe9adbd452019-12-20 08:45:55 -07002288 if (req->file->f_op->read_iter)
2289 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002290 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002291 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002292
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002293 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002294 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002295 kiocb_done(kiocb, ret2, nxt);
Jens Axboef67676d2019-12-02 11:03:47 -07002296 } else {
2297copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002298 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002299 inline_vecs, &iter);
2300 if (ret)
2301 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002302 /* any defer here is final, must blocking retry */
2303 if (!(req->flags & REQ_F_NOWAIT))
2304 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002305 return -EAGAIN;
2306 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002307 }
Jens Axboef67676d2019-12-02 11:03:47 -07002308out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002309 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002310 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002311 return ret;
2312}
2313
Jens Axboe3529d8c2019-12-19 18:24:38 -07002314static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2315 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002316{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002317 struct io_async_ctx *io;
2318 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002319 ssize_t ret;
2320
Jens Axboe3529d8c2019-12-19 18:24:38 -07002321 ret = io_prep_rw(req, sqe, force_nonblock);
2322 if (ret)
2323 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002324
Jens Axboe3529d8c2019-12-19 18:24:38 -07002325 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2326 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002327
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002328 /* either don't need iovec imported or already have it */
2329 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002330 return 0;
2331
2332 io = req->io;
2333 io->rw.iov = io->rw.fast_iov;
2334 req->io = NULL;
2335 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2336 req->io = io;
2337 if (ret < 0)
2338 return ret;
2339
2340 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2341 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002342}
2343
Pavel Begunkov267bc902019-11-07 01:41:08 +03002344static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002345 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002346{
2347 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002348 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002349 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002350 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002351 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002352
Jens Axboe3529d8c2019-12-19 18:24:38 -07002353 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002354 if (ret < 0)
2355 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002356
Jens Axboefd6c2e42019-12-18 12:19:41 -07002357 /* Ensure we clear previously set non-block flag */
2358 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002359 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002360
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002361 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002362 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002363 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002364 req->result = io_size;
2365
2366 /*
2367 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2368 * we know to async punt it even if it was opened O_NONBLOCK
2369 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002370 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002371 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002372
Jens Axboe10d59342019-12-09 20:16:22 -07002373 /* file path doesn't support NOWAIT for non-direct_IO */
2374 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2375 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002376 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002377
Jens Axboe31b51512019-01-18 22:56:34 -07002378 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002379 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002380 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002381 ssize_t ret2;
2382
Jens Axboe2b188cc2019-01-07 10:46:33 -07002383 /*
2384 * Open-code file_start_write here to grab freeze protection,
2385 * which will be released by another thread in
2386 * io_complete_rw(). Fool lockdep by telling it the lock got
2387 * released so that it doesn't complain about the held lock when
2388 * we return to userspace.
2389 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002390 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002391 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002392 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002393 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002394 SB_FREEZE_WRITE);
2395 }
2396 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002397
Jens Axboe9adbd452019-12-20 08:45:55 -07002398 if (req->file->f_op->write_iter)
2399 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002400 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002401 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002402 /*
2403 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2404 * retry them without IOCB_NOWAIT.
2405 */
2406 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2407 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002408 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002409 kiocb_done(kiocb, ret2, nxt);
Jens Axboef67676d2019-12-02 11:03:47 -07002410 } else {
2411copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002412 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002413 inline_vecs, &iter);
2414 if (ret)
2415 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002416 /* any defer here is final, must blocking retry */
2417 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002418 return -EAGAIN;
2419 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002420 }
Jens Axboe31b51512019-01-18 22:56:34 -07002421out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002422 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002423 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002424 return ret;
2425}
2426
Pavel Begunkov7d67af22020-02-24 11:32:45 +03002427static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2428{
2429 struct io_splice* sp = &req->splice;
2430 unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL;
2431 int ret;
2432
2433 if (req->flags & REQ_F_NEED_CLEANUP)
2434 return 0;
2435
2436 sp->file_in = NULL;
2437 sp->off_in = READ_ONCE(sqe->splice_off_in);
2438 sp->off_out = READ_ONCE(sqe->off);
2439 sp->len = READ_ONCE(sqe->len);
2440 sp->flags = READ_ONCE(sqe->splice_flags);
2441
2442 if (unlikely(sp->flags & ~valid_flags))
2443 return -EINVAL;
2444
2445 ret = io_file_get(NULL, req, READ_ONCE(sqe->splice_fd_in), &sp->file_in,
2446 (sp->flags & SPLICE_F_FD_IN_FIXED));
2447 if (ret)
2448 return ret;
2449 req->flags |= REQ_F_NEED_CLEANUP;
2450
2451 if (!S_ISREG(file_inode(sp->file_in)->i_mode))
2452 req->work.flags |= IO_WQ_WORK_UNBOUND;
2453
2454 return 0;
2455}
2456
2457static bool io_splice_punt(struct file *file)
2458{
2459 if (get_pipe_info(file))
2460 return false;
2461 if (!io_file_supports_async(file))
2462 return true;
2463 return !(file->f_mode & O_NONBLOCK);
2464}
2465
2466static int io_splice(struct io_kiocb *req, struct io_kiocb **nxt,
2467 bool force_nonblock)
2468{
2469 struct io_splice *sp = &req->splice;
2470 struct file *in = sp->file_in;
2471 struct file *out = sp->file_out;
2472 unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED;
2473 loff_t *poff_in, *poff_out;
2474 long ret;
2475
2476 if (force_nonblock) {
2477 if (io_splice_punt(in) || io_splice_punt(out))
2478 return -EAGAIN;
2479 flags |= SPLICE_F_NONBLOCK;
2480 }
2481
2482 poff_in = (sp->off_in == -1) ? NULL : &sp->off_in;
2483 poff_out = (sp->off_out == -1) ? NULL : &sp->off_out;
2484 ret = do_splice(in, poff_in, out, poff_out, sp->len, flags);
2485 if (force_nonblock && ret == -EAGAIN)
2486 return -EAGAIN;
2487
2488 io_put_file(req, in, (sp->flags & SPLICE_F_FD_IN_FIXED));
2489 req->flags &= ~REQ_F_NEED_CLEANUP;
2490
2491 io_cqring_add_event(req, ret);
2492 if (ret != sp->len)
2493 req_set_fail_links(req);
2494 io_put_req_find_next(req, nxt);
2495 return 0;
2496}
2497
Jens Axboe2b188cc2019-01-07 10:46:33 -07002498/*
2499 * IORING_OP_NOP just posts a completion event, nothing else.
2500 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002501static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002502{
2503 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002504
Jens Axboedef596e2019-01-09 08:59:42 -07002505 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2506 return -EINVAL;
2507
Jens Axboe78e19bb2019-11-06 15:21:34 -07002508 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002509 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002510 return 0;
2511}
2512
Jens Axboe3529d8c2019-12-19 18:24:38 -07002513static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002514{
Jens Axboe6b063142019-01-10 22:13:58 -07002515 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002516
Jens Axboe09bb8392019-03-13 12:39:28 -06002517 if (!req->file)
2518 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002519
Jens Axboe6b063142019-01-10 22:13:58 -07002520 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002521 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002522 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002523 return -EINVAL;
2524
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002525 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2526 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2527 return -EINVAL;
2528
2529 req->sync.off = READ_ONCE(sqe->off);
2530 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002531 return 0;
2532}
2533
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002534static bool io_req_cancelled(struct io_kiocb *req)
2535{
2536 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2537 req_set_fail_links(req);
2538 io_cqring_add_event(req, -ECANCELED);
2539 io_put_req(req);
2540 return true;
2541 }
2542
2543 return false;
2544}
2545
Jens Axboe78912932020-01-14 22:09:06 -07002546static void io_link_work_cb(struct io_wq_work **workptr)
2547{
2548 struct io_wq_work *work = *workptr;
2549 struct io_kiocb *link = work->data;
2550
2551 io_queue_linked_timeout(link);
2552 work->func = io_wq_submit_work;
2553}
2554
2555static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2556{
2557 struct io_kiocb *link;
2558
Pavel Begunkovdeb6dc02020-02-24 11:30:17 +03002559 io_prep_next_work(nxt, &link);
Jens Axboe78912932020-01-14 22:09:06 -07002560 *workptr = &nxt->work;
2561 if (link) {
2562 nxt->work.flags |= IO_WQ_WORK_CB;
2563 nxt->work.func = io_link_work_cb;
2564 nxt->work.data = link;
2565 }
2566}
2567
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002568static void __io_fsync(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002569{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002570 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002571 int ret;
2572
Jens Axboe9adbd452019-12-20 08:45:55 -07002573 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002574 end > 0 ? end : LLONG_MAX,
2575 req->sync.flags & IORING_FSYNC_DATASYNC);
2576 if (ret < 0)
2577 req_set_fail_links(req);
2578 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002579 io_put_req_find_next(req, nxt);
2580}
2581
2582static void io_fsync_finish(struct io_wq_work **workptr)
2583{
2584 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2585 struct io_kiocb *nxt = NULL;
2586
2587 if (io_req_cancelled(req))
2588 return;
2589 __io_fsync(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002590 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002591 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002592}
2593
Jens Axboefc4df992019-12-10 14:38:45 -07002594static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2595 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002596{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002597 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002598 if (force_nonblock) {
2599 io_put_req(req);
2600 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002601 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002602 }
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002603 __io_fsync(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002604 return 0;
2605}
2606
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002607static void __io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboed63d1b52019-12-10 10:38:56 -07002608{
Jens Axboed63d1b52019-12-10 10:38:56 -07002609 int ret;
2610
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002611 if (io_req_cancelled(req))
2612 return;
2613
Jens Axboed63d1b52019-12-10 10:38:56 -07002614 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2615 req->sync.len);
2616 if (ret < 0)
2617 req_set_fail_links(req);
2618 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002619 io_put_req_find_next(req, nxt);
2620}
2621
2622static void io_fallocate_finish(struct io_wq_work **workptr)
2623{
2624 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2625 struct io_kiocb *nxt = NULL;
2626
2627 __io_fallocate(req, &nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002628 if (nxt)
2629 io_wq_assign_next(workptr, nxt);
2630}
2631
2632static int io_fallocate_prep(struct io_kiocb *req,
2633 const struct io_uring_sqe *sqe)
2634{
2635 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2636 return -EINVAL;
2637
2638 req->sync.off = READ_ONCE(sqe->off);
2639 req->sync.len = READ_ONCE(sqe->addr);
2640 req->sync.mode = READ_ONCE(sqe->len);
2641 return 0;
2642}
2643
2644static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2645 bool force_nonblock)
2646{
Jens Axboed63d1b52019-12-10 10:38:56 -07002647 /* fallocate always requiring blocking context */
2648 if (force_nonblock) {
2649 io_put_req(req);
2650 req->work.func = io_fallocate_finish;
2651 return -EAGAIN;
2652 }
2653
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002654 __io_fallocate(req, nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002655 return 0;
2656}
2657
Jens Axboe15b71ab2019-12-11 11:20:36 -07002658static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2659{
Jens Axboef8748882020-01-08 17:47:02 -07002660 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002661 int ret;
2662
2663 if (sqe->ioprio || sqe->buf_index)
2664 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002665 if (sqe->flags & IOSQE_FIXED_FILE)
2666 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002667 if (req->flags & REQ_F_NEED_CLEANUP)
2668 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002669
2670 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002671 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002672 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002673 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002674
Jens Axboef8748882020-01-08 17:47:02 -07002675 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002676 if (IS_ERR(req->open.filename)) {
2677 ret = PTR_ERR(req->open.filename);
2678 req->open.filename = NULL;
2679 return ret;
2680 }
2681
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002682 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002683 return 0;
2684}
2685
Jens Axboecebdb982020-01-08 17:59:24 -07002686static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2687{
2688 struct open_how __user *how;
2689 const char __user *fname;
2690 size_t len;
2691 int ret;
2692
2693 if (sqe->ioprio || sqe->buf_index)
2694 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002695 if (sqe->flags & IOSQE_FIXED_FILE)
2696 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002697 if (req->flags & REQ_F_NEED_CLEANUP)
2698 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002699
2700 req->open.dfd = READ_ONCE(sqe->fd);
2701 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2702 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2703 len = READ_ONCE(sqe->len);
2704
2705 if (len < OPEN_HOW_SIZE_VER0)
2706 return -EINVAL;
2707
2708 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2709 len);
2710 if (ret)
2711 return ret;
2712
2713 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2714 req->open.how.flags |= O_LARGEFILE;
2715
2716 req->open.filename = getname(fname);
2717 if (IS_ERR(req->open.filename)) {
2718 ret = PTR_ERR(req->open.filename);
2719 req->open.filename = NULL;
2720 return ret;
2721 }
2722
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002723 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002724 return 0;
2725}
2726
2727static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2728 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002729{
2730 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002731 struct file *file;
2732 int ret;
2733
Jens Axboef86cd202020-01-29 13:46:44 -07002734 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002735 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002736
Jens Axboecebdb982020-01-08 17:59:24 -07002737 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002738 if (ret)
2739 goto err;
2740
Jens Axboecebdb982020-01-08 17:59:24 -07002741 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002742 if (ret < 0)
2743 goto err;
2744
2745 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2746 if (IS_ERR(file)) {
2747 put_unused_fd(ret);
2748 ret = PTR_ERR(file);
2749 } else {
2750 fsnotify_open(file);
2751 fd_install(ret, file);
2752 }
2753err:
2754 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002755 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002756 if (ret < 0)
2757 req_set_fail_links(req);
2758 io_cqring_add_event(req, ret);
2759 io_put_req_find_next(req, nxt);
2760 return 0;
2761}
2762
Jens Axboecebdb982020-01-08 17:59:24 -07002763static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2764 bool force_nonblock)
2765{
2766 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2767 return io_openat2(req, nxt, force_nonblock);
2768}
2769
Jens Axboe3e4827b2020-01-08 15:18:09 -07002770static int io_epoll_ctl_prep(struct io_kiocb *req,
2771 const struct io_uring_sqe *sqe)
2772{
2773#if defined(CONFIG_EPOLL)
2774 if (sqe->ioprio || sqe->buf_index)
2775 return -EINVAL;
2776
2777 req->epoll.epfd = READ_ONCE(sqe->fd);
2778 req->epoll.op = READ_ONCE(sqe->len);
2779 req->epoll.fd = READ_ONCE(sqe->off);
2780
2781 if (ep_op_has_event(req->epoll.op)) {
2782 struct epoll_event __user *ev;
2783
2784 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2785 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2786 return -EFAULT;
2787 }
2788
2789 return 0;
2790#else
2791 return -EOPNOTSUPP;
2792#endif
2793}
2794
2795static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2796 bool force_nonblock)
2797{
2798#if defined(CONFIG_EPOLL)
2799 struct io_epoll *ie = &req->epoll;
2800 int ret;
2801
2802 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2803 if (force_nonblock && ret == -EAGAIN)
2804 return -EAGAIN;
2805
2806 if (ret < 0)
2807 req_set_fail_links(req);
2808 io_cqring_add_event(req, ret);
2809 io_put_req_find_next(req, nxt);
2810 return 0;
2811#else
2812 return -EOPNOTSUPP;
2813#endif
2814}
2815
Jens Axboec1ca7572019-12-25 22:18:28 -07002816static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2817{
2818#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2819 if (sqe->ioprio || sqe->buf_index || sqe->off)
2820 return -EINVAL;
2821
2822 req->madvise.addr = READ_ONCE(sqe->addr);
2823 req->madvise.len = READ_ONCE(sqe->len);
2824 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2825 return 0;
2826#else
2827 return -EOPNOTSUPP;
2828#endif
2829}
2830
2831static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2832 bool force_nonblock)
2833{
2834#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2835 struct io_madvise *ma = &req->madvise;
2836 int ret;
2837
2838 if (force_nonblock)
2839 return -EAGAIN;
2840
2841 ret = do_madvise(ma->addr, ma->len, ma->advice);
2842 if (ret < 0)
2843 req_set_fail_links(req);
2844 io_cqring_add_event(req, ret);
2845 io_put_req_find_next(req, nxt);
2846 return 0;
2847#else
2848 return -EOPNOTSUPP;
2849#endif
2850}
2851
Jens Axboe4840e412019-12-25 22:03:45 -07002852static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2853{
2854 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2855 return -EINVAL;
2856
2857 req->fadvise.offset = READ_ONCE(sqe->off);
2858 req->fadvise.len = READ_ONCE(sqe->len);
2859 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2860 return 0;
2861}
2862
2863static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2864 bool force_nonblock)
2865{
2866 struct io_fadvise *fa = &req->fadvise;
2867 int ret;
2868
Jens Axboe3e694262020-02-01 09:22:49 -07002869 if (force_nonblock) {
2870 switch (fa->advice) {
2871 case POSIX_FADV_NORMAL:
2872 case POSIX_FADV_RANDOM:
2873 case POSIX_FADV_SEQUENTIAL:
2874 break;
2875 default:
2876 return -EAGAIN;
2877 }
2878 }
Jens Axboe4840e412019-12-25 22:03:45 -07002879
2880 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2881 if (ret < 0)
2882 req_set_fail_links(req);
2883 io_cqring_add_event(req, ret);
2884 io_put_req_find_next(req, nxt);
2885 return 0;
2886}
2887
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002888static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2889{
Jens Axboef8748882020-01-08 17:47:02 -07002890 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002891 unsigned lookup_flags;
2892 int ret;
2893
2894 if (sqe->ioprio || sqe->buf_index)
2895 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002896 if (sqe->flags & IOSQE_FIXED_FILE)
2897 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002898 if (req->flags & REQ_F_NEED_CLEANUP)
2899 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002900
2901 req->open.dfd = READ_ONCE(sqe->fd);
2902 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002903 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002904 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002905 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002906
Jens Axboec12cedf2020-01-08 17:41:21 -07002907 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002908 return -EINVAL;
2909
Jens Axboef8748882020-01-08 17:47:02 -07002910 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002911 if (IS_ERR(req->open.filename)) {
2912 ret = PTR_ERR(req->open.filename);
2913 req->open.filename = NULL;
2914 return ret;
2915 }
2916
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002917 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002918 return 0;
2919}
2920
2921static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2922 bool force_nonblock)
2923{
2924 struct io_open *ctx = &req->open;
2925 unsigned lookup_flags;
2926 struct path path;
2927 struct kstat stat;
2928 int ret;
2929
2930 if (force_nonblock)
2931 return -EAGAIN;
2932
Jens Axboec12cedf2020-01-08 17:41:21 -07002933 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002934 return -EINVAL;
2935
2936retry:
2937 /* filename_lookup() drops it, keep a reference */
2938 ctx->filename->refcnt++;
2939
2940 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2941 NULL);
2942 if (ret)
2943 goto err;
2944
Jens Axboec12cedf2020-01-08 17:41:21 -07002945 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002946 path_put(&path);
2947 if (retry_estale(ret, lookup_flags)) {
2948 lookup_flags |= LOOKUP_REVAL;
2949 goto retry;
2950 }
2951 if (!ret)
2952 ret = cp_statx(&stat, ctx->buffer);
2953err:
2954 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002955 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002956 if (ret < 0)
2957 req_set_fail_links(req);
2958 io_cqring_add_event(req, ret);
2959 io_put_req_find_next(req, nxt);
2960 return 0;
2961}
2962
Jens Axboeb5dba592019-12-11 14:02:38 -07002963static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2964{
2965 /*
2966 * If we queue this for async, it must not be cancellable. That would
2967 * leave the 'file' in an undeterminate state.
2968 */
2969 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2970
2971 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2972 sqe->rw_flags || sqe->buf_index)
2973 return -EINVAL;
2974 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002975 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002976
2977 req->close.fd = READ_ONCE(sqe->fd);
2978 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002979 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002980 return -EBADF;
2981
2982 return 0;
2983}
2984
Pavel Begunkova93b3332020-02-08 14:04:34 +03002985/* only called when __close_fd_get_file() is done */
2986static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt)
2987{
2988 int ret;
2989
2990 ret = filp_close(req->close.put_file, req->work.files);
2991 if (ret < 0)
2992 req_set_fail_links(req);
2993 io_cqring_add_event(req, ret);
2994 fput(req->close.put_file);
2995 io_put_req_find_next(req, nxt);
2996}
2997
Jens Axboeb5dba592019-12-11 14:02:38 -07002998static void io_close_finish(struct io_wq_work **workptr)
2999{
3000 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3001 struct io_kiocb *nxt = NULL;
3002
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03003003 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkova93b3332020-02-08 14:04:34 +03003004 __io_close_finish(req, &nxt);
Jens Axboeb5dba592019-12-11 14:02:38 -07003005 if (nxt)
3006 io_wq_assign_next(workptr, nxt);
3007}
3008
3009static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
3010 bool force_nonblock)
3011{
3012 int ret;
3013
3014 req->close.put_file = NULL;
3015 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
3016 if (ret < 0)
3017 return ret;
3018
3019 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07003020 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07003021 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07003022
3023 /*
3024 * No ->flush(), safely close from here and just punt the
3025 * fput() to async context.
3026 */
Pavel Begunkova93b3332020-02-08 14:04:34 +03003027 __io_close_finish(req, nxt);
3028 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003029eagain:
3030 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07003031 /*
3032 * Do manual async queue here to avoid grabbing files - we don't
3033 * need the files, and it'll cause io_close_finish() to close
3034 * the file again and cause a double CQE entry for this request
3035 */
3036 io_queue_async_work(req);
3037 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07003038}
3039
Jens Axboe3529d8c2019-12-19 18:24:38 -07003040static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003041{
3042 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003043
3044 if (!req->file)
3045 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003046
3047 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
3048 return -EINVAL;
3049 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
3050 return -EINVAL;
3051
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003052 req->sync.off = READ_ONCE(sqe->off);
3053 req->sync.len = READ_ONCE(sqe->len);
3054 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003055 return 0;
3056}
3057
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003058static void __io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003059{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003060 int ret;
3061
Jens Axboe9adbd452019-12-20 08:45:55 -07003062 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003063 req->sync.flags);
3064 if (ret < 0)
3065 req_set_fail_links(req);
3066 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003067 io_put_req_find_next(req, nxt);
3068}
3069
3070
3071static void io_sync_file_range_finish(struct io_wq_work **workptr)
3072{
3073 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3074 struct io_kiocb *nxt = NULL;
3075
3076 if (io_req_cancelled(req))
3077 return;
3078 __io_sync_file_range(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003079 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003080 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003081}
3082
Jens Axboefc4df992019-12-10 14:38:45 -07003083static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003084 bool force_nonblock)
3085{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003086 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003087 if (force_nonblock) {
3088 io_put_req(req);
3089 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003090 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003091 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003092
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003093 __io_sync_file_range(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003094 return 0;
3095}
3096
Jens Axboe3529d8c2019-12-19 18:24:38 -07003097static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003098{
Jens Axboe03b12302019-12-02 18:50:25 -07003099#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003100 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003101 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003102 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003103
Jens Axboee47293f2019-12-20 08:58:21 -07003104 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3105 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003106 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003107
Jens Axboed8768362020-02-27 14:17:49 -07003108#ifdef CONFIG_COMPAT
3109 if (req->ctx->compat)
3110 sr->msg_flags |= MSG_CMSG_COMPAT;
3111#endif
3112
Jens Axboefddafac2020-01-04 20:19:44 -07003113 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003114 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003115 /* iovec is already imported */
3116 if (req->flags & REQ_F_NEED_CLEANUP)
3117 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003118
Jens Axboed9688562019-12-09 19:35:20 -07003119 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003120 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003121 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003122 if (!ret)
3123 req->flags |= REQ_F_NEED_CLEANUP;
3124 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003125#else
Jens Axboee47293f2019-12-20 08:58:21 -07003126 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003127#endif
3128}
3129
Jens Axboefc4df992019-12-10 14:38:45 -07003130static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3131 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003132{
3133#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003134 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003135 struct socket *sock;
3136 int ret;
3137
3138 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3139 return -EINVAL;
3140
3141 sock = sock_from_file(req->file, &ret);
3142 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003143 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003144 unsigned flags;
3145
Jens Axboe03b12302019-12-02 18:50:25 -07003146 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003147 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003148 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003149 /* if iov is set, it's allocated already */
3150 if (!kmsg->iov)
3151 kmsg->iov = kmsg->fast_iov;
3152 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003153 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003154 struct io_sr_msg *sr = &req->sr_msg;
3155
Jens Axboe0b416c32019-12-15 10:57:46 -07003156 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003157 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003158
3159 io.msg.iov = io.msg.fast_iov;
3160 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3161 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003162 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003163 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003164 }
3165
Jens Axboee47293f2019-12-20 08:58:21 -07003166 flags = req->sr_msg.msg_flags;
3167 if (flags & MSG_DONTWAIT)
3168 req->flags |= REQ_F_NOWAIT;
3169 else if (force_nonblock)
3170 flags |= MSG_DONTWAIT;
3171
Jens Axboe0b416c32019-12-15 10:57:46 -07003172 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003173 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003174 if (req->io)
3175 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003176 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003177 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003178 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003179 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003180 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003181 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003182 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003183 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003184 }
3185 if (ret == -ERESTARTSYS)
3186 ret = -EINTR;
3187 }
3188
Pavel Begunkov1e950812020-02-06 19:51:16 +03003189 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003190 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003191 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003192 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003193 if (ret < 0)
3194 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003195 io_put_req_find_next(req, nxt);
3196 return 0;
3197#else
3198 return -EOPNOTSUPP;
3199#endif
3200}
3201
Jens Axboefddafac2020-01-04 20:19:44 -07003202static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3203 bool force_nonblock)
3204{
3205#if defined(CONFIG_NET)
3206 struct socket *sock;
3207 int ret;
3208
3209 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3210 return -EINVAL;
3211
3212 sock = sock_from_file(req->file, &ret);
3213 if (sock) {
3214 struct io_sr_msg *sr = &req->sr_msg;
3215 struct msghdr msg;
3216 struct iovec iov;
3217 unsigned flags;
3218
3219 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3220 &msg.msg_iter);
3221 if (ret)
3222 return ret;
3223
3224 msg.msg_name = NULL;
3225 msg.msg_control = NULL;
3226 msg.msg_controllen = 0;
3227 msg.msg_namelen = 0;
3228
3229 flags = req->sr_msg.msg_flags;
3230 if (flags & MSG_DONTWAIT)
3231 req->flags |= REQ_F_NOWAIT;
3232 else if (force_nonblock)
3233 flags |= MSG_DONTWAIT;
3234
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003235 msg.msg_flags = flags;
3236 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003237 if (force_nonblock && ret == -EAGAIN)
3238 return -EAGAIN;
3239 if (ret == -ERESTARTSYS)
3240 ret = -EINTR;
3241 }
3242
3243 io_cqring_add_event(req, ret);
3244 if (ret < 0)
3245 req_set_fail_links(req);
3246 io_put_req_find_next(req, nxt);
3247 return 0;
3248#else
3249 return -EOPNOTSUPP;
3250#endif
3251}
3252
Jens Axboe3529d8c2019-12-19 18:24:38 -07003253static int io_recvmsg_prep(struct io_kiocb *req,
3254 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003255{
3256#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003257 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003258 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003259 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003260
Jens Axboe3529d8c2019-12-19 18:24:38 -07003261 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3262 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003263 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003264
Jens Axboed8768362020-02-27 14:17:49 -07003265#ifdef CONFIG_COMPAT
3266 if (req->ctx->compat)
3267 sr->msg_flags |= MSG_CMSG_COMPAT;
3268#endif
3269
Jens Axboefddafac2020-01-04 20:19:44 -07003270 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003271 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003272 /* iovec is already imported */
3273 if (req->flags & REQ_F_NEED_CLEANUP)
3274 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003275
Jens Axboed9688562019-12-09 19:35:20 -07003276 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003277 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003278 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003279 if (!ret)
3280 req->flags |= REQ_F_NEED_CLEANUP;
3281 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003282#else
Jens Axboee47293f2019-12-20 08:58:21 -07003283 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003284#endif
3285}
3286
Jens Axboefc4df992019-12-10 14:38:45 -07003287static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3288 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003289{
3290#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003291 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003292 struct socket *sock;
3293 int ret;
3294
3295 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3296 return -EINVAL;
3297
3298 sock = sock_from_file(req->file, &ret);
3299 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003300 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003301 unsigned flags;
3302
Jens Axboe03b12302019-12-02 18:50:25 -07003303 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003304 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003305 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003306 /* if iov is set, it's allocated already */
3307 if (!kmsg->iov)
3308 kmsg->iov = kmsg->fast_iov;
3309 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003310 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003311 struct io_sr_msg *sr = &req->sr_msg;
3312
Jens Axboe0b416c32019-12-15 10:57:46 -07003313 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003314 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003315
3316 io.msg.iov = io.msg.fast_iov;
3317 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3318 sr->msg_flags, &io.msg.uaddr,
3319 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003320 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003321 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003322 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003323
Jens Axboee47293f2019-12-20 08:58:21 -07003324 flags = req->sr_msg.msg_flags;
3325 if (flags & MSG_DONTWAIT)
3326 req->flags |= REQ_F_NOWAIT;
3327 else if (force_nonblock)
3328 flags |= MSG_DONTWAIT;
3329
3330 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3331 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003332 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003333 if (req->io)
3334 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003335 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003336 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003337 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003338 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003339 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003340 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003341 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe0b416c32019-12-15 10:57:46 -07003342 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003343 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003344 if (ret == -ERESTARTSYS)
3345 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003346 }
3347
Pavel Begunkov1e950812020-02-06 19:51:16 +03003348 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003349 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003350 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003351 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003352 if (ret < 0)
3353 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003354 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003355 return 0;
3356#else
3357 return -EOPNOTSUPP;
3358#endif
3359}
3360
Jens Axboefddafac2020-01-04 20:19:44 -07003361static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3362 bool force_nonblock)
3363{
3364#if defined(CONFIG_NET)
3365 struct socket *sock;
3366 int ret;
3367
3368 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3369 return -EINVAL;
3370
3371 sock = sock_from_file(req->file, &ret);
3372 if (sock) {
3373 struct io_sr_msg *sr = &req->sr_msg;
3374 struct msghdr msg;
3375 struct iovec iov;
3376 unsigned flags;
3377
3378 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3379 &msg.msg_iter);
3380 if (ret)
3381 return ret;
3382
3383 msg.msg_name = NULL;
3384 msg.msg_control = NULL;
3385 msg.msg_controllen = 0;
3386 msg.msg_namelen = 0;
3387 msg.msg_iocb = NULL;
3388 msg.msg_flags = 0;
3389
3390 flags = req->sr_msg.msg_flags;
3391 if (flags & MSG_DONTWAIT)
3392 req->flags |= REQ_F_NOWAIT;
3393 else if (force_nonblock)
3394 flags |= MSG_DONTWAIT;
3395
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003396 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003397 if (force_nonblock && ret == -EAGAIN)
3398 return -EAGAIN;
3399 if (ret == -ERESTARTSYS)
3400 ret = -EINTR;
3401 }
3402
3403 io_cqring_add_event(req, ret);
3404 if (ret < 0)
3405 req_set_fail_links(req);
3406 io_put_req_find_next(req, nxt);
3407 return 0;
3408#else
3409 return -EOPNOTSUPP;
3410#endif
3411}
3412
3413
Jens Axboe3529d8c2019-12-19 18:24:38 -07003414static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003415{
3416#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003417 struct io_accept *accept = &req->accept;
3418
Jens Axboe17f2fe32019-10-17 14:42:58 -06003419 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3420 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003421 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003422 return -EINVAL;
3423
Jens Axboed55e5f52019-12-11 16:12:15 -07003424 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3425 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003426 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003427 return 0;
3428#else
3429 return -EOPNOTSUPP;
3430#endif
3431}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003432
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003433#if defined(CONFIG_NET)
3434static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3435 bool force_nonblock)
3436{
3437 struct io_accept *accept = &req->accept;
3438 unsigned file_flags;
3439 int ret;
3440
3441 file_flags = force_nonblock ? O_NONBLOCK : 0;
3442 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3443 accept->addr_len, accept->flags);
3444 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003445 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003446 if (ret == -ERESTARTSYS)
3447 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003448 if (ret < 0)
3449 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003450 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003451 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003452 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003453}
3454
3455static void io_accept_finish(struct io_wq_work **workptr)
3456{
3457 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3458 struct io_kiocb *nxt = NULL;
3459
Jens Axboee441d1c2020-02-20 09:59:02 -07003460 io_put_req(req);
3461
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003462 if (io_req_cancelled(req))
3463 return;
3464 __io_accept(req, &nxt, false);
3465 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003466 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003467}
3468#endif
3469
3470static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3471 bool force_nonblock)
3472{
3473#if defined(CONFIG_NET)
3474 int ret;
3475
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003476 ret = __io_accept(req, nxt, force_nonblock);
3477 if (ret == -EAGAIN && force_nonblock) {
3478 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003479 return -EAGAIN;
3480 }
3481 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003482#else
3483 return -EOPNOTSUPP;
3484#endif
3485}
3486
Jens Axboe3529d8c2019-12-19 18:24:38 -07003487static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003488{
3489#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003490 struct io_connect *conn = &req->connect;
3491 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003492
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003493 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3494 return -EINVAL;
3495 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3496 return -EINVAL;
3497
Jens Axboe3529d8c2019-12-19 18:24:38 -07003498 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3499 conn->addr_len = READ_ONCE(sqe->addr2);
3500
3501 if (!io)
3502 return 0;
3503
3504 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003505 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003506#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003507 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003508#endif
3509}
3510
Jens Axboefc4df992019-12-10 14:38:45 -07003511static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3512 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003513{
3514#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003515 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003516 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003517 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003518
Jens Axboef499a022019-12-02 16:28:46 -07003519 if (req->io) {
3520 io = req->io;
3521 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003522 ret = move_addr_to_kernel(req->connect.addr,
3523 req->connect.addr_len,
3524 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003525 if (ret)
3526 goto out;
3527 io = &__io;
3528 }
3529
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003530 file_flags = force_nonblock ? O_NONBLOCK : 0;
3531
3532 ret = __sys_connect_file(req->file, &io->connect.address,
3533 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003534 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003535 if (req->io)
3536 return -EAGAIN;
3537 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003538 ret = -ENOMEM;
3539 goto out;
3540 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003541 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003542 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003543 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003544 if (ret == -ERESTARTSYS)
3545 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003546out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003547 if (ret < 0)
3548 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003549 io_cqring_add_event(req, ret);
3550 io_put_req_find_next(req, nxt);
3551 return 0;
3552#else
3553 return -EOPNOTSUPP;
3554#endif
3555}
3556
Jens Axboe221c5eb2019-01-17 09:41:58 -07003557static void io_poll_remove_one(struct io_kiocb *req)
3558{
3559 struct io_poll_iocb *poll = &req->poll;
3560
3561 spin_lock(&poll->head->lock);
3562 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003563 if (!list_empty(&poll->wait.entry)) {
3564 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003565 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003566 }
3567 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003568 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003569}
3570
3571static void io_poll_remove_all(struct io_ring_ctx *ctx)
3572{
Jens Axboe78076bb2019-12-04 19:56:40 -07003573 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003574 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003575 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003576
3577 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003578 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3579 struct hlist_head *list;
3580
3581 list = &ctx->cancel_hash[i];
3582 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3583 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003584 }
3585 spin_unlock_irq(&ctx->completion_lock);
3586}
3587
Jens Axboe47f46762019-11-09 17:43:02 -07003588static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3589{
Jens Axboe78076bb2019-12-04 19:56:40 -07003590 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003591 struct io_kiocb *req;
3592
Jens Axboe78076bb2019-12-04 19:56:40 -07003593 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3594 hlist_for_each_entry(req, list, hash_node) {
3595 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003596 io_poll_remove_one(req);
3597 return 0;
3598 }
Jens Axboe47f46762019-11-09 17:43:02 -07003599 }
3600
3601 return -ENOENT;
3602}
3603
Jens Axboe3529d8c2019-12-19 18:24:38 -07003604static int io_poll_remove_prep(struct io_kiocb *req,
3605 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003606{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003607 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3608 return -EINVAL;
3609 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3610 sqe->poll_events)
3611 return -EINVAL;
3612
Jens Axboe0969e782019-12-17 18:40:57 -07003613 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003614 return 0;
3615}
3616
3617/*
3618 * Find a running poll command that matches one specified in sqe->addr,
3619 * and remove it if found.
3620 */
3621static int io_poll_remove(struct io_kiocb *req)
3622{
3623 struct io_ring_ctx *ctx = req->ctx;
3624 u64 addr;
3625 int ret;
3626
Jens Axboe0969e782019-12-17 18:40:57 -07003627 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003628 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003629 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003630 spin_unlock_irq(&ctx->completion_lock);
3631
Jens Axboe78e19bb2019-11-06 15:21:34 -07003632 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003633 if (ret < 0)
3634 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003635 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003636 return 0;
3637}
3638
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003639static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003640{
Jackie Liua197f662019-11-08 08:09:12 -07003641 struct io_ring_ctx *ctx = req->ctx;
3642
Jens Axboe8c838782019-03-12 15:48:16 -06003643 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003644 if (error)
3645 io_cqring_fill_event(req, error);
3646 else
3647 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003648 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003649}
3650
Jens Axboe561fb042019-10-24 07:25:42 -06003651static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003652{
Jens Axboe561fb042019-10-24 07:25:42 -06003653 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003654 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3655 struct io_poll_iocb *poll = &req->poll;
3656 struct poll_table_struct pt = { ._key = poll->events };
3657 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003658 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003659 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003660 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003661
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003662 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003663 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003664 ret = -ECANCELED;
3665 } else if (READ_ONCE(poll->canceled)) {
3666 ret = -ECANCELED;
3667 }
Jens Axboe561fb042019-10-24 07:25:42 -06003668
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003669 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003670 mask = vfs_poll(poll->file, &pt) & poll->events;
3671
3672 /*
3673 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3674 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3675 * synchronize with them. In the cancellation case the list_del_init
3676 * itself is not actually needed, but harmless so we keep it in to
3677 * avoid further branches in the fast path.
3678 */
3679 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003680 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003681 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003682 spin_unlock_irq(&ctx->completion_lock);
3683 return;
3684 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003685 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003686 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003687 spin_unlock_irq(&ctx->completion_lock);
3688
Jens Axboe8c838782019-03-12 15:48:16 -06003689 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003690
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003691 if (ret < 0)
3692 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003693 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003694 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003695 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003696}
3697
Jens Axboee94f1412019-12-19 12:06:02 -07003698static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3699{
Jens Axboee94f1412019-12-19 12:06:02 -07003700 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003701 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003702
Jens Axboec6ca97b302019-12-28 12:11:08 -07003703 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003704 spin_lock_irq(&ctx->completion_lock);
3705 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3706 hash_del(&req->hash_node);
3707 io_poll_complete(req, req->result, 0);
3708
Jens Axboe8237e042019-12-28 10:48:22 -07003709 if (refcount_dec_and_test(&req->refs) &&
3710 !io_req_multi_free(&rb, req)) {
3711 req->flags |= REQ_F_COMP_LOCKED;
3712 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003713 }
3714 }
3715 spin_unlock_irq(&ctx->completion_lock);
3716
3717 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003718 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003719}
3720
3721static void io_poll_flush(struct io_wq_work **workptr)
3722{
3723 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3724 struct llist_node *nodes;
3725
3726 nodes = llist_del_all(&req->ctx->poll_llist);
3727 if (nodes)
3728 __io_poll_flush(req->ctx, nodes);
3729}
3730
Jens Axboef0b493e2020-02-01 21:30:11 -07003731static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3732{
3733 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3734
3735 eventfd_signal(req->ctx->cq_ev_fd, 1);
3736 io_put_req(req);
3737}
3738
Jens Axboe221c5eb2019-01-17 09:41:58 -07003739static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3740 void *key)
3741{
Jens Axboee9444752019-11-26 15:02:04 -07003742 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003743 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3744 struct io_ring_ctx *ctx = req->ctx;
3745 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003746
3747 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003748 if (mask && !(mask & poll->events))
3749 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003750
Jens Axboe392edb42019-12-09 17:52:20 -07003751 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003752
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003753 /*
3754 * Run completion inline if we can. We're using trylock here because
3755 * we are violating the completion_lock -> poll wq lock ordering.
3756 * If we have a link timeout we're going to need the completion_lock
3757 * for finalizing the request, mark us as having grabbed that already.
3758 */
Jens Axboee94f1412019-12-19 12:06:02 -07003759 if (mask) {
3760 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003761
Jens Axboee94f1412019-12-19 12:06:02 -07003762 if (llist_empty(&ctx->poll_llist) &&
3763 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003764 bool trigger_ev;
3765
Jens Axboee94f1412019-12-19 12:06:02 -07003766 hash_del(&req->hash_node);
3767 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003768
Jens Axboef0b493e2020-02-01 21:30:11 -07003769 trigger_ev = io_should_trigger_evfd(ctx);
3770 if (trigger_ev && eventfd_signal_count()) {
3771 trigger_ev = false;
3772 req->work.func = io_poll_trigger_evfd;
3773 } else {
3774 req->flags |= REQ_F_COMP_LOCKED;
3775 io_put_req(req);
3776 req = NULL;
3777 }
3778 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3779 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003780 } else {
3781 req->result = mask;
3782 req->llist_node.next = NULL;
3783 /* if the list wasn't empty, we're done */
3784 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3785 req = NULL;
3786 else
3787 req->work.func = io_poll_flush;
3788 }
Jens Axboe8c838782019-03-12 15:48:16 -06003789 }
Jens Axboee94f1412019-12-19 12:06:02 -07003790 if (req)
3791 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003792
Jens Axboe221c5eb2019-01-17 09:41:58 -07003793 return 1;
3794}
3795
3796struct io_poll_table {
3797 struct poll_table_struct pt;
3798 struct io_kiocb *req;
3799 int error;
3800};
3801
3802static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3803 struct poll_table_struct *p)
3804{
3805 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3806
3807 if (unlikely(pt->req->poll.head)) {
3808 pt->error = -EINVAL;
3809 return;
3810 }
3811
3812 pt->error = 0;
3813 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003814 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003815}
3816
Jens Axboeeac406c2019-11-14 12:09:58 -07003817static void io_poll_req_insert(struct io_kiocb *req)
3818{
3819 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003820 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003821
Jens Axboe78076bb2019-12-04 19:56:40 -07003822 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3823 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003824}
3825
Jens Axboe3529d8c2019-12-19 18:24:38 -07003826static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003827{
3828 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003829 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003830
3831 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3832 return -EINVAL;
3833 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3834 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003835 if (!poll->file)
3836 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003837
Jens Axboe221c5eb2019-01-17 09:41:58 -07003838 events = READ_ONCE(sqe->poll_events);
3839 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003840 return 0;
3841}
3842
3843static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3844{
3845 struct io_poll_iocb *poll = &req->poll;
3846 struct io_ring_ctx *ctx = req->ctx;
3847 struct io_poll_table ipt;
3848 bool cancel = false;
3849 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003850
3851 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003852 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003853
Jens Axboe221c5eb2019-01-17 09:41:58 -07003854 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003855 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003856 poll->canceled = false;
3857
3858 ipt.pt._qproc = io_poll_queue_proc;
3859 ipt.pt._key = poll->events;
3860 ipt.req = req;
3861 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3862
3863 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003864 INIT_LIST_HEAD(&poll->wait.entry);
3865 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3866 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003867
Jens Axboe36703242019-07-25 10:20:18 -06003868 INIT_LIST_HEAD(&req->list);
3869
Jens Axboe221c5eb2019-01-17 09:41:58 -07003870 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003871
3872 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003873 if (likely(poll->head)) {
3874 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003875 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003876 if (ipt.error)
3877 cancel = true;
3878 ipt.error = 0;
3879 mask = 0;
3880 }
3881 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003882 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003883 else if (cancel)
3884 WRITE_ONCE(poll->canceled, true);
3885 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003886 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003887 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003888 }
Jens Axboe8c838782019-03-12 15:48:16 -06003889 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003890 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003891 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003892 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003893 spin_unlock_irq(&ctx->completion_lock);
3894
Jens Axboe8c838782019-03-12 15:48:16 -06003895 if (mask) {
3896 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003897 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003898 }
Jens Axboe8c838782019-03-12 15:48:16 -06003899 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003900}
3901
Jens Axboe5262f562019-09-17 12:26:57 -06003902static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3903{
Jens Axboead8a48a2019-11-15 08:49:11 -07003904 struct io_timeout_data *data = container_of(timer,
3905 struct io_timeout_data, timer);
3906 struct io_kiocb *req = data->req;
3907 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003908 unsigned long flags;
3909
Jens Axboe5262f562019-09-17 12:26:57 -06003910 atomic_inc(&ctx->cq_timeouts);
3911
3912 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003913 /*
Jens Axboe11365042019-10-16 09:08:32 -06003914 * We could be racing with timeout deletion. If the list is empty,
3915 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003916 */
Jens Axboe842f9612019-10-29 12:34:10 -06003917 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003918 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003919
Jens Axboe11365042019-10-16 09:08:32 -06003920 /*
3921 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003922 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003923 * pointer will be increased, otherwise other timeout reqs may
3924 * return in advance without waiting for enough wait_nr.
3925 */
3926 prev = req;
3927 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3928 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003929 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003930 }
Jens Axboe842f9612019-10-29 12:34:10 -06003931
Jens Axboe78e19bb2019-11-06 15:21:34 -07003932 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003933 io_commit_cqring(ctx);
3934 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3935
3936 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003937 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003938 io_put_req(req);
3939 return HRTIMER_NORESTART;
3940}
3941
Jens Axboe47f46762019-11-09 17:43:02 -07003942static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3943{
3944 struct io_kiocb *req;
3945 int ret = -ENOENT;
3946
3947 list_for_each_entry(req, &ctx->timeout_list, list) {
3948 if (user_data == req->user_data) {
3949 list_del_init(&req->list);
3950 ret = 0;
3951 break;
3952 }
3953 }
3954
3955 if (ret == -ENOENT)
3956 return ret;
3957
Jens Axboe2d283902019-12-04 11:08:05 -07003958 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003959 if (ret == -1)
3960 return -EALREADY;
3961
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003962 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003963 io_cqring_fill_event(req, -ECANCELED);
3964 io_put_req(req);
3965 return 0;
3966}
3967
Jens Axboe3529d8c2019-12-19 18:24:38 -07003968static int io_timeout_remove_prep(struct io_kiocb *req,
3969 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003970{
Jens Axboeb29472e2019-12-17 18:50:29 -07003971 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3972 return -EINVAL;
3973 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3974 return -EINVAL;
3975
3976 req->timeout.addr = READ_ONCE(sqe->addr);
3977 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3978 if (req->timeout.flags)
3979 return -EINVAL;
3980
Jens Axboeb29472e2019-12-17 18:50:29 -07003981 return 0;
3982}
3983
Jens Axboe11365042019-10-16 09:08:32 -06003984/*
3985 * Remove or update an existing timeout command
3986 */
Jens Axboefc4df992019-12-10 14:38:45 -07003987static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003988{
3989 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003990 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003991
Jens Axboe11365042019-10-16 09:08:32 -06003992 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003993 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003994
Jens Axboe47f46762019-11-09 17:43:02 -07003995 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003996 io_commit_cqring(ctx);
3997 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003998 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003999 if (ret < 0)
4000 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08004001 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06004002 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06004003}
4004
Jens Axboe3529d8c2019-12-19 18:24:38 -07004005static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07004006 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06004007{
Jens Axboead8a48a2019-11-15 08:49:11 -07004008 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06004009 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06004010
Jens Axboead8a48a2019-11-15 08:49:11 -07004011 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06004012 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07004013 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06004014 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07004015 if (sqe->off && is_timeout_link)
4016 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06004017 flags = READ_ONCE(sqe->timeout_flags);
4018 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06004019 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06004020
Jens Axboe26a61672019-12-20 09:02:01 -07004021 req->timeout.count = READ_ONCE(sqe->off);
4022
Jens Axboe3529d8c2019-12-19 18:24:38 -07004023 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07004024 return -ENOMEM;
4025
4026 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07004027 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07004028 req->flags |= REQ_F_TIMEOUT;
4029
4030 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06004031 return -EFAULT;
4032
Jens Axboe11365042019-10-16 09:08:32 -06004033 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07004034 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06004035 else
Jens Axboead8a48a2019-11-15 08:49:11 -07004036 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06004037
Jens Axboead8a48a2019-11-15 08:49:11 -07004038 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
4039 return 0;
4040}
4041
Jens Axboefc4df992019-12-10 14:38:45 -07004042static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07004043{
4044 unsigned count;
4045 struct io_ring_ctx *ctx = req->ctx;
4046 struct io_timeout_data *data;
4047 struct list_head *entry;
4048 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07004049
Jens Axboe2d283902019-12-04 11:08:05 -07004050 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004051
Jens Axboe5262f562019-09-17 12:26:57 -06004052 /*
4053 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07004054 * timeout event to be satisfied. If it isn't set, then this is
4055 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06004056 */
Jens Axboe26a61672019-12-20 09:02:01 -07004057 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004058 if (!count) {
4059 req->flags |= REQ_F_TIMEOUT_NOSEQ;
4060 spin_lock_irq(&ctx->completion_lock);
4061 entry = ctx->timeout_list.prev;
4062 goto add;
4063 }
Jens Axboe5262f562019-09-17 12:26:57 -06004064
4065 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07004066 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06004067
4068 /*
4069 * Insertion sort, ensuring the first entry in the list is always
4070 * the one we need first.
4071 */
Jens Axboe5262f562019-09-17 12:26:57 -06004072 spin_lock_irq(&ctx->completion_lock);
4073 list_for_each_prev(entry, &ctx->timeout_list) {
4074 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08004075 unsigned nxt_sq_head;
4076 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07004077 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06004078
Jens Axboe93bd25b2019-11-11 23:34:31 -07004079 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
4080 continue;
4081
yangerkun5da0fb12019-10-15 21:59:29 +08004082 /*
4083 * Since cached_sq_head + count - 1 can overflow, use type long
4084 * long to store it.
4085 */
4086 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03004087 nxt_sq_head = nxt->sequence - nxt_offset + 1;
4088 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08004089
4090 /*
4091 * cached_sq_head may overflow, and it will never overflow twice
4092 * once there is some timeout req still be valid.
4093 */
4094 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004095 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004096
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004097 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004098 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004099
4100 /*
4101 * Sequence of reqs after the insert one and itself should
4102 * be adjusted because each timeout req consumes a slot.
4103 */
4104 span++;
4105 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004106 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004107 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004108add:
Jens Axboe5262f562019-09-17 12:26:57 -06004109 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004110 data->timer.function = io_timeout_fn;
4111 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004112 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004113 return 0;
4114}
4115
Jens Axboe62755e32019-10-28 21:49:21 -06004116static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004117{
Jens Axboe62755e32019-10-28 21:49:21 -06004118 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004119
Jens Axboe62755e32019-10-28 21:49:21 -06004120 return req->user_data == (unsigned long) data;
4121}
4122
Jens Axboee977d6d2019-11-05 12:39:45 -07004123static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004124{
Jens Axboe62755e32019-10-28 21:49:21 -06004125 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004126 int ret = 0;
4127
Jens Axboe62755e32019-10-28 21:49:21 -06004128 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4129 switch (cancel_ret) {
4130 case IO_WQ_CANCEL_OK:
4131 ret = 0;
4132 break;
4133 case IO_WQ_CANCEL_RUNNING:
4134 ret = -EALREADY;
4135 break;
4136 case IO_WQ_CANCEL_NOTFOUND:
4137 ret = -ENOENT;
4138 break;
4139 }
4140
Jens Axboee977d6d2019-11-05 12:39:45 -07004141 return ret;
4142}
4143
Jens Axboe47f46762019-11-09 17:43:02 -07004144static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4145 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004146 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004147{
4148 unsigned long flags;
4149 int ret;
4150
4151 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4152 if (ret != -ENOENT) {
4153 spin_lock_irqsave(&ctx->completion_lock, flags);
4154 goto done;
4155 }
4156
4157 spin_lock_irqsave(&ctx->completion_lock, flags);
4158 ret = io_timeout_cancel(ctx, sqe_addr);
4159 if (ret != -ENOENT)
4160 goto done;
4161 ret = io_poll_cancel(ctx, sqe_addr);
4162done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004163 if (!ret)
4164 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004165 io_cqring_fill_event(req, ret);
4166 io_commit_cqring(ctx);
4167 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4168 io_cqring_ev_posted(ctx);
4169
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004170 if (ret < 0)
4171 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004172 io_put_req_find_next(req, nxt);
4173}
4174
Jens Axboe3529d8c2019-12-19 18:24:38 -07004175static int io_async_cancel_prep(struct io_kiocb *req,
4176 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004177{
Jens Axboefbf23842019-12-17 18:45:56 -07004178 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004179 return -EINVAL;
4180 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4181 sqe->cancel_flags)
4182 return -EINVAL;
4183
Jens Axboefbf23842019-12-17 18:45:56 -07004184 req->cancel.addr = READ_ONCE(sqe->addr);
4185 return 0;
4186}
4187
4188static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4189{
4190 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004191
4192 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004193 return 0;
4194}
4195
Jens Axboe05f3fb32019-12-09 11:22:50 -07004196static int io_files_update_prep(struct io_kiocb *req,
4197 const struct io_uring_sqe *sqe)
4198{
4199 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4200 return -EINVAL;
4201
4202 req->files_update.offset = READ_ONCE(sqe->off);
4203 req->files_update.nr_args = READ_ONCE(sqe->len);
4204 if (!req->files_update.nr_args)
4205 return -EINVAL;
4206 req->files_update.arg = READ_ONCE(sqe->addr);
4207 return 0;
4208}
4209
4210static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4211{
4212 struct io_ring_ctx *ctx = req->ctx;
4213 struct io_uring_files_update up;
4214 int ret;
4215
Jens Axboef86cd202020-01-29 13:46:44 -07004216 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004217 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004218
4219 up.offset = req->files_update.offset;
4220 up.fds = req->files_update.arg;
4221
4222 mutex_lock(&ctx->uring_lock);
4223 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4224 mutex_unlock(&ctx->uring_lock);
4225
4226 if (ret < 0)
4227 req_set_fail_links(req);
4228 io_cqring_add_event(req, ret);
4229 io_put_req(req);
4230 return 0;
4231}
4232
Jens Axboe3529d8c2019-12-19 18:24:38 -07004233static int io_req_defer_prep(struct io_kiocb *req,
4234 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004235{
Jens Axboee7815732019-12-17 19:45:06 -07004236 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004237
Jens Axboef86cd202020-01-29 13:46:44 -07004238 if (io_op_defs[req->opcode].file_table) {
4239 ret = io_grab_files(req);
4240 if (unlikely(ret))
4241 return ret;
4242 }
4243
Jens Axboecccf0ee2020-01-27 16:34:48 -07004244 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4245
Jens Axboed625c6e2019-12-17 19:53:05 -07004246 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004247 case IORING_OP_NOP:
4248 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004249 case IORING_OP_READV:
4250 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004251 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004252 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004253 break;
4254 case IORING_OP_WRITEV:
4255 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004256 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004257 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004258 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004259 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004260 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004261 break;
4262 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004263 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004264 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004265 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004266 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004267 break;
4268 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004269 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004270 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004271 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004272 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004273 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004274 break;
4275 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004276 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004277 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004278 break;
Jens Axboef499a022019-12-02 16:28:46 -07004279 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004280 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004281 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004282 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004283 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004284 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004285 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004286 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004287 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004288 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004289 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004290 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004291 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004292 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004293 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004294 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004295 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004296 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004297 case IORING_OP_FALLOCATE:
4298 ret = io_fallocate_prep(req, sqe);
4299 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004300 case IORING_OP_OPENAT:
4301 ret = io_openat_prep(req, sqe);
4302 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004303 case IORING_OP_CLOSE:
4304 ret = io_close_prep(req, sqe);
4305 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004306 case IORING_OP_FILES_UPDATE:
4307 ret = io_files_update_prep(req, sqe);
4308 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004309 case IORING_OP_STATX:
4310 ret = io_statx_prep(req, sqe);
4311 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004312 case IORING_OP_FADVISE:
4313 ret = io_fadvise_prep(req, sqe);
4314 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004315 case IORING_OP_MADVISE:
4316 ret = io_madvise_prep(req, sqe);
4317 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004318 case IORING_OP_OPENAT2:
4319 ret = io_openat2_prep(req, sqe);
4320 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004321 case IORING_OP_EPOLL_CTL:
4322 ret = io_epoll_ctl_prep(req, sqe);
4323 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004324 case IORING_OP_SPLICE:
4325 ret = io_splice_prep(req, sqe);
4326 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004327 default:
Jens Axboee7815732019-12-17 19:45:06 -07004328 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4329 req->opcode);
4330 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004331 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004332 }
4333
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004334 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004335}
4336
Jens Axboe3529d8c2019-12-19 18:24:38 -07004337static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004338{
Jackie Liua197f662019-11-08 08:09:12 -07004339 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004340 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004341
Bob Liu9d858b22019-11-13 18:06:25 +08004342 /* Still need defer if there is pending req in defer list. */
4343 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004344 return 0;
4345
Jens Axboe3529d8c2019-12-19 18:24:38 -07004346 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004347 return -EAGAIN;
4348
Jens Axboe3529d8c2019-12-19 18:24:38 -07004349 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004350 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004351 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004352
Jens Axboede0617e2019-04-06 21:51:27 -06004353 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004354 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004355 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004356 return 0;
4357 }
4358
Jens Axboe915967f2019-11-21 09:01:20 -07004359 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004360 list_add_tail(&req->list, &ctx->defer_list);
4361 spin_unlock_irq(&ctx->completion_lock);
4362 return -EIOCBQUEUED;
4363}
4364
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004365static void io_cleanup_req(struct io_kiocb *req)
4366{
4367 struct io_async_ctx *io = req->io;
4368
4369 switch (req->opcode) {
4370 case IORING_OP_READV:
4371 case IORING_OP_READ_FIXED:
4372 case IORING_OP_READ:
4373 case IORING_OP_WRITEV:
4374 case IORING_OP_WRITE_FIXED:
4375 case IORING_OP_WRITE:
4376 if (io->rw.iov != io->rw.fast_iov)
4377 kfree(io->rw.iov);
4378 break;
4379 case IORING_OP_SENDMSG:
4380 case IORING_OP_RECVMSG:
4381 if (io->msg.iov != io->msg.fast_iov)
4382 kfree(io->msg.iov);
4383 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004384 case IORING_OP_OPENAT:
4385 case IORING_OP_OPENAT2:
4386 case IORING_OP_STATX:
4387 putname(req->open.filename);
4388 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004389 case IORING_OP_SPLICE:
4390 io_put_file(req, req->splice.file_in,
4391 (req->splice.flags & SPLICE_F_FD_IN_FIXED));
4392 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004393 }
4394
4395 req->flags &= ~REQ_F_NEED_CLEANUP;
4396}
4397
Jens Axboe3529d8c2019-12-19 18:24:38 -07004398static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4399 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004400{
Jackie Liua197f662019-11-08 08:09:12 -07004401 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004402 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004403
Jens Axboed625c6e2019-12-17 19:53:05 -07004404 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004405 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004406 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004407 break;
4408 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004409 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004410 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004411 if (sqe) {
4412 ret = io_read_prep(req, sqe, force_nonblock);
4413 if (ret < 0)
4414 break;
4415 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004416 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004417 break;
4418 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004419 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004420 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004421 if (sqe) {
4422 ret = io_write_prep(req, sqe, force_nonblock);
4423 if (ret < 0)
4424 break;
4425 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004426 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004427 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004428 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004429 if (sqe) {
4430 ret = io_prep_fsync(req, sqe);
4431 if (ret < 0)
4432 break;
4433 }
Jens Axboefc4df992019-12-10 14:38:45 -07004434 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004435 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004436 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004437 if (sqe) {
4438 ret = io_poll_add_prep(req, sqe);
4439 if (ret)
4440 break;
4441 }
Jens Axboefc4df992019-12-10 14:38:45 -07004442 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004443 break;
4444 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004445 if (sqe) {
4446 ret = io_poll_remove_prep(req, sqe);
4447 if (ret < 0)
4448 break;
4449 }
Jens Axboefc4df992019-12-10 14:38:45 -07004450 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004451 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004452 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004453 if (sqe) {
4454 ret = io_prep_sfr(req, sqe);
4455 if (ret < 0)
4456 break;
4457 }
Jens Axboefc4df992019-12-10 14:38:45 -07004458 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004459 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004460 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004461 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004462 if (sqe) {
4463 ret = io_sendmsg_prep(req, sqe);
4464 if (ret < 0)
4465 break;
4466 }
Jens Axboefddafac2020-01-04 20:19:44 -07004467 if (req->opcode == IORING_OP_SENDMSG)
4468 ret = io_sendmsg(req, nxt, force_nonblock);
4469 else
4470 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004471 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004472 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004473 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004474 if (sqe) {
4475 ret = io_recvmsg_prep(req, sqe);
4476 if (ret)
4477 break;
4478 }
Jens Axboefddafac2020-01-04 20:19:44 -07004479 if (req->opcode == IORING_OP_RECVMSG)
4480 ret = io_recvmsg(req, nxt, force_nonblock);
4481 else
4482 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004483 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004484 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004485 if (sqe) {
4486 ret = io_timeout_prep(req, sqe, false);
4487 if (ret)
4488 break;
4489 }
Jens Axboefc4df992019-12-10 14:38:45 -07004490 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004491 break;
Jens Axboe11365042019-10-16 09:08:32 -06004492 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004493 if (sqe) {
4494 ret = io_timeout_remove_prep(req, sqe);
4495 if (ret)
4496 break;
4497 }
Jens Axboefc4df992019-12-10 14:38:45 -07004498 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004499 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004500 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004501 if (sqe) {
4502 ret = io_accept_prep(req, sqe);
4503 if (ret)
4504 break;
4505 }
Jens Axboefc4df992019-12-10 14:38:45 -07004506 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004507 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004508 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004509 if (sqe) {
4510 ret = io_connect_prep(req, sqe);
4511 if (ret)
4512 break;
4513 }
Jens Axboefc4df992019-12-10 14:38:45 -07004514 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004515 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004516 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004517 if (sqe) {
4518 ret = io_async_cancel_prep(req, sqe);
4519 if (ret)
4520 break;
4521 }
Jens Axboefc4df992019-12-10 14:38:45 -07004522 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004523 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004524 case IORING_OP_FALLOCATE:
4525 if (sqe) {
4526 ret = io_fallocate_prep(req, sqe);
4527 if (ret)
4528 break;
4529 }
4530 ret = io_fallocate(req, nxt, force_nonblock);
4531 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004532 case IORING_OP_OPENAT:
4533 if (sqe) {
4534 ret = io_openat_prep(req, sqe);
4535 if (ret)
4536 break;
4537 }
4538 ret = io_openat(req, nxt, force_nonblock);
4539 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004540 case IORING_OP_CLOSE:
4541 if (sqe) {
4542 ret = io_close_prep(req, sqe);
4543 if (ret)
4544 break;
4545 }
4546 ret = io_close(req, nxt, force_nonblock);
4547 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004548 case IORING_OP_FILES_UPDATE:
4549 if (sqe) {
4550 ret = io_files_update_prep(req, sqe);
4551 if (ret)
4552 break;
4553 }
4554 ret = io_files_update(req, force_nonblock);
4555 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004556 case IORING_OP_STATX:
4557 if (sqe) {
4558 ret = io_statx_prep(req, sqe);
4559 if (ret)
4560 break;
4561 }
4562 ret = io_statx(req, nxt, force_nonblock);
4563 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004564 case IORING_OP_FADVISE:
4565 if (sqe) {
4566 ret = io_fadvise_prep(req, sqe);
4567 if (ret)
4568 break;
4569 }
4570 ret = io_fadvise(req, nxt, force_nonblock);
4571 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004572 case IORING_OP_MADVISE:
4573 if (sqe) {
4574 ret = io_madvise_prep(req, sqe);
4575 if (ret)
4576 break;
4577 }
4578 ret = io_madvise(req, nxt, force_nonblock);
4579 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004580 case IORING_OP_OPENAT2:
4581 if (sqe) {
4582 ret = io_openat2_prep(req, sqe);
4583 if (ret)
4584 break;
4585 }
4586 ret = io_openat2(req, nxt, force_nonblock);
4587 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004588 case IORING_OP_EPOLL_CTL:
4589 if (sqe) {
4590 ret = io_epoll_ctl_prep(req, sqe);
4591 if (ret)
4592 break;
4593 }
4594 ret = io_epoll_ctl(req, nxt, force_nonblock);
4595 break;
Pavel Begunkov7d67af22020-02-24 11:32:45 +03004596 case IORING_OP_SPLICE:
4597 if (sqe) {
4598 ret = io_splice_prep(req, sqe);
4599 if (ret < 0)
4600 break;
4601 }
4602 ret = io_splice(req, nxt, force_nonblock);
4603 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004604 default:
4605 ret = -EINVAL;
4606 break;
4607 }
4608
Jens Axboedef596e2019-01-09 08:59:42 -07004609 if (ret)
4610 return ret;
4611
4612 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004613 const bool in_async = io_wq_current_is_worker();
4614
Jens Axboe9e645e112019-05-10 16:07:28 -06004615 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004616 return -EAGAIN;
4617
Jens Axboe11ba8202020-01-15 21:51:17 -07004618 /* workqueue context doesn't hold uring_lock, grab it now */
4619 if (in_async)
4620 mutex_lock(&ctx->uring_lock);
4621
Jens Axboedef596e2019-01-09 08:59:42 -07004622 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004623
4624 if (in_async)
4625 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004626 }
4627
4628 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004629}
4630
Jens Axboe561fb042019-10-24 07:25:42 -06004631static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004632{
Jens Axboe561fb042019-10-24 07:25:42 -06004633 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004634 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004635 struct io_kiocb *nxt = NULL;
4636 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004637
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004638 /* if NO_CANCEL is set, we must still run the work */
4639 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4640 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004641 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004642 }
Jens Axboe31b51512019-01-18 22:56:34 -07004643
Jens Axboe561fb042019-10-24 07:25:42 -06004644 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06004645 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004646 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004647 /*
4648 * We can get EAGAIN for polled IO even though we're
4649 * forcing a sync submission from here, since we can't
4650 * wait for request slots on the block side.
4651 */
4652 if (ret != -EAGAIN)
4653 break;
4654 cond_resched();
4655 } while (1);
4656 }
Jens Axboe31b51512019-01-18 22:56:34 -07004657
Jens Axboe561fb042019-10-24 07:25:42 -06004658 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004659 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004660
Jens Axboe561fb042019-10-24 07:25:42 -06004661 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004662 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004663 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004664 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004665 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004666
Jens Axboe561fb042019-10-24 07:25:42 -06004667 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004668 if (!ret && nxt)
4669 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004670}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004671
Jens Axboe15b71ab2019-12-11 11:20:36 -07004672static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004673{
Jens Axboed3656342019-12-18 09:50:26 -07004674 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004675 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004676 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004677 return 0;
4678 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004679}
4680
Jens Axboe65e19f52019-10-26 07:20:21 -06004681static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4682 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004683{
Jens Axboe65e19f52019-10-26 07:20:21 -06004684 struct fixed_file_table *table;
4685
Jens Axboe05f3fb32019-12-09 11:22:50 -07004686 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4687 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004688}
4689
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004690static int io_file_get(struct io_submit_state *state, struct io_kiocb *req,
4691 int fd, struct file **out_file, bool fixed)
4692{
4693 struct io_ring_ctx *ctx = req->ctx;
4694 struct file *file;
4695
4696 if (fixed) {
4697 if (unlikely(!ctx->file_data ||
4698 (unsigned) fd >= ctx->nr_user_files))
4699 return -EBADF;
4700 fd = array_index_nospec(fd, ctx->nr_user_files);
4701 file = io_file_from_index(ctx, fd);
4702 if (!file)
4703 return -EBADF;
4704 percpu_ref_get(&ctx->file_data->refs);
4705 } else {
4706 trace_io_uring_file_get(ctx, fd);
4707 file = __io_file_get(state, fd);
4708 if (unlikely(!file))
4709 return -EBADF;
4710 }
4711
4712 *out_file = file;
4713 return 0;
4714}
4715
Jens Axboe3529d8c2019-12-19 18:24:38 -07004716static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4717 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004718{
4719 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004720 int fd;
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004721 bool fixed;
Jens Axboe09bb8392019-03-13 12:39:28 -06004722
Jens Axboe3529d8c2019-12-19 18:24:38 -07004723 flags = READ_ONCE(sqe->flags);
4724 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004725
Jens Axboed3656342019-12-18 09:50:26 -07004726 if (!io_req_needs_file(req, fd))
4727 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004728
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004729 fixed = (flags & IOSQE_FIXED_FILE);
4730 if (unlikely(!fixed && req->needs_fixed_file))
4731 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004732
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004733 return io_file_get(state, req, fd, &req->file, fixed);
Jens Axboe09bb8392019-03-13 12:39:28 -06004734}
4735
Jackie Liua197f662019-11-08 08:09:12 -07004736static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004737{
Jens Axboefcb323c2019-10-24 12:39:47 -06004738 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004739 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004740
Jens Axboef86cd202020-01-29 13:46:44 -07004741 if (req->work.files)
4742 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004743 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004744 return -EBADF;
4745
Jens Axboefcb323c2019-10-24 12:39:47 -06004746 rcu_read_lock();
4747 spin_lock_irq(&ctx->inflight_lock);
4748 /*
4749 * We use the f_ops->flush() handler to ensure that we can flush
4750 * out work accessing these files if the fd is closed. Check if
4751 * the fd has changed since we started down this path, and disallow
4752 * this operation if it has.
4753 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004754 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004755 list_add(&req->inflight_entry, &ctx->inflight_list);
4756 req->flags |= REQ_F_INFLIGHT;
4757 req->work.files = current->files;
4758 ret = 0;
4759 }
4760 spin_unlock_irq(&ctx->inflight_lock);
4761 rcu_read_unlock();
4762
4763 return ret;
4764}
4765
Jens Axboe2665abf2019-11-05 12:40:47 -07004766static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4767{
Jens Axboead8a48a2019-11-15 08:49:11 -07004768 struct io_timeout_data *data = container_of(timer,
4769 struct io_timeout_data, timer);
4770 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004771 struct io_ring_ctx *ctx = req->ctx;
4772 struct io_kiocb *prev = NULL;
4773 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004774
4775 spin_lock_irqsave(&ctx->completion_lock, flags);
4776
4777 /*
4778 * We don't expect the list to be empty, that will only happen if we
4779 * race with the completion of the linked work.
4780 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004781 if (!list_empty(&req->link_list)) {
4782 prev = list_entry(req->link_list.prev, struct io_kiocb,
4783 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004784 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004785 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004786 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4787 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004788 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004789 }
4790
4791 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4792
4793 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004794 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004795 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4796 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004797 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004798 } else {
4799 io_cqring_add_event(req, -ETIME);
4800 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004801 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004802 return HRTIMER_NORESTART;
4803}
4804
Jens Axboead8a48a2019-11-15 08:49:11 -07004805static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004806{
Jens Axboe76a46e02019-11-10 23:34:16 -07004807 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004808
Jens Axboe76a46e02019-11-10 23:34:16 -07004809 /*
4810 * If the list is now empty, then our linked request finished before
4811 * we got a chance to setup the timer
4812 */
4813 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004814 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004815 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004816
Jens Axboead8a48a2019-11-15 08:49:11 -07004817 data->timer.function = io_link_timeout_fn;
4818 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4819 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004820 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004821 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004822
Jens Axboe2665abf2019-11-05 12:40:47 -07004823 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004824 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004825}
4826
Jens Axboead8a48a2019-11-15 08:49:11 -07004827static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004828{
4829 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004830
Jens Axboe2665abf2019-11-05 12:40:47 -07004831 if (!(req->flags & REQ_F_LINK))
4832 return NULL;
4833
Pavel Begunkov44932332019-12-05 16:16:35 +03004834 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4835 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004836 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004837 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004838
Jens Axboe76a46e02019-11-10 23:34:16 -07004839 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004840 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004841}
4842
Jens Axboe3529d8c2019-12-19 18:24:38 -07004843static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004844{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004845 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004846 struct io_kiocb *nxt = NULL;
Jens Axboe193155c2020-02-22 23:22:19 -07004847 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004848 int ret;
4849
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004850again:
4851 linked_timeout = io_prep_linked_timeout(req);
4852
Jens Axboe193155c2020-02-22 23:22:19 -07004853 if (req->work.creds && req->work.creds != current_cred()) {
4854 if (old_creds)
4855 revert_creds(old_creds);
4856 if (old_creds == req->work.creds)
4857 old_creds = NULL; /* restored original creds */
4858 else
4859 old_creds = override_creds(req->work.creds);
4860 }
4861
Jens Axboe3529d8c2019-12-19 18:24:38 -07004862 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004863
4864 /*
4865 * We async punt it if the file wasn't marked NOWAIT, or if the file
4866 * doesn't support non-blocking read/write attempts
4867 */
4868 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4869 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004870punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004871 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004872 ret = io_grab_files(req);
4873 if (ret)
4874 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004875 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004876
4877 /*
4878 * Queued up for async execution, worker will release
4879 * submit reference when the iocb is actually submitted.
4880 */
4881 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004882 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004883 }
Jens Axboee65ef562019-03-12 10:16:44 -06004884
Jens Axboefcb323c2019-10-24 12:39:47 -06004885err:
Jens Axboee65ef562019-03-12 10:16:44 -06004886 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004887 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004888
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004889 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004890 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004891 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004892 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004893 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004894 }
4895
Jens Axboee65ef562019-03-12 10:16:44 -06004896 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004897 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004898 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004899 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004900 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004901 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004902done_req:
4903 if (nxt) {
4904 req = nxt;
4905 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004906
4907 if (req->flags & REQ_F_FORCE_ASYNC)
4908 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004909 goto again;
4910 }
Jens Axboe193155c2020-02-22 23:22:19 -07004911 if (old_creds)
4912 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004913}
4914
Jens Axboe3529d8c2019-12-19 18:24:38 -07004915static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004916{
4917 int ret;
4918
Jens Axboe3529d8c2019-12-19 18:24:38 -07004919 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004920 if (ret) {
4921 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004922fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004923 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004924 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004925 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004926 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004927 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004928 ret = io_req_defer_prep(req, sqe);
4929 if (unlikely(ret < 0))
4930 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004931 /*
4932 * Never try inline submit of IOSQE_ASYNC is set, go straight
4933 * to async execution.
4934 */
4935 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4936 io_queue_async_work(req);
4937 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004938 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004939 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004940}
4941
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004942static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004943{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004944 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004945 io_cqring_add_event(req, -ECANCELED);
4946 io_double_put_req(req);
4947 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004948 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004949}
4950
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004951#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004952 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004953
Jens Axboe3529d8c2019-12-19 18:24:38 -07004954static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4955 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004956{
Jackie Liua197f662019-11-08 08:09:12 -07004957 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004958 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004959 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004960
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004961 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004962
4963 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004964 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004965 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004966 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004967 }
4968
Jens Axboe75c6a032020-01-28 10:15:23 -07004969 id = READ_ONCE(sqe->personality);
4970 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07004971 req->work.creds = idr_find(&ctx->personality_idr, id);
4972 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07004973 ret = -EINVAL;
4974 goto err_req;
4975 }
Jens Axboe193155c2020-02-22 23:22:19 -07004976 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07004977 }
4978
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004979 /* same numerical values with corresponding REQ_F_*, safe to copy */
Pavel Begunkov8da11c12020-02-24 11:32:44 +03004980 req->flags |= sqe_flags & (IOSQE_IO_DRAIN | IOSQE_IO_HARDLINK |
4981 IOSQE_ASYNC | IOSQE_FIXED_FILE);
Jens Axboe9e645e112019-05-10 16:07:28 -06004982
Jens Axboe3529d8c2019-12-19 18:24:38 -07004983 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004984 if (unlikely(ret)) {
4985err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004986 io_cqring_add_event(req, ret);
4987 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004988 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004989 }
4990
Jens Axboe9e645e112019-05-10 16:07:28 -06004991 /*
4992 * If we already have a head request, queue this one for async
4993 * submittal once the head completes. If we don't have a head but
4994 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4995 * submitted sync once the chain is complete. If none of those
4996 * conditions are true (normal request), then just queue it.
4997 */
4998 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004999 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06005000
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03005001 /*
5002 * Taking sequential execution of a link, draining both sides
5003 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
5004 * requests in the link. So, it drains the head and the
5005 * next after the link request. The last one is done via
5006 * drain_next flag to persist the effect across calls.
5007 */
Pavel Begunkov711be032020-01-17 03:57:59 +03005008 if (sqe_flags & IOSQE_IO_DRAIN) {
5009 head->flags |= REQ_F_IO_DRAIN;
5010 ctx->drain_next = 1;
5011 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07005012 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06005013 ret = -EAGAIN;
5014 goto err_req;
5015 }
5016
Jens Axboe3529d8c2019-12-19 18:24:38 -07005017 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07005018 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07005019 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03005020 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07005021 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07005022 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03005023 trace_io_uring_link(ctx, req, head);
5024 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06005025
Pavel Begunkov32fe5252019-12-17 22:26:58 +03005026 /* last request of a link, enqueue the link */
5027 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
5028 io_queue_link_head(head);
5029 *link = NULL;
5030 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005031 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03005032 if (unlikely(ctx->drain_next)) {
5033 req->flags |= REQ_F_IO_DRAIN;
5034 req->ctx->drain_next = 0;
5035 }
5036 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
5037 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03005038 INIT_LIST_HEAD(&req->link_list);
5039 ret = io_req_defer_prep(req, sqe);
5040 if (ret)
5041 req->flags |= REQ_F_FAIL_LINK;
5042 *link = req;
5043 } else {
5044 io_queue_sqe(req, sqe);
5045 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005046 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005047
5048 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06005049}
5050
Jens Axboe9a56a232019-01-09 09:06:50 -07005051/*
5052 * Batched submission is done, ensure local IO is flushed out.
5053 */
5054static void io_submit_state_end(struct io_submit_state *state)
5055{
5056 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06005057 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07005058 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03005059 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07005060}
5061
5062/*
5063 * Start submission side cache.
5064 */
5065static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08005066 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07005067{
5068 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07005069 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07005070 state->file = NULL;
5071 state->ios_left = max_ios;
5072}
5073
Jens Axboe2b188cc2019-01-07 10:46:33 -07005074static void io_commit_sqring(struct io_ring_ctx *ctx)
5075{
Hristo Venev75b28af2019-08-26 17:23:46 +00005076 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005077
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03005078 /*
5079 * Ensure any loads from the SQEs are done at this point,
5080 * since once we write the new head, the application could
5081 * write new data to them.
5082 */
5083 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005084}
5085
5086/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07005087 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07005088 * that is mapped by userspace. This means that care needs to be taken to
5089 * ensure that reads are stable, as we cannot rely on userspace always
5090 * being a good citizen. If members of the sqe are validated and then later
5091 * used, it's important that those reads are done through READ_ONCE() to
5092 * prevent a re-load down the line.
5093 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07005094static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
5095 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005096{
Hristo Venev75b28af2019-08-26 17:23:46 +00005097 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005098 unsigned head;
5099
5100 /*
5101 * The cached sq head (or cq tail) serves two purposes:
5102 *
5103 * 1) allows us to batch the cost of updating the user visible
5104 * head updates.
5105 * 2) allows the kernel side to track the head on its own, even
5106 * though the application is the one updating it.
5107 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005108 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03005109 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005110 /*
5111 * All io need record the previous position, if LINK vs DARIN,
5112 * it can be used to mark the position of the first IO in the
5113 * link list.
5114 */
5115 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07005116 *sqe_ptr = &ctx->sq_sqes[head];
5117 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5118 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005119 ctx->cached_sq_head++;
5120 return true;
5121 }
5122
5123 /* drop invalid entries */
5124 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005125 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005126 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005127 return false;
5128}
5129
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005130static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005131 struct file *ring_file, int ring_fd,
5132 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005133{
5134 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005135 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005136 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005137 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005138
Jens Axboec4a2ed72019-11-21 21:01:26 -07005139 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005140 if (test_bit(0, &ctx->sq_check_overflow)) {
5141 if (!list_empty(&ctx->cq_overflow_list) &&
5142 !io_cqring_overflow_flush(ctx, false))
5143 return -EBUSY;
5144 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005145
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005146 /* make sure SQ entry isn't read before tail */
5147 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005148
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005149 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5150 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005151
5152 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005153 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005154 statep = &state;
5155 }
5156
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005157 ctx->ring_fd = ring_fd;
5158 ctx->ring_file = ring_file;
5159
Jens Axboe6c271ce2019-01-10 11:22:30 -07005160 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005161 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005162 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005163 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005164
Pavel Begunkov196be952019-11-07 01:41:06 +03005165 req = io_get_req(ctx, statep);
5166 if (unlikely(!req)) {
5167 if (!submitted)
5168 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005169 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005170 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005171 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005172 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005173 break;
5174 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005175
Jens Axboed3656342019-12-18 09:50:26 -07005176 /* will complete beyond this point, count as submitted */
5177 submitted++;
5178
5179 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005180 err = -EINVAL;
5181fail_req:
5182 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005183 io_double_put_req(req);
5184 break;
5185 }
5186
5187 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005188 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005189 if (unlikely(mm_fault)) {
5190 err = -EFAULT;
5191 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005192 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005193 use_mm(ctx->sqo_mm);
5194 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005195 }
5196
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005197 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005198 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5199 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005200 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005201 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005202 }
5203
Pavel Begunkov9466f432020-01-25 22:34:01 +03005204 if (unlikely(submitted != nr)) {
5205 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5206
5207 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5208 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005209 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005210 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005211 if (statep)
5212 io_submit_state_end(&state);
5213
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005214 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5215 io_commit_sqring(ctx);
5216
Jens Axboe6c271ce2019-01-10 11:22:30 -07005217 return submitted;
5218}
5219
5220static int io_sq_thread(void *data)
5221{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005222 struct io_ring_ctx *ctx = data;
5223 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005224 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005225 mm_segment_t old_fs;
5226 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005227 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005228 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005229
Jens Axboe206aefd2019-11-07 18:27:42 -07005230 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005231
Jens Axboe6c271ce2019-01-10 11:22:30 -07005232 old_fs = get_fs();
5233 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005234 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005235
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005236 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005237 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005238 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005239
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005240 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005241 unsigned nr_events = 0;
5242
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005243 mutex_lock(&ctx->uring_lock);
5244 if (!list_empty(&ctx->poll_list))
5245 io_iopoll_getevents(ctx, &nr_events, 0);
5246 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005247 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005248 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005249 }
5250
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005251 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005252
5253 /*
5254 * If submit got -EBUSY, flag us as needing the application
5255 * to enter the kernel to reap and flush events.
5256 */
5257 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005258 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005259 * Drop cur_mm before scheduling, we can't hold it for
5260 * long periods (or over schedule()). Do this before
5261 * adding ourselves to the waitqueue, as the unuse/drop
5262 * may sleep.
5263 */
5264 if (cur_mm) {
5265 unuse_mm(cur_mm);
5266 mmput(cur_mm);
5267 cur_mm = NULL;
5268 }
5269
5270 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005271 * We're polling. If we're within the defined idle
5272 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005273 * to sleep. The exception is if we got EBUSY doing
5274 * more IO, we should wait for the application to
5275 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005276 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005277 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005278 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5279 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005280 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005281 continue;
5282 }
5283
Jens Axboe6c271ce2019-01-10 11:22:30 -07005284 prepare_to_wait(&ctx->sqo_wait, &wait,
5285 TASK_INTERRUPTIBLE);
5286
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005287 /*
5288 * While doing polled IO, before going to sleep, we need
5289 * to check if there are new reqs added to poll_list, it
5290 * is because reqs may have been punted to io worker and
5291 * will be added to poll_list later, hence check the
5292 * poll_list again.
5293 */
5294 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5295 !list_empty_careful(&ctx->poll_list)) {
5296 finish_wait(&ctx->sqo_wait, &wait);
5297 continue;
5298 }
5299
Jens Axboe6c271ce2019-01-10 11:22:30 -07005300 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005301 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005302 /* make sure to read SQ tail after writing flags */
5303 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005304
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005305 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005306 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005307 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005308 finish_wait(&ctx->sqo_wait, &wait);
5309 break;
5310 }
5311 if (signal_pending(current))
5312 flush_signals(current);
5313 schedule();
5314 finish_wait(&ctx->sqo_wait, &wait);
5315
Hristo Venev75b28af2019-08-26 17:23:46 +00005316 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005317 continue;
5318 }
5319 finish_wait(&ctx->sqo_wait, &wait);
5320
Hristo Venev75b28af2019-08-26 17:23:46 +00005321 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005322 }
5323
Jens Axboe8a4955f2019-12-09 14:52:35 -07005324 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005325 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005326 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005327 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005328 }
5329
5330 set_fs(old_fs);
5331 if (cur_mm) {
5332 unuse_mm(cur_mm);
5333 mmput(cur_mm);
5334 }
Jens Axboe181e4482019-11-25 08:52:30 -07005335 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005336
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005337 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005338
Jens Axboe6c271ce2019-01-10 11:22:30 -07005339 return 0;
5340}
5341
Jens Axboebda52162019-09-24 13:47:15 -06005342struct io_wait_queue {
5343 struct wait_queue_entry wq;
5344 struct io_ring_ctx *ctx;
5345 unsigned to_wait;
5346 unsigned nr_timeouts;
5347};
5348
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005349static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005350{
5351 struct io_ring_ctx *ctx = iowq->ctx;
5352
5353 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005354 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005355 * started waiting. For timeouts, we always want to return to userspace,
5356 * regardless of event count.
5357 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005358 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005359 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5360}
5361
5362static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5363 int wake_flags, void *key)
5364{
5365 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5366 wq);
5367
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005368 /* use noflush == true, as we can't safely rely on locking context */
5369 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005370 return -1;
5371
5372 return autoremove_wake_function(curr, mode, wake_flags, key);
5373}
5374
Jens Axboe2b188cc2019-01-07 10:46:33 -07005375/*
5376 * Wait until events become available, if we don't already have some. The
5377 * application must reap them itself, as they reside on the shared cq ring.
5378 */
5379static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5380 const sigset_t __user *sig, size_t sigsz)
5381{
Jens Axboebda52162019-09-24 13:47:15 -06005382 struct io_wait_queue iowq = {
5383 .wq = {
5384 .private = current,
5385 .func = io_wake_function,
5386 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5387 },
5388 .ctx = ctx,
5389 .to_wait = min_events,
5390 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005391 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005392 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005393
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005394 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005395 return 0;
5396
5397 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005398#ifdef CONFIG_COMPAT
5399 if (in_compat_syscall())
5400 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005401 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005402 else
5403#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005404 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005405
Jens Axboe2b188cc2019-01-07 10:46:33 -07005406 if (ret)
5407 return ret;
5408 }
5409
Jens Axboebda52162019-09-24 13:47:15 -06005410 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005411 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005412 do {
5413 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5414 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005415 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005416 break;
5417 schedule();
5418 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005419 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005420 break;
5421 }
5422 } while (1);
5423 finish_wait(&ctx->wait, &iowq.wq);
5424
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005425 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005426
Hristo Venev75b28af2019-08-26 17:23:46 +00005427 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005428}
5429
Jens Axboe6b063142019-01-10 22:13:58 -07005430static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5431{
5432#if defined(CONFIG_UNIX)
5433 if (ctx->ring_sock) {
5434 struct sock *sock = ctx->ring_sock->sk;
5435 struct sk_buff *skb;
5436
5437 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5438 kfree_skb(skb);
5439 }
5440#else
5441 int i;
5442
Jens Axboe65e19f52019-10-26 07:20:21 -06005443 for (i = 0; i < ctx->nr_user_files; i++) {
5444 struct file *file;
5445
5446 file = io_file_from_index(ctx, i);
5447 if (file)
5448 fput(file);
5449 }
Jens Axboe6b063142019-01-10 22:13:58 -07005450#endif
5451}
5452
Jens Axboe05f3fb32019-12-09 11:22:50 -07005453static void io_file_ref_kill(struct percpu_ref *ref)
5454{
5455 struct fixed_file_data *data;
5456
5457 data = container_of(ref, struct fixed_file_data, refs);
5458 complete(&data->done);
5459}
5460
Jens Axboe6b063142019-01-10 22:13:58 -07005461static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5462{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005463 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005464 unsigned nr_tables, i;
5465
Jens Axboe05f3fb32019-12-09 11:22:50 -07005466 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005467 return -ENXIO;
5468
Jens Axboe05f3fb32019-12-09 11:22:50 -07005469 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005470 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005471 wait_for_completion(&data->done);
5472 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005473 percpu_ref_exit(&data->refs);
5474
Jens Axboe6b063142019-01-10 22:13:58 -07005475 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005476 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5477 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005478 kfree(data->table[i].files);
5479 kfree(data->table);
5480 kfree(data);
5481 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005482 ctx->nr_user_files = 0;
5483 return 0;
5484}
5485
Jens Axboe6c271ce2019-01-10 11:22:30 -07005486static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5487{
5488 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005489 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005490 /*
5491 * The park is a bit of a work-around, without it we get
5492 * warning spews on shutdown with SQPOLL set and affinity
5493 * set to a single CPU.
5494 */
Jens Axboe06058632019-04-13 09:26:03 -06005495 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005496 kthread_stop(ctx->sqo_thread);
5497 ctx->sqo_thread = NULL;
5498 }
5499}
5500
Jens Axboe6b063142019-01-10 22:13:58 -07005501static void io_finish_async(struct io_ring_ctx *ctx)
5502{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005503 io_sq_thread_stop(ctx);
5504
Jens Axboe561fb042019-10-24 07:25:42 -06005505 if (ctx->io_wq) {
5506 io_wq_destroy(ctx->io_wq);
5507 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005508 }
5509}
5510
5511#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005512/*
5513 * Ensure the UNIX gc is aware of our file set, so we are certain that
5514 * the io_uring can be safely unregistered on process exit, even if we have
5515 * loops in the file referencing.
5516 */
5517static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5518{
5519 struct sock *sk = ctx->ring_sock->sk;
5520 struct scm_fp_list *fpl;
5521 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005522 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005523
5524 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5525 unsigned long inflight = ctx->user->unix_inflight + nr;
5526
5527 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5528 return -EMFILE;
5529 }
5530
5531 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5532 if (!fpl)
5533 return -ENOMEM;
5534
5535 skb = alloc_skb(0, GFP_KERNEL);
5536 if (!skb) {
5537 kfree(fpl);
5538 return -ENOMEM;
5539 }
5540
5541 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005542
Jens Axboe08a45172019-10-03 08:11:03 -06005543 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005544 fpl->user = get_uid(ctx->user);
5545 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005546 struct file *file = io_file_from_index(ctx, i + offset);
5547
5548 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005549 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005550 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005551 unix_inflight(fpl->user, fpl->fp[nr_files]);
5552 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005553 }
5554
Jens Axboe08a45172019-10-03 08:11:03 -06005555 if (nr_files) {
5556 fpl->max = SCM_MAX_FD;
5557 fpl->count = nr_files;
5558 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005559 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005560 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5561 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005562
Jens Axboe08a45172019-10-03 08:11:03 -06005563 for (i = 0; i < nr_files; i++)
5564 fput(fpl->fp[i]);
5565 } else {
5566 kfree_skb(skb);
5567 kfree(fpl);
5568 }
Jens Axboe6b063142019-01-10 22:13:58 -07005569
5570 return 0;
5571}
5572
5573/*
5574 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5575 * causes regular reference counting to break down. We rely on the UNIX
5576 * garbage collection to take care of this problem for us.
5577 */
5578static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5579{
5580 unsigned left, total;
5581 int ret = 0;
5582
5583 total = 0;
5584 left = ctx->nr_user_files;
5585 while (left) {
5586 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005587
5588 ret = __io_sqe_files_scm(ctx, this_files, total);
5589 if (ret)
5590 break;
5591 left -= this_files;
5592 total += this_files;
5593 }
5594
5595 if (!ret)
5596 return 0;
5597
5598 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005599 struct file *file = io_file_from_index(ctx, total);
5600
5601 if (file)
5602 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005603 total++;
5604 }
5605
5606 return ret;
5607}
5608#else
5609static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5610{
5611 return 0;
5612}
5613#endif
5614
Jens Axboe65e19f52019-10-26 07:20:21 -06005615static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5616 unsigned nr_files)
5617{
5618 int i;
5619
5620 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005621 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005622 unsigned this_files;
5623
5624 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5625 table->files = kcalloc(this_files, sizeof(struct file *),
5626 GFP_KERNEL);
5627 if (!table->files)
5628 break;
5629 nr_files -= this_files;
5630 }
5631
5632 if (i == nr_tables)
5633 return 0;
5634
5635 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005636 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005637 kfree(table->files);
5638 }
5639 return 1;
5640}
5641
Jens Axboe05f3fb32019-12-09 11:22:50 -07005642static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005643{
5644#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005645 struct sock *sock = ctx->ring_sock->sk;
5646 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5647 struct sk_buff *skb;
5648 int i;
5649
5650 __skb_queue_head_init(&list);
5651
5652 /*
5653 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5654 * remove this entry and rearrange the file array.
5655 */
5656 skb = skb_dequeue(head);
5657 while (skb) {
5658 struct scm_fp_list *fp;
5659
5660 fp = UNIXCB(skb).fp;
5661 for (i = 0; i < fp->count; i++) {
5662 int left;
5663
5664 if (fp->fp[i] != file)
5665 continue;
5666
5667 unix_notinflight(fp->user, fp->fp[i]);
5668 left = fp->count - 1 - i;
5669 if (left) {
5670 memmove(&fp->fp[i], &fp->fp[i + 1],
5671 left * sizeof(struct file *));
5672 }
5673 fp->count--;
5674 if (!fp->count) {
5675 kfree_skb(skb);
5676 skb = NULL;
5677 } else {
5678 __skb_queue_tail(&list, skb);
5679 }
5680 fput(file);
5681 file = NULL;
5682 break;
5683 }
5684
5685 if (!file)
5686 break;
5687
5688 __skb_queue_tail(&list, skb);
5689
5690 skb = skb_dequeue(head);
5691 }
5692
5693 if (skb_peek(&list)) {
5694 spin_lock_irq(&head->lock);
5695 while ((skb = __skb_dequeue(&list)) != NULL)
5696 __skb_queue_tail(head, skb);
5697 spin_unlock_irq(&head->lock);
5698 }
5699#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005700 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005701#endif
5702}
5703
Jens Axboe05f3fb32019-12-09 11:22:50 -07005704struct io_file_put {
5705 struct llist_node llist;
5706 struct file *file;
5707 struct completion *done;
5708};
5709
Jens Axboe2faf8522020-02-04 19:54:55 -07005710static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005711{
5712 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005713 struct llist_node *node;
5714
Jens Axboe05f3fb32019-12-09 11:22:50 -07005715 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5716 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5717 io_ring_file_put(data->ctx, pfile->file);
5718 if (pfile->done)
5719 complete(pfile->done);
5720 else
5721 kfree(pfile);
5722 }
5723 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005724}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005725
Jens Axboe2faf8522020-02-04 19:54:55 -07005726static void io_ring_file_ref_switch(struct work_struct *work)
5727{
5728 struct fixed_file_data *data;
5729
5730 data = container_of(work, struct fixed_file_data, ref_work);
5731 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005732 percpu_ref_switch_to_percpu(&data->refs);
5733}
5734
5735static void io_file_data_ref_zero(struct percpu_ref *ref)
5736{
5737 struct fixed_file_data *data;
5738
5739 data = container_of(ref, struct fixed_file_data, refs);
5740
Jens Axboe2faf8522020-02-04 19:54:55 -07005741 /*
5742 * We can't safely switch from inside this context, punt to wq. If
5743 * the table ref is going away, the table is being unregistered.
5744 * Don't queue up the async work for that case, the caller will
5745 * handle it.
5746 */
5747 if (!percpu_ref_is_dying(&data->refs))
5748 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005749}
5750
5751static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5752 unsigned nr_args)
5753{
5754 __s32 __user *fds = (__s32 __user *) arg;
5755 unsigned nr_tables;
5756 struct file *file;
5757 int fd, ret = 0;
5758 unsigned i;
5759
5760 if (ctx->file_data)
5761 return -EBUSY;
5762 if (!nr_args)
5763 return -EINVAL;
5764 if (nr_args > IORING_MAX_FIXED_FILES)
5765 return -EMFILE;
5766
5767 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5768 if (!ctx->file_data)
5769 return -ENOMEM;
5770 ctx->file_data->ctx = ctx;
5771 init_completion(&ctx->file_data->done);
5772
5773 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5774 ctx->file_data->table = kcalloc(nr_tables,
5775 sizeof(struct fixed_file_table),
5776 GFP_KERNEL);
5777 if (!ctx->file_data->table) {
5778 kfree(ctx->file_data);
5779 ctx->file_data = NULL;
5780 return -ENOMEM;
5781 }
5782
5783 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5784 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5785 kfree(ctx->file_data->table);
5786 kfree(ctx->file_data);
5787 ctx->file_data = NULL;
5788 return -ENOMEM;
5789 }
5790 ctx->file_data->put_llist.first = NULL;
5791 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5792
5793 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5794 percpu_ref_exit(&ctx->file_data->refs);
5795 kfree(ctx->file_data->table);
5796 kfree(ctx->file_data);
5797 ctx->file_data = NULL;
5798 return -ENOMEM;
5799 }
5800
5801 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5802 struct fixed_file_table *table;
5803 unsigned index;
5804
5805 ret = -EFAULT;
5806 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5807 break;
5808 /* allow sparse sets */
5809 if (fd == -1) {
5810 ret = 0;
5811 continue;
5812 }
5813
5814 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5815 index = i & IORING_FILE_TABLE_MASK;
5816 file = fget(fd);
5817
5818 ret = -EBADF;
5819 if (!file)
5820 break;
5821
5822 /*
5823 * Don't allow io_uring instances to be registered. If UNIX
5824 * isn't enabled, then this causes a reference cycle and this
5825 * instance can never get freed. If UNIX is enabled we'll
5826 * handle it just fine, but there's still no point in allowing
5827 * a ring fd as it doesn't support regular read/write anyway.
5828 */
5829 if (file->f_op == &io_uring_fops) {
5830 fput(file);
5831 break;
5832 }
5833 ret = 0;
5834 table->files[index] = file;
5835 }
5836
5837 if (ret) {
5838 for (i = 0; i < ctx->nr_user_files; i++) {
5839 file = io_file_from_index(ctx, i);
5840 if (file)
5841 fput(file);
5842 }
5843 for (i = 0; i < nr_tables; i++)
5844 kfree(ctx->file_data->table[i].files);
5845
5846 kfree(ctx->file_data->table);
5847 kfree(ctx->file_data);
5848 ctx->file_data = NULL;
5849 ctx->nr_user_files = 0;
5850 return ret;
5851 }
5852
5853 ret = io_sqe_files_scm(ctx);
5854 if (ret)
5855 io_sqe_files_unregister(ctx);
5856
5857 return ret;
5858}
5859
Jens Axboec3a31e62019-10-03 13:59:56 -06005860static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5861 int index)
5862{
5863#if defined(CONFIG_UNIX)
5864 struct sock *sock = ctx->ring_sock->sk;
5865 struct sk_buff_head *head = &sock->sk_receive_queue;
5866 struct sk_buff *skb;
5867
5868 /*
5869 * See if we can merge this file into an existing skb SCM_RIGHTS
5870 * file set. If there's no room, fall back to allocating a new skb
5871 * and filling it in.
5872 */
5873 spin_lock_irq(&head->lock);
5874 skb = skb_peek(head);
5875 if (skb) {
5876 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5877
5878 if (fpl->count < SCM_MAX_FD) {
5879 __skb_unlink(skb, head);
5880 spin_unlock_irq(&head->lock);
5881 fpl->fp[fpl->count] = get_file(file);
5882 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5883 fpl->count++;
5884 spin_lock_irq(&head->lock);
5885 __skb_queue_head(head, skb);
5886 } else {
5887 skb = NULL;
5888 }
5889 }
5890 spin_unlock_irq(&head->lock);
5891
5892 if (skb) {
5893 fput(file);
5894 return 0;
5895 }
5896
5897 return __io_sqe_files_scm(ctx, 1, index);
5898#else
5899 return 0;
5900#endif
5901}
5902
Jens Axboe05f3fb32019-12-09 11:22:50 -07005903static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005904{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005905 struct fixed_file_data *data;
5906
Jens Axboedd3db2a2020-02-26 10:23:43 -07005907 /*
5908 * Juggle reference to ensure we hit zero, if needed, so we can
5909 * switch back to percpu mode
5910 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005911 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07005912 percpu_ref_put(&data->refs);
5913 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005914}
5915
5916static bool io_queue_file_removal(struct fixed_file_data *data,
5917 struct file *file)
5918{
5919 struct io_file_put *pfile, pfile_stack;
5920 DECLARE_COMPLETION_ONSTACK(done);
5921
5922 /*
5923 * If we fail allocating the struct we need for doing async reomval
5924 * of this file, just punt to sync and wait for it.
5925 */
5926 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5927 if (!pfile) {
5928 pfile = &pfile_stack;
5929 pfile->done = &done;
5930 }
5931
5932 pfile->file = file;
5933 llist_add(&pfile->llist, &data->put_llist);
5934
5935 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07005936 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005937 wait_for_completion(&done);
5938 flush_work(&data->ref_work);
5939 return false;
5940 }
5941
5942 return true;
5943}
5944
5945static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5946 struct io_uring_files_update *up,
5947 unsigned nr_args)
5948{
5949 struct fixed_file_data *data = ctx->file_data;
5950 bool ref_switch = false;
5951 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005952 __s32 __user *fds;
5953 int fd, i, err;
5954 __u32 done;
5955
Jens Axboe05f3fb32019-12-09 11:22:50 -07005956 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005957 return -EOVERFLOW;
5958 if (done > ctx->nr_user_files)
5959 return -EINVAL;
5960
5961 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005962 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005963 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005964 struct fixed_file_table *table;
5965 unsigned index;
5966
Jens Axboec3a31e62019-10-03 13:59:56 -06005967 err = 0;
5968 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5969 err = -EFAULT;
5970 break;
5971 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005972 i = array_index_nospec(up->offset, ctx->nr_user_files);
5973 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005974 index = i & IORING_FILE_TABLE_MASK;
5975 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005976 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005977 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005978 if (io_queue_file_removal(data, file))
5979 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005980 }
5981 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005982 file = fget(fd);
5983 if (!file) {
5984 err = -EBADF;
5985 break;
5986 }
5987 /*
5988 * Don't allow io_uring instances to be registered. If
5989 * UNIX isn't enabled, then this causes a reference
5990 * cycle and this instance can never get freed. If UNIX
5991 * is enabled we'll handle it just fine, but there's
5992 * still no point in allowing a ring fd as it doesn't
5993 * support regular read/write anyway.
5994 */
5995 if (file->f_op == &io_uring_fops) {
5996 fput(file);
5997 err = -EBADF;
5998 break;
5999 }
Jens Axboe65e19f52019-10-26 07:20:21 -06006000 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06006001 err = io_sqe_file_register(ctx, file, i);
6002 if (err)
6003 break;
6004 }
6005 nr_args--;
6006 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07006007 up->offset++;
6008 }
6009
Jens Axboedd3db2a2020-02-26 10:23:43 -07006010 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07006011 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06006012
6013 return done ? done : err;
6014}
Jens Axboe05f3fb32019-12-09 11:22:50 -07006015static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
6016 unsigned nr_args)
6017{
6018 struct io_uring_files_update up;
6019
6020 if (!ctx->file_data)
6021 return -ENXIO;
6022 if (!nr_args)
6023 return -EINVAL;
6024 if (copy_from_user(&up, arg, sizeof(up)))
6025 return -EFAULT;
6026 if (up.resv)
6027 return -EINVAL;
6028
6029 return __io_sqe_files_update(ctx, &up, nr_args);
6030}
Jens Axboec3a31e62019-10-03 13:59:56 -06006031
Jens Axboe7d723062019-11-12 22:31:31 -07006032static void io_put_work(struct io_wq_work *work)
6033{
6034 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6035
6036 io_put_req(req);
6037}
6038
6039static void io_get_work(struct io_wq_work *work)
6040{
6041 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
6042
6043 refcount_inc(&req->refs);
6044}
6045
Pavel Begunkov24369c22020-01-28 03:15:48 +03006046static int io_init_wq_offload(struct io_ring_ctx *ctx,
6047 struct io_uring_params *p)
6048{
6049 struct io_wq_data data;
6050 struct fd f;
6051 struct io_ring_ctx *ctx_attach;
6052 unsigned int concurrency;
6053 int ret = 0;
6054
6055 data.user = ctx->user;
6056 data.get_work = io_get_work;
6057 data.put_work = io_put_work;
6058
6059 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
6060 /* Do QD, or 4 * CPUS, whatever is smallest */
6061 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
6062
6063 ctx->io_wq = io_wq_create(concurrency, &data);
6064 if (IS_ERR(ctx->io_wq)) {
6065 ret = PTR_ERR(ctx->io_wq);
6066 ctx->io_wq = NULL;
6067 }
6068 return ret;
6069 }
6070
6071 f = fdget(p->wq_fd);
6072 if (!f.file)
6073 return -EBADF;
6074
6075 if (f.file->f_op != &io_uring_fops) {
6076 ret = -EINVAL;
6077 goto out_fput;
6078 }
6079
6080 ctx_attach = f.file->private_data;
6081 /* @io_wq is protected by holding the fd */
6082 if (!io_wq_get(ctx_attach->io_wq, &data)) {
6083 ret = -EINVAL;
6084 goto out_fput;
6085 }
6086
6087 ctx->io_wq = ctx_attach->io_wq;
6088out_fput:
6089 fdput(f);
6090 return ret;
6091}
6092
Jens Axboe6c271ce2019-01-10 11:22:30 -07006093static int io_sq_offload_start(struct io_ring_ctx *ctx,
6094 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006095{
6096 int ret;
6097
Jens Axboe6c271ce2019-01-10 11:22:30 -07006098 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006099 mmgrab(current->mm);
6100 ctx->sqo_mm = current->mm;
6101
Jens Axboe6c271ce2019-01-10 11:22:30 -07006102 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006103 ret = -EPERM;
6104 if (!capable(CAP_SYS_ADMIN))
6105 goto err;
6106
Jens Axboe917257d2019-04-13 09:28:55 -06006107 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6108 if (!ctx->sq_thread_idle)
6109 ctx->sq_thread_idle = HZ;
6110
Jens Axboe6c271ce2019-01-10 11:22:30 -07006111 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006112 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006113
Jens Axboe917257d2019-04-13 09:28:55 -06006114 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006115 if (cpu >= nr_cpu_ids)
6116 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006117 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006118 goto err;
6119
Jens Axboe6c271ce2019-01-10 11:22:30 -07006120 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6121 ctx, cpu,
6122 "io_uring-sq");
6123 } else {
6124 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6125 "io_uring-sq");
6126 }
6127 if (IS_ERR(ctx->sqo_thread)) {
6128 ret = PTR_ERR(ctx->sqo_thread);
6129 ctx->sqo_thread = NULL;
6130 goto err;
6131 }
6132 wake_up_process(ctx->sqo_thread);
6133 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6134 /* Can't have SQ_AFF without SQPOLL */
6135 ret = -EINVAL;
6136 goto err;
6137 }
6138
Pavel Begunkov24369c22020-01-28 03:15:48 +03006139 ret = io_init_wq_offload(ctx, p);
6140 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006141 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006142
6143 return 0;
6144err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006145 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006146 mmdrop(ctx->sqo_mm);
6147 ctx->sqo_mm = NULL;
6148 return ret;
6149}
6150
6151static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6152{
6153 atomic_long_sub(nr_pages, &user->locked_vm);
6154}
6155
6156static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6157{
6158 unsigned long page_limit, cur_pages, new_pages;
6159
6160 /* Don't allow more pages than we can safely lock */
6161 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6162
6163 do {
6164 cur_pages = atomic_long_read(&user->locked_vm);
6165 new_pages = cur_pages + nr_pages;
6166 if (new_pages > page_limit)
6167 return -ENOMEM;
6168 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6169 new_pages) != cur_pages);
6170
6171 return 0;
6172}
6173
6174static void io_mem_free(void *ptr)
6175{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006176 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006177
Mark Rutland52e04ef2019-04-30 17:30:21 +01006178 if (!ptr)
6179 return;
6180
6181 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006182 if (put_page_testzero(page))
6183 free_compound_page(page);
6184}
6185
6186static void *io_mem_alloc(size_t size)
6187{
6188 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6189 __GFP_NORETRY;
6190
6191 return (void *) __get_free_pages(gfp_flags, get_order(size));
6192}
6193
Hristo Venev75b28af2019-08-26 17:23:46 +00006194static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6195 size_t *sq_offset)
6196{
6197 struct io_rings *rings;
6198 size_t off, sq_array_size;
6199
6200 off = struct_size(rings, cqes, cq_entries);
6201 if (off == SIZE_MAX)
6202 return SIZE_MAX;
6203
6204#ifdef CONFIG_SMP
6205 off = ALIGN(off, SMP_CACHE_BYTES);
6206 if (off == 0)
6207 return SIZE_MAX;
6208#endif
6209
6210 sq_array_size = array_size(sizeof(u32), sq_entries);
6211 if (sq_array_size == SIZE_MAX)
6212 return SIZE_MAX;
6213
6214 if (check_add_overflow(off, sq_array_size, &off))
6215 return SIZE_MAX;
6216
6217 if (sq_offset)
6218 *sq_offset = off;
6219
6220 return off;
6221}
6222
Jens Axboe2b188cc2019-01-07 10:46:33 -07006223static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6224{
Hristo Venev75b28af2019-08-26 17:23:46 +00006225 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006226
Hristo Venev75b28af2019-08-26 17:23:46 +00006227 pages = (size_t)1 << get_order(
6228 rings_size(sq_entries, cq_entries, NULL));
6229 pages += (size_t)1 << get_order(
6230 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006231
Hristo Venev75b28af2019-08-26 17:23:46 +00006232 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006233}
6234
Jens Axboeedafcce2019-01-09 09:16:05 -07006235static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6236{
6237 int i, j;
6238
6239 if (!ctx->user_bufs)
6240 return -ENXIO;
6241
6242 for (i = 0; i < ctx->nr_user_bufs; i++) {
6243 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6244
6245 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006246 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006247
6248 if (ctx->account_mem)
6249 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006250 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006251 imu->nr_bvecs = 0;
6252 }
6253
6254 kfree(ctx->user_bufs);
6255 ctx->user_bufs = NULL;
6256 ctx->nr_user_bufs = 0;
6257 return 0;
6258}
6259
6260static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6261 void __user *arg, unsigned index)
6262{
6263 struct iovec __user *src;
6264
6265#ifdef CONFIG_COMPAT
6266 if (ctx->compat) {
6267 struct compat_iovec __user *ciovs;
6268 struct compat_iovec ciov;
6269
6270 ciovs = (struct compat_iovec __user *) arg;
6271 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6272 return -EFAULT;
6273
Jens Axboed55e5f52019-12-11 16:12:15 -07006274 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006275 dst->iov_len = ciov.iov_len;
6276 return 0;
6277 }
6278#endif
6279 src = (struct iovec __user *) arg;
6280 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6281 return -EFAULT;
6282 return 0;
6283}
6284
6285static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6286 unsigned nr_args)
6287{
6288 struct vm_area_struct **vmas = NULL;
6289 struct page **pages = NULL;
6290 int i, j, got_pages = 0;
6291 int ret = -EINVAL;
6292
6293 if (ctx->user_bufs)
6294 return -EBUSY;
6295 if (!nr_args || nr_args > UIO_MAXIOV)
6296 return -EINVAL;
6297
6298 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6299 GFP_KERNEL);
6300 if (!ctx->user_bufs)
6301 return -ENOMEM;
6302
6303 for (i = 0; i < nr_args; i++) {
6304 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6305 unsigned long off, start, end, ubuf;
6306 int pret, nr_pages;
6307 struct iovec iov;
6308 size_t size;
6309
6310 ret = io_copy_iov(ctx, &iov, arg, i);
6311 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006312 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006313
6314 /*
6315 * Don't impose further limits on the size and buffer
6316 * constraints here, we'll -EINVAL later when IO is
6317 * submitted if they are wrong.
6318 */
6319 ret = -EFAULT;
6320 if (!iov.iov_base || !iov.iov_len)
6321 goto err;
6322
6323 /* arbitrary limit, but we need something */
6324 if (iov.iov_len > SZ_1G)
6325 goto err;
6326
6327 ubuf = (unsigned long) iov.iov_base;
6328 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6329 start = ubuf >> PAGE_SHIFT;
6330 nr_pages = end - start;
6331
6332 if (ctx->account_mem) {
6333 ret = io_account_mem(ctx->user, nr_pages);
6334 if (ret)
6335 goto err;
6336 }
6337
6338 ret = 0;
6339 if (!pages || nr_pages > got_pages) {
6340 kfree(vmas);
6341 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006342 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006343 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006344 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006345 sizeof(struct vm_area_struct *),
6346 GFP_KERNEL);
6347 if (!pages || !vmas) {
6348 ret = -ENOMEM;
6349 if (ctx->account_mem)
6350 io_unaccount_mem(ctx->user, nr_pages);
6351 goto err;
6352 }
6353 got_pages = nr_pages;
6354 }
6355
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006356 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006357 GFP_KERNEL);
6358 ret = -ENOMEM;
6359 if (!imu->bvec) {
6360 if (ctx->account_mem)
6361 io_unaccount_mem(ctx->user, nr_pages);
6362 goto err;
6363 }
6364
6365 ret = 0;
6366 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006367 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006368 FOLL_WRITE | FOLL_LONGTERM,
6369 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006370 if (pret == nr_pages) {
6371 /* don't support file backed memory */
6372 for (j = 0; j < nr_pages; j++) {
6373 struct vm_area_struct *vma = vmas[j];
6374
6375 if (vma->vm_file &&
6376 !is_file_hugepages(vma->vm_file)) {
6377 ret = -EOPNOTSUPP;
6378 break;
6379 }
6380 }
6381 } else {
6382 ret = pret < 0 ? pret : -EFAULT;
6383 }
6384 up_read(&current->mm->mmap_sem);
6385 if (ret) {
6386 /*
6387 * if we did partial map, or found file backed vmas,
6388 * release any pages we did get
6389 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006390 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006391 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006392 if (ctx->account_mem)
6393 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006394 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006395 goto err;
6396 }
6397
6398 off = ubuf & ~PAGE_MASK;
6399 size = iov.iov_len;
6400 for (j = 0; j < nr_pages; j++) {
6401 size_t vec_len;
6402
6403 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6404 imu->bvec[j].bv_page = pages[j];
6405 imu->bvec[j].bv_len = vec_len;
6406 imu->bvec[j].bv_offset = off;
6407 off = 0;
6408 size -= vec_len;
6409 }
6410 /* store original address for later verification */
6411 imu->ubuf = ubuf;
6412 imu->len = iov.iov_len;
6413 imu->nr_bvecs = nr_pages;
6414
6415 ctx->nr_user_bufs++;
6416 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006417 kvfree(pages);
6418 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006419 return 0;
6420err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006421 kvfree(pages);
6422 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006423 io_sqe_buffer_unregister(ctx);
6424 return ret;
6425}
6426
Jens Axboe9b402842019-04-11 11:45:41 -06006427static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6428{
6429 __s32 __user *fds = arg;
6430 int fd;
6431
6432 if (ctx->cq_ev_fd)
6433 return -EBUSY;
6434
6435 if (copy_from_user(&fd, fds, sizeof(*fds)))
6436 return -EFAULT;
6437
6438 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6439 if (IS_ERR(ctx->cq_ev_fd)) {
6440 int ret = PTR_ERR(ctx->cq_ev_fd);
6441 ctx->cq_ev_fd = NULL;
6442 return ret;
6443 }
6444
6445 return 0;
6446}
6447
6448static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6449{
6450 if (ctx->cq_ev_fd) {
6451 eventfd_ctx_put(ctx->cq_ev_fd);
6452 ctx->cq_ev_fd = NULL;
6453 return 0;
6454 }
6455
6456 return -ENXIO;
6457}
6458
Jens Axboe2b188cc2019-01-07 10:46:33 -07006459static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6460{
Jens Axboe6b063142019-01-10 22:13:58 -07006461 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006462 if (ctx->sqo_mm)
6463 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006464
6465 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006466 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006467 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006468 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006469 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006470
Jens Axboe2b188cc2019-01-07 10:46:33 -07006471#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006472 if (ctx->ring_sock) {
6473 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006474 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006475 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006476#endif
6477
Hristo Venev75b28af2019-08-26 17:23:46 +00006478 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006479 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006480
6481 percpu_ref_exit(&ctx->refs);
6482 if (ctx->account_mem)
6483 io_unaccount_mem(ctx->user,
6484 ring_pages(ctx->sq_entries, ctx->cq_entries));
6485 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006486 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006487 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006488 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006489 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006490 kfree(ctx);
6491}
6492
6493static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6494{
6495 struct io_ring_ctx *ctx = file->private_data;
6496 __poll_t mask = 0;
6497
6498 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006499 /*
6500 * synchronizes with barrier from wq_has_sleeper call in
6501 * io_commit_cqring
6502 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006503 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006504 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6505 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006506 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006507 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006508 mask |= EPOLLIN | EPOLLRDNORM;
6509
6510 return mask;
6511}
6512
6513static int io_uring_fasync(int fd, struct file *file, int on)
6514{
6515 struct io_ring_ctx *ctx = file->private_data;
6516
6517 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6518}
6519
Jens Axboe071698e2020-01-28 10:04:42 -07006520static int io_remove_personalities(int id, void *p, void *data)
6521{
6522 struct io_ring_ctx *ctx = data;
6523 const struct cred *cred;
6524
6525 cred = idr_remove(&ctx->personality_idr, id);
6526 if (cred)
6527 put_cred(cred);
6528 return 0;
6529}
6530
Jens Axboe2b188cc2019-01-07 10:46:33 -07006531static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6532{
6533 mutex_lock(&ctx->uring_lock);
6534 percpu_ref_kill(&ctx->refs);
6535 mutex_unlock(&ctx->uring_lock);
6536
Jens Axboedf069d82020-02-04 16:48:34 -07006537 /*
6538 * Wait for sq thread to idle, if we have one. It won't spin on new
6539 * work after we've killed the ctx ref above. This is important to do
6540 * before we cancel existing commands, as the thread could otherwise
6541 * be queueing new work post that. If that's work we need to cancel,
6542 * it could cause shutdown to hang.
6543 */
6544 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6545 cpu_relax();
6546
Jens Axboe5262f562019-09-17 12:26:57 -06006547 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006548 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006549
6550 if (ctx->io_wq)
6551 io_wq_cancel_all(ctx->io_wq);
6552
Jens Axboedef596e2019-01-09 08:59:42 -07006553 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006554 /* if we failed setting up the ctx, we might not have any rings */
6555 if (ctx->rings)
6556 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006557 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006558 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006559 io_ring_ctx_free(ctx);
6560}
6561
6562static int io_uring_release(struct inode *inode, struct file *file)
6563{
6564 struct io_ring_ctx *ctx = file->private_data;
6565
6566 file->private_data = NULL;
6567 io_ring_ctx_wait_and_kill(ctx);
6568 return 0;
6569}
6570
Jens Axboefcb323c2019-10-24 12:39:47 -06006571static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6572 struct files_struct *files)
6573{
6574 struct io_kiocb *req;
6575 DEFINE_WAIT(wait);
6576
6577 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006578 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006579
6580 spin_lock_irq(&ctx->inflight_lock);
6581 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006582 if (req->work.files != files)
6583 continue;
6584 /* req is being completed, ignore */
6585 if (!refcount_inc_not_zero(&req->refs))
6586 continue;
6587 cancel_req = req;
6588 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006589 }
Jens Axboe768134d2019-11-10 20:30:53 -07006590 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006591 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006592 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006593 spin_unlock_irq(&ctx->inflight_lock);
6594
Jens Axboe768134d2019-11-10 20:30:53 -07006595 /* We need to keep going until we don't find a matching req */
6596 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006597 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006598
Jens Axboe2ca10252020-02-13 17:17:35 -07006599 if (cancel_req->flags & REQ_F_OVERFLOW) {
6600 spin_lock_irq(&ctx->completion_lock);
6601 list_del(&cancel_req->list);
6602 cancel_req->flags &= ~REQ_F_OVERFLOW;
6603 if (list_empty(&ctx->cq_overflow_list)) {
6604 clear_bit(0, &ctx->sq_check_overflow);
6605 clear_bit(0, &ctx->cq_check_overflow);
6606 }
6607 spin_unlock_irq(&ctx->completion_lock);
6608
6609 WRITE_ONCE(ctx->rings->cq_overflow,
6610 atomic_inc_return(&ctx->cached_cq_overflow));
6611
6612 /*
6613 * Put inflight ref and overflow ref. If that's
6614 * all we had, then we're done with this request.
6615 */
6616 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6617 io_put_req(cancel_req);
6618 continue;
6619 }
6620 }
6621
Bob Liu2f6d9b92019-11-13 18:06:24 +08006622 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6623 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006624 schedule();
6625 }
Jens Axboe768134d2019-11-10 20:30:53 -07006626 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006627}
6628
6629static int io_uring_flush(struct file *file, void *data)
6630{
6631 struct io_ring_ctx *ctx = file->private_data;
6632
6633 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006634
6635 /*
6636 * If the task is going away, cancel work it may have pending
6637 */
6638 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6639 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6640
Jens Axboefcb323c2019-10-24 12:39:47 -06006641 return 0;
6642}
6643
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006644static void *io_uring_validate_mmap_request(struct file *file,
6645 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006646{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006647 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006648 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006649 struct page *page;
6650 void *ptr;
6651
6652 switch (offset) {
6653 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006654 case IORING_OFF_CQ_RING:
6655 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006656 break;
6657 case IORING_OFF_SQES:
6658 ptr = ctx->sq_sqes;
6659 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006660 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006661 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006662 }
6663
6664 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006665 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006666 return ERR_PTR(-EINVAL);
6667
6668 return ptr;
6669}
6670
6671#ifdef CONFIG_MMU
6672
6673static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6674{
6675 size_t sz = vma->vm_end - vma->vm_start;
6676 unsigned long pfn;
6677 void *ptr;
6678
6679 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6680 if (IS_ERR(ptr))
6681 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006682
6683 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6684 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6685}
6686
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006687#else /* !CONFIG_MMU */
6688
6689static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6690{
6691 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6692}
6693
6694static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6695{
6696 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6697}
6698
6699static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6700 unsigned long addr, unsigned long len,
6701 unsigned long pgoff, unsigned long flags)
6702{
6703 void *ptr;
6704
6705 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6706 if (IS_ERR(ptr))
6707 return PTR_ERR(ptr);
6708
6709 return (unsigned long) ptr;
6710}
6711
6712#endif /* !CONFIG_MMU */
6713
Jens Axboe2b188cc2019-01-07 10:46:33 -07006714SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6715 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6716 size_t, sigsz)
6717{
6718 struct io_ring_ctx *ctx;
6719 long ret = -EBADF;
6720 int submitted = 0;
6721 struct fd f;
6722
Jens Axboe6c271ce2019-01-10 11:22:30 -07006723 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006724 return -EINVAL;
6725
6726 f = fdget(fd);
6727 if (!f.file)
6728 return -EBADF;
6729
6730 ret = -EOPNOTSUPP;
6731 if (f.file->f_op != &io_uring_fops)
6732 goto out_fput;
6733
6734 ret = -ENXIO;
6735 ctx = f.file->private_data;
6736 if (!percpu_ref_tryget(&ctx->refs))
6737 goto out_fput;
6738
Jens Axboe6c271ce2019-01-10 11:22:30 -07006739 /*
6740 * For SQ polling, the thread will do all submissions and completions.
6741 * Just return the requested submit count, and wake the thread if
6742 * we were asked to.
6743 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006744 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006745 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006746 if (!list_empty_careful(&ctx->cq_overflow_list))
6747 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006748 if (flags & IORING_ENTER_SQ_WAKEUP)
6749 wake_up(&ctx->sqo_wait);
6750 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006751 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006752 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006753
6754 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006755 /* already have mm, so io_submit_sqes() won't try to grab it */
6756 cur_mm = ctx->sqo_mm;
6757 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6758 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006759 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006760
6761 if (submitted != to_submit)
6762 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006763 }
6764 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006765 unsigned nr_events = 0;
6766
Jens Axboe2b188cc2019-01-07 10:46:33 -07006767 min_complete = min(min_complete, ctx->cq_entries);
6768
Jens Axboedef596e2019-01-09 08:59:42 -07006769 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006770 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006771 } else {
6772 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6773 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006774 }
6775
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006776out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006777 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006778out_fput:
6779 fdput(f);
6780 return submitted ? submitted : ret;
6781}
6782
Tobias Klauserbebdb652020-02-26 18:38:32 +01006783#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006784static int io_uring_show_cred(int id, void *p, void *data)
6785{
6786 const struct cred *cred = p;
6787 struct seq_file *m = data;
6788 struct user_namespace *uns = seq_user_ns(m);
6789 struct group_info *gi;
6790 kernel_cap_t cap;
6791 unsigned __capi;
6792 int g;
6793
6794 seq_printf(m, "%5d\n", id);
6795 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6796 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6797 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6798 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6799 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6800 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6801 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6802 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6803 seq_puts(m, "\n\tGroups:\t");
6804 gi = cred->group_info;
6805 for (g = 0; g < gi->ngroups; g++) {
6806 seq_put_decimal_ull(m, g ? " " : "",
6807 from_kgid_munged(uns, gi->gid[g]));
6808 }
6809 seq_puts(m, "\n\tCapEff:\t");
6810 cap = cred->cap_effective;
6811 CAP_FOR_EACH_U32(__capi)
6812 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6813 seq_putc(m, '\n');
6814 return 0;
6815}
6816
6817static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6818{
6819 int i;
6820
6821 mutex_lock(&ctx->uring_lock);
6822 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6823 for (i = 0; i < ctx->nr_user_files; i++) {
6824 struct fixed_file_table *table;
6825 struct file *f;
6826
6827 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6828 f = table->files[i & IORING_FILE_TABLE_MASK];
6829 if (f)
6830 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6831 else
6832 seq_printf(m, "%5u: <none>\n", i);
6833 }
6834 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6835 for (i = 0; i < ctx->nr_user_bufs; i++) {
6836 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6837
6838 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6839 (unsigned int) buf->len);
6840 }
6841 if (!idr_is_empty(&ctx->personality_idr)) {
6842 seq_printf(m, "Personalities:\n");
6843 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6844 }
6845 mutex_unlock(&ctx->uring_lock);
6846}
6847
6848static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6849{
6850 struct io_ring_ctx *ctx = f->private_data;
6851
6852 if (percpu_ref_tryget(&ctx->refs)) {
6853 __io_uring_show_fdinfo(ctx, m);
6854 percpu_ref_put(&ctx->refs);
6855 }
6856}
Tobias Klauserbebdb652020-02-26 18:38:32 +01006857#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07006858
Jens Axboe2b188cc2019-01-07 10:46:33 -07006859static const struct file_operations io_uring_fops = {
6860 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006861 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006862 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006863#ifndef CONFIG_MMU
6864 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6865 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6866#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006867 .poll = io_uring_poll,
6868 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006869#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006870 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006871#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006872};
6873
6874static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6875 struct io_uring_params *p)
6876{
Hristo Venev75b28af2019-08-26 17:23:46 +00006877 struct io_rings *rings;
6878 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006879
Hristo Venev75b28af2019-08-26 17:23:46 +00006880 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6881 if (size == SIZE_MAX)
6882 return -EOVERFLOW;
6883
6884 rings = io_mem_alloc(size);
6885 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006886 return -ENOMEM;
6887
Hristo Venev75b28af2019-08-26 17:23:46 +00006888 ctx->rings = rings;
6889 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6890 rings->sq_ring_mask = p->sq_entries - 1;
6891 rings->cq_ring_mask = p->cq_entries - 1;
6892 rings->sq_ring_entries = p->sq_entries;
6893 rings->cq_ring_entries = p->cq_entries;
6894 ctx->sq_mask = rings->sq_ring_mask;
6895 ctx->cq_mask = rings->cq_ring_mask;
6896 ctx->sq_entries = rings->sq_ring_entries;
6897 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006898
6899 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006900 if (size == SIZE_MAX) {
6901 io_mem_free(ctx->rings);
6902 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006903 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006904 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006905
6906 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006907 if (!ctx->sq_sqes) {
6908 io_mem_free(ctx->rings);
6909 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006910 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006911 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006912
Jens Axboe2b188cc2019-01-07 10:46:33 -07006913 return 0;
6914}
6915
6916/*
6917 * Allocate an anonymous fd, this is what constitutes the application
6918 * visible backing of an io_uring instance. The application mmaps this
6919 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6920 * we have to tie this fd to a socket for file garbage collection purposes.
6921 */
6922static int io_uring_get_fd(struct io_ring_ctx *ctx)
6923{
6924 struct file *file;
6925 int ret;
6926
6927#if defined(CONFIG_UNIX)
6928 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6929 &ctx->ring_sock);
6930 if (ret)
6931 return ret;
6932#endif
6933
6934 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6935 if (ret < 0)
6936 goto err;
6937
6938 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6939 O_RDWR | O_CLOEXEC);
6940 if (IS_ERR(file)) {
6941 put_unused_fd(ret);
6942 ret = PTR_ERR(file);
6943 goto err;
6944 }
6945
6946#if defined(CONFIG_UNIX)
6947 ctx->ring_sock->file = file;
6948#endif
6949 fd_install(ret, file);
6950 return ret;
6951err:
6952#if defined(CONFIG_UNIX)
6953 sock_release(ctx->ring_sock);
6954 ctx->ring_sock = NULL;
6955#endif
6956 return ret;
6957}
6958
6959static int io_uring_create(unsigned entries, struct io_uring_params *p)
6960{
6961 struct user_struct *user = NULL;
6962 struct io_ring_ctx *ctx;
6963 bool account_mem;
6964 int ret;
6965
Jens Axboe8110c1a2019-12-28 15:39:54 -07006966 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006967 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006968 if (entries > IORING_MAX_ENTRIES) {
6969 if (!(p->flags & IORING_SETUP_CLAMP))
6970 return -EINVAL;
6971 entries = IORING_MAX_ENTRIES;
6972 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006973
6974 /*
6975 * Use twice as many entries for the CQ ring. It's possible for the
6976 * application to drive a higher depth than the size of the SQ ring,
6977 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006978 * some flexibility in overcommitting a bit. If the application has
6979 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6980 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006981 */
6982 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006983 if (p->flags & IORING_SETUP_CQSIZE) {
6984 /*
6985 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6986 * to a power-of-two, if it isn't already. We do NOT impose
6987 * any cq vs sq ring sizing.
6988 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006989 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006990 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006991 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6992 if (!(p->flags & IORING_SETUP_CLAMP))
6993 return -EINVAL;
6994 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6995 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006996 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6997 } else {
6998 p->cq_entries = 2 * p->sq_entries;
6999 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07007000
7001 user = get_uid(current_user());
7002 account_mem = !capable(CAP_IPC_LOCK);
7003
7004 if (account_mem) {
7005 ret = io_account_mem(user,
7006 ring_pages(p->sq_entries, p->cq_entries));
7007 if (ret) {
7008 free_uid(user);
7009 return ret;
7010 }
7011 }
7012
7013 ctx = io_ring_ctx_alloc(p);
7014 if (!ctx) {
7015 if (account_mem)
7016 io_unaccount_mem(user, ring_pages(p->sq_entries,
7017 p->cq_entries));
7018 free_uid(user);
7019 return -ENOMEM;
7020 }
7021 ctx->compat = in_compat_syscall();
7022 ctx->account_mem = account_mem;
7023 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07007024 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07007025
7026 ret = io_allocate_scq_urings(ctx, p);
7027 if (ret)
7028 goto err;
7029
Jens Axboe6c271ce2019-01-10 11:22:30 -07007030 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007031 if (ret)
7032 goto err;
7033
Jens Axboe2b188cc2019-01-07 10:46:33 -07007034 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007035 p->sq_off.head = offsetof(struct io_rings, sq.head);
7036 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
7037 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
7038 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
7039 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
7040 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
7041 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07007042
7043 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00007044 p->cq_off.head = offsetof(struct io_rings, cq.head);
7045 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
7046 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
7047 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
7048 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
7049 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06007050
Jens Axboe044c1ab2019-10-28 09:15:33 -06007051 /*
7052 * Install ring fd as the very last thing, so we don't risk someone
7053 * having closed it before we finish setup
7054 */
7055 ret = io_uring_get_fd(ctx);
7056 if (ret < 0)
7057 goto err;
7058
Jens Axboeda8c9692019-12-02 18:51:26 -07007059 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07007060 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
7061 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007062 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007063 return ret;
7064err:
7065 io_ring_ctx_wait_and_kill(ctx);
7066 return ret;
7067}
7068
7069/*
7070 * Sets up an aio uring context, and returns the fd. Applications asks for a
7071 * ring size, we return the actual sq/cq ring sizes (among other things) in the
7072 * params structure passed in.
7073 */
7074static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
7075{
7076 struct io_uring_params p;
7077 long ret;
7078 int i;
7079
7080 if (copy_from_user(&p, params, sizeof(p)))
7081 return -EFAULT;
7082 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
7083 if (p.resv[i])
7084 return -EINVAL;
7085 }
7086
Jens Axboe6c271ce2019-01-10 11:22:30 -07007087 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07007088 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03007089 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07007090 return -EINVAL;
7091
7092 ret = io_uring_create(entries, &p);
7093 if (ret < 0)
7094 return ret;
7095
7096 if (copy_to_user(params, &p, sizeof(p)))
7097 return -EFAULT;
7098
7099 return ret;
7100}
7101
7102SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7103 struct io_uring_params __user *, params)
7104{
7105 return io_uring_setup(entries, params);
7106}
7107
Jens Axboe66f4af92020-01-16 15:36:52 -07007108static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7109{
7110 struct io_uring_probe *p;
7111 size_t size;
7112 int i, ret;
7113
7114 size = struct_size(p, ops, nr_args);
7115 if (size == SIZE_MAX)
7116 return -EOVERFLOW;
7117 p = kzalloc(size, GFP_KERNEL);
7118 if (!p)
7119 return -ENOMEM;
7120
7121 ret = -EFAULT;
7122 if (copy_from_user(p, arg, size))
7123 goto out;
7124 ret = -EINVAL;
7125 if (memchr_inv(p, 0, size))
7126 goto out;
7127
7128 p->last_op = IORING_OP_LAST - 1;
7129 if (nr_args > IORING_OP_LAST)
7130 nr_args = IORING_OP_LAST;
7131
7132 for (i = 0; i < nr_args; i++) {
7133 p->ops[i].op = i;
7134 if (!io_op_defs[i].not_supported)
7135 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7136 }
7137 p->ops_len = i;
7138
7139 ret = 0;
7140 if (copy_to_user(arg, p, size))
7141 ret = -EFAULT;
7142out:
7143 kfree(p);
7144 return ret;
7145}
7146
Jens Axboe071698e2020-01-28 10:04:42 -07007147static int io_register_personality(struct io_ring_ctx *ctx)
7148{
7149 const struct cred *creds = get_current_cred();
7150 int id;
7151
7152 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7153 USHRT_MAX, GFP_KERNEL);
7154 if (id < 0)
7155 put_cred(creds);
7156 return id;
7157}
7158
7159static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7160{
7161 const struct cred *old_creds;
7162
7163 old_creds = idr_remove(&ctx->personality_idr, id);
7164 if (old_creds) {
7165 put_cred(old_creds);
7166 return 0;
7167 }
7168
7169 return -EINVAL;
7170}
7171
7172static bool io_register_op_must_quiesce(int op)
7173{
7174 switch (op) {
7175 case IORING_UNREGISTER_FILES:
7176 case IORING_REGISTER_FILES_UPDATE:
7177 case IORING_REGISTER_PROBE:
7178 case IORING_REGISTER_PERSONALITY:
7179 case IORING_UNREGISTER_PERSONALITY:
7180 return false;
7181 default:
7182 return true;
7183 }
7184}
7185
Jens Axboeedafcce2019-01-09 09:16:05 -07007186static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7187 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007188 __releases(ctx->uring_lock)
7189 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007190{
7191 int ret;
7192
Jens Axboe35fa71a2019-04-22 10:23:23 -06007193 /*
7194 * We're inside the ring mutex, if the ref is already dying, then
7195 * someone else killed the ctx or is already going through
7196 * io_uring_register().
7197 */
7198 if (percpu_ref_is_dying(&ctx->refs))
7199 return -ENXIO;
7200
Jens Axboe071698e2020-01-28 10:04:42 -07007201 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007202 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007203
Jens Axboe05f3fb32019-12-09 11:22:50 -07007204 /*
7205 * Drop uring mutex before waiting for references to exit. If
7206 * another thread is currently inside io_uring_enter() it might
7207 * need to grab the uring_lock to make progress. If we hold it
7208 * here across the drain wait, then we can deadlock. It's safe
7209 * to drop the mutex here, since no new references will come in
7210 * after we've killed the percpu ref.
7211 */
7212 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007213 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007214 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007215 if (ret) {
7216 percpu_ref_resurrect(&ctx->refs);
7217 ret = -EINTR;
7218 goto out;
7219 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007220 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007221
7222 switch (opcode) {
7223 case IORING_REGISTER_BUFFERS:
7224 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7225 break;
7226 case IORING_UNREGISTER_BUFFERS:
7227 ret = -EINVAL;
7228 if (arg || nr_args)
7229 break;
7230 ret = io_sqe_buffer_unregister(ctx);
7231 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007232 case IORING_REGISTER_FILES:
7233 ret = io_sqe_files_register(ctx, arg, nr_args);
7234 break;
7235 case IORING_UNREGISTER_FILES:
7236 ret = -EINVAL;
7237 if (arg || nr_args)
7238 break;
7239 ret = io_sqe_files_unregister(ctx);
7240 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007241 case IORING_REGISTER_FILES_UPDATE:
7242 ret = io_sqe_files_update(ctx, arg, nr_args);
7243 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007244 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007245 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007246 ret = -EINVAL;
7247 if (nr_args != 1)
7248 break;
7249 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007250 if (ret)
7251 break;
7252 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7253 ctx->eventfd_async = 1;
7254 else
7255 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007256 break;
7257 case IORING_UNREGISTER_EVENTFD:
7258 ret = -EINVAL;
7259 if (arg || nr_args)
7260 break;
7261 ret = io_eventfd_unregister(ctx);
7262 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007263 case IORING_REGISTER_PROBE:
7264 ret = -EINVAL;
7265 if (!arg || nr_args > 256)
7266 break;
7267 ret = io_probe(ctx, arg, nr_args);
7268 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007269 case IORING_REGISTER_PERSONALITY:
7270 ret = -EINVAL;
7271 if (arg || nr_args)
7272 break;
7273 ret = io_register_personality(ctx);
7274 break;
7275 case IORING_UNREGISTER_PERSONALITY:
7276 ret = -EINVAL;
7277 if (arg)
7278 break;
7279 ret = io_unregister_personality(ctx, nr_args);
7280 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007281 default:
7282 ret = -EINVAL;
7283 break;
7284 }
7285
Jens Axboe071698e2020-01-28 10:04:42 -07007286 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007287 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007288 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007289out:
7290 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007291 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007292 return ret;
7293}
7294
7295SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7296 void __user *, arg, unsigned int, nr_args)
7297{
7298 struct io_ring_ctx *ctx;
7299 long ret = -EBADF;
7300 struct fd f;
7301
7302 f = fdget(fd);
7303 if (!f.file)
7304 return -EBADF;
7305
7306 ret = -EOPNOTSUPP;
7307 if (f.file->f_op != &io_uring_fops)
7308 goto out_fput;
7309
7310 ctx = f.file->private_data;
7311
7312 mutex_lock(&ctx->uring_lock);
7313 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7314 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007315 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7316 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007317out_fput:
7318 fdput(f);
7319 return ret;
7320}
7321
Jens Axboe2b188cc2019-01-07 10:46:33 -07007322static int __init io_uring_init(void)
7323{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007324#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7325 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7326 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7327} while (0)
7328
7329#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7330 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7331 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7332 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7333 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7334 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7335 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7336 BUILD_BUG_SQE_ELEM(8, __u64, off);
7337 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7338 BUILD_BUG_SQE_ELEM(16, __u64, addr);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007339 BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007340 BUILD_BUG_SQE_ELEM(24, __u32, len);
7341 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7342 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7343 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7344 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7345 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7346 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7347 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7348 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7349 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7350 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7351 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7352 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7353 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007354 BUILD_BUG_SQE_ELEM(28, __u32, splice_flags);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007355 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7356 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7357 BUILD_BUG_SQE_ELEM(42, __u16, personality);
Pavel Begunkov7d67af22020-02-24 11:32:45 +03007358 BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in);
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007359
Jens Axboed3656342019-12-18 09:50:26 -07007360 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007361 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7362 return 0;
7363};
7364__initcall(io_uring_init);