blob: 9ffcfdc6382b5e732ba5ec1ca8ce1db8b17ee4e3 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070059#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070073#include <linux/namei.h>
74#include <linux/fsnotify.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070075
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020076#define CREATE_TRACE_POINTS
77#include <trace/events/io_uring.h>
78
Jens Axboe2b188cc2019-01-07 10:46:33 -070079#include <uapi/linux/io_uring.h>
80
81#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060082#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070083
Daniel Xu5277dea2019-09-14 14:23:45 -070084#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060085#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060086
87/*
88 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
89 */
90#define IORING_FILE_TABLE_SHIFT 9
91#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
92#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
93#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070094
95struct io_uring {
96 u32 head ____cacheline_aligned_in_smp;
97 u32 tail ____cacheline_aligned_in_smp;
98};
99
Stefan Bühler1e84b972019-04-24 23:54:16 +0200100/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000101 * This data is shared with the application through the mmap at offsets
102 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200103 *
104 * The offsets to the member fields are published through struct
105 * io_sqring_offsets when calling io_uring_setup.
106 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000107struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200108 /*
109 * Head and tail offsets into the ring; the offsets need to be
110 * masked to get valid indices.
111 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000112 * The kernel controls head of the sq ring and the tail of the cq ring,
113 * and the application controls tail of the sq ring and the head of the
114 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200117 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 * ring_entries - 1)
120 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000121 u32 sq_ring_mask, cq_ring_mask;
122 /* Ring sizes (constant, power of 2) */
123 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
125 * Number of invalid entries dropped by the kernel due to
126 * invalid index stored in array
127 *
128 * Written by the kernel, shouldn't be modified by the
129 * application (i.e. get number of "new events" by comparing to
130 * cached value).
131 *
132 * After a new SQ head value was read by the application this
133 * counter includes all submissions that were dropped reaching
134 * the new SQ head (and possibly more).
135 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000136 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200137 /*
138 * Runtime flags
139 *
140 * Written by the kernel, shouldn't be modified by the
141 * application.
142 *
143 * The application needs a full memory barrier before checking
144 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200147 /*
148 * Number of completion events lost because the queue was full;
149 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800150 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 * the completion queue.
152 *
153 * Written by the kernel, shouldn't be modified by the
154 * application (i.e. get number of "new events" by comparing to
155 * cached value).
156 *
157 * As completion events come in out of order this counter is not
158 * ordered with any other data.
159 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000160 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200161 /*
162 * Ring buffer of completion events.
163 *
164 * The kernel writes completion events fresh every time they are
165 * produced, so the application is allowed to modify pending
166 * entries.
167 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000168 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700169};
170
Jens Axboeedafcce2019-01-09 09:16:05 -0700171struct io_mapped_ubuf {
172 u64 ubuf;
173 size_t len;
174 struct bio_vec *bvec;
175 unsigned int nr_bvecs;
176};
177
Jens Axboe65e19f52019-10-26 07:20:21 -0600178struct fixed_file_table {
179 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700180};
181
Jens Axboe05f3fb32019-12-09 11:22:50 -0700182enum {
183 FFD_F_ATOMIC,
184};
185
186struct fixed_file_data {
187 struct fixed_file_table *table;
188 struct io_ring_ctx *ctx;
189
190 struct percpu_ref refs;
191 struct llist_head put_llist;
192 unsigned long state;
193 struct work_struct ref_work;
194 struct completion done;
195};
196
Jens Axboe2b188cc2019-01-07 10:46:33 -0700197struct io_ring_ctx {
198 struct {
199 struct percpu_ref refs;
200 } ____cacheline_aligned_in_smp;
201
202 struct {
203 unsigned int flags;
204 bool compat;
205 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700206 bool cq_overflow_flushed;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300207 bool drain_next;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208
Hristo Venev75b28af2019-08-26 17:23:46 +0000209 /*
210 * Ring buffer of indices into array of io_uring_sqe, which is
211 * mmapped by the application using the IORING_OFF_SQES offset.
212 *
213 * This indirection could e.g. be used to assign fixed
214 * io_uring_sqe entries to operations and only submit them to
215 * the queue when needed.
216 *
217 * The kernel modifies neither the indices array nor the entries
218 * array.
219 */
220 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 unsigned cached_sq_head;
222 unsigned sq_entries;
223 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700224 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600225 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700226 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700227 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600228
229 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600230 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700231 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700232
Jens Axboefcb323c2019-10-24 12:39:47 -0600233 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234 } ____cacheline_aligned_in_smp;
235
Hristo Venev75b28af2019-08-26 17:23:46 +0000236 struct io_rings *rings;
237
Jens Axboe2b188cc2019-01-07 10:46:33 -0700238 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600239 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700240 struct task_struct *sqo_thread; /* if using sq thread polling */
241 struct mm_struct *sqo_mm;
242 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700243
Jens Axboe6b063142019-01-10 22:13:58 -0700244 /*
245 * If used, fixed file set. Writers must ensure that ->refs is dead,
246 * readers must ensure that ->refs is alive as long as the file* is
247 * used. Only updated through io_uring_register(2).
248 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700249 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700250 unsigned nr_user_files;
251
Jens Axboeedafcce2019-01-09 09:16:05 -0700252 /* if used, fixed mapped user buffers */
253 unsigned nr_user_bufs;
254 struct io_mapped_ubuf *user_bufs;
255
Jens Axboe2b188cc2019-01-07 10:46:33 -0700256 struct user_struct *user;
257
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700258 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700259
Jens Axboe206aefd2019-11-07 18:27:42 -0700260 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
261 struct completion *completions;
262
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700263 /* if all else fails... */
264 struct io_kiocb *fallback_req;
265
Jens Axboe206aefd2019-11-07 18:27:42 -0700266#if defined(CONFIG_UNIX)
267 struct socket *ring_sock;
268#endif
269
270 struct {
271 unsigned cached_cq_tail;
272 unsigned cq_entries;
273 unsigned cq_mask;
274 atomic_t cq_timeouts;
275 struct wait_queue_head cq_wait;
276 struct fasync_struct *cq_fasync;
277 struct eventfd_ctx *cq_ev_fd;
278 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279
280 struct {
281 struct mutex uring_lock;
282 wait_queue_head_t wait;
283 } ____cacheline_aligned_in_smp;
284
285 struct {
286 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700287 bool poll_multi_file;
288 /*
289 * ->poll_list is protected by the ctx->uring_lock for
290 * io_uring instances that don't use IORING_SETUP_SQPOLL.
291 * For SQPOLL, only the single threaded io_sq_thread() will
292 * manipulate the list, hence no extra locking is needed there.
293 */
294 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700295 struct hlist_head *cancel_hash;
296 unsigned cancel_hash_bits;
Jens Axboefcb323c2019-10-24 12:39:47 -0600297
298 spinlock_t inflight_lock;
299 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700300 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700301};
302
Jens Axboe09bb8392019-03-13 12:39:28 -0600303/*
304 * First field must be the file pointer in all the
305 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
306 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700307struct io_poll_iocb {
308 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700309 union {
310 struct wait_queue_head *head;
311 u64 addr;
312 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700313 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600314 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700315 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700316 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700317};
318
Jens Axboeb5dba592019-12-11 14:02:38 -0700319struct io_close {
320 struct file *file;
321 struct file *put_file;
322 int fd;
323};
324
Jens Axboead8a48a2019-11-15 08:49:11 -0700325struct io_timeout_data {
326 struct io_kiocb *req;
327 struct hrtimer timer;
328 struct timespec64 ts;
329 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300330 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700331};
332
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700333struct io_accept {
334 struct file *file;
335 struct sockaddr __user *addr;
336 int __user *addr_len;
337 int flags;
338};
339
340struct io_sync {
341 struct file *file;
342 loff_t len;
343 loff_t off;
344 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700345 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700346};
347
Jens Axboefbf23842019-12-17 18:45:56 -0700348struct io_cancel {
349 struct file *file;
350 u64 addr;
351};
352
Jens Axboeb29472e2019-12-17 18:50:29 -0700353struct io_timeout {
354 struct file *file;
355 u64 addr;
356 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700357 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700358};
359
Jens Axboe9adbd452019-12-20 08:45:55 -0700360struct io_rw {
361 /* NOTE: kiocb has the file as the first member, so don't do it here */
362 struct kiocb kiocb;
363 u64 addr;
364 u64 len;
365};
366
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700367struct io_connect {
368 struct file *file;
369 struct sockaddr __user *addr;
370 int addr_len;
371};
372
Jens Axboee47293f2019-12-20 08:58:21 -0700373struct io_sr_msg {
374 struct file *file;
375 struct user_msghdr __user *msg;
376 int msg_flags;
377};
378
Jens Axboe15b71ab2019-12-11 11:20:36 -0700379struct io_open {
380 struct file *file;
381 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700382 union {
383 umode_t mode;
384 unsigned mask;
385 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700386 const char __user *fname;
387 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700388 struct statx __user *buffer;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700389 int flags;
390};
391
Jens Axboe05f3fb32019-12-09 11:22:50 -0700392struct io_files_update {
393 struct file *file;
394 u64 arg;
395 u32 nr_args;
396 u32 offset;
397};
398
Jens Axboef499a022019-12-02 16:28:46 -0700399struct io_async_connect {
400 struct sockaddr_storage address;
401};
402
Jens Axboe03b12302019-12-02 18:50:25 -0700403struct io_async_msghdr {
404 struct iovec fast_iov[UIO_FASTIOV];
405 struct iovec *iov;
406 struct sockaddr __user *uaddr;
407 struct msghdr msg;
408};
409
Jens Axboef67676d2019-12-02 11:03:47 -0700410struct io_async_rw {
411 struct iovec fast_iov[UIO_FASTIOV];
412 struct iovec *iov;
413 ssize_t nr_segs;
414 ssize_t size;
415};
416
Jens Axboe15b71ab2019-12-11 11:20:36 -0700417struct io_async_open {
418 struct filename *filename;
419};
420
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700421struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700422 union {
423 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700424 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700425 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700426 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700427 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700428 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700429};
430
Jens Axboe09bb8392019-03-13 12:39:28 -0600431/*
432 * NOTE! Each of the iocb union members has the file pointer
433 * as the first entry in their struct definition. So you can
434 * access the file pointer through any of the sub-structs,
435 * or directly as just 'ki_filp' in this struct.
436 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700437struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700438 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600439 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700440 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700441 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700442 struct io_accept accept;
443 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700444 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700445 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700446 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700447 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700448 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700449 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700450 struct io_files_update files_update;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700451 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700452
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700453 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300454 struct file *ring_file;
455 int ring_fd;
456 bool has_user;
457 bool in_async;
458 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700459 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700460
461 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700462 union {
463 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700464 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700465 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600466 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700467 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700468 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200469#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700470#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700471#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700472#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200473#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
474#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600475#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700476#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800477#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300478#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600479#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600480#define REQ_F_ISREG 2048 /* regular file */
481#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700482#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800483#define REQ_F_INFLIGHT 16384 /* on inflight list */
484#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700485#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboece35a472019-12-17 08:04:44 -0700486#define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700487 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600488 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600489 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700490
Jens Axboefcb323c2019-10-24 12:39:47 -0600491 struct list_head inflight_entry;
492
Jens Axboe561fb042019-10-24 07:25:42 -0600493 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700494};
495
496#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700497#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700498
Jens Axboe9a56a232019-01-09 09:06:50 -0700499struct io_submit_state {
500 struct blk_plug plug;
501
502 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700503 * io_kiocb alloc cache
504 */
505 void *reqs[IO_IOPOLL_BATCH];
506 unsigned int free_reqs;
507 unsigned int cur_req;
508
509 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700510 * File reference cache
511 */
512 struct file *file;
513 unsigned int fd;
514 unsigned int has_refs;
515 unsigned int used_refs;
516 unsigned int ios_left;
517};
518
Jens Axboe561fb042019-10-24 07:25:42 -0600519static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700520static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800521static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800522static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700523static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700524static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700525static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
526static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700527static int __io_sqe_files_update(struct io_ring_ctx *ctx,
528 struct io_uring_files_update *ip,
529 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600530
Jens Axboe2b188cc2019-01-07 10:46:33 -0700531static struct kmem_cache *req_cachep;
532
533static const struct file_operations io_uring_fops;
534
535struct sock *io_uring_get_socket(struct file *file)
536{
537#if defined(CONFIG_UNIX)
538 if (file->f_op == &io_uring_fops) {
539 struct io_ring_ctx *ctx = file->private_data;
540
541 return ctx->ring_sock->sk;
542 }
543#endif
544 return NULL;
545}
546EXPORT_SYMBOL(io_uring_get_socket);
547
548static void io_ring_ctx_ref_free(struct percpu_ref *ref)
549{
550 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
551
Jens Axboe206aefd2019-11-07 18:27:42 -0700552 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700553}
554
555static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
556{
557 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700558 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700559
560 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
561 if (!ctx)
562 return NULL;
563
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700564 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
565 if (!ctx->fallback_req)
566 goto err;
567
Jens Axboe206aefd2019-11-07 18:27:42 -0700568 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
569 if (!ctx->completions)
570 goto err;
571
Jens Axboe78076bb2019-12-04 19:56:40 -0700572 /*
573 * Use 5 bits less than the max cq entries, that should give us around
574 * 32 entries per hash list if totally full and uniformly spread.
575 */
576 hash_bits = ilog2(p->cq_entries);
577 hash_bits -= 5;
578 if (hash_bits <= 0)
579 hash_bits = 1;
580 ctx->cancel_hash_bits = hash_bits;
581 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
582 GFP_KERNEL);
583 if (!ctx->cancel_hash)
584 goto err;
585 __hash_init(ctx->cancel_hash, 1U << hash_bits);
586
Roman Gushchin21482892019-05-07 10:01:48 -0700587 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700588 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
589 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700590
591 ctx->flags = p->flags;
592 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700593 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700594 init_completion(&ctx->completions[0]);
595 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700596 mutex_init(&ctx->uring_lock);
597 init_waitqueue_head(&ctx->wait);
598 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700599 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600600 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600601 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600602 init_waitqueue_head(&ctx->inflight_wait);
603 spin_lock_init(&ctx->inflight_lock);
604 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700605 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700606err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700607 if (ctx->fallback_req)
608 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700609 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700610 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700611 kfree(ctx);
612 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700613}
614
Bob Liu9d858b22019-11-13 18:06:25 +0800615static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600616{
Jackie Liua197f662019-11-08 08:09:12 -0700617 struct io_ring_ctx *ctx = req->ctx;
618
Jens Axboe498ccd92019-10-25 10:04:25 -0600619 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
620 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600621}
622
Bob Liu9d858b22019-11-13 18:06:25 +0800623static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600624{
Bob Liu9d858b22019-11-13 18:06:25 +0800625 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
626 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600627
Bob Liu9d858b22019-11-13 18:06:25 +0800628 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600629}
630
631static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600632{
633 struct io_kiocb *req;
634
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600635 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800636 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600637 list_del_init(&req->list);
638 return req;
639 }
640
641 return NULL;
642}
643
Jens Axboe5262f562019-09-17 12:26:57 -0600644static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
645{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600646 struct io_kiocb *req;
647
648 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700649 if (req) {
650 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
651 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800652 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700653 list_del_init(&req->list);
654 return req;
655 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600656 }
657
658 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600659}
660
Jens Axboede0617e2019-04-06 21:51:27 -0600661static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700662{
Hristo Venev75b28af2019-08-26 17:23:46 +0000663 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700664
Hristo Venev75b28af2019-08-26 17:23:46 +0000665 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700666 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000667 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700668
Jens Axboe2b188cc2019-01-07 10:46:33 -0700669 if (wq_has_sleeper(&ctx->cq_wait)) {
670 wake_up_interruptible(&ctx->cq_wait);
671 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
672 }
673 }
674}
675
Jens Axboed625c6e2019-12-17 19:53:05 -0700676static inline bool io_req_needs_user(struct io_kiocb *req)
Jens Axboe18d9be12019-09-10 09:13:05 -0600677{
Jens Axboed625c6e2019-12-17 19:53:05 -0700678 return !(req->opcode == IORING_OP_READ_FIXED ||
679 req->opcode == IORING_OP_WRITE_FIXED);
Jens Axboe561fb042019-10-24 07:25:42 -0600680}
681
Jens Axboe94ae5e72019-11-14 19:39:52 -0700682static inline bool io_prep_async_work(struct io_kiocb *req,
683 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600684{
685 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600686
Jens Axboe3529d8c2019-12-19 18:24:38 -0700687 switch (req->opcode) {
688 case IORING_OP_WRITEV:
689 case IORING_OP_WRITE_FIXED:
690 /* only regular files should be hashed for writes */
691 if (req->flags & REQ_F_ISREG)
692 do_hashed = true;
693 /* fall-through */
694 case IORING_OP_READV:
695 case IORING_OP_READ_FIXED:
696 case IORING_OP_SENDMSG:
697 case IORING_OP_RECVMSG:
698 case IORING_OP_ACCEPT:
699 case IORING_OP_POLL_ADD:
700 case IORING_OP_CONNECT:
701 /*
702 * We know REQ_F_ISREG is not set on some of these
703 * opcodes, but this enables us to keep the check in
704 * just one place.
705 */
706 if (!(req->flags & REQ_F_ISREG))
707 req->work.flags |= IO_WQ_WORK_UNBOUND;
708 break;
Jens Axboe54a91f32019-09-10 09:15:04 -0600709 }
Jens Axboe3529d8c2019-12-19 18:24:38 -0700710 if (io_req_needs_user(req))
711 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600712
Jens Axboe94ae5e72019-11-14 19:39:52 -0700713 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600714 return do_hashed;
715}
716
Jackie Liua197f662019-11-08 08:09:12 -0700717static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600718{
Jackie Liua197f662019-11-08 08:09:12 -0700719 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700720 struct io_kiocb *link;
721 bool do_hashed;
722
723 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600724
725 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
726 req->flags);
727 if (!do_hashed) {
728 io_wq_enqueue(ctx->io_wq, &req->work);
729 } else {
730 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
731 file_inode(req->file));
732 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700733
734 if (link)
735 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600736}
737
Jens Axboe5262f562019-09-17 12:26:57 -0600738static void io_kill_timeout(struct io_kiocb *req)
739{
740 int ret;
741
Jens Axboe2d283902019-12-04 11:08:05 -0700742 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600743 if (ret != -1) {
744 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600745 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700746 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800747 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600748 }
749}
750
751static void io_kill_timeouts(struct io_ring_ctx *ctx)
752{
753 struct io_kiocb *req, *tmp;
754
755 spin_lock_irq(&ctx->completion_lock);
756 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
757 io_kill_timeout(req);
758 spin_unlock_irq(&ctx->completion_lock);
759}
760
Jens Axboede0617e2019-04-06 21:51:27 -0600761static void io_commit_cqring(struct io_ring_ctx *ctx)
762{
763 struct io_kiocb *req;
764
Jens Axboe5262f562019-09-17 12:26:57 -0600765 while ((req = io_get_timeout_req(ctx)) != NULL)
766 io_kill_timeout(req);
767
Jens Axboede0617e2019-04-06 21:51:27 -0600768 __io_commit_cqring(ctx);
769
770 while ((req = io_get_deferred_req(ctx)) != NULL) {
771 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700772 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600773 }
774}
775
Jens Axboe2b188cc2019-01-07 10:46:33 -0700776static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
777{
Hristo Venev75b28af2019-08-26 17:23:46 +0000778 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700779 unsigned tail;
780
781 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200782 /*
783 * writes to the cq entry need to come after reading head; the
784 * control dependency is enough as we're using WRITE_ONCE to
785 * fill the cq entry
786 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000787 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700788 return NULL;
789
790 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000791 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700792}
793
Jens Axboe8c838782019-03-12 15:48:16 -0600794static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
795{
796 if (waitqueue_active(&ctx->wait))
797 wake_up(&ctx->wait);
798 if (waitqueue_active(&ctx->sqo_wait))
799 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600800 if (ctx->cq_ev_fd)
801 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600802}
803
Jens Axboec4a2ed72019-11-21 21:01:26 -0700804/* Returns true if there are no backlogged entries after the flush */
805static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700806{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700807 struct io_rings *rings = ctx->rings;
808 struct io_uring_cqe *cqe;
809 struct io_kiocb *req;
810 unsigned long flags;
811 LIST_HEAD(list);
812
813 if (!force) {
814 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700815 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700816 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
817 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700818 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700819 }
820
821 spin_lock_irqsave(&ctx->completion_lock, flags);
822
823 /* if force is set, the ring is going away. always drop after that */
824 if (force)
825 ctx->cq_overflow_flushed = true;
826
Jens Axboec4a2ed72019-11-21 21:01:26 -0700827 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700828 while (!list_empty(&ctx->cq_overflow_list)) {
829 cqe = io_get_cqring(ctx);
830 if (!cqe && !force)
831 break;
832
833 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
834 list);
835 list_move(&req->list, &list);
836 if (cqe) {
837 WRITE_ONCE(cqe->user_data, req->user_data);
838 WRITE_ONCE(cqe->res, req->result);
839 WRITE_ONCE(cqe->flags, 0);
840 } else {
841 WRITE_ONCE(ctx->rings->cq_overflow,
842 atomic_inc_return(&ctx->cached_cq_overflow));
843 }
844 }
845
846 io_commit_cqring(ctx);
847 spin_unlock_irqrestore(&ctx->completion_lock, flags);
848 io_cqring_ev_posted(ctx);
849
850 while (!list_empty(&list)) {
851 req = list_first_entry(&list, struct io_kiocb, list);
852 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800853 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700854 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700855
856 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700857}
858
Jens Axboe78e19bb2019-11-06 15:21:34 -0700859static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700860{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700861 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700862 struct io_uring_cqe *cqe;
863
Jens Axboe78e19bb2019-11-06 15:21:34 -0700864 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700865
Jens Axboe2b188cc2019-01-07 10:46:33 -0700866 /*
867 * If we can't get a cq entry, userspace overflowed the
868 * submission (by quite a lot). Increment the overflow count in
869 * the ring.
870 */
871 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700872 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700873 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700874 WRITE_ONCE(cqe->res, res);
875 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700876 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700877 WRITE_ONCE(ctx->rings->cq_overflow,
878 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700879 } else {
880 refcount_inc(&req->refs);
881 req->result = res;
882 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700883 }
884}
885
Jens Axboe78e19bb2019-11-06 15:21:34 -0700886static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700887{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700888 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700889 unsigned long flags;
890
891 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700892 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700893 io_commit_cqring(ctx);
894 spin_unlock_irqrestore(&ctx->completion_lock, flags);
895
Jens Axboe8c838782019-03-12 15:48:16 -0600896 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700897}
898
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700899static inline bool io_is_fallback_req(struct io_kiocb *req)
900{
901 return req == (struct io_kiocb *)
902 ((unsigned long) req->ctx->fallback_req & ~1UL);
903}
904
905static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
906{
907 struct io_kiocb *req;
908
909 req = ctx->fallback_req;
910 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
911 return req;
912
913 return NULL;
914}
915
Jens Axboe2579f912019-01-09 09:10:43 -0700916static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
917 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700918{
Jens Axboefd6fab22019-03-14 16:30:06 -0600919 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700920 struct io_kiocb *req;
921
922 if (!percpu_ref_tryget(&ctx->refs))
923 return NULL;
924
Jens Axboe2579f912019-01-09 09:10:43 -0700925 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600926 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700927 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700928 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700929 } else if (!state->free_reqs) {
930 size_t sz;
931 int ret;
932
933 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600934 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
935
936 /*
937 * Bulk alloc is all-or-nothing. If we fail to get a batch,
938 * retry single alloc to be on the safe side.
939 */
940 if (unlikely(ret <= 0)) {
941 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
942 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700943 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600944 ret = 1;
945 }
Jens Axboe2579f912019-01-09 09:10:43 -0700946 state->free_reqs = ret - 1;
947 state->cur_req = 1;
948 req = state->reqs[0];
949 } else {
950 req = state->reqs[state->cur_req];
951 state->free_reqs--;
952 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700953 }
954
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700955got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700956 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300957 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600958 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700959 req->ctx = ctx;
960 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600961 /* one is dropped after submission, the other at completion */
962 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600963 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600964 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700965 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700966fallback:
967 req = io_get_fallback_req(ctx);
968 if (req)
969 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300970 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700971 return NULL;
972}
973
Jens Axboedef596e2019-01-09 08:59:42 -0700974static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
975{
976 if (*nr) {
977 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300978 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700979 percpu_ref_put_many(&ctx->file_data->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700980 *nr = 0;
981 }
982}
983
Jens Axboe9e645e112019-05-10 16:07:28 -0600984static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700985{
Jens Axboefcb323c2019-10-24 12:39:47 -0600986 struct io_ring_ctx *ctx = req->ctx;
987
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700988 if (req->io)
989 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700990 if (req->file) {
991 if (req->flags & REQ_F_FIXED_FILE)
992 percpu_ref_put(&ctx->file_data->refs);
993 else
994 fput(req->file);
995 }
Jens Axboefcb323c2019-10-24 12:39:47 -0600996 if (req->flags & REQ_F_INFLIGHT) {
997 unsigned long flags;
998
999 spin_lock_irqsave(&ctx->inflight_lock, flags);
1000 list_del(&req->inflight_entry);
1001 if (waitqueue_active(&ctx->inflight_wait))
1002 wake_up(&ctx->inflight_wait);
1003 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1004 }
1005 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001006 if (likely(!io_is_fallback_req(req)))
1007 kmem_cache_free(req_cachep, req);
1008 else
1009 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001010}
1011
Jackie Liua197f662019-11-08 08:09:12 -07001012static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001013{
Jackie Liua197f662019-11-08 08:09:12 -07001014 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001015 int ret;
1016
Jens Axboe2d283902019-12-04 11:08:05 -07001017 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001018 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001019 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001020 io_commit_cqring(ctx);
1021 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001022 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001023 return true;
1024 }
1025
1026 return false;
1027}
1028
Jens Axboeba816ad2019-09-28 11:36:45 -06001029static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001030{
Jens Axboe2665abf2019-11-05 12:40:47 -07001031 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001032 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001033
Jens Axboe4d7dd462019-11-20 13:03:52 -07001034 /* Already got next link */
1035 if (req->flags & REQ_F_LINK_NEXT)
1036 return;
1037
Jens Axboe9e645e112019-05-10 16:07:28 -06001038 /*
1039 * The list should never be empty when we are called here. But could
1040 * potentially happen if the chain is messed up, check to be on the
1041 * safe side.
1042 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001043 while (!list_empty(&req->link_list)) {
1044 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1045 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001046
Pavel Begunkov44932332019-12-05 16:16:35 +03001047 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1048 (nxt->flags & REQ_F_TIMEOUT))) {
1049 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001050 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001051 req->flags &= ~REQ_F_LINK_TIMEOUT;
1052 continue;
1053 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001054
Pavel Begunkov44932332019-12-05 16:16:35 +03001055 list_del_init(&req->link_list);
1056 if (!list_empty(&nxt->link_list))
1057 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001058 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001059 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001060 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001061
Jens Axboe4d7dd462019-11-20 13:03:52 -07001062 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001063 if (wake_ev)
1064 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001065}
1066
1067/*
1068 * Called if REQ_F_LINK is set, and we fail the head request
1069 */
1070static void io_fail_links(struct io_kiocb *req)
1071{
Jens Axboe2665abf2019-11-05 12:40:47 -07001072 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001073 unsigned long flags;
1074
1075 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001076
1077 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001078 struct io_kiocb *link = list_first_entry(&req->link_list,
1079 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001080
Pavel Begunkov44932332019-12-05 16:16:35 +03001081 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001082 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001083
1084 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001085 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001086 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001087 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001088 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001089 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001090 }
Jens Axboe5d960722019-11-19 15:31:28 -07001091 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001092 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001093
1094 io_commit_cqring(ctx);
1095 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1096 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001097}
1098
Jens Axboe4d7dd462019-11-20 13:03:52 -07001099static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001100{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001101 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001102 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001103
Jens Axboe9e645e112019-05-10 16:07:28 -06001104 /*
1105 * If LINK is set, we have dependent requests in this chain. If we
1106 * didn't fail this request, queue the first one up, moving any other
1107 * dependencies to the next request. In case of failure, fail the rest
1108 * of the chain.
1109 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001110 if (req->flags & REQ_F_FAIL_LINK) {
1111 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001112 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1113 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001114 struct io_ring_ctx *ctx = req->ctx;
1115 unsigned long flags;
1116
1117 /*
1118 * If this is a timeout link, we could be racing with the
1119 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001120 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001121 */
1122 spin_lock_irqsave(&ctx->completion_lock, flags);
1123 io_req_link_next(req, nxt);
1124 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1125 } else {
1126 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001127 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001128}
Jens Axboe9e645e112019-05-10 16:07:28 -06001129
Jackie Liuc69f8db2019-11-09 11:00:08 +08001130static void io_free_req(struct io_kiocb *req)
1131{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001132 struct io_kiocb *nxt = NULL;
1133
1134 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001135 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001136
1137 if (nxt)
1138 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001139}
1140
Jens Axboeba816ad2019-09-28 11:36:45 -06001141/*
1142 * Drop reference to request, return next in chain (if there is one) if this
1143 * was the last reference to this request.
1144 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001145__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001146static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001147{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001148 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001149
Jens Axboee65ef562019-03-12 10:16:44 -06001150 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001151 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001152}
1153
Jens Axboe2b188cc2019-01-07 10:46:33 -07001154static void io_put_req(struct io_kiocb *req)
1155{
Jens Axboedef596e2019-01-09 08:59:42 -07001156 if (refcount_dec_and_test(&req->refs))
1157 io_free_req(req);
1158}
1159
Jens Axboe978db572019-11-14 22:39:04 -07001160/*
1161 * Must only be used if we don't need to care about links, usually from
1162 * within the completion handling itself.
1163 */
1164static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001165{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001166 /* drop both submit and complete references */
1167 if (refcount_sub_and_test(2, &req->refs))
1168 __io_free_req(req);
1169}
1170
Jens Axboe978db572019-11-14 22:39:04 -07001171static void io_double_put_req(struct io_kiocb *req)
1172{
1173 /* drop both submit and complete references */
1174 if (refcount_sub_and_test(2, &req->refs))
1175 io_free_req(req);
1176}
1177
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001178static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001179{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001180 struct io_rings *rings = ctx->rings;
1181
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001182 /*
1183 * noflush == true is from the waitqueue handler, just ensure we wake
1184 * up the task, and the next invocation will flush the entries. We
1185 * cannot safely to it from here.
1186 */
1187 if (noflush && !list_empty(&ctx->cq_overflow_list))
1188 return -1U;
1189
1190 io_cqring_overflow_flush(ctx, false);
1191
Jens Axboea3a0e432019-08-20 11:03:11 -06001192 /* See comment at the top of this file */
1193 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001194 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001195}
1196
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001197static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1198{
1199 struct io_rings *rings = ctx->rings;
1200
1201 /* make sure SQ entry isn't read before tail */
1202 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1203}
1204
Jens Axboedef596e2019-01-09 08:59:42 -07001205/*
1206 * Find and free completed poll iocbs
1207 */
1208static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1209 struct list_head *done)
1210{
1211 void *reqs[IO_IOPOLL_BATCH];
1212 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001213 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001214
Jens Axboe09bb8392019-03-13 12:39:28 -06001215 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001216 while (!list_empty(done)) {
1217 req = list_first_entry(done, struct io_kiocb, list);
1218 list_del(&req->list);
1219
Jens Axboe78e19bb2019-11-06 15:21:34 -07001220 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001221 (*nr_events)++;
1222
Jens Axboe09bb8392019-03-13 12:39:28 -06001223 if (refcount_dec_and_test(&req->refs)) {
1224 /* If we're not using fixed files, we have to pair the
1225 * completion part with the file put. Use regular
1226 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001227 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001228 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001229 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1230 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1231 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001232 reqs[to_free++] = req;
1233 if (to_free == ARRAY_SIZE(reqs))
1234 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001235 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001236 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001237 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001238 }
Jens Axboedef596e2019-01-09 08:59:42 -07001239 }
Jens Axboedef596e2019-01-09 08:59:42 -07001240
Jens Axboe09bb8392019-03-13 12:39:28 -06001241 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001242 io_free_req_many(ctx, reqs, &to_free);
1243}
1244
1245static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1246 long min)
1247{
1248 struct io_kiocb *req, *tmp;
1249 LIST_HEAD(done);
1250 bool spin;
1251 int ret;
1252
1253 /*
1254 * Only spin for completions if we don't have multiple devices hanging
1255 * off our complete list, and we're under the requested amount.
1256 */
1257 spin = !ctx->poll_multi_file && *nr_events < min;
1258
1259 ret = 0;
1260 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001261 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001262
1263 /*
1264 * Move completed entries to our local list. If we find a
1265 * request that requires polling, break out and complete
1266 * the done list first, if we have entries there.
1267 */
1268 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1269 list_move_tail(&req->list, &done);
1270 continue;
1271 }
1272 if (!list_empty(&done))
1273 break;
1274
1275 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1276 if (ret < 0)
1277 break;
1278
1279 if (ret && spin)
1280 spin = false;
1281 ret = 0;
1282 }
1283
1284 if (!list_empty(&done))
1285 io_iopoll_complete(ctx, nr_events, &done);
1286
1287 return ret;
1288}
1289
1290/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001291 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001292 * non-spinning poll check - we'll still enter the driver poll loop, but only
1293 * as a non-spinning completion check.
1294 */
1295static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1296 long min)
1297{
Jens Axboe08f54392019-08-21 22:19:11 -06001298 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001299 int ret;
1300
1301 ret = io_do_iopoll(ctx, nr_events, min);
1302 if (ret < 0)
1303 return ret;
1304 if (!min || *nr_events >= min)
1305 return 0;
1306 }
1307
1308 return 1;
1309}
1310
1311/*
1312 * We can't just wait for polled events to come to us, we have to actively
1313 * find and complete them.
1314 */
1315static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1316{
1317 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1318 return;
1319
1320 mutex_lock(&ctx->uring_lock);
1321 while (!list_empty(&ctx->poll_list)) {
1322 unsigned int nr_events = 0;
1323
1324 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001325
1326 /*
1327 * Ensure we allow local-to-the-cpu processing to take place,
1328 * in this case we need to ensure that we reap all events.
1329 */
1330 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001331 }
1332 mutex_unlock(&ctx->uring_lock);
1333}
1334
Jens Axboe2b2ed972019-10-25 10:06:15 -06001335static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1336 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001337{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001338 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001339
1340 do {
1341 int tmin = 0;
1342
Jens Axboe500f9fb2019-08-19 12:15:59 -06001343 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001344 * Don't enter poll loop if we already have events pending.
1345 * If we do, we can potentially be spinning for commands that
1346 * already triggered a CQE (eg in error).
1347 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001348 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001349 break;
1350
1351 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001352 * If a submit got punted to a workqueue, we can have the
1353 * application entering polling for a command before it gets
1354 * issued. That app will hold the uring_lock for the duration
1355 * of the poll right here, so we need to take a breather every
1356 * now and then to ensure that the issue has a chance to add
1357 * the poll to the issued list. Otherwise we can spin here
1358 * forever, while the workqueue is stuck trying to acquire the
1359 * very same mutex.
1360 */
1361 if (!(++iters & 7)) {
1362 mutex_unlock(&ctx->uring_lock);
1363 mutex_lock(&ctx->uring_lock);
1364 }
1365
Jens Axboedef596e2019-01-09 08:59:42 -07001366 if (*nr_events < min)
1367 tmin = min - *nr_events;
1368
1369 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1370 if (ret <= 0)
1371 break;
1372 ret = 0;
1373 } while (min && !*nr_events && !need_resched());
1374
Jens Axboe2b2ed972019-10-25 10:06:15 -06001375 return ret;
1376}
1377
1378static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1379 long min)
1380{
1381 int ret;
1382
1383 /*
1384 * We disallow the app entering submit/complete with polling, but we
1385 * still need to lock the ring to prevent racing with polled issue
1386 * that got punted to a workqueue.
1387 */
1388 mutex_lock(&ctx->uring_lock);
1389 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001390 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001391 return ret;
1392}
1393
Jens Axboe491381ce2019-10-17 09:20:46 -06001394static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001395{
Jens Axboe491381ce2019-10-17 09:20:46 -06001396 /*
1397 * Tell lockdep we inherited freeze protection from submission
1398 * thread.
1399 */
1400 if (req->flags & REQ_F_ISREG) {
1401 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001402
Jens Axboe491381ce2019-10-17 09:20:46 -06001403 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001404 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001405 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001406}
1407
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001408static inline void req_set_fail_links(struct io_kiocb *req)
1409{
1410 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1411 req->flags |= REQ_F_FAIL_LINK;
1412}
1413
Jens Axboeba816ad2019-09-28 11:36:45 -06001414static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001415{
Jens Axboe9adbd452019-12-20 08:45:55 -07001416 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001417
Jens Axboe491381ce2019-10-17 09:20:46 -06001418 if (kiocb->ki_flags & IOCB_WRITE)
1419 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001421 if (res != req->result)
1422 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001423 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001424}
1425
1426static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1427{
Jens Axboe9adbd452019-12-20 08:45:55 -07001428 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001429
1430 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001431 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001432}
1433
Jens Axboeba816ad2019-09-28 11:36:45 -06001434static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1435{
Jens Axboe9adbd452019-12-20 08:45:55 -07001436 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001437 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001438
1439 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001440 io_put_req_find_next(req, &nxt);
1441
1442 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001443}
1444
Jens Axboedef596e2019-01-09 08:59:42 -07001445static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1446{
Jens Axboe9adbd452019-12-20 08:45:55 -07001447 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001448
Jens Axboe491381ce2019-10-17 09:20:46 -06001449 if (kiocb->ki_flags & IOCB_WRITE)
1450 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001451
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001452 if (res != req->result)
1453 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001454 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001455 if (res != -EAGAIN)
1456 req->flags |= REQ_F_IOPOLL_COMPLETED;
1457}
1458
1459/*
1460 * After the iocb has been issued, it's safe to be found on the poll list.
1461 * Adding the kiocb to the list AFTER submission ensures that we don't
1462 * find it from a io_iopoll_getevents() thread before the issuer is done
1463 * accessing the kiocb cookie.
1464 */
1465static void io_iopoll_req_issued(struct io_kiocb *req)
1466{
1467 struct io_ring_ctx *ctx = req->ctx;
1468
1469 /*
1470 * Track whether we have multiple files in our lists. This will impact
1471 * how we do polling eventually, not spinning if we're on potentially
1472 * different devices.
1473 */
1474 if (list_empty(&ctx->poll_list)) {
1475 ctx->poll_multi_file = false;
1476 } else if (!ctx->poll_multi_file) {
1477 struct io_kiocb *list_req;
1478
1479 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1480 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001481 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001482 ctx->poll_multi_file = true;
1483 }
1484
1485 /*
1486 * For fast devices, IO may have already completed. If it has, add
1487 * it to the front so we find it first.
1488 */
1489 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1490 list_add(&req->list, &ctx->poll_list);
1491 else
1492 list_add_tail(&req->list, &ctx->poll_list);
1493}
1494
Jens Axboe3d6770f2019-04-13 11:50:54 -06001495static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001496{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001497 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001498 int diff = state->has_refs - state->used_refs;
1499
1500 if (diff)
1501 fput_many(state->file, diff);
1502 state->file = NULL;
1503 }
1504}
1505
1506/*
1507 * Get as many references to a file as we have IOs left in this submission,
1508 * assuming most submissions are for one file, or at least that each file
1509 * has more than one submission.
1510 */
1511static struct file *io_file_get(struct io_submit_state *state, int fd)
1512{
1513 if (!state)
1514 return fget(fd);
1515
1516 if (state->file) {
1517 if (state->fd == fd) {
1518 state->used_refs++;
1519 state->ios_left--;
1520 return state->file;
1521 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001522 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001523 }
1524 state->file = fget_many(fd, state->ios_left);
1525 if (!state->file)
1526 return NULL;
1527
1528 state->fd = fd;
1529 state->has_refs = state->ios_left;
1530 state->used_refs = 1;
1531 state->ios_left--;
1532 return state->file;
1533}
1534
Jens Axboe2b188cc2019-01-07 10:46:33 -07001535/*
1536 * If we tracked the file through the SCM inflight mechanism, we could support
1537 * any file. For now, just ensure that anything potentially problematic is done
1538 * inline.
1539 */
1540static bool io_file_supports_async(struct file *file)
1541{
1542 umode_t mode = file_inode(file)->i_mode;
1543
Jens Axboe10d59342019-12-09 20:16:22 -07001544 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001545 return true;
1546 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1547 return true;
1548
1549 return false;
1550}
1551
Jens Axboe3529d8c2019-12-19 18:24:38 -07001552static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1553 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001554{
Jens Axboedef596e2019-01-09 08:59:42 -07001555 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001556 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001557 unsigned ioprio;
1558 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001559
Jens Axboe09bb8392019-03-13 12:39:28 -06001560 if (!req->file)
1561 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001562
Jens Axboe491381ce2019-10-17 09:20:46 -06001563 if (S_ISREG(file_inode(req->file)->i_mode))
1564 req->flags |= REQ_F_ISREG;
1565
Jens Axboe2b188cc2019-01-07 10:46:33 -07001566 kiocb->ki_pos = READ_ONCE(sqe->off);
1567 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1568 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1569
1570 ioprio = READ_ONCE(sqe->ioprio);
1571 if (ioprio) {
1572 ret = ioprio_check_cap(ioprio);
1573 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001574 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001575
1576 kiocb->ki_ioprio = ioprio;
1577 } else
1578 kiocb->ki_ioprio = get_current_ioprio();
1579
1580 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1581 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001582 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001583
1584 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001585 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1586 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001587 req->flags |= REQ_F_NOWAIT;
1588
1589 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001590 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001591
Jens Axboedef596e2019-01-09 08:59:42 -07001592 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001593 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1594 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001595 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001596
Jens Axboedef596e2019-01-09 08:59:42 -07001597 kiocb->ki_flags |= IOCB_HIPRI;
1598 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001599 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001600 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001601 if (kiocb->ki_flags & IOCB_HIPRI)
1602 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001603 kiocb->ki_complete = io_complete_rw;
1604 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001605
Jens Axboe3529d8c2019-12-19 18:24:38 -07001606 req->rw.addr = READ_ONCE(sqe->addr);
1607 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001608 /* we own ->private, reuse it for the buffer index */
1609 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001610 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001611 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001612}
1613
1614static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1615{
1616 switch (ret) {
1617 case -EIOCBQUEUED:
1618 break;
1619 case -ERESTARTSYS:
1620 case -ERESTARTNOINTR:
1621 case -ERESTARTNOHAND:
1622 case -ERESTART_RESTARTBLOCK:
1623 /*
1624 * We can't just restart the syscall, since previously
1625 * submitted sqes may already be in progress. Just fail this
1626 * IO with EINTR.
1627 */
1628 ret = -EINTR;
1629 /* fall through */
1630 default:
1631 kiocb->ki_complete(kiocb, ret, 0);
1632 }
1633}
1634
Jens Axboeba816ad2019-09-28 11:36:45 -06001635static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1636 bool in_async)
1637{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001638 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001639 *nxt = __io_complete_rw(kiocb, ret);
1640 else
1641 io_rw_done(kiocb, ret);
1642}
1643
Jens Axboe9adbd452019-12-20 08:45:55 -07001644static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001645 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001646{
Jens Axboe9adbd452019-12-20 08:45:55 -07001647 struct io_ring_ctx *ctx = req->ctx;
1648 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001649 struct io_mapped_ubuf *imu;
1650 unsigned index, buf_index;
1651 size_t offset;
1652 u64 buf_addr;
1653
1654 /* attempt to use fixed buffers without having provided iovecs */
1655 if (unlikely(!ctx->user_bufs))
1656 return -EFAULT;
1657
Jens Axboe9adbd452019-12-20 08:45:55 -07001658 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001659 if (unlikely(buf_index >= ctx->nr_user_bufs))
1660 return -EFAULT;
1661
1662 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1663 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001664 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001665
1666 /* overflow */
1667 if (buf_addr + len < buf_addr)
1668 return -EFAULT;
1669 /* not inside the mapped region */
1670 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1671 return -EFAULT;
1672
1673 /*
1674 * May not be a start of buffer, set size appropriately
1675 * and advance us to the beginning.
1676 */
1677 offset = buf_addr - imu->ubuf;
1678 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001679
1680 if (offset) {
1681 /*
1682 * Don't use iov_iter_advance() here, as it's really slow for
1683 * using the latter parts of a big fixed buffer - it iterates
1684 * over each segment manually. We can cheat a bit here, because
1685 * we know that:
1686 *
1687 * 1) it's a BVEC iter, we set it up
1688 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1689 * first and last bvec
1690 *
1691 * So just find our index, and adjust the iterator afterwards.
1692 * If the offset is within the first bvec (or the whole first
1693 * bvec, just use iov_iter_advance(). This makes it easier
1694 * since we can just skip the first segment, which may not
1695 * be PAGE_SIZE aligned.
1696 */
1697 const struct bio_vec *bvec = imu->bvec;
1698
1699 if (offset <= bvec->bv_len) {
1700 iov_iter_advance(iter, offset);
1701 } else {
1702 unsigned long seg_skip;
1703
1704 /* skip first vec */
1705 offset -= bvec->bv_len;
1706 seg_skip = 1 + (offset >> PAGE_SHIFT);
1707
1708 iter->bvec = bvec + seg_skip;
1709 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001710 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001711 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001712 }
1713 }
1714
Jens Axboe5e559562019-11-13 16:12:46 -07001715 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001716}
1717
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001718static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1719 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001720{
Jens Axboe9adbd452019-12-20 08:45:55 -07001721 void __user *buf = u64_to_user_ptr(req->rw.addr);
1722 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001723 u8 opcode;
1724
Jens Axboed625c6e2019-12-17 19:53:05 -07001725 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001726 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001727 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001728 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001729 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001730
Jens Axboe9adbd452019-12-20 08:45:55 -07001731 /* buffer index only valid with fixed read/write */
1732 if (req->rw.kiocb.private)
1733 return -EINVAL;
1734
Jens Axboef67676d2019-12-02 11:03:47 -07001735 if (req->io) {
1736 struct io_async_rw *iorw = &req->io->rw;
1737
1738 *iovec = iorw->iov;
1739 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1740 if (iorw->iov == iorw->fast_iov)
1741 *iovec = NULL;
1742 return iorw->size;
1743 }
1744
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001745 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001746 return -EFAULT;
1747
1748#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001749 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001750 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1751 iovec, iter);
1752#endif
1753
1754 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1755}
1756
Jens Axboe32960612019-09-23 11:05:34 -06001757/*
1758 * For files that don't have ->read_iter() and ->write_iter(), handle them
1759 * by looping over ->read() or ->write() manually.
1760 */
1761static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1762 struct iov_iter *iter)
1763{
1764 ssize_t ret = 0;
1765
1766 /*
1767 * Don't support polled IO through this interface, and we can't
1768 * support non-blocking either. For the latter, this just causes
1769 * the kiocb to be handled from an async context.
1770 */
1771 if (kiocb->ki_flags & IOCB_HIPRI)
1772 return -EOPNOTSUPP;
1773 if (kiocb->ki_flags & IOCB_NOWAIT)
1774 return -EAGAIN;
1775
1776 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001777 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001778 ssize_t nr;
1779
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001780 if (!iov_iter_is_bvec(iter)) {
1781 iovec = iov_iter_iovec(iter);
1782 } else {
1783 /* fixed buffers import bvec */
1784 iovec.iov_base = kmap(iter->bvec->bv_page)
1785 + iter->iov_offset;
1786 iovec.iov_len = min(iter->count,
1787 iter->bvec->bv_len - iter->iov_offset);
1788 }
1789
Jens Axboe32960612019-09-23 11:05:34 -06001790 if (rw == READ) {
1791 nr = file->f_op->read(file, iovec.iov_base,
1792 iovec.iov_len, &kiocb->ki_pos);
1793 } else {
1794 nr = file->f_op->write(file, iovec.iov_base,
1795 iovec.iov_len, &kiocb->ki_pos);
1796 }
1797
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001798 if (iov_iter_is_bvec(iter))
1799 kunmap(iter->bvec->bv_page);
1800
Jens Axboe32960612019-09-23 11:05:34 -06001801 if (nr < 0) {
1802 if (!ret)
1803 ret = nr;
1804 break;
1805 }
1806 ret += nr;
1807 if (nr != iovec.iov_len)
1808 break;
1809 iov_iter_advance(iter, nr);
1810 }
1811
1812 return ret;
1813}
1814
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001815static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07001816 struct iovec *iovec, struct iovec *fast_iov,
1817 struct iov_iter *iter)
1818{
1819 req->io->rw.nr_segs = iter->nr_segs;
1820 req->io->rw.size = io_size;
1821 req->io->rw.iov = iovec;
1822 if (!req->io->rw.iov) {
1823 req->io->rw.iov = req->io->rw.fast_iov;
1824 memcpy(req->io->rw.iov, fast_iov,
1825 sizeof(struct iovec) * iter->nr_segs);
1826 }
1827}
1828
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001829static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07001830{
1831 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07001832 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001833}
1834
1835static void io_rw_async(struct io_wq_work **workptr)
1836{
1837 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1838 struct iovec *iov = NULL;
1839
1840 if (req->io->rw.iov != req->io->rw.fast_iov)
1841 iov = req->io->rw.iov;
1842 io_wq_submit_work(workptr);
1843 kfree(iov);
1844}
1845
1846static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
1847 struct iovec *iovec, struct iovec *fast_iov,
1848 struct iov_iter *iter)
1849{
Jens Axboe74566df2020-01-13 19:23:24 -07001850 if (req->opcode == IORING_OP_READ_FIXED ||
1851 req->opcode == IORING_OP_WRITE_FIXED)
1852 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001853 if (!req->io && io_alloc_async_ctx(req))
1854 return -ENOMEM;
1855
1856 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
1857 req->work.func = io_rw_async;
1858 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001859}
1860
Jens Axboe3529d8c2019-12-19 18:24:38 -07001861static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1862 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07001863{
Jens Axboe3529d8c2019-12-19 18:24:38 -07001864 struct io_async_ctx *io;
1865 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07001866 ssize_t ret;
1867
Jens Axboe3529d8c2019-12-19 18:24:38 -07001868 ret = io_prep_rw(req, sqe, force_nonblock);
1869 if (ret)
1870 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07001871
Jens Axboe3529d8c2019-12-19 18:24:38 -07001872 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1873 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07001874
Jens Axboe3529d8c2019-12-19 18:24:38 -07001875 if (!req->io)
1876 return 0;
1877
1878 io = req->io;
1879 io->rw.iov = io->rw.fast_iov;
1880 req->io = NULL;
1881 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
1882 req->io = io;
1883 if (ret < 0)
1884 return ret;
1885
1886 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
1887 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001888}
1889
Pavel Begunkov267bc902019-11-07 01:41:08 +03001890static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001891 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001892{
1893 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001894 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001895 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001896 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001897 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001898
Jens Axboe3529d8c2019-12-19 18:24:38 -07001899 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07001900 if (ret < 0)
1901 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001902
Jens Axboefd6c2e42019-12-18 12:19:41 -07001903 /* Ensure we clear previously set non-block flag */
1904 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001905 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001906
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08001907 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001908 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001909 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001910 req->result = io_size;
1911
1912 /*
1913 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1914 * we know to async punt it even if it was opened O_NONBLOCK
1915 */
Jens Axboe9adbd452019-12-20 08:45:55 -07001916 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07001917 req->flags |= REQ_F_MUST_PUNT;
1918 goto copy_iov;
1919 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001920
Jens Axboe31b51512019-01-18 22:56:34 -07001921 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07001922 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001923 if (!ret) {
1924 ssize_t ret2;
1925
Jens Axboe9adbd452019-12-20 08:45:55 -07001926 if (req->file->f_op->read_iter)
1927 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001928 else
Jens Axboe9adbd452019-12-20 08:45:55 -07001929 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001930
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001931 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07001932 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001933 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001934 } else {
1935copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001936 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001937 inline_vecs, &iter);
1938 if (ret)
1939 goto out_free;
1940 return -EAGAIN;
1941 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001942 }
Jens Axboef67676d2019-12-02 11:03:47 -07001943out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001944 if (!io_wq_current_is_worker())
1945 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001946 return ret;
1947}
1948
Jens Axboe3529d8c2019-12-19 18:24:38 -07001949static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1950 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07001951{
Jens Axboe3529d8c2019-12-19 18:24:38 -07001952 struct io_async_ctx *io;
1953 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07001954 ssize_t ret;
1955
Jens Axboe3529d8c2019-12-19 18:24:38 -07001956 ret = io_prep_rw(req, sqe, force_nonblock);
1957 if (ret)
1958 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07001959
Jens Axboe3529d8c2019-12-19 18:24:38 -07001960 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
1961 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07001962
Jens Axboe3529d8c2019-12-19 18:24:38 -07001963 if (!req->io)
1964 return 0;
1965
1966 io = req->io;
1967 io->rw.iov = io->rw.fast_iov;
1968 req->io = NULL;
1969 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
1970 req->io = io;
1971 if (ret < 0)
1972 return ret;
1973
1974 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
1975 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001976}
1977
Pavel Begunkov267bc902019-11-07 01:41:08 +03001978static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001979 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001980{
1981 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001982 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001983 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001984 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001985 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001986
Jens Axboe3529d8c2019-12-19 18:24:38 -07001987 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07001988 if (ret < 0)
1989 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001990
Jens Axboefd6c2e42019-12-18 12:19:41 -07001991 /* Ensure we clear previously set non-block flag */
1992 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001993 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001994
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08001995 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001996 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001997 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001998 req->result = io_size;
1999
2000 /*
2001 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2002 * we know to async punt it even if it was opened O_NONBLOCK
2003 */
2004 if (force_nonblock && !io_file_supports_async(req->file)) {
2005 req->flags |= REQ_F_MUST_PUNT;
2006 goto copy_iov;
2007 }
2008
Jens Axboe10d59342019-12-09 20:16:22 -07002009 /* file path doesn't support NOWAIT for non-direct_IO */
2010 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2011 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002012 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002013
Jens Axboe31b51512019-01-18 22:56:34 -07002014 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002015 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002016 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002017 ssize_t ret2;
2018
Jens Axboe2b188cc2019-01-07 10:46:33 -07002019 /*
2020 * Open-code file_start_write here to grab freeze protection,
2021 * which will be released by another thread in
2022 * io_complete_rw(). Fool lockdep by telling it the lock got
2023 * released so that it doesn't complain about the held lock when
2024 * we return to userspace.
2025 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002026 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002027 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002028 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002029 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002030 SB_FREEZE_WRITE);
2031 }
2032 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002033
Jens Axboe9adbd452019-12-20 08:45:55 -07002034 if (req->file->f_op->write_iter)
2035 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002036 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002037 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002038 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002039 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002040 } else {
2041copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002042 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002043 inline_vecs, &iter);
2044 if (ret)
2045 goto out_free;
2046 return -EAGAIN;
2047 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002048 }
Jens Axboe31b51512019-01-18 22:56:34 -07002049out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002050 if (!io_wq_current_is_worker())
2051 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002052 return ret;
2053}
2054
2055/*
2056 * IORING_OP_NOP just posts a completion event, nothing else.
2057 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002058static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002059{
2060 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002061
Jens Axboedef596e2019-01-09 08:59:42 -07002062 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2063 return -EINVAL;
2064
Jens Axboe78e19bb2019-11-06 15:21:34 -07002065 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002066 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002067 return 0;
2068}
2069
Jens Axboe3529d8c2019-12-19 18:24:38 -07002070static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002071{
Jens Axboe6b063142019-01-10 22:13:58 -07002072 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002073
Jens Axboe09bb8392019-03-13 12:39:28 -06002074 if (!req->file)
2075 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002076
Jens Axboe6b063142019-01-10 22:13:58 -07002077 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002078 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002079 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002080 return -EINVAL;
2081
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002082 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2083 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2084 return -EINVAL;
2085
2086 req->sync.off = READ_ONCE(sqe->off);
2087 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002088 return 0;
2089}
2090
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002091static bool io_req_cancelled(struct io_kiocb *req)
2092{
2093 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2094 req_set_fail_links(req);
2095 io_cqring_add_event(req, -ECANCELED);
2096 io_put_req(req);
2097 return true;
2098 }
2099
2100 return false;
2101}
2102
Jens Axboe78912932020-01-14 22:09:06 -07002103static void io_link_work_cb(struct io_wq_work **workptr)
2104{
2105 struct io_wq_work *work = *workptr;
2106 struct io_kiocb *link = work->data;
2107
2108 io_queue_linked_timeout(link);
2109 work->func = io_wq_submit_work;
2110}
2111
2112static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2113{
2114 struct io_kiocb *link;
2115
2116 io_prep_async_work(nxt, &link);
2117 *workptr = &nxt->work;
2118 if (link) {
2119 nxt->work.flags |= IO_WQ_WORK_CB;
2120 nxt->work.func = io_link_work_cb;
2121 nxt->work.data = link;
2122 }
2123}
2124
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002125static void io_fsync_finish(struct io_wq_work **workptr)
2126{
2127 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2128 loff_t end = req->sync.off + req->sync.len;
2129 struct io_kiocb *nxt = NULL;
2130 int ret;
2131
2132 if (io_req_cancelled(req))
2133 return;
2134
Jens Axboe9adbd452019-12-20 08:45:55 -07002135 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002136 end > 0 ? end : LLONG_MAX,
2137 req->sync.flags & IORING_FSYNC_DATASYNC);
2138 if (ret < 0)
2139 req_set_fail_links(req);
2140 io_cqring_add_event(req, ret);
2141 io_put_req_find_next(req, &nxt);
2142 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002143 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002144}
2145
Jens Axboefc4df992019-12-10 14:38:45 -07002146static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2147 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002148{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002149 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002150
2151 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002152 if (force_nonblock) {
2153 io_put_req(req);
2154 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002155 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002156 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002157
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002158 work = old_work = &req->work;
2159 io_fsync_finish(&work);
2160 if (work && work != old_work)
2161 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002162 return 0;
2163}
2164
Jens Axboed63d1b52019-12-10 10:38:56 -07002165static void io_fallocate_finish(struct io_wq_work **workptr)
2166{
2167 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2168 struct io_kiocb *nxt = NULL;
2169 int ret;
2170
2171 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2172 req->sync.len);
2173 if (ret < 0)
2174 req_set_fail_links(req);
2175 io_cqring_add_event(req, ret);
2176 io_put_req_find_next(req, &nxt);
2177 if (nxt)
2178 io_wq_assign_next(workptr, nxt);
2179}
2180
2181static int io_fallocate_prep(struct io_kiocb *req,
2182 const struct io_uring_sqe *sqe)
2183{
2184 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2185 return -EINVAL;
2186
2187 req->sync.off = READ_ONCE(sqe->off);
2188 req->sync.len = READ_ONCE(sqe->addr);
2189 req->sync.mode = READ_ONCE(sqe->len);
2190 return 0;
2191}
2192
2193static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2194 bool force_nonblock)
2195{
2196 struct io_wq_work *work, *old_work;
2197
2198 /* fallocate always requiring blocking context */
2199 if (force_nonblock) {
2200 io_put_req(req);
2201 req->work.func = io_fallocate_finish;
2202 return -EAGAIN;
2203 }
2204
2205 work = old_work = &req->work;
2206 io_fallocate_finish(&work);
2207 if (work && work != old_work)
2208 *nxt = container_of(work, struct io_kiocb, work);
2209
2210 return 0;
2211}
2212
Jens Axboe15b71ab2019-12-11 11:20:36 -07002213static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2214{
2215 int ret;
2216
2217 if (sqe->ioprio || sqe->buf_index)
2218 return -EINVAL;
2219
2220 req->open.dfd = READ_ONCE(sqe->fd);
2221 req->open.mode = READ_ONCE(sqe->len);
2222 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2223 req->open.flags = READ_ONCE(sqe->open_flags);
2224
2225 req->open.filename = getname(req->open.fname);
2226 if (IS_ERR(req->open.filename)) {
2227 ret = PTR_ERR(req->open.filename);
2228 req->open.filename = NULL;
2229 return ret;
2230 }
2231
2232 return 0;
2233}
2234
2235static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2236 bool force_nonblock)
2237{
2238 struct open_flags op;
2239 struct open_how how;
2240 struct file *file;
2241 int ret;
2242
2243 if (force_nonblock) {
2244 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2245 return -EAGAIN;
2246 }
2247
2248 how = build_open_how(req->open.flags, req->open.mode);
2249 ret = build_open_flags(&how, &op);
2250 if (ret)
2251 goto err;
2252
2253 ret = get_unused_fd_flags(how.flags);
2254 if (ret < 0)
2255 goto err;
2256
2257 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2258 if (IS_ERR(file)) {
2259 put_unused_fd(ret);
2260 ret = PTR_ERR(file);
2261 } else {
2262 fsnotify_open(file);
2263 fd_install(ret, file);
2264 }
2265err:
2266 putname(req->open.filename);
2267 if (ret < 0)
2268 req_set_fail_links(req);
2269 io_cqring_add_event(req, ret);
2270 io_put_req_find_next(req, nxt);
2271 return 0;
2272}
2273
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002274static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2275{
2276 unsigned lookup_flags;
2277 int ret;
2278
2279 if (sqe->ioprio || sqe->buf_index)
2280 return -EINVAL;
2281
2282 req->open.dfd = READ_ONCE(sqe->fd);
2283 req->open.mask = READ_ONCE(sqe->len);
2284 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2285 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2286 req->open.flags = READ_ONCE(sqe->statx_flags);
2287
2288 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.flags))
2289 return -EINVAL;
2290
2291 req->open.filename = getname_flags(req->open.fname, lookup_flags, NULL);
2292 if (IS_ERR(req->open.filename)) {
2293 ret = PTR_ERR(req->open.filename);
2294 req->open.filename = NULL;
2295 return ret;
2296 }
2297
2298 return 0;
2299}
2300
2301static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2302 bool force_nonblock)
2303{
2304 struct io_open *ctx = &req->open;
2305 unsigned lookup_flags;
2306 struct path path;
2307 struct kstat stat;
2308 int ret;
2309
2310 if (force_nonblock)
2311 return -EAGAIN;
2312
2313 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->flags))
2314 return -EINVAL;
2315
2316retry:
2317 /* filename_lookup() drops it, keep a reference */
2318 ctx->filename->refcnt++;
2319
2320 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2321 NULL);
2322 if (ret)
2323 goto err;
2324
2325 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->flags);
2326 path_put(&path);
2327 if (retry_estale(ret, lookup_flags)) {
2328 lookup_flags |= LOOKUP_REVAL;
2329 goto retry;
2330 }
2331 if (!ret)
2332 ret = cp_statx(&stat, ctx->buffer);
2333err:
2334 putname(ctx->filename);
2335 if (ret < 0)
2336 req_set_fail_links(req);
2337 io_cqring_add_event(req, ret);
2338 io_put_req_find_next(req, nxt);
2339 return 0;
2340}
2341
Jens Axboeb5dba592019-12-11 14:02:38 -07002342static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2343{
2344 /*
2345 * If we queue this for async, it must not be cancellable. That would
2346 * leave the 'file' in an undeterminate state.
2347 */
2348 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2349
2350 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2351 sqe->rw_flags || sqe->buf_index)
2352 return -EINVAL;
2353 if (sqe->flags & IOSQE_FIXED_FILE)
2354 return -EINVAL;
2355
2356 req->close.fd = READ_ONCE(sqe->fd);
2357 if (req->file->f_op == &io_uring_fops ||
2358 req->close.fd == req->ring_fd)
2359 return -EBADF;
2360
2361 return 0;
2362}
2363
2364static void io_close_finish(struct io_wq_work **workptr)
2365{
2366 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2367 struct io_kiocb *nxt = NULL;
2368
2369 /* Invoked with files, we need to do the close */
2370 if (req->work.files) {
2371 int ret;
2372
2373 ret = filp_close(req->close.put_file, req->work.files);
2374 if (ret < 0) {
2375 req_set_fail_links(req);
2376 }
2377 io_cqring_add_event(req, ret);
2378 }
2379
2380 fput(req->close.put_file);
2381
2382 /* we bypassed the re-issue, drop the submission reference */
2383 io_put_req(req);
2384 io_put_req_find_next(req, &nxt);
2385 if (nxt)
2386 io_wq_assign_next(workptr, nxt);
2387}
2388
2389static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2390 bool force_nonblock)
2391{
2392 int ret;
2393
2394 req->close.put_file = NULL;
2395 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2396 if (ret < 0)
2397 return ret;
2398
2399 /* if the file has a flush method, be safe and punt to async */
2400 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2401 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2402 goto eagain;
2403 }
2404
2405 /*
2406 * No ->flush(), safely close from here and just punt the
2407 * fput() to async context.
2408 */
2409 ret = filp_close(req->close.put_file, current->files);
2410
2411 if (ret < 0)
2412 req_set_fail_links(req);
2413 io_cqring_add_event(req, ret);
2414
2415 if (io_wq_current_is_worker()) {
2416 struct io_wq_work *old_work, *work;
2417
2418 old_work = work = &req->work;
2419 io_close_finish(&work);
2420 if (work && work != old_work)
2421 *nxt = container_of(work, struct io_kiocb, work);
2422 return 0;
2423 }
2424
2425eagain:
2426 req->work.func = io_close_finish;
2427 return -EAGAIN;
2428}
2429
Jens Axboe3529d8c2019-12-19 18:24:38 -07002430static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002431{
2432 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002433
2434 if (!req->file)
2435 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002436
2437 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2438 return -EINVAL;
2439 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2440 return -EINVAL;
2441
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002442 req->sync.off = READ_ONCE(sqe->off);
2443 req->sync.len = READ_ONCE(sqe->len);
2444 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002445 return 0;
2446}
2447
2448static void io_sync_file_range_finish(struct io_wq_work **workptr)
2449{
2450 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2451 struct io_kiocb *nxt = NULL;
2452 int ret;
2453
2454 if (io_req_cancelled(req))
2455 return;
2456
Jens Axboe9adbd452019-12-20 08:45:55 -07002457 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002458 req->sync.flags);
2459 if (ret < 0)
2460 req_set_fail_links(req);
2461 io_cqring_add_event(req, ret);
2462 io_put_req_find_next(req, &nxt);
2463 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002464 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002465}
2466
Jens Axboefc4df992019-12-10 14:38:45 -07002467static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002468 bool force_nonblock)
2469{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002470 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002471
2472 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002473 if (force_nonblock) {
2474 io_put_req(req);
2475 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002476 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002477 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002478
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002479 work = old_work = &req->work;
2480 io_sync_file_range_finish(&work);
2481 if (work && work != old_work)
2482 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002483 return 0;
2484}
2485
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002486#if defined(CONFIG_NET)
2487static void io_sendrecv_async(struct io_wq_work **workptr)
2488{
2489 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2490 struct iovec *iov = NULL;
2491
2492 if (req->io->rw.iov != req->io->rw.fast_iov)
2493 iov = req->io->msg.iov;
2494 io_wq_submit_work(workptr);
2495 kfree(iov);
2496}
2497#endif
2498
Jens Axboe3529d8c2019-12-19 18:24:38 -07002499static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002500{
Jens Axboe03b12302019-12-02 18:50:25 -07002501#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002502 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002503 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002504
Jens Axboee47293f2019-12-20 08:58:21 -07002505 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2506 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe3529d8c2019-12-19 18:24:38 -07002507
2508 if (!io)
2509 return 0;
2510
Jens Axboed9688562019-12-09 19:35:20 -07002511 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002512 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002513 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002514#else
Jens Axboee47293f2019-12-20 08:58:21 -07002515 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002516#endif
2517}
2518
Jens Axboefc4df992019-12-10 14:38:45 -07002519static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2520 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002521{
2522#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002523 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002524 struct socket *sock;
2525 int ret;
2526
2527 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2528 return -EINVAL;
2529
2530 sock = sock_from_file(req->file, &ret);
2531 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002532 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002533 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002534 unsigned flags;
2535
Jens Axboe03b12302019-12-02 18:50:25 -07002536 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002537 kmsg = &req->io->msg;
2538 kmsg->msg.msg_name = &addr;
2539 /* if iov is set, it's allocated already */
2540 if (!kmsg->iov)
2541 kmsg->iov = kmsg->fast_iov;
2542 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002543 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002544 struct io_sr_msg *sr = &req->sr_msg;
2545
Jens Axboe0b416c32019-12-15 10:57:46 -07002546 kmsg = &io.msg;
2547 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002548
2549 io.msg.iov = io.msg.fast_iov;
2550 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2551 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002552 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002553 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002554 }
2555
Jens Axboee47293f2019-12-20 08:58:21 -07002556 flags = req->sr_msg.msg_flags;
2557 if (flags & MSG_DONTWAIT)
2558 req->flags |= REQ_F_NOWAIT;
2559 else if (force_nonblock)
2560 flags |= MSG_DONTWAIT;
2561
Jens Axboe0b416c32019-12-15 10:57:46 -07002562 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002563 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002564 if (req->io)
2565 return -EAGAIN;
2566 if (io_alloc_async_ctx(req))
2567 return -ENOMEM;
2568 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2569 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002570 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002571 }
2572 if (ret == -ERESTARTSYS)
2573 ret = -EINTR;
2574 }
2575
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002576 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002577 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002578 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002579 if (ret < 0)
2580 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002581 io_put_req_find_next(req, nxt);
2582 return 0;
2583#else
2584 return -EOPNOTSUPP;
2585#endif
2586}
2587
Jens Axboe3529d8c2019-12-19 18:24:38 -07002588static int io_recvmsg_prep(struct io_kiocb *req,
2589 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002590{
2591#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002592 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002593 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002594
Jens Axboe3529d8c2019-12-19 18:24:38 -07002595 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2596 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2597
2598 if (!io)
Jens Axboe06b76d42019-12-19 14:44:26 -07002599 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002600
Jens Axboed9688562019-12-09 19:35:20 -07002601 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002602 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002603 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002604#else
Jens Axboee47293f2019-12-20 08:58:21 -07002605 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002606#endif
2607}
2608
Jens Axboefc4df992019-12-10 14:38:45 -07002609static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2610 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002611{
2612#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002613 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002614 struct socket *sock;
2615 int ret;
2616
2617 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2618 return -EINVAL;
2619
2620 sock = sock_from_file(req->file, &ret);
2621 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002622 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002623 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002624 unsigned flags;
2625
Jens Axboe03b12302019-12-02 18:50:25 -07002626 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002627 kmsg = &req->io->msg;
2628 kmsg->msg.msg_name = &addr;
2629 /* if iov is set, it's allocated already */
2630 if (!kmsg->iov)
2631 kmsg->iov = kmsg->fast_iov;
2632 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002633 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002634 struct io_sr_msg *sr = &req->sr_msg;
2635
Jens Axboe0b416c32019-12-15 10:57:46 -07002636 kmsg = &io.msg;
2637 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002638
2639 io.msg.iov = io.msg.fast_iov;
2640 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
2641 sr->msg_flags, &io.msg.uaddr,
2642 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002643 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002644 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002645 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002646
Jens Axboee47293f2019-12-20 08:58:21 -07002647 flags = req->sr_msg.msg_flags;
2648 if (flags & MSG_DONTWAIT)
2649 req->flags |= REQ_F_NOWAIT;
2650 else if (force_nonblock)
2651 flags |= MSG_DONTWAIT;
2652
2653 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
2654 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002655 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002656 if (req->io)
2657 return -EAGAIN;
2658 if (io_alloc_async_ctx(req))
2659 return -ENOMEM;
2660 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2661 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002662 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002663 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002664 if (ret == -ERESTARTSYS)
2665 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002666 }
2667
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002668 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002669 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002670 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002671 if (ret < 0)
2672 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002673 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002674 return 0;
2675#else
2676 return -EOPNOTSUPP;
2677#endif
2678}
2679
Jens Axboe3529d8c2019-12-19 18:24:38 -07002680static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002681{
2682#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002683 struct io_accept *accept = &req->accept;
2684
Jens Axboe17f2fe32019-10-17 14:42:58 -06002685 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2686 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002687 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002688 return -EINVAL;
2689
Jens Axboed55e5f52019-12-11 16:12:15 -07002690 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2691 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002692 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002693 return 0;
2694#else
2695 return -EOPNOTSUPP;
2696#endif
2697}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002698
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002699#if defined(CONFIG_NET)
2700static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2701 bool force_nonblock)
2702{
2703 struct io_accept *accept = &req->accept;
2704 unsigned file_flags;
2705 int ret;
2706
2707 file_flags = force_nonblock ? O_NONBLOCK : 0;
2708 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2709 accept->addr_len, accept->flags);
2710 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002711 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002712 if (ret == -ERESTARTSYS)
2713 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002714 if (ret < 0)
2715 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002716 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002717 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002718 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002719}
2720
2721static void io_accept_finish(struct io_wq_work **workptr)
2722{
2723 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2724 struct io_kiocb *nxt = NULL;
2725
2726 if (io_req_cancelled(req))
2727 return;
2728 __io_accept(req, &nxt, false);
2729 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002730 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002731}
2732#endif
2733
2734static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2735 bool force_nonblock)
2736{
2737#if defined(CONFIG_NET)
2738 int ret;
2739
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002740 ret = __io_accept(req, nxt, force_nonblock);
2741 if (ret == -EAGAIN && force_nonblock) {
2742 req->work.func = io_accept_finish;
2743 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2744 io_put_req(req);
2745 return -EAGAIN;
2746 }
2747 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002748#else
2749 return -EOPNOTSUPP;
2750#endif
2751}
2752
Jens Axboe3529d8c2019-12-19 18:24:38 -07002753static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07002754{
2755#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002756 struct io_connect *conn = &req->connect;
2757 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07002758
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002759 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2760 return -EINVAL;
2761 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2762 return -EINVAL;
2763
Jens Axboe3529d8c2019-12-19 18:24:38 -07002764 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2765 conn->addr_len = READ_ONCE(sqe->addr2);
2766
2767 if (!io)
2768 return 0;
2769
2770 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002771 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002772#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002773 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07002774#endif
2775}
2776
Jens Axboefc4df992019-12-10 14:38:45 -07002777static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
2778 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07002779{
2780#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07002781 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002782 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002783 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002784
Jens Axboef499a022019-12-02 16:28:46 -07002785 if (req->io) {
2786 io = req->io;
2787 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002788 ret = move_addr_to_kernel(req->connect.addr,
2789 req->connect.addr_len,
2790 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002791 if (ret)
2792 goto out;
2793 io = &__io;
2794 }
2795
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002796 file_flags = force_nonblock ? O_NONBLOCK : 0;
2797
2798 ret = __sys_connect_file(req->file, &io->connect.address,
2799 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002800 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002801 if (req->io)
2802 return -EAGAIN;
2803 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07002804 ret = -ENOMEM;
2805 goto out;
2806 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002807 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07002808 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002809 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002810 if (ret == -ERESTARTSYS)
2811 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002812out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002813 if (ret < 0)
2814 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002815 io_cqring_add_event(req, ret);
2816 io_put_req_find_next(req, nxt);
2817 return 0;
2818#else
2819 return -EOPNOTSUPP;
2820#endif
2821}
2822
Jens Axboe221c5eb2019-01-17 09:41:58 -07002823static void io_poll_remove_one(struct io_kiocb *req)
2824{
2825 struct io_poll_iocb *poll = &req->poll;
2826
2827 spin_lock(&poll->head->lock);
2828 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07002829 if (!list_empty(&poll->wait.entry)) {
2830 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002831 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002832 }
2833 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002834 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002835}
2836
2837static void io_poll_remove_all(struct io_ring_ctx *ctx)
2838{
Jens Axboe78076bb2019-12-04 19:56:40 -07002839 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002840 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07002841 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002842
2843 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002844 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2845 struct hlist_head *list;
2846
2847 list = &ctx->cancel_hash[i];
2848 hlist_for_each_entry_safe(req, tmp, list, hash_node)
2849 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002850 }
2851 spin_unlock_irq(&ctx->completion_lock);
2852}
2853
Jens Axboe47f46762019-11-09 17:43:02 -07002854static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2855{
Jens Axboe78076bb2019-12-04 19:56:40 -07002856 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07002857 struct io_kiocb *req;
2858
Jens Axboe78076bb2019-12-04 19:56:40 -07002859 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2860 hlist_for_each_entry(req, list, hash_node) {
2861 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002862 io_poll_remove_one(req);
2863 return 0;
2864 }
Jens Axboe47f46762019-11-09 17:43:02 -07002865 }
2866
2867 return -ENOENT;
2868}
2869
Jens Axboe3529d8c2019-12-19 18:24:38 -07002870static int io_poll_remove_prep(struct io_kiocb *req,
2871 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002872{
Jens Axboe221c5eb2019-01-17 09:41:58 -07002873 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2874 return -EINVAL;
2875 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2876 sqe->poll_events)
2877 return -EINVAL;
2878
Jens Axboe0969e782019-12-17 18:40:57 -07002879 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07002880 return 0;
2881}
2882
2883/*
2884 * Find a running poll command that matches one specified in sqe->addr,
2885 * and remove it if found.
2886 */
2887static int io_poll_remove(struct io_kiocb *req)
2888{
2889 struct io_ring_ctx *ctx = req->ctx;
2890 u64 addr;
2891 int ret;
2892
Jens Axboe0969e782019-12-17 18:40:57 -07002893 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002894 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07002895 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002896 spin_unlock_irq(&ctx->completion_lock);
2897
Jens Axboe78e19bb2019-11-06 15:21:34 -07002898 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002899 if (ret < 0)
2900 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002901 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002902 return 0;
2903}
2904
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002905static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002906{
Jackie Liua197f662019-11-08 08:09:12 -07002907 struct io_ring_ctx *ctx = req->ctx;
2908
Jens Axboe8c838782019-03-12 15:48:16 -06002909 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002910 if (error)
2911 io_cqring_fill_event(req, error);
2912 else
2913 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002914 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002915}
2916
Jens Axboe561fb042019-10-24 07:25:42 -06002917static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002918{
Jens Axboe561fb042019-10-24 07:25:42 -06002919 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002920 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2921 struct io_poll_iocb *poll = &req->poll;
2922 struct poll_table_struct pt = { ._key = poll->events };
2923 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002924 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002925 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002926 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002927
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002928 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002929 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002930 ret = -ECANCELED;
2931 } else if (READ_ONCE(poll->canceled)) {
2932 ret = -ECANCELED;
2933 }
Jens Axboe561fb042019-10-24 07:25:42 -06002934
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002935 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002936 mask = vfs_poll(poll->file, &pt) & poll->events;
2937
2938 /*
2939 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2940 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2941 * synchronize with them. In the cancellation case the list_del_init
2942 * itself is not actually needed, but harmless so we keep it in to
2943 * avoid further branches in the fast path.
2944 */
2945 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002946 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07002947 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002948 spin_unlock_irq(&ctx->completion_lock);
2949 return;
2950 }
Jens Axboe78076bb2019-12-04 19:56:40 -07002951 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002952 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002953 spin_unlock_irq(&ctx->completion_lock);
2954
Jens Axboe8c838782019-03-12 15:48:16 -06002955 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002956
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002957 if (ret < 0)
2958 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002959 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002960 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002961 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002962}
2963
2964static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2965 void *key)
2966{
Jens Axboee9444752019-11-26 15:02:04 -07002967 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002968 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2969 struct io_ring_ctx *ctx = req->ctx;
2970 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002971 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002972
2973 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002974 if (mask && !(mask & poll->events))
2975 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002976
Jens Axboe392edb42019-12-09 17:52:20 -07002977 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002978
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002979 /*
2980 * Run completion inline if we can. We're using trylock here because
2981 * we are violating the completion_lock -> poll wq lock ordering.
2982 * If we have a link timeout we're going to need the completion_lock
2983 * for finalizing the request, mark us as having grabbed that already.
2984 */
Jens Axboe8c838782019-03-12 15:48:16 -06002985 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboe78076bb2019-12-04 19:56:40 -07002986 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002987 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002988 req->flags |= REQ_F_COMP_LOCKED;
2989 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002990 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2991
2992 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002993 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002994 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002995 }
2996
Jens Axboe221c5eb2019-01-17 09:41:58 -07002997 return 1;
2998}
2999
3000struct io_poll_table {
3001 struct poll_table_struct pt;
3002 struct io_kiocb *req;
3003 int error;
3004};
3005
3006static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3007 struct poll_table_struct *p)
3008{
3009 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3010
3011 if (unlikely(pt->req->poll.head)) {
3012 pt->error = -EINVAL;
3013 return;
3014 }
3015
3016 pt->error = 0;
3017 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003018 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003019}
3020
Jens Axboeeac406c2019-11-14 12:09:58 -07003021static void io_poll_req_insert(struct io_kiocb *req)
3022{
3023 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003024 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003025
Jens Axboe78076bb2019-12-04 19:56:40 -07003026 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3027 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003028}
3029
Jens Axboe3529d8c2019-12-19 18:24:38 -07003030static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003031{
3032 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003033 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003034
3035 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3036 return -EINVAL;
3037 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3038 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003039 if (!poll->file)
3040 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003041
Jens Axboe221c5eb2019-01-17 09:41:58 -07003042 events = READ_ONCE(sqe->poll_events);
3043 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003044 return 0;
3045}
3046
3047static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3048{
3049 struct io_poll_iocb *poll = &req->poll;
3050 struct io_ring_ctx *ctx = req->ctx;
3051 struct io_poll_table ipt;
3052 bool cancel = false;
3053 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003054
3055 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003056 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003057
Jens Axboe221c5eb2019-01-17 09:41:58 -07003058 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003059 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003060 poll->canceled = false;
3061
3062 ipt.pt._qproc = io_poll_queue_proc;
3063 ipt.pt._key = poll->events;
3064 ipt.req = req;
3065 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3066
3067 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003068 INIT_LIST_HEAD(&poll->wait.entry);
3069 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3070 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003071
Jens Axboe36703242019-07-25 10:20:18 -06003072 INIT_LIST_HEAD(&req->list);
3073
Jens Axboe221c5eb2019-01-17 09:41:58 -07003074 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003075
3076 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003077 if (likely(poll->head)) {
3078 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003079 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003080 if (ipt.error)
3081 cancel = true;
3082 ipt.error = 0;
3083 mask = 0;
3084 }
3085 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003086 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003087 else if (cancel)
3088 WRITE_ONCE(poll->canceled, true);
3089 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003090 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003091 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003092 }
Jens Axboe8c838782019-03-12 15:48:16 -06003093 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003094 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003095 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003096 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003097 spin_unlock_irq(&ctx->completion_lock);
3098
Jens Axboe8c838782019-03-12 15:48:16 -06003099 if (mask) {
3100 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003101 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003102 }
Jens Axboe8c838782019-03-12 15:48:16 -06003103 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003104}
3105
Jens Axboe5262f562019-09-17 12:26:57 -06003106static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3107{
Jens Axboead8a48a2019-11-15 08:49:11 -07003108 struct io_timeout_data *data = container_of(timer,
3109 struct io_timeout_data, timer);
3110 struct io_kiocb *req = data->req;
3111 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003112 unsigned long flags;
3113
Jens Axboe5262f562019-09-17 12:26:57 -06003114 atomic_inc(&ctx->cq_timeouts);
3115
3116 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003117 /*
Jens Axboe11365042019-10-16 09:08:32 -06003118 * We could be racing with timeout deletion. If the list is empty,
3119 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003120 */
Jens Axboe842f9612019-10-29 12:34:10 -06003121 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003122 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003123
Jens Axboe11365042019-10-16 09:08:32 -06003124 /*
3125 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003126 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003127 * pointer will be increased, otherwise other timeout reqs may
3128 * return in advance without waiting for enough wait_nr.
3129 */
3130 prev = req;
3131 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3132 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003133 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003134 }
Jens Axboe842f9612019-10-29 12:34:10 -06003135
Jens Axboe78e19bb2019-11-06 15:21:34 -07003136 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003137 io_commit_cqring(ctx);
3138 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3139
3140 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003141 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003142 io_put_req(req);
3143 return HRTIMER_NORESTART;
3144}
3145
Jens Axboe47f46762019-11-09 17:43:02 -07003146static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3147{
3148 struct io_kiocb *req;
3149 int ret = -ENOENT;
3150
3151 list_for_each_entry(req, &ctx->timeout_list, list) {
3152 if (user_data == req->user_data) {
3153 list_del_init(&req->list);
3154 ret = 0;
3155 break;
3156 }
3157 }
3158
3159 if (ret == -ENOENT)
3160 return ret;
3161
Jens Axboe2d283902019-12-04 11:08:05 -07003162 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003163 if (ret == -1)
3164 return -EALREADY;
3165
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003166 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003167 io_cqring_fill_event(req, -ECANCELED);
3168 io_put_req(req);
3169 return 0;
3170}
3171
Jens Axboe3529d8c2019-12-19 18:24:38 -07003172static int io_timeout_remove_prep(struct io_kiocb *req,
3173 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003174{
Jens Axboeb29472e2019-12-17 18:50:29 -07003175 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3176 return -EINVAL;
3177 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3178 return -EINVAL;
3179
3180 req->timeout.addr = READ_ONCE(sqe->addr);
3181 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3182 if (req->timeout.flags)
3183 return -EINVAL;
3184
Jens Axboeb29472e2019-12-17 18:50:29 -07003185 return 0;
3186}
3187
Jens Axboe11365042019-10-16 09:08:32 -06003188/*
3189 * Remove or update an existing timeout command
3190 */
Jens Axboefc4df992019-12-10 14:38:45 -07003191static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003192{
3193 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003194 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003195
Jens Axboe11365042019-10-16 09:08:32 -06003196 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003197 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003198
Jens Axboe47f46762019-11-09 17:43:02 -07003199 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003200 io_commit_cqring(ctx);
3201 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003202 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003203 if (ret < 0)
3204 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003205 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003206 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003207}
3208
Jens Axboe3529d8c2019-12-19 18:24:38 -07003209static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003210 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003211{
Jens Axboead8a48a2019-11-15 08:49:11 -07003212 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003213 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003214
Jens Axboead8a48a2019-11-15 08:49:11 -07003215 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003216 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003217 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003218 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003219 if (sqe->off && is_timeout_link)
3220 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003221 flags = READ_ONCE(sqe->timeout_flags);
3222 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003223 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003224
Jens Axboe26a61672019-12-20 09:02:01 -07003225 req->timeout.count = READ_ONCE(sqe->off);
3226
Jens Axboe3529d8c2019-12-19 18:24:38 -07003227 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003228 return -ENOMEM;
3229
3230 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003231 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003232 req->flags |= REQ_F_TIMEOUT;
3233
3234 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003235 return -EFAULT;
3236
Jens Axboe11365042019-10-16 09:08:32 -06003237 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003238 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003239 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003240 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003241
Jens Axboead8a48a2019-11-15 08:49:11 -07003242 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3243 return 0;
3244}
3245
Jens Axboefc4df992019-12-10 14:38:45 -07003246static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003247{
3248 unsigned count;
3249 struct io_ring_ctx *ctx = req->ctx;
3250 struct io_timeout_data *data;
3251 struct list_head *entry;
3252 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003253
Jens Axboe2d283902019-12-04 11:08:05 -07003254 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003255
Jens Axboe5262f562019-09-17 12:26:57 -06003256 /*
3257 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003258 * timeout event to be satisfied. If it isn't set, then this is
3259 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003260 */
Jens Axboe26a61672019-12-20 09:02:01 -07003261 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003262 if (!count) {
3263 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3264 spin_lock_irq(&ctx->completion_lock);
3265 entry = ctx->timeout_list.prev;
3266 goto add;
3267 }
Jens Axboe5262f562019-09-17 12:26:57 -06003268
3269 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003270 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003271
3272 /*
3273 * Insertion sort, ensuring the first entry in the list is always
3274 * the one we need first.
3275 */
Jens Axboe5262f562019-09-17 12:26:57 -06003276 spin_lock_irq(&ctx->completion_lock);
3277 list_for_each_prev(entry, &ctx->timeout_list) {
3278 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003279 unsigned nxt_sq_head;
3280 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003281 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003282
Jens Axboe93bd25b2019-11-11 23:34:31 -07003283 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3284 continue;
3285
yangerkun5da0fb12019-10-15 21:59:29 +08003286 /*
3287 * Since cached_sq_head + count - 1 can overflow, use type long
3288 * long to store it.
3289 */
3290 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003291 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3292 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003293
3294 /*
3295 * cached_sq_head may overflow, and it will never overflow twice
3296 * once there is some timeout req still be valid.
3297 */
3298 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003299 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003300
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003301 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003302 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003303
3304 /*
3305 * Sequence of reqs after the insert one and itself should
3306 * be adjusted because each timeout req consumes a slot.
3307 */
3308 span++;
3309 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003310 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003311 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003312add:
Jens Axboe5262f562019-09-17 12:26:57 -06003313 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003314 data->timer.function = io_timeout_fn;
3315 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003316 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003317 return 0;
3318}
3319
Jens Axboe62755e32019-10-28 21:49:21 -06003320static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003321{
Jens Axboe62755e32019-10-28 21:49:21 -06003322 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003323
Jens Axboe62755e32019-10-28 21:49:21 -06003324 return req->user_data == (unsigned long) data;
3325}
3326
Jens Axboee977d6d2019-11-05 12:39:45 -07003327static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003328{
Jens Axboe62755e32019-10-28 21:49:21 -06003329 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003330 int ret = 0;
3331
Jens Axboe62755e32019-10-28 21:49:21 -06003332 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3333 switch (cancel_ret) {
3334 case IO_WQ_CANCEL_OK:
3335 ret = 0;
3336 break;
3337 case IO_WQ_CANCEL_RUNNING:
3338 ret = -EALREADY;
3339 break;
3340 case IO_WQ_CANCEL_NOTFOUND:
3341 ret = -ENOENT;
3342 break;
3343 }
3344
Jens Axboee977d6d2019-11-05 12:39:45 -07003345 return ret;
3346}
3347
Jens Axboe47f46762019-11-09 17:43:02 -07003348static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3349 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003350 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003351{
3352 unsigned long flags;
3353 int ret;
3354
3355 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3356 if (ret != -ENOENT) {
3357 spin_lock_irqsave(&ctx->completion_lock, flags);
3358 goto done;
3359 }
3360
3361 spin_lock_irqsave(&ctx->completion_lock, flags);
3362 ret = io_timeout_cancel(ctx, sqe_addr);
3363 if (ret != -ENOENT)
3364 goto done;
3365 ret = io_poll_cancel(ctx, sqe_addr);
3366done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003367 if (!ret)
3368 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003369 io_cqring_fill_event(req, ret);
3370 io_commit_cqring(ctx);
3371 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3372 io_cqring_ev_posted(ctx);
3373
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003374 if (ret < 0)
3375 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003376 io_put_req_find_next(req, nxt);
3377}
3378
Jens Axboe3529d8c2019-12-19 18:24:38 -07003379static int io_async_cancel_prep(struct io_kiocb *req,
3380 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003381{
Jens Axboefbf23842019-12-17 18:45:56 -07003382 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003383 return -EINVAL;
3384 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3385 sqe->cancel_flags)
3386 return -EINVAL;
3387
Jens Axboefbf23842019-12-17 18:45:56 -07003388 req->cancel.addr = READ_ONCE(sqe->addr);
3389 return 0;
3390}
3391
3392static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3393{
3394 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003395
3396 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003397 return 0;
3398}
3399
Jens Axboe05f3fb32019-12-09 11:22:50 -07003400static int io_files_update_prep(struct io_kiocb *req,
3401 const struct io_uring_sqe *sqe)
3402{
3403 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3404 return -EINVAL;
3405
3406 req->files_update.offset = READ_ONCE(sqe->off);
3407 req->files_update.nr_args = READ_ONCE(sqe->len);
3408 if (!req->files_update.nr_args)
3409 return -EINVAL;
3410 req->files_update.arg = READ_ONCE(sqe->addr);
3411 return 0;
3412}
3413
3414static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3415{
3416 struct io_ring_ctx *ctx = req->ctx;
3417 struct io_uring_files_update up;
3418 int ret;
3419
3420 if (force_nonblock) {
3421 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3422 return -EAGAIN;
3423 }
3424
3425 up.offset = req->files_update.offset;
3426 up.fds = req->files_update.arg;
3427
3428 mutex_lock(&ctx->uring_lock);
3429 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3430 mutex_unlock(&ctx->uring_lock);
3431
3432 if (ret < 0)
3433 req_set_fail_links(req);
3434 io_cqring_add_event(req, ret);
3435 io_put_req(req);
3436 return 0;
3437}
3438
Jens Axboe3529d8c2019-12-19 18:24:38 -07003439static int io_req_defer_prep(struct io_kiocb *req,
3440 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003441{
Jens Axboee7815732019-12-17 19:45:06 -07003442 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003443
Jens Axboed625c6e2019-12-17 19:53:05 -07003444 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003445 case IORING_OP_NOP:
3446 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003447 case IORING_OP_READV:
3448 case IORING_OP_READ_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003449 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003450 break;
3451 case IORING_OP_WRITEV:
3452 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003453 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003454 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003455 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003456 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003457 break;
3458 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003459 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003460 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003461 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003462 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003463 break;
3464 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003465 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003466 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003467 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003468 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003469 break;
3470 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003471 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003472 break;
Jens Axboef499a022019-12-02 16:28:46 -07003473 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003474 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003475 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003476 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003477 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003478 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003479 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003480 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003481 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003482 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003483 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003484 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003485 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003486 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003487 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003488 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003489 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003490 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003491 case IORING_OP_FALLOCATE:
3492 ret = io_fallocate_prep(req, sqe);
3493 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003494 case IORING_OP_OPENAT:
3495 ret = io_openat_prep(req, sqe);
3496 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003497 case IORING_OP_CLOSE:
3498 ret = io_close_prep(req, sqe);
3499 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003500 case IORING_OP_FILES_UPDATE:
3501 ret = io_files_update_prep(req, sqe);
3502 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003503 case IORING_OP_STATX:
3504 ret = io_statx_prep(req, sqe);
3505 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003506 default:
Jens Axboee7815732019-12-17 19:45:06 -07003507 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3508 req->opcode);
3509 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003510 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003511 }
3512
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003513 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003514}
3515
Jens Axboe3529d8c2019-12-19 18:24:38 -07003516static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003517{
Jackie Liua197f662019-11-08 08:09:12 -07003518 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003519 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003520
Bob Liu9d858b22019-11-13 18:06:25 +08003521 /* Still need defer if there is pending req in defer list. */
3522 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003523 return 0;
3524
Jens Axboe3529d8c2019-12-19 18:24:38 -07003525 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003526 return -EAGAIN;
3527
Jens Axboe3529d8c2019-12-19 18:24:38 -07003528 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003529 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003530 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003531
Jens Axboede0617e2019-04-06 21:51:27 -06003532 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003533 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003534 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003535 return 0;
3536 }
3537
Jens Axboe915967f2019-11-21 09:01:20 -07003538 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003539 list_add_tail(&req->list, &ctx->defer_list);
3540 spin_unlock_irq(&ctx->completion_lock);
3541 return -EIOCBQUEUED;
3542}
3543
Jens Axboe3529d8c2019-12-19 18:24:38 -07003544static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3545 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003546{
Jackie Liua197f662019-11-08 08:09:12 -07003547 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07003548 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003549
Jens Axboed625c6e2019-12-17 19:53:05 -07003550 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003551 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003552 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003553 break;
3554 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003555 case IORING_OP_READ_FIXED:
3556 if (sqe) {
3557 ret = io_read_prep(req, sqe, force_nonblock);
3558 if (ret < 0)
3559 break;
3560 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003561 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003562 break;
3563 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07003564 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003565 if (sqe) {
3566 ret = io_write_prep(req, sqe, force_nonblock);
3567 if (ret < 0)
3568 break;
3569 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003570 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003571 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003572 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003573 if (sqe) {
3574 ret = io_prep_fsync(req, sqe);
3575 if (ret < 0)
3576 break;
3577 }
Jens Axboefc4df992019-12-10 14:38:45 -07003578 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003579 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003580 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003581 if (sqe) {
3582 ret = io_poll_add_prep(req, sqe);
3583 if (ret)
3584 break;
3585 }
Jens Axboefc4df992019-12-10 14:38:45 -07003586 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003587 break;
3588 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003589 if (sqe) {
3590 ret = io_poll_remove_prep(req, sqe);
3591 if (ret < 0)
3592 break;
3593 }
Jens Axboefc4df992019-12-10 14:38:45 -07003594 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003595 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003596 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003597 if (sqe) {
3598 ret = io_prep_sfr(req, sqe);
3599 if (ret < 0)
3600 break;
3601 }
Jens Axboefc4df992019-12-10 14:38:45 -07003602 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003603 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003604 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003605 if (sqe) {
3606 ret = io_sendmsg_prep(req, sqe);
3607 if (ret < 0)
3608 break;
3609 }
Jens Axboefc4df992019-12-10 14:38:45 -07003610 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003611 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003612 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003613 if (sqe) {
3614 ret = io_recvmsg_prep(req, sqe);
3615 if (ret)
3616 break;
3617 }
Jens Axboefc4df992019-12-10 14:38:45 -07003618 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003619 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003620 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003621 if (sqe) {
3622 ret = io_timeout_prep(req, sqe, false);
3623 if (ret)
3624 break;
3625 }
Jens Axboefc4df992019-12-10 14:38:45 -07003626 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003627 break;
Jens Axboe11365042019-10-16 09:08:32 -06003628 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003629 if (sqe) {
3630 ret = io_timeout_remove_prep(req, sqe);
3631 if (ret)
3632 break;
3633 }
Jens Axboefc4df992019-12-10 14:38:45 -07003634 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003635 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003636 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003637 if (sqe) {
3638 ret = io_accept_prep(req, sqe);
3639 if (ret)
3640 break;
3641 }
Jens Axboefc4df992019-12-10 14:38:45 -07003642 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003643 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003644 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003645 if (sqe) {
3646 ret = io_connect_prep(req, sqe);
3647 if (ret)
3648 break;
3649 }
Jens Axboefc4df992019-12-10 14:38:45 -07003650 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003651 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003652 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003653 if (sqe) {
3654 ret = io_async_cancel_prep(req, sqe);
3655 if (ret)
3656 break;
3657 }
Jens Axboefc4df992019-12-10 14:38:45 -07003658 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003659 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003660 case IORING_OP_FALLOCATE:
3661 if (sqe) {
3662 ret = io_fallocate_prep(req, sqe);
3663 if (ret)
3664 break;
3665 }
3666 ret = io_fallocate(req, nxt, force_nonblock);
3667 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003668 case IORING_OP_OPENAT:
3669 if (sqe) {
3670 ret = io_openat_prep(req, sqe);
3671 if (ret)
3672 break;
3673 }
3674 ret = io_openat(req, nxt, force_nonblock);
3675 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003676 case IORING_OP_CLOSE:
3677 if (sqe) {
3678 ret = io_close_prep(req, sqe);
3679 if (ret)
3680 break;
3681 }
3682 ret = io_close(req, nxt, force_nonblock);
3683 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003684 case IORING_OP_FILES_UPDATE:
3685 if (sqe) {
3686 ret = io_files_update_prep(req, sqe);
3687 if (ret)
3688 break;
3689 }
3690 ret = io_files_update(req, force_nonblock);
3691 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003692 case IORING_OP_STATX:
3693 if (sqe) {
3694 ret = io_statx_prep(req, sqe);
3695 if (ret)
3696 break;
3697 }
3698 ret = io_statx(req, nxt, force_nonblock);
3699 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003700 default:
3701 ret = -EINVAL;
3702 break;
3703 }
3704
Jens Axboedef596e2019-01-09 08:59:42 -07003705 if (ret)
3706 return ret;
3707
3708 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07003709 const bool in_async = io_wq_current_is_worker();
3710
Jens Axboe9e645e112019-05-10 16:07:28 -06003711 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07003712 return -EAGAIN;
3713
Jens Axboe11ba8202020-01-15 21:51:17 -07003714 /* workqueue context doesn't hold uring_lock, grab it now */
3715 if (in_async)
3716 mutex_lock(&ctx->uring_lock);
3717
Jens Axboedef596e2019-01-09 08:59:42 -07003718 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07003719
3720 if (in_async)
3721 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07003722 }
3723
3724 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003725}
3726
Jens Axboe561fb042019-10-24 07:25:42 -06003727static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003728{
Jens Axboe561fb042019-10-24 07:25:42 -06003729 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003730 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003731 struct io_kiocb *nxt = NULL;
3732 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003733
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003734 /* if NO_CANCEL is set, we must still run the work */
3735 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
3736 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003737 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003738 }
Jens Axboe31b51512019-01-18 22:56:34 -07003739
Jens Axboe561fb042019-10-24 07:25:42 -06003740 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003741 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3742 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003743 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003744 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003745 /*
3746 * We can get EAGAIN for polled IO even though we're
3747 * forcing a sync submission from here, since we can't
3748 * wait for request slots on the block side.
3749 */
3750 if (ret != -EAGAIN)
3751 break;
3752 cond_resched();
3753 } while (1);
3754 }
Jens Axboe31b51512019-01-18 22:56:34 -07003755
Jens Axboe561fb042019-10-24 07:25:42 -06003756 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003757 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003758
Jens Axboe561fb042019-10-24 07:25:42 -06003759 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003760 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003761 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003762 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003763 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003764
Jens Axboe561fb042019-10-24 07:25:42 -06003765 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07003766 if (!ret && nxt)
3767 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07003768}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003769
Jens Axboe9e3aa612019-12-11 15:55:43 -07003770static bool io_req_op_valid(int op)
3771{
3772 return op >= IORING_OP_NOP && op < IORING_OP_LAST;
3773}
3774
Jens Axboe15b71ab2019-12-11 11:20:36 -07003775static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06003776{
Jens Axboed625c6e2019-12-17 19:53:05 -07003777 switch (req->opcode) {
Jens Axboe09bb8392019-03-13 12:39:28 -06003778 case IORING_OP_NOP:
3779 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03003780 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03003781 case IORING_OP_TIMEOUT_REMOVE:
3782 case IORING_OP_ASYNC_CANCEL:
3783 case IORING_OP_LINK_TIMEOUT:
Jens Axboe9e3aa612019-12-11 15:55:43 -07003784 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003785 case IORING_OP_OPENAT:
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003786 case IORING_OP_STATX:
Jens Axboe15b71ab2019-12-11 11:20:36 -07003787 return fd != -1;
Jens Axboe09bb8392019-03-13 12:39:28 -06003788 default:
Jens Axboed625c6e2019-12-17 19:53:05 -07003789 if (io_req_op_valid(req->opcode))
Jens Axboe9e3aa612019-12-11 15:55:43 -07003790 return 1;
3791 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003792 }
3793}
3794
Jens Axboe65e19f52019-10-26 07:20:21 -06003795static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3796 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003797{
Jens Axboe65e19f52019-10-26 07:20:21 -06003798 struct fixed_file_table *table;
3799
Jens Axboe05f3fb32019-12-09 11:22:50 -07003800 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
3801 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06003802}
3803
Jens Axboe3529d8c2019-12-19 18:24:38 -07003804static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
3805 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06003806{
Jackie Liua197f662019-11-08 08:09:12 -07003807 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003808 unsigned flags;
Jens Axboe9e3aa612019-12-11 15:55:43 -07003809 int fd, ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003810
Jens Axboe3529d8c2019-12-19 18:24:38 -07003811 flags = READ_ONCE(sqe->flags);
3812 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003813
Jackie Liu4fe2c962019-09-09 20:50:40 +08003814 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003815 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003816
Jens Axboe15b71ab2019-12-11 11:20:36 -07003817 ret = io_req_needs_file(req, fd);
Jens Axboe9e3aa612019-12-11 15:55:43 -07003818 if (ret <= 0)
3819 return ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003820
3821 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07003822 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003823 (unsigned) fd >= ctx->nr_user_files))
3824 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003825 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003826 req->file = io_file_from_index(ctx, fd);
3827 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003828 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003829 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003830 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06003831 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003832 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003833 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003834 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003835 req->file = io_file_get(state, fd);
3836 if (unlikely(!req->file))
3837 return -EBADF;
3838 }
3839
3840 return 0;
3841}
3842
Jackie Liua197f662019-11-08 08:09:12 -07003843static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003844{
Jens Axboefcb323c2019-10-24 12:39:47 -06003845 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003846 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003847
Jens Axboeb5dba592019-12-11 14:02:38 -07003848 if (!req->ring_file)
3849 return -EBADF;
3850
Jens Axboefcb323c2019-10-24 12:39:47 -06003851 rcu_read_lock();
3852 spin_lock_irq(&ctx->inflight_lock);
3853 /*
3854 * We use the f_ops->flush() handler to ensure that we can flush
3855 * out work accessing these files if the fd is closed. Check if
3856 * the fd has changed since we started down this path, and disallow
3857 * this operation if it has.
3858 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003859 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003860 list_add(&req->inflight_entry, &ctx->inflight_list);
3861 req->flags |= REQ_F_INFLIGHT;
3862 req->work.files = current->files;
3863 ret = 0;
3864 }
3865 spin_unlock_irq(&ctx->inflight_lock);
3866 rcu_read_unlock();
3867
3868 return ret;
3869}
3870
Jens Axboe2665abf2019-11-05 12:40:47 -07003871static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3872{
Jens Axboead8a48a2019-11-15 08:49:11 -07003873 struct io_timeout_data *data = container_of(timer,
3874 struct io_timeout_data, timer);
3875 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003876 struct io_ring_ctx *ctx = req->ctx;
3877 struct io_kiocb *prev = NULL;
3878 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003879
3880 spin_lock_irqsave(&ctx->completion_lock, flags);
3881
3882 /*
3883 * We don't expect the list to be empty, that will only happen if we
3884 * race with the completion of the linked work.
3885 */
Pavel Begunkov44932332019-12-05 16:16:35 +03003886 if (!list_empty(&req->link_list)) {
3887 prev = list_entry(req->link_list.prev, struct io_kiocb,
3888 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003889 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03003890 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003891 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3892 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003893 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003894 }
3895
3896 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3897
3898 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003899 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003900 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3901 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003902 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003903 } else {
3904 io_cqring_add_event(req, -ETIME);
3905 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003906 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003907 return HRTIMER_NORESTART;
3908}
3909
Jens Axboead8a48a2019-11-15 08:49:11 -07003910static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003911{
Jens Axboe76a46e02019-11-10 23:34:16 -07003912 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07003913
Jens Axboe76a46e02019-11-10 23:34:16 -07003914 /*
3915 * If the list is now empty, then our linked request finished before
3916 * we got a chance to setup the timer
3917 */
3918 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03003919 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07003920 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003921
Jens Axboead8a48a2019-11-15 08:49:11 -07003922 data->timer.function = io_link_timeout_fn;
3923 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3924 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07003925 }
Jens Axboe76a46e02019-11-10 23:34:16 -07003926 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07003927
Jens Axboe2665abf2019-11-05 12:40:47 -07003928 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07003929 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003930}
3931
Jens Axboead8a48a2019-11-15 08:49:11 -07003932static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003933{
3934 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003935
Jens Axboe2665abf2019-11-05 12:40:47 -07003936 if (!(req->flags & REQ_F_LINK))
3937 return NULL;
3938
Pavel Begunkov44932332019-12-05 16:16:35 +03003939 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
3940 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07003941 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07003942 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003943
Jens Axboe76a46e02019-11-10 23:34:16 -07003944 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07003945 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07003946}
3947
Jens Axboe3529d8c2019-12-19 18:24:38 -07003948static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003949{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003950 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003951 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003952 int ret;
3953
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003954again:
3955 linked_timeout = io_prep_linked_timeout(req);
3956
Jens Axboe3529d8c2019-12-19 18:24:38 -07003957 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06003958
3959 /*
3960 * We async punt it if the file wasn't marked NOWAIT, or if the file
3961 * doesn't support non-blocking read/write attempts
3962 */
3963 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3964 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003965 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3966 ret = io_grab_files(req);
3967 if (ret)
3968 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003969 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003970
3971 /*
3972 * Queued up for async execution, worker will release
3973 * submit reference when the iocb is actually submitted.
3974 */
3975 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003976 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003977 }
Jens Axboee65ef562019-03-12 10:16:44 -06003978
Jens Axboefcb323c2019-10-24 12:39:47 -06003979err:
Jens Axboee65ef562019-03-12 10:16:44 -06003980 /* drop submission reference */
3981 io_put_req(req);
3982
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003983 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003984 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003985 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003986 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003987 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003988 }
3989
Jens Axboee65ef562019-03-12 10:16:44 -06003990 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003991 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003992 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003993 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003994 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003995 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003996done_req:
3997 if (nxt) {
3998 req = nxt;
3999 nxt = NULL;
4000 goto again;
4001 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004002}
4003
Jens Axboe3529d8c2019-12-19 18:24:38 -07004004static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004005{
4006 int ret;
4007
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004008 if (unlikely(req->ctx->drain_next)) {
4009 req->flags |= REQ_F_IO_DRAIN;
4010 req->ctx->drain_next = false;
4011 }
4012 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
4013
Jens Axboe3529d8c2019-12-19 18:24:38 -07004014 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004015 if (ret) {
4016 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004017 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004018 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004019 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004020 }
Jens Axboece35a472019-12-17 08:04:44 -07004021 } else if ((req->flags & REQ_F_FORCE_ASYNC) &&
4022 !io_wq_current_is_worker()) {
4023 /*
4024 * Never try inline submit of IOSQE_ASYNC is set, go straight
4025 * to async execution.
4026 */
4027 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4028 io_queue_async_work(req);
4029 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004030 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004031 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004032}
4033
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004034static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004035{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004036 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004037 io_cqring_add_event(req, -ECANCELED);
4038 io_double_put_req(req);
4039 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004040 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004041}
4042
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004043#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004044 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004045
Jens Axboe3529d8c2019-12-19 18:24:38 -07004046static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4047 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004048{
Jackie Liua197f662019-11-08 08:09:12 -07004049 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06004050 int ret;
4051
4052 /* enforce forwards compatibility on users */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004053 if (unlikely(sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004054 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004055 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004056 }
Jens Axboece35a472019-12-17 08:04:44 -07004057 if (sqe->flags & IOSQE_ASYNC)
4058 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004059
Jens Axboe3529d8c2019-12-19 18:24:38 -07004060 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004061 if (unlikely(ret)) {
4062err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004063 io_cqring_add_event(req, ret);
4064 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004065 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004066 }
4067
Jens Axboe9e645e112019-05-10 16:07:28 -06004068 /*
4069 * If we already have a head request, queue this one for async
4070 * submittal once the head completes. If we don't have a head but
4071 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4072 * submitted sync once the chain is complete. If none of those
4073 * conditions are true (normal request), then just queue it.
4074 */
4075 if (*link) {
4076 struct io_kiocb *prev = *link;
4077
Jens Axboe3529d8c2019-12-19 18:24:38 -07004078 if (sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004079 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
4080
Jens Axboe3529d8c2019-12-19 18:24:38 -07004081 if (sqe->flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004082 req->flags |= REQ_F_HARDLINK;
4083
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004084 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004085 ret = -EAGAIN;
4086 goto err_req;
4087 }
4088
Jens Axboe3529d8c2019-12-19 18:24:38 -07004089 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004090 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004091 /* fail even hard links since we don't submit */
Jens Axboe2d283902019-12-04 11:08:05 -07004092 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004093 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004094 }
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004095 trace_io_uring_link(ctx, req, prev);
Pavel Begunkov44932332019-12-05 16:16:35 +03004096 list_add_tail(&req->link_list, &prev->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004097 } else if (sqe->flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004098 req->flags |= REQ_F_LINK;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004099 if (sqe->flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004100 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004101
Jens Axboe9e645e112019-05-10 16:07:28 -06004102 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004103 ret = io_req_defer_prep(req, sqe);
4104 if (ret)
4105 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004106 *link = req;
4107 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004108 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004109 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004110
4111 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004112}
4113
Jens Axboe9a56a232019-01-09 09:06:50 -07004114/*
4115 * Batched submission is done, ensure local IO is flushed out.
4116 */
4117static void io_submit_state_end(struct io_submit_state *state)
4118{
4119 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004120 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004121 if (state->free_reqs)
4122 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4123 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004124}
4125
4126/*
4127 * Start submission side cache.
4128 */
4129static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004130 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004131{
4132 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004133 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004134 state->file = NULL;
4135 state->ios_left = max_ios;
4136}
4137
Jens Axboe2b188cc2019-01-07 10:46:33 -07004138static void io_commit_sqring(struct io_ring_ctx *ctx)
4139{
Hristo Venev75b28af2019-08-26 17:23:46 +00004140 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004141
Hristo Venev75b28af2019-08-26 17:23:46 +00004142 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004143 /*
4144 * Ensure any loads from the SQEs are done at this point,
4145 * since once we write the new head, the application could
4146 * write new data to them.
4147 */
Hristo Venev75b28af2019-08-26 17:23:46 +00004148 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004149 }
4150}
4151
4152/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004153 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004154 * that is mapped by userspace. This means that care needs to be taken to
4155 * ensure that reads are stable, as we cannot rely on userspace always
4156 * being a good citizen. If members of the sqe are validated and then later
4157 * used, it's important that those reads are done through READ_ONCE() to
4158 * prevent a re-load down the line.
4159 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004160static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4161 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004162{
Hristo Venev75b28af2019-08-26 17:23:46 +00004163 struct io_rings *rings = ctx->rings;
4164 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004165 unsigned head;
4166
4167 /*
4168 * The cached sq head (or cq tail) serves two purposes:
4169 *
4170 * 1) allows us to batch the cost of updating the user visible
4171 * head updates.
4172 * 2) allows the kernel side to track the head on its own, even
4173 * though the application is the one updating it.
4174 */
4175 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02004176 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004177 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004178 return false;
4179
Hristo Venev75b28af2019-08-26 17:23:46 +00004180 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004181 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004182 /*
4183 * All io need record the previous position, if LINK vs DARIN,
4184 * it can be used to mark the position of the first IO in the
4185 * link list.
4186 */
4187 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004188 *sqe_ptr = &ctx->sq_sqes[head];
4189 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4190 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004191 ctx->cached_sq_head++;
4192 return true;
4193 }
4194
4195 /* drop invalid entries */
4196 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004197 ctx->cached_sq_dropped++;
4198 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004199 return false;
4200}
4201
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004202static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004203 struct file *ring_file, int ring_fd,
4204 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004205{
4206 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004207 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004208 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004209 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004210
Jens Axboec4a2ed72019-11-21 21:01:26 -07004211 /* if we have a backlog and couldn't flush it all, return BUSY */
4212 if (!list_empty(&ctx->cq_overflow_list) &&
4213 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004214 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004215
4216 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004217 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004218 statep = &state;
4219 }
4220
4221 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004222 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004223 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03004224 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004225
Pavel Begunkov196be952019-11-07 01:41:06 +03004226 req = io_get_req(ctx, statep);
4227 if (unlikely(!req)) {
4228 if (!submitted)
4229 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004230 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004231 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004232 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03004233 __io_free_req(req);
4234 break;
4235 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004236
Jens Axboed625c6e2019-12-17 19:53:05 -07004237 if (io_req_needs_user(req) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004238 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4239 if (!mm_fault) {
4240 use_mm(ctx->sqo_mm);
4241 *mm = ctx->sqo_mm;
4242 }
4243 }
4244
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004245 submitted++;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004246 sqe_flags = sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03004247
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004248 req->ring_file = ring_file;
4249 req->ring_fd = ring_fd;
4250 req->has_user = *mm != NULL;
4251 req->in_async = async;
4252 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07004253 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004254 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004255 break;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03004256 /*
4257 * If previous wasn't linked and we have a linked command,
4258 * that's the end of the chain. Submit the previous link.
4259 */
Pavel Begunkovffbb8d62019-12-17 20:57:05 +03004260 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004261 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03004262 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004263 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004264 }
4265
Jens Axboe9e645e112019-05-10 16:07:28 -06004266 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004267 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004268 if (statep)
4269 io_submit_state_end(&state);
4270
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004271 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4272 io_commit_sqring(ctx);
4273
Jens Axboe6c271ce2019-01-10 11:22:30 -07004274 return submitted;
4275}
4276
4277static int io_sq_thread(void *data)
4278{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004279 struct io_ring_ctx *ctx = data;
4280 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004281 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004282 mm_segment_t old_fs;
4283 DEFINE_WAIT(wait);
4284 unsigned inflight;
4285 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004286 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004287
Jens Axboe206aefd2019-11-07 18:27:42 -07004288 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004289
Jens Axboe6c271ce2019-01-10 11:22:30 -07004290 old_fs = get_fs();
4291 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004292 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004293
Jens Axboec1edbf52019-11-10 16:56:04 -07004294 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004295 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004296 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004297
4298 if (inflight) {
4299 unsigned nr_events = 0;
4300
4301 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004302 /*
4303 * inflight is the count of the maximum possible
4304 * entries we submitted, but it can be smaller
4305 * if we dropped some of them. If we don't have
4306 * poll entries available, then we know that we
4307 * have nothing left to poll for. Reset the
4308 * inflight count to zero in that case.
4309 */
4310 mutex_lock(&ctx->uring_lock);
4311 if (!list_empty(&ctx->poll_list))
4312 __io_iopoll_check(ctx, &nr_events, 0);
4313 else
4314 inflight = 0;
4315 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004316 } else {
4317 /*
4318 * Normal IO, just pretend everything completed.
4319 * We don't have to poll completions for that.
4320 */
4321 nr_events = inflight;
4322 }
4323
4324 inflight -= nr_events;
4325 if (!inflight)
4326 timeout = jiffies + ctx->sq_thread_idle;
4327 }
4328
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004329 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004330
4331 /*
4332 * If submit got -EBUSY, flag us as needing the application
4333 * to enter the kernel to reap and flush events.
4334 */
4335 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004336 /*
4337 * We're polling. If we're within the defined idle
4338 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004339 * to sleep. The exception is if we got EBUSY doing
4340 * more IO, we should wait for the application to
4341 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004342 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004343 if (inflight ||
4344 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004345 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004346 continue;
4347 }
4348
4349 /*
4350 * Drop cur_mm before scheduling, we can't hold it for
4351 * long periods (or over schedule()). Do this before
4352 * adding ourselves to the waitqueue, as the unuse/drop
4353 * may sleep.
4354 */
4355 if (cur_mm) {
4356 unuse_mm(cur_mm);
4357 mmput(cur_mm);
4358 cur_mm = NULL;
4359 }
4360
4361 prepare_to_wait(&ctx->sqo_wait, &wait,
4362 TASK_INTERRUPTIBLE);
4363
4364 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004365 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004366 /* make sure to read SQ tail after writing flags */
4367 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004368
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004369 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004370 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004371 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004372 finish_wait(&ctx->sqo_wait, &wait);
4373 break;
4374 }
4375 if (signal_pending(current))
4376 flush_signals(current);
4377 schedule();
4378 finish_wait(&ctx->sqo_wait, &wait);
4379
Hristo Venev75b28af2019-08-26 17:23:46 +00004380 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004381 continue;
4382 }
4383 finish_wait(&ctx->sqo_wait, &wait);
4384
Hristo Venev75b28af2019-08-26 17:23:46 +00004385 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004386 }
4387
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004388 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004389 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004390 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004391 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004392 if (ret > 0)
4393 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004394 }
4395
4396 set_fs(old_fs);
4397 if (cur_mm) {
4398 unuse_mm(cur_mm);
4399 mmput(cur_mm);
4400 }
Jens Axboe181e4482019-11-25 08:52:30 -07004401 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004402
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004403 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004404
Jens Axboe6c271ce2019-01-10 11:22:30 -07004405 return 0;
4406}
4407
Jens Axboebda52162019-09-24 13:47:15 -06004408struct io_wait_queue {
4409 struct wait_queue_entry wq;
4410 struct io_ring_ctx *ctx;
4411 unsigned to_wait;
4412 unsigned nr_timeouts;
4413};
4414
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004415static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004416{
4417 struct io_ring_ctx *ctx = iowq->ctx;
4418
4419 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004420 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004421 * started waiting. For timeouts, we always want to return to userspace,
4422 * regardless of event count.
4423 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004424 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004425 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4426}
4427
4428static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4429 int wake_flags, void *key)
4430{
4431 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4432 wq);
4433
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004434 /* use noflush == true, as we can't safely rely on locking context */
4435 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004436 return -1;
4437
4438 return autoremove_wake_function(curr, mode, wake_flags, key);
4439}
4440
Jens Axboe2b188cc2019-01-07 10:46:33 -07004441/*
4442 * Wait until events become available, if we don't already have some. The
4443 * application must reap them itself, as they reside on the shared cq ring.
4444 */
4445static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4446 const sigset_t __user *sig, size_t sigsz)
4447{
Jens Axboebda52162019-09-24 13:47:15 -06004448 struct io_wait_queue iowq = {
4449 .wq = {
4450 .private = current,
4451 .func = io_wake_function,
4452 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4453 },
4454 .ctx = ctx,
4455 .to_wait = min_events,
4456 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004457 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004458 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004459
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004460 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004461 return 0;
4462
4463 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004464#ifdef CONFIG_COMPAT
4465 if (in_compat_syscall())
4466 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004467 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004468 else
4469#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004470 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004471
Jens Axboe2b188cc2019-01-07 10:46:33 -07004472 if (ret)
4473 return ret;
4474 }
4475
Jens Axboebda52162019-09-24 13:47:15 -06004476 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004477 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004478 do {
4479 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4480 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004481 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004482 break;
4483 schedule();
4484 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004485 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004486 break;
4487 }
4488 } while (1);
4489 finish_wait(&ctx->wait, &iowq.wq);
4490
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004491 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004492
Hristo Venev75b28af2019-08-26 17:23:46 +00004493 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004494}
4495
Jens Axboe6b063142019-01-10 22:13:58 -07004496static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4497{
4498#if defined(CONFIG_UNIX)
4499 if (ctx->ring_sock) {
4500 struct sock *sock = ctx->ring_sock->sk;
4501 struct sk_buff *skb;
4502
4503 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4504 kfree_skb(skb);
4505 }
4506#else
4507 int i;
4508
Jens Axboe65e19f52019-10-26 07:20:21 -06004509 for (i = 0; i < ctx->nr_user_files; i++) {
4510 struct file *file;
4511
4512 file = io_file_from_index(ctx, i);
4513 if (file)
4514 fput(file);
4515 }
Jens Axboe6b063142019-01-10 22:13:58 -07004516#endif
4517}
4518
Jens Axboe05f3fb32019-12-09 11:22:50 -07004519static void io_file_ref_kill(struct percpu_ref *ref)
4520{
4521 struct fixed_file_data *data;
4522
4523 data = container_of(ref, struct fixed_file_data, refs);
4524 complete(&data->done);
4525}
4526
Jens Axboe6b063142019-01-10 22:13:58 -07004527static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4528{
Jens Axboe05f3fb32019-12-09 11:22:50 -07004529 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06004530 unsigned nr_tables, i;
4531
Jens Axboe05f3fb32019-12-09 11:22:50 -07004532 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07004533 return -ENXIO;
4534
Jens Axboe05f3fb32019-12-09 11:22:50 -07004535 /* protect against inflight atomic switch, which drops the ref */
4536 flush_work(&data->ref_work);
4537 percpu_ref_get(&data->refs);
4538 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
4539 wait_for_completion(&data->done);
4540 percpu_ref_put(&data->refs);
4541 percpu_ref_exit(&data->refs);
4542
Jens Axboe6b063142019-01-10 22:13:58 -07004543 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004544 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4545 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004546 kfree(data->table[i].files);
4547 kfree(data->table);
4548 kfree(data);
4549 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004550 ctx->nr_user_files = 0;
4551 return 0;
4552}
4553
Jens Axboe6c271ce2019-01-10 11:22:30 -07004554static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4555{
4556 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004557 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004558 /*
4559 * The park is a bit of a work-around, without it we get
4560 * warning spews on shutdown with SQPOLL set and affinity
4561 * set to a single CPU.
4562 */
Jens Axboe06058632019-04-13 09:26:03 -06004563 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004564 kthread_stop(ctx->sqo_thread);
4565 ctx->sqo_thread = NULL;
4566 }
4567}
4568
Jens Axboe6b063142019-01-10 22:13:58 -07004569static void io_finish_async(struct io_ring_ctx *ctx)
4570{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004571 io_sq_thread_stop(ctx);
4572
Jens Axboe561fb042019-10-24 07:25:42 -06004573 if (ctx->io_wq) {
4574 io_wq_destroy(ctx->io_wq);
4575 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004576 }
4577}
4578
4579#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07004580/*
4581 * Ensure the UNIX gc is aware of our file set, so we are certain that
4582 * the io_uring can be safely unregistered on process exit, even if we have
4583 * loops in the file referencing.
4584 */
4585static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4586{
4587 struct sock *sk = ctx->ring_sock->sk;
4588 struct scm_fp_list *fpl;
4589 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004590 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004591
4592 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4593 unsigned long inflight = ctx->user->unix_inflight + nr;
4594
4595 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4596 return -EMFILE;
4597 }
4598
4599 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4600 if (!fpl)
4601 return -ENOMEM;
4602
4603 skb = alloc_skb(0, GFP_KERNEL);
4604 if (!skb) {
4605 kfree(fpl);
4606 return -ENOMEM;
4607 }
4608
4609 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004610
Jens Axboe08a45172019-10-03 08:11:03 -06004611 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004612 fpl->user = get_uid(ctx->user);
4613 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004614 struct file *file = io_file_from_index(ctx, i + offset);
4615
4616 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004617 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004618 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004619 unix_inflight(fpl->user, fpl->fp[nr_files]);
4620 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004621 }
4622
Jens Axboe08a45172019-10-03 08:11:03 -06004623 if (nr_files) {
4624 fpl->max = SCM_MAX_FD;
4625 fpl->count = nr_files;
4626 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004627 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06004628 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4629 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004630
Jens Axboe08a45172019-10-03 08:11:03 -06004631 for (i = 0; i < nr_files; i++)
4632 fput(fpl->fp[i]);
4633 } else {
4634 kfree_skb(skb);
4635 kfree(fpl);
4636 }
Jens Axboe6b063142019-01-10 22:13:58 -07004637
4638 return 0;
4639}
4640
4641/*
4642 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4643 * causes regular reference counting to break down. We rely on the UNIX
4644 * garbage collection to take care of this problem for us.
4645 */
4646static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4647{
4648 unsigned left, total;
4649 int ret = 0;
4650
4651 total = 0;
4652 left = ctx->nr_user_files;
4653 while (left) {
4654 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004655
4656 ret = __io_sqe_files_scm(ctx, this_files, total);
4657 if (ret)
4658 break;
4659 left -= this_files;
4660 total += this_files;
4661 }
4662
4663 if (!ret)
4664 return 0;
4665
4666 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004667 struct file *file = io_file_from_index(ctx, total);
4668
4669 if (file)
4670 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004671 total++;
4672 }
4673
4674 return ret;
4675}
4676#else
4677static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4678{
4679 return 0;
4680}
4681#endif
4682
Jens Axboe65e19f52019-10-26 07:20:21 -06004683static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4684 unsigned nr_files)
4685{
4686 int i;
4687
4688 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004689 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004690 unsigned this_files;
4691
4692 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4693 table->files = kcalloc(this_files, sizeof(struct file *),
4694 GFP_KERNEL);
4695 if (!table->files)
4696 break;
4697 nr_files -= this_files;
4698 }
4699
4700 if (i == nr_tables)
4701 return 0;
4702
4703 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004704 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004705 kfree(table->files);
4706 }
4707 return 1;
4708}
4709
Jens Axboe05f3fb32019-12-09 11:22:50 -07004710static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06004711{
4712#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06004713 struct sock *sock = ctx->ring_sock->sk;
4714 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4715 struct sk_buff *skb;
4716 int i;
4717
4718 __skb_queue_head_init(&list);
4719
4720 /*
4721 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4722 * remove this entry and rearrange the file array.
4723 */
4724 skb = skb_dequeue(head);
4725 while (skb) {
4726 struct scm_fp_list *fp;
4727
4728 fp = UNIXCB(skb).fp;
4729 for (i = 0; i < fp->count; i++) {
4730 int left;
4731
4732 if (fp->fp[i] != file)
4733 continue;
4734
4735 unix_notinflight(fp->user, fp->fp[i]);
4736 left = fp->count - 1 - i;
4737 if (left) {
4738 memmove(&fp->fp[i], &fp->fp[i + 1],
4739 left * sizeof(struct file *));
4740 }
4741 fp->count--;
4742 if (!fp->count) {
4743 kfree_skb(skb);
4744 skb = NULL;
4745 } else {
4746 __skb_queue_tail(&list, skb);
4747 }
4748 fput(file);
4749 file = NULL;
4750 break;
4751 }
4752
4753 if (!file)
4754 break;
4755
4756 __skb_queue_tail(&list, skb);
4757
4758 skb = skb_dequeue(head);
4759 }
4760
4761 if (skb_peek(&list)) {
4762 spin_lock_irq(&head->lock);
4763 while ((skb = __skb_dequeue(&list)) != NULL)
4764 __skb_queue_tail(head, skb);
4765 spin_unlock_irq(&head->lock);
4766 }
4767#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07004768 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06004769#endif
4770}
4771
Jens Axboe05f3fb32019-12-09 11:22:50 -07004772struct io_file_put {
4773 struct llist_node llist;
4774 struct file *file;
4775 struct completion *done;
4776};
4777
4778static void io_ring_file_ref_switch(struct work_struct *work)
4779{
4780 struct io_file_put *pfile, *tmp;
4781 struct fixed_file_data *data;
4782 struct llist_node *node;
4783
4784 data = container_of(work, struct fixed_file_data, ref_work);
4785
4786 while ((node = llist_del_all(&data->put_llist)) != NULL) {
4787 llist_for_each_entry_safe(pfile, tmp, node, llist) {
4788 io_ring_file_put(data->ctx, pfile->file);
4789 if (pfile->done)
4790 complete(pfile->done);
4791 else
4792 kfree(pfile);
4793 }
4794 }
4795
4796 percpu_ref_get(&data->refs);
4797 percpu_ref_switch_to_percpu(&data->refs);
4798}
4799
4800static void io_file_data_ref_zero(struct percpu_ref *ref)
4801{
4802 struct fixed_file_data *data;
4803
4804 data = container_of(ref, struct fixed_file_data, refs);
4805
4806 /* we can't safely switch from inside this context, punt to wq */
4807 queue_work(system_wq, &data->ref_work);
4808}
4809
4810static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
4811 unsigned nr_args)
4812{
4813 __s32 __user *fds = (__s32 __user *) arg;
4814 unsigned nr_tables;
4815 struct file *file;
4816 int fd, ret = 0;
4817 unsigned i;
4818
4819 if (ctx->file_data)
4820 return -EBUSY;
4821 if (!nr_args)
4822 return -EINVAL;
4823 if (nr_args > IORING_MAX_FIXED_FILES)
4824 return -EMFILE;
4825
4826 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
4827 if (!ctx->file_data)
4828 return -ENOMEM;
4829 ctx->file_data->ctx = ctx;
4830 init_completion(&ctx->file_data->done);
4831
4832 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
4833 ctx->file_data->table = kcalloc(nr_tables,
4834 sizeof(struct fixed_file_table),
4835 GFP_KERNEL);
4836 if (!ctx->file_data->table) {
4837 kfree(ctx->file_data);
4838 ctx->file_data = NULL;
4839 return -ENOMEM;
4840 }
4841
4842 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
4843 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
4844 kfree(ctx->file_data->table);
4845 kfree(ctx->file_data);
4846 ctx->file_data = NULL;
4847 return -ENOMEM;
4848 }
4849 ctx->file_data->put_llist.first = NULL;
4850 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
4851
4852 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
4853 percpu_ref_exit(&ctx->file_data->refs);
4854 kfree(ctx->file_data->table);
4855 kfree(ctx->file_data);
4856 ctx->file_data = NULL;
4857 return -ENOMEM;
4858 }
4859
4860 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
4861 struct fixed_file_table *table;
4862 unsigned index;
4863
4864 ret = -EFAULT;
4865 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
4866 break;
4867 /* allow sparse sets */
4868 if (fd == -1) {
4869 ret = 0;
4870 continue;
4871 }
4872
4873 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
4874 index = i & IORING_FILE_TABLE_MASK;
4875 file = fget(fd);
4876
4877 ret = -EBADF;
4878 if (!file)
4879 break;
4880
4881 /*
4882 * Don't allow io_uring instances to be registered. If UNIX
4883 * isn't enabled, then this causes a reference cycle and this
4884 * instance can never get freed. If UNIX is enabled we'll
4885 * handle it just fine, but there's still no point in allowing
4886 * a ring fd as it doesn't support regular read/write anyway.
4887 */
4888 if (file->f_op == &io_uring_fops) {
4889 fput(file);
4890 break;
4891 }
4892 ret = 0;
4893 table->files[index] = file;
4894 }
4895
4896 if (ret) {
4897 for (i = 0; i < ctx->nr_user_files; i++) {
4898 file = io_file_from_index(ctx, i);
4899 if (file)
4900 fput(file);
4901 }
4902 for (i = 0; i < nr_tables; i++)
4903 kfree(ctx->file_data->table[i].files);
4904
4905 kfree(ctx->file_data->table);
4906 kfree(ctx->file_data);
4907 ctx->file_data = NULL;
4908 ctx->nr_user_files = 0;
4909 return ret;
4910 }
4911
4912 ret = io_sqe_files_scm(ctx);
4913 if (ret)
4914 io_sqe_files_unregister(ctx);
4915
4916 return ret;
4917}
4918
Jens Axboec3a31e62019-10-03 13:59:56 -06004919static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4920 int index)
4921{
4922#if defined(CONFIG_UNIX)
4923 struct sock *sock = ctx->ring_sock->sk;
4924 struct sk_buff_head *head = &sock->sk_receive_queue;
4925 struct sk_buff *skb;
4926
4927 /*
4928 * See if we can merge this file into an existing skb SCM_RIGHTS
4929 * file set. If there's no room, fall back to allocating a new skb
4930 * and filling it in.
4931 */
4932 spin_lock_irq(&head->lock);
4933 skb = skb_peek(head);
4934 if (skb) {
4935 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4936
4937 if (fpl->count < SCM_MAX_FD) {
4938 __skb_unlink(skb, head);
4939 spin_unlock_irq(&head->lock);
4940 fpl->fp[fpl->count] = get_file(file);
4941 unix_inflight(fpl->user, fpl->fp[fpl->count]);
4942 fpl->count++;
4943 spin_lock_irq(&head->lock);
4944 __skb_queue_head(head, skb);
4945 } else {
4946 skb = NULL;
4947 }
4948 }
4949 spin_unlock_irq(&head->lock);
4950
4951 if (skb) {
4952 fput(file);
4953 return 0;
4954 }
4955
4956 return __io_sqe_files_scm(ctx, 1, index);
4957#else
4958 return 0;
4959#endif
4960}
4961
Jens Axboe05f3fb32019-12-09 11:22:50 -07004962static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06004963{
Jens Axboe05f3fb32019-12-09 11:22:50 -07004964 struct fixed_file_data *data;
4965
4966 data = container_of(ref, struct fixed_file_data, refs);
4967 clear_bit(FFD_F_ATOMIC, &data->state);
4968}
4969
4970static bool io_queue_file_removal(struct fixed_file_data *data,
4971 struct file *file)
4972{
4973 struct io_file_put *pfile, pfile_stack;
4974 DECLARE_COMPLETION_ONSTACK(done);
4975
4976 /*
4977 * If we fail allocating the struct we need for doing async reomval
4978 * of this file, just punt to sync and wait for it.
4979 */
4980 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
4981 if (!pfile) {
4982 pfile = &pfile_stack;
4983 pfile->done = &done;
4984 }
4985
4986 pfile->file = file;
4987 llist_add(&pfile->llist, &data->put_llist);
4988
4989 if (pfile == &pfile_stack) {
4990 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
4991 percpu_ref_put(&data->refs);
4992 percpu_ref_switch_to_atomic(&data->refs,
4993 io_atomic_switch);
4994 }
4995 wait_for_completion(&done);
4996 flush_work(&data->ref_work);
4997 return false;
4998 }
4999
5000 return true;
5001}
5002
5003static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5004 struct io_uring_files_update *up,
5005 unsigned nr_args)
5006{
5007 struct fixed_file_data *data = ctx->file_data;
5008 bool ref_switch = false;
5009 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005010 __s32 __user *fds;
5011 int fd, i, err;
5012 __u32 done;
5013
Jens Axboe05f3fb32019-12-09 11:22:50 -07005014 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005015 return -EOVERFLOW;
5016 if (done > ctx->nr_user_files)
5017 return -EINVAL;
5018
5019 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005020 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005021 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005022 struct fixed_file_table *table;
5023 unsigned index;
5024
Jens Axboec3a31e62019-10-03 13:59:56 -06005025 err = 0;
5026 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5027 err = -EFAULT;
5028 break;
5029 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005030 i = array_index_nospec(up->offset, ctx->nr_user_files);
5031 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005032 index = i & IORING_FILE_TABLE_MASK;
5033 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005034 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005035 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005036 if (io_queue_file_removal(data, file))
5037 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005038 }
5039 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005040 file = fget(fd);
5041 if (!file) {
5042 err = -EBADF;
5043 break;
5044 }
5045 /*
5046 * Don't allow io_uring instances to be registered. If
5047 * UNIX isn't enabled, then this causes a reference
5048 * cycle and this instance can never get freed. If UNIX
5049 * is enabled we'll handle it just fine, but there's
5050 * still no point in allowing a ring fd as it doesn't
5051 * support regular read/write anyway.
5052 */
5053 if (file->f_op == &io_uring_fops) {
5054 fput(file);
5055 err = -EBADF;
5056 break;
5057 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005058 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005059 err = io_sqe_file_register(ctx, file, i);
5060 if (err)
5061 break;
5062 }
5063 nr_args--;
5064 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005065 up->offset++;
5066 }
5067
5068 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5069 percpu_ref_put(&data->refs);
5070 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005071 }
5072
5073 return done ? done : err;
5074}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005075static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5076 unsigned nr_args)
5077{
5078 struct io_uring_files_update up;
5079
5080 if (!ctx->file_data)
5081 return -ENXIO;
5082 if (!nr_args)
5083 return -EINVAL;
5084 if (copy_from_user(&up, arg, sizeof(up)))
5085 return -EFAULT;
5086 if (up.resv)
5087 return -EINVAL;
5088
5089 return __io_sqe_files_update(ctx, &up, nr_args);
5090}
Jens Axboec3a31e62019-10-03 13:59:56 -06005091
Jens Axboe7d723062019-11-12 22:31:31 -07005092static void io_put_work(struct io_wq_work *work)
5093{
5094 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5095
5096 io_put_req(req);
5097}
5098
5099static void io_get_work(struct io_wq_work *work)
5100{
5101 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5102
5103 refcount_inc(&req->refs);
5104}
5105
Jens Axboe6c271ce2019-01-10 11:22:30 -07005106static int io_sq_offload_start(struct io_ring_ctx *ctx,
5107 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005108{
Jens Axboe576a3472019-11-25 08:49:20 -07005109 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005110 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005111 int ret;
5112
Jens Axboe6c271ce2019-01-10 11:22:30 -07005113 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005114 mmgrab(current->mm);
5115 ctx->sqo_mm = current->mm;
5116
Jens Axboe6c271ce2019-01-10 11:22:30 -07005117 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005118 ret = -EPERM;
5119 if (!capable(CAP_SYS_ADMIN))
5120 goto err;
5121
Jens Axboe917257d2019-04-13 09:28:55 -06005122 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5123 if (!ctx->sq_thread_idle)
5124 ctx->sq_thread_idle = HZ;
5125
Jens Axboe6c271ce2019-01-10 11:22:30 -07005126 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005127 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005128
Jens Axboe917257d2019-04-13 09:28:55 -06005129 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005130 if (cpu >= nr_cpu_ids)
5131 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005132 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005133 goto err;
5134
Jens Axboe6c271ce2019-01-10 11:22:30 -07005135 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5136 ctx, cpu,
5137 "io_uring-sq");
5138 } else {
5139 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5140 "io_uring-sq");
5141 }
5142 if (IS_ERR(ctx->sqo_thread)) {
5143 ret = PTR_ERR(ctx->sqo_thread);
5144 ctx->sqo_thread = NULL;
5145 goto err;
5146 }
5147 wake_up_process(ctx->sqo_thread);
5148 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5149 /* Can't have SQ_AFF without SQPOLL */
5150 ret = -EINVAL;
5151 goto err;
5152 }
5153
Jens Axboe576a3472019-11-25 08:49:20 -07005154 data.mm = ctx->sqo_mm;
5155 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005156 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005157 data.get_work = io_get_work;
5158 data.put_work = io_put_work;
5159
Jens Axboe561fb042019-10-24 07:25:42 -06005160 /* Do QD, or 4 * CPUS, whatever is smallest */
5161 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005162 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005163 if (IS_ERR(ctx->io_wq)) {
5164 ret = PTR_ERR(ctx->io_wq);
5165 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005166 goto err;
5167 }
5168
5169 return 0;
5170err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005171 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005172 mmdrop(ctx->sqo_mm);
5173 ctx->sqo_mm = NULL;
5174 return ret;
5175}
5176
5177static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5178{
5179 atomic_long_sub(nr_pages, &user->locked_vm);
5180}
5181
5182static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5183{
5184 unsigned long page_limit, cur_pages, new_pages;
5185
5186 /* Don't allow more pages than we can safely lock */
5187 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5188
5189 do {
5190 cur_pages = atomic_long_read(&user->locked_vm);
5191 new_pages = cur_pages + nr_pages;
5192 if (new_pages > page_limit)
5193 return -ENOMEM;
5194 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5195 new_pages) != cur_pages);
5196
5197 return 0;
5198}
5199
5200static void io_mem_free(void *ptr)
5201{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005202 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005203
Mark Rutland52e04ef2019-04-30 17:30:21 +01005204 if (!ptr)
5205 return;
5206
5207 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005208 if (put_page_testzero(page))
5209 free_compound_page(page);
5210}
5211
5212static void *io_mem_alloc(size_t size)
5213{
5214 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5215 __GFP_NORETRY;
5216
5217 return (void *) __get_free_pages(gfp_flags, get_order(size));
5218}
5219
Hristo Venev75b28af2019-08-26 17:23:46 +00005220static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5221 size_t *sq_offset)
5222{
5223 struct io_rings *rings;
5224 size_t off, sq_array_size;
5225
5226 off = struct_size(rings, cqes, cq_entries);
5227 if (off == SIZE_MAX)
5228 return SIZE_MAX;
5229
5230#ifdef CONFIG_SMP
5231 off = ALIGN(off, SMP_CACHE_BYTES);
5232 if (off == 0)
5233 return SIZE_MAX;
5234#endif
5235
5236 sq_array_size = array_size(sizeof(u32), sq_entries);
5237 if (sq_array_size == SIZE_MAX)
5238 return SIZE_MAX;
5239
5240 if (check_add_overflow(off, sq_array_size, &off))
5241 return SIZE_MAX;
5242
5243 if (sq_offset)
5244 *sq_offset = off;
5245
5246 return off;
5247}
5248
Jens Axboe2b188cc2019-01-07 10:46:33 -07005249static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5250{
Hristo Venev75b28af2019-08-26 17:23:46 +00005251 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005252
Hristo Venev75b28af2019-08-26 17:23:46 +00005253 pages = (size_t)1 << get_order(
5254 rings_size(sq_entries, cq_entries, NULL));
5255 pages += (size_t)1 << get_order(
5256 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005257
Hristo Venev75b28af2019-08-26 17:23:46 +00005258 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005259}
5260
Jens Axboeedafcce2019-01-09 09:16:05 -07005261static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5262{
5263 int i, j;
5264
5265 if (!ctx->user_bufs)
5266 return -ENXIO;
5267
5268 for (i = 0; i < ctx->nr_user_bufs; i++) {
5269 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5270
5271 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005272 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005273
5274 if (ctx->account_mem)
5275 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005276 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005277 imu->nr_bvecs = 0;
5278 }
5279
5280 kfree(ctx->user_bufs);
5281 ctx->user_bufs = NULL;
5282 ctx->nr_user_bufs = 0;
5283 return 0;
5284}
5285
5286static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5287 void __user *arg, unsigned index)
5288{
5289 struct iovec __user *src;
5290
5291#ifdef CONFIG_COMPAT
5292 if (ctx->compat) {
5293 struct compat_iovec __user *ciovs;
5294 struct compat_iovec ciov;
5295
5296 ciovs = (struct compat_iovec __user *) arg;
5297 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5298 return -EFAULT;
5299
Jens Axboed55e5f52019-12-11 16:12:15 -07005300 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005301 dst->iov_len = ciov.iov_len;
5302 return 0;
5303 }
5304#endif
5305 src = (struct iovec __user *) arg;
5306 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5307 return -EFAULT;
5308 return 0;
5309}
5310
5311static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5312 unsigned nr_args)
5313{
5314 struct vm_area_struct **vmas = NULL;
5315 struct page **pages = NULL;
5316 int i, j, got_pages = 0;
5317 int ret = -EINVAL;
5318
5319 if (ctx->user_bufs)
5320 return -EBUSY;
5321 if (!nr_args || nr_args > UIO_MAXIOV)
5322 return -EINVAL;
5323
5324 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5325 GFP_KERNEL);
5326 if (!ctx->user_bufs)
5327 return -ENOMEM;
5328
5329 for (i = 0; i < nr_args; i++) {
5330 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5331 unsigned long off, start, end, ubuf;
5332 int pret, nr_pages;
5333 struct iovec iov;
5334 size_t size;
5335
5336 ret = io_copy_iov(ctx, &iov, arg, i);
5337 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005338 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005339
5340 /*
5341 * Don't impose further limits on the size and buffer
5342 * constraints here, we'll -EINVAL later when IO is
5343 * submitted if they are wrong.
5344 */
5345 ret = -EFAULT;
5346 if (!iov.iov_base || !iov.iov_len)
5347 goto err;
5348
5349 /* arbitrary limit, but we need something */
5350 if (iov.iov_len > SZ_1G)
5351 goto err;
5352
5353 ubuf = (unsigned long) iov.iov_base;
5354 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5355 start = ubuf >> PAGE_SHIFT;
5356 nr_pages = end - start;
5357
5358 if (ctx->account_mem) {
5359 ret = io_account_mem(ctx->user, nr_pages);
5360 if (ret)
5361 goto err;
5362 }
5363
5364 ret = 0;
5365 if (!pages || nr_pages > got_pages) {
5366 kfree(vmas);
5367 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005368 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005369 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005370 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005371 sizeof(struct vm_area_struct *),
5372 GFP_KERNEL);
5373 if (!pages || !vmas) {
5374 ret = -ENOMEM;
5375 if (ctx->account_mem)
5376 io_unaccount_mem(ctx->user, nr_pages);
5377 goto err;
5378 }
5379 got_pages = nr_pages;
5380 }
5381
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005382 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005383 GFP_KERNEL);
5384 ret = -ENOMEM;
5385 if (!imu->bvec) {
5386 if (ctx->account_mem)
5387 io_unaccount_mem(ctx->user, nr_pages);
5388 goto err;
5389 }
5390
5391 ret = 0;
5392 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005393 pret = get_user_pages(ubuf, nr_pages,
5394 FOLL_WRITE | FOLL_LONGTERM,
5395 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005396 if (pret == nr_pages) {
5397 /* don't support file backed memory */
5398 for (j = 0; j < nr_pages; j++) {
5399 struct vm_area_struct *vma = vmas[j];
5400
5401 if (vma->vm_file &&
5402 !is_file_hugepages(vma->vm_file)) {
5403 ret = -EOPNOTSUPP;
5404 break;
5405 }
5406 }
5407 } else {
5408 ret = pret < 0 ? pret : -EFAULT;
5409 }
5410 up_read(&current->mm->mmap_sem);
5411 if (ret) {
5412 /*
5413 * if we did partial map, or found file backed vmas,
5414 * release any pages we did get
5415 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005416 if (pret > 0)
5417 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005418 if (ctx->account_mem)
5419 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005420 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005421 goto err;
5422 }
5423
5424 off = ubuf & ~PAGE_MASK;
5425 size = iov.iov_len;
5426 for (j = 0; j < nr_pages; j++) {
5427 size_t vec_len;
5428
5429 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5430 imu->bvec[j].bv_page = pages[j];
5431 imu->bvec[j].bv_len = vec_len;
5432 imu->bvec[j].bv_offset = off;
5433 off = 0;
5434 size -= vec_len;
5435 }
5436 /* store original address for later verification */
5437 imu->ubuf = ubuf;
5438 imu->len = iov.iov_len;
5439 imu->nr_bvecs = nr_pages;
5440
5441 ctx->nr_user_bufs++;
5442 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005443 kvfree(pages);
5444 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005445 return 0;
5446err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005447 kvfree(pages);
5448 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005449 io_sqe_buffer_unregister(ctx);
5450 return ret;
5451}
5452
Jens Axboe9b402842019-04-11 11:45:41 -06005453static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5454{
5455 __s32 __user *fds = arg;
5456 int fd;
5457
5458 if (ctx->cq_ev_fd)
5459 return -EBUSY;
5460
5461 if (copy_from_user(&fd, fds, sizeof(*fds)))
5462 return -EFAULT;
5463
5464 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5465 if (IS_ERR(ctx->cq_ev_fd)) {
5466 int ret = PTR_ERR(ctx->cq_ev_fd);
5467 ctx->cq_ev_fd = NULL;
5468 return ret;
5469 }
5470
5471 return 0;
5472}
5473
5474static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5475{
5476 if (ctx->cq_ev_fd) {
5477 eventfd_ctx_put(ctx->cq_ev_fd);
5478 ctx->cq_ev_fd = NULL;
5479 return 0;
5480 }
5481
5482 return -ENXIO;
5483}
5484
Jens Axboe2b188cc2019-01-07 10:46:33 -07005485static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5486{
Jens Axboe6b063142019-01-10 22:13:58 -07005487 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005488 if (ctx->sqo_mm)
5489 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005490
5491 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005492 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005493 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005494 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005495
Jens Axboe2b188cc2019-01-07 10:46:33 -07005496#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005497 if (ctx->ring_sock) {
5498 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005499 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005500 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005501#endif
5502
Hristo Venev75b28af2019-08-26 17:23:46 +00005503 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005504 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005505
5506 percpu_ref_exit(&ctx->refs);
5507 if (ctx->account_mem)
5508 io_unaccount_mem(ctx->user,
5509 ring_pages(ctx->sq_entries, ctx->cq_entries));
5510 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07005511 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07005512 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07005513 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07005514 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005515 kfree(ctx);
5516}
5517
5518static __poll_t io_uring_poll(struct file *file, poll_table *wait)
5519{
5520 struct io_ring_ctx *ctx = file->private_data;
5521 __poll_t mask = 0;
5522
5523 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02005524 /*
5525 * synchronizes with barrier from wq_has_sleeper call in
5526 * io_commit_cqring
5527 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005528 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00005529 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
5530 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005531 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08005532 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005533 mask |= EPOLLIN | EPOLLRDNORM;
5534
5535 return mask;
5536}
5537
5538static int io_uring_fasync(int fd, struct file *file, int on)
5539{
5540 struct io_ring_ctx *ctx = file->private_data;
5541
5542 return fasync_helper(fd, file, on, &ctx->cq_fasync);
5543}
5544
5545static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
5546{
5547 mutex_lock(&ctx->uring_lock);
5548 percpu_ref_kill(&ctx->refs);
5549 mutex_unlock(&ctx->uring_lock);
5550
Jens Axboe5262f562019-09-17 12:26:57 -06005551 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005552 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06005553
5554 if (ctx->io_wq)
5555 io_wq_cancel_all(ctx->io_wq);
5556
Jens Axboedef596e2019-01-09 08:59:42 -07005557 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07005558 /* if we failed setting up the ctx, we might not have any rings */
5559 if (ctx->rings)
5560 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07005561 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005562 io_ring_ctx_free(ctx);
5563}
5564
5565static int io_uring_release(struct inode *inode, struct file *file)
5566{
5567 struct io_ring_ctx *ctx = file->private_data;
5568
5569 file->private_data = NULL;
5570 io_ring_ctx_wait_and_kill(ctx);
5571 return 0;
5572}
5573
Jens Axboefcb323c2019-10-24 12:39:47 -06005574static void io_uring_cancel_files(struct io_ring_ctx *ctx,
5575 struct files_struct *files)
5576{
5577 struct io_kiocb *req;
5578 DEFINE_WAIT(wait);
5579
5580 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07005581 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06005582
5583 spin_lock_irq(&ctx->inflight_lock);
5584 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07005585 if (req->work.files != files)
5586 continue;
5587 /* req is being completed, ignore */
5588 if (!refcount_inc_not_zero(&req->refs))
5589 continue;
5590 cancel_req = req;
5591 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06005592 }
Jens Axboe768134d2019-11-10 20:30:53 -07005593 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005594 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07005595 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06005596 spin_unlock_irq(&ctx->inflight_lock);
5597
Jens Axboe768134d2019-11-10 20:30:53 -07005598 /* We need to keep going until we don't find a matching req */
5599 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005600 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005601
5602 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5603 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005604 schedule();
5605 }
Jens Axboe768134d2019-11-10 20:30:53 -07005606 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005607}
5608
5609static int io_uring_flush(struct file *file, void *data)
5610{
5611 struct io_ring_ctx *ctx = file->private_data;
5612
5613 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005614 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5615 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005616 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005617 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005618 return 0;
5619}
5620
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005621static void *io_uring_validate_mmap_request(struct file *file,
5622 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005623{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005624 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005625 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005626 struct page *page;
5627 void *ptr;
5628
5629 switch (offset) {
5630 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005631 case IORING_OFF_CQ_RING:
5632 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005633 break;
5634 case IORING_OFF_SQES:
5635 ptr = ctx->sq_sqes;
5636 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005637 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005638 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005639 }
5640
5641 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005642 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005643 return ERR_PTR(-EINVAL);
5644
5645 return ptr;
5646}
5647
5648#ifdef CONFIG_MMU
5649
5650static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5651{
5652 size_t sz = vma->vm_end - vma->vm_start;
5653 unsigned long pfn;
5654 void *ptr;
5655
5656 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5657 if (IS_ERR(ptr))
5658 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005659
5660 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5661 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5662}
5663
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005664#else /* !CONFIG_MMU */
5665
5666static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5667{
5668 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5669}
5670
5671static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5672{
5673 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5674}
5675
5676static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5677 unsigned long addr, unsigned long len,
5678 unsigned long pgoff, unsigned long flags)
5679{
5680 void *ptr;
5681
5682 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5683 if (IS_ERR(ptr))
5684 return PTR_ERR(ptr);
5685
5686 return (unsigned long) ptr;
5687}
5688
5689#endif /* !CONFIG_MMU */
5690
Jens Axboe2b188cc2019-01-07 10:46:33 -07005691SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5692 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5693 size_t, sigsz)
5694{
5695 struct io_ring_ctx *ctx;
5696 long ret = -EBADF;
5697 int submitted = 0;
5698 struct fd f;
5699
Jens Axboe6c271ce2019-01-10 11:22:30 -07005700 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005701 return -EINVAL;
5702
5703 f = fdget(fd);
5704 if (!f.file)
5705 return -EBADF;
5706
5707 ret = -EOPNOTSUPP;
5708 if (f.file->f_op != &io_uring_fops)
5709 goto out_fput;
5710
5711 ret = -ENXIO;
5712 ctx = f.file->private_data;
5713 if (!percpu_ref_tryget(&ctx->refs))
5714 goto out_fput;
5715
Jens Axboe6c271ce2019-01-10 11:22:30 -07005716 /*
5717 * For SQ polling, the thread will do all submissions and completions.
5718 * Just return the requested submit count, and wake the thread if
5719 * we were asked to.
5720 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005721 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005722 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005723 if (!list_empty_careful(&ctx->cq_overflow_list))
5724 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005725 if (flags & IORING_ENTER_SQ_WAKEUP)
5726 wake_up(&ctx->sqo_wait);
5727 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005728 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005729 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005730
Jens Axboe44d28272020-01-16 19:00:24 -07005731 if (current->mm != ctx->sqo_mm ||
5732 current_cred() != ctx->creds) {
5733 ret = -EPERM;
5734 goto out;
5735 }
5736
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005737 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005738 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005739 /* already have mm, so io_submit_sqes() won't try to grab it */
5740 cur_mm = ctx->sqo_mm;
5741 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
5742 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005743 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005744
5745 if (submitted != to_submit)
5746 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005747 }
5748 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07005749 unsigned nr_events = 0;
5750
Jens Axboe2b188cc2019-01-07 10:46:33 -07005751 min_complete = min(min_complete, ctx->cq_entries);
5752
Jens Axboedef596e2019-01-09 08:59:42 -07005753 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07005754 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07005755 } else {
5756 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
5757 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005758 }
5759
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005760out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03005761 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005762out_fput:
5763 fdput(f);
5764 return submitted ? submitted : ret;
5765}
5766
5767static const struct file_operations io_uring_fops = {
5768 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06005769 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07005770 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005771#ifndef CONFIG_MMU
5772 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
5773 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
5774#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07005775 .poll = io_uring_poll,
5776 .fasync = io_uring_fasync,
5777};
5778
5779static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
5780 struct io_uring_params *p)
5781{
Hristo Venev75b28af2019-08-26 17:23:46 +00005782 struct io_rings *rings;
5783 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005784
Hristo Venev75b28af2019-08-26 17:23:46 +00005785 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
5786 if (size == SIZE_MAX)
5787 return -EOVERFLOW;
5788
5789 rings = io_mem_alloc(size);
5790 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005791 return -ENOMEM;
5792
Hristo Venev75b28af2019-08-26 17:23:46 +00005793 ctx->rings = rings;
5794 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
5795 rings->sq_ring_mask = p->sq_entries - 1;
5796 rings->cq_ring_mask = p->cq_entries - 1;
5797 rings->sq_ring_entries = p->sq_entries;
5798 rings->cq_ring_entries = p->cq_entries;
5799 ctx->sq_mask = rings->sq_ring_mask;
5800 ctx->cq_mask = rings->cq_ring_mask;
5801 ctx->sq_entries = rings->sq_ring_entries;
5802 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005803
5804 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07005805 if (size == SIZE_MAX) {
5806 io_mem_free(ctx->rings);
5807 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005808 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07005809 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005810
5811 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07005812 if (!ctx->sq_sqes) {
5813 io_mem_free(ctx->rings);
5814 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005815 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07005816 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005817
Jens Axboe2b188cc2019-01-07 10:46:33 -07005818 return 0;
5819}
5820
5821/*
5822 * Allocate an anonymous fd, this is what constitutes the application
5823 * visible backing of an io_uring instance. The application mmaps this
5824 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
5825 * we have to tie this fd to a socket for file garbage collection purposes.
5826 */
5827static int io_uring_get_fd(struct io_ring_ctx *ctx)
5828{
5829 struct file *file;
5830 int ret;
5831
5832#if defined(CONFIG_UNIX)
5833 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
5834 &ctx->ring_sock);
5835 if (ret)
5836 return ret;
5837#endif
5838
5839 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
5840 if (ret < 0)
5841 goto err;
5842
5843 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
5844 O_RDWR | O_CLOEXEC);
5845 if (IS_ERR(file)) {
5846 put_unused_fd(ret);
5847 ret = PTR_ERR(file);
5848 goto err;
5849 }
5850
5851#if defined(CONFIG_UNIX)
5852 ctx->ring_sock->file = file;
5853#endif
5854 fd_install(ret, file);
5855 return ret;
5856err:
5857#if defined(CONFIG_UNIX)
5858 sock_release(ctx->ring_sock);
5859 ctx->ring_sock = NULL;
5860#endif
5861 return ret;
5862}
5863
5864static int io_uring_create(unsigned entries, struct io_uring_params *p)
5865{
5866 struct user_struct *user = NULL;
5867 struct io_ring_ctx *ctx;
5868 bool account_mem;
5869 int ret;
5870
5871 if (!entries || entries > IORING_MAX_ENTRIES)
5872 return -EINVAL;
5873
5874 /*
5875 * Use twice as many entries for the CQ ring. It's possible for the
5876 * application to drive a higher depth than the size of the SQ ring,
5877 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005878 * some flexibility in overcommitting a bit. If the application has
5879 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5880 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005881 */
5882 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005883 if (p->flags & IORING_SETUP_CQSIZE) {
5884 /*
5885 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5886 * to a power-of-two, if it isn't already. We do NOT impose
5887 * any cq vs sq ring sizing.
5888 */
5889 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5890 return -EINVAL;
5891 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5892 } else {
5893 p->cq_entries = 2 * p->sq_entries;
5894 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005895
5896 user = get_uid(current_user());
5897 account_mem = !capable(CAP_IPC_LOCK);
5898
5899 if (account_mem) {
5900 ret = io_account_mem(user,
5901 ring_pages(p->sq_entries, p->cq_entries));
5902 if (ret) {
5903 free_uid(user);
5904 return ret;
5905 }
5906 }
5907
5908 ctx = io_ring_ctx_alloc(p);
5909 if (!ctx) {
5910 if (account_mem)
5911 io_unaccount_mem(user, ring_pages(p->sq_entries,
5912 p->cq_entries));
5913 free_uid(user);
5914 return -ENOMEM;
5915 }
5916 ctx->compat = in_compat_syscall();
5917 ctx->account_mem = account_mem;
5918 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07005919 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07005920
5921 ret = io_allocate_scq_urings(ctx, p);
5922 if (ret)
5923 goto err;
5924
Jens Axboe6c271ce2019-01-10 11:22:30 -07005925 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005926 if (ret)
5927 goto err;
5928
Jens Axboe2b188cc2019-01-07 10:46:33 -07005929 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005930 p->sq_off.head = offsetof(struct io_rings, sq.head);
5931 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5932 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5933 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5934 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5935 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5936 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005937
5938 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005939 p->cq_off.head = offsetof(struct io_rings, cq.head);
5940 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5941 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5942 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5943 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5944 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06005945
Jens Axboe044c1ab2019-10-28 09:15:33 -06005946 /*
5947 * Install ring fd as the very last thing, so we don't risk someone
5948 * having closed it before we finish setup
5949 */
5950 ret = io_uring_get_fd(ctx);
5951 if (ret < 0)
5952 goto err;
5953
Jens Axboeda8c9692019-12-02 18:51:26 -07005954 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
5955 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005956 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005957 return ret;
5958err:
5959 io_ring_ctx_wait_and_kill(ctx);
5960 return ret;
5961}
5962
5963/*
5964 * Sets up an aio uring context, and returns the fd. Applications asks for a
5965 * ring size, we return the actual sq/cq ring sizes (among other things) in the
5966 * params structure passed in.
5967 */
5968static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5969{
5970 struct io_uring_params p;
5971 long ret;
5972 int i;
5973
5974 if (copy_from_user(&p, params, sizeof(p)))
5975 return -EFAULT;
5976 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5977 if (p.resv[i])
5978 return -EINVAL;
5979 }
5980
Jens Axboe6c271ce2019-01-10 11:22:30 -07005981 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06005982 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005983 return -EINVAL;
5984
5985 ret = io_uring_create(entries, &p);
5986 if (ret < 0)
5987 return ret;
5988
5989 if (copy_to_user(params, &p, sizeof(p)))
5990 return -EFAULT;
5991
5992 return ret;
5993}
5994
5995SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5996 struct io_uring_params __user *, params)
5997{
5998 return io_uring_setup(entries, params);
5999}
6000
Jens Axboeedafcce2019-01-09 09:16:05 -07006001static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6002 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006003 __releases(ctx->uring_lock)
6004 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006005{
6006 int ret;
6007
Jens Axboe35fa71a2019-04-22 10:23:23 -06006008 /*
6009 * We're inside the ring mutex, if the ref is already dying, then
6010 * someone else killed the ctx or is already going through
6011 * io_uring_register().
6012 */
6013 if (percpu_ref_is_dying(&ctx->refs))
6014 return -ENXIO;
6015
Jens Axboe05f3fb32019-12-09 11:22:50 -07006016 if (opcode != IORING_UNREGISTER_FILES &&
6017 opcode != IORING_REGISTER_FILES_UPDATE) {
6018 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006019
Jens Axboe05f3fb32019-12-09 11:22:50 -07006020 /*
6021 * Drop uring mutex before waiting for references to exit. If
6022 * another thread is currently inside io_uring_enter() it might
6023 * need to grab the uring_lock to make progress. If we hold it
6024 * here across the drain wait, then we can deadlock. It's safe
6025 * to drop the mutex here, since no new references will come in
6026 * after we've killed the percpu ref.
6027 */
6028 mutex_unlock(&ctx->uring_lock);
6029 wait_for_completion(&ctx->completions[0]);
6030 mutex_lock(&ctx->uring_lock);
6031 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006032
6033 switch (opcode) {
6034 case IORING_REGISTER_BUFFERS:
6035 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6036 break;
6037 case IORING_UNREGISTER_BUFFERS:
6038 ret = -EINVAL;
6039 if (arg || nr_args)
6040 break;
6041 ret = io_sqe_buffer_unregister(ctx);
6042 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006043 case IORING_REGISTER_FILES:
6044 ret = io_sqe_files_register(ctx, arg, nr_args);
6045 break;
6046 case IORING_UNREGISTER_FILES:
6047 ret = -EINVAL;
6048 if (arg || nr_args)
6049 break;
6050 ret = io_sqe_files_unregister(ctx);
6051 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006052 case IORING_REGISTER_FILES_UPDATE:
6053 ret = io_sqe_files_update(ctx, arg, nr_args);
6054 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006055 case IORING_REGISTER_EVENTFD:
6056 ret = -EINVAL;
6057 if (nr_args != 1)
6058 break;
6059 ret = io_eventfd_register(ctx, arg);
6060 break;
6061 case IORING_UNREGISTER_EVENTFD:
6062 ret = -EINVAL;
6063 if (arg || nr_args)
6064 break;
6065 ret = io_eventfd_unregister(ctx);
6066 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006067 default:
6068 ret = -EINVAL;
6069 break;
6070 }
6071
Jens Axboe05f3fb32019-12-09 11:22:50 -07006072
6073 if (opcode != IORING_UNREGISTER_FILES &&
6074 opcode != IORING_REGISTER_FILES_UPDATE) {
6075 /* bring the ctx back to life */
6076 reinit_completion(&ctx->completions[0]);
6077 percpu_ref_reinit(&ctx->refs);
6078 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006079 return ret;
6080}
6081
6082SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6083 void __user *, arg, unsigned int, nr_args)
6084{
6085 struct io_ring_ctx *ctx;
6086 long ret = -EBADF;
6087 struct fd f;
6088
6089 f = fdget(fd);
6090 if (!f.file)
6091 return -EBADF;
6092
6093 ret = -EOPNOTSUPP;
6094 if (f.file->f_op != &io_uring_fops)
6095 goto out_fput;
6096
6097 ctx = f.file->private_data;
6098
6099 mutex_lock(&ctx->uring_lock);
6100 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6101 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006102 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6103 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006104out_fput:
6105 fdput(f);
6106 return ret;
6107}
6108
Jens Axboe2b188cc2019-01-07 10:46:33 -07006109static int __init io_uring_init(void)
6110{
6111 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6112 return 0;
6113};
6114__initcall(io_uring_init);