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