blob: 34cbce622fcd25418c4b23ad1e00ad754afb1bed [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 Axboe561fb042019-10-24 07:25:42 -06003463 if (work->flags & IO_WQ_WORK_CANCEL)
3464 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07003465
Jens Axboe561fb042019-10-24 07:25:42 -06003466 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003467 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3468 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003469 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003470 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003471 /*
3472 * We can get EAGAIN for polled IO even though we're
3473 * forcing a sync submission from here, since we can't
3474 * wait for request slots on the block side.
3475 */
3476 if (ret != -EAGAIN)
3477 break;
3478 cond_resched();
3479 } while (1);
3480 }
Jens Axboe31b51512019-01-18 22:56:34 -07003481
Jens Axboe561fb042019-10-24 07:25:42 -06003482 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003483 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003484
Jens Axboe561fb042019-10-24 07:25:42 -06003485 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003486 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003487 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003488 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003489 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003490
Jens Axboe561fb042019-10-24 07:25:42 -06003491 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07003492 if (!ret && nxt)
3493 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07003494}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003495
Jens Axboe9e3aa612019-12-11 15:55:43 -07003496static bool io_req_op_valid(int op)
3497{
3498 return op >= IORING_OP_NOP && op < IORING_OP_LAST;
3499}
3500
Jens Axboe15b71ab2019-12-11 11:20:36 -07003501static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06003502{
Jens Axboed625c6e2019-12-17 19:53:05 -07003503 switch (req->opcode) {
Jens Axboe09bb8392019-03-13 12:39:28 -06003504 case IORING_OP_NOP:
3505 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03003506 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03003507 case IORING_OP_TIMEOUT_REMOVE:
3508 case IORING_OP_ASYNC_CANCEL:
3509 case IORING_OP_LINK_TIMEOUT:
Jens Axboe9e3aa612019-12-11 15:55:43 -07003510 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003511 case IORING_OP_OPENAT:
3512 return fd != -1;
Jens Axboe09bb8392019-03-13 12:39:28 -06003513 default:
Jens Axboed625c6e2019-12-17 19:53:05 -07003514 if (io_req_op_valid(req->opcode))
Jens Axboe9e3aa612019-12-11 15:55:43 -07003515 return 1;
3516 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003517 }
3518}
3519
Jens Axboe65e19f52019-10-26 07:20:21 -06003520static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3521 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003522{
Jens Axboe65e19f52019-10-26 07:20:21 -06003523 struct fixed_file_table *table;
3524
3525 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
3526 return table->files[index & IORING_FILE_TABLE_MASK];
3527}
3528
Jens Axboe3529d8c2019-12-19 18:24:38 -07003529static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
3530 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06003531{
Jackie Liua197f662019-11-08 08:09:12 -07003532 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003533 unsigned flags;
Jens Axboe9e3aa612019-12-11 15:55:43 -07003534 int fd, ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003535
Jens Axboe3529d8c2019-12-19 18:24:38 -07003536 flags = READ_ONCE(sqe->flags);
3537 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003538
Jackie Liu4fe2c962019-09-09 20:50:40 +08003539 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003540 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003541
Jens Axboe15b71ab2019-12-11 11:20:36 -07003542 ret = io_req_needs_file(req, fd);
Jens Axboe9e3aa612019-12-11 15:55:43 -07003543 if (ret <= 0)
3544 return ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003545
3546 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003547 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003548 (unsigned) fd >= ctx->nr_user_files))
3549 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003550 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003551 req->file = io_file_from_index(ctx, fd);
3552 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003553 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003554 req->flags |= REQ_F_FIXED_FILE;
3555 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003556 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003557 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003558 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003559 req->file = io_file_get(state, fd);
3560 if (unlikely(!req->file))
3561 return -EBADF;
3562 }
3563
3564 return 0;
3565}
3566
Jackie Liua197f662019-11-08 08:09:12 -07003567static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003568{
Jens Axboefcb323c2019-10-24 12:39:47 -06003569 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003570 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003571
3572 rcu_read_lock();
3573 spin_lock_irq(&ctx->inflight_lock);
3574 /*
3575 * We use the f_ops->flush() handler to ensure that we can flush
3576 * out work accessing these files if the fd is closed. Check if
3577 * the fd has changed since we started down this path, and disallow
3578 * this operation if it has.
3579 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003580 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003581 list_add(&req->inflight_entry, &ctx->inflight_list);
3582 req->flags |= REQ_F_INFLIGHT;
3583 req->work.files = current->files;
3584 ret = 0;
3585 }
3586 spin_unlock_irq(&ctx->inflight_lock);
3587 rcu_read_unlock();
3588
3589 return ret;
3590}
3591
Jens Axboe2665abf2019-11-05 12:40:47 -07003592static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3593{
Jens Axboead8a48a2019-11-15 08:49:11 -07003594 struct io_timeout_data *data = container_of(timer,
3595 struct io_timeout_data, timer);
3596 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003597 struct io_ring_ctx *ctx = req->ctx;
3598 struct io_kiocb *prev = NULL;
3599 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003600
3601 spin_lock_irqsave(&ctx->completion_lock, flags);
3602
3603 /*
3604 * We don't expect the list to be empty, that will only happen if we
3605 * race with the completion of the linked work.
3606 */
Pavel Begunkov44932332019-12-05 16:16:35 +03003607 if (!list_empty(&req->link_list)) {
3608 prev = list_entry(req->link_list.prev, struct io_kiocb,
3609 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003610 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03003611 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003612 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3613 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003614 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003615 }
3616
3617 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3618
3619 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003620 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003621 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3622 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003623 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003624 } else {
3625 io_cqring_add_event(req, -ETIME);
3626 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003627 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003628 return HRTIMER_NORESTART;
3629}
3630
Jens Axboead8a48a2019-11-15 08:49:11 -07003631static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003632{
Jens Axboe76a46e02019-11-10 23:34:16 -07003633 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07003634
Jens Axboe76a46e02019-11-10 23:34:16 -07003635 /*
3636 * If the list is now empty, then our linked request finished before
3637 * we got a chance to setup the timer
3638 */
3639 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03003640 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07003641 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003642
Jens Axboead8a48a2019-11-15 08:49:11 -07003643 data->timer.function = io_link_timeout_fn;
3644 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3645 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07003646 }
Jens Axboe76a46e02019-11-10 23:34:16 -07003647 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07003648
Jens Axboe2665abf2019-11-05 12:40:47 -07003649 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07003650 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003651}
3652
Jens Axboead8a48a2019-11-15 08:49:11 -07003653static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003654{
3655 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003656
Jens Axboe2665abf2019-11-05 12:40:47 -07003657 if (!(req->flags & REQ_F_LINK))
3658 return NULL;
3659
Pavel Begunkov44932332019-12-05 16:16:35 +03003660 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
3661 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07003662 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07003663 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003664
Jens Axboe76a46e02019-11-10 23:34:16 -07003665 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07003666 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07003667}
3668
Jens Axboe3529d8c2019-12-19 18:24:38 -07003669static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003670{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003671 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003672 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003673 int ret;
3674
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003675again:
3676 linked_timeout = io_prep_linked_timeout(req);
3677
Jens Axboe3529d8c2019-12-19 18:24:38 -07003678 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06003679
3680 /*
3681 * We async punt it if the file wasn't marked NOWAIT, or if the file
3682 * doesn't support non-blocking read/write attempts
3683 */
3684 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3685 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003686 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3687 ret = io_grab_files(req);
3688 if (ret)
3689 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003690 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003691
3692 /*
3693 * Queued up for async execution, worker will release
3694 * submit reference when the iocb is actually submitted.
3695 */
3696 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003697 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003698 }
Jens Axboee65ef562019-03-12 10:16:44 -06003699
Jens Axboefcb323c2019-10-24 12:39:47 -06003700err:
Jens Axboee65ef562019-03-12 10:16:44 -06003701 /* drop submission reference */
3702 io_put_req(req);
3703
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003704 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003705 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003706 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003707 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003708 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003709 }
3710
Jens Axboee65ef562019-03-12 10:16:44 -06003711 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003712 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003713 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003714 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003715 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003716 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003717done_req:
3718 if (nxt) {
3719 req = nxt;
3720 nxt = NULL;
3721 goto again;
3722 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003723}
3724
Jens Axboe3529d8c2019-12-19 18:24:38 -07003725static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003726{
3727 int ret;
3728
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003729 if (unlikely(req->ctx->drain_next)) {
3730 req->flags |= REQ_F_IO_DRAIN;
3731 req->ctx->drain_next = false;
3732 }
3733 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3734
Jens Axboe3529d8c2019-12-19 18:24:38 -07003735 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003736 if (ret) {
3737 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003738 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003739 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003740 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003741 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003742 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07003743 __io_queue_sqe(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003744}
3745
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003746static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003747{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003748 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003749 io_cqring_add_event(req, -ECANCELED);
3750 io_double_put_req(req);
3751 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07003752 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003753}
3754
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003755#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
3756 IOSQE_IO_HARDLINK)
Jens Axboe9e645e112019-05-10 16:07:28 -06003757
Jens Axboe3529d8c2019-12-19 18:24:38 -07003758static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3759 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003760{
Jackie Liua197f662019-11-08 08:09:12 -07003761 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003762 int ret;
3763
3764 /* enforce forwards compatibility on users */
Jens Axboe3529d8c2019-12-19 18:24:38 -07003765 if (unlikely(sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003766 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003767 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003768 }
3769
Jens Axboe3529d8c2019-12-19 18:24:38 -07003770 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06003771 if (unlikely(ret)) {
3772err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003773 io_cqring_add_event(req, ret);
3774 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003775 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06003776 }
3777
Jens Axboe9e645e112019-05-10 16:07:28 -06003778 /*
3779 * If we already have a head request, queue this one for async
3780 * submittal once the head completes. If we don't have a head but
3781 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3782 * submitted sync once the chain is complete. If none of those
3783 * conditions are true (normal request), then just queue it.
3784 */
3785 if (*link) {
3786 struct io_kiocb *prev = *link;
3787
Jens Axboe3529d8c2019-12-19 18:24:38 -07003788 if (sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003789 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3790
Jens Axboe3529d8c2019-12-19 18:24:38 -07003791 if (sqe->flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003792 req->flags |= REQ_F_HARDLINK;
3793
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003794 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003795 ret = -EAGAIN;
3796 goto err_req;
3797 }
3798
Jens Axboe3529d8c2019-12-19 18:24:38 -07003799 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07003800 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003801 /* fail even hard links since we don't submit */
Jens Axboe2d283902019-12-04 11:08:05 -07003802 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07003803 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07003804 }
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003805 trace_io_uring_link(ctx, req, prev);
Pavel Begunkov44932332019-12-05 16:16:35 +03003806 list_add_tail(&req->link_list, &prev->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003807 } else if (sqe->flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003808 req->flags |= REQ_F_LINK;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003809 if (sqe->flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003810 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06003811
Jens Axboe9e645e112019-05-10 16:07:28 -06003812 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003813 ret = io_req_defer_prep(req, sqe);
3814 if (ret)
3815 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06003816 *link = req;
3817 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003818 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06003819 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003820
3821 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06003822}
3823
Jens Axboe9a56a232019-01-09 09:06:50 -07003824/*
3825 * Batched submission is done, ensure local IO is flushed out.
3826 */
3827static void io_submit_state_end(struct io_submit_state *state)
3828{
3829 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003830 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003831 if (state->free_reqs)
3832 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3833 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003834}
3835
3836/*
3837 * Start submission side cache.
3838 */
3839static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08003840 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07003841{
3842 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003843 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003844 state->file = NULL;
3845 state->ios_left = max_ios;
3846}
3847
Jens Axboe2b188cc2019-01-07 10:46:33 -07003848static void io_commit_sqring(struct io_ring_ctx *ctx)
3849{
Hristo Venev75b28af2019-08-26 17:23:46 +00003850 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003851
Hristo Venev75b28af2019-08-26 17:23:46 +00003852 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003853 /*
3854 * Ensure any loads from the SQEs are done at this point,
3855 * since once we write the new head, the application could
3856 * write new data to them.
3857 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003858 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003859 }
3860}
3861
3862/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07003863 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07003864 * that is mapped by userspace. This means that care needs to be taken to
3865 * ensure that reads are stable, as we cannot rely on userspace always
3866 * being a good citizen. If members of the sqe are validated and then later
3867 * used, it's important that those reads are done through READ_ONCE() to
3868 * prevent a re-load down the line.
3869 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07003870static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
3871 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003872{
Hristo Venev75b28af2019-08-26 17:23:46 +00003873 struct io_rings *rings = ctx->rings;
3874 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003875 unsigned head;
3876
3877 /*
3878 * The cached sq head (or cq tail) serves two purposes:
3879 *
3880 * 1) allows us to batch the cost of updating the user visible
3881 * head updates.
3882 * 2) allows the kernel side to track the head on its own, even
3883 * though the application is the one updating it.
3884 */
3885 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003886 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003887 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003888 return false;
3889
Hristo Venev75b28af2019-08-26 17:23:46 +00003890 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003891 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003892 /*
3893 * All io need record the previous position, if LINK vs DARIN,
3894 * it can be used to mark the position of the first IO in the
3895 * link list.
3896 */
3897 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003898 *sqe_ptr = &ctx->sq_sqes[head];
3899 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
3900 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003901 ctx->cached_sq_head++;
3902 return true;
3903 }
3904
3905 /* drop invalid entries */
3906 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003907 ctx->cached_sq_dropped++;
3908 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003909 return false;
3910}
3911
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003912static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003913 struct file *ring_file, int ring_fd,
3914 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003915{
3916 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003917 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003918 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003919 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003920
Jens Axboec4a2ed72019-11-21 21:01:26 -07003921 /* if we have a backlog and couldn't flush it all, return BUSY */
3922 if (!list_empty(&ctx->cq_overflow_list) &&
3923 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003924 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003925
3926 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08003927 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003928 statep = &state;
3929 }
3930
3931 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003932 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03003933 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003934 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003935
Pavel Begunkov196be952019-11-07 01:41:06 +03003936 req = io_get_req(ctx, statep);
3937 if (unlikely(!req)) {
3938 if (!submitted)
3939 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003940 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003941 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07003942 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003943 __io_free_req(req);
3944 break;
3945 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003946
Jens Axboed625c6e2019-12-17 19:53:05 -07003947 if (io_req_needs_user(req) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003948 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3949 if (!mm_fault) {
3950 use_mm(ctx->sqo_mm);
3951 *mm = ctx->sqo_mm;
3952 }
3953 }
3954
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003955 submitted++;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003956 sqe_flags = sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003957
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003958 req->ring_file = ring_file;
3959 req->ring_fd = ring_fd;
3960 req->has_user = *mm != NULL;
3961 req->in_async = async;
3962 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07003963 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003964 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003965 break;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003966 /*
3967 * If previous wasn't linked and we have a linked command,
3968 * that's the end of the chain. Submit the previous link.
3969 */
Pavel Begunkovffbb8d62019-12-17 20:57:05 +03003970 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003971 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003972 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003973 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003974 }
3975
Jens Axboe9e645e112019-05-10 16:07:28 -06003976 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003977 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003978 if (statep)
3979 io_submit_state_end(&state);
3980
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003981 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3982 io_commit_sqring(ctx);
3983
Jens Axboe6c271ce2019-01-10 11:22:30 -07003984 return submitted;
3985}
3986
3987static int io_sq_thread(void *data)
3988{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003989 struct io_ring_ctx *ctx = data;
3990 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003991 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003992 mm_segment_t old_fs;
3993 DEFINE_WAIT(wait);
3994 unsigned inflight;
3995 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003996 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003997
Jens Axboe206aefd2019-11-07 18:27:42 -07003998 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003999
Jens Axboe6c271ce2019-01-10 11:22:30 -07004000 old_fs = get_fs();
4001 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004002 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004003
Jens Axboec1edbf52019-11-10 16:56:04 -07004004 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004005 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004006 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004007
4008 if (inflight) {
4009 unsigned nr_events = 0;
4010
4011 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004012 /*
4013 * inflight is the count of the maximum possible
4014 * entries we submitted, but it can be smaller
4015 * if we dropped some of them. If we don't have
4016 * poll entries available, then we know that we
4017 * have nothing left to poll for. Reset the
4018 * inflight count to zero in that case.
4019 */
4020 mutex_lock(&ctx->uring_lock);
4021 if (!list_empty(&ctx->poll_list))
4022 __io_iopoll_check(ctx, &nr_events, 0);
4023 else
4024 inflight = 0;
4025 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004026 } else {
4027 /*
4028 * Normal IO, just pretend everything completed.
4029 * We don't have to poll completions for that.
4030 */
4031 nr_events = inflight;
4032 }
4033
4034 inflight -= nr_events;
4035 if (!inflight)
4036 timeout = jiffies + ctx->sq_thread_idle;
4037 }
4038
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004039 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004040
4041 /*
4042 * If submit got -EBUSY, flag us as needing the application
4043 * to enter the kernel to reap and flush events.
4044 */
4045 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004046 /*
4047 * We're polling. If we're within the defined idle
4048 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004049 * to sleep. The exception is if we got EBUSY doing
4050 * more IO, we should wait for the application to
4051 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004052 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004053 if (inflight ||
4054 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004055 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004056 continue;
4057 }
4058
4059 /*
4060 * Drop cur_mm before scheduling, we can't hold it for
4061 * long periods (or over schedule()). Do this before
4062 * adding ourselves to the waitqueue, as the unuse/drop
4063 * may sleep.
4064 */
4065 if (cur_mm) {
4066 unuse_mm(cur_mm);
4067 mmput(cur_mm);
4068 cur_mm = NULL;
4069 }
4070
4071 prepare_to_wait(&ctx->sqo_wait, &wait,
4072 TASK_INTERRUPTIBLE);
4073
4074 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004075 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004076 /* make sure to read SQ tail after writing flags */
4077 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004078
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004079 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004080 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004081 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004082 finish_wait(&ctx->sqo_wait, &wait);
4083 break;
4084 }
4085 if (signal_pending(current))
4086 flush_signals(current);
4087 schedule();
4088 finish_wait(&ctx->sqo_wait, &wait);
4089
Hristo Venev75b28af2019-08-26 17:23:46 +00004090 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004091 continue;
4092 }
4093 finish_wait(&ctx->sqo_wait, &wait);
4094
Hristo Venev75b28af2019-08-26 17:23:46 +00004095 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004096 }
4097
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004098 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004099 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004100 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004101 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004102 if (ret > 0)
4103 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004104 }
4105
4106 set_fs(old_fs);
4107 if (cur_mm) {
4108 unuse_mm(cur_mm);
4109 mmput(cur_mm);
4110 }
Jens Axboe181e4482019-11-25 08:52:30 -07004111 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004112
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004113 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004114
Jens Axboe6c271ce2019-01-10 11:22:30 -07004115 return 0;
4116}
4117
Jens Axboebda52162019-09-24 13:47:15 -06004118struct io_wait_queue {
4119 struct wait_queue_entry wq;
4120 struct io_ring_ctx *ctx;
4121 unsigned to_wait;
4122 unsigned nr_timeouts;
4123};
4124
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004125static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004126{
4127 struct io_ring_ctx *ctx = iowq->ctx;
4128
4129 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004130 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004131 * started waiting. For timeouts, we always want to return to userspace,
4132 * regardless of event count.
4133 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004134 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004135 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4136}
4137
4138static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4139 int wake_flags, void *key)
4140{
4141 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4142 wq);
4143
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004144 /* use noflush == true, as we can't safely rely on locking context */
4145 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004146 return -1;
4147
4148 return autoremove_wake_function(curr, mode, wake_flags, key);
4149}
4150
Jens Axboe2b188cc2019-01-07 10:46:33 -07004151/*
4152 * Wait until events become available, if we don't already have some. The
4153 * application must reap them itself, as they reside on the shared cq ring.
4154 */
4155static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4156 const sigset_t __user *sig, size_t sigsz)
4157{
Jens Axboebda52162019-09-24 13:47:15 -06004158 struct io_wait_queue iowq = {
4159 .wq = {
4160 .private = current,
4161 .func = io_wake_function,
4162 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4163 },
4164 .ctx = ctx,
4165 .to_wait = min_events,
4166 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004167 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004168 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004169
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004170 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004171 return 0;
4172
4173 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004174#ifdef CONFIG_COMPAT
4175 if (in_compat_syscall())
4176 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004177 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004178 else
4179#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004180 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004181
Jens Axboe2b188cc2019-01-07 10:46:33 -07004182 if (ret)
4183 return ret;
4184 }
4185
Jens Axboebda52162019-09-24 13:47:15 -06004186 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004187 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004188 do {
4189 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4190 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004191 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004192 break;
4193 schedule();
4194 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004195 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004196 break;
4197 }
4198 } while (1);
4199 finish_wait(&ctx->wait, &iowq.wq);
4200
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004201 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004202
Hristo Venev75b28af2019-08-26 17:23:46 +00004203 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004204}
4205
Jens Axboe6b063142019-01-10 22:13:58 -07004206static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4207{
4208#if defined(CONFIG_UNIX)
4209 if (ctx->ring_sock) {
4210 struct sock *sock = ctx->ring_sock->sk;
4211 struct sk_buff *skb;
4212
4213 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4214 kfree_skb(skb);
4215 }
4216#else
4217 int i;
4218
Jens Axboe65e19f52019-10-26 07:20:21 -06004219 for (i = 0; i < ctx->nr_user_files; i++) {
4220 struct file *file;
4221
4222 file = io_file_from_index(ctx, i);
4223 if (file)
4224 fput(file);
4225 }
Jens Axboe6b063142019-01-10 22:13:58 -07004226#endif
4227}
4228
4229static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4230{
Jens Axboe65e19f52019-10-26 07:20:21 -06004231 unsigned nr_tables, i;
4232
4233 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004234 return -ENXIO;
4235
4236 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004237 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4238 for (i = 0; i < nr_tables; i++)
4239 kfree(ctx->file_table[i].files);
4240 kfree(ctx->file_table);
4241 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004242 ctx->nr_user_files = 0;
4243 return 0;
4244}
4245
Jens Axboe6c271ce2019-01-10 11:22:30 -07004246static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4247{
4248 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004249 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004250 /*
4251 * The park is a bit of a work-around, without it we get
4252 * warning spews on shutdown with SQPOLL set and affinity
4253 * set to a single CPU.
4254 */
Jens Axboe06058632019-04-13 09:26:03 -06004255 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004256 kthread_stop(ctx->sqo_thread);
4257 ctx->sqo_thread = NULL;
4258 }
4259}
4260
Jens Axboe6b063142019-01-10 22:13:58 -07004261static void io_finish_async(struct io_ring_ctx *ctx)
4262{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004263 io_sq_thread_stop(ctx);
4264
Jens Axboe561fb042019-10-24 07:25:42 -06004265 if (ctx->io_wq) {
4266 io_wq_destroy(ctx->io_wq);
4267 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004268 }
4269}
4270
4271#if defined(CONFIG_UNIX)
4272static void io_destruct_skb(struct sk_buff *skb)
4273{
4274 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
4275
Jens Axboe561fb042019-10-24 07:25:42 -06004276 if (ctx->io_wq)
4277 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06004278
Jens Axboe6b063142019-01-10 22:13:58 -07004279 unix_destruct_scm(skb);
4280}
4281
4282/*
4283 * Ensure the UNIX gc is aware of our file set, so we are certain that
4284 * the io_uring can be safely unregistered on process exit, even if we have
4285 * loops in the file referencing.
4286 */
4287static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4288{
4289 struct sock *sk = ctx->ring_sock->sk;
4290 struct scm_fp_list *fpl;
4291 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004292 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004293
4294 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4295 unsigned long inflight = ctx->user->unix_inflight + nr;
4296
4297 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4298 return -EMFILE;
4299 }
4300
4301 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4302 if (!fpl)
4303 return -ENOMEM;
4304
4305 skb = alloc_skb(0, GFP_KERNEL);
4306 if (!skb) {
4307 kfree(fpl);
4308 return -ENOMEM;
4309 }
4310
4311 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004312
Jens Axboe08a45172019-10-03 08:11:03 -06004313 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004314 fpl->user = get_uid(ctx->user);
4315 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004316 struct file *file = io_file_from_index(ctx, i + offset);
4317
4318 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004319 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004320 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004321 unix_inflight(fpl->user, fpl->fp[nr_files]);
4322 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004323 }
4324
Jens Axboe08a45172019-10-03 08:11:03 -06004325 if (nr_files) {
4326 fpl->max = SCM_MAX_FD;
4327 fpl->count = nr_files;
4328 UNIXCB(skb).fp = fpl;
4329 skb->destructor = io_destruct_skb;
4330 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4331 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004332
Jens Axboe08a45172019-10-03 08:11:03 -06004333 for (i = 0; i < nr_files; i++)
4334 fput(fpl->fp[i]);
4335 } else {
4336 kfree_skb(skb);
4337 kfree(fpl);
4338 }
Jens Axboe6b063142019-01-10 22:13:58 -07004339
4340 return 0;
4341}
4342
4343/*
4344 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4345 * causes regular reference counting to break down. We rely on the UNIX
4346 * garbage collection to take care of this problem for us.
4347 */
4348static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4349{
4350 unsigned left, total;
4351 int ret = 0;
4352
4353 total = 0;
4354 left = ctx->nr_user_files;
4355 while (left) {
4356 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004357
4358 ret = __io_sqe_files_scm(ctx, this_files, total);
4359 if (ret)
4360 break;
4361 left -= this_files;
4362 total += this_files;
4363 }
4364
4365 if (!ret)
4366 return 0;
4367
4368 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004369 struct file *file = io_file_from_index(ctx, total);
4370
4371 if (file)
4372 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004373 total++;
4374 }
4375
4376 return ret;
4377}
4378#else
4379static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4380{
4381 return 0;
4382}
4383#endif
4384
Jens Axboe65e19f52019-10-26 07:20:21 -06004385static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4386 unsigned nr_files)
4387{
4388 int i;
4389
4390 for (i = 0; i < nr_tables; i++) {
4391 struct fixed_file_table *table = &ctx->file_table[i];
4392 unsigned this_files;
4393
4394 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4395 table->files = kcalloc(this_files, sizeof(struct file *),
4396 GFP_KERNEL);
4397 if (!table->files)
4398 break;
4399 nr_files -= this_files;
4400 }
4401
4402 if (i == nr_tables)
4403 return 0;
4404
4405 for (i = 0; i < nr_tables; i++) {
4406 struct fixed_file_table *table = &ctx->file_table[i];
4407 kfree(table->files);
4408 }
4409 return 1;
4410}
4411
Jens Axboe6b063142019-01-10 22:13:58 -07004412static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
4413 unsigned nr_args)
4414{
4415 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06004416 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07004417 int fd, ret = 0;
4418 unsigned i;
4419
Jens Axboe65e19f52019-10-26 07:20:21 -06004420 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004421 return -EBUSY;
4422 if (!nr_args)
4423 return -EINVAL;
4424 if (nr_args > IORING_MAX_FIXED_FILES)
4425 return -EMFILE;
4426
Jens Axboe65e19f52019-10-26 07:20:21 -06004427 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
4428 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
4429 GFP_KERNEL);
4430 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004431 return -ENOMEM;
4432
Jens Axboe65e19f52019-10-26 07:20:21 -06004433 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
4434 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07004435 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06004436 return -ENOMEM;
4437 }
4438
Jens Axboe08a45172019-10-03 08:11:03 -06004439 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004440 struct fixed_file_table *table;
4441 unsigned index;
4442
Jens Axboe6b063142019-01-10 22:13:58 -07004443 ret = -EFAULT;
4444 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
4445 break;
Jens Axboe08a45172019-10-03 08:11:03 -06004446 /* allow sparse sets */
4447 if (fd == -1) {
4448 ret = 0;
4449 continue;
4450 }
Jens Axboe6b063142019-01-10 22:13:58 -07004451
Jens Axboe65e19f52019-10-26 07:20:21 -06004452 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4453 index = i & IORING_FILE_TABLE_MASK;
4454 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07004455
4456 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06004457 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07004458 break;
4459 /*
4460 * Don't allow io_uring instances to be registered. If UNIX
4461 * isn't enabled, then this causes a reference cycle and this
4462 * instance can never get freed. If UNIX is enabled we'll
4463 * handle it just fine, but there's still no point in allowing
4464 * a ring fd as it doesn't support regular read/write anyway.
4465 */
Jens Axboe65e19f52019-10-26 07:20:21 -06004466 if (table->files[index]->f_op == &io_uring_fops) {
4467 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07004468 break;
4469 }
Jens Axboe6b063142019-01-10 22:13:58 -07004470 ret = 0;
4471 }
4472
4473 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004474 for (i = 0; i < ctx->nr_user_files; i++) {
4475 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07004476
Jens Axboe65e19f52019-10-26 07:20:21 -06004477 file = io_file_from_index(ctx, i);
4478 if (file)
4479 fput(file);
4480 }
4481 for (i = 0; i < nr_tables; i++)
4482 kfree(ctx->file_table[i].files);
4483
4484 kfree(ctx->file_table);
4485 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004486 ctx->nr_user_files = 0;
4487 return ret;
4488 }
4489
4490 ret = io_sqe_files_scm(ctx);
4491 if (ret)
4492 io_sqe_files_unregister(ctx);
4493
4494 return ret;
4495}
4496
Jens Axboec3a31e62019-10-03 13:59:56 -06004497static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
4498{
4499#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06004500 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06004501 struct sock *sock = ctx->ring_sock->sk;
4502 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4503 struct sk_buff *skb;
4504 int i;
4505
4506 __skb_queue_head_init(&list);
4507
4508 /*
4509 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4510 * remove this entry and rearrange the file array.
4511 */
4512 skb = skb_dequeue(head);
4513 while (skb) {
4514 struct scm_fp_list *fp;
4515
4516 fp = UNIXCB(skb).fp;
4517 for (i = 0; i < fp->count; i++) {
4518 int left;
4519
4520 if (fp->fp[i] != file)
4521 continue;
4522
4523 unix_notinflight(fp->user, fp->fp[i]);
4524 left = fp->count - 1 - i;
4525 if (left) {
4526 memmove(&fp->fp[i], &fp->fp[i + 1],
4527 left * sizeof(struct file *));
4528 }
4529 fp->count--;
4530 if (!fp->count) {
4531 kfree_skb(skb);
4532 skb = NULL;
4533 } else {
4534 __skb_queue_tail(&list, skb);
4535 }
4536 fput(file);
4537 file = NULL;
4538 break;
4539 }
4540
4541 if (!file)
4542 break;
4543
4544 __skb_queue_tail(&list, skb);
4545
4546 skb = skb_dequeue(head);
4547 }
4548
4549 if (skb_peek(&list)) {
4550 spin_lock_irq(&head->lock);
4551 while ((skb = __skb_dequeue(&list)) != NULL)
4552 __skb_queue_tail(head, skb);
4553 spin_unlock_irq(&head->lock);
4554 }
4555#else
Jens Axboe65e19f52019-10-26 07:20:21 -06004556 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06004557#endif
4558}
4559
4560static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4561 int index)
4562{
4563#if defined(CONFIG_UNIX)
4564 struct sock *sock = ctx->ring_sock->sk;
4565 struct sk_buff_head *head = &sock->sk_receive_queue;
4566 struct sk_buff *skb;
4567
4568 /*
4569 * See if we can merge this file into an existing skb SCM_RIGHTS
4570 * file set. If there's no room, fall back to allocating a new skb
4571 * and filling it in.
4572 */
4573 spin_lock_irq(&head->lock);
4574 skb = skb_peek(head);
4575 if (skb) {
4576 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4577
4578 if (fpl->count < SCM_MAX_FD) {
4579 __skb_unlink(skb, head);
4580 spin_unlock_irq(&head->lock);
4581 fpl->fp[fpl->count] = get_file(file);
4582 unix_inflight(fpl->user, fpl->fp[fpl->count]);
4583 fpl->count++;
4584 spin_lock_irq(&head->lock);
4585 __skb_queue_head(head, skb);
4586 } else {
4587 skb = NULL;
4588 }
4589 }
4590 spin_unlock_irq(&head->lock);
4591
4592 if (skb) {
4593 fput(file);
4594 return 0;
4595 }
4596
4597 return __io_sqe_files_scm(ctx, 1, index);
4598#else
4599 return 0;
4600#endif
4601}
4602
4603static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
4604 unsigned nr_args)
4605{
4606 struct io_uring_files_update up;
4607 __s32 __user *fds;
4608 int fd, i, err;
4609 __u32 done;
4610
Jens Axboe65e19f52019-10-26 07:20:21 -06004611 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06004612 return -ENXIO;
4613 if (!nr_args)
4614 return -EINVAL;
4615 if (copy_from_user(&up, arg, sizeof(up)))
4616 return -EFAULT;
Eugene Syromiatnikov1292e972020-01-15 17:35:38 +01004617 if (up.resv)
4618 return -EINVAL;
Jens Axboec3a31e62019-10-03 13:59:56 -06004619 if (check_add_overflow(up.offset, nr_args, &done))
4620 return -EOVERFLOW;
4621 if (done > ctx->nr_user_files)
4622 return -EINVAL;
4623
4624 done = 0;
Eugene Syromiatnikov1292e972020-01-15 17:35:38 +01004625 fds = u64_to_user_ptr(up.fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06004626 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004627 struct fixed_file_table *table;
4628 unsigned index;
4629
Jens Axboec3a31e62019-10-03 13:59:56 -06004630 err = 0;
4631 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
4632 err = -EFAULT;
4633 break;
4634 }
4635 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004636 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4637 index = i & IORING_FILE_TABLE_MASK;
4638 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06004639 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06004640 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06004641 }
4642 if (fd != -1) {
4643 struct file *file;
4644
4645 file = fget(fd);
4646 if (!file) {
4647 err = -EBADF;
4648 break;
4649 }
4650 /*
4651 * Don't allow io_uring instances to be registered. If
4652 * UNIX isn't enabled, then this causes a reference
4653 * cycle and this instance can never get freed. If UNIX
4654 * is enabled we'll handle it just fine, but there's
4655 * still no point in allowing a ring fd as it doesn't
4656 * support regular read/write anyway.
4657 */
4658 if (file->f_op == &io_uring_fops) {
4659 fput(file);
4660 err = -EBADF;
4661 break;
4662 }
Jens Axboe65e19f52019-10-26 07:20:21 -06004663 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06004664 err = io_sqe_file_register(ctx, file, i);
4665 if (err)
4666 break;
4667 }
4668 nr_args--;
4669 done++;
4670 up.offset++;
4671 }
4672
4673 return done ? done : err;
4674}
4675
Jens Axboe7d723062019-11-12 22:31:31 -07004676static void io_put_work(struct io_wq_work *work)
4677{
4678 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4679
4680 io_put_req(req);
4681}
4682
4683static void io_get_work(struct io_wq_work *work)
4684{
4685 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4686
4687 refcount_inc(&req->refs);
4688}
4689
Jens Axboe6c271ce2019-01-10 11:22:30 -07004690static int io_sq_offload_start(struct io_ring_ctx *ctx,
4691 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004692{
Jens Axboe576a3472019-11-25 08:49:20 -07004693 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06004694 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004695 int ret;
4696
Jens Axboe6c271ce2019-01-10 11:22:30 -07004697 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004698 mmgrab(current->mm);
4699 ctx->sqo_mm = current->mm;
4700
Jens Axboe6c271ce2019-01-10 11:22:30 -07004701 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06004702 ret = -EPERM;
4703 if (!capable(CAP_SYS_ADMIN))
4704 goto err;
4705
Jens Axboe917257d2019-04-13 09:28:55 -06004706 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
4707 if (!ctx->sq_thread_idle)
4708 ctx->sq_thread_idle = HZ;
4709
Jens Axboe6c271ce2019-01-10 11:22:30 -07004710 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06004711 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004712
Jens Axboe917257d2019-04-13 09:28:55 -06004713 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06004714 if (cpu >= nr_cpu_ids)
4715 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08004716 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06004717 goto err;
4718
Jens Axboe6c271ce2019-01-10 11:22:30 -07004719 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
4720 ctx, cpu,
4721 "io_uring-sq");
4722 } else {
4723 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4724 "io_uring-sq");
4725 }
4726 if (IS_ERR(ctx->sqo_thread)) {
4727 ret = PTR_ERR(ctx->sqo_thread);
4728 ctx->sqo_thread = NULL;
4729 goto err;
4730 }
4731 wake_up_process(ctx->sqo_thread);
4732 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4733 /* Can't have SQ_AFF without SQPOLL */
4734 ret = -EINVAL;
4735 goto err;
4736 }
4737
Jens Axboe576a3472019-11-25 08:49:20 -07004738 data.mm = ctx->sqo_mm;
4739 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004740 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004741 data.get_work = io_get_work;
4742 data.put_work = io_put_work;
4743
Jens Axboe561fb042019-10-24 07:25:42 -06004744 /* Do QD, or 4 * CPUS, whatever is smallest */
4745 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004746 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004747 if (IS_ERR(ctx->io_wq)) {
4748 ret = PTR_ERR(ctx->io_wq);
4749 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004750 goto err;
4751 }
4752
4753 return 0;
4754err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004755 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004756 mmdrop(ctx->sqo_mm);
4757 ctx->sqo_mm = NULL;
4758 return ret;
4759}
4760
4761static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4762{
4763 atomic_long_sub(nr_pages, &user->locked_vm);
4764}
4765
4766static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4767{
4768 unsigned long page_limit, cur_pages, new_pages;
4769
4770 /* Don't allow more pages than we can safely lock */
4771 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4772
4773 do {
4774 cur_pages = atomic_long_read(&user->locked_vm);
4775 new_pages = cur_pages + nr_pages;
4776 if (new_pages > page_limit)
4777 return -ENOMEM;
4778 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4779 new_pages) != cur_pages);
4780
4781 return 0;
4782}
4783
4784static void io_mem_free(void *ptr)
4785{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004786 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004787
Mark Rutland52e04ef2019-04-30 17:30:21 +01004788 if (!ptr)
4789 return;
4790
4791 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004792 if (put_page_testzero(page))
4793 free_compound_page(page);
4794}
4795
4796static void *io_mem_alloc(size_t size)
4797{
4798 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4799 __GFP_NORETRY;
4800
4801 return (void *) __get_free_pages(gfp_flags, get_order(size));
4802}
4803
Hristo Venev75b28af2019-08-26 17:23:46 +00004804static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4805 size_t *sq_offset)
4806{
4807 struct io_rings *rings;
4808 size_t off, sq_array_size;
4809
4810 off = struct_size(rings, cqes, cq_entries);
4811 if (off == SIZE_MAX)
4812 return SIZE_MAX;
4813
4814#ifdef CONFIG_SMP
4815 off = ALIGN(off, SMP_CACHE_BYTES);
4816 if (off == 0)
4817 return SIZE_MAX;
4818#endif
4819
4820 sq_array_size = array_size(sizeof(u32), sq_entries);
4821 if (sq_array_size == SIZE_MAX)
4822 return SIZE_MAX;
4823
4824 if (check_add_overflow(off, sq_array_size, &off))
4825 return SIZE_MAX;
4826
4827 if (sq_offset)
4828 *sq_offset = off;
4829
4830 return off;
4831}
4832
Jens Axboe2b188cc2019-01-07 10:46:33 -07004833static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4834{
Hristo Venev75b28af2019-08-26 17:23:46 +00004835 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004836
Hristo Venev75b28af2019-08-26 17:23:46 +00004837 pages = (size_t)1 << get_order(
4838 rings_size(sq_entries, cq_entries, NULL));
4839 pages += (size_t)1 << get_order(
4840 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004841
Hristo Venev75b28af2019-08-26 17:23:46 +00004842 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004843}
4844
Jens Axboeedafcce2019-01-09 09:16:05 -07004845static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4846{
4847 int i, j;
4848
4849 if (!ctx->user_bufs)
4850 return -ENXIO;
4851
4852 for (i = 0; i < ctx->nr_user_bufs; i++) {
4853 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4854
4855 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004856 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004857
4858 if (ctx->account_mem)
4859 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004860 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004861 imu->nr_bvecs = 0;
4862 }
4863
4864 kfree(ctx->user_bufs);
4865 ctx->user_bufs = NULL;
4866 ctx->nr_user_bufs = 0;
4867 return 0;
4868}
4869
4870static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4871 void __user *arg, unsigned index)
4872{
4873 struct iovec __user *src;
4874
4875#ifdef CONFIG_COMPAT
4876 if (ctx->compat) {
4877 struct compat_iovec __user *ciovs;
4878 struct compat_iovec ciov;
4879
4880 ciovs = (struct compat_iovec __user *) arg;
4881 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4882 return -EFAULT;
4883
Jens Axboed55e5f52019-12-11 16:12:15 -07004884 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07004885 dst->iov_len = ciov.iov_len;
4886 return 0;
4887 }
4888#endif
4889 src = (struct iovec __user *) arg;
4890 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4891 return -EFAULT;
4892 return 0;
4893}
4894
4895static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4896 unsigned nr_args)
4897{
4898 struct vm_area_struct **vmas = NULL;
4899 struct page **pages = NULL;
4900 int i, j, got_pages = 0;
4901 int ret = -EINVAL;
4902
4903 if (ctx->user_bufs)
4904 return -EBUSY;
4905 if (!nr_args || nr_args > UIO_MAXIOV)
4906 return -EINVAL;
4907
4908 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4909 GFP_KERNEL);
4910 if (!ctx->user_bufs)
4911 return -ENOMEM;
4912
4913 for (i = 0; i < nr_args; i++) {
4914 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4915 unsigned long off, start, end, ubuf;
4916 int pret, nr_pages;
4917 struct iovec iov;
4918 size_t size;
4919
4920 ret = io_copy_iov(ctx, &iov, arg, i);
4921 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004922 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004923
4924 /*
4925 * Don't impose further limits on the size and buffer
4926 * constraints here, we'll -EINVAL later when IO is
4927 * submitted if they are wrong.
4928 */
4929 ret = -EFAULT;
4930 if (!iov.iov_base || !iov.iov_len)
4931 goto err;
4932
4933 /* arbitrary limit, but we need something */
4934 if (iov.iov_len > SZ_1G)
4935 goto err;
4936
4937 ubuf = (unsigned long) iov.iov_base;
4938 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4939 start = ubuf >> PAGE_SHIFT;
4940 nr_pages = end - start;
4941
4942 if (ctx->account_mem) {
4943 ret = io_account_mem(ctx->user, nr_pages);
4944 if (ret)
4945 goto err;
4946 }
4947
4948 ret = 0;
4949 if (!pages || nr_pages > got_pages) {
4950 kfree(vmas);
4951 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004952 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004953 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004954 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004955 sizeof(struct vm_area_struct *),
4956 GFP_KERNEL);
4957 if (!pages || !vmas) {
4958 ret = -ENOMEM;
4959 if (ctx->account_mem)
4960 io_unaccount_mem(ctx->user, nr_pages);
4961 goto err;
4962 }
4963 got_pages = nr_pages;
4964 }
4965
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004966 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004967 GFP_KERNEL);
4968 ret = -ENOMEM;
4969 if (!imu->bvec) {
4970 if (ctx->account_mem)
4971 io_unaccount_mem(ctx->user, nr_pages);
4972 goto err;
4973 }
4974
4975 ret = 0;
4976 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004977 pret = get_user_pages(ubuf, nr_pages,
4978 FOLL_WRITE | FOLL_LONGTERM,
4979 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004980 if (pret == nr_pages) {
4981 /* don't support file backed memory */
4982 for (j = 0; j < nr_pages; j++) {
4983 struct vm_area_struct *vma = vmas[j];
4984
4985 if (vma->vm_file &&
4986 !is_file_hugepages(vma->vm_file)) {
4987 ret = -EOPNOTSUPP;
4988 break;
4989 }
4990 }
4991 } else {
4992 ret = pret < 0 ? pret : -EFAULT;
4993 }
4994 up_read(&current->mm->mmap_sem);
4995 if (ret) {
4996 /*
4997 * if we did partial map, or found file backed vmas,
4998 * release any pages we did get
4999 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005000 if (pret > 0)
5001 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005002 if (ctx->account_mem)
5003 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005004 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005005 goto err;
5006 }
5007
5008 off = ubuf & ~PAGE_MASK;
5009 size = iov.iov_len;
5010 for (j = 0; j < nr_pages; j++) {
5011 size_t vec_len;
5012
5013 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5014 imu->bvec[j].bv_page = pages[j];
5015 imu->bvec[j].bv_len = vec_len;
5016 imu->bvec[j].bv_offset = off;
5017 off = 0;
5018 size -= vec_len;
5019 }
5020 /* store original address for later verification */
5021 imu->ubuf = ubuf;
5022 imu->len = iov.iov_len;
5023 imu->nr_bvecs = nr_pages;
5024
5025 ctx->nr_user_bufs++;
5026 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005027 kvfree(pages);
5028 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005029 return 0;
5030err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005031 kvfree(pages);
5032 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005033 io_sqe_buffer_unregister(ctx);
5034 return ret;
5035}
5036
Jens Axboe9b402842019-04-11 11:45:41 -06005037static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5038{
5039 __s32 __user *fds = arg;
5040 int fd;
5041
5042 if (ctx->cq_ev_fd)
5043 return -EBUSY;
5044
5045 if (copy_from_user(&fd, fds, sizeof(*fds)))
5046 return -EFAULT;
5047
5048 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5049 if (IS_ERR(ctx->cq_ev_fd)) {
5050 int ret = PTR_ERR(ctx->cq_ev_fd);
5051 ctx->cq_ev_fd = NULL;
5052 return ret;
5053 }
5054
5055 return 0;
5056}
5057
5058static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5059{
5060 if (ctx->cq_ev_fd) {
5061 eventfd_ctx_put(ctx->cq_ev_fd);
5062 ctx->cq_ev_fd = NULL;
5063 return 0;
5064 }
5065
5066 return -ENXIO;
5067}
5068
Jens Axboe2b188cc2019-01-07 10:46:33 -07005069static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5070{
Jens Axboe6b063142019-01-10 22:13:58 -07005071 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005072 if (ctx->sqo_mm)
5073 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005074
5075 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005076 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005077 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005078 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005079
Jens Axboe2b188cc2019-01-07 10:46:33 -07005080#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005081 if (ctx->ring_sock) {
5082 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005083 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005084 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005085#endif
5086
Hristo Venev75b28af2019-08-26 17:23:46 +00005087 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005088 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005089
5090 percpu_ref_exit(&ctx->refs);
5091 if (ctx->account_mem)
5092 io_unaccount_mem(ctx->user,
5093 ring_pages(ctx->sq_entries, ctx->cq_entries));
5094 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07005095 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07005096 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07005097 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07005098 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005099 kfree(ctx);
5100}
5101
5102static __poll_t io_uring_poll(struct file *file, poll_table *wait)
5103{
5104 struct io_ring_ctx *ctx = file->private_data;
5105 __poll_t mask = 0;
5106
5107 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02005108 /*
5109 * synchronizes with barrier from wq_has_sleeper call in
5110 * io_commit_cqring
5111 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005112 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00005113 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
5114 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005115 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08005116 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005117 mask |= EPOLLIN | EPOLLRDNORM;
5118
5119 return mask;
5120}
5121
5122static int io_uring_fasync(int fd, struct file *file, int on)
5123{
5124 struct io_ring_ctx *ctx = file->private_data;
5125
5126 return fasync_helper(fd, file, on, &ctx->cq_fasync);
5127}
5128
5129static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
5130{
5131 mutex_lock(&ctx->uring_lock);
5132 percpu_ref_kill(&ctx->refs);
5133 mutex_unlock(&ctx->uring_lock);
5134
Jens Axboe5262f562019-09-17 12:26:57 -06005135 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005136 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06005137
5138 if (ctx->io_wq)
5139 io_wq_cancel_all(ctx->io_wq);
5140
Jens Axboedef596e2019-01-09 08:59:42 -07005141 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07005142 /* if we failed setting up the ctx, we might not have any rings */
5143 if (ctx->rings)
5144 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07005145 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005146 io_ring_ctx_free(ctx);
5147}
5148
5149static int io_uring_release(struct inode *inode, struct file *file)
5150{
5151 struct io_ring_ctx *ctx = file->private_data;
5152
5153 file->private_data = NULL;
5154 io_ring_ctx_wait_and_kill(ctx);
5155 return 0;
5156}
5157
Jens Axboefcb323c2019-10-24 12:39:47 -06005158static void io_uring_cancel_files(struct io_ring_ctx *ctx,
5159 struct files_struct *files)
5160{
5161 struct io_kiocb *req;
5162 DEFINE_WAIT(wait);
5163
5164 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07005165 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06005166
5167 spin_lock_irq(&ctx->inflight_lock);
5168 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07005169 if (req->work.files != files)
5170 continue;
5171 /* req is being completed, ignore */
5172 if (!refcount_inc_not_zero(&req->refs))
5173 continue;
5174 cancel_req = req;
5175 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06005176 }
Jens Axboe768134d2019-11-10 20:30:53 -07005177 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005178 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07005179 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06005180 spin_unlock_irq(&ctx->inflight_lock);
5181
Jens Axboe768134d2019-11-10 20:30:53 -07005182 /* We need to keep going until we don't find a matching req */
5183 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005184 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005185
5186 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5187 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005188 schedule();
5189 }
Jens Axboe768134d2019-11-10 20:30:53 -07005190 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005191}
5192
5193static int io_uring_flush(struct file *file, void *data)
5194{
5195 struct io_ring_ctx *ctx = file->private_data;
5196
5197 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005198 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5199 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005200 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005201 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005202 return 0;
5203}
5204
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005205static void *io_uring_validate_mmap_request(struct file *file,
5206 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005207{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005208 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005209 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005210 struct page *page;
5211 void *ptr;
5212
5213 switch (offset) {
5214 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005215 case IORING_OFF_CQ_RING:
5216 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005217 break;
5218 case IORING_OFF_SQES:
5219 ptr = ctx->sq_sqes;
5220 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005221 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005222 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005223 }
5224
5225 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005226 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005227 return ERR_PTR(-EINVAL);
5228
5229 return ptr;
5230}
5231
5232#ifdef CONFIG_MMU
5233
5234static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5235{
5236 size_t sz = vma->vm_end - vma->vm_start;
5237 unsigned long pfn;
5238 void *ptr;
5239
5240 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5241 if (IS_ERR(ptr))
5242 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005243
5244 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5245 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5246}
5247
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005248#else /* !CONFIG_MMU */
5249
5250static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5251{
5252 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5253}
5254
5255static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5256{
5257 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5258}
5259
5260static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5261 unsigned long addr, unsigned long len,
5262 unsigned long pgoff, unsigned long flags)
5263{
5264 void *ptr;
5265
5266 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5267 if (IS_ERR(ptr))
5268 return PTR_ERR(ptr);
5269
5270 return (unsigned long) ptr;
5271}
5272
5273#endif /* !CONFIG_MMU */
5274
Jens Axboe2b188cc2019-01-07 10:46:33 -07005275SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5276 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5277 size_t, sigsz)
5278{
5279 struct io_ring_ctx *ctx;
5280 long ret = -EBADF;
5281 int submitted = 0;
5282 struct fd f;
5283
Jens Axboe6c271ce2019-01-10 11:22:30 -07005284 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005285 return -EINVAL;
5286
5287 f = fdget(fd);
5288 if (!f.file)
5289 return -EBADF;
5290
5291 ret = -EOPNOTSUPP;
5292 if (f.file->f_op != &io_uring_fops)
5293 goto out_fput;
5294
5295 ret = -ENXIO;
5296 ctx = f.file->private_data;
5297 if (!percpu_ref_tryget(&ctx->refs))
5298 goto out_fput;
5299
Jens Axboe6c271ce2019-01-10 11:22:30 -07005300 /*
5301 * For SQ polling, the thread will do all submissions and completions.
5302 * Just return the requested submit count, and wake the thread if
5303 * we were asked to.
5304 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005305 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005306 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005307 if (!list_empty_careful(&ctx->cq_overflow_list))
5308 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005309 if (flags & IORING_ENTER_SQ_WAKEUP)
5310 wake_up(&ctx->sqo_wait);
5311 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005312 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005313 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005314
Jens Axboe44d28272020-01-16 19:00:24 -07005315 if (current->mm != ctx->sqo_mm ||
5316 current_cred() != ctx->creds) {
5317 ret = -EPERM;
5318 goto out;
5319 }
5320
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005321 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005322 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005323 /* already have mm, so io_submit_sqes() won't try to grab it */
5324 cur_mm = ctx->sqo_mm;
5325 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
5326 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005327 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005328
5329 if (submitted != to_submit)
5330 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005331 }
5332 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07005333 unsigned nr_events = 0;
5334
Jens Axboe2b188cc2019-01-07 10:46:33 -07005335 min_complete = min(min_complete, ctx->cq_entries);
5336
Jens Axboedef596e2019-01-09 08:59:42 -07005337 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07005338 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07005339 } else {
5340 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
5341 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005342 }
5343
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005344out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03005345 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005346out_fput:
5347 fdput(f);
5348 return submitted ? submitted : ret;
5349}
5350
5351static const struct file_operations io_uring_fops = {
5352 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06005353 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07005354 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005355#ifndef CONFIG_MMU
5356 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
5357 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
5358#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07005359 .poll = io_uring_poll,
5360 .fasync = io_uring_fasync,
5361};
5362
5363static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
5364 struct io_uring_params *p)
5365{
Hristo Venev75b28af2019-08-26 17:23:46 +00005366 struct io_rings *rings;
5367 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005368
Hristo Venev75b28af2019-08-26 17:23:46 +00005369 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
5370 if (size == SIZE_MAX)
5371 return -EOVERFLOW;
5372
5373 rings = io_mem_alloc(size);
5374 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005375 return -ENOMEM;
5376
Hristo Venev75b28af2019-08-26 17:23:46 +00005377 ctx->rings = rings;
5378 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
5379 rings->sq_ring_mask = p->sq_entries - 1;
5380 rings->cq_ring_mask = p->cq_entries - 1;
5381 rings->sq_ring_entries = p->sq_entries;
5382 rings->cq_ring_entries = p->cq_entries;
5383 ctx->sq_mask = rings->sq_ring_mask;
5384 ctx->cq_mask = rings->cq_ring_mask;
5385 ctx->sq_entries = rings->sq_ring_entries;
5386 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005387
5388 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07005389 if (size == SIZE_MAX) {
5390 io_mem_free(ctx->rings);
5391 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005392 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07005393 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005394
5395 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07005396 if (!ctx->sq_sqes) {
5397 io_mem_free(ctx->rings);
5398 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005399 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07005400 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005401
Jens Axboe2b188cc2019-01-07 10:46:33 -07005402 return 0;
5403}
5404
5405/*
5406 * Allocate an anonymous fd, this is what constitutes the application
5407 * visible backing of an io_uring instance. The application mmaps this
5408 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
5409 * we have to tie this fd to a socket for file garbage collection purposes.
5410 */
5411static int io_uring_get_fd(struct io_ring_ctx *ctx)
5412{
5413 struct file *file;
5414 int ret;
5415
5416#if defined(CONFIG_UNIX)
5417 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
5418 &ctx->ring_sock);
5419 if (ret)
5420 return ret;
5421#endif
5422
5423 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
5424 if (ret < 0)
5425 goto err;
5426
5427 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
5428 O_RDWR | O_CLOEXEC);
5429 if (IS_ERR(file)) {
5430 put_unused_fd(ret);
5431 ret = PTR_ERR(file);
5432 goto err;
5433 }
5434
5435#if defined(CONFIG_UNIX)
5436 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07005437 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005438#endif
5439 fd_install(ret, file);
5440 return ret;
5441err:
5442#if defined(CONFIG_UNIX)
5443 sock_release(ctx->ring_sock);
5444 ctx->ring_sock = NULL;
5445#endif
5446 return ret;
5447}
5448
5449static int io_uring_create(unsigned entries, struct io_uring_params *p)
5450{
5451 struct user_struct *user = NULL;
5452 struct io_ring_ctx *ctx;
5453 bool account_mem;
5454 int ret;
5455
5456 if (!entries || entries > IORING_MAX_ENTRIES)
5457 return -EINVAL;
5458
5459 /*
5460 * Use twice as many entries for the CQ ring. It's possible for the
5461 * application to drive a higher depth than the size of the SQ ring,
5462 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005463 * some flexibility in overcommitting a bit. If the application has
5464 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5465 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005466 */
5467 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005468 if (p->flags & IORING_SETUP_CQSIZE) {
5469 /*
5470 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5471 * to a power-of-two, if it isn't already. We do NOT impose
5472 * any cq vs sq ring sizing.
5473 */
5474 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5475 return -EINVAL;
5476 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5477 } else {
5478 p->cq_entries = 2 * p->sq_entries;
5479 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005480
5481 user = get_uid(current_user());
5482 account_mem = !capable(CAP_IPC_LOCK);
5483
5484 if (account_mem) {
5485 ret = io_account_mem(user,
5486 ring_pages(p->sq_entries, p->cq_entries));
5487 if (ret) {
5488 free_uid(user);
5489 return ret;
5490 }
5491 }
5492
5493 ctx = io_ring_ctx_alloc(p);
5494 if (!ctx) {
5495 if (account_mem)
5496 io_unaccount_mem(user, ring_pages(p->sq_entries,
5497 p->cq_entries));
5498 free_uid(user);
5499 return -ENOMEM;
5500 }
5501 ctx->compat = in_compat_syscall();
5502 ctx->account_mem = account_mem;
5503 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07005504 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07005505
5506 ret = io_allocate_scq_urings(ctx, p);
5507 if (ret)
5508 goto err;
5509
Jens Axboe6c271ce2019-01-10 11:22:30 -07005510 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005511 if (ret)
5512 goto err;
5513
Jens Axboe2b188cc2019-01-07 10:46:33 -07005514 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005515 p->sq_off.head = offsetof(struct io_rings, sq.head);
5516 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5517 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5518 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5519 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5520 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5521 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005522
5523 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005524 p->cq_off.head = offsetof(struct io_rings, cq.head);
5525 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5526 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5527 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5528 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5529 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06005530
Jens Axboe044c1ab2019-10-28 09:15:33 -06005531 /*
5532 * Install ring fd as the very last thing, so we don't risk someone
5533 * having closed it before we finish setup
5534 */
5535 ret = io_uring_get_fd(ctx);
5536 if (ret < 0)
5537 goto err;
5538
Jens Axboeda8c9692019-12-02 18:51:26 -07005539 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
5540 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005541 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005542 return ret;
5543err:
5544 io_ring_ctx_wait_and_kill(ctx);
5545 return ret;
5546}
5547
5548/*
5549 * Sets up an aio uring context, and returns the fd. Applications asks for a
5550 * ring size, we return the actual sq/cq ring sizes (among other things) in the
5551 * params structure passed in.
5552 */
5553static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5554{
5555 struct io_uring_params p;
5556 long ret;
5557 int i;
5558
5559 if (copy_from_user(&p, params, sizeof(p)))
5560 return -EFAULT;
5561 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5562 if (p.resv[i])
5563 return -EINVAL;
5564 }
5565
Jens Axboe6c271ce2019-01-10 11:22:30 -07005566 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06005567 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005568 return -EINVAL;
5569
5570 ret = io_uring_create(entries, &p);
5571 if (ret < 0)
5572 return ret;
5573
5574 if (copy_to_user(params, &p, sizeof(p)))
5575 return -EFAULT;
5576
5577 return ret;
5578}
5579
5580SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5581 struct io_uring_params __user *, params)
5582{
5583 return io_uring_setup(entries, params);
5584}
5585
Jens Axboeedafcce2019-01-09 09:16:05 -07005586static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
5587 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06005588 __releases(ctx->uring_lock)
5589 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07005590{
5591 int ret;
5592
Jens Axboe35fa71a2019-04-22 10:23:23 -06005593 /*
5594 * We're inside the ring mutex, if the ref is already dying, then
5595 * someone else killed the ctx or is already going through
5596 * io_uring_register().
5597 */
5598 if (percpu_ref_is_dying(&ctx->refs))
5599 return -ENXIO;
5600
Jens Axboeedafcce2019-01-09 09:16:05 -07005601 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06005602
5603 /*
5604 * Drop uring mutex before waiting for references to exit. If another
5605 * thread is currently inside io_uring_enter() it might need to grab
5606 * the uring_lock to make progress. If we hold it here across the drain
5607 * wait, then we can deadlock. It's safe to drop the mutex here, since
5608 * no new references will come in after we've killed the percpu ref.
5609 */
5610 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07005611 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06005612 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07005613
5614 switch (opcode) {
5615 case IORING_REGISTER_BUFFERS:
5616 ret = io_sqe_buffer_register(ctx, arg, nr_args);
5617 break;
5618 case IORING_UNREGISTER_BUFFERS:
5619 ret = -EINVAL;
5620 if (arg || nr_args)
5621 break;
5622 ret = io_sqe_buffer_unregister(ctx);
5623 break;
Jens Axboe6b063142019-01-10 22:13:58 -07005624 case IORING_REGISTER_FILES:
5625 ret = io_sqe_files_register(ctx, arg, nr_args);
5626 break;
5627 case IORING_UNREGISTER_FILES:
5628 ret = -EINVAL;
5629 if (arg || nr_args)
5630 break;
5631 ret = io_sqe_files_unregister(ctx);
5632 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06005633 case IORING_REGISTER_FILES_UPDATE:
5634 ret = io_sqe_files_update(ctx, arg, nr_args);
5635 break;
Jens Axboe9b402842019-04-11 11:45:41 -06005636 case IORING_REGISTER_EVENTFD:
5637 ret = -EINVAL;
5638 if (nr_args != 1)
5639 break;
5640 ret = io_eventfd_register(ctx, arg);
5641 break;
5642 case IORING_UNREGISTER_EVENTFD:
5643 ret = -EINVAL;
5644 if (arg || nr_args)
5645 break;
5646 ret = io_eventfd_unregister(ctx);
5647 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07005648 default:
5649 ret = -EINVAL;
5650 break;
5651 }
5652
5653 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07005654 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07005655 percpu_ref_reinit(&ctx->refs);
5656 return ret;
5657}
5658
5659SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
5660 void __user *, arg, unsigned int, nr_args)
5661{
5662 struct io_ring_ctx *ctx;
5663 long ret = -EBADF;
5664 struct fd f;
5665
5666 f = fdget(fd);
5667 if (!f.file)
5668 return -EBADF;
5669
5670 ret = -EOPNOTSUPP;
5671 if (f.file->f_op != &io_uring_fops)
5672 goto out_fput;
5673
5674 ctx = f.file->private_data;
5675
5676 mutex_lock(&ctx->uring_lock);
5677 ret = __io_uring_register(ctx, opcode, arg, nr_args);
5678 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005679 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
5680 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005681out_fput:
5682 fdput(f);
5683 return ret;
5684}
5685
Jens Axboe2b188cc2019-01-07 10:46:33 -07005686static int __init io_uring_init(void)
5687{
5688 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
5689 return 0;
5690};
5691__initcall(io_uring_init);