blob: dfa99da8b2d1479dc51a966c6ee5243ae1ce1838 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070059#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070073#include <linux/namei.h>
74#include <linux/fsnotify.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070075
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020076#define CREATE_TRACE_POINTS
77#include <trace/events/io_uring.h>
78
Jens Axboe2b188cc2019-01-07 10:46:33 -070079#include <uapi/linux/io_uring.h>
80
81#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060082#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070083
Daniel Xu5277dea2019-09-14 14:23:45 -070084#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060085#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060086
87/*
88 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
89 */
90#define IORING_FILE_TABLE_SHIFT 9
91#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
92#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
93#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070094
95struct io_uring {
96 u32 head ____cacheline_aligned_in_smp;
97 u32 tail ____cacheline_aligned_in_smp;
98};
99
Stefan Bühler1e84b972019-04-24 23:54:16 +0200100/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000101 * This data is shared with the application through the mmap at offsets
102 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200103 *
104 * The offsets to the member fields are published through struct
105 * io_sqring_offsets when calling io_uring_setup.
106 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000107struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200108 /*
109 * Head and tail offsets into the ring; the offsets need to be
110 * masked to get valid indices.
111 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000112 * The kernel controls head of the sq ring and the tail of the cq ring,
113 * and the application controls tail of the sq ring and the head of the
114 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200117 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 * ring_entries - 1)
120 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000121 u32 sq_ring_mask, cq_ring_mask;
122 /* Ring sizes (constant, power of 2) */
123 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
125 * Number of invalid entries dropped by the kernel due to
126 * invalid index stored in array
127 *
128 * Written by the kernel, shouldn't be modified by the
129 * application (i.e. get number of "new events" by comparing to
130 * cached value).
131 *
132 * After a new SQ head value was read by the application this
133 * counter includes all submissions that were dropped reaching
134 * the new SQ head (and possibly more).
135 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000136 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200137 /*
138 * Runtime flags
139 *
140 * Written by the kernel, shouldn't be modified by the
141 * application.
142 *
143 * The application needs a full memory barrier before checking
144 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200147 /*
148 * Number of completion events lost because the queue was full;
149 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800150 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 * the completion queue.
152 *
153 * Written by the kernel, shouldn't be modified by the
154 * application (i.e. get number of "new events" by comparing to
155 * cached value).
156 *
157 * As completion events come in out of order this counter is not
158 * ordered with any other data.
159 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000160 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200161 /*
162 * Ring buffer of completion events.
163 *
164 * The kernel writes completion events fresh every time they are
165 * produced, so the application is allowed to modify pending
166 * entries.
167 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000168 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700169};
170
Jens Axboeedafcce2019-01-09 09:16:05 -0700171struct io_mapped_ubuf {
172 u64 ubuf;
173 size_t len;
174 struct bio_vec *bvec;
175 unsigned int nr_bvecs;
176};
177
Jens Axboe65e19f52019-10-26 07:20:21 -0600178struct fixed_file_table {
179 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700180};
181
Jens Axboe05f3fb32019-12-09 11:22:50 -0700182enum {
183 FFD_F_ATOMIC,
184};
185
186struct fixed_file_data {
187 struct fixed_file_table *table;
188 struct io_ring_ctx *ctx;
189
190 struct percpu_ref refs;
191 struct llist_head put_llist;
192 unsigned long state;
193 struct work_struct ref_work;
194 struct completion done;
195};
196
Jens Axboe2b188cc2019-01-07 10:46:33 -0700197struct io_ring_ctx {
198 struct {
199 struct percpu_ref refs;
200 } ____cacheline_aligned_in_smp;
201
202 struct {
203 unsigned int flags;
204 bool compat;
205 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700206 bool cq_overflow_flushed;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300207 bool drain_next;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208
Hristo Venev75b28af2019-08-26 17:23:46 +0000209 /*
210 * Ring buffer of indices into array of io_uring_sqe, which is
211 * mmapped by the application using the IORING_OFF_SQES offset.
212 *
213 * This indirection could e.g. be used to assign fixed
214 * io_uring_sqe entries to operations and only submit them to
215 * the queue when needed.
216 *
217 * The kernel modifies neither the indices array nor the entries
218 * array.
219 */
220 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 unsigned cached_sq_head;
222 unsigned sq_entries;
223 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700224 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600225 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700226 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700227 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600228
229 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600230 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700231 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700232
Jens Axboefcb323c2019-10-24 12:39:47 -0600233 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700234 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700235 } ____cacheline_aligned_in_smp;
236
Hristo Venev75b28af2019-08-26 17:23:46 +0000237 struct io_rings *rings;
238
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600240 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241 struct task_struct *sqo_thread; /* if using sq thread polling */
242 struct mm_struct *sqo_mm;
243 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700244
Jens Axboe6b063142019-01-10 22:13:58 -0700245 /*
246 * If used, fixed file set. Writers must ensure that ->refs is dead,
247 * readers must ensure that ->refs is alive as long as the file* is
248 * used. Only updated through io_uring_register(2).
249 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700250 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700251 unsigned nr_user_files;
252
Jens Axboeedafcce2019-01-09 09:16:05 -0700253 /* if used, fixed mapped user buffers */
254 unsigned nr_user_bufs;
255 struct io_mapped_ubuf *user_bufs;
256
Jens Axboe2b188cc2019-01-07 10:46:33 -0700257 struct user_struct *user;
258
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700259 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700260
Jens Axboe206aefd2019-11-07 18:27:42 -0700261 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
262 struct completion *completions;
263
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700264 /* if all else fails... */
265 struct io_kiocb *fallback_req;
266
Jens Axboe206aefd2019-11-07 18:27:42 -0700267#if defined(CONFIG_UNIX)
268 struct socket *ring_sock;
269#endif
270
271 struct {
272 unsigned cached_cq_tail;
273 unsigned cq_entries;
274 unsigned cq_mask;
275 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700276 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700277 struct wait_queue_head cq_wait;
278 struct fasync_struct *cq_fasync;
279 struct eventfd_ctx *cq_ev_fd;
280 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700281
282 struct {
283 struct mutex uring_lock;
284 wait_queue_head_t wait;
285 } ____cacheline_aligned_in_smp;
286
287 struct {
288 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700289 bool poll_multi_file;
290 /*
291 * ->poll_list is protected by the ctx->uring_lock for
292 * io_uring instances that don't use IORING_SETUP_SQPOLL.
293 * For SQPOLL, only the single threaded io_sq_thread() will
294 * manipulate the list, hence no extra locking is needed there.
295 */
296 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700297 struct hlist_head *cancel_hash;
298 unsigned cancel_hash_bits;
Jens Axboefcb323c2019-10-24 12:39:47 -0600299
300 spinlock_t inflight_lock;
301 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700302 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700303};
304
Jens Axboe09bb8392019-03-13 12:39:28 -0600305/*
306 * First field must be the file pointer in all the
307 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
308 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700309struct io_poll_iocb {
310 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700311 union {
312 struct wait_queue_head *head;
313 u64 addr;
314 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700315 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600316 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700317 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700318 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700319};
320
Jens Axboeb5dba592019-12-11 14:02:38 -0700321struct io_close {
322 struct file *file;
323 struct file *put_file;
324 int fd;
325};
326
Jens Axboead8a48a2019-11-15 08:49:11 -0700327struct io_timeout_data {
328 struct io_kiocb *req;
329 struct hrtimer timer;
330 struct timespec64 ts;
331 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300332 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700333};
334
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700335struct io_accept {
336 struct file *file;
337 struct sockaddr __user *addr;
338 int __user *addr_len;
339 int flags;
340};
341
342struct io_sync {
343 struct file *file;
344 loff_t len;
345 loff_t off;
346 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700347 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700348};
349
Jens Axboefbf23842019-12-17 18:45:56 -0700350struct io_cancel {
351 struct file *file;
352 u64 addr;
353};
354
Jens Axboeb29472e2019-12-17 18:50:29 -0700355struct io_timeout {
356 struct file *file;
357 u64 addr;
358 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700359 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700360};
361
Jens Axboe9adbd452019-12-20 08:45:55 -0700362struct io_rw {
363 /* NOTE: kiocb has the file as the first member, so don't do it here */
364 struct kiocb kiocb;
365 u64 addr;
366 u64 len;
367};
368
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700369struct io_connect {
370 struct file *file;
371 struct sockaddr __user *addr;
372 int addr_len;
373};
374
Jens Axboee47293f2019-12-20 08:58:21 -0700375struct io_sr_msg {
376 struct file *file;
377 struct user_msghdr __user *msg;
378 int msg_flags;
379};
380
Jens Axboe15b71ab2019-12-11 11:20:36 -0700381struct io_open {
382 struct file *file;
383 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700384 union {
385 umode_t mode;
386 unsigned mask;
387 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700388 const char __user *fname;
389 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700390 struct statx __user *buffer;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700391 int flags;
392};
393
Jens Axboe05f3fb32019-12-09 11:22:50 -0700394struct io_files_update {
395 struct file *file;
396 u64 arg;
397 u32 nr_args;
398 u32 offset;
399};
400
Jens Axboef499a022019-12-02 16:28:46 -0700401struct io_async_connect {
402 struct sockaddr_storage address;
403};
404
Jens Axboe03b12302019-12-02 18:50:25 -0700405struct io_async_msghdr {
406 struct iovec fast_iov[UIO_FASTIOV];
407 struct iovec *iov;
408 struct sockaddr __user *uaddr;
409 struct msghdr msg;
410};
411
Jens Axboef67676d2019-12-02 11:03:47 -0700412struct io_async_rw {
413 struct iovec fast_iov[UIO_FASTIOV];
414 struct iovec *iov;
415 ssize_t nr_segs;
416 ssize_t size;
417};
418
Jens Axboe15b71ab2019-12-11 11:20:36 -0700419struct io_async_open {
420 struct filename *filename;
421};
422
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700423struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700424 union {
425 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700426 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700427 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700428 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700429 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700430 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700431};
432
Jens Axboe09bb8392019-03-13 12:39:28 -0600433/*
434 * NOTE! Each of the iocb union members has the file pointer
435 * as the first entry in their struct definition. So you can
436 * access the file pointer through any of the sub-structs,
437 * or directly as just 'ki_filp' in this struct.
438 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700439struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700440 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600441 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700442 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700443 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700444 struct io_accept accept;
445 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700446 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700447 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700448 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700449 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700450 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700451 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700452 struct io_files_update files_update;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700453 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700454
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700455 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300456 struct file *ring_file;
457 int ring_fd;
458 bool has_user;
459 bool in_async;
460 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700461 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700462
463 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700464 union {
465 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700466 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700467 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600468 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700469 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700470 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200471#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700472#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700473#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700474#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200475#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
476#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600477#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700478#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800479#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300480#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600481#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600482#define REQ_F_ISREG 2048 /* regular file */
483#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700484#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800485#define REQ_F_INFLIGHT 16384 /* on inflight list */
486#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700487#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboece35a472019-12-17 08:04:44 -0700488#define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700489 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600490 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600491 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700492
Jens Axboefcb323c2019-10-24 12:39:47 -0600493 struct list_head inflight_entry;
494
Jens Axboe561fb042019-10-24 07:25:42 -0600495 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700496};
497
498#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700499#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700500
Jens Axboe9a56a232019-01-09 09:06:50 -0700501struct io_submit_state {
502 struct blk_plug plug;
503
504 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700505 * io_kiocb alloc cache
506 */
507 void *reqs[IO_IOPOLL_BATCH];
508 unsigned int free_reqs;
509 unsigned int cur_req;
510
511 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700512 * File reference cache
513 */
514 struct file *file;
515 unsigned int fd;
516 unsigned int has_refs;
517 unsigned int used_refs;
518 unsigned int ios_left;
519};
520
Jens Axboed3656342019-12-18 09:50:26 -0700521struct io_op_def {
522 /* needs req->io allocated for deferral/async */
523 unsigned async_ctx : 1;
524 /* needs current->mm setup, does mm access */
525 unsigned needs_mm : 1;
526 /* needs req->file assigned */
527 unsigned needs_file : 1;
528 /* needs req->file assigned IFF fd is >= 0 */
529 unsigned fd_non_neg : 1;
530 /* hash wq insertion if file is a regular file */
531 unsigned hash_reg_file : 1;
532 /* unbound wq insertion if file is a non-regular file */
533 unsigned unbound_nonreg_file : 1;
534};
535
536static const struct io_op_def io_op_defs[] = {
537 {
538 /* IORING_OP_NOP */
539 },
540 {
541 /* IORING_OP_READV */
542 .async_ctx = 1,
543 .needs_mm = 1,
544 .needs_file = 1,
545 .unbound_nonreg_file = 1,
546 },
547 {
548 /* IORING_OP_WRITEV */
549 .async_ctx = 1,
550 .needs_mm = 1,
551 .needs_file = 1,
552 .hash_reg_file = 1,
553 .unbound_nonreg_file = 1,
554 },
555 {
556 /* IORING_OP_FSYNC */
557 .needs_file = 1,
558 },
559 {
560 /* IORING_OP_READ_FIXED */
561 .needs_file = 1,
562 .unbound_nonreg_file = 1,
563 },
564 {
565 /* IORING_OP_WRITE_FIXED */
566 .needs_file = 1,
567 .hash_reg_file = 1,
568 .unbound_nonreg_file = 1,
569 },
570 {
571 /* IORING_OP_POLL_ADD */
572 .needs_file = 1,
573 .unbound_nonreg_file = 1,
574 },
575 {
576 /* IORING_OP_POLL_REMOVE */
577 },
578 {
579 /* IORING_OP_SYNC_FILE_RANGE */
580 .needs_file = 1,
581 },
582 {
583 /* IORING_OP_SENDMSG */
584 .async_ctx = 1,
585 .needs_mm = 1,
586 .needs_file = 1,
587 .unbound_nonreg_file = 1,
588 },
589 {
590 /* IORING_OP_RECVMSG */
591 .async_ctx = 1,
592 .needs_mm = 1,
593 .needs_file = 1,
594 .unbound_nonreg_file = 1,
595 },
596 {
597 /* IORING_OP_TIMEOUT */
598 .async_ctx = 1,
599 .needs_mm = 1,
600 },
601 {
602 /* IORING_OP_TIMEOUT_REMOVE */
603 },
604 {
605 /* IORING_OP_ACCEPT */
606 .needs_mm = 1,
607 .needs_file = 1,
608 .unbound_nonreg_file = 1,
609 },
610 {
611 /* IORING_OP_ASYNC_CANCEL */
612 },
613 {
614 /* IORING_OP_LINK_TIMEOUT */
615 .async_ctx = 1,
616 .needs_mm = 1,
617 },
618 {
619 /* IORING_OP_CONNECT */
620 .async_ctx = 1,
621 .needs_mm = 1,
622 .needs_file = 1,
623 .unbound_nonreg_file = 1,
624 },
625 {
626 /* IORING_OP_FALLOCATE */
627 .needs_file = 1,
628 },
629 {
630 /* IORING_OP_OPENAT */
631 .needs_file = 1,
632 .fd_non_neg = 1,
633 },
634 {
635 /* IORING_OP_CLOSE */
636 .needs_file = 1,
637 },
638 {
639 /* IORING_OP_FILES_UPDATE */
640 .needs_mm = 1,
641 },
642 {
643 /* IORING_OP_STATX */
644 .needs_mm = 1,
645 .needs_file = 1,
646 .fd_non_neg = 1,
647 },
648};
649
Jens Axboe561fb042019-10-24 07:25:42 -0600650static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700651static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800652static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700653static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700654static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
655static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700656static int __io_sqe_files_update(struct io_ring_ctx *ctx,
657 struct io_uring_files_update *ip,
658 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600659
Jens Axboe2b188cc2019-01-07 10:46:33 -0700660static struct kmem_cache *req_cachep;
661
662static const struct file_operations io_uring_fops;
663
664struct sock *io_uring_get_socket(struct file *file)
665{
666#if defined(CONFIG_UNIX)
667 if (file->f_op == &io_uring_fops) {
668 struct io_ring_ctx *ctx = file->private_data;
669
670 return ctx->ring_sock->sk;
671 }
672#endif
673 return NULL;
674}
675EXPORT_SYMBOL(io_uring_get_socket);
676
677static void io_ring_ctx_ref_free(struct percpu_ref *ref)
678{
679 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
680
Jens Axboe206aefd2019-11-07 18:27:42 -0700681 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700682}
683
684static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
685{
686 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700687 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700688
689 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
690 if (!ctx)
691 return NULL;
692
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700693 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
694 if (!ctx->fallback_req)
695 goto err;
696
Jens Axboe206aefd2019-11-07 18:27:42 -0700697 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
698 if (!ctx->completions)
699 goto err;
700
Jens Axboe78076bb2019-12-04 19:56:40 -0700701 /*
702 * Use 5 bits less than the max cq entries, that should give us around
703 * 32 entries per hash list if totally full and uniformly spread.
704 */
705 hash_bits = ilog2(p->cq_entries);
706 hash_bits -= 5;
707 if (hash_bits <= 0)
708 hash_bits = 1;
709 ctx->cancel_hash_bits = hash_bits;
710 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
711 GFP_KERNEL);
712 if (!ctx->cancel_hash)
713 goto err;
714 __hash_init(ctx->cancel_hash, 1U << hash_bits);
715
Roman Gushchin21482892019-05-07 10:01:48 -0700716 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700717 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
718 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700719
720 ctx->flags = p->flags;
721 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700722 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700723 init_completion(&ctx->completions[0]);
724 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700725 mutex_init(&ctx->uring_lock);
726 init_waitqueue_head(&ctx->wait);
727 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700728 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600729 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600730 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600731 init_waitqueue_head(&ctx->inflight_wait);
732 spin_lock_init(&ctx->inflight_lock);
733 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700734 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700735err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700736 if (ctx->fallback_req)
737 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700738 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700739 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700740 kfree(ctx);
741 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700742}
743
Bob Liu9d858b22019-11-13 18:06:25 +0800744static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600745{
Jackie Liua197f662019-11-08 08:09:12 -0700746 struct io_ring_ctx *ctx = req->ctx;
747
Jens Axboe498ccd92019-10-25 10:04:25 -0600748 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
749 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600750}
751
Bob Liu9d858b22019-11-13 18:06:25 +0800752static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600753{
Bob Liu9d858b22019-11-13 18:06:25 +0800754 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
755 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600756
Bob Liu9d858b22019-11-13 18:06:25 +0800757 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600758}
759
760static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600761{
762 struct io_kiocb *req;
763
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600764 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800765 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600766 list_del_init(&req->list);
767 return req;
768 }
769
770 return NULL;
771}
772
Jens Axboe5262f562019-09-17 12:26:57 -0600773static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
774{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600775 struct io_kiocb *req;
776
777 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700778 if (req) {
779 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
780 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800781 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700782 list_del_init(&req->list);
783 return req;
784 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600785 }
786
787 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600788}
789
Jens Axboede0617e2019-04-06 21:51:27 -0600790static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700791{
Hristo Venev75b28af2019-08-26 17:23:46 +0000792 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700793
Hristo Venev75b28af2019-08-26 17:23:46 +0000794 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700795 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000796 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700797
Jens Axboe2b188cc2019-01-07 10:46:33 -0700798 if (wq_has_sleeper(&ctx->cq_wait)) {
799 wake_up_interruptible(&ctx->cq_wait);
800 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
801 }
802 }
803}
804
Jens Axboe94ae5e72019-11-14 19:39:52 -0700805static inline bool io_prep_async_work(struct io_kiocb *req,
806 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600807{
Jens Axboed3656342019-12-18 09:50:26 -0700808 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600809 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600810
Jens Axboed3656342019-12-18 09:50:26 -0700811 if (req->flags & REQ_F_ISREG) {
812 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700813 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700814 } else {
815 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700816 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600817 }
Jens Axboed3656342019-12-18 09:50:26 -0700818 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700819 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600820
Jens Axboe94ae5e72019-11-14 19:39:52 -0700821 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600822 return do_hashed;
823}
824
Jackie Liua197f662019-11-08 08:09:12 -0700825static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600826{
Jackie Liua197f662019-11-08 08:09:12 -0700827 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700828 struct io_kiocb *link;
829 bool do_hashed;
830
831 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600832
833 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
834 req->flags);
835 if (!do_hashed) {
836 io_wq_enqueue(ctx->io_wq, &req->work);
837 } else {
838 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
839 file_inode(req->file));
840 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700841
842 if (link)
843 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600844}
845
Jens Axboe5262f562019-09-17 12:26:57 -0600846static void io_kill_timeout(struct io_kiocb *req)
847{
848 int ret;
849
Jens Axboe2d283902019-12-04 11:08:05 -0700850 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600851 if (ret != -1) {
852 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600853 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700854 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800855 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600856 }
857}
858
859static void io_kill_timeouts(struct io_ring_ctx *ctx)
860{
861 struct io_kiocb *req, *tmp;
862
863 spin_lock_irq(&ctx->completion_lock);
864 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
865 io_kill_timeout(req);
866 spin_unlock_irq(&ctx->completion_lock);
867}
868
Jens Axboede0617e2019-04-06 21:51:27 -0600869static void io_commit_cqring(struct io_ring_ctx *ctx)
870{
871 struct io_kiocb *req;
872
Jens Axboe5262f562019-09-17 12:26:57 -0600873 while ((req = io_get_timeout_req(ctx)) != NULL)
874 io_kill_timeout(req);
875
Jens Axboede0617e2019-04-06 21:51:27 -0600876 __io_commit_cqring(ctx);
877
878 while ((req = io_get_deferred_req(ctx)) != NULL) {
879 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700880 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600881 }
882}
883
Jens Axboe2b188cc2019-01-07 10:46:33 -0700884static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
885{
Hristo Venev75b28af2019-08-26 17:23:46 +0000886 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700887 unsigned tail;
888
889 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200890 /*
891 * writes to the cq entry need to come after reading head; the
892 * control dependency is enough as we're using WRITE_ONCE to
893 * fill the cq entry
894 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000895 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700896 return NULL;
897
898 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000899 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700900}
901
Jens Axboe8c838782019-03-12 15:48:16 -0600902static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
903{
904 if (waitqueue_active(&ctx->wait))
905 wake_up(&ctx->wait);
906 if (waitqueue_active(&ctx->sqo_wait))
907 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600908 if (ctx->cq_ev_fd)
909 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600910}
911
Jens Axboec4a2ed72019-11-21 21:01:26 -0700912/* Returns true if there are no backlogged entries after the flush */
913static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700914{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700915 struct io_rings *rings = ctx->rings;
916 struct io_uring_cqe *cqe;
917 struct io_kiocb *req;
918 unsigned long flags;
919 LIST_HEAD(list);
920
921 if (!force) {
922 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700923 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700924 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
925 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700926 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700927 }
928
929 spin_lock_irqsave(&ctx->completion_lock, flags);
930
931 /* if force is set, the ring is going away. always drop after that */
932 if (force)
933 ctx->cq_overflow_flushed = true;
934
Jens Axboec4a2ed72019-11-21 21:01:26 -0700935 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700936 while (!list_empty(&ctx->cq_overflow_list)) {
937 cqe = io_get_cqring(ctx);
938 if (!cqe && !force)
939 break;
940
941 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
942 list);
943 list_move(&req->list, &list);
944 if (cqe) {
945 WRITE_ONCE(cqe->user_data, req->user_data);
946 WRITE_ONCE(cqe->res, req->result);
947 WRITE_ONCE(cqe->flags, 0);
948 } else {
949 WRITE_ONCE(ctx->rings->cq_overflow,
950 atomic_inc_return(&ctx->cached_cq_overflow));
951 }
952 }
953
954 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -0700955 if (cqe) {
956 clear_bit(0, &ctx->sq_check_overflow);
957 clear_bit(0, &ctx->cq_check_overflow);
958 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700959 spin_unlock_irqrestore(&ctx->completion_lock, flags);
960 io_cqring_ev_posted(ctx);
961
962 while (!list_empty(&list)) {
963 req = list_first_entry(&list, struct io_kiocb, list);
964 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800965 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700966 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700967
968 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700969}
970
Jens Axboe78e19bb2019-11-06 15:21:34 -0700971static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700972{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700973 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700974 struct io_uring_cqe *cqe;
975
Jens Axboe78e19bb2019-11-06 15:21:34 -0700976 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700977
Jens Axboe2b188cc2019-01-07 10:46:33 -0700978 /*
979 * If we can't get a cq entry, userspace overflowed the
980 * submission (by quite a lot). Increment the overflow count in
981 * the ring.
982 */
983 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700984 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700985 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700986 WRITE_ONCE(cqe->res, res);
987 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700988 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700989 WRITE_ONCE(ctx->rings->cq_overflow,
990 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700991 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -0700992 if (list_empty(&ctx->cq_overflow_list)) {
993 set_bit(0, &ctx->sq_check_overflow);
994 set_bit(0, &ctx->cq_check_overflow);
995 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700996 refcount_inc(&req->refs);
997 req->result = res;
998 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700999 }
1000}
1001
Jens Axboe78e19bb2019-11-06 15:21:34 -07001002static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001003{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001004 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001005 unsigned long flags;
1006
1007 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001008 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001009 io_commit_cqring(ctx);
1010 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1011
Jens Axboe8c838782019-03-12 15:48:16 -06001012 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001013}
1014
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001015static inline bool io_is_fallback_req(struct io_kiocb *req)
1016{
1017 return req == (struct io_kiocb *)
1018 ((unsigned long) req->ctx->fallback_req & ~1UL);
1019}
1020
1021static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1022{
1023 struct io_kiocb *req;
1024
1025 req = ctx->fallback_req;
1026 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1027 return req;
1028
1029 return NULL;
1030}
1031
Jens Axboe2579f912019-01-09 09:10:43 -07001032static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1033 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001034{
Jens Axboefd6fab22019-03-14 16:30:06 -06001035 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001036 struct io_kiocb *req;
1037
1038 if (!percpu_ref_tryget(&ctx->refs))
1039 return NULL;
1040
Jens Axboe2579f912019-01-09 09:10:43 -07001041 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001042 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001043 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001044 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001045 } else if (!state->free_reqs) {
1046 size_t sz;
1047 int ret;
1048
1049 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001050 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1051
1052 /*
1053 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1054 * retry single alloc to be on the safe side.
1055 */
1056 if (unlikely(ret <= 0)) {
1057 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1058 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001059 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001060 ret = 1;
1061 }
Jens Axboe2579f912019-01-09 09:10:43 -07001062 state->free_reqs = ret - 1;
1063 state->cur_req = 1;
1064 req = state->reqs[0];
1065 } else {
1066 req = state->reqs[state->cur_req];
1067 state->free_reqs--;
1068 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001069 }
1070
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001071got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001072 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001073 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001074 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001075 req->ctx = ctx;
1076 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001077 /* one is dropped after submission, the other at completion */
1078 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001079 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001080 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001081 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001082fallback:
1083 req = io_get_fallback_req(ctx);
1084 if (req)
1085 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001086 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001087 return NULL;
1088}
1089
Jens Axboedef596e2019-01-09 08:59:42 -07001090static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
1091{
1092 if (*nr) {
1093 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +03001094 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001095 percpu_ref_put_many(&ctx->file_data->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -07001096 *nr = 0;
1097 }
1098}
1099
Jens Axboe9e645e112019-05-10 16:07:28 -06001100static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001101{
Jens Axboefcb323c2019-10-24 12:39:47 -06001102 struct io_ring_ctx *ctx = req->ctx;
1103
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001104 if (req->io)
1105 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001106 if (req->file) {
1107 if (req->flags & REQ_F_FIXED_FILE)
1108 percpu_ref_put(&ctx->file_data->refs);
1109 else
1110 fput(req->file);
1111 }
Jens Axboefcb323c2019-10-24 12:39:47 -06001112 if (req->flags & REQ_F_INFLIGHT) {
1113 unsigned long flags;
1114
1115 spin_lock_irqsave(&ctx->inflight_lock, flags);
1116 list_del(&req->inflight_entry);
1117 if (waitqueue_active(&ctx->inflight_wait))
1118 wake_up(&ctx->inflight_wait);
1119 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1120 }
1121 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001122 if (likely(!io_is_fallback_req(req)))
1123 kmem_cache_free(req_cachep, req);
1124 else
1125 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001126}
1127
Jackie Liua197f662019-11-08 08:09:12 -07001128static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001129{
Jackie Liua197f662019-11-08 08:09:12 -07001130 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001131 int ret;
1132
Jens Axboe2d283902019-12-04 11:08:05 -07001133 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001134 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001135 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001136 io_commit_cqring(ctx);
1137 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001138 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001139 return true;
1140 }
1141
1142 return false;
1143}
1144
Jens Axboeba816ad2019-09-28 11:36:45 -06001145static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001146{
Jens Axboe2665abf2019-11-05 12:40:47 -07001147 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001148 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001149
Jens Axboe4d7dd462019-11-20 13:03:52 -07001150 /* Already got next link */
1151 if (req->flags & REQ_F_LINK_NEXT)
1152 return;
1153
Jens Axboe9e645e112019-05-10 16:07:28 -06001154 /*
1155 * The list should never be empty when we are called here. But could
1156 * potentially happen if the chain is messed up, check to be on the
1157 * safe side.
1158 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001159 while (!list_empty(&req->link_list)) {
1160 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1161 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001162
Pavel Begunkov44932332019-12-05 16:16:35 +03001163 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1164 (nxt->flags & REQ_F_TIMEOUT))) {
1165 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001166 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001167 req->flags &= ~REQ_F_LINK_TIMEOUT;
1168 continue;
1169 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001170
Pavel Begunkov44932332019-12-05 16:16:35 +03001171 list_del_init(&req->link_list);
1172 if (!list_empty(&nxt->link_list))
1173 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001174 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001175 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001176 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001177
Jens Axboe4d7dd462019-11-20 13:03:52 -07001178 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001179 if (wake_ev)
1180 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001181}
1182
1183/*
1184 * Called if REQ_F_LINK is set, and we fail the head request
1185 */
1186static void io_fail_links(struct io_kiocb *req)
1187{
Jens Axboe2665abf2019-11-05 12:40:47 -07001188 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001189 unsigned long flags;
1190
1191 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001192
1193 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001194 struct io_kiocb *link = list_first_entry(&req->link_list,
1195 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001196
Pavel Begunkov44932332019-12-05 16:16:35 +03001197 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001198 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001199
1200 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001201 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001202 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001203 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001204 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001205 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001206 }
Jens Axboe5d960722019-11-19 15:31:28 -07001207 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001208 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001209
1210 io_commit_cqring(ctx);
1211 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1212 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001213}
1214
Jens Axboe4d7dd462019-11-20 13:03:52 -07001215static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001216{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001217 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001218 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001219
Jens Axboe9e645e112019-05-10 16:07:28 -06001220 /*
1221 * If LINK is set, we have dependent requests in this chain. If we
1222 * didn't fail this request, queue the first one up, moving any other
1223 * dependencies to the next request. In case of failure, fail the rest
1224 * of the chain.
1225 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001226 if (req->flags & REQ_F_FAIL_LINK) {
1227 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001228 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1229 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001230 struct io_ring_ctx *ctx = req->ctx;
1231 unsigned long flags;
1232
1233 /*
1234 * If this is a timeout link, we could be racing with the
1235 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001236 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001237 */
1238 spin_lock_irqsave(&ctx->completion_lock, flags);
1239 io_req_link_next(req, nxt);
1240 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1241 } else {
1242 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001243 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001244}
Jens Axboe9e645e112019-05-10 16:07:28 -06001245
Jackie Liuc69f8db2019-11-09 11:00:08 +08001246static void io_free_req(struct io_kiocb *req)
1247{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001248 struct io_kiocb *nxt = NULL;
1249
1250 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001251 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001252
1253 if (nxt)
1254 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001255}
1256
Jens Axboeba816ad2019-09-28 11:36:45 -06001257/*
1258 * Drop reference to request, return next in chain (if there is one) if this
1259 * was the last reference to this request.
1260 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001261__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001262static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001263{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001264 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001265
Jens Axboee65ef562019-03-12 10:16:44 -06001266 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001267 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001268}
1269
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270static void io_put_req(struct io_kiocb *req)
1271{
Jens Axboedef596e2019-01-09 08:59:42 -07001272 if (refcount_dec_and_test(&req->refs))
1273 io_free_req(req);
1274}
1275
Jens Axboe978db572019-11-14 22:39:04 -07001276/*
1277 * Must only be used if we don't need to care about links, usually from
1278 * within the completion handling itself.
1279 */
1280static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001281{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001282 /* drop both submit and complete references */
1283 if (refcount_sub_and_test(2, &req->refs))
1284 __io_free_req(req);
1285}
1286
Jens Axboe978db572019-11-14 22:39:04 -07001287static void io_double_put_req(struct io_kiocb *req)
1288{
1289 /* drop both submit and complete references */
1290 if (refcount_sub_and_test(2, &req->refs))
1291 io_free_req(req);
1292}
1293
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001294static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001295{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001296 struct io_rings *rings = ctx->rings;
1297
Jens Axboead3eb2c2019-12-18 17:12:20 -07001298 if (test_bit(0, &ctx->cq_check_overflow)) {
1299 /*
1300 * noflush == true is from the waitqueue handler, just ensure
1301 * we wake up the task, and the next invocation will flush the
1302 * entries. We cannot safely to it from here.
1303 */
1304 if (noflush && !list_empty(&ctx->cq_overflow_list))
1305 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001306
Jens Axboead3eb2c2019-12-18 17:12:20 -07001307 io_cqring_overflow_flush(ctx, false);
1308 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001309
Jens Axboea3a0e432019-08-20 11:03:11 -06001310 /* See comment at the top of this file */
1311 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001312 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001313}
1314
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001315static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1316{
1317 struct io_rings *rings = ctx->rings;
1318
1319 /* make sure SQ entry isn't read before tail */
1320 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1321}
1322
Jens Axboedef596e2019-01-09 08:59:42 -07001323/*
1324 * Find and free completed poll iocbs
1325 */
1326static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1327 struct list_head *done)
1328{
1329 void *reqs[IO_IOPOLL_BATCH];
1330 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001331 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001332
Jens Axboe09bb8392019-03-13 12:39:28 -06001333 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001334 while (!list_empty(done)) {
1335 req = list_first_entry(done, struct io_kiocb, list);
1336 list_del(&req->list);
1337
Jens Axboe78e19bb2019-11-06 15:21:34 -07001338 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001339 (*nr_events)++;
1340
Jens Axboe09bb8392019-03-13 12:39:28 -06001341 if (refcount_dec_and_test(&req->refs)) {
1342 /* If we're not using fixed files, we have to pair the
1343 * completion part with the file put. Use regular
1344 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001345 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001346 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001347 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1348 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1349 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001350 reqs[to_free++] = req;
1351 if (to_free == ARRAY_SIZE(reqs))
1352 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001353 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001354 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001355 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001356 }
Jens Axboedef596e2019-01-09 08:59:42 -07001357 }
Jens Axboedef596e2019-01-09 08:59:42 -07001358
Jens Axboe09bb8392019-03-13 12:39:28 -06001359 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001360 io_free_req_many(ctx, reqs, &to_free);
1361}
1362
1363static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1364 long min)
1365{
1366 struct io_kiocb *req, *tmp;
1367 LIST_HEAD(done);
1368 bool spin;
1369 int ret;
1370
1371 /*
1372 * Only spin for completions if we don't have multiple devices hanging
1373 * off our complete list, and we're under the requested amount.
1374 */
1375 spin = !ctx->poll_multi_file && *nr_events < min;
1376
1377 ret = 0;
1378 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001379 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001380
1381 /*
1382 * Move completed entries to our local list. If we find a
1383 * request that requires polling, break out and complete
1384 * the done list first, if we have entries there.
1385 */
1386 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1387 list_move_tail(&req->list, &done);
1388 continue;
1389 }
1390 if (!list_empty(&done))
1391 break;
1392
1393 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1394 if (ret < 0)
1395 break;
1396
1397 if (ret && spin)
1398 spin = false;
1399 ret = 0;
1400 }
1401
1402 if (!list_empty(&done))
1403 io_iopoll_complete(ctx, nr_events, &done);
1404
1405 return ret;
1406}
1407
1408/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001409 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001410 * non-spinning poll check - we'll still enter the driver poll loop, but only
1411 * as a non-spinning completion check.
1412 */
1413static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1414 long min)
1415{
Jens Axboe08f54392019-08-21 22:19:11 -06001416 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001417 int ret;
1418
1419 ret = io_do_iopoll(ctx, nr_events, min);
1420 if (ret < 0)
1421 return ret;
1422 if (!min || *nr_events >= min)
1423 return 0;
1424 }
1425
1426 return 1;
1427}
1428
1429/*
1430 * We can't just wait for polled events to come to us, we have to actively
1431 * find and complete them.
1432 */
1433static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1434{
1435 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1436 return;
1437
1438 mutex_lock(&ctx->uring_lock);
1439 while (!list_empty(&ctx->poll_list)) {
1440 unsigned int nr_events = 0;
1441
1442 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001443
1444 /*
1445 * Ensure we allow local-to-the-cpu processing to take place,
1446 * in this case we need to ensure that we reap all events.
1447 */
1448 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001449 }
1450 mutex_unlock(&ctx->uring_lock);
1451}
1452
Jens Axboe2b2ed972019-10-25 10:06:15 -06001453static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1454 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001455{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001456 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001457
1458 do {
1459 int tmin = 0;
1460
Jens Axboe500f9fb2019-08-19 12:15:59 -06001461 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001462 * Don't enter poll loop if we already have events pending.
1463 * If we do, we can potentially be spinning for commands that
1464 * already triggered a CQE (eg in error).
1465 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001466 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001467 break;
1468
1469 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001470 * If a submit got punted to a workqueue, we can have the
1471 * application entering polling for a command before it gets
1472 * issued. That app will hold the uring_lock for the duration
1473 * of the poll right here, so we need to take a breather every
1474 * now and then to ensure that the issue has a chance to add
1475 * the poll to the issued list. Otherwise we can spin here
1476 * forever, while the workqueue is stuck trying to acquire the
1477 * very same mutex.
1478 */
1479 if (!(++iters & 7)) {
1480 mutex_unlock(&ctx->uring_lock);
1481 mutex_lock(&ctx->uring_lock);
1482 }
1483
Jens Axboedef596e2019-01-09 08:59:42 -07001484 if (*nr_events < min)
1485 tmin = min - *nr_events;
1486
1487 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1488 if (ret <= 0)
1489 break;
1490 ret = 0;
1491 } while (min && !*nr_events && !need_resched());
1492
Jens Axboe2b2ed972019-10-25 10:06:15 -06001493 return ret;
1494}
1495
1496static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1497 long min)
1498{
1499 int ret;
1500
1501 /*
1502 * We disallow the app entering submit/complete with polling, but we
1503 * still need to lock the ring to prevent racing with polled issue
1504 * that got punted to a workqueue.
1505 */
1506 mutex_lock(&ctx->uring_lock);
1507 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001508 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001509 return ret;
1510}
1511
Jens Axboe491381ce2019-10-17 09:20:46 -06001512static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001513{
Jens Axboe491381ce2019-10-17 09:20:46 -06001514 /*
1515 * Tell lockdep we inherited freeze protection from submission
1516 * thread.
1517 */
1518 if (req->flags & REQ_F_ISREG) {
1519 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001520
Jens Axboe491381ce2019-10-17 09:20:46 -06001521 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001522 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001523 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001524}
1525
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001526static inline void req_set_fail_links(struct io_kiocb *req)
1527{
1528 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1529 req->flags |= REQ_F_FAIL_LINK;
1530}
1531
Jens Axboeba816ad2019-09-28 11:36:45 -06001532static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001533{
Jens Axboe9adbd452019-12-20 08:45:55 -07001534 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001535
Jens Axboe491381ce2019-10-17 09:20:46 -06001536 if (kiocb->ki_flags & IOCB_WRITE)
1537 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001538
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001539 if (res != req->result)
1540 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001541 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001542}
1543
1544static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1545{
Jens Axboe9adbd452019-12-20 08:45:55 -07001546 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001547
1548 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001549 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001550}
1551
Jens Axboeba816ad2019-09-28 11:36:45 -06001552static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1553{
Jens Axboe9adbd452019-12-20 08:45:55 -07001554 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001555 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001556
1557 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001558 io_put_req_find_next(req, &nxt);
1559
1560 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001561}
1562
Jens Axboedef596e2019-01-09 08:59:42 -07001563static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1564{
Jens Axboe9adbd452019-12-20 08:45:55 -07001565 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001566
Jens Axboe491381ce2019-10-17 09:20:46 -06001567 if (kiocb->ki_flags & IOCB_WRITE)
1568 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001569
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001570 if (res != req->result)
1571 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001572 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001573 if (res != -EAGAIN)
1574 req->flags |= REQ_F_IOPOLL_COMPLETED;
1575}
1576
1577/*
1578 * After the iocb has been issued, it's safe to be found on the poll list.
1579 * Adding the kiocb to the list AFTER submission ensures that we don't
1580 * find it from a io_iopoll_getevents() thread before the issuer is done
1581 * accessing the kiocb cookie.
1582 */
1583static void io_iopoll_req_issued(struct io_kiocb *req)
1584{
1585 struct io_ring_ctx *ctx = req->ctx;
1586
1587 /*
1588 * Track whether we have multiple files in our lists. This will impact
1589 * how we do polling eventually, not spinning if we're on potentially
1590 * different devices.
1591 */
1592 if (list_empty(&ctx->poll_list)) {
1593 ctx->poll_multi_file = false;
1594 } else if (!ctx->poll_multi_file) {
1595 struct io_kiocb *list_req;
1596
1597 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1598 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001599 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001600 ctx->poll_multi_file = true;
1601 }
1602
1603 /*
1604 * For fast devices, IO may have already completed. If it has, add
1605 * it to the front so we find it first.
1606 */
1607 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1608 list_add(&req->list, &ctx->poll_list);
1609 else
1610 list_add_tail(&req->list, &ctx->poll_list);
1611}
1612
Jens Axboe3d6770f2019-04-13 11:50:54 -06001613static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001614{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001615 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001616 int diff = state->has_refs - state->used_refs;
1617
1618 if (diff)
1619 fput_many(state->file, diff);
1620 state->file = NULL;
1621 }
1622}
1623
1624/*
1625 * Get as many references to a file as we have IOs left in this submission,
1626 * assuming most submissions are for one file, or at least that each file
1627 * has more than one submission.
1628 */
1629static struct file *io_file_get(struct io_submit_state *state, int fd)
1630{
1631 if (!state)
1632 return fget(fd);
1633
1634 if (state->file) {
1635 if (state->fd == fd) {
1636 state->used_refs++;
1637 state->ios_left--;
1638 return state->file;
1639 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001640 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001641 }
1642 state->file = fget_many(fd, state->ios_left);
1643 if (!state->file)
1644 return NULL;
1645
1646 state->fd = fd;
1647 state->has_refs = state->ios_left;
1648 state->used_refs = 1;
1649 state->ios_left--;
1650 return state->file;
1651}
1652
Jens Axboe2b188cc2019-01-07 10:46:33 -07001653/*
1654 * If we tracked the file through the SCM inflight mechanism, we could support
1655 * any file. For now, just ensure that anything potentially problematic is done
1656 * inline.
1657 */
1658static bool io_file_supports_async(struct file *file)
1659{
1660 umode_t mode = file_inode(file)->i_mode;
1661
Jens Axboe10d59342019-12-09 20:16:22 -07001662 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001663 return true;
1664 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1665 return true;
1666
1667 return false;
1668}
1669
Jens Axboe3529d8c2019-12-19 18:24:38 -07001670static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1671 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001672{
Jens Axboedef596e2019-01-09 08:59:42 -07001673 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001674 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001675 unsigned ioprio;
1676 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001677
Jens Axboe09bb8392019-03-13 12:39:28 -06001678 if (!req->file)
1679 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001680
Jens Axboe491381ce2019-10-17 09:20:46 -06001681 if (S_ISREG(file_inode(req->file)->i_mode))
1682 req->flags |= REQ_F_ISREG;
1683
Jens Axboe2b188cc2019-01-07 10:46:33 -07001684 kiocb->ki_pos = READ_ONCE(sqe->off);
1685 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1686 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1687
1688 ioprio = READ_ONCE(sqe->ioprio);
1689 if (ioprio) {
1690 ret = ioprio_check_cap(ioprio);
1691 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001692 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001693
1694 kiocb->ki_ioprio = ioprio;
1695 } else
1696 kiocb->ki_ioprio = get_current_ioprio();
1697
1698 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1699 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001700 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001701
1702 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001703 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1704 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001705 req->flags |= REQ_F_NOWAIT;
1706
1707 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001709
Jens Axboedef596e2019-01-09 08:59:42 -07001710 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001711 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1712 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001713 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001714
Jens Axboedef596e2019-01-09 08:59:42 -07001715 kiocb->ki_flags |= IOCB_HIPRI;
1716 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001717 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001718 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001719 if (kiocb->ki_flags & IOCB_HIPRI)
1720 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001721 kiocb->ki_complete = io_complete_rw;
1722 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001723
Jens Axboe3529d8c2019-12-19 18:24:38 -07001724 req->rw.addr = READ_ONCE(sqe->addr);
1725 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001726 /* we own ->private, reuse it for the buffer index */
1727 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001728 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001729 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001730}
1731
1732static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1733{
1734 switch (ret) {
1735 case -EIOCBQUEUED:
1736 break;
1737 case -ERESTARTSYS:
1738 case -ERESTARTNOINTR:
1739 case -ERESTARTNOHAND:
1740 case -ERESTART_RESTARTBLOCK:
1741 /*
1742 * We can't just restart the syscall, since previously
1743 * submitted sqes may already be in progress. Just fail this
1744 * IO with EINTR.
1745 */
1746 ret = -EINTR;
1747 /* fall through */
1748 default:
1749 kiocb->ki_complete(kiocb, ret, 0);
1750 }
1751}
1752
Jens Axboeba816ad2019-09-28 11:36:45 -06001753static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1754 bool in_async)
1755{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001756 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001757 *nxt = __io_complete_rw(kiocb, ret);
1758 else
1759 io_rw_done(kiocb, ret);
1760}
1761
Jens Axboe9adbd452019-12-20 08:45:55 -07001762static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001763 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001764{
Jens Axboe9adbd452019-12-20 08:45:55 -07001765 struct io_ring_ctx *ctx = req->ctx;
1766 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001767 struct io_mapped_ubuf *imu;
1768 unsigned index, buf_index;
1769 size_t offset;
1770 u64 buf_addr;
1771
1772 /* attempt to use fixed buffers without having provided iovecs */
1773 if (unlikely(!ctx->user_bufs))
1774 return -EFAULT;
1775
Jens Axboe9adbd452019-12-20 08:45:55 -07001776 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001777 if (unlikely(buf_index >= ctx->nr_user_bufs))
1778 return -EFAULT;
1779
1780 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1781 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001782 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001783
1784 /* overflow */
1785 if (buf_addr + len < buf_addr)
1786 return -EFAULT;
1787 /* not inside the mapped region */
1788 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1789 return -EFAULT;
1790
1791 /*
1792 * May not be a start of buffer, set size appropriately
1793 * and advance us to the beginning.
1794 */
1795 offset = buf_addr - imu->ubuf;
1796 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001797
1798 if (offset) {
1799 /*
1800 * Don't use iov_iter_advance() here, as it's really slow for
1801 * using the latter parts of a big fixed buffer - it iterates
1802 * over each segment manually. We can cheat a bit here, because
1803 * we know that:
1804 *
1805 * 1) it's a BVEC iter, we set it up
1806 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1807 * first and last bvec
1808 *
1809 * So just find our index, and adjust the iterator afterwards.
1810 * If the offset is within the first bvec (or the whole first
1811 * bvec, just use iov_iter_advance(). This makes it easier
1812 * since we can just skip the first segment, which may not
1813 * be PAGE_SIZE aligned.
1814 */
1815 const struct bio_vec *bvec = imu->bvec;
1816
1817 if (offset <= bvec->bv_len) {
1818 iov_iter_advance(iter, offset);
1819 } else {
1820 unsigned long seg_skip;
1821
1822 /* skip first vec */
1823 offset -= bvec->bv_len;
1824 seg_skip = 1 + (offset >> PAGE_SHIFT);
1825
1826 iter->bvec = bvec + seg_skip;
1827 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001828 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001829 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001830 }
1831 }
1832
Jens Axboe5e559562019-11-13 16:12:46 -07001833 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001834}
1835
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001836static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1837 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001838{
Jens Axboe9adbd452019-12-20 08:45:55 -07001839 void __user *buf = u64_to_user_ptr(req->rw.addr);
1840 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001841 u8 opcode;
1842
Jens Axboed625c6e2019-12-17 19:53:05 -07001843 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001844 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001845 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001846 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001847 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001848
Jens Axboe9adbd452019-12-20 08:45:55 -07001849 /* buffer index only valid with fixed read/write */
1850 if (req->rw.kiocb.private)
1851 return -EINVAL;
1852
Jens Axboef67676d2019-12-02 11:03:47 -07001853 if (req->io) {
1854 struct io_async_rw *iorw = &req->io->rw;
1855
1856 *iovec = iorw->iov;
1857 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1858 if (iorw->iov == iorw->fast_iov)
1859 *iovec = NULL;
1860 return iorw->size;
1861 }
1862
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001863 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001864 return -EFAULT;
1865
1866#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001867 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001868 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1869 iovec, iter);
1870#endif
1871
1872 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1873}
1874
Jens Axboe32960612019-09-23 11:05:34 -06001875/*
1876 * For files that don't have ->read_iter() and ->write_iter(), handle them
1877 * by looping over ->read() or ->write() manually.
1878 */
1879static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1880 struct iov_iter *iter)
1881{
1882 ssize_t ret = 0;
1883
1884 /*
1885 * Don't support polled IO through this interface, and we can't
1886 * support non-blocking either. For the latter, this just causes
1887 * the kiocb to be handled from an async context.
1888 */
1889 if (kiocb->ki_flags & IOCB_HIPRI)
1890 return -EOPNOTSUPP;
1891 if (kiocb->ki_flags & IOCB_NOWAIT)
1892 return -EAGAIN;
1893
1894 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001895 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001896 ssize_t nr;
1897
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001898 if (!iov_iter_is_bvec(iter)) {
1899 iovec = iov_iter_iovec(iter);
1900 } else {
1901 /* fixed buffers import bvec */
1902 iovec.iov_base = kmap(iter->bvec->bv_page)
1903 + iter->iov_offset;
1904 iovec.iov_len = min(iter->count,
1905 iter->bvec->bv_len - iter->iov_offset);
1906 }
1907
Jens Axboe32960612019-09-23 11:05:34 -06001908 if (rw == READ) {
1909 nr = file->f_op->read(file, iovec.iov_base,
1910 iovec.iov_len, &kiocb->ki_pos);
1911 } else {
1912 nr = file->f_op->write(file, iovec.iov_base,
1913 iovec.iov_len, &kiocb->ki_pos);
1914 }
1915
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001916 if (iov_iter_is_bvec(iter))
1917 kunmap(iter->bvec->bv_page);
1918
Jens Axboe32960612019-09-23 11:05:34 -06001919 if (nr < 0) {
1920 if (!ret)
1921 ret = nr;
1922 break;
1923 }
1924 ret += nr;
1925 if (nr != iovec.iov_len)
1926 break;
1927 iov_iter_advance(iter, nr);
1928 }
1929
1930 return ret;
1931}
1932
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001933static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07001934 struct iovec *iovec, struct iovec *fast_iov,
1935 struct iov_iter *iter)
1936{
1937 req->io->rw.nr_segs = iter->nr_segs;
1938 req->io->rw.size = io_size;
1939 req->io->rw.iov = iovec;
1940 if (!req->io->rw.iov) {
1941 req->io->rw.iov = req->io->rw.fast_iov;
1942 memcpy(req->io->rw.iov, fast_iov,
1943 sizeof(struct iovec) * iter->nr_segs);
1944 }
1945}
1946
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001947static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07001948{
Jens Axboed3656342019-12-18 09:50:26 -07001949 if (!io_op_defs[req->opcode].async_ctx)
1950 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001951 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07001952 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001953}
1954
1955static void io_rw_async(struct io_wq_work **workptr)
1956{
1957 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1958 struct iovec *iov = NULL;
1959
1960 if (req->io->rw.iov != req->io->rw.fast_iov)
1961 iov = req->io->rw.iov;
1962 io_wq_submit_work(workptr);
1963 kfree(iov);
1964}
1965
1966static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
1967 struct iovec *iovec, struct iovec *fast_iov,
1968 struct iov_iter *iter)
1969{
Jens Axboe74566df2020-01-13 19:23:24 -07001970 if (req->opcode == IORING_OP_READ_FIXED ||
1971 req->opcode == IORING_OP_WRITE_FIXED)
1972 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001973 if (!req->io && io_alloc_async_ctx(req))
1974 return -ENOMEM;
1975
1976 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
1977 req->work.func = io_rw_async;
1978 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001979}
1980
Jens Axboe3529d8c2019-12-19 18:24:38 -07001981static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1982 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07001983{
Jens Axboe3529d8c2019-12-19 18:24:38 -07001984 struct io_async_ctx *io;
1985 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07001986 ssize_t ret;
1987
Jens Axboe3529d8c2019-12-19 18:24:38 -07001988 ret = io_prep_rw(req, sqe, force_nonblock);
1989 if (ret)
1990 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07001991
Jens Axboe3529d8c2019-12-19 18:24:38 -07001992 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1993 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07001994
Jens Axboe3529d8c2019-12-19 18:24:38 -07001995 if (!req->io)
1996 return 0;
1997
1998 io = req->io;
1999 io->rw.iov = io->rw.fast_iov;
2000 req->io = NULL;
2001 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2002 req->io = io;
2003 if (ret < 0)
2004 return ret;
2005
2006 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2007 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002008}
2009
Pavel Begunkov267bc902019-11-07 01:41:08 +03002010static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002011 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002012{
2013 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002014 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002015 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002016 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002017 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002018
Jens Axboe3529d8c2019-12-19 18:24:38 -07002019 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002020 if (ret < 0)
2021 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002022
Jens Axboefd6c2e42019-12-18 12:19:41 -07002023 /* Ensure we clear previously set non-block flag */
2024 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002025 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002026
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002027 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002028 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002029 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002030 req->result = io_size;
2031
2032 /*
2033 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2034 * we know to async punt it even if it was opened O_NONBLOCK
2035 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002036 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002037 req->flags |= REQ_F_MUST_PUNT;
2038 goto copy_iov;
2039 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002040
Jens Axboe31b51512019-01-18 22:56:34 -07002041 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002042 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002043 if (!ret) {
2044 ssize_t ret2;
2045
Jens Axboe9adbd452019-12-20 08:45:55 -07002046 if (req->file->f_op->read_iter)
2047 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002048 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002049 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002050
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002051 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002052 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002053 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002054 } else {
2055copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002056 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002057 inline_vecs, &iter);
2058 if (ret)
2059 goto out_free;
2060 return -EAGAIN;
2061 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002062 }
Jens Axboef67676d2019-12-02 11:03:47 -07002063out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002064 if (!io_wq_current_is_worker())
2065 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002066 return ret;
2067}
2068
Jens Axboe3529d8c2019-12-19 18:24:38 -07002069static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2070 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002071{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002072 struct io_async_ctx *io;
2073 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002074 ssize_t ret;
2075
Jens Axboe3529d8c2019-12-19 18:24:38 -07002076 ret = io_prep_rw(req, sqe, force_nonblock);
2077 if (ret)
2078 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002079
Jens Axboe3529d8c2019-12-19 18:24:38 -07002080 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2081 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002082
Jens Axboe3529d8c2019-12-19 18:24:38 -07002083 if (!req->io)
2084 return 0;
2085
2086 io = req->io;
2087 io->rw.iov = io->rw.fast_iov;
2088 req->io = NULL;
2089 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2090 req->io = io;
2091 if (ret < 0)
2092 return ret;
2093
2094 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2095 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002096}
2097
Pavel Begunkov267bc902019-11-07 01:41:08 +03002098static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002099 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002100{
2101 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002102 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002103 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002104 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002105 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002106
Jens Axboe3529d8c2019-12-19 18:24:38 -07002107 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002108 if (ret < 0)
2109 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002110
Jens Axboefd6c2e42019-12-18 12:19:41 -07002111 /* Ensure we clear previously set non-block flag */
2112 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002113 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002114
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002115 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002116 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002117 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002118 req->result = io_size;
2119
2120 /*
2121 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2122 * we know to async punt it even if it was opened O_NONBLOCK
2123 */
2124 if (force_nonblock && !io_file_supports_async(req->file)) {
2125 req->flags |= REQ_F_MUST_PUNT;
2126 goto copy_iov;
2127 }
2128
Jens Axboe10d59342019-12-09 20:16:22 -07002129 /* file path doesn't support NOWAIT for non-direct_IO */
2130 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2131 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002132 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002133
Jens Axboe31b51512019-01-18 22:56:34 -07002134 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002135 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002136 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002137 ssize_t ret2;
2138
Jens Axboe2b188cc2019-01-07 10:46:33 -07002139 /*
2140 * Open-code file_start_write here to grab freeze protection,
2141 * which will be released by another thread in
2142 * io_complete_rw(). Fool lockdep by telling it the lock got
2143 * released so that it doesn't complain about the held lock when
2144 * we return to userspace.
2145 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002146 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002147 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002148 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002149 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002150 SB_FREEZE_WRITE);
2151 }
2152 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002153
Jens Axboe9adbd452019-12-20 08:45:55 -07002154 if (req->file->f_op->write_iter)
2155 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002156 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002157 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002158 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002159 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002160 } else {
2161copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002162 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002163 inline_vecs, &iter);
2164 if (ret)
2165 goto out_free;
2166 return -EAGAIN;
2167 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002168 }
Jens Axboe31b51512019-01-18 22:56:34 -07002169out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002170 if (!io_wq_current_is_worker())
2171 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002172 return ret;
2173}
2174
2175/*
2176 * IORING_OP_NOP just posts a completion event, nothing else.
2177 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002178static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002179{
2180 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002181
Jens Axboedef596e2019-01-09 08:59:42 -07002182 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2183 return -EINVAL;
2184
Jens Axboe78e19bb2019-11-06 15:21:34 -07002185 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002186 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002187 return 0;
2188}
2189
Jens Axboe3529d8c2019-12-19 18:24:38 -07002190static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002191{
Jens Axboe6b063142019-01-10 22:13:58 -07002192 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002193
Jens Axboe09bb8392019-03-13 12:39:28 -06002194 if (!req->file)
2195 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002196
Jens Axboe6b063142019-01-10 22:13:58 -07002197 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002198 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002199 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002200 return -EINVAL;
2201
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002202 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2203 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2204 return -EINVAL;
2205
2206 req->sync.off = READ_ONCE(sqe->off);
2207 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002208 return 0;
2209}
2210
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002211static bool io_req_cancelled(struct io_kiocb *req)
2212{
2213 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2214 req_set_fail_links(req);
2215 io_cqring_add_event(req, -ECANCELED);
2216 io_put_req(req);
2217 return true;
2218 }
2219
2220 return false;
2221}
2222
Jens Axboe78912932020-01-14 22:09:06 -07002223static void io_link_work_cb(struct io_wq_work **workptr)
2224{
2225 struct io_wq_work *work = *workptr;
2226 struct io_kiocb *link = work->data;
2227
2228 io_queue_linked_timeout(link);
2229 work->func = io_wq_submit_work;
2230}
2231
2232static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2233{
2234 struct io_kiocb *link;
2235
2236 io_prep_async_work(nxt, &link);
2237 *workptr = &nxt->work;
2238 if (link) {
2239 nxt->work.flags |= IO_WQ_WORK_CB;
2240 nxt->work.func = io_link_work_cb;
2241 nxt->work.data = link;
2242 }
2243}
2244
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002245static void io_fsync_finish(struct io_wq_work **workptr)
2246{
2247 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2248 loff_t end = req->sync.off + req->sync.len;
2249 struct io_kiocb *nxt = NULL;
2250 int ret;
2251
2252 if (io_req_cancelled(req))
2253 return;
2254
Jens Axboe9adbd452019-12-20 08:45:55 -07002255 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002256 end > 0 ? end : LLONG_MAX,
2257 req->sync.flags & IORING_FSYNC_DATASYNC);
2258 if (ret < 0)
2259 req_set_fail_links(req);
2260 io_cqring_add_event(req, ret);
2261 io_put_req_find_next(req, &nxt);
2262 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002263 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002264}
2265
Jens Axboefc4df992019-12-10 14:38:45 -07002266static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2267 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002268{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002269 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002270
2271 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002272 if (force_nonblock) {
2273 io_put_req(req);
2274 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002275 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002276 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002277
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002278 work = old_work = &req->work;
2279 io_fsync_finish(&work);
2280 if (work && work != old_work)
2281 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002282 return 0;
2283}
2284
Jens Axboed63d1b52019-12-10 10:38:56 -07002285static void io_fallocate_finish(struct io_wq_work **workptr)
2286{
2287 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2288 struct io_kiocb *nxt = NULL;
2289 int ret;
2290
2291 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2292 req->sync.len);
2293 if (ret < 0)
2294 req_set_fail_links(req);
2295 io_cqring_add_event(req, ret);
2296 io_put_req_find_next(req, &nxt);
2297 if (nxt)
2298 io_wq_assign_next(workptr, nxt);
2299}
2300
2301static int io_fallocate_prep(struct io_kiocb *req,
2302 const struct io_uring_sqe *sqe)
2303{
2304 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2305 return -EINVAL;
2306
2307 req->sync.off = READ_ONCE(sqe->off);
2308 req->sync.len = READ_ONCE(sqe->addr);
2309 req->sync.mode = READ_ONCE(sqe->len);
2310 return 0;
2311}
2312
2313static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2314 bool force_nonblock)
2315{
2316 struct io_wq_work *work, *old_work;
2317
2318 /* fallocate always requiring blocking context */
2319 if (force_nonblock) {
2320 io_put_req(req);
2321 req->work.func = io_fallocate_finish;
2322 return -EAGAIN;
2323 }
2324
2325 work = old_work = &req->work;
2326 io_fallocate_finish(&work);
2327 if (work && work != old_work)
2328 *nxt = container_of(work, struct io_kiocb, work);
2329
2330 return 0;
2331}
2332
Jens Axboe15b71ab2019-12-11 11:20:36 -07002333static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2334{
2335 int ret;
2336
2337 if (sqe->ioprio || sqe->buf_index)
2338 return -EINVAL;
2339
2340 req->open.dfd = READ_ONCE(sqe->fd);
2341 req->open.mode = READ_ONCE(sqe->len);
2342 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2343 req->open.flags = READ_ONCE(sqe->open_flags);
2344
2345 req->open.filename = getname(req->open.fname);
2346 if (IS_ERR(req->open.filename)) {
2347 ret = PTR_ERR(req->open.filename);
2348 req->open.filename = NULL;
2349 return ret;
2350 }
2351
2352 return 0;
2353}
2354
2355static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2356 bool force_nonblock)
2357{
2358 struct open_flags op;
2359 struct open_how how;
2360 struct file *file;
2361 int ret;
2362
2363 if (force_nonblock) {
2364 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2365 return -EAGAIN;
2366 }
2367
2368 how = build_open_how(req->open.flags, req->open.mode);
2369 ret = build_open_flags(&how, &op);
2370 if (ret)
2371 goto err;
2372
2373 ret = get_unused_fd_flags(how.flags);
2374 if (ret < 0)
2375 goto err;
2376
2377 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2378 if (IS_ERR(file)) {
2379 put_unused_fd(ret);
2380 ret = PTR_ERR(file);
2381 } else {
2382 fsnotify_open(file);
2383 fd_install(ret, file);
2384 }
2385err:
2386 putname(req->open.filename);
2387 if (ret < 0)
2388 req_set_fail_links(req);
2389 io_cqring_add_event(req, ret);
2390 io_put_req_find_next(req, nxt);
2391 return 0;
2392}
2393
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002394static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2395{
2396 unsigned lookup_flags;
2397 int ret;
2398
2399 if (sqe->ioprio || sqe->buf_index)
2400 return -EINVAL;
2401
2402 req->open.dfd = READ_ONCE(sqe->fd);
2403 req->open.mask = READ_ONCE(sqe->len);
2404 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2405 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2406 req->open.flags = READ_ONCE(sqe->statx_flags);
2407
2408 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.flags))
2409 return -EINVAL;
2410
2411 req->open.filename = getname_flags(req->open.fname, lookup_flags, NULL);
2412 if (IS_ERR(req->open.filename)) {
2413 ret = PTR_ERR(req->open.filename);
2414 req->open.filename = NULL;
2415 return ret;
2416 }
2417
2418 return 0;
2419}
2420
2421static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2422 bool force_nonblock)
2423{
2424 struct io_open *ctx = &req->open;
2425 unsigned lookup_flags;
2426 struct path path;
2427 struct kstat stat;
2428 int ret;
2429
2430 if (force_nonblock)
2431 return -EAGAIN;
2432
2433 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->flags))
2434 return -EINVAL;
2435
2436retry:
2437 /* filename_lookup() drops it, keep a reference */
2438 ctx->filename->refcnt++;
2439
2440 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2441 NULL);
2442 if (ret)
2443 goto err;
2444
2445 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->flags);
2446 path_put(&path);
2447 if (retry_estale(ret, lookup_flags)) {
2448 lookup_flags |= LOOKUP_REVAL;
2449 goto retry;
2450 }
2451 if (!ret)
2452 ret = cp_statx(&stat, ctx->buffer);
2453err:
2454 putname(ctx->filename);
2455 if (ret < 0)
2456 req_set_fail_links(req);
2457 io_cqring_add_event(req, ret);
2458 io_put_req_find_next(req, nxt);
2459 return 0;
2460}
2461
Jens Axboeb5dba592019-12-11 14:02:38 -07002462static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2463{
2464 /*
2465 * If we queue this for async, it must not be cancellable. That would
2466 * leave the 'file' in an undeterminate state.
2467 */
2468 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2469
2470 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2471 sqe->rw_flags || sqe->buf_index)
2472 return -EINVAL;
2473 if (sqe->flags & IOSQE_FIXED_FILE)
2474 return -EINVAL;
2475
2476 req->close.fd = READ_ONCE(sqe->fd);
2477 if (req->file->f_op == &io_uring_fops ||
2478 req->close.fd == req->ring_fd)
2479 return -EBADF;
2480
2481 return 0;
2482}
2483
2484static void io_close_finish(struct io_wq_work **workptr)
2485{
2486 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2487 struct io_kiocb *nxt = NULL;
2488
2489 /* Invoked with files, we need to do the close */
2490 if (req->work.files) {
2491 int ret;
2492
2493 ret = filp_close(req->close.put_file, req->work.files);
2494 if (ret < 0) {
2495 req_set_fail_links(req);
2496 }
2497 io_cqring_add_event(req, ret);
2498 }
2499
2500 fput(req->close.put_file);
2501
2502 /* we bypassed the re-issue, drop the submission reference */
2503 io_put_req(req);
2504 io_put_req_find_next(req, &nxt);
2505 if (nxt)
2506 io_wq_assign_next(workptr, nxt);
2507}
2508
2509static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2510 bool force_nonblock)
2511{
2512 int ret;
2513
2514 req->close.put_file = NULL;
2515 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2516 if (ret < 0)
2517 return ret;
2518
2519 /* if the file has a flush method, be safe and punt to async */
2520 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2521 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2522 goto eagain;
2523 }
2524
2525 /*
2526 * No ->flush(), safely close from here and just punt the
2527 * fput() to async context.
2528 */
2529 ret = filp_close(req->close.put_file, current->files);
2530
2531 if (ret < 0)
2532 req_set_fail_links(req);
2533 io_cqring_add_event(req, ret);
2534
2535 if (io_wq_current_is_worker()) {
2536 struct io_wq_work *old_work, *work;
2537
2538 old_work = work = &req->work;
2539 io_close_finish(&work);
2540 if (work && work != old_work)
2541 *nxt = container_of(work, struct io_kiocb, work);
2542 return 0;
2543 }
2544
2545eagain:
2546 req->work.func = io_close_finish;
2547 return -EAGAIN;
2548}
2549
Jens Axboe3529d8c2019-12-19 18:24:38 -07002550static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002551{
2552 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002553
2554 if (!req->file)
2555 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002556
2557 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2558 return -EINVAL;
2559 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2560 return -EINVAL;
2561
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002562 req->sync.off = READ_ONCE(sqe->off);
2563 req->sync.len = READ_ONCE(sqe->len);
2564 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002565 return 0;
2566}
2567
2568static void io_sync_file_range_finish(struct io_wq_work **workptr)
2569{
2570 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2571 struct io_kiocb *nxt = NULL;
2572 int ret;
2573
2574 if (io_req_cancelled(req))
2575 return;
2576
Jens Axboe9adbd452019-12-20 08:45:55 -07002577 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002578 req->sync.flags);
2579 if (ret < 0)
2580 req_set_fail_links(req);
2581 io_cqring_add_event(req, ret);
2582 io_put_req_find_next(req, &nxt);
2583 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002584 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002585}
2586
Jens Axboefc4df992019-12-10 14:38:45 -07002587static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002588 bool force_nonblock)
2589{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002590 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002591
2592 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002593 if (force_nonblock) {
2594 io_put_req(req);
2595 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002596 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002597 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002598
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002599 work = old_work = &req->work;
2600 io_sync_file_range_finish(&work);
2601 if (work && work != old_work)
2602 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002603 return 0;
2604}
2605
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002606#if defined(CONFIG_NET)
2607static void io_sendrecv_async(struct io_wq_work **workptr)
2608{
2609 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2610 struct iovec *iov = NULL;
2611
2612 if (req->io->rw.iov != req->io->rw.fast_iov)
2613 iov = req->io->msg.iov;
2614 io_wq_submit_work(workptr);
2615 kfree(iov);
2616}
2617#endif
2618
Jens Axboe3529d8c2019-12-19 18:24:38 -07002619static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002620{
Jens Axboe03b12302019-12-02 18:50:25 -07002621#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002622 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002623 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002624
Jens Axboee47293f2019-12-20 08:58:21 -07002625 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2626 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe3529d8c2019-12-19 18:24:38 -07002627
2628 if (!io)
2629 return 0;
2630
Jens Axboed9688562019-12-09 19:35:20 -07002631 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002632 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002633 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002634#else
Jens Axboee47293f2019-12-20 08:58:21 -07002635 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002636#endif
2637}
2638
Jens Axboefc4df992019-12-10 14:38:45 -07002639static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2640 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002641{
2642#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002643 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002644 struct socket *sock;
2645 int ret;
2646
2647 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2648 return -EINVAL;
2649
2650 sock = sock_from_file(req->file, &ret);
2651 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002652 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002653 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002654 unsigned flags;
2655
Jens Axboe03b12302019-12-02 18:50:25 -07002656 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002657 kmsg = &req->io->msg;
2658 kmsg->msg.msg_name = &addr;
2659 /* if iov is set, it's allocated already */
2660 if (!kmsg->iov)
2661 kmsg->iov = kmsg->fast_iov;
2662 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002663 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002664 struct io_sr_msg *sr = &req->sr_msg;
2665
Jens Axboe0b416c32019-12-15 10:57:46 -07002666 kmsg = &io.msg;
2667 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002668
2669 io.msg.iov = io.msg.fast_iov;
2670 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2671 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002672 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002673 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002674 }
2675
Jens Axboee47293f2019-12-20 08:58:21 -07002676 flags = req->sr_msg.msg_flags;
2677 if (flags & MSG_DONTWAIT)
2678 req->flags |= REQ_F_NOWAIT;
2679 else if (force_nonblock)
2680 flags |= MSG_DONTWAIT;
2681
Jens Axboe0b416c32019-12-15 10:57:46 -07002682 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002683 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002684 if (req->io)
2685 return -EAGAIN;
2686 if (io_alloc_async_ctx(req))
2687 return -ENOMEM;
2688 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2689 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002690 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002691 }
2692 if (ret == -ERESTARTSYS)
2693 ret = -EINTR;
2694 }
2695
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002696 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002697 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002698 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002699 if (ret < 0)
2700 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002701 io_put_req_find_next(req, nxt);
2702 return 0;
2703#else
2704 return -EOPNOTSUPP;
2705#endif
2706}
2707
Jens Axboe3529d8c2019-12-19 18:24:38 -07002708static int io_recvmsg_prep(struct io_kiocb *req,
2709 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002710{
2711#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002712 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002713 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002714
Jens Axboe3529d8c2019-12-19 18:24:38 -07002715 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2716 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2717
2718 if (!io)
Jens Axboe06b76d42019-12-19 14:44:26 -07002719 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002720
Jens Axboed9688562019-12-09 19:35:20 -07002721 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002722 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002723 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002724#else
Jens Axboee47293f2019-12-20 08:58:21 -07002725 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002726#endif
2727}
2728
Jens Axboefc4df992019-12-10 14:38:45 -07002729static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2730 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002731{
2732#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002733 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002734 struct socket *sock;
2735 int ret;
2736
2737 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2738 return -EINVAL;
2739
2740 sock = sock_from_file(req->file, &ret);
2741 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002742 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002743 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002744 unsigned flags;
2745
Jens Axboe03b12302019-12-02 18:50:25 -07002746 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002747 kmsg = &req->io->msg;
2748 kmsg->msg.msg_name = &addr;
2749 /* if iov is set, it's allocated already */
2750 if (!kmsg->iov)
2751 kmsg->iov = kmsg->fast_iov;
2752 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002753 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002754 struct io_sr_msg *sr = &req->sr_msg;
2755
Jens Axboe0b416c32019-12-15 10:57:46 -07002756 kmsg = &io.msg;
2757 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002758
2759 io.msg.iov = io.msg.fast_iov;
2760 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
2761 sr->msg_flags, &io.msg.uaddr,
2762 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002763 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002764 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002765 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002766
Jens Axboee47293f2019-12-20 08:58:21 -07002767 flags = req->sr_msg.msg_flags;
2768 if (flags & MSG_DONTWAIT)
2769 req->flags |= REQ_F_NOWAIT;
2770 else if (force_nonblock)
2771 flags |= MSG_DONTWAIT;
2772
2773 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
2774 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002775 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002776 if (req->io)
2777 return -EAGAIN;
2778 if (io_alloc_async_ctx(req))
2779 return -ENOMEM;
2780 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2781 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002782 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002783 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002784 if (ret == -ERESTARTSYS)
2785 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002786 }
2787
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002788 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002789 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002790 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002791 if (ret < 0)
2792 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002793 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002794 return 0;
2795#else
2796 return -EOPNOTSUPP;
2797#endif
2798}
2799
Jens Axboe3529d8c2019-12-19 18:24:38 -07002800static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002801{
2802#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002803 struct io_accept *accept = &req->accept;
2804
Jens Axboe17f2fe32019-10-17 14:42:58 -06002805 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2806 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002807 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002808 return -EINVAL;
2809
Jens Axboed55e5f52019-12-11 16:12:15 -07002810 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2811 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002812 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002813 return 0;
2814#else
2815 return -EOPNOTSUPP;
2816#endif
2817}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002818
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002819#if defined(CONFIG_NET)
2820static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2821 bool force_nonblock)
2822{
2823 struct io_accept *accept = &req->accept;
2824 unsigned file_flags;
2825 int ret;
2826
2827 file_flags = force_nonblock ? O_NONBLOCK : 0;
2828 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2829 accept->addr_len, accept->flags);
2830 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002831 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002832 if (ret == -ERESTARTSYS)
2833 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002834 if (ret < 0)
2835 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002836 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002837 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002838 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002839}
2840
2841static void io_accept_finish(struct io_wq_work **workptr)
2842{
2843 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2844 struct io_kiocb *nxt = NULL;
2845
2846 if (io_req_cancelled(req))
2847 return;
2848 __io_accept(req, &nxt, false);
2849 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002850 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002851}
2852#endif
2853
2854static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2855 bool force_nonblock)
2856{
2857#if defined(CONFIG_NET)
2858 int ret;
2859
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002860 ret = __io_accept(req, nxt, force_nonblock);
2861 if (ret == -EAGAIN && force_nonblock) {
2862 req->work.func = io_accept_finish;
2863 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2864 io_put_req(req);
2865 return -EAGAIN;
2866 }
2867 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002868#else
2869 return -EOPNOTSUPP;
2870#endif
2871}
2872
Jens Axboe3529d8c2019-12-19 18:24:38 -07002873static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07002874{
2875#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002876 struct io_connect *conn = &req->connect;
2877 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07002878
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002879 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2880 return -EINVAL;
2881 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2882 return -EINVAL;
2883
Jens Axboe3529d8c2019-12-19 18:24:38 -07002884 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2885 conn->addr_len = READ_ONCE(sqe->addr2);
2886
2887 if (!io)
2888 return 0;
2889
2890 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002891 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002892#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002893 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07002894#endif
2895}
2896
Jens Axboefc4df992019-12-10 14:38:45 -07002897static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
2898 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07002899{
2900#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07002901 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002902 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002903 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002904
Jens Axboef499a022019-12-02 16:28:46 -07002905 if (req->io) {
2906 io = req->io;
2907 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002908 ret = move_addr_to_kernel(req->connect.addr,
2909 req->connect.addr_len,
2910 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002911 if (ret)
2912 goto out;
2913 io = &__io;
2914 }
2915
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002916 file_flags = force_nonblock ? O_NONBLOCK : 0;
2917
2918 ret = __sys_connect_file(req->file, &io->connect.address,
2919 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002920 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002921 if (req->io)
2922 return -EAGAIN;
2923 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07002924 ret = -ENOMEM;
2925 goto out;
2926 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002927 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07002928 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002929 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002930 if (ret == -ERESTARTSYS)
2931 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002932out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002933 if (ret < 0)
2934 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002935 io_cqring_add_event(req, ret);
2936 io_put_req_find_next(req, nxt);
2937 return 0;
2938#else
2939 return -EOPNOTSUPP;
2940#endif
2941}
2942
Jens Axboe221c5eb2019-01-17 09:41:58 -07002943static void io_poll_remove_one(struct io_kiocb *req)
2944{
2945 struct io_poll_iocb *poll = &req->poll;
2946
2947 spin_lock(&poll->head->lock);
2948 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07002949 if (!list_empty(&poll->wait.entry)) {
2950 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002951 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002952 }
2953 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002954 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002955}
2956
2957static void io_poll_remove_all(struct io_ring_ctx *ctx)
2958{
Jens Axboe78076bb2019-12-04 19:56:40 -07002959 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002960 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07002961 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002962
2963 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002964 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2965 struct hlist_head *list;
2966
2967 list = &ctx->cancel_hash[i];
2968 hlist_for_each_entry_safe(req, tmp, list, hash_node)
2969 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002970 }
2971 spin_unlock_irq(&ctx->completion_lock);
2972}
2973
Jens Axboe47f46762019-11-09 17:43:02 -07002974static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2975{
Jens Axboe78076bb2019-12-04 19:56:40 -07002976 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07002977 struct io_kiocb *req;
2978
Jens Axboe78076bb2019-12-04 19:56:40 -07002979 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2980 hlist_for_each_entry(req, list, hash_node) {
2981 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002982 io_poll_remove_one(req);
2983 return 0;
2984 }
Jens Axboe47f46762019-11-09 17:43:02 -07002985 }
2986
2987 return -ENOENT;
2988}
2989
Jens Axboe3529d8c2019-12-19 18:24:38 -07002990static int io_poll_remove_prep(struct io_kiocb *req,
2991 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002992{
Jens Axboe221c5eb2019-01-17 09:41:58 -07002993 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2994 return -EINVAL;
2995 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2996 sqe->poll_events)
2997 return -EINVAL;
2998
Jens Axboe0969e782019-12-17 18:40:57 -07002999 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003000 return 0;
3001}
3002
3003/*
3004 * Find a running poll command that matches one specified in sqe->addr,
3005 * and remove it if found.
3006 */
3007static int io_poll_remove(struct io_kiocb *req)
3008{
3009 struct io_ring_ctx *ctx = req->ctx;
3010 u64 addr;
3011 int ret;
3012
Jens Axboe0969e782019-12-17 18:40:57 -07003013 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003014 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003015 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003016 spin_unlock_irq(&ctx->completion_lock);
3017
Jens Axboe78e19bb2019-11-06 15:21:34 -07003018 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003019 if (ret < 0)
3020 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003021 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003022 return 0;
3023}
3024
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003025static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003026{
Jackie Liua197f662019-11-08 08:09:12 -07003027 struct io_ring_ctx *ctx = req->ctx;
3028
Jens Axboe8c838782019-03-12 15:48:16 -06003029 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003030 if (error)
3031 io_cqring_fill_event(req, error);
3032 else
3033 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003034 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003035}
3036
Jens Axboe561fb042019-10-24 07:25:42 -06003037static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003038{
Jens Axboe561fb042019-10-24 07:25:42 -06003039 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003040 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3041 struct io_poll_iocb *poll = &req->poll;
3042 struct poll_table_struct pt = { ._key = poll->events };
3043 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003044 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003045 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003046 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003047
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003048 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003049 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003050 ret = -ECANCELED;
3051 } else if (READ_ONCE(poll->canceled)) {
3052 ret = -ECANCELED;
3053 }
Jens Axboe561fb042019-10-24 07:25:42 -06003054
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003055 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003056 mask = vfs_poll(poll->file, &pt) & poll->events;
3057
3058 /*
3059 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3060 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3061 * synchronize with them. In the cancellation case the list_del_init
3062 * itself is not actually needed, but harmless so we keep it in to
3063 * avoid further branches in the fast path.
3064 */
3065 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003066 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003067 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003068 spin_unlock_irq(&ctx->completion_lock);
3069 return;
3070 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003071 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003072 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003073 spin_unlock_irq(&ctx->completion_lock);
3074
Jens Axboe8c838782019-03-12 15:48:16 -06003075 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003076
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003077 if (ret < 0)
3078 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003079 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003080 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003081 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003082}
3083
3084static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3085 void *key)
3086{
Jens Axboee9444752019-11-26 15:02:04 -07003087 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003088 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3089 struct io_ring_ctx *ctx = req->ctx;
3090 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06003091 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003092
3093 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003094 if (mask && !(mask & poll->events))
3095 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003096
Jens Axboe392edb42019-12-09 17:52:20 -07003097 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003098
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003099 /*
3100 * Run completion inline if we can. We're using trylock here because
3101 * we are violating the completion_lock -> poll wq lock ordering.
3102 * If we have a link timeout we're going to need the completion_lock
3103 * for finalizing the request, mark us as having grabbed that already.
3104 */
Jens Axboe8c838782019-03-12 15:48:16 -06003105 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboe78076bb2019-12-04 19:56:40 -07003106 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003107 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003108 req->flags |= REQ_F_COMP_LOCKED;
3109 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003110 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3111
3112 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06003113 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003114 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003115 }
3116
Jens Axboe221c5eb2019-01-17 09:41:58 -07003117 return 1;
3118}
3119
3120struct io_poll_table {
3121 struct poll_table_struct pt;
3122 struct io_kiocb *req;
3123 int error;
3124};
3125
3126static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3127 struct poll_table_struct *p)
3128{
3129 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3130
3131 if (unlikely(pt->req->poll.head)) {
3132 pt->error = -EINVAL;
3133 return;
3134 }
3135
3136 pt->error = 0;
3137 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003138 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003139}
3140
Jens Axboeeac406c2019-11-14 12:09:58 -07003141static void io_poll_req_insert(struct io_kiocb *req)
3142{
3143 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003144 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003145
Jens Axboe78076bb2019-12-04 19:56:40 -07003146 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3147 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003148}
3149
Jens Axboe3529d8c2019-12-19 18:24:38 -07003150static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003151{
3152 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003153 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003154
3155 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3156 return -EINVAL;
3157 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3158 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003159 if (!poll->file)
3160 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003161
Jens Axboe221c5eb2019-01-17 09:41:58 -07003162 events = READ_ONCE(sqe->poll_events);
3163 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003164 return 0;
3165}
3166
3167static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3168{
3169 struct io_poll_iocb *poll = &req->poll;
3170 struct io_ring_ctx *ctx = req->ctx;
3171 struct io_poll_table ipt;
3172 bool cancel = false;
3173 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003174
3175 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003176 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003177
Jens Axboe221c5eb2019-01-17 09:41:58 -07003178 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003179 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003180 poll->canceled = false;
3181
3182 ipt.pt._qproc = io_poll_queue_proc;
3183 ipt.pt._key = poll->events;
3184 ipt.req = req;
3185 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3186
3187 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003188 INIT_LIST_HEAD(&poll->wait.entry);
3189 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3190 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003191
Jens Axboe36703242019-07-25 10:20:18 -06003192 INIT_LIST_HEAD(&req->list);
3193
Jens Axboe221c5eb2019-01-17 09:41:58 -07003194 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003195
3196 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003197 if (likely(poll->head)) {
3198 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003199 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003200 if (ipt.error)
3201 cancel = true;
3202 ipt.error = 0;
3203 mask = 0;
3204 }
3205 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003206 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003207 else if (cancel)
3208 WRITE_ONCE(poll->canceled, true);
3209 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003210 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003211 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003212 }
Jens Axboe8c838782019-03-12 15:48:16 -06003213 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003214 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003215 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003216 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003217 spin_unlock_irq(&ctx->completion_lock);
3218
Jens Axboe8c838782019-03-12 15:48:16 -06003219 if (mask) {
3220 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003221 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003222 }
Jens Axboe8c838782019-03-12 15:48:16 -06003223 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003224}
3225
Jens Axboe5262f562019-09-17 12:26:57 -06003226static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3227{
Jens Axboead8a48a2019-11-15 08:49:11 -07003228 struct io_timeout_data *data = container_of(timer,
3229 struct io_timeout_data, timer);
3230 struct io_kiocb *req = data->req;
3231 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003232 unsigned long flags;
3233
Jens Axboe5262f562019-09-17 12:26:57 -06003234 atomic_inc(&ctx->cq_timeouts);
3235
3236 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003237 /*
Jens Axboe11365042019-10-16 09:08:32 -06003238 * We could be racing with timeout deletion. If the list is empty,
3239 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003240 */
Jens Axboe842f9612019-10-29 12:34:10 -06003241 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003242 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003243
Jens Axboe11365042019-10-16 09:08:32 -06003244 /*
3245 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003246 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003247 * pointer will be increased, otherwise other timeout reqs may
3248 * return in advance without waiting for enough wait_nr.
3249 */
3250 prev = req;
3251 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3252 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003253 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003254 }
Jens Axboe842f9612019-10-29 12:34:10 -06003255
Jens Axboe78e19bb2019-11-06 15:21:34 -07003256 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003257 io_commit_cqring(ctx);
3258 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3259
3260 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003261 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003262 io_put_req(req);
3263 return HRTIMER_NORESTART;
3264}
3265
Jens Axboe47f46762019-11-09 17:43:02 -07003266static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3267{
3268 struct io_kiocb *req;
3269 int ret = -ENOENT;
3270
3271 list_for_each_entry(req, &ctx->timeout_list, list) {
3272 if (user_data == req->user_data) {
3273 list_del_init(&req->list);
3274 ret = 0;
3275 break;
3276 }
3277 }
3278
3279 if (ret == -ENOENT)
3280 return ret;
3281
Jens Axboe2d283902019-12-04 11:08:05 -07003282 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003283 if (ret == -1)
3284 return -EALREADY;
3285
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003286 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003287 io_cqring_fill_event(req, -ECANCELED);
3288 io_put_req(req);
3289 return 0;
3290}
3291
Jens Axboe3529d8c2019-12-19 18:24:38 -07003292static int io_timeout_remove_prep(struct io_kiocb *req,
3293 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003294{
Jens Axboeb29472e2019-12-17 18:50:29 -07003295 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3296 return -EINVAL;
3297 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3298 return -EINVAL;
3299
3300 req->timeout.addr = READ_ONCE(sqe->addr);
3301 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3302 if (req->timeout.flags)
3303 return -EINVAL;
3304
Jens Axboeb29472e2019-12-17 18:50:29 -07003305 return 0;
3306}
3307
Jens Axboe11365042019-10-16 09:08:32 -06003308/*
3309 * Remove or update an existing timeout command
3310 */
Jens Axboefc4df992019-12-10 14:38:45 -07003311static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003312{
3313 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003314 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003315
Jens Axboe11365042019-10-16 09:08:32 -06003316 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003317 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003318
Jens Axboe47f46762019-11-09 17:43:02 -07003319 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003320 io_commit_cqring(ctx);
3321 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003322 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003323 if (ret < 0)
3324 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003325 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003326 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003327}
3328
Jens Axboe3529d8c2019-12-19 18:24:38 -07003329static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003330 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003331{
Jens Axboead8a48a2019-11-15 08:49:11 -07003332 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003333 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003334
Jens Axboead8a48a2019-11-15 08:49:11 -07003335 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003336 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003337 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003338 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003339 if (sqe->off && is_timeout_link)
3340 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003341 flags = READ_ONCE(sqe->timeout_flags);
3342 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003343 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003344
Jens Axboe26a61672019-12-20 09:02:01 -07003345 req->timeout.count = READ_ONCE(sqe->off);
3346
Jens Axboe3529d8c2019-12-19 18:24:38 -07003347 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003348 return -ENOMEM;
3349
3350 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003351 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003352 req->flags |= REQ_F_TIMEOUT;
3353
3354 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003355 return -EFAULT;
3356
Jens Axboe11365042019-10-16 09:08:32 -06003357 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003358 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003359 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003360 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003361
Jens Axboead8a48a2019-11-15 08:49:11 -07003362 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3363 return 0;
3364}
3365
Jens Axboefc4df992019-12-10 14:38:45 -07003366static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003367{
3368 unsigned count;
3369 struct io_ring_ctx *ctx = req->ctx;
3370 struct io_timeout_data *data;
3371 struct list_head *entry;
3372 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003373
Jens Axboe2d283902019-12-04 11:08:05 -07003374 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003375
Jens Axboe5262f562019-09-17 12:26:57 -06003376 /*
3377 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003378 * timeout event to be satisfied. If it isn't set, then this is
3379 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003380 */
Jens Axboe26a61672019-12-20 09:02:01 -07003381 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003382 if (!count) {
3383 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3384 spin_lock_irq(&ctx->completion_lock);
3385 entry = ctx->timeout_list.prev;
3386 goto add;
3387 }
Jens Axboe5262f562019-09-17 12:26:57 -06003388
3389 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003390 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003391
3392 /*
3393 * Insertion sort, ensuring the first entry in the list is always
3394 * the one we need first.
3395 */
Jens Axboe5262f562019-09-17 12:26:57 -06003396 spin_lock_irq(&ctx->completion_lock);
3397 list_for_each_prev(entry, &ctx->timeout_list) {
3398 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003399 unsigned nxt_sq_head;
3400 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003401 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003402
Jens Axboe93bd25b2019-11-11 23:34:31 -07003403 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3404 continue;
3405
yangerkun5da0fb12019-10-15 21:59:29 +08003406 /*
3407 * Since cached_sq_head + count - 1 can overflow, use type long
3408 * long to store it.
3409 */
3410 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003411 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3412 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003413
3414 /*
3415 * cached_sq_head may overflow, and it will never overflow twice
3416 * once there is some timeout req still be valid.
3417 */
3418 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003419 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003420
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003421 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003422 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003423
3424 /*
3425 * Sequence of reqs after the insert one and itself should
3426 * be adjusted because each timeout req consumes a slot.
3427 */
3428 span++;
3429 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003430 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003431 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003432add:
Jens Axboe5262f562019-09-17 12:26:57 -06003433 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003434 data->timer.function = io_timeout_fn;
3435 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003436 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003437 return 0;
3438}
3439
Jens Axboe62755e32019-10-28 21:49:21 -06003440static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003441{
Jens Axboe62755e32019-10-28 21:49:21 -06003442 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003443
Jens Axboe62755e32019-10-28 21:49:21 -06003444 return req->user_data == (unsigned long) data;
3445}
3446
Jens Axboee977d6d2019-11-05 12:39:45 -07003447static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003448{
Jens Axboe62755e32019-10-28 21:49:21 -06003449 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003450 int ret = 0;
3451
Jens Axboe62755e32019-10-28 21:49:21 -06003452 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3453 switch (cancel_ret) {
3454 case IO_WQ_CANCEL_OK:
3455 ret = 0;
3456 break;
3457 case IO_WQ_CANCEL_RUNNING:
3458 ret = -EALREADY;
3459 break;
3460 case IO_WQ_CANCEL_NOTFOUND:
3461 ret = -ENOENT;
3462 break;
3463 }
3464
Jens Axboee977d6d2019-11-05 12:39:45 -07003465 return ret;
3466}
3467
Jens Axboe47f46762019-11-09 17:43:02 -07003468static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3469 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003470 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003471{
3472 unsigned long flags;
3473 int ret;
3474
3475 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3476 if (ret != -ENOENT) {
3477 spin_lock_irqsave(&ctx->completion_lock, flags);
3478 goto done;
3479 }
3480
3481 spin_lock_irqsave(&ctx->completion_lock, flags);
3482 ret = io_timeout_cancel(ctx, sqe_addr);
3483 if (ret != -ENOENT)
3484 goto done;
3485 ret = io_poll_cancel(ctx, sqe_addr);
3486done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003487 if (!ret)
3488 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003489 io_cqring_fill_event(req, ret);
3490 io_commit_cqring(ctx);
3491 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3492 io_cqring_ev_posted(ctx);
3493
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003494 if (ret < 0)
3495 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003496 io_put_req_find_next(req, nxt);
3497}
3498
Jens Axboe3529d8c2019-12-19 18:24:38 -07003499static int io_async_cancel_prep(struct io_kiocb *req,
3500 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003501{
Jens Axboefbf23842019-12-17 18:45:56 -07003502 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003503 return -EINVAL;
3504 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3505 sqe->cancel_flags)
3506 return -EINVAL;
3507
Jens Axboefbf23842019-12-17 18:45:56 -07003508 req->cancel.addr = READ_ONCE(sqe->addr);
3509 return 0;
3510}
3511
3512static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3513{
3514 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003515
3516 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003517 return 0;
3518}
3519
Jens Axboe05f3fb32019-12-09 11:22:50 -07003520static int io_files_update_prep(struct io_kiocb *req,
3521 const struct io_uring_sqe *sqe)
3522{
3523 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3524 return -EINVAL;
3525
3526 req->files_update.offset = READ_ONCE(sqe->off);
3527 req->files_update.nr_args = READ_ONCE(sqe->len);
3528 if (!req->files_update.nr_args)
3529 return -EINVAL;
3530 req->files_update.arg = READ_ONCE(sqe->addr);
3531 return 0;
3532}
3533
3534static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3535{
3536 struct io_ring_ctx *ctx = req->ctx;
3537 struct io_uring_files_update up;
3538 int ret;
3539
3540 if (force_nonblock) {
3541 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3542 return -EAGAIN;
3543 }
3544
3545 up.offset = req->files_update.offset;
3546 up.fds = req->files_update.arg;
3547
3548 mutex_lock(&ctx->uring_lock);
3549 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3550 mutex_unlock(&ctx->uring_lock);
3551
3552 if (ret < 0)
3553 req_set_fail_links(req);
3554 io_cqring_add_event(req, ret);
3555 io_put_req(req);
3556 return 0;
3557}
3558
Jens Axboe3529d8c2019-12-19 18:24:38 -07003559static int io_req_defer_prep(struct io_kiocb *req,
3560 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003561{
Jens Axboee7815732019-12-17 19:45:06 -07003562 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003563
Jens Axboed625c6e2019-12-17 19:53:05 -07003564 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003565 case IORING_OP_NOP:
3566 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003567 case IORING_OP_READV:
3568 case IORING_OP_READ_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003569 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003570 break;
3571 case IORING_OP_WRITEV:
3572 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003573 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003574 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003575 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003576 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003577 break;
3578 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003579 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003580 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003581 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003582 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003583 break;
3584 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003585 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003586 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003587 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003588 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003589 break;
3590 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003591 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003592 break;
Jens Axboef499a022019-12-02 16:28:46 -07003593 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003594 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003595 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003596 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003597 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003598 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003599 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003600 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003601 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003602 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003603 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003604 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003605 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003606 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003607 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003608 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003609 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003610 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003611 case IORING_OP_FALLOCATE:
3612 ret = io_fallocate_prep(req, sqe);
3613 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003614 case IORING_OP_OPENAT:
3615 ret = io_openat_prep(req, sqe);
3616 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003617 case IORING_OP_CLOSE:
3618 ret = io_close_prep(req, sqe);
3619 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003620 case IORING_OP_FILES_UPDATE:
3621 ret = io_files_update_prep(req, sqe);
3622 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003623 case IORING_OP_STATX:
3624 ret = io_statx_prep(req, sqe);
3625 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003626 default:
Jens Axboee7815732019-12-17 19:45:06 -07003627 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3628 req->opcode);
3629 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003630 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003631 }
3632
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003633 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003634}
3635
Jens Axboe3529d8c2019-12-19 18:24:38 -07003636static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003637{
Jackie Liua197f662019-11-08 08:09:12 -07003638 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003639 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003640
Bob Liu9d858b22019-11-13 18:06:25 +08003641 /* Still need defer if there is pending req in defer list. */
3642 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003643 return 0;
3644
Jens Axboe3529d8c2019-12-19 18:24:38 -07003645 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003646 return -EAGAIN;
3647
Jens Axboe3529d8c2019-12-19 18:24:38 -07003648 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003649 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003650 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003651
Jens Axboede0617e2019-04-06 21:51:27 -06003652 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003653 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003654 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003655 return 0;
3656 }
3657
Jens Axboe915967f2019-11-21 09:01:20 -07003658 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003659 list_add_tail(&req->list, &ctx->defer_list);
3660 spin_unlock_irq(&ctx->completion_lock);
3661 return -EIOCBQUEUED;
3662}
3663
Jens Axboe3529d8c2019-12-19 18:24:38 -07003664static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3665 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003666{
Jackie Liua197f662019-11-08 08:09:12 -07003667 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07003668 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003669
Jens Axboed625c6e2019-12-17 19:53:05 -07003670 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003671 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003672 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003673 break;
3674 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003675 case IORING_OP_READ_FIXED:
3676 if (sqe) {
3677 ret = io_read_prep(req, sqe, force_nonblock);
3678 if (ret < 0)
3679 break;
3680 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003681 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003682 break;
3683 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07003684 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003685 if (sqe) {
3686 ret = io_write_prep(req, sqe, force_nonblock);
3687 if (ret < 0)
3688 break;
3689 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003690 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003691 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003692 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003693 if (sqe) {
3694 ret = io_prep_fsync(req, sqe);
3695 if (ret < 0)
3696 break;
3697 }
Jens Axboefc4df992019-12-10 14:38:45 -07003698 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003699 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003700 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003701 if (sqe) {
3702 ret = io_poll_add_prep(req, sqe);
3703 if (ret)
3704 break;
3705 }
Jens Axboefc4df992019-12-10 14:38:45 -07003706 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003707 break;
3708 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003709 if (sqe) {
3710 ret = io_poll_remove_prep(req, sqe);
3711 if (ret < 0)
3712 break;
3713 }
Jens Axboefc4df992019-12-10 14:38:45 -07003714 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003715 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003716 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003717 if (sqe) {
3718 ret = io_prep_sfr(req, sqe);
3719 if (ret < 0)
3720 break;
3721 }
Jens Axboefc4df992019-12-10 14:38:45 -07003722 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003723 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003724 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003725 if (sqe) {
3726 ret = io_sendmsg_prep(req, sqe);
3727 if (ret < 0)
3728 break;
3729 }
Jens Axboefc4df992019-12-10 14:38:45 -07003730 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003731 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003732 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003733 if (sqe) {
3734 ret = io_recvmsg_prep(req, sqe);
3735 if (ret)
3736 break;
3737 }
Jens Axboefc4df992019-12-10 14:38:45 -07003738 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003739 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003740 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003741 if (sqe) {
3742 ret = io_timeout_prep(req, sqe, false);
3743 if (ret)
3744 break;
3745 }
Jens Axboefc4df992019-12-10 14:38:45 -07003746 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003747 break;
Jens Axboe11365042019-10-16 09:08:32 -06003748 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003749 if (sqe) {
3750 ret = io_timeout_remove_prep(req, sqe);
3751 if (ret)
3752 break;
3753 }
Jens Axboefc4df992019-12-10 14:38:45 -07003754 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003755 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003756 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003757 if (sqe) {
3758 ret = io_accept_prep(req, sqe);
3759 if (ret)
3760 break;
3761 }
Jens Axboefc4df992019-12-10 14:38:45 -07003762 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003763 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003764 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003765 if (sqe) {
3766 ret = io_connect_prep(req, sqe);
3767 if (ret)
3768 break;
3769 }
Jens Axboefc4df992019-12-10 14:38:45 -07003770 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003771 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003772 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003773 if (sqe) {
3774 ret = io_async_cancel_prep(req, sqe);
3775 if (ret)
3776 break;
3777 }
Jens Axboefc4df992019-12-10 14:38:45 -07003778 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003779 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003780 case IORING_OP_FALLOCATE:
3781 if (sqe) {
3782 ret = io_fallocate_prep(req, sqe);
3783 if (ret)
3784 break;
3785 }
3786 ret = io_fallocate(req, nxt, force_nonblock);
3787 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003788 case IORING_OP_OPENAT:
3789 if (sqe) {
3790 ret = io_openat_prep(req, sqe);
3791 if (ret)
3792 break;
3793 }
3794 ret = io_openat(req, nxt, force_nonblock);
3795 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003796 case IORING_OP_CLOSE:
3797 if (sqe) {
3798 ret = io_close_prep(req, sqe);
3799 if (ret)
3800 break;
3801 }
3802 ret = io_close(req, nxt, force_nonblock);
3803 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003804 case IORING_OP_FILES_UPDATE:
3805 if (sqe) {
3806 ret = io_files_update_prep(req, sqe);
3807 if (ret)
3808 break;
3809 }
3810 ret = io_files_update(req, force_nonblock);
3811 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003812 case IORING_OP_STATX:
3813 if (sqe) {
3814 ret = io_statx_prep(req, sqe);
3815 if (ret)
3816 break;
3817 }
3818 ret = io_statx(req, nxt, force_nonblock);
3819 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003820 default:
3821 ret = -EINVAL;
3822 break;
3823 }
3824
Jens Axboedef596e2019-01-09 08:59:42 -07003825 if (ret)
3826 return ret;
3827
3828 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07003829 const bool in_async = io_wq_current_is_worker();
3830
Jens Axboe9e645e112019-05-10 16:07:28 -06003831 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07003832 return -EAGAIN;
3833
Jens Axboe11ba8202020-01-15 21:51:17 -07003834 /* workqueue context doesn't hold uring_lock, grab it now */
3835 if (in_async)
3836 mutex_lock(&ctx->uring_lock);
3837
Jens Axboedef596e2019-01-09 08:59:42 -07003838 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07003839
3840 if (in_async)
3841 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07003842 }
3843
3844 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003845}
3846
Jens Axboe561fb042019-10-24 07:25:42 -06003847static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003848{
Jens Axboe561fb042019-10-24 07:25:42 -06003849 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003850 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003851 struct io_kiocb *nxt = NULL;
3852 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003853
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003854 /* if NO_CANCEL is set, we must still run the work */
3855 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
3856 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003857 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003858 }
Jens Axboe31b51512019-01-18 22:56:34 -07003859
Jens Axboe561fb042019-10-24 07:25:42 -06003860 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003861 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3862 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003863 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003864 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003865 /*
3866 * We can get EAGAIN for polled IO even though we're
3867 * forcing a sync submission from here, since we can't
3868 * wait for request slots on the block side.
3869 */
3870 if (ret != -EAGAIN)
3871 break;
3872 cond_resched();
3873 } while (1);
3874 }
Jens Axboe31b51512019-01-18 22:56:34 -07003875
Jens Axboe561fb042019-10-24 07:25:42 -06003876 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003877 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003878
Jens Axboe561fb042019-10-24 07:25:42 -06003879 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003880 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003881 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003882 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003883 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003884
Jens Axboe561fb042019-10-24 07:25:42 -06003885 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07003886 if (!ret && nxt)
3887 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07003888}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003889
Jens Axboe15b71ab2019-12-11 11:20:36 -07003890static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06003891{
Jens Axboed3656342019-12-18 09:50:26 -07003892 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07003893 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07003894 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
3895 return 0;
3896 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06003897}
3898
Jens Axboe65e19f52019-10-26 07:20:21 -06003899static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3900 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003901{
Jens Axboe65e19f52019-10-26 07:20:21 -06003902 struct fixed_file_table *table;
3903
Jens Axboe05f3fb32019-12-09 11:22:50 -07003904 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
3905 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06003906}
3907
Jens Axboe3529d8c2019-12-19 18:24:38 -07003908static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
3909 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06003910{
Jackie Liua197f662019-11-08 08:09:12 -07003911 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003912 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07003913 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06003914
Jens Axboe3529d8c2019-12-19 18:24:38 -07003915 flags = READ_ONCE(sqe->flags);
3916 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003917
Jackie Liu4fe2c962019-09-09 20:50:40 +08003918 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003919 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003920
Jens Axboed3656342019-12-18 09:50:26 -07003921 if (!io_req_needs_file(req, fd))
3922 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06003923
3924 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07003925 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003926 (unsigned) fd >= ctx->nr_user_files))
3927 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003928 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003929 req->file = io_file_from_index(ctx, fd);
3930 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003931 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003932 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003933 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06003934 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003935 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003936 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003937 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003938 req->file = io_file_get(state, fd);
3939 if (unlikely(!req->file))
3940 return -EBADF;
3941 }
3942
3943 return 0;
3944}
3945
Jackie Liua197f662019-11-08 08:09:12 -07003946static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003947{
Jens Axboefcb323c2019-10-24 12:39:47 -06003948 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003949 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003950
Jens Axboeb5dba592019-12-11 14:02:38 -07003951 if (!req->ring_file)
3952 return -EBADF;
3953
Jens Axboefcb323c2019-10-24 12:39:47 -06003954 rcu_read_lock();
3955 spin_lock_irq(&ctx->inflight_lock);
3956 /*
3957 * We use the f_ops->flush() handler to ensure that we can flush
3958 * out work accessing these files if the fd is closed. Check if
3959 * the fd has changed since we started down this path, and disallow
3960 * this operation if it has.
3961 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003962 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003963 list_add(&req->inflight_entry, &ctx->inflight_list);
3964 req->flags |= REQ_F_INFLIGHT;
3965 req->work.files = current->files;
3966 ret = 0;
3967 }
3968 spin_unlock_irq(&ctx->inflight_lock);
3969 rcu_read_unlock();
3970
3971 return ret;
3972}
3973
Jens Axboe2665abf2019-11-05 12:40:47 -07003974static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3975{
Jens Axboead8a48a2019-11-15 08:49:11 -07003976 struct io_timeout_data *data = container_of(timer,
3977 struct io_timeout_data, timer);
3978 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003979 struct io_ring_ctx *ctx = req->ctx;
3980 struct io_kiocb *prev = NULL;
3981 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003982
3983 spin_lock_irqsave(&ctx->completion_lock, flags);
3984
3985 /*
3986 * We don't expect the list to be empty, that will only happen if we
3987 * race with the completion of the linked work.
3988 */
Pavel Begunkov44932332019-12-05 16:16:35 +03003989 if (!list_empty(&req->link_list)) {
3990 prev = list_entry(req->link_list.prev, struct io_kiocb,
3991 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003992 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03003993 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003994 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3995 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003996 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003997 }
3998
3999 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4000
4001 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004002 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004003 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4004 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004005 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004006 } else {
4007 io_cqring_add_event(req, -ETIME);
4008 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004009 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004010 return HRTIMER_NORESTART;
4011}
4012
Jens Axboead8a48a2019-11-15 08:49:11 -07004013static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004014{
Jens Axboe76a46e02019-11-10 23:34:16 -07004015 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004016
Jens Axboe76a46e02019-11-10 23:34:16 -07004017 /*
4018 * If the list is now empty, then our linked request finished before
4019 * we got a chance to setup the timer
4020 */
4021 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004022 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004023 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004024
Jens Axboead8a48a2019-11-15 08:49:11 -07004025 data->timer.function = io_link_timeout_fn;
4026 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4027 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004028 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004029 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004030
Jens Axboe2665abf2019-11-05 12:40:47 -07004031 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004032 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004033}
4034
Jens Axboead8a48a2019-11-15 08:49:11 -07004035static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004036{
4037 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004038
Jens Axboe2665abf2019-11-05 12:40:47 -07004039 if (!(req->flags & REQ_F_LINK))
4040 return NULL;
4041
Pavel Begunkov44932332019-12-05 16:16:35 +03004042 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4043 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004044 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004045 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004046
Jens Axboe76a46e02019-11-10 23:34:16 -07004047 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004048 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004049}
4050
Jens Axboe3529d8c2019-12-19 18:24:38 -07004051static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004052{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004053 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004054 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004055 int ret;
4056
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004057again:
4058 linked_timeout = io_prep_linked_timeout(req);
4059
Jens Axboe3529d8c2019-12-19 18:24:38 -07004060 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004061
4062 /*
4063 * We async punt it if the file wasn't marked NOWAIT, or if the file
4064 * doesn't support non-blocking read/write attempts
4065 */
4066 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4067 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004068 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4069 ret = io_grab_files(req);
4070 if (ret)
4071 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004072 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004073
4074 /*
4075 * Queued up for async execution, worker will release
4076 * submit reference when the iocb is actually submitted.
4077 */
4078 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004079 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004080 }
Jens Axboee65ef562019-03-12 10:16:44 -06004081
Jens Axboefcb323c2019-10-24 12:39:47 -06004082err:
Jens Axboee65ef562019-03-12 10:16:44 -06004083 /* drop submission reference */
4084 io_put_req(req);
4085
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004086 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004087 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004088 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004089 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004090 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004091 }
4092
Jens Axboee65ef562019-03-12 10:16:44 -06004093 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004094 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004095 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004096 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004097 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004098 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004099done_req:
4100 if (nxt) {
4101 req = nxt;
4102 nxt = NULL;
4103 goto again;
4104 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004105}
4106
Jens Axboe3529d8c2019-12-19 18:24:38 -07004107static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004108{
4109 int ret;
4110
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004111 if (unlikely(req->ctx->drain_next)) {
4112 req->flags |= REQ_F_IO_DRAIN;
4113 req->ctx->drain_next = false;
4114 }
4115 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
4116
Jens Axboe3529d8c2019-12-19 18:24:38 -07004117 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004118 if (ret) {
4119 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004120 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004121 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004122 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004123 }
Jens Axboece35a472019-12-17 08:04:44 -07004124 } else if ((req->flags & REQ_F_FORCE_ASYNC) &&
4125 !io_wq_current_is_worker()) {
4126 /*
4127 * Never try inline submit of IOSQE_ASYNC is set, go straight
4128 * to async execution.
4129 */
4130 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4131 io_queue_async_work(req);
4132 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004133 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004134 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004135}
4136
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004137static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004138{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004139 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004140 io_cqring_add_event(req, -ECANCELED);
4141 io_double_put_req(req);
4142 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004143 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004144}
4145
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004146#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004147 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004148
Jens Axboe3529d8c2019-12-19 18:24:38 -07004149static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4150 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004151{
Jackie Liua197f662019-11-08 08:09:12 -07004152 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004153 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004154 int ret;
4155
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004156 sqe_flags = READ_ONCE(sqe->flags);
4157
Jens Axboe9e645e112019-05-10 16:07:28 -06004158 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004159 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004160 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004161 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004162 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004163 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004164 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004165
Jens Axboe3529d8c2019-12-19 18:24:38 -07004166 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004167 if (unlikely(ret)) {
4168err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004169 io_cqring_add_event(req, ret);
4170 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004171 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004172 }
4173
Jens Axboe9e645e112019-05-10 16:07:28 -06004174 /*
4175 * If we already have a head request, queue this one for async
4176 * submittal once the head completes. If we don't have a head but
4177 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4178 * submitted sync once the chain is complete. If none of those
4179 * conditions are true (normal request), then just queue it.
4180 */
4181 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004182 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004183
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004184 if (sqe_flags & IOSQE_IO_DRAIN)
Pavel Begunkov9d763772019-12-17 02:22:07 +03004185 head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004186
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004187 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004188 req->flags |= REQ_F_HARDLINK;
4189
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004190 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004191 ret = -EAGAIN;
4192 goto err_req;
4193 }
4194
Jens Axboe3529d8c2019-12-19 18:24:38 -07004195 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004196 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004197 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004198 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004199 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004200 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004201 trace_io_uring_link(ctx, req, head);
4202 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004203
4204 /* last request of a link, enqueue the link */
4205 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4206 io_queue_link_head(head);
4207 *link = NULL;
4208 }
4209 } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004210 req->flags |= REQ_F_LINK;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004211 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004212 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004213
Jens Axboe9e645e112019-05-10 16:07:28 -06004214 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004215 ret = io_req_defer_prep(req, sqe);
4216 if (ret)
4217 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004218 *link = req;
4219 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004220 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004221 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004222
4223 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004224}
4225
Jens Axboe9a56a232019-01-09 09:06:50 -07004226/*
4227 * Batched submission is done, ensure local IO is flushed out.
4228 */
4229static void io_submit_state_end(struct io_submit_state *state)
4230{
4231 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004232 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004233 if (state->free_reqs)
4234 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4235 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004236}
4237
4238/*
4239 * Start submission side cache.
4240 */
4241static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004242 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004243{
4244 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004245 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004246 state->file = NULL;
4247 state->ios_left = max_ios;
4248}
4249
Jens Axboe2b188cc2019-01-07 10:46:33 -07004250static void io_commit_sqring(struct io_ring_ctx *ctx)
4251{
Hristo Venev75b28af2019-08-26 17:23:46 +00004252 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004253
Hristo Venev75b28af2019-08-26 17:23:46 +00004254 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004255 /*
4256 * Ensure any loads from the SQEs are done at this point,
4257 * since once we write the new head, the application could
4258 * write new data to them.
4259 */
Hristo Venev75b28af2019-08-26 17:23:46 +00004260 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004261 }
4262}
4263
4264/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004265 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004266 * that is mapped by userspace. This means that care needs to be taken to
4267 * ensure that reads are stable, as we cannot rely on userspace always
4268 * being a good citizen. If members of the sqe are validated and then later
4269 * used, it's important that those reads are done through READ_ONCE() to
4270 * prevent a re-load down the line.
4271 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004272static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4273 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004274{
Hristo Venev75b28af2019-08-26 17:23:46 +00004275 struct io_rings *rings = ctx->rings;
4276 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004277 unsigned head;
4278
4279 /*
4280 * The cached sq head (or cq tail) serves two purposes:
4281 *
4282 * 1) allows us to batch the cost of updating the user visible
4283 * head updates.
4284 * 2) allows the kernel side to track the head on its own, even
4285 * though the application is the one updating it.
4286 */
4287 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02004288 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004289 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004290 return false;
4291
Hristo Venev75b28af2019-08-26 17:23:46 +00004292 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004293 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004294 /*
4295 * All io need record the previous position, if LINK vs DARIN,
4296 * it can be used to mark the position of the first IO in the
4297 * link list.
4298 */
4299 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004300 *sqe_ptr = &ctx->sq_sqes[head];
4301 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4302 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004303 ctx->cached_sq_head++;
4304 return true;
4305 }
4306
4307 /* drop invalid entries */
4308 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004309 ctx->cached_sq_dropped++;
4310 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004311 return false;
4312}
4313
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004314static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004315 struct file *ring_file, int ring_fd,
4316 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004317{
4318 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004319 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004320 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004321 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004322
Jens Axboec4a2ed72019-11-21 21:01:26 -07004323 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004324 if (test_bit(0, &ctx->sq_check_overflow)) {
4325 if (!list_empty(&ctx->cq_overflow_list) &&
4326 !io_cqring_overflow_flush(ctx, false))
4327 return -EBUSY;
4328 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004329
4330 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004331 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004332 statep = &state;
4333 }
4334
4335 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004336 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004337 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004338
Pavel Begunkov196be952019-11-07 01:41:06 +03004339 req = io_get_req(ctx, statep);
4340 if (unlikely(!req)) {
4341 if (!submitted)
4342 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004343 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004344 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004345 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03004346 __io_free_req(req);
4347 break;
4348 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004349
Jens Axboed3656342019-12-18 09:50:26 -07004350 /* will complete beyond this point, count as submitted */
4351 submitted++;
4352
4353 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4354 io_cqring_add_event(req, -EINVAL);
4355 io_double_put_req(req);
4356 break;
4357 }
4358
4359 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004360 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4361 if (!mm_fault) {
4362 use_mm(ctx->sqo_mm);
4363 *mm = ctx->sqo_mm;
4364 }
4365 }
4366
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004367 req->ring_file = ring_file;
4368 req->ring_fd = ring_fd;
4369 req->has_user = *mm != NULL;
4370 req->in_async = async;
4371 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07004372 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004373 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004374 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004375 }
4376
Jens Axboe9e645e112019-05-10 16:07:28 -06004377 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004378 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004379 if (statep)
4380 io_submit_state_end(&state);
4381
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004382 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4383 io_commit_sqring(ctx);
4384
Jens Axboe6c271ce2019-01-10 11:22:30 -07004385 return submitted;
4386}
4387
4388static int io_sq_thread(void *data)
4389{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004390 struct io_ring_ctx *ctx = data;
4391 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004392 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004393 mm_segment_t old_fs;
4394 DEFINE_WAIT(wait);
4395 unsigned inflight;
4396 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004397 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004398
Jens Axboe206aefd2019-11-07 18:27:42 -07004399 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004400
Jens Axboe6c271ce2019-01-10 11:22:30 -07004401 old_fs = get_fs();
4402 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004403 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004404
Jens Axboec1edbf52019-11-10 16:56:04 -07004405 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004406 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004407 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004408
4409 if (inflight) {
4410 unsigned nr_events = 0;
4411
4412 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004413 /*
4414 * inflight is the count of the maximum possible
4415 * entries we submitted, but it can be smaller
4416 * if we dropped some of them. If we don't have
4417 * poll entries available, then we know that we
4418 * have nothing left to poll for. Reset the
4419 * inflight count to zero in that case.
4420 */
4421 mutex_lock(&ctx->uring_lock);
4422 if (!list_empty(&ctx->poll_list))
4423 __io_iopoll_check(ctx, &nr_events, 0);
4424 else
4425 inflight = 0;
4426 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004427 } else {
4428 /*
4429 * Normal IO, just pretend everything completed.
4430 * We don't have to poll completions for that.
4431 */
4432 nr_events = inflight;
4433 }
4434
4435 inflight -= nr_events;
4436 if (!inflight)
4437 timeout = jiffies + ctx->sq_thread_idle;
4438 }
4439
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004440 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004441
4442 /*
4443 * If submit got -EBUSY, flag us as needing the application
4444 * to enter the kernel to reap and flush events.
4445 */
4446 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004447 /*
4448 * We're polling. If we're within the defined idle
4449 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004450 * to sleep. The exception is if we got EBUSY doing
4451 * more IO, we should wait for the application to
4452 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004453 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004454 if (inflight ||
4455 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004456 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004457 continue;
4458 }
4459
4460 /*
4461 * Drop cur_mm before scheduling, we can't hold it for
4462 * long periods (or over schedule()). Do this before
4463 * adding ourselves to the waitqueue, as the unuse/drop
4464 * may sleep.
4465 */
4466 if (cur_mm) {
4467 unuse_mm(cur_mm);
4468 mmput(cur_mm);
4469 cur_mm = NULL;
4470 }
4471
4472 prepare_to_wait(&ctx->sqo_wait, &wait,
4473 TASK_INTERRUPTIBLE);
4474
4475 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004476 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004477 /* make sure to read SQ tail after writing flags */
4478 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004479
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004480 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004481 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004482 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004483 finish_wait(&ctx->sqo_wait, &wait);
4484 break;
4485 }
4486 if (signal_pending(current))
4487 flush_signals(current);
4488 schedule();
4489 finish_wait(&ctx->sqo_wait, &wait);
4490
Hristo Venev75b28af2019-08-26 17:23:46 +00004491 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004492 continue;
4493 }
4494 finish_wait(&ctx->sqo_wait, &wait);
4495
Hristo Venev75b28af2019-08-26 17:23:46 +00004496 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004497 }
4498
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004499 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004500 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004501 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004502 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004503 if (ret > 0)
4504 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004505 }
4506
4507 set_fs(old_fs);
4508 if (cur_mm) {
4509 unuse_mm(cur_mm);
4510 mmput(cur_mm);
4511 }
Jens Axboe181e4482019-11-25 08:52:30 -07004512 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004513
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004514 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004515
Jens Axboe6c271ce2019-01-10 11:22:30 -07004516 return 0;
4517}
4518
Jens Axboebda52162019-09-24 13:47:15 -06004519struct io_wait_queue {
4520 struct wait_queue_entry wq;
4521 struct io_ring_ctx *ctx;
4522 unsigned to_wait;
4523 unsigned nr_timeouts;
4524};
4525
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004526static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004527{
4528 struct io_ring_ctx *ctx = iowq->ctx;
4529
4530 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004531 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004532 * started waiting. For timeouts, we always want to return to userspace,
4533 * regardless of event count.
4534 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004535 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004536 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4537}
4538
4539static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4540 int wake_flags, void *key)
4541{
4542 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4543 wq);
4544
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004545 /* use noflush == true, as we can't safely rely on locking context */
4546 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004547 return -1;
4548
4549 return autoremove_wake_function(curr, mode, wake_flags, key);
4550}
4551
Jens Axboe2b188cc2019-01-07 10:46:33 -07004552/*
4553 * Wait until events become available, if we don't already have some. The
4554 * application must reap them itself, as they reside on the shared cq ring.
4555 */
4556static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4557 const sigset_t __user *sig, size_t sigsz)
4558{
Jens Axboebda52162019-09-24 13:47:15 -06004559 struct io_wait_queue iowq = {
4560 .wq = {
4561 .private = current,
4562 .func = io_wake_function,
4563 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4564 },
4565 .ctx = ctx,
4566 .to_wait = min_events,
4567 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004568 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004569 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004570
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004571 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004572 return 0;
4573
4574 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004575#ifdef CONFIG_COMPAT
4576 if (in_compat_syscall())
4577 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004578 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004579 else
4580#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004581 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004582
Jens Axboe2b188cc2019-01-07 10:46:33 -07004583 if (ret)
4584 return ret;
4585 }
4586
Jens Axboebda52162019-09-24 13:47:15 -06004587 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004588 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004589 do {
4590 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4591 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004592 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004593 break;
4594 schedule();
4595 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004596 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004597 break;
4598 }
4599 } while (1);
4600 finish_wait(&ctx->wait, &iowq.wq);
4601
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004602 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004603
Hristo Venev75b28af2019-08-26 17:23:46 +00004604 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004605}
4606
Jens Axboe6b063142019-01-10 22:13:58 -07004607static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4608{
4609#if defined(CONFIG_UNIX)
4610 if (ctx->ring_sock) {
4611 struct sock *sock = ctx->ring_sock->sk;
4612 struct sk_buff *skb;
4613
4614 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4615 kfree_skb(skb);
4616 }
4617#else
4618 int i;
4619
Jens Axboe65e19f52019-10-26 07:20:21 -06004620 for (i = 0; i < ctx->nr_user_files; i++) {
4621 struct file *file;
4622
4623 file = io_file_from_index(ctx, i);
4624 if (file)
4625 fput(file);
4626 }
Jens Axboe6b063142019-01-10 22:13:58 -07004627#endif
4628}
4629
Jens Axboe05f3fb32019-12-09 11:22:50 -07004630static void io_file_ref_kill(struct percpu_ref *ref)
4631{
4632 struct fixed_file_data *data;
4633
4634 data = container_of(ref, struct fixed_file_data, refs);
4635 complete(&data->done);
4636}
4637
Jens Axboe6b063142019-01-10 22:13:58 -07004638static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4639{
Jens Axboe05f3fb32019-12-09 11:22:50 -07004640 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06004641 unsigned nr_tables, i;
4642
Jens Axboe05f3fb32019-12-09 11:22:50 -07004643 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07004644 return -ENXIO;
4645
Jens Axboe05f3fb32019-12-09 11:22:50 -07004646 /* protect against inflight atomic switch, which drops the ref */
4647 flush_work(&data->ref_work);
4648 percpu_ref_get(&data->refs);
4649 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
4650 wait_for_completion(&data->done);
4651 percpu_ref_put(&data->refs);
4652 percpu_ref_exit(&data->refs);
4653
Jens Axboe6b063142019-01-10 22:13:58 -07004654 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004655 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4656 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004657 kfree(data->table[i].files);
4658 kfree(data->table);
4659 kfree(data);
4660 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004661 ctx->nr_user_files = 0;
4662 return 0;
4663}
4664
Jens Axboe6c271ce2019-01-10 11:22:30 -07004665static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4666{
4667 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004668 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004669 /*
4670 * The park is a bit of a work-around, without it we get
4671 * warning spews on shutdown with SQPOLL set and affinity
4672 * set to a single CPU.
4673 */
Jens Axboe06058632019-04-13 09:26:03 -06004674 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004675 kthread_stop(ctx->sqo_thread);
4676 ctx->sqo_thread = NULL;
4677 }
4678}
4679
Jens Axboe6b063142019-01-10 22:13:58 -07004680static void io_finish_async(struct io_ring_ctx *ctx)
4681{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004682 io_sq_thread_stop(ctx);
4683
Jens Axboe561fb042019-10-24 07:25:42 -06004684 if (ctx->io_wq) {
4685 io_wq_destroy(ctx->io_wq);
4686 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004687 }
4688}
4689
4690#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07004691/*
4692 * Ensure the UNIX gc is aware of our file set, so we are certain that
4693 * the io_uring can be safely unregistered on process exit, even if we have
4694 * loops in the file referencing.
4695 */
4696static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4697{
4698 struct sock *sk = ctx->ring_sock->sk;
4699 struct scm_fp_list *fpl;
4700 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004701 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004702
4703 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4704 unsigned long inflight = ctx->user->unix_inflight + nr;
4705
4706 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4707 return -EMFILE;
4708 }
4709
4710 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4711 if (!fpl)
4712 return -ENOMEM;
4713
4714 skb = alloc_skb(0, GFP_KERNEL);
4715 if (!skb) {
4716 kfree(fpl);
4717 return -ENOMEM;
4718 }
4719
4720 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004721
Jens Axboe08a45172019-10-03 08:11:03 -06004722 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004723 fpl->user = get_uid(ctx->user);
4724 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004725 struct file *file = io_file_from_index(ctx, i + offset);
4726
4727 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004728 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004729 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004730 unix_inflight(fpl->user, fpl->fp[nr_files]);
4731 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004732 }
4733
Jens Axboe08a45172019-10-03 08:11:03 -06004734 if (nr_files) {
4735 fpl->max = SCM_MAX_FD;
4736 fpl->count = nr_files;
4737 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004738 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06004739 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4740 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004741
Jens Axboe08a45172019-10-03 08:11:03 -06004742 for (i = 0; i < nr_files; i++)
4743 fput(fpl->fp[i]);
4744 } else {
4745 kfree_skb(skb);
4746 kfree(fpl);
4747 }
Jens Axboe6b063142019-01-10 22:13:58 -07004748
4749 return 0;
4750}
4751
4752/*
4753 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4754 * causes regular reference counting to break down. We rely on the UNIX
4755 * garbage collection to take care of this problem for us.
4756 */
4757static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4758{
4759 unsigned left, total;
4760 int ret = 0;
4761
4762 total = 0;
4763 left = ctx->nr_user_files;
4764 while (left) {
4765 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004766
4767 ret = __io_sqe_files_scm(ctx, this_files, total);
4768 if (ret)
4769 break;
4770 left -= this_files;
4771 total += this_files;
4772 }
4773
4774 if (!ret)
4775 return 0;
4776
4777 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004778 struct file *file = io_file_from_index(ctx, total);
4779
4780 if (file)
4781 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004782 total++;
4783 }
4784
4785 return ret;
4786}
4787#else
4788static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4789{
4790 return 0;
4791}
4792#endif
4793
Jens Axboe65e19f52019-10-26 07:20:21 -06004794static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4795 unsigned nr_files)
4796{
4797 int i;
4798
4799 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004800 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004801 unsigned this_files;
4802
4803 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4804 table->files = kcalloc(this_files, sizeof(struct file *),
4805 GFP_KERNEL);
4806 if (!table->files)
4807 break;
4808 nr_files -= this_files;
4809 }
4810
4811 if (i == nr_tables)
4812 return 0;
4813
4814 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004815 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004816 kfree(table->files);
4817 }
4818 return 1;
4819}
4820
Jens Axboe05f3fb32019-12-09 11:22:50 -07004821static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06004822{
4823#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06004824 struct sock *sock = ctx->ring_sock->sk;
4825 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4826 struct sk_buff *skb;
4827 int i;
4828
4829 __skb_queue_head_init(&list);
4830
4831 /*
4832 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4833 * remove this entry and rearrange the file array.
4834 */
4835 skb = skb_dequeue(head);
4836 while (skb) {
4837 struct scm_fp_list *fp;
4838
4839 fp = UNIXCB(skb).fp;
4840 for (i = 0; i < fp->count; i++) {
4841 int left;
4842
4843 if (fp->fp[i] != file)
4844 continue;
4845
4846 unix_notinflight(fp->user, fp->fp[i]);
4847 left = fp->count - 1 - i;
4848 if (left) {
4849 memmove(&fp->fp[i], &fp->fp[i + 1],
4850 left * sizeof(struct file *));
4851 }
4852 fp->count--;
4853 if (!fp->count) {
4854 kfree_skb(skb);
4855 skb = NULL;
4856 } else {
4857 __skb_queue_tail(&list, skb);
4858 }
4859 fput(file);
4860 file = NULL;
4861 break;
4862 }
4863
4864 if (!file)
4865 break;
4866
4867 __skb_queue_tail(&list, skb);
4868
4869 skb = skb_dequeue(head);
4870 }
4871
4872 if (skb_peek(&list)) {
4873 spin_lock_irq(&head->lock);
4874 while ((skb = __skb_dequeue(&list)) != NULL)
4875 __skb_queue_tail(head, skb);
4876 spin_unlock_irq(&head->lock);
4877 }
4878#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07004879 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06004880#endif
4881}
4882
Jens Axboe05f3fb32019-12-09 11:22:50 -07004883struct io_file_put {
4884 struct llist_node llist;
4885 struct file *file;
4886 struct completion *done;
4887};
4888
4889static void io_ring_file_ref_switch(struct work_struct *work)
4890{
4891 struct io_file_put *pfile, *tmp;
4892 struct fixed_file_data *data;
4893 struct llist_node *node;
4894
4895 data = container_of(work, struct fixed_file_data, ref_work);
4896
4897 while ((node = llist_del_all(&data->put_llist)) != NULL) {
4898 llist_for_each_entry_safe(pfile, tmp, node, llist) {
4899 io_ring_file_put(data->ctx, pfile->file);
4900 if (pfile->done)
4901 complete(pfile->done);
4902 else
4903 kfree(pfile);
4904 }
4905 }
4906
4907 percpu_ref_get(&data->refs);
4908 percpu_ref_switch_to_percpu(&data->refs);
4909}
4910
4911static void io_file_data_ref_zero(struct percpu_ref *ref)
4912{
4913 struct fixed_file_data *data;
4914
4915 data = container_of(ref, struct fixed_file_data, refs);
4916
4917 /* we can't safely switch from inside this context, punt to wq */
4918 queue_work(system_wq, &data->ref_work);
4919}
4920
4921static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
4922 unsigned nr_args)
4923{
4924 __s32 __user *fds = (__s32 __user *) arg;
4925 unsigned nr_tables;
4926 struct file *file;
4927 int fd, ret = 0;
4928 unsigned i;
4929
4930 if (ctx->file_data)
4931 return -EBUSY;
4932 if (!nr_args)
4933 return -EINVAL;
4934 if (nr_args > IORING_MAX_FIXED_FILES)
4935 return -EMFILE;
4936
4937 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
4938 if (!ctx->file_data)
4939 return -ENOMEM;
4940 ctx->file_data->ctx = ctx;
4941 init_completion(&ctx->file_data->done);
4942
4943 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
4944 ctx->file_data->table = kcalloc(nr_tables,
4945 sizeof(struct fixed_file_table),
4946 GFP_KERNEL);
4947 if (!ctx->file_data->table) {
4948 kfree(ctx->file_data);
4949 ctx->file_data = NULL;
4950 return -ENOMEM;
4951 }
4952
4953 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
4954 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
4955 kfree(ctx->file_data->table);
4956 kfree(ctx->file_data);
4957 ctx->file_data = NULL;
4958 return -ENOMEM;
4959 }
4960 ctx->file_data->put_llist.first = NULL;
4961 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
4962
4963 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
4964 percpu_ref_exit(&ctx->file_data->refs);
4965 kfree(ctx->file_data->table);
4966 kfree(ctx->file_data);
4967 ctx->file_data = NULL;
4968 return -ENOMEM;
4969 }
4970
4971 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
4972 struct fixed_file_table *table;
4973 unsigned index;
4974
4975 ret = -EFAULT;
4976 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
4977 break;
4978 /* allow sparse sets */
4979 if (fd == -1) {
4980 ret = 0;
4981 continue;
4982 }
4983
4984 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
4985 index = i & IORING_FILE_TABLE_MASK;
4986 file = fget(fd);
4987
4988 ret = -EBADF;
4989 if (!file)
4990 break;
4991
4992 /*
4993 * Don't allow io_uring instances to be registered. If UNIX
4994 * isn't enabled, then this causes a reference cycle and this
4995 * instance can never get freed. If UNIX is enabled we'll
4996 * handle it just fine, but there's still no point in allowing
4997 * a ring fd as it doesn't support regular read/write anyway.
4998 */
4999 if (file->f_op == &io_uring_fops) {
5000 fput(file);
5001 break;
5002 }
5003 ret = 0;
5004 table->files[index] = file;
5005 }
5006
5007 if (ret) {
5008 for (i = 0; i < ctx->nr_user_files; i++) {
5009 file = io_file_from_index(ctx, i);
5010 if (file)
5011 fput(file);
5012 }
5013 for (i = 0; i < nr_tables; i++)
5014 kfree(ctx->file_data->table[i].files);
5015
5016 kfree(ctx->file_data->table);
5017 kfree(ctx->file_data);
5018 ctx->file_data = NULL;
5019 ctx->nr_user_files = 0;
5020 return ret;
5021 }
5022
5023 ret = io_sqe_files_scm(ctx);
5024 if (ret)
5025 io_sqe_files_unregister(ctx);
5026
5027 return ret;
5028}
5029
Jens Axboec3a31e62019-10-03 13:59:56 -06005030static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5031 int index)
5032{
5033#if defined(CONFIG_UNIX)
5034 struct sock *sock = ctx->ring_sock->sk;
5035 struct sk_buff_head *head = &sock->sk_receive_queue;
5036 struct sk_buff *skb;
5037
5038 /*
5039 * See if we can merge this file into an existing skb SCM_RIGHTS
5040 * file set. If there's no room, fall back to allocating a new skb
5041 * and filling it in.
5042 */
5043 spin_lock_irq(&head->lock);
5044 skb = skb_peek(head);
5045 if (skb) {
5046 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5047
5048 if (fpl->count < SCM_MAX_FD) {
5049 __skb_unlink(skb, head);
5050 spin_unlock_irq(&head->lock);
5051 fpl->fp[fpl->count] = get_file(file);
5052 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5053 fpl->count++;
5054 spin_lock_irq(&head->lock);
5055 __skb_queue_head(head, skb);
5056 } else {
5057 skb = NULL;
5058 }
5059 }
5060 spin_unlock_irq(&head->lock);
5061
5062 if (skb) {
5063 fput(file);
5064 return 0;
5065 }
5066
5067 return __io_sqe_files_scm(ctx, 1, index);
5068#else
5069 return 0;
5070#endif
5071}
5072
Jens Axboe05f3fb32019-12-09 11:22:50 -07005073static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005074{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005075 struct fixed_file_data *data;
5076
5077 data = container_of(ref, struct fixed_file_data, refs);
5078 clear_bit(FFD_F_ATOMIC, &data->state);
5079}
5080
5081static bool io_queue_file_removal(struct fixed_file_data *data,
5082 struct file *file)
5083{
5084 struct io_file_put *pfile, pfile_stack;
5085 DECLARE_COMPLETION_ONSTACK(done);
5086
5087 /*
5088 * If we fail allocating the struct we need for doing async reomval
5089 * of this file, just punt to sync and wait for it.
5090 */
5091 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5092 if (!pfile) {
5093 pfile = &pfile_stack;
5094 pfile->done = &done;
5095 }
5096
5097 pfile->file = file;
5098 llist_add(&pfile->llist, &data->put_llist);
5099
5100 if (pfile == &pfile_stack) {
5101 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5102 percpu_ref_put(&data->refs);
5103 percpu_ref_switch_to_atomic(&data->refs,
5104 io_atomic_switch);
5105 }
5106 wait_for_completion(&done);
5107 flush_work(&data->ref_work);
5108 return false;
5109 }
5110
5111 return true;
5112}
5113
5114static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5115 struct io_uring_files_update *up,
5116 unsigned nr_args)
5117{
5118 struct fixed_file_data *data = ctx->file_data;
5119 bool ref_switch = false;
5120 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005121 __s32 __user *fds;
5122 int fd, i, err;
5123 __u32 done;
5124
Jens Axboe05f3fb32019-12-09 11:22:50 -07005125 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005126 return -EOVERFLOW;
5127 if (done > ctx->nr_user_files)
5128 return -EINVAL;
5129
5130 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005131 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005132 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005133 struct fixed_file_table *table;
5134 unsigned index;
5135
Jens Axboec3a31e62019-10-03 13:59:56 -06005136 err = 0;
5137 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5138 err = -EFAULT;
5139 break;
5140 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005141 i = array_index_nospec(up->offset, ctx->nr_user_files);
5142 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005143 index = i & IORING_FILE_TABLE_MASK;
5144 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005145 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005146 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005147 if (io_queue_file_removal(data, file))
5148 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005149 }
5150 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005151 file = fget(fd);
5152 if (!file) {
5153 err = -EBADF;
5154 break;
5155 }
5156 /*
5157 * Don't allow io_uring instances to be registered. If
5158 * UNIX isn't enabled, then this causes a reference
5159 * cycle and this instance can never get freed. If UNIX
5160 * is enabled we'll handle it just fine, but there's
5161 * still no point in allowing a ring fd as it doesn't
5162 * support regular read/write anyway.
5163 */
5164 if (file->f_op == &io_uring_fops) {
5165 fput(file);
5166 err = -EBADF;
5167 break;
5168 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005169 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005170 err = io_sqe_file_register(ctx, file, i);
5171 if (err)
5172 break;
5173 }
5174 nr_args--;
5175 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005176 up->offset++;
5177 }
5178
5179 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5180 percpu_ref_put(&data->refs);
5181 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005182 }
5183
5184 return done ? done : err;
5185}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005186static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5187 unsigned nr_args)
5188{
5189 struct io_uring_files_update up;
5190
5191 if (!ctx->file_data)
5192 return -ENXIO;
5193 if (!nr_args)
5194 return -EINVAL;
5195 if (copy_from_user(&up, arg, sizeof(up)))
5196 return -EFAULT;
5197 if (up.resv)
5198 return -EINVAL;
5199
5200 return __io_sqe_files_update(ctx, &up, nr_args);
5201}
Jens Axboec3a31e62019-10-03 13:59:56 -06005202
Jens Axboe7d723062019-11-12 22:31:31 -07005203static void io_put_work(struct io_wq_work *work)
5204{
5205 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5206
5207 io_put_req(req);
5208}
5209
5210static void io_get_work(struct io_wq_work *work)
5211{
5212 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5213
5214 refcount_inc(&req->refs);
5215}
5216
Jens Axboe6c271ce2019-01-10 11:22:30 -07005217static int io_sq_offload_start(struct io_ring_ctx *ctx,
5218 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005219{
Jens Axboe576a3472019-11-25 08:49:20 -07005220 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005221 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005222 int ret;
5223
Jens Axboe6c271ce2019-01-10 11:22:30 -07005224 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005225 mmgrab(current->mm);
5226 ctx->sqo_mm = current->mm;
5227
Jens Axboe6c271ce2019-01-10 11:22:30 -07005228 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005229 ret = -EPERM;
5230 if (!capable(CAP_SYS_ADMIN))
5231 goto err;
5232
Jens Axboe917257d2019-04-13 09:28:55 -06005233 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5234 if (!ctx->sq_thread_idle)
5235 ctx->sq_thread_idle = HZ;
5236
Jens Axboe6c271ce2019-01-10 11:22:30 -07005237 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005238 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005239
Jens Axboe917257d2019-04-13 09:28:55 -06005240 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005241 if (cpu >= nr_cpu_ids)
5242 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005243 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005244 goto err;
5245
Jens Axboe6c271ce2019-01-10 11:22:30 -07005246 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5247 ctx, cpu,
5248 "io_uring-sq");
5249 } else {
5250 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5251 "io_uring-sq");
5252 }
5253 if (IS_ERR(ctx->sqo_thread)) {
5254 ret = PTR_ERR(ctx->sqo_thread);
5255 ctx->sqo_thread = NULL;
5256 goto err;
5257 }
5258 wake_up_process(ctx->sqo_thread);
5259 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5260 /* Can't have SQ_AFF without SQPOLL */
5261 ret = -EINVAL;
5262 goto err;
5263 }
5264
Jens Axboe576a3472019-11-25 08:49:20 -07005265 data.mm = ctx->sqo_mm;
5266 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005267 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005268 data.get_work = io_get_work;
5269 data.put_work = io_put_work;
5270
Jens Axboe561fb042019-10-24 07:25:42 -06005271 /* Do QD, or 4 * CPUS, whatever is smallest */
5272 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005273 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005274 if (IS_ERR(ctx->io_wq)) {
5275 ret = PTR_ERR(ctx->io_wq);
5276 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005277 goto err;
5278 }
5279
5280 return 0;
5281err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005282 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005283 mmdrop(ctx->sqo_mm);
5284 ctx->sqo_mm = NULL;
5285 return ret;
5286}
5287
5288static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5289{
5290 atomic_long_sub(nr_pages, &user->locked_vm);
5291}
5292
5293static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5294{
5295 unsigned long page_limit, cur_pages, new_pages;
5296
5297 /* Don't allow more pages than we can safely lock */
5298 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5299
5300 do {
5301 cur_pages = atomic_long_read(&user->locked_vm);
5302 new_pages = cur_pages + nr_pages;
5303 if (new_pages > page_limit)
5304 return -ENOMEM;
5305 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5306 new_pages) != cur_pages);
5307
5308 return 0;
5309}
5310
5311static void io_mem_free(void *ptr)
5312{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005313 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005314
Mark Rutland52e04ef2019-04-30 17:30:21 +01005315 if (!ptr)
5316 return;
5317
5318 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005319 if (put_page_testzero(page))
5320 free_compound_page(page);
5321}
5322
5323static void *io_mem_alloc(size_t size)
5324{
5325 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5326 __GFP_NORETRY;
5327
5328 return (void *) __get_free_pages(gfp_flags, get_order(size));
5329}
5330
Hristo Venev75b28af2019-08-26 17:23:46 +00005331static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5332 size_t *sq_offset)
5333{
5334 struct io_rings *rings;
5335 size_t off, sq_array_size;
5336
5337 off = struct_size(rings, cqes, cq_entries);
5338 if (off == SIZE_MAX)
5339 return SIZE_MAX;
5340
5341#ifdef CONFIG_SMP
5342 off = ALIGN(off, SMP_CACHE_BYTES);
5343 if (off == 0)
5344 return SIZE_MAX;
5345#endif
5346
5347 sq_array_size = array_size(sizeof(u32), sq_entries);
5348 if (sq_array_size == SIZE_MAX)
5349 return SIZE_MAX;
5350
5351 if (check_add_overflow(off, sq_array_size, &off))
5352 return SIZE_MAX;
5353
5354 if (sq_offset)
5355 *sq_offset = off;
5356
5357 return off;
5358}
5359
Jens Axboe2b188cc2019-01-07 10:46:33 -07005360static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5361{
Hristo Venev75b28af2019-08-26 17:23:46 +00005362 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005363
Hristo Venev75b28af2019-08-26 17:23:46 +00005364 pages = (size_t)1 << get_order(
5365 rings_size(sq_entries, cq_entries, NULL));
5366 pages += (size_t)1 << get_order(
5367 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005368
Hristo Venev75b28af2019-08-26 17:23:46 +00005369 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005370}
5371
Jens Axboeedafcce2019-01-09 09:16:05 -07005372static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5373{
5374 int i, j;
5375
5376 if (!ctx->user_bufs)
5377 return -ENXIO;
5378
5379 for (i = 0; i < ctx->nr_user_bufs; i++) {
5380 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5381
5382 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005383 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005384
5385 if (ctx->account_mem)
5386 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005387 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005388 imu->nr_bvecs = 0;
5389 }
5390
5391 kfree(ctx->user_bufs);
5392 ctx->user_bufs = NULL;
5393 ctx->nr_user_bufs = 0;
5394 return 0;
5395}
5396
5397static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5398 void __user *arg, unsigned index)
5399{
5400 struct iovec __user *src;
5401
5402#ifdef CONFIG_COMPAT
5403 if (ctx->compat) {
5404 struct compat_iovec __user *ciovs;
5405 struct compat_iovec ciov;
5406
5407 ciovs = (struct compat_iovec __user *) arg;
5408 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5409 return -EFAULT;
5410
Jens Axboed55e5f52019-12-11 16:12:15 -07005411 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005412 dst->iov_len = ciov.iov_len;
5413 return 0;
5414 }
5415#endif
5416 src = (struct iovec __user *) arg;
5417 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5418 return -EFAULT;
5419 return 0;
5420}
5421
5422static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5423 unsigned nr_args)
5424{
5425 struct vm_area_struct **vmas = NULL;
5426 struct page **pages = NULL;
5427 int i, j, got_pages = 0;
5428 int ret = -EINVAL;
5429
5430 if (ctx->user_bufs)
5431 return -EBUSY;
5432 if (!nr_args || nr_args > UIO_MAXIOV)
5433 return -EINVAL;
5434
5435 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5436 GFP_KERNEL);
5437 if (!ctx->user_bufs)
5438 return -ENOMEM;
5439
5440 for (i = 0; i < nr_args; i++) {
5441 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5442 unsigned long off, start, end, ubuf;
5443 int pret, nr_pages;
5444 struct iovec iov;
5445 size_t size;
5446
5447 ret = io_copy_iov(ctx, &iov, arg, i);
5448 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005449 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005450
5451 /*
5452 * Don't impose further limits on the size and buffer
5453 * constraints here, we'll -EINVAL later when IO is
5454 * submitted if they are wrong.
5455 */
5456 ret = -EFAULT;
5457 if (!iov.iov_base || !iov.iov_len)
5458 goto err;
5459
5460 /* arbitrary limit, but we need something */
5461 if (iov.iov_len > SZ_1G)
5462 goto err;
5463
5464 ubuf = (unsigned long) iov.iov_base;
5465 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5466 start = ubuf >> PAGE_SHIFT;
5467 nr_pages = end - start;
5468
5469 if (ctx->account_mem) {
5470 ret = io_account_mem(ctx->user, nr_pages);
5471 if (ret)
5472 goto err;
5473 }
5474
5475 ret = 0;
5476 if (!pages || nr_pages > got_pages) {
5477 kfree(vmas);
5478 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005479 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005480 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005481 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005482 sizeof(struct vm_area_struct *),
5483 GFP_KERNEL);
5484 if (!pages || !vmas) {
5485 ret = -ENOMEM;
5486 if (ctx->account_mem)
5487 io_unaccount_mem(ctx->user, nr_pages);
5488 goto err;
5489 }
5490 got_pages = nr_pages;
5491 }
5492
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005493 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005494 GFP_KERNEL);
5495 ret = -ENOMEM;
5496 if (!imu->bvec) {
5497 if (ctx->account_mem)
5498 io_unaccount_mem(ctx->user, nr_pages);
5499 goto err;
5500 }
5501
5502 ret = 0;
5503 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005504 pret = get_user_pages(ubuf, nr_pages,
5505 FOLL_WRITE | FOLL_LONGTERM,
5506 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005507 if (pret == nr_pages) {
5508 /* don't support file backed memory */
5509 for (j = 0; j < nr_pages; j++) {
5510 struct vm_area_struct *vma = vmas[j];
5511
5512 if (vma->vm_file &&
5513 !is_file_hugepages(vma->vm_file)) {
5514 ret = -EOPNOTSUPP;
5515 break;
5516 }
5517 }
5518 } else {
5519 ret = pret < 0 ? pret : -EFAULT;
5520 }
5521 up_read(&current->mm->mmap_sem);
5522 if (ret) {
5523 /*
5524 * if we did partial map, or found file backed vmas,
5525 * release any pages we did get
5526 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005527 if (pret > 0)
5528 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005529 if (ctx->account_mem)
5530 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005531 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005532 goto err;
5533 }
5534
5535 off = ubuf & ~PAGE_MASK;
5536 size = iov.iov_len;
5537 for (j = 0; j < nr_pages; j++) {
5538 size_t vec_len;
5539
5540 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5541 imu->bvec[j].bv_page = pages[j];
5542 imu->bvec[j].bv_len = vec_len;
5543 imu->bvec[j].bv_offset = off;
5544 off = 0;
5545 size -= vec_len;
5546 }
5547 /* store original address for later verification */
5548 imu->ubuf = ubuf;
5549 imu->len = iov.iov_len;
5550 imu->nr_bvecs = nr_pages;
5551
5552 ctx->nr_user_bufs++;
5553 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005554 kvfree(pages);
5555 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005556 return 0;
5557err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005558 kvfree(pages);
5559 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005560 io_sqe_buffer_unregister(ctx);
5561 return ret;
5562}
5563
Jens Axboe9b402842019-04-11 11:45:41 -06005564static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5565{
5566 __s32 __user *fds = arg;
5567 int fd;
5568
5569 if (ctx->cq_ev_fd)
5570 return -EBUSY;
5571
5572 if (copy_from_user(&fd, fds, sizeof(*fds)))
5573 return -EFAULT;
5574
5575 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5576 if (IS_ERR(ctx->cq_ev_fd)) {
5577 int ret = PTR_ERR(ctx->cq_ev_fd);
5578 ctx->cq_ev_fd = NULL;
5579 return ret;
5580 }
5581
5582 return 0;
5583}
5584
5585static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5586{
5587 if (ctx->cq_ev_fd) {
5588 eventfd_ctx_put(ctx->cq_ev_fd);
5589 ctx->cq_ev_fd = NULL;
5590 return 0;
5591 }
5592
5593 return -ENXIO;
5594}
5595
Jens Axboe2b188cc2019-01-07 10:46:33 -07005596static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5597{
Jens Axboe6b063142019-01-10 22:13:58 -07005598 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005599 if (ctx->sqo_mm)
5600 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005601
5602 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005603 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005604 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005605 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005606
Jens Axboe2b188cc2019-01-07 10:46:33 -07005607#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005608 if (ctx->ring_sock) {
5609 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005610 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005611 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005612#endif
5613
Hristo Venev75b28af2019-08-26 17:23:46 +00005614 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005615 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005616
5617 percpu_ref_exit(&ctx->refs);
5618 if (ctx->account_mem)
5619 io_unaccount_mem(ctx->user,
5620 ring_pages(ctx->sq_entries, ctx->cq_entries));
5621 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07005622 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07005623 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07005624 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07005625 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005626 kfree(ctx);
5627}
5628
5629static __poll_t io_uring_poll(struct file *file, poll_table *wait)
5630{
5631 struct io_ring_ctx *ctx = file->private_data;
5632 __poll_t mask = 0;
5633
5634 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02005635 /*
5636 * synchronizes with barrier from wq_has_sleeper call in
5637 * io_commit_cqring
5638 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005639 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00005640 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
5641 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005642 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08005643 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005644 mask |= EPOLLIN | EPOLLRDNORM;
5645
5646 return mask;
5647}
5648
5649static int io_uring_fasync(int fd, struct file *file, int on)
5650{
5651 struct io_ring_ctx *ctx = file->private_data;
5652
5653 return fasync_helper(fd, file, on, &ctx->cq_fasync);
5654}
5655
5656static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
5657{
5658 mutex_lock(&ctx->uring_lock);
5659 percpu_ref_kill(&ctx->refs);
5660 mutex_unlock(&ctx->uring_lock);
5661
Jens Axboe5262f562019-09-17 12:26:57 -06005662 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005663 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06005664
5665 if (ctx->io_wq)
5666 io_wq_cancel_all(ctx->io_wq);
5667
Jens Axboedef596e2019-01-09 08:59:42 -07005668 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07005669 /* if we failed setting up the ctx, we might not have any rings */
5670 if (ctx->rings)
5671 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07005672 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005673 io_ring_ctx_free(ctx);
5674}
5675
5676static int io_uring_release(struct inode *inode, struct file *file)
5677{
5678 struct io_ring_ctx *ctx = file->private_data;
5679
5680 file->private_data = NULL;
5681 io_ring_ctx_wait_and_kill(ctx);
5682 return 0;
5683}
5684
Jens Axboefcb323c2019-10-24 12:39:47 -06005685static void io_uring_cancel_files(struct io_ring_ctx *ctx,
5686 struct files_struct *files)
5687{
5688 struct io_kiocb *req;
5689 DEFINE_WAIT(wait);
5690
5691 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07005692 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06005693
5694 spin_lock_irq(&ctx->inflight_lock);
5695 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07005696 if (req->work.files != files)
5697 continue;
5698 /* req is being completed, ignore */
5699 if (!refcount_inc_not_zero(&req->refs))
5700 continue;
5701 cancel_req = req;
5702 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06005703 }
Jens Axboe768134d2019-11-10 20:30:53 -07005704 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005705 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07005706 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06005707 spin_unlock_irq(&ctx->inflight_lock);
5708
Jens Axboe768134d2019-11-10 20:30:53 -07005709 /* We need to keep going until we don't find a matching req */
5710 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005711 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005712
5713 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5714 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005715 schedule();
5716 }
Jens Axboe768134d2019-11-10 20:30:53 -07005717 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005718}
5719
5720static int io_uring_flush(struct file *file, void *data)
5721{
5722 struct io_ring_ctx *ctx = file->private_data;
5723
5724 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005725 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5726 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005727 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005728 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005729 return 0;
5730}
5731
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005732static void *io_uring_validate_mmap_request(struct file *file,
5733 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005734{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005735 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005736 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005737 struct page *page;
5738 void *ptr;
5739
5740 switch (offset) {
5741 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005742 case IORING_OFF_CQ_RING:
5743 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005744 break;
5745 case IORING_OFF_SQES:
5746 ptr = ctx->sq_sqes;
5747 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005748 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005749 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005750 }
5751
5752 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005753 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005754 return ERR_PTR(-EINVAL);
5755
5756 return ptr;
5757}
5758
5759#ifdef CONFIG_MMU
5760
5761static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5762{
5763 size_t sz = vma->vm_end - vma->vm_start;
5764 unsigned long pfn;
5765 void *ptr;
5766
5767 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5768 if (IS_ERR(ptr))
5769 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005770
5771 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5772 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5773}
5774
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005775#else /* !CONFIG_MMU */
5776
5777static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5778{
5779 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5780}
5781
5782static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5783{
5784 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5785}
5786
5787static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5788 unsigned long addr, unsigned long len,
5789 unsigned long pgoff, unsigned long flags)
5790{
5791 void *ptr;
5792
5793 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5794 if (IS_ERR(ptr))
5795 return PTR_ERR(ptr);
5796
5797 return (unsigned long) ptr;
5798}
5799
5800#endif /* !CONFIG_MMU */
5801
Jens Axboe2b188cc2019-01-07 10:46:33 -07005802SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5803 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5804 size_t, sigsz)
5805{
5806 struct io_ring_ctx *ctx;
5807 long ret = -EBADF;
5808 int submitted = 0;
5809 struct fd f;
5810
Jens Axboe6c271ce2019-01-10 11:22:30 -07005811 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005812 return -EINVAL;
5813
5814 f = fdget(fd);
5815 if (!f.file)
5816 return -EBADF;
5817
5818 ret = -EOPNOTSUPP;
5819 if (f.file->f_op != &io_uring_fops)
5820 goto out_fput;
5821
5822 ret = -ENXIO;
5823 ctx = f.file->private_data;
5824 if (!percpu_ref_tryget(&ctx->refs))
5825 goto out_fput;
5826
Jens Axboe6c271ce2019-01-10 11:22:30 -07005827 /*
5828 * For SQ polling, the thread will do all submissions and completions.
5829 * Just return the requested submit count, and wake the thread if
5830 * we were asked to.
5831 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005832 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005833 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005834 if (!list_empty_careful(&ctx->cq_overflow_list))
5835 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005836 if (flags & IORING_ENTER_SQ_WAKEUP)
5837 wake_up(&ctx->sqo_wait);
5838 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005839 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005840 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005841
Jens Axboe44d28272020-01-16 19:00:24 -07005842 if (current->mm != ctx->sqo_mm ||
5843 current_cred() != ctx->creds) {
5844 ret = -EPERM;
5845 goto out;
5846 }
5847
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005848 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005849 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005850 /* already have mm, so io_submit_sqes() won't try to grab it */
5851 cur_mm = ctx->sqo_mm;
5852 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
5853 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005854 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005855
5856 if (submitted != to_submit)
5857 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005858 }
5859 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07005860 unsigned nr_events = 0;
5861
Jens Axboe2b188cc2019-01-07 10:46:33 -07005862 min_complete = min(min_complete, ctx->cq_entries);
5863
Jens Axboedef596e2019-01-09 08:59:42 -07005864 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07005865 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07005866 } else {
5867 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
5868 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005869 }
5870
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005871out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03005872 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005873out_fput:
5874 fdput(f);
5875 return submitted ? submitted : ret;
5876}
5877
5878static const struct file_operations io_uring_fops = {
5879 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06005880 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07005881 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005882#ifndef CONFIG_MMU
5883 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
5884 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
5885#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07005886 .poll = io_uring_poll,
5887 .fasync = io_uring_fasync,
5888};
5889
5890static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
5891 struct io_uring_params *p)
5892{
Hristo Venev75b28af2019-08-26 17:23:46 +00005893 struct io_rings *rings;
5894 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005895
Hristo Venev75b28af2019-08-26 17:23:46 +00005896 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
5897 if (size == SIZE_MAX)
5898 return -EOVERFLOW;
5899
5900 rings = io_mem_alloc(size);
5901 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005902 return -ENOMEM;
5903
Hristo Venev75b28af2019-08-26 17:23:46 +00005904 ctx->rings = rings;
5905 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
5906 rings->sq_ring_mask = p->sq_entries - 1;
5907 rings->cq_ring_mask = p->cq_entries - 1;
5908 rings->sq_ring_entries = p->sq_entries;
5909 rings->cq_ring_entries = p->cq_entries;
5910 ctx->sq_mask = rings->sq_ring_mask;
5911 ctx->cq_mask = rings->cq_ring_mask;
5912 ctx->sq_entries = rings->sq_ring_entries;
5913 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005914
5915 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07005916 if (size == SIZE_MAX) {
5917 io_mem_free(ctx->rings);
5918 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005919 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07005920 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005921
5922 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07005923 if (!ctx->sq_sqes) {
5924 io_mem_free(ctx->rings);
5925 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005926 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07005927 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005928
Jens Axboe2b188cc2019-01-07 10:46:33 -07005929 return 0;
5930}
5931
5932/*
5933 * Allocate an anonymous fd, this is what constitutes the application
5934 * visible backing of an io_uring instance. The application mmaps this
5935 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
5936 * we have to tie this fd to a socket for file garbage collection purposes.
5937 */
5938static int io_uring_get_fd(struct io_ring_ctx *ctx)
5939{
5940 struct file *file;
5941 int ret;
5942
5943#if defined(CONFIG_UNIX)
5944 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
5945 &ctx->ring_sock);
5946 if (ret)
5947 return ret;
5948#endif
5949
5950 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
5951 if (ret < 0)
5952 goto err;
5953
5954 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
5955 O_RDWR | O_CLOEXEC);
5956 if (IS_ERR(file)) {
5957 put_unused_fd(ret);
5958 ret = PTR_ERR(file);
5959 goto err;
5960 }
5961
5962#if defined(CONFIG_UNIX)
5963 ctx->ring_sock->file = file;
5964#endif
5965 fd_install(ret, file);
5966 return ret;
5967err:
5968#if defined(CONFIG_UNIX)
5969 sock_release(ctx->ring_sock);
5970 ctx->ring_sock = NULL;
5971#endif
5972 return ret;
5973}
5974
5975static int io_uring_create(unsigned entries, struct io_uring_params *p)
5976{
5977 struct user_struct *user = NULL;
5978 struct io_ring_ctx *ctx;
5979 bool account_mem;
5980 int ret;
5981
5982 if (!entries || entries > IORING_MAX_ENTRIES)
5983 return -EINVAL;
5984
5985 /*
5986 * Use twice as many entries for the CQ ring. It's possible for the
5987 * application to drive a higher depth than the size of the SQ ring,
5988 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005989 * some flexibility in overcommitting a bit. If the application has
5990 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5991 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005992 */
5993 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005994 if (p->flags & IORING_SETUP_CQSIZE) {
5995 /*
5996 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5997 * to a power-of-two, if it isn't already. We do NOT impose
5998 * any cq vs sq ring sizing.
5999 */
6000 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
6001 return -EINVAL;
6002 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6003 } else {
6004 p->cq_entries = 2 * p->sq_entries;
6005 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006006
6007 user = get_uid(current_user());
6008 account_mem = !capable(CAP_IPC_LOCK);
6009
6010 if (account_mem) {
6011 ret = io_account_mem(user,
6012 ring_pages(p->sq_entries, p->cq_entries));
6013 if (ret) {
6014 free_uid(user);
6015 return ret;
6016 }
6017 }
6018
6019 ctx = io_ring_ctx_alloc(p);
6020 if (!ctx) {
6021 if (account_mem)
6022 io_unaccount_mem(user, ring_pages(p->sq_entries,
6023 p->cq_entries));
6024 free_uid(user);
6025 return -ENOMEM;
6026 }
6027 ctx->compat = in_compat_syscall();
6028 ctx->account_mem = account_mem;
6029 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006030 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006031
6032 ret = io_allocate_scq_urings(ctx, p);
6033 if (ret)
6034 goto err;
6035
Jens Axboe6c271ce2019-01-10 11:22:30 -07006036 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006037 if (ret)
6038 goto err;
6039
Jens Axboe2b188cc2019-01-07 10:46:33 -07006040 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006041 p->sq_off.head = offsetof(struct io_rings, sq.head);
6042 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6043 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6044 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6045 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6046 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6047 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006048
6049 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006050 p->cq_off.head = offsetof(struct io_rings, cq.head);
6051 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6052 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6053 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6054 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6055 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006056
Jens Axboe044c1ab2019-10-28 09:15:33 -06006057 /*
6058 * Install ring fd as the very last thing, so we don't risk someone
6059 * having closed it before we finish setup
6060 */
6061 ret = io_uring_get_fd(ctx);
6062 if (ret < 0)
6063 goto err;
6064
Jens Axboeda8c9692019-12-02 18:51:26 -07006065 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
6066 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006067 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006068 return ret;
6069err:
6070 io_ring_ctx_wait_and_kill(ctx);
6071 return ret;
6072}
6073
6074/*
6075 * Sets up an aio uring context, and returns the fd. Applications asks for a
6076 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6077 * params structure passed in.
6078 */
6079static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6080{
6081 struct io_uring_params p;
6082 long ret;
6083 int i;
6084
6085 if (copy_from_user(&p, params, sizeof(p)))
6086 return -EFAULT;
6087 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6088 if (p.resv[i])
6089 return -EINVAL;
6090 }
6091
Jens Axboe6c271ce2019-01-10 11:22:30 -07006092 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06006093 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006094 return -EINVAL;
6095
6096 ret = io_uring_create(entries, &p);
6097 if (ret < 0)
6098 return ret;
6099
6100 if (copy_to_user(params, &p, sizeof(p)))
6101 return -EFAULT;
6102
6103 return ret;
6104}
6105
6106SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6107 struct io_uring_params __user *, params)
6108{
6109 return io_uring_setup(entries, params);
6110}
6111
Jens Axboeedafcce2019-01-09 09:16:05 -07006112static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6113 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006114 __releases(ctx->uring_lock)
6115 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006116{
6117 int ret;
6118
Jens Axboe35fa71a2019-04-22 10:23:23 -06006119 /*
6120 * We're inside the ring mutex, if the ref is already dying, then
6121 * someone else killed the ctx or is already going through
6122 * io_uring_register().
6123 */
6124 if (percpu_ref_is_dying(&ctx->refs))
6125 return -ENXIO;
6126
Jens Axboe05f3fb32019-12-09 11:22:50 -07006127 if (opcode != IORING_UNREGISTER_FILES &&
6128 opcode != IORING_REGISTER_FILES_UPDATE) {
6129 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006130
Jens Axboe05f3fb32019-12-09 11:22:50 -07006131 /*
6132 * Drop uring mutex before waiting for references to exit. If
6133 * another thread is currently inside io_uring_enter() it might
6134 * need to grab the uring_lock to make progress. If we hold it
6135 * here across the drain wait, then we can deadlock. It's safe
6136 * to drop the mutex here, since no new references will come in
6137 * after we've killed the percpu ref.
6138 */
6139 mutex_unlock(&ctx->uring_lock);
6140 wait_for_completion(&ctx->completions[0]);
6141 mutex_lock(&ctx->uring_lock);
6142 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006143
6144 switch (opcode) {
6145 case IORING_REGISTER_BUFFERS:
6146 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6147 break;
6148 case IORING_UNREGISTER_BUFFERS:
6149 ret = -EINVAL;
6150 if (arg || nr_args)
6151 break;
6152 ret = io_sqe_buffer_unregister(ctx);
6153 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006154 case IORING_REGISTER_FILES:
6155 ret = io_sqe_files_register(ctx, arg, nr_args);
6156 break;
6157 case IORING_UNREGISTER_FILES:
6158 ret = -EINVAL;
6159 if (arg || nr_args)
6160 break;
6161 ret = io_sqe_files_unregister(ctx);
6162 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006163 case IORING_REGISTER_FILES_UPDATE:
6164 ret = io_sqe_files_update(ctx, arg, nr_args);
6165 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006166 case IORING_REGISTER_EVENTFD:
6167 ret = -EINVAL;
6168 if (nr_args != 1)
6169 break;
6170 ret = io_eventfd_register(ctx, arg);
6171 break;
6172 case IORING_UNREGISTER_EVENTFD:
6173 ret = -EINVAL;
6174 if (arg || nr_args)
6175 break;
6176 ret = io_eventfd_unregister(ctx);
6177 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006178 default:
6179 ret = -EINVAL;
6180 break;
6181 }
6182
Jens Axboe05f3fb32019-12-09 11:22:50 -07006183
6184 if (opcode != IORING_UNREGISTER_FILES &&
6185 opcode != IORING_REGISTER_FILES_UPDATE) {
6186 /* bring the ctx back to life */
6187 reinit_completion(&ctx->completions[0]);
6188 percpu_ref_reinit(&ctx->refs);
6189 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006190 return ret;
6191}
6192
6193SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6194 void __user *, arg, unsigned int, nr_args)
6195{
6196 struct io_ring_ctx *ctx;
6197 long ret = -EBADF;
6198 struct fd f;
6199
6200 f = fdget(fd);
6201 if (!f.file)
6202 return -EBADF;
6203
6204 ret = -EOPNOTSUPP;
6205 if (f.file->f_op != &io_uring_fops)
6206 goto out_fput;
6207
6208 ctx = f.file->private_data;
6209
6210 mutex_lock(&ctx->uring_lock);
6211 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6212 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006213 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6214 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006215out_fput:
6216 fdput(f);
6217 return ret;
6218}
6219
Jens Axboe2b188cc2019-01-07 10:46:33 -07006220static int __init io_uring_init(void)
6221{
Jens Axboed3656342019-12-18 09:50:26 -07006222 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006223 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6224 return 0;
6225};
6226__initcall(io_uring_init);