blob: 68050b61ad0e879c827b334b6a9bbea2a277f1b4 [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>
Jens Axboe2b188cc2019-01-07 10:46:33 -070079
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020080#define CREATE_TRACE_POINTS
81#include <trace/events/io_uring.h>
82
Jens Axboe2b188cc2019-01-07 10:46:33 -070083#include <uapi/linux/io_uring.h>
84
85#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060086#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070087
Daniel Xu5277dea2019-09-14 14:23:45 -070088#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060089#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060090
91/*
92 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
93 */
94#define IORING_FILE_TABLE_SHIFT 9
95#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
96#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
97#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070098
99struct io_uring {
100 u32 head ____cacheline_aligned_in_smp;
101 u32 tail ____cacheline_aligned_in_smp;
102};
103
Stefan Bühler1e84b972019-04-24 23:54:16 +0200104/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000105 * This data is shared with the application through the mmap at offsets
106 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107 *
108 * The offsets to the member fields are published through struct
109 * io_sqring_offsets when calling io_uring_setup.
110 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000111struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112 /*
113 * Head and tail offsets into the ring; the offsets need to be
114 * masked to get valid indices.
115 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 * The kernel controls head of the sq ring and the tail of the cq ring,
117 * and the application controls tail of the sq ring and the head of the
118 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000120 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 * ring_entries - 1)
124 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 u32 sq_ring_mask, cq_ring_mask;
126 /* Ring sizes (constant, power of 2) */
127 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200128 /*
129 * Number of invalid entries dropped by the kernel due to
130 * invalid index stored in array
131 *
132 * Written by the kernel, shouldn't be modified by the
133 * application (i.e. get number of "new events" by comparing to
134 * cached value).
135 *
136 * After a new SQ head value was read by the application this
137 * counter includes all submissions that were dropped reaching
138 * the new SQ head (and possibly more).
139 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000140 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200141 /*
142 * Runtime flags
143 *
144 * Written by the kernel, shouldn't be modified by the
145 * application.
146 *
147 * The application needs a full memory barrier before checking
148 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
149 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000150 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 /*
152 * Number of completion events lost because the queue was full;
153 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800154 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200155 * the completion queue.
156 *
157 * Written by the kernel, shouldn't be modified by the
158 * application (i.e. get number of "new events" by comparing to
159 * cached value).
160 *
161 * As completion events come in out of order this counter is not
162 * ordered with any other data.
163 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000164 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 /*
166 * Ring buffer of completion events.
167 *
168 * The kernel writes completion events fresh every time they are
169 * produced, so the application is allowed to modify pending
170 * entries.
171 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000172 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700173};
174
Jens Axboeedafcce2019-01-09 09:16:05 -0700175struct io_mapped_ubuf {
176 u64 ubuf;
177 size_t len;
178 struct bio_vec *bvec;
179 unsigned int nr_bvecs;
180};
181
Jens Axboe65e19f52019-10-26 07:20:21 -0600182struct fixed_file_table {
183 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700184};
185
Jens Axboe05f3fb32019-12-09 11:22:50 -0700186struct fixed_file_data {
187 struct fixed_file_table *table;
188 struct io_ring_ctx *ctx;
189
190 struct percpu_ref refs;
191 struct llist_head put_llist;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700192 struct work_struct ref_work;
193 struct completion done;
Jens Axboec1e21482020-03-04 07:25:50 -0700194 struct rcu_head rcu;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700195};
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
Jens Axboef499a022019-12-02 16:28:46 -0700432struct io_async_connect {
433 struct sockaddr_storage address;
434};
435
Jens Axboe03b12302019-12-02 18:50:25 -0700436struct io_async_msghdr {
437 struct iovec fast_iov[UIO_FASTIOV];
438 struct iovec *iov;
439 struct sockaddr __user *uaddr;
440 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700441 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700442};
443
Jens Axboef67676d2019-12-02 11:03:47 -0700444struct io_async_rw {
445 struct iovec fast_iov[UIO_FASTIOV];
446 struct iovec *iov;
447 ssize_t nr_segs;
448 ssize_t size;
449};
450
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700451struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700452 union {
453 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700454 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700455 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700456 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700457 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700458};
459
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300460enum {
461 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
462 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
463 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
464 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
465 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
466
467 REQ_F_LINK_NEXT_BIT,
468 REQ_F_FAIL_LINK_BIT,
469 REQ_F_INFLIGHT_BIT,
470 REQ_F_CUR_POS_BIT,
471 REQ_F_NOWAIT_BIT,
472 REQ_F_IOPOLL_COMPLETED_BIT,
473 REQ_F_LINK_TIMEOUT_BIT,
474 REQ_F_TIMEOUT_BIT,
475 REQ_F_ISREG_BIT,
476 REQ_F_MUST_PUNT_BIT,
477 REQ_F_TIMEOUT_NOSEQ_BIT,
478 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300479 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700480 REQ_F_OVERFLOW_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300481};
482
483enum {
484 /* ctx owns file */
485 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
486 /* drain existing IO first */
487 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
488 /* linked sqes */
489 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
490 /* doesn't sever on completion < 0 */
491 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
492 /* IOSQE_ASYNC */
493 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
494
495 /* already grabbed next link */
496 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
497 /* fail rest of links */
498 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
499 /* on inflight list */
500 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
501 /* read/write uses file position */
502 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
503 /* must not punt to workers */
504 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
505 /* polled IO has completed */
506 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
507 /* has linked timeout */
508 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
509 /* timeout request */
510 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
511 /* regular file */
512 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
513 /* must be punted even for NONBLOCK */
514 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
515 /* no timeout sequence */
516 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
517 /* completion under lock */
518 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300519 /* needs cleanup */
520 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700521 /* in overflow list */
522 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300523};
524
Jens Axboe09bb8392019-03-13 12:39:28 -0600525/*
526 * NOTE! Each of the iocb union members has the file pointer
527 * as the first entry in their struct definition. So you can
528 * access the file pointer through any of the sub-structs,
529 * or directly as just 'ki_filp' in this struct.
530 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700531struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700532 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600533 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700534 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700535 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700536 struct io_accept accept;
537 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700538 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700539 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700540 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700541 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700542 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700543 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700544 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700545 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700546 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700547 struct io_epoll epoll;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700548 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700549
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700550 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300551 /*
552 * llist_node is only used for poll deferred completions
553 */
554 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300555 bool in_async;
556 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700557 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700558
559 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700560 union {
561 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700562 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700563 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600564 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700565 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700566 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700567 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600568 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600569 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700570
Jens Axboefcb323c2019-10-24 12:39:47 -0600571 struct list_head inflight_entry;
572
Jens Axboe561fb042019-10-24 07:25:42 -0600573 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700574};
575
576#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700577#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700578
Jens Axboe9a56a232019-01-09 09:06:50 -0700579struct io_submit_state {
580 struct blk_plug plug;
581
582 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700583 * io_kiocb alloc cache
584 */
585 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300586 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700587
588 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700589 * File reference cache
590 */
591 struct file *file;
592 unsigned int fd;
593 unsigned int has_refs;
594 unsigned int used_refs;
595 unsigned int ios_left;
596};
597
Jens Axboed3656342019-12-18 09:50:26 -0700598struct io_op_def {
599 /* needs req->io allocated for deferral/async */
600 unsigned async_ctx : 1;
601 /* needs current->mm setup, does mm access */
602 unsigned needs_mm : 1;
603 /* needs req->file assigned */
604 unsigned needs_file : 1;
605 /* needs req->file assigned IFF fd is >= 0 */
606 unsigned fd_non_neg : 1;
607 /* hash wq insertion if file is a regular file */
608 unsigned hash_reg_file : 1;
609 /* unbound wq insertion if file is a non-regular file */
610 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700611 /* opcode is not supported by this kernel */
612 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700613 /* needs file table */
614 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700615 /* needs ->fs */
616 unsigned needs_fs : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700617};
618
619static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300620 [IORING_OP_NOP] = {},
621 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700622 .async_ctx = 1,
623 .needs_mm = 1,
624 .needs_file = 1,
625 .unbound_nonreg_file = 1,
626 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300627 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700628 .async_ctx = 1,
629 .needs_mm = 1,
630 .needs_file = 1,
631 .hash_reg_file = 1,
632 .unbound_nonreg_file = 1,
633 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300634 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700635 .needs_file = 1,
636 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300637 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700638 .needs_file = 1,
639 .unbound_nonreg_file = 1,
640 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300641 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700642 .needs_file = 1,
643 .hash_reg_file = 1,
644 .unbound_nonreg_file = 1,
645 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300646 [IORING_OP_POLL_ADD] = {
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_POLL_REMOVE] = {},
651 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700652 .needs_file = 1,
653 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300654 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700655 .async_ctx = 1,
656 .needs_mm = 1,
657 .needs_file = 1,
658 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700659 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700660 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300661 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700662 .async_ctx = 1,
663 .needs_mm = 1,
664 .needs_file = 1,
665 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700666 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700667 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300668 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700669 .async_ctx = 1,
670 .needs_mm = 1,
671 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300672 [IORING_OP_TIMEOUT_REMOVE] = {},
673 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700674 .needs_mm = 1,
675 .needs_file = 1,
676 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700677 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700678 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300679 [IORING_OP_ASYNC_CANCEL] = {},
680 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700681 .async_ctx = 1,
682 .needs_mm = 1,
683 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300684 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700685 .async_ctx = 1,
686 .needs_mm = 1,
687 .needs_file = 1,
688 .unbound_nonreg_file = 1,
689 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300690 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700691 .needs_file = 1,
692 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300693 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700694 .needs_file = 1,
695 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700696 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700697 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700698 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300699 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700700 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700701 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700702 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300703 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700704 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700705 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700706 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300707 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700708 .needs_mm = 1,
709 .needs_file = 1,
710 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700711 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700712 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300713 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700714 .needs_mm = 1,
715 .needs_file = 1,
716 .unbound_nonreg_file = 1,
717 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300718 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700719 .needs_mm = 1,
720 .needs_file = 1,
721 .unbound_nonreg_file = 1,
722 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300723 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700724 .needs_file = 1,
725 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300726 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700727 .needs_mm = 1,
728 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300729 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700730 .needs_mm = 1,
731 .needs_file = 1,
732 .unbound_nonreg_file = 1,
733 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300734 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700735 .needs_mm = 1,
736 .needs_file = 1,
737 .unbound_nonreg_file = 1,
738 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300739 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700740 .needs_file = 1,
741 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700742 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700743 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700744 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700745 [IORING_OP_EPOLL_CTL] = {
746 .unbound_nonreg_file = 1,
747 .file_table = 1,
748 },
Jens Axboed3656342019-12-18 09:50:26 -0700749};
750
Jens Axboe561fb042019-10-24 07:25:42 -0600751static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700752static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800753static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700754static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700755static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
756static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700757static int __io_sqe_files_update(struct io_ring_ctx *ctx,
758 struct io_uring_files_update *ip,
759 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700760static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700761static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300762static void io_cleanup_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600763
Jens Axboe2b188cc2019-01-07 10:46:33 -0700764static struct kmem_cache *req_cachep;
765
766static const struct file_operations io_uring_fops;
767
768struct sock *io_uring_get_socket(struct file *file)
769{
770#if defined(CONFIG_UNIX)
771 if (file->f_op == &io_uring_fops) {
772 struct io_ring_ctx *ctx = file->private_data;
773
774 return ctx->ring_sock->sk;
775 }
776#endif
777 return NULL;
778}
779EXPORT_SYMBOL(io_uring_get_socket);
780
781static void io_ring_ctx_ref_free(struct percpu_ref *ref)
782{
783 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
784
Jens Axboe206aefd2019-11-07 18:27:42 -0700785 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700786}
787
788static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
789{
790 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700791 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700792
793 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
794 if (!ctx)
795 return NULL;
796
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700797 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
798 if (!ctx->fallback_req)
799 goto err;
800
Jens Axboe206aefd2019-11-07 18:27:42 -0700801 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
802 if (!ctx->completions)
803 goto err;
804
Jens Axboe78076bb2019-12-04 19:56:40 -0700805 /*
806 * Use 5 bits less than the max cq entries, that should give us around
807 * 32 entries per hash list if totally full and uniformly spread.
808 */
809 hash_bits = ilog2(p->cq_entries);
810 hash_bits -= 5;
811 if (hash_bits <= 0)
812 hash_bits = 1;
813 ctx->cancel_hash_bits = hash_bits;
814 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
815 GFP_KERNEL);
816 if (!ctx->cancel_hash)
817 goto err;
818 __hash_init(ctx->cancel_hash, 1U << hash_bits);
819
Roman Gushchin21482892019-05-07 10:01:48 -0700820 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700821 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
822 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700823
824 ctx->flags = p->flags;
825 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700826 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700827 init_completion(&ctx->completions[0]);
828 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700829 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700830 mutex_init(&ctx->uring_lock);
831 init_waitqueue_head(&ctx->wait);
832 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700833 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700834 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600835 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600836 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600837 init_waitqueue_head(&ctx->inflight_wait);
838 spin_lock_init(&ctx->inflight_lock);
839 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700840 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700841err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700842 if (ctx->fallback_req)
843 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700844 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700845 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700846 kfree(ctx);
847 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700848}
849
Bob Liu9d858b22019-11-13 18:06:25 +0800850static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600851{
Jackie Liua197f662019-11-08 08:09:12 -0700852 struct io_ring_ctx *ctx = req->ctx;
853
Jens Axboe498ccd92019-10-25 10:04:25 -0600854 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
855 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600856}
857
Bob Liu9d858b22019-11-13 18:06:25 +0800858static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600859{
Pavel Begunkov87987892020-01-18 01:22:30 +0300860 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800861 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600862
Bob Liu9d858b22019-11-13 18:06:25 +0800863 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600864}
865
866static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600867{
868 struct io_kiocb *req;
869
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600870 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800871 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600872 list_del_init(&req->list);
873 return req;
874 }
875
876 return NULL;
877}
878
Jens Axboe5262f562019-09-17 12:26:57 -0600879static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
880{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600881 struct io_kiocb *req;
882
883 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700884 if (req) {
885 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
886 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800887 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700888 list_del_init(&req->list);
889 return req;
890 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600891 }
892
893 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600894}
895
Jens Axboede0617e2019-04-06 21:51:27 -0600896static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700897{
Hristo Venev75b28af2019-08-26 17:23:46 +0000898 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700899
Pavel Begunkov07910152020-01-17 03:52:46 +0300900 /* order cqe stores with ring update */
901 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700902
Pavel Begunkov07910152020-01-17 03:52:46 +0300903 if (wq_has_sleeper(&ctx->cq_wait)) {
904 wake_up_interruptible(&ctx->cq_wait);
905 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700906 }
907}
908
Jens Axboecccf0ee2020-01-27 16:34:48 -0700909static inline void io_req_work_grab_env(struct io_kiocb *req,
910 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600911{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700912 if (!req->work.mm && def->needs_mm) {
913 mmgrab(current->mm);
914 req->work.mm = current->mm;
915 }
916 if (!req->work.creds)
917 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700918 if (!req->work.fs && def->needs_fs) {
919 spin_lock(&current->fs->lock);
920 if (!current->fs->in_exec) {
921 req->work.fs = current->fs;
922 req->work.fs->users++;
923 } else {
924 req->work.flags |= IO_WQ_WORK_CANCEL;
925 }
926 spin_unlock(&current->fs->lock);
927 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700928 if (!req->work.task_pid)
929 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700930}
931
932static inline void io_req_work_drop_env(struct io_kiocb *req)
933{
934 if (req->work.mm) {
935 mmdrop(req->work.mm);
936 req->work.mm = NULL;
937 }
938 if (req->work.creds) {
939 put_cred(req->work.creds);
940 req->work.creds = NULL;
941 }
Jens Axboeff002b32020-02-07 16:05:21 -0700942 if (req->work.fs) {
943 struct fs_struct *fs = req->work.fs;
944
945 spin_lock(&req->work.fs->lock);
946 if (--fs->users)
947 fs = NULL;
948 spin_unlock(&req->work.fs->lock);
949 if (fs)
950 free_fs_struct(fs);
951 }
Jens Axboe561fb042019-10-24 07:25:42 -0600952}
953
Jens Axboe94ae5e72019-11-14 19:39:52 -0700954static inline bool io_prep_async_work(struct io_kiocb *req,
955 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600956{
Jens Axboed3656342019-12-18 09:50:26 -0700957 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600958 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600959
Jens Axboed3656342019-12-18 09:50:26 -0700960 if (req->flags & REQ_F_ISREG) {
961 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700962 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700963 } else {
964 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700965 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600966 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700967
968 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600969
Jens Axboe94ae5e72019-11-14 19:39:52 -0700970 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600971 return do_hashed;
972}
973
Jackie Liua197f662019-11-08 08:09:12 -0700974static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600975{
Jackie Liua197f662019-11-08 08:09:12 -0700976 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700977 struct io_kiocb *link;
978 bool do_hashed;
979
980 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600981
982 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
983 req->flags);
984 if (!do_hashed) {
985 io_wq_enqueue(ctx->io_wq, &req->work);
986 } else {
987 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
988 file_inode(req->file));
989 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700990
991 if (link)
992 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600993}
994
Jens Axboe5262f562019-09-17 12:26:57 -0600995static void io_kill_timeout(struct io_kiocb *req)
996{
997 int ret;
998
Jens Axboe2d283902019-12-04 11:08:05 -0700999 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001000 if (ret != -1) {
1001 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001002 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001003 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001004 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001005 }
1006}
1007
1008static void io_kill_timeouts(struct io_ring_ctx *ctx)
1009{
1010 struct io_kiocb *req, *tmp;
1011
1012 spin_lock_irq(&ctx->completion_lock);
1013 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1014 io_kill_timeout(req);
1015 spin_unlock_irq(&ctx->completion_lock);
1016}
1017
Jens Axboede0617e2019-04-06 21:51:27 -06001018static void io_commit_cqring(struct io_ring_ctx *ctx)
1019{
1020 struct io_kiocb *req;
1021
Jens Axboe5262f562019-09-17 12:26:57 -06001022 while ((req = io_get_timeout_req(ctx)) != NULL)
1023 io_kill_timeout(req);
1024
Jens Axboede0617e2019-04-06 21:51:27 -06001025 __io_commit_cqring(ctx);
1026
Pavel Begunkov87987892020-01-18 01:22:30 +03001027 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001028 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001029}
1030
Jens Axboe2b188cc2019-01-07 10:46:33 -07001031static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1032{
Hristo Venev75b28af2019-08-26 17:23:46 +00001033 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001034 unsigned tail;
1035
1036 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001037 /*
1038 * writes to the cq entry need to come after reading head; the
1039 * control dependency is enough as we're using WRITE_ONCE to
1040 * fill the cq entry
1041 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001042 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001043 return NULL;
1044
1045 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001046 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047}
1048
Jens Axboef2842ab2020-01-08 11:04:00 -07001049static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1050{
Jens Axboef0b493e2020-02-01 21:30:11 -07001051 if (!ctx->cq_ev_fd)
1052 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001053 if (!ctx->eventfd_async)
1054 return true;
1055 return io_wq_current_is_worker() || in_interrupt();
1056}
1057
Jens Axboef0b493e2020-02-01 21:30:11 -07001058static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001059{
1060 if (waitqueue_active(&ctx->wait))
1061 wake_up(&ctx->wait);
1062 if (waitqueue_active(&ctx->sqo_wait))
1063 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001064 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001065 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001066}
1067
Jens Axboef0b493e2020-02-01 21:30:11 -07001068static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1069{
1070 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1071}
1072
Jens Axboec4a2ed72019-11-21 21:01:26 -07001073/* Returns true if there are no backlogged entries after the flush */
1074static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001075{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001076 struct io_rings *rings = ctx->rings;
1077 struct io_uring_cqe *cqe;
1078 struct io_kiocb *req;
1079 unsigned long flags;
1080 LIST_HEAD(list);
1081
1082 if (!force) {
1083 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001084 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001085 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1086 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001087 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001088 }
1089
1090 spin_lock_irqsave(&ctx->completion_lock, flags);
1091
1092 /* if force is set, the ring is going away. always drop after that */
1093 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001094 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001095
Jens Axboec4a2ed72019-11-21 21:01:26 -07001096 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001097 while (!list_empty(&ctx->cq_overflow_list)) {
1098 cqe = io_get_cqring(ctx);
1099 if (!cqe && !force)
1100 break;
1101
1102 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1103 list);
1104 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001105 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001106 if (cqe) {
1107 WRITE_ONCE(cqe->user_data, req->user_data);
1108 WRITE_ONCE(cqe->res, req->result);
1109 WRITE_ONCE(cqe->flags, 0);
1110 } else {
1111 WRITE_ONCE(ctx->rings->cq_overflow,
1112 atomic_inc_return(&ctx->cached_cq_overflow));
1113 }
1114 }
1115
1116 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001117 if (cqe) {
1118 clear_bit(0, &ctx->sq_check_overflow);
1119 clear_bit(0, &ctx->cq_check_overflow);
1120 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001121 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1122 io_cqring_ev_posted(ctx);
1123
1124 while (!list_empty(&list)) {
1125 req = list_first_entry(&list, struct io_kiocb, list);
1126 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001127 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001128 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001129
1130 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001131}
1132
Jens Axboe78e19bb2019-11-06 15:21:34 -07001133static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001134{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001135 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001136 struct io_uring_cqe *cqe;
1137
Jens Axboe78e19bb2019-11-06 15:21:34 -07001138 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001139
Jens Axboe2b188cc2019-01-07 10:46:33 -07001140 /*
1141 * If we can't get a cq entry, userspace overflowed the
1142 * submission (by quite a lot). Increment the overflow count in
1143 * the ring.
1144 */
1145 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001146 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001147 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001148 WRITE_ONCE(cqe->res, res);
1149 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001150 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001151 WRITE_ONCE(ctx->rings->cq_overflow,
1152 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001153 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001154 if (list_empty(&ctx->cq_overflow_list)) {
1155 set_bit(0, &ctx->sq_check_overflow);
1156 set_bit(0, &ctx->cq_check_overflow);
1157 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001158 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001159 refcount_inc(&req->refs);
1160 req->result = res;
1161 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001162 }
1163}
1164
Jens Axboe78e19bb2019-11-06 15:21:34 -07001165static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001166{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001167 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001168 unsigned long flags;
1169
1170 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001171 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001172 io_commit_cqring(ctx);
1173 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1174
Jens Axboe8c838782019-03-12 15:48:16 -06001175 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001176}
1177
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001178static inline bool io_is_fallback_req(struct io_kiocb *req)
1179{
1180 return req == (struct io_kiocb *)
1181 ((unsigned long) req->ctx->fallback_req & ~1UL);
1182}
1183
1184static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1185{
1186 struct io_kiocb *req;
1187
1188 req = ctx->fallback_req;
1189 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1190 return req;
1191
1192 return NULL;
1193}
1194
Jens Axboe2579f912019-01-09 09:10:43 -07001195static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1196 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001197{
Jens Axboefd6fab22019-03-14 16:30:06 -06001198 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001199 struct io_kiocb *req;
1200
Jens Axboe2579f912019-01-09 09:10:43 -07001201 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001202 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001203 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001204 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001205 } else if (!state->free_reqs) {
1206 size_t sz;
1207 int ret;
1208
1209 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001210 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1211
1212 /*
1213 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1214 * retry single alloc to be on the safe side.
1215 */
1216 if (unlikely(ret <= 0)) {
1217 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1218 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001219 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001220 ret = 1;
1221 }
Jens Axboe2579f912019-01-09 09:10:43 -07001222 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001223 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001224 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001225 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001226 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001227 }
1228
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001229got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001230 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001231 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001232 req->ctx = ctx;
1233 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001234 /* one is dropped after submission, the other at completion */
1235 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001236 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001237 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001238 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001239fallback:
1240 req = io_get_fallback_req(ctx);
1241 if (req)
1242 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001243 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001244 return NULL;
1245}
1246
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001247static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001248{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001249 if (likely(!io_is_fallback_req(req)))
1250 kmem_cache_free(req_cachep, req);
1251 else
1252 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1253}
1254
Jens Axboec6ca97b302019-12-28 12:11:08 -07001255static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001256{
Jens Axboefcb323c2019-10-24 12:39:47 -06001257 struct io_ring_ctx *ctx = req->ctx;
1258
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001259 if (req->flags & REQ_F_NEED_CLEANUP)
1260 io_cleanup_req(req);
1261
YueHaibing96fd84d2020-01-07 22:22:44 +08001262 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001263 if (req->file) {
1264 if (req->flags & REQ_F_FIXED_FILE)
1265 percpu_ref_put(&ctx->file_data->refs);
1266 else
1267 fput(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001268 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001269
1270 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001271}
1272
1273static void __io_free_req(struct io_kiocb *req)
1274{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001275 __io_req_aux_free(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001276
Jens Axboefcb323c2019-10-24 12:39:47 -06001277 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001278 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001279 unsigned long flags;
1280
1281 spin_lock_irqsave(&ctx->inflight_lock, flags);
1282 list_del(&req->inflight_entry);
1283 if (waitqueue_active(&ctx->inflight_wait))
1284 wake_up(&ctx->inflight_wait);
1285 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1286 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001287
1288 percpu_ref_put(&req->ctx->refs);
1289 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001290}
1291
Jens Axboec6ca97b302019-12-28 12:11:08 -07001292struct req_batch {
1293 void *reqs[IO_IOPOLL_BATCH];
1294 int to_free;
1295 int need_iter;
1296};
1297
1298static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1299{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001300 int fixed_refs = rb->to_free;
1301
Jens Axboec6ca97b302019-12-28 12:11:08 -07001302 if (!rb->to_free)
1303 return;
1304 if (rb->need_iter) {
1305 int i, inflight = 0;
1306 unsigned long flags;
1307
Jens Axboe10fef4b2020-01-09 07:52:28 -07001308 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001309 for (i = 0; i < rb->to_free; i++) {
1310 struct io_kiocb *req = rb->reqs[i];
1311
Jens Axboe10fef4b2020-01-09 07:52:28 -07001312 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001313 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001314 fixed_refs++;
1315 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001316 if (req->flags & REQ_F_INFLIGHT)
1317 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001318 __io_req_aux_free(req);
1319 }
1320 if (!inflight)
1321 goto do_free;
1322
1323 spin_lock_irqsave(&ctx->inflight_lock, flags);
1324 for (i = 0; i < rb->to_free; i++) {
1325 struct io_kiocb *req = rb->reqs[i];
1326
Jens Axboe10fef4b2020-01-09 07:52:28 -07001327 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001328 list_del(&req->inflight_entry);
1329 if (!--inflight)
1330 break;
1331 }
1332 }
1333 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1334
1335 if (waitqueue_active(&ctx->inflight_wait))
1336 wake_up(&ctx->inflight_wait);
1337 }
1338do_free:
1339 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001340 if (fixed_refs)
1341 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001342 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001343 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001344}
1345
Jackie Liua197f662019-11-08 08:09:12 -07001346static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001347{
Jackie Liua197f662019-11-08 08:09:12 -07001348 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001349 int ret;
1350
Jens Axboe2d283902019-12-04 11:08:05 -07001351 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001352 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001353 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001354 io_commit_cqring(ctx);
1355 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001356 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001357 return true;
1358 }
1359
1360 return false;
1361}
1362
Jens Axboeba816ad2019-09-28 11:36:45 -06001363static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001364{
Jens Axboe2665abf2019-11-05 12:40:47 -07001365 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001366 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001367
Jens Axboe4d7dd462019-11-20 13:03:52 -07001368 /* Already got next link */
1369 if (req->flags & REQ_F_LINK_NEXT)
1370 return;
1371
Jens Axboe9e645e112019-05-10 16:07:28 -06001372 /*
1373 * The list should never be empty when we are called here. But could
1374 * potentially happen if the chain is messed up, check to be on the
1375 * safe side.
1376 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001377 while (!list_empty(&req->link_list)) {
1378 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1379 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001380
Pavel Begunkov44932332019-12-05 16:16:35 +03001381 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1382 (nxt->flags & REQ_F_TIMEOUT))) {
1383 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001384 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001385 req->flags &= ~REQ_F_LINK_TIMEOUT;
1386 continue;
1387 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001388
Pavel Begunkov44932332019-12-05 16:16:35 +03001389 list_del_init(&req->link_list);
1390 if (!list_empty(&nxt->link_list))
1391 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001392 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001393 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001394 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001395
Jens Axboe4d7dd462019-11-20 13:03:52 -07001396 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001397 if (wake_ev)
1398 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001399}
1400
1401/*
1402 * Called if REQ_F_LINK is set, and we fail the head request
1403 */
1404static void io_fail_links(struct io_kiocb *req)
1405{
Jens Axboe2665abf2019-11-05 12:40:47 -07001406 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001407 unsigned long flags;
1408
1409 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001410
1411 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001412 struct io_kiocb *link = list_first_entry(&req->link_list,
1413 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001414
Pavel Begunkov44932332019-12-05 16:16:35 +03001415 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001416 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001417
1418 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001419 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001420 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001421 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001422 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001423 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001424 }
Jens Axboe5d960722019-11-19 15:31:28 -07001425 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001426 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001427
1428 io_commit_cqring(ctx);
1429 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1430 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001431}
1432
Jens Axboe4d7dd462019-11-20 13:03:52 -07001433static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001434{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001435 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001436 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001437
Jens Axboe9e645e112019-05-10 16:07:28 -06001438 /*
1439 * If LINK is set, we have dependent requests in this chain. If we
1440 * didn't fail this request, queue the first one up, moving any other
1441 * dependencies to the next request. In case of failure, fail the rest
1442 * of the chain.
1443 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001444 if (req->flags & REQ_F_FAIL_LINK) {
1445 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001446 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1447 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001448 struct io_ring_ctx *ctx = req->ctx;
1449 unsigned long flags;
1450
1451 /*
1452 * If this is a timeout link, we could be racing with the
1453 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001454 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001455 */
1456 spin_lock_irqsave(&ctx->completion_lock, flags);
1457 io_req_link_next(req, nxt);
1458 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1459 } else {
1460 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001461 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001462}
Jens Axboe9e645e112019-05-10 16:07:28 -06001463
Jackie Liuc69f8db2019-11-09 11:00:08 +08001464static void io_free_req(struct io_kiocb *req)
1465{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001466 struct io_kiocb *nxt = NULL;
1467
1468 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001469 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001470
1471 if (nxt)
1472 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001473}
1474
Jens Axboeba816ad2019-09-28 11:36:45 -06001475/*
1476 * Drop reference to request, return next in chain (if there is one) if this
1477 * was the last reference to this request.
1478 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001479__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001480static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001481{
Jens Axboe2a44f462020-02-25 13:25:41 -07001482 if (refcount_dec_and_test(&req->refs)) {
1483 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001484 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001485 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001486}
1487
Jens Axboe2b188cc2019-01-07 10:46:33 -07001488static void io_put_req(struct io_kiocb *req)
1489{
Jens Axboedef596e2019-01-09 08:59:42 -07001490 if (refcount_dec_and_test(&req->refs))
1491 io_free_req(req);
1492}
1493
Jens Axboe978db572019-11-14 22:39:04 -07001494/*
1495 * Must only be used if we don't need to care about links, usually from
1496 * within the completion handling itself.
1497 */
1498static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001499{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001500 /* drop both submit and complete references */
1501 if (refcount_sub_and_test(2, &req->refs))
1502 __io_free_req(req);
1503}
1504
Jens Axboe978db572019-11-14 22:39:04 -07001505static void io_double_put_req(struct io_kiocb *req)
1506{
1507 /* drop both submit and complete references */
1508 if (refcount_sub_and_test(2, &req->refs))
1509 io_free_req(req);
1510}
1511
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001512static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001513{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001514 struct io_rings *rings = ctx->rings;
1515
Jens Axboead3eb2c2019-12-18 17:12:20 -07001516 if (test_bit(0, &ctx->cq_check_overflow)) {
1517 /*
1518 * noflush == true is from the waitqueue handler, just ensure
1519 * we wake up the task, and the next invocation will flush the
1520 * entries. We cannot safely to it from here.
1521 */
1522 if (noflush && !list_empty(&ctx->cq_overflow_list))
1523 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001524
Jens Axboead3eb2c2019-12-18 17:12:20 -07001525 io_cqring_overflow_flush(ctx, false);
1526 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001527
Jens Axboea3a0e432019-08-20 11:03:11 -06001528 /* See comment at the top of this file */
1529 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001530 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001531}
1532
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001533static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1534{
1535 struct io_rings *rings = ctx->rings;
1536
1537 /* make sure SQ entry isn't read before tail */
1538 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1539}
1540
Jens Axboe8237e042019-12-28 10:48:22 -07001541static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001542{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001543 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1544 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001545
Jens Axboec6ca97b302019-12-28 12:11:08 -07001546 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1547 rb->need_iter++;
1548
1549 rb->reqs[rb->to_free++] = req;
1550 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1551 io_free_req_many(req->ctx, rb);
1552 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001553}
1554
Jens Axboedef596e2019-01-09 08:59:42 -07001555/*
1556 * Find and free completed poll iocbs
1557 */
1558static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1559 struct list_head *done)
1560{
Jens Axboe8237e042019-12-28 10:48:22 -07001561 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001562 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001563
Jens Axboec6ca97b302019-12-28 12:11:08 -07001564 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001565 while (!list_empty(done)) {
1566 req = list_first_entry(done, struct io_kiocb, list);
1567 list_del(&req->list);
1568
Jens Axboe78e19bb2019-11-06 15:21:34 -07001569 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001570 (*nr_events)++;
1571
Jens Axboe8237e042019-12-28 10:48:22 -07001572 if (refcount_dec_and_test(&req->refs) &&
1573 !io_req_multi_free(&rb, req))
1574 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001575 }
Jens Axboedef596e2019-01-09 08:59:42 -07001576
Jens Axboe09bb8392019-03-13 12:39:28 -06001577 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001578 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001579}
1580
1581static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1582 long min)
1583{
1584 struct io_kiocb *req, *tmp;
1585 LIST_HEAD(done);
1586 bool spin;
1587 int ret;
1588
1589 /*
1590 * Only spin for completions if we don't have multiple devices hanging
1591 * off our complete list, and we're under the requested amount.
1592 */
1593 spin = !ctx->poll_multi_file && *nr_events < min;
1594
1595 ret = 0;
1596 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001597 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001598
1599 /*
1600 * Move completed entries to our local list. If we find a
1601 * request that requires polling, break out and complete
1602 * the done list first, if we have entries there.
1603 */
1604 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1605 list_move_tail(&req->list, &done);
1606 continue;
1607 }
1608 if (!list_empty(&done))
1609 break;
1610
1611 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1612 if (ret < 0)
1613 break;
1614
1615 if (ret && spin)
1616 spin = false;
1617 ret = 0;
1618 }
1619
1620 if (!list_empty(&done))
1621 io_iopoll_complete(ctx, nr_events, &done);
1622
1623 return ret;
1624}
1625
1626/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001627 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001628 * non-spinning poll check - we'll still enter the driver poll loop, but only
1629 * as a non-spinning completion check.
1630 */
1631static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1632 long min)
1633{
Jens Axboe08f54392019-08-21 22:19:11 -06001634 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001635 int ret;
1636
1637 ret = io_do_iopoll(ctx, nr_events, min);
1638 if (ret < 0)
1639 return ret;
1640 if (!min || *nr_events >= min)
1641 return 0;
1642 }
1643
1644 return 1;
1645}
1646
1647/*
1648 * We can't just wait for polled events to come to us, we have to actively
1649 * find and complete them.
1650 */
1651static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1652{
1653 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1654 return;
1655
1656 mutex_lock(&ctx->uring_lock);
1657 while (!list_empty(&ctx->poll_list)) {
1658 unsigned int nr_events = 0;
1659
1660 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001661
1662 /*
1663 * Ensure we allow local-to-the-cpu processing to take place,
1664 * in this case we need to ensure that we reap all events.
1665 */
1666 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001667 }
1668 mutex_unlock(&ctx->uring_lock);
1669}
1670
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001671static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1672 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001673{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001674 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001675
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001676 /*
1677 * We disallow the app entering submit/complete with polling, but we
1678 * still need to lock the ring to prevent racing with polled issue
1679 * that got punted to a workqueue.
1680 */
1681 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001682 do {
1683 int tmin = 0;
1684
Jens Axboe500f9fb2019-08-19 12:15:59 -06001685 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001686 * Don't enter poll loop if we already have events pending.
1687 * If we do, we can potentially be spinning for commands that
1688 * already triggered a CQE (eg in error).
1689 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001690 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001691 break;
1692
1693 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001694 * If a submit got punted to a workqueue, we can have the
1695 * application entering polling for a command before it gets
1696 * issued. That app will hold the uring_lock for the duration
1697 * of the poll right here, so we need to take a breather every
1698 * now and then to ensure that the issue has a chance to add
1699 * the poll to the issued list. Otherwise we can spin here
1700 * forever, while the workqueue is stuck trying to acquire the
1701 * very same mutex.
1702 */
1703 if (!(++iters & 7)) {
1704 mutex_unlock(&ctx->uring_lock);
1705 mutex_lock(&ctx->uring_lock);
1706 }
1707
Jens Axboedef596e2019-01-09 08:59:42 -07001708 if (*nr_events < min)
1709 tmin = min - *nr_events;
1710
1711 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1712 if (ret <= 0)
1713 break;
1714 ret = 0;
1715 } while (min && !*nr_events && !need_resched());
1716
Jens Axboe500f9fb2019-08-19 12:15:59 -06001717 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001718 return ret;
1719}
1720
Jens Axboe491381ce2019-10-17 09:20:46 -06001721static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001722{
Jens Axboe491381ce2019-10-17 09:20:46 -06001723 /*
1724 * Tell lockdep we inherited freeze protection from submission
1725 * thread.
1726 */
1727 if (req->flags & REQ_F_ISREG) {
1728 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001729
Jens Axboe491381ce2019-10-17 09:20:46 -06001730 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001731 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001732 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001733}
1734
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001735static inline void req_set_fail_links(struct io_kiocb *req)
1736{
1737 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1738 req->flags |= REQ_F_FAIL_LINK;
1739}
1740
Jens Axboeba816ad2019-09-28 11:36:45 -06001741static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001742{
Jens Axboe9adbd452019-12-20 08:45:55 -07001743 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001744
Jens Axboe491381ce2019-10-17 09:20:46 -06001745 if (kiocb->ki_flags & IOCB_WRITE)
1746 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001747
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001748 if (res != req->result)
1749 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001750 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001751}
1752
1753static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1754{
Jens Axboe9adbd452019-12-20 08:45:55 -07001755 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001756
1757 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001758 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001759}
1760
Jens Axboeba816ad2019-09-28 11:36:45 -06001761static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1762{
Jens Axboe9adbd452019-12-20 08:45:55 -07001763 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001764 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001765
1766 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001767 io_put_req_find_next(req, &nxt);
1768
1769 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001770}
1771
Jens Axboedef596e2019-01-09 08:59:42 -07001772static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1773{
Jens Axboe9adbd452019-12-20 08:45:55 -07001774 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001775
Jens Axboe491381ce2019-10-17 09:20:46 -06001776 if (kiocb->ki_flags & IOCB_WRITE)
1777 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001778
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001779 if (res != req->result)
1780 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001781 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001782 if (res != -EAGAIN)
1783 req->flags |= REQ_F_IOPOLL_COMPLETED;
1784}
1785
1786/*
1787 * After the iocb has been issued, it's safe to be found on the poll list.
1788 * Adding the kiocb to the list AFTER submission ensures that we don't
1789 * find it from a io_iopoll_getevents() thread before the issuer is done
1790 * accessing the kiocb cookie.
1791 */
1792static void io_iopoll_req_issued(struct io_kiocb *req)
1793{
1794 struct io_ring_ctx *ctx = req->ctx;
1795
1796 /*
1797 * Track whether we have multiple files in our lists. This will impact
1798 * how we do polling eventually, not spinning if we're on potentially
1799 * different devices.
1800 */
1801 if (list_empty(&ctx->poll_list)) {
1802 ctx->poll_multi_file = false;
1803 } else if (!ctx->poll_multi_file) {
1804 struct io_kiocb *list_req;
1805
1806 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1807 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001808 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001809 ctx->poll_multi_file = true;
1810 }
1811
1812 /*
1813 * For fast devices, IO may have already completed. If it has, add
1814 * it to the front so we find it first.
1815 */
1816 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1817 list_add(&req->list, &ctx->poll_list);
1818 else
1819 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001820
1821 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1822 wq_has_sleeper(&ctx->sqo_wait))
1823 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001824}
1825
Jens Axboe3d6770f2019-04-13 11:50:54 -06001826static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001827{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001828 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001829 int diff = state->has_refs - state->used_refs;
1830
1831 if (diff)
1832 fput_many(state->file, diff);
1833 state->file = NULL;
1834 }
1835}
1836
1837/*
1838 * Get as many references to a file as we have IOs left in this submission,
1839 * assuming most submissions are for one file, or at least that each file
1840 * has more than one submission.
1841 */
1842static struct file *io_file_get(struct io_submit_state *state, int fd)
1843{
1844 if (!state)
1845 return fget(fd);
1846
1847 if (state->file) {
1848 if (state->fd == fd) {
1849 state->used_refs++;
1850 state->ios_left--;
1851 return state->file;
1852 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001853 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001854 }
1855 state->file = fget_many(fd, state->ios_left);
1856 if (!state->file)
1857 return NULL;
1858
1859 state->fd = fd;
1860 state->has_refs = state->ios_left;
1861 state->used_refs = 1;
1862 state->ios_left--;
1863 return state->file;
1864}
1865
Jens Axboe2b188cc2019-01-07 10:46:33 -07001866/*
1867 * If we tracked the file through the SCM inflight mechanism, we could support
1868 * any file. For now, just ensure that anything potentially problematic is done
1869 * inline.
1870 */
1871static bool io_file_supports_async(struct file *file)
1872{
1873 umode_t mode = file_inode(file)->i_mode;
1874
Jens Axboe10d59342019-12-09 20:16:22 -07001875 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001876 return true;
1877 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1878 return true;
1879
1880 return false;
1881}
1882
Jens Axboe3529d8c2019-12-19 18:24:38 -07001883static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1884 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001885{
Jens Axboedef596e2019-01-09 08:59:42 -07001886 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001887 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001888 unsigned ioprio;
1889 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001890
Jens Axboe491381ce2019-10-17 09:20:46 -06001891 if (S_ISREG(file_inode(req->file)->i_mode))
1892 req->flags |= REQ_F_ISREG;
1893
Jens Axboe2b188cc2019-01-07 10:46:33 -07001894 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001895 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1896 req->flags |= REQ_F_CUR_POS;
1897 kiocb->ki_pos = req->file->f_pos;
1898 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001900 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1901 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1902 if (unlikely(ret))
1903 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001904
1905 ioprio = READ_ONCE(sqe->ioprio);
1906 if (ioprio) {
1907 ret = ioprio_check_cap(ioprio);
1908 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001909 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001910
1911 kiocb->ki_ioprio = ioprio;
1912 } else
1913 kiocb->ki_ioprio = get_current_ioprio();
1914
Stefan Bühler8449eed2019-04-27 20:34:19 +02001915 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001916 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1917 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001918 req->flags |= REQ_F_NOWAIT;
1919
1920 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001921 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001922
Jens Axboedef596e2019-01-09 08:59:42 -07001923 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001924 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1925 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001926 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001927
Jens Axboedef596e2019-01-09 08:59:42 -07001928 kiocb->ki_flags |= IOCB_HIPRI;
1929 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001930 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001931 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001932 if (kiocb->ki_flags & IOCB_HIPRI)
1933 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001934 kiocb->ki_complete = io_complete_rw;
1935 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001936
Jens Axboe3529d8c2019-12-19 18:24:38 -07001937 req->rw.addr = READ_ONCE(sqe->addr);
1938 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001939 /* we own ->private, reuse it for the buffer index */
1940 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001941 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001942 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001943}
1944
1945static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1946{
1947 switch (ret) {
1948 case -EIOCBQUEUED:
1949 break;
1950 case -ERESTARTSYS:
1951 case -ERESTARTNOINTR:
1952 case -ERESTARTNOHAND:
1953 case -ERESTART_RESTARTBLOCK:
1954 /*
1955 * We can't just restart the syscall, since previously
1956 * submitted sqes may already be in progress. Just fail this
1957 * IO with EINTR.
1958 */
1959 ret = -EINTR;
1960 /* fall through */
1961 default:
1962 kiocb->ki_complete(kiocb, ret, 0);
1963 }
1964}
1965
Jens Axboeba816ad2019-09-28 11:36:45 -06001966static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1967 bool in_async)
1968{
Jens Axboeba042912019-12-25 16:33:42 -07001969 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1970
1971 if (req->flags & REQ_F_CUR_POS)
1972 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001973 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001974 *nxt = __io_complete_rw(kiocb, ret);
1975 else
1976 io_rw_done(kiocb, ret);
1977}
1978
Jens Axboe9adbd452019-12-20 08:45:55 -07001979static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001980 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001981{
Jens Axboe9adbd452019-12-20 08:45:55 -07001982 struct io_ring_ctx *ctx = req->ctx;
1983 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001984 struct io_mapped_ubuf *imu;
1985 unsigned index, buf_index;
1986 size_t offset;
1987 u64 buf_addr;
1988
1989 /* attempt to use fixed buffers without having provided iovecs */
1990 if (unlikely(!ctx->user_bufs))
1991 return -EFAULT;
1992
Jens Axboe9adbd452019-12-20 08:45:55 -07001993 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001994 if (unlikely(buf_index >= ctx->nr_user_bufs))
1995 return -EFAULT;
1996
1997 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1998 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001999 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002000
2001 /* overflow */
2002 if (buf_addr + len < buf_addr)
2003 return -EFAULT;
2004 /* not inside the mapped region */
2005 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2006 return -EFAULT;
2007
2008 /*
2009 * May not be a start of buffer, set size appropriately
2010 * and advance us to the beginning.
2011 */
2012 offset = buf_addr - imu->ubuf;
2013 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002014
2015 if (offset) {
2016 /*
2017 * Don't use iov_iter_advance() here, as it's really slow for
2018 * using the latter parts of a big fixed buffer - it iterates
2019 * over each segment manually. We can cheat a bit here, because
2020 * we know that:
2021 *
2022 * 1) it's a BVEC iter, we set it up
2023 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2024 * first and last bvec
2025 *
2026 * So just find our index, and adjust the iterator afterwards.
2027 * If the offset is within the first bvec (or the whole first
2028 * bvec, just use iov_iter_advance(). This makes it easier
2029 * since we can just skip the first segment, which may not
2030 * be PAGE_SIZE aligned.
2031 */
2032 const struct bio_vec *bvec = imu->bvec;
2033
2034 if (offset <= bvec->bv_len) {
2035 iov_iter_advance(iter, offset);
2036 } else {
2037 unsigned long seg_skip;
2038
2039 /* skip first vec */
2040 offset -= bvec->bv_len;
2041 seg_skip = 1 + (offset >> PAGE_SHIFT);
2042
2043 iter->bvec = bvec + seg_skip;
2044 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002045 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002046 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002047 }
2048 }
2049
Jens Axboe5e559562019-11-13 16:12:46 -07002050 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002051}
2052
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002053static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2054 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002055{
Jens Axboe9adbd452019-12-20 08:45:55 -07002056 void __user *buf = u64_to_user_ptr(req->rw.addr);
2057 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002058 u8 opcode;
2059
Jens Axboed625c6e2019-12-17 19:53:05 -07002060 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002061 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002062 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002063 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002064 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002065
Jens Axboe9adbd452019-12-20 08:45:55 -07002066 /* buffer index only valid with fixed read/write */
2067 if (req->rw.kiocb.private)
2068 return -EINVAL;
2069
Jens Axboe3a6820f2019-12-22 15:19:35 -07002070 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2071 ssize_t ret;
2072 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2073 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002074 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002075 }
2076
Jens Axboef67676d2019-12-02 11:03:47 -07002077 if (req->io) {
2078 struct io_async_rw *iorw = &req->io->rw;
2079
2080 *iovec = iorw->iov;
2081 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2082 if (iorw->iov == iorw->fast_iov)
2083 *iovec = NULL;
2084 return iorw->size;
2085 }
2086
Jens Axboe2b188cc2019-01-07 10:46:33 -07002087#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002088 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002089 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2090 iovec, iter);
2091#endif
2092
2093 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2094}
2095
Jens Axboe32960612019-09-23 11:05:34 -06002096/*
2097 * For files that don't have ->read_iter() and ->write_iter(), handle them
2098 * by looping over ->read() or ->write() manually.
2099 */
2100static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2101 struct iov_iter *iter)
2102{
2103 ssize_t ret = 0;
2104
2105 /*
2106 * Don't support polled IO through this interface, and we can't
2107 * support non-blocking either. For the latter, this just causes
2108 * the kiocb to be handled from an async context.
2109 */
2110 if (kiocb->ki_flags & IOCB_HIPRI)
2111 return -EOPNOTSUPP;
2112 if (kiocb->ki_flags & IOCB_NOWAIT)
2113 return -EAGAIN;
2114
2115 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002116 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002117 ssize_t nr;
2118
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002119 if (!iov_iter_is_bvec(iter)) {
2120 iovec = iov_iter_iovec(iter);
2121 } else {
2122 /* fixed buffers import bvec */
2123 iovec.iov_base = kmap(iter->bvec->bv_page)
2124 + iter->iov_offset;
2125 iovec.iov_len = min(iter->count,
2126 iter->bvec->bv_len - iter->iov_offset);
2127 }
2128
Jens Axboe32960612019-09-23 11:05:34 -06002129 if (rw == READ) {
2130 nr = file->f_op->read(file, iovec.iov_base,
2131 iovec.iov_len, &kiocb->ki_pos);
2132 } else {
2133 nr = file->f_op->write(file, iovec.iov_base,
2134 iovec.iov_len, &kiocb->ki_pos);
2135 }
2136
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002137 if (iov_iter_is_bvec(iter))
2138 kunmap(iter->bvec->bv_page);
2139
Jens Axboe32960612019-09-23 11:05:34 -06002140 if (nr < 0) {
2141 if (!ret)
2142 ret = nr;
2143 break;
2144 }
2145 ret += nr;
2146 if (nr != iovec.iov_len)
2147 break;
2148 iov_iter_advance(iter, nr);
2149 }
2150
2151 return ret;
2152}
2153
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002154static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002155 struct iovec *iovec, struct iovec *fast_iov,
2156 struct iov_iter *iter)
2157{
2158 req->io->rw.nr_segs = iter->nr_segs;
2159 req->io->rw.size = io_size;
2160 req->io->rw.iov = iovec;
2161 if (!req->io->rw.iov) {
2162 req->io->rw.iov = req->io->rw.fast_iov;
2163 memcpy(req->io->rw.iov, fast_iov,
2164 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002165 } else {
2166 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002167 }
2168}
2169
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002170static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002171{
Jens Axboed3656342019-12-18 09:50:26 -07002172 if (!io_op_defs[req->opcode].async_ctx)
2173 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002174 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002175 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002176}
2177
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002178static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2179 struct iovec *iovec, struct iovec *fast_iov,
2180 struct iov_iter *iter)
2181{
Jens Axboe980ad262020-01-24 23:08:54 -07002182 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002183 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002184 if (!req->io) {
2185 if (io_alloc_async_ctx(req))
2186 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002187
Jens Axboe5d204bc2020-01-31 12:06:52 -07002188 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2189 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002190 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002191}
2192
Jens Axboe3529d8c2019-12-19 18:24:38 -07002193static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2194 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002195{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002196 struct io_async_ctx *io;
2197 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002198 ssize_t ret;
2199
Jens Axboe3529d8c2019-12-19 18:24:38 -07002200 ret = io_prep_rw(req, sqe, force_nonblock);
2201 if (ret)
2202 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002203
Jens Axboe3529d8c2019-12-19 18:24:38 -07002204 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2205 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002206
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002207 /* either don't need iovec imported or already have it */
2208 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002209 return 0;
2210
2211 io = req->io;
2212 io->rw.iov = io->rw.fast_iov;
2213 req->io = NULL;
2214 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2215 req->io = io;
2216 if (ret < 0)
2217 return ret;
2218
2219 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2220 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002221}
2222
Pavel Begunkov267bc902019-11-07 01:41:08 +03002223static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002224 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002225{
2226 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002227 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002228 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002229 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002230 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002231
Jens Axboe3529d8c2019-12-19 18:24:38 -07002232 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002233 if (ret < 0)
2234 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002235
Jens Axboefd6c2e42019-12-18 12:19:41 -07002236 /* Ensure we clear previously set non-block flag */
2237 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002238 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002239
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002240 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002241 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002242 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002243 req->result = io_size;
2244
2245 /*
2246 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2247 * we know to async punt it even if it was opened O_NONBLOCK
2248 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002249 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002250 req->flags |= REQ_F_MUST_PUNT;
2251 goto copy_iov;
2252 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002253
Jens Axboe31b51512019-01-18 22:56:34 -07002254 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002255 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002256 if (!ret) {
2257 ssize_t ret2;
2258
Jens Axboe9adbd452019-12-20 08:45:55 -07002259 if (req->file->f_op->read_iter)
2260 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002261 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002262 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002263
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002264 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002265 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002266 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002267 } else {
2268copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002269 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002270 inline_vecs, &iter);
2271 if (ret)
2272 goto out_free;
2273 return -EAGAIN;
2274 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002275 }
Jens Axboef67676d2019-12-02 11:03:47 -07002276out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002277 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002278 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002279 return ret;
2280}
2281
Jens Axboe3529d8c2019-12-19 18:24:38 -07002282static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2283 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002284{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002285 struct io_async_ctx *io;
2286 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002287 ssize_t ret;
2288
Jens Axboe3529d8c2019-12-19 18:24:38 -07002289 ret = io_prep_rw(req, sqe, force_nonblock);
2290 if (ret)
2291 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002292
Jens Axboe3529d8c2019-12-19 18:24:38 -07002293 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2294 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002295
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002296 /* either don't need iovec imported or already have it */
2297 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002298 return 0;
2299
2300 io = req->io;
2301 io->rw.iov = io->rw.fast_iov;
2302 req->io = NULL;
2303 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2304 req->io = io;
2305 if (ret < 0)
2306 return ret;
2307
2308 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2309 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002310}
2311
Pavel Begunkov267bc902019-11-07 01:41:08 +03002312static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002313 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002314{
2315 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002316 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002317 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002318 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002319 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002320
Jens Axboe3529d8c2019-12-19 18:24:38 -07002321 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002322 if (ret < 0)
2323 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002324
Jens Axboefd6c2e42019-12-18 12:19:41 -07002325 /* Ensure we clear previously set non-block flag */
2326 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002327 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002328
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002329 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002330 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002331 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002332 req->result = io_size;
2333
2334 /*
2335 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2336 * we know to async punt it even if it was opened O_NONBLOCK
2337 */
2338 if (force_nonblock && !io_file_supports_async(req->file)) {
2339 req->flags |= REQ_F_MUST_PUNT;
2340 goto copy_iov;
2341 }
2342
Jens Axboe10d59342019-12-09 20:16:22 -07002343 /* file path doesn't support NOWAIT for non-direct_IO */
2344 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2345 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002346 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002347
Jens Axboe31b51512019-01-18 22:56:34 -07002348 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002349 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002350 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002351 ssize_t ret2;
2352
Jens Axboe2b188cc2019-01-07 10:46:33 -07002353 /*
2354 * Open-code file_start_write here to grab freeze protection,
2355 * which will be released by another thread in
2356 * io_complete_rw(). Fool lockdep by telling it the lock got
2357 * released so that it doesn't complain about the held lock when
2358 * we return to userspace.
2359 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002360 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002361 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002362 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002363 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002364 SB_FREEZE_WRITE);
2365 }
2366 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002367
Jens Axboe9adbd452019-12-20 08:45:55 -07002368 if (req->file->f_op->write_iter)
2369 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002370 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002371 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002372 /*
2373 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2374 * retry them without IOCB_NOWAIT.
2375 */
2376 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2377 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002378 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002379 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002380 } else {
2381copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002382 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002383 inline_vecs, &iter);
2384 if (ret)
2385 goto out_free;
2386 return -EAGAIN;
2387 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002388 }
Jens Axboe31b51512019-01-18 22:56:34 -07002389out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002390 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002391 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002392 return ret;
2393}
2394
2395/*
2396 * IORING_OP_NOP just posts a completion event, nothing else.
2397 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002398static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002399{
2400 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002401
Jens Axboedef596e2019-01-09 08:59:42 -07002402 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2403 return -EINVAL;
2404
Jens Axboe78e19bb2019-11-06 15:21:34 -07002405 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002406 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002407 return 0;
2408}
2409
Jens Axboe3529d8c2019-12-19 18:24:38 -07002410static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002411{
Jens Axboe6b063142019-01-10 22:13:58 -07002412 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002413
Jens Axboe09bb8392019-03-13 12:39:28 -06002414 if (!req->file)
2415 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002416
Jens Axboe6b063142019-01-10 22:13:58 -07002417 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002418 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002419 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002420 return -EINVAL;
2421
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002422 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2423 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2424 return -EINVAL;
2425
2426 req->sync.off = READ_ONCE(sqe->off);
2427 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002428 return 0;
2429}
2430
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002431static bool io_req_cancelled(struct io_kiocb *req)
2432{
2433 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2434 req_set_fail_links(req);
2435 io_cqring_add_event(req, -ECANCELED);
2436 io_put_req(req);
2437 return true;
2438 }
2439
2440 return false;
2441}
2442
Jens Axboe78912932020-01-14 22:09:06 -07002443static void io_link_work_cb(struct io_wq_work **workptr)
2444{
2445 struct io_wq_work *work = *workptr;
2446 struct io_kiocb *link = work->data;
2447
2448 io_queue_linked_timeout(link);
2449 work->func = io_wq_submit_work;
2450}
2451
2452static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2453{
2454 struct io_kiocb *link;
2455
2456 io_prep_async_work(nxt, &link);
2457 *workptr = &nxt->work;
2458 if (link) {
2459 nxt->work.flags |= IO_WQ_WORK_CB;
2460 nxt->work.func = io_link_work_cb;
2461 nxt->work.data = link;
2462 }
2463}
2464
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002465static void io_fsync_finish(struct io_wq_work **workptr)
2466{
2467 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2468 loff_t end = req->sync.off + req->sync.len;
2469 struct io_kiocb *nxt = NULL;
2470 int ret;
2471
2472 if (io_req_cancelled(req))
2473 return;
2474
Jens Axboe9adbd452019-12-20 08:45:55 -07002475 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002476 end > 0 ? end : LLONG_MAX,
2477 req->sync.flags & IORING_FSYNC_DATASYNC);
2478 if (ret < 0)
2479 req_set_fail_links(req);
2480 io_cqring_add_event(req, ret);
2481 io_put_req_find_next(req, &nxt);
2482 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002483 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002484}
2485
Jens Axboefc4df992019-12-10 14:38:45 -07002486static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2487 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002488{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002489 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002490
2491 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002492 if (force_nonblock) {
2493 io_put_req(req);
2494 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002495 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002496 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002497
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002498 work = old_work = &req->work;
2499 io_fsync_finish(&work);
2500 if (work && work != old_work)
2501 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002502 return 0;
2503}
2504
Jens Axboed63d1b52019-12-10 10:38:56 -07002505static void io_fallocate_finish(struct io_wq_work **workptr)
2506{
2507 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2508 struct io_kiocb *nxt = NULL;
2509 int ret;
2510
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002511 if (io_req_cancelled(req))
2512 return;
2513
Jens Axboed63d1b52019-12-10 10:38:56 -07002514 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2515 req->sync.len);
2516 if (ret < 0)
2517 req_set_fail_links(req);
2518 io_cqring_add_event(req, ret);
2519 io_put_req_find_next(req, &nxt);
2520 if (nxt)
2521 io_wq_assign_next(workptr, nxt);
2522}
2523
2524static int io_fallocate_prep(struct io_kiocb *req,
2525 const struct io_uring_sqe *sqe)
2526{
2527 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2528 return -EINVAL;
2529
2530 req->sync.off = READ_ONCE(sqe->off);
2531 req->sync.len = READ_ONCE(sqe->addr);
2532 req->sync.mode = READ_ONCE(sqe->len);
2533 return 0;
2534}
2535
2536static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2537 bool force_nonblock)
2538{
2539 struct io_wq_work *work, *old_work;
2540
2541 /* fallocate always requiring blocking context */
2542 if (force_nonblock) {
2543 io_put_req(req);
2544 req->work.func = io_fallocate_finish;
2545 return -EAGAIN;
2546 }
2547
2548 work = old_work = &req->work;
2549 io_fallocate_finish(&work);
2550 if (work && work != old_work)
2551 *nxt = container_of(work, struct io_kiocb, work);
2552
2553 return 0;
2554}
2555
Jens Axboe15b71ab2019-12-11 11:20:36 -07002556static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2557{
Jens Axboef8748882020-01-08 17:47:02 -07002558 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002559 int ret;
2560
2561 if (sqe->ioprio || sqe->buf_index)
2562 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002563 if (sqe->flags & IOSQE_FIXED_FILE)
2564 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002565 if (req->flags & REQ_F_NEED_CLEANUP)
2566 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002567
2568 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002569 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002570 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002571 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002572
Jens Axboef8748882020-01-08 17:47:02 -07002573 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002574 if (IS_ERR(req->open.filename)) {
2575 ret = PTR_ERR(req->open.filename);
2576 req->open.filename = NULL;
2577 return ret;
2578 }
2579
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002580 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002581 return 0;
2582}
2583
Jens Axboecebdb982020-01-08 17:59:24 -07002584static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2585{
2586 struct open_how __user *how;
2587 const char __user *fname;
2588 size_t len;
2589 int ret;
2590
2591 if (sqe->ioprio || sqe->buf_index)
2592 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002593 if (sqe->flags & IOSQE_FIXED_FILE)
2594 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002595 if (req->flags & REQ_F_NEED_CLEANUP)
2596 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002597
2598 req->open.dfd = READ_ONCE(sqe->fd);
2599 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2600 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2601 len = READ_ONCE(sqe->len);
2602
2603 if (len < OPEN_HOW_SIZE_VER0)
2604 return -EINVAL;
2605
2606 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2607 len);
2608 if (ret)
2609 return ret;
2610
2611 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2612 req->open.how.flags |= O_LARGEFILE;
2613
2614 req->open.filename = getname(fname);
2615 if (IS_ERR(req->open.filename)) {
2616 ret = PTR_ERR(req->open.filename);
2617 req->open.filename = NULL;
2618 return ret;
2619 }
2620
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002621 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002622 return 0;
2623}
2624
2625static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2626 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002627{
2628 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002629 struct file *file;
2630 int ret;
2631
Jens Axboef86cd202020-01-29 13:46:44 -07002632 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002633 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002634
Jens Axboecebdb982020-01-08 17:59:24 -07002635 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002636 if (ret)
2637 goto err;
2638
Jens Axboecebdb982020-01-08 17:59:24 -07002639 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002640 if (ret < 0)
2641 goto err;
2642
2643 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2644 if (IS_ERR(file)) {
2645 put_unused_fd(ret);
2646 ret = PTR_ERR(file);
2647 } else {
2648 fsnotify_open(file);
2649 fd_install(ret, file);
2650 }
2651err:
2652 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002653 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002654 if (ret < 0)
2655 req_set_fail_links(req);
2656 io_cqring_add_event(req, ret);
2657 io_put_req_find_next(req, nxt);
2658 return 0;
2659}
2660
Jens Axboecebdb982020-01-08 17:59:24 -07002661static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2662 bool force_nonblock)
2663{
2664 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2665 return io_openat2(req, nxt, force_nonblock);
2666}
2667
Jens Axboe3e4827b2020-01-08 15:18:09 -07002668static int io_epoll_ctl_prep(struct io_kiocb *req,
2669 const struct io_uring_sqe *sqe)
2670{
2671#if defined(CONFIG_EPOLL)
2672 if (sqe->ioprio || sqe->buf_index)
2673 return -EINVAL;
2674
2675 req->epoll.epfd = READ_ONCE(sqe->fd);
2676 req->epoll.op = READ_ONCE(sqe->len);
2677 req->epoll.fd = READ_ONCE(sqe->off);
2678
2679 if (ep_op_has_event(req->epoll.op)) {
2680 struct epoll_event __user *ev;
2681
2682 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2683 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2684 return -EFAULT;
2685 }
2686
2687 return 0;
2688#else
2689 return -EOPNOTSUPP;
2690#endif
2691}
2692
2693static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2694 bool force_nonblock)
2695{
2696#if defined(CONFIG_EPOLL)
2697 struct io_epoll *ie = &req->epoll;
2698 int ret;
2699
2700 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2701 if (force_nonblock && ret == -EAGAIN)
2702 return -EAGAIN;
2703
2704 if (ret < 0)
2705 req_set_fail_links(req);
2706 io_cqring_add_event(req, ret);
2707 io_put_req_find_next(req, nxt);
2708 return 0;
2709#else
2710 return -EOPNOTSUPP;
2711#endif
2712}
2713
Jens Axboec1ca7572019-12-25 22:18:28 -07002714static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2715{
2716#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2717 if (sqe->ioprio || sqe->buf_index || sqe->off)
2718 return -EINVAL;
2719
2720 req->madvise.addr = READ_ONCE(sqe->addr);
2721 req->madvise.len = READ_ONCE(sqe->len);
2722 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2723 return 0;
2724#else
2725 return -EOPNOTSUPP;
2726#endif
2727}
2728
2729static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2730 bool force_nonblock)
2731{
2732#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2733 struct io_madvise *ma = &req->madvise;
2734 int ret;
2735
2736 if (force_nonblock)
2737 return -EAGAIN;
2738
2739 ret = do_madvise(ma->addr, ma->len, ma->advice);
2740 if (ret < 0)
2741 req_set_fail_links(req);
2742 io_cqring_add_event(req, ret);
2743 io_put_req_find_next(req, nxt);
2744 return 0;
2745#else
2746 return -EOPNOTSUPP;
2747#endif
2748}
2749
Jens Axboe4840e412019-12-25 22:03:45 -07002750static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2751{
2752 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2753 return -EINVAL;
2754
2755 req->fadvise.offset = READ_ONCE(sqe->off);
2756 req->fadvise.len = READ_ONCE(sqe->len);
2757 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2758 return 0;
2759}
2760
2761static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2762 bool force_nonblock)
2763{
2764 struct io_fadvise *fa = &req->fadvise;
2765 int ret;
2766
Jens Axboe3e694262020-02-01 09:22:49 -07002767 if (force_nonblock) {
2768 switch (fa->advice) {
2769 case POSIX_FADV_NORMAL:
2770 case POSIX_FADV_RANDOM:
2771 case POSIX_FADV_SEQUENTIAL:
2772 break;
2773 default:
2774 return -EAGAIN;
2775 }
2776 }
Jens Axboe4840e412019-12-25 22:03:45 -07002777
2778 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2779 if (ret < 0)
2780 req_set_fail_links(req);
2781 io_cqring_add_event(req, ret);
2782 io_put_req_find_next(req, nxt);
2783 return 0;
2784}
2785
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002786static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2787{
Jens Axboef8748882020-01-08 17:47:02 -07002788 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002789 unsigned lookup_flags;
2790 int ret;
2791
2792 if (sqe->ioprio || sqe->buf_index)
2793 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002794 if (sqe->flags & IOSQE_FIXED_FILE)
2795 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002796 if (req->flags & REQ_F_NEED_CLEANUP)
2797 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002798
2799 req->open.dfd = READ_ONCE(sqe->fd);
2800 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002801 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002802 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002803 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002804
Jens Axboec12cedf2020-01-08 17:41:21 -07002805 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002806 return -EINVAL;
2807
Jens Axboef8748882020-01-08 17:47:02 -07002808 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002809 if (IS_ERR(req->open.filename)) {
2810 ret = PTR_ERR(req->open.filename);
2811 req->open.filename = NULL;
2812 return ret;
2813 }
2814
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002815 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002816 return 0;
2817}
2818
2819static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2820 bool force_nonblock)
2821{
2822 struct io_open *ctx = &req->open;
2823 unsigned lookup_flags;
2824 struct path path;
2825 struct kstat stat;
2826 int ret;
2827
2828 if (force_nonblock)
2829 return -EAGAIN;
2830
Jens Axboec12cedf2020-01-08 17:41:21 -07002831 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002832 return -EINVAL;
2833
2834retry:
2835 /* filename_lookup() drops it, keep a reference */
2836 ctx->filename->refcnt++;
2837
2838 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2839 NULL);
2840 if (ret)
2841 goto err;
2842
Jens Axboec12cedf2020-01-08 17:41:21 -07002843 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002844 path_put(&path);
2845 if (retry_estale(ret, lookup_flags)) {
2846 lookup_flags |= LOOKUP_REVAL;
2847 goto retry;
2848 }
2849 if (!ret)
2850 ret = cp_statx(&stat, ctx->buffer);
2851err:
2852 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002853 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002854 if (ret < 0)
2855 req_set_fail_links(req);
2856 io_cqring_add_event(req, ret);
2857 io_put_req_find_next(req, nxt);
2858 return 0;
2859}
2860
Jens Axboeb5dba592019-12-11 14:02:38 -07002861static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2862{
2863 /*
2864 * If we queue this for async, it must not be cancellable. That would
2865 * leave the 'file' in an undeterminate state.
2866 */
2867 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2868
2869 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2870 sqe->rw_flags || sqe->buf_index)
2871 return -EINVAL;
2872 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002873 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002874
2875 req->close.fd = READ_ONCE(sqe->fd);
2876 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002877 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002878 return -EBADF;
2879
2880 return 0;
2881}
2882
Pavel Begunkova93b3332020-02-08 14:04:34 +03002883/* only called when __close_fd_get_file() is done */
2884static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt)
2885{
2886 int ret;
2887
2888 ret = filp_close(req->close.put_file, req->work.files);
2889 if (ret < 0)
2890 req_set_fail_links(req);
2891 io_cqring_add_event(req, ret);
2892 fput(req->close.put_file);
2893 io_put_req_find_next(req, nxt);
2894}
2895
Jens Axboeb5dba592019-12-11 14:02:38 -07002896static void io_close_finish(struct io_wq_work **workptr)
2897{
2898 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2899 struct io_kiocb *nxt = NULL;
2900
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002901 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002902 __io_close_finish(req, &nxt);
Jens Axboeb5dba592019-12-11 14:02:38 -07002903 if (nxt)
2904 io_wq_assign_next(workptr, nxt);
2905}
2906
2907static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2908 bool force_nonblock)
2909{
2910 int ret;
2911
2912 req->close.put_file = NULL;
2913 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2914 if (ret < 0)
2915 return ret;
2916
2917 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002918 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002919 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002920
2921 /*
2922 * No ->flush(), safely close from here and just punt the
2923 * fput() to async context.
2924 */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002925 __io_close_finish(req, nxt);
2926 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002927eagain:
2928 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07002929 /*
2930 * Do manual async queue here to avoid grabbing files - we don't
2931 * need the files, and it'll cause io_close_finish() to close
2932 * the file again and cause a double CQE entry for this request
2933 */
2934 io_queue_async_work(req);
2935 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002936}
2937
Jens Axboe3529d8c2019-12-19 18:24:38 -07002938static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002939{
2940 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002941
2942 if (!req->file)
2943 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002944
2945 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2946 return -EINVAL;
2947 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2948 return -EINVAL;
2949
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002950 req->sync.off = READ_ONCE(sqe->off);
2951 req->sync.len = READ_ONCE(sqe->len);
2952 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002953 return 0;
2954}
2955
2956static void io_sync_file_range_finish(struct io_wq_work **workptr)
2957{
2958 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2959 struct io_kiocb *nxt = NULL;
2960 int ret;
2961
2962 if (io_req_cancelled(req))
2963 return;
2964
Jens Axboe9adbd452019-12-20 08:45:55 -07002965 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002966 req->sync.flags);
2967 if (ret < 0)
2968 req_set_fail_links(req);
2969 io_cqring_add_event(req, ret);
2970 io_put_req_find_next(req, &nxt);
2971 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002972 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002973}
2974
Jens Axboefc4df992019-12-10 14:38:45 -07002975static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002976 bool force_nonblock)
2977{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002978 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002979
2980 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002981 if (force_nonblock) {
2982 io_put_req(req);
2983 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002984 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002985 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002986
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002987 work = old_work = &req->work;
2988 io_sync_file_range_finish(&work);
2989 if (work && work != old_work)
2990 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002991 return 0;
2992}
2993
Jens Axboe3529d8c2019-12-19 18:24:38 -07002994static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002995{
Jens Axboe03b12302019-12-02 18:50:25 -07002996#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002997 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002998 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002999 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003000
Jens Axboee47293f2019-12-20 08:58:21 -07003001 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3002 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003003 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003004
Jens Axboed8768362020-02-27 14:17:49 -07003005#ifdef CONFIG_COMPAT
3006 if (req->ctx->compat)
3007 sr->msg_flags |= MSG_CMSG_COMPAT;
3008#endif
3009
Jens Axboefddafac2020-01-04 20:19:44 -07003010 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003011 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003012 /* iovec is already imported */
3013 if (req->flags & REQ_F_NEED_CLEANUP)
3014 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003015
Jens Axboed9688562019-12-09 19:35:20 -07003016 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003017 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003018 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003019 if (!ret)
3020 req->flags |= REQ_F_NEED_CLEANUP;
3021 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003022#else
Jens Axboee47293f2019-12-20 08:58:21 -07003023 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003024#endif
3025}
3026
Jens Axboefc4df992019-12-10 14:38:45 -07003027static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3028 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003029{
3030#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003031 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003032 struct socket *sock;
3033 int ret;
3034
3035 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3036 return -EINVAL;
3037
3038 sock = sock_from_file(req->file, &ret);
3039 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003040 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003041 unsigned flags;
3042
Jens Axboe03b12302019-12-02 18:50:25 -07003043 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003044 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003045 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003046 /* if iov is set, it's allocated already */
3047 if (!kmsg->iov)
3048 kmsg->iov = kmsg->fast_iov;
3049 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003050 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003051 struct io_sr_msg *sr = &req->sr_msg;
3052
Jens Axboe0b416c32019-12-15 10:57:46 -07003053 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003054 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003055
3056 io.msg.iov = io.msg.fast_iov;
3057 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3058 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003059 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003060 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003061 }
3062
Jens Axboee47293f2019-12-20 08:58:21 -07003063 flags = req->sr_msg.msg_flags;
3064 if (flags & MSG_DONTWAIT)
3065 req->flags |= REQ_F_NOWAIT;
3066 else if (force_nonblock)
3067 flags |= MSG_DONTWAIT;
3068
Jens Axboe0b416c32019-12-15 10:57:46 -07003069 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003070 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003071 if (req->io)
3072 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003073 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003074 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003075 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003076 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003077 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003078 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003079 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003080 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003081 }
3082 if (ret == -ERESTARTSYS)
3083 ret = -EINTR;
3084 }
3085
Pavel Begunkov1e950812020-02-06 19:51:16 +03003086 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003087 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003088 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003089 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003090 if (ret < 0)
3091 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003092 io_put_req_find_next(req, nxt);
3093 return 0;
3094#else
3095 return -EOPNOTSUPP;
3096#endif
3097}
3098
Jens Axboefddafac2020-01-04 20:19:44 -07003099static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3100 bool force_nonblock)
3101{
3102#if defined(CONFIG_NET)
3103 struct socket *sock;
3104 int ret;
3105
3106 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3107 return -EINVAL;
3108
3109 sock = sock_from_file(req->file, &ret);
3110 if (sock) {
3111 struct io_sr_msg *sr = &req->sr_msg;
3112 struct msghdr msg;
3113 struct iovec iov;
3114 unsigned flags;
3115
3116 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3117 &msg.msg_iter);
3118 if (ret)
3119 return ret;
3120
3121 msg.msg_name = NULL;
3122 msg.msg_control = NULL;
3123 msg.msg_controllen = 0;
3124 msg.msg_namelen = 0;
3125
3126 flags = req->sr_msg.msg_flags;
3127 if (flags & MSG_DONTWAIT)
3128 req->flags |= REQ_F_NOWAIT;
3129 else if (force_nonblock)
3130 flags |= MSG_DONTWAIT;
3131
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003132 msg.msg_flags = flags;
3133 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003134 if (force_nonblock && ret == -EAGAIN)
3135 return -EAGAIN;
3136 if (ret == -ERESTARTSYS)
3137 ret = -EINTR;
3138 }
3139
3140 io_cqring_add_event(req, ret);
3141 if (ret < 0)
3142 req_set_fail_links(req);
3143 io_put_req_find_next(req, nxt);
3144 return 0;
3145#else
3146 return -EOPNOTSUPP;
3147#endif
3148}
3149
Jens Axboe3529d8c2019-12-19 18:24:38 -07003150static int io_recvmsg_prep(struct io_kiocb *req,
3151 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003152{
3153#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003154 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003155 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003156 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003157
Jens Axboe3529d8c2019-12-19 18:24:38 -07003158 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3159 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003160 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003161
Jens Axboed8768362020-02-27 14:17:49 -07003162#ifdef CONFIG_COMPAT
3163 if (req->ctx->compat)
3164 sr->msg_flags |= MSG_CMSG_COMPAT;
3165#endif
3166
Jens Axboefddafac2020-01-04 20:19:44 -07003167 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003168 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003169 /* iovec is already imported */
3170 if (req->flags & REQ_F_NEED_CLEANUP)
3171 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003172
Jens Axboed9688562019-12-09 19:35:20 -07003173 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003174 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003175 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003176 if (!ret)
3177 req->flags |= REQ_F_NEED_CLEANUP;
3178 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003179#else
Jens Axboee47293f2019-12-20 08:58:21 -07003180 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003181#endif
3182}
3183
Jens Axboefc4df992019-12-10 14:38:45 -07003184static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3185 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003186{
3187#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003188 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003189 struct socket *sock;
3190 int ret;
3191
3192 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3193 return -EINVAL;
3194
3195 sock = sock_from_file(req->file, &ret);
3196 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003197 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003198 unsigned flags;
3199
Jens Axboe03b12302019-12-02 18:50:25 -07003200 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003201 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003202 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003203 /* if iov is set, it's allocated already */
3204 if (!kmsg->iov)
3205 kmsg->iov = kmsg->fast_iov;
3206 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003207 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003208 struct io_sr_msg *sr = &req->sr_msg;
3209
Jens Axboe0b416c32019-12-15 10:57:46 -07003210 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003211 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003212
3213 io.msg.iov = io.msg.fast_iov;
3214 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3215 sr->msg_flags, &io.msg.uaddr,
3216 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003217 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003218 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003219 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003220
Jens Axboee47293f2019-12-20 08:58:21 -07003221 flags = req->sr_msg.msg_flags;
3222 if (flags & MSG_DONTWAIT)
3223 req->flags |= REQ_F_NOWAIT;
3224 else if (force_nonblock)
3225 flags |= MSG_DONTWAIT;
3226
3227 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3228 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003229 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003230 if (req->io)
3231 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003232 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003233 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003234 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003235 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003236 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003237 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003238 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe0b416c32019-12-15 10:57:46 -07003239 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003240 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003241 if (ret == -ERESTARTSYS)
3242 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003243 }
3244
Pavel Begunkov1e950812020-02-06 19:51:16 +03003245 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003246 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003247 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003248 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003249 if (ret < 0)
3250 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003251 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003252 return 0;
3253#else
3254 return -EOPNOTSUPP;
3255#endif
3256}
3257
Jens Axboefddafac2020-01-04 20:19:44 -07003258static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3259 bool force_nonblock)
3260{
3261#if defined(CONFIG_NET)
3262 struct socket *sock;
3263 int ret;
3264
3265 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3266 return -EINVAL;
3267
3268 sock = sock_from_file(req->file, &ret);
3269 if (sock) {
3270 struct io_sr_msg *sr = &req->sr_msg;
3271 struct msghdr msg;
3272 struct iovec iov;
3273 unsigned flags;
3274
3275 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3276 &msg.msg_iter);
3277 if (ret)
3278 return ret;
3279
3280 msg.msg_name = NULL;
3281 msg.msg_control = NULL;
3282 msg.msg_controllen = 0;
3283 msg.msg_namelen = 0;
3284 msg.msg_iocb = NULL;
3285 msg.msg_flags = 0;
3286
3287 flags = req->sr_msg.msg_flags;
3288 if (flags & MSG_DONTWAIT)
3289 req->flags |= REQ_F_NOWAIT;
3290 else if (force_nonblock)
3291 flags |= MSG_DONTWAIT;
3292
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003293 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003294 if (force_nonblock && ret == -EAGAIN)
3295 return -EAGAIN;
3296 if (ret == -ERESTARTSYS)
3297 ret = -EINTR;
3298 }
3299
3300 io_cqring_add_event(req, ret);
3301 if (ret < 0)
3302 req_set_fail_links(req);
3303 io_put_req_find_next(req, nxt);
3304 return 0;
3305#else
3306 return -EOPNOTSUPP;
3307#endif
3308}
3309
3310
Jens Axboe3529d8c2019-12-19 18:24:38 -07003311static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003312{
3313#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003314 struct io_accept *accept = &req->accept;
3315
Jens Axboe17f2fe32019-10-17 14:42:58 -06003316 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3317 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003318 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003319 return -EINVAL;
3320
Jens Axboed55e5f52019-12-11 16:12:15 -07003321 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3322 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003323 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003324 return 0;
3325#else
3326 return -EOPNOTSUPP;
3327#endif
3328}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003329
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003330#if defined(CONFIG_NET)
3331static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3332 bool force_nonblock)
3333{
3334 struct io_accept *accept = &req->accept;
3335 unsigned file_flags;
3336 int ret;
3337
3338 file_flags = force_nonblock ? O_NONBLOCK : 0;
3339 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3340 accept->addr_len, accept->flags);
3341 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003342 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003343 if (ret == -ERESTARTSYS)
3344 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003345 if (ret < 0)
3346 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003347 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003348 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003349 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003350}
3351
3352static void io_accept_finish(struct io_wq_work **workptr)
3353{
3354 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3355 struct io_kiocb *nxt = NULL;
3356
3357 if (io_req_cancelled(req))
3358 return;
3359 __io_accept(req, &nxt, false);
3360 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003361 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003362}
3363#endif
3364
3365static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3366 bool force_nonblock)
3367{
3368#if defined(CONFIG_NET)
3369 int ret;
3370
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003371 ret = __io_accept(req, nxt, force_nonblock);
3372 if (ret == -EAGAIN && force_nonblock) {
3373 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003374 io_put_req(req);
3375 return -EAGAIN;
3376 }
3377 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003378#else
3379 return -EOPNOTSUPP;
3380#endif
3381}
3382
Jens Axboe3529d8c2019-12-19 18:24:38 -07003383static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003384{
3385#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003386 struct io_connect *conn = &req->connect;
3387 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003388
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003389 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3390 return -EINVAL;
3391 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3392 return -EINVAL;
3393
Jens Axboe3529d8c2019-12-19 18:24:38 -07003394 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3395 conn->addr_len = READ_ONCE(sqe->addr2);
3396
3397 if (!io)
3398 return 0;
3399
3400 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003401 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003402#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003403 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003404#endif
3405}
3406
Jens Axboefc4df992019-12-10 14:38:45 -07003407static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3408 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003409{
3410#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003411 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003412 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003413 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003414
Jens Axboef499a022019-12-02 16:28:46 -07003415 if (req->io) {
3416 io = req->io;
3417 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003418 ret = move_addr_to_kernel(req->connect.addr,
3419 req->connect.addr_len,
3420 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003421 if (ret)
3422 goto out;
3423 io = &__io;
3424 }
3425
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003426 file_flags = force_nonblock ? O_NONBLOCK : 0;
3427
3428 ret = __sys_connect_file(req->file, &io->connect.address,
3429 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003430 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003431 if (req->io)
3432 return -EAGAIN;
3433 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003434 ret = -ENOMEM;
3435 goto out;
3436 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003437 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003438 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003439 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003440 if (ret == -ERESTARTSYS)
3441 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003442out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003443 if (ret < 0)
3444 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003445 io_cqring_add_event(req, ret);
3446 io_put_req_find_next(req, nxt);
3447 return 0;
3448#else
3449 return -EOPNOTSUPP;
3450#endif
3451}
3452
Jens Axboe221c5eb2019-01-17 09:41:58 -07003453static void io_poll_remove_one(struct io_kiocb *req)
3454{
3455 struct io_poll_iocb *poll = &req->poll;
3456
3457 spin_lock(&poll->head->lock);
3458 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003459 if (!list_empty(&poll->wait.entry)) {
3460 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003461 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003462 }
3463 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003464 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003465}
3466
3467static void io_poll_remove_all(struct io_ring_ctx *ctx)
3468{
Jens Axboe78076bb2019-12-04 19:56:40 -07003469 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003470 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003471 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003472
3473 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003474 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3475 struct hlist_head *list;
3476
3477 list = &ctx->cancel_hash[i];
3478 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3479 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003480 }
3481 spin_unlock_irq(&ctx->completion_lock);
3482}
3483
Jens Axboe47f46762019-11-09 17:43:02 -07003484static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3485{
Jens Axboe78076bb2019-12-04 19:56:40 -07003486 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003487 struct io_kiocb *req;
3488
Jens Axboe78076bb2019-12-04 19:56:40 -07003489 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3490 hlist_for_each_entry(req, list, hash_node) {
3491 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003492 io_poll_remove_one(req);
3493 return 0;
3494 }
Jens Axboe47f46762019-11-09 17:43:02 -07003495 }
3496
3497 return -ENOENT;
3498}
3499
Jens Axboe3529d8c2019-12-19 18:24:38 -07003500static int io_poll_remove_prep(struct io_kiocb *req,
3501 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003502{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003503 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3504 return -EINVAL;
3505 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3506 sqe->poll_events)
3507 return -EINVAL;
3508
Jens Axboe0969e782019-12-17 18:40:57 -07003509 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003510 return 0;
3511}
3512
3513/*
3514 * Find a running poll command that matches one specified in sqe->addr,
3515 * and remove it if found.
3516 */
3517static int io_poll_remove(struct io_kiocb *req)
3518{
3519 struct io_ring_ctx *ctx = req->ctx;
3520 u64 addr;
3521 int ret;
3522
Jens Axboe0969e782019-12-17 18:40:57 -07003523 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003524 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003525 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003526 spin_unlock_irq(&ctx->completion_lock);
3527
Jens Axboe78e19bb2019-11-06 15:21:34 -07003528 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003529 if (ret < 0)
3530 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003531 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003532 return 0;
3533}
3534
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003535static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003536{
Jackie Liua197f662019-11-08 08:09:12 -07003537 struct io_ring_ctx *ctx = req->ctx;
3538
Jens Axboe8c838782019-03-12 15:48:16 -06003539 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003540 if (error)
3541 io_cqring_fill_event(req, error);
3542 else
3543 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003544 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003545}
3546
Jens Axboe561fb042019-10-24 07:25:42 -06003547static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003548{
Jens Axboe561fb042019-10-24 07:25:42 -06003549 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003550 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3551 struct io_poll_iocb *poll = &req->poll;
3552 struct poll_table_struct pt = { ._key = poll->events };
3553 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003554 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003555 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003556 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003557
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003558 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003559 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003560 ret = -ECANCELED;
3561 } else if (READ_ONCE(poll->canceled)) {
3562 ret = -ECANCELED;
3563 }
Jens Axboe561fb042019-10-24 07:25:42 -06003564
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003565 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003566 mask = vfs_poll(poll->file, &pt) & poll->events;
3567
3568 /*
3569 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3570 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3571 * synchronize with them. In the cancellation case the list_del_init
3572 * itself is not actually needed, but harmless so we keep it in to
3573 * avoid further branches in the fast path.
3574 */
3575 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003576 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003577 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003578 spin_unlock_irq(&ctx->completion_lock);
3579 return;
3580 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003581 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003582 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003583 spin_unlock_irq(&ctx->completion_lock);
3584
Jens Axboe8c838782019-03-12 15:48:16 -06003585 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003586
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003587 if (ret < 0)
3588 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003589 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003590 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003591 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003592}
3593
Jens Axboee94f1412019-12-19 12:06:02 -07003594static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3595{
Jens Axboee94f1412019-12-19 12:06:02 -07003596 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003597 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003598
Jens Axboec6ca97b302019-12-28 12:11:08 -07003599 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003600 spin_lock_irq(&ctx->completion_lock);
3601 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3602 hash_del(&req->hash_node);
3603 io_poll_complete(req, req->result, 0);
3604
Jens Axboe8237e042019-12-28 10:48:22 -07003605 if (refcount_dec_and_test(&req->refs) &&
3606 !io_req_multi_free(&rb, req)) {
3607 req->flags |= REQ_F_COMP_LOCKED;
3608 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003609 }
3610 }
3611 spin_unlock_irq(&ctx->completion_lock);
3612
3613 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003614 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003615}
3616
3617static void io_poll_flush(struct io_wq_work **workptr)
3618{
3619 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3620 struct llist_node *nodes;
3621
3622 nodes = llist_del_all(&req->ctx->poll_llist);
3623 if (nodes)
3624 __io_poll_flush(req->ctx, nodes);
3625}
3626
Jens Axboef0b493e2020-02-01 21:30:11 -07003627static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3628{
3629 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3630
3631 eventfd_signal(req->ctx->cq_ev_fd, 1);
3632 io_put_req(req);
3633}
3634
Jens Axboe221c5eb2019-01-17 09:41:58 -07003635static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3636 void *key)
3637{
Jens Axboee9444752019-11-26 15:02:04 -07003638 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003639 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3640 struct io_ring_ctx *ctx = req->ctx;
3641 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003642
3643 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003644 if (mask && !(mask & poll->events))
3645 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003646
Jens Axboe392edb42019-12-09 17:52:20 -07003647 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003648
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003649 /*
3650 * Run completion inline if we can. We're using trylock here because
3651 * we are violating the completion_lock -> poll wq lock ordering.
3652 * If we have a link timeout we're going to need the completion_lock
3653 * for finalizing the request, mark us as having grabbed that already.
3654 */
Jens Axboee94f1412019-12-19 12:06:02 -07003655 if (mask) {
3656 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003657
Jens Axboee94f1412019-12-19 12:06:02 -07003658 if (llist_empty(&ctx->poll_llist) &&
3659 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003660 bool trigger_ev;
3661
Jens Axboee94f1412019-12-19 12:06:02 -07003662 hash_del(&req->hash_node);
3663 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003664
Jens Axboef0b493e2020-02-01 21:30:11 -07003665 trigger_ev = io_should_trigger_evfd(ctx);
3666 if (trigger_ev && eventfd_signal_count()) {
3667 trigger_ev = false;
3668 req->work.func = io_poll_trigger_evfd;
3669 } else {
3670 req->flags |= REQ_F_COMP_LOCKED;
3671 io_put_req(req);
3672 req = NULL;
3673 }
3674 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3675 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003676 } else {
3677 req->result = mask;
3678 req->llist_node.next = NULL;
3679 /* if the list wasn't empty, we're done */
3680 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3681 req = NULL;
3682 else
3683 req->work.func = io_poll_flush;
3684 }
Jens Axboe8c838782019-03-12 15:48:16 -06003685 }
Jens Axboee94f1412019-12-19 12:06:02 -07003686 if (req)
3687 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003688
Jens Axboe221c5eb2019-01-17 09:41:58 -07003689 return 1;
3690}
3691
3692struct io_poll_table {
3693 struct poll_table_struct pt;
3694 struct io_kiocb *req;
3695 int error;
3696};
3697
3698static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3699 struct poll_table_struct *p)
3700{
3701 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3702
3703 if (unlikely(pt->req->poll.head)) {
3704 pt->error = -EINVAL;
3705 return;
3706 }
3707
3708 pt->error = 0;
3709 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003710 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003711}
3712
Jens Axboeeac406c2019-11-14 12:09:58 -07003713static void io_poll_req_insert(struct io_kiocb *req)
3714{
3715 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003716 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003717
Jens Axboe78076bb2019-12-04 19:56:40 -07003718 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3719 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003720}
3721
Jens Axboe3529d8c2019-12-19 18:24:38 -07003722static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003723{
3724 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003725 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003726
3727 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3728 return -EINVAL;
3729 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3730 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003731 if (!poll->file)
3732 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003733
Jens Axboe221c5eb2019-01-17 09:41:58 -07003734 events = READ_ONCE(sqe->poll_events);
3735 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003736 return 0;
3737}
3738
3739static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3740{
3741 struct io_poll_iocb *poll = &req->poll;
3742 struct io_ring_ctx *ctx = req->ctx;
3743 struct io_poll_table ipt;
3744 bool cancel = false;
3745 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003746
3747 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003748 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003749
Jens Axboe221c5eb2019-01-17 09:41:58 -07003750 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003751 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003752 poll->canceled = false;
3753
3754 ipt.pt._qproc = io_poll_queue_proc;
3755 ipt.pt._key = poll->events;
3756 ipt.req = req;
3757 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3758
3759 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003760 INIT_LIST_HEAD(&poll->wait.entry);
3761 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3762 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003763
Jens Axboe36703242019-07-25 10:20:18 -06003764 INIT_LIST_HEAD(&req->list);
3765
Jens Axboe221c5eb2019-01-17 09:41:58 -07003766 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003767
3768 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003769 if (likely(poll->head)) {
3770 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003771 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003772 if (ipt.error)
3773 cancel = true;
3774 ipt.error = 0;
3775 mask = 0;
3776 }
3777 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003778 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003779 else if (cancel)
3780 WRITE_ONCE(poll->canceled, true);
3781 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003782 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003783 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003784 }
Jens Axboe8c838782019-03-12 15:48:16 -06003785 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003786 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003787 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003788 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003789 spin_unlock_irq(&ctx->completion_lock);
3790
Jens Axboe8c838782019-03-12 15:48:16 -06003791 if (mask) {
3792 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003793 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003794 }
Jens Axboe8c838782019-03-12 15:48:16 -06003795 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003796}
3797
Jens Axboe5262f562019-09-17 12:26:57 -06003798static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3799{
Jens Axboead8a48a2019-11-15 08:49:11 -07003800 struct io_timeout_data *data = container_of(timer,
3801 struct io_timeout_data, timer);
3802 struct io_kiocb *req = data->req;
3803 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003804 unsigned long flags;
3805
Jens Axboe5262f562019-09-17 12:26:57 -06003806 atomic_inc(&ctx->cq_timeouts);
3807
3808 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003809 /*
Jens Axboe11365042019-10-16 09:08:32 -06003810 * We could be racing with timeout deletion. If the list is empty,
3811 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003812 */
Jens Axboe842f9612019-10-29 12:34:10 -06003813 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003814 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003815
Jens Axboe11365042019-10-16 09:08:32 -06003816 /*
3817 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003818 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003819 * pointer will be increased, otherwise other timeout reqs may
3820 * return in advance without waiting for enough wait_nr.
3821 */
3822 prev = req;
3823 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3824 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003825 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003826 }
Jens Axboe842f9612019-10-29 12:34:10 -06003827
Jens Axboe78e19bb2019-11-06 15:21:34 -07003828 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003829 io_commit_cqring(ctx);
3830 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3831
3832 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003833 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003834 io_put_req(req);
3835 return HRTIMER_NORESTART;
3836}
3837
Jens Axboe47f46762019-11-09 17:43:02 -07003838static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3839{
3840 struct io_kiocb *req;
3841 int ret = -ENOENT;
3842
3843 list_for_each_entry(req, &ctx->timeout_list, list) {
3844 if (user_data == req->user_data) {
3845 list_del_init(&req->list);
3846 ret = 0;
3847 break;
3848 }
3849 }
3850
3851 if (ret == -ENOENT)
3852 return ret;
3853
Jens Axboe2d283902019-12-04 11:08:05 -07003854 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003855 if (ret == -1)
3856 return -EALREADY;
3857
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003858 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003859 io_cqring_fill_event(req, -ECANCELED);
3860 io_put_req(req);
3861 return 0;
3862}
3863
Jens Axboe3529d8c2019-12-19 18:24:38 -07003864static int io_timeout_remove_prep(struct io_kiocb *req,
3865 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003866{
Jens Axboeb29472e2019-12-17 18:50:29 -07003867 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3868 return -EINVAL;
3869 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3870 return -EINVAL;
3871
3872 req->timeout.addr = READ_ONCE(sqe->addr);
3873 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3874 if (req->timeout.flags)
3875 return -EINVAL;
3876
Jens Axboeb29472e2019-12-17 18:50:29 -07003877 return 0;
3878}
3879
Jens Axboe11365042019-10-16 09:08:32 -06003880/*
3881 * Remove or update an existing timeout command
3882 */
Jens Axboefc4df992019-12-10 14:38:45 -07003883static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003884{
3885 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003886 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003887
Jens Axboe11365042019-10-16 09:08:32 -06003888 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003889 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003890
Jens Axboe47f46762019-11-09 17:43:02 -07003891 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003892 io_commit_cqring(ctx);
3893 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003894 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003895 if (ret < 0)
3896 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003897 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003898 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003899}
3900
Jens Axboe3529d8c2019-12-19 18:24:38 -07003901static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003902 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003903{
Jens Axboead8a48a2019-11-15 08:49:11 -07003904 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003905 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003906
Jens Axboead8a48a2019-11-15 08:49:11 -07003907 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003908 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003909 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003910 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003911 if (sqe->off && is_timeout_link)
3912 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003913 flags = READ_ONCE(sqe->timeout_flags);
3914 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003915 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003916
Jens Axboe26a61672019-12-20 09:02:01 -07003917 req->timeout.count = READ_ONCE(sqe->off);
3918
Jens Axboe3529d8c2019-12-19 18:24:38 -07003919 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003920 return -ENOMEM;
3921
3922 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003923 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003924 req->flags |= REQ_F_TIMEOUT;
3925
3926 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003927 return -EFAULT;
3928
Jens Axboe11365042019-10-16 09:08:32 -06003929 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003930 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003931 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003932 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003933
Jens Axboead8a48a2019-11-15 08:49:11 -07003934 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3935 return 0;
3936}
3937
Jens Axboefc4df992019-12-10 14:38:45 -07003938static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003939{
3940 unsigned count;
3941 struct io_ring_ctx *ctx = req->ctx;
3942 struct io_timeout_data *data;
3943 struct list_head *entry;
3944 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003945
Jens Axboe2d283902019-12-04 11:08:05 -07003946 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003947
Jens Axboe5262f562019-09-17 12:26:57 -06003948 /*
3949 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003950 * timeout event to be satisfied. If it isn't set, then this is
3951 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003952 */
Jens Axboe26a61672019-12-20 09:02:01 -07003953 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003954 if (!count) {
3955 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3956 spin_lock_irq(&ctx->completion_lock);
3957 entry = ctx->timeout_list.prev;
3958 goto add;
3959 }
Jens Axboe5262f562019-09-17 12:26:57 -06003960
3961 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003962 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003963
3964 /*
3965 * Insertion sort, ensuring the first entry in the list is always
3966 * the one we need first.
3967 */
Jens Axboe5262f562019-09-17 12:26:57 -06003968 spin_lock_irq(&ctx->completion_lock);
3969 list_for_each_prev(entry, &ctx->timeout_list) {
3970 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003971 unsigned nxt_sq_head;
3972 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003973 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003974
Jens Axboe93bd25b2019-11-11 23:34:31 -07003975 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3976 continue;
3977
yangerkun5da0fb12019-10-15 21:59:29 +08003978 /*
3979 * Since cached_sq_head + count - 1 can overflow, use type long
3980 * long to store it.
3981 */
3982 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003983 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3984 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003985
3986 /*
3987 * cached_sq_head may overflow, and it will never overflow twice
3988 * once there is some timeout req still be valid.
3989 */
3990 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003991 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003992
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003993 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003994 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003995
3996 /*
3997 * Sequence of reqs after the insert one and itself should
3998 * be adjusted because each timeout req consumes a slot.
3999 */
4000 span++;
4001 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004002 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004003 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004004add:
Jens Axboe5262f562019-09-17 12:26:57 -06004005 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004006 data->timer.function = io_timeout_fn;
4007 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004008 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004009 return 0;
4010}
4011
Jens Axboe62755e32019-10-28 21:49:21 -06004012static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004013{
Jens Axboe62755e32019-10-28 21:49:21 -06004014 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004015
Jens Axboe62755e32019-10-28 21:49:21 -06004016 return req->user_data == (unsigned long) data;
4017}
4018
Jens Axboee977d6d2019-11-05 12:39:45 -07004019static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004020{
Jens Axboe62755e32019-10-28 21:49:21 -06004021 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004022 int ret = 0;
4023
Jens Axboe62755e32019-10-28 21:49:21 -06004024 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4025 switch (cancel_ret) {
4026 case IO_WQ_CANCEL_OK:
4027 ret = 0;
4028 break;
4029 case IO_WQ_CANCEL_RUNNING:
4030 ret = -EALREADY;
4031 break;
4032 case IO_WQ_CANCEL_NOTFOUND:
4033 ret = -ENOENT;
4034 break;
4035 }
4036
Jens Axboee977d6d2019-11-05 12:39:45 -07004037 return ret;
4038}
4039
Jens Axboe47f46762019-11-09 17:43:02 -07004040static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4041 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004042 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004043{
4044 unsigned long flags;
4045 int ret;
4046
4047 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4048 if (ret != -ENOENT) {
4049 spin_lock_irqsave(&ctx->completion_lock, flags);
4050 goto done;
4051 }
4052
4053 spin_lock_irqsave(&ctx->completion_lock, flags);
4054 ret = io_timeout_cancel(ctx, sqe_addr);
4055 if (ret != -ENOENT)
4056 goto done;
4057 ret = io_poll_cancel(ctx, sqe_addr);
4058done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004059 if (!ret)
4060 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004061 io_cqring_fill_event(req, ret);
4062 io_commit_cqring(ctx);
4063 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4064 io_cqring_ev_posted(ctx);
4065
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004066 if (ret < 0)
4067 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004068 io_put_req_find_next(req, nxt);
4069}
4070
Jens Axboe3529d8c2019-12-19 18:24:38 -07004071static int io_async_cancel_prep(struct io_kiocb *req,
4072 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004073{
Jens Axboefbf23842019-12-17 18:45:56 -07004074 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004075 return -EINVAL;
4076 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4077 sqe->cancel_flags)
4078 return -EINVAL;
4079
Jens Axboefbf23842019-12-17 18:45:56 -07004080 req->cancel.addr = READ_ONCE(sqe->addr);
4081 return 0;
4082}
4083
4084static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4085{
4086 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004087
4088 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004089 return 0;
4090}
4091
Jens Axboe05f3fb32019-12-09 11:22:50 -07004092static int io_files_update_prep(struct io_kiocb *req,
4093 const struct io_uring_sqe *sqe)
4094{
4095 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4096 return -EINVAL;
4097
4098 req->files_update.offset = READ_ONCE(sqe->off);
4099 req->files_update.nr_args = READ_ONCE(sqe->len);
4100 if (!req->files_update.nr_args)
4101 return -EINVAL;
4102 req->files_update.arg = READ_ONCE(sqe->addr);
4103 return 0;
4104}
4105
4106static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4107{
4108 struct io_ring_ctx *ctx = req->ctx;
4109 struct io_uring_files_update up;
4110 int ret;
4111
Jens Axboef86cd202020-01-29 13:46:44 -07004112 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004113 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004114
4115 up.offset = req->files_update.offset;
4116 up.fds = req->files_update.arg;
4117
4118 mutex_lock(&ctx->uring_lock);
4119 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4120 mutex_unlock(&ctx->uring_lock);
4121
4122 if (ret < 0)
4123 req_set_fail_links(req);
4124 io_cqring_add_event(req, ret);
4125 io_put_req(req);
4126 return 0;
4127}
4128
Jens Axboe3529d8c2019-12-19 18:24:38 -07004129static int io_req_defer_prep(struct io_kiocb *req,
4130 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004131{
Jens Axboee7815732019-12-17 19:45:06 -07004132 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004133
Jens Axboef86cd202020-01-29 13:46:44 -07004134 if (io_op_defs[req->opcode].file_table) {
4135 ret = io_grab_files(req);
4136 if (unlikely(ret))
4137 return ret;
4138 }
4139
Jens Axboecccf0ee2020-01-27 16:34:48 -07004140 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4141
Jens Axboed625c6e2019-12-17 19:53:05 -07004142 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004143 case IORING_OP_NOP:
4144 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004145 case IORING_OP_READV:
4146 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004147 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004148 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004149 break;
4150 case IORING_OP_WRITEV:
4151 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004152 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004153 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004154 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004155 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004156 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004157 break;
4158 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004159 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004160 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004161 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004162 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004163 break;
4164 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004165 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004166 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004167 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004168 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004169 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004170 break;
4171 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004172 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004173 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004174 break;
Jens Axboef499a022019-12-02 16:28:46 -07004175 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004176 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004177 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004178 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004179 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004180 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004181 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004182 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004183 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004184 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004185 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004186 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004187 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004188 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004189 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004190 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004191 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004192 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004193 case IORING_OP_FALLOCATE:
4194 ret = io_fallocate_prep(req, sqe);
4195 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004196 case IORING_OP_OPENAT:
4197 ret = io_openat_prep(req, sqe);
4198 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004199 case IORING_OP_CLOSE:
4200 ret = io_close_prep(req, sqe);
4201 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004202 case IORING_OP_FILES_UPDATE:
4203 ret = io_files_update_prep(req, sqe);
4204 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004205 case IORING_OP_STATX:
4206 ret = io_statx_prep(req, sqe);
4207 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004208 case IORING_OP_FADVISE:
4209 ret = io_fadvise_prep(req, sqe);
4210 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004211 case IORING_OP_MADVISE:
4212 ret = io_madvise_prep(req, sqe);
4213 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004214 case IORING_OP_OPENAT2:
4215 ret = io_openat2_prep(req, sqe);
4216 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004217 case IORING_OP_EPOLL_CTL:
4218 ret = io_epoll_ctl_prep(req, sqe);
4219 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004220 default:
Jens Axboee7815732019-12-17 19:45:06 -07004221 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4222 req->opcode);
4223 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004224 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004225 }
4226
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004227 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004228}
4229
Jens Axboe3529d8c2019-12-19 18:24:38 -07004230static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004231{
Jackie Liua197f662019-11-08 08:09:12 -07004232 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004233 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004234
Bob Liu9d858b22019-11-13 18:06:25 +08004235 /* Still need defer if there is pending req in defer list. */
4236 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004237 return 0;
4238
Jens Axboe3529d8c2019-12-19 18:24:38 -07004239 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004240 return -EAGAIN;
4241
Jens Axboe3529d8c2019-12-19 18:24:38 -07004242 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004243 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004244 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004245
Jens Axboede0617e2019-04-06 21:51:27 -06004246 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004247 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004248 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004249 return 0;
4250 }
4251
Jens Axboe915967f2019-11-21 09:01:20 -07004252 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004253 list_add_tail(&req->list, &ctx->defer_list);
4254 spin_unlock_irq(&ctx->completion_lock);
4255 return -EIOCBQUEUED;
4256}
4257
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004258static void io_cleanup_req(struct io_kiocb *req)
4259{
4260 struct io_async_ctx *io = req->io;
4261
4262 switch (req->opcode) {
4263 case IORING_OP_READV:
4264 case IORING_OP_READ_FIXED:
4265 case IORING_OP_READ:
4266 case IORING_OP_WRITEV:
4267 case IORING_OP_WRITE_FIXED:
4268 case IORING_OP_WRITE:
4269 if (io->rw.iov != io->rw.fast_iov)
4270 kfree(io->rw.iov);
4271 break;
4272 case IORING_OP_SENDMSG:
4273 case IORING_OP_RECVMSG:
4274 if (io->msg.iov != io->msg.fast_iov)
4275 kfree(io->msg.iov);
4276 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004277 case IORING_OP_OPENAT:
4278 case IORING_OP_OPENAT2:
4279 case IORING_OP_STATX:
4280 putname(req->open.filename);
4281 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004282 }
4283
4284 req->flags &= ~REQ_F_NEED_CLEANUP;
4285}
4286
Jens Axboe3529d8c2019-12-19 18:24:38 -07004287static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4288 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004289{
Jackie Liua197f662019-11-08 08:09:12 -07004290 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004291 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004292
Jens Axboed625c6e2019-12-17 19:53:05 -07004293 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004294 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004295 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004296 break;
4297 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004298 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004299 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004300 if (sqe) {
4301 ret = io_read_prep(req, sqe, force_nonblock);
4302 if (ret < 0)
4303 break;
4304 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004305 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004306 break;
4307 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004308 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004309 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004310 if (sqe) {
4311 ret = io_write_prep(req, sqe, force_nonblock);
4312 if (ret < 0)
4313 break;
4314 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004315 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004316 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004317 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004318 if (sqe) {
4319 ret = io_prep_fsync(req, sqe);
4320 if (ret < 0)
4321 break;
4322 }
Jens Axboefc4df992019-12-10 14:38:45 -07004323 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004324 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004325 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004326 if (sqe) {
4327 ret = io_poll_add_prep(req, sqe);
4328 if (ret)
4329 break;
4330 }
Jens Axboefc4df992019-12-10 14:38:45 -07004331 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004332 break;
4333 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004334 if (sqe) {
4335 ret = io_poll_remove_prep(req, sqe);
4336 if (ret < 0)
4337 break;
4338 }
Jens Axboefc4df992019-12-10 14:38:45 -07004339 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004340 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004341 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004342 if (sqe) {
4343 ret = io_prep_sfr(req, sqe);
4344 if (ret < 0)
4345 break;
4346 }
Jens Axboefc4df992019-12-10 14:38:45 -07004347 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004348 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004349 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004350 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004351 if (sqe) {
4352 ret = io_sendmsg_prep(req, sqe);
4353 if (ret < 0)
4354 break;
4355 }
Jens Axboefddafac2020-01-04 20:19:44 -07004356 if (req->opcode == IORING_OP_SENDMSG)
4357 ret = io_sendmsg(req, nxt, force_nonblock);
4358 else
4359 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004360 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004361 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004362 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004363 if (sqe) {
4364 ret = io_recvmsg_prep(req, sqe);
4365 if (ret)
4366 break;
4367 }
Jens Axboefddafac2020-01-04 20:19:44 -07004368 if (req->opcode == IORING_OP_RECVMSG)
4369 ret = io_recvmsg(req, nxt, force_nonblock);
4370 else
4371 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004372 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004373 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004374 if (sqe) {
4375 ret = io_timeout_prep(req, sqe, false);
4376 if (ret)
4377 break;
4378 }
Jens Axboefc4df992019-12-10 14:38:45 -07004379 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004380 break;
Jens Axboe11365042019-10-16 09:08:32 -06004381 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004382 if (sqe) {
4383 ret = io_timeout_remove_prep(req, sqe);
4384 if (ret)
4385 break;
4386 }
Jens Axboefc4df992019-12-10 14:38:45 -07004387 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004388 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004389 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004390 if (sqe) {
4391 ret = io_accept_prep(req, sqe);
4392 if (ret)
4393 break;
4394 }
Jens Axboefc4df992019-12-10 14:38:45 -07004395 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004396 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004397 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004398 if (sqe) {
4399 ret = io_connect_prep(req, sqe);
4400 if (ret)
4401 break;
4402 }
Jens Axboefc4df992019-12-10 14:38:45 -07004403 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004404 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004405 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004406 if (sqe) {
4407 ret = io_async_cancel_prep(req, sqe);
4408 if (ret)
4409 break;
4410 }
Jens Axboefc4df992019-12-10 14:38:45 -07004411 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004412 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004413 case IORING_OP_FALLOCATE:
4414 if (sqe) {
4415 ret = io_fallocate_prep(req, sqe);
4416 if (ret)
4417 break;
4418 }
4419 ret = io_fallocate(req, nxt, force_nonblock);
4420 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004421 case IORING_OP_OPENAT:
4422 if (sqe) {
4423 ret = io_openat_prep(req, sqe);
4424 if (ret)
4425 break;
4426 }
4427 ret = io_openat(req, nxt, force_nonblock);
4428 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004429 case IORING_OP_CLOSE:
4430 if (sqe) {
4431 ret = io_close_prep(req, sqe);
4432 if (ret)
4433 break;
4434 }
4435 ret = io_close(req, nxt, force_nonblock);
4436 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004437 case IORING_OP_FILES_UPDATE:
4438 if (sqe) {
4439 ret = io_files_update_prep(req, sqe);
4440 if (ret)
4441 break;
4442 }
4443 ret = io_files_update(req, force_nonblock);
4444 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004445 case IORING_OP_STATX:
4446 if (sqe) {
4447 ret = io_statx_prep(req, sqe);
4448 if (ret)
4449 break;
4450 }
4451 ret = io_statx(req, nxt, force_nonblock);
4452 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004453 case IORING_OP_FADVISE:
4454 if (sqe) {
4455 ret = io_fadvise_prep(req, sqe);
4456 if (ret)
4457 break;
4458 }
4459 ret = io_fadvise(req, nxt, force_nonblock);
4460 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004461 case IORING_OP_MADVISE:
4462 if (sqe) {
4463 ret = io_madvise_prep(req, sqe);
4464 if (ret)
4465 break;
4466 }
4467 ret = io_madvise(req, nxt, force_nonblock);
4468 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004469 case IORING_OP_OPENAT2:
4470 if (sqe) {
4471 ret = io_openat2_prep(req, sqe);
4472 if (ret)
4473 break;
4474 }
4475 ret = io_openat2(req, nxt, force_nonblock);
4476 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004477 case IORING_OP_EPOLL_CTL:
4478 if (sqe) {
4479 ret = io_epoll_ctl_prep(req, sqe);
4480 if (ret)
4481 break;
4482 }
4483 ret = io_epoll_ctl(req, nxt, force_nonblock);
4484 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004485 default:
4486 ret = -EINVAL;
4487 break;
4488 }
4489
Jens Axboedef596e2019-01-09 08:59:42 -07004490 if (ret)
4491 return ret;
4492
4493 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004494 const bool in_async = io_wq_current_is_worker();
4495
Jens Axboe9e645e112019-05-10 16:07:28 -06004496 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004497 return -EAGAIN;
4498
Jens Axboe11ba8202020-01-15 21:51:17 -07004499 /* workqueue context doesn't hold uring_lock, grab it now */
4500 if (in_async)
4501 mutex_lock(&ctx->uring_lock);
4502
Jens Axboedef596e2019-01-09 08:59:42 -07004503 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004504
4505 if (in_async)
4506 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004507 }
4508
4509 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004510}
4511
Jens Axboe561fb042019-10-24 07:25:42 -06004512static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004513{
Jens Axboe561fb042019-10-24 07:25:42 -06004514 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004515 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004516 struct io_kiocb *nxt = NULL;
4517 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004518
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004519 /* if NO_CANCEL is set, we must still run the work */
4520 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4521 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004522 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004523 }
Jens Axboe31b51512019-01-18 22:56:34 -07004524
Jens Axboe561fb042019-10-24 07:25:42 -06004525 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004526 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004527 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004528 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004529 /*
4530 * We can get EAGAIN for polled IO even though we're
4531 * forcing a sync submission from here, since we can't
4532 * wait for request slots on the block side.
4533 */
4534 if (ret != -EAGAIN)
4535 break;
4536 cond_resched();
4537 } while (1);
4538 }
Jens Axboe31b51512019-01-18 22:56:34 -07004539
Jens Axboe561fb042019-10-24 07:25:42 -06004540 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004541 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004542
Jens Axboe561fb042019-10-24 07:25:42 -06004543 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004544 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004545 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004546 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004547 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004548
Jens Axboe561fb042019-10-24 07:25:42 -06004549 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004550 if (!ret && nxt)
4551 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004552}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004553
Jens Axboe15b71ab2019-12-11 11:20:36 -07004554static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004555{
Jens Axboed3656342019-12-18 09:50:26 -07004556 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004557 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004558 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004559 return 0;
4560 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004561}
4562
Jens Axboe65e19f52019-10-26 07:20:21 -06004563static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4564 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004565{
Jens Axboe65e19f52019-10-26 07:20:21 -06004566 struct fixed_file_table *table;
4567
Jens Axboe05f3fb32019-12-09 11:22:50 -07004568 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4569 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004570}
4571
Jens Axboe3529d8c2019-12-19 18:24:38 -07004572static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4573 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004574{
Jackie Liua197f662019-11-08 08:09:12 -07004575 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004576 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004577 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004578
Jens Axboe3529d8c2019-12-19 18:24:38 -07004579 flags = READ_ONCE(sqe->flags);
4580 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004581
Jens Axboed3656342019-12-18 09:50:26 -07004582 if (!io_req_needs_file(req, fd))
4583 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004584
4585 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004586 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004587 (unsigned) fd >= ctx->nr_user_files))
4588 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004589 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004590 req->file = io_file_from_index(ctx, fd);
4591 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004592 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004593 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004594 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004595 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004596 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004597 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004598 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004599 req->file = io_file_get(state, fd);
4600 if (unlikely(!req->file))
4601 return -EBADF;
4602 }
4603
4604 return 0;
4605}
4606
Jackie Liua197f662019-11-08 08:09:12 -07004607static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004608{
Jens Axboefcb323c2019-10-24 12:39:47 -06004609 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004610 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004611
Jens Axboef86cd202020-01-29 13:46:44 -07004612 if (req->work.files)
4613 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004614 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004615 return -EBADF;
4616
Jens Axboefcb323c2019-10-24 12:39:47 -06004617 rcu_read_lock();
4618 spin_lock_irq(&ctx->inflight_lock);
4619 /*
4620 * We use the f_ops->flush() handler to ensure that we can flush
4621 * out work accessing these files if the fd is closed. Check if
4622 * the fd has changed since we started down this path, and disallow
4623 * this operation if it has.
4624 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004625 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004626 list_add(&req->inflight_entry, &ctx->inflight_list);
4627 req->flags |= REQ_F_INFLIGHT;
4628 req->work.files = current->files;
4629 ret = 0;
4630 }
4631 spin_unlock_irq(&ctx->inflight_lock);
4632 rcu_read_unlock();
4633
4634 return ret;
4635}
4636
Jens Axboe2665abf2019-11-05 12:40:47 -07004637static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4638{
Jens Axboead8a48a2019-11-15 08:49:11 -07004639 struct io_timeout_data *data = container_of(timer,
4640 struct io_timeout_data, timer);
4641 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004642 struct io_ring_ctx *ctx = req->ctx;
4643 struct io_kiocb *prev = NULL;
4644 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004645
4646 spin_lock_irqsave(&ctx->completion_lock, flags);
4647
4648 /*
4649 * We don't expect the list to be empty, that will only happen if we
4650 * race with the completion of the linked work.
4651 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004652 if (!list_empty(&req->link_list)) {
4653 prev = list_entry(req->link_list.prev, struct io_kiocb,
4654 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004655 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004656 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004657 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4658 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004659 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004660 }
4661
4662 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4663
4664 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004665 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004666 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4667 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004668 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004669 } else {
4670 io_cqring_add_event(req, -ETIME);
4671 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004672 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004673 return HRTIMER_NORESTART;
4674}
4675
Jens Axboead8a48a2019-11-15 08:49:11 -07004676static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004677{
Jens Axboe76a46e02019-11-10 23:34:16 -07004678 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004679
Jens Axboe76a46e02019-11-10 23:34:16 -07004680 /*
4681 * If the list is now empty, then our linked request finished before
4682 * we got a chance to setup the timer
4683 */
4684 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004685 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004686 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004687
Jens Axboead8a48a2019-11-15 08:49:11 -07004688 data->timer.function = io_link_timeout_fn;
4689 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4690 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004691 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004692 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004693
Jens Axboe2665abf2019-11-05 12:40:47 -07004694 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004695 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004696}
4697
Jens Axboead8a48a2019-11-15 08:49:11 -07004698static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004699{
4700 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004701
Jens Axboe2665abf2019-11-05 12:40:47 -07004702 if (!(req->flags & REQ_F_LINK))
4703 return NULL;
4704
Pavel Begunkov44932332019-12-05 16:16:35 +03004705 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4706 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004707 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004708 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004709
Jens Axboe76a46e02019-11-10 23:34:16 -07004710 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004711 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004712}
4713
Jens Axboe3529d8c2019-12-19 18:24:38 -07004714static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004715{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004716 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004717 struct io_kiocb *nxt = NULL;
Jens Axboe193155c2020-02-22 23:22:19 -07004718 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004719 int ret;
4720
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004721again:
4722 linked_timeout = io_prep_linked_timeout(req);
4723
Jens Axboe193155c2020-02-22 23:22:19 -07004724 if (req->work.creds && req->work.creds != current_cred()) {
4725 if (old_creds)
4726 revert_creds(old_creds);
4727 if (old_creds == req->work.creds)
4728 old_creds = NULL; /* restored original creds */
4729 else
4730 old_creds = override_creds(req->work.creds);
4731 }
4732
Jens Axboe3529d8c2019-12-19 18:24:38 -07004733 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004734
4735 /*
4736 * We async punt it if the file wasn't marked NOWAIT, or if the file
4737 * doesn't support non-blocking read/write attempts
4738 */
4739 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4740 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004741punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004742 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004743 ret = io_grab_files(req);
4744 if (ret)
4745 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004746 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004747
4748 /*
4749 * Queued up for async execution, worker will release
4750 * submit reference when the iocb is actually submitted.
4751 */
4752 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004753 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004754 }
Jens Axboee65ef562019-03-12 10:16:44 -06004755
Jens Axboefcb323c2019-10-24 12:39:47 -06004756err:
Jens Axboee65ef562019-03-12 10:16:44 -06004757 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004758 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004759
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004760 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004761 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004762 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004763 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004764 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004765 }
4766
Jens Axboee65ef562019-03-12 10:16:44 -06004767 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004768 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004769 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004770 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004771 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004772 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004773done_req:
4774 if (nxt) {
4775 req = nxt;
4776 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004777
4778 if (req->flags & REQ_F_FORCE_ASYNC)
4779 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004780 goto again;
4781 }
Jens Axboe193155c2020-02-22 23:22:19 -07004782 if (old_creds)
4783 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004784}
4785
Jens Axboe3529d8c2019-12-19 18:24:38 -07004786static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004787{
4788 int ret;
4789
Jens Axboe3529d8c2019-12-19 18:24:38 -07004790 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004791 if (ret) {
4792 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004793fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004794 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004795 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004796 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004797 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004798 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004799 ret = io_req_defer_prep(req, sqe);
4800 if (unlikely(ret < 0))
4801 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004802 /*
4803 * Never try inline submit of IOSQE_ASYNC is set, go straight
4804 * to async execution.
4805 */
4806 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4807 io_queue_async_work(req);
4808 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004809 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004810 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004811}
4812
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004813static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004814{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004815 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004816 io_cqring_add_event(req, -ECANCELED);
4817 io_double_put_req(req);
4818 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004819 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004820}
4821
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004822#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004823 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004824
Jens Axboe3529d8c2019-12-19 18:24:38 -07004825static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4826 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004827{
Jackie Liua197f662019-11-08 08:09:12 -07004828 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004829 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004830 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004831
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004832 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004833
4834 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004835 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004836 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004837 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004838 }
4839
Jens Axboe75c6a032020-01-28 10:15:23 -07004840 id = READ_ONCE(sqe->personality);
4841 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07004842 req->work.creds = idr_find(&ctx->personality_idr, id);
4843 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07004844 ret = -EINVAL;
4845 goto err_req;
4846 }
Jens Axboe193155c2020-02-22 23:22:19 -07004847 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07004848 }
4849
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004850 /* same numerical values with corresponding REQ_F_*, safe to copy */
4851 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4852 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004853
Jens Axboe3529d8c2019-12-19 18:24:38 -07004854 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004855 if (unlikely(ret)) {
4856err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004857 io_cqring_add_event(req, ret);
4858 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004859 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004860 }
4861
Jens Axboe9e645e112019-05-10 16:07:28 -06004862 /*
4863 * If we already have a head request, queue this one for async
4864 * submittal once the head completes. If we don't have a head but
4865 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4866 * submitted sync once the chain is complete. If none of those
4867 * conditions are true (normal request), then just queue it.
4868 */
4869 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004870 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004871
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004872 /*
4873 * Taking sequential execution of a link, draining both sides
4874 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4875 * requests in the link. So, it drains the head and the
4876 * next after the link request. The last one is done via
4877 * drain_next flag to persist the effect across calls.
4878 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004879 if (sqe_flags & IOSQE_IO_DRAIN) {
4880 head->flags |= REQ_F_IO_DRAIN;
4881 ctx->drain_next = 1;
4882 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004883 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004884 ret = -EAGAIN;
4885 goto err_req;
4886 }
4887
Jens Axboe3529d8c2019-12-19 18:24:38 -07004888 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004889 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004890 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004891 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004892 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004893 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004894 trace_io_uring_link(ctx, req, head);
4895 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004896
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004897 /* last request of a link, enqueue the link */
4898 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4899 io_queue_link_head(head);
4900 *link = NULL;
4901 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004902 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004903 if (unlikely(ctx->drain_next)) {
4904 req->flags |= REQ_F_IO_DRAIN;
4905 req->ctx->drain_next = 0;
4906 }
4907 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4908 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004909 INIT_LIST_HEAD(&req->link_list);
4910 ret = io_req_defer_prep(req, sqe);
4911 if (ret)
4912 req->flags |= REQ_F_FAIL_LINK;
4913 *link = req;
4914 } else {
4915 io_queue_sqe(req, sqe);
4916 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004917 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004918
4919 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004920}
4921
Jens Axboe9a56a232019-01-09 09:06:50 -07004922/*
4923 * Batched submission is done, ensure local IO is flushed out.
4924 */
4925static void io_submit_state_end(struct io_submit_state *state)
4926{
4927 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004928 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004929 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004930 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004931}
4932
4933/*
4934 * Start submission side cache.
4935 */
4936static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004937 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004938{
4939 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004940 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004941 state->file = NULL;
4942 state->ios_left = max_ios;
4943}
4944
Jens Axboe2b188cc2019-01-07 10:46:33 -07004945static void io_commit_sqring(struct io_ring_ctx *ctx)
4946{
Hristo Venev75b28af2019-08-26 17:23:46 +00004947 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004948
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004949 /*
4950 * Ensure any loads from the SQEs are done at this point,
4951 * since once we write the new head, the application could
4952 * write new data to them.
4953 */
4954 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004955}
4956
4957/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004958 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004959 * that is mapped by userspace. This means that care needs to be taken to
4960 * ensure that reads are stable, as we cannot rely on userspace always
4961 * being a good citizen. If members of the sqe are validated and then later
4962 * used, it's important that those reads are done through READ_ONCE() to
4963 * prevent a re-load down the line.
4964 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004965static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4966 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004967{
Hristo Venev75b28af2019-08-26 17:23:46 +00004968 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004969 unsigned head;
4970
4971 /*
4972 * The cached sq head (or cq tail) serves two purposes:
4973 *
4974 * 1) allows us to batch the cost of updating the user visible
4975 * head updates.
4976 * 2) allows the kernel side to track the head on its own, even
4977 * though the application is the one updating it.
4978 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004979 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004980 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004981 /*
4982 * All io need record the previous position, if LINK vs DARIN,
4983 * it can be used to mark the position of the first IO in the
4984 * link list.
4985 */
4986 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004987 *sqe_ptr = &ctx->sq_sqes[head];
4988 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4989 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004990 ctx->cached_sq_head++;
4991 return true;
4992 }
4993
4994 /* drop invalid entries */
4995 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004996 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004997 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004998 return false;
4999}
5000
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005001static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005002 struct file *ring_file, int ring_fd,
5003 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005004{
5005 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005006 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005007 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005008 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005009
Jens Axboec4a2ed72019-11-21 21:01:26 -07005010 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005011 if (test_bit(0, &ctx->sq_check_overflow)) {
5012 if (!list_empty(&ctx->cq_overflow_list) &&
5013 !io_cqring_overflow_flush(ctx, false))
5014 return -EBUSY;
5015 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005016
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005017 /* make sure SQ entry isn't read before tail */
5018 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005019
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005020 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5021 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005022
5023 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005024 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005025 statep = &state;
5026 }
5027
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005028 ctx->ring_fd = ring_fd;
5029 ctx->ring_file = ring_file;
5030
Jens Axboe6c271ce2019-01-10 11:22:30 -07005031 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005032 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005033 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005034 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005035
Pavel Begunkov196be952019-11-07 01:41:06 +03005036 req = io_get_req(ctx, statep);
5037 if (unlikely(!req)) {
5038 if (!submitted)
5039 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005040 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005041 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005042 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005043 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005044 break;
5045 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005046
Jens Axboed3656342019-12-18 09:50:26 -07005047 /* will complete beyond this point, count as submitted */
5048 submitted++;
5049
5050 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005051 err = -EINVAL;
5052fail_req:
5053 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005054 io_double_put_req(req);
5055 break;
5056 }
5057
5058 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005059 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005060 if (unlikely(mm_fault)) {
5061 err = -EFAULT;
5062 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005063 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005064 use_mm(ctx->sqo_mm);
5065 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005066 }
5067
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005068 req->in_async = async;
5069 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005070 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5071 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005072 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005073 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005074 }
5075
Pavel Begunkov9466f432020-01-25 22:34:01 +03005076 if (unlikely(submitted != nr)) {
5077 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5078
5079 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5080 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005081 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005082 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005083 if (statep)
5084 io_submit_state_end(&state);
5085
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005086 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5087 io_commit_sqring(ctx);
5088
Jens Axboe6c271ce2019-01-10 11:22:30 -07005089 return submitted;
5090}
5091
5092static int io_sq_thread(void *data)
5093{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005094 struct io_ring_ctx *ctx = data;
5095 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005096 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005097 mm_segment_t old_fs;
5098 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005099 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005100 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005101
Jens Axboe206aefd2019-11-07 18:27:42 -07005102 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005103
Jens Axboe6c271ce2019-01-10 11:22:30 -07005104 old_fs = get_fs();
5105 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005106 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005107
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005108 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005109 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005110 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005111
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005112 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005113 unsigned nr_events = 0;
5114
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005115 mutex_lock(&ctx->uring_lock);
5116 if (!list_empty(&ctx->poll_list))
5117 io_iopoll_getevents(ctx, &nr_events, 0);
5118 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005119 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005120 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005121 }
5122
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005123 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005124
5125 /*
5126 * If submit got -EBUSY, flag us as needing the application
5127 * to enter the kernel to reap and flush events.
5128 */
5129 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005130 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005131 * Drop cur_mm before scheduling, we can't hold it for
5132 * long periods (or over schedule()). Do this before
5133 * adding ourselves to the waitqueue, as the unuse/drop
5134 * may sleep.
5135 */
5136 if (cur_mm) {
5137 unuse_mm(cur_mm);
5138 mmput(cur_mm);
5139 cur_mm = NULL;
5140 }
5141
5142 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005143 * We're polling. If we're within the defined idle
5144 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005145 * to sleep. The exception is if we got EBUSY doing
5146 * more IO, we should wait for the application to
5147 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005148 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005149 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005150 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5151 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005152 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005153 continue;
5154 }
5155
Jens Axboe6c271ce2019-01-10 11:22:30 -07005156 prepare_to_wait(&ctx->sqo_wait, &wait,
5157 TASK_INTERRUPTIBLE);
5158
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005159 /*
5160 * While doing polled IO, before going to sleep, we need
5161 * to check if there are new reqs added to poll_list, it
5162 * is because reqs may have been punted to io worker and
5163 * will be added to poll_list later, hence check the
5164 * poll_list again.
5165 */
5166 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5167 !list_empty_careful(&ctx->poll_list)) {
5168 finish_wait(&ctx->sqo_wait, &wait);
5169 continue;
5170 }
5171
Jens Axboe6c271ce2019-01-10 11:22:30 -07005172 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005173 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005174 /* make sure to read SQ tail after writing flags */
5175 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005176
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005177 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005178 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005179 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005180 finish_wait(&ctx->sqo_wait, &wait);
5181 break;
5182 }
5183 if (signal_pending(current))
5184 flush_signals(current);
5185 schedule();
5186 finish_wait(&ctx->sqo_wait, &wait);
5187
Hristo Venev75b28af2019-08-26 17:23:46 +00005188 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005189 continue;
5190 }
5191 finish_wait(&ctx->sqo_wait, &wait);
5192
Hristo Venev75b28af2019-08-26 17:23:46 +00005193 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005194 }
5195
Jens Axboe8a4955f2019-12-09 14:52:35 -07005196 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005197 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005198 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005199 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005200 }
5201
5202 set_fs(old_fs);
5203 if (cur_mm) {
5204 unuse_mm(cur_mm);
5205 mmput(cur_mm);
5206 }
Jens Axboe181e4482019-11-25 08:52:30 -07005207 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005208
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005209 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005210
Jens Axboe6c271ce2019-01-10 11:22:30 -07005211 return 0;
5212}
5213
Jens Axboebda52162019-09-24 13:47:15 -06005214struct io_wait_queue {
5215 struct wait_queue_entry wq;
5216 struct io_ring_ctx *ctx;
5217 unsigned to_wait;
5218 unsigned nr_timeouts;
5219};
5220
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005221static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005222{
5223 struct io_ring_ctx *ctx = iowq->ctx;
5224
5225 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005226 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005227 * started waiting. For timeouts, we always want to return to userspace,
5228 * regardless of event count.
5229 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005230 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005231 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5232}
5233
5234static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5235 int wake_flags, void *key)
5236{
5237 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5238 wq);
5239
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005240 /* use noflush == true, as we can't safely rely on locking context */
5241 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005242 return -1;
5243
5244 return autoremove_wake_function(curr, mode, wake_flags, key);
5245}
5246
Jens Axboe2b188cc2019-01-07 10:46:33 -07005247/*
5248 * Wait until events become available, if we don't already have some. The
5249 * application must reap them itself, as they reside on the shared cq ring.
5250 */
5251static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5252 const sigset_t __user *sig, size_t sigsz)
5253{
Jens Axboebda52162019-09-24 13:47:15 -06005254 struct io_wait_queue iowq = {
5255 .wq = {
5256 .private = current,
5257 .func = io_wake_function,
5258 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5259 },
5260 .ctx = ctx,
5261 .to_wait = min_events,
5262 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005263 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005264 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005265
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005266 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005267 return 0;
5268
5269 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005270#ifdef CONFIG_COMPAT
5271 if (in_compat_syscall())
5272 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005273 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005274 else
5275#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005276 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005277
Jens Axboe2b188cc2019-01-07 10:46:33 -07005278 if (ret)
5279 return ret;
5280 }
5281
Jens Axboebda52162019-09-24 13:47:15 -06005282 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005283 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005284 do {
5285 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5286 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005287 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005288 break;
5289 schedule();
5290 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005291 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005292 break;
5293 }
5294 } while (1);
5295 finish_wait(&ctx->wait, &iowq.wq);
5296
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005297 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005298
Hristo Venev75b28af2019-08-26 17:23:46 +00005299 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005300}
5301
Jens Axboe6b063142019-01-10 22:13:58 -07005302static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5303{
5304#if defined(CONFIG_UNIX)
5305 if (ctx->ring_sock) {
5306 struct sock *sock = ctx->ring_sock->sk;
5307 struct sk_buff *skb;
5308
5309 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5310 kfree_skb(skb);
5311 }
5312#else
5313 int i;
5314
Jens Axboe65e19f52019-10-26 07:20:21 -06005315 for (i = 0; i < ctx->nr_user_files; i++) {
5316 struct file *file;
5317
5318 file = io_file_from_index(ctx, i);
5319 if (file)
5320 fput(file);
5321 }
Jens Axboe6b063142019-01-10 22:13:58 -07005322#endif
5323}
5324
Jens Axboe05f3fb32019-12-09 11:22:50 -07005325static void io_file_ref_kill(struct percpu_ref *ref)
5326{
5327 struct fixed_file_data *data;
5328
5329 data = container_of(ref, struct fixed_file_data, refs);
5330 complete(&data->done);
5331}
5332
Jens Axboec1e21482020-03-04 07:25:50 -07005333static void __io_file_ref_exit_and_free(struct rcu_head *rcu)
5334{
5335 struct fixed_file_data *data = container_of(rcu, struct fixed_file_data,
5336 rcu);
5337 percpu_ref_exit(&data->refs);
5338 kfree(data);
5339}
5340
5341static void io_file_ref_exit_and_free(struct rcu_head *rcu)
5342{
5343 /*
5344 * We need to order our exit+free call against the potentially
5345 * existing call_rcu() for switching to atomic. One way to do that
5346 * is to have this rcu callback queue the final put and free, as we
5347 * could otherwise have a pre-existing atomic switch complete _after_
5348 * the free callback we queued.
5349 */
5350 call_rcu(rcu, __io_file_ref_exit_and_free);
5351}
5352
Jens Axboe6b063142019-01-10 22:13:58 -07005353static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5354{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005355 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005356 unsigned nr_tables, i;
5357
Jens Axboe05f3fb32019-12-09 11:22:50 -07005358 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005359 return -ENXIO;
5360
Jens Axboe05f3fb32019-12-09 11:22:50 -07005361 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005362 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005363 wait_for_completion(&data->done);
5364 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005365
Jens Axboe6b063142019-01-10 22:13:58 -07005366 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005367 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5368 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005369 kfree(data->table[i].files);
5370 kfree(data->table);
Jens Axboec1e21482020-03-04 07:25:50 -07005371 call_rcu(&data->rcu, io_file_ref_exit_and_free);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005372 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005373 ctx->nr_user_files = 0;
5374 return 0;
5375}
5376
Jens Axboe6c271ce2019-01-10 11:22:30 -07005377static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5378{
5379 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005380 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005381 /*
5382 * The park is a bit of a work-around, without it we get
5383 * warning spews on shutdown with SQPOLL set and affinity
5384 * set to a single CPU.
5385 */
Jens Axboe06058632019-04-13 09:26:03 -06005386 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005387 kthread_stop(ctx->sqo_thread);
5388 ctx->sqo_thread = NULL;
5389 }
5390}
5391
Jens Axboe6b063142019-01-10 22:13:58 -07005392static void io_finish_async(struct io_ring_ctx *ctx)
5393{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005394 io_sq_thread_stop(ctx);
5395
Jens Axboe561fb042019-10-24 07:25:42 -06005396 if (ctx->io_wq) {
5397 io_wq_destroy(ctx->io_wq);
5398 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005399 }
5400}
5401
5402#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005403/*
5404 * Ensure the UNIX gc is aware of our file set, so we are certain that
5405 * the io_uring can be safely unregistered on process exit, even if we have
5406 * loops in the file referencing.
5407 */
5408static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5409{
5410 struct sock *sk = ctx->ring_sock->sk;
5411 struct scm_fp_list *fpl;
5412 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005413 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005414
5415 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5416 unsigned long inflight = ctx->user->unix_inflight + nr;
5417
5418 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5419 return -EMFILE;
5420 }
5421
5422 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5423 if (!fpl)
5424 return -ENOMEM;
5425
5426 skb = alloc_skb(0, GFP_KERNEL);
5427 if (!skb) {
5428 kfree(fpl);
5429 return -ENOMEM;
5430 }
5431
5432 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005433
Jens Axboe08a45172019-10-03 08:11:03 -06005434 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005435 fpl->user = get_uid(ctx->user);
5436 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005437 struct file *file = io_file_from_index(ctx, i + offset);
5438
5439 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005440 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005441 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005442 unix_inflight(fpl->user, fpl->fp[nr_files]);
5443 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005444 }
5445
Jens Axboe08a45172019-10-03 08:11:03 -06005446 if (nr_files) {
5447 fpl->max = SCM_MAX_FD;
5448 fpl->count = nr_files;
5449 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005450 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005451 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5452 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005453
Jens Axboe08a45172019-10-03 08:11:03 -06005454 for (i = 0; i < nr_files; i++)
5455 fput(fpl->fp[i]);
5456 } else {
5457 kfree_skb(skb);
5458 kfree(fpl);
5459 }
Jens Axboe6b063142019-01-10 22:13:58 -07005460
5461 return 0;
5462}
5463
5464/*
5465 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5466 * causes regular reference counting to break down. We rely on the UNIX
5467 * garbage collection to take care of this problem for us.
5468 */
5469static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5470{
5471 unsigned left, total;
5472 int ret = 0;
5473
5474 total = 0;
5475 left = ctx->nr_user_files;
5476 while (left) {
5477 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005478
5479 ret = __io_sqe_files_scm(ctx, this_files, total);
5480 if (ret)
5481 break;
5482 left -= this_files;
5483 total += this_files;
5484 }
5485
5486 if (!ret)
5487 return 0;
5488
5489 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005490 struct file *file = io_file_from_index(ctx, total);
5491
5492 if (file)
5493 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005494 total++;
5495 }
5496
5497 return ret;
5498}
5499#else
5500static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5501{
5502 return 0;
5503}
5504#endif
5505
Jens Axboe65e19f52019-10-26 07:20:21 -06005506static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5507 unsigned nr_files)
5508{
5509 int i;
5510
5511 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005512 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005513 unsigned this_files;
5514
5515 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5516 table->files = kcalloc(this_files, sizeof(struct file *),
5517 GFP_KERNEL);
5518 if (!table->files)
5519 break;
5520 nr_files -= this_files;
5521 }
5522
5523 if (i == nr_tables)
5524 return 0;
5525
5526 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005527 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005528 kfree(table->files);
5529 }
5530 return 1;
5531}
5532
Jens Axboe05f3fb32019-12-09 11:22:50 -07005533static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005534{
5535#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005536 struct sock *sock = ctx->ring_sock->sk;
5537 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5538 struct sk_buff *skb;
5539 int i;
5540
5541 __skb_queue_head_init(&list);
5542
5543 /*
5544 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5545 * remove this entry and rearrange the file array.
5546 */
5547 skb = skb_dequeue(head);
5548 while (skb) {
5549 struct scm_fp_list *fp;
5550
5551 fp = UNIXCB(skb).fp;
5552 for (i = 0; i < fp->count; i++) {
5553 int left;
5554
5555 if (fp->fp[i] != file)
5556 continue;
5557
5558 unix_notinflight(fp->user, fp->fp[i]);
5559 left = fp->count - 1 - i;
5560 if (left) {
5561 memmove(&fp->fp[i], &fp->fp[i + 1],
5562 left * sizeof(struct file *));
5563 }
5564 fp->count--;
5565 if (!fp->count) {
5566 kfree_skb(skb);
5567 skb = NULL;
5568 } else {
5569 __skb_queue_tail(&list, skb);
5570 }
5571 fput(file);
5572 file = NULL;
5573 break;
5574 }
5575
5576 if (!file)
5577 break;
5578
5579 __skb_queue_tail(&list, skb);
5580
5581 skb = skb_dequeue(head);
5582 }
5583
5584 if (skb_peek(&list)) {
5585 spin_lock_irq(&head->lock);
5586 while ((skb = __skb_dequeue(&list)) != NULL)
5587 __skb_queue_tail(head, skb);
5588 spin_unlock_irq(&head->lock);
5589 }
5590#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005591 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005592#endif
5593}
5594
Jens Axboe05f3fb32019-12-09 11:22:50 -07005595struct io_file_put {
5596 struct llist_node llist;
5597 struct file *file;
5598 struct completion *done;
5599};
5600
Jens Axboe2faf8522020-02-04 19:54:55 -07005601static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005602{
5603 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005604 struct llist_node *node;
5605
Jens Axboe05f3fb32019-12-09 11:22:50 -07005606 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5607 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5608 io_ring_file_put(data->ctx, pfile->file);
5609 if (pfile->done)
5610 complete(pfile->done);
5611 else
5612 kfree(pfile);
5613 }
5614 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005615}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005616
Jens Axboe2faf8522020-02-04 19:54:55 -07005617static void io_ring_file_ref_switch(struct work_struct *work)
5618{
5619 struct fixed_file_data *data;
5620
5621 data = container_of(work, struct fixed_file_data, ref_work);
5622 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005623 percpu_ref_switch_to_percpu(&data->refs);
5624}
5625
5626static void io_file_data_ref_zero(struct percpu_ref *ref)
5627{
5628 struct fixed_file_data *data;
5629
5630 data = container_of(ref, struct fixed_file_data, refs);
5631
Jens Axboe2faf8522020-02-04 19:54:55 -07005632 /*
5633 * We can't safely switch from inside this context, punt to wq. If
5634 * the table ref is going away, the table is being unregistered.
5635 * Don't queue up the async work for that case, the caller will
5636 * handle it.
5637 */
5638 if (!percpu_ref_is_dying(&data->refs))
5639 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005640}
5641
5642static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5643 unsigned nr_args)
5644{
5645 __s32 __user *fds = (__s32 __user *) arg;
5646 unsigned nr_tables;
5647 struct file *file;
5648 int fd, ret = 0;
5649 unsigned i;
5650
5651 if (ctx->file_data)
5652 return -EBUSY;
5653 if (!nr_args)
5654 return -EINVAL;
5655 if (nr_args > IORING_MAX_FIXED_FILES)
5656 return -EMFILE;
5657
5658 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5659 if (!ctx->file_data)
5660 return -ENOMEM;
5661 ctx->file_data->ctx = ctx;
5662 init_completion(&ctx->file_data->done);
5663
5664 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5665 ctx->file_data->table = kcalloc(nr_tables,
5666 sizeof(struct fixed_file_table),
5667 GFP_KERNEL);
5668 if (!ctx->file_data->table) {
5669 kfree(ctx->file_data);
5670 ctx->file_data = NULL;
5671 return -ENOMEM;
5672 }
5673
5674 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5675 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5676 kfree(ctx->file_data->table);
5677 kfree(ctx->file_data);
5678 ctx->file_data = NULL;
5679 return -ENOMEM;
5680 }
5681 ctx->file_data->put_llist.first = NULL;
5682 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5683
5684 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5685 percpu_ref_exit(&ctx->file_data->refs);
5686 kfree(ctx->file_data->table);
5687 kfree(ctx->file_data);
5688 ctx->file_data = NULL;
5689 return -ENOMEM;
5690 }
5691
5692 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5693 struct fixed_file_table *table;
5694 unsigned index;
5695
5696 ret = -EFAULT;
5697 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5698 break;
5699 /* allow sparse sets */
5700 if (fd == -1) {
5701 ret = 0;
5702 continue;
5703 }
5704
5705 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5706 index = i & IORING_FILE_TABLE_MASK;
5707 file = fget(fd);
5708
5709 ret = -EBADF;
5710 if (!file)
5711 break;
5712
5713 /*
5714 * Don't allow io_uring instances to be registered. If UNIX
5715 * isn't enabled, then this causes a reference cycle and this
5716 * instance can never get freed. If UNIX is enabled we'll
5717 * handle it just fine, but there's still no point in allowing
5718 * a ring fd as it doesn't support regular read/write anyway.
5719 */
5720 if (file->f_op == &io_uring_fops) {
5721 fput(file);
5722 break;
5723 }
5724 ret = 0;
5725 table->files[index] = file;
5726 }
5727
5728 if (ret) {
5729 for (i = 0; i < ctx->nr_user_files; i++) {
5730 file = io_file_from_index(ctx, i);
5731 if (file)
5732 fput(file);
5733 }
5734 for (i = 0; i < nr_tables; i++)
5735 kfree(ctx->file_data->table[i].files);
5736
5737 kfree(ctx->file_data->table);
5738 kfree(ctx->file_data);
5739 ctx->file_data = NULL;
5740 ctx->nr_user_files = 0;
5741 return ret;
5742 }
5743
5744 ret = io_sqe_files_scm(ctx);
5745 if (ret)
5746 io_sqe_files_unregister(ctx);
5747
5748 return ret;
5749}
5750
Jens Axboec3a31e62019-10-03 13:59:56 -06005751static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5752 int index)
5753{
5754#if defined(CONFIG_UNIX)
5755 struct sock *sock = ctx->ring_sock->sk;
5756 struct sk_buff_head *head = &sock->sk_receive_queue;
5757 struct sk_buff *skb;
5758
5759 /*
5760 * See if we can merge this file into an existing skb SCM_RIGHTS
5761 * file set. If there's no room, fall back to allocating a new skb
5762 * and filling it in.
5763 */
5764 spin_lock_irq(&head->lock);
5765 skb = skb_peek(head);
5766 if (skb) {
5767 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5768
5769 if (fpl->count < SCM_MAX_FD) {
5770 __skb_unlink(skb, head);
5771 spin_unlock_irq(&head->lock);
5772 fpl->fp[fpl->count] = get_file(file);
5773 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5774 fpl->count++;
5775 spin_lock_irq(&head->lock);
5776 __skb_queue_head(head, skb);
5777 } else {
5778 skb = NULL;
5779 }
5780 }
5781 spin_unlock_irq(&head->lock);
5782
5783 if (skb) {
5784 fput(file);
5785 return 0;
5786 }
5787
5788 return __io_sqe_files_scm(ctx, 1, index);
5789#else
5790 return 0;
5791#endif
5792}
5793
Jens Axboe05f3fb32019-12-09 11:22:50 -07005794static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005795{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005796 struct fixed_file_data *data;
5797
Jens Axboedd3db2a2020-02-26 10:23:43 -07005798 /*
5799 * Juggle reference to ensure we hit zero, if needed, so we can
5800 * switch back to percpu mode
5801 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005802 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07005803 percpu_ref_put(&data->refs);
5804 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005805}
5806
5807static bool io_queue_file_removal(struct fixed_file_data *data,
5808 struct file *file)
5809{
5810 struct io_file_put *pfile, pfile_stack;
5811 DECLARE_COMPLETION_ONSTACK(done);
5812
5813 /*
5814 * If we fail allocating the struct we need for doing async reomval
5815 * of this file, just punt to sync and wait for it.
5816 */
5817 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5818 if (!pfile) {
5819 pfile = &pfile_stack;
5820 pfile->done = &done;
5821 }
5822
5823 pfile->file = file;
5824 llist_add(&pfile->llist, &data->put_llist);
5825
5826 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07005827 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005828 wait_for_completion(&done);
5829 flush_work(&data->ref_work);
5830 return false;
5831 }
5832
5833 return true;
5834}
5835
5836static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5837 struct io_uring_files_update *up,
5838 unsigned nr_args)
5839{
5840 struct fixed_file_data *data = ctx->file_data;
5841 bool ref_switch = false;
5842 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005843 __s32 __user *fds;
5844 int fd, i, err;
5845 __u32 done;
5846
Jens Axboe05f3fb32019-12-09 11:22:50 -07005847 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005848 return -EOVERFLOW;
5849 if (done > ctx->nr_user_files)
5850 return -EINVAL;
5851
5852 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005853 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005854 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005855 struct fixed_file_table *table;
5856 unsigned index;
5857
Jens Axboec3a31e62019-10-03 13:59:56 -06005858 err = 0;
5859 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5860 err = -EFAULT;
5861 break;
5862 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005863 i = array_index_nospec(up->offset, ctx->nr_user_files);
5864 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005865 index = i & IORING_FILE_TABLE_MASK;
5866 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005867 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005868 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005869 if (io_queue_file_removal(data, file))
5870 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005871 }
5872 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005873 file = fget(fd);
5874 if (!file) {
5875 err = -EBADF;
5876 break;
5877 }
5878 /*
5879 * Don't allow io_uring instances to be registered. If
5880 * UNIX isn't enabled, then this causes a reference
5881 * cycle and this instance can never get freed. If UNIX
5882 * is enabled we'll handle it just fine, but there's
5883 * still no point in allowing a ring fd as it doesn't
5884 * support regular read/write anyway.
5885 */
5886 if (file->f_op == &io_uring_fops) {
5887 fput(file);
5888 err = -EBADF;
5889 break;
5890 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005891 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005892 err = io_sqe_file_register(ctx, file, i);
5893 if (err)
5894 break;
5895 }
5896 nr_args--;
5897 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005898 up->offset++;
5899 }
5900
Jens Axboedd3db2a2020-02-26 10:23:43 -07005901 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005902 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005903
5904 return done ? done : err;
5905}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005906static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5907 unsigned nr_args)
5908{
5909 struct io_uring_files_update up;
5910
5911 if (!ctx->file_data)
5912 return -ENXIO;
5913 if (!nr_args)
5914 return -EINVAL;
5915 if (copy_from_user(&up, arg, sizeof(up)))
5916 return -EFAULT;
5917 if (up.resv)
5918 return -EINVAL;
5919
5920 return __io_sqe_files_update(ctx, &up, nr_args);
5921}
Jens Axboec3a31e62019-10-03 13:59:56 -06005922
Jens Axboe7d723062019-11-12 22:31:31 -07005923static void io_put_work(struct io_wq_work *work)
5924{
5925 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5926
5927 io_put_req(req);
5928}
5929
5930static void io_get_work(struct io_wq_work *work)
5931{
5932 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5933
5934 refcount_inc(&req->refs);
5935}
5936
Pavel Begunkov24369c22020-01-28 03:15:48 +03005937static int io_init_wq_offload(struct io_ring_ctx *ctx,
5938 struct io_uring_params *p)
5939{
5940 struct io_wq_data data;
5941 struct fd f;
5942 struct io_ring_ctx *ctx_attach;
5943 unsigned int concurrency;
5944 int ret = 0;
5945
5946 data.user = ctx->user;
5947 data.get_work = io_get_work;
5948 data.put_work = io_put_work;
5949
5950 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5951 /* Do QD, or 4 * CPUS, whatever is smallest */
5952 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5953
5954 ctx->io_wq = io_wq_create(concurrency, &data);
5955 if (IS_ERR(ctx->io_wq)) {
5956 ret = PTR_ERR(ctx->io_wq);
5957 ctx->io_wq = NULL;
5958 }
5959 return ret;
5960 }
5961
5962 f = fdget(p->wq_fd);
5963 if (!f.file)
5964 return -EBADF;
5965
5966 if (f.file->f_op != &io_uring_fops) {
5967 ret = -EINVAL;
5968 goto out_fput;
5969 }
5970
5971 ctx_attach = f.file->private_data;
5972 /* @io_wq is protected by holding the fd */
5973 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5974 ret = -EINVAL;
5975 goto out_fput;
5976 }
5977
5978 ctx->io_wq = ctx_attach->io_wq;
5979out_fput:
5980 fdput(f);
5981 return ret;
5982}
5983
Jens Axboe6c271ce2019-01-10 11:22:30 -07005984static int io_sq_offload_start(struct io_ring_ctx *ctx,
5985 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005986{
5987 int ret;
5988
Jens Axboe6c271ce2019-01-10 11:22:30 -07005989 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005990 mmgrab(current->mm);
5991 ctx->sqo_mm = current->mm;
5992
Jens Axboe6c271ce2019-01-10 11:22:30 -07005993 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005994 ret = -EPERM;
5995 if (!capable(CAP_SYS_ADMIN))
5996 goto err;
5997
Jens Axboe917257d2019-04-13 09:28:55 -06005998 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5999 if (!ctx->sq_thread_idle)
6000 ctx->sq_thread_idle = HZ;
6001
Jens Axboe6c271ce2019-01-10 11:22:30 -07006002 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006003 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006004
Jens Axboe917257d2019-04-13 09:28:55 -06006005 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006006 if (cpu >= nr_cpu_ids)
6007 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006008 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006009 goto err;
6010
Jens Axboe6c271ce2019-01-10 11:22:30 -07006011 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6012 ctx, cpu,
6013 "io_uring-sq");
6014 } else {
6015 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6016 "io_uring-sq");
6017 }
6018 if (IS_ERR(ctx->sqo_thread)) {
6019 ret = PTR_ERR(ctx->sqo_thread);
6020 ctx->sqo_thread = NULL;
6021 goto err;
6022 }
6023 wake_up_process(ctx->sqo_thread);
6024 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6025 /* Can't have SQ_AFF without SQPOLL */
6026 ret = -EINVAL;
6027 goto err;
6028 }
6029
Pavel Begunkov24369c22020-01-28 03:15:48 +03006030 ret = io_init_wq_offload(ctx, p);
6031 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006032 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006033
6034 return 0;
6035err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006036 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006037 mmdrop(ctx->sqo_mm);
6038 ctx->sqo_mm = NULL;
6039 return ret;
6040}
6041
6042static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6043{
6044 atomic_long_sub(nr_pages, &user->locked_vm);
6045}
6046
6047static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6048{
6049 unsigned long page_limit, cur_pages, new_pages;
6050
6051 /* Don't allow more pages than we can safely lock */
6052 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6053
6054 do {
6055 cur_pages = atomic_long_read(&user->locked_vm);
6056 new_pages = cur_pages + nr_pages;
6057 if (new_pages > page_limit)
6058 return -ENOMEM;
6059 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6060 new_pages) != cur_pages);
6061
6062 return 0;
6063}
6064
6065static void io_mem_free(void *ptr)
6066{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006067 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006068
Mark Rutland52e04ef2019-04-30 17:30:21 +01006069 if (!ptr)
6070 return;
6071
6072 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006073 if (put_page_testzero(page))
6074 free_compound_page(page);
6075}
6076
6077static void *io_mem_alloc(size_t size)
6078{
6079 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6080 __GFP_NORETRY;
6081
6082 return (void *) __get_free_pages(gfp_flags, get_order(size));
6083}
6084
Hristo Venev75b28af2019-08-26 17:23:46 +00006085static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6086 size_t *sq_offset)
6087{
6088 struct io_rings *rings;
6089 size_t off, sq_array_size;
6090
6091 off = struct_size(rings, cqes, cq_entries);
6092 if (off == SIZE_MAX)
6093 return SIZE_MAX;
6094
6095#ifdef CONFIG_SMP
6096 off = ALIGN(off, SMP_CACHE_BYTES);
6097 if (off == 0)
6098 return SIZE_MAX;
6099#endif
6100
6101 sq_array_size = array_size(sizeof(u32), sq_entries);
6102 if (sq_array_size == SIZE_MAX)
6103 return SIZE_MAX;
6104
6105 if (check_add_overflow(off, sq_array_size, &off))
6106 return SIZE_MAX;
6107
6108 if (sq_offset)
6109 *sq_offset = off;
6110
6111 return off;
6112}
6113
Jens Axboe2b188cc2019-01-07 10:46:33 -07006114static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6115{
Hristo Venev75b28af2019-08-26 17:23:46 +00006116 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006117
Hristo Venev75b28af2019-08-26 17:23:46 +00006118 pages = (size_t)1 << get_order(
6119 rings_size(sq_entries, cq_entries, NULL));
6120 pages += (size_t)1 << get_order(
6121 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006122
Hristo Venev75b28af2019-08-26 17:23:46 +00006123 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006124}
6125
Jens Axboeedafcce2019-01-09 09:16:05 -07006126static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6127{
6128 int i, j;
6129
6130 if (!ctx->user_bufs)
6131 return -ENXIO;
6132
6133 for (i = 0; i < ctx->nr_user_bufs; i++) {
6134 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6135
6136 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006137 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006138
6139 if (ctx->account_mem)
6140 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006141 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006142 imu->nr_bvecs = 0;
6143 }
6144
6145 kfree(ctx->user_bufs);
6146 ctx->user_bufs = NULL;
6147 ctx->nr_user_bufs = 0;
6148 return 0;
6149}
6150
6151static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6152 void __user *arg, unsigned index)
6153{
6154 struct iovec __user *src;
6155
6156#ifdef CONFIG_COMPAT
6157 if (ctx->compat) {
6158 struct compat_iovec __user *ciovs;
6159 struct compat_iovec ciov;
6160
6161 ciovs = (struct compat_iovec __user *) arg;
6162 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6163 return -EFAULT;
6164
Jens Axboed55e5f52019-12-11 16:12:15 -07006165 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006166 dst->iov_len = ciov.iov_len;
6167 return 0;
6168 }
6169#endif
6170 src = (struct iovec __user *) arg;
6171 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6172 return -EFAULT;
6173 return 0;
6174}
6175
6176static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6177 unsigned nr_args)
6178{
6179 struct vm_area_struct **vmas = NULL;
6180 struct page **pages = NULL;
6181 int i, j, got_pages = 0;
6182 int ret = -EINVAL;
6183
6184 if (ctx->user_bufs)
6185 return -EBUSY;
6186 if (!nr_args || nr_args > UIO_MAXIOV)
6187 return -EINVAL;
6188
6189 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6190 GFP_KERNEL);
6191 if (!ctx->user_bufs)
6192 return -ENOMEM;
6193
6194 for (i = 0; i < nr_args; i++) {
6195 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6196 unsigned long off, start, end, ubuf;
6197 int pret, nr_pages;
6198 struct iovec iov;
6199 size_t size;
6200
6201 ret = io_copy_iov(ctx, &iov, arg, i);
6202 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006203 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006204
6205 /*
6206 * Don't impose further limits on the size and buffer
6207 * constraints here, we'll -EINVAL later when IO is
6208 * submitted if they are wrong.
6209 */
6210 ret = -EFAULT;
6211 if (!iov.iov_base || !iov.iov_len)
6212 goto err;
6213
6214 /* arbitrary limit, but we need something */
6215 if (iov.iov_len > SZ_1G)
6216 goto err;
6217
6218 ubuf = (unsigned long) iov.iov_base;
6219 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6220 start = ubuf >> PAGE_SHIFT;
6221 nr_pages = end - start;
6222
6223 if (ctx->account_mem) {
6224 ret = io_account_mem(ctx->user, nr_pages);
6225 if (ret)
6226 goto err;
6227 }
6228
6229 ret = 0;
6230 if (!pages || nr_pages > got_pages) {
6231 kfree(vmas);
6232 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006233 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006234 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006235 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006236 sizeof(struct vm_area_struct *),
6237 GFP_KERNEL);
6238 if (!pages || !vmas) {
6239 ret = -ENOMEM;
6240 if (ctx->account_mem)
6241 io_unaccount_mem(ctx->user, nr_pages);
6242 goto err;
6243 }
6244 got_pages = nr_pages;
6245 }
6246
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006247 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006248 GFP_KERNEL);
6249 ret = -ENOMEM;
6250 if (!imu->bvec) {
6251 if (ctx->account_mem)
6252 io_unaccount_mem(ctx->user, nr_pages);
6253 goto err;
6254 }
6255
6256 ret = 0;
6257 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006258 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006259 FOLL_WRITE | FOLL_LONGTERM,
6260 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006261 if (pret == nr_pages) {
6262 /* don't support file backed memory */
6263 for (j = 0; j < nr_pages; j++) {
6264 struct vm_area_struct *vma = vmas[j];
6265
6266 if (vma->vm_file &&
6267 !is_file_hugepages(vma->vm_file)) {
6268 ret = -EOPNOTSUPP;
6269 break;
6270 }
6271 }
6272 } else {
6273 ret = pret < 0 ? pret : -EFAULT;
6274 }
6275 up_read(&current->mm->mmap_sem);
6276 if (ret) {
6277 /*
6278 * if we did partial map, or found file backed vmas,
6279 * release any pages we did get
6280 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006281 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006282 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006283 if (ctx->account_mem)
6284 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006285 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006286 goto err;
6287 }
6288
6289 off = ubuf & ~PAGE_MASK;
6290 size = iov.iov_len;
6291 for (j = 0; j < nr_pages; j++) {
6292 size_t vec_len;
6293
6294 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6295 imu->bvec[j].bv_page = pages[j];
6296 imu->bvec[j].bv_len = vec_len;
6297 imu->bvec[j].bv_offset = off;
6298 off = 0;
6299 size -= vec_len;
6300 }
6301 /* store original address for later verification */
6302 imu->ubuf = ubuf;
6303 imu->len = iov.iov_len;
6304 imu->nr_bvecs = nr_pages;
6305
6306 ctx->nr_user_bufs++;
6307 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006308 kvfree(pages);
6309 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006310 return 0;
6311err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006312 kvfree(pages);
6313 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006314 io_sqe_buffer_unregister(ctx);
6315 return ret;
6316}
6317
Jens Axboe9b402842019-04-11 11:45:41 -06006318static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6319{
6320 __s32 __user *fds = arg;
6321 int fd;
6322
6323 if (ctx->cq_ev_fd)
6324 return -EBUSY;
6325
6326 if (copy_from_user(&fd, fds, sizeof(*fds)))
6327 return -EFAULT;
6328
6329 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6330 if (IS_ERR(ctx->cq_ev_fd)) {
6331 int ret = PTR_ERR(ctx->cq_ev_fd);
6332 ctx->cq_ev_fd = NULL;
6333 return ret;
6334 }
6335
6336 return 0;
6337}
6338
6339static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6340{
6341 if (ctx->cq_ev_fd) {
6342 eventfd_ctx_put(ctx->cq_ev_fd);
6343 ctx->cq_ev_fd = NULL;
6344 return 0;
6345 }
6346
6347 return -ENXIO;
6348}
6349
Jens Axboe2b188cc2019-01-07 10:46:33 -07006350static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6351{
Jens Axboe6b063142019-01-10 22:13:58 -07006352 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006353 if (ctx->sqo_mm)
6354 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006355
6356 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006357 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006358 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006359 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006360 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006361
Jens Axboe2b188cc2019-01-07 10:46:33 -07006362#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006363 if (ctx->ring_sock) {
6364 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006365 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006366 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006367#endif
6368
Hristo Venev75b28af2019-08-26 17:23:46 +00006369 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006370 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006371
6372 percpu_ref_exit(&ctx->refs);
6373 if (ctx->account_mem)
6374 io_unaccount_mem(ctx->user,
6375 ring_pages(ctx->sq_entries, ctx->cq_entries));
6376 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006377 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006378 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006379 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006380 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006381 kfree(ctx);
6382}
6383
6384static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6385{
6386 struct io_ring_ctx *ctx = file->private_data;
6387 __poll_t mask = 0;
6388
6389 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006390 /*
6391 * synchronizes with barrier from wq_has_sleeper call in
6392 * io_commit_cqring
6393 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006394 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006395 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6396 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006397 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006398 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006399 mask |= EPOLLIN | EPOLLRDNORM;
6400
6401 return mask;
6402}
6403
6404static int io_uring_fasync(int fd, struct file *file, int on)
6405{
6406 struct io_ring_ctx *ctx = file->private_data;
6407
6408 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6409}
6410
Jens Axboe071698e2020-01-28 10:04:42 -07006411static int io_remove_personalities(int id, void *p, void *data)
6412{
6413 struct io_ring_ctx *ctx = data;
6414 const struct cred *cred;
6415
6416 cred = idr_remove(&ctx->personality_idr, id);
6417 if (cred)
6418 put_cred(cred);
6419 return 0;
6420}
6421
Jens Axboe2b188cc2019-01-07 10:46:33 -07006422static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6423{
6424 mutex_lock(&ctx->uring_lock);
6425 percpu_ref_kill(&ctx->refs);
6426 mutex_unlock(&ctx->uring_lock);
6427
Jens Axboedf069d82020-02-04 16:48:34 -07006428 /*
6429 * Wait for sq thread to idle, if we have one. It won't spin on new
6430 * work after we've killed the ctx ref above. This is important to do
6431 * before we cancel existing commands, as the thread could otherwise
6432 * be queueing new work post that. If that's work we need to cancel,
6433 * it could cause shutdown to hang.
6434 */
6435 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6436 cpu_relax();
6437
Jens Axboe5262f562019-09-17 12:26:57 -06006438 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006439 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006440
6441 if (ctx->io_wq)
6442 io_wq_cancel_all(ctx->io_wq);
6443
Jens Axboedef596e2019-01-09 08:59:42 -07006444 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006445 /* if we failed setting up the ctx, we might not have any rings */
6446 if (ctx->rings)
6447 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006448 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006449 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006450 io_ring_ctx_free(ctx);
6451}
6452
6453static int io_uring_release(struct inode *inode, struct file *file)
6454{
6455 struct io_ring_ctx *ctx = file->private_data;
6456
6457 file->private_data = NULL;
6458 io_ring_ctx_wait_and_kill(ctx);
6459 return 0;
6460}
6461
Jens Axboefcb323c2019-10-24 12:39:47 -06006462static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6463 struct files_struct *files)
6464{
6465 struct io_kiocb *req;
6466 DEFINE_WAIT(wait);
6467
6468 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006469 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006470
6471 spin_lock_irq(&ctx->inflight_lock);
6472 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006473 if (req->work.files != files)
6474 continue;
6475 /* req is being completed, ignore */
6476 if (!refcount_inc_not_zero(&req->refs))
6477 continue;
6478 cancel_req = req;
6479 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006480 }
Jens Axboe768134d2019-11-10 20:30:53 -07006481 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006482 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006483 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006484 spin_unlock_irq(&ctx->inflight_lock);
6485
Jens Axboe768134d2019-11-10 20:30:53 -07006486 /* We need to keep going until we don't find a matching req */
6487 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006488 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006489
Jens Axboe2ca10252020-02-13 17:17:35 -07006490 if (cancel_req->flags & REQ_F_OVERFLOW) {
6491 spin_lock_irq(&ctx->completion_lock);
6492 list_del(&cancel_req->list);
6493 cancel_req->flags &= ~REQ_F_OVERFLOW;
6494 if (list_empty(&ctx->cq_overflow_list)) {
6495 clear_bit(0, &ctx->sq_check_overflow);
6496 clear_bit(0, &ctx->cq_check_overflow);
6497 }
6498 spin_unlock_irq(&ctx->completion_lock);
6499
6500 WRITE_ONCE(ctx->rings->cq_overflow,
6501 atomic_inc_return(&ctx->cached_cq_overflow));
6502
6503 /*
6504 * Put inflight ref and overflow ref. If that's
6505 * all we had, then we're done with this request.
6506 */
6507 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6508 io_put_req(cancel_req);
6509 continue;
6510 }
6511 }
6512
Bob Liu2f6d9b92019-11-13 18:06:24 +08006513 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6514 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006515 schedule();
6516 }
Jens Axboe768134d2019-11-10 20:30:53 -07006517 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006518}
6519
6520static int io_uring_flush(struct file *file, void *data)
6521{
6522 struct io_ring_ctx *ctx = file->private_data;
6523
6524 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006525
6526 /*
6527 * If the task is going away, cancel work it may have pending
6528 */
6529 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6530 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6531
Jens Axboefcb323c2019-10-24 12:39:47 -06006532 return 0;
6533}
6534
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006535static void *io_uring_validate_mmap_request(struct file *file,
6536 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006537{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006538 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006539 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006540 struct page *page;
6541 void *ptr;
6542
6543 switch (offset) {
6544 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006545 case IORING_OFF_CQ_RING:
6546 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006547 break;
6548 case IORING_OFF_SQES:
6549 ptr = ctx->sq_sqes;
6550 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006551 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006552 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006553 }
6554
6555 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006556 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006557 return ERR_PTR(-EINVAL);
6558
6559 return ptr;
6560}
6561
6562#ifdef CONFIG_MMU
6563
6564static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6565{
6566 size_t sz = vma->vm_end - vma->vm_start;
6567 unsigned long pfn;
6568 void *ptr;
6569
6570 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6571 if (IS_ERR(ptr))
6572 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006573
6574 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6575 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6576}
6577
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006578#else /* !CONFIG_MMU */
6579
6580static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6581{
6582 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6583}
6584
6585static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6586{
6587 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6588}
6589
6590static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6591 unsigned long addr, unsigned long len,
6592 unsigned long pgoff, unsigned long flags)
6593{
6594 void *ptr;
6595
6596 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6597 if (IS_ERR(ptr))
6598 return PTR_ERR(ptr);
6599
6600 return (unsigned long) ptr;
6601}
6602
6603#endif /* !CONFIG_MMU */
6604
Jens Axboe2b188cc2019-01-07 10:46:33 -07006605SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6606 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6607 size_t, sigsz)
6608{
6609 struct io_ring_ctx *ctx;
6610 long ret = -EBADF;
6611 int submitted = 0;
6612 struct fd f;
6613
Jens Axboe6c271ce2019-01-10 11:22:30 -07006614 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006615 return -EINVAL;
6616
6617 f = fdget(fd);
6618 if (!f.file)
6619 return -EBADF;
6620
6621 ret = -EOPNOTSUPP;
6622 if (f.file->f_op != &io_uring_fops)
6623 goto out_fput;
6624
6625 ret = -ENXIO;
6626 ctx = f.file->private_data;
6627 if (!percpu_ref_tryget(&ctx->refs))
6628 goto out_fput;
6629
Jens Axboe6c271ce2019-01-10 11:22:30 -07006630 /*
6631 * For SQ polling, the thread will do all submissions and completions.
6632 * Just return the requested submit count, and wake the thread if
6633 * we were asked to.
6634 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006635 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006636 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006637 if (!list_empty_careful(&ctx->cq_overflow_list))
6638 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006639 if (flags & IORING_ENTER_SQ_WAKEUP)
6640 wake_up(&ctx->sqo_wait);
6641 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006642 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006643 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006644
6645 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006646 /* already have mm, so io_submit_sqes() won't try to grab it */
6647 cur_mm = ctx->sqo_mm;
6648 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6649 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006650 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006651
6652 if (submitted != to_submit)
6653 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006654 }
6655 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006656 unsigned nr_events = 0;
6657
Jens Axboe2b188cc2019-01-07 10:46:33 -07006658 min_complete = min(min_complete, ctx->cq_entries);
6659
Jens Axboedef596e2019-01-09 08:59:42 -07006660 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006661 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006662 } else {
6663 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6664 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006665 }
6666
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006667out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006668 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006669out_fput:
6670 fdput(f);
6671 return submitted ? submitted : ret;
6672}
6673
Tobias Klauserbebdb652020-02-26 18:38:32 +01006674#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006675static int io_uring_show_cred(int id, void *p, void *data)
6676{
6677 const struct cred *cred = p;
6678 struct seq_file *m = data;
6679 struct user_namespace *uns = seq_user_ns(m);
6680 struct group_info *gi;
6681 kernel_cap_t cap;
6682 unsigned __capi;
6683 int g;
6684
6685 seq_printf(m, "%5d\n", id);
6686 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6687 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6688 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6689 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6690 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6691 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6692 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6693 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6694 seq_puts(m, "\n\tGroups:\t");
6695 gi = cred->group_info;
6696 for (g = 0; g < gi->ngroups; g++) {
6697 seq_put_decimal_ull(m, g ? " " : "",
6698 from_kgid_munged(uns, gi->gid[g]));
6699 }
6700 seq_puts(m, "\n\tCapEff:\t");
6701 cap = cred->cap_effective;
6702 CAP_FOR_EACH_U32(__capi)
6703 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6704 seq_putc(m, '\n');
6705 return 0;
6706}
6707
6708static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6709{
6710 int i;
6711
6712 mutex_lock(&ctx->uring_lock);
6713 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6714 for (i = 0; i < ctx->nr_user_files; i++) {
6715 struct fixed_file_table *table;
6716 struct file *f;
6717
6718 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6719 f = table->files[i & IORING_FILE_TABLE_MASK];
6720 if (f)
6721 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6722 else
6723 seq_printf(m, "%5u: <none>\n", i);
6724 }
6725 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6726 for (i = 0; i < ctx->nr_user_bufs; i++) {
6727 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6728
6729 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6730 (unsigned int) buf->len);
6731 }
6732 if (!idr_is_empty(&ctx->personality_idr)) {
6733 seq_printf(m, "Personalities:\n");
6734 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6735 }
6736 mutex_unlock(&ctx->uring_lock);
6737}
6738
6739static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6740{
6741 struct io_ring_ctx *ctx = f->private_data;
6742
6743 if (percpu_ref_tryget(&ctx->refs)) {
6744 __io_uring_show_fdinfo(ctx, m);
6745 percpu_ref_put(&ctx->refs);
6746 }
6747}
Tobias Klauserbebdb652020-02-26 18:38:32 +01006748#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07006749
Jens Axboe2b188cc2019-01-07 10:46:33 -07006750static const struct file_operations io_uring_fops = {
6751 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006752 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006753 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006754#ifndef CONFIG_MMU
6755 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6756 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6757#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006758 .poll = io_uring_poll,
6759 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006760#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006761 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006762#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006763};
6764
6765static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6766 struct io_uring_params *p)
6767{
Hristo Venev75b28af2019-08-26 17:23:46 +00006768 struct io_rings *rings;
6769 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006770
Hristo Venev75b28af2019-08-26 17:23:46 +00006771 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6772 if (size == SIZE_MAX)
6773 return -EOVERFLOW;
6774
6775 rings = io_mem_alloc(size);
6776 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006777 return -ENOMEM;
6778
Hristo Venev75b28af2019-08-26 17:23:46 +00006779 ctx->rings = rings;
6780 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6781 rings->sq_ring_mask = p->sq_entries - 1;
6782 rings->cq_ring_mask = p->cq_entries - 1;
6783 rings->sq_ring_entries = p->sq_entries;
6784 rings->cq_ring_entries = p->cq_entries;
6785 ctx->sq_mask = rings->sq_ring_mask;
6786 ctx->cq_mask = rings->cq_ring_mask;
6787 ctx->sq_entries = rings->sq_ring_entries;
6788 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006789
6790 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006791 if (size == SIZE_MAX) {
6792 io_mem_free(ctx->rings);
6793 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006794 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006795 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006796
6797 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006798 if (!ctx->sq_sqes) {
6799 io_mem_free(ctx->rings);
6800 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006801 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006802 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006803
Jens Axboe2b188cc2019-01-07 10:46:33 -07006804 return 0;
6805}
6806
6807/*
6808 * Allocate an anonymous fd, this is what constitutes the application
6809 * visible backing of an io_uring instance. The application mmaps this
6810 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6811 * we have to tie this fd to a socket for file garbage collection purposes.
6812 */
6813static int io_uring_get_fd(struct io_ring_ctx *ctx)
6814{
6815 struct file *file;
6816 int ret;
6817
6818#if defined(CONFIG_UNIX)
6819 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6820 &ctx->ring_sock);
6821 if (ret)
6822 return ret;
6823#endif
6824
6825 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6826 if (ret < 0)
6827 goto err;
6828
6829 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6830 O_RDWR | O_CLOEXEC);
6831 if (IS_ERR(file)) {
6832 put_unused_fd(ret);
6833 ret = PTR_ERR(file);
6834 goto err;
6835 }
6836
6837#if defined(CONFIG_UNIX)
6838 ctx->ring_sock->file = file;
6839#endif
6840 fd_install(ret, file);
6841 return ret;
6842err:
6843#if defined(CONFIG_UNIX)
6844 sock_release(ctx->ring_sock);
6845 ctx->ring_sock = NULL;
6846#endif
6847 return ret;
6848}
6849
6850static int io_uring_create(unsigned entries, struct io_uring_params *p)
6851{
6852 struct user_struct *user = NULL;
6853 struct io_ring_ctx *ctx;
6854 bool account_mem;
6855 int ret;
6856
Jens Axboe8110c1a2019-12-28 15:39:54 -07006857 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006858 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006859 if (entries > IORING_MAX_ENTRIES) {
6860 if (!(p->flags & IORING_SETUP_CLAMP))
6861 return -EINVAL;
6862 entries = IORING_MAX_ENTRIES;
6863 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006864
6865 /*
6866 * Use twice as many entries for the CQ ring. It's possible for the
6867 * application to drive a higher depth than the size of the SQ ring,
6868 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006869 * some flexibility in overcommitting a bit. If the application has
6870 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6871 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006872 */
6873 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006874 if (p->flags & IORING_SETUP_CQSIZE) {
6875 /*
6876 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6877 * to a power-of-two, if it isn't already. We do NOT impose
6878 * any cq vs sq ring sizing.
6879 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006880 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006881 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006882 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6883 if (!(p->flags & IORING_SETUP_CLAMP))
6884 return -EINVAL;
6885 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6886 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006887 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6888 } else {
6889 p->cq_entries = 2 * p->sq_entries;
6890 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006891
6892 user = get_uid(current_user());
6893 account_mem = !capable(CAP_IPC_LOCK);
6894
6895 if (account_mem) {
6896 ret = io_account_mem(user,
6897 ring_pages(p->sq_entries, p->cq_entries));
6898 if (ret) {
6899 free_uid(user);
6900 return ret;
6901 }
6902 }
6903
6904 ctx = io_ring_ctx_alloc(p);
6905 if (!ctx) {
6906 if (account_mem)
6907 io_unaccount_mem(user, ring_pages(p->sq_entries,
6908 p->cq_entries));
6909 free_uid(user);
6910 return -ENOMEM;
6911 }
6912 ctx->compat = in_compat_syscall();
6913 ctx->account_mem = account_mem;
6914 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006915 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006916
6917 ret = io_allocate_scq_urings(ctx, p);
6918 if (ret)
6919 goto err;
6920
Jens Axboe6c271ce2019-01-10 11:22:30 -07006921 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006922 if (ret)
6923 goto err;
6924
Jens Axboe2b188cc2019-01-07 10:46:33 -07006925 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006926 p->sq_off.head = offsetof(struct io_rings, sq.head);
6927 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6928 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6929 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6930 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6931 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6932 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006933
6934 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006935 p->cq_off.head = offsetof(struct io_rings, cq.head);
6936 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6937 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6938 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6939 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6940 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006941
Jens Axboe044c1ab2019-10-28 09:15:33 -06006942 /*
6943 * Install ring fd as the very last thing, so we don't risk someone
6944 * having closed it before we finish setup
6945 */
6946 ret = io_uring_get_fd(ctx);
6947 if (ret < 0)
6948 goto err;
6949
Jens Axboeda8c9692019-12-02 18:51:26 -07006950 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006951 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6952 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006953 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006954 return ret;
6955err:
6956 io_ring_ctx_wait_and_kill(ctx);
6957 return ret;
6958}
6959
6960/*
6961 * Sets up an aio uring context, and returns the fd. Applications asks for a
6962 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6963 * params structure passed in.
6964 */
6965static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6966{
6967 struct io_uring_params p;
6968 long ret;
6969 int i;
6970
6971 if (copy_from_user(&p, params, sizeof(p)))
6972 return -EFAULT;
6973 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6974 if (p.resv[i])
6975 return -EINVAL;
6976 }
6977
Jens Axboe6c271ce2019-01-10 11:22:30 -07006978 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006979 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006980 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006981 return -EINVAL;
6982
6983 ret = io_uring_create(entries, &p);
6984 if (ret < 0)
6985 return ret;
6986
6987 if (copy_to_user(params, &p, sizeof(p)))
6988 return -EFAULT;
6989
6990 return ret;
6991}
6992
6993SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6994 struct io_uring_params __user *, params)
6995{
6996 return io_uring_setup(entries, params);
6997}
6998
Jens Axboe66f4af92020-01-16 15:36:52 -07006999static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7000{
7001 struct io_uring_probe *p;
7002 size_t size;
7003 int i, ret;
7004
7005 size = struct_size(p, ops, nr_args);
7006 if (size == SIZE_MAX)
7007 return -EOVERFLOW;
7008 p = kzalloc(size, GFP_KERNEL);
7009 if (!p)
7010 return -ENOMEM;
7011
7012 ret = -EFAULT;
7013 if (copy_from_user(p, arg, size))
7014 goto out;
7015 ret = -EINVAL;
7016 if (memchr_inv(p, 0, size))
7017 goto out;
7018
7019 p->last_op = IORING_OP_LAST - 1;
7020 if (nr_args > IORING_OP_LAST)
7021 nr_args = IORING_OP_LAST;
7022
7023 for (i = 0; i < nr_args; i++) {
7024 p->ops[i].op = i;
7025 if (!io_op_defs[i].not_supported)
7026 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7027 }
7028 p->ops_len = i;
7029
7030 ret = 0;
7031 if (copy_to_user(arg, p, size))
7032 ret = -EFAULT;
7033out:
7034 kfree(p);
7035 return ret;
7036}
7037
Jens Axboe071698e2020-01-28 10:04:42 -07007038static int io_register_personality(struct io_ring_ctx *ctx)
7039{
7040 const struct cred *creds = get_current_cred();
7041 int id;
7042
7043 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7044 USHRT_MAX, GFP_KERNEL);
7045 if (id < 0)
7046 put_cred(creds);
7047 return id;
7048}
7049
7050static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7051{
7052 const struct cred *old_creds;
7053
7054 old_creds = idr_remove(&ctx->personality_idr, id);
7055 if (old_creds) {
7056 put_cred(old_creds);
7057 return 0;
7058 }
7059
7060 return -EINVAL;
7061}
7062
7063static bool io_register_op_must_quiesce(int op)
7064{
7065 switch (op) {
7066 case IORING_UNREGISTER_FILES:
7067 case IORING_REGISTER_FILES_UPDATE:
7068 case IORING_REGISTER_PROBE:
7069 case IORING_REGISTER_PERSONALITY:
7070 case IORING_UNREGISTER_PERSONALITY:
7071 return false;
7072 default:
7073 return true;
7074 }
7075}
7076
Jens Axboeedafcce2019-01-09 09:16:05 -07007077static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7078 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007079 __releases(ctx->uring_lock)
7080 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007081{
7082 int ret;
7083
Jens Axboe35fa71a2019-04-22 10:23:23 -06007084 /*
7085 * We're inside the ring mutex, if the ref is already dying, then
7086 * someone else killed the ctx or is already going through
7087 * io_uring_register().
7088 */
7089 if (percpu_ref_is_dying(&ctx->refs))
7090 return -ENXIO;
7091
Jens Axboe071698e2020-01-28 10:04:42 -07007092 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007093 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007094
Jens Axboe05f3fb32019-12-09 11:22:50 -07007095 /*
7096 * Drop uring mutex before waiting for references to exit. If
7097 * another thread is currently inside io_uring_enter() it might
7098 * need to grab the uring_lock to make progress. If we hold it
7099 * here across the drain wait, then we can deadlock. It's safe
7100 * to drop the mutex here, since no new references will come in
7101 * after we've killed the percpu ref.
7102 */
7103 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007104 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007105 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007106 if (ret) {
7107 percpu_ref_resurrect(&ctx->refs);
7108 ret = -EINTR;
7109 goto out;
7110 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007111 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007112
7113 switch (opcode) {
7114 case IORING_REGISTER_BUFFERS:
7115 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7116 break;
7117 case IORING_UNREGISTER_BUFFERS:
7118 ret = -EINVAL;
7119 if (arg || nr_args)
7120 break;
7121 ret = io_sqe_buffer_unregister(ctx);
7122 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007123 case IORING_REGISTER_FILES:
7124 ret = io_sqe_files_register(ctx, arg, nr_args);
7125 break;
7126 case IORING_UNREGISTER_FILES:
7127 ret = -EINVAL;
7128 if (arg || nr_args)
7129 break;
7130 ret = io_sqe_files_unregister(ctx);
7131 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007132 case IORING_REGISTER_FILES_UPDATE:
7133 ret = io_sqe_files_update(ctx, arg, nr_args);
7134 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007135 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007136 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007137 ret = -EINVAL;
7138 if (nr_args != 1)
7139 break;
7140 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007141 if (ret)
7142 break;
7143 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7144 ctx->eventfd_async = 1;
7145 else
7146 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007147 break;
7148 case IORING_UNREGISTER_EVENTFD:
7149 ret = -EINVAL;
7150 if (arg || nr_args)
7151 break;
7152 ret = io_eventfd_unregister(ctx);
7153 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007154 case IORING_REGISTER_PROBE:
7155 ret = -EINVAL;
7156 if (!arg || nr_args > 256)
7157 break;
7158 ret = io_probe(ctx, arg, nr_args);
7159 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007160 case IORING_REGISTER_PERSONALITY:
7161 ret = -EINVAL;
7162 if (arg || nr_args)
7163 break;
7164 ret = io_register_personality(ctx);
7165 break;
7166 case IORING_UNREGISTER_PERSONALITY:
7167 ret = -EINVAL;
7168 if (arg)
7169 break;
7170 ret = io_unregister_personality(ctx, nr_args);
7171 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007172 default:
7173 ret = -EINVAL;
7174 break;
7175 }
7176
Jens Axboe071698e2020-01-28 10:04:42 -07007177 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007178 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007179 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007180out:
7181 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007182 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007183 return ret;
7184}
7185
7186SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7187 void __user *, arg, unsigned int, nr_args)
7188{
7189 struct io_ring_ctx *ctx;
7190 long ret = -EBADF;
7191 struct fd f;
7192
7193 f = fdget(fd);
7194 if (!f.file)
7195 return -EBADF;
7196
7197 ret = -EOPNOTSUPP;
7198 if (f.file->f_op != &io_uring_fops)
7199 goto out_fput;
7200
7201 ctx = f.file->private_data;
7202
7203 mutex_lock(&ctx->uring_lock);
7204 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7205 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007206 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7207 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007208out_fput:
7209 fdput(f);
7210 return ret;
7211}
7212
Jens Axboe2b188cc2019-01-07 10:46:33 -07007213static int __init io_uring_init(void)
7214{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007215#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7216 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7217 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7218} while (0)
7219
7220#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7221 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7222 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7223 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7224 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7225 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7226 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7227 BUILD_BUG_SQE_ELEM(8, __u64, off);
7228 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7229 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7230 BUILD_BUG_SQE_ELEM(24, __u32, len);
7231 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7232 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7233 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7234 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7235 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7236 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7237 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7238 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7239 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7240 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7241 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7242 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7243 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7244 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7245 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7246 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7247
Jens Axboed3656342019-12-18 09:50:26 -07007248 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007249 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7250 return 0;
7251};
7252__initcall(io_uring_init);