blob: fe227650efd66c14343bd28b7f235b08eeb399f2 [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 Axboe2b188cc2019-01-07 10:46:33 -0700182struct io_ring_ctx {
183 struct {
184 struct percpu_ref refs;
185 } ____cacheline_aligned_in_smp;
186
187 struct {
188 unsigned int flags;
189 bool compat;
190 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700191 bool cq_overflow_flushed;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300192 bool drain_next;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700193
Hristo Venev75b28af2019-08-26 17:23:46 +0000194 /*
195 * Ring buffer of indices into array of io_uring_sqe, which is
196 * mmapped by the application using the IORING_OFF_SQES offset.
197 *
198 * This indirection could e.g. be used to assign fixed
199 * io_uring_sqe entries to operations and only submit them to
200 * the queue when needed.
201 *
202 * The kernel modifies neither the indices array nor the entries
203 * array.
204 */
205 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700206 unsigned cached_sq_head;
207 unsigned sq_entries;
208 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700209 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600210 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700211 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700212 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600213
214 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600215 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700216 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700217
Jens Axboefcb323c2019-10-24 12:39:47 -0600218 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700219 } ____cacheline_aligned_in_smp;
220
Hristo Venev75b28af2019-08-26 17:23:46 +0000221 struct io_rings *rings;
222
Jens Axboe2b188cc2019-01-07 10:46:33 -0700223 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600224 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700225 struct task_struct *sqo_thread; /* if using sq thread polling */
226 struct mm_struct *sqo_mm;
227 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700228
Jens Axboe6b063142019-01-10 22:13:58 -0700229 /*
230 * If used, fixed file set. Writers must ensure that ->refs is dead,
231 * readers must ensure that ->refs is alive as long as the file* is
232 * used. Only updated through io_uring_register(2).
233 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600234 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700235 unsigned nr_user_files;
236
Jens Axboeedafcce2019-01-09 09:16:05 -0700237 /* if used, fixed mapped user buffers */
238 unsigned nr_user_bufs;
239 struct io_mapped_ubuf *user_bufs;
240
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241 struct user_struct *user;
242
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700243 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700244
Jens Axboe206aefd2019-11-07 18:27:42 -0700245 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
246 struct completion *completions;
247
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700248 /* if all else fails... */
249 struct io_kiocb *fallback_req;
250
Jens Axboe206aefd2019-11-07 18:27:42 -0700251#if defined(CONFIG_UNIX)
252 struct socket *ring_sock;
253#endif
254
255 struct {
256 unsigned cached_cq_tail;
257 unsigned cq_entries;
258 unsigned cq_mask;
259 atomic_t cq_timeouts;
260 struct wait_queue_head cq_wait;
261 struct fasync_struct *cq_fasync;
262 struct eventfd_ctx *cq_ev_fd;
263 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700264
265 struct {
266 struct mutex uring_lock;
267 wait_queue_head_t wait;
268 } ____cacheline_aligned_in_smp;
269
270 struct {
271 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700272 bool poll_multi_file;
273 /*
274 * ->poll_list is protected by the ctx->uring_lock for
275 * io_uring instances that don't use IORING_SETUP_SQPOLL.
276 * For SQPOLL, only the single threaded io_sq_thread() will
277 * manipulate the list, hence no extra locking is needed there.
278 */
279 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700280 struct hlist_head *cancel_hash;
281 unsigned cancel_hash_bits;
Jens Axboefcb323c2019-10-24 12:39:47 -0600282
283 spinlock_t inflight_lock;
284 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700286};
287
Jens Axboe09bb8392019-03-13 12:39:28 -0600288/*
289 * First field must be the file pointer in all the
290 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
291 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700292struct io_poll_iocb {
293 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700294 union {
295 struct wait_queue_head *head;
296 u64 addr;
297 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700298 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600299 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700300 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700301 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700302};
303
Jens Axboead8a48a2019-11-15 08:49:11 -0700304struct io_timeout_data {
305 struct io_kiocb *req;
306 struct hrtimer timer;
307 struct timespec64 ts;
308 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300309 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700310};
311
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700312struct io_accept {
313 struct file *file;
314 struct sockaddr __user *addr;
315 int __user *addr_len;
316 int flags;
317};
318
319struct io_sync {
320 struct file *file;
321 loff_t len;
322 loff_t off;
323 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700324 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700325};
326
Jens Axboefbf23842019-12-17 18:45:56 -0700327struct io_cancel {
328 struct file *file;
329 u64 addr;
330};
331
Jens Axboeb29472e2019-12-17 18:50:29 -0700332struct io_timeout {
333 struct file *file;
334 u64 addr;
335 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700336 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700337};
338
Jens Axboe9adbd452019-12-20 08:45:55 -0700339struct io_rw {
340 /* NOTE: kiocb has the file as the first member, so don't do it here */
341 struct kiocb kiocb;
342 u64 addr;
343 u64 len;
344};
345
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700346struct io_connect {
347 struct file *file;
348 struct sockaddr __user *addr;
349 int addr_len;
350};
351
Jens Axboee47293f2019-12-20 08:58:21 -0700352struct io_sr_msg {
353 struct file *file;
354 struct user_msghdr __user *msg;
355 int msg_flags;
356};
357
Jens Axboe15b71ab2019-12-11 11:20:36 -0700358struct io_open {
359 struct file *file;
360 int dfd;
361 umode_t mode;
362 const char __user *fname;
363 struct filename *filename;
364 int flags;
365};
366
Jens Axboef499a022019-12-02 16:28:46 -0700367struct io_async_connect {
368 struct sockaddr_storage address;
369};
370
Jens Axboe03b12302019-12-02 18:50:25 -0700371struct io_async_msghdr {
372 struct iovec fast_iov[UIO_FASTIOV];
373 struct iovec *iov;
374 struct sockaddr __user *uaddr;
375 struct msghdr msg;
376};
377
Jens Axboef67676d2019-12-02 11:03:47 -0700378struct io_async_rw {
379 struct iovec fast_iov[UIO_FASTIOV];
380 struct iovec *iov;
381 ssize_t nr_segs;
382 ssize_t size;
383};
384
Jens Axboe15b71ab2019-12-11 11:20:36 -0700385struct io_async_open {
386 struct filename *filename;
387};
388
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700389struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700390 union {
391 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700392 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700393 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700394 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700395 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700396 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700397};
398
Jens Axboe09bb8392019-03-13 12:39:28 -0600399/*
400 * NOTE! Each of the iocb union members has the file pointer
401 * as the first entry in their struct definition. So you can
402 * access the file pointer through any of the sub-structs,
403 * or directly as just 'ki_filp' in this struct.
404 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700405struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700406 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600407 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700408 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700409 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700410 struct io_accept accept;
411 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700412 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700413 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700414 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700415 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700416 struct io_open open;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700417 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700418
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700419 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300420 struct file *ring_file;
421 int ring_fd;
422 bool has_user;
423 bool in_async;
424 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700425 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700426
427 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700428 union {
429 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700430 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700431 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600432 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700433 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700434 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200435#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700436#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700437#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700438#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200439#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
440#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600441#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700442#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800443#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300444#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600445#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600446#define REQ_F_ISREG 2048 /* regular file */
447#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700448#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800449#define REQ_F_INFLIGHT 16384 /* on inflight list */
450#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700451#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700452 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600453 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600454 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700455
Jens Axboefcb323c2019-10-24 12:39:47 -0600456 struct list_head inflight_entry;
457
Jens Axboe561fb042019-10-24 07:25:42 -0600458 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700459};
460
461#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700462#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700463
Jens Axboe9a56a232019-01-09 09:06:50 -0700464struct io_submit_state {
465 struct blk_plug plug;
466
467 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700468 * io_kiocb alloc cache
469 */
470 void *reqs[IO_IOPOLL_BATCH];
471 unsigned int free_reqs;
472 unsigned int cur_req;
473
474 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700475 * File reference cache
476 */
477 struct file *file;
478 unsigned int fd;
479 unsigned int has_refs;
480 unsigned int used_refs;
481 unsigned int ios_left;
482};
483
Jens Axboe561fb042019-10-24 07:25:42 -0600484static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700485static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800486static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800487static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700488static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700489static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700490static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
491static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600492
Jens Axboe2b188cc2019-01-07 10:46:33 -0700493static struct kmem_cache *req_cachep;
494
495static const struct file_operations io_uring_fops;
496
497struct sock *io_uring_get_socket(struct file *file)
498{
499#if defined(CONFIG_UNIX)
500 if (file->f_op == &io_uring_fops) {
501 struct io_ring_ctx *ctx = file->private_data;
502
503 return ctx->ring_sock->sk;
504 }
505#endif
506 return NULL;
507}
508EXPORT_SYMBOL(io_uring_get_socket);
509
510static void io_ring_ctx_ref_free(struct percpu_ref *ref)
511{
512 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
513
Jens Axboe206aefd2019-11-07 18:27:42 -0700514 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700515}
516
517static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
518{
519 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700520 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700521
522 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
523 if (!ctx)
524 return NULL;
525
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700526 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
527 if (!ctx->fallback_req)
528 goto err;
529
Jens Axboe206aefd2019-11-07 18:27:42 -0700530 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
531 if (!ctx->completions)
532 goto err;
533
Jens Axboe78076bb2019-12-04 19:56:40 -0700534 /*
535 * Use 5 bits less than the max cq entries, that should give us around
536 * 32 entries per hash list if totally full and uniformly spread.
537 */
538 hash_bits = ilog2(p->cq_entries);
539 hash_bits -= 5;
540 if (hash_bits <= 0)
541 hash_bits = 1;
542 ctx->cancel_hash_bits = hash_bits;
543 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
544 GFP_KERNEL);
545 if (!ctx->cancel_hash)
546 goto err;
547 __hash_init(ctx->cancel_hash, 1U << hash_bits);
548
Roman Gushchin21482892019-05-07 10:01:48 -0700549 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700550 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
551 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700552
553 ctx->flags = p->flags;
554 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700555 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700556 init_completion(&ctx->completions[0]);
557 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700558 mutex_init(&ctx->uring_lock);
559 init_waitqueue_head(&ctx->wait);
560 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700561 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600562 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600563 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600564 init_waitqueue_head(&ctx->inflight_wait);
565 spin_lock_init(&ctx->inflight_lock);
566 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700567 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700568err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700569 if (ctx->fallback_req)
570 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700571 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700572 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700573 kfree(ctx);
574 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700575}
576
Bob Liu9d858b22019-11-13 18:06:25 +0800577static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600578{
Jackie Liua197f662019-11-08 08:09:12 -0700579 struct io_ring_ctx *ctx = req->ctx;
580
Jens Axboe498ccd92019-10-25 10:04:25 -0600581 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
582 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600583}
584
Bob Liu9d858b22019-11-13 18:06:25 +0800585static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600586{
Bob Liu9d858b22019-11-13 18:06:25 +0800587 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
588 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600589
Bob Liu9d858b22019-11-13 18:06:25 +0800590 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600591}
592
593static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600594{
595 struct io_kiocb *req;
596
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600597 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800598 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600599 list_del_init(&req->list);
600 return req;
601 }
602
603 return NULL;
604}
605
Jens Axboe5262f562019-09-17 12:26:57 -0600606static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
607{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600608 struct io_kiocb *req;
609
610 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700611 if (req) {
612 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
613 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800614 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700615 list_del_init(&req->list);
616 return req;
617 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600618 }
619
620 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600621}
622
Jens Axboede0617e2019-04-06 21:51:27 -0600623static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700624{
Hristo Venev75b28af2019-08-26 17:23:46 +0000625 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700626
Hristo Venev75b28af2019-08-26 17:23:46 +0000627 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700628 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000629 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700630
Jens Axboe2b188cc2019-01-07 10:46:33 -0700631 if (wq_has_sleeper(&ctx->cq_wait)) {
632 wake_up_interruptible(&ctx->cq_wait);
633 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
634 }
635 }
636}
637
Jens Axboed625c6e2019-12-17 19:53:05 -0700638static inline bool io_req_needs_user(struct io_kiocb *req)
Jens Axboe18d9be12019-09-10 09:13:05 -0600639{
Jens Axboed625c6e2019-12-17 19:53:05 -0700640 return !(req->opcode == IORING_OP_READ_FIXED ||
641 req->opcode == IORING_OP_WRITE_FIXED);
Jens Axboe561fb042019-10-24 07:25:42 -0600642}
643
Jens Axboe94ae5e72019-11-14 19:39:52 -0700644static inline bool io_prep_async_work(struct io_kiocb *req,
645 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600646{
647 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600648
Jens Axboe3529d8c2019-12-19 18:24:38 -0700649 switch (req->opcode) {
650 case IORING_OP_WRITEV:
651 case IORING_OP_WRITE_FIXED:
652 /* only regular files should be hashed for writes */
653 if (req->flags & REQ_F_ISREG)
654 do_hashed = true;
655 /* fall-through */
656 case IORING_OP_READV:
657 case IORING_OP_READ_FIXED:
658 case IORING_OP_SENDMSG:
659 case IORING_OP_RECVMSG:
660 case IORING_OP_ACCEPT:
661 case IORING_OP_POLL_ADD:
662 case IORING_OP_CONNECT:
663 /*
664 * We know REQ_F_ISREG is not set on some of these
665 * opcodes, but this enables us to keep the check in
666 * just one place.
667 */
668 if (!(req->flags & REQ_F_ISREG))
669 req->work.flags |= IO_WQ_WORK_UNBOUND;
670 break;
Jens Axboe54a91f32019-09-10 09:15:04 -0600671 }
Jens Axboe3529d8c2019-12-19 18:24:38 -0700672 if (io_req_needs_user(req))
673 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600674
Jens Axboe94ae5e72019-11-14 19:39:52 -0700675 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600676 return do_hashed;
677}
678
Jackie Liua197f662019-11-08 08:09:12 -0700679static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600680{
Jackie Liua197f662019-11-08 08:09:12 -0700681 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700682 struct io_kiocb *link;
683 bool do_hashed;
684
685 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600686
687 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
688 req->flags);
689 if (!do_hashed) {
690 io_wq_enqueue(ctx->io_wq, &req->work);
691 } else {
692 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
693 file_inode(req->file));
694 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700695
696 if (link)
697 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600698}
699
Jens Axboe5262f562019-09-17 12:26:57 -0600700static void io_kill_timeout(struct io_kiocb *req)
701{
702 int ret;
703
Jens Axboe2d283902019-12-04 11:08:05 -0700704 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600705 if (ret != -1) {
706 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600707 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700708 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800709 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600710 }
711}
712
713static void io_kill_timeouts(struct io_ring_ctx *ctx)
714{
715 struct io_kiocb *req, *tmp;
716
717 spin_lock_irq(&ctx->completion_lock);
718 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
719 io_kill_timeout(req);
720 spin_unlock_irq(&ctx->completion_lock);
721}
722
Jens Axboede0617e2019-04-06 21:51:27 -0600723static void io_commit_cqring(struct io_ring_ctx *ctx)
724{
725 struct io_kiocb *req;
726
Jens Axboe5262f562019-09-17 12:26:57 -0600727 while ((req = io_get_timeout_req(ctx)) != NULL)
728 io_kill_timeout(req);
729
Jens Axboede0617e2019-04-06 21:51:27 -0600730 __io_commit_cqring(ctx);
731
732 while ((req = io_get_deferred_req(ctx)) != NULL) {
733 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700734 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600735 }
736}
737
Jens Axboe2b188cc2019-01-07 10:46:33 -0700738static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
739{
Hristo Venev75b28af2019-08-26 17:23:46 +0000740 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700741 unsigned tail;
742
743 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200744 /*
745 * writes to the cq entry need to come after reading head; the
746 * control dependency is enough as we're using WRITE_ONCE to
747 * fill the cq entry
748 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000749 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700750 return NULL;
751
752 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000753 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700754}
755
Jens Axboe8c838782019-03-12 15:48:16 -0600756static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
757{
758 if (waitqueue_active(&ctx->wait))
759 wake_up(&ctx->wait);
760 if (waitqueue_active(&ctx->sqo_wait))
761 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600762 if (ctx->cq_ev_fd)
763 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600764}
765
Jens Axboec4a2ed72019-11-21 21:01:26 -0700766/* Returns true if there are no backlogged entries after the flush */
767static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700768{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700769 struct io_rings *rings = ctx->rings;
770 struct io_uring_cqe *cqe;
771 struct io_kiocb *req;
772 unsigned long flags;
773 LIST_HEAD(list);
774
775 if (!force) {
776 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700777 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700778 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
779 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700780 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700781 }
782
783 spin_lock_irqsave(&ctx->completion_lock, flags);
784
785 /* if force is set, the ring is going away. always drop after that */
786 if (force)
787 ctx->cq_overflow_flushed = true;
788
Jens Axboec4a2ed72019-11-21 21:01:26 -0700789 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700790 while (!list_empty(&ctx->cq_overflow_list)) {
791 cqe = io_get_cqring(ctx);
792 if (!cqe && !force)
793 break;
794
795 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
796 list);
797 list_move(&req->list, &list);
798 if (cqe) {
799 WRITE_ONCE(cqe->user_data, req->user_data);
800 WRITE_ONCE(cqe->res, req->result);
801 WRITE_ONCE(cqe->flags, 0);
802 } else {
803 WRITE_ONCE(ctx->rings->cq_overflow,
804 atomic_inc_return(&ctx->cached_cq_overflow));
805 }
806 }
807
808 io_commit_cqring(ctx);
809 spin_unlock_irqrestore(&ctx->completion_lock, flags);
810 io_cqring_ev_posted(ctx);
811
812 while (!list_empty(&list)) {
813 req = list_first_entry(&list, struct io_kiocb, list);
814 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800815 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700816 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700817
818 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700819}
820
Jens Axboe78e19bb2019-11-06 15:21:34 -0700821static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700822{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700823 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700824 struct io_uring_cqe *cqe;
825
Jens Axboe78e19bb2019-11-06 15:21:34 -0700826 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700827
Jens Axboe2b188cc2019-01-07 10:46:33 -0700828 /*
829 * If we can't get a cq entry, userspace overflowed the
830 * submission (by quite a lot). Increment the overflow count in
831 * the ring.
832 */
833 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700834 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700835 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700836 WRITE_ONCE(cqe->res, res);
837 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700838 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700839 WRITE_ONCE(ctx->rings->cq_overflow,
840 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700841 } else {
842 refcount_inc(&req->refs);
843 req->result = res;
844 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700845 }
846}
847
Jens Axboe78e19bb2019-11-06 15:21:34 -0700848static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700849{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700850 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700851 unsigned long flags;
852
853 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700854 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700855 io_commit_cqring(ctx);
856 spin_unlock_irqrestore(&ctx->completion_lock, flags);
857
Jens Axboe8c838782019-03-12 15:48:16 -0600858 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700859}
860
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700861static inline bool io_is_fallback_req(struct io_kiocb *req)
862{
863 return req == (struct io_kiocb *)
864 ((unsigned long) req->ctx->fallback_req & ~1UL);
865}
866
867static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
868{
869 struct io_kiocb *req;
870
871 req = ctx->fallback_req;
872 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
873 return req;
874
875 return NULL;
876}
877
Jens Axboe2579f912019-01-09 09:10:43 -0700878static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
879 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700880{
Jens Axboefd6fab22019-03-14 16:30:06 -0600881 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700882 struct io_kiocb *req;
883
884 if (!percpu_ref_tryget(&ctx->refs))
885 return NULL;
886
Jens Axboe2579f912019-01-09 09:10:43 -0700887 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600888 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700889 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700890 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700891 } else if (!state->free_reqs) {
892 size_t sz;
893 int ret;
894
895 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600896 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
897
898 /*
899 * Bulk alloc is all-or-nothing. If we fail to get a batch,
900 * retry single alloc to be on the safe side.
901 */
902 if (unlikely(ret <= 0)) {
903 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
904 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700905 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600906 ret = 1;
907 }
Jens Axboe2579f912019-01-09 09:10:43 -0700908 state->free_reqs = ret - 1;
909 state->cur_req = 1;
910 req = state->reqs[0];
911 } else {
912 req = state->reqs[state->cur_req];
913 state->free_reqs--;
914 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700915 }
916
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700917got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700918 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300919 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600920 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700921 req->ctx = ctx;
922 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600923 /* one is dropped after submission, the other at completion */
924 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600925 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600926 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700927 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700928fallback:
929 req = io_get_fallback_req(ctx);
930 if (req)
931 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300932 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700933 return NULL;
934}
935
Jens Axboedef596e2019-01-09 08:59:42 -0700936static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
937{
938 if (*nr) {
939 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300940 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700941 *nr = 0;
942 }
943}
944
Jens Axboe9e645e112019-05-10 16:07:28 -0600945static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700946{
Jens Axboefcb323c2019-10-24 12:39:47 -0600947 struct io_ring_ctx *ctx = req->ctx;
948
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700949 if (req->io)
950 kfree(req->io);
Jens Axboe09bb8392019-03-13 12:39:28 -0600951 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
952 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600953 if (req->flags & REQ_F_INFLIGHT) {
954 unsigned long flags;
955
956 spin_lock_irqsave(&ctx->inflight_lock, flags);
957 list_del(&req->inflight_entry);
958 if (waitqueue_active(&ctx->inflight_wait))
959 wake_up(&ctx->inflight_wait);
960 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
961 }
962 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700963 if (likely(!io_is_fallback_req(req)))
964 kmem_cache_free(req_cachep, req);
965 else
966 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600967}
968
Jackie Liua197f662019-11-08 08:09:12 -0700969static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600970{
Jackie Liua197f662019-11-08 08:09:12 -0700971 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700972 int ret;
973
Jens Axboe2d283902019-12-04 11:08:05 -0700974 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700975 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700976 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700977 io_commit_cqring(ctx);
978 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800979 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700980 return true;
981 }
982
983 return false;
984}
985
Jens Axboeba816ad2019-09-28 11:36:45 -0600986static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600987{
Jens Axboe2665abf2019-11-05 12:40:47 -0700988 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700989 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600990
Jens Axboe4d7dd462019-11-20 13:03:52 -0700991 /* Already got next link */
992 if (req->flags & REQ_F_LINK_NEXT)
993 return;
994
Jens Axboe9e645e112019-05-10 16:07:28 -0600995 /*
996 * The list should never be empty when we are called here. But could
997 * potentially happen if the chain is messed up, check to be on the
998 * safe side.
999 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001000 while (!list_empty(&req->link_list)) {
1001 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1002 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001003
Pavel Begunkov44932332019-12-05 16:16:35 +03001004 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1005 (nxt->flags & REQ_F_TIMEOUT))) {
1006 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001007 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001008 req->flags &= ~REQ_F_LINK_TIMEOUT;
1009 continue;
1010 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001011
Pavel Begunkov44932332019-12-05 16:16:35 +03001012 list_del_init(&req->link_list);
1013 if (!list_empty(&nxt->link_list))
1014 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001015 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001016 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001017 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001018
Jens Axboe4d7dd462019-11-20 13:03:52 -07001019 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001020 if (wake_ev)
1021 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001022}
1023
1024/*
1025 * Called if REQ_F_LINK is set, and we fail the head request
1026 */
1027static void io_fail_links(struct io_kiocb *req)
1028{
Jens Axboe2665abf2019-11-05 12:40:47 -07001029 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001030 unsigned long flags;
1031
1032 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001033
1034 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001035 struct io_kiocb *link = list_first_entry(&req->link_list,
1036 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001037
Pavel Begunkov44932332019-12-05 16:16:35 +03001038 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001039 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001040
1041 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001042 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001043 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001044 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001045 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001046 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001047 }
Jens Axboe5d960722019-11-19 15:31:28 -07001048 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001049 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001050
1051 io_commit_cqring(ctx);
1052 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1053 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001054}
1055
Jens Axboe4d7dd462019-11-20 13:03:52 -07001056static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001057{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001058 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001059 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001060
Jens Axboe9e645e112019-05-10 16:07:28 -06001061 /*
1062 * If LINK is set, we have dependent requests in this chain. If we
1063 * didn't fail this request, queue the first one up, moving any other
1064 * dependencies to the next request. In case of failure, fail the rest
1065 * of the chain.
1066 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001067 if (req->flags & REQ_F_FAIL_LINK) {
1068 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001069 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1070 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001071 struct io_ring_ctx *ctx = req->ctx;
1072 unsigned long flags;
1073
1074 /*
1075 * If this is a timeout link, we could be racing with the
1076 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001077 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001078 */
1079 spin_lock_irqsave(&ctx->completion_lock, flags);
1080 io_req_link_next(req, nxt);
1081 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1082 } else {
1083 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001084 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001085}
Jens Axboe9e645e112019-05-10 16:07:28 -06001086
Jackie Liuc69f8db2019-11-09 11:00:08 +08001087static void io_free_req(struct io_kiocb *req)
1088{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001089 struct io_kiocb *nxt = NULL;
1090
1091 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001092 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001093
1094 if (nxt)
1095 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001096}
1097
Jens Axboeba816ad2019-09-28 11:36:45 -06001098/*
1099 * Drop reference to request, return next in chain (if there is one) if this
1100 * was the last reference to this request.
1101 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001102__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001103static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001104{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001105 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001106
Jens Axboee65ef562019-03-12 10:16:44 -06001107 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001108 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001109}
1110
Jens Axboe2b188cc2019-01-07 10:46:33 -07001111static void io_put_req(struct io_kiocb *req)
1112{
Jens Axboedef596e2019-01-09 08:59:42 -07001113 if (refcount_dec_and_test(&req->refs))
1114 io_free_req(req);
1115}
1116
Jens Axboe978db572019-11-14 22:39:04 -07001117/*
1118 * Must only be used if we don't need to care about links, usually from
1119 * within the completion handling itself.
1120 */
1121static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001122{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001123 /* drop both submit and complete references */
1124 if (refcount_sub_and_test(2, &req->refs))
1125 __io_free_req(req);
1126}
1127
Jens Axboe978db572019-11-14 22:39:04 -07001128static void io_double_put_req(struct io_kiocb *req)
1129{
1130 /* drop both submit and complete references */
1131 if (refcount_sub_and_test(2, &req->refs))
1132 io_free_req(req);
1133}
1134
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001135static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001136{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001137 struct io_rings *rings = ctx->rings;
1138
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001139 /*
1140 * noflush == true is from the waitqueue handler, just ensure we wake
1141 * up the task, and the next invocation will flush the entries. We
1142 * cannot safely to it from here.
1143 */
1144 if (noflush && !list_empty(&ctx->cq_overflow_list))
1145 return -1U;
1146
1147 io_cqring_overflow_flush(ctx, false);
1148
Jens Axboea3a0e432019-08-20 11:03:11 -06001149 /* See comment at the top of this file */
1150 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001151 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001152}
1153
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001154static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1155{
1156 struct io_rings *rings = ctx->rings;
1157
1158 /* make sure SQ entry isn't read before tail */
1159 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1160}
1161
Jens Axboedef596e2019-01-09 08:59:42 -07001162/*
1163 * Find and free completed poll iocbs
1164 */
1165static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1166 struct list_head *done)
1167{
1168 void *reqs[IO_IOPOLL_BATCH];
1169 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001170 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001171
Jens Axboe09bb8392019-03-13 12:39:28 -06001172 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001173 while (!list_empty(done)) {
1174 req = list_first_entry(done, struct io_kiocb, list);
1175 list_del(&req->list);
1176
Jens Axboe78e19bb2019-11-06 15:21:34 -07001177 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001178 (*nr_events)++;
1179
Jens Axboe09bb8392019-03-13 12:39:28 -06001180 if (refcount_dec_and_test(&req->refs)) {
1181 /* If we're not using fixed files, we have to pair the
1182 * completion part with the file put. Use regular
1183 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001184 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001185 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001186 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1187 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1188 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001189 reqs[to_free++] = req;
1190 if (to_free == ARRAY_SIZE(reqs))
1191 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001192 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001193 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001194 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001195 }
Jens Axboedef596e2019-01-09 08:59:42 -07001196 }
Jens Axboedef596e2019-01-09 08:59:42 -07001197
Jens Axboe09bb8392019-03-13 12:39:28 -06001198 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001199 io_free_req_many(ctx, reqs, &to_free);
1200}
1201
1202static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1203 long min)
1204{
1205 struct io_kiocb *req, *tmp;
1206 LIST_HEAD(done);
1207 bool spin;
1208 int ret;
1209
1210 /*
1211 * Only spin for completions if we don't have multiple devices hanging
1212 * off our complete list, and we're under the requested amount.
1213 */
1214 spin = !ctx->poll_multi_file && *nr_events < min;
1215
1216 ret = 0;
1217 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001218 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001219
1220 /*
1221 * Move completed entries to our local list. If we find a
1222 * request that requires polling, break out and complete
1223 * the done list first, if we have entries there.
1224 */
1225 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1226 list_move_tail(&req->list, &done);
1227 continue;
1228 }
1229 if (!list_empty(&done))
1230 break;
1231
1232 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1233 if (ret < 0)
1234 break;
1235
1236 if (ret && spin)
1237 spin = false;
1238 ret = 0;
1239 }
1240
1241 if (!list_empty(&done))
1242 io_iopoll_complete(ctx, nr_events, &done);
1243
1244 return ret;
1245}
1246
1247/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001248 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001249 * non-spinning poll check - we'll still enter the driver poll loop, but only
1250 * as a non-spinning completion check.
1251 */
1252static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1253 long min)
1254{
Jens Axboe08f54392019-08-21 22:19:11 -06001255 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001256 int ret;
1257
1258 ret = io_do_iopoll(ctx, nr_events, min);
1259 if (ret < 0)
1260 return ret;
1261 if (!min || *nr_events >= min)
1262 return 0;
1263 }
1264
1265 return 1;
1266}
1267
1268/*
1269 * We can't just wait for polled events to come to us, we have to actively
1270 * find and complete them.
1271 */
1272static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1273{
1274 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1275 return;
1276
1277 mutex_lock(&ctx->uring_lock);
1278 while (!list_empty(&ctx->poll_list)) {
1279 unsigned int nr_events = 0;
1280
1281 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001282
1283 /*
1284 * Ensure we allow local-to-the-cpu processing to take place,
1285 * in this case we need to ensure that we reap all events.
1286 */
1287 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001288 }
1289 mutex_unlock(&ctx->uring_lock);
1290}
1291
Jens Axboe2b2ed972019-10-25 10:06:15 -06001292static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1293 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001294{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001295 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001296
1297 do {
1298 int tmin = 0;
1299
Jens Axboe500f9fb2019-08-19 12:15:59 -06001300 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001301 * Don't enter poll loop if we already have events pending.
1302 * If we do, we can potentially be spinning for commands that
1303 * already triggered a CQE (eg in error).
1304 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001305 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001306 break;
1307
1308 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001309 * If a submit got punted to a workqueue, we can have the
1310 * application entering polling for a command before it gets
1311 * issued. That app will hold the uring_lock for the duration
1312 * of the poll right here, so we need to take a breather every
1313 * now and then to ensure that the issue has a chance to add
1314 * the poll to the issued list. Otherwise we can spin here
1315 * forever, while the workqueue is stuck trying to acquire the
1316 * very same mutex.
1317 */
1318 if (!(++iters & 7)) {
1319 mutex_unlock(&ctx->uring_lock);
1320 mutex_lock(&ctx->uring_lock);
1321 }
1322
Jens Axboedef596e2019-01-09 08:59:42 -07001323 if (*nr_events < min)
1324 tmin = min - *nr_events;
1325
1326 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1327 if (ret <= 0)
1328 break;
1329 ret = 0;
1330 } while (min && !*nr_events && !need_resched());
1331
Jens Axboe2b2ed972019-10-25 10:06:15 -06001332 return ret;
1333}
1334
1335static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1336 long min)
1337{
1338 int ret;
1339
1340 /*
1341 * We disallow the app entering submit/complete with polling, but we
1342 * still need to lock the ring to prevent racing with polled issue
1343 * that got punted to a workqueue.
1344 */
1345 mutex_lock(&ctx->uring_lock);
1346 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001347 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001348 return ret;
1349}
1350
Jens Axboe491381ce2019-10-17 09:20:46 -06001351static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001352{
Jens Axboe491381ce2019-10-17 09:20:46 -06001353 /*
1354 * Tell lockdep we inherited freeze protection from submission
1355 * thread.
1356 */
1357 if (req->flags & REQ_F_ISREG) {
1358 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001359
Jens Axboe491381ce2019-10-17 09:20:46 -06001360 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001361 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001362 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001363}
1364
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001365static inline void req_set_fail_links(struct io_kiocb *req)
1366{
1367 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1368 req->flags |= REQ_F_FAIL_LINK;
1369}
1370
Jens Axboeba816ad2019-09-28 11:36:45 -06001371static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001372{
Jens Axboe9adbd452019-12-20 08:45:55 -07001373 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001374
Jens Axboe491381ce2019-10-17 09:20:46 -06001375 if (kiocb->ki_flags & IOCB_WRITE)
1376 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001377
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001378 if (res != req->result)
1379 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001380 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001381}
1382
1383static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1384{
Jens Axboe9adbd452019-12-20 08:45:55 -07001385 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001386
1387 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001388 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001389}
1390
Jens Axboeba816ad2019-09-28 11:36:45 -06001391static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1392{
Jens Axboe9adbd452019-12-20 08:45:55 -07001393 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001394 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001395
1396 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001397 io_put_req_find_next(req, &nxt);
1398
1399 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001400}
1401
Jens Axboedef596e2019-01-09 08:59:42 -07001402static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1403{
Jens Axboe9adbd452019-12-20 08:45:55 -07001404 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001405
Jens Axboe491381ce2019-10-17 09:20:46 -06001406 if (kiocb->ki_flags & IOCB_WRITE)
1407 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001408
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001409 if (res != req->result)
1410 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001411 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001412 if (res != -EAGAIN)
1413 req->flags |= REQ_F_IOPOLL_COMPLETED;
1414}
1415
1416/*
1417 * After the iocb has been issued, it's safe to be found on the poll list.
1418 * Adding the kiocb to the list AFTER submission ensures that we don't
1419 * find it from a io_iopoll_getevents() thread before the issuer is done
1420 * accessing the kiocb cookie.
1421 */
1422static void io_iopoll_req_issued(struct io_kiocb *req)
1423{
1424 struct io_ring_ctx *ctx = req->ctx;
1425
1426 /*
1427 * Track whether we have multiple files in our lists. This will impact
1428 * how we do polling eventually, not spinning if we're on potentially
1429 * different devices.
1430 */
1431 if (list_empty(&ctx->poll_list)) {
1432 ctx->poll_multi_file = false;
1433 } else if (!ctx->poll_multi_file) {
1434 struct io_kiocb *list_req;
1435
1436 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1437 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001438 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001439 ctx->poll_multi_file = true;
1440 }
1441
1442 /*
1443 * For fast devices, IO may have already completed. If it has, add
1444 * it to the front so we find it first.
1445 */
1446 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1447 list_add(&req->list, &ctx->poll_list);
1448 else
1449 list_add_tail(&req->list, &ctx->poll_list);
1450}
1451
Jens Axboe3d6770f2019-04-13 11:50:54 -06001452static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001453{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001454 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001455 int diff = state->has_refs - state->used_refs;
1456
1457 if (diff)
1458 fput_many(state->file, diff);
1459 state->file = NULL;
1460 }
1461}
1462
1463/*
1464 * Get as many references to a file as we have IOs left in this submission,
1465 * assuming most submissions are for one file, or at least that each file
1466 * has more than one submission.
1467 */
1468static struct file *io_file_get(struct io_submit_state *state, int fd)
1469{
1470 if (!state)
1471 return fget(fd);
1472
1473 if (state->file) {
1474 if (state->fd == fd) {
1475 state->used_refs++;
1476 state->ios_left--;
1477 return state->file;
1478 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001479 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001480 }
1481 state->file = fget_many(fd, state->ios_left);
1482 if (!state->file)
1483 return NULL;
1484
1485 state->fd = fd;
1486 state->has_refs = state->ios_left;
1487 state->used_refs = 1;
1488 state->ios_left--;
1489 return state->file;
1490}
1491
Jens Axboe2b188cc2019-01-07 10:46:33 -07001492/*
1493 * If we tracked the file through the SCM inflight mechanism, we could support
1494 * any file. For now, just ensure that anything potentially problematic is done
1495 * inline.
1496 */
1497static bool io_file_supports_async(struct file *file)
1498{
1499 umode_t mode = file_inode(file)->i_mode;
1500
Jens Axboe10d59342019-12-09 20:16:22 -07001501 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001502 return true;
1503 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1504 return true;
1505
1506 return false;
1507}
1508
Jens Axboe3529d8c2019-12-19 18:24:38 -07001509static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1510 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001511{
Jens Axboedef596e2019-01-09 08:59:42 -07001512 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001513 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001514 unsigned ioprio;
1515 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001516
Jens Axboe09bb8392019-03-13 12:39:28 -06001517 if (!req->file)
1518 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001519
Jens Axboe491381ce2019-10-17 09:20:46 -06001520 if (S_ISREG(file_inode(req->file)->i_mode))
1521 req->flags |= REQ_F_ISREG;
1522
Jens Axboe2b188cc2019-01-07 10:46:33 -07001523 kiocb->ki_pos = READ_ONCE(sqe->off);
1524 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1525 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1526
1527 ioprio = READ_ONCE(sqe->ioprio);
1528 if (ioprio) {
1529 ret = ioprio_check_cap(ioprio);
1530 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001531 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001532
1533 kiocb->ki_ioprio = ioprio;
1534 } else
1535 kiocb->ki_ioprio = get_current_ioprio();
1536
1537 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1538 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001539 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001540
1541 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001542 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1543 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001544 req->flags |= REQ_F_NOWAIT;
1545
1546 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001547 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001548
Jens Axboedef596e2019-01-09 08:59:42 -07001549 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001550 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1551 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001552 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001553
Jens Axboedef596e2019-01-09 08:59:42 -07001554 kiocb->ki_flags |= IOCB_HIPRI;
1555 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001556 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001557 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001558 if (kiocb->ki_flags & IOCB_HIPRI)
1559 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001560 kiocb->ki_complete = io_complete_rw;
1561 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001562
Jens Axboe3529d8c2019-12-19 18:24:38 -07001563 req->rw.addr = READ_ONCE(sqe->addr);
1564 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001565 /* we own ->private, reuse it for the buffer index */
1566 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001567 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001568 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001569}
1570
1571static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1572{
1573 switch (ret) {
1574 case -EIOCBQUEUED:
1575 break;
1576 case -ERESTARTSYS:
1577 case -ERESTARTNOINTR:
1578 case -ERESTARTNOHAND:
1579 case -ERESTART_RESTARTBLOCK:
1580 /*
1581 * We can't just restart the syscall, since previously
1582 * submitted sqes may already be in progress. Just fail this
1583 * IO with EINTR.
1584 */
1585 ret = -EINTR;
1586 /* fall through */
1587 default:
1588 kiocb->ki_complete(kiocb, ret, 0);
1589 }
1590}
1591
Jens Axboeba816ad2019-09-28 11:36:45 -06001592static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1593 bool in_async)
1594{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001595 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001596 *nxt = __io_complete_rw(kiocb, ret);
1597 else
1598 io_rw_done(kiocb, ret);
1599}
1600
Jens Axboe9adbd452019-12-20 08:45:55 -07001601static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001602 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001603{
Jens Axboe9adbd452019-12-20 08:45:55 -07001604 struct io_ring_ctx *ctx = req->ctx;
1605 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001606 struct io_mapped_ubuf *imu;
1607 unsigned index, buf_index;
1608 size_t offset;
1609 u64 buf_addr;
1610
1611 /* attempt to use fixed buffers without having provided iovecs */
1612 if (unlikely(!ctx->user_bufs))
1613 return -EFAULT;
1614
Jens Axboe9adbd452019-12-20 08:45:55 -07001615 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001616 if (unlikely(buf_index >= ctx->nr_user_bufs))
1617 return -EFAULT;
1618
1619 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1620 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001621 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001622
1623 /* overflow */
1624 if (buf_addr + len < buf_addr)
1625 return -EFAULT;
1626 /* not inside the mapped region */
1627 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1628 return -EFAULT;
1629
1630 /*
1631 * May not be a start of buffer, set size appropriately
1632 * and advance us to the beginning.
1633 */
1634 offset = buf_addr - imu->ubuf;
1635 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001636
1637 if (offset) {
1638 /*
1639 * Don't use iov_iter_advance() here, as it's really slow for
1640 * using the latter parts of a big fixed buffer - it iterates
1641 * over each segment manually. We can cheat a bit here, because
1642 * we know that:
1643 *
1644 * 1) it's a BVEC iter, we set it up
1645 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1646 * first and last bvec
1647 *
1648 * So just find our index, and adjust the iterator afterwards.
1649 * If the offset is within the first bvec (or the whole first
1650 * bvec, just use iov_iter_advance(). This makes it easier
1651 * since we can just skip the first segment, which may not
1652 * be PAGE_SIZE aligned.
1653 */
1654 const struct bio_vec *bvec = imu->bvec;
1655
1656 if (offset <= bvec->bv_len) {
1657 iov_iter_advance(iter, offset);
1658 } else {
1659 unsigned long seg_skip;
1660
1661 /* skip first vec */
1662 offset -= bvec->bv_len;
1663 seg_skip = 1 + (offset >> PAGE_SHIFT);
1664
1665 iter->bvec = bvec + seg_skip;
1666 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001667 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001668 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001669 }
1670 }
1671
Jens Axboe5e559562019-11-13 16:12:46 -07001672 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001673}
1674
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001675static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1676 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001677{
Jens Axboe9adbd452019-12-20 08:45:55 -07001678 void __user *buf = u64_to_user_ptr(req->rw.addr);
1679 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001680 u8 opcode;
1681
Jens Axboed625c6e2019-12-17 19:53:05 -07001682 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001683 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001684 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001685 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001686 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001687
Jens Axboe9adbd452019-12-20 08:45:55 -07001688 /* buffer index only valid with fixed read/write */
1689 if (req->rw.kiocb.private)
1690 return -EINVAL;
1691
Jens Axboef67676d2019-12-02 11:03:47 -07001692 if (req->io) {
1693 struct io_async_rw *iorw = &req->io->rw;
1694
1695 *iovec = iorw->iov;
1696 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1697 if (iorw->iov == iorw->fast_iov)
1698 *iovec = NULL;
1699 return iorw->size;
1700 }
1701
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001702 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001703 return -EFAULT;
1704
1705#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001706 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001707 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1708 iovec, iter);
1709#endif
1710
1711 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1712}
1713
Jens Axboe32960612019-09-23 11:05:34 -06001714/*
1715 * For files that don't have ->read_iter() and ->write_iter(), handle them
1716 * by looping over ->read() or ->write() manually.
1717 */
1718static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1719 struct iov_iter *iter)
1720{
1721 ssize_t ret = 0;
1722
1723 /*
1724 * Don't support polled IO through this interface, and we can't
1725 * support non-blocking either. For the latter, this just causes
1726 * the kiocb to be handled from an async context.
1727 */
1728 if (kiocb->ki_flags & IOCB_HIPRI)
1729 return -EOPNOTSUPP;
1730 if (kiocb->ki_flags & IOCB_NOWAIT)
1731 return -EAGAIN;
1732
1733 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001734 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001735 ssize_t nr;
1736
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001737 if (!iov_iter_is_bvec(iter)) {
1738 iovec = iov_iter_iovec(iter);
1739 } else {
1740 /* fixed buffers import bvec */
1741 iovec.iov_base = kmap(iter->bvec->bv_page)
1742 + iter->iov_offset;
1743 iovec.iov_len = min(iter->count,
1744 iter->bvec->bv_len - iter->iov_offset);
1745 }
1746
Jens Axboe32960612019-09-23 11:05:34 -06001747 if (rw == READ) {
1748 nr = file->f_op->read(file, iovec.iov_base,
1749 iovec.iov_len, &kiocb->ki_pos);
1750 } else {
1751 nr = file->f_op->write(file, iovec.iov_base,
1752 iovec.iov_len, &kiocb->ki_pos);
1753 }
1754
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001755 if (iov_iter_is_bvec(iter))
1756 kunmap(iter->bvec->bv_page);
1757
Jens Axboe32960612019-09-23 11:05:34 -06001758 if (nr < 0) {
1759 if (!ret)
1760 ret = nr;
1761 break;
1762 }
1763 ret += nr;
1764 if (nr != iovec.iov_len)
1765 break;
1766 iov_iter_advance(iter, nr);
1767 }
1768
1769 return ret;
1770}
1771
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001772static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07001773 struct iovec *iovec, struct iovec *fast_iov,
1774 struct iov_iter *iter)
1775{
1776 req->io->rw.nr_segs = iter->nr_segs;
1777 req->io->rw.size = io_size;
1778 req->io->rw.iov = iovec;
1779 if (!req->io->rw.iov) {
1780 req->io->rw.iov = req->io->rw.fast_iov;
1781 memcpy(req->io->rw.iov, fast_iov,
1782 sizeof(struct iovec) * iter->nr_segs);
1783 }
1784}
1785
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001786static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07001787{
1788 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07001789 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001790}
1791
1792static void io_rw_async(struct io_wq_work **workptr)
1793{
1794 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1795 struct iovec *iov = NULL;
1796
1797 if (req->io->rw.iov != req->io->rw.fast_iov)
1798 iov = req->io->rw.iov;
1799 io_wq_submit_work(workptr);
1800 kfree(iov);
1801}
1802
1803static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
1804 struct iovec *iovec, struct iovec *fast_iov,
1805 struct iov_iter *iter)
1806{
Jens Axboe74566df2020-01-13 19:23:24 -07001807 if (req->opcode == IORING_OP_READ_FIXED ||
1808 req->opcode == IORING_OP_WRITE_FIXED)
1809 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001810 if (!req->io && io_alloc_async_ctx(req))
1811 return -ENOMEM;
1812
1813 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
1814 req->work.func = io_rw_async;
1815 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001816}
1817
Jens Axboe3529d8c2019-12-19 18:24:38 -07001818static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1819 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07001820{
Jens Axboe3529d8c2019-12-19 18:24:38 -07001821 struct io_async_ctx *io;
1822 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07001823 ssize_t ret;
1824
Jens Axboe3529d8c2019-12-19 18:24:38 -07001825 ret = io_prep_rw(req, sqe, force_nonblock);
1826 if (ret)
1827 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07001828
Jens Axboe3529d8c2019-12-19 18:24:38 -07001829 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1830 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07001831
Jens Axboe3529d8c2019-12-19 18:24:38 -07001832 if (!req->io)
1833 return 0;
1834
1835 io = req->io;
1836 io->rw.iov = io->rw.fast_iov;
1837 req->io = NULL;
1838 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
1839 req->io = io;
1840 if (ret < 0)
1841 return ret;
1842
1843 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
1844 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001845}
1846
Pavel Begunkov267bc902019-11-07 01:41:08 +03001847static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001848 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001849{
1850 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001851 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001852 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001853 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001854 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001855
Jens Axboe3529d8c2019-12-19 18:24:38 -07001856 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07001857 if (ret < 0)
1858 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001859
Jens Axboefd6c2e42019-12-18 12:19:41 -07001860 /* Ensure we clear previously set non-block flag */
1861 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001862 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001863
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08001864 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001865 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001866 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001867 req->result = io_size;
1868
1869 /*
1870 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1871 * we know to async punt it even if it was opened O_NONBLOCK
1872 */
Jens Axboe9adbd452019-12-20 08:45:55 -07001873 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07001874 req->flags |= REQ_F_MUST_PUNT;
1875 goto copy_iov;
1876 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001877
Jens Axboe31b51512019-01-18 22:56:34 -07001878 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07001879 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001880 if (!ret) {
1881 ssize_t ret2;
1882
Jens Axboe9adbd452019-12-20 08:45:55 -07001883 if (req->file->f_op->read_iter)
1884 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001885 else
Jens Axboe9adbd452019-12-20 08:45:55 -07001886 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001887
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001888 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07001889 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001890 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001891 } else {
1892copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001893 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001894 inline_vecs, &iter);
1895 if (ret)
1896 goto out_free;
1897 return -EAGAIN;
1898 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899 }
Jens Axboef67676d2019-12-02 11:03:47 -07001900out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001901 if (!io_wq_current_is_worker())
1902 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001903 return ret;
1904}
1905
Jens Axboe3529d8c2019-12-19 18:24:38 -07001906static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1907 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07001908{
Jens Axboe3529d8c2019-12-19 18:24:38 -07001909 struct io_async_ctx *io;
1910 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07001911 ssize_t ret;
1912
Jens Axboe3529d8c2019-12-19 18:24:38 -07001913 ret = io_prep_rw(req, sqe, force_nonblock);
1914 if (ret)
1915 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07001916
Jens Axboe3529d8c2019-12-19 18:24:38 -07001917 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
1918 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07001919
Jens Axboe3529d8c2019-12-19 18:24:38 -07001920 if (!req->io)
1921 return 0;
1922
1923 io = req->io;
1924 io->rw.iov = io->rw.fast_iov;
1925 req->io = NULL;
1926 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
1927 req->io = io;
1928 if (ret < 0)
1929 return ret;
1930
1931 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
1932 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001933}
1934
Pavel Begunkov267bc902019-11-07 01:41:08 +03001935static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001936 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001937{
1938 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001939 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001940 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001941 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001942 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001943
Jens Axboe3529d8c2019-12-19 18:24:38 -07001944 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07001945 if (ret < 0)
1946 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001947
Jens Axboefd6c2e42019-12-18 12:19:41 -07001948 /* Ensure we clear previously set non-block flag */
1949 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001950 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001951
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08001952 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001953 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001954 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001955 req->result = io_size;
1956
1957 /*
1958 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1959 * we know to async punt it even if it was opened O_NONBLOCK
1960 */
1961 if (force_nonblock && !io_file_supports_async(req->file)) {
1962 req->flags |= REQ_F_MUST_PUNT;
1963 goto copy_iov;
1964 }
1965
Jens Axboe10d59342019-12-09 20:16:22 -07001966 /* file path doesn't support NOWAIT for non-direct_IO */
1967 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
1968 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07001969 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06001970
Jens Axboe31b51512019-01-18 22:56:34 -07001971 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07001972 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001973 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001974 ssize_t ret2;
1975
Jens Axboe2b188cc2019-01-07 10:46:33 -07001976 /*
1977 * Open-code file_start_write here to grab freeze protection,
1978 * which will be released by another thread in
1979 * io_complete_rw(). Fool lockdep by telling it the lock got
1980 * released so that it doesn't complain about the held lock when
1981 * we return to userspace.
1982 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001983 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001984 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07001985 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07001986 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07001987 SB_FREEZE_WRITE);
1988 }
1989 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001990
Jens Axboe9adbd452019-12-20 08:45:55 -07001991 if (req->file->f_op->write_iter)
1992 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001993 else
Jens Axboe9adbd452019-12-20 08:45:55 -07001994 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07001995 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001996 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001997 } else {
1998copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001999 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002000 inline_vecs, &iter);
2001 if (ret)
2002 goto out_free;
2003 return -EAGAIN;
2004 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002005 }
Jens Axboe31b51512019-01-18 22:56:34 -07002006out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002007 if (!io_wq_current_is_worker())
2008 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002009 return ret;
2010}
2011
2012/*
2013 * IORING_OP_NOP just posts a completion event, nothing else.
2014 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002015static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002016{
2017 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002018
Jens Axboedef596e2019-01-09 08:59:42 -07002019 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2020 return -EINVAL;
2021
Jens Axboe78e19bb2019-11-06 15:21:34 -07002022 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002023 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002024 return 0;
2025}
2026
Jens Axboe3529d8c2019-12-19 18:24:38 -07002027static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002028{
Jens Axboe6b063142019-01-10 22:13:58 -07002029 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002030
Jens Axboe09bb8392019-03-13 12:39:28 -06002031 if (!req->file)
2032 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002033
Jens Axboe6b063142019-01-10 22:13:58 -07002034 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002035 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002036 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002037 return -EINVAL;
2038
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002039 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2040 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2041 return -EINVAL;
2042
2043 req->sync.off = READ_ONCE(sqe->off);
2044 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002045 return 0;
2046}
2047
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002048static bool io_req_cancelled(struct io_kiocb *req)
2049{
2050 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2051 req_set_fail_links(req);
2052 io_cqring_add_event(req, -ECANCELED);
2053 io_put_req(req);
2054 return true;
2055 }
2056
2057 return false;
2058}
2059
Jens Axboe78912932020-01-14 22:09:06 -07002060static void io_link_work_cb(struct io_wq_work **workptr)
2061{
2062 struct io_wq_work *work = *workptr;
2063 struct io_kiocb *link = work->data;
2064
2065 io_queue_linked_timeout(link);
2066 work->func = io_wq_submit_work;
2067}
2068
2069static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2070{
2071 struct io_kiocb *link;
2072
2073 io_prep_async_work(nxt, &link);
2074 *workptr = &nxt->work;
2075 if (link) {
2076 nxt->work.flags |= IO_WQ_WORK_CB;
2077 nxt->work.func = io_link_work_cb;
2078 nxt->work.data = link;
2079 }
2080}
2081
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002082static void io_fsync_finish(struct io_wq_work **workptr)
2083{
2084 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2085 loff_t end = req->sync.off + req->sync.len;
2086 struct io_kiocb *nxt = NULL;
2087 int ret;
2088
2089 if (io_req_cancelled(req))
2090 return;
2091
Jens Axboe9adbd452019-12-20 08:45:55 -07002092 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002093 end > 0 ? end : LLONG_MAX,
2094 req->sync.flags & IORING_FSYNC_DATASYNC);
2095 if (ret < 0)
2096 req_set_fail_links(req);
2097 io_cqring_add_event(req, ret);
2098 io_put_req_find_next(req, &nxt);
2099 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002100 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002101}
2102
Jens Axboefc4df992019-12-10 14:38:45 -07002103static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2104 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002105{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002106 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002107
2108 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002109 if (force_nonblock) {
2110 io_put_req(req);
2111 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002112 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002113 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002114
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002115 work = old_work = &req->work;
2116 io_fsync_finish(&work);
2117 if (work && work != old_work)
2118 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002119 return 0;
2120}
2121
Jens Axboed63d1b52019-12-10 10:38:56 -07002122static void io_fallocate_finish(struct io_wq_work **workptr)
2123{
2124 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2125 struct io_kiocb *nxt = NULL;
2126 int ret;
2127
2128 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2129 req->sync.len);
2130 if (ret < 0)
2131 req_set_fail_links(req);
2132 io_cqring_add_event(req, ret);
2133 io_put_req_find_next(req, &nxt);
2134 if (nxt)
2135 io_wq_assign_next(workptr, nxt);
2136}
2137
2138static int io_fallocate_prep(struct io_kiocb *req,
2139 const struct io_uring_sqe *sqe)
2140{
2141 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2142 return -EINVAL;
2143
2144 req->sync.off = READ_ONCE(sqe->off);
2145 req->sync.len = READ_ONCE(sqe->addr);
2146 req->sync.mode = READ_ONCE(sqe->len);
2147 return 0;
2148}
2149
2150static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2151 bool force_nonblock)
2152{
2153 struct io_wq_work *work, *old_work;
2154
2155 /* fallocate always requiring blocking context */
2156 if (force_nonblock) {
2157 io_put_req(req);
2158 req->work.func = io_fallocate_finish;
2159 return -EAGAIN;
2160 }
2161
2162 work = old_work = &req->work;
2163 io_fallocate_finish(&work);
2164 if (work && work != old_work)
2165 *nxt = container_of(work, struct io_kiocb, work);
2166
2167 return 0;
2168}
2169
Jens Axboe15b71ab2019-12-11 11:20:36 -07002170static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2171{
2172 int ret;
2173
2174 if (sqe->ioprio || sqe->buf_index)
2175 return -EINVAL;
2176
2177 req->open.dfd = READ_ONCE(sqe->fd);
2178 req->open.mode = READ_ONCE(sqe->len);
2179 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2180 req->open.flags = READ_ONCE(sqe->open_flags);
2181
2182 req->open.filename = getname(req->open.fname);
2183 if (IS_ERR(req->open.filename)) {
2184 ret = PTR_ERR(req->open.filename);
2185 req->open.filename = NULL;
2186 return ret;
2187 }
2188
2189 return 0;
2190}
2191
2192static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2193 bool force_nonblock)
2194{
2195 struct open_flags op;
2196 struct open_how how;
2197 struct file *file;
2198 int ret;
2199
2200 if (force_nonblock) {
2201 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2202 return -EAGAIN;
2203 }
2204
2205 how = build_open_how(req->open.flags, req->open.mode);
2206 ret = build_open_flags(&how, &op);
2207 if (ret)
2208 goto err;
2209
2210 ret = get_unused_fd_flags(how.flags);
2211 if (ret < 0)
2212 goto err;
2213
2214 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2215 if (IS_ERR(file)) {
2216 put_unused_fd(ret);
2217 ret = PTR_ERR(file);
2218 } else {
2219 fsnotify_open(file);
2220 fd_install(ret, file);
2221 }
2222err:
2223 putname(req->open.filename);
2224 if (ret < 0)
2225 req_set_fail_links(req);
2226 io_cqring_add_event(req, ret);
2227 io_put_req_find_next(req, nxt);
2228 return 0;
2229}
2230
Jens Axboe3529d8c2019-12-19 18:24:38 -07002231static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002232{
2233 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002234
2235 if (!req->file)
2236 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002237
2238 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2239 return -EINVAL;
2240 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2241 return -EINVAL;
2242
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002243 req->sync.off = READ_ONCE(sqe->off);
2244 req->sync.len = READ_ONCE(sqe->len);
2245 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002246 return 0;
2247}
2248
2249static void io_sync_file_range_finish(struct io_wq_work **workptr)
2250{
2251 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2252 struct io_kiocb *nxt = NULL;
2253 int ret;
2254
2255 if (io_req_cancelled(req))
2256 return;
2257
Jens Axboe9adbd452019-12-20 08:45:55 -07002258 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002259 req->sync.flags);
2260 if (ret < 0)
2261 req_set_fail_links(req);
2262 io_cqring_add_event(req, ret);
2263 io_put_req_find_next(req, &nxt);
2264 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002265 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002266}
2267
Jens Axboefc4df992019-12-10 14:38:45 -07002268static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002269 bool force_nonblock)
2270{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002271 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002272
2273 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002274 if (force_nonblock) {
2275 io_put_req(req);
2276 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002277 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002278 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002279
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002280 work = old_work = &req->work;
2281 io_sync_file_range_finish(&work);
2282 if (work && work != old_work)
2283 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002284 return 0;
2285}
2286
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002287#if defined(CONFIG_NET)
2288static void io_sendrecv_async(struct io_wq_work **workptr)
2289{
2290 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2291 struct iovec *iov = NULL;
2292
2293 if (req->io->rw.iov != req->io->rw.fast_iov)
2294 iov = req->io->msg.iov;
2295 io_wq_submit_work(workptr);
2296 kfree(iov);
2297}
2298#endif
2299
Jens Axboe3529d8c2019-12-19 18:24:38 -07002300static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002301{
Jens Axboe03b12302019-12-02 18:50:25 -07002302#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002303 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002304 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002305
Jens Axboee47293f2019-12-20 08:58:21 -07002306 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2307 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe3529d8c2019-12-19 18:24:38 -07002308
2309 if (!io)
2310 return 0;
2311
Jens Axboed9688562019-12-09 19:35:20 -07002312 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002313 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002314 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002315#else
Jens Axboee47293f2019-12-20 08:58:21 -07002316 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002317#endif
2318}
2319
Jens Axboefc4df992019-12-10 14:38:45 -07002320static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2321 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002322{
2323#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002324 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002325 struct socket *sock;
2326 int ret;
2327
2328 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2329 return -EINVAL;
2330
2331 sock = sock_from_file(req->file, &ret);
2332 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002333 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002334 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002335 unsigned flags;
2336
Jens Axboe03b12302019-12-02 18:50:25 -07002337 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002338 kmsg = &req->io->msg;
2339 kmsg->msg.msg_name = &addr;
2340 /* if iov is set, it's allocated already */
2341 if (!kmsg->iov)
2342 kmsg->iov = kmsg->fast_iov;
2343 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002344 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002345 struct io_sr_msg *sr = &req->sr_msg;
2346
Jens Axboe0b416c32019-12-15 10:57:46 -07002347 kmsg = &io.msg;
2348 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002349
2350 io.msg.iov = io.msg.fast_iov;
2351 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2352 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002353 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002354 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002355 }
2356
Jens Axboee47293f2019-12-20 08:58:21 -07002357 flags = req->sr_msg.msg_flags;
2358 if (flags & MSG_DONTWAIT)
2359 req->flags |= REQ_F_NOWAIT;
2360 else if (force_nonblock)
2361 flags |= MSG_DONTWAIT;
2362
Jens Axboe0b416c32019-12-15 10:57:46 -07002363 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002364 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002365 if (req->io)
2366 return -EAGAIN;
2367 if (io_alloc_async_ctx(req))
2368 return -ENOMEM;
2369 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2370 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002371 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002372 }
2373 if (ret == -ERESTARTSYS)
2374 ret = -EINTR;
2375 }
2376
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002377 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002378 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002379 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002380 if (ret < 0)
2381 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002382 io_put_req_find_next(req, nxt);
2383 return 0;
2384#else
2385 return -EOPNOTSUPP;
2386#endif
2387}
2388
Jens Axboe3529d8c2019-12-19 18:24:38 -07002389static int io_recvmsg_prep(struct io_kiocb *req,
2390 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002391{
2392#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002393 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002394 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002395
Jens Axboe3529d8c2019-12-19 18:24:38 -07002396 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2397 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2398
2399 if (!io)
Jens Axboe06b76d42019-12-19 14:44:26 -07002400 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002401
Jens Axboed9688562019-12-09 19:35:20 -07002402 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002403 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002404 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002405#else
Jens Axboee47293f2019-12-20 08:58:21 -07002406 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002407#endif
2408}
2409
Jens Axboefc4df992019-12-10 14:38:45 -07002410static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2411 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002412{
2413#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002414 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002415 struct socket *sock;
2416 int ret;
2417
2418 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2419 return -EINVAL;
2420
2421 sock = sock_from_file(req->file, &ret);
2422 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002423 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002424 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002425 unsigned flags;
2426
Jens Axboe03b12302019-12-02 18:50:25 -07002427 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002428 kmsg = &req->io->msg;
2429 kmsg->msg.msg_name = &addr;
2430 /* if iov is set, it's allocated already */
2431 if (!kmsg->iov)
2432 kmsg->iov = kmsg->fast_iov;
2433 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002434 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002435 struct io_sr_msg *sr = &req->sr_msg;
2436
Jens Axboe0b416c32019-12-15 10:57:46 -07002437 kmsg = &io.msg;
2438 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002439
2440 io.msg.iov = io.msg.fast_iov;
2441 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
2442 sr->msg_flags, &io.msg.uaddr,
2443 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002444 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002445 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002446 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002447
Jens Axboee47293f2019-12-20 08:58:21 -07002448 flags = req->sr_msg.msg_flags;
2449 if (flags & MSG_DONTWAIT)
2450 req->flags |= REQ_F_NOWAIT;
2451 else if (force_nonblock)
2452 flags |= MSG_DONTWAIT;
2453
2454 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
2455 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002456 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002457 if (req->io)
2458 return -EAGAIN;
2459 if (io_alloc_async_ctx(req))
2460 return -ENOMEM;
2461 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2462 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002463 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002464 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002465 if (ret == -ERESTARTSYS)
2466 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002467 }
2468
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002469 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002470 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002471 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002472 if (ret < 0)
2473 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002474 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002475 return 0;
2476#else
2477 return -EOPNOTSUPP;
2478#endif
2479}
2480
Jens Axboe3529d8c2019-12-19 18:24:38 -07002481static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002482{
2483#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002484 struct io_accept *accept = &req->accept;
2485
Jens Axboe17f2fe32019-10-17 14:42:58 -06002486 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2487 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002488 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002489 return -EINVAL;
2490
Jens Axboed55e5f52019-12-11 16:12:15 -07002491 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2492 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002493 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002494 return 0;
2495#else
2496 return -EOPNOTSUPP;
2497#endif
2498}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002499
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002500#if defined(CONFIG_NET)
2501static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2502 bool force_nonblock)
2503{
2504 struct io_accept *accept = &req->accept;
2505 unsigned file_flags;
2506 int ret;
2507
2508 file_flags = force_nonblock ? O_NONBLOCK : 0;
2509 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2510 accept->addr_len, accept->flags);
2511 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002512 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002513 if (ret == -ERESTARTSYS)
2514 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002515 if (ret < 0)
2516 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002517 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002518 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002519 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002520}
2521
2522static void io_accept_finish(struct io_wq_work **workptr)
2523{
2524 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2525 struct io_kiocb *nxt = NULL;
2526
2527 if (io_req_cancelled(req))
2528 return;
2529 __io_accept(req, &nxt, false);
2530 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002531 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002532}
2533#endif
2534
2535static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2536 bool force_nonblock)
2537{
2538#if defined(CONFIG_NET)
2539 int ret;
2540
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002541 ret = __io_accept(req, nxt, force_nonblock);
2542 if (ret == -EAGAIN && force_nonblock) {
2543 req->work.func = io_accept_finish;
2544 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2545 io_put_req(req);
2546 return -EAGAIN;
2547 }
2548 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002549#else
2550 return -EOPNOTSUPP;
2551#endif
2552}
2553
Jens Axboe3529d8c2019-12-19 18:24:38 -07002554static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07002555{
2556#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002557 struct io_connect *conn = &req->connect;
2558 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07002559
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002560 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2561 return -EINVAL;
2562 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2563 return -EINVAL;
2564
Jens Axboe3529d8c2019-12-19 18:24:38 -07002565 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2566 conn->addr_len = READ_ONCE(sqe->addr2);
2567
2568 if (!io)
2569 return 0;
2570
2571 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002572 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002573#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002574 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07002575#endif
2576}
2577
Jens Axboefc4df992019-12-10 14:38:45 -07002578static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
2579 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07002580{
2581#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07002582 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002583 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002584 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002585
Jens Axboef499a022019-12-02 16:28:46 -07002586 if (req->io) {
2587 io = req->io;
2588 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002589 ret = move_addr_to_kernel(req->connect.addr,
2590 req->connect.addr_len,
2591 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002592 if (ret)
2593 goto out;
2594 io = &__io;
2595 }
2596
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002597 file_flags = force_nonblock ? O_NONBLOCK : 0;
2598
2599 ret = __sys_connect_file(req->file, &io->connect.address,
2600 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002601 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002602 if (req->io)
2603 return -EAGAIN;
2604 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07002605 ret = -ENOMEM;
2606 goto out;
2607 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002608 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07002609 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002610 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002611 if (ret == -ERESTARTSYS)
2612 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002613out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002614 if (ret < 0)
2615 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002616 io_cqring_add_event(req, ret);
2617 io_put_req_find_next(req, nxt);
2618 return 0;
2619#else
2620 return -EOPNOTSUPP;
2621#endif
2622}
2623
Jens Axboe221c5eb2019-01-17 09:41:58 -07002624static void io_poll_remove_one(struct io_kiocb *req)
2625{
2626 struct io_poll_iocb *poll = &req->poll;
2627
2628 spin_lock(&poll->head->lock);
2629 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07002630 if (!list_empty(&poll->wait.entry)) {
2631 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002632 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002633 }
2634 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002635 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002636}
2637
2638static void io_poll_remove_all(struct io_ring_ctx *ctx)
2639{
Jens Axboe78076bb2019-12-04 19:56:40 -07002640 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002641 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07002642 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002643
2644 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002645 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2646 struct hlist_head *list;
2647
2648 list = &ctx->cancel_hash[i];
2649 hlist_for_each_entry_safe(req, tmp, list, hash_node)
2650 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002651 }
2652 spin_unlock_irq(&ctx->completion_lock);
2653}
2654
Jens Axboe47f46762019-11-09 17:43:02 -07002655static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2656{
Jens Axboe78076bb2019-12-04 19:56:40 -07002657 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07002658 struct io_kiocb *req;
2659
Jens Axboe78076bb2019-12-04 19:56:40 -07002660 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2661 hlist_for_each_entry(req, list, hash_node) {
2662 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002663 io_poll_remove_one(req);
2664 return 0;
2665 }
Jens Axboe47f46762019-11-09 17:43:02 -07002666 }
2667
2668 return -ENOENT;
2669}
2670
Jens Axboe3529d8c2019-12-19 18:24:38 -07002671static int io_poll_remove_prep(struct io_kiocb *req,
2672 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002673{
Jens Axboe221c5eb2019-01-17 09:41:58 -07002674 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2675 return -EINVAL;
2676 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2677 sqe->poll_events)
2678 return -EINVAL;
2679
Jens Axboe0969e782019-12-17 18:40:57 -07002680 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07002681 return 0;
2682}
2683
2684/*
2685 * Find a running poll command that matches one specified in sqe->addr,
2686 * and remove it if found.
2687 */
2688static int io_poll_remove(struct io_kiocb *req)
2689{
2690 struct io_ring_ctx *ctx = req->ctx;
2691 u64 addr;
2692 int ret;
2693
Jens Axboe0969e782019-12-17 18:40:57 -07002694 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002695 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07002696 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002697 spin_unlock_irq(&ctx->completion_lock);
2698
Jens Axboe78e19bb2019-11-06 15:21:34 -07002699 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002700 if (ret < 0)
2701 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002702 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002703 return 0;
2704}
2705
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002706static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002707{
Jackie Liua197f662019-11-08 08:09:12 -07002708 struct io_ring_ctx *ctx = req->ctx;
2709
Jens Axboe8c838782019-03-12 15:48:16 -06002710 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002711 if (error)
2712 io_cqring_fill_event(req, error);
2713 else
2714 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002715 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002716}
2717
Jens Axboe561fb042019-10-24 07:25:42 -06002718static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002719{
Jens Axboe561fb042019-10-24 07:25:42 -06002720 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002721 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2722 struct io_poll_iocb *poll = &req->poll;
2723 struct poll_table_struct pt = { ._key = poll->events };
2724 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002725 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002726 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002727 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002728
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002729 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002730 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002731 ret = -ECANCELED;
2732 } else if (READ_ONCE(poll->canceled)) {
2733 ret = -ECANCELED;
2734 }
Jens Axboe561fb042019-10-24 07:25:42 -06002735
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002736 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002737 mask = vfs_poll(poll->file, &pt) & poll->events;
2738
2739 /*
2740 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2741 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2742 * synchronize with them. In the cancellation case the list_del_init
2743 * itself is not actually needed, but harmless so we keep it in to
2744 * avoid further branches in the fast path.
2745 */
2746 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002747 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07002748 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002749 spin_unlock_irq(&ctx->completion_lock);
2750 return;
2751 }
Jens Axboe78076bb2019-12-04 19:56:40 -07002752 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002753 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002754 spin_unlock_irq(&ctx->completion_lock);
2755
Jens Axboe8c838782019-03-12 15:48:16 -06002756 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002757
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002758 if (ret < 0)
2759 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002760 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002761 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002762 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002763}
2764
2765static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2766 void *key)
2767{
Jens Axboee9444752019-11-26 15:02:04 -07002768 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002769 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2770 struct io_ring_ctx *ctx = req->ctx;
2771 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002772 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002773
2774 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002775 if (mask && !(mask & poll->events))
2776 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002777
Jens Axboe392edb42019-12-09 17:52:20 -07002778 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002779
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002780 /*
2781 * Run completion inline if we can. We're using trylock here because
2782 * we are violating the completion_lock -> poll wq lock ordering.
2783 * If we have a link timeout we're going to need the completion_lock
2784 * for finalizing the request, mark us as having grabbed that already.
2785 */
Jens Axboe8c838782019-03-12 15:48:16 -06002786 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboe78076bb2019-12-04 19:56:40 -07002787 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002788 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002789 req->flags |= REQ_F_COMP_LOCKED;
2790 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002791 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2792
2793 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002794 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002795 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002796 }
2797
Jens Axboe221c5eb2019-01-17 09:41:58 -07002798 return 1;
2799}
2800
2801struct io_poll_table {
2802 struct poll_table_struct pt;
2803 struct io_kiocb *req;
2804 int error;
2805};
2806
2807static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2808 struct poll_table_struct *p)
2809{
2810 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2811
2812 if (unlikely(pt->req->poll.head)) {
2813 pt->error = -EINVAL;
2814 return;
2815 }
2816
2817 pt->error = 0;
2818 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07002819 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002820}
2821
Jens Axboeeac406c2019-11-14 12:09:58 -07002822static void io_poll_req_insert(struct io_kiocb *req)
2823{
2824 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07002825 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07002826
Jens Axboe78076bb2019-12-04 19:56:40 -07002827 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
2828 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07002829}
2830
Jens Axboe3529d8c2019-12-19 18:24:38 -07002831static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002832{
2833 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002834 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002835
2836 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2837 return -EINVAL;
2838 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2839 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002840 if (!poll->file)
2841 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002842
Jens Axboe221c5eb2019-01-17 09:41:58 -07002843 events = READ_ONCE(sqe->poll_events);
2844 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07002845 return 0;
2846}
2847
2848static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
2849{
2850 struct io_poll_iocb *poll = &req->poll;
2851 struct io_ring_ctx *ctx = req->ctx;
2852 struct io_poll_table ipt;
2853 bool cancel = false;
2854 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07002855
2856 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07002857 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002858
Jens Axboe221c5eb2019-01-17 09:41:58 -07002859 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002860 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002861 poll->canceled = false;
2862
2863 ipt.pt._qproc = io_poll_queue_proc;
2864 ipt.pt._key = poll->events;
2865 ipt.req = req;
2866 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2867
2868 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07002869 INIT_LIST_HEAD(&poll->wait.entry);
2870 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2871 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002872
Jens Axboe36703242019-07-25 10:20:18 -06002873 INIT_LIST_HEAD(&req->list);
2874
Jens Axboe221c5eb2019-01-17 09:41:58 -07002875 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002876
2877 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002878 if (likely(poll->head)) {
2879 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07002880 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06002881 if (ipt.error)
2882 cancel = true;
2883 ipt.error = 0;
2884 mask = 0;
2885 }
2886 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07002887 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002888 else if (cancel)
2889 WRITE_ONCE(poll->canceled, true);
2890 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002891 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002892 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002893 }
Jens Axboe8c838782019-03-12 15:48:16 -06002894 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002895 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002896 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002897 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002898 spin_unlock_irq(&ctx->completion_lock);
2899
Jens Axboe8c838782019-03-12 15:48:16 -06002900 if (mask) {
2901 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002902 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002903 }
Jens Axboe8c838782019-03-12 15:48:16 -06002904 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002905}
2906
Jens Axboe5262f562019-09-17 12:26:57 -06002907static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2908{
Jens Axboead8a48a2019-11-15 08:49:11 -07002909 struct io_timeout_data *data = container_of(timer,
2910 struct io_timeout_data, timer);
2911 struct io_kiocb *req = data->req;
2912 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002913 unsigned long flags;
2914
Jens Axboe5262f562019-09-17 12:26:57 -06002915 atomic_inc(&ctx->cq_timeouts);
2916
2917 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002918 /*
Jens Axboe11365042019-10-16 09:08:32 -06002919 * We could be racing with timeout deletion. If the list is empty,
2920 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002921 */
Jens Axboe842f9612019-10-29 12:34:10 -06002922 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002923 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002924
Jens Axboe11365042019-10-16 09:08:32 -06002925 /*
2926 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08002927 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06002928 * pointer will be increased, otherwise other timeout reqs may
2929 * return in advance without waiting for enough wait_nr.
2930 */
2931 prev = req;
2932 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2933 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002934 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002935 }
Jens Axboe842f9612019-10-29 12:34:10 -06002936
Jens Axboe78e19bb2019-11-06 15:21:34 -07002937 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002938 io_commit_cqring(ctx);
2939 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2940
2941 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002942 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06002943 io_put_req(req);
2944 return HRTIMER_NORESTART;
2945}
2946
Jens Axboe47f46762019-11-09 17:43:02 -07002947static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2948{
2949 struct io_kiocb *req;
2950 int ret = -ENOENT;
2951
2952 list_for_each_entry(req, &ctx->timeout_list, list) {
2953 if (user_data == req->user_data) {
2954 list_del_init(&req->list);
2955 ret = 0;
2956 break;
2957 }
2958 }
2959
2960 if (ret == -ENOENT)
2961 return ret;
2962
Jens Axboe2d283902019-12-04 11:08:05 -07002963 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002964 if (ret == -1)
2965 return -EALREADY;
2966
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002967 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07002968 io_cqring_fill_event(req, -ECANCELED);
2969 io_put_req(req);
2970 return 0;
2971}
2972
Jens Axboe3529d8c2019-12-19 18:24:38 -07002973static int io_timeout_remove_prep(struct io_kiocb *req,
2974 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07002975{
Jens Axboeb29472e2019-12-17 18:50:29 -07002976 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2977 return -EINVAL;
2978 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2979 return -EINVAL;
2980
2981 req->timeout.addr = READ_ONCE(sqe->addr);
2982 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
2983 if (req->timeout.flags)
2984 return -EINVAL;
2985
Jens Axboeb29472e2019-12-17 18:50:29 -07002986 return 0;
2987}
2988
Jens Axboe11365042019-10-16 09:08:32 -06002989/*
2990 * Remove or update an existing timeout command
2991 */
Jens Axboefc4df992019-12-10 14:38:45 -07002992static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06002993{
2994 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002995 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002996
Jens Axboe11365042019-10-16 09:08:32 -06002997 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07002998 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06002999
Jens Axboe47f46762019-11-09 17:43:02 -07003000 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003001 io_commit_cqring(ctx);
3002 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003003 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003004 if (ret < 0)
3005 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003006 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003007 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003008}
3009
Jens Axboe3529d8c2019-12-19 18:24:38 -07003010static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003011 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003012{
Jens Axboead8a48a2019-11-15 08:49:11 -07003013 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003014 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003015
Jens Axboead8a48a2019-11-15 08:49:11 -07003016 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003017 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003018 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003019 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003020 if (sqe->off && is_timeout_link)
3021 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003022 flags = READ_ONCE(sqe->timeout_flags);
3023 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003024 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003025
Jens Axboe26a61672019-12-20 09:02:01 -07003026 req->timeout.count = READ_ONCE(sqe->off);
3027
Jens Axboe3529d8c2019-12-19 18:24:38 -07003028 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003029 return -ENOMEM;
3030
3031 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003032 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003033 req->flags |= REQ_F_TIMEOUT;
3034
3035 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003036 return -EFAULT;
3037
Jens Axboe11365042019-10-16 09:08:32 -06003038 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003039 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003040 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003041 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003042
Jens Axboead8a48a2019-11-15 08:49:11 -07003043 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3044 return 0;
3045}
3046
Jens Axboefc4df992019-12-10 14:38:45 -07003047static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003048{
3049 unsigned count;
3050 struct io_ring_ctx *ctx = req->ctx;
3051 struct io_timeout_data *data;
3052 struct list_head *entry;
3053 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003054
Jens Axboe2d283902019-12-04 11:08:05 -07003055 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003056
Jens Axboe5262f562019-09-17 12:26:57 -06003057 /*
3058 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003059 * timeout event to be satisfied. If it isn't set, then this is
3060 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003061 */
Jens Axboe26a61672019-12-20 09:02:01 -07003062 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003063 if (!count) {
3064 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3065 spin_lock_irq(&ctx->completion_lock);
3066 entry = ctx->timeout_list.prev;
3067 goto add;
3068 }
Jens Axboe5262f562019-09-17 12:26:57 -06003069
3070 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003071 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003072
3073 /*
3074 * Insertion sort, ensuring the first entry in the list is always
3075 * the one we need first.
3076 */
Jens Axboe5262f562019-09-17 12:26:57 -06003077 spin_lock_irq(&ctx->completion_lock);
3078 list_for_each_prev(entry, &ctx->timeout_list) {
3079 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003080 unsigned nxt_sq_head;
3081 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003082 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003083
Jens Axboe93bd25b2019-11-11 23:34:31 -07003084 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3085 continue;
3086
yangerkun5da0fb12019-10-15 21:59:29 +08003087 /*
3088 * Since cached_sq_head + count - 1 can overflow, use type long
3089 * long to store it.
3090 */
3091 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003092 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3093 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003094
3095 /*
3096 * cached_sq_head may overflow, and it will never overflow twice
3097 * once there is some timeout req still be valid.
3098 */
3099 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003100 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003101
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003102 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003103 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003104
3105 /*
3106 * Sequence of reqs after the insert one and itself should
3107 * be adjusted because each timeout req consumes a slot.
3108 */
3109 span++;
3110 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003111 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003112 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003113add:
Jens Axboe5262f562019-09-17 12:26:57 -06003114 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003115 data->timer.function = io_timeout_fn;
3116 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003117 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003118 return 0;
3119}
3120
Jens Axboe62755e32019-10-28 21:49:21 -06003121static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003122{
Jens Axboe62755e32019-10-28 21:49:21 -06003123 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003124
Jens Axboe62755e32019-10-28 21:49:21 -06003125 return req->user_data == (unsigned long) data;
3126}
3127
Jens Axboee977d6d2019-11-05 12:39:45 -07003128static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003129{
Jens Axboe62755e32019-10-28 21:49:21 -06003130 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003131 int ret = 0;
3132
Jens Axboe62755e32019-10-28 21:49:21 -06003133 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3134 switch (cancel_ret) {
3135 case IO_WQ_CANCEL_OK:
3136 ret = 0;
3137 break;
3138 case IO_WQ_CANCEL_RUNNING:
3139 ret = -EALREADY;
3140 break;
3141 case IO_WQ_CANCEL_NOTFOUND:
3142 ret = -ENOENT;
3143 break;
3144 }
3145
Jens Axboee977d6d2019-11-05 12:39:45 -07003146 return ret;
3147}
3148
Jens Axboe47f46762019-11-09 17:43:02 -07003149static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3150 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003151 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003152{
3153 unsigned long flags;
3154 int ret;
3155
3156 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3157 if (ret != -ENOENT) {
3158 spin_lock_irqsave(&ctx->completion_lock, flags);
3159 goto done;
3160 }
3161
3162 spin_lock_irqsave(&ctx->completion_lock, flags);
3163 ret = io_timeout_cancel(ctx, sqe_addr);
3164 if (ret != -ENOENT)
3165 goto done;
3166 ret = io_poll_cancel(ctx, sqe_addr);
3167done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003168 if (!ret)
3169 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003170 io_cqring_fill_event(req, ret);
3171 io_commit_cqring(ctx);
3172 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3173 io_cqring_ev_posted(ctx);
3174
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003175 if (ret < 0)
3176 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003177 io_put_req_find_next(req, nxt);
3178}
3179
Jens Axboe3529d8c2019-12-19 18:24:38 -07003180static int io_async_cancel_prep(struct io_kiocb *req,
3181 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003182{
Jens Axboefbf23842019-12-17 18:45:56 -07003183 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003184 return -EINVAL;
3185 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3186 sqe->cancel_flags)
3187 return -EINVAL;
3188
Jens Axboefbf23842019-12-17 18:45:56 -07003189 req->cancel.addr = READ_ONCE(sqe->addr);
3190 return 0;
3191}
3192
3193static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3194{
3195 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003196
3197 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003198 return 0;
3199}
3200
Jens Axboe3529d8c2019-12-19 18:24:38 -07003201static int io_req_defer_prep(struct io_kiocb *req,
3202 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003203{
Jens Axboee7815732019-12-17 19:45:06 -07003204 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003205
Jens Axboed625c6e2019-12-17 19:53:05 -07003206 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003207 case IORING_OP_NOP:
3208 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003209 case IORING_OP_READV:
3210 case IORING_OP_READ_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003211 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003212 break;
3213 case IORING_OP_WRITEV:
3214 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003215 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003216 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003217 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003218 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003219 break;
3220 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003221 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003222 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003223 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003224 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003225 break;
3226 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003227 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003228 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003229 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003230 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003231 break;
3232 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003233 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003234 break;
Jens Axboef499a022019-12-02 16:28:46 -07003235 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003236 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003237 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003238 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003239 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003240 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003241 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003242 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003243 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003244 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003245 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003246 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003247 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003248 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003249 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003250 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003251 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003252 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003253 case IORING_OP_FALLOCATE:
3254 ret = io_fallocate_prep(req, sqe);
3255 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003256 case IORING_OP_OPENAT:
3257 ret = io_openat_prep(req, sqe);
3258 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003259 default:
Jens Axboee7815732019-12-17 19:45:06 -07003260 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3261 req->opcode);
3262 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003263 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003264 }
3265
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003266 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003267}
3268
Jens Axboe3529d8c2019-12-19 18:24:38 -07003269static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003270{
Jackie Liua197f662019-11-08 08:09:12 -07003271 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003272 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003273
Bob Liu9d858b22019-11-13 18:06:25 +08003274 /* Still need defer if there is pending req in defer list. */
3275 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003276 return 0;
3277
Jens Axboe3529d8c2019-12-19 18:24:38 -07003278 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003279 return -EAGAIN;
3280
Jens Axboe3529d8c2019-12-19 18:24:38 -07003281 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003282 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003283 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003284
Jens Axboede0617e2019-04-06 21:51:27 -06003285 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003286 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003287 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003288 return 0;
3289 }
3290
Jens Axboe915967f2019-11-21 09:01:20 -07003291 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003292 list_add_tail(&req->list, &ctx->defer_list);
3293 spin_unlock_irq(&ctx->completion_lock);
3294 return -EIOCBQUEUED;
3295}
3296
Jens Axboe3529d8c2019-12-19 18:24:38 -07003297static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3298 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003299{
Jackie Liua197f662019-11-08 08:09:12 -07003300 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07003301 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003302
Jens Axboed625c6e2019-12-17 19:53:05 -07003303 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003304 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003305 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003306 break;
3307 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003308 case IORING_OP_READ_FIXED:
3309 if (sqe) {
3310 ret = io_read_prep(req, sqe, force_nonblock);
3311 if (ret < 0)
3312 break;
3313 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003314 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003315 break;
3316 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07003317 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003318 if (sqe) {
3319 ret = io_write_prep(req, sqe, force_nonblock);
3320 if (ret < 0)
3321 break;
3322 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003323 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003324 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003325 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003326 if (sqe) {
3327 ret = io_prep_fsync(req, sqe);
3328 if (ret < 0)
3329 break;
3330 }
Jens Axboefc4df992019-12-10 14:38:45 -07003331 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003332 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003333 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003334 if (sqe) {
3335 ret = io_poll_add_prep(req, sqe);
3336 if (ret)
3337 break;
3338 }
Jens Axboefc4df992019-12-10 14:38:45 -07003339 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003340 break;
3341 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003342 if (sqe) {
3343 ret = io_poll_remove_prep(req, sqe);
3344 if (ret < 0)
3345 break;
3346 }
Jens Axboefc4df992019-12-10 14:38:45 -07003347 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003348 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003349 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003350 if (sqe) {
3351 ret = io_prep_sfr(req, sqe);
3352 if (ret < 0)
3353 break;
3354 }
Jens Axboefc4df992019-12-10 14:38:45 -07003355 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003356 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003357 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003358 if (sqe) {
3359 ret = io_sendmsg_prep(req, sqe);
3360 if (ret < 0)
3361 break;
3362 }
Jens Axboefc4df992019-12-10 14:38:45 -07003363 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003364 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003365 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003366 if (sqe) {
3367 ret = io_recvmsg_prep(req, sqe);
3368 if (ret)
3369 break;
3370 }
Jens Axboefc4df992019-12-10 14:38:45 -07003371 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003372 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003373 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003374 if (sqe) {
3375 ret = io_timeout_prep(req, sqe, false);
3376 if (ret)
3377 break;
3378 }
Jens Axboefc4df992019-12-10 14:38:45 -07003379 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003380 break;
Jens Axboe11365042019-10-16 09:08:32 -06003381 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003382 if (sqe) {
3383 ret = io_timeout_remove_prep(req, sqe);
3384 if (ret)
3385 break;
3386 }
Jens Axboefc4df992019-12-10 14:38:45 -07003387 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003388 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003389 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003390 if (sqe) {
3391 ret = io_accept_prep(req, sqe);
3392 if (ret)
3393 break;
3394 }
Jens Axboefc4df992019-12-10 14:38:45 -07003395 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003396 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003397 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003398 if (sqe) {
3399 ret = io_connect_prep(req, sqe);
3400 if (ret)
3401 break;
3402 }
Jens Axboefc4df992019-12-10 14:38:45 -07003403 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003404 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003405 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003406 if (sqe) {
3407 ret = io_async_cancel_prep(req, sqe);
3408 if (ret)
3409 break;
3410 }
Jens Axboefc4df992019-12-10 14:38:45 -07003411 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003412 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003413 case IORING_OP_FALLOCATE:
3414 if (sqe) {
3415 ret = io_fallocate_prep(req, sqe);
3416 if (ret)
3417 break;
3418 }
3419 ret = io_fallocate(req, nxt, force_nonblock);
3420 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003421 case IORING_OP_OPENAT:
3422 if (sqe) {
3423 ret = io_openat_prep(req, sqe);
3424 if (ret)
3425 break;
3426 }
3427 ret = io_openat(req, nxt, force_nonblock);
3428 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003429 default:
3430 ret = -EINVAL;
3431 break;
3432 }
3433
Jens Axboedef596e2019-01-09 08:59:42 -07003434 if (ret)
3435 return ret;
3436
3437 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07003438 const bool in_async = io_wq_current_is_worker();
3439
Jens Axboe9e645e112019-05-10 16:07:28 -06003440 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07003441 return -EAGAIN;
3442
Jens Axboe11ba8202020-01-15 21:51:17 -07003443 /* workqueue context doesn't hold uring_lock, grab it now */
3444 if (in_async)
3445 mutex_lock(&ctx->uring_lock);
3446
Jens Axboedef596e2019-01-09 08:59:42 -07003447 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07003448
3449 if (in_async)
3450 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07003451 }
3452
3453 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003454}
3455
Jens Axboe561fb042019-10-24 07:25:42 -06003456static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003457{
Jens Axboe561fb042019-10-24 07:25:42 -06003458 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003459 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003460 struct io_kiocb *nxt = NULL;
3461 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003462
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003463 /* if NO_CANCEL is set, we must still run the work */
3464 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
3465 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003466 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003467 }
Jens Axboe31b51512019-01-18 22:56:34 -07003468
Jens Axboe561fb042019-10-24 07:25:42 -06003469 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003470 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3471 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003472 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003473 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003474 /*
3475 * We can get EAGAIN for polled IO even though we're
3476 * forcing a sync submission from here, since we can't
3477 * wait for request slots on the block side.
3478 */
3479 if (ret != -EAGAIN)
3480 break;
3481 cond_resched();
3482 } while (1);
3483 }
Jens Axboe31b51512019-01-18 22:56:34 -07003484
Jens Axboe561fb042019-10-24 07:25:42 -06003485 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003486 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003487
Jens Axboe561fb042019-10-24 07:25:42 -06003488 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003489 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003490 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003491 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003492 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003493
Jens Axboe561fb042019-10-24 07:25:42 -06003494 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07003495 if (!ret && nxt)
3496 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07003497}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003498
Jens Axboe9e3aa612019-12-11 15:55:43 -07003499static bool io_req_op_valid(int op)
3500{
3501 return op >= IORING_OP_NOP && op < IORING_OP_LAST;
3502}
3503
Jens Axboe15b71ab2019-12-11 11:20:36 -07003504static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06003505{
Jens Axboed625c6e2019-12-17 19:53:05 -07003506 switch (req->opcode) {
Jens Axboe09bb8392019-03-13 12:39:28 -06003507 case IORING_OP_NOP:
3508 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03003509 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03003510 case IORING_OP_TIMEOUT_REMOVE:
3511 case IORING_OP_ASYNC_CANCEL:
3512 case IORING_OP_LINK_TIMEOUT:
Jens Axboe9e3aa612019-12-11 15:55:43 -07003513 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003514 case IORING_OP_OPENAT:
3515 return fd != -1;
Jens Axboe09bb8392019-03-13 12:39:28 -06003516 default:
Jens Axboed625c6e2019-12-17 19:53:05 -07003517 if (io_req_op_valid(req->opcode))
Jens Axboe9e3aa612019-12-11 15:55:43 -07003518 return 1;
3519 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003520 }
3521}
3522
Jens Axboe65e19f52019-10-26 07:20:21 -06003523static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3524 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003525{
Jens Axboe65e19f52019-10-26 07:20:21 -06003526 struct fixed_file_table *table;
3527
3528 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
3529 return table->files[index & IORING_FILE_TABLE_MASK];
3530}
3531
Jens Axboe3529d8c2019-12-19 18:24:38 -07003532static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
3533 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06003534{
Jackie Liua197f662019-11-08 08:09:12 -07003535 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003536 unsigned flags;
Jens Axboe9e3aa612019-12-11 15:55:43 -07003537 int fd, ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003538
Jens Axboe3529d8c2019-12-19 18:24:38 -07003539 flags = READ_ONCE(sqe->flags);
3540 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003541
Jackie Liu4fe2c962019-09-09 20:50:40 +08003542 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003543 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003544
Jens Axboe15b71ab2019-12-11 11:20:36 -07003545 ret = io_req_needs_file(req, fd);
Jens Axboe9e3aa612019-12-11 15:55:43 -07003546 if (ret <= 0)
3547 return ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003548
3549 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003550 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003551 (unsigned) fd >= ctx->nr_user_files))
3552 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003553 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003554 req->file = io_file_from_index(ctx, fd);
3555 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003556 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003557 req->flags |= REQ_F_FIXED_FILE;
3558 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003559 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003560 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003561 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003562 req->file = io_file_get(state, fd);
3563 if (unlikely(!req->file))
3564 return -EBADF;
3565 }
3566
3567 return 0;
3568}
3569
Jackie Liua197f662019-11-08 08:09:12 -07003570static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003571{
Jens Axboefcb323c2019-10-24 12:39:47 -06003572 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003573 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003574
3575 rcu_read_lock();
3576 spin_lock_irq(&ctx->inflight_lock);
3577 /*
3578 * We use the f_ops->flush() handler to ensure that we can flush
3579 * out work accessing these files if the fd is closed. Check if
3580 * the fd has changed since we started down this path, and disallow
3581 * this operation if it has.
3582 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003583 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003584 list_add(&req->inflight_entry, &ctx->inflight_list);
3585 req->flags |= REQ_F_INFLIGHT;
3586 req->work.files = current->files;
3587 ret = 0;
3588 }
3589 spin_unlock_irq(&ctx->inflight_lock);
3590 rcu_read_unlock();
3591
3592 return ret;
3593}
3594
Jens Axboe2665abf2019-11-05 12:40:47 -07003595static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3596{
Jens Axboead8a48a2019-11-15 08:49:11 -07003597 struct io_timeout_data *data = container_of(timer,
3598 struct io_timeout_data, timer);
3599 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003600 struct io_ring_ctx *ctx = req->ctx;
3601 struct io_kiocb *prev = NULL;
3602 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003603
3604 spin_lock_irqsave(&ctx->completion_lock, flags);
3605
3606 /*
3607 * We don't expect the list to be empty, that will only happen if we
3608 * race with the completion of the linked work.
3609 */
Pavel Begunkov44932332019-12-05 16:16:35 +03003610 if (!list_empty(&req->link_list)) {
3611 prev = list_entry(req->link_list.prev, struct io_kiocb,
3612 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003613 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03003614 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003615 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3616 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003617 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003618 }
3619
3620 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3621
3622 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003623 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003624 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3625 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003626 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003627 } else {
3628 io_cqring_add_event(req, -ETIME);
3629 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003630 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003631 return HRTIMER_NORESTART;
3632}
3633
Jens Axboead8a48a2019-11-15 08:49:11 -07003634static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003635{
Jens Axboe76a46e02019-11-10 23:34:16 -07003636 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07003637
Jens Axboe76a46e02019-11-10 23:34:16 -07003638 /*
3639 * If the list is now empty, then our linked request finished before
3640 * we got a chance to setup the timer
3641 */
3642 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03003643 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07003644 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003645
Jens Axboead8a48a2019-11-15 08:49:11 -07003646 data->timer.function = io_link_timeout_fn;
3647 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3648 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07003649 }
Jens Axboe76a46e02019-11-10 23:34:16 -07003650 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07003651
Jens Axboe2665abf2019-11-05 12:40:47 -07003652 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07003653 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003654}
3655
Jens Axboead8a48a2019-11-15 08:49:11 -07003656static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003657{
3658 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003659
Jens Axboe2665abf2019-11-05 12:40:47 -07003660 if (!(req->flags & REQ_F_LINK))
3661 return NULL;
3662
Pavel Begunkov44932332019-12-05 16:16:35 +03003663 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
3664 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07003665 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07003666 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003667
Jens Axboe76a46e02019-11-10 23:34:16 -07003668 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07003669 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07003670}
3671
Jens Axboe3529d8c2019-12-19 18:24:38 -07003672static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003673{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003674 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003675 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003676 int ret;
3677
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003678again:
3679 linked_timeout = io_prep_linked_timeout(req);
3680
Jens Axboe3529d8c2019-12-19 18:24:38 -07003681 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06003682
3683 /*
3684 * We async punt it if the file wasn't marked NOWAIT, or if the file
3685 * doesn't support non-blocking read/write attempts
3686 */
3687 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3688 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003689 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3690 ret = io_grab_files(req);
3691 if (ret)
3692 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003693 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003694
3695 /*
3696 * Queued up for async execution, worker will release
3697 * submit reference when the iocb is actually submitted.
3698 */
3699 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003700 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003701 }
Jens Axboee65ef562019-03-12 10:16:44 -06003702
Jens Axboefcb323c2019-10-24 12:39:47 -06003703err:
Jens Axboee65ef562019-03-12 10:16:44 -06003704 /* drop submission reference */
3705 io_put_req(req);
3706
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003707 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003708 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003709 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003710 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003711 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003712 }
3713
Jens Axboee65ef562019-03-12 10:16:44 -06003714 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003715 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003716 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003717 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003718 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003719 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003720done_req:
3721 if (nxt) {
3722 req = nxt;
3723 nxt = NULL;
3724 goto again;
3725 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003726}
3727
Jens Axboe3529d8c2019-12-19 18:24:38 -07003728static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003729{
3730 int ret;
3731
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003732 if (unlikely(req->ctx->drain_next)) {
3733 req->flags |= REQ_F_IO_DRAIN;
3734 req->ctx->drain_next = false;
3735 }
3736 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3737
Jens Axboe3529d8c2019-12-19 18:24:38 -07003738 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003739 if (ret) {
3740 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003741 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003742 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003743 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003744 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003745 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07003746 __io_queue_sqe(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003747}
3748
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003749static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003750{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003751 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003752 io_cqring_add_event(req, -ECANCELED);
3753 io_double_put_req(req);
3754 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07003755 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003756}
3757
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003758#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
3759 IOSQE_IO_HARDLINK)
Jens Axboe9e645e112019-05-10 16:07:28 -06003760
Jens Axboe3529d8c2019-12-19 18:24:38 -07003761static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3762 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003763{
Jackie Liua197f662019-11-08 08:09:12 -07003764 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003765 int ret;
3766
3767 /* enforce forwards compatibility on users */
Jens Axboe3529d8c2019-12-19 18:24:38 -07003768 if (unlikely(sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003769 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003770 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003771 }
3772
Jens Axboe3529d8c2019-12-19 18:24:38 -07003773 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06003774 if (unlikely(ret)) {
3775err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003776 io_cqring_add_event(req, ret);
3777 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003778 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06003779 }
3780
Jens Axboe9e645e112019-05-10 16:07:28 -06003781 /*
3782 * If we already have a head request, queue this one for async
3783 * submittal once the head completes. If we don't have a head but
3784 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3785 * submitted sync once the chain is complete. If none of those
3786 * conditions are true (normal request), then just queue it.
3787 */
3788 if (*link) {
3789 struct io_kiocb *prev = *link;
3790
Jens Axboe3529d8c2019-12-19 18:24:38 -07003791 if (sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003792 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3793
Jens Axboe3529d8c2019-12-19 18:24:38 -07003794 if (sqe->flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003795 req->flags |= REQ_F_HARDLINK;
3796
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003797 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003798 ret = -EAGAIN;
3799 goto err_req;
3800 }
3801
Jens Axboe3529d8c2019-12-19 18:24:38 -07003802 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07003803 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003804 /* fail even hard links since we don't submit */
Jens Axboe2d283902019-12-04 11:08:05 -07003805 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07003806 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07003807 }
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003808 trace_io_uring_link(ctx, req, prev);
Pavel Begunkov44932332019-12-05 16:16:35 +03003809 list_add_tail(&req->link_list, &prev->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003810 } else if (sqe->flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003811 req->flags |= REQ_F_LINK;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003812 if (sqe->flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003813 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06003814
Jens Axboe9e645e112019-05-10 16:07:28 -06003815 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003816 ret = io_req_defer_prep(req, sqe);
3817 if (ret)
3818 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06003819 *link = req;
3820 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003821 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06003822 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003823
3824 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06003825}
3826
Jens Axboe9a56a232019-01-09 09:06:50 -07003827/*
3828 * Batched submission is done, ensure local IO is flushed out.
3829 */
3830static void io_submit_state_end(struct io_submit_state *state)
3831{
3832 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003833 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003834 if (state->free_reqs)
3835 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3836 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003837}
3838
3839/*
3840 * Start submission side cache.
3841 */
3842static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08003843 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07003844{
3845 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003846 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003847 state->file = NULL;
3848 state->ios_left = max_ios;
3849}
3850
Jens Axboe2b188cc2019-01-07 10:46:33 -07003851static void io_commit_sqring(struct io_ring_ctx *ctx)
3852{
Hristo Venev75b28af2019-08-26 17:23:46 +00003853 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003854
Hristo Venev75b28af2019-08-26 17:23:46 +00003855 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003856 /*
3857 * Ensure any loads from the SQEs are done at this point,
3858 * since once we write the new head, the application could
3859 * write new data to them.
3860 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003861 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003862 }
3863}
3864
3865/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07003866 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07003867 * that is mapped by userspace. This means that care needs to be taken to
3868 * ensure that reads are stable, as we cannot rely on userspace always
3869 * being a good citizen. If members of the sqe are validated and then later
3870 * used, it's important that those reads are done through READ_ONCE() to
3871 * prevent a re-load down the line.
3872 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07003873static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
3874 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003875{
Hristo Venev75b28af2019-08-26 17:23:46 +00003876 struct io_rings *rings = ctx->rings;
3877 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003878 unsigned head;
3879
3880 /*
3881 * The cached sq head (or cq tail) serves two purposes:
3882 *
3883 * 1) allows us to batch the cost of updating the user visible
3884 * head updates.
3885 * 2) allows the kernel side to track the head on its own, even
3886 * though the application is the one updating it.
3887 */
3888 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003889 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003890 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003891 return false;
3892
Hristo Venev75b28af2019-08-26 17:23:46 +00003893 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003894 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003895 /*
3896 * All io need record the previous position, if LINK vs DARIN,
3897 * it can be used to mark the position of the first IO in the
3898 * link list.
3899 */
3900 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003901 *sqe_ptr = &ctx->sq_sqes[head];
3902 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
3903 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003904 ctx->cached_sq_head++;
3905 return true;
3906 }
3907
3908 /* drop invalid entries */
3909 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003910 ctx->cached_sq_dropped++;
3911 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003912 return false;
3913}
3914
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003915static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003916 struct file *ring_file, int ring_fd,
3917 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003918{
3919 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003920 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003921 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003922 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003923
Jens Axboec4a2ed72019-11-21 21:01:26 -07003924 /* if we have a backlog and couldn't flush it all, return BUSY */
3925 if (!list_empty(&ctx->cq_overflow_list) &&
3926 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003927 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003928
3929 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08003930 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003931 statep = &state;
3932 }
3933
3934 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003935 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03003936 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003937 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003938
Pavel Begunkov196be952019-11-07 01:41:06 +03003939 req = io_get_req(ctx, statep);
3940 if (unlikely(!req)) {
3941 if (!submitted)
3942 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003943 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003944 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07003945 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003946 __io_free_req(req);
3947 break;
3948 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003949
Jens Axboed625c6e2019-12-17 19:53:05 -07003950 if (io_req_needs_user(req) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003951 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3952 if (!mm_fault) {
3953 use_mm(ctx->sqo_mm);
3954 *mm = ctx->sqo_mm;
3955 }
3956 }
3957
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003958 submitted++;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003959 sqe_flags = sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003960
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003961 req->ring_file = ring_file;
3962 req->ring_fd = ring_fd;
3963 req->has_user = *mm != NULL;
3964 req->in_async = async;
3965 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07003966 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003967 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003968 break;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003969 /*
3970 * If previous wasn't linked and we have a linked command,
3971 * that's the end of the chain. Submit the previous link.
3972 */
Pavel Begunkovffbb8d62019-12-17 20:57:05 +03003973 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003974 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003975 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003976 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003977 }
3978
Jens Axboe9e645e112019-05-10 16:07:28 -06003979 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003980 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003981 if (statep)
3982 io_submit_state_end(&state);
3983
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003984 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3985 io_commit_sqring(ctx);
3986
Jens Axboe6c271ce2019-01-10 11:22:30 -07003987 return submitted;
3988}
3989
3990static int io_sq_thread(void *data)
3991{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003992 struct io_ring_ctx *ctx = data;
3993 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003994 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003995 mm_segment_t old_fs;
3996 DEFINE_WAIT(wait);
3997 unsigned inflight;
3998 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003999 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004000
Jens Axboe206aefd2019-11-07 18:27:42 -07004001 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004002
Jens Axboe6c271ce2019-01-10 11:22:30 -07004003 old_fs = get_fs();
4004 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004005 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004006
Jens Axboec1edbf52019-11-10 16:56:04 -07004007 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004008 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004009 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004010
4011 if (inflight) {
4012 unsigned nr_events = 0;
4013
4014 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004015 /*
4016 * inflight is the count of the maximum possible
4017 * entries we submitted, but it can be smaller
4018 * if we dropped some of them. If we don't have
4019 * poll entries available, then we know that we
4020 * have nothing left to poll for. Reset the
4021 * inflight count to zero in that case.
4022 */
4023 mutex_lock(&ctx->uring_lock);
4024 if (!list_empty(&ctx->poll_list))
4025 __io_iopoll_check(ctx, &nr_events, 0);
4026 else
4027 inflight = 0;
4028 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004029 } else {
4030 /*
4031 * Normal IO, just pretend everything completed.
4032 * We don't have to poll completions for that.
4033 */
4034 nr_events = inflight;
4035 }
4036
4037 inflight -= nr_events;
4038 if (!inflight)
4039 timeout = jiffies + ctx->sq_thread_idle;
4040 }
4041
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004042 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004043
4044 /*
4045 * If submit got -EBUSY, flag us as needing the application
4046 * to enter the kernel to reap and flush events.
4047 */
4048 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004049 /*
4050 * We're polling. If we're within the defined idle
4051 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004052 * to sleep. The exception is if we got EBUSY doing
4053 * more IO, we should wait for the application to
4054 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004055 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004056 if (inflight ||
4057 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004058 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004059 continue;
4060 }
4061
4062 /*
4063 * Drop cur_mm before scheduling, we can't hold it for
4064 * long periods (or over schedule()). Do this before
4065 * adding ourselves to the waitqueue, as the unuse/drop
4066 * may sleep.
4067 */
4068 if (cur_mm) {
4069 unuse_mm(cur_mm);
4070 mmput(cur_mm);
4071 cur_mm = NULL;
4072 }
4073
4074 prepare_to_wait(&ctx->sqo_wait, &wait,
4075 TASK_INTERRUPTIBLE);
4076
4077 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004078 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004079 /* make sure to read SQ tail after writing flags */
4080 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004081
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004082 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004083 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004084 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004085 finish_wait(&ctx->sqo_wait, &wait);
4086 break;
4087 }
4088 if (signal_pending(current))
4089 flush_signals(current);
4090 schedule();
4091 finish_wait(&ctx->sqo_wait, &wait);
4092
Hristo Venev75b28af2019-08-26 17:23:46 +00004093 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004094 continue;
4095 }
4096 finish_wait(&ctx->sqo_wait, &wait);
4097
Hristo Venev75b28af2019-08-26 17:23:46 +00004098 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004099 }
4100
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004101 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004102 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004103 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004104 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004105 if (ret > 0)
4106 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004107 }
4108
4109 set_fs(old_fs);
4110 if (cur_mm) {
4111 unuse_mm(cur_mm);
4112 mmput(cur_mm);
4113 }
Jens Axboe181e4482019-11-25 08:52:30 -07004114 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004115
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004116 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004117
Jens Axboe6c271ce2019-01-10 11:22:30 -07004118 return 0;
4119}
4120
Jens Axboebda52162019-09-24 13:47:15 -06004121struct io_wait_queue {
4122 struct wait_queue_entry wq;
4123 struct io_ring_ctx *ctx;
4124 unsigned to_wait;
4125 unsigned nr_timeouts;
4126};
4127
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004128static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004129{
4130 struct io_ring_ctx *ctx = iowq->ctx;
4131
4132 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004133 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004134 * started waiting. For timeouts, we always want to return to userspace,
4135 * regardless of event count.
4136 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004137 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004138 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4139}
4140
4141static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4142 int wake_flags, void *key)
4143{
4144 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4145 wq);
4146
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004147 /* use noflush == true, as we can't safely rely on locking context */
4148 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004149 return -1;
4150
4151 return autoremove_wake_function(curr, mode, wake_flags, key);
4152}
4153
Jens Axboe2b188cc2019-01-07 10:46:33 -07004154/*
4155 * Wait until events become available, if we don't already have some. The
4156 * application must reap them itself, as they reside on the shared cq ring.
4157 */
4158static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4159 const sigset_t __user *sig, size_t sigsz)
4160{
Jens Axboebda52162019-09-24 13:47:15 -06004161 struct io_wait_queue iowq = {
4162 .wq = {
4163 .private = current,
4164 .func = io_wake_function,
4165 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4166 },
4167 .ctx = ctx,
4168 .to_wait = min_events,
4169 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004170 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004171 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004172
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004173 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004174 return 0;
4175
4176 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004177#ifdef CONFIG_COMPAT
4178 if (in_compat_syscall())
4179 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004180 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004181 else
4182#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004183 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004184
Jens Axboe2b188cc2019-01-07 10:46:33 -07004185 if (ret)
4186 return ret;
4187 }
4188
Jens Axboebda52162019-09-24 13:47:15 -06004189 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004190 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004191 do {
4192 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4193 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004194 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004195 break;
4196 schedule();
4197 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004198 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004199 break;
4200 }
4201 } while (1);
4202 finish_wait(&ctx->wait, &iowq.wq);
4203
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004204 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004205
Hristo Venev75b28af2019-08-26 17:23:46 +00004206 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004207}
4208
Jens Axboe6b063142019-01-10 22:13:58 -07004209static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4210{
4211#if defined(CONFIG_UNIX)
4212 if (ctx->ring_sock) {
4213 struct sock *sock = ctx->ring_sock->sk;
4214 struct sk_buff *skb;
4215
4216 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4217 kfree_skb(skb);
4218 }
4219#else
4220 int i;
4221
Jens Axboe65e19f52019-10-26 07:20:21 -06004222 for (i = 0; i < ctx->nr_user_files; i++) {
4223 struct file *file;
4224
4225 file = io_file_from_index(ctx, i);
4226 if (file)
4227 fput(file);
4228 }
Jens Axboe6b063142019-01-10 22:13:58 -07004229#endif
4230}
4231
4232static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4233{
Jens Axboe65e19f52019-10-26 07:20:21 -06004234 unsigned nr_tables, i;
4235
4236 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004237 return -ENXIO;
4238
4239 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004240 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4241 for (i = 0; i < nr_tables; i++)
4242 kfree(ctx->file_table[i].files);
4243 kfree(ctx->file_table);
4244 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004245 ctx->nr_user_files = 0;
4246 return 0;
4247}
4248
Jens Axboe6c271ce2019-01-10 11:22:30 -07004249static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4250{
4251 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004252 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004253 /*
4254 * The park is a bit of a work-around, without it we get
4255 * warning spews on shutdown with SQPOLL set and affinity
4256 * set to a single CPU.
4257 */
Jens Axboe06058632019-04-13 09:26:03 -06004258 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004259 kthread_stop(ctx->sqo_thread);
4260 ctx->sqo_thread = NULL;
4261 }
4262}
4263
Jens Axboe6b063142019-01-10 22:13:58 -07004264static void io_finish_async(struct io_ring_ctx *ctx)
4265{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004266 io_sq_thread_stop(ctx);
4267
Jens Axboe561fb042019-10-24 07:25:42 -06004268 if (ctx->io_wq) {
4269 io_wq_destroy(ctx->io_wq);
4270 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004271 }
4272}
4273
4274#if defined(CONFIG_UNIX)
4275static void io_destruct_skb(struct sk_buff *skb)
4276{
4277 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
4278
Jens Axboe561fb042019-10-24 07:25:42 -06004279 if (ctx->io_wq)
4280 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06004281
Jens Axboe6b063142019-01-10 22:13:58 -07004282 unix_destruct_scm(skb);
4283}
4284
4285/*
4286 * Ensure the UNIX gc is aware of our file set, so we are certain that
4287 * the io_uring can be safely unregistered on process exit, even if we have
4288 * loops in the file referencing.
4289 */
4290static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4291{
4292 struct sock *sk = ctx->ring_sock->sk;
4293 struct scm_fp_list *fpl;
4294 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004295 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004296
4297 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4298 unsigned long inflight = ctx->user->unix_inflight + nr;
4299
4300 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4301 return -EMFILE;
4302 }
4303
4304 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4305 if (!fpl)
4306 return -ENOMEM;
4307
4308 skb = alloc_skb(0, GFP_KERNEL);
4309 if (!skb) {
4310 kfree(fpl);
4311 return -ENOMEM;
4312 }
4313
4314 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004315
Jens Axboe08a45172019-10-03 08:11:03 -06004316 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004317 fpl->user = get_uid(ctx->user);
4318 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004319 struct file *file = io_file_from_index(ctx, i + offset);
4320
4321 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004322 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004323 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004324 unix_inflight(fpl->user, fpl->fp[nr_files]);
4325 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004326 }
4327
Jens Axboe08a45172019-10-03 08:11:03 -06004328 if (nr_files) {
4329 fpl->max = SCM_MAX_FD;
4330 fpl->count = nr_files;
4331 UNIXCB(skb).fp = fpl;
4332 skb->destructor = io_destruct_skb;
4333 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4334 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004335
Jens Axboe08a45172019-10-03 08:11:03 -06004336 for (i = 0; i < nr_files; i++)
4337 fput(fpl->fp[i]);
4338 } else {
4339 kfree_skb(skb);
4340 kfree(fpl);
4341 }
Jens Axboe6b063142019-01-10 22:13:58 -07004342
4343 return 0;
4344}
4345
4346/*
4347 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4348 * causes regular reference counting to break down. We rely on the UNIX
4349 * garbage collection to take care of this problem for us.
4350 */
4351static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4352{
4353 unsigned left, total;
4354 int ret = 0;
4355
4356 total = 0;
4357 left = ctx->nr_user_files;
4358 while (left) {
4359 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004360
4361 ret = __io_sqe_files_scm(ctx, this_files, total);
4362 if (ret)
4363 break;
4364 left -= this_files;
4365 total += this_files;
4366 }
4367
4368 if (!ret)
4369 return 0;
4370
4371 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004372 struct file *file = io_file_from_index(ctx, total);
4373
4374 if (file)
4375 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004376 total++;
4377 }
4378
4379 return ret;
4380}
4381#else
4382static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4383{
4384 return 0;
4385}
4386#endif
4387
Jens Axboe65e19f52019-10-26 07:20:21 -06004388static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4389 unsigned nr_files)
4390{
4391 int i;
4392
4393 for (i = 0; i < nr_tables; i++) {
4394 struct fixed_file_table *table = &ctx->file_table[i];
4395 unsigned this_files;
4396
4397 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4398 table->files = kcalloc(this_files, sizeof(struct file *),
4399 GFP_KERNEL);
4400 if (!table->files)
4401 break;
4402 nr_files -= this_files;
4403 }
4404
4405 if (i == nr_tables)
4406 return 0;
4407
4408 for (i = 0; i < nr_tables; i++) {
4409 struct fixed_file_table *table = &ctx->file_table[i];
4410 kfree(table->files);
4411 }
4412 return 1;
4413}
4414
Jens Axboe6b063142019-01-10 22:13:58 -07004415static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
4416 unsigned nr_args)
4417{
4418 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06004419 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07004420 int fd, ret = 0;
4421 unsigned i;
4422
Jens Axboe65e19f52019-10-26 07:20:21 -06004423 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004424 return -EBUSY;
4425 if (!nr_args)
4426 return -EINVAL;
4427 if (nr_args > IORING_MAX_FIXED_FILES)
4428 return -EMFILE;
4429
Jens Axboe65e19f52019-10-26 07:20:21 -06004430 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
4431 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
4432 GFP_KERNEL);
4433 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004434 return -ENOMEM;
4435
Jens Axboe65e19f52019-10-26 07:20:21 -06004436 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
4437 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07004438 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06004439 return -ENOMEM;
4440 }
4441
Jens Axboe08a45172019-10-03 08:11:03 -06004442 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004443 struct fixed_file_table *table;
4444 unsigned index;
4445
Jens Axboe6b063142019-01-10 22:13:58 -07004446 ret = -EFAULT;
4447 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
4448 break;
Jens Axboe08a45172019-10-03 08:11:03 -06004449 /* allow sparse sets */
4450 if (fd == -1) {
4451 ret = 0;
4452 continue;
4453 }
Jens Axboe6b063142019-01-10 22:13:58 -07004454
Jens Axboe65e19f52019-10-26 07:20:21 -06004455 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4456 index = i & IORING_FILE_TABLE_MASK;
4457 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07004458
4459 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06004460 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07004461 break;
4462 /*
4463 * Don't allow io_uring instances to be registered. If UNIX
4464 * isn't enabled, then this causes a reference cycle and this
4465 * instance can never get freed. If UNIX is enabled we'll
4466 * handle it just fine, but there's still no point in allowing
4467 * a ring fd as it doesn't support regular read/write anyway.
4468 */
Jens Axboe65e19f52019-10-26 07:20:21 -06004469 if (table->files[index]->f_op == &io_uring_fops) {
4470 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07004471 break;
4472 }
Jens Axboe6b063142019-01-10 22:13:58 -07004473 ret = 0;
4474 }
4475
4476 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004477 for (i = 0; i < ctx->nr_user_files; i++) {
4478 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07004479
Jens Axboe65e19f52019-10-26 07:20:21 -06004480 file = io_file_from_index(ctx, i);
4481 if (file)
4482 fput(file);
4483 }
4484 for (i = 0; i < nr_tables; i++)
4485 kfree(ctx->file_table[i].files);
4486
4487 kfree(ctx->file_table);
4488 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004489 ctx->nr_user_files = 0;
4490 return ret;
4491 }
4492
4493 ret = io_sqe_files_scm(ctx);
4494 if (ret)
4495 io_sqe_files_unregister(ctx);
4496
4497 return ret;
4498}
4499
Jens Axboec3a31e62019-10-03 13:59:56 -06004500static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
4501{
4502#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06004503 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06004504 struct sock *sock = ctx->ring_sock->sk;
4505 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4506 struct sk_buff *skb;
4507 int i;
4508
4509 __skb_queue_head_init(&list);
4510
4511 /*
4512 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4513 * remove this entry and rearrange the file array.
4514 */
4515 skb = skb_dequeue(head);
4516 while (skb) {
4517 struct scm_fp_list *fp;
4518
4519 fp = UNIXCB(skb).fp;
4520 for (i = 0; i < fp->count; i++) {
4521 int left;
4522
4523 if (fp->fp[i] != file)
4524 continue;
4525
4526 unix_notinflight(fp->user, fp->fp[i]);
4527 left = fp->count - 1 - i;
4528 if (left) {
4529 memmove(&fp->fp[i], &fp->fp[i + 1],
4530 left * sizeof(struct file *));
4531 }
4532 fp->count--;
4533 if (!fp->count) {
4534 kfree_skb(skb);
4535 skb = NULL;
4536 } else {
4537 __skb_queue_tail(&list, skb);
4538 }
4539 fput(file);
4540 file = NULL;
4541 break;
4542 }
4543
4544 if (!file)
4545 break;
4546
4547 __skb_queue_tail(&list, skb);
4548
4549 skb = skb_dequeue(head);
4550 }
4551
4552 if (skb_peek(&list)) {
4553 spin_lock_irq(&head->lock);
4554 while ((skb = __skb_dequeue(&list)) != NULL)
4555 __skb_queue_tail(head, skb);
4556 spin_unlock_irq(&head->lock);
4557 }
4558#else
Jens Axboe65e19f52019-10-26 07:20:21 -06004559 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06004560#endif
4561}
4562
4563static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4564 int index)
4565{
4566#if defined(CONFIG_UNIX)
4567 struct sock *sock = ctx->ring_sock->sk;
4568 struct sk_buff_head *head = &sock->sk_receive_queue;
4569 struct sk_buff *skb;
4570
4571 /*
4572 * See if we can merge this file into an existing skb SCM_RIGHTS
4573 * file set. If there's no room, fall back to allocating a new skb
4574 * and filling it in.
4575 */
4576 spin_lock_irq(&head->lock);
4577 skb = skb_peek(head);
4578 if (skb) {
4579 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4580
4581 if (fpl->count < SCM_MAX_FD) {
4582 __skb_unlink(skb, head);
4583 spin_unlock_irq(&head->lock);
4584 fpl->fp[fpl->count] = get_file(file);
4585 unix_inflight(fpl->user, fpl->fp[fpl->count]);
4586 fpl->count++;
4587 spin_lock_irq(&head->lock);
4588 __skb_queue_head(head, skb);
4589 } else {
4590 skb = NULL;
4591 }
4592 }
4593 spin_unlock_irq(&head->lock);
4594
4595 if (skb) {
4596 fput(file);
4597 return 0;
4598 }
4599
4600 return __io_sqe_files_scm(ctx, 1, index);
4601#else
4602 return 0;
4603#endif
4604}
4605
4606static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
4607 unsigned nr_args)
4608{
4609 struct io_uring_files_update up;
4610 __s32 __user *fds;
4611 int fd, i, err;
4612 __u32 done;
4613
Jens Axboe65e19f52019-10-26 07:20:21 -06004614 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06004615 return -ENXIO;
4616 if (!nr_args)
4617 return -EINVAL;
4618 if (copy_from_user(&up, arg, sizeof(up)))
4619 return -EFAULT;
Eugene Syromiatnikov1292e972020-01-15 17:35:38 +01004620 if (up.resv)
4621 return -EINVAL;
Jens Axboec3a31e62019-10-03 13:59:56 -06004622 if (check_add_overflow(up.offset, nr_args, &done))
4623 return -EOVERFLOW;
4624 if (done > ctx->nr_user_files)
4625 return -EINVAL;
4626
4627 done = 0;
Eugene Syromiatnikov1292e972020-01-15 17:35:38 +01004628 fds = u64_to_user_ptr(up.fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06004629 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004630 struct fixed_file_table *table;
4631 unsigned index;
4632
Jens Axboec3a31e62019-10-03 13:59:56 -06004633 err = 0;
4634 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
4635 err = -EFAULT;
4636 break;
4637 }
4638 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004639 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4640 index = i & IORING_FILE_TABLE_MASK;
4641 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06004642 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06004643 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06004644 }
4645 if (fd != -1) {
4646 struct file *file;
4647
4648 file = fget(fd);
4649 if (!file) {
4650 err = -EBADF;
4651 break;
4652 }
4653 /*
4654 * Don't allow io_uring instances to be registered. If
4655 * UNIX isn't enabled, then this causes a reference
4656 * cycle and this instance can never get freed. If UNIX
4657 * is enabled we'll handle it just fine, but there's
4658 * still no point in allowing a ring fd as it doesn't
4659 * support regular read/write anyway.
4660 */
4661 if (file->f_op == &io_uring_fops) {
4662 fput(file);
4663 err = -EBADF;
4664 break;
4665 }
Jens Axboe65e19f52019-10-26 07:20:21 -06004666 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06004667 err = io_sqe_file_register(ctx, file, i);
4668 if (err)
4669 break;
4670 }
4671 nr_args--;
4672 done++;
4673 up.offset++;
4674 }
4675
4676 return done ? done : err;
4677}
4678
Jens Axboe7d723062019-11-12 22:31:31 -07004679static void io_put_work(struct io_wq_work *work)
4680{
4681 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4682
4683 io_put_req(req);
4684}
4685
4686static void io_get_work(struct io_wq_work *work)
4687{
4688 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4689
4690 refcount_inc(&req->refs);
4691}
4692
Jens Axboe6c271ce2019-01-10 11:22:30 -07004693static int io_sq_offload_start(struct io_ring_ctx *ctx,
4694 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004695{
Jens Axboe576a3472019-11-25 08:49:20 -07004696 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06004697 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004698 int ret;
4699
Jens Axboe6c271ce2019-01-10 11:22:30 -07004700 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004701 mmgrab(current->mm);
4702 ctx->sqo_mm = current->mm;
4703
Jens Axboe6c271ce2019-01-10 11:22:30 -07004704 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06004705 ret = -EPERM;
4706 if (!capable(CAP_SYS_ADMIN))
4707 goto err;
4708
Jens Axboe917257d2019-04-13 09:28:55 -06004709 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
4710 if (!ctx->sq_thread_idle)
4711 ctx->sq_thread_idle = HZ;
4712
Jens Axboe6c271ce2019-01-10 11:22:30 -07004713 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06004714 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004715
Jens Axboe917257d2019-04-13 09:28:55 -06004716 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06004717 if (cpu >= nr_cpu_ids)
4718 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08004719 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06004720 goto err;
4721
Jens Axboe6c271ce2019-01-10 11:22:30 -07004722 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
4723 ctx, cpu,
4724 "io_uring-sq");
4725 } else {
4726 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4727 "io_uring-sq");
4728 }
4729 if (IS_ERR(ctx->sqo_thread)) {
4730 ret = PTR_ERR(ctx->sqo_thread);
4731 ctx->sqo_thread = NULL;
4732 goto err;
4733 }
4734 wake_up_process(ctx->sqo_thread);
4735 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4736 /* Can't have SQ_AFF without SQPOLL */
4737 ret = -EINVAL;
4738 goto err;
4739 }
4740
Jens Axboe576a3472019-11-25 08:49:20 -07004741 data.mm = ctx->sqo_mm;
4742 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004743 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004744 data.get_work = io_get_work;
4745 data.put_work = io_put_work;
4746
Jens Axboe561fb042019-10-24 07:25:42 -06004747 /* Do QD, or 4 * CPUS, whatever is smallest */
4748 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004749 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004750 if (IS_ERR(ctx->io_wq)) {
4751 ret = PTR_ERR(ctx->io_wq);
4752 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004753 goto err;
4754 }
4755
4756 return 0;
4757err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004758 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004759 mmdrop(ctx->sqo_mm);
4760 ctx->sqo_mm = NULL;
4761 return ret;
4762}
4763
4764static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4765{
4766 atomic_long_sub(nr_pages, &user->locked_vm);
4767}
4768
4769static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4770{
4771 unsigned long page_limit, cur_pages, new_pages;
4772
4773 /* Don't allow more pages than we can safely lock */
4774 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4775
4776 do {
4777 cur_pages = atomic_long_read(&user->locked_vm);
4778 new_pages = cur_pages + nr_pages;
4779 if (new_pages > page_limit)
4780 return -ENOMEM;
4781 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4782 new_pages) != cur_pages);
4783
4784 return 0;
4785}
4786
4787static void io_mem_free(void *ptr)
4788{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004789 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004790
Mark Rutland52e04ef2019-04-30 17:30:21 +01004791 if (!ptr)
4792 return;
4793
4794 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004795 if (put_page_testzero(page))
4796 free_compound_page(page);
4797}
4798
4799static void *io_mem_alloc(size_t size)
4800{
4801 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4802 __GFP_NORETRY;
4803
4804 return (void *) __get_free_pages(gfp_flags, get_order(size));
4805}
4806
Hristo Venev75b28af2019-08-26 17:23:46 +00004807static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4808 size_t *sq_offset)
4809{
4810 struct io_rings *rings;
4811 size_t off, sq_array_size;
4812
4813 off = struct_size(rings, cqes, cq_entries);
4814 if (off == SIZE_MAX)
4815 return SIZE_MAX;
4816
4817#ifdef CONFIG_SMP
4818 off = ALIGN(off, SMP_CACHE_BYTES);
4819 if (off == 0)
4820 return SIZE_MAX;
4821#endif
4822
4823 sq_array_size = array_size(sizeof(u32), sq_entries);
4824 if (sq_array_size == SIZE_MAX)
4825 return SIZE_MAX;
4826
4827 if (check_add_overflow(off, sq_array_size, &off))
4828 return SIZE_MAX;
4829
4830 if (sq_offset)
4831 *sq_offset = off;
4832
4833 return off;
4834}
4835
Jens Axboe2b188cc2019-01-07 10:46:33 -07004836static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4837{
Hristo Venev75b28af2019-08-26 17:23:46 +00004838 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004839
Hristo Venev75b28af2019-08-26 17:23:46 +00004840 pages = (size_t)1 << get_order(
4841 rings_size(sq_entries, cq_entries, NULL));
4842 pages += (size_t)1 << get_order(
4843 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004844
Hristo Venev75b28af2019-08-26 17:23:46 +00004845 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004846}
4847
Jens Axboeedafcce2019-01-09 09:16:05 -07004848static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4849{
4850 int i, j;
4851
4852 if (!ctx->user_bufs)
4853 return -ENXIO;
4854
4855 for (i = 0; i < ctx->nr_user_bufs; i++) {
4856 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4857
4858 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004859 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004860
4861 if (ctx->account_mem)
4862 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004863 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004864 imu->nr_bvecs = 0;
4865 }
4866
4867 kfree(ctx->user_bufs);
4868 ctx->user_bufs = NULL;
4869 ctx->nr_user_bufs = 0;
4870 return 0;
4871}
4872
4873static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4874 void __user *arg, unsigned index)
4875{
4876 struct iovec __user *src;
4877
4878#ifdef CONFIG_COMPAT
4879 if (ctx->compat) {
4880 struct compat_iovec __user *ciovs;
4881 struct compat_iovec ciov;
4882
4883 ciovs = (struct compat_iovec __user *) arg;
4884 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4885 return -EFAULT;
4886
Jens Axboed55e5f52019-12-11 16:12:15 -07004887 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07004888 dst->iov_len = ciov.iov_len;
4889 return 0;
4890 }
4891#endif
4892 src = (struct iovec __user *) arg;
4893 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4894 return -EFAULT;
4895 return 0;
4896}
4897
4898static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4899 unsigned nr_args)
4900{
4901 struct vm_area_struct **vmas = NULL;
4902 struct page **pages = NULL;
4903 int i, j, got_pages = 0;
4904 int ret = -EINVAL;
4905
4906 if (ctx->user_bufs)
4907 return -EBUSY;
4908 if (!nr_args || nr_args > UIO_MAXIOV)
4909 return -EINVAL;
4910
4911 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4912 GFP_KERNEL);
4913 if (!ctx->user_bufs)
4914 return -ENOMEM;
4915
4916 for (i = 0; i < nr_args; i++) {
4917 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4918 unsigned long off, start, end, ubuf;
4919 int pret, nr_pages;
4920 struct iovec iov;
4921 size_t size;
4922
4923 ret = io_copy_iov(ctx, &iov, arg, i);
4924 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004925 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004926
4927 /*
4928 * Don't impose further limits on the size and buffer
4929 * constraints here, we'll -EINVAL later when IO is
4930 * submitted if they are wrong.
4931 */
4932 ret = -EFAULT;
4933 if (!iov.iov_base || !iov.iov_len)
4934 goto err;
4935
4936 /* arbitrary limit, but we need something */
4937 if (iov.iov_len > SZ_1G)
4938 goto err;
4939
4940 ubuf = (unsigned long) iov.iov_base;
4941 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4942 start = ubuf >> PAGE_SHIFT;
4943 nr_pages = end - start;
4944
4945 if (ctx->account_mem) {
4946 ret = io_account_mem(ctx->user, nr_pages);
4947 if (ret)
4948 goto err;
4949 }
4950
4951 ret = 0;
4952 if (!pages || nr_pages > got_pages) {
4953 kfree(vmas);
4954 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004955 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004956 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004957 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004958 sizeof(struct vm_area_struct *),
4959 GFP_KERNEL);
4960 if (!pages || !vmas) {
4961 ret = -ENOMEM;
4962 if (ctx->account_mem)
4963 io_unaccount_mem(ctx->user, nr_pages);
4964 goto err;
4965 }
4966 got_pages = nr_pages;
4967 }
4968
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004969 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004970 GFP_KERNEL);
4971 ret = -ENOMEM;
4972 if (!imu->bvec) {
4973 if (ctx->account_mem)
4974 io_unaccount_mem(ctx->user, nr_pages);
4975 goto err;
4976 }
4977
4978 ret = 0;
4979 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004980 pret = get_user_pages(ubuf, nr_pages,
4981 FOLL_WRITE | FOLL_LONGTERM,
4982 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004983 if (pret == nr_pages) {
4984 /* don't support file backed memory */
4985 for (j = 0; j < nr_pages; j++) {
4986 struct vm_area_struct *vma = vmas[j];
4987
4988 if (vma->vm_file &&
4989 !is_file_hugepages(vma->vm_file)) {
4990 ret = -EOPNOTSUPP;
4991 break;
4992 }
4993 }
4994 } else {
4995 ret = pret < 0 ? pret : -EFAULT;
4996 }
4997 up_read(&current->mm->mmap_sem);
4998 if (ret) {
4999 /*
5000 * if we did partial map, or found file backed vmas,
5001 * release any pages we did get
5002 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005003 if (pret > 0)
5004 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005005 if (ctx->account_mem)
5006 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005007 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005008 goto err;
5009 }
5010
5011 off = ubuf & ~PAGE_MASK;
5012 size = iov.iov_len;
5013 for (j = 0; j < nr_pages; j++) {
5014 size_t vec_len;
5015
5016 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5017 imu->bvec[j].bv_page = pages[j];
5018 imu->bvec[j].bv_len = vec_len;
5019 imu->bvec[j].bv_offset = off;
5020 off = 0;
5021 size -= vec_len;
5022 }
5023 /* store original address for later verification */
5024 imu->ubuf = ubuf;
5025 imu->len = iov.iov_len;
5026 imu->nr_bvecs = nr_pages;
5027
5028 ctx->nr_user_bufs++;
5029 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005030 kvfree(pages);
5031 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005032 return 0;
5033err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005034 kvfree(pages);
5035 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005036 io_sqe_buffer_unregister(ctx);
5037 return ret;
5038}
5039
Jens Axboe9b402842019-04-11 11:45:41 -06005040static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5041{
5042 __s32 __user *fds = arg;
5043 int fd;
5044
5045 if (ctx->cq_ev_fd)
5046 return -EBUSY;
5047
5048 if (copy_from_user(&fd, fds, sizeof(*fds)))
5049 return -EFAULT;
5050
5051 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5052 if (IS_ERR(ctx->cq_ev_fd)) {
5053 int ret = PTR_ERR(ctx->cq_ev_fd);
5054 ctx->cq_ev_fd = NULL;
5055 return ret;
5056 }
5057
5058 return 0;
5059}
5060
5061static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5062{
5063 if (ctx->cq_ev_fd) {
5064 eventfd_ctx_put(ctx->cq_ev_fd);
5065 ctx->cq_ev_fd = NULL;
5066 return 0;
5067 }
5068
5069 return -ENXIO;
5070}
5071
Jens Axboe2b188cc2019-01-07 10:46:33 -07005072static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5073{
Jens Axboe6b063142019-01-10 22:13:58 -07005074 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005075 if (ctx->sqo_mm)
5076 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005077
5078 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005079 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005080 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005081 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005082
Jens Axboe2b188cc2019-01-07 10:46:33 -07005083#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005084 if (ctx->ring_sock) {
5085 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005086 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005087 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005088#endif
5089
Hristo Venev75b28af2019-08-26 17:23:46 +00005090 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005091 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005092
5093 percpu_ref_exit(&ctx->refs);
5094 if (ctx->account_mem)
5095 io_unaccount_mem(ctx->user,
5096 ring_pages(ctx->sq_entries, ctx->cq_entries));
5097 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07005098 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07005099 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07005100 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07005101 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005102 kfree(ctx);
5103}
5104
5105static __poll_t io_uring_poll(struct file *file, poll_table *wait)
5106{
5107 struct io_ring_ctx *ctx = file->private_data;
5108 __poll_t mask = 0;
5109
5110 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02005111 /*
5112 * synchronizes with barrier from wq_has_sleeper call in
5113 * io_commit_cqring
5114 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005115 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00005116 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
5117 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005118 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08005119 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005120 mask |= EPOLLIN | EPOLLRDNORM;
5121
5122 return mask;
5123}
5124
5125static int io_uring_fasync(int fd, struct file *file, int on)
5126{
5127 struct io_ring_ctx *ctx = file->private_data;
5128
5129 return fasync_helper(fd, file, on, &ctx->cq_fasync);
5130}
5131
5132static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
5133{
5134 mutex_lock(&ctx->uring_lock);
5135 percpu_ref_kill(&ctx->refs);
5136 mutex_unlock(&ctx->uring_lock);
5137
Jens Axboe5262f562019-09-17 12:26:57 -06005138 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005139 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06005140
5141 if (ctx->io_wq)
5142 io_wq_cancel_all(ctx->io_wq);
5143
Jens Axboedef596e2019-01-09 08:59:42 -07005144 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07005145 /* if we failed setting up the ctx, we might not have any rings */
5146 if (ctx->rings)
5147 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07005148 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005149 io_ring_ctx_free(ctx);
5150}
5151
5152static int io_uring_release(struct inode *inode, struct file *file)
5153{
5154 struct io_ring_ctx *ctx = file->private_data;
5155
5156 file->private_data = NULL;
5157 io_ring_ctx_wait_and_kill(ctx);
5158 return 0;
5159}
5160
Jens Axboefcb323c2019-10-24 12:39:47 -06005161static void io_uring_cancel_files(struct io_ring_ctx *ctx,
5162 struct files_struct *files)
5163{
5164 struct io_kiocb *req;
5165 DEFINE_WAIT(wait);
5166
5167 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07005168 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06005169
5170 spin_lock_irq(&ctx->inflight_lock);
5171 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07005172 if (req->work.files != files)
5173 continue;
5174 /* req is being completed, ignore */
5175 if (!refcount_inc_not_zero(&req->refs))
5176 continue;
5177 cancel_req = req;
5178 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06005179 }
Jens Axboe768134d2019-11-10 20:30:53 -07005180 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005181 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07005182 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06005183 spin_unlock_irq(&ctx->inflight_lock);
5184
Jens Axboe768134d2019-11-10 20:30:53 -07005185 /* We need to keep going until we don't find a matching req */
5186 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005187 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005188
5189 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5190 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005191 schedule();
5192 }
Jens Axboe768134d2019-11-10 20:30:53 -07005193 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005194}
5195
5196static int io_uring_flush(struct file *file, void *data)
5197{
5198 struct io_ring_ctx *ctx = file->private_data;
5199
5200 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005201 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5202 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005203 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005204 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005205 return 0;
5206}
5207
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005208static void *io_uring_validate_mmap_request(struct file *file,
5209 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005210{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005211 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005212 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005213 struct page *page;
5214 void *ptr;
5215
5216 switch (offset) {
5217 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005218 case IORING_OFF_CQ_RING:
5219 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005220 break;
5221 case IORING_OFF_SQES:
5222 ptr = ctx->sq_sqes;
5223 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005224 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005225 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005226 }
5227
5228 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005229 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005230 return ERR_PTR(-EINVAL);
5231
5232 return ptr;
5233}
5234
5235#ifdef CONFIG_MMU
5236
5237static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5238{
5239 size_t sz = vma->vm_end - vma->vm_start;
5240 unsigned long pfn;
5241 void *ptr;
5242
5243 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5244 if (IS_ERR(ptr))
5245 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005246
5247 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5248 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5249}
5250
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005251#else /* !CONFIG_MMU */
5252
5253static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5254{
5255 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5256}
5257
5258static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5259{
5260 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5261}
5262
5263static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5264 unsigned long addr, unsigned long len,
5265 unsigned long pgoff, unsigned long flags)
5266{
5267 void *ptr;
5268
5269 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5270 if (IS_ERR(ptr))
5271 return PTR_ERR(ptr);
5272
5273 return (unsigned long) ptr;
5274}
5275
5276#endif /* !CONFIG_MMU */
5277
Jens Axboe2b188cc2019-01-07 10:46:33 -07005278SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5279 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5280 size_t, sigsz)
5281{
5282 struct io_ring_ctx *ctx;
5283 long ret = -EBADF;
5284 int submitted = 0;
5285 struct fd f;
5286
Jens Axboe6c271ce2019-01-10 11:22:30 -07005287 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005288 return -EINVAL;
5289
5290 f = fdget(fd);
5291 if (!f.file)
5292 return -EBADF;
5293
5294 ret = -EOPNOTSUPP;
5295 if (f.file->f_op != &io_uring_fops)
5296 goto out_fput;
5297
5298 ret = -ENXIO;
5299 ctx = f.file->private_data;
5300 if (!percpu_ref_tryget(&ctx->refs))
5301 goto out_fput;
5302
Jens Axboe6c271ce2019-01-10 11:22:30 -07005303 /*
5304 * For SQ polling, the thread will do all submissions and completions.
5305 * Just return the requested submit count, and wake the thread if
5306 * we were asked to.
5307 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005308 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005309 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005310 if (!list_empty_careful(&ctx->cq_overflow_list))
5311 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005312 if (flags & IORING_ENTER_SQ_WAKEUP)
5313 wake_up(&ctx->sqo_wait);
5314 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005315 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005316 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005317
Jens Axboe44d28272020-01-16 19:00:24 -07005318 if (current->mm != ctx->sqo_mm ||
5319 current_cred() != ctx->creds) {
5320 ret = -EPERM;
5321 goto out;
5322 }
5323
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005324 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005325 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005326 /* already have mm, so io_submit_sqes() won't try to grab it */
5327 cur_mm = ctx->sqo_mm;
5328 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
5329 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005330 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005331
5332 if (submitted != to_submit)
5333 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005334 }
5335 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07005336 unsigned nr_events = 0;
5337
Jens Axboe2b188cc2019-01-07 10:46:33 -07005338 min_complete = min(min_complete, ctx->cq_entries);
5339
Jens Axboedef596e2019-01-09 08:59:42 -07005340 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07005341 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07005342 } else {
5343 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
5344 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005345 }
5346
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005347out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03005348 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005349out_fput:
5350 fdput(f);
5351 return submitted ? submitted : ret;
5352}
5353
5354static const struct file_operations io_uring_fops = {
5355 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06005356 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07005357 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005358#ifndef CONFIG_MMU
5359 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
5360 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
5361#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07005362 .poll = io_uring_poll,
5363 .fasync = io_uring_fasync,
5364};
5365
5366static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
5367 struct io_uring_params *p)
5368{
Hristo Venev75b28af2019-08-26 17:23:46 +00005369 struct io_rings *rings;
5370 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005371
Hristo Venev75b28af2019-08-26 17:23:46 +00005372 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
5373 if (size == SIZE_MAX)
5374 return -EOVERFLOW;
5375
5376 rings = io_mem_alloc(size);
5377 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005378 return -ENOMEM;
5379
Hristo Venev75b28af2019-08-26 17:23:46 +00005380 ctx->rings = rings;
5381 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
5382 rings->sq_ring_mask = p->sq_entries - 1;
5383 rings->cq_ring_mask = p->cq_entries - 1;
5384 rings->sq_ring_entries = p->sq_entries;
5385 rings->cq_ring_entries = p->cq_entries;
5386 ctx->sq_mask = rings->sq_ring_mask;
5387 ctx->cq_mask = rings->cq_ring_mask;
5388 ctx->sq_entries = rings->sq_ring_entries;
5389 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005390
5391 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07005392 if (size == SIZE_MAX) {
5393 io_mem_free(ctx->rings);
5394 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005395 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07005396 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005397
5398 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07005399 if (!ctx->sq_sqes) {
5400 io_mem_free(ctx->rings);
5401 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005402 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07005403 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005404
Jens Axboe2b188cc2019-01-07 10:46:33 -07005405 return 0;
5406}
5407
5408/*
5409 * Allocate an anonymous fd, this is what constitutes the application
5410 * visible backing of an io_uring instance. The application mmaps this
5411 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
5412 * we have to tie this fd to a socket for file garbage collection purposes.
5413 */
5414static int io_uring_get_fd(struct io_ring_ctx *ctx)
5415{
5416 struct file *file;
5417 int ret;
5418
5419#if defined(CONFIG_UNIX)
5420 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
5421 &ctx->ring_sock);
5422 if (ret)
5423 return ret;
5424#endif
5425
5426 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
5427 if (ret < 0)
5428 goto err;
5429
5430 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
5431 O_RDWR | O_CLOEXEC);
5432 if (IS_ERR(file)) {
5433 put_unused_fd(ret);
5434 ret = PTR_ERR(file);
5435 goto err;
5436 }
5437
5438#if defined(CONFIG_UNIX)
5439 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07005440 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005441#endif
5442 fd_install(ret, file);
5443 return ret;
5444err:
5445#if defined(CONFIG_UNIX)
5446 sock_release(ctx->ring_sock);
5447 ctx->ring_sock = NULL;
5448#endif
5449 return ret;
5450}
5451
5452static int io_uring_create(unsigned entries, struct io_uring_params *p)
5453{
5454 struct user_struct *user = NULL;
5455 struct io_ring_ctx *ctx;
5456 bool account_mem;
5457 int ret;
5458
5459 if (!entries || entries > IORING_MAX_ENTRIES)
5460 return -EINVAL;
5461
5462 /*
5463 * Use twice as many entries for the CQ ring. It's possible for the
5464 * application to drive a higher depth than the size of the SQ ring,
5465 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005466 * some flexibility in overcommitting a bit. If the application has
5467 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5468 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005469 */
5470 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005471 if (p->flags & IORING_SETUP_CQSIZE) {
5472 /*
5473 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5474 * to a power-of-two, if it isn't already. We do NOT impose
5475 * any cq vs sq ring sizing.
5476 */
5477 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5478 return -EINVAL;
5479 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5480 } else {
5481 p->cq_entries = 2 * p->sq_entries;
5482 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005483
5484 user = get_uid(current_user());
5485 account_mem = !capable(CAP_IPC_LOCK);
5486
5487 if (account_mem) {
5488 ret = io_account_mem(user,
5489 ring_pages(p->sq_entries, p->cq_entries));
5490 if (ret) {
5491 free_uid(user);
5492 return ret;
5493 }
5494 }
5495
5496 ctx = io_ring_ctx_alloc(p);
5497 if (!ctx) {
5498 if (account_mem)
5499 io_unaccount_mem(user, ring_pages(p->sq_entries,
5500 p->cq_entries));
5501 free_uid(user);
5502 return -ENOMEM;
5503 }
5504 ctx->compat = in_compat_syscall();
5505 ctx->account_mem = account_mem;
5506 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07005507 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07005508
5509 ret = io_allocate_scq_urings(ctx, p);
5510 if (ret)
5511 goto err;
5512
Jens Axboe6c271ce2019-01-10 11:22:30 -07005513 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005514 if (ret)
5515 goto err;
5516
Jens Axboe2b188cc2019-01-07 10:46:33 -07005517 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005518 p->sq_off.head = offsetof(struct io_rings, sq.head);
5519 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5520 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5521 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5522 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5523 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5524 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005525
5526 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005527 p->cq_off.head = offsetof(struct io_rings, cq.head);
5528 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5529 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5530 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5531 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5532 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06005533
Jens Axboe044c1ab2019-10-28 09:15:33 -06005534 /*
5535 * Install ring fd as the very last thing, so we don't risk someone
5536 * having closed it before we finish setup
5537 */
5538 ret = io_uring_get_fd(ctx);
5539 if (ret < 0)
5540 goto err;
5541
Jens Axboeda8c9692019-12-02 18:51:26 -07005542 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
5543 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005544 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005545 return ret;
5546err:
5547 io_ring_ctx_wait_and_kill(ctx);
5548 return ret;
5549}
5550
5551/*
5552 * Sets up an aio uring context, and returns the fd. Applications asks for a
5553 * ring size, we return the actual sq/cq ring sizes (among other things) in the
5554 * params structure passed in.
5555 */
5556static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5557{
5558 struct io_uring_params p;
5559 long ret;
5560 int i;
5561
5562 if (copy_from_user(&p, params, sizeof(p)))
5563 return -EFAULT;
5564 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5565 if (p.resv[i])
5566 return -EINVAL;
5567 }
5568
Jens Axboe6c271ce2019-01-10 11:22:30 -07005569 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06005570 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005571 return -EINVAL;
5572
5573 ret = io_uring_create(entries, &p);
5574 if (ret < 0)
5575 return ret;
5576
5577 if (copy_to_user(params, &p, sizeof(p)))
5578 return -EFAULT;
5579
5580 return ret;
5581}
5582
5583SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5584 struct io_uring_params __user *, params)
5585{
5586 return io_uring_setup(entries, params);
5587}
5588
Jens Axboeedafcce2019-01-09 09:16:05 -07005589static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
5590 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06005591 __releases(ctx->uring_lock)
5592 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07005593{
5594 int ret;
5595
Jens Axboe35fa71a2019-04-22 10:23:23 -06005596 /*
5597 * We're inside the ring mutex, if the ref is already dying, then
5598 * someone else killed the ctx or is already going through
5599 * io_uring_register().
5600 */
5601 if (percpu_ref_is_dying(&ctx->refs))
5602 return -ENXIO;
5603
Jens Axboeedafcce2019-01-09 09:16:05 -07005604 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06005605
5606 /*
5607 * Drop uring mutex before waiting for references to exit. If another
5608 * thread is currently inside io_uring_enter() it might need to grab
5609 * the uring_lock to make progress. If we hold it here across the drain
5610 * wait, then we can deadlock. It's safe to drop the mutex here, since
5611 * no new references will come in after we've killed the percpu ref.
5612 */
5613 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07005614 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06005615 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07005616
5617 switch (opcode) {
5618 case IORING_REGISTER_BUFFERS:
5619 ret = io_sqe_buffer_register(ctx, arg, nr_args);
5620 break;
5621 case IORING_UNREGISTER_BUFFERS:
5622 ret = -EINVAL;
5623 if (arg || nr_args)
5624 break;
5625 ret = io_sqe_buffer_unregister(ctx);
5626 break;
Jens Axboe6b063142019-01-10 22:13:58 -07005627 case IORING_REGISTER_FILES:
5628 ret = io_sqe_files_register(ctx, arg, nr_args);
5629 break;
5630 case IORING_UNREGISTER_FILES:
5631 ret = -EINVAL;
5632 if (arg || nr_args)
5633 break;
5634 ret = io_sqe_files_unregister(ctx);
5635 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06005636 case IORING_REGISTER_FILES_UPDATE:
5637 ret = io_sqe_files_update(ctx, arg, nr_args);
5638 break;
Jens Axboe9b402842019-04-11 11:45:41 -06005639 case IORING_REGISTER_EVENTFD:
5640 ret = -EINVAL;
5641 if (nr_args != 1)
5642 break;
5643 ret = io_eventfd_register(ctx, arg);
5644 break;
5645 case IORING_UNREGISTER_EVENTFD:
5646 ret = -EINVAL;
5647 if (arg || nr_args)
5648 break;
5649 ret = io_eventfd_unregister(ctx);
5650 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07005651 default:
5652 ret = -EINVAL;
5653 break;
5654 }
5655
5656 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07005657 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07005658 percpu_ref_reinit(&ctx->refs);
5659 return ret;
5660}
5661
5662SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
5663 void __user *, arg, unsigned int, nr_args)
5664{
5665 struct io_ring_ctx *ctx;
5666 long ret = -EBADF;
5667 struct fd f;
5668
5669 f = fdget(fd);
5670 if (!f.file)
5671 return -EBADF;
5672
5673 ret = -EOPNOTSUPP;
5674 if (f.file->f_op != &io_uring_fops)
5675 goto out_fput;
5676
5677 ctx = f.file->private_data;
5678
5679 mutex_lock(&ctx->uring_lock);
5680 ret = __io_uring_register(ctx, opcode, arg, nr_args);
5681 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005682 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
5683 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005684out_fput:
5685 fdput(f);
5686 return ret;
5687}
5688
Jens Axboe2b188cc2019-01-07 10:46:33 -07005689static int __init io_uring_init(void)
5690{
5691 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
5692 return 0;
5693};
5694__initcall(io_uring_init);