blob: 407ba3388e1433d4323cb86ce259da6d6a5269d8 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070059#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070073#include <linux/namei.h>
74#include <linux/fsnotify.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070075
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020076#define CREATE_TRACE_POINTS
77#include <trace/events/io_uring.h>
78
Jens Axboe2b188cc2019-01-07 10:46:33 -070079#include <uapi/linux/io_uring.h>
80
81#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060082#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070083
Daniel Xu5277dea2019-09-14 14:23:45 -070084#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060085#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060086
87/*
88 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
89 */
90#define IORING_FILE_TABLE_SHIFT 9
91#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
92#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
93#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070094
95struct io_uring {
96 u32 head ____cacheline_aligned_in_smp;
97 u32 tail ____cacheline_aligned_in_smp;
98};
99
Stefan Bühler1e84b972019-04-24 23:54:16 +0200100/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000101 * This data is shared with the application through the mmap at offsets
102 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200103 *
104 * The offsets to the member fields are published through struct
105 * io_sqring_offsets when calling io_uring_setup.
106 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000107struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200108 /*
109 * Head and tail offsets into the ring; the offsets need to be
110 * masked to get valid indices.
111 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000112 * The kernel controls head of the sq ring and the tail of the cq ring,
113 * and the application controls tail of the sq ring and the head of the
114 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200117 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 * ring_entries - 1)
120 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000121 u32 sq_ring_mask, cq_ring_mask;
122 /* Ring sizes (constant, power of 2) */
123 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
125 * Number of invalid entries dropped by the kernel due to
126 * invalid index stored in array
127 *
128 * Written by the kernel, shouldn't be modified by the
129 * application (i.e. get number of "new events" by comparing to
130 * cached value).
131 *
132 * After a new SQ head value was read by the application this
133 * counter includes all submissions that were dropped reaching
134 * the new SQ head (and possibly more).
135 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000136 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200137 /*
138 * Runtime flags
139 *
140 * Written by the kernel, shouldn't be modified by the
141 * application.
142 *
143 * The application needs a full memory barrier before checking
144 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200147 /*
148 * Number of completion events lost because the queue was full;
149 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800150 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 * the completion queue.
152 *
153 * Written by the kernel, shouldn't be modified by the
154 * application (i.e. get number of "new events" by comparing to
155 * cached value).
156 *
157 * As completion events come in out of order this counter is not
158 * ordered with any other data.
159 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000160 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200161 /*
162 * Ring buffer of completion events.
163 *
164 * The kernel writes completion events fresh every time they are
165 * produced, so the application is allowed to modify pending
166 * entries.
167 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000168 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700169};
170
Jens Axboeedafcce2019-01-09 09:16:05 -0700171struct io_mapped_ubuf {
172 u64 ubuf;
173 size_t len;
174 struct bio_vec *bvec;
175 unsigned int nr_bvecs;
176};
177
Jens Axboe65e19f52019-10-26 07:20:21 -0600178struct fixed_file_table {
179 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700180};
181
Jens Axboe05f3fb32019-12-09 11:22:50 -0700182enum {
183 FFD_F_ATOMIC,
184};
185
186struct 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;
192 unsigned long state;
193 struct work_struct ref_work;
194 struct completion done;
195};
196
Jens Axboe2b188cc2019-01-07 10:46:33 -0700197struct io_ring_ctx {
198 struct {
199 struct percpu_ref refs;
200 } ____cacheline_aligned_in_smp;
201
202 struct {
203 unsigned int flags;
204 bool compat;
205 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700206 bool cq_overflow_flushed;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300207 bool drain_next;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208
Hristo Venev75b28af2019-08-26 17:23:46 +0000209 /*
210 * Ring buffer of indices into array of io_uring_sqe, which is
211 * mmapped by the application using the IORING_OFF_SQES offset.
212 *
213 * This indirection could e.g. be used to assign fixed
214 * io_uring_sqe entries to operations and only submit them to
215 * the queue when needed.
216 *
217 * The kernel modifies neither the indices array nor the entries
218 * array.
219 */
220 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 unsigned cached_sq_head;
222 unsigned sq_entries;
223 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700224 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600225 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700226 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700227 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600228
229 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600230 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700231 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700232
Jens Axboefcb323c2019-10-24 12:39:47 -0600233 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700234 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700235 } ____cacheline_aligned_in_smp;
236
Hristo Venev75b28af2019-08-26 17:23:46 +0000237 struct io_rings *rings;
238
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600240 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241 struct task_struct *sqo_thread; /* if using sq thread polling */
242 struct mm_struct *sqo_mm;
243 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700244
Jens Axboe6b063142019-01-10 22:13:58 -0700245 /*
246 * If used, fixed file set. Writers must ensure that ->refs is dead,
247 * readers must ensure that ->refs is alive as long as the file* is
248 * used. Only updated through io_uring_register(2).
249 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700250 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700251 unsigned nr_user_files;
252
Jens Axboeedafcce2019-01-09 09:16:05 -0700253 /* if used, fixed mapped user buffers */
254 unsigned nr_user_bufs;
255 struct io_mapped_ubuf *user_bufs;
256
Jens Axboe2b188cc2019-01-07 10:46:33 -0700257 struct user_struct *user;
258
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700259 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700260
Jens Axboe206aefd2019-11-07 18:27:42 -0700261 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
262 struct completion *completions;
263
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700264 /* if all else fails... */
265 struct io_kiocb *fallback_req;
266
Jens Axboe206aefd2019-11-07 18:27:42 -0700267#if defined(CONFIG_UNIX)
268 struct socket *ring_sock;
269#endif
270
271 struct {
272 unsigned cached_cq_tail;
273 unsigned cq_entries;
274 unsigned cq_mask;
275 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700276 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700277 struct wait_queue_head cq_wait;
278 struct fasync_struct *cq_fasync;
279 struct eventfd_ctx *cq_ev_fd;
280 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700281
282 struct {
283 struct mutex uring_lock;
284 wait_queue_head_t wait;
285 } ____cacheline_aligned_in_smp;
286
287 struct {
288 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700289 struct llist_head poll_llist;
290
Jens Axboedef596e2019-01-09 08:59:42 -0700291 /*
292 * ->poll_list is protected by the ctx->uring_lock for
293 * io_uring instances that don't use IORING_SETUP_SQPOLL.
294 * For SQPOLL, only the single threaded io_sq_thread() will
295 * manipulate the list, hence no extra locking is needed there.
296 */
297 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700298 struct hlist_head *cancel_hash;
299 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700300 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600301
302 spinlock_t inflight_lock;
303 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700304 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700305};
306
Jens Axboe09bb8392019-03-13 12:39:28 -0600307/*
308 * First field must be the file pointer in all the
309 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
310 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700311struct io_poll_iocb {
312 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700313 union {
314 struct wait_queue_head *head;
315 u64 addr;
316 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700317 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600318 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700319 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700320 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700321};
322
Jens Axboeb5dba592019-12-11 14:02:38 -0700323struct io_close {
324 struct file *file;
325 struct file *put_file;
326 int fd;
327};
328
Jens Axboead8a48a2019-11-15 08:49:11 -0700329struct io_timeout_data {
330 struct io_kiocb *req;
331 struct hrtimer timer;
332 struct timespec64 ts;
333 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300334 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700335};
336
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700337struct io_accept {
338 struct file *file;
339 struct sockaddr __user *addr;
340 int __user *addr_len;
341 int flags;
342};
343
344struct io_sync {
345 struct file *file;
346 loff_t len;
347 loff_t off;
348 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700349 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700350};
351
Jens Axboefbf23842019-12-17 18:45:56 -0700352struct io_cancel {
353 struct file *file;
354 u64 addr;
355};
356
Jens Axboeb29472e2019-12-17 18:50:29 -0700357struct io_timeout {
358 struct file *file;
359 u64 addr;
360 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700361 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700362};
363
Jens Axboe9adbd452019-12-20 08:45:55 -0700364struct io_rw {
365 /* NOTE: kiocb has the file as the first member, so don't do it here */
366 struct kiocb kiocb;
367 u64 addr;
368 u64 len;
369};
370
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700371struct io_connect {
372 struct file *file;
373 struct sockaddr __user *addr;
374 int addr_len;
375};
376
Jens Axboee47293f2019-12-20 08:58:21 -0700377struct io_sr_msg {
378 struct file *file;
379 struct user_msghdr __user *msg;
380 int msg_flags;
381};
382
Jens Axboe15b71ab2019-12-11 11:20:36 -0700383struct io_open {
384 struct file *file;
385 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700386 union {
387 umode_t mode;
388 unsigned mask;
389 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700390 const char __user *fname;
391 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700392 struct statx __user *buffer;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700393 int flags;
394};
395
Jens Axboe05f3fb32019-12-09 11:22:50 -0700396struct io_files_update {
397 struct file *file;
398 u64 arg;
399 u32 nr_args;
400 u32 offset;
401};
402
Jens Axboef499a022019-12-02 16:28:46 -0700403struct io_async_connect {
404 struct sockaddr_storage address;
405};
406
Jens Axboe03b12302019-12-02 18:50:25 -0700407struct io_async_msghdr {
408 struct iovec fast_iov[UIO_FASTIOV];
409 struct iovec *iov;
410 struct sockaddr __user *uaddr;
411 struct msghdr msg;
412};
413
Jens Axboef67676d2019-12-02 11:03:47 -0700414struct io_async_rw {
415 struct iovec fast_iov[UIO_FASTIOV];
416 struct iovec *iov;
417 ssize_t nr_segs;
418 ssize_t size;
419};
420
Jens Axboe15b71ab2019-12-11 11:20:36 -0700421struct io_async_open {
422 struct filename *filename;
423};
424
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700425struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700426 union {
427 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700428 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700429 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700430 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700431 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700432 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700433};
434
Jens Axboe09bb8392019-03-13 12:39:28 -0600435/*
436 * NOTE! Each of the iocb union members has the file pointer
437 * as the first entry in their struct definition. So you can
438 * access the file pointer through any of the sub-structs,
439 * or directly as just 'ki_filp' in this struct.
440 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700441struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700442 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600443 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700444 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700445 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700446 struct io_accept accept;
447 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700448 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700449 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700450 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700451 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700452 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700453 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700454 struct io_files_update files_update;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700455 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700456
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700457 struct io_async_ctx *io;
Jens Axboee94f1412019-12-19 12:06:02 -0700458 union {
459 /*
460 * ring_file is only used in the submission path, and
461 * llist_node is only used for poll deferred completions
462 */
463 struct file *ring_file;
464 struct llist_node llist_node;
465 };
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300466 int ring_fd;
467 bool has_user;
468 bool in_async;
469 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700470 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700471
472 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700473 union {
474 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700475 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700476 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600477 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700478 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700479 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200480#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700481#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700482#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700483#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200484#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
485#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600486#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700487#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800488#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300489#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600490#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600491#define REQ_F_ISREG 2048 /* regular file */
492#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700493#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800494#define REQ_F_INFLIGHT 16384 /* on inflight list */
495#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700496#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboece35a472019-12-17 08:04:44 -0700497#define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700498 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600499 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600500 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700501
Jens Axboefcb323c2019-10-24 12:39:47 -0600502 struct list_head inflight_entry;
503
Jens Axboe561fb042019-10-24 07:25:42 -0600504 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700505};
506
507#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700508#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700509
Jens Axboe9a56a232019-01-09 09:06:50 -0700510struct io_submit_state {
511 struct blk_plug plug;
512
513 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700514 * io_kiocb alloc cache
515 */
516 void *reqs[IO_IOPOLL_BATCH];
517 unsigned int free_reqs;
518 unsigned int cur_req;
519
520 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700521 * File reference cache
522 */
523 struct file *file;
524 unsigned int fd;
525 unsigned int has_refs;
526 unsigned int used_refs;
527 unsigned int ios_left;
528};
529
Jens Axboed3656342019-12-18 09:50:26 -0700530struct io_op_def {
531 /* needs req->io allocated for deferral/async */
532 unsigned async_ctx : 1;
533 /* needs current->mm setup, does mm access */
534 unsigned needs_mm : 1;
535 /* needs req->file assigned */
536 unsigned needs_file : 1;
537 /* needs req->file assigned IFF fd is >= 0 */
538 unsigned fd_non_neg : 1;
539 /* hash wq insertion if file is a regular file */
540 unsigned hash_reg_file : 1;
541 /* unbound wq insertion if file is a non-regular file */
542 unsigned unbound_nonreg_file : 1;
543};
544
545static const struct io_op_def io_op_defs[] = {
546 {
547 /* IORING_OP_NOP */
548 },
549 {
550 /* IORING_OP_READV */
551 .async_ctx = 1,
552 .needs_mm = 1,
553 .needs_file = 1,
554 .unbound_nonreg_file = 1,
555 },
556 {
557 /* IORING_OP_WRITEV */
558 .async_ctx = 1,
559 .needs_mm = 1,
560 .needs_file = 1,
561 .hash_reg_file = 1,
562 .unbound_nonreg_file = 1,
563 },
564 {
565 /* IORING_OP_FSYNC */
566 .needs_file = 1,
567 },
568 {
569 /* IORING_OP_READ_FIXED */
570 .needs_file = 1,
571 .unbound_nonreg_file = 1,
572 },
573 {
574 /* IORING_OP_WRITE_FIXED */
575 .needs_file = 1,
576 .hash_reg_file = 1,
577 .unbound_nonreg_file = 1,
578 },
579 {
580 /* IORING_OP_POLL_ADD */
581 .needs_file = 1,
582 .unbound_nonreg_file = 1,
583 },
584 {
585 /* IORING_OP_POLL_REMOVE */
586 },
587 {
588 /* IORING_OP_SYNC_FILE_RANGE */
589 .needs_file = 1,
590 },
591 {
592 /* IORING_OP_SENDMSG */
593 .async_ctx = 1,
594 .needs_mm = 1,
595 .needs_file = 1,
596 .unbound_nonreg_file = 1,
597 },
598 {
599 /* IORING_OP_RECVMSG */
600 .async_ctx = 1,
601 .needs_mm = 1,
602 .needs_file = 1,
603 .unbound_nonreg_file = 1,
604 },
605 {
606 /* IORING_OP_TIMEOUT */
607 .async_ctx = 1,
608 .needs_mm = 1,
609 },
610 {
611 /* IORING_OP_TIMEOUT_REMOVE */
612 },
613 {
614 /* IORING_OP_ACCEPT */
615 .needs_mm = 1,
616 .needs_file = 1,
617 .unbound_nonreg_file = 1,
618 },
619 {
620 /* IORING_OP_ASYNC_CANCEL */
621 },
622 {
623 /* IORING_OP_LINK_TIMEOUT */
624 .async_ctx = 1,
625 .needs_mm = 1,
626 },
627 {
628 /* IORING_OP_CONNECT */
629 .async_ctx = 1,
630 .needs_mm = 1,
631 .needs_file = 1,
632 .unbound_nonreg_file = 1,
633 },
634 {
635 /* IORING_OP_FALLOCATE */
636 .needs_file = 1,
637 },
638 {
639 /* IORING_OP_OPENAT */
640 .needs_file = 1,
641 .fd_non_neg = 1,
642 },
643 {
644 /* IORING_OP_CLOSE */
645 .needs_file = 1,
646 },
647 {
648 /* IORING_OP_FILES_UPDATE */
649 .needs_mm = 1,
650 },
651 {
652 /* IORING_OP_STATX */
653 .needs_mm = 1,
654 .needs_file = 1,
655 .fd_non_neg = 1,
656 },
Jens Axboe3a6820f2019-12-22 15:19:35 -0700657 {
658 /* IORING_OP_READ */
659 .needs_mm = 1,
660 .needs_file = 1,
661 .unbound_nonreg_file = 1,
662 },
663 {
664 /* IORING_OP_WRITE */
665 .needs_mm = 1,
666 .needs_file = 1,
667 .unbound_nonreg_file = 1,
668 },
Jens Axboed3656342019-12-18 09:50:26 -0700669};
670
Jens Axboe561fb042019-10-24 07:25:42 -0600671static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700672static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800673static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700674static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700675static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
676static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700677static int __io_sqe_files_update(struct io_ring_ctx *ctx,
678 struct io_uring_files_update *ip,
679 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600680
Jens Axboe2b188cc2019-01-07 10:46:33 -0700681static struct kmem_cache *req_cachep;
682
683static const struct file_operations io_uring_fops;
684
685struct sock *io_uring_get_socket(struct file *file)
686{
687#if defined(CONFIG_UNIX)
688 if (file->f_op == &io_uring_fops) {
689 struct io_ring_ctx *ctx = file->private_data;
690
691 return ctx->ring_sock->sk;
692 }
693#endif
694 return NULL;
695}
696EXPORT_SYMBOL(io_uring_get_socket);
697
698static void io_ring_ctx_ref_free(struct percpu_ref *ref)
699{
700 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
701
Jens Axboe206aefd2019-11-07 18:27:42 -0700702 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700703}
704
705static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
706{
707 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700708 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700709
710 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
711 if (!ctx)
712 return NULL;
713
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700714 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
715 if (!ctx->fallback_req)
716 goto err;
717
Jens Axboe206aefd2019-11-07 18:27:42 -0700718 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
719 if (!ctx->completions)
720 goto err;
721
Jens Axboe78076bb2019-12-04 19:56:40 -0700722 /*
723 * Use 5 bits less than the max cq entries, that should give us around
724 * 32 entries per hash list if totally full and uniformly spread.
725 */
726 hash_bits = ilog2(p->cq_entries);
727 hash_bits -= 5;
728 if (hash_bits <= 0)
729 hash_bits = 1;
730 ctx->cancel_hash_bits = hash_bits;
731 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
732 GFP_KERNEL);
733 if (!ctx->cancel_hash)
734 goto err;
735 __hash_init(ctx->cancel_hash, 1U << hash_bits);
736
Roman Gushchin21482892019-05-07 10:01:48 -0700737 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700738 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
739 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700740
741 ctx->flags = p->flags;
742 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700743 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700744 init_completion(&ctx->completions[0]);
745 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700746 mutex_init(&ctx->uring_lock);
747 init_waitqueue_head(&ctx->wait);
748 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700749 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700750 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600751 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600752 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600753 init_waitqueue_head(&ctx->inflight_wait);
754 spin_lock_init(&ctx->inflight_lock);
755 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700756 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700757err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700758 if (ctx->fallback_req)
759 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700760 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700761 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700762 kfree(ctx);
763 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700764}
765
Bob Liu9d858b22019-11-13 18:06:25 +0800766static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600767{
Jackie Liua197f662019-11-08 08:09:12 -0700768 struct io_ring_ctx *ctx = req->ctx;
769
Jens Axboe498ccd92019-10-25 10:04:25 -0600770 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
771 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600772}
773
Bob Liu9d858b22019-11-13 18:06:25 +0800774static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600775{
Bob Liu9d858b22019-11-13 18:06:25 +0800776 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
777 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600778
Bob Liu9d858b22019-11-13 18:06:25 +0800779 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600780}
781
782static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600783{
784 struct io_kiocb *req;
785
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600786 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800787 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600788 list_del_init(&req->list);
789 return req;
790 }
791
792 return NULL;
793}
794
Jens Axboe5262f562019-09-17 12:26:57 -0600795static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
796{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600797 struct io_kiocb *req;
798
799 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700800 if (req) {
801 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
802 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800803 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700804 list_del_init(&req->list);
805 return req;
806 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600807 }
808
809 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600810}
811
Jens Axboede0617e2019-04-06 21:51:27 -0600812static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700813{
Hristo Venev75b28af2019-08-26 17:23:46 +0000814 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700815
Hristo Venev75b28af2019-08-26 17:23:46 +0000816 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700817 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000818 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700819
Jens Axboe2b188cc2019-01-07 10:46:33 -0700820 if (wq_has_sleeper(&ctx->cq_wait)) {
821 wake_up_interruptible(&ctx->cq_wait);
822 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
823 }
824 }
825}
826
Jens Axboe94ae5e72019-11-14 19:39:52 -0700827static inline bool io_prep_async_work(struct io_kiocb *req,
828 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600829{
Jens Axboed3656342019-12-18 09:50:26 -0700830 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600831 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600832
Jens Axboed3656342019-12-18 09:50:26 -0700833 if (req->flags & REQ_F_ISREG) {
834 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700835 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700836 } else {
837 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700838 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600839 }
Jens Axboed3656342019-12-18 09:50:26 -0700840 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700841 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600842
Jens Axboe94ae5e72019-11-14 19:39:52 -0700843 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600844 return do_hashed;
845}
846
Jackie Liua197f662019-11-08 08:09:12 -0700847static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600848{
Jackie Liua197f662019-11-08 08:09:12 -0700849 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700850 struct io_kiocb *link;
851 bool do_hashed;
852
853 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600854
855 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
856 req->flags);
857 if (!do_hashed) {
858 io_wq_enqueue(ctx->io_wq, &req->work);
859 } else {
860 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
861 file_inode(req->file));
862 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700863
864 if (link)
865 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600866}
867
Jens Axboe5262f562019-09-17 12:26:57 -0600868static void io_kill_timeout(struct io_kiocb *req)
869{
870 int ret;
871
Jens Axboe2d283902019-12-04 11:08:05 -0700872 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600873 if (ret != -1) {
874 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600875 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700876 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800877 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600878 }
879}
880
881static void io_kill_timeouts(struct io_ring_ctx *ctx)
882{
883 struct io_kiocb *req, *tmp;
884
885 spin_lock_irq(&ctx->completion_lock);
886 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
887 io_kill_timeout(req);
888 spin_unlock_irq(&ctx->completion_lock);
889}
890
Jens Axboede0617e2019-04-06 21:51:27 -0600891static void io_commit_cqring(struct io_ring_ctx *ctx)
892{
893 struct io_kiocb *req;
894
Jens Axboe5262f562019-09-17 12:26:57 -0600895 while ((req = io_get_timeout_req(ctx)) != NULL)
896 io_kill_timeout(req);
897
Jens Axboede0617e2019-04-06 21:51:27 -0600898 __io_commit_cqring(ctx);
899
900 while ((req = io_get_deferred_req(ctx)) != NULL) {
901 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700902 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600903 }
904}
905
Jens Axboe2b188cc2019-01-07 10:46:33 -0700906static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
907{
Hristo Venev75b28af2019-08-26 17:23:46 +0000908 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700909 unsigned tail;
910
911 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200912 /*
913 * writes to the cq entry need to come after reading head; the
914 * control dependency is enough as we're using WRITE_ONCE to
915 * fill the cq entry
916 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000917 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700918 return NULL;
919
920 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000921 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700922}
923
Jens Axboe8c838782019-03-12 15:48:16 -0600924static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
925{
926 if (waitqueue_active(&ctx->wait))
927 wake_up(&ctx->wait);
928 if (waitqueue_active(&ctx->sqo_wait))
929 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600930 if (ctx->cq_ev_fd)
931 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600932}
933
Jens Axboec4a2ed72019-11-21 21:01:26 -0700934/* Returns true if there are no backlogged entries after the flush */
935static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700936{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700937 struct io_rings *rings = ctx->rings;
938 struct io_uring_cqe *cqe;
939 struct io_kiocb *req;
940 unsigned long flags;
941 LIST_HEAD(list);
942
943 if (!force) {
944 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700945 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700946 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
947 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700948 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700949 }
950
951 spin_lock_irqsave(&ctx->completion_lock, flags);
952
953 /* if force is set, the ring is going away. always drop after that */
954 if (force)
955 ctx->cq_overflow_flushed = true;
956
Jens Axboec4a2ed72019-11-21 21:01:26 -0700957 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700958 while (!list_empty(&ctx->cq_overflow_list)) {
959 cqe = io_get_cqring(ctx);
960 if (!cqe && !force)
961 break;
962
963 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
964 list);
965 list_move(&req->list, &list);
966 if (cqe) {
967 WRITE_ONCE(cqe->user_data, req->user_data);
968 WRITE_ONCE(cqe->res, req->result);
969 WRITE_ONCE(cqe->flags, 0);
970 } else {
971 WRITE_ONCE(ctx->rings->cq_overflow,
972 atomic_inc_return(&ctx->cached_cq_overflow));
973 }
974 }
975
976 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -0700977 if (cqe) {
978 clear_bit(0, &ctx->sq_check_overflow);
979 clear_bit(0, &ctx->cq_check_overflow);
980 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700981 spin_unlock_irqrestore(&ctx->completion_lock, flags);
982 io_cqring_ev_posted(ctx);
983
984 while (!list_empty(&list)) {
985 req = list_first_entry(&list, struct io_kiocb, list);
986 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800987 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700988 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700989
990 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700991}
992
Jens Axboe78e19bb2019-11-06 15:21:34 -0700993static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700994{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700995 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700996 struct io_uring_cqe *cqe;
997
Jens Axboe78e19bb2019-11-06 15:21:34 -0700998 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700999
Jens Axboe2b188cc2019-01-07 10:46:33 -07001000 /*
1001 * If we can't get a cq entry, userspace overflowed the
1002 * submission (by quite a lot). Increment the overflow count in
1003 * the ring.
1004 */
1005 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001006 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001007 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001008 WRITE_ONCE(cqe->res, res);
1009 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001010 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001011 WRITE_ONCE(ctx->rings->cq_overflow,
1012 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001013 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001014 if (list_empty(&ctx->cq_overflow_list)) {
1015 set_bit(0, &ctx->sq_check_overflow);
1016 set_bit(0, &ctx->cq_check_overflow);
1017 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001018 refcount_inc(&req->refs);
1019 req->result = res;
1020 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001021 }
1022}
1023
Jens Axboe78e19bb2019-11-06 15:21:34 -07001024static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001025{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001026 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001027 unsigned long flags;
1028
1029 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001030 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001031 io_commit_cqring(ctx);
1032 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1033
Jens Axboe8c838782019-03-12 15:48:16 -06001034 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001035}
1036
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001037static inline bool io_is_fallback_req(struct io_kiocb *req)
1038{
1039 return req == (struct io_kiocb *)
1040 ((unsigned long) req->ctx->fallback_req & ~1UL);
1041}
1042
1043static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1044{
1045 struct io_kiocb *req;
1046
1047 req = ctx->fallback_req;
1048 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1049 return req;
1050
1051 return NULL;
1052}
1053
Jens Axboe2579f912019-01-09 09:10:43 -07001054static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1055 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001056{
Jens Axboefd6fab22019-03-14 16:30:06 -06001057 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001058 struct io_kiocb *req;
1059
1060 if (!percpu_ref_tryget(&ctx->refs))
1061 return NULL;
1062
Jens Axboe2579f912019-01-09 09:10:43 -07001063 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001064 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001065 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001066 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001067 } else if (!state->free_reqs) {
1068 size_t sz;
1069 int ret;
1070
1071 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001072 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1073
1074 /*
1075 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1076 * retry single alloc to be on the safe side.
1077 */
1078 if (unlikely(ret <= 0)) {
1079 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1080 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001081 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001082 ret = 1;
1083 }
Jens Axboe2579f912019-01-09 09:10:43 -07001084 state->free_reqs = ret - 1;
1085 state->cur_req = 1;
1086 req = state->reqs[0];
1087 } else {
1088 req = state->reqs[state->cur_req];
1089 state->free_reqs--;
1090 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001091 }
1092
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001093got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001094 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001095 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001096 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001097 req->ctx = ctx;
1098 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001099 /* one is dropped after submission, the other at completion */
1100 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001101 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001102 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001103 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001104fallback:
1105 req = io_get_fallback_req(ctx);
1106 if (req)
1107 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001108 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001109 return NULL;
1110}
1111
Jens Axboedef596e2019-01-09 08:59:42 -07001112static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
1113{
1114 if (*nr) {
1115 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +03001116 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001117 percpu_ref_put_many(&ctx->file_data->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -07001118 *nr = 0;
1119 }
1120}
1121
Jens Axboe9e645e112019-05-10 16:07:28 -06001122static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001123{
Jens Axboefcb323c2019-10-24 12:39:47 -06001124 struct io_ring_ctx *ctx = req->ctx;
1125
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001126 if (req->io)
1127 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001128 if (req->file) {
1129 if (req->flags & REQ_F_FIXED_FILE)
1130 percpu_ref_put(&ctx->file_data->refs);
1131 else
1132 fput(req->file);
1133 }
Jens Axboefcb323c2019-10-24 12:39:47 -06001134 if (req->flags & REQ_F_INFLIGHT) {
1135 unsigned long flags;
1136
1137 spin_lock_irqsave(&ctx->inflight_lock, flags);
1138 list_del(&req->inflight_entry);
1139 if (waitqueue_active(&ctx->inflight_wait))
1140 wake_up(&ctx->inflight_wait);
1141 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1142 }
1143 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001144 if (likely(!io_is_fallback_req(req)))
1145 kmem_cache_free(req_cachep, req);
1146 else
1147 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001148}
1149
Jackie Liua197f662019-11-08 08:09:12 -07001150static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001151{
Jackie Liua197f662019-11-08 08:09:12 -07001152 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001153 int ret;
1154
Jens Axboe2d283902019-12-04 11:08:05 -07001155 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001156 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001157 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001158 io_commit_cqring(ctx);
1159 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001160 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001161 return true;
1162 }
1163
1164 return false;
1165}
1166
Jens Axboeba816ad2019-09-28 11:36:45 -06001167static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001168{
Jens Axboe2665abf2019-11-05 12:40:47 -07001169 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001170 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001171
Jens Axboe4d7dd462019-11-20 13:03:52 -07001172 /* Already got next link */
1173 if (req->flags & REQ_F_LINK_NEXT)
1174 return;
1175
Jens Axboe9e645e112019-05-10 16:07:28 -06001176 /*
1177 * The list should never be empty when we are called here. But could
1178 * potentially happen if the chain is messed up, check to be on the
1179 * safe side.
1180 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001181 while (!list_empty(&req->link_list)) {
1182 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1183 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001184
Pavel Begunkov44932332019-12-05 16:16:35 +03001185 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1186 (nxt->flags & REQ_F_TIMEOUT))) {
1187 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001188 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001189 req->flags &= ~REQ_F_LINK_TIMEOUT;
1190 continue;
1191 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001192
Pavel Begunkov44932332019-12-05 16:16:35 +03001193 list_del_init(&req->link_list);
1194 if (!list_empty(&nxt->link_list))
1195 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001196 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001197 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001198 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001199
Jens Axboe4d7dd462019-11-20 13:03:52 -07001200 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001201 if (wake_ev)
1202 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001203}
1204
1205/*
1206 * Called if REQ_F_LINK is set, and we fail the head request
1207 */
1208static void io_fail_links(struct io_kiocb *req)
1209{
Jens Axboe2665abf2019-11-05 12:40:47 -07001210 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001211 unsigned long flags;
1212
1213 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001214
1215 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001216 struct io_kiocb *link = list_first_entry(&req->link_list,
1217 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001218
Pavel Begunkov44932332019-12-05 16:16:35 +03001219 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001220 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001221
1222 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001223 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001224 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001225 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001226 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001227 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001228 }
Jens Axboe5d960722019-11-19 15:31:28 -07001229 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001230 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001231
1232 io_commit_cqring(ctx);
1233 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1234 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001235}
1236
Jens Axboe4d7dd462019-11-20 13:03:52 -07001237static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001238{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001239 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001240 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001241
Jens Axboe9e645e112019-05-10 16:07:28 -06001242 /*
1243 * If LINK is set, we have dependent requests in this chain. If we
1244 * didn't fail this request, queue the first one up, moving any other
1245 * dependencies to the next request. In case of failure, fail the rest
1246 * of the chain.
1247 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001248 if (req->flags & REQ_F_FAIL_LINK) {
1249 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001250 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1251 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001252 struct io_ring_ctx *ctx = req->ctx;
1253 unsigned long flags;
1254
1255 /*
1256 * If this is a timeout link, we could be racing with the
1257 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001258 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001259 */
1260 spin_lock_irqsave(&ctx->completion_lock, flags);
1261 io_req_link_next(req, nxt);
1262 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1263 } else {
1264 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001265 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001266}
Jens Axboe9e645e112019-05-10 16:07:28 -06001267
Jackie Liuc69f8db2019-11-09 11:00:08 +08001268static void io_free_req(struct io_kiocb *req)
1269{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001270 struct io_kiocb *nxt = NULL;
1271
1272 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001273 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001274
1275 if (nxt)
1276 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001277}
1278
Jens Axboeba816ad2019-09-28 11:36:45 -06001279/*
1280 * Drop reference to request, return next in chain (if there is one) if this
1281 * was the last reference to this request.
1282 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001283__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001284static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001285{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001286 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001287
Jens Axboee65ef562019-03-12 10:16:44 -06001288 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001289 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001290}
1291
Jens Axboe2b188cc2019-01-07 10:46:33 -07001292static void io_put_req(struct io_kiocb *req)
1293{
Jens Axboedef596e2019-01-09 08:59:42 -07001294 if (refcount_dec_and_test(&req->refs))
1295 io_free_req(req);
1296}
1297
Jens Axboe978db572019-11-14 22:39:04 -07001298/*
1299 * Must only be used if we don't need to care about links, usually from
1300 * within the completion handling itself.
1301 */
1302static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001303{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001304 /* drop both submit and complete references */
1305 if (refcount_sub_and_test(2, &req->refs))
1306 __io_free_req(req);
1307}
1308
Jens Axboe978db572019-11-14 22:39:04 -07001309static void io_double_put_req(struct io_kiocb *req)
1310{
1311 /* drop both submit and complete references */
1312 if (refcount_sub_and_test(2, &req->refs))
1313 io_free_req(req);
1314}
1315
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001316static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001317{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001318 struct io_rings *rings = ctx->rings;
1319
Jens Axboead3eb2c2019-12-18 17:12:20 -07001320 if (test_bit(0, &ctx->cq_check_overflow)) {
1321 /*
1322 * noflush == true is from the waitqueue handler, just ensure
1323 * we wake up the task, and the next invocation will flush the
1324 * entries. We cannot safely to it from here.
1325 */
1326 if (noflush && !list_empty(&ctx->cq_overflow_list))
1327 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001328
Jens Axboead3eb2c2019-12-18 17:12:20 -07001329 io_cqring_overflow_flush(ctx, false);
1330 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001331
Jens Axboea3a0e432019-08-20 11:03:11 -06001332 /* See comment at the top of this file */
1333 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001334 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001335}
1336
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001337static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1338{
1339 struct io_rings *rings = ctx->rings;
1340
1341 /* make sure SQ entry isn't read before tail */
1342 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1343}
1344
Jens Axboee94f1412019-12-19 12:06:02 -07001345static inline bool io_req_multi_free(struct io_kiocb *req)
1346{
1347 /*
1348 * If we're not using fixed files, we have to pair the completion part
1349 * with the file put. Use regular completions for those, only batch
1350 * free for fixed file and non-linked commands.
1351 */
1352 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) == REQ_F_FIXED_FILE)
1353 && !io_is_fallback_req(req) && !req->io)
1354 return true;
1355
1356 return false;
1357}
1358
Jens Axboedef596e2019-01-09 08:59:42 -07001359/*
1360 * Find and free completed poll iocbs
1361 */
1362static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1363 struct list_head *done)
1364{
1365 void *reqs[IO_IOPOLL_BATCH];
1366 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001367 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001368
Jens Axboe09bb8392019-03-13 12:39:28 -06001369 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001370 while (!list_empty(done)) {
1371 req = list_first_entry(done, struct io_kiocb, list);
1372 list_del(&req->list);
1373
Jens Axboe78e19bb2019-11-06 15:21:34 -07001374 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001375 (*nr_events)++;
1376
Jens Axboe09bb8392019-03-13 12:39:28 -06001377 if (refcount_dec_and_test(&req->refs)) {
Jens Axboee94f1412019-12-19 12:06:02 -07001378 if (io_req_multi_free(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001379 reqs[to_free++] = req;
1380 if (to_free == ARRAY_SIZE(reqs))
1381 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001382 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001383 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001384 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001385 }
Jens Axboedef596e2019-01-09 08:59:42 -07001386 }
Jens Axboedef596e2019-01-09 08:59:42 -07001387
Jens Axboe09bb8392019-03-13 12:39:28 -06001388 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001389 io_free_req_many(ctx, reqs, &to_free);
1390}
1391
1392static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1393 long min)
1394{
1395 struct io_kiocb *req, *tmp;
1396 LIST_HEAD(done);
1397 bool spin;
1398 int ret;
1399
1400 /*
1401 * Only spin for completions if we don't have multiple devices hanging
1402 * off our complete list, and we're under the requested amount.
1403 */
1404 spin = !ctx->poll_multi_file && *nr_events < min;
1405
1406 ret = 0;
1407 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001408 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001409
1410 /*
1411 * Move completed entries to our local list. If we find a
1412 * request that requires polling, break out and complete
1413 * the done list first, if we have entries there.
1414 */
1415 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1416 list_move_tail(&req->list, &done);
1417 continue;
1418 }
1419 if (!list_empty(&done))
1420 break;
1421
1422 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1423 if (ret < 0)
1424 break;
1425
1426 if (ret && spin)
1427 spin = false;
1428 ret = 0;
1429 }
1430
1431 if (!list_empty(&done))
1432 io_iopoll_complete(ctx, nr_events, &done);
1433
1434 return ret;
1435}
1436
1437/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001438 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001439 * non-spinning poll check - we'll still enter the driver poll loop, but only
1440 * as a non-spinning completion check.
1441 */
1442static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1443 long min)
1444{
Jens Axboe08f54392019-08-21 22:19:11 -06001445 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001446 int ret;
1447
1448 ret = io_do_iopoll(ctx, nr_events, min);
1449 if (ret < 0)
1450 return ret;
1451 if (!min || *nr_events >= min)
1452 return 0;
1453 }
1454
1455 return 1;
1456}
1457
1458/*
1459 * We can't just wait for polled events to come to us, we have to actively
1460 * find and complete them.
1461 */
1462static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1463{
1464 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1465 return;
1466
1467 mutex_lock(&ctx->uring_lock);
1468 while (!list_empty(&ctx->poll_list)) {
1469 unsigned int nr_events = 0;
1470
1471 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001472
1473 /*
1474 * Ensure we allow local-to-the-cpu processing to take place,
1475 * in this case we need to ensure that we reap all events.
1476 */
1477 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001478 }
1479 mutex_unlock(&ctx->uring_lock);
1480}
1481
Jens Axboe2b2ed972019-10-25 10:06:15 -06001482static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1483 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001484{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001485 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001486
1487 do {
1488 int tmin = 0;
1489
Jens Axboe500f9fb2019-08-19 12:15:59 -06001490 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001491 * Don't enter poll loop if we already have events pending.
1492 * If we do, we can potentially be spinning for commands that
1493 * already triggered a CQE (eg in error).
1494 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001495 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001496 break;
1497
1498 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001499 * If a submit got punted to a workqueue, we can have the
1500 * application entering polling for a command before it gets
1501 * issued. That app will hold the uring_lock for the duration
1502 * of the poll right here, so we need to take a breather every
1503 * now and then to ensure that the issue has a chance to add
1504 * the poll to the issued list. Otherwise we can spin here
1505 * forever, while the workqueue is stuck trying to acquire the
1506 * very same mutex.
1507 */
1508 if (!(++iters & 7)) {
1509 mutex_unlock(&ctx->uring_lock);
1510 mutex_lock(&ctx->uring_lock);
1511 }
1512
Jens Axboedef596e2019-01-09 08:59:42 -07001513 if (*nr_events < min)
1514 tmin = min - *nr_events;
1515
1516 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1517 if (ret <= 0)
1518 break;
1519 ret = 0;
1520 } while (min && !*nr_events && !need_resched());
1521
Jens Axboe2b2ed972019-10-25 10:06:15 -06001522 return ret;
1523}
1524
1525static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1526 long min)
1527{
1528 int ret;
1529
1530 /*
1531 * We disallow the app entering submit/complete with polling, but we
1532 * still need to lock the ring to prevent racing with polled issue
1533 * that got punted to a workqueue.
1534 */
1535 mutex_lock(&ctx->uring_lock);
1536 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001537 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001538 return ret;
1539}
1540
Jens Axboe491381ce2019-10-17 09:20:46 -06001541static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001542{
Jens Axboe491381ce2019-10-17 09:20:46 -06001543 /*
1544 * Tell lockdep we inherited freeze protection from submission
1545 * thread.
1546 */
1547 if (req->flags & REQ_F_ISREG) {
1548 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001549
Jens Axboe491381ce2019-10-17 09:20:46 -06001550 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001551 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001552 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001553}
1554
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001555static inline void req_set_fail_links(struct io_kiocb *req)
1556{
1557 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1558 req->flags |= REQ_F_FAIL_LINK;
1559}
1560
Jens Axboeba816ad2019-09-28 11:36:45 -06001561static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001562{
Jens Axboe9adbd452019-12-20 08:45:55 -07001563 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001564
Jens Axboe491381ce2019-10-17 09:20:46 -06001565 if (kiocb->ki_flags & IOCB_WRITE)
1566 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001567
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001568 if (res != req->result)
1569 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001570 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001571}
1572
1573static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1574{
Jens Axboe9adbd452019-12-20 08:45:55 -07001575 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001576
1577 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001578 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001579}
1580
Jens Axboeba816ad2019-09-28 11:36:45 -06001581static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1582{
Jens Axboe9adbd452019-12-20 08:45:55 -07001583 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001584 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001585
1586 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001587 io_put_req_find_next(req, &nxt);
1588
1589 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001590}
1591
Jens Axboedef596e2019-01-09 08:59:42 -07001592static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1593{
Jens Axboe9adbd452019-12-20 08:45:55 -07001594 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001595
Jens Axboe491381ce2019-10-17 09:20:46 -06001596 if (kiocb->ki_flags & IOCB_WRITE)
1597 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001598
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001599 if (res != req->result)
1600 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001601 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001602 if (res != -EAGAIN)
1603 req->flags |= REQ_F_IOPOLL_COMPLETED;
1604}
1605
1606/*
1607 * After the iocb has been issued, it's safe to be found on the poll list.
1608 * Adding the kiocb to the list AFTER submission ensures that we don't
1609 * find it from a io_iopoll_getevents() thread before the issuer is done
1610 * accessing the kiocb cookie.
1611 */
1612static void io_iopoll_req_issued(struct io_kiocb *req)
1613{
1614 struct io_ring_ctx *ctx = req->ctx;
1615
1616 /*
1617 * Track whether we have multiple files in our lists. This will impact
1618 * how we do polling eventually, not spinning if we're on potentially
1619 * different devices.
1620 */
1621 if (list_empty(&ctx->poll_list)) {
1622 ctx->poll_multi_file = false;
1623 } else if (!ctx->poll_multi_file) {
1624 struct io_kiocb *list_req;
1625
1626 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1627 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001628 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001629 ctx->poll_multi_file = true;
1630 }
1631
1632 /*
1633 * For fast devices, IO may have already completed. If it has, add
1634 * it to the front so we find it first.
1635 */
1636 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1637 list_add(&req->list, &ctx->poll_list);
1638 else
1639 list_add_tail(&req->list, &ctx->poll_list);
1640}
1641
Jens Axboe3d6770f2019-04-13 11:50:54 -06001642static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001643{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001644 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001645 int diff = state->has_refs - state->used_refs;
1646
1647 if (diff)
1648 fput_many(state->file, diff);
1649 state->file = NULL;
1650 }
1651}
1652
1653/*
1654 * Get as many references to a file as we have IOs left in this submission,
1655 * assuming most submissions are for one file, or at least that each file
1656 * has more than one submission.
1657 */
1658static struct file *io_file_get(struct io_submit_state *state, int fd)
1659{
1660 if (!state)
1661 return fget(fd);
1662
1663 if (state->file) {
1664 if (state->fd == fd) {
1665 state->used_refs++;
1666 state->ios_left--;
1667 return state->file;
1668 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001669 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001670 }
1671 state->file = fget_many(fd, state->ios_left);
1672 if (!state->file)
1673 return NULL;
1674
1675 state->fd = fd;
1676 state->has_refs = state->ios_left;
1677 state->used_refs = 1;
1678 state->ios_left--;
1679 return state->file;
1680}
1681
Jens Axboe2b188cc2019-01-07 10:46:33 -07001682/*
1683 * If we tracked the file through the SCM inflight mechanism, we could support
1684 * any file. For now, just ensure that anything potentially problematic is done
1685 * inline.
1686 */
1687static bool io_file_supports_async(struct file *file)
1688{
1689 umode_t mode = file_inode(file)->i_mode;
1690
Jens Axboe10d59342019-12-09 20:16:22 -07001691 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001692 return true;
1693 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1694 return true;
1695
1696 return false;
1697}
1698
Jens Axboe3529d8c2019-12-19 18:24:38 -07001699static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1700 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001701{
Jens Axboedef596e2019-01-09 08:59:42 -07001702 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001703 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001704 unsigned ioprio;
1705 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001706
Jens Axboe09bb8392019-03-13 12:39:28 -06001707 if (!req->file)
1708 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001709
Jens Axboe491381ce2019-10-17 09:20:46 -06001710 if (S_ISREG(file_inode(req->file)->i_mode))
1711 req->flags |= REQ_F_ISREG;
1712
Jens Axboe2b188cc2019-01-07 10:46:33 -07001713 kiocb->ki_pos = READ_ONCE(sqe->off);
1714 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1715 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1716
1717 ioprio = READ_ONCE(sqe->ioprio);
1718 if (ioprio) {
1719 ret = ioprio_check_cap(ioprio);
1720 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001721 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001722
1723 kiocb->ki_ioprio = ioprio;
1724 } else
1725 kiocb->ki_ioprio = get_current_ioprio();
1726
1727 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1728 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001729 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001730
1731 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001732 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1733 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001734 req->flags |= REQ_F_NOWAIT;
1735
1736 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001738
Jens Axboedef596e2019-01-09 08:59:42 -07001739 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001740 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1741 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001742 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001743
Jens Axboedef596e2019-01-09 08:59:42 -07001744 kiocb->ki_flags |= IOCB_HIPRI;
1745 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001746 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001747 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001748 if (kiocb->ki_flags & IOCB_HIPRI)
1749 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001750 kiocb->ki_complete = io_complete_rw;
1751 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001752
Jens Axboe3529d8c2019-12-19 18:24:38 -07001753 req->rw.addr = READ_ONCE(sqe->addr);
1754 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001755 /* we own ->private, reuse it for the buffer index */
1756 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001757 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001758 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001759}
1760
1761static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1762{
1763 switch (ret) {
1764 case -EIOCBQUEUED:
1765 break;
1766 case -ERESTARTSYS:
1767 case -ERESTARTNOINTR:
1768 case -ERESTARTNOHAND:
1769 case -ERESTART_RESTARTBLOCK:
1770 /*
1771 * We can't just restart the syscall, since previously
1772 * submitted sqes may already be in progress. Just fail this
1773 * IO with EINTR.
1774 */
1775 ret = -EINTR;
1776 /* fall through */
1777 default:
1778 kiocb->ki_complete(kiocb, ret, 0);
1779 }
1780}
1781
Jens Axboeba816ad2019-09-28 11:36:45 -06001782static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1783 bool in_async)
1784{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001785 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001786 *nxt = __io_complete_rw(kiocb, ret);
1787 else
1788 io_rw_done(kiocb, ret);
1789}
1790
Jens Axboe9adbd452019-12-20 08:45:55 -07001791static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001792 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001793{
Jens Axboe9adbd452019-12-20 08:45:55 -07001794 struct io_ring_ctx *ctx = req->ctx;
1795 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001796 struct io_mapped_ubuf *imu;
1797 unsigned index, buf_index;
1798 size_t offset;
1799 u64 buf_addr;
1800
1801 /* attempt to use fixed buffers without having provided iovecs */
1802 if (unlikely(!ctx->user_bufs))
1803 return -EFAULT;
1804
Jens Axboe9adbd452019-12-20 08:45:55 -07001805 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001806 if (unlikely(buf_index >= ctx->nr_user_bufs))
1807 return -EFAULT;
1808
1809 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1810 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001811 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001812
1813 /* overflow */
1814 if (buf_addr + len < buf_addr)
1815 return -EFAULT;
1816 /* not inside the mapped region */
1817 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1818 return -EFAULT;
1819
1820 /*
1821 * May not be a start of buffer, set size appropriately
1822 * and advance us to the beginning.
1823 */
1824 offset = buf_addr - imu->ubuf;
1825 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001826
1827 if (offset) {
1828 /*
1829 * Don't use iov_iter_advance() here, as it's really slow for
1830 * using the latter parts of a big fixed buffer - it iterates
1831 * over each segment manually. We can cheat a bit here, because
1832 * we know that:
1833 *
1834 * 1) it's a BVEC iter, we set it up
1835 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1836 * first and last bvec
1837 *
1838 * So just find our index, and adjust the iterator afterwards.
1839 * If the offset is within the first bvec (or the whole first
1840 * bvec, just use iov_iter_advance(). This makes it easier
1841 * since we can just skip the first segment, which may not
1842 * be PAGE_SIZE aligned.
1843 */
1844 const struct bio_vec *bvec = imu->bvec;
1845
1846 if (offset <= bvec->bv_len) {
1847 iov_iter_advance(iter, offset);
1848 } else {
1849 unsigned long seg_skip;
1850
1851 /* skip first vec */
1852 offset -= bvec->bv_len;
1853 seg_skip = 1 + (offset >> PAGE_SHIFT);
1854
1855 iter->bvec = bvec + seg_skip;
1856 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001857 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001858 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001859 }
1860 }
1861
Jens Axboe5e559562019-11-13 16:12:46 -07001862 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001863}
1864
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001865static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1866 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001867{
Jens Axboe9adbd452019-12-20 08:45:55 -07001868 void __user *buf = u64_to_user_ptr(req->rw.addr);
1869 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001870 u8 opcode;
1871
Jens Axboed625c6e2019-12-17 19:53:05 -07001872 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001873 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001874 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001875 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001876 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001877
Jens Axboe9adbd452019-12-20 08:45:55 -07001878 /* buffer index only valid with fixed read/write */
1879 if (req->rw.kiocb.private)
1880 return -EINVAL;
1881
Jens Axboe3a6820f2019-12-22 15:19:35 -07001882 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1883 ssize_t ret;
1884 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1885 *iovec = NULL;
1886 return ret;
1887 }
1888
Jens Axboef67676d2019-12-02 11:03:47 -07001889 if (req->io) {
1890 struct io_async_rw *iorw = &req->io->rw;
1891
1892 *iovec = iorw->iov;
1893 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1894 if (iorw->iov == iorw->fast_iov)
1895 *iovec = NULL;
1896 return iorw->size;
1897 }
1898
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001899 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001900 return -EFAULT;
1901
1902#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001903 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001904 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1905 iovec, iter);
1906#endif
1907
1908 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1909}
1910
Jens Axboe32960612019-09-23 11:05:34 -06001911/*
1912 * For files that don't have ->read_iter() and ->write_iter(), handle them
1913 * by looping over ->read() or ->write() manually.
1914 */
1915static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1916 struct iov_iter *iter)
1917{
1918 ssize_t ret = 0;
1919
1920 /*
1921 * Don't support polled IO through this interface, and we can't
1922 * support non-blocking either. For the latter, this just causes
1923 * the kiocb to be handled from an async context.
1924 */
1925 if (kiocb->ki_flags & IOCB_HIPRI)
1926 return -EOPNOTSUPP;
1927 if (kiocb->ki_flags & IOCB_NOWAIT)
1928 return -EAGAIN;
1929
1930 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001931 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001932 ssize_t nr;
1933
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001934 if (!iov_iter_is_bvec(iter)) {
1935 iovec = iov_iter_iovec(iter);
1936 } else {
1937 /* fixed buffers import bvec */
1938 iovec.iov_base = kmap(iter->bvec->bv_page)
1939 + iter->iov_offset;
1940 iovec.iov_len = min(iter->count,
1941 iter->bvec->bv_len - iter->iov_offset);
1942 }
1943
Jens Axboe32960612019-09-23 11:05:34 -06001944 if (rw == READ) {
1945 nr = file->f_op->read(file, iovec.iov_base,
1946 iovec.iov_len, &kiocb->ki_pos);
1947 } else {
1948 nr = file->f_op->write(file, iovec.iov_base,
1949 iovec.iov_len, &kiocb->ki_pos);
1950 }
1951
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001952 if (iov_iter_is_bvec(iter))
1953 kunmap(iter->bvec->bv_page);
1954
Jens Axboe32960612019-09-23 11:05:34 -06001955 if (nr < 0) {
1956 if (!ret)
1957 ret = nr;
1958 break;
1959 }
1960 ret += nr;
1961 if (nr != iovec.iov_len)
1962 break;
1963 iov_iter_advance(iter, nr);
1964 }
1965
1966 return ret;
1967}
1968
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001969static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07001970 struct iovec *iovec, struct iovec *fast_iov,
1971 struct iov_iter *iter)
1972{
1973 req->io->rw.nr_segs = iter->nr_segs;
1974 req->io->rw.size = io_size;
1975 req->io->rw.iov = iovec;
1976 if (!req->io->rw.iov) {
1977 req->io->rw.iov = req->io->rw.fast_iov;
1978 memcpy(req->io->rw.iov, fast_iov,
1979 sizeof(struct iovec) * iter->nr_segs);
1980 }
1981}
1982
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001983static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07001984{
Jens Axboed3656342019-12-18 09:50:26 -07001985 if (!io_op_defs[req->opcode].async_ctx)
1986 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001987 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07001988 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001989}
1990
1991static void io_rw_async(struct io_wq_work **workptr)
1992{
1993 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1994 struct iovec *iov = NULL;
1995
1996 if (req->io->rw.iov != req->io->rw.fast_iov)
1997 iov = req->io->rw.iov;
1998 io_wq_submit_work(workptr);
1999 kfree(iov);
2000}
2001
2002static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2003 struct iovec *iovec, struct iovec *fast_iov,
2004 struct iov_iter *iter)
2005{
Jens Axboe74566df2020-01-13 19:23:24 -07002006 if (req->opcode == IORING_OP_READ_FIXED ||
2007 req->opcode == IORING_OP_WRITE_FIXED)
2008 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002009 if (!req->io && io_alloc_async_ctx(req))
2010 return -ENOMEM;
2011
2012 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2013 req->work.func = io_rw_async;
2014 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002015}
2016
Jens Axboe3529d8c2019-12-19 18:24:38 -07002017static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2018 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002019{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002020 struct io_async_ctx *io;
2021 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002022 ssize_t ret;
2023
Jens Axboe3529d8c2019-12-19 18:24:38 -07002024 ret = io_prep_rw(req, sqe, force_nonblock);
2025 if (ret)
2026 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002027
Jens Axboe3529d8c2019-12-19 18:24:38 -07002028 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2029 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002030
Jens Axboe3529d8c2019-12-19 18:24:38 -07002031 if (!req->io)
2032 return 0;
2033
2034 io = req->io;
2035 io->rw.iov = io->rw.fast_iov;
2036 req->io = NULL;
2037 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2038 req->io = io;
2039 if (ret < 0)
2040 return ret;
2041
2042 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2043 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002044}
2045
Pavel Begunkov267bc902019-11-07 01:41:08 +03002046static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002047 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002048{
2049 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002050 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002051 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002052 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002053 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002054
Jens Axboe3529d8c2019-12-19 18:24:38 -07002055 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002056 if (ret < 0)
2057 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002058
Jens Axboefd6c2e42019-12-18 12:19:41 -07002059 /* Ensure we clear previously set non-block flag */
2060 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002061 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002062
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002063 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002064 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002065 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002066 req->result = io_size;
2067
2068 /*
2069 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2070 * we know to async punt it even if it was opened O_NONBLOCK
2071 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002072 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002073 req->flags |= REQ_F_MUST_PUNT;
2074 goto copy_iov;
2075 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002076
Jens Axboe31b51512019-01-18 22:56:34 -07002077 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002078 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002079 if (!ret) {
2080 ssize_t ret2;
2081
Jens Axboe9adbd452019-12-20 08:45:55 -07002082 if (req->file->f_op->read_iter)
2083 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002084 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002085 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002086
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002087 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002088 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002089 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002090 } else {
2091copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002092 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002093 inline_vecs, &iter);
2094 if (ret)
2095 goto out_free;
2096 return -EAGAIN;
2097 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002098 }
Jens Axboef67676d2019-12-02 11:03:47 -07002099out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002100 if (!io_wq_current_is_worker())
2101 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002102 return ret;
2103}
2104
Jens Axboe3529d8c2019-12-19 18:24:38 -07002105static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2106 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002107{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002108 struct io_async_ctx *io;
2109 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002110 ssize_t ret;
2111
Jens Axboe3529d8c2019-12-19 18:24:38 -07002112 ret = io_prep_rw(req, sqe, force_nonblock);
2113 if (ret)
2114 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002115
Jens Axboe3529d8c2019-12-19 18:24:38 -07002116 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2117 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002118
Jens Axboe3529d8c2019-12-19 18:24:38 -07002119 if (!req->io)
2120 return 0;
2121
2122 io = req->io;
2123 io->rw.iov = io->rw.fast_iov;
2124 req->io = NULL;
2125 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2126 req->io = io;
2127 if (ret < 0)
2128 return ret;
2129
2130 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2131 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002132}
2133
Pavel Begunkov267bc902019-11-07 01:41:08 +03002134static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002135 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002136{
2137 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002138 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002139 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002140 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002141 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002142
Jens Axboe3529d8c2019-12-19 18:24:38 -07002143 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002144 if (ret < 0)
2145 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002146
Jens Axboefd6c2e42019-12-18 12:19:41 -07002147 /* Ensure we clear previously set non-block flag */
2148 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002149 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002150
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002151 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002152 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002153 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002154 req->result = io_size;
2155
2156 /*
2157 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2158 * we know to async punt it even if it was opened O_NONBLOCK
2159 */
2160 if (force_nonblock && !io_file_supports_async(req->file)) {
2161 req->flags |= REQ_F_MUST_PUNT;
2162 goto copy_iov;
2163 }
2164
Jens Axboe10d59342019-12-09 20:16:22 -07002165 /* file path doesn't support NOWAIT for non-direct_IO */
2166 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2167 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002168 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002169
Jens Axboe31b51512019-01-18 22:56:34 -07002170 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002171 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002172 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002173 ssize_t ret2;
2174
Jens Axboe2b188cc2019-01-07 10:46:33 -07002175 /*
2176 * Open-code file_start_write here to grab freeze protection,
2177 * which will be released by another thread in
2178 * io_complete_rw(). Fool lockdep by telling it the lock got
2179 * released so that it doesn't complain about the held lock when
2180 * we return to userspace.
2181 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002182 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002183 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002184 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002185 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002186 SB_FREEZE_WRITE);
2187 }
2188 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002189
Jens Axboe9adbd452019-12-20 08:45:55 -07002190 if (req->file->f_op->write_iter)
2191 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002192 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002193 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002194 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002195 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002196 } else {
2197copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002198 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002199 inline_vecs, &iter);
2200 if (ret)
2201 goto out_free;
2202 return -EAGAIN;
2203 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002204 }
Jens Axboe31b51512019-01-18 22:56:34 -07002205out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002206 if (!io_wq_current_is_worker())
2207 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002208 return ret;
2209}
2210
2211/*
2212 * IORING_OP_NOP just posts a completion event, nothing else.
2213 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002214static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002215{
2216 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002217
Jens Axboedef596e2019-01-09 08:59:42 -07002218 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2219 return -EINVAL;
2220
Jens Axboe78e19bb2019-11-06 15:21:34 -07002221 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002222 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002223 return 0;
2224}
2225
Jens Axboe3529d8c2019-12-19 18:24:38 -07002226static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002227{
Jens Axboe6b063142019-01-10 22:13:58 -07002228 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002229
Jens Axboe09bb8392019-03-13 12:39:28 -06002230 if (!req->file)
2231 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002232
Jens Axboe6b063142019-01-10 22:13:58 -07002233 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002234 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002235 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002236 return -EINVAL;
2237
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002238 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2239 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2240 return -EINVAL;
2241
2242 req->sync.off = READ_ONCE(sqe->off);
2243 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002244 return 0;
2245}
2246
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002247static bool io_req_cancelled(struct io_kiocb *req)
2248{
2249 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2250 req_set_fail_links(req);
2251 io_cqring_add_event(req, -ECANCELED);
2252 io_put_req(req);
2253 return true;
2254 }
2255
2256 return false;
2257}
2258
Jens Axboe78912932020-01-14 22:09:06 -07002259static void io_link_work_cb(struct io_wq_work **workptr)
2260{
2261 struct io_wq_work *work = *workptr;
2262 struct io_kiocb *link = work->data;
2263
2264 io_queue_linked_timeout(link);
2265 work->func = io_wq_submit_work;
2266}
2267
2268static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2269{
2270 struct io_kiocb *link;
2271
2272 io_prep_async_work(nxt, &link);
2273 *workptr = &nxt->work;
2274 if (link) {
2275 nxt->work.flags |= IO_WQ_WORK_CB;
2276 nxt->work.func = io_link_work_cb;
2277 nxt->work.data = link;
2278 }
2279}
2280
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002281static void io_fsync_finish(struct io_wq_work **workptr)
2282{
2283 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2284 loff_t end = req->sync.off + req->sync.len;
2285 struct io_kiocb *nxt = NULL;
2286 int ret;
2287
2288 if (io_req_cancelled(req))
2289 return;
2290
Jens Axboe9adbd452019-12-20 08:45:55 -07002291 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002292 end > 0 ? end : LLONG_MAX,
2293 req->sync.flags & IORING_FSYNC_DATASYNC);
2294 if (ret < 0)
2295 req_set_fail_links(req);
2296 io_cqring_add_event(req, ret);
2297 io_put_req_find_next(req, &nxt);
2298 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002299 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002300}
2301
Jens Axboefc4df992019-12-10 14:38:45 -07002302static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2303 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002304{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002305 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002306
2307 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002308 if (force_nonblock) {
2309 io_put_req(req);
2310 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002311 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002312 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002313
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002314 work = old_work = &req->work;
2315 io_fsync_finish(&work);
2316 if (work && work != old_work)
2317 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002318 return 0;
2319}
2320
Jens Axboed63d1b52019-12-10 10:38:56 -07002321static void io_fallocate_finish(struct io_wq_work **workptr)
2322{
2323 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2324 struct io_kiocb *nxt = NULL;
2325 int ret;
2326
2327 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2328 req->sync.len);
2329 if (ret < 0)
2330 req_set_fail_links(req);
2331 io_cqring_add_event(req, ret);
2332 io_put_req_find_next(req, &nxt);
2333 if (nxt)
2334 io_wq_assign_next(workptr, nxt);
2335}
2336
2337static int io_fallocate_prep(struct io_kiocb *req,
2338 const struct io_uring_sqe *sqe)
2339{
2340 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2341 return -EINVAL;
2342
2343 req->sync.off = READ_ONCE(sqe->off);
2344 req->sync.len = READ_ONCE(sqe->addr);
2345 req->sync.mode = READ_ONCE(sqe->len);
2346 return 0;
2347}
2348
2349static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2350 bool force_nonblock)
2351{
2352 struct io_wq_work *work, *old_work;
2353
2354 /* fallocate always requiring blocking context */
2355 if (force_nonblock) {
2356 io_put_req(req);
2357 req->work.func = io_fallocate_finish;
2358 return -EAGAIN;
2359 }
2360
2361 work = old_work = &req->work;
2362 io_fallocate_finish(&work);
2363 if (work && work != old_work)
2364 *nxt = container_of(work, struct io_kiocb, work);
2365
2366 return 0;
2367}
2368
Jens Axboe15b71ab2019-12-11 11:20:36 -07002369static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2370{
2371 int ret;
2372
2373 if (sqe->ioprio || sqe->buf_index)
2374 return -EINVAL;
2375
2376 req->open.dfd = READ_ONCE(sqe->fd);
2377 req->open.mode = READ_ONCE(sqe->len);
2378 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2379 req->open.flags = READ_ONCE(sqe->open_flags);
2380
2381 req->open.filename = getname(req->open.fname);
2382 if (IS_ERR(req->open.filename)) {
2383 ret = PTR_ERR(req->open.filename);
2384 req->open.filename = NULL;
2385 return ret;
2386 }
2387
2388 return 0;
2389}
2390
2391static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2392 bool force_nonblock)
2393{
2394 struct open_flags op;
2395 struct open_how how;
2396 struct file *file;
2397 int ret;
2398
2399 if (force_nonblock) {
2400 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2401 return -EAGAIN;
2402 }
2403
2404 how = build_open_how(req->open.flags, req->open.mode);
2405 ret = build_open_flags(&how, &op);
2406 if (ret)
2407 goto err;
2408
2409 ret = get_unused_fd_flags(how.flags);
2410 if (ret < 0)
2411 goto err;
2412
2413 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2414 if (IS_ERR(file)) {
2415 put_unused_fd(ret);
2416 ret = PTR_ERR(file);
2417 } else {
2418 fsnotify_open(file);
2419 fd_install(ret, file);
2420 }
2421err:
2422 putname(req->open.filename);
2423 if (ret < 0)
2424 req_set_fail_links(req);
2425 io_cqring_add_event(req, ret);
2426 io_put_req_find_next(req, nxt);
2427 return 0;
2428}
2429
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002430static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2431{
2432 unsigned lookup_flags;
2433 int ret;
2434
2435 if (sqe->ioprio || sqe->buf_index)
2436 return -EINVAL;
2437
2438 req->open.dfd = READ_ONCE(sqe->fd);
2439 req->open.mask = READ_ONCE(sqe->len);
2440 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2441 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2442 req->open.flags = READ_ONCE(sqe->statx_flags);
2443
2444 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.flags))
2445 return -EINVAL;
2446
2447 req->open.filename = getname_flags(req->open.fname, lookup_flags, NULL);
2448 if (IS_ERR(req->open.filename)) {
2449 ret = PTR_ERR(req->open.filename);
2450 req->open.filename = NULL;
2451 return ret;
2452 }
2453
2454 return 0;
2455}
2456
2457static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2458 bool force_nonblock)
2459{
2460 struct io_open *ctx = &req->open;
2461 unsigned lookup_flags;
2462 struct path path;
2463 struct kstat stat;
2464 int ret;
2465
2466 if (force_nonblock)
2467 return -EAGAIN;
2468
2469 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->flags))
2470 return -EINVAL;
2471
2472retry:
2473 /* filename_lookup() drops it, keep a reference */
2474 ctx->filename->refcnt++;
2475
2476 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2477 NULL);
2478 if (ret)
2479 goto err;
2480
2481 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->flags);
2482 path_put(&path);
2483 if (retry_estale(ret, lookup_flags)) {
2484 lookup_flags |= LOOKUP_REVAL;
2485 goto retry;
2486 }
2487 if (!ret)
2488 ret = cp_statx(&stat, ctx->buffer);
2489err:
2490 putname(ctx->filename);
2491 if (ret < 0)
2492 req_set_fail_links(req);
2493 io_cqring_add_event(req, ret);
2494 io_put_req_find_next(req, nxt);
2495 return 0;
2496}
2497
Jens Axboeb5dba592019-12-11 14:02:38 -07002498static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2499{
2500 /*
2501 * If we queue this for async, it must not be cancellable. That would
2502 * leave the 'file' in an undeterminate state.
2503 */
2504 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2505
2506 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2507 sqe->rw_flags || sqe->buf_index)
2508 return -EINVAL;
2509 if (sqe->flags & IOSQE_FIXED_FILE)
2510 return -EINVAL;
2511
2512 req->close.fd = READ_ONCE(sqe->fd);
2513 if (req->file->f_op == &io_uring_fops ||
2514 req->close.fd == req->ring_fd)
2515 return -EBADF;
2516
2517 return 0;
2518}
2519
2520static void io_close_finish(struct io_wq_work **workptr)
2521{
2522 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2523 struct io_kiocb *nxt = NULL;
2524
2525 /* Invoked with files, we need to do the close */
2526 if (req->work.files) {
2527 int ret;
2528
2529 ret = filp_close(req->close.put_file, req->work.files);
2530 if (ret < 0) {
2531 req_set_fail_links(req);
2532 }
2533 io_cqring_add_event(req, ret);
2534 }
2535
2536 fput(req->close.put_file);
2537
2538 /* we bypassed the re-issue, drop the submission reference */
2539 io_put_req(req);
2540 io_put_req_find_next(req, &nxt);
2541 if (nxt)
2542 io_wq_assign_next(workptr, nxt);
2543}
2544
2545static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2546 bool force_nonblock)
2547{
2548 int ret;
2549
2550 req->close.put_file = NULL;
2551 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2552 if (ret < 0)
2553 return ret;
2554
2555 /* if the file has a flush method, be safe and punt to async */
2556 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2557 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2558 goto eagain;
2559 }
2560
2561 /*
2562 * No ->flush(), safely close from here and just punt the
2563 * fput() to async context.
2564 */
2565 ret = filp_close(req->close.put_file, current->files);
2566
2567 if (ret < 0)
2568 req_set_fail_links(req);
2569 io_cqring_add_event(req, ret);
2570
2571 if (io_wq_current_is_worker()) {
2572 struct io_wq_work *old_work, *work;
2573
2574 old_work = work = &req->work;
2575 io_close_finish(&work);
2576 if (work && work != old_work)
2577 *nxt = container_of(work, struct io_kiocb, work);
2578 return 0;
2579 }
2580
2581eagain:
2582 req->work.func = io_close_finish;
2583 return -EAGAIN;
2584}
2585
Jens Axboe3529d8c2019-12-19 18:24:38 -07002586static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002587{
2588 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002589
2590 if (!req->file)
2591 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002592
2593 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2594 return -EINVAL;
2595 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2596 return -EINVAL;
2597
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002598 req->sync.off = READ_ONCE(sqe->off);
2599 req->sync.len = READ_ONCE(sqe->len);
2600 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002601 return 0;
2602}
2603
2604static void io_sync_file_range_finish(struct io_wq_work **workptr)
2605{
2606 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2607 struct io_kiocb *nxt = NULL;
2608 int ret;
2609
2610 if (io_req_cancelled(req))
2611 return;
2612
Jens Axboe9adbd452019-12-20 08:45:55 -07002613 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002614 req->sync.flags);
2615 if (ret < 0)
2616 req_set_fail_links(req);
2617 io_cqring_add_event(req, ret);
2618 io_put_req_find_next(req, &nxt);
2619 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002620 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002621}
2622
Jens Axboefc4df992019-12-10 14:38:45 -07002623static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002624 bool force_nonblock)
2625{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002626 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002627
2628 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002629 if (force_nonblock) {
2630 io_put_req(req);
2631 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002632 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002633 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002634
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002635 work = old_work = &req->work;
2636 io_sync_file_range_finish(&work);
2637 if (work && work != old_work)
2638 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002639 return 0;
2640}
2641
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002642#if defined(CONFIG_NET)
2643static void io_sendrecv_async(struct io_wq_work **workptr)
2644{
2645 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2646 struct iovec *iov = NULL;
2647
2648 if (req->io->rw.iov != req->io->rw.fast_iov)
2649 iov = req->io->msg.iov;
2650 io_wq_submit_work(workptr);
2651 kfree(iov);
2652}
2653#endif
2654
Jens Axboe3529d8c2019-12-19 18:24:38 -07002655static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002656{
Jens Axboe03b12302019-12-02 18:50:25 -07002657#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002658 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002659 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002660
Jens Axboee47293f2019-12-20 08:58:21 -07002661 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2662 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe3529d8c2019-12-19 18:24:38 -07002663
2664 if (!io)
2665 return 0;
2666
Jens Axboed9688562019-12-09 19:35:20 -07002667 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002668 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002669 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002670#else
Jens Axboee47293f2019-12-20 08:58:21 -07002671 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002672#endif
2673}
2674
Jens Axboefc4df992019-12-10 14:38:45 -07002675static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2676 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002677{
2678#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002679 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002680 struct socket *sock;
2681 int ret;
2682
2683 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2684 return -EINVAL;
2685
2686 sock = sock_from_file(req->file, &ret);
2687 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002688 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002689 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002690 unsigned flags;
2691
Jens Axboe03b12302019-12-02 18:50:25 -07002692 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002693 kmsg = &req->io->msg;
2694 kmsg->msg.msg_name = &addr;
2695 /* if iov is set, it's allocated already */
2696 if (!kmsg->iov)
2697 kmsg->iov = kmsg->fast_iov;
2698 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002699 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002700 struct io_sr_msg *sr = &req->sr_msg;
2701
Jens Axboe0b416c32019-12-15 10:57:46 -07002702 kmsg = &io.msg;
2703 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002704
2705 io.msg.iov = io.msg.fast_iov;
2706 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2707 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002708 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002709 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002710 }
2711
Jens Axboee47293f2019-12-20 08:58:21 -07002712 flags = req->sr_msg.msg_flags;
2713 if (flags & MSG_DONTWAIT)
2714 req->flags |= REQ_F_NOWAIT;
2715 else if (force_nonblock)
2716 flags |= MSG_DONTWAIT;
2717
Jens Axboe0b416c32019-12-15 10:57:46 -07002718 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002719 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002720 if (req->io)
2721 return -EAGAIN;
2722 if (io_alloc_async_ctx(req))
2723 return -ENOMEM;
2724 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2725 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002726 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002727 }
2728 if (ret == -ERESTARTSYS)
2729 ret = -EINTR;
2730 }
2731
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002732 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002733 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002734 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002735 if (ret < 0)
2736 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002737 io_put_req_find_next(req, nxt);
2738 return 0;
2739#else
2740 return -EOPNOTSUPP;
2741#endif
2742}
2743
Jens Axboe3529d8c2019-12-19 18:24:38 -07002744static int io_recvmsg_prep(struct io_kiocb *req,
2745 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002746{
2747#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002748 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002749 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002750
Jens Axboe3529d8c2019-12-19 18:24:38 -07002751 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2752 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2753
2754 if (!io)
Jens Axboe06b76d42019-12-19 14:44:26 -07002755 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002756
Jens Axboed9688562019-12-09 19:35:20 -07002757 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002758 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002759 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002760#else
Jens Axboee47293f2019-12-20 08:58:21 -07002761 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002762#endif
2763}
2764
Jens Axboefc4df992019-12-10 14:38:45 -07002765static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2766 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002767{
2768#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002769 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002770 struct socket *sock;
2771 int ret;
2772
2773 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2774 return -EINVAL;
2775
2776 sock = sock_from_file(req->file, &ret);
2777 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002778 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002779 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002780 unsigned flags;
2781
Jens Axboe03b12302019-12-02 18:50:25 -07002782 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002783 kmsg = &req->io->msg;
2784 kmsg->msg.msg_name = &addr;
2785 /* if iov is set, it's allocated already */
2786 if (!kmsg->iov)
2787 kmsg->iov = kmsg->fast_iov;
2788 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002789 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002790 struct io_sr_msg *sr = &req->sr_msg;
2791
Jens Axboe0b416c32019-12-15 10:57:46 -07002792 kmsg = &io.msg;
2793 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002794
2795 io.msg.iov = io.msg.fast_iov;
2796 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
2797 sr->msg_flags, &io.msg.uaddr,
2798 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002799 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002800 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002801 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002802
Jens Axboee47293f2019-12-20 08:58:21 -07002803 flags = req->sr_msg.msg_flags;
2804 if (flags & MSG_DONTWAIT)
2805 req->flags |= REQ_F_NOWAIT;
2806 else if (force_nonblock)
2807 flags |= MSG_DONTWAIT;
2808
2809 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
2810 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002811 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002812 if (req->io)
2813 return -EAGAIN;
2814 if (io_alloc_async_ctx(req))
2815 return -ENOMEM;
2816 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2817 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002818 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002819 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002820 if (ret == -ERESTARTSYS)
2821 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002822 }
2823
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002824 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002825 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002826 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002827 if (ret < 0)
2828 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002829 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002830 return 0;
2831#else
2832 return -EOPNOTSUPP;
2833#endif
2834}
2835
Jens Axboe3529d8c2019-12-19 18:24:38 -07002836static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002837{
2838#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002839 struct io_accept *accept = &req->accept;
2840
Jens Axboe17f2fe32019-10-17 14:42:58 -06002841 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2842 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002843 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002844 return -EINVAL;
2845
Jens Axboed55e5f52019-12-11 16:12:15 -07002846 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2847 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002848 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002849 return 0;
2850#else
2851 return -EOPNOTSUPP;
2852#endif
2853}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002854
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002855#if defined(CONFIG_NET)
2856static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2857 bool force_nonblock)
2858{
2859 struct io_accept *accept = &req->accept;
2860 unsigned file_flags;
2861 int ret;
2862
2863 file_flags = force_nonblock ? O_NONBLOCK : 0;
2864 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2865 accept->addr_len, accept->flags);
2866 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002867 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002868 if (ret == -ERESTARTSYS)
2869 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002870 if (ret < 0)
2871 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002872 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002873 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002874 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002875}
2876
2877static void io_accept_finish(struct io_wq_work **workptr)
2878{
2879 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2880 struct io_kiocb *nxt = NULL;
2881
2882 if (io_req_cancelled(req))
2883 return;
2884 __io_accept(req, &nxt, false);
2885 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002886 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002887}
2888#endif
2889
2890static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2891 bool force_nonblock)
2892{
2893#if defined(CONFIG_NET)
2894 int ret;
2895
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002896 ret = __io_accept(req, nxt, force_nonblock);
2897 if (ret == -EAGAIN && force_nonblock) {
2898 req->work.func = io_accept_finish;
2899 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2900 io_put_req(req);
2901 return -EAGAIN;
2902 }
2903 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002904#else
2905 return -EOPNOTSUPP;
2906#endif
2907}
2908
Jens Axboe3529d8c2019-12-19 18:24:38 -07002909static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07002910{
2911#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002912 struct io_connect *conn = &req->connect;
2913 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07002914
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002915 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2916 return -EINVAL;
2917 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2918 return -EINVAL;
2919
Jens Axboe3529d8c2019-12-19 18:24:38 -07002920 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2921 conn->addr_len = READ_ONCE(sqe->addr2);
2922
2923 if (!io)
2924 return 0;
2925
2926 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002927 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002928#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002929 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07002930#endif
2931}
2932
Jens Axboefc4df992019-12-10 14:38:45 -07002933static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
2934 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07002935{
2936#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07002937 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002938 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002939 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002940
Jens Axboef499a022019-12-02 16:28:46 -07002941 if (req->io) {
2942 io = req->io;
2943 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002944 ret = move_addr_to_kernel(req->connect.addr,
2945 req->connect.addr_len,
2946 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002947 if (ret)
2948 goto out;
2949 io = &__io;
2950 }
2951
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002952 file_flags = force_nonblock ? O_NONBLOCK : 0;
2953
2954 ret = __sys_connect_file(req->file, &io->connect.address,
2955 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002956 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002957 if (req->io)
2958 return -EAGAIN;
2959 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07002960 ret = -ENOMEM;
2961 goto out;
2962 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002963 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07002964 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002965 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002966 if (ret == -ERESTARTSYS)
2967 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002968out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002969 if (ret < 0)
2970 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002971 io_cqring_add_event(req, ret);
2972 io_put_req_find_next(req, nxt);
2973 return 0;
2974#else
2975 return -EOPNOTSUPP;
2976#endif
2977}
2978
Jens Axboe221c5eb2019-01-17 09:41:58 -07002979static void io_poll_remove_one(struct io_kiocb *req)
2980{
2981 struct io_poll_iocb *poll = &req->poll;
2982
2983 spin_lock(&poll->head->lock);
2984 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07002985 if (!list_empty(&poll->wait.entry)) {
2986 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002987 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002988 }
2989 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002990 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002991}
2992
2993static void io_poll_remove_all(struct io_ring_ctx *ctx)
2994{
Jens Axboe78076bb2019-12-04 19:56:40 -07002995 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002996 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07002997 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002998
2999 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003000 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3001 struct hlist_head *list;
3002
3003 list = &ctx->cancel_hash[i];
3004 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3005 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003006 }
3007 spin_unlock_irq(&ctx->completion_lock);
3008}
3009
Jens Axboe47f46762019-11-09 17:43:02 -07003010static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3011{
Jens Axboe78076bb2019-12-04 19:56:40 -07003012 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003013 struct io_kiocb *req;
3014
Jens Axboe78076bb2019-12-04 19:56:40 -07003015 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3016 hlist_for_each_entry(req, list, hash_node) {
3017 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003018 io_poll_remove_one(req);
3019 return 0;
3020 }
Jens Axboe47f46762019-11-09 17:43:02 -07003021 }
3022
3023 return -ENOENT;
3024}
3025
Jens Axboe3529d8c2019-12-19 18:24:38 -07003026static int io_poll_remove_prep(struct io_kiocb *req,
3027 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003028{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003029 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3030 return -EINVAL;
3031 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3032 sqe->poll_events)
3033 return -EINVAL;
3034
Jens Axboe0969e782019-12-17 18:40:57 -07003035 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003036 return 0;
3037}
3038
3039/*
3040 * Find a running poll command that matches one specified in sqe->addr,
3041 * and remove it if found.
3042 */
3043static int io_poll_remove(struct io_kiocb *req)
3044{
3045 struct io_ring_ctx *ctx = req->ctx;
3046 u64 addr;
3047 int ret;
3048
Jens Axboe0969e782019-12-17 18:40:57 -07003049 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003050 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003051 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003052 spin_unlock_irq(&ctx->completion_lock);
3053
Jens Axboe78e19bb2019-11-06 15:21:34 -07003054 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003055 if (ret < 0)
3056 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003057 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003058 return 0;
3059}
3060
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003061static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003062{
Jackie Liua197f662019-11-08 08:09:12 -07003063 struct io_ring_ctx *ctx = req->ctx;
3064
Jens Axboe8c838782019-03-12 15:48:16 -06003065 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003066 if (error)
3067 io_cqring_fill_event(req, error);
3068 else
3069 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003070 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003071}
3072
Jens Axboe561fb042019-10-24 07:25:42 -06003073static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003074{
Jens Axboe561fb042019-10-24 07:25:42 -06003075 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003076 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3077 struct io_poll_iocb *poll = &req->poll;
3078 struct poll_table_struct pt = { ._key = poll->events };
3079 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003080 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003081 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003082 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003083
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003084 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003085 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003086 ret = -ECANCELED;
3087 } else if (READ_ONCE(poll->canceled)) {
3088 ret = -ECANCELED;
3089 }
Jens Axboe561fb042019-10-24 07:25:42 -06003090
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003091 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003092 mask = vfs_poll(poll->file, &pt) & poll->events;
3093
3094 /*
3095 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3096 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3097 * synchronize with them. In the cancellation case the list_del_init
3098 * itself is not actually needed, but harmless so we keep it in to
3099 * avoid further branches in the fast path.
3100 */
3101 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003102 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003103 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003104 spin_unlock_irq(&ctx->completion_lock);
3105 return;
3106 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003107 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003108 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003109 spin_unlock_irq(&ctx->completion_lock);
3110
Jens Axboe8c838782019-03-12 15:48:16 -06003111 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003112
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003113 if (ret < 0)
3114 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003115 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003116 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003117 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003118}
3119
Jens Axboee94f1412019-12-19 12:06:02 -07003120static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3121{
3122 void *reqs[IO_IOPOLL_BATCH];
3123 struct io_kiocb *req, *tmp;
3124 int to_free = 0;
3125
3126 spin_lock_irq(&ctx->completion_lock);
3127 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3128 hash_del(&req->hash_node);
3129 io_poll_complete(req, req->result, 0);
3130
3131 if (refcount_dec_and_test(&req->refs)) {
3132 if (io_req_multi_free(req)) {
3133 reqs[to_free++] = req;
3134 if (to_free == ARRAY_SIZE(reqs))
3135 io_free_req_many(ctx, reqs, &to_free);
3136 } else {
3137 req->flags |= REQ_F_COMP_LOCKED;
3138 io_free_req(req);
3139 }
3140 }
3141 }
3142 spin_unlock_irq(&ctx->completion_lock);
3143
3144 io_cqring_ev_posted(ctx);
3145 io_free_req_many(ctx, reqs, &to_free);
3146}
3147
3148static void io_poll_flush(struct io_wq_work **workptr)
3149{
3150 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3151 struct llist_node *nodes;
3152
3153 nodes = llist_del_all(&req->ctx->poll_llist);
3154 if (nodes)
3155 __io_poll_flush(req->ctx, nodes);
3156}
3157
Jens Axboe221c5eb2019-01-17 09:41:58 -07003158static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3159 void *key)
3160{
Jens Axboee9444752019-11-26 15:02:04 -07003161 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003162 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3163 struct io_ring_ctx *ctx = req->ctx;
3164 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003165
3166 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003167 if (mask && !(mask & poll->events))
3168 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003169
Jens Axboe392edb42019-12-09 17:52:20 -07003170 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003171
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003172 /*
3173 * Run completion inline if we can. We're using trylock here because
3174 * we are violating the completion_lock -> poll wq lock ordering.
3175 * If we have a link timeout we're going to need the completion_lock
3176 * for finalizing the request, mark us as having grabbed that already.
3177 */
Jens Axboee94f1412019-12-19 12:06:02 -07003178 if (mask) {
3179 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003180
Jens Axboee94f1412019-12-19 12:06:02 -07003181 if (llist_empty(&ctx->poll_llist) &&
3182 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3183 hash_del(&req->hash_node);
3184 io_poll_complete(req, mask, 0);
3185 req->flags |= REQ_F_COMP_LOCKED;
3186 io_put_req(req);
3187 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3188
3189 io_cqring_ev_posted(ctx);
3190 req = NULL;
3191 } else {
3192 req->result = mask;
3193 req->llist_node.next = NULL;
3194 /* if the list wasn't empty, we're done */
3195 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3196 req = NULL;
3197 else
3198 req->work.func = io_poll_flush;
3199 }
Jens Axboe8c838782019-03-12 15:48:16 -06003200 }
Jens Axboee94f1412019-12-19 12:06:02 -07003201 if (req)
3202 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003203
Jens Axboe221c5eb2019-01-17 09:41:58 -07003204 return 1;
3205}
3206
3207struct io_poll_table {
3208 struct poll_table_struct pt;
3209 struct io_kiocb *req;
3210 int error;
3211};
3212
3213static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3214 struct poll_table_struct *p)
3215{
3216 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3217
3218 if (unlikely(pt->req->poll.head)) {
3219 pt->error = -EINVAL;
3220 return;
3221 }
3222
3223 pt->error = 0;
3224 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003225 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003226}
3227
Jens Axboeeac406c2019-11-14 12:09:58 -07003228static void io_poll_req_insert(struct io_kiocb *req)
3229{
3230 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003231 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003232
Jens Axboe78076bb2019-12-04 19:56:40 -07003233 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3234 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003235}
3236
Jens Axboe3529d8c2019-12-19 18:24:38 -07003237static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003238{
3239 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003240 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003241
3242 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3243 return -EINVAL;
3244 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3245 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003246 if (!poll->file)
3247 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003248
Jens Axboe221c5eb2019-01-17 09:41:58 -07003249 events = READ_ONCE(sqe->poll_events);
3250 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003251 return 0;
3252}
3253
3254static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3255{
3256 struct io_poll_iocb *poll = &req->poll;
3257 struct io_ring_ctx *ctx = req->ctx;
3258 struct io_poll_table ipt;
3259 bool cancel = false;
3260 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003261
3262 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003263 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003264
Jens Axboe221c5eb2019-01-17 09:41:58 -07003265 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003266 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003267 poll->canceled = false;
3268
3269 ipt.pt._qproc = io_poll_queue_proc;
3270 ipt.pt._key = poll->events;
3271 ipt.req = req;
3272 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3273
3274 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003275 INIT_LIST_HEAD(&poll->wait.entry);
3276 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3277 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003278
Jens Axboe36703242019-07-25 10:20:18 -06003279 INIT_LIST_HEAD(&req->list);
3280
Jens Axboe221c5eb2019-01-17 09:41:58 -07003281 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003282
3283 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003284 if (likely(poll->head)) {
3285 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003286 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003287 if (ipt.error)
3288 cancel = true;
3289 ipt.error = 0;
3290 mask = 0;
3291 }
3292 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003293 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003294 else if (cancel)
3295 WRITE_ONCE(poll->canceled, true);
3296 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003297 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003298 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003299 }
Jens Axboe8c838782019-03-12 15:48:16 -06003300 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003301 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003302 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003303 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003304 spin_unlock_irq(&ctx->completion_lock);
3305
Jens Axboe8c838782019-03-12 15:48:16 -06003306 if (mask) {
3307 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003308 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003309 }
Jens Axboe8c838782019-03-12 15:48:16 -06003310 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003311}
3312
Jens Axboe5262f562019-09-17 12:26:57 -06003313static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3314{
Jens Axboead8a48a2019-11-15 08:49:11 -07003315 struct io_timeout_data *data = container_of(timer,
3316 struct io_timeout_data, timer);
3317 struct io_kiocb *req = data->req;
3318 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003319 unsigned long flags;
3320
Jens Axboe5262f562019-09-17 12:26:57 -06003321 atomic_inc(&ctx->cq_timeouts);
3322
3323 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003324 /*
Jens Axboe11365042019-10-16 09:08:32 -06003325 * We could be racing with timeout deletion. If the list is empty,
3326 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003327 */
Jens Axboe842f9612019-10-29 12:34:10 -06003328 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003329 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003330
Jens Axboe11365042019-10-16 09:08:32 -06003331 /*
3332 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003333 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003334 * pointer will be increased, otherwise other timeout reqs may
3335 * return in advance without waiting for enough wait_nr.
3336 */
3337 prev = req;
3338 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3339 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003340 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003341 }
Jens Axboe842f9612019-10-29 12:34:10 -06003342
Jens Axboe78e19bb2019-11-06 15:21:34 -07003343 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003344 io_commit_cqring(ctx);
3345 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3346
3347 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003348 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003349 io_put_req(req);
3350 return HRTIMER_NORESTART;
3351}
3352
Jens Axboe47f46762019-11-09 17:43:02 -07003353static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3354{
3355 struct io_kiocb *req;
3356 int ret = -ENOENT;
3357
3358 list_for_each_entry(req, &ctx->timeout_list, list) {
3359 if (user_data == req->user_data) {
3360 list_del_init(&req->list);
3361 ret = 0;
3362 break;
3363 }
3364 }
3365
3366 if (ret == -ENOENT)
3367 return ret;
3368
Jens Axboe2d283902019-12-04 11:08:05 -07003369 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003370 if (ret == -1)
3371 return -EALREADY;
3372
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003373 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003374 io_cqring_fill_event(req, -ECANCELED);
3375 io_put_req(req);
3376 return 0;
3377}
3378
Jens Axboe3529d8c2019-12-19 18:24:38 -07003379static int io_timeout_remove_prep(struct io_kiocb *req,
3380 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003381{
Jens Axboeb29472e2019-12-17 18:50:29 -07003382 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3383 return -EINVAL;
3384 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3385 return -EINVAL;
3386
3387 req->timeout.addr = READ_ONCE(sqe->addr);
3388 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3389 if (req->timeout.flags)
3390 return -EINVAL;
3391
Jens Axboeb29472e2019-12-17 18:50:29 -07003392 return 0;
3393}
3394
Jens Axboe11365042019-10-16 09:08:32 -06003395/*
3396 * Remove or update an existing timeout command
3397 */
Jens Axboefc4df992019-12-10 14:38:45 -07003398static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003399{
3400 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003401 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003402
Jens Axboe11365042019-10-16 09:08:32 -06003403 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003404 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003405
Jens Axboe47f46762019-11-09 17:43:02 -07003406 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003407 io_commit_cqring(ctx);
3408 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003409 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003410 if (ret < 0)
3411 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003412 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003413 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003414}
3415
Jens Axboe3529d8c2019-12-19 18:24:38 -07003416static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003417 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003418{
Jens Axboead8a48a2019-11-15 08:49:11 -07003419 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003420 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003421
Jens Axboead8a48a2019-11-15 08:49:11 -07003422 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003423 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003424 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003425 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003426 if (sqe->off && is_timeout_link)
3427 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003428 flags = READ_ONCE(sqe->timeout_flags);
3429 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003430 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003431
Jens Axboe26a61672019-12-20 09:02:01 -07003432 req->timeout.count = READ_ONCE(sqe->off);
3433
Jens Axboe3529d8c2019-12-19 18:24:38 -07003434 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003435 return -ENOMEM;
3436
3437 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003438 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003439 req->flags |= REQ_F_TIMEOUT;
3440
3441 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003442 return -EFAULT;
3443
Jens Axboe11365042019-10-16 09:08:32 -06003444 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003445 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003446 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003447 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003448
Jens Axboead8a48a2019-11-15 08:49:11 -07003449 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3450 return 0;
3451}
3452
Jens Axboefc4df992019-12-10 14:38:45 -07003453static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003454{
3455 unsigned count;
3456 struct io_ring_ctx *ctx = req->ctx;
3457 struct io_timeout_data *data;
3458 struct list_head *entry;
3459 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003460
Jens Axboe2d283902019-12-04 11:08:05 -07003461 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003462
Jens Axboe5262f562019-09-17 12:26:57 -06003463 /*
3464 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003465 * timeout event to be satisfied. If it isn't set, then this is
3466 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003467 */
Jens Axboe26a61672019-12-20 09:02:01 -07003468 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003469 if (!count) {
3470 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3471 spin_lock_irq(&ctx->completion_lock);
3472 entry = ctx->timeout_list.prev;
3473 goto add;
3474 }
Jens Axboe5262f562019-09-17 12:26:57 -06003475
3476 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003477 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003478
3479 /*
3480 * Insertion sort, ensuring the first entry in the list is always
3481 * the one we need first.
3482 */
Jens Axboe5262f562019-09-17 12:26:57 -06003483 spin_lock_irq(&ctx->completion_lock);
3484 list_for_each_prev(entry, &ctx->timeout_list) {
3485 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003486 unsigned nxt_sq_head;
3487 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003488 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003489
Jens Axboe93bd25b2019-11-11 23:34:31 -07003490 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3491 continue;
3492
yangerkun5da0fb12019-10-15 21:59:29 +08003493 /*
3494 * Since cached_sq_head + count - 1 can overflow, use type long
3495 * long to store it.
3496 */
3497 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003498 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3499 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003500
3501 /*
3502 * cached_sq_head may overflow, and it will never overflow twice
3503 * once there is some timeout req still be valid.
3504 */
3505 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003506 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003507
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003508 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003509 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003510
3511 /*
3512 * Sequence of reqs after the insert one and itself should
3513 * be adjusted because each timeout req consumes a slot.
3514 */
3515 span++;
3516 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003517 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003518 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003519add:
Jens Axboe5262f562019-09-17 12:26:57 -06003520 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003521 data->timer.function = io_timeout_fn;
3522 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003523 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003524 return 0;
3525}
3526
Jens Axboe62755e32019-10-28 21:49:21 -06003527static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003528{
Jens Axboe62755e32019-10-28 21:49:21 -06003529 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003530
Jens Axboe62755e32019-10-28 21:49:21 -06003531 return req->user_data == (unsigned long) data;
3532}
3533
Jens Axboee977d6d2019-11-05 12:39:45 -07003534static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003535{
Jens Axboe62755e32019-10-28 21:49:21 -06003536 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003537 int ret = 0;
3538
Jens Axboe62755e32019-10-28 21:49:21 -06003539 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3540 switch (cancel_ret) {
3541 case IO_WQ_CANCEL_OK:
3542 ret = 0;
3543 break;
3544 case IO_WQ_CANCEL_RUNNING:
3545 ret = -EALREADY;
3546 break;
3547 case IO_WQ_CANCEL_NOTFOUND:
3548 ret = -ENOENT;
3549 break;
3550 }
3551
Jens Axboee977d6d2019-11-05 12:39:45 -07003552 return ret;
3553}
3554
Jens Axboe47f46762019-11-09 17:43:02 -07003555static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3556 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003557 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003558{
3559 unsigned long flags;
3560 int ret;
3561
3562 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3563 if (ret != -ENOENT) {
3564 spin_lock_irqsave(&ctx->completion_lock, flags);
3565 goto done;
3566 }
3567
3568 spin_lock_irqsave(&ctx->completion_lock, flags);
3569 ret = io_timeout_cancel(ctx, sqe_addr);
3570 if (ret != -ENOENT)
3571 goto done;
3572 ret = io_poll_cancel(ctx, sqe_addr);
3573done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003574 if (!ret)
3575 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003576 io_cqring_fill_event(req, ret);
3577 io_commit_cqring(ctx);
3578 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3579 io_cqring_ev_posted(ctx);
3580
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003581 if (ret < 0)
3582 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003583 io_put_req_find_next(req, nxt);
3584}
3585
Jens Axboe3529d8c2019-12-19 18:24:38 -07003586static int io_async_cancel_prep(struct io_kiocb *req,
3587 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003588{
Jens Axboefbf23842019-12-17 18:45:56 -07003589 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003590 return -EINVAL;
3591 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3592 sqe->cancel_flags)
3593 return -EINVAL;
3594
Jens Axboefbf23842019-12-17 18:45:56 -07003595 req->cancel.addr = READ_ONCE(sqe->addr);
3596 return 0;
3597}
3598
3599static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3600{
3601 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003602
3603 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003604 return 0;
3605}
3606
Jens Axboe05f3fb32019-12-09 11:22:50 -07003607static int io_files_update_prep(struct io_kiocb *req,
3608 const struct io_uring_sqe *sqe)
3609{
3610 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3611 return -EINVAL;
3612
3613 req->files_update.offset = READ_ONCE(sqe->off);
3614 req->files_update.nr_args = READ_ONCE(sqe->len);
3615 if (!req->files_update.nr_args)
3616 return -EINVAL;
3617 req->files_update.arg = READ_ONCE(sqe->addr);
3618 return 0;
3619}
3620
3621static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3622{
3623 struct io_ring_ctx *ctx = req->ctx;
3624 struct io_uring_files_update up;
3625 int ret;
3626
3627 if (force_nonblock) {
3628 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3629 return -EAGAIN;
3630 }
3631
3632 up.offset = req->files_update.offset;
3633 up.fds = req->files_update.arg;
3634
3635 mutex_lock(&ctx->uring_lock);
3636 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3637 mutex_unlock(&ctx->uring_lock);
3638
3639 if (ret < 0)
3640 req_set_fail_links(req);
3641 io_cqring_add_event(req, ret);
3642 io_put_req(req);
3643 return 0;
3644}
3645
Jens Axboe3529d8c2019-12-19 18:24:38 -07003646static int io_req_defer_prep(struct io_kiocb *req,
3647 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003648{
Jens Axboee7815732019-12-17 19:45:06 -07003649 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003650
Jens Axboed625c6e2019-12-17 19:53:05 -07003651 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003652 case IORING_OP_NOP:
3653 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003654 case IORING_OP_READV:
3655 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003656 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003657 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003658 break;
3659 case IORING_OP_WRITEV:
3660 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003661 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003662 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003663 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003664 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003665 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003666 break;
3667 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003668 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003669 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003670 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003671 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003672 break;
3673 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003674 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003675 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003676 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003677 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003678 break;
3679 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003680 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003681 break;
Jens Axboef499a022019-12-02 16:28:46 -07003682 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003683 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003684 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003685 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003686 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003687 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003688 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003689 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003690 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003691 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003692 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003693 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003694 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003695 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003696 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003697 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003698 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003699 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003700 case IORING_OP_FALLOCATE:
3701 ret = io_fallocate_prep(req, sqe);
3702 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003703 case IORING_OP_OPENAT:
3704 ret = io_openat_prep(req, sqe);
3705 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003706 case IORING_OP_CLOSE:
3707 ret = io_close_prep(req, sqe);
3708 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003709 case IORING_OP_FILES_UPDATE:
3710 ret = io_files_update_prep(req, sqe);
3711 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003712 case IORING_OP_STATX:
3713 ret = io_statx_prep(req, sqe);
3714 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003715 default:
Jens Axboee7815732019-12-17 19:45:06 -07003716 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3717 req->opcode);
3718 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003719 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003720 }
3721
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003722 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003723}
3724
Jens Axboe3529d8c2019-12-19 18:24:38 -07003725static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003726{
Jackie Liua197f662019-11-08 08:09:12 -07003727 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003728 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003729
Bob Liu9d858b22019-11-13 18:06:25 +08003730 /* Still need defer if there is pending req in defer list. */
3731 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003732 return 0;
3733
Jens Axboe3529d8c2019-12-19 18:24:38 -07003734 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003735 return -EAGAIN;
3736
Jens Axboe3529d8c2019-12-19 18:24:38 -07003737 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003738 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003739 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003740
Jens Axboede0617e2019-04-06 21:51:27 -06003741 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003742 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003743 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003744 return 0;
3745 }
3746
Jens Axboe915967f2019-11-21 09:01:20 -07003747 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003748 list_add_tail(&req->list, &ctx->defer_list);
3749 spin_unlock_irq(&ctx->completion_lock);
3750 return -EIOCBQUEUED;
3751}
3752
Jens Axboe3529d8c2019-12-19 18:24:38 -07003753static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3754 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003755{
Jackie Liua197f662019-11-08 08:09:12 -07003756 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07003757 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003758
Jens Axboed625c6e2019-12-17 19:53:05 -07003759 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003760 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003761 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003762 break;
3763 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003764 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003765 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003766 if (sqe) {
3767 ret = io_read_prep(req, sqe, force_nonblock);
3768 if (ret < 0)
3769 break;
3770 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003771 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003772 break;
3773 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07003774 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003775 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003776 if (sqe) {
3777 ret = io_write_prep(req, sqe, force_nonblock);
3778 if (ret < 0)
3779 break;
3780 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003781 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003782 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003783 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003784 if (sqe) {
3785 ret = io_prep_fsync(req, sqe);
3786 if (ret < 0)
3787 break;
3788 }
Jens Axboefc4df992019-12-10 14:38:45 -07003789 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003790 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003791 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003792 if (sqe) {
3793 ret = io_poll_add_prep(req, sqe);
3794 if (ret)
3795 break;
3796 }
Jens Axboefc4df992019-12-10 14:38:45 -07003797 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003798 break;
3799 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003800 if (sqe) {
3801 ret = io_poll_remove_prep(req, sqe);
3802 if (ret < 0)
3803 break;
3804 }
Jens Axboefc4df992019-12-10 14:38:45 -07003805 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003806 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003807 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003808 if (sqe) {
3809 ret = io_prep_sfr(req, sqe);
3810 if (ret < 0)
3811 break;
3812 }
Jens Axboefc4df992019-12-10 14:38:45 -07003813 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003814 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003815 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003816 if (sqe) {
3817 ret = io_sendmsg_prep(req, sqe);
3818 if (ret < 0)
3819 break;
3820 }
Jens Axboefc4df992019-12-10 14:38:45 -07003821 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003822 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003823 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003824 if (sqe) {
3825 ret = io_recvmsg_prep(req, sqe);
3826 if (ret)
3827 break;
3828 }
Jens Axboefc4df992019-12-10 14:38:45 -07003829 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003830 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003831 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003832 if (sqe) {
3833 ret = io_timeout_prep(req, sqe, false);
3834 if (ret)
3835 break;
3836 }
Jens Axboefc4df992019-12-10 14:38:45 -07003837 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003838 break;
Jens Axboe11365042019-10-16 09:08:32 -06003839 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003840 if (sqe) {
3841 ret = io_timeout_remove_prep(req, sqe);
3842 if (ret)
3843 break;
3844 }
Jens Axboefc4df992019-12-10 14:38:45 -07003845 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003846 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003847 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003848 if (sqe) {
3849 ret = io_accept_prep(req, sqe);
3850 if (ret)
3851 break;
3852 }
Jens Axboefc4df992019-12-10 14:38:45 -07003853 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003854 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003855 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003856 if (sqe) {
3857 ret = io_connect_prep(req, sqe);
3858 if (ret)
3859 break;
3860 }
Jens Axboefc4df992019-12-10 14:38:45 -07003861 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003862 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003863 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003864 if (sqe) {
3865 ret = io_async_cancel_prep(req, sqe);
3866 if (ret)
3867 break;
3868 }
Jens Axboefc4df992019-12-10 14:38:45 -07003869 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003870 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003871 case IORING_OP_FALLOCATE:
3872 if (sqe) {
3873 ret = io_fallocate_prep(req, sqe);
3874 if (ret)
3875 break;
3876 }
3877 ret = io_fallocate(req, nxt, force_nonblock);
3878 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003879 case IORING_OP_OPENAT:
3880 if (sqe) {
3881 ret = io_openat_prep(req, sqe);
3882 if (ret)
3883 break;
3884 }
3885 ret = io_openat(req, nxt, force_nonblock);
3886 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003887 case IORING_OP_CLOSE:
3888 if (sqe) {
3889 ret = io_close_prep(req, sqe);
3890 if (ret)
3891 break;
3892 }
3893 ret = io_close(req, nxt, force_nonblock);
3894 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003895 case IORING_OP_FILES_UPDATE:
3896 if (sqe) {
3897 ret = io_files_update_prep(req, sqe);
3898 if (ret)
3899 break;
3900 }
3901 ret = io_files_update(req, force_nonblock);
3902 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003903 case IORING_OP_STATX:
3904 if (sqe) {
3905 ret = io_statx_prep(req, sqe);
3906 if (ret)
3907 break;
3908 }
3909 ret = io_statx(req, nxt, force_nonblock);
3910 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003911 default:
3912 ret = -EINVAL;
3913 break;
3914 }
3915
Jens Axboedef596e2019-01-09 08:59:42 -07003916 if (ret)
3917 return ret;
3918
3919 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07003920 const bool in_async = io_wq_current_is_worker();
3921
Jens Axboe9e645e112019-05-10 16:07:28 -06003922 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07003923 return -EAGAIN;
3924
Jens Axboe11ba8202020-01-15 21:51:17 -07003925 /* workqueue context doesn't hold uring_lock, grab it now */
3926 if (in_async)
3927 mutex_lock(&ctx->uring_lock);
3928
Jens Axboedef596e2019-01-09 08:59:42 -07003929 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07003930
3931 if (in_async)
3932 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07003933 }
3934
3935 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003936}
3937
Jens Axboe561fb042019-10-24 07:25:42 -06003938static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003939{
Jens Axboe561fb042019-10-24 07:25:42 -06003940 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003941 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003942 struct io_kiocb *nxt = NULL;
3943 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003944
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003945 /* if NO_CANCEL is set, we must still run the work */
3946 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
3947 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003948 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003949 }
Jens Axboe31b51512019-01-18 22:56:34 -07003950
Jens Axboe561fb042019-10-24 07:25:42 -06003951 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003952 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3953 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003954 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003955 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003956 /*
3957 * We can get EAGAIN for polled IO even though we're
3958 * forcing a sync submission from here, since we can't
3959 * wait for request slots on the block side.
3960 */
3961 if (ret != -EAGAIN)
3962 break;
3963 cond_resched();
3964 } while (1);
3965 }
Jens Axboe31b51512019-01-18 22:56:34 -07003966
Jens Axboe561fb042019-10-24 07:25:42 -06003967 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003968 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003969
Jens Axboe561fb042019-10-24 07:25:42 -06003970 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003971 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003972 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003973 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003974 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003975
Jens Axboe561fb042019-10-24 07:25:42 -06003976 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07003977 if (!ret && nxt)
3978 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07003979}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003980
Jens Axboe15b71ab2019-12-11 11:20:36 -07003981static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06003982{
Jens Axboed3656342019-12-18 09:50:26 -07003983 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07003984 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07003985 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
3986 return 0;
3987 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06003988}
3989
Jens Axboe65e19f52019-10-26 07:20:21 -06003990static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3991 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003992{
Jens Axboe65e19f52019-10-26 07:20:21 -06003993 struct fixed_file_table *table;
3994
Jens Axboe05f3fb32019-12-09 11:22:50 -07003995 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
3996 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06003997}
3998
Jens Axboe3529d8c2019-12-19 18:24:38 -07003999static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4000 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004001{
Jackie Liua197f662019-11-08 08:09:12 -07004002 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004003 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004004 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004005
Jens Axboe3529d8c2019-12-19 18:24:38 -07004006 flags = READ_ONCE(sqe->flags);
4007 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004008
Jackie Liu4fe2c962019-09-09 20:50:40 +08004009 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06004010 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06004011
Jens Axboed3656342019-12-18 09:50:26 -07004012 if (!io_req_needs_file(req, fd))
4013 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004014
4015 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004016 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004017 (unsigned) fd >= ctx->nr_user_files))
4018 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004019 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004020 req->file = io_file_from_index(ctx, fd);
4021 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004022 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004023 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004024 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004025 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004026 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004027 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004028 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004029 req->file = io_file_get(state, fd);
4030 if (unlikely(!req->file))
4031 return -EBADF;
4032 }
4033
4034 return 0;
4035}
4036
Jackie Liua197f662019-11-08 08:09:12 -07004037static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004038{
Jens Axboefcb323c2019-10-24 12:39:47 -06004039 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004040 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004041
Jens Axboeb5dba592019-12-11 14:02:38 -07004042 if (!req->ring_file)
4043 return -EBADF;
4044
Jens Axboefcb323c2019-10-24 12:39:47 -06004045 rcu_read_lock();
4046 spin_lock_irq(&ctx->inflight_lock);
4047 /*
4048 * We use the f_ops->flush() handler to ensure that we can flush
4049 * out work accessing these files if the fd is closed. Check if
4050 * the fd has changed since we started down this path, and disallow
4051 * this operation if it has.
4052 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004053 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004054 list_add(&req->inflight_entry, &ctx->inflight_list);
4055 req->flags |= REQ_F_INFLIGHT;
4056 req->work.files = current->files;
4057 ret = 0;
4058 }
4059 spin_unlock_irq(&ctx->inflight_lock);
4060 rcu_read_unlock();
4061
4062 return ret;
4063}
4064
Jens Axboe2665abf2019-11-05 12:40:47 -07004065static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4066{
Jens Axboead8a48a2019-11-15 08:49:11 -07004067 struct io_timeout_data *data = container_of(timer,
4068 struct io_timeout_data, timer);
4069 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004070 struct io_ring_ctx *ctx = req->ctx;
4071 struct io_kiocb *prev = NULL;
4072 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004073
4074 spin_lock_irqsave(&ctx->completion_lock, flags);
4075
4076 /*
4077 * We don't expect the list to be empty, that will only happen if we
4078 * race with the completion of the linked work.
4079 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004080 if (!list_empty(&req->link_list)) {
4081 prev = list_entry(req->link_list.prev, struct io_kiocb,
4082 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004083 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004084 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004085 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4086 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004087 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004088 }
4089
4090 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4091
4092 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004093 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004094 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4095 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004096 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004097 } else {
4098 io_cqring_add_event(req, -ETIME);
4099 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004100 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004101 return HRTIMER_NORESTART;
4102}
4103
Jens Axboead8a48a2019-11-15 08:49:11 -07004104static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004105{
Jens Axboe76a46e02019-11-10 23:34:16 -07004106 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004107
Jens Axboe76a46e02019-11-10 23:34:16 -07004108 /*
4109 * If the list is now empty, then our linked request finished before
4110 * we got a chance to setup the timer
4111 */
4112 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004113 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004114 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004115
Jens Axboead8a48a2019-11-15 08:49:11 -07004116 data->timer.function = io_link_timeout_fn;
4117 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4118 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004119 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004120 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004121
Jens Axboe2665abf2019-11-05 12:40:47 -07004122 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004123 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004124}
4125
Jens Axboead8a48a2019-11-15 08:49:11 -07004126static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004127{
4128 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004129
Jens Axboe2665abf2019-11-05 12:40:47 -07004130 if (!(req->flags & REQ_F_LINK))
4131 return NULL;
4132
Pavel Begunkov44932332019-12-05 16:16:35 +03004133 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4134 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004135 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004136 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004137
Jens Axboe76a46e02019-11-10 23:34:16 -07004138 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004139 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004140}
4141
Jens Axboe3529d8c2019-12-19 18:24:38 -07004142static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004143{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004144 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004145 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004146 int ret;
4147
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004148again:
4149 linked_timeout = io_prep_linked_timeout(req);
4150
Jens Axboe3529d8c2019-12-19 18:24:38 -07004151 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004152
4153 /*
4154 * We async punt it if the file wasn't marked NOWAIT, or if the file
4155 * doesn't support non-blocking read/write attempts
4156 */
4157 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4158 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004159 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4160 ret = io_grab_files(req);
4161 if (ret)
4162 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004163 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004164
4165 /*
4166 * Queued up for async execution, worker will release
4167 * submit reference when the iocb is actually submitted.
4168 */
4169 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004170 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004171 }
Jens Axboee65ef562019-03-12 10:16:44 -06004172
Jens Axboefcb323c2019-10-24 12:39:47 -06004173err:
Jens Axboee65ef562019-03-12 10:16:44 -06004174 /* drop submission reference */
4175 io_put_req(req);
4176
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004177 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004178 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004179 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004180 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004181 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004182 }
4183
Jens Axboee65ef562019-03-12 10:16:44 -06004184 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004185 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004186 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004187 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004188 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004189 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004190done_req:
4191 if (nxt) {
4192 req = nxt;
4193 nxt = NULL;
4194 goto again;
4195 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004196}
4197
Jens Axboe3529d8c2019-12-19 18:24:38 -07004198static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004199{
4200 int ret;
4201
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004202 if (unlikely(req->ctx->drain_next)) {
4203 req->flags |= REQ_F_IO_DRAIN;
4204 req->ctx->drain_next = false;
4205 }
4206 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
4207
Jens Axboe3529d8c2019-12-19 18:24:38 -07004208 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004209 if (ret) {
4210 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004211 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004212 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004213 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004214 }
Jens Axboece35a472019-12-17 08:04:44 -07004215 } else if ((req->flags & REQ_F_FORCE_ASYNC) &&
4216 !io_wq_current_is_worker()) {
4217 /*
4218 * Never try inline submit of IOSQE_ASYNC is set, go straight
4219 * to async execution.
4220 */
4221 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4222 io_queue_async_work(req);
4223 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004224 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004225 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004226}
4227
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004228static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004229{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004230 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004231 io_cqring_add_event(req, -ECANCELED);
4232 io_double_put_req(req);
4233 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004234 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004235}
4236
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004237#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004238 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004239
Jens Axboe3529d8c2019-12-19 18:24:38 -07004240static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4241 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004242{
Jackie Liua197f662019-11-08 08:09:12 -07004243 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004244 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004245 int ret;
4246
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004247 sqe_flags = READ_ONCE(sqe->flags);
4248
Jens Axboe9e645e112019-05-10 16:07:28 -06004249 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004250 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004251 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004252 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004253 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004254 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004255 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004256
Jens Axboe3529d8c2019-12-19 18:24:38 -07004257 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004258 if (unlikely(ret)) {
4259err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004260 io_cqring_add_event(req, ret);
4261 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004262 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004263 }
4264
Jens Axboe9e645e112019-05-10 16:07:28 -06004265 /*
4266 * If we already have a head request, queue this one for async
4267 * submittal once the head completes. If we don't have a head but
4268 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4269 * submitted sync once the chain is complete. If none of those
4270 * conditions are true (normal request), then just queue it.
4271 */
4272 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004273 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004274
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004275 if (sqe_flags & IOSQE_IO_DRAIN)
Pavel Begunkov9d763772019-12-17 02:22:07 +03004276 head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004277
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004278 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004279 req->flags |= REQ_F_HARDLINK;
4280
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004281 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004282 ret = -EAGAIN;
4283 goto err_req;
4284 }
4285
Jens Axboe3529d8c2019-12-19 18:24:38 -07004286 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004287 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004288 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004289 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004290 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004291 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004292 trace_io_uring_link(ctx, req, head);
4293 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004294
4295 /* last request of a link, enqueue the link */
4296 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4297 io_queue_link_head(head);
4298 *link = NULL;
4299 }
4300 } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004301 req->flags |= REQ_F_LINK;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004302 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004303 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004304
Jens Axboe9e645e112019-05-10 16:07:28 -06004305 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004306 ret = io_req_defer_prep(req, sqe);
4307 if (ret)
4308 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004309 *link = req;
4310 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004311 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004312 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004313
4314 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004315}
4316
Jens Axboe9a56a232019-01-09 09:06:50 -07004317/*
4318 * Batched submission is done, ensure local IO is flushed out.
4319 */
4320static void io_submit_state_end(struct io_submit_state *state)
4321{
4322 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004323 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004324 if (state->free_reqs)
4325 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4326 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004327}
4328
4329/*
4330 * Start submission side cache.
4331 */
4332static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004333 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004334{
4335 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004336 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004337 state->file = NULL;
4338 state->ios_left = max_ios;
4339}
4340
Jens Axboe2b188cc2019-01-07 10:46:33 -07004341static void io_commit_sqring(struct io_ring_ctx *ctx)
4342{
Hristo Venev75b28af2019-08-26 17:23:46 +00004343 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004344
Hristo Venev75b28af2019-08-26 17:23:46 +00004345 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004346 /*
4347 * Ensure any loads from the SQEs are done at this point,
4348 * since once we write the new head, the application could
4349 * write new data to them.
4350 */
Hristo Venev75b28af2019-08-26 17:23:46 +00004351 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004352 }
4353}
4354
4355/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004356 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004357 * that is mapped by userspace. This means that care needs to be taken to
4358 * ensure that reads are stable, as we cannot rely on userspace always
4359 * being a good citizen. If members of the sqe are validated and then later
4360 * used, it's important that those reads are done through READ_ONCE() to
4361 * prevent a re-load down the line.
4362 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004363static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4364 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004365{
Hristo Venev75b28af2019-08-26 17:23:46 +00004366 struct io_rings *rings = ctx->rings;
4367 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004368 unsigned head;
4369
4370 /*
4371 * The cached sq head (or cq tail) serves two purposes:
4372 *
4373 * 1) allows us to batch the cost of updating the user visible
4374 * head updates.
4375 * 2) allows the kernel side to track the head on its own, even
4376 * though the application is the one updating it.
4377 */
4378 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02004379 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004380 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004381 return false;
4382
Hristo Venev75b28af2019-08-26 17:23:46 +00004383 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004384 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004385 /*
4386 * All io need record the previous position, if LINK vs DARIN,
4387 * it can be used to mark the position of the first IO in the
4388 * link list.
4389 */
4390 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004391 *sqe_ptr = &ctx->sq_sqes[head];
4392 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4393 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004394 ctx->cached_sq_head++;
4395 return true;
4396 }
4397
4398 /* drop invalid entries */
4399 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004400 ctx->cached_sq_dropped++;
4401 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004402 return false;
4403}
4404
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004405static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004406 struct file *ring_file, int ring_fd,
4407 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004408{
4409 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004410 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004411 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004412 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004413
Jens Axboec4a2ed72019-11-21 21:01:26 -07004414 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004415 if (test_bit(0, &ctx->sq_check_overflow)) {
4416 if (!list_empty(&ctx->cq_overflow_list) &&
4417 !io_cqring_overflow_flush(ctx, false))
4418 return -EBUSY;
4419 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004420
4421 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004422 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004423 statep = &state;
4424 }
4425
4426 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004427 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004428 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004429
Pavel Begunkov196be952019-11-07 01:41:06 +03004430 req = io_get_req(ctx, statep);
4431 if (unlikely(!req)) {
4432 if (!submitted)
4433 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004434 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004435 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004436 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03004437 __io_free_req(req);
4438 break;
4439 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004440
Jens Axboed3656342019-12-18 09:50:26 -07004441 /* will complete beyond this point, count as submitted */
4442 submitted++;
4443
4444 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4445 io_cqring_add_event(req, -EINVAL);
4446 io_double_put_req(req);
4447 break;
4448 }
4449
4450 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004451 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4452 if (!mm_fault) {
4453 use_mm(ctx->sqo_mm);
4454 *mm = ctx->sqo_mm;
4455 }
4456 }
4457
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004458 req->ring_file = ring_file;
4459 req->ring_fd = ring_fd;
4460 req->has_user = *mm != NULL;
4461 req->in_async = async;
4462 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07004463 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004464 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004465 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004466 }
4467
Jens Axboe9e645e112019-05-10 16:07:28 -06004468 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004469 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004470 if (statep)
4471 io_submit_state_end(&state);
4472
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004473 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4474 io_commit_sqring(ctx);
4475
Jens Axboe6c271ce2019-01-10 11:22:30 -07004476 return submitted;
4477}
4478
4479static int io_sq_thread(void *data)
4480{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004481 struct io_ring_ctx *ctx = data;
4482 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004483 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004484 mm_segment_t old_fs;
4485 DEFINE_WAIT(wait);
4486 unsigned inflight;
4487 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004488 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004489
Jens Axboe206aefd2019-11-07 18:27:42 -07004490 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004491
Jens Axboe6c271ce2019-01-10 11:22:30 -07004492 old_fs = get_fs();
4493 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004494 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004495
Jens Axboec1edbf52019-11-10 16:56:04 -07004496 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004497 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004498 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004499
4500 if (inflight) {
4501 unsigned nr_events = 0;
4502
4503 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004504 /*
4505 * inflight is the count of the maximum possible
4506 * entries we submitted, but it can be smaller
4507 * if we dropped some of them. If we don't have
4508 * poll entries available, then we know that we
4509 * have nothing left to poll for. Reset the
4510 * inflight count to zero in that case.
4511 */
4512 mutex_lock(&ctx->uring_lock);
4513 if (!list_empty(&ctx->poll_list))
4514 __io_iopoll_check(ctx, &nr_events, 0);
4515 else
4516 inflight = 0;
4517 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004518 } else {
4519 /*
4520 * Normal IO, just pretend everything completed.
4521 * We don't have to poll completions for that.
4522 */
4523 nr_events = inflight;
4524 }
4525
4526 inflight -= nr_events;
4527 if (!inflight)
4528 timeout = jiffies + ctx->sq_thread_idle;
4529 }
4530
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004531 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004532
4533 /*
4534 * If submit got -EBUSY, flag us as needing the application
4535 * to enter the kernel to reap and flush events.
4536 */
4537 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004538 /*
4539 * We're polling. If we're within the defined idle
4540 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004541 * to sleep. The exception is if we got EBUSY doing
4542 * more IO, we should wait for the application to
4543 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004544 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004545 if (inflight ||
4546 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004547 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004548 continue;
4549 }
4550
4551 /*
4552 * Drop cur_mm before scheduling, we can't hold it for
4553 * long periods (or over schedule()). Do this before
4554 * adding ourselves to the waitqueue, as the unuse/drop
4555 * may sleep.
4556 */
4557 if (cur_mm) {
4558 unuse_mm(cur_mm);
4559 mmput(cur_mm);
4560 cur_mm = NULL;
4561 }
4562
4563 prepare_to_wait(&ctx->sqo_wait, &wait,
4564 TASK_INTERRUPTIBLE);
4565
4566 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004567 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004568 /* make sure to read SQ tail after writing flags */
4569 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004570
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004571 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004572 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004573 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004574 finish_wait(&ctx->sqo_wait, &wait);
4575 break;
4576 }
4577 if (signal_pending(current))
4578 flush_signals(current);
4579 schedule();
4580 finish_wait(&ctx->sqo_wait, &wait);
4581
Hristo Venev75b28af2019-08-26 17:23:46 +00004582 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004583 continue;
4584 }
4585 finish_wait(&ctx->sqo_wait, &wait);
4586
Hristo Venev75b28af2019-08-26 17:23:46 +00004587 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004588 }
4589
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004590 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004591 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004592 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004593 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004594 if (ret > 0)
4595 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004596 }
4597
4598 set_fs(old_fs);
4599 if (cur_mm) {
4600 unuse_mm(cur_mm);
4601 mmput(cur_mm);
4602 }
Jens Axboe181e4482019-11-25 08:52:30 -07004603 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004604
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004605 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004606
Jens Axboe6c271ce2019-01-10 11:22:30 -07004607 return 0;
4608}
4609
Jens Axboebda52162019-09-24 13:47:15 -06004610struct io_wait_queue {
4611 struct wait_queue_entry wq;
4612 struct io_ring_ctx *ctx;
4613 unsigned to_wait;
4614 unsigned nr_timeouts;
4615};
4616
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004617static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004618{
4619 struct io_ring_ctx *ctx = iowq->ctx;
4620
4621 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004622 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004623 * started waiting. For timeouts, we always want to return to userspace,
4624 * regardless of event count.
4625 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004626 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004627 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4628}
4629
4630static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4631 int wake_flags, void *key)
4632{
4633 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4634 wq);
4635
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004636 /* use noflush == true, as we can't safely rely on locking context */
4637 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004638 return -1;
4639
4640 return autoremove_wake_function(curr, mode, wake_flags, key);
4641}
4642
Jens Axboe2b188cc2019-01-07 10:46:33 -07004643/*
4644 * Wait until events become available, if we don't already have some. The
4645 * application must reap them itself, as they reside on the shared cq ring.
4646 */
4647static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4648 const sigset_t __user *sig, size_t sigsz)
4649{
Jens Axboebda52162019-09-24 13:47:15 -06004650 struct io_wait_queue iowq = {
4651 .wq = {
4652 .private = current,
4653 .func = io_wake_function,
4654 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4655 },
4656 .ctx = ctx,
4657 .to_wait = min_events,
4658 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004659 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004660 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004661
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004662 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004663 return 0;
4664
4665 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004666#ifdef CONFIG_COMPAT
4667 if (in_compat_syscall())
4668 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004669 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004670 else
4671#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004672 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004673
Jens Axboe2b188cc2019-01-07 10:46:33 -07004674 if (ret)
4675 return ret;
4676 }
4677
Jens Axboebda52162019-09-24 13:47:15 -06004678 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004679 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004680 do {
4681 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4682 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004683 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004684 break;
4685 schedule();
4686 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004687 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004688 break;
4689 }
4690 } while (1);
4691 finish_wait(&ctx->wait, &iowq.wq);
4692
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004693 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004694
Hristo Venev75b28af2019-08-26 17:23:46 +00004695 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004696}
4697
Jens Axboe6b063142019-01-10 22:13:58 -07004698static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4699{
4700#if defined(CONFIG_UNIX)
4701 if (ctx->ring_sock) {
4702 struct sock *sock = ctx->ring_sock->sk;
4703 struct sk_buff *skb;
4704
4705 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4706 kfree_skb(skb);
4707 }
4708#else
4709 int i;
4710
Jens Axboe65e19f52019-10-26 07:20:21 -06004711 for (i = 0; i < ctx->nr_user_files; i++) {
4712 struct file *file;
4713
4714 file = io_file_from_index(ctx, i);
4715 if (file)
4716 fput(file);
4717 }
Jens Axboe6b063142019-01-10 22:13:58 -07004718#endif
4719}
4720
Jens Axboe05f3fb32019-12-09 11:22:50 -07004721static void io_file_ref_kill(struct percpu_ref *ref)
4722{
4723 struct fixed_file_data *data;
4724
4725 data = container_of(ref, struct fixed_file_data, refs);
4726 complete(&data->done);
4727}
4728
Jens Axboe6b063142019-01-10 22:13:58 -07004729static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4730{
Jens Axboe05f3fb32019-12-09 11:22:50 -07004731 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06004732 unsigned nr_tables, i;
4733
Jens Axboe05f3fb32019-12-09 11:22:50 -07004734 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07004735 return -ENXIO;
4736
Jens Axboe05f3fb32019-12-09 11:22:50 -07004737 /* protect against inflight atomic switch, which drops the ref */
4738 flush_work(&data->ref_work);
4739 percpu_ref_get(&data->refs);
4740 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
4741 wait_for_completion(&data->done);
4742 percpu_ref_put(&data->refs);
4743 percpu_ref_exit(&data->refs);
4744
Jens Axboe6b063142019-01-10 22:13:58 -07004745 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004746 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4747 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004748 kfree(data->table[i].files);
4749 kfree(data->table);
4750 kfree(data);
4751 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004752 ctx->nr_user_files = 0;
4753 return 0;
4754}
4755
Jens Axboe6c271ce2019-01-10 11:22:30 -07004756static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4757{
4758 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004759 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004760 /*
4761 * The park is a bit of a work-around, without it we get
4762 * warning spews on shutdown with SQPOLL set and affinity
4763 * set to a single CPU.
4764 */
Jens Axboe06058632019-04-13 09:26:03 -06004765 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004766 kthread_stop(ctx->sqo_thread);
4767 ctx->sqo_thread = NULL;
4768 }
4769}
4770
Jens Axboe6b063142019-01-10 22:13:58 -07004771static void io_finish_async(struct io_ring_ctx *ctx)
4772{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004773 io_sq_thread_stop(ctx);
4774
Jens Axboe561fb042019-10-24 07:25:42 -06004775 if (ctx->io_wq) {
4776 io_wq_destroy(ctx->io_wq);
4777 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004778 }
4779}
4780
4781#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07004782/*
4783 * Ensure the UNIX gc is aware of our file set, so we are certain that
4784 * the io_uring can be safely unregistered on process exit, even if we have
4785 * loops in the file referencing.
4786 */
4787static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4788{
4789 struct sock *sk = ctx->ring_sock->sk;
4790 struct scm_fp_list *fpl;
4791 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004792 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004793
4794 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4795 unsigned long inflight = ctx->user->unix_inflight + nr;
4796
4797 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4798 return -EMFILE;
4799 }
4800
4801 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4802 if (!fpl)
4803 return -ENOMEM;
4804
4805 skb = alloc_skb(0, GFP_KERNEL);
4806 if (!skb) {
4807 kfree(fpl);
4808 return -ENOMEM;
4809 }
4810
4811 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004812
Jens Axboe08a45172019-10-03 08:11:03 -06004813 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004814 fpl->user = get_uid(ctx->user);
4815 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004816 struct file *file = io_file_from_index(ctx, i + offset);
4817
4818 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004819 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004820 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004821 unix_inflight(fpl->user, fpl->fp[nr_files]);
4822 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004823 }
4824
Jens Axboe08a45172019-10-03 08:11:03 -06004825 if (nr_files) {
4826 fpl->max = SCM_MAX_FD;
4827 fpl->count = nr_files;
4828 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004829 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06004830 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4831 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004832
Jens Axboe08a45172019-10-03 08:11:03 -06004833 for (i = 0; i < nr_files; i++)
4834 fput(fpl->fp[i]);
4835 } else {
4836 kfree_skb(skb);
4837 kfree(fpl);
4838 }
Jens Axboe6b063142019-01-10 22:13:58 -07004839
4840 return 0;
4841}
4842
4843/*
4844 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4845 * causes regular reference counting to break down. We rely on the UNIX
4846 * garbage collection to take care of this problem for us.
4847 */
4848static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4849{
4850 unsigned left, total;
4851 int ret = 0;
4852
4853 total = 0;
4854 left = ctx->nr_user_files;
4855 while (left) {
4856 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004857
4858 ret = __io_sqe_files_scm(ctx, this_files, total);
4859 if (ret)
4860 break;
4861 left -= this_files;
4862 total += this_files;
4863 }
4864
4865 if (!ret)
4866 return 0;
4867
4868 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004869 struct file *file = io_file_from_index(ctx, total);
4870
4871 if (file)
4872 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004873 total++;
4874 }
4875
4876 return ret;
4877}
4878#else
4879static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4880{
4881 return 0;
4882}
4883#endif
4884
Jens Axboe65e19f52019-10-26 07:20:21 -06004885static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4886 unsigned nr_files)
4887{
4888 int i;
4889
4890 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004891 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004892 unsigned this_files;
4893
4894 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4895 table->files = kcalloc(this_files, sizeof(struct file *),
4896 GFP_KERNEL);
4897 if (!table->files)
4898 break;
4899 nr_files -= this_files;
4900 }
4901
4902 if (i == nr_tables)
4903 return 0;
4904
4905 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004906 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004907 kfree(table->files);
4908 }
4909 return 1;
4910}
4911
Jens Axboe05f3fb32019-12-09 11:22:50 -07004912static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06004913{
4914#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06004915 struct sock *sock = ctx->ring_sock->sk;
4916 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4917 struct sk_buff *skb;
4918 int i;
4919
4920 __skb_queue_head_init(&list);
4921
4922 /*
4923 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4924 * remove this entry and rearrange the file array.
4925 */
4926 skb = skb_dequeue(head);
4927 while (skb) {
4928 struct scm_fp_list *fp;
4929
4930 fp = UNIXCB(skb).fp;
4931 for (i = 0; i < fp->count; i++) {
4932 int left;
4933
4934 if (fp->fp[i] != file)
4935 continue;
4936
4937 unix_notinflight(fp->user, fp->fp[i]);
4938 left = fp->count - 1 - i;
4939 if (left) {
4940 memmove(&fp->fp[i], &fp->fp[i + 1],
4941 left * sizeof(struct file *));
4942 }
4943 fp->count--;
4944 if (!fp->count) {
4945 kfree_skb(skb);
4946 skb = NULL;
4947 } else {
4948 __skb_queue_tail(&list, skb);
4949 }
4950 fput(file);
4951 file = NULL;
4952 break;
4953 }
4954
4955 if (!file)
4956 break;
4957
4958 __skb_queue_tail(&list, skb);
4959
4960 skb = skb_dequeue(head);
4961 }
4962
4963 if (skb_peek(&list)) {
4964 spin_lock_irq(&head->lock);
4965 while ((skb = __skb_dequeue(&list)) != NULL)
4966 __skb_queue_tail(head, skb);
4967 spin_unlock_irq(&head->lock);
4968 }
4969#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07004970 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06004971#endif
4972}
4973
Jens Axboe05f3fb32019-12-09 11:22:50 -07004974struct io_file_put {
4975 struct llist_node llist;
4976 struct file *file;
4977 struct completion *done;
4978};
4979
4980static void io_ring_file_ref_switch(struct work_struct *work)
4981{
4982 struct io_file_put *pfile, *tmp;
4983 struct fixed_file_data *data;
4984 struct llist_node *node;
4985
4986 data = container_of(work, struct fixed_file_data, ref_work);
4987
4988 while ((node = llist_del_all(&data->put_llist)) != NULL) {
4989 llist_for_each_entry_safe(pfile, tmp, node, llist) {
4990 io_ring_file_put(data->ctx, pfile->file);
4991 if (pfile->done)
4992 complete(pfile->done);
4993 else
4994 kfree(pfile);
4995 }
4996 }
4997
4998 percpu_ref_get(&data->refs);
4999 percpu_ref_switch_to_percpu(&data->refs);
5000}
5001
5002static void io_file_data_ref_zero(struct percpu_ref *ref)
5003{
5004 struct fixed_file_data *data;
5005
5006 data = container_of(ref, struct fixed_file_data, refs);
5007
5008 /* we can't safely switch from inside this context, punt to wq */
5009 queue_work(system_wq, &data->ref_work);
5010}
5011
5012static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5013 unsigned nr_args)
5014{
5015 __s32 __user *fds = (__s32 __user *) arg;
5016 unsigned nr_tables;
5017 struct file *file;
5018 int fd, ret = 0;
5019 unsigned i;
5020
5021 if (ctx->file_data)
5022 return -EBUSY;
5023 if (!nr_args)
5024 return -EINVAL;
5025 if (nr_args > IORING_MAX_FIXED_FILES)
5026 return -EMFILE;
5027
5028 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5029 if (!ctx->file_data)
5030 return -ENOMEM;
5031 ctx->file_data->ctx = ctx;
5032 init_completion(&ctx->file_data->done);
5033
5034 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5035 ctx->file_data->table = kcalloc(nr_tables,
5036 sizeof(struct fixed_file_table),
5037 GFP_KERNEL);
5038 if (!ctx->file_data->table) {
5039 kfree(ctx->file_data);
5040 ctx->file_data = NULL;
5041 return -ENOMEM;
5042 }
5043
5044 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5045 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5046 kfree(ctx->file_data->table);
5047 kfree(ctx->file_data);
5048 ctx->file_data = NULL;
5049 return -ENOMEM;
5050 }
5051 ctx->file_data->put_llist.first = NULL;
5052 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5053
5054 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5055 percpu_ref_exit(&ctx->file_data->refs);
5056 kfree(ctx->file_data->table);
5057 kfree(ctx->file_data);
5058 ctx->file_data = NULL;
5059 return -ENOMEM;
5060 }
5061
5062 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5063 struct fixed_file_table *table;
5064 unsigned index;
5065
5066 ret = -EFAULT;
5067 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5068 break;
5069 /* allow sparse sets */
5070 if (fd == -1) {
5071 ret = 0;
5072 continue;
5073 }
5074
5075 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5076 index = i & IORING_FILE_TABLE_MASK;
5077 file = fget(fd);
5078
5079 ret = -EBADF;
5080 if (!file)
5081 break;
5082
5083 /*
5084 * Don't allow io_uring instances to be registered. If UNIX
5085 * isn't enabled, then this causes a reference cycle and this
5086 * instance can never get freed. If UNIX is enabled we'll
5087 * handle it just fine, but there's still no point in allowing
5088 * a ring fd as it doesn't support regular read/write anyway.
5089 */
5090 if (file->f_op == &io_uring_fops) {
5091 fput(file);
5092 break;
5093 }
5094 ret = 0;
5095 table->files[index] = file;
5096 }
5097
5098 if (ret) {
5099 for (i = 0; i < ctx->nr_user_files; i++) {
5100 file = io_file_from_index(ctx, i);
5101 if (file)
5102 fput(file);
5103 }
5104 for (i = 0; i < nr_tables; i++)
5105 kfree(ctx->file_data->table[i].files);
5106
5107 kfree(ctx->file_data->table);
5108 kfree(ctx->file_data);
5109 ctx->file_data = NULL;
5110 ctx->nr_user_files = 0;
5111 return ret;
5112 }
5113
5114 ret = io_sqe_files_scm(ctx);
5115 if (ret)
5116 io_sqe_files_unregister(ctx);
5117
5118 return ret;
5119}
5120
Jens Axboec3a31e62019-10-03 13:59:56 -06005121static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5122 int index)
5123{
5124#if defined(CONFIG_UNIX)
5125 struct sock *sock = ctx->ring_sock->sk;
5126 struct sk_buff_head *head = &sock->sk_receive_queue;
5127 struct sk_buff *skb;
5128
5129 /*
5130 * See if we can merge this file into an existing skb SCM_RIGHTS
5131 * file set. If there's no room, fall back to allocating a new skb
5132 * and filling it in.
5133 */
5134 spin_lock_irq(&head->lock);
5135 skb = skb_peek(head);
5136 if (skb) {
5137 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5138
5139 if (fpl->count < SCM_MAX_FD) {
5140 __skb_unlink(skb, head);
5141 spin_unlock_irq(&head->lock);
5142 fpl->fp[fpl->count] = get_file(file);
5143 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5144 fpl->count++;
5145 spin_lock_irq(&head->lock);
5146 __skb_queue_head(head, skb);
5147 } else {
5148 skb = NULL;
5149 }
5150 }
5151 spin_unlock_irq(&head->lock);
5152
5153 if (skb) {
5154 fput(file);
5155 return 0;
5156 }
5157
5158 return __io_sqe_files_scm(ctx, 1, index);
5159#else
5160 return 0;
5161#endif
5162}
5163
Jens Axboe05f3fb32019-12-09 11:22:50 -07005164static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005165{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005166 struct fixed_file_data *data;
5167
5168 data = container_of(ref, struct fixed_file_data, refs);
5169 clear_bit(FFD_F_ATOMIC, &data->state);
5170}
5171
5172static bool io_queue_file_removal(struct fixed_file_data *data,
5173 struct file *file)
5174{
5175 struct io_file_put *pfile, pfile_stack;
5176 DECLARE_COMPLETION_ONSTACK(done);
5177
5178 /*
5179 * If we fail allocating the struct we need for doing async reomval
5180 * of this file, just punt to sync and wait for it.
5181 */
5182 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5183 if (!pfile) {
5184 pfile = &pfile_stack;
5185 pfile->done = &done;
5186 }
5187
5188 pfile->file = file;
5189 llist_add(&pfile->llist, &data->put_llist);
5190
5191 if (pfile == &pfile_stack) {
5192 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5193 percpu_ref_put(&data->refs);
5194 percpu_ref_switch_to_atomic(&data->refs,
5195 io_atomic_switch);
5196 }
5197 wait_for_completion(&done);
5198 flush_work(&data->ref_work);
5199 return false;
5200 }
5201
5202 return true;
5203}
5204
5205static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5206 struct io_uring_files_update *up,
5207 unsigned nr_args)
5208{
5209 struct fixed_file_data *data = ctx->file_data;
5210 bool ref_switch = false;
5211 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005212 __s32 __user *fds;
5213 int fd, i, err;
5214 __u32 done;
5215
Jens Axboe05f3fb32019-12-09 11:22:50 -07005216 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005217 return -EOVERFLOW;
5218 if (done > ctx->nr_user_files)
5219 return -EINVAL;
5220
5221 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005222 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005223 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005224 struct fixed_file_table *table;
5225 unsigned index;
5226
Jens Axboec3a31e62019-10-03 13:59:56 -06005227 err = 0;
5228 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5229 err = -EFAULT;
5230 break;
5231 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005232 i = array_index_nospec(up->offset, ctx->nr_user_files);
5233 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005234 index = i & IORING_FILE_TABLE_MASK;
5235 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005236 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005237 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005238 if (io_queue_file_removal(data, file))
5239 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005240 }
5241 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005242 file = fget(fd);
5243 if (!file) {
5244 err = -EBADF;
5245 break;
5246 }
5247 /*
5248 * Don't allow io_uring instances to be registered. If
5249 * UNIX isn't enabled, then this causes a reference
5250 * cycle and this instance can never get freed. If UNIX
5251 * is enabled we'll handle it just fine, but there's
5252 * still no point in allowing a ring fd as it doesn't
5253 * support regular read/write anyway.
5254 */
5255 if (file->f_op == &io_uring_fops) {
5256 fput(file);
5257 err = -EBADF;
5258 break;
5259 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005260 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005261 err = io_sqe_file_register(ctx, file, i);
5262 if (err)
5263 break;
5264 }
5265 nr_args--;
5266 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005267 up->offset++;
5268 }
5269
5270 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5271 percpu_ref_put(&data->refs);
5272 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005273 }
5274
5275 return done ? done : err;
5276}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005277static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5278 unsigned nr_args)
5279{
5280 struct io_uring_files_update up;
5281
5282 if (!ctx->file_data)
5283 return -ENXIO;
5284 if (!nr_args)
5285 return -EINVAL;
5286 if (copy_from_user(&up, arg, sizeof(up)))
5287 return -EFAULT;
5288 if (up.resv)
5289 return -EINVAL;
5290
5291 return __io_sqe_files_update(ctx, &up, nr_args);
5292}
Jens Axboec3a31e62019-10-03 13:59:56 -06005293
Jens Axboe7d723062019-11-12 22:31:31 -07005294static void io_put_work(struct io_wq_work *work)
5295{
5296 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5297
5298 io_put_req(req);
5299}
5300
5301static void io_get_work(struct io_wq_work *work)
5302{
5303 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5304
5305 refcount_inc(&req->refs);
5306}
5307
Jens Axboe6c271ce2019-01-10 11:22:30 -07005308static int io_sq_offload_start(struct io_ring_ctx *ctx,
5309 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005310{
Jens Axboe576a3472019-11-25 08:49:20 -07005311 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005312 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005313 int ret;
5314
Jens Axboe6c271ce2019-01-10 11:22:30 -07005315 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005316 mmgrab(current->mm);
5317 ctx->sqo_mm = current->mm;
5318
Jens Axboe6c271ce2019-01-10 11:22:30 -07005319 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005320 ret = -EPERM;
5321 if (!capable(CAP_SYS_ADMIN))
5322 goto err;
5323
Jens Axboe917257d2019-04-13 09:28:55 -06005324 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5325 if (!ctx->sq_thread_idle)
5326 ctx->sq_thread_idle = HZ;
5327
Jens Axboe6c271ce2019-01-10 11:22:30 -07005328 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005329 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005330
Jens Axboe917257d2019-04-13 09:28:55 -06005331 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005332 if (cpu >= nr_cpu_ids)
5333 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005334 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005335 goto err;
5336
Jens Axboe6c271ce2019-01-10 11:22:30 -07005337 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5338 ctx, cpu,
5339 "io_uring-sq");
5340 } else {
5341 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5342 "io_uring-sq");
5343 }
5344 if (IS_ERR(ctx->sqo_thread)) {
5345 ret = PTR_ERR(ctx->sqo_thread);
5346 ctx->sqo_thread = NULL;
5347 goto err;
5348 }
5349 wake_up_process(ctx->sqo_thread);
5350 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5351 /* Can't have SQ_AFF without SQPOLL */
5352 ret = -EINVAL;
5353 goto err;
5354 }
5355
Jens Axboe576a3472019-11-25 08:49:20 -07005356 data.mm = ctx->sqo_mm;
5357 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005358 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005359 data.get_work = io_get_work;
5360 data.put_work = io_put_work;
5361
Jens Axboe561fb042019-10-24 07:25:42 -06005362 /* Do QD, or 4 * CPUS, whatever is smallest */
5363 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005364 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005365 if (IS_ERR(ctx->io_wq)) {
5366 ret = PTR_ERR(ctx->io_wq);
5367 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005368 goto err;
5369 }
5370
5371 return 0;
5372err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005373 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005374 mmdrop(ctx->sqo_mm);
5375 ctx->sqo_mm = NULL;
5376 return ret;
5377}
5378
5379static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5380{
5381 atomic_long_sub(nr_pages, &user->locked_vm);
5382}
5383
5384static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5385{
5386 unsigned long page_limit, cur_pages, new_pages;
5387
5388 /* Don't allow more pages than we can safely lock */
5389 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5390
5391 do {
5392 cur_pages = atomic_long_read(&user->locked_vm);
5393 new_pages = cur_pages + nr_pages;
5394 if (new_pages > page_limit)
5395 return -ENOMEM;
5396 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5397 new_pages) != cur_pages);
5398
5399 return 0;
5400}
5401
5402static void io_mem_free(void *ptr)
5403{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005404 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005405
Mark Rutland52e04ef2019-04-30 17:30:21 +01005406 if (!ptr)
5407 return;
5408
5409 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005410 if (put_page_testzero(page))
5411 free_compound_page(page);
5412}
5413
5414static void *io_mem_alloc(size_t size)
5415{
5416 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5417 __GFP_NORETRY;
5418
5419 return (void *) __get_free_pages(gfp_flags, get_order(size));
5420}
5421
Hristo Venev75b28af2019-08-26 17:23:46 +00005422static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5423 size_t *sq_offset)
5424{
5425 struct io_rings *rings;
5426 size_t off, sq_array_size;
5427
5428 off = struct_size(rings, cqes, cq_entries);
5429 if (off == SIZE_MAX)
5430 return SIZE_MAX;
5431
5432#ifdef CONFIG_SMP
5433 off = ALIGN(off, SMP_CACHE_BYTES);
5434 if (off == 0)
5435 return SIZE_MAX;
5436#endif
5437
5438 sq_array_size = array_size(sizeof(u32), sq_entries);
5439 if (sq_array_size == SIZE_MAX)
5440 return SIZE_MAX;
5441
5442 if (check_add_overflow(off, sq_array_size, &off))
5443 return SIZE_MAX;
5444
5445 if (sq_offset)
5446 *sq_offset = off;
5447
5448 return off;
5449}
5450
Jens Axboe2b188cc2019-01-07 10:46:33 -07005451static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5452{
Hristo Venev75b28af2019-08-26 17:23:46 +00005453 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005454
Hristo Venev75b28af2019-08-26 17:23:46 +00005455 pages = (size_t)1 << get_order(
5456 rings_size(sq_entries, cq_entries, NULL));
5457 pages += (size_t)1 << get_order(
5458 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005459
Hristo Venev75b28af2019-08-26 17:23:46 +00005460 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005461}
5462
Jens Axboeedafcce2019-01-09 09:16:05 -07005463static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5464{
5465 int i, j;
5466
5467 if (!ctx->user_bufs)
5468 return -ENXIO;
5469
5470 for (i = 0; i < ctx->nr_user_bufs; i++) {
5471 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5472
5473 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005474 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005475
5476 if (ctx->account_mem)
5477 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005478 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005479 imu->nr_bvecs = 0;
5480 }
5481
5482 kfree(ctx->user_bufs);
5483 ctx->user_bufs = NULL;
5484 ctx->nr_user_bufs = 0;
5485 return 0;
5486}
5487
5488static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5489 void __user *arg, unsigned index)
5490{
5491 struct iovec __user *src;
5492
5493#ifdef CONFIG_COMPAT
5494 if (ctx->compat) {
5495 struct compat_iovec __user *ciovs;
5496 struct compat_iovec ciov;
5497
5498 ciovs = (struct compat_iovec __user *) arg;
5499 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5500 return -EFAULT;
5501
Jens Axboed55e5f52019-12-11 16:12:15 -07005502 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005503 dst->iov_len = ciov.iov_len;
5504 return 0;
5505 }
5506#endif
5507 src = (struct iovec __user *) arg;
5508 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5509 return -EFAULT;
5510 return 0;
5511}
5512
5513static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5514 unsigned nr_args)
5515{
5516 struct vm_area_struct **vmas = NULL;
5517 struct page **pages = NULL;
5518 int i, j, got_pages = 0;
5519 int ret = -EINVAL;
5520
5521 if (ctx->user_bufs)
5522 return -EBUSY;
5523 if (!nr_args || nr_args > UIO_MAXIOV)
5524 return -EINVAL;
5525
5526 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5527 GFP_KERNEL);
5528 if (!ctx->user_bufs)
5529 return -ENOMEM;
5530
5531 for (i = 0; i < nr_args; i++) {
5532 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5533 unsigned long off, start, end, ubuf;
5534 int pret, nr_pages;
5535 struct iovec iov;
5536 size_t size;
5537
5538 ret = io_copy_iov(ctx, &iov, arg, i);
5539 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005540 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005541
5542 /*
5543 * Don't impose further limits on the size and buffer
5544 * constraints here, we'll -EINVAL later when IO is
5545 * submitted if they are wrong.
5546 */
5547 ret = -EFAULT;
5548 if (!iov.iov_base || !iov.iov_len)
5549 goto err;
5550
5551 /* arbitrary limit, but we need something */
5552 if (iov.iov_len > SZ_1G)
5553 goto err;
5554
5555 ubuf = (unsigned long) iov.iov_base;
5556 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5557 start = ubuf >> PAGE_SHIFT;
5558 nr_pages = end - start;
5559
5560 if (ctx->account_mem) {
5561 ret = io_account_mem(ctx->user, nr_pages);
5562 if (ret)
5563 goto err;
5564 }
5565
5566 ret = 0;
5567 if (!pages || nr_pages > got_pages) {
5568 kfree(vmas);
5569 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005570 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005571 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005572 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005573 sizeof(struct vm_area_struct *),
5574 GFP_KERNEL);
5575 if (!pages || !vmas) {
5576 ret = -ENOMEM;
5577 if (ctx->account_mem)
5578 io_unaccount_mem(ctx->user, nr_pages);
5579 goto err;
5580 }
5581 got_pages = nr_pages;
5582 }
5583
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005584 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005585 GFP_KERNEL);
5586 ret = -ENOMEM;
5587 if (!imu->bvec) {
5588 if (ctx->account_mem)
5589 io_unaccount_mem(ctx->user, nr_pages);
5590 goto err;
5591 }
5592
5593 ret = 0;
5594 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005595 pret = get_user_pages(ubuf, nr_pages,
5596 FOLL_WRITE | FOLL_LONGTERM,
5597 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005598 if (pret == nr_pages) {
5599 /* don't support file backed memory */
5600 for (j = 0; j < nr_pages; j++) {
5601 struct vm_area_struct *vma = vmas[j];
5602
5603 if (vma->vm_file &&
5604 !is_file_hugepages(vma->vm_file)) {
5605 ret = -EOPNOTSUPP;
5606 break;
5607 }
5608 }
5609 } else {
5610 ret = pret < 0 ? pret : -EFAULT;
5611 }
5612 up_read(&current->mm->mmap_sem);
5613 if (ret) {
5614 /*
5615 * if we did partial map, or found file backed vmas,
5616 * release any pages we did get
5617 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005618 if (pret > 0)
5619 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005620 if (ctx->account_mem)
5621 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005622 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005623 goto err;
5624 }
5625
5626 off = ubuf & ~PAGE_MASK;
5627 size = iov.iov_len;
5628 for (j = 0; j < nr_pages; j++) {
5629 size_t vec_len;
5630
5631 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5632 imu->bvec[j].bv_page = pages[j];
5633 imu->bvec[j].bv_len = vec_len;
5634 imu->bvec[j].bv_offset = off;
5635 off = 0;
5636 size -= vec_len;
5637 }
5638 /* store original address for later verification */
5639 imu->ubuf = ubuf;
5640 imu->len = iov.iov_len;
5641 imu->nr_bvecs = nr_pages;
5642
5643 ctx->nr_user_bufs++;
5644 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005645 kvfree(pages);
5646 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005647 return 0;
5648err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005649 kvfree(pages);
5650 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005651 io_sqe_buffer_unregister(ctx);
5652 return ret;
5653}
5654
Jens Axboe9b402842019-04-11 11:45:41 -06005655static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5656{
5657 __s32 __user *fds = arg;
5658 int fd;
5659
5660 if (ctx->cq_ev_fd)
5661 return -EBUSY;
5662
5663 if (copy_from_user(&fd, fds, sizeof(*fds)))
5664 return -EFAULT;
5665
5666 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5667 if (IS_ERR(ctx->cq_ev_fd)) {
5668 int ret = PTR_ERR(ctx->cq_ev_fd);
5669 ctx->cq_ev_fd = NULL;
5670 return ret;
5671 }
5672
5673 return 0;
5674}
5675
5676static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5677{
5678 if (ctx->cq_ev_fd) {
5679 eventfd_ctx_put(ctx->cq_ev_fd);
5680 ctx->cq_ev_fd = NULL;
5681 return 0;
5682 }
5683
5684 return -ENXIO;
5685}
5686
Jens Axboe2b188cc2019-01-07 10:46:33 -07005687static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5688{
Jens Axboe6b063142019-01-10 22:13:58 -07005689 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005690 if (ctx->sqo_mm)
5691 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005692
5693 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005694 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005695 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005696 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005697
Jens Axboe2b188cc2019-01-07 10:46:33 -07005698#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005699 if (ctx->ring_sock) {
5700 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005701 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005702 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005703#endif
5704
Hristo Venev75b28af2019-08-26 17:23:46 +00005705 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005706 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005707
5708 percpu_ref_exit(&ctx->refs);
5709 if (ctx->account_mem)
5710 io_unaccount_mem(ctx->user,
5711 ring_pages(ctx->sq_entries, ctx->cq_entries));
5712 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07005713 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07005714 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07005715 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07005716 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005717 kfree(ctx);
5718}
5719
5720static __poll_t io_uring_poll(struct file *file, poll_table *wait)
5721{
5722 struct io_ring_ctx *ctx = file->private_data;
5723 __poll_t mask = 0;
5724
5725 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02005726 /*
5727 * synchronizes with barrier from wq_has_sleeper call in
5728 * io_commit_cqring
5729 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005730 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00005731 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
5732 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005733 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08005734 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005735 mask |= EPOLLIN | EPOLLRDNORM;
5736
5737 return mask;
5738}
5739
5740static int io_uring_fasync(int fd, struct file *file, int on)
5741{
5742 struct io_ring_ctx *ctx = file->private_data;
5743
5744 return fasync_helper(fd, file, on, &ctx->cq_fasync);
5745}
5746
5747static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
5748{
5749 mutex_lock(&ctx->uring_lock);
5750 percpu_ref_kill(&ctx->refs);
5751 mutex_unlock(&ctx->uring_lock);
5752
Jens Axboe5262f562019-09-17 12:26:57 -06005753 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005754 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06005755
5756 if (ctx->io_wq)
5757 io_wq_cancel_all(ctx->io_wq);
5758
Jens Axboedef596e2019-01-09 08:59:42 -07005759 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07005760 /* if we failed setting up the ctx, we might not have any rings */
5761 if (ctx->rings)
5762 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07005763 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005764 io_ring_ctx_free(ctx);
5765}
5766
5767static int io_uring_release(struct inode *inode, struct file *file)
5768{
5769 struct io_ring_ctx *ctx = file->private_data;
5770
5771 file->private_data = NULL;
5772 io_ring_ctx_wait_and_kill(ctx);
5773 return 0;
5774}
5775
Jens Axboefcb323c2019-10-24 12:39:47 -06005776static void io_uring_cancel_files(struct io_ring_ctx *ctx,
5777 struct files_struct *files)
5778{
5779 struct io_kiocb *req;
5780 DEFINE_WAIT(wait);
5781
5782 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07005783 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06005784
5785 spin_lock_irq(&ctx->inflight_lock);
5786 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07005787 if (req->work.files != files)
5788 continue;
5789 /* req is being completed, ignore */
5790 if (!refcount_inc_not_zero(&req->refs))
5791 continue;
5792 cancel_req = req;
5793 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06005794 }
Jens Axboe768134d2019-11-10 20:30:53 -07005795 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005796 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07005797 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06005798 spin_unlock_irq(&ctx->inflight_lock);
5799
Jens Axboe768134d2019-11-10 20:30:53 -07005800 /* We need to keep going until we don't find a matching req */
5801 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005802 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005803
5804 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5805 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005806 schedule();
5807 }
Jens Axboe768134d2019-11-10 20:30:53 -07005808 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005809}
5810
5811static int io_uring_flush(struct file *file, void *data)
5812{
5813 struct io_ring_ctx *ctx = file->private_data;
5814
5815 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005816 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5817 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005818 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005819 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005820 return 0;
5821}
5822
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005823static void *io_uring_validate_mmap_request(struct file *file,
5824 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005825{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005826 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005827 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005828 struct page *page;
5829 void *ptr;
5830
5831 switch (offset) {
5832 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005833 case IORING_OFF_CQ_RING:
5834 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005835 break;
5836 case IORING_OFF_SQES:
5837 ptr = ctx->sq_sqes;
5838 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005839 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005840 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005841 }
5842
5843 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005844 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005845 return ERR_PTR(-EINVAL);
5846
5847 return ptr;
5848}
5849
5850#ifdef CONFIG_MMU
5851
5852static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5853{
5854 size_t sz = vma->vm_end - vma->vm_start;
5855 unsigned long pfn;
5856 void *ptr;
5857
5858 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5859 if (IS_ERR(ptr))
5860 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005861
5862 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5863 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5864}
5865
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005866#else /* !CONFIG_MMU */
5867
5868static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5869{
5870 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5871}
5872
5873static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5874{
5875 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5876}
5877
5878static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5879 unsigned long addr, unsigned long len,
5880 unsigned long pgoff, unsigned long flags)
5881{
5882 void *ptr;
5883
5884 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5885 if (IS_ERR(ptr))
5886 return PTR_ERR(ptr);
5887
5888 return (unsigned long) ptr;
5889}
5890
5891#endif /* !CONFIG_MMU */
5892
Jens Axboe2b188cc2019-01-07 10:46:33 -07005893SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5894 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5895 size_t, sigsz)
5896{
5897 struct io_ring_ctx *ctx;
5898 long ret = -EBADF;
5899 int submitted = 0;
5900 struct fd f;
5901
Jens Axboe6c271ce2019-01-10 11:22:30 -07005902 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005903 return -EINVAL;
5904
5905 f = fdget(fd);
5906 if (!f.file)
5907 return -EBADF;
5908
5909 ret = -EOPNOTSUPP;
5910 if (f.file->f_op != &io_uring_fops)
5911 goto out_fput;
5912
5913 ret = -ENXIO;
5914 ctx = f.file->private_data;
5915 if (!percpu_ref_tryget(&ctx->refs))
5916 goto out_fput;
5917
Jens Axboe6c271ce2019-01-10 11:22:30 -07005918 /*
5919 * For SQ polling, the thread will do all submissions and completions.
5920 * Just return the requested submit count, and wake the thread if
5921 * we were asked to.
5922 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005923 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005924 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005925 if (!list_empty_careful(&ctx->cq_overflow_list))
5926 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005927 if (flags & IORING_ENTER_SQ_WAKEUP)
5928 wake_up(&ctx->sqo_wait);
5929 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005930 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005931 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005932
Jens Axboe44d28272020-01-16 19:00:24 -07005933 if (current->mm != ctx->sqo_mm ||
5934 current_cred() != ctx->creds) {
5935 ret = -EPERM;
5936 goto out;
5937 }
5938
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005939 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005940 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005941 /* already have mm, so io_submit_sqes() won't try to grab it */
5942 cur_mm = ctx->sqo_mm;
5943 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
5944 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005945 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005946
5947 if (submitted != to_submit)
5948 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005949 }
5950 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07005951 unsigned nr_events = 0;
5952
Jens Axboe2b188cc2019-01-07 10:46:33 -07005953 min_complete = min(min_complete, ctx->cq_entries);
5954
Jens Axboedef596e2019-01-09 08:59:42 -07005955 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07005956 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07005957 } else {
5958 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
5959 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005960 }
5961
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005962out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03005963 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005964out_fput:
5965 fdput(f);
5966 return submitted ? submitted : ret;
5967}
5968
5969static const struct file_operations io_uring_fops = {
5970 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06005971 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07005972 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005973#ifndef CONFIG_MMU
5974 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
5975 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
5976#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07005977 .poll = io_uring_poll,
5978 .fasync = io_uring_fasync,
5979};
5980
5981static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
5982 struct io_uring_params *p)
5983{
Hristo Venev75b28af2019-08-26 17:23:46 +00005984 struct io_rings *rings;
5985 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005986
Hristo Venev75b28af2019-08-26 17:23:46 +00005987 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
5988 if (size == SIZE_MAX)
5989 return -EOVERFLOW;
5990
5991 rings = io_mem_alloc(size);
5992 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005993 return -ENOMEM;
5994
Hristo Venev75b28af2019-08-26 17:23:46 +00005995 ctx->rings = rings;
5996 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
5997 rings->sq_ring_mask = p->sq_entries - 1;
5998 rings->cq_ring_mask = p->cq_entries - 1;
5999 rings->sq_ring_entries = p->sq_entries;
6000 rings->cq_ring_entries = p->cq_entries;
6001 ctx->sq_mask = rings->sq_ring_mask;
6002 ctx->cq_mask = rings->cq_ring_mask;
6003 ctx->sq_entries = rings->sq_ring_entries;
6004 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006005
6006 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006007 if (size == SIZE_MAX) {
6008 io_mem_free(ctx->rings);
6009 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006010 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006011 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006012
6013 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006014 if (!ctx->sq_sqes) {
6015 io_mem_free(ctx->rings);
6016 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006017 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006018 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006019
Jens Axboe2b188cc2019-01-07 10:46:33 -07006020 return 0;
6021}
6022
6023/*
6024 * Allocate an anonymous fd, this is what constitutes the application
6025 * visible backing of an io_uring instance. The application mmaps this
6026 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6027 * we have to tie this fd to a socket for file garbage collection purposes.
6028 */
6029static int io_uring_get_fd(struct io_ring_ctx *ctx)
6030{
6031 struct file *file;
6032 int ret;
6033
6034#if defined(CONFIG_UNIX)
6035 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6036 &ctx->ring_sock);
6037 if (ret)
6038 return ret;
6039#endif
6040
6041 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6042 if (ret < 0)
6043 goto err;
6044
6045 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6046 O_RDWR | O_CLOEXEC);
6047 if (IS_ERR(file)) {
6048 put_unused_fd(ret);
6049 ret = PTR_ERR(file);
6050 goto err;
6051 }
6052
6053#if defined(CONFIG_UNIX)
6054 ctx->ring_sock->file = file;
6055#endif
6056 fd_install(ret, file);
6057 return ret;
6058err:
6059#if defined(CONFIG_UNIX)
6060 sock_release(ctx->ring_sock);
6061 ctx->ring_sock = NULL;
6062#endif
6063 return ret;
6064}
6065
6066static int io_uring_create(unsigned entries, struct io_uring_params *p)
6067{
6068 struct user_struct *user = NULL;
6069 struct io_ring_ctx *ctx;
6070 bool account_mem;
6071 int ret;
6072
6073 if (!entries || entries > IORING_MAX_ENTRIES)
6074 return -EINVAL;
6075
6076 /*
6077 * Use twice as many entries for the CQ ring. It's possible for the
6078 * application to drive a higher depth than the size of the SQ ring,
6079 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006080 * some flexibility in overcommitting a bit. If the application has
6081 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6082 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006083 */
6084 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006085 if (p->flags & IORING_SETUP_CQSIZE) {
6086 /*
6087 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6088 * to a power-of-two, if it isn't already. We do NOT impose
6089 * any cq vs sq ring sizing.
6090 */
6091 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
6092 return -EINVAL;
6093 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6094 } else {
6095 p->cq_entries = 2 * p->sq_entries;
6096 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006097
6098 user = get_uid(current_user());
6099 account_mem = !capable(CAP_IPC_LOCK);
6100
6101 if (account_mem) {
6102 ret = io_account_mem(user,
6103 ring_pages(p->sq_entries, p->cq_entries));
6104 if (ret) {
6105 free_uid(user);
6106 return ret;
6107 }
6108 }
6109
6110 ctx = io_ring_ctx_alloc(p);
6111 if (!ctx) {
6112 if (account_mem)
6113 io_unaccount_mem(user, ring_pages(p->sq_entries,
6114 p->cq_entries));
6115 free_uid(user);
6116 return -ENOMEM;
6117 }
6118 ctx->compat = in_compat_syscall();
6119 ctx->account_mem = account_mem;
6120 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006121 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006122
6123 ret = io_allocate_scq_urings(ctx, p);
6124 if (ret)
6125 goto err;
6126
Jens Axboe6c271ce2019-01-10 11:22:30 -07006127 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006128 if (ret)
6129 goto err;
6130
Jens Axboe2b188cc2019-01-07 10:46:33 -07006131 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006132 p->sq_off.head = offsetof(struct io_rings, sq.head);
6133 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6134 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6135 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6136 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6137 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6138 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006139
6140 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006141 p->cq_off.head = offsetof(struct io_rings, cq.head);
6142 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6143 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6144 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6145 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6146 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006147
Jens Axboe044c1ab2019-10-28 09:15:33 -06006148 /*
6149 * Install ring fd as the very last thing, so we don't risk someone
6150 * having closed it before we finish setup
6151 */
6152 ret = io_uring_get_fd(ctx);
6153 if (ret < 0)
6154 goto err;
6155
Jens Axboeda8c9692019-12-02 18:51:26 -07006156 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
6157 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006158 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006159 return ret;
6160err:
6161 io_ring_ctx_wait_and_kill(ctx);
6162 return ret;
6163}
6164
6165/*
6166 * Sets up an aio uring context, and returns the fd. Applications asks for a
6167 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6168 * params structure passed in.
6169 */
6170static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6171{
6172 struct io_uring_params p;
6173 long ret;
6174 int i;
6175
6176 if (copy_from_user(&p, params, sizeof(p)))
6177 return -EFAULT;
6178 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6179 if (p.resv[i])
6180 return -EINVAL;
6181 }
6182
Jens Axboe6c271ce2019-01-10 11:22:30 -07006183 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06006184 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006185 return -EINVAL;
6186
6187 ret = io_uring_create(entries, &p);
6188 if (ret < 0)
6189 return ret;
6190
6191 if (copy_to_user(params, &p, sizeof(p)))
6192 return -EFAULT;
6193
6194 return ret;
6195}
6196
6197SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6198 struct io_uring_params __user *, params)
6199{
6200 return io_uring_setup(entries, params);
6201}
6202
Jens Axboeedafcce2019-01-09 09:16:05 -07006203static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6204 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006205 __releases(ctx->uring_lock)
6206 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006207{
6208 int ret;
6209
Jens Axboe35fa71a2019-04-22 10:23:23 -06006210 /*
6211 * We're inside the ring mutex, if the ref is already dying, then
6212 * someone else killed the ctx or is already going through
6213 * io_uring_register().
6214 */
6215 if (percpu_ref_is_dying(&ctx->refs))
6216 return -ENXIO;
6217
Jens Axboe05f3fb32019-12-09 11:22:50 -07006218 if (opcode != IORING_UNREGISTER_FILES &&
6219 opcode != IORING_REGISTER_FILES_UPDATE) {
6220 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006221
Jens Axboe05f3fb32019-12-09 11:22:50 -07006222 /*
6223 * Drop uring mutex before waiting for references to exit. If
6224 * another thread is currently inside io_uring_enter() it might
6225 * need to grab the uring_lock to make progress. If we hold it
6226 * here across the drain wait, then we can deadlock. It's safe
6227 * to drop the mutex here, since no new references will come in
6228 * after we've killed the percpu ref.
6229 */
6230 mutex_unlock(&ctx->uring_lock);
6231 wait_for_completion(&ctx->completions[0]);
6232 mutex_lock(&ctx->uring_lock);
6233 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006234
6235 switch (opcode) {
6236 case IORING_REGISTER_BUFFERS:
6237 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6238 break;
6239 case IORING_UNREGISTER_BUFFERS:
6240 ret = -EINVAL;
6241 if (arg || nr_args)
6242 break;
6243 ret = io_sqe_buffer_unregister(ctx);
6244 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006245 case IORING_REGISTER_FILES:
6246 ret = io_sqe_files_register(ctx, arg, nr_args);
6247 break;
6248 case IORING_UNREGISTER_FILES:
6249 ret = -EINVAL;
6250 if (arg || nr_args)
6251 break;
6252 ret = io_sqe_files_unregister(ctx);
6253 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006254 case IORING_REGISTER_FILES_UPDATE:
6255 ret = io_sqe_files_update(ctx, arg, nr_args);
6256 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006257 case IORING_REGISTER_EVENTFD:
6258 ret = -EINVAL;
6259 if (nr_args != 1)
6260 break;
6261 ret = io_eventfd_register(ctx, arg);
6262 break;
6263 case IORING_UNREGISTER_EVENTFD:
6264 ret = -EINVAL;
6265 if (arg || nr_args)
6266 break;
6267 ret = io_eventfd_unregister(ctx);
6268 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006269 default:
6270 ret = -EINVAL;
6271 break;
6272 }
6273
Jens Axboe05f3fb32019-12-09 11:22:50 -07006274
6275 if (opcode != IORING_UNREGISTER_FILES &&
6276 opcode != IORING_REGISTER_FILES_UPDATE) {
6277 /* bring the ctx back to life */
6278 reinit_completion(&ctx->completions[0]);
6279 percpu_ref_reinit(&ctx->refs);
6280 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006281 return ret;
6282}
6283
6284SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6285 void __user *, arg, unsigned int, nr_args)
6286{
6287 struct io_ring_ctx *ctx;
6288 long ret = -EBADF;
6289 struct fd f;
6290
6291 f = fdget(fd);
6292 if (!f.file)
6293 return -EBADF;
6294
6295 ret = -EOPNOTSUPP;
6296 if (f.file->f_op != &io_uring_fops)
6297 goto out_fput;
6298
6299 ctx = f.file->private_data;
6300
6301 mutex_lock(&ctx->uring_lock);
6302 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6303 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006304 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6305 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006306out_fput:
6307 fdput(f);
6308 return ret;
6309}
6310
Jens Axboe2b188cc2019-01-07 10:46:33 -07006311static int __init io_uring_init(void)
6312{
Jens Axboed3656342019-12-18 09:50:26 -07006313 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006314 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6315 return 0;
6316};
6317__initcall(io_uring_init);