blob: 115c7d41337289004a3a6c25aa83a5537903f2ed [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070059#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070073#include <linux/namei.h>
74#include <linux/fsnotify.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070075
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020076#define CREATE_TRACE_POINTS
77#include <trace/events/io_uring.h>
78
Jens Axboe2b188cc2019-01-07 10:46:33 -070079#include <uapi/linux/io_uring.h>
80
81#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060082#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070083
Daniel Xu5277dea2019-09-14 14:23:45 -070084#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060085#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060086
87/*
88 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
89 */
90#define IORING_FILE_TABLE_SHIFT 9
91#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
92#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
93#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070094
95struct io_uring {
96 u32 head ____cacheline_aligned_in_smp;
97 u32 tail ____cacheline_aligned_in_smp;
98};
99
Stefan Bühler1e84b972019-04-24 23:54:16 +0200100/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000101 * This data is shared with the application through the mmap at offsets
102 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200103 *
104 * The offsets to the member fields are published through struct
105 * io_sqring_offsets when calling io_uring_setup.
106 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000107struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200108 /*
109 * Head and tail offsets into the ring; the offsets need to be
110 * masked to get valid indices.
111 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000112 * The kernel controls head of the sq ring and the tail of the cq ring,
113 * and the application controls tail of the sq ring and the head of the
114 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200117 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 * ring_entries - 1)
120 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000121 u32 sq_ring_mask, cq_ring_mask;
122 /* Ring sizes (constant, power of 2) */
123 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200124 /*
125 * Number of invalid entries dropped by the kernel due to
126 * invalid index stored in array
127 *
128 * Written by the kernel, shouldn't be modified by the
129 * application (i.e. get number of "new events" by comparing to
130 * cached value).
131 *
132 * After a new SQ head value was read by the application this
133 * counter includes all submissions that were dropped reaching
134 * the new SQ head (and possibly more).
135 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000136 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200137 /*
138 * Runtime flags
139 *
140 * Written by the kernel, shouldn't be modified by the
141 * application.
142 *
143 * The application needs a full memory barrier before checking
144 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200147 /*
148 * Number of completion events lost because the queue was full;
149 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800150 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 * the completion queue.
152 *
153 * Written by the kernel, shouldn't be modified by the
154 * application (i.e. get number of "new events" by comparing to
155 * cached value).
156 *
157 * As completion events come in out of order this counter is not
158 * ordered with any other data.
159 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000160 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200161 /*
162 * Ring buffer of completion events.
163 *
164 * The kernel writes completion events fresh every time they are
165 * produced, so the application is allowed to modify pending
166 * entries.
167 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000168 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700169};
170
Jens Axboeedafcce2019-01-09 09:16:05 -0700171struct io_mapped_ubuf {
172 u64 ubuf;
173 size_t len;
174 struct bio_vec *bvec;
175 unsigned int nr_bvecs;
176};
177
Jens Axboe65e19f52019-10-26 07:20:21 -0600178struct fixed_file_table {
179 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700180};
181
Jens Axboe05f3fb32019-12-09 11:22:50 -0700182enum {
183 FFD_F_ATOMIC,
184};
185
186struct fixed_file_data {
187 struct fixed_file_table *table;
188 struct io_ring_ctx *ctx;
189
190 struct percpu_ref refs;
191 struct llist_head put_llist;
192 unsigned long state;
193 struct work_struct ref_work;
194 struct completion done;
195};
196
Jens Axboe2b188cc2019-01-07 10:46:33 -0700197struct io_ring_ctx {
198 struct {
199 struct percpu_ref refs;
200 } ____cacheline_aligned_in_smp;
201
202 struct {
203 unsigned int flags;
204 bool compat;
205 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700206 bool cq_overflow_flushed;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300207 bool drain_next;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208
Hristo Venev75b28af2019-08-26 17:23:46 +0000209 /*
210 * Ring buffer of indices into array of io_uring_sqe, which is
211 * mmapped by the application using the IORING_OFF_SQES offset.
212 *
213 * This indirection could e.g. be used to assign fixed
214 * io_uring_sqe entries to operations and only submit them to
215 * the queue when needed.
216 *
217 * The kernel modifies neither the indices array nor the entries
218 * array.
219 */
220 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 unsigned cached_sq_head;
222 unsigned sq_entries;
223 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700224 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600225 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700226 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700227 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600228
229 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600230 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700231 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700232
Jens Axboefcb323c2019-10-24 12:39:47 -0600233 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234 } ____cacheline_aligned_in_smp;
235
Hristo Venev75b28af2019-08-26 17:23:46 +0000236 struct io_rings *rings;
237
Jens Axboe2b188cc2019-01-07 10:46:33 -0700238 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600239 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700240 struct task_struct *sqo_thread; /* if using sq thread polling */
241 struct mm_struct *sqo_mm;
242 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700243
Jens Axboe6b063142019-01-10 22:13:58 -0700244 /*
245 * If used, fixed file set. Writers must ensure that ->refs is dead,
246 * readers must ensure that ->refs is alive as long as the file* is
247 * used. Only updated through io_uring_register(2).
248 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700249 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700250 unsigned nr_user_files;
251
Jens Axboeedafcce2019-01-09 09:16:05 -0700252 /* if used, fixed mapped user buffers */
253 unsigned nr_user_bufs;
254 struct io_mapped_ubuf *user_bufs;
255
Jens Axboe2b188cc2019-01-07 10:46:33 -0700256 struct user_struct *user;
257
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700258 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700259
Jens Axboe206aefd2019-11-07 18:27:42 -0700260 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
261 struct completion *completions;
262
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700263 /* if all else fails... */
264 struct io_kiocb *fallback_req;
265
Jens Axboe206aefd2019-11-07 18:27:42 -0700266#if defined(CONFIG_UNIX)
267 struct socket *ring_sock;
268#endif
269
270 struct {
271 unsigned cached_cq_tail;
272 unsigned cq_entries;
273 unsigned cq_mask;
274 atomic_t cq_timeouts;
275 struct wait_queue_head cq_wait;
276 struct fasync_struct *cq_fasync;
277 struct eventfd_ctx *cq_ev_fd;
278 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279
280 struct {
281 struct mutex uring_lock;
282 wait_queue_head_t wait;
283 } ____cacheline_aligned_in_smp;
284
285 struct {
286 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700287 bool poll_multi_file;
288 /*
289 * ->poll_list is protected by the ctx->uring_lock for
290 * io_uring instances that don't use IORING_SETUP_SQPOLL.
291 * For SQPOLL, only the single threaded io_sq_thread() will
292 * manipulate the list, hence no extra locking is needed there.
293 */
294 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700295 struct hlist_head *cancel_hash;
296 unsigned cancel_hash_bits;
Jens Axboefcb323c2019-10-24 12:39:47 -0600297
298 spinlock_t inflight_lock;
299 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700300 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700301};
302
Jens Axboe09bb8392019-03-13 12:39:28 -0600303/*
304 * First field must be the file pointer in all the
305 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
306 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700307struct io_poll_iocb {
308 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700309 union {
310 struct wait_queue_head *head;
311 u64 addr;
312 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700313 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600314 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700315 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700316 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700317};
318
Jens Axboeb5dba592019-12-11 14:02:38 -0700319struct io_close {
320 struct file *file;
321 struct file *put_file;
322 int fd;
323};
324
Jens Axboead8a48a2019-11-15 08:49:11 -0700325struct io_timeout_data {
326 struct io_kiocb *req;
327 struct hrtimer timer;
328 struct timespec64 ts;
329 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300330 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700331};
332
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700333struct io_accept {
334 struct file *file;
335 struct sockaddr __user *addr;
336 int __user *addr_len;
337 int flags;
338};
339
340struct io_sync {
341 struct file *file;
342 loff_t len;
343 loff_t off;
344 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700345 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700346};
347
Jens Axboefbf23842019-12-17 18:45:56 -0700348struct io_cancel {
349 struct file *file;
350 u64 addr;
351};
352
Jens Axboeb29472e2019-12-17 18:50:29 -0700353struct io_timeout {
354 struct file *file;
355 u64 addr;
356 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700357 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700358};
359
Jens Axboe9adbd452019-12-20 08:45:55 -0700360struct io_rw {
361 /* NOTE: kiocb has the file as the first member, so don't do it here */
362 struct kiocb kiocb;
363 u64 addr;
364 u64 len;
365};
366
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700367struct io_connect {
368 struct file *file;
369 struct sockaddr __user *addr;
370 int addr_len;
371};
372
Jens Axboee47293f2019-12-20 08:58:21 -0700373struct io_sr_msg {
374 struct file *file;
375 struct user_msghdr __user *msg;
376 int msg_flags;
377};
378
Jens Axboe15b71ab2019-12-11 11:20:36 -0700379struct io_open {
380 struct file *file;
381 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700382 union {
383 umode_t mode;
384 unsigned mask;
385 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700386 const char __user *fname;
387 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700388 struct statx __user *buffer;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700389 int flags;
390};
391
Jens Axboe05f3fb32019-12-09 11:22:50 -0700392struct io_files_update {
393 struct file *file;
394 u64 arg;
395 u32 nr_args;
396 u32 offset;
397};
398
Jens Axboef499a022019-12-02 16:28:46 -0700399struct io_async_connect {
400 struct sockaddr_storage address;
401};
402
Jens Axboe03b12302019-12-02 18:50:25 -0700403struct io_async_msghdr {
404 struct iovec fast_iov[UIO_FASTIOV];
405 struct iovec *iov;
406 struct sockaddr __user *uaddr;
407 struct msghdr msg;
408};
409
Jens Axboef67676d2019-12-02 11:03:47 -0700410struct io_async_rw {
411 struct iovec fast_iov[UIO_FASTIOV];
412 struct iovec *iov;
413 ssize_t nr_segs;
414 ssize_t size;
415};
416
Jens Axboe15b71ab2019-12-11 11:20:36 -0700417struct io_async_open {
418 struct filename *filename;
419};
420
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700421struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700422 union {
423 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700424 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700425 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700426 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700427 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700428 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700429};
430
Jens Axboe09bb8392019-03-13 12:39:28 -0600431/*
432 * NOTE! Each of the iocb union members has the file pointer
433 * as the first entry in their struct definition. So you can
434 * access the file pointer through any of the sub-structs,
435 * or directly as just 'ki_filp' in this struct.
436 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700437struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700438 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600439 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700440 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700441 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700442 struct io_accept accept;
443 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700444 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700445 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700446 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700447 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700448 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700449 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700450 struct io_files_update files_update;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700451 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700452
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700453 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300454 struct file *ring_file;
455 int ring_fd;
456 bool has_user;
457 bool in_async;
458 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700459 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700460
461 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700462 union {
463 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700464 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700465 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600466 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700467 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700468 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200469#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700470#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700471#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700472#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200473#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
474#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600475#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700476#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800477#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300478#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600479#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600480#define REQ_F_ISREG 2048 /* regular file */
481#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700482#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800483#define REQ_F_INFLIGHT 16384 /* on inflight list */
484#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700485#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700486 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600487 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600488 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700489
Jens Axboefcb323c2019-10-24 12:39:47 -0600490 struct list_head inflight_entry;
491
Jens Axboe561fb042019-10-24 07:25:42 -0600492 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700493};
494
495#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700496#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700497
Jens Axboe9a56a232019-01-09 09:06:50 -0700498struct io_submit_state {
499 struct blk_plug plug;
500
501 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700502 * io_kiocb alloc cache
503 */
504 void *reqs[IO_IOPOLL_BATCH];
505 unsigned int free_reqs;
506 unsigned int cur_req;
507
508 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700509 * File reference cache
510 */
511 struct file *file;
512 unsigned int fd;
513 unsigned int has_refs;
514 unsigned int used_refs;
515 unsigned int ios_left;
516};
517
Jens Axboe561fb042019-10-24 07:25:42 -0600518static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700519static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800520static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800521static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700522static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700523static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700524static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
525static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700526static int __io_sqe_files_update(struct io_ring_ctx *ctx,
527 struct io_uring_files_update *ip,
528 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600529
Jens Axboe2b188cc2019-01-07 10:46:33 -0700530static struct kmem_cache *req_cachep;
531
532static const struct file_operations io_uring_fops;
533
534struct sock *io_uring_get_socket(struct file *file)
535{
536#if defined(CONFIG_UNIX)
537 if (file->f_op == &io_uring_fops) {
538 struct io_ring_ctx *ctx = file->private_data;
539
540 return ctx->ring_sock->sk;
541 }
542#endif
543 return NULL;
544}
545EXPORT_SYMBOL(io_uring_get_socket);
546
547static void io_ring_ctx_ref_free(struct percpu_ref *ref)
548{
549 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
550
Jens Axboe206aefd2019-11-07 18:27:42 -0700551 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700552}
553
554static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
555{
556 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700557 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700558
559 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
560 if (!ctx)
561 return NULL;
562
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700563 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
564 if (!ctx->fallback_req)
565 goto err;
566
Jens Axboe206aefd2019-11-07 18:27:42 -0700567 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
568 if (!ctx->completions)
569 goto err;
570
Jens Axboe78076bb2019-12-04 19:56:40 -0700571 /*
572 * Use 5 bits less than the max cq entries, that should give us around
573 * 32 entries per hash list if totally full and uniformly spread.
574 */
575 hash_bits = ilog2(p->cq_entries);
576 hash_bits -= 5;
577 if (hash_bits <= 0)
578 hash_bits = 1;
579 ctx->cancel_hash_bits = hash_bits;
580 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
581 GFP_KERNEL);
582 if (!ctx->cancel_hash)
583 goto err;
584 __hash_init(ctx->cancel_hash, 1U << hash_bits);
585
Roman Gushchin21482892019-05-07 10:01:48 -0700586 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700587 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
588 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700589
590 ctx->flags = p->flags;
591 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700592 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700593 init_completion(&ctx->completions[0]);
594 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700595 mutex_init(&ctx->uring_lock);
596 init_waitqueue_head(&ctx->wait);
597 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700598 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600599 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600600 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600601 init_waitqueue_head(&ctx->inflight_wait);
602 spin_lock_init(&ctx->inflight_lock);
603 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700604 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700605err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700606 if (ctx->fallback_req)
607 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700608 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700609 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700610 kfree(ctx);
611 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700612}
613
Bob Liu9d858b22019-11-13 18:06:25 +0800614static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600615{
Jackie Liua197f662019-11-08 08:09:12 -0700616 struct io_ring_ctx *ctx = req->ctx;
617
Jens Axboe498ccd92019-10-25 10:04:25 -0600618 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
619 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600620}
621
Bob Liu9d858b22019-11-13 18:06:25 +0800622static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600623{
Bob Liu9d858b22019-11-13 18:06:25 +0800624 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
625 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600626
Bob Liu9d858b22019-11-13 18:06:25 +0800627 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600628}
629
630static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600631{
632 struct io_kiocb *req;
633
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600634 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800635 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600636 list_del_init(&req->list);
637 return req;
638 }
639
640 return NULL;
641}
642
Jens Axboe5262f562019-09-17 12:26:57 -0600643static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
644{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600645 struct io_kiocb *req;
646
647 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700648 if (req) {
649 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
650 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800651 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700652 list_del_init(&req->list);
653 return req;
654 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600655 }
656
657 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600658}
659
Jens Axboede0617e2019-04-06 21:51:27 -0600660static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700661{
Hristo Venev75b28af2019-08-26 17:23:46 +0000662 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700663
Hristo Venev75b28af2019-08-26 17:23:46 +0000664 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700665 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000666 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700667
Jens Axboe2b188cc2019-01-07 10:46:33 -0700668 if (wq_has_sleeper(&ctx->cq_wait)) {
669 wake_up_interruptible(&ctx->cq_wait);
670 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
671 }
672 }
673}
674
Jens Axboed625c6e2019-12-17 19:53:05 -0700675static inline bool io_req_needs_user(struct io_kiocb *req)
Jens Axboe18d9be12019-09-10 09:13:05 -0600676{
Jens Axboed625c6e2019-12-17 19:53:05 -0700677 return !(req->opcode == IORING_OP_READ_FIXED ||
678 req->opcode == IORING_OP_WRITE_FIXED);
Jens Axboe561fb042019-10-24 07:25:42 -0600679}
680
Jens Axboe94ae5e72019-11-14 19:39:52 -0700681static inline bool io_prep_async_work(struct io_kiocb *req,
682 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600683{
684 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600685
Jens Axboe3529d8c2019-12-19 18:24:38 -0700686 switch (req->opcode) {
687 case IORING_OP_WRITEV:
688 case IORING_OP_WRITE_FIXED:
689 /* only regular files should be hashed for writes */
690 if (req->flags & REQ_F_ISREG)
691 do_hashed = true;
692 /* fall-through */
693 case IORING_OP_READV:
694 case IORING_OP_READ_FIXED:
695 case IORING_OP_SENDMSG:
696 case IORING_OP_RECVMSG:
697 case IORING_OP_ACCEPT:
698 case IORING_OP_POLL_ADD:
699 case IORING_OP_CONNECT:
700 /*
701 * We know REQ_F_ISREG is not set on some of these
702 * opcodes, but this enables us to keep the check in
703 * just one place.
704 */
705 if (!(req->flags & REQ_F_ISREG))
706 req->work.flags |= IO_WQ_WORK_UNBOUND;
707 break;
Jens Axboe54a91f32019-09-10 09:15:04 -0600708 }
Jens Axboe3529d8c2019-12-19 18:24:38 -0700709 if (io_req_needs_user(req))
710 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600711
Jens Axboe94ae5e72019-11-14 19:39:52 -0700712 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600713 return do_hashed;
714}
715
Jackie Liua197f662019-11-08 08:09:12 -0700716static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600717{
Jackie Liua197f662019-11-08 08:09:12 -0700718 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700719 struct io_kiocb *link;
720 bool do_hashed;
721
722 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600723
724 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
725 req->flags);
726 if (!do_hashed) {
727 io_wq_enqueue(ctx->io_wq, &req->work);
728 } else {
729 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
730 file_inode(req->file));
731 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700732
733 if (link)
734 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600735}
736
Jens Axboe5262f562019-09-17 12:26:57 -0600737static void io_kill_timeout(struct io_kiocb *req)
738{
739 int ret;
740
Jens Axboe2d283902019-12-04 11:08:05 -0700741 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600742 if (ret != -1) {
743 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600744 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700745 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800746 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600747 }
748}
749
750static void io_kill_timeouts(struct io_ring_ctx *ctx)
751{
752 struct io_kiocb *req, *tmp;
753
754 spin_lock_irq(&ctx->completion_lock);
755 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
756 io_kill_timeout(req);
757 spin_unlock_irq(&ctx->completion_lock);
758}
759
Jens Axboede0617e2019-04-06 21:51:27 -0600760static void io_commit_cqring(struct io_ring_ctx *ctx)
761{
762 struct io_kiocb *req;
763
Jens Axboe5262f562019-09-17 12:26:57 -0600764 while ((req = io_get_timeout_req(ctx)) != NULL)
765 io_kill_timeout(req);
766
Jens Axboede0617e2019-04-06 21:51:27 -0600767 __io_commit_cqring(ctx);
768
769 while ((req = io_get_deferred_req(ctx)) != NULL) {
770 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700771 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600772 }
773}
774
Jens Axboe2b188cc2019-01-07 10:46:33 -0700775static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
776{
Hristo Venev75b28af2019-08-26 17:23:46 +0000777 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700778 unsigned tail;
779
780 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200781 /*
782 * writes to the cq entry need to come after reading head; the
783 * control dependency is enough as we're using WRITE_ONCE to
784 * fill the cq entry
785 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000786 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700787 return NULL;
788
789 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000790 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700791}
792
Jens Axboe8c838782019-03-12 15:48:16 -0600793static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
794{
795 if (waitqueue_active(&ctx->wait))
796 wake_up(&ctx->wait);
797 if (waitqueue_active(&ctx->sqo_wait))
798 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600799 if (ctx->cq_ev_fd)
800 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600801}
802
Jens Axboec4a2ed72019-11-21 21:01:26 -0700803/* Returns true if there are no backlogged entries after the flush */
804static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700805{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700806 struct io_rings *rings = ctx->rings;
807 struct io_uring_cqe *cqe;
808 struct io_kiocb *req;
809 unsigned long flags;
810 LIST_HEAD(list);
811
812 if (!force) {
813 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700814 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700815 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
816 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700817 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700818 }
819
820 spin_lock_irqsave(&ctx->completion_lock, flags);
821
822 /* if force is set, the ring is going away. always drop after that */
823 if (force)
824 ctx->cq_overflow_flushed = true;
825
Jens Axboec4a2ed72019-11-21 21:01:26 -0700826 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700827 while (!list_empty(&ctx->cq_overflow_list)) {
828 cqe = io_get_cqring(ctx);
829 if (!cqe && !force)
830 break;
831
832 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
833 list);
834 list_move(&req->list, &list);
835 if (cqe) {
836 WRITE_ONCE(cqe->user_data, req->user_data);
837 WRITE_ONCE(cqe->res, req->result);
838 WRITE_ONCE(cqe->flags, 0);
839 } else {
840 WRITE_ONCE(ctx->rings->cq_overflow,
841 atomic_inc_return(&ctx->cached_cq_overflow));
842 }
843 }
844
845 io_commit_cqring(ctx);
846 spin_unlock_irqrestore(&ctx->completion_lock, flags);
847 io_cqring_ev_posted(ctx);
848
849 while (!list_empty(&list)) {
850 req = list_first_entry(&list, struct io_kiocb, list);
851 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800852 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700853 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700854
855 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700856}
857
Jens Axboe78e19bb2019-11-06 15:21:34 -0700858static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700859{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700860 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700861 struct io_uring_cqe *cqe;
862
Jens Axboe78e19bb2019-11-06 15:21:34 -0700863 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700864
Jens Axboe2b188cc2019-01-07 10:46:33 -0700865 /*
866 * If we can't get a cq entry, userspace overflowed the
867 * submission (by quite a lot). Increment the overflow count in
868 * the ring.
869 */
870 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700871 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700872 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700873 WRITE_ONCE(cqe->res, res);
874 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700875 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700876 WRITE_ONCE(ctx->rings->cq_overflow,
877 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700878 } else {
879 refcount_inc(&req->refs);
880 req->result = res;
881 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700882 }
883}
884
Jens Axboe78e19bb2019-11-06 15:21:34 -0700885static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700886{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700887 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700888 unsigned long flags;
889
890 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700891 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700892 io_commit_cqring(ctx);
893 spin_unlock_irqrestore(&ctx->completion_lock, flags);
894
Jens Axboe8c838782019-03-12 15:48:16 -0600895 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700896}
897
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700898static inline bool io_is_fallback_req(struct io_kiocb *req)
899{
900 return req == (struct io_kiocb *)
901 ((unsigned long) req->ctx->fallback_req & ~1UL);
902}
903
904static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
905{
906 struct io_kiocb *req;
907
908 req = ctx->fallback_req;
909 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
910 return req;
911
912 return NULL;
913}
914
Jens Axboe2579f912019-01-09 09:10:43 -0700915static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
916 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700917{
Jens Axboefd6fab22019-03-14 16:30:06 -0600918 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700919 struct io_kiocb *req;
920
921 if (!percpu_ref_tryget(&ctx->refs))
922 return NULL;
923
Jens Axboe2579f912019-01-09 09:10:43 -0700924 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600925 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700926 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700927 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700928 } else if (!state->free_reqs) {
929 size_t sz;
930 int ret;
931
932 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600933 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
934
935 /*
936 * Bulk alloc is all-or-nothing. If we fail to get a batch,
937 * retry single alloc to be on the safe side.
938 */
939 if (unlikely(ret <= 0)) {
940 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
941 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700942 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600943 ret = 1;
944 }
Jens Axboe2579f912019-01-09 09:10:43 -0700945 state->free_reqs = ret - 1;
946 state->cur_req = 1;
947 req = state->reqs[0];
948 } else {
949 req = state->reqs[state->cur_req];
950 state->free_reqs--;
951 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700952 }
953
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700954got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700955 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300956 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600957 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700958 req->ctx = ctx;
959 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600960 /* one is dropped after submission, the other at completion */
961 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600962 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600963 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700964 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700965fallback:
966 req = io_get_fallback_req(ctx);
967 if (req)
968 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300969 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700970 return NULL;
971}
972
Jens Axboedef596e2019-01-09 08:59:42 -0700973static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
974{
975 if (*nr) {
976 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300977 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700978 percpu_ref_put_many(&ctx->file_data->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700979 *nr = 0;
980 }
981}
982
Jens Axboe9e645e112019-05-10 16:07:28 -0600983static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700984{
Jens Axboefcb323c2019-10-24 12:39:47 -0600985 struct io_ring_ctx *ctx = req->ctx;
986
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700987 if (req->io)
988 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700989 if (req->file) {
990 if (req->flags & REQ_F_FIXED_FILE)
991 percpu_ref_put(&ctx->file_data->refs);
992 else
993 fput(req->file);
994 }
Jens Axboefcb323c2019-10-24 12:39:47 -0600995 if (req->flags & REQ_F_INFLIGHT) {
996 unsigned long flags;
997
998 spin_lock_irqsave(&ctx->inflight_lock, flags);
999 list_del(&req->inflight_entry);
1000 if (waitqueue_active(&ctx->inflight_wait))
1001 wake_up(&ctx->inflight_wait);
1002 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1003 }
1004 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001005 if (likely(!io_is_fallback_req(req)))
1006 kmem_cache_free(req_cachep, req);
1007 else
1008 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001009}
1010
Jackie Liua197f662019-11-08 08:09:12 -07001011static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001012{
Jackie Liua197f662019-11-08 08:09:12 -07001013 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001014 int ret;
1015
Jens Axboe2d283902019-12-04 11:08:05 -07001016 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001017 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001018 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001019 io_commit_cqring(ctx);
1020 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001021 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001022 return true;
1023 }
1024
1025 return false;
1026}
1027
Jens Axboeba816ad2019-09-28 11:36:45 -06001028static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001029{
Jens Axboe2665abf2019-11-05 12:40:47 -07001030 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001031 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001032
Jens Axboe4d7dd462019-11-20 13:03:52 -07001033 /* Already got next link */
1034 if (req->flags & REQ_F_LINK_NEXT)
1035 return;
1036
Jens Axboe9e645e112019-05-10 16:07:28 -06001037 /*
1038 * The list should never be empty when we are called here. But could
1039 * potentially happen if the chain is messed up, check to be on the
1040 * safe side.
1041 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001042 while (!list_empty(&req->link_list)) {
1043 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1044 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001045
Pavel Begunkov44932332019-12-05 16:16:35 +03001046 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1047 (nxt->flags & REQ_F_TIMEOUT))) {
1048 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001049 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001050 req->flags &= ~REQ_F_LINK_TIMEOUT;
1051 continue;
1052 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001053
Pavel Begunkov44932332019-12-05 16:16:35 +03001054 list_del_init(&req->link_list);
1055 if (!list_empty(&nxt->link_list))
1056 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001057 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001058 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001059 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001060
Jens Axboe4d7dd462019-11-20 13:03:52 -07001061 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001062 if (wake_ev)
1063 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001064}
1065
1066/*
1067 * Called if REQ_F_LINK is set, and we fail the head request
1068 */
1069static void io_fail_links(struct io_kiocb *req)
1070{
Jens Axboe2665abf2019-11-05 12:40:47 -07001071 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001072 unsigned long flags;
1073
1074 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001075
1076 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001077 struct io_kiocb *link = list_first_entry(&req->link_list,
1078 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001079
Pavel Begunkov44932332019-12-05 16:16:35 +03001080 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001081 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001082
1083 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001084 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001085 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001086 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001087 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001088 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001089 }
Jens Axboe5d960722019-11-19 15:31:28 -07001090 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001091 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001092
1093 io_commit_cqring(ctx);
1094 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1095 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001096}
1097
Jens Axboe4d7dd462019-11-20 13:03:52 -07001098static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001099{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001100 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001101 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001102
Jens Axboe9e645e112019-05-10 16:07:28 -06001103 /*
1104 * If LINK is set, we have dependent requests in this chain. If we
1105 * didn't fail this request, queue the first one up, moving any other
1106 * dependencies to the next request. In case of failure, fail the rest
1107 * of the chain.
1108 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001109 if (req->flags & REQ_F_FAIL_LINK) {
1110 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001111 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1112 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001113 struct io_ring_ctx *ctx = req->ctx;
1114 unsigned long flags;
1115
1116 /*
1117 * If this is a timeout link, we could be racing with the
1118 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001119 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001120 */
1121 spin_lock_irqsave(&ctx->completion_lock, flags);
1122 io_req_link_next(req, nxt);
1123 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1124 } else {
1125 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001126 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001127}
Jens Axboe9e645e112019-05-10 16:07:28 -06001128
Jackie Liuc69f8db2019-11-09 11:00:08 +08001129static void io_free_req(struct io_kiocb *req)
1130{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001131 struct io_kiocb *nxt = NULL;
1132
1133 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001134 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001135
1136 if (nxt)
1137 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001138}
1139
Jens Axboeba816ad2019-09-28 11:36:45 -06001140/*
1141 * Drop reference to request, return next in chain (if there is one) if this
1142 * was the last reference to this request.
1143 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001144__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001145static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001146{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001147 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001148
Jens Axboee65ef562019-03-12 10:16:44 -06001149 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001150 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001151}
1152
Jens Axboe2b188cc2019-01-07 10:46:33 -07001153static void io_put_req(struct io_kiocb *req)
1154{
Jens Axboedef596e2019-01-09 08:59:42 -07001155 if (refcount_dec_and_test(&req->refs))
1156 io_free_req(req);
1157}
1158
Jens Axboe978db572019-11-14 22:39:04 -07001159/*
1160 * Must only be used if we don't need to care about links, usually from
1161 * within the completion handling itself.
1162 */
1163static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001164{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001165 /* drop both submit and complete references */
1166 if (refcount_sub_and_test(2, &req->refs))
1167 __io_free_req(req);
1168}
1169
Jens Axboe978db572019-11-14 22:39:04 -07001170static void io_double_put_req(struct io_kiocb *req)
1171{
1172 /* drop both submit and complete references */
1173 if (refcount_sub_and_test(2, &req->refs))
1174 io_free_req(req);
1175}
1176
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001177static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001178{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001179 struct io_rings *rings = ctx->rings;
1180
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001181 /*
1182 * noflush == true is from the waitqueue handler, just ensure we wake
1183 * up the task, and the next invocation will flush the entries. We
1184 * cannot safely to it from here.
1185 */
1186 if (noflush && !list_empty(&ctx->cq_overflow_list))
1187 return -1U;
1188
1189 io_cqring_overflow_flush(ctx, false);
1190
Jens Axboea3a0e432019-08-20 11:03:11 -06001191 /* See comment at the top of this file */
1192 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001193 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001194}
1195
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001196static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1197{
1198 struct io_rings *rings = ctx->rings;
1199
1200 /* make sure SQ entry isn't read before tail */
1201 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1202}
1203
Jens Axboedef596e2019-01-09 08:59:42 -07001204/*
1205 * Find and free completed poll iocbs
1206 */
1207static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1208 struct list_head *done)
1209{
1210 void *reqs[IO_IOPOLL_BATCH];
1211 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001212 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001213
Jens Axboe09bb8392019-03-13 12:39:28 -06001214 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001215 while (!list_empty(done)) {
1216 req = list_first_entry(done, struct io_kiocb, list);
1217 list_del(&req->list);
1218
Jens Axboe78e19bb2019-11-06 15:21:34 -07001219 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001220 (*nr_events)++;
1221
Jens Axboe09bb8392019-03-13 12:39:28 -06001222 if (refcount_dec_and_test(&req->refs)) {
1223 /* If we're not using fixed files, we have to pair the
1224 * completion part with the file put. Use regular
1225 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001226 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001227 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001228 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1229 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1230 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001231 reqs[to_free++] = req;
1232 if (to_free == ARRAY_SIZE(reqs))
1233 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001234 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001235 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001236 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001237 }
Jens Axboedef596e2019-01-09 08:59:42 -07001238 }
Jens Axboedef596e2019-01-09 08:59:42 -07001239
Jens Axboe09bb8392019-03-13 12:39:28 -06001240 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001241 io_free_req_many(ctx, reqs, &to_free);
1242}
1243
1244static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1245 long min)
1246{
1247 struct io_kiocb *req, *tmp;
1248 LIST_HEAD(done);
1249 bool spin;
1250 int ret;
1251
1252 /*
1253 * Only spin for completions if we don't have multiple devices hanging
1254 * off our complete list, and we're under the requested amount.
1255 */
1256 spin = !ctx->poll_multi_file && *nr_events < min;
1257
1258 ret = 0;
1259 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001260 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001261
1262 /*
1263 * Move completed entries to our local list. If we find a
1264 * request that requires polling, break out and complete
1265 * the done list first, if we have entries there.
1266 */
1267 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1268 list_move_tail(&req->list, &done);
1269 continue;
1270 }
1271 if (!list_empty(&done))
1272 break;
1273
1274 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1275 if (ret < 0)
1276 break;
1277
1278 if (ret && spin)
1279 spin = false;
1280 ret = 0;
1281 }
1282
1283 if (!list_empty(&done))
1284 io_iopoll_complete(ctx, nr_events, &done);
1285
1286 return ret;
1287}
1288
1289/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001290 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001291 * non-spinning poll check - we'll still enter the driver poll loop, but only
1292 * as a non-spinning completion check.
1293 */
1294static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1295 long min)
1296{
Jens Axboe08f54392019-08-21 22:19:11 -06001297 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001298 int ret;
1299
1300 ret = io_do_iopoll(ctx, nr_events, min);
1301 if (ret < 0)
1302 return ret;
1303 if (!min || *nr_events >= min)
1304 return 0;
1305 }
1306
1307 return 1;
1308}
1309
1310/*
1311 * We can't just wait for polled events to come to us, we have to actively
1312 * find and complete them.
1313 */
1314static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1315{
1316 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1317 return;
1318
1319 mutex_lock(&ctx->uring_lock);
1320 while (!list_empty(&ctx->poll_list)) {
1321 unsigned int nr_events = 0;
1322
1323 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001324
1325 /*
1326 * Ensure we allow local-to-the-cpu processing to take place,
1327 * in this case we need to ensure that we reap all events.
1328 */
1329 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001330 }
1331 mutex_unlock(&ctx->uring_lock);
1332}
1333
Jens Axboe2b2ed972019-10-25 10:06:15 -06001334static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1335 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001336{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001337 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001338
1339 do {
1340 int tmin = 0;
1341
Jens Axboe500f9fb2019-08-19 12:15:59 -06001342 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001343 * Don't enter poll loop if we already have events pending.
1344 * If we do, we can potentially be spinning for commands that
1345 * already triggered a CQE (eg in error).
1346 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001347 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001348 break;
1349
1350 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001351 * If a submit got punted to a workqueue, we can have the
1352 * application entering polling for a command before it gets
1353 * issued. That app will hold the uring_lock for the duration
1354 * of the poll right here, so we need to take a breather every
1355 * now and then to ensure that the issue has a chance to add
1356 * the poll to the issued list. Otherwise we can spin here
1357 * forever, while the workqueue is stuck trying to acquire the
1358 * very same mutex.
1359 */
1360 if (!(++iters & 7)) {
1361 mutex_unlock(&ctx->uring_lock);
1362 mutex_lock(&ctx->uring_lock);
1363 }
1364
Jens Axboedef596e2019-01-09 08:59:42 -07001365 if (*nr_events < min)
1366 tmin = min - *nr_events;
1367
1368 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1369 if (ret <= 0)
1370 break;
1371 ret = 0;
1372 } while (min && !*nr_events && !need_resched());
1373
Jens Axboe2b2ed972019-10-25 10:06:15 -06001374 return ret;
1375}
1376
1377static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1378 long min)
1379{
1380 int ret;
1381
1382 /*
1383 * We disallow the app entering submit/complete with polling, but we
1384 * still need to lock the ring to prevent racing with polled issue
1385 * that got punted to a workqueue.
1386 */
1387 mutex_lock(&ctx->uring_lock);
1388 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001389 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001390 return ret;
1391}
1392
Jens Axboe491381ce2019-10-17 09:20:46 -06001393static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001394{
Jens Axboe491381ce2019-10-17 09:20:46 -06001395 /*
1396 * Tell lockdep we inherited freeze protection from submission
1397 * thread.
1398 */
1399 if (req->flags & REQ_F_ISREG) {
1400 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001401
Jens Axboe491381ce2019-10-17 09:20:46 -06001402 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001403 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001404 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001405}
1406
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001407static inline void req_set_fail_links(struct io_kiocb *req)
1408{
1409 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1410 req->flags |= REQ_F_FAIL_LINK;
1411}
1412
Jens Axboeba816ad2019-09-28 11:36:45 -06001413static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001414{
Jens Axboe9adbd452019-12-20 08:45:55 -07001415 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001416
Jens Axboe491381ce2019-10-17 09:20:46 -06001417 if (kiocb->ki_flags & IOCB_WRITE)
1418 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001419
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001420 if (res != req->result)
1421 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001422 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001423}
1424
1425static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1426{
Jens Axboe9adbd452019-12-20 08:45:55 -07001427 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001428
1429 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001430 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001431}
1432
Jens Axboeba816ad2019-09-28 11:36:45 -06001433static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1434{
Jens Axboe9adbd452019-12-20 08:45:55 -07001435 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001436 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001437
1438 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001439 io_put_req_find_next(req, &nxt);
1440
1441 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001442}
1443
Jens Axboedef596e2019-01-09 08:59:42 -07001444static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1445{
Jens Axboe9adbd452019-12-20 08:45:55 -07001446 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001447
Jens Axboe491381ce2019-10-17 09:20:46 -06001448 if (kiocb->ki_flags & IOCB_WRITE)
1449 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001450
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001451 if (res != req->result)
1452 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001453 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001454 if (res != -EAGAIN)
1455 req->flags |= REQ_F_IOPOLL_COMPLETED;
1456}
1457
1458/*
1459 * After the iocb has been issued, it's safe to be found on the poll list.
1460 * Adding the kiocb to the list AFTER submission ensures that we don't
1461 * find it from a io_iopoll_getevents() thread before the issuer is done
1462 * accessing the kiocb cookie.
1463 */
1464static void io_iopoll_req_issued(struct io_kiocb *req)
1465{
1466 struct io_ring_ctx *ctx = req->ctx;
1467
1468 /*
1469 * Track whether we have multiple files in our lists. This will impact
1470 * how we do polling eventually, not spinning if we're on potentially
1471 * different devices.
1472 */
1473 if (list_empty(&ctx->poll_list)) {
1474 ctx->poll_multi_file = false;
1475 } else if (!ctx->poll_multi_file) {
1476 struct io_kiocb *list_req;
1477
1478 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1479 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001480 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001481 ctx->poll_multi_file = true;
1482 }
1483
1484 /*
1485 * For fast devices, IO may have already completed. If it has, add
1486 * it to the front so we find it first.
1487 */
1488 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1489 list_add(&req->list, &ctx->poll_list);
1490 else
1491 list_add_tail(&req->list, &ctx->poll_list);
1492}
1493
Jens Axboe3d6770f2019-04-13 11:50:54 -06001494static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001495{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001496 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001497 int diff = state->has_refs - state->used_refs;
1498
1499 if (diff)
1500 fput_many(state->file, diff);
1501 state->file = NULL;
1502 }
1503}
1504
1505/*
1506 * Get as many references to a file as we have IOs left in this submission,
1507 * assuming most submissions are for one file, or at least that each file
1508 * has more than one submission.
1509 */
1510static struct file *io_file_get(struct io_submit_state *state, int fd)
1511{
1512 if (!state)
1513 return fget(fd);
1514
1515 if (state->file) {
1516 if (state->fd == fd) {
1517 state->used_refs++;
1518 state->ios_left--;
1519 return state->file;
1520 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001521 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001522 }
1523 state->file = fget_many(fd, state->ios_left);
1524 if (!state->file)
1525 return NULL;
1526
1527 state->fd = fd;
1528 state->has_refs = state->ios_left;
1529 state->used_refs = 1;
1530 state->ios_left--;
1531 return state->file;
1532}
1533
Jens Axboe2b188cc2019-01-07 10:46:33 -07001534/*
1535 * If we tracked the file through the SCM inflight mechanism, we could support
1536 * any file. For now, just ensure that anything potentially problematic is done
1537 * inline.
1538 */
1539static bool io_file_supports_async(struct file *file)
1540{
1541 umode_t mode = file_inode(file)->i_mode;
1542
Jens Axboe10d59342019-12-09 20:16:22 -07001543 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001544 return true;
1545 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1546 return true;
1547
1548 return false;
1549}
1550
Jens Axboe3529d8c2019-12-19 18:24:38 -07001551static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1552 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001553{
Jens Axboedef596e2019-01-09 08:59:42 -07001554 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001555 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001556 unsigned ioprio;
1557 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001558
Jens Axboe09bb8392019-03-13 12:39:28 -06001559 if (!req->file)
1560 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001561
Jens Axboe491381ce2019-10-17 09:20:46 -06001562 if (S_ISREG(file_inode(req->file)->i_mode))
1563 req->flags |= REQ_F_ISREG;
1564
Jens Axboe2b188cc2019-01-07 10:46:33 -07001565 kiocb->ki_pos = READ_ONCE(sqe->off);
1566 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1567 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1568
1569 ioprio = READ_ONCE(sqe->ioprio);
1570 if (ioprio) {
1571 ret = ioprio_check_cap(ioprio);
1572 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001573 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001574
1575 kiocb->ki_ioprio = ioprio;
1576 } else
1577 kiocb->ki_ioprio = get_current_ioprio();
1578
1579 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1580 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001581 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001582
1583 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001584 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1585 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001586 req->flags |= REQ_F_NOWAIT;
1587
1588 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001589 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001590
Jens Axboedef596e2019-01-09 08:59:42 -07001591 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001592 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1593 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001594 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001595
Jens Axboedef596e2019-01-09 08:59:42 -07001596 kiocb->ki_flags |= IOCB_HIPRI;
1597 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001598 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001599 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001600 if (kiocb->ki_flags & IOCB_HIPRI)
1601 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001602 kiocb->ki_complete = io_complete_rw;
1603 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001604
Jens Axboe3529d8c2019-12-19 18:24:38 -07001605 req->rw.addr = READ_ONCE(sqe->addr);
1606 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001607 /* we own ->private, reuse it for the buffer index */
1608 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001609 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001610 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001611}
1612
1613static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1614{
1615 switch (ret) {
1616 case -EIOCBQUEUED:
1617 break;
1618 case -ERESTARTSYS:
1619 case -ERESTARTNOINTR:
1620 case -ERESTARTNOHAND:
1621 case -ERESTART_RESTARTBLOCK:
1622 /*
1623 * We can't just restart the syscall, since previously
1624 * submitted sqes may already be in progress. Just fail this
1625 * IO with EINTR.
1626 */
1627 ret = -EINTR;
1628 /* fall through */
1629 default:
1630 kiocb->ki_complete(kiocb, ret, 0);
1631 }
1632}
1633
Jens Axboeba816ad2019-09-28 11:36:45 -06001634static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1635 bool in_async)
1636{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001637 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001638 *nxt = __io_complete_rw(kiocb, ret);
1639 else
1640 io_rw_done(kiocb, ret);
1641}
1642
Jens Axboe9adbd452019-12-20 08:45:55 -07001643static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001644 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001645{
Jens Axboe9adbd452019-12-20 08:45:55 -07001646 struct io_ring_ctx *ctx = req->ctx;
1647 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001648 struct io_mapped_ubuf *imu;
1649 unsigned index, buf_index;
1650 size_t offset;
1651 u64 buf_addr;
1652
1653 /* attempt to use fixed buffers without having provided iovecs */
1654 if (unlikely(!ctx->user_bufs))
1655 return -EFAULT;
1656
Jens Axboe9adbd452019-12-20 08:45:55 -07001657 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001658 if (unlikely(buf_index >= ctx->nr_user_bufs))
1659 return -EFAULT;
1660
1661 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1662 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001663 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001664
1665 /* overflow */
1666 if (buf_addr + len < buf_addr)
1667 return -EFAULT;
1668 /* not inside the mapped region */
1669 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1670 return -EFAULT;
1671
1672 /*
1673 * May not be a start of buffer, set size appropriately
1674 * and advance us to the beginning.
1675 */
1676 offset = buf_addr - imu->ubuf;
1677 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001678
1679 if (offset) {
1680 /*
1681 * Don't use iov_iter_advance() here, as it's really slow for
1682 * using the latter parts of a big fixed buffer - it iterates
1683 * over each segment manually. We can cheat a bit here, because
1684 * we know that:
1685 *
1686 * 1) it's a BVEC iter, we set it up
1687 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1688 * first and last bvec
1689 *
1690 * So just find our index, and adjust the iterator afterwards.
1691 * If the offset is within the first bvec (or the whole first
1692 * bvec, just use iov_iter_advance(). This makes it easier
1693 * since we can just skip the first segment, which may not
1694 * be PAGE_SIZE aligned.
1695 */
1696 const struct bio_vec *bvec = imu->bvec;
1697
1698 if (offset <= bvec->bv_len) {
1699 iov_iter_advance(iter, offset);
1700 } else {
1701 unsigned long seg_skip;
1702
1703 /* skip first vec */
1704 offset -= bvec->bv_len;
1705 seg_skip = 1 + (offset >> PAGE_SHIFT);
1706
1707 iter->bvec = bvec + seg_skip;
1708 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001709 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001710 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001711 }
1712 }
1713
Jens Axboe5e559562019-11-13 16:12:46 -07001714 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001715}
1716
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001717static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1718 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001719{
Jens Axboe9adbd452019-12-20 08:45:55 -07001720 void __user *buf = u64_to_user_ptr(req->rw.addr);
1721 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001722 u8 opcode;
1723
Jens Axboed625c6e2019-12-17 19:53:05 -07001724 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001725 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001726 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001727 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001728 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001729
Jens Axboe9adbd452019-12-20 08:45:55 -07001730 /* buffer index only valid with fixed read/write */
1731 if (req->rw.kiocb.private)
1732 return -EINVAL;
1733
Jens Axboef67676d2019-12-02 11:03:47 -07001734 if (req->io) {
1735 struct io_async_rw *iorw = &req->io->rw;
1736
1737 *iovec = iorw->iov;
1738 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1739 if (iorw->iov == iorw->fast_iov)
1740 *iovec = NULL;
1741 return iorw->size;
1742 }
1743
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001744 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001745 return -EFAULT;
1746
1747#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001748 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001749 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1750 iovec, iter);
1751#endif
1752
1753 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1754}
1755
Jens Axboe32960612019-09-23 11:05:34 -06001756/*
1757 * For files that don't have ->read_iter() and ->write_iter(), handle them
1758 * by looping over ->read() or ->write() manually.
1759 */
1760static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1761 struct iov_iter *iter)
1762{
1763 ssize_t ret = 0;
1764
1765 /*
1766 * Don't support polled IO through this interface, and we can't
1767 * support non-blocking either. For the latter, this just causes
1768 * the kiocb to be handled from an async context.
1769 */
1770 if (kiocb->ki_flags & IOCB_HIPRI)
1771 return -EOPNOTSUPP;
1772 if (kiocb->ki_flags & IOCB_NOWAIT)
1773 return -EAGAIN;
1774
1775 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001776 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001777 ssize_t nr;
1778
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001779 if (!iov_iter_is_bvec(iter)) {
1780 iovec = iov_iter_iovec(iter);
1781 } else {
1782 /* fixed buffers import bvec */
1783 iovec.iov_base = kmap(iter->bvec->bv_page)
1784 + iter->iov_offset;
1785 iovec.iov_len = min(iter->count,
1786 iter->bvec->bv_len - iter->iov_offset);
1787 }
1788
Jens Axboe32960612019-09-23 11:05:34 -06001789 if (rw == READ) {
1790 nr = file->f_op->read(file, iovec.iov_base,
1791 iovec.iov_len, &kiocb->ki_pos);
1792 } else {
1793 nr = file->f_op->write(file, iovec.iov_base,
1794 iovec.iov_len, &kiocb->ki_pos);
1795 }
1796
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001797 if (iov_iter_is_bvec(iter))
1798 kunmap(iter->bvec->bv_page);
1799
Jens Axboe32960612019-09-23 11:05:34 -06001800 if (nr < 0) {
1801 if (!ret)
1802 ret = nr;
1803 break;
1804 }
1805 ret += nr;
1806 if (nr != iovec.iov_len)
1807 break;
1808 iov_iter_advance(iter, nr);
1809 }
1810
1811 return ret;
1812}
1813
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001814static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07001815 struct iovec *iovec, struct iovec *fast_iov,
1816 struct iov_iter *iter)
1817{
1818 req->io->rw.nr_segs = iter->nr_segs;
1819 req->io->rw.size = io_size;
1820 req->io->rw.iov = iovec;
1821 if (!req->io->rw.iov) {
1822 req->io->rw.iov = req->io->rw.fast_iov;
1823 memcpy(req->io->rw.iov, fast_iov,
1824 sizeof(struct iovec) * iter->nr_segs);
1825 }
1826}
1827
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001828static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07001829{
1830 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07001831 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001832}
1833
1834static void io_rw_async(struct io_wq_work **workptr)
1835{
1836 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1837 struct iovec *iov = NULL;
1838
1839 if (req->io->rw.iov != req->io->rw.fast_iov)
1840 iov = req->io->rw.iov;
1841 io_wq_submit_work(workptr);
1842 kfree(iov);
1843}
1844
1845static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
1846 struct iovec *iovec, struct iovec *fast_iov,
1847 struct iov_iter *iter)
1848{
Jens Axboe74566df2020-01-13 19:23:24 -07001849 if (req->opcode == IORING_OP_READ_FIXED ||
1850 req->opcode == IORING_OP_WRITE_FIXED)
1851 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001852 if (!req->io && io_alloc_async_ctx(req))
1853 return -ENOMEM;
1854
1855 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
1856 req->work.func = io_rw_async;
1857 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001858}
1859
Jens Axboe3529d8c2019-12-19 18:24:38 -07001860static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1861 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07001862{
Jens Axboe3529d8c2019-12-19 18:24:38 -07001863 struct io_async_ctx *io;
1864 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07001865 ssize_t ret;
1866
Jens Axboe3529d8c2019-12-19 18:24:38 -07001867 ret = io_prep_rw(req, sqe, force_nonblock);
1868 if (ret)
1869 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07001870
Jens Axboe3529d8c2019-12-19 18:24:38 -07001871 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1872 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07001873
Jens Axboe3529d8c2019-12-19 18:24:38 -07001874 if (!req->io)
1875 return 0;
1876
1877 io = req->io;
1878 io->rw.iov = io->rw.fast_iov;
1879 req->io = NULL;
1880 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
1881 req->io = io;
1882 if (ret < 0)
1883 return ret;
1884
1885 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
1886 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001887}
1888
Pavel Begunkov267bc902019-11-07 01:41:08 +03001889static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001890 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001891{
1892 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001893 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001894 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001895 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001896 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001897
Jens Axboe3529d8c2019-12-19 18:24:38 -07001898 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07001899 if (ret < 0)
1900 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001901
Jens Axboefd6c2e42019-12-18 12:19:41 -07001902 /* Ensure we clear previously set non-block flag */
1903 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001904 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001905
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08001906 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001907 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001908 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001909 req->result = io_size;
1910
1911 /*
1912 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1913 * we know to async punt it even if it was opened O_NONBLOCK
1914 */
Jens Axboe9adbd452019-12-20 08:45:55 -07001915 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07001916 req->flags |= REQ_F_MUST_PUNT;
1917 goto copy_iov;
1918 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001919
Jens Axboe31b51512019-01-18 22:56:34 -07001920 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07001921 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001922 if (!ret) {
1923 ssize_t ret2;
1924
Jens Axboe9adbd452019-12-20 08:45:55 -07001925 if (req->file->f_op->read_iter)
1926 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001927 else
Jens Axboe9adbd452019-12-20 08:45:55 -07001928 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001929
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001930 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07001931 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001932 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001933 } else {
1934copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001935 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001936 inline_vecs, &iter);
1937 if (ret)
1938 goto out_free;
1939 return -EAGAIN;
1940 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001941 }
Jens Axboef67676d2019-12-02 11:03:47 -07001942out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001943 if (!io_wq_current_is_worker())
1944 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001945 return ret;
1946}
1947
Jens Axboe3529d8c2019-12-19 18:24:38 -07001948static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1949 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07001950{
Jens Axboe3529d8c2019-12-19 18:24:38 -07001951 struct io_async_ctx *io;
1952 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07001953 ssize_t ret;
1954
Jens Axboe3529d8c2019-12-19 18:24:38 -07001955 ret = io_prep_rw(req, sqe, force_nonblock);
1956 if (ret)
1957 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07001958
Jens Axboe3529d8c2019-12-19 18:24:38 -07001959 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
1960 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07001961
Jens Axboe3529d8c2019-12-19 18:24:38 -07001962 if (!req->io)
1963 return 0;
1964
1965 io = req->io;
1966 io->rw.iov = io->rw.fast_iov;
1967 req->io = NULL;
1968 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
1969 req->io = io;
1970 if (ret < 0)
1971 return ret;
1972
1973 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
1974 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001975}
1976
Pavel Begunkov267bc902019-11-07 01:41:08 +03001977static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001978 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001979{
1980 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001981 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001982 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001983 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001984 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001985
Jens Axboe3529d8c2019-12-19 18:24:38 -07001986 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07001987 if (ret < 0)
1988 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001989
Jens Axboefd6c2e42019-12-18 12:19:41 -07001990 /* Ensure we clear previously set non-block flag */
1991 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001992 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001993
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08001994 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001995 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001996 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001997 req->result = io_size;
1998
1999 /*
2000 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2001 * we know to async punt it even if it was opened O_NONBLOCK
2002 */
2003 if (force_nonblock && !io_file_supports_async(req->file)) {
2004 req->flags |= REQ_F_MUST_PUNT;
2005 goto copy_iov;
2006 }
2007
Jens Axboe10d59342019-12-09 20:16:22 -07002008 /* file path doesn't support NOWAIT for non-direct_IO */
2009 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2010 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002011 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002012
Jens Axboe31b51512019-01-18 22:56:34 -07002013 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002014 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002015 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002016 ssize_t ret2;
2017
Jens Axboe2b188cc2019-01-07 10:46:33 -07002018 /*
2019 * Open-code file_start_write here to grab freeze protection,
2020 * which will be released by another thread in
2021 * io_complete_rw(). Fool lockdep by telling it the lock got
2022 * released so that it doesn't complain about the held lock when
2023 * we return to userspace.
2024 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002025 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002026 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002027 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002028 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002029 SB_FREEZE_WRITE);
2030 }
2031 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002032
Jens Axboe9adbd452019-12-20 08:45:55 -07002033 if (req->file->f_op->write_iter)
2034 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002035 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002036 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002037 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002038 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002039 } else {
2040copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002041 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002042 inline_vecs, &iter);
2043 if (ret)
2044 goto out_free;
2045 return -EAGAIN;
2046 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002047 }
Jens Axboe31b51512019-01-18 22:56:34 -07002048out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002049 if (!io_wq_current_is_worker())
2050 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002051 return ret;
2052}
2053
2054/*
2055 * IORING_OP_NOP just posts a completion event, nothing else.
2056 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002057static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002058{
2059 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002060
Jens Axboedef596e2019-01-09 08:59:42 -07002061 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2062 return -EINVAL;
2063
Jens Axboe78e19bb2019-11-06 15:21:34 -07002064 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002065 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002066 return 0;
2067}
2068
Jens Axboe3529d8c2019-12-19 18:24:38 -07002069static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002070{
Jens Axboe6b063142019-01-10 22:13:58 -07002071 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002072
Jens Axboe09bb8392019-03-13 12:39:28 -06002073 if (!req->file)
2074 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002075
Jens Axboe6b063142019-01-10 22:13:58 -07002076 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002077 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002078 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002079 return -EINVAL;
2080
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002081 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2082 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2083 return -EINVAL;
2084
2085 req->sync.off = READ_ONCE(sqe->off);
2086 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002087 return 0;
2088}
2089
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002090static bool io_req_cancelled(struct io_kiocb *req)
2091{
2092 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2093 req_set_fail_links(req);
2094 io_cqring_add_event(req, -ECANCELED);
2095 io_put_req(req);
2096 return true;
2097 }
2098
2099 return false;
2100}
2101
Jens Axboe78912932020-01-14 22:09:06 -07002102static void io_link_work_cb(struct io_wq_work **workptr)
2103{
2104 struct io_wq_work *work = *workptr;
2105 struct io_kiocb *link = work->data;
2106
2107 io_queue_linked_timeout(link);
2108 work->func = io_wq_submit_work;
2109}
2110
2111static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2112{
2113 struct io_kiocb *link;
2114
2115 io_prep_async_work(nxt, &link);
2116 *workptr = &nxt->work;
2117 if (link) {
2118 nxt->work.flags |= IO_WQ_WORK_CB;
2119 nxt->work.func = io_link_work_cb;
2120 nxt->work.data = link;
2121 }
2122}
2123
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002124static void io_fsync_finish(struct io_wq_work **workptr)
2125{
2126 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2127 loff_t end = req->sync.off + req->sync.len;
2128 struct io_kiocb *nxt = NULL;
2129 int ret;
2130
2131 if (io_req_cancelled(req))
2132 return;
2133
Jens Axboe9adbd452019-12-20 08:45:55 -07002134 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002135 end > 0 ? end : LLONG_MAX,
2136 req->sync.flags & IORING_FSYNC_DATASYNC);
2137 if (ret < 0)
2138 req_set_fail_links(req);
2139 io_cqring_add_event(req, ret);
2140 io_put_req_find_next(req, &nxt);
2141 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002142 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002143}
2144
Jens Axboefc4df992019-12-10 14:38:45 -07002145static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2146 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002147{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002148 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002149
2150 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002151 if (force_nonblock) {
2152 io_put_req(req);
2153 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002154 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002155 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002156
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002157 work = old_work = &req->work;
2158 io_fsync_finish(&work);
2159 if (work && work != old_work)
2160 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002161 return 0;
2162}
2163
Jens Axboed63d1b52019-12-10 10:38:56 -07002164static void io_fallocate_finish(struct io_wq_work **workptr)
2165{
2166 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2167 struct io_kiocb *nxt = NULL;
2168 int ret;
2169
2170 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2171 req->sync.len);
2172 if (ret < 0)
2173 req_set_fail_links(req);
2174 io_cqring_add_event(req, ret);
2175 io_put_req_find_next(req, &nxt);
2176 if (nxt)
2177 io_wq_assign_next(workptr, nxt);
2178}
2179
2180static int io_fallocate_prep(struct io_kiocb *req,
2181 const struct io_uring_sqe *sqe)
2182{
2183 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2184 return -EINVAL;
2185
2186 req->sync.off = READ_ONCE(sqe->off);
2187 req->sync.len = READ_ONCE(sqe->addr);
2188 req->sync.mode = READ_ONCE(sqe->len);
2189 return 0;
2190}
2191
2192static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2193 bool force_nonblock)
2194{
2195 struct io_wq_work *work, *old_work;
2196
2197 /* fallocate always requiring blocking context */
2198 if (force_nonblock) {
2199 io_put_req(req);
2200 req->work.func = io_fallocate_finish;
2201 return -EAGAIN;
2202 }
2203
2204 work = old_work = &req->work;
2205 io_fallocate_finish(&work);
2206 if (work && work != old_work)
2207 *nxt = container_of(work, struct io_kiocb, work);
2208
2209 return 0;
2210}
2211
Jens Axboe15b71ab2019-12-11 11:20:36 -07002212static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2213{
2214 int ret;
2215
2216 if (sqe->ioprio || sqe->buf_index)
2217 return -EINVAL;
2218
2219 req->open.dfd = READ_ONCE(sqe->fd);
2220 req->open.mode = READ_ONCE(sqe->len);
2221 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2222 req->open.flags = READ_ONCE(sqe->open_flags);
2223
2224 req->open.filename = getname(req->open.fname);
2225 if (IS_ERR(req->open.filename)) {
2226 ret = PTR_ERR(req->open.filename);
2227 req->open.filename = NULL;
2228 return ret;
2229 }
2230
2231 return 0;
2232}
2233
2234static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2235 bool force_nonblock)
2236{
2237 struct open_flags op;
2238 struct open_how how;
2239 struct file *file;
2240 int ret;
2241
2242 if (force_nonblock) {
2243 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2244 return -EAGAIN;
2245 }
2246
2247 how = build_open_how(req->open.flags, req->open.mode);
2248 ret = build_open_flags(&how, &op);
2249 if (ret)
2250 goto err;
2251
2252 ret = get_unused_fd_flags(how.flags);
2253 if (ret < 0)
2254 goto err;
2255
2256 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2257 if (IS_ERR(file)) {
2258 put_unused_fd(ret);
2259 ret = PTR_ERR(file);
2260 } else {
2261 fsnotify_open(file);
2262 fd_install(ret, file);
2263 }
2264err:
2265 putname(req->open.filename);
2266 if (ret < 0)
2267 req_set_fail_links(req);
2268 io_cqring_add_event(req, ret);
2269 io_put_req_find_next(req, nxt);
2270 return 0;
2271}
2272
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002273static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2274{
2275 unsigned lookup_flags;
2276 int ret;
2277
2278 if (sqe->ioprio || sqe->buf_index)
2279 return -EINVAL;
2280
2281 req->open.dfd = READ_ONCE(sqe->fd);
2282 req->open.mask = READ_ONCE(sqe->len);
2283 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2284 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2285 req->open.flags = READ_ONCE(sqe->statx_flags);
2286
2287 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.flags))
2288 return -EINVAL;
2289
2290 req->open.filename = getname_flags(req->open.fname, lookup_flags, NULL);
2291 if (IS_ERR(req->open.filename)) {
2292 ret = PTR_ERR(req->open.filename);
2293 req->open.filename = NULL;
2294 return ret;
2295 }
2296
2297 return 0;
2298}
2299
2300static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2301 bool force_nonblock)
2302{
2303 struct io_open *ctx = &req->open;
2304 unsigned lookup_flags;
2305 struct path path;
2306 struct kstat stat;
2307 int ret;
2308
2309 if (force_nonblock)
2310 return -EAGAIN;
2311
2312 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->flags))
2313 return -EINVAL;
2314
2315retry:
2316 /* filename_lookup() drops it, keep a reference */
2317 ctx->filename->refcnt++;
2318
2319 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2320 NULL);
2321 if (ret)
2322 goto err;
2323
2324 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->flags);
2325 path_put(&path);
2326 if (retry_estale(ret, lookup_flags)) {
2327 lookup_flags |= LOOKUP_REVAL;
2328 goto retry;
2329 }
2330 if (!ret)
2331 ret = cp_statx(&stat, ctx->buffer);
2332err:
2333 putname(ctx->filename);
2334 if (ret < 0)
2335 req_set_fail_links(req);
2336 io_cqring_add_event(req, ret);
2337 io_put_req_find_next(req, nxt);
2338 return 0;
2339}
2340
Jens Axboeb5dba592019-12-11 14:02:38 -07002341static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2342{
2343 /*
2344 * If we queue this for async, it must not be cancellable. That would
2345 * leave the 'file' in an undeterminate state.
2346 */
2347 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2348
2349 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2350 sqe->rw_flags || sqe->buf_index)
2351 return -EINVAL;
2352 if (sqe->flags & IOSQE_FIXED_FILE)
2353 return -EINVAL;
2354
2355 req->close.fd = READ_ONCE(sqe->fd);
2356 if (req->file->f_op == &io_uring_fops ||
2357 req->close.fd == req->ring_fd)
2358 return -EBADF;
2359
2360 return 0;
2361}
2362
2363static void io_close_finish(struct io_wq_work **workptr)
2364{
2365 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2366 struct io_kiocb *nxt = NULL;
2367
2368 /* Invoked with files, we need to do the close */
2369 if (req->work.files) {
2370 int ret;
2371
2372 ret = filp_close(req->close.put_file, req->work.files);
2373 if (ret < 0) {
2374 req_set_fail_links(req);
2375 }
2376 io_cqring_add_event(req, ret);
2377 }
2378
2379 fput(req->close.put_file);
2380
2381 /* we bypassed the re-issue, drop the submission reference */
2382 io_put_req(req);
2383 io_put_req_find_next(req, &nxt);
2384 if (nxt)
2385 io_wq_assign_next(workptr, nxt);
2386}
2387
2388static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2389 bool force_nonblock)
2390{
2391 int ret;
2392
2393 req->close.put_file = NULL;
2394 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2395 if (ret < 0)
2396 return ret;
2397
2398 /* if the file has a flush method, be safe and punt to async */
2399 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2400 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2401 goto eagain;
2402 }
2403
2404 /*
2405 * No ->flush(), safely close from here and just punt the
2406 * fput() to async context.
2407 */
2408 ret = filp_close(req->close.put_file, current->files);
2409
2410 if (ret < 0)
2411 req_set_fail_links(req);
2412 io_cqring_add_event(req, ret);
2413
2414 if (io_wq_current_is_worker()) {
2415 struct io_wq_work *old_work, *work;
2416
2417 old_work = work = &req->work;
2418 io_close_finish(&work);
2419 if (work && work != old_work)
2420 *nxt = container_of(work, struct io_kiocb, work);
2421 return 0;
2422 }
2423
2424eagain:
2425 req->work.func = io_close_finish;
2426 return -EAGAIN;
2427}
2428
Jens Axboe3529d8c2019-12-19 18:24:38 -07002429static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002430{
2431 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002432
2433 if (!req->file)
2434 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002435
2436 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2437 return -EINVAL;
2438 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2439 return -EINVAL;
2440
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002441 req->sync.off = READ_ONCE(sqe->off);
2442 req->sync.len = READ_ONCE(sqe->len);
2443 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002444 return 0;
2445}
2446
2447static void io_sync_file_range_finish(struct io_wq_work **workptr)
2448{
2449 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2450 struct io_kiocb *nxt = NULL;
2451 int ret;
2452
2453 if (io_req_cancelled(req))
2454 return;
2455
Jens Axboe9adbd452019-12-20 08:45:55 -07002456 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002457 req->sync.flags);
2458 if (ret < 0)
2459 req_set_fail_links(req);
2460 io_cqring_add_event(req, ret);
2461 io_put_req_find_next(req, &nxt);
2462 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002463 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002464}
2465
Jens Axboefc4df992019-12-10 14:38:45 -07002466static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002467 bool force_nonblock)
2468{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002469 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002470
2471 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002472 if (force_nonblock) {
2473 io_put_req(req);
2474 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002475 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002476 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002477
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002478 work = old_work = &req->work;
2479 io_sync_file_range_finish(&work);
2480 if (work && work != old_work)
2481 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002482 return 0;
2483}
2484
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002485#if defined(CONFIG_NET)
2486static void io_sendrecv_async(struct io_wq_work **workptr)
2487{
2488 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2489 struct iovec *iov = NULL;
2490
2491 if (req->io->rw.iov != req->io->rw.fast_iov)
2492 iov = req->io->msg.iov;
2493 io_wq_submit_work(workptr);
2494 kfree(iov);
2495}
2496#endif
2497
Jens Axboe3529d8c2019-12-19 18:24:38 -07002498static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002499{
Jens Axboe03b12302019-12-02 18:50:25 -07002500#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002501 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002502 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002503
Jens Axboee47293f2019-12-20 08:58:21 -07002504 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2505 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe3529d8c2019-12-19 18:24:38 -07002506
2507 if (!io)
2508 return 0;
2509
Jens Axboed9688562019-12-09 19:35:20 -07002510 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002511 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002512 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002513#else
Jens Axboee47293f2019-12-20 08:58:21 -07002514 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002515#endif
2516}
2517
Jens Axboefc4df992019-12-10 14:38:45 -07002518static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2519 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002520{
2521#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002522 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002523 struct socket *sock;
2524 int ret;
2525
2526 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2527 return -EINVAL;
2528
2529 sock = sock_from_file(req->file, &ret);
2530 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002531 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002532 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002533 unsigned flags;
2534
Jens Axboe03b12302019-12-02 18:50:25 -07002535 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002536 kmsg = &req->io->msg;
2537 kmsg->msg.msg_name = &addr;
2538 /* if iov is set, it's allocated already */
2539 if (!kmsg->iov)
2540 kmsg->iov = kmsg->fast_iov;
2541 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002542 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002543 struct io_sr_msg *sr = &req->sr_msg;
2544
Jens Axboe0b416c32019-12-15 10:57:46 -07002545 kmsg = &io.msg;
2546 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002547
2548 io.msg.iov = io.msg.fast_iov;
2549 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2550 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002551 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002552 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002553 }
2554
Jens Axboee47293f2019-12-20 08:58:21 -07002555 flags = req->sr_msg.msg_flags;
2556 if (flags & MSG_DONTWAIT)
2557 req->flags |= REQ_F_NOWAIT;
2558 else if (force_nonblock)
2559 flags |= MSG_DONTWAIT;
2560
Jens Axboe0b416c32019-12-15 10:57:46 -07002561 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002562 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002563 if (req->io)
2564 return -EAGAIN;
2565 if (io_alloc_async_ctx(req))
2566 return -ENOMEM;
2567 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2568 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002569 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002570 }
2571 if (ret == -ERESTARTSYS)
2572 ret = -EINTR;
2573 }
2574
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002575 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002576 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002577 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002578 if (ret < 0)
2579 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002580 io_put_req_find_next(req, nxt);
2581 return 0;
2582#else
2583 return -EOPNOTSUPP;
2584#endif
2585}
2586
Jens Axboe3529d8c2019-12-19 18:24:38 -07002587static int io_recvmsg_prep(struct io_kiocb *req,
2588 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002589{
2590#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002591 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002592 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002593
Jens Axboe3529d8c2019-12-19 18:24:38 -07002594 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2595 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2596
2597 if (!io)
Jens Axboe06b76d42019-12-19 14:44:26 -07002598 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002599
Jens Axboed9688562019-12-09 19:35:20 -07002600 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002601 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002602 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002603#else
Jens Axboee47293f2019-12-20 08:58:21 -07002604 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002605#endif
2606}
2607
Jens Axboefc4df992019-12-10 14:38:45 -07002608static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2609 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002610{
2611#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002612 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002613 struct socket *sock;
2614 int ret;
2615
2616 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2617 return -EINVAL;
2618
2619 sock = sock_from_file(req->file, &ret);
2620 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002621 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002622 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002623 unsigned flags;
2624
Jens Axboe03b12302019-12-02 18:50:25 -07002625 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002626 kmsg = &req->io->msg;
2627 kmsg->msg.msg_name = &addr;
2628 /* if iov is set, it's allocated already */
2629 if (!kmsg->iov)
2630 kmsg->iov = kmsg->fast_iov;
2631 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002632 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002633 struct io_sr_msg *sr = &req->sr_msg;
2634
Jens Axboe0b416c32019-12-15 10:57:46 -07002635 kmsg = &io.msg;
2636 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002637
2638 io.msg.iov = io.msg.fast_iov;
2639 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
2640 sr->msg_flags, &io.msg.uaddr,
2641 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002642 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002643 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002644 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002645
Jens Axboee47293f2019-12-20 08:58:21 -07002646 flags = req->sr_msg.msg_flags;
2647 if (flags & MSG_DONTWAIT)
2648 req->flags |= REQ_F_NOWAIT;
2649 else if (force_nonblock)
2650 flags |= MSG_DONTWAIT;
2651
2652 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
2653 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002654 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002655 if (req->io)
2656 return -EAGAIN;
2657 if (io_alloc_async_ctx(req))
2658 return -ENOMEM;
2659 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2660 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002661 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002662 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002663 if (ret == -ERESTARTSYS)
2664 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002665 }
2666
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002667 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002668 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002669 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002670 if (ret < 0)
2671 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002672 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002673 return 0;
2674#else
2675 return -EOPNOTSUPP;
2676#endif
2677}
2678
Jens Axboe3529d8c2019-12-19 18:24:38 -07002679static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002680{
2681#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002682 struct io_accept *accept = &req->accept;
2683
Jens Axboe17f2fe32019-10-17 14:42:58 -06002684 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2685 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002686 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002687 return -EINVAL;
2688
Jens Axboed55e5f52019-12-11 16:12:15 -07002689 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2690 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002691 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002692 return 0;
2693#else
2694 return -EOPNOTSUPP;
2695#endif
2696}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002697
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002698#if defined(CONFIG_NET)
2699static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2700 bool force_nonblock)
2701{
2702 struct io_accept *accept = &req->accept;
2703 unsigned file_flags;
2704 int ret;
2705
2706 file_flags = force_nonblock ? O_NONBLOCK : 0;
2707 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2708 accept->addr_len, accept->flags);
2709 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002710 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002711 if (ret == -ERESTARTSYS)
2712 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002713 if (ret < 0)
2714 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002715 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002716 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002717 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002718}
2719
2720static void io_accept_finish(struct io_wq_work **workptr)
2721{
2722 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2723 struct io_kiocb *nxt = NULL;
2724
2725 if (io_req_cancelled(req))
2726 return;
2727 __io_accept(req, &nxt, false);
2728 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002729 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002730}
2731#endif
2732
2733static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2734 bool force_nonblock)
2735{
2736#if defined(CONFIG_NET)
2737 int ret;
2738
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002739 ret = __io_accept(req, nxt, force_nonblock);
2740 if (ret == -EAGAIN && force_nonblock) {
2741 req->work.func = io_accept_finish;
2742 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2743 io_put_req(req);
2744 return -EAGAIN;
2745 }
2746 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002747#else
2748 return -EOPNOTSUPP;
2749#endif
2750}
2751
Jens Axboe3529d8c2019-12-19 18:24:38 -07002752static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07002753{
2754#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002755 struct io_connect *conn = &req->connect;
2756 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07002757
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002758 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2759 return -EINVAL;
2760 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2761 return -EINVAL;
2762
Jens Axboe3529d8c2019-12-19 18:24:38 -07002763 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2764 conn->addr_len = READ_ONCE(sqe->addr2);
2765
2766 if (!io)
2767 return 0;
2768
2769 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002770 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002771#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002772 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07002773#endif
2774}
2775
Jens Axboefc4df992019-12-10 14:38:45 -07002776static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
2777 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07002778{
2779#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07002780 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002781 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002782 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002783
Jens Axboef499a022019-12-02 16:28:46 -07002784 if (req->io) {
2785 io = req->io;
2786 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002787 ret = move_addr_to_kernel(req->connect.addr,
2788 req->connect.addr_len,
2789 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002790 if (ret)
2791 goto out;
2792 io = &__io;
2793 }
2794
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002795 file_flags = force_nonblock ? O_NONBLOCK : 0;
2796
2797 ret = __sys_connect_file(req->file, &io->connect.address,
2798 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002799 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002800 if (req->io)
2801 return -EAGAIN;
2802 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07002803 ret = -ENOMEM;
2804 goto out;
2805 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002806 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07002807 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002808 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002809 if (ret == -ERESTARTSYS)
2810 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002811out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002812 if (ret < 0)
2813 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002814 io_cqring_add_event(req, ret);
2815 io_put_req_find_next(req, nxt);
2816 return 0;
2817#else
2818 return -EOPNOTSUPP;
2819#endif
2820}
2821
Jens Axboe221c5eb2019-01-17 09:41:58 -07002822static void io_poll_remove_one(struct io_kiocb *req)
2823{
2824 struct io_poll_iocb *poll = &req->poll;
2825
2826 spin_lock(&poll->head->lock);
2827 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07002828 if (!list_empty(&poll->wait.entry)) {
2829 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002830 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002831 }
2832 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002833 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002834}
2835
2836static void io_poll_remove_all(struct io_ring_ctx *ctx)
2837{
Jens Axboe78076bb2019-12-04 19:56:40 -07002838 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002839 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07002840 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002841
2842 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002843 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2844 struct hlist_head *list;
2845
2846 list = &ctx->cancel_hash[i];
2847 hlist_for_each_entry_safe(req, tmp, list, hash_node)
2848 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002849 }
2850 spin_unlock_irq(&ctx->completion_lock);
2851}
2852
Jens Axboe47f46762019-11-09 17:43:02 -07002853static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2854{
Jens Axboe78076bb2019-12-04 19:56:40 -07002855 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07002856 struct io_kiocb *req;
2857
Jens Axboe78076bb2019-12-04 19:56:40 -07002858 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2859 hlist_for_each_entry(req, list, hash_node) {
2860 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002861 io_poll_remove_one(req);
2862 return 0;
2863 }
Jens Axboe47f46762019-11-09 17:43:02 -07002864 }
2865
2866 return -ENOENT;
2867}
2868
Jens Axboe3529d8c2019-12-19 18:24:38 -07002869static int io_poll_remove_prep(struct io_kiocb *req,
2870 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002871{
Jens Axboe221c5eb2019-01-17 09:41:58 -07002872 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2873 return -EINVAL;
2874 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2875 sqe->poll_events)
2876 return -EINVAL;
2877
Jens Axboe0969e782019-12-17 18:40:57 -07002878 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07002879 return 0;
2880}
2881
2882/*
2883 * Find a running poll command that matches one specified in sqe->addr,
2884 * and remove it if found.
2885 */
2886static int io_poll_remove(struct io_kiocb *req)
2887{
2888 struct io_ring_ctx *ctx = req->ctx;
2889 u64 addr;
2890 int ret;
2891
Jens Axboe0969e782019-12-17 18:40:57 -07002892 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002893 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07002894 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002895 spin_unlock_irq(&ctx->completion_lock);
2896
Jens Axboe78e19bb2019-11-06 15:21:34 -07002897 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002898 if (ret < 0)
2899 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002900 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002901 return 0;
2902}
2903
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002904static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002905{
Jackie Liua197f662019-11-08 08:09:12 -07002906 struct io_ring_ctx *ctx = req->ctx;
2907
Jens Axboe8c838782019-03-12 15:48:16 -06002908 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002909 if (error)
2910 io_cqring_fill_event(req, error);
2911 else
2912 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002913 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002914}
2915
Jens Axboe561fb042019-10-24 07:25:42 -06002916static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002917{
Jens Axboe561fb042019-10-24 07:25:42 -06002918 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002919 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2920 struct io_poll_iocb *poll = &req->poll;
2921 struct poll_table_struct pt = { ._key = poll->events };
2922 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002923 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002924 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002925 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002926
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002927 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002928 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002929 ret = -ECANCELED;
2930 } else if (READ_ONCE(poll->canceled)) {
2931 ret = -ECANCELED;
2932 }
Jens Axboe561fb042019-10-24 07:25:42 -06002933
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002934 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002935 mask = vfs_poll(poll->file, &pt) & poll->events;
2936
2937 /*
2938 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2939 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2940 * synchronize with them. In the cancellation case the list_del_init
2941 * itself is not actually needed, but harmless so we keep it in to
2942 * avoid further branches in the fast path.
2943 */
2944 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002945 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07002946 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002947 spin_unlock_irq(&ctx->completion_lock);
2948 return;
2949 }
Jens Axboe78076bb2019-12-04 19:56:40 -07002950 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002951 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002952 spin_unlock_irq(&ctx->completion_lock);
2953
Jens Axboe8c838782019-03-12 15:48:16 -06002954 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002955
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002956 if (ret < 0)
2957 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002958 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002959 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002960 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002961}
2962
2963static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2964 void *key)
2965{
Jens Axboee9444752019-11-26 15:02:04 -07002966 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002967 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2968 struct io_ring_ctx *ctx = req->ctx;
2969 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002970 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002971
2972 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002973 if (mask && !(mask & poll->events))
2974 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002975
Jens Axboe392edb42019-12-09 17:52:20 -07002976 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002977
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002978 /*
2979 * Run completion inline if we can. We're using trylock here because
2980 * we are violating the completion_lock -> poll wq lock ordering.
2981 * If we have a link timeout we're going to need the completion_lock
2982 * for finalizing the request, mark us as having grabbed that already.
2983 */
Jens Axboe8c838782019-03-12 15:48:16 -06002984 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboe78076bb2019-12-04 19:56:40 -07002985 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002986 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002987 req->flags |= REQ_F_COMP_LOCKED;
2988 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002989 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2990
2991 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002992 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002993 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002994 }
2995
Jens Axboe221c5eb2019-01-17 09:41:58 -07002996 return 1;
2997}
2998
2999struct io_poll_table {
3000 struct poll_table_struct pt;
3001 struct io_kiocb *req;
3002 int error;
3003};
3004
3005static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3006 struct poll_table_struct *p)
3007{
3008 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3009
3010 if (unlikely(pt->req->poll.head)) {
3011 pt->error = -EINVAL;
3012 return;
3013 }
3014
3015 pt->error = 0;
3016 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003017 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003018}
3019
Jens Axboeeac406c2019-11-14 12:09:58 -07003020static void io_poll_req_insert(struct io_kiocb *req)
3021{
3022 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003023 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003024
Jens Axboe78076bb2019-12-04 19:56:40 -07003025 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3026 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003027}
3028
Jens Axboe3529d8c2019-12-19 18:24:38 -07003029static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003030{
3031 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003032 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003033
3034 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3035 return -EINVAL;
3036 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3037 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003038 if (!poll->file)
3039 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003040
Jens Axboe221c5eb2019-01-17 09:41:58 -07003041 events = READ_ONCE(sqe->poll_events);
3042 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003043 return 0;
3044}
3045
3046static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3047{
3048 struct io_poll_iocb *poll = &req->poll;
3049 struct io_ring_ctx *ctx = req->ctx;
3050 struct io_poll_table ipt;
3051 bool cancel = false;
3052 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003053
3054 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003055 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003056
Jens Axboe221c5eb2019-01-17 09:41:58 -07003057 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003058 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003059 poll->canceled = false;
3060
3061 ipt.pt._qproc = io_poll_queue_proc;
3062 ipt.pt._key = poll->events;
3063 ipt.req = req;
3064 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3065
3066 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003067 INIT_LIST_HEAD(&poll->wait.entry);
3068 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3069 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003070
Jens Axboe36703242019-07-25 10:20:18 -06003071 INIT_LIST_HEAD(&req->list);
3072
Jens Axboe221c5eb2019-01-17 09:41:58 -07003073 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003074
3075 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003076 if (likely(poll->head)) {
3077 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003078 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003079 if (ipt.error)
3080 cancel = true;
3081 ipt.error = 0;
3082 mask = 0;
3083 }
3084 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003085 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003086 else if (cancel)
3087 WRITE_ONCE(poll->canceled, true);
3088 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003089 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003090 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003091 }
Jens Axboe8c838782019-03-12 15:48:16 -06003092 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003093 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003094 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003095 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003096 spin_unlock_irq(&ctx->completion_lock);
3097
Jens Axboe8c838782019-03-12 15:48:16 -06003098 if (mask) {
3099 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003100 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003101 }
Jens Axboe8c838782019-03-12 15:48:16 -06003102 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003103}
3104
Jens Axboe5262f562019-09-17 12:26:57 -06003105static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3106{
Jens Axboead8a48a2019-11-15 08:49:11 -07003107 struct io_timeout_data *data = container_of(timer,
3108 struct io_timeout_data, timer);
3109 struct io_kiocb *req = data->req;
3110 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003111 unsigned long flags;
3112
Jens Axboe5262f562019-09-17 12:26:57 -06003113 atomic_inc(&ctx->cq_timeouts);
3114
3115 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003116 /*
Jens Axboe11365042019-10-16 09:08:32 -06003117 * We could be racing with timeout deletion. If the list is empty,
3118 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003119 */
Jens Axboe842f9612019-10-29 12:34:10 -06003120 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003121 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003122
Jens Axboe11365042019-10-16 09:08:32 -06003123 /*
3124 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003125 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003126 * pointer will be increased, otherwise other timeout reqs may
3127 * return in advance without waiting for enough wait_nr.
3128 */
3129 prev = req;
3130 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3131 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003132 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003133 }
Jens Axboe842f9612019-10-29 12:34:10 -06003134
Jens Axboe78e19bb2019-11-06 15:21:34 -07003135 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003136 io_commit_cqring(ctx);
3137 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3138
3139 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003140 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003141 io_put_req(req);
3142 return HRTIMER_NORESTART;
3143}
3144
Jens Axboe47f46762019-11-09 17:43:02 -07003145static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3146{
3147 struct io_kiocb *req;
3148 int ret = -ENOENT;
3149
3150 list_for_each_entry(req, &ctx->timeout_list, list) {
3151 if (user_data == req->user_data) {
3152 list_del_init(&req->list);
3153 ret = 0;
3154 break;
3155 }
3156 }
3157
3158 if (ret == -ENOENT)
3159 return ret;
3160
Jens Axboe2d283902019-12-04 11:08:05 -07003161 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003162 if (ret == -1)
3163 return -EALREADY;
3164
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003165 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003166 io_cqring_fill_event(req, -ECANCELED);
3167 io_put_req(req);
3168 return 0;
3169}
3170
Jens Axboe3529d8c2019-12-19 18:24:38 -07003171static int io_timeout_remove_prep(struct io_kiocb *req,
3172 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003173{
Jens Axboeb29472e2019-12-17 18:50:29 -07003174 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3175 return -EINVAL;
3176 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3177 return -EINVAL;
3178
3179 req->timeout.addr = READ_ONCE(sqe->addr);
3180 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3181 if (req->timeout.flags)
3182 return -EINVAL;
3183
Jens Axboeb29472e2019-12-17 18:50:29 -07003184 return 0;
3185}
3186
Jens Axboe11365042019-10-16 09:08:32 -06003187/*
3188 * Remove or update an existing timeout command
3189 */
Jens Axboefc4df992019-12-10 14:38:45 -07003190static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003191{
3192 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003193 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003194
Jens Axboe11365042019-10-16 09:08:32 -06003195 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003196 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003197
Jens Axboe47f46762019-11-09 17:43:02 -07003198 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003199 io_commit_cqring(ctx);
3200 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003201 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003202 if (ret < 0)
3203 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003204 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003205 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003206}
3207
Jens Axboe3529d8c2019-12-19 18:24:38 -07003208static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003209 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003210{
Jens Axboead8a48a2019-11-15 08:49:11 -07003211 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003212 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003213
Jens Axboead8a48a2019-11-15 08:49:11 -07003214 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003215 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003216 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003217 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003218 if (sqe->off && is_timeout_link)
3219 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003220 flags = READ_ONCE(sqe->timeout_flags);
3221 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003222 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003223
Jens Axboe26a61672019-12-20 09:02:01 -07003224 req->timeout.count = READ_ONCE(sqe->off);
3225
Jens Axboe3529d8c2019-12-19 18:24:38 -07003226 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003227 return -ENOMEM;
3228
3229 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003230 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003231 req->flags |= REQ_F_TIMEOUT;
3232
3233 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003234 return -EFAULT;
3235
Jens Axboe11365042019-10-16 09:08:32 -06003236 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003237 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003238 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003239 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003240
Jens Axboead8a48a2019-11-15 08:49:11 -07003241 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3242 return 0;
3243}
3244
Jens Axboefc4df992019-12-10 14:38:45 -07003245static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003246{
3247 unsigned count;
3248 struct io_ring_ctx *ctx = req->ctx;
3249 struct io_timeout_data *data;
3250 struct list_head *entry;
3251 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003252
Jens Axboe2d283902019-12-04 11:08:05 -07003253 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003254
Jens Axboe5262f562019-09-17 12:26:57 -06003255 /*
3256 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003257 * timeout event to be satisfied. If it isn't set, then this is
3258 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003259 */
Jens Axboe26a61672019-12-20 09:02:01 -07003260 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003261 if (!count) {
3262 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3263 spin_lock_irq(&ctx->completion_lock);
3264 entry = ctx->timeout_list.prev;
3265 goto add;
3266 }
Jens Axboe5262f562019-09-17 12:26:57 -06003267
3268 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003269 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003270
3271 /*
3272 * Insertion sort, ensuring the first entry in the list is always
3273 * the one we need first.
3274 */
Jens Axboe5262f562019-09-17 12:26:57 -06003275 spin_lock_irq(&ctx->completion_lock);
3276 list_for_each_prev(entry, &ctx->timeout_list) {
3277 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003278 unsigned nxt_sq_head;
3279 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003280 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003281
Jens Axboe93bd25b2019-11-11 23:34:31 -07003282 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3283 continue;
3284
yangerkun5da0fb12019-10-15 21:59:29 +08003285 /*
3286 * Since cached_sq_head + count - 1 can overflow, use type long
3287 * long to store it.
3288 */
3289 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003290 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3291 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003292
3293 /*
3294 * cached_sq_head may overflow, and it will never overflow twice
3295 * once there is some timeout req still be valid.
3296 */
3297 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003298 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003299
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003300 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003301 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003302
3303 /*
3304 * Sequence of reqs after the insert one and itself should
3305 * be adjusted because each timeout req consumes a slot.
3306 */
3307 span++;
3308 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003309 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003310 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003311add:
Jens Axboe5262f562019-09-17 12:26:57 -06003312 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003313 data->timer.function = io_timeout_fn;
3314 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003315 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003316 return 0;
3317}
3318
Jens Axboe62755e32019-10-28 21:49:21 -06003319static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003320{
Jens Axboe62755e32019-10-28 21:49:21 -06003321 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003322
Jens Axboe62755e32019-10-28 21:49:21 -06003323 return req->user_data == (unsigned long) data;
3324}
3325
Jens Axboee977d6d2019-11-05 12:39:45 -07003326static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003327{
Jens Axboe62755e32019-10-28 21:49:21 -06003328 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003329 int ret = 0;
3330
Jens Axboe62755e32019-10-28 21:49:21 -06003331 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3332 switch (cancel_ret) {
3333 case IO_WQ_CANCEL_OK:
3334 ret = 0;
3335 break;
3336 case IO_WQ_CANCEL_RUNNING:
3337 ret = -EALREADY;
3338 break;
3339 case IO_WQ_CANCEL_NOTFOUND:
3340 ret = -ENOENT;
3341 break;
3342 }
3343
Jens Axboee977d6d2019-11-05 12:39:45 -07003344 return ret;
3345}
3346
Jens Axboe47f46762019-11-09 17:43:02 -07003347static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3348 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003349 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003350{
3351 unsigned long flags;
3352 int ret;
3353
3354 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3355 if (ret != -ENOENT) {
3356 spin_lock_irqsave(&ctx->completion_lock, flags);
3357 goto done;
3358 }
3359
3360 spin_lock_irqsave(&ctx->completion_lock, flags);
3361 ret = io_timeout_cancel(ctx, sqe_addr);
3362 if (ret != -ENOENT)
3363 goto done;
3364 ret = io_poll_cancel(ctx, sqe_addr);
3365done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003366 if (!ret)
3367 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003368 io_cqring_fill_event(req, ret);
3369 io_commit_cqring(ctx);
3370 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3371 io_cqring_ev_posted(ctx);
3372
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003373 if (ret < 0)
3374 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003375 io_put_req_find_next(req, nxt);
3376}
3377
Jens Axboe3529d8c2019-12-19 18:24:38 -07003378static int io_async_cancel_prep(struct io_kiocb *req,
3379 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003380{
Jens Axboefbf23842019-12-17 18:45:56 -07003381 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003382 return -EINVAL;
3383 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3384 sqe->cancel_flags)
3385 return -EINVAL;
3386
Jens Axboefbf23842019-12-17 18:45:56 -07003387 req->cancel.addr = READ_ONCE(sqe->addr);
3388 return 0;
3389}
3390
3391static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3392{
3393 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003394
3395 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003396 return 0;
3397}
3398
Jens Axboe05f3fb32019-12-09 11:22:50 -07003399static int io_files_update_prep(struct io_kiocb *req,
3400 const struct io_uring_sqe *sqe)
3401{
3402 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3403 return -EINVAL;
3404
3405 req->files_update.offset = READ_ONCE(sqe->off);
3406 req->files_update.nr_args = READ_ONCE(sqe->len);
3407 if (!req->files_update.nr_args)
3408 return -EINVAL;
3409 req->files_update.arg = READ_ONCE(sqe->addr);
3410 return 0;
3411}
3412
3413static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3414{
3415 struct io_ring_ctx *ctx = req->ctx;
3416 struct io_uring_files_update up;
3417 int ret;
3418
3419 if (force_nonblock) {
3420 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3421 return -EAGAIN;
3422 }
3423
3424 up.offset = req->files_update.offset;
3425 up.fds = req->files_update.arg;
3426
3427 mutex_lock(&ctx->uring_lock);
3428 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3429 mutex_unlock(&ctx->uring_lock);
3430
3431 if (ret < 0)
3432 req_set_fail_links(req);
3433 io_cqring_add_event(req, ret);
3434 io_put_req(req);
3435 return 0;
3436}
3437
Jens Axboe3529d8c2019-12-19 18:24:38 -07003438static int io_req_defer_prep(struct io_kiocb *req,
3439 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003440{
Jens Axboee7815732019-12-17 19:45:06 -07003441 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003442
Jens Axboed625c6e2019-12-17 19:53:05 -07003443 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003444 case IORING_OP_NOP:
3445 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003446 case IORING_OP_READV:
3447 case IORING_OP_READ_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003448 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003449 break;
3450 case IORING_OP_WRITEV:
3451 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003452 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003453 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003454 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003455 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003456 break;
3457 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003458 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003459 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003460 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003461 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003462 break;
3463 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003464 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003465 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003466 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003467 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003468 break;
3469 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003470 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003471 break;
Jens Axboef499a022019-12-02 16:28:46 -07003472 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003473 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003474 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003475 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003476 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003477 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003478 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003479 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003480 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003481 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003482 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003483 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003484 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003485 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003486 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003487 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003488 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003489 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003490 case IORING_OP_FALLOCATE:
3491 ret = io_fallocate_prep(req, sqe);
3492 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003493 case IORING_OP_OPENAT:
3494 ret = io_openat_prep(req, sqe);
3495 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003496 case IORING_OP_CLOSE:
3497 ret = io_close_prep(req, sqe);
3498 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003499 case IORING_OP_FILES_UPDATE:
3500 ret = io_files_update_prep(req, sqe);
3501 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003502 case IORING_OP_STATX:
3503 ret = io_statx_prep(req, sqe);
3504 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003505 default:
Jens Axboee7815732019-12-17 19:45:06 -07003506 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3507 req->opcode);
3508 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003509 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003510 }
3511
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003512 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003513}
3514
Jens Axboe3529d8c2019-12-19 18:24:38 -07003515static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003516{
Jackie Liua197f662019-11-08 08:09:12 -07003517 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003518 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003519
Bob Liu9d858b22019-11-13 18:06:25 +08003520 /* Still need defer if there is pending req in defer list. */
3521 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003522 return 0;
3523
Jens Axboe3529d8c2019-12-19 18:24:38 -07003524 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003525 return -EAGAIN;
3526
Jens Axboe3529d8c2019-12-19 18:24:38 -07003527 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003528 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003529 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003530
Jens Axboede0617e2019-04-06 21:51:27 -06003531 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003532 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003533 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003534 return 0;
3535 }
3536
Jens Axboe915967f2019-11-21 09:01:20 -07003537 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003538 list_add_tail(&req->list, &ctx->defer_list);
3539 spin_unlock_irq(&ctx->completion_lock);
3540 return -EIOCBQUEUED;
3541}
3542
Jens Axboe3529d8c2019-12-19 18:24:38 -07003543static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3544 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003545{
Jackie Liua197f662019-11-08 08:09:12 -07003546 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07003547 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003548
Jens Axboed625c6e2019-12-17 19:53:05 -07003549 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003550 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003551 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003552 break;
3553 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003554 case IORING_OP_READ_FIXED:
3555 if (sqe) {
3556 ret = io_read_prep(req, sqe, force_nonblock);
3557 if (ret < 0)
3558 break;
3559 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003560 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003561 break;
3562 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07003563 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003564 if (sqe) {
3565 ret = io_write_prep(req, sqe, force_nonblock);
3566 if (ret < 0)
3567 break;
3568 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003569 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003570 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003571 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003572 if (sqe) {
3573 ret = io_prep_fsync(req, sqe);
3574 if (ret < 0)
3575 break;
3576 }
Jens Axboefc4df992019-12-10 14:38:45 -07003577 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003578 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003579 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003580 if (sqe) {
3581 ret = io_poll_add_prep(req, sqe);
3582 if (ret)
3583 break;
3584 }
Jens Axboefc4df992019-12-10 14:38:45 -07003585 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003586 break;
3587 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003588 if (sqe) {
3589 ret = io_poll_remove_prep(req, sqe);
3590 if (ret < 0)
3591 break;
3592 }
Jens Axboefc4df992019-12-10 14:38:45 -07003593 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003594 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003595 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003596 if (sqe) {
3597 ret = io_prep_sfr(req, sqe);
3598 if (ret < 0)
3599 break;
3600 }
Jens Axboefc4df992019-12-10 14:38:45 -07003601 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003602 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003603 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003604 if (sqe) {
3605 ret = io_sendmsg_prep(req, sqe);
3606 if (ret < 0)
3607 break;
3608 }
Jens Axboefc4df992019-12-10 14:38:45 -07003609 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003610 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003611 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003612 if (sqe) {
3613 ret = io_recvmsg_prep(req, sqe);
3614 if (ret)
3615 break;
3616 }
Jens Axboefc4df992019-12-10 14:38:45 -07003617 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003618 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003619 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003620 if (sqe) {
3621 ret = io_timeout_prep(req, sqe, false);
3622 if (ret)
3623 break;
3624 }
Jens Axboefc4df992019-12-10 14:38:45 -07003625 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003626 break;
Jens Axboe11365042019-10-16 09:08:32 -06003627 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003628 if (sqe) {
3629 ret = io_timeout_remove_prep(req, sqe);
3630 if (ret)
3631 break;
3632 }
Jens Axboefc4df992019-12-10 14:38:45 -07003633 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003634 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003635 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003636 if (sqe) {
3637 ret = io_accept_prep(req, sqe);
3638 if (ret)
3639 break;
3640 }
Jens Axboefc4df992019-12-10 14:38:45 -07003641 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003642 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003643 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003644 if (sqe) {
3645 ret = io_connect_prep(req, sqe);
3646 if (ret)
3647 break;
3648 }
Jens Axboefc4df992019-12-10 14:38:45 -07003649 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003650 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003651 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003652 if (sqe) {
3653 ret = io_async_cancel_prep(req, sqe);
3654 if (ret)
3655 break;
3656 }
Jens Axboefc4df992019-12-10 14:38:45 -07003657 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003658 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003659 case IORING_OP_FALLOCATE:
3660 if (sqe) {
3661 ret = io_fallocate_prep(req, sqe);
3662 if (ret)
3663 break;
3664 }
3665 ret = io_fallocate(req, nxt, force_nonblock);
3666 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003667 case IORING_OP_OPENAT:
3668 if (sqe) {
3669 ret = io_openat_prep(req, sqe);
3670 if (ret)
3671 break;
3672 }
3673 ret = io_openat(req, nxt, force_nonblock);
3674 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003675 case IORING_OP_CLOSE:
3676 if (sqe) {
3677 ret = io_close_prep(req, sqe);
3678 if (ret)
3679 break;
3680 }
3681 ret = io_close(req, nxt, force_nonblock);
3682 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003683 case IORING_OP_FILES_UPDATE:
3684 if (sqe) {
3685 ret = io_files_update_prep(req, sqe);
3686 if (ret)
3687 break;
3688 }
3689 ret = io_files_update(req, force_nonblock);
3690 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003691 case IORING_OP_STATX:
3692 if (sqe) {
3693 ret = io_statx_prep(req, sqe);
3694 if (ret)
3695 break;
3696 }
3697 ret = io_statx(req, nxt, force_nonblock);
3698 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003699 default:
3700 ret = -EINVAL;
3701 break;
3702 }
3703
Jens Axboedef596e2019-01-09 08:59:42 -07003704 if (ret)
3705 return ret;
3706
3707 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07003708 const bool in_async = io_wq_current_is_worker();
3709
Jens Axboe9e645e112019-05-10 16:07:28 -06003710 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07003711 return -EAGAIN;
3712
Jens Axboe11ba8202020-01-15 21:51:17 -07003713 /* workqueue context doesn't hold uring_lock, grab it now */
3714 if (in_async)
3715 mutex_lock(&ctx->uring_lock);
3716
Jens Axboedef596e2019-01-09 08:59:42 -07003717 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07003718
3719 if (in_async)
3720 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07003721 }
3722
3723 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003724}
3725
Jens Axboe561fb042019-10-24 07:25:42 -06003726static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003727{
Jens Axboe561fb042019-10-24 07:25:42 -06003728 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003729 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003730 struct io_kiocb *nxt = NULL;
3731 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003732
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003733 /* if NO_CANCEL is set, we must still run the work */
3734 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
3735 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003736 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003737 }
Jens Axboe31b51512019-01-18 22:56:34 -07003738
Jens Axboe561fb042019-10-24 07:25:42 -06003739 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003740 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3741 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003742 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003743 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003744 /*
3745 * We can get EAGAIN for polled IO even though we're
3746 * forcing a sync submission from here, since we can't
3747 * wait for request slots on the block side.
3748 */
3749 if (ret != -EAGAIN)
3750 break;
3751 cond_resched();
3752 } while (1);
3753 }
Jens Axboe31b51512019-01-18 22:56:34 -07003754
Jens Axboe561fb042019-10-24 07:25:42 -06003755 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003756 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003757
Jens Axboe561fb042019-10-24 07:25:42 -06003758 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003759 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003760 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003761 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003762 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003763
Jens Axboe561fb042019-10-24 07:25:42 -06003764 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07003765 if (!ret && nxt)
3766 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07003767}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003768
Jens Axboe9e3aa612019-12-11 15:55:43 -07003769static bool io_req_op_valid(int op)
3770{
3771 return op >= IORING_OP_NOP && op < IORING_OP_LAST;
3772}
3773
Jens Axboe15b71ab2019-12-11 11:20:36 -07003774static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06003775{
Jens Axboed625c6e2019-12-17 19:53:05 -07003776 switch (req->opcode) {
Jens Axboe09bb8392019-03-13 12:39:28 -06003777 case IORING_OP_NOP:
3778 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03003779 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03003780 case IORING_OP_TIMEOUT_REMOVE:
3781 case IORING_OP_ASYNC_CANCEL:
3782 case IORING_OP_LINK_TIMEOUT:
Jens Axboe9e3aa612019-12-11 15:55:43 -07003783 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003784 case IORING_OP_OPENAT:
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003785 case IORING_OP_STATX:
Jens Axboe15b71ab2019-12-11 11:20:36 -07003786 return fd != -1;
Jens Axboe09bb8392019-03-13 12:39:28 -06003787 default:
Jens Axboed625c6e2019-12-17 19:53:05 -07003788 if (io_req_op_valid(req->opcode))
Jens Axboe9e3aa612019-12-11 15:55:43 -07003789 return 1;
3790 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003791 }
3792}
3793
Jens Axboe65e19f52019-10-26 07:20:21 -06003794static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3795 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003796{
Jens Axboe65e19f52019-10-26 07:20:21 -06003797 struct fixed_file_table *table;
3798
Jens Axboe05f3fb32019-12-09 11:22:50 -07003799 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
3800 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06003801}
3802
Jens Axboe3529d8c2019-12-19 18:24:38 -07003803static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
3804 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06003805{
Jackie Liua197f662019-11-08 08:09:12 -07003806 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003807 unsigned flags;
Jens Axboe9e3aa612019-12-11 15:55:43 -07003808 int fd, ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003809
Jens Axboe3529d8c2019-12-19 18:24:38 -07003810 flags = READ_ONCE(sqe->flags);
3811 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003812
Jackie Liu4fe2c962019-09-09 20:50:40 +08003813 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003814 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003815
Jens Axboe15b71ab2019-12-11 11:20:36 -07003816 ret = io_req_needs_file(req, fd);
Jens Axboe9e3aa612019-12-11 15:55:43 -07003817 if (ret <= 0)
3818 return ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003819
3820 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07003821 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003822 (unsigned) fd >= ctx->nr_user_files))
3823 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003824 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003825 req->file = io_file_from_index(ctx, fd);
3826 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003827 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003828 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003829 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06003830 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003831 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003832 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003833 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003834 req->file = io_file_get(state, fd);
3835 if (unlikely(!req->file))
3836 return -EBADF;
3837 }
3838
3839 return 0;
3840}
3841
Jackie Liua197f662019-11-08 08:09:12 -07003842static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003843{
Jens Axboefcb323c2019-10-24 12:39:47 -06003844 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003845 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003846
Jens Axboeb5dba592019-12-11 14:02:38 -07003847 if (!req->ring_file)
3848 return -EBADF;
3849
Jens Axboefcb323c2019-10-24 12:39:47 -06003850 rcu_read_lock();
3851 spin_lock_irq(&ctx->inflight_lock);
3852 /*
3853 * We use the f_ops->flush() handler to ensure that we can flush
3854 * out work accessing these files if the fd is closed. Check if
3855 * the fd has changed since we started down this path, and disallow
3856 * this operation if it has.
3857 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003858 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003859 list_add(&req->inflight_entry, &ctx->inflight_list);
3860 req->flags |= REQ_F_INFLIGHT;
3861 req->work.files = current->files;
3862 ret = 0;
3863 }
3864 spin_unlock_irq(&ctx->inflight_lock);
3865 rcu_read_unlock();
3866
3867 return ret;
3868}
3869
Jens Axboe2665abf2019-11-05 12:40:47 -07003870static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3871{
Jens Axboead8a48a2019-11-15 08:49:11 -07003872 struct io_timeout_data *data = container_of(timer,
3873 struct io_timeout_data, timer);
3874 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003875 struct io_ring_ctx *ctx = req->ctx;
3876 struct io_kiocb *prev = NULL;
3877 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003878
3879 spin_lock_irqsave(&ctx->completion_lock, flags);
3880
3881 /*
3882 * We don't expect the list to be empty, that will only happen if we
3883 * race with the completion of the linked work.
3884 */
Pavel Begunkov44932332019-12-05 16:16:35 +03003885 if (!list_empty(&req->link_list)) {
3886 prev = list_entry(req->link_list.prev, struct io_kiocb,
3887 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003888 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03003889 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003890 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3891 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003892 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003893 }
3894
3895 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3896
3897 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003898 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003899 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3900 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003901 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003902 } else {
3903 io_cqring_add_event(req, -ETIME);
3904 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003905 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003906 return HRTIMER_NORESTART;
3907}
3908
Jens Axboead8a48a2019-11-15 08:49:11 -07003909static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003910{
Jens Axboe76a46e02019-11-10 23:34:16 -07003911 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07003912
Jens Axboe76a46e02019-11-10 23:34:16 -07003913 /*
3914 * If the list is now empty, then our linked request finished before
3915 * we got a chance to setup the timer
3916 */
3917 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03003918 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07003919 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003920
Jens Axboead8a48a2019-11-15 08:49:11 -07003921 data->timer.function = io_link_timeout_fn;
3922 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3923 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07003924 }
Jens Axboe76a46e02019-11-10 23:34:16 -07003925 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07003926
Jens Axboe2665abf2019-11-05 12:40:47 -07003927 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07003928 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003929}
3930
Jens Axboead8a48a2019-11-15 08:49:11 -07003931static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003932{
3933 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003934
Jens Axboe2665abf2019-11-05 12:40:47 -07003935 if (!(req->flags & REQ_F_LINK))
3936 return NULL;
3937
Pavel Begunkov44932332019-12-05 16:16:35 +03003938 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
3939 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07003940 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07003941 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003942
Jens Axboe76a46e02019-11-10 23:34:16 -07003943 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07003944 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07003945}
3946
Jens Axboe3529d8c2019-12-19 18:24:38 -07003947static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003948{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003949 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003950 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003951 int ret;
3952
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003953again:
3954 linked_timeout = io_prep_linked_timeout(req);
3955
Jens Axboe3529d8c2019-12-19 18:24:38 -07003956 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06003957
3958 /*
3959 * We async punt it if the file wasn't marked NOWAIT, or if the file
3960 * doesn't support non-blocking read/write attempts
3961 */
3962 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3963 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003964 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3965 ret = io_grab_files(req);
3966 if (ret)
3967 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003968 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003969
3970 /*
3971 * Queued up for async execution, worker will release
3972 * submit reference when the iocb is actually submitted.
3973 */
3974 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003975 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003976 }
Jens Axboee65ef562019-03-12 10:16:44 -06003977
Jens Axboefcb323c2019-10-24 12:39:47 -06003978err:
Jens Axboee65ef562019-03-12 10:16:44 -06003979 /* drop submission reference */
3980 io_put_req(req);
3981
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003982 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003983 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003984 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003985 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003986 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003987 }
3988
Jens Axboee65ef562019-03-12 10:16:44 -06003989 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003990 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003991 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003992 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003993 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003994 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003995done_req:
3996 if (nxt) {
3997 req = nxt;
3998 nxt = NULL;
3999 goto again;
4000 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004001}
4002
Jens Axboe3529d8c2019-12-19 18:24:38 -07004003static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004004{
4005 int ret;
4006
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004007 if (unlikely(req->ctx->drain_next)) {
4008 req->flags |= REQ_F_IO_DRAIN;
4009 req->ctx->drain_next = false;
4010 }
4011 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
4012
Jens Axboe3529d8c2019-12-19 18:24:38 -07004013 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004014 if (ret) {
4015 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004016 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004017 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004018 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004019 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07004020 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004021 __io_queue_sqe(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004022}
4023
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004024static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004025{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004026 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004027 io_cqring_add_event(req, -ECANCELED);
4028 io_double_put_req(req);
4029 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004030 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004031}
4032
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004033#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
4034 IOSQE_IO_HARDLINK)
Jens Axboe9e645e112019-05-10 16:07:28 -06004035
Jens Axboe3529d8c2019-12-19 18:24:38 -07004036static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4037 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004038{
Jackie Liua197f662019-11-08 08:09:12 -07004039 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06004040 int ret;
4041
4042 /* enforce forwards compatibility on users */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004043 if (unlikely(sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004044 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004045 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004046 }
4047
Jens Axboe3529d8c2019-12-19 18:24:38 -07004048 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004049 if (unlikely(ret)) {
4050err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004051 io_cqring_add_event(req, ret);
4052 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004053 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004054 }
4055
Jens Axboe9e645e112019-05-10 16:07:28 -06004056 /*
4057 * If we already have a head request, queue this one for async
4058 * submittal once the head completes. If we don't have a head but
4059 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4060 * submitted sync once the chain is complete. If none of those
4061 * conditions are true (normal request), then just queue it.
4062 */
4063 if (*link) {
4064 struct io_kiocb *prev = *link;
4065
Jens Axboe3529d8c2019-12-19 18:24:38 -07004066 if (sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004067 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
4068
Jens Axboe3529d8c2019-12-19 18:24:38 -07004069 if (sqe->flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004070 req->flags |= REQ_F_HARDLINK;
4071
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004072 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004073 ret = -EAGAIN;
4074 goto err_req;
4075 }
4076
Jens Axboe3529d8c2019-12-19 18:24:38 -07004077 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004078 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004079 /* fail even hard links since we don't submit */
Jens Axboe2d283902019-12-04 11:08:05 -07004080 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004081 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004082 }
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004083 trace_io_uring_link(ctx, req, prev);
Pavel Begunkov44932332019-12-05 16:16:35 +03004084 list_add_tail(&req->link_list, &prev->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004085 } else if (sqe->flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004086 req->flags |= REQ_F_LINK;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004087 if (sqe->flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004088 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004089
Jens Axboe9e645e112019-05-10 16:07:28 -06004090 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004091 ret = io_req_defer_prep(req, sqe);
4092 if (ret)
4093 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004094 *link = req;
4095 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004096 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004097 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004098
4099 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004100}
4101
Jens Axboe9a56a232019-01-09 09:06:50 -07004102/*
4103 * Batched submission is done, ensure local IO is flushed out.
4104 */
4105static void io_submit_state_end(struct io_submit_state *state)
4106{
4107 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004108 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004109 if (state->free_reqs)
4110 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4111 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004112}
4113
4114/*
4115 * Start submission side cache.
4116 */
4117static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004118 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004119{
4120 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004121 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004122 state->file = NULL;
4123 state->ios_left = max_ios;
4124}
4125
Jens Axboe2b188cc2019-01-07 10:46:33 -07004126static void io_commit_sqring(struct io_ring_ctx *ctx)
4127{
Hristo Venev75b28af2019-08-26 17:23:46 +00004128 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004129
Hristo Venev75b28af2019-08-26 17:23:46 +00004130 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004131 /*
4132 * Ensure any loads from the SQEs are done at this point,
4133 * since once we write the new head, the application could
4134 * write new data to them.
4135 */
Hristo Venev75b28af2019-08-26 17:23:46 +00004136 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004137 }
4138}
4139
4140/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004141 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004142 * that is mapped by userspace. This means that care needs to be taken to
4143 * ensure that reads are stable, as we cannot rely on userspace always
4144 * being a good citizen. If members of the sqe are validated and then later
4145 * used, it's important that those reads are done through READ_ONCE() to
4146 * prevent a re-load down the line.
4147 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004148static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4149 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004150{
Hristo Venev75b28af2019-08-26 17:23:46 +00004151 struct io_rings *rings = ctx->rings;
4152 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004153 unsigned head;
4154
4155 /*
4156 * The cached sq head (or cq tail) serves two purposes:
4157 *
4158 * 1) allows us to batch the cost of updating the user visible
4159 * head updates.
4160 * 2) allows the kernel side to track the head on its own, even
4161 * though the application is the one updating it.
4162 */
4163 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02004164 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004165 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004166 return false;
4167
Hristo Venev75b28af2019-08-26 17:23:46 +00004168 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004169 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004170 /*
4171 * All io need record the previous position, if LINK vs DARIN,
4172 * it can be used to mark the position of the first IO in the
4173 * link list.
4174 */
4175 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004176 *sqe_ptr = &ctx->sq_sqes[head];
4177 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4178 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004179 ctx->cached_sq_head++;
4180 return true;
4181 }
4182
4183 /* drop invalid entries */
4184 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004185 ctx->cached_sq_dropped++;
4186 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004187 return false;
4188}
4189
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004190static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004191 struct file *ring_file, int ring_fd,
4192 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004193{
4194 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004195 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004196 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004197 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004198
Jens Axboec4a2ed72019-11-21 21:01:26 -07004199 /* if we have a backlog and couldn't flush it all, return BUSY */
4200 if (!list_empty(&ctx->cq_overflow_list) &&
4201 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004202 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004203
4204 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004205 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004206 statep = &state;
4207 }
4208
4209 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004210 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004211 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03004212 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004213
Pavel Begunkov196be952019-11-07 01:41:06 +03004214 req = io_get_req(ctx, statep);
4215 if (unlikely(!req)) {
4216 if (!submitted)
4217 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004218 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004219 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004220 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03004221 __io_free_req(req);
4222 break;
4223 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004224
Jens Axboed625c6e2019-12-17 19:53:05 -07004225 if (io_req_needs_user(req) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004226 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4227 if (!mm_fault) {
4228 use_mm(ctx->sqo_mm);
4229 *mm = ctx->sqo_mm;
4230 }
4231 }
4232
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004233 submitted++;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004234 sqe_flags = sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03004235
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004236 req->ring_file = ring_file;
4237 req->ring_fd = ring_fd;
4238 req->has_user = *mm != NULL;
4239 req->in_async = async;
4240 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07004241 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004242 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004243 break;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03004244 /*
4245 * If previous wasn't linked and we have a linked command,
4246 * that's the end of the chain. Submit the previous link.
4247 */
Pavel Begunkovffbb8d62019-12-17 20:57:05 +03004248 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004249 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03004250 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004251 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004252 }
4253
Jens Axboe9e645e112019-05-10 16:07:28 -06004254 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004255 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004256 if (statep)
4257 io_submit_state_end(&state);
4258
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004259 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4260 io_commit_sqring(ctx);
4261
Jens Axboe6c271ce2019-01-10 11:22:30 -07004262 return submitted;
4263}
4264
4265static int io_sq_thread(void *data)
4266{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004267 struct io_ring_ctx *ctx = data;
4268 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004269 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004270 mm_segment_t old_fs;
4271 DEFINE_WAIT(wait);
4272 unsigned inflight;
4273 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004274 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004275
Jens Axboe206aefd2019-11-07 18:27:42 -07004276 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004277
Jens Axboe6c271ce2019-01-10 11:22:30 -07004278 old_fs = get_fs();
4279 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004280 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004281
Jens Axboec1edbf52019-11-10 16:56:04 -07004282 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004283 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004284 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004285
4286 if (inflight) {
4287 unsigned nr_events = 0;
4288
4289 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004290 /*
4291 * inflight is the count of the maximum possible
4292 * entries we submitted, but it can be smaller
4293 * if we dropped some of them. If we don't have
4294 * poll entries available, then we know that we
4295 * have nothing left to poll for. Reset the
4296 * inflight count to zero in that case.
4297 */
4298 mutex_lock(&ctx->uring_lock);
4299 if (!list_empty(&ctx->poll_list))
4300 __io_iopoll_check(ctx, &nr_events, 0);
4301 else
4302 inflight = 0;
4303 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004304 } else {
4305 /*
4306 * Normal IO, just pretend everything completed.
4307 * We don't have to poll completions for that.
4308 */
4309 nr_events = inflight;
4310 }
4311
4312 inflight -= nr_events;
4313 if (!inflight)
4314 timeout = jiffies + ctx->sq_thread_idle;
4315 }
4316
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004317 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004318
4319 /*
4320 * If submit got -EBUSY, flag us as needing the application
4321 * to enter the kernel to reap and flush events.
4322 */
4323 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004324 /*
4325 * We're polling. If we're within the defined idle
4326 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004327 * to sleep. The exception is if we got EBUSY doing
4328 * more IO, we should wait for the application to
4329 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004330 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004331 if (inflight ||
4332 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004333 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004334 continue;
4335 }
4336
4337 /*
4338 * Drop cur_mm before scheduling, we can't hold it for
4339 * long periods (or over schedule()). Do this before
4340 * adding ourselves to the waitqueue, as the unuse/drop
4341 * may sleep.
4342 */
4343 if (cur_mm) {
4344 unuse_mm(cur_mm);
4345 mmput(cur_mm);
4346 cur_mm = NULL;
4347 }
4348
4349 prepare_to_wait(&ctx->sqo_wait, &wait,
4350 TASK_INTERRUPTIBLE);
4351
4352 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004353 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004354 /* make sure to read SQ tail after writing flags */
4355 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004356
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004357 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004358 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004359 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004360 finish_wait(&ctx->sqo_wait, &wait);
4361 break;
4362 }
4363 if (signal_pending(current))
4364 flush_signals(current);
4365 schedule();
4366 finish_wait(&ctx->sqo_wait, &wait);
4367
Hristo Venev75b28af2019-08-26 17:23:46 +00004368 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004369 continue;
4370 }
4371 finish_wait(&ctx->sqo_wait, &wait);
4372
Hristo Venev75b28af2019-08-26 17:23:46 +00004373 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004374 }
4375
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004376 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004377 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004378 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004379 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004380 if (ret > 0)
4381 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004382 }
4383
4384 set_fs(old_fs);
4385 if (cur_mm) {
4386 unuse_mm(cur_mm);
4387 mmput(cur_mm);
4388 }
Jens Axboe181e4482019-11-25 08:52:30 -07004389 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004390
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004391 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004392
Jens Axboe6c271ce2019-01-10 11:22:30 -07004393 return 0;
4394}
4395
Jens Axboebda52162019-09-24 13:47:15 -06004396struct io_wait_queue {
4397 struct wait_queue_entry wq;
4398 struct io_ring_ctx *ctx;
4399 unsigned to_wait;
4400 unsigned nr_timeouts;
4401};
4402
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004403static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004404{
4405 struct io_ring_ctx *ctx = iowq->ctx;
4406
4407 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004408 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004409 * started waiting. For timeouts, we always want to return to userspace,
4410 * regardless of event count.
4411 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004412 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004413 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4414}
4415
4416static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4417 int wake_flags, void *key)
4418{
4419 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4420 wq);
4421
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004422 /* use noflush == true, as we can't safely rely on locking context */
4423 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004424 return -1;
4425
4426 return autoremove_wake_function(curr, mode, wake_flags, key);
4427}
4428
Jens Axboe2b188cc2019-01-07 10:46:33 -07004429/*
4430 * Wait until events become available, if we don't already have some. The
4431 * application must reap them itself, as they reside on the shared cq ring.
4432 */
4433static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4434 const sigset_t __user *sig, size_t sigsz)
4435{
Jens Axboebda52162019-09-24 13:47:15 -06004436 struct io_wait_queue iowq = {
4437 .wq = {
4438 .private = current,
4439 .func = io_wake_function,
4440 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4441 },
4442 .ctx = ctx,
4443 .to_wait = min_events,
4444 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004445 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004446 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004447
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004448 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004449 return 0;
4450
4451 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004452#ifdef CONFIG_COMPAT
4453 if (in_compat_syscall())
4454 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004455 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004456 else
4457#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004458 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004459
Jens Axboe2b188cc2019-01-07 10:46:33 -07004460 if (ret)
4461 return ret;
4462 }
4463
Jens Axboebda52162019-09-24 13:47:15 -06004464 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004465 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004466 do {
4467 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4468 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004469 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004470 break;
4471 schedule();
4472 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004473 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004474 break;
4475 }
4476 } while (1);
4477 finish_wait(&ctx->wait, &iowq.wq);
4478
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004479 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004480
Hristo Venev75b28af2019-08-26 17:23:46 +00004481 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004482}
4483
Jens Axboe6b063142019-01-10 22:13:58 -07004484static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4485{
4486#if defined(CONFIG_UNIX)
4487 if (ctx->ring_sock) {
4488 struct sock *sock = ctx->ring_sock->sk;
4489 struct sk_buff *skb;
4490
4491 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4492 kfree_skb(skb);
4493 }
4494#else
4495 int i;
4496
Jens Axboe65e19f52019-10-26 07:20:21 -06004497 for (i = 0; i < ctx->nr_user_files; i++) {
4498 struct file *file;
4499
4500 file = io_file_from_index(ctx, i);
4501 if (file)
4502 fput(file);
4503 }
Jens Axboe6b063142019-01-10 22:13:58 -07004504#endif
4505}
4506
Jens Axboe05f3fb32019-12-09 11:22:50 -07004507static void io_file_ref_kill(struct percpu_ref *ref)
4508{
4509 struct fixed_file_data *data;
4510
4511 data = container_of(ref, struct fixed_file_data, refs);
4512 complete(&data->done);
4513}
4514
Jens Axboe6b063142019-01-10 22:13:58 -07004515static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4516{
Jens Axboe05f3fb32019-12-09 11:22:50 -07004517 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06004518 unsigned nr_tables, i;
4519
Jens Axboe05f3fb32019-12-09 11:22:50 -07004520 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07004521 return -ENXIO;
4522
Jens Axboe05f3fb32019-12-09 11:22:50 -07004523 /* protect against inflight atomic switch, which drops the ref */
4524 flush_work(&data->ref_work);
4525 percpu_ref_get(&data->refs);
4526 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
4527 wait_for_completion(&data->done);
4528 percpu_ref_put(&data->refs);
4529 percpu_ref_exit(&data->refs);
4530
Jens Axboe6b063142019-01-10 22:13:58 -07004531 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004532 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4533 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004534 kfree(data->table[i].files);
4535 kfree(data->table);
4536 kfree(data);
4537 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004538 ctx->nr_user_files = 0;
4539 return 0;
4540}
4541
Jens Axboe6c271ce2019-01-10 11:22:30 -07004542static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4543{
4544 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004545 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004546 /*
4547 * The park is a bit of a work-around, without it we get
4548 * warning spews on shutdown with SQPOLL set and affinity
4549 * set to a single CPU.
4550 */
Jens Axboe06058632019-04-13 09:26:03 -06004551 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004552 kthread_stop(ctx->sqo_thread);
4553 ctx->sqo_thread = NULL;
4554 }
4555}
4556
Jens Axboe6b063142019-01-10 22:13:58 -07004557static void io_finish_async(struct io_ring_ctx *ctx)
4558{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004559 io_sq_thread_stop(ctx);
4560
Jens Axboe561fb042019-10-24 07:25:42 -06004561 if (ctx->io_wq) {
4562 io_wq_destroy(ctx->io_wq);
4563 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004564 }
4565}
4566
4567#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07004568/*
4569 * Ensure the UNIX gc is aware of our file set, so we are certain that
4570 * the io_uring can be safely unregistered on process exit, even if we have
4571 * loops in the file referencing.
4572 */
4573static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4574{
4575 struct sock *sk = ctx->ring_sock->sk;
4576 struct scm_fp_list *fpl;
4577 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004578 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004579
4580 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4581 unsigned long inflight = ctx->user->unix_inflight + nr;
4582
4583 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4584 return -EMFILE;
4585 }
4586
4587 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4588 if (!fpl)
4589 return -ENOMEM;
4590
4591 skb = alloc_skb(0, GFP_KERNEL);
4592 if (!skb) {
4593 kfree(fpl);
4594 return -ENOMEM;
4595 }
4596
4597 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004598
Jens Axboe08a45172019-10-03 08:11:03 -06004599 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004600 fpl->user = get_uid(ctx->user);
4601 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004602 struct file *file = io_file_from_index(ctx, i + offset);
4603
4604 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004605 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004606 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004607 unix_inflight(fpl->user, fpl->fp[nr_files]);
4608 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004609 }
4610
Jens Axboe08a45172019-10-03 08:11:03 -06004611 if (nr_files) {
4612 fpl->max = SCM_MAX_FD;
4613 fpl->count = nr_files;
4614 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004615 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06004616 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4617 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004618
Jens Axboe08a45172019-10-03 08:11:03 -06004619 for (i = 0; i < nr_files; i++)
4620 fput(fpl->fp[i]);
4621 } else {
4622 kfree_skb(skb);
4623 kfree(fpl);
4624 }
Jens Axboe6b063142019-01-10 22:13:58 -07004625
4626 return 0;
4627}
4628
4629/*
4630 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4631 * causes regular reference counting to break down. We rely on the UNIX
4632 * garbage collection to take care of this problem for us.
4633 */
4634static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4635{
4636 unsigned left, total;
4637 int ret = 0;
4638
4639 total = 0;
4640 left = ctx->nr_user_files;
4641 while (left) {
4642 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004643
4644 ret = __io_sqe_files_scm(ctx, this_files, total);
4645 if (ret)
4646 break;
4647 left -= this_files;
4648 total += this_files;
4649 }
4650
4651 if (!ret)
4652 return 0;
4653
4654 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004655 struct file *file = io_file_from_index(ctx, total);
4656
4657 if (file)
4658 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004659 total++;
4660 }
4661
4662 return ret;
4663}
4664#else
4665static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4666{
4667 return 0;
4668}
4669#endif
4670
Jens Axboe65e19f52019-10-26 07:20:21 -06004671static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4672 unsigned nr_files)
4673{
4674 int i;
4675
4676 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004677 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004678 unsigned this_files;
4679
4680 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4681 table->files = kcalloc(this_files, sizeof(struct file *),
4682 GFP_KERNEL);
4683 if (!table->files)
4684 break;
4685 nr_files -= this_files;
4686 }
4687
4688 if (i == nr_tables)
4689 return 0;
4690
4691 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004692 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004693 kfree(table->files);
4694 }
4695 return 1;
4696}
4697
Jens Axboe05f3fb32019-12-09 11:22:50 -07004698static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06004699{
4700#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06004701 struct sock *sock = ctx->ring_sock->sk;
4702 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4703 struct sk_buff *skb;
4704 int i;
4705
4706 __skb_queue_head_init(&list);
4707
4708 /*
4709 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4710 * remove this entry and rearrange the file array.
4711 */
4712 skb = skb_dequeue(head);
4713 while (skb) {
4714 struct scm_fp_list *fp;
4715
4716 fp = UNIXCB(skb).fp;
4717 for (i = 0; i < fp->count; i++) {
4718 int left;
4719
4720 if (fp->fp[i] != file)
4721 continue;
4722
4723 unix_notinflight(fp->user, fp->fp[i]);
4724 left = fp->count - 1 - i;
4725 if (left) {
4726 memmove(&fp->fp[i], &fp->fp[i + 1],
4727 left * sizeof(struct file *));
4728 }
4729 fp->count--;
4730 if (!fp->count) {
4731 kfree_skb(skb);
4732 skb = NULL;
4733 } else {
4734 __skb_queue_tail(&list, skb);
4735 }
4736 fput(file);
4737 file = NULL;
4738 break;
4739 }
4740
4741 if (!file)
4742 break;
4743
4744 __skb_queue_tail(&list, skb);
4745
4746 skb = skb_dequeue(head);
4747 }
4748
4749 if (skb_peek(&list)) {
4750 spin_lock_irq(&head->lock);
4751 while ((skb = __skb_dequeue(&list)) != NULL)
4752 __skb_queue_tail(head, skb);
4753 spin_unlock_irq(&head->lock);
4754 }
4755#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07004756 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06004757#endif
4758}
4759
Jens Axboe05f3fb32019-12-09 11:22:50 -07004760struct io_file_put {
4761 struct llist_node llist;
4762 struct file *file;
4763 struct completion *done;
4764};
4765
4766static void io_ring_file_ref_switch(struct work_struct *work)
4767{
4768 struct io_file_put *pfile, *tmp;
4769 struct fixed_file_data *data;
4770 struct llist_node *node;
4771
4772 data = container_of(work, struct fixed_file_data, ref_work);
4773
4774 while ((node = llist_del_all(&data->put_llist)) != NULL) {
4775 llist_for_each_entry_safe(pfile, tmp, node, llist) {
4776 io_ring_file_put(data->ctx, pfile->file);
4777 if (pfile->done)
4778 complete(pfile->done);
4779 else
4780 kfree(pfile);
4781 }
4782 }
4783
4784 percpu_ref_get(&data->refs);
4785 percpu_ref_switch_to_percpu(&data->refs);
4786}
4787
4788static void io_file_data_ref_zero(struct percpu_ref *ref)
4789{
4790 struct fixed_file_data *data;
4791
4792 data = container_of(ref, struct fixed_file_data, refs);
4793
4794 /* we can't safely switch from inside this context, punt to wq */
4795 queue_work(system_wq, &data->ref_work);
4796}
4797
4798static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
4799 unsigned nr_args)
4800{
4801 __s32 __user *fds = (__s32 __user *) arg;
4802 unsigned nr_tables;
4803 struct file *file;
4804 int fd, ret = 0;
4805 unsigned i;
4806
4807 if (ctx->file_data)
4808 return -EBUSY;
4809 if (!nr_args)
4810 return -EINVAL;
4811 if (nr_args > IORING_MAX_FIXED_FILES)
4812 return -EMFILE;
4813
4814 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
4815 if (!ctx->file_data)
4816 return -ENOMEM;
4817 ctx->file_data->ctx = ctx;
4818 init_completion(&ctx->file_data->done);
4819
4820 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
4821 ctx->file_data->table = kcalloc(nr_tables,
4822 sizeof(struct fixed_file_table),
4823 GFP_KERNEL);
4824 if (!ctx->file_data->table) {
4825 kfree(ctx->file_data);
4826 ctx->file_data = NULL;
4827 return -ENOMEM;
4828 }
4829
4830 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
4831 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
4832 kfree(ctx->file_data->table);
4833 kfree(ctx->file_data);
4834 ctx->file_data = NULL;
4835 return -ENOMEM;
4836 }
4837 ctx->file_data->put_llist.first = NULL;
4838 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
4839
4840 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
4841 percpu_ref_exit(&ctx->file_data->refs);
4842 kfree(ctx->file_data->table);
4843 kfree(ctx->file_data);
4844 ctx->file_data = NULL;
4845 return -ENOMEM;
4846 }
4847
4848 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
4849 struct fixed_file_table *table;
4850 unsigned index;
4851
4852 ret = -EFAULT;
4853 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
4854 break;
4855 /* allow sparse sets */
4856 if (fd == -1) {
4857 ret = 0;
4858 continue;
4859 }
4860
4861 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
4862 index = i & IORING_FILE_TABLE_MASK;
4863 file = fget(fd);
4864
4865 ret = -EBADF;
4866 if (!file)
4867 break;
4868
4869 /*
4870 * Don't allow io_uring instances to be registered. If UNIX
4871 * isn't enabled, then this causes a reference cycle and this
4872 * instance can never get freed. If UNIX is enabled we'll
4873 * handle it just fine, but there's still no point in allowing
4874 * a ring fd as it doesn't support regular read/write anyway.
4875 */
4876 if (file->f_op == &io_uring_fops) {
4877 fput(file);
4878 break;
4879 }
4880 ret = 0;
4881 table->files[index] = file;
4882 }
4883
4884 if (ret) {
4885 for (i = 0; i < ctx->nr_user_files; i++) {
4886 file = io_file_from_index(ctx, i);
4887 if (file)
4888 fput(file);
4889 }
4890 for (i = 0; i < nr_tables; i++)
4891 kfree(ctx->file_data->table[i].files);
4892
4893 kfree(ctx->file_data->table);
4894 kfree(ctx->file_data);
4895 ctx->file_data = NULL;
4896 ctx->nr_user_files = 0;
4897 return ret;
4898 }
4899
4900 ret = io_sqe_files_scm(ctx);
4901 if (ret)
4902 io_sqe_files_unregister(ctx);
4903
4904 return ret;
4905}
4906
Jens Axboec3a31e62019-10-03 13:59:56 -06004907static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4908 int index)
4909{
4910#if defined(CONFIG_UNIX)
4911 struct sock *sock = ctx->ring_sock->sk;
4912 struct sk_buff_head *head = &sock->sk_receive_queue;
4913 struct sk_buff *skb;
4914
4915 /*
4916 * See if we can merge this file into an existing skb SCM_RIGHTS
4917 * file set. If there's no room, fall back to allocating a new skb
4918 * and filling it in.
4919 */
4920 spin_lock_irq(&head->lock);
4921 skb = skb_peek(head);
4922 if (skb) {
4923 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4924
4925 if (fpl->count < SCM_MAX_FD) {
4926 __skb_unlink(skb, head);
4927 spin_unlock_irq(&head->lock);
4928 fpl->fp[fpl->count] = get_file(file);
4929 unix_inflight(fpl->user, fpl->fp[fpl->count]);
4930 fpl->count++;
4931 spin_lock_irq(&head->lock);
4932 __skb_queue_head(head, skb);
4933 } else {
4934 skb = NULL;
4935 }
4936 }
4937 spin_unlock_irq(&head->lock);
4938
4939 if (skb) {
4940 fput(file);
4941 return 0;
4942 }
4943
4944 return __io_sqe_files_scm(ctx, 1, index);
4945#else
4946 return 0;
4947#endif
4948}
4949
Jens Axboe05f3fb32019-12-09 11:22:50 -07004950static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06004951{
Jens Axboe05f3fb32019-12-09 11:22:50 -07004952 struct fixed_file_data *data;
4953
4954 data = container_of(ref, struct fixed_file_data, refs);
4955 clear_bit(FFD_F_ATOMIC, &data->state);
4956}
4957
4958static bool io_queue_file_removal(struct fixed_file_data *data,
4959 struct file *file)
4960{
4961 struct io_file_put *pfile, pfile_stack;
4962 DECLARE_COMPLETION_ONSTACK(done);
4963
4964 /*
4965 * If we fail allocating the struct we need for doing async reomval
4966 * of this file, just punt to sync and wait for it.
4967 */
4968 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
4969 if (!pfile) {
4970 pfile = &pfile_stack;
4971 pfile->done = &done;
4972 }
4973
4974 pfile->file = file;
4975 llist_add(&pfile->llist, &data->put_llist);
4976
4977 if (pfile == &pfile_stack) {
4978 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
4979 percpu_ref_put(&data->refs);
4980 percpu_ref_switch_to_atomic(&data->refs,
4981 io_atomic_switch);
4982 }
4983 wait_for_completion(&done);
4984 flush_work(&data->ref_work);
4985 return false;
4986 }
4987
4988 return true;
4989}
4990
4991static int __io_sqe_files_update(struct io_ring_ctx *ctx,
4992 struct io_uring_files_update *up,
4993 unsigned nr_args)
4994{
4995 struct fixed_file_data *data = ctx->file_data;
4996 bool ref_switch = false;
4997 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06004998 __s32 __user *fds;
4999 int fd, i, err;
5000 __u32 done;
5001
Jens Axboe05f3fb32019-12-09 11:22:50 -07005002 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005003 return -EOVERFLOW;
5004 if (done > ctx->nr_user_files)
5005 return -EINVAL;
5006
5007 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005008 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005009 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005010 struct fixed_file_table *table;
5011 unsigned index;
5012
Jens Axboec3a31e62019-10-03 13:59:56 -06005013 err = 0;
5014 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5015 err = -EFAULT;
5016 break;
5017 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005018 i = array_index_nospec(up->offset, ctx->nr_user_files);
5019 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005020 index = i & IORING_FILE_TABLE_MASK;
5021 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005022 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005023 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005024 if (io_queue_file_removal(data, file))
5025 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005026 }
5027 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005028 file = fget(fd);
5029 if (!file) {
5030 err = -EBADF;
5031 break;
5032 }
5033 /*
5034 * Don't allow io_uring instances to be registered. If
5035 * UNIX isn't enabled, then this causes a reference
5036 * cycle and this instance can never get freed. If UNIX
5037 * is enabled we'll handle it just fine, but there's
5038 * still no point in allowing a ring fd as it doesn't
5039 * support regular read/write anyway.
5040 */
5041 if (file->f_op == &io_uring_fops) {
5042 fput(file);
5043 err = -EBADF;
5044 break;
5045 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005046 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005047 err = io_sqe_file_register(ctx, file, i);
5048 if (err)
5049 break;
5050 }
5051 nr_args--;
5052 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005053 up->offset++;
5054 }
5055
5056 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5057 percpu_ref_put(&data->refs);
5058 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005059 }
5060
5061 return done ? done : err;
5062}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005063static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5064 unsigned nr_args)
5065{
5066 struct io_uring_files_update up;
5067
5068 if (!ctx->file_data)
5069 return -ENXIO;
5070 if (!nr_args)
5071 return -EINVAL;
5072 if (copy_from_user(&up, arg, sizeof(up)))
5073 return -EFAULT;
5074 if (up.resv)
5075 return -EINVAL;
5076
5077 return __io_sqe_files_update(ctx, &up, nr_args);
5078}
Jens Axboec3a31e62019-10-03 13:59:56 -06005079
Jens Axboe7d723062019-11-12 22:31:31 -07005080static void io_put_work(struct io_wq_work *work)
5081{
5082 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5083
5084 io_put_req(req);
5085}
5086
5087static void io_get_work(struct io_wq_work *work)
5088{
5089 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5090
5091 refcount_inc(&req->refs);
5092}
5093
Jens Axboe6c271ce2019-01-10 11:22:30 -07005094static int io_sq_offload_start(struct io_ring_ctx *ctx,
5095 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005096{
Jens Axboe576a3472019-11-25 08:49:20 -07005097 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005098 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005099 int ret;
5100
Jens Axboe6c271ce2019-01-10 11:22:30 -07005101 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005102 mmgrab(current->mm);
5103 ctx->sqo_mm = current->mm;
5104
Jens Axboe6c271ce2019-01-10 11:22:30 -07005105 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005106 ret = -EPERM;
5107 if (!capable(CAP_SYS_ADMIN))
5108 goto err;
5109
Jens Axboe917257d2019-04-13 09:28:55 -06005110 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5111 if (!ctx->sq_thread_idle)
5112 ctx->sq_thread_idle = HZ;
5113
Jens Axboe6c271ce2019-01-10 11:22:30 -07005114 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005115 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005116
Jens Axboe917257d2019-04-13 09:28:55 -06005117 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005118 if (cpu >= nr_cpu_ids)
5119 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005120 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005121 goto err;
5122
Jens Axboe6c271ce2019-01-10 11:22:30 -07005123 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5124 ctx, cpu,
5125 "io_uring-sq");
5126 } else {
5127 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5128 "io_uring-sq");
5129 }
5130 if (IS_ERR(ctx->sqo_thread)) {
5131 ret = PTR_ERR(ctx->sqo_thread);
5132 ctx->sqo_thread = NULL;
5133 goto err;
5134 }
5135 wake_up_process(ctx->sqo_thread);
5136 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5137 /* Can't have SQ_AFF without SQPOLL */
5138 ret = -EINVAL;
5139 goto err;
5140 }
5141
Jens Axboe576a3472019-11-25 08:49:20 -07005142 data.mm = ctx->sqo_mm;
5143 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005144 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005145 data.get_work = io_get_work;
5146 data.put_work = io_put_work;
5147
Jens Axboe561fb042019-10-24 07:25:42 -06005148 /* Do QD, or 4 * CPUS, whatever is smallest */
5149 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005150 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005151 if (IS_ERR(ctx->io_wq)) {
5152 ret = PTR_ERR(ctx->io_wq);
5153 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005154 goto err;
5155 }
5156
5157 return 0;
5158err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005159 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005160 mmdrop(ctx->sqo_mm);
5161 ctx->sqo_mm = NULL;
5162 return ret;
5163}
5164
5165static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5166{
5167 atomic_long_sub(nr_pages, &user->locked_vm);
5168}
5169
5170static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5171{
5172 unsigned long page_limit, cur_pages, new_pages;
5173
5174 /* Don't allow more pages than we can safely lock */
5175 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5176
5177 do {
5178 cur_pages = atomic_long_read(&user->locked_vm);
5179 new_pages = cur_pages + nr_pages;
5180 if (new_pages > page_limit)
5181 return -ENOMEM;
5182 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5183 new_pages) != cur_pages);
5184
5185 return 0;
5186}
5187
5188static void io_mem_free(void *ptr)
5189{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005190 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005191
Mark Rutland52e04ef2019-04-30 17:30:21 +01005192 if (!ptr)
5193 return;
5194
5195 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005196 if (put_page_testzero(page))
5197 free_compound_page(page);
5198}
5199
5200static void *io_mem_alloc(size_t size)
5201{
5202 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5203 __GFP_NORETRY;
5204
5205 return (void *) __get_free_pages(gfp_flags, get_order(size));
5206}
5207
Hristo Venev75b28af2019-08-26 17:23:46 +00005208static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5209 size_t *sq_offset)
5210{
5211 struct io_rings *rings;
5212 size_t off, sq_array_size;
5213
5214 off = struct_size(rings, cqes, cq_entries);
5215 if (off == SIZE_MAX)
5216 return SIZE_MAX;
5217
5218#ifdef CONFIG_SMP
5219 off = ALIGN(off, SMP_CACHE_BYTES);
5220 if (off == 0)
5221 return SIZE_MAX;
5222#endif
5223
5224 sq_array_size = array_size(sizeof(u32), sq_entries);
5225 if (sq_array_size == SIZE_MAX)
5226 return SIZE_MAX;
5227
5228 if (check_add_overflow(off, sq_array_size, &off))
5229 return SIZE_MAX;
5230
5231 if (sq_offset)
5232 *sq_offset = off;
5233
5234 return off;
5235}
5236
Jens Axboe2b188cc2019-01-07 10:46:33 -07005237static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5238{
Hristo Venev75b28af2019-08-26 17:23:46 +00005239 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005240
Hristo Venev75b28af2019-08-26 17:23:46 +00005241 pages = (size_t)1 << get_order(
5242 rings_size(sq_entries, cq_entries, NULL));
5243 pages += (size_t)1 << get_order(
5244 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005245
Hristo Venev75b28af2019-08-26 17:23:46 +00005246 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005247}
5248
Jens Axboeedafcce2019-01-09 09:16:05 -07005249static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5250{
5251 int i, j;
5252
5253 if (!ctx->user_bufs)
5254 return -ENXIO;
5255
5256 for (i = 0; i < ctx->nr_user_bufs; i++) {
5257 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5258
5259 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005260 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005261
5262 if (ctx->account_mem)
5263 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005264 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005265 imu->nr_bvecs = 0;
5266 }
5267
5268 kfree(ctx->user_bufs);
5269 ctx->user_bufs = NULL;
5270 ctx->nr_user_bufs = 0;
5271 return 0;
5272}
5273
5274static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5275 void __user *arg, unsigned index)
5276{
5277 struct iovec __user *src;
5278
5279#ifdef CONFIG_COMPAT
5280 if (ctx->compat) {
5281 struct compat_iovec __user *ciovs;
5282 struct compat_iovec ciov;
5283
5284 ciovs = (struct compat_iovec __user *) arg;
5285 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5286 return -EFAULT;
5287
Jens Axboed55e5f52019-12-11 16:12:15 -07005288 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005289 dst->iov_len = ciov.iov_len;
5290 return 0;
5291 }
5292#endif
5293 src = (struct iovec __user *) arg;
5294 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5295 return -EFAULT;
5296 return 0;
5297}
5298
5299static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5300 unsigned nr_args)
5301{
5302 struct vm_area_struct **vmas = NULL;
5303 struct page **pages = NULL;
5304 int i, j, got_pages = 0;
5305 int ret = -EINVAL;
5306
5307 if (ctx->user_bufs)
5308 return -EBUSY;
5309 if (!nr_args || nr_args > UIO_MAXIOV)
5310 return -EINVAL;
5311
5312 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5313 GFP_KERNEL);
5314 if (!ctx->user_bufs)
5315 return -ENOMEM;
5316
5317 for (i = 0; i < nr_args; i++) {
5318 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5319 unsigned long off, start, end, ubuf;
5320 int pret, nr_pages;
5321 struct iovec iov;
5322 size_t size;
5323
5324 ret = io_copy_iov(ctx, &iov, arg, i);
5325 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005326 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005327
5328 /*
5329 * Don't impose further limits on the size and buffer
5330 * constraints here, we'll -EINVAL later when IO is
5331 * submitted if they are wrong.
5332 */
5333 ret = -EFAULT;
5334 if (!iov.iov_base || !iov.iov_len)
5335 goto err;
5336
5337 /* arbitrary limit, but we need something */
5338 if (iov.iov_len > SZ_1G)
5339 goto err;
5340
5341 ubuf = (unsigned long) iov.iov_base;
5342 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5343 start = ubuf >> PAGE_SHIFT;
5344 nr_pages = end - start;
5345
5346 if (ctx->account_mem) {
5347 ret = io_account_mem(ctx->user, nr_pages);
5348 if (ret)
5349 goto err;
5350 }
5351
5352 ret = 0;
5353 if (!pages || nr_pages > got_pages) {
5354 kfree(vmas);
5355 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005356 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005357 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005358 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005359 sizeof(struct vm_area_struct *),
5360 GFP_KERNEL);
5361 if (!pages || !vmas) {
5362 ret = -ENOMEM;
5363 if (ctx->account_mem)
5364 io_unaccount_mem(ctx->user, nr_pages);
5365 goto err;
5366 }
5367 got_pages = nr_pages;
5368 }
5369
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005370 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005371 GFP_KERNEL);
5372 ret = -ENOMEM;
5373 if (!imu->bvec) {
5374 if (ctx->account_mem)
5375 io_unaccount_mem(ctx->user, nr_pages);
5376 goto err;
5377 }
5378
5379 ret = 0;
5380 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005381 pret = get_user_pages(ubuf, nr_pages,
5382 FOLL_WRITE | FOLL_LONGTERM,
5383 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005384 if (pret == nr_pages) {
5385 /* don't support file backed memory */
5386 for (j = 0; j < nr_pages; j++) {
5387 struct vm_area_struct *vma = vmas[j];
5388
5389 if (vma->vm_file &&
5390 !is_file_hugepages(vma->vm_file)) {
5391 ret = -EOPNOTSUPP;
5392 break;
5393 }
5394 }
5395 } else {
5396 ret = pret < 0 ? pret : -EFAULT;
5397 }
5398 up_read(&current->mm->mmap_sem);
5399 if (ret) {
5400 /*
5401 * if we did partial map, or found file backed vmas,
5402 * release any pages we did get
5403 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005404 if (pret > 0)
5405 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005406 if (ctx->account_mem)
5407 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005408 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005409 goto err;
5410 }
5411
5412 off = ubuf & ~PAGE_MASK;
5413 size = iov.iov_len;
5414 for (j = 0; j < nr_pages; j++) {
5415 size_t vec_len;
5416
5417 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5418 imu->bvec[j].bv_page = pages[j];
5419 imu->bvec[j].bv_len = vec_len;
5420 imu->bvec[j].bv_offset = off;
5421 off = 0;
5422 size -= vec_len;
5423 }
5424 /* store original address for later verification */
5425 imu->ubuf = ubuf;
5426 imu->len = iov.iov_len;
5427 imu->nr_bvecs = nr_pages;
5428
5429 ctx->nr_user_bufs++;
5430 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005431 kvfree(pages);
5432 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005433 return 0;
5434err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005435 kvfree(pages);
5436 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005437 io_sqe_buffer_unregister(ctx);
5438 return ret;
5439}
5440
Jens Axboe9b402842019-04-11 11:45:41 -06005441static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5442{
5443 __s32 __user *fds = arg;
5444 int fd;
5445
5446 if (ctx->cq_ev_fd)
5447 return -EBUSY;
5448
5449 if (copy_from_user(&fd, fds, sizeof(*fds)))
5450 return -EFAULT;
5451
5452 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5453 if (IS_ERR(ctx->cq_ev_fd)) {
5454 int ret = PTR_ERR(ctx->cq_ev_fd);
5455 ctx->cq_ev_fd = NULL;
5456 return ret;
5457 }
5458
5459 return 0;
5460}
5461
5462static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5463{
5464 if (ctx->cq_ev_fd) {
5465 eventfd_ctx_put(ctx->cq_ev_fd);
5466 ctx->cq_ev_fd = NULL;
5467 return 0;
5468 }
5469
5470 return -ENXIO;
5471}
5472
Jens Axboe2b188cc2019-01-07 10:46:33 -07005473static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5474{
Jens Axboe6b063142019-01-10 22:13:58 -07005475 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005476 if (ctx->sqo_mm)
5477 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005478
5479 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005480 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005481 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005482 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005483
Jens Axboe2b188cc2019-01-07 10:46:33 -07005484#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005485 if (ctx->ring_sock) {
5486 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005487 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005488 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005489#endif
5490
Hristo Venev75b28af2019-08-26 17:23:46 +00005491 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005492 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005493
5494 percpu_ref_exit(&ctx->refs);
5495 if (ctx->account_mem)
5496 io_unaccount_mem(ctx->user,
5497 ring_pages(ctx->sq_entries, ctx->cq_entries));
5498 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07005499 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07005500 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07005501 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07005502 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005503 kfree(ctx);
5504}
5505
5506static __poll_t io_uring_poll(struct file *file, poll_table *wait)
5507{
5508 struct io_ring_ctx *ctx = file->private_data;
5509 __poll_t mask = 0;
5510
5511 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02005512 /*
5513 * synchronizes with barrier from wq_has_sleeper call in
5514 * io_commit_cqring
5515 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005516 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00005517 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
5518 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005519 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08005520 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005521 mask |= EPOLLIN | EPOLLRDNORM;
5522
5523 return mask;
5524}
5525
5526static int io_uring_fasync(int fd, struct file *file, int on)
5527{
5528 struct io_ring_ctx *ctx = file->private_data;
5529
5530 return fasync_helper(fd, file, on, &ctx->cq_fasync);
5531}
5532
5533static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
5534{
5535 mutex_lock(&ctx->uring_lock);
5536 percpu_ref_kill(&ctx->refs);
5537 mutex_unlock(&ctx->uring_lock);
5538
Jens Axboe5262f562019-09-17 12:26:57 -06005539 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005540 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06005541
5542 if (ctx->io_wq)
5543 io_wq_cancel_all(ctx->io_wq);
5544
Jens Axboedef596e2019-01-09 08:59:42 -07005545 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07005546 /* if we failed setting up the ctx, we might not have any rings */
5547 if (ctx->rings)
5548 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07005549 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005550 io_ring_ctx_free(ctx);
5551}
5552
5553static int io_uring_release(struct inode *inode, struct file *file)
5554{
5555 struct io_ring_ctx *ctx = file->private_data;
5556
5557 file->private_data = NULL;
5558 io_ring_ctx_wait_and_kill(ctx);
5559 return 0;
5560}
5561
Jens Axboefcb323c2019-10-24 12:39:47 -06005562static void io_uring_cancel_files(struct io_ring_ctx *ctx,
5563 struct files_struct *files)
5564{
5565 struct io_kiocb *req;
5566 DEFINE_WAIT(wait);
5567
5568 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07005569 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06005570
5571 spin_lock_irq(&ctx->inflight_lock);
5572 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07005573 if (req->work.files != files)
5574 continue;
5575 /* req is being completed, ignore */
5576 if (!refcount_inc_not_zero(&req->refs))
5577 continue;
5578 cancel_req = req;
5579 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06005580 }
Jens Axboe768134d2019-11-10 20:30:53 -07005581 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005582 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07005583 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06005584 spin_unlock_irq(&ctx->inflight_lock);
5585
Jens Axboe768134d2019-11-10 20:30:53 -07005586 /* We need to keep going until we don't find a matching req */
5587 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005588 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005589
5590 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5591 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005592 schedule();
5593 }
Jens Axboe768134d2019-11-10 20:30:53 -07005594 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005595}
5596
5597static int io_uring_flush(struct file *file, void *data)
5598{
5599 struct io_ring_ctx *ctx = file->private_data;
5600
5601 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005602 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5603 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005604 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005605 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005606 return 0;
5607}
5608
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005609static void *io_uring_validate_mmap_request(struct file *file,
5610 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005611{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005612 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005613 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005614 struct page *page;
5615 void *ptr;
5616
5617 switch (offset) {
5618 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005619 case IORING_OFF_CQ_RING:
5620 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005621 break;
5622 case IORING_OFF_SQES:
5623 ptr = ctx->sq_sqes;
5624 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005625 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005626 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005627 }
5628
5629 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005630 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005631 return ERR_PTR(-EINVAL);
5632
5633 return ptr;
5634}
5635
5636#ifdef CONFIG_MMU
5637
5638static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5639{
5640 size_t sz = vma->vm_end - vma->vm_start;
5641 unsigned long pfn;
5642 void *ptr;
5643
5644 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5645 if (IS_ERR(ptr))
5646 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005647
5648 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5649 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5650}
5651
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005652#else /* !CONFIG_MMU */
5653
5654static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5655{
5656 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5657}
5658
5659static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5660{
5661 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5662}
5663
5664static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5665 unsigned long addr, unsigned long len,
5666 unsigned long pgoff, unsigned long flags)
5667{
5668 void *ptr;
5669
5670 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5671 if (IS_ERR(ptr))
5672 return PTR_ERR(ptr);
5673
5674 return (unsigned long) ptr;
5675}
5676
5677#endif /* !CONFIG_MMU */
5678
Jens Axboe2b188cc2019-01-07 10:46:33 -07005679SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5680 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5681 size_t, sigsz)
5682{
5683 struct io_ring_ctx *ctx;
5684 long ret = -EBADF;
5685 int submitted = 0;
5686 struct fd f;
5687
Jens Axboe6c271ce2019-01-10 11:22:30 -07005688 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005689 return -EINVAL;
5690
5691 f = fdget(fd);
5692 if (!f.file)
5693 return -EBADF;
5694
5695 ret = -EOPNOTSUPP;
5696 if (f.file->f_op != &io_uring_fops)
5697 goto out_fput;
5698
5699 ret = -ENXIO;
5700 ctx = f.file->private_data;
5701 if (!percpu_ref_tryget(&ctx->refs))
5702 goto out_fput;
5703
Jens Axboe6c271ce2019-01-10 11:22:30 -07005704 /*
5705 * For SQ polling, the thread will do all submissions and completions.
5706 * Just return the requested submit count, and wake the thread if
5707 * we were asked to.
5708 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005709 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005710 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005711 if (!list_empty_careful(&ctx->cq_overflow_list))
5712 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005713 if (flags & IORING_ENTER_SQ_WAKEUP)
5714 wake_up(&ctx->sqo_wait);
5715 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005716 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005717 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005718
Jens Axboe44d28272020-01-16 19:00:24 -07005719 if (current->mm != ctx->sqo_mm ||
5720 current_cred() != ctx->creds) {
5721 ret = -EPERM;
5722 goto out;
5723 }
5724
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005725 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005726 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005727 /* already have mm, so io_submit_sqes() won't try to grab it */
5728 cur_mm = ctx->sqo_mm;
5729 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
5730 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005731 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005732
5733 if (submitted != to_submit)
5734 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005735 }
5736 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07005737 unsigned nr_events = 0;
5738
Jens Axboe2b188cc2019-01-07 10:46:33 -07005739 min_complete = min(min_complete, ctx->cq_entries);
5740
Jens Axboedef596e2019-01-09 08:59:42 -07005741 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07005742 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07005743 } else {
5744 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
5745 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005746 }
5747
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005748out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03005749 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005750out_fput:
5751 fdput(f);
5752 return submitted ? submitted : ret;
5753}
5754
5755static const struct file_operations io_uring_fops = {
5756 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06005757 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07005758 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005759#ifndef CONFIG_MMU
5760 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
5761 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
5762#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07005763 .poll = io_uring_poll,
5764 .fasync = io_uring_fasync,
5765};
5766
5767static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
5768 struct io_uring_params *p)
5769{
Hristo Venev75b28af2019-08-26 17:23:46 +00005770 struct io_rings *rings;
5771 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005772
Hristo Venev75b28af2019-08-26 17:23:46 +00005773 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
5774 if (size == SIZE_MAX)
5775 return -EOVERFLOW;
5776
5777 rings = io_mem_alloc(size);
5778 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005779 return -ENOMEM;
5780
Hristo Venev75b28af2019-08-26 17:23:46 +00005781 ctx->rings = rings;
5782 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
5783 rings->sq_ring_mask = p->sq_entries - 1;
5784 rings->cq_ring_mask = p->cq_entries - 1;
5785 rings->sq_ring_entries = p->sq_entries;
5786 rings->cq_ring_entries = p->cq_entries;
5787 ctx->sq_mask = rings->sq_ring_mask;
5788 ctx->cq_mask = rings->cq_ring_mask;
5789 ctx->sq_entries = rings->sq_ring_entries;
5790 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005791
5792 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07005793 if (size == SIZE_MAX) {
5794 io_mem_free(ctx->rings);
5795 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005796 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07005797 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005798
5799 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07005800 if (!ctx->sq_sqes) {
5801 io_mem_free(ctx->rings);
5802 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005803 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07005804 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005805
Jens Axboe2b188cc2019-01-07 10:46:33 -07005806 return 0;
5807}
5808
5809/*
5810 * Allocate an anonymous fd, this is what constitutes the application
5811 * visible backing of an io_uring instance. The application mmaps this
5812 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
5813 * we have to tie this fd to a socket for file garbage collection purposes.
5814 */
5815static int io_uring_get_fd(struct io_ring_ctx *ctx)
5816{
5817 struct file *file;
5818 int ret;
5819
5820#if defined(CONFIG_UNIX)
5821 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
5822 &ctx->ring_sock);
5823 if (ret)
5824 return ret;
5825#endif
5826
5827 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
5828 if (ret < 0)
5829 goto err;
5830
5831 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
5832 O_RDWR | O_CLOEXEC);
5833 if (IS_ERR(file)) {
5834 put_unused_fd(ret);
5835 ret = PTR_ERR(file);
5836 goto err;
5837 }
5838
5839#if defined(CONFIG_UNIX)
5840 ctx->ring_sock->file = file;
5841#endif
5842 fd_install(ret, file);
5843 return ret;
5844err:
5845#if defined(CONFIG_UNIX)
5846 sock_release(ctx->ring_sock);
5847 ctx->ring_sock = NULL;
5848#endif
5849 return ret;
5850}
5851
5852static int io_uring_create(unsigned entries, struct io_uring_params *p)
5853{
5854 struct user_struct *user = NULL;
5855 struct io_ring_ctx *ctx;
5856 bool account_mem;
5857 int ret;
5858
5859 if (!entries || entries > IORING_MAX_ENTRIES)
5860 return -EINVAL;
5861
5862 /*
5863 * Use twice as many entries for the CQ ring. It's possible for the
5864 * application to drive a higher depth than the size of the SQ ring,
5865 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005866 * some flexibility in overcommitting a bit. If the application has
5867 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5868 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005869 */
5870 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005871 if (p->flags & IORING_SETUP_CQSIZE) {
5872 /*
5873 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5874 * to a power-of-two, if it isn't already. We do NOT impose
5875 * any cq vs sq ring sizing.
5876 */
5877 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5878 return -EINVAL;
5879 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5880 } else {
5881 p->cq_entries = 2 * p->sq_entries;
5882 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005883
5884 user = get_uid(current_user());
5885 account_mem = !capable(CAP_IPC_LOCK);
5886
5887 if (account_mem) {
5888 ret = io_account_mem(user,
5889 ring_pages(p->sq_entries, p->cq_entries));
5890 if (ret) {
5891 free_uid(user);
5892 return ret;
5893 }
5894 }
5895
5896 ctx = io_ring_ctx_alloc(p);
5897 if (!ctx) {
5898 if (account_mem)
5899 io_unaccount_mem(user, ring_pages(p->sq_entries,
5900 p->cq_entries));
5901 free_uid(user);
5902 return -ENOMEM;
5903 }
5904 ctx->compat = in_compat_syscall();
5905 ctx->account_mem = account_mem;
5906 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07005907 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07005908
5909 ret = io_allocate_scq_urings(ctx, p);
5910 if (ret)
5911 goto err;
5912
Jens Axboe6c271ce2019-01-10 11:22:30 -07005913 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005914 if (ret)
5915 goto err;
5916
Jens Axboe2b188cc2019-01-07 10:46:33 -07005917 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005918 p->sq_off.head = offsetof(struct io_rings, sq.head);
5919 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5920 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5921 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5922 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5923 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5924 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005925
5926 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005927 p->cq_off.head = offsetof(struct io_rings, cq.head);
5928 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5929 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5930 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5931 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5932 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06005933
Jens Axboe044c1ab2019-10-28 09:15:33 -06005934 /*
5935 * Install ring fd as the very last thing, so we don't risk someone
5936 * having closed it before we finish setup
5937 */
5938 ret = io_uring_get_fd(ctx);
5939 if (ret < 0)
5940 goto err;
5941
Jens Axboeda8c9692019-12-02 18:51:26 -07005942 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
5943 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005944 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005945 return ret;
5946err:
5947 io_ring_ctx_wait_and_kill(ctx);
5948 return ret;
5949}
5950
5951/*
5952 * Sets up an aio uring context, and returns the fd. Applications asks for a
5953 * ring size, we return the actual sq/cq ring sizes (among other things) in the
5954 * params structure passed in.
5955 */
5956static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5957{
5958 struct io_uring_params p;
5959 long ret;
5960 int i;
5961
5962 if (copy_from_user(&p, params, sizeof(p)))
5963 return -EFAULT;
5964 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5965 if (p.resv[i])
5966 return -EINVAL;
5967 }
5968
Jens Axboe6c271ce2019-01-10 11:22:30 -07005969 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06005970 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005971 return -EINVAL;
5972
5973 ret = io_uring_create(entries, &p);
5974 if (ret < 0)
5975 return ret;
5976
5977 if (copy_to_user(params, &p, sizeof(p)))
5978 return -EFAULT;
5979
5980 return ret;
5981}
5982
5983SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5984 struct io_uring_params __user *, params)
5985{
5986 return io_uring_setup(entries, params);
5987}
5988
Jens Axboeedafcce2019-01-09 09:16:05 -07005989static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
5990 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06005991 __releases(ctx->uring_lock)
5992 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07005993{
5994 int ret;
5995
Jens Axboe35fa71a2019-04-22 10:23:23 -06005996 /*
5997 * We're inside the ring mutex, if the ref is already dying, then
5998 * someone else killed the ctx or is already going through
5999 * io_uring_register().
6000 */
6001 if (percpu_ref_is_dying(&ctx->refs))
6002 return -ENXIO;
6003
Jens Axboe05f3fb32019-12-09 11:22:50 -07006004 if (opcode != IORING_UNREGISTER_FILES &&
6005 opcode != IORING_REGISTER_FILES_UPDATE) {
6006 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006007
Jens Axboe05f3fb32019-12-09 11:22:50 -07006008 /*
6009 * Drop uring mutex before waiting for references to exit. If
6010 * another thread is currently inside io_uring_enter() it might
6011 * need to grab the uring_lock to make progress. If we hold it
6012 * here across the drain wait, then we can deadlock. It's safe
6013 * to drop the mutex here, since no new references will come in
6014 * after we've killed the percpu ref.
6015 */
6016 mutex_unlock(&ctx->uring_lock);
6017 wait_for_completion(&ctx->completions[0]);
6018 mutex_lock(&ctx->uring_lock);
6019 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006020
6021 switch (opcode) {
6022 case IORING_REGISTER_BUFFERS:
6023 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6024 break;
6025 case IORING_UNREGISTER_BUFFERS:
6026 ret = -EINVAL;
6027 if (arg || nr_args)
6028 break;
6029 ret = io_sqe_buffer_unregister(ctx);
6030 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006031 case IORING_REGISTER_FILES:
6032 ret = io_sqe_files_register(ctx, arg, nr_args);
6033 break;
6034 case IORING_UNREGISTER_FILES:
6035 ret = -EINVAL;
6036 if (arg || nr_args)
6037 break;
6038 ret = io_sqe_files_unregister(ctx);
6039 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006040 case IORING_REGISTER_FILES_UPDATE:
6041 ret = io_sqe_files_update(ctx, arg, nr_args);
6042 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006043 case IORING_REGISTER_EVENTFD:
6044 ret = -EINVAL;
6045 if (nr_args != 1)
6046 break;
6047 ret = io_eventfd_register(ctx, arg);
6048 break;
6049 case IORING_UNREGISTER_EVENTFD:
6050 ret = -EINVAL;
6051 if (arg || nr_args)
6052 break;
6053 ret = io_eventfd_unregister(ctx);
6054 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006055 default:
6056 ret = -EINVAL;
6057 break;
6058 }
6059
Jens Axboe05f3fb32019-12-09 11:22:50 -07006060
6061 if (opcode != IORING_UNREGISTER_FILES &&
6062 opcode != IORING_REGISTER_FILES_UPDATE) {
6063 /* bring the ctx back to life */
6064 reinit_completion(&ctx->completions[0]);
6065 percpu_ref_reinit(&ctx->refs);
6066 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006067 return ret;
6068}
6069
6070SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6071 void __user *, arg, unsigned int, nr_args)
6072{
6073 struct io_ring_ctx *ctx;
6074 long ret = -EBADF;
6075 struct fd f;
6076
6077 f = fdget(fd);
6078 if (!f.file)
6079 return -EBADF;
6080
6081 ret = -EOPNOTSUPP;
6082 if (f.file->f_op != &io_uring_fops)
6083 goto out_fput;
6084
6085 ctx = f.file->private_data;
6086
6087 mutex_lock(&ctx->uring_lock);
6088 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6089 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006090 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6091 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006092out_fput:
6093 fdput(f);
6094 return ret;
6095}
6096
Jens Axboe2b188cc2019-01-07 10:46:33 -07006097static int __init io_uring_init(void)
6098{
6099 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6100 return 0;
6101};
6102__initcall(io_uring_init);