blob: 244aaccbc82bdb011002e8676dbf96fad95443c5 [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 Axboed3656342019-12-18 09:50:26 -0700519struct io_op_def {
520 /* needs req->io allocated for deferral/async */
521 unsigned async_ctx : 1;
522 /* needs current->mm setup, does mm access */
523 unsigned needs_mm : 1;
524 /* needs req->file assigned */
525 unsigned needs_file : 1;
526 /* needs req->file assigned IFF fd is >= 0 */
527 unsigned fd_non_neg : 1;
528 /* hash wq insertion if file is a regular file */
529 unsigned hash_reg_file : 1;
530 /* unbound wq insertion if file is a non-regular file */
531 unsigned unbound_nonreg_file : 1;
532};
533
534static const struct io_op_def io_op_defs[] = {
535 {
536 /* IORING_OP_NOP */
537 },
538 {
539 /* IORING_OP_READV */
540 .async_ctx = 1,
541 .needs_mm = 1,
542 .needs_file = 1,
543 .unbound_nonreg_file = 1,
544 },
545 {
546 /* IORING_OP_WRITEV */
547 .async_ctx = 1,
548 .needs_mm = 1,
549 .needs_file = 1,
550 .hash_reg_file = 1,
551 .unbound_nonreg_file = 1,
552 },
553 {
554 /* IORING_OP_FSYNC */
555 .needs_file = 1,
556 },
557 {
558 /* IORING_OP_READ_FIXED */
559 .needs_file = 1,
560 .unbound_nonreg_file = 1,
561 },
562 {
563 /* IORING_OP_WRITE_FIXED */
564 .needs_file = 1,
565 .hash_reg_file = 1,
566 .unbound_nonreg_file = 1,
567 },
568 {
569 /* IORING_OP_POLL_ADD */
570 .needs_file = 1,
571 .unbound_nonreg_file = 1,
572 },
573 {
574 /* IORING_OP_POLL_REMOVE */
575 },
576 {
577 /* IORING_OP_SYNC_FILE_RANGE */
578 .needs_file = 1,
579 },
580 {
581 /* IORING_OP_SENDMSG */
582 .async_ctx = 1,
583 .needs_mm = 1,
584 .needs_file = 1,
585 .unbound_nonreg_file = 1,
586 },
587 {
588 /* IORING_OP_RECVMSG */
589 .async_ctx = 1,
590 .needs_mm = 1,
591 .needs_file = 1,
592 .unbound_nonreg_file = 1,
593 },
594 {
595 /* IORING_OP_TIMEOUT */
596 .async_ctx = 1,
597 .needs_mm = 1,
598 },
599 {
600 /* IORING_OP_TIMEOUT_REMOVE */
601 },
602 {
603 /* IORING_OP_ACCEPT */
604 .needs_mm = 1,
605 .needs_file = 1,
606 .unbound_nonreg_file = 1,
607 },
608 {
609 /* IORING_OP_ASYNC_CANCEL */
610 },
611 {
612 /* IORING_OP_LINK_TIMEOUT */
613 .async_ctx = 1,
614 .needs_mm = 1,
615 },
616 {
617 /* IORING_OP_CONNECT */
618 .async_ctx = 1,
619 .needs_mm = 1,
620 .needs_file = 1,
621 .unbound_nonreg_file = 1,
622 },
623 {
624 /* IORING_OP_FALLOCATE */
625 .needs_file = 1,
626 },
627 {
628 /* IORING_OP_OPENAT */
629 .needs_file = 1,
630 .fd_non_neg = 1,
631 },
632 {
633 /* IORING_OP_CLOSE */
634 .needs_file = 1,
635 },
636 {
637 /* IORING_OP_FILES_UPDATE */
638 .needs_mm = 1,
639 },
640 {
641 /* IORING_OP_STATX */
642 .needs_mm = 1,
643 .needs_file = 1,
644 .fd_non_neg = 1,
645 },
646};
647
Jens Axboe561fb042019-10-24 07:25:42 -0600648static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700649static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800650static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700651static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700652static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
653static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700654static int __io_sqe_files_update(struct io_ring_ctx *ctx,
655 struct io_uring_files_update *ip,
656 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600657
Jens Axboe2b188cc2019-01-07 10:46:33 -0700658static struct kmem_cache *req_cachep;
659
660static const struct file_operations io_uring_fops;
661
662struct sock *io_uring_get_socket(struct file *file)
663{
664#if defined(CONFIG_UNIX)
665 if (file->f_op == &io_uring_fops) {
666 struct io_ring_ctx *ctx = file->private_data;
667
668 return ctx->ring_sock->sk;
669 }
670#endif
671 return NULL;
672}
673EXPORT_SYMBOL(io_uring_get_socket);
674
675static void io_ring_ctx_ref_free(struct percpu_ref *ref)
676{
677 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
678
Jens Axboe206aefd2019-11-07 18:27:42 -0700679 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700680}
681
682static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
683{
684 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700685 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700686
687 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
688 if (!ctx)
689 return NULL;
690
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700691 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
692 if (!ctx->fallback_req)
693 goto err;
694
Jens Axboe206aefd2019-11-07 18:27:42 -0700695 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
696 if (!ctx->completions)
697 goto err;
698
Jens Axboe78076bb2019-12-04 19:56:40 -0700699 /*
700 * Use 5 bits less than the max cq entries, that should give us around
701 * 32 entries per hash list if totally full and uniformly spread.
702 */
703 hash_bits = ilog2(p->cq_entries);
704 hash_bits -= 5;
705 if (hash_bits <= 0)
706 hash_bits = 1;
707 ctx->cancel_hash_bits = hash_bits;
708 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
709 GFP_KERNEL);
710 if (!ctx->cancel_hash)
711 goto err;
712 __hash_init(ctx->cancel_hash, 1U << hash_bits);
713
Roman Gushchin21482892019-05-07 10:01:48 -0700714 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700715 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
716 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700717
718 ctx->flags = p->flags;
719 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700720 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700721 init_completion(&ctx->completions[0]);
722 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700723 mutex_init(&ctx->uring_lock);
724 init_waitqueue_head(&ctx->wait);
725 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700726 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600727 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600728 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600729 init_waitqueue_head(&ctx->inflight_wait);
730 spin_lock_init(&ctx->inflight_lock);
731 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700732 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700733err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700734 if (ctx->fallback_req)
735 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700736 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700737 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700738 kfree(ctx);
739 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700740}
741
Bob Liu9d858b22019-11-13 18:06:25 +0800742static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600743{
Jackie Liua197f662019-11-08 08:09:12 -0700744 struct io_ring_ctx *ctx = req->ctx;
745
Jens Axboe498ccd92019-10-25 10:04:25 -0600746 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
747 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600748}
749
Bob Liu9d858b22019-11-13 18:06:25 +0800750static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600751{
Bob Liu9d858b22019-11-13 18:06:25 +0800752 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
753 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600754
Bob Liu9d858b22019-11-13 18:06:25 +0800755 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600756}
757
758static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600759{
760 struct io_kiocb *req;
761
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600762 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800763 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600764 list_del_init(&req->list);
765 return req;
766 }
767
768 return NULL;
769}
770
Jens Axboe5262f562019-09-17 12:26:57 -0600771static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
772{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600773 struct io_kiocb *req;
774
775 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700776 if (req) {
777 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
778 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800779 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700780 list_del_init(&req->list);
781 return req;
782 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600783 }
784
785 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600786}
787
Jens Axboede0617e2019-04-06 21:51:27 -0600788static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700789{
Hristo Venev75b28af2019-08-26 17:23:46 +0000790 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700791
Hristo Venev75b28af2019-08-26 17:23:46 +0000792 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700793 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000794 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700795
Jens Axboe2b188cc2019-01-07 10:46:33 -0700796 if (wq_has_sleeper(&ctx->cq_wait)) {
797 wake_up_interruptible(&ctx->cq_wait);
798 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
799 }
800 }
801}
802
Jens Axboe94ae5e72019-11-14 19:39:52 -0700803static inline bool io_prep_async_work(struct io_kiocb *req,
804 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600805{
Jens Axboed3656342019-12-18 09:50:26 -0700806 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600807 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600808
Jens Axboed3656342019-12-18 09:50:26 -0700809 if (req->flags & REQ_F_ISREG) {
810 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700811 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700812 } else {
813 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700814 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600815 }
Jens Axboed3656342019-12-18 09:50:26 -0700816 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700817 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600818
Jens Axboe94ae5e72019-11-14 19:39:52 -0700819 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600820 return do_hashed;
821}
822
Jackie Liua197f662019-11-08 08:09:12 -0700823static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600824{
Jackie Liua197f662019-11-08 08:09:12 -0700825 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700826 struct io_kiocb *link;
827 bool do_hashed;
828
829 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600830
831 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
832 req->flags);
833 if (!do_hashed) {
834 io_wq_enqueue(ctx->io_wq, &req->work);
835 } else {
836 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
837 file_inode(req->file));
838 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700839
840 if (link)
841 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600842}
843
Jens Axboe5262f562019-09-17 12:26:57 -0600844static void io_kill_timeout(struct io_kiocb *req)
845{
846 int ret;
847
Jens Axboe2d283902019-12-04 11:08:05 -0700848 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600849 if (ret != -1) {
850 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600851 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700852 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800853 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600854 }
855}
856
857static void io_kill_timeouts(struct io_ring_ctx *ctx)
858{
859 struct io_kiocb *req, *tmp;
860
861 spin_lock_irq(&ctx->completion_lock);
862 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
863 io_kill_timeout(req);
864 spin_unlock_irq(&ctx->completion_lock);
865}
866
Jens Axboede0617e2019-04-06 21:51:27 -0600867static void io_commit_cqring(struct io_ring_ctx *ctx)
868{
869 struct io_kiocb *req;
870
Jens Axboe5262f562019-09-17 12:26:57 -0600871 while ((req = io_get_timeout_req(ctx)) != NULL)
872 io_kill_timeout(req);
873
Jens Axboede0617e2019-04-06 21:51:27 -0600874 __io_commit_cqring(ctx);
875
876 while ((req = io_get_deferred_req(ctx)) != NULL) {
877 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700878 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600879 }
880}
881
Jens Axboe2b188cc2019-01-07 10:46:33 -0700882static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
883{
Hristo Venev75b28af2019-08-26 17:23:46 +0000884 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700885 unsigned tail;
886
887 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200888 /*
889 * writes to the cq entry need to come after reading head; the
890 * control dependency is enough as we're using WRITE_ONCE to
891 * fill the cq entry
892 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000893 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700894 return NULL;
895
896 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000897 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700898}
899
Jens Axboe8c838782019-03-12 15:48:16 -0600900static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
901{
902 if (waitqueue_active(&ctx->wait))
903 wake_up(&ctx->wait);
904 if (waitqueue_active(&ctx->sqo_wait))
905 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600906 if (ctx->cq_ev_fd)
907 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600908}
909
Jens Axboec4a2ed72019-11-21 21:01:26 -0700910/* Returns true if there are no backlogged entries after the flush */
911static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700912{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700913 struct io_rings *rings = ctx->rings;
914 struct io_uring_cqe *cqe;
915 struct io_kiocb *req;
916 unsigned long flags;
917 LIST_HEAD(list);
918
919 if (!force) {
920 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700921 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700922 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
923 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700924 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700925 }
926
927 spin_lock_irqsave(&ctx->completion_lock, flags);
928
929 /* if force is set, the ring is going away. always drop after that */
930 if (force)
931 ctx->cq_overflow_flushed = true;
932
Jens Axboec4a2ed72019-11-21 21:01:26 -0700933 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700934 while (!list_empty(&ctx->cq_overflow_list)) {
935 cqe = io_get_cqring(ctx);
936 if (!cqe && !force)
937 break;
938
939 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
940 list);
941 list_move(&req->list, &list);
942 if (cqe) {
943 WRITE_ONCE(cqe->user_data, req->user_data);
944 WRITE_ONCE(cqe->res, req->result);
945 WRITE_ONCE(cqe->flags, 0);
946 } else {
947 WRITE_ONCE(ctx->rings->cq_overflow,
948 atomic_inc_return(&ctx->cached_cq_overflow));
949 }
950 }
951
952 io_commit_cqring(ctx);
953 spin_unlock_irqrestore(&ctx->completion_lock, flags);
954 io_cqring_ev_posted(ctx);
955
956 while (!list_empty(&list)) {
957 req = list_first_entry(&list, struct io_kiocb, list);
958 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800959 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700960 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700961
962 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700963}
964
Jens Axboe78e19bb2019-11-06 15:21:34 -0700965static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700966{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700967 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700968 struct io_uring_cqe *cqe;
969
Jens Axboe78e19bb2019-11-06 15:21:34 -0700970 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700971
Jens Axboe2b188cc2019-01-07 10:46:33 -0700972 /*
973 * If we can't get a cq entry, userspace overflowed the
974 * submission (by quite a lot). Increment the overflow count in
975 * the ring.
976 */
977 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700978 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700979 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700980 WRITE_ONCE(cqe->res, res);
981 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700982 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700983 WRITE_ONCE(ctx->rings->cq_overflow,
984 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700985 } else {
986 refcount_inc(&req->refs);
987 req->result = res;
988 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700989 }
990}
991
Jens Axboe78e19bb2019-11-06 15:21:34 -0700992static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700993{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700994 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700995 unsigned long flags;
996
997 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700998 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700999 io_commit_cqring(ctx);
1000 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1001
Jens Axboe8c838782019-03-12 15:48:16 -06001002 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001003}
1004
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001005static inline bool io_is_fallback_req(struct io_kiocb *req)
1006{
1007 return req == (struct io_kiocb *)
1008 ((unsigned long) req->ctx->fallback_req & ~1UL);
1009}
1010
1011static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1012{
1013 struct io_kiocb *req;
1014
1015 req = ctx->fallback_req;
1016 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1017 return req;
1018
1019 return NULL;
1020}
1021
Jens Axboe2579f912019-01-09 09:10:43 -07001022static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1023 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001024{
Jens Axboefd6fab22019-03-14 16:30:06 -06001025 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001026 struct io_kiocb *req;
1027
1028 if (!percpu_ref_tryget(&ctx->refs))
1029 return NULL;
1030
Jens Axboe2579f912019-01-09 09:10:43 -07001031 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001032 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001033 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001034 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001035 } else if (!state->free_reqs) {
1036 size_t sz;
1037 int ret;
1038
1039 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001040 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1041
1042 /*
1043 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1044 * retry single alloc to be on the safe side.
1045 */
1046 if (unlikely(ret <= 0)) {
1047 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1048 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001049 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001050 ret = 1;
1051 }
Jens Axboe2579f912019-01-09 09:10:43 -07001052 state->free_reqs = ret - 1;
1053 state->cur_req = 1;
1054 req = state->reqs[0];
1055 } else {
1056 req = state->reqs[state->cur_req];
1057 state->free_reqs--;
1058 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001059 }
1060
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001061got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001062 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001063 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001064 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001065 req->ctx = ctx;
1066 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001067 /* one is dropped after submission, the other at completion */
1068 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001069 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001070 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001071 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001072fallback:
1073 req = io_get_fallback_req(ctx);
1074 if (req)
1075 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001076 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001077 return NULL;
1078}
1079
Jens Axboedef596e2019-01-09 08:59:42 -07001080static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
1081{
1082 if (*nr) {
1083 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +03001084 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001085 percpu_ref_put_many(&ctx->file_data->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -07001086 *nr = 0;
1087 }
1088}
1089
Jens Axboe9e645e112019-05-10 16:07:28 -06001090static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001091{
Jens Axboefcb323c2019-10-24 12:39:47 -06001092 struct io_ring_ctx *ctx = req->ctx;
1093
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001094 if (req->io)
1095 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001096 if (req->file) {
1097 if (req->flags & REQ_F_FIXED_FILE)
1098 percpu_ref_put(&ctx->file_data->refs);
1099 else
1100 fput(req->file);
1101 }
Jens Axboefcb323c2019-10-24 12:39:47 -06001102 if (req->flags & REQ_F_INFLIGHT) {
1103 unsigned long flags;
1104
1105 spin_lock_irqsave(&ctx->inflight_lock, flags);
1106 list_del(&req->inflight_entry);
1107 if (waitqueue_active(&ctx->inflight_wait))
1108 wake_up(&ctx->inflight_wait);
1109 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1110 }
1111 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001112 if (likely(!io_is_fallback_req(req)))
1113 kmem_cache_free(req_cachep, req);
1114 else
1115 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001116}
1117
Jackie Liua197f662019-11-08 08:09:12 -07001118static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001119{
Jackie Liua197f662019-11-08 08:09:12 -07001120 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001121 int ret;
1122
Jens Axboe2d283902019-12-04 11:08:05 -07001123 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001124 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001125 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001126 io_commit_cqring(ctx);
1127 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001128 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001129 return true;
1130 }
1131
1132 return false;
1133}
1134
Jens Axboeba816ad2019-09-28 11:36:45 -06001135static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001136{
Jens Axboe2665abf2019-11-05 12:40:47 -07001137 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001138 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001139
Jens Axboe4d7dd462019-11-20 13:03:52 -07001140 /* Already got next link */
1141 if (req->flags & REQ_F_LINK_NEXT)
1142 return;
1143
Jens Axboe9e645e112019-05-10 16:07:28 -06001144 /*
1145 * The list should never be empty when we are called here. But could
1146 * potentially happen if the chain is messed up, check to be on the
1147 * safe side.
1148 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001149 while (!list_empty(&req->link_list)) {
1150 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1151 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001152
Pavel Begunkov44932332019-12-05 16:16:35 +03001153 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1154 (nxt->flags & REQ_F_TIMEOUT))) {
1155 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001156 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001157 req->flags &= ~REQ_F_LINK_TIMEOUT;
1158 continue;
1159 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001160
Pavel Begunkov44932332019-12-05 16:16:35 +03001161 list_del_init(&req->link_list);
1162 if (!list_empty(&nxt->link_list))
1163 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001164 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001165 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001166 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001167
Jens Axboe4d7dd462019-11-20 13:03:52 -07001168 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001169 if (wake_ev)
1170 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001171}
1172
1173/*
1174 * Called if REQ_F_LINK is set, and we fail the head request
1175 */
1176static void io_fail_links(struct io_kiocb *req)
1177{
Jens Axboe2665abf2019-11-05 12:40:47 -07001178 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001179 unsigned long flags;
1180
1181 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001182
1183 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001184 struct io_kiocb *link = list_first_entry(&req->link_list,
1185 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001186
Pavel Begunkov44932332019-12-05 16:16:35 +03001187 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001188 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001189
1190 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001191 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001192 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001193 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001194 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001195 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001196 }
Jens Axboe5d960722019-11-19 15:31:28 -07001197 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001198 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001199
1200 io_commit_cqring(ctx);
1201 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1202 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001203}
1204
Jens Axboe4d7dd462019-11-20 13:03:52 -07001205static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001206{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001207 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001208 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001209
Jens Axboe9e645e112019-05-10 16:07:28 -06001210 /*
1211 * If LINK is set, we have dependent requests in this chain. If we
1212 * didn't fail this request, queue the first one up, moving any other
1213 * dependencies to the next request. In case of failure, fail the rest
1214 * of the chain.
1215 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001216 if (req->flags & REQ_F_FAIL_LINK) {
1217 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001218 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1219 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001220 struct io_ring_ctx *ctx = req->ctx;
1221 unsigned long flags;
1222
1223 /*
1224 * If this is a timeout link, we could be racing with the
1225 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001226 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001227 */
1228 spin_lock_irqsave(&ctx->completion_lock, flags);
1229 io_req_link_next(req, nxt);
1230 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1231 } else {
1232 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001233 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001234}
Jens Axboe9e645e112019-05-10 16:07:28 -06001235
Jackie Liuc69f8db2019-11-09 11:00:08 +08001236static void io_free_req(struct io_kiocb *req)
1237{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001238 struct io_kiocb *nxt = NULL;
1239
1240 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001241 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001242
1243 if (nxt)
1244 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001245}
1246
Jens Axboeba816ad2019-09-28 11:36:45 -06001247/*
1248 * Drop reference to request, return next in chain (if there is one) if this
1249 * was the last reference to this request.
1250 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001251__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001252static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001253{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001254 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001255
Jens Axboee65ef562019-03-12 10:16:44 -06001256 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001257 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001258}
1259
Jens Axboe2b188cc2019-01-07 10:46:33 -07001260static void io_put_req(struct io_kiocb *req)
1261{
Jens Axboedef596e2019-01-09 08:59:42 -07001262 if (refcount_dec_and_test(&req->refs))
1263 io_free_req(req);
1264}
1265
Jens Axboe978db572019-11-14 22:39:04 -07001266/*
1267 * Must only be used if we don't need to care about links, usually from
1268 * within the completion handling itself.
1269 */
1270static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001271{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001272 /* drop both submit and complete references */
1273 if (refcount_sub_and_test(2, &req->refs))
1274 __io_free_req(req);
1275}
1276
Jens Axboe978db572019-11-14 22:39:04 -07001277static void io_double_put_req(struct io_kiocb *req)
1278{
1279 /* drop both submit and complete references */
1280 if (refcount_sub_and_test(2, &req->refs))
1281 io_free_req(req);
1282}
1283
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001284static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001285{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001286 struct io_rings *rings = ctx->rings;
1287
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001288 /*
1289 * noflush == true is from the waitqueue handler, just ensure we wake
1290 * up the task, and the next invocation will flush the entries. We
1291 * cannot safely to it from here.
1292 */
1293 if (noflush && !list_empty(&ctx->cq_overflow_list))
1294 return -1U;
1295
1296 io_cqring_overflow_flush(ctx, false);
1297
Jens Axboea3a0e432019-08-20 11:03:11 -06001298 /* See comment at the top of this file */
1299 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001300 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001301}
1302
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001303static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1304{
1305 struct io_rings *rings = ctx->rings;
1306
1307 /* make sure SQ entry isn't read before tail */
1308 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1309}
1310
Jens Axboedef596e2019-01-09 08:59:42 -07001311/*
1312 * Find and free completed poll iocbs
1313 */
1314static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1315 struct list_head *done)
1316{
1317 void *reqs[IO_IOPOLL_BATCH];
1318 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001319 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001320
Jens Axboe09bb8392019-03-13 12:39:28 -06001321 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001322 while (!list_empty(done)) {
1323 req = list_first_entry(done, struct io_kiocb, list);
1324 list_del(&req->list);
1325
Jens Axboe78e19bb2019-11-06 15:21:34 -07001326 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001327 (*nr_events)++;
1328
Jens Axboe09bb8392019-03-13 12:39:28 -06001329 if (refcount_dec_and_test(&req->refs)) {
1330 /* If we're not using fixed files, we have to pair the
1331 * completion part with the file put. Use regular
1332 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001333 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001334 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001335 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1336 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1337 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001338 reqs[to_free++] = req;
1339 if (to_free == ARRAY_SIZE(reqs))
1340 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001341 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001342 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001343 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001344 }
Jens Axboedef596e2019-01-09 08:59:42 -07001345 }
Jens Axboedef596e2019-01-09 08:59:42 -07001346
Jens Axboe09bb8392019-03-13 12:39:28 -06001347 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001348 io_free_req_many(ctx, reqs, &to_free);
1349}
1350
1351static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1352 long min)
1353{
1354 struct io_kiocb *req, *tmp;
1355 LIST_HEAD(done);
1356 bool spin;
1357 int ret;
1358
1359 /*
1360 * Only spin for completions if we don't have multiple devices hanging
1361 * off our complete list, and we're under the requested amount.
1362 */
1363 spin = !ctx->poll_multi_file && *nr_events < min;
1364
1365 ret = 0;
1366 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001367 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001368
1369 /*
1370 * Move completed entries to our local list. If we find a
1371 * request that requires polling, break out and complete
1372 * the done list first, if we have entries there.
1373 */
1374 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1375 list_move_tail(&req->list, &done);
1376 continue;
1377 }
1378 if (!list_empty(&done))
1379 break;
1380
1381 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1382 if (ret < 0)
1383 break;
1384
1385 if (ret && spin)
1386 spin = false;
1387 ret = 0;
1388 }
1389
1390 if (!list_empty(&done))
1391 io_iopoll_complete(ctx, nr_events, &done);
1392
1393 return ret;
1394}
1395
1396/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001397 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001398 * non-spinning poll check - we'll still enter the driver poll loop, but only
1399 * as a non-spinning completion check.
1400 */
1401static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1402 long min)
1403{
Jens Axboe08f54392019-08-21 22:19:11 -06001404 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001405 int ret;
1406
1407 ret = io_do_iopoll(ctx, nr_events, min);
1408 if (ret < 0)
1409 return ret;
1410 if (!min || *nr_events >= min)
1411 return 0;
1412 }
1413
1414 return 1;
1415}
1416
1417/*
1418 * We can't just wait for polled events to come to us, we have to actively
1419 * find and complete them.
1420 */
1421static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1422{
1423 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1424 return;
1425
1426 mutex_lock(&ctx->uring_lock);
1427 while (!list_empty(&ctx->poll_list)) {
1428 unsigned int nr_events = 0;
1429
1430 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001431
1432 /*
1433 * Ensure we allow local-to-the-cpu processing to take place,
1434 * in this case we need to ensure that we reap all events.
1435 */
1436 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001437 }
1438 mutex_unlock(&ctx->uring_lock);
1439}
1440
Jens Axboe2b2ed972019-10-25 10:06:15 -06001441static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1442 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001443{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001444 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001445
1446 do {
1447 int tmin = 0;
1448
Jens Axboe500f9fb2019-08-19 12:15:59 -06001449 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001450 * Don't enter poll loop if we already have events pending.
1451 * If we do, we can potentially be spinning for commands that
1452 * already triggered a CQE (eg in error).
1453 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001454 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001455 break;
1456
1457 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001458 * If a submit got punted to a workqueue, we can have the
1459 * application entering polling for a command before it gets
1460 * issued. That app will hold the uring_lock for the duration
1461 * of the poll right here, so we need to take a breather every
1462 * now and then to ensure that the issue has a chance to add
1463 * the poll to the issued list. Otherwise we can spin here
1464 * forever, while the workqueue is stuck trying to acquire the
1465 * very same mutex.
1466 */
1467 if (!(++iters & 7)) {
1468 mutex_unlock(&ctx->uring_lock);
1469 mutex_lock(&ctx->uring_lock);
1470 }
1471
Jens Axboedef596e2019-01-09 08:59:42 -07001472 if (*nr_events < min)
1473 tmin = min - *nr_events;
1474
1475 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1476 if (ret <= 0)
1477 break;
1478 ret = 0;
1479 } while (min && !*nr_events && !need_resched());
1480
Jens Axboe2b2ed972019-10-25 10:06:15 -06001481 return ret;
1482}
1483
1484static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1485 long min)
1486{
1487 int ret;
1488
1489 /*
1490 * We disallow the app entering submit/complete with polling, but we
1491 * still need to lock the ring to prevent racing with polled issue
1492 * that got punted to a workqueue.
1493 */
1494 mutex_lock(&ctx->uring_lock);
1495 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001496 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001497 return ret;
1498}
1499
Jens Axboe491381ce2019-10-17 09:20:46 -06001500static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001501{
Jens Axboe491381ce2019-10-17 09:20:46 -06001502 /*
1503 * Tell lockdep we inherited freeze protection from submission
1504 * thread.
1505 */
1506 if (req->flags & REQ_F_ISREG) {
1507 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001508
Jens Axboe491381ce2019-10-17 09:20:46 -06001509 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001510 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001511 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001512}
1513
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001514static inline void req_set_fail_links(struct io_kiocb *req)
1515{
1516 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1517 req->flags |= REQ_F_FAIL_LINK;
1518}
1519
Jens Axboeba816ad2019-09-28 11:36:45 -06001520static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001521{
Jens Axboe9adbd452019-12-20 08:45:55 -07001522 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001523
Jens Axboe491381ce2019-10-17 09:20:46 -06001524 if (kiocb->ki_flags & IOCB_WRITE)
1525 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001526
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001527 if (res != req->result)
1528 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001529 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001530}
1531
1532static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1533{
Jens Axboe9adbd452019-12-20 08:45:55 -07001534 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001535
1536 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001537 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001538}
1539
Jens Axboeba816ad2019-09-28 11:36:45 -06001540static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1541{
Jens Axboe9adbd452019-12-20 08:45:55 -07001542 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001543 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001544
1545 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001546 io_put_req_find_next(req, &nxt);
1547
1548 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001549}
1550
Jens Axboedef596e2019-01-09 08:59:42 -07001551static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1552{
Jens Axboe9adbd452019-12-20 08:45:55 -07001553 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001554
Jens Axboe491381ce2019-10-17 09:20:46 -06001555 if (kiocb->ki_flags & IOCB_WRITE)
1556 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001557
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001558 if (res != req->result)
1559 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001560 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001561 if (res != -EAGAIN)
1562 req->flags |= REQ_F_IOPOLL_COMPLETED;
1563}
1564
1565/*
1566 * After the iocb has been issued, it's safe to be found on the poll list.
1567 * Adding the kiocb to the list AFTER submission ensures that we don't
1568 * find it from a io_iopoll_getevents() thread before the issuer is done
1569 * accessing the kiocb cookie.
1570 */
1571static void io_iopoll_req_issued(struct io_kiocb *req)
1572{
1573 struct io_ring_ctx *ctx = req->ctx;
1574
1575 /*
1576 * Track whether we have multiple files in our lists. This will impact
1577 * how we do polling eventually, not spinning if we're on potentially
1578 * different devices.
1579 */
1580 if (list_empty(&ctx->poll_list)) {
1581 ctx->poll_multi_file = false;
1582 } else if (!ctx->poll_multi_file) {
1583 struct io_kiocb *list_req;
1584
1585 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1586 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001587 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001588 ctx->poll_multi_file = true;
1589 }
1590
1591 /*
1592 * For fast devices, IO may have already completed. If it has, add
1593 * it to the front so we find it first.
1594 */
1595 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1596 list_add(&req->list, &ctx->poll_list);
1597 else
1598 list_add_tail(&req->list, &ctx->poll_list);
1599}
1600
Jens Axboe3d6770f2019-04-13 11:50:54 -06001601static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001602{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001603 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001604 int diff = state->has_refs - state->used_refs;
1605
1606 if (diff)
1607 fput_many(state->file, diff);
1608 state->file = NULL;
1609 }
1610}
1611
1612/*
1613 * Get as many references to a file as we have IOs left in this submission,
1614 * assuming most submissions are for one file, or at least that each file
1615 * has more than one submission.
1616 */
1617static struct file *io_file_get(struct io_submit_state *state, int fd)
1618{
1619 if (!state)
1620 return fget(fd);
1621
1622 if (state->file) {
1623 if (state->fd == fd) {
1624 state->used_refs++;
1625 state->ios_left--;
1626 return state->file;
1627 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001628 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001629 }
1630 state->file = fget_many(fd, state->ios_left);
1631 if (!state->file)
1632 return NULL;
1633
1634 state->fd = fd;
1635 state->has_refs = state->ios_left;
1636 state->used_refs = 1;
1637 state->ios_left--;
1638 return state->file;
1639}
1640
Jens Axboe2b188cc2019-01-07 10:46:33 -07001641/*
1642 * If we tracked the file through the SCM inflight mechanism, we could support
1643 * any file. For now, just ensure that anything potentially problematic is done
1644 * inline.
1645 */
1646static bool io_file_supports_async(struct file *file)
1647{
1648 umode_t mode = file_inode(file)->i_mode;
1649
Jens Axboe10d59342019-12-09 20:16:22 -07001650 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001651 return true;
1652 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1653 return true;
1654
1655 return false;
1656}
1657
Jens Axboe3529d8c2019-12-19 18:24:38 -07001658static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1659 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001660{
Jens Axboedef596e2019-01-09 08:59:42 -07001661 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001662 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001663 unsigned ioprio;
1664 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001665
Jens Axboe09bb8392019-03-13 12:39:28 -06001666 if (!req->file)
1667 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001668
Jens Axboe491381ce2019-10-17 09:20:46 -06001669 if (S_ISREG(file_inode(req->file)->i_mode))
1670 req->flags |= REQ_F_ISREG;
1671
Jens Axboe2b188cc2019-01-07 10:46:33 -07001672 kiocb->ki_pos = READ_ONCE(sqe->off);
1673 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1674 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1675
1676 ioprio = READ_ONCE(sqe->ioprio);
1677 if (ioprio) {
1678 ret = ioprio_check_cap(ioprio);
1679 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001680 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681
1682 kiocb->ki_ioprio = ioprio;
1683 } else
1684 kiocb->ki_ioprio = get_current_ioprio();
1685
1686 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1687 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001688 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001689
1690 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001691 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1692 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001693 req->flags |= REQ_F_NOWAIT;
1694
1695 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001696 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001697
Jens Axboedef596e2019-01-09 08:59:42 -07001698 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001699 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1700 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001701 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001702
Jens Axboedef596e2019-01-09 08:59:42 -07001703 kiocb->ki_flags |= IOCB_HIPRI;
1704 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001705 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001706 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001707 if (kiocb->ki_flags & IOCB_HIPRI)
1708 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001709 kiocb->ki_complete = io_complete_rw;
1710 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001711
Jens Axboe3529d8c2019-12-19 18:24:38 -07001712 req->rw.addr = READ_ONCE(sqe->addr);
1713 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001714 /* we own ->private, reuse it for the buffer index */
1715 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001716 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001717 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001718}
1719
1720static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1721{
1722 switch (ret) {
1723 case -EIOCBQUEUED:
1724 break;
1725 case -ERESTARTSYS:
1726 case -ERESTARTNOINTR:
1727 case -ERESTARTNOHAND:
1728 case -ERESTART_RESTARTBLOCK:
1729 /*
1730 * We can't just restart the syscall, since previously
1731 * submitted sqes may already be in progress. Just fail this
1732 * IO with EINTR.
1733 */
1734 ret = -EINTR;
1735 /* fall through */
1736 default:
1737 kiocb->ki_complete(kiocb, ret, 0);
1738 }
1739}
1740
Jens Axboeba816ad2019-09-28 11:36:45 -06001741static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1742 bool in_async)
1743{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001744 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001745 *nxt = __io_complete_rw(kiocb, ret);
1746 else
1747 io_rw_done(kiocb, ret);
1748}
1749
Jens Axboe9adbd452019-12-20 08:45:55 -07001750static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001751 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001752{
Jens Axboe9adbd452019-12-20 08:45:55 -07001753 struct io_ring_ctx *ctx = req->ctx;
1754 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001755 struct io_mapped_ubuf *imu;
1756 unsigned index, buf_index;
1757 size_t offset;
1758 u64 buf_addr;
1759
1760 /* attempt to use fixed buffers without having provided iovecs */
1761 if (unlikely(!ctx->user_bufs))
1762 return -EFAULT;
1763
Jens Axboe9adbd452019-12-20 08:45:55 -07001764 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001765 if (unlikely(buf_index >= ctx->nr_user_bufs))
1766 return -EFAULT;
1767
1768 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1769 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001770 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001771
1772 /* overflow */
1773 if (buf_addr + len < buf_addr)
1774 return -EFAULT;
1775 /* not inside the mapped region */
1776 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1777 return -EFAULT;
1778
1779 /*
1780 * May not be a start of buffer, set size appropriately
1781 * and advance us to the beginning.
1782 */
1783 offset = buf_addr - imu->ubuf;
1784 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001785
1786 if (offset) {
1787 /*
1788 * Don't use iov_iter_advance() here, as it's really slow for
1789 * using the latter parts of a big fixed buffer - it iterates
1790 * over each segment manually. We can cheat a bit here, because
1791 * we know that:
1792 *
1793 * 1) it's a BVEC iter, we set it up
1794 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1795 * first and last bvec
1796 *
1797 * So just find our index, and adjust the iterator afterwards.
1798 * If the offset is within the first bvec (or the whole first
1799 * bvec, just use iov_iter_advance(). This makes it easier
1800 * since we can just skip the first segment, which may not
1801 * be PAGE_SIZE aligned.
1802 */
1803 const struct bio_vec *bvec = imu->bvec;
1804
1805 if (offset <= bvec->bv_len) {
1806 iov_iter_advance(iter, offset);
1807 } else {
1808 unsigned long seg_skip;
1809
1810 /* skip first vec */
1811 offset -= bvec->bv_len;
1812 seg_skip = 1 + (offset >> PAGE_SHIFT);
1813
1814 iter->bvec = bvec + seg_skip;
1815 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001816 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001817 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001818 }
1819 }
1820
Jens Axboe5e559562019-11-13 16:12:46 -07001821 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001822}
1823
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001824static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1825 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001826{
Jens Axboe9adbd452019-12-20 08:45:55 -07001827 void __user *buf = u64_to_user_ptr(req->rw.addr);
1828 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001829 u8 opcode;
1830
Jens Axboed625c6e2019-12-17 19:53:05 -07001831 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001832 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001833 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001834 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001835 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001836
Jens Axboe9adbd452019-12-20 08:45:55 -07001837 /* buffer index only valid with fixed read/write */
1838 if (req->rw.kiocb.private)
1839 return -EINVAL;
1840
Jens Axboef67676d2019-12-02 11:03:47 -07001841 if (req->io) {
1842 struct io_async_rw *iorw = &req->io->rw;
1843
1844 *iovec = iorw->iov;
1845 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1846 if (iorw->iov == iorw->fast_iov)
1847 *iovec = NULL;
1848 return iorw->size;
1849 }
1850
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001851 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001852 return -EFAULT;
1853
1854#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001855 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001856 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1857 iovec, iter);
1858#endif
1859
1860 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1861}
1862
Jens Axboe32960612019-09-23 11:05:34 -06001863/*
1864 * For files that don't have ->read_iter() and ->write_iter(), handle them
1865 * by looping over ->read() or ->write() manually.
1866 */
1867static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1868 struct iov_iter *iter)
1869{
1870 ssize_t ret = 0;
1871
1872 /*
1873 * Don't support polled IO through this interface, and we can't
1874 * support non-blocking either. For the latter, this just causes
1875 * the kiocb to be handled from an async context.
1876 */
1877 if (kiocb->ki_flags & IOCB_HIPRI)
1878 return -EOPNOTSUPP;
1879 if (kiocb->ki_flags & IOCB_NOWAIT)
1880 return -EAGAIN;
1881
1882 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001883 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001884 ssize_t nr;
1885
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001886 if (!iov_iter_is_bvec(iter)) {
1887 iovec = iov_iter_iovec(iter);
1888 } else {
1889 /* fixed buffers import bvec */
1890 iovec.iov_base = kmap(iter->bvec->bv_page)
1891 + iter->iov_offset;
1892 iovec.iov_len = min(iter->count,
1893 iter->bvec->bv_len - iter->iov_offset);
1894 }
1895
Jens Axboe32960612019-09-23 11:05:34 -06001896 if (rw == READ) {
1897 nr = file->f_op->read(file, iovec.iov_base,
1898 iovec.iov_len, &kiocb->ki_pos);
1899 } else {
1900 nr = file->f_op->write(file, iovec.iov_base,
1901 iovec.iov_len, &kiocb->ki_pos);
1902 }
1903
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001904 if (iov_iter_is_bvec(iter))
1905 kunmap(iter->bvec->bv_page);
1906
Jens Axboe32960612019-09-23 11:05:34 -06001907 if (nr < 0) {
1908 if (!ret)
1909 ret = nr;
1910 break;
1911 }
1912 ret += nr;
1913 if (nr != iovec.iov_len)
1914 break;
1915 iov_iter_advance(iter, nr);
1916 }
1917
1918 return ret;
1919}
1920
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001921static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07001922 struct iovec *iovec, struct iovec *fast_iov,
1923 struct iov_iter *iter)
1924{
1925 req->io->rw.nr_segs = iter->nr_segs;
1926 req->io->rw.size = io_size;
1927 req->io->rw.iov = iovec;
1928 if (!req->io->rw.iov) {
1929 req->io->rw.iov = req->io->rw.fast_iov;
1930 memcpy(req->io->rw.iov, fast_iov,
1931 sizeof(struct iovec) * iter->nr_segs);
1932 }
1933}
1934
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001935static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07001936{
Jens Axboed3656342019-12-18 09:50:26 -07001937 if (!io_op_defs[req->opcode].async_ctx)
1938 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001939 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07001940 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001941}
1942
1943static void io_rw_async(struct io_wq_work **workptr)
1944{
1945 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1946 struct iovec *iov = NULL;
1947
1948 if (req->io->rw.iov != req->io->rw.fast_iov)
1949 iov = req->io->rw.iov;
1950 io_wq_submit_work(workptr);
1951 kfree(iov);
1952}
1953
1954static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
1955 struct iovec *iovec, struct iovec *fast_iov,
1956 struct iov_iter *iter)
1957{
Jens Axboe74566df2020-01-13 19:23:24 -07001958 if (req->opcode == IORING_OP_READ_FIXED ||
1959 req->opcode == IORING_OP_WRITE_FIXED)
1960 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001961 if (!req->io && io_alloc_async_ctx(req))
1962 return -ENOMEM;
1963
1964 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
1965 req->work.func = io_rw_async;
1966 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001967}
1968
Jens Axboe3529d8c2019-12-19 18:24:38 -07001969static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1970 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07001971{
Jens Axboe3529d8c2019-12-19 18:24:38 -07001972 struct io_async_ctx *io;
1973 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07001974 ssize_t ret;
1975
Jens Axboe3529d8c2019-12-19 18:24:38 -07001976 ret = io_prep_rw(req, sqe, force_nonblock);
1977 if (ret)
1978 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07001979
Jens Axboe3529d8c2019-12-19 18:24:38 -07001980 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1981 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07001982
Jens Axboe3529d8c2019-12-19 18:24:38 -07001983 if (!req->io)
1984 return 0;
1985
1986 io = req->io;
1987 io->rw.iov = io->rw.fast_iov;
1988 req->io = NULL;
1989 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
1990 req->io = io;
1991 if (ret < 0)
1992 return ret;
1993
1994 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
1995 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001996}
1997
Pavel Begunkov267bc902019-11-07 01:41:08 +03001998static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001999 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002000{
2001 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002002 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002003 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002004 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002005 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002006
Jens Axboe3529d8c2019-12-19 18:24:38 -07002007 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002008 if (ret < 0)
2009 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002010
Jens Axboefd6c2e42019-12-18 12:19:41 -07002011 /* Ensure we clear previously set non-block flag */
2012 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002013 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002014
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002015 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002016 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002017 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002018 req->result = io_size;
2019
2020 /*
2021 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2022 * we know to async punt it even if it was opened O_NONBLOCK
2023 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002024 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002025 req->flags |= REQ_F_MUST_PUNT;
2026 goto copy_iov;
2027 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002028
Jens Axboe31b51512019-01-18 22:56:34 -07002029 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002030 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002031 if (!ret) {
2032 ssize_t ret2;
2033
Jens Axboe9adbd452019-12-20 08:45:55 -07002034 if (req->file->f_op->read_iter)
2035 ret2 = call_read_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(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002038
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002039 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002040 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002041 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002042 } else {
2043copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002044 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002045 inline_vecs, &iter);
2046 if (ret)
2047 goto out_free;
2048 return -EAGAIN;
2049 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002050 }
Jens Axboef67676d2019-12-02 11:03:47 -07002051out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002052 if (!io_wq_current_is_worker())
2053 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002054 return ret;
2055}
2056
Jens Axboe3529d8c2019-12-19 18:24:38 -07002057static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2058 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002059{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002060 struct io_async_ctx *io;
2061 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002062 ssize_t ret;
2063
Jens Axboe3529d8c2019-12-19 18:24:38 -07002064 ret = io_prep_rw(req, sqe, force_nonblock);
2065 if (ret)
2066 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002067
Jens Axboe3529d8c2019-12-19 18:24:38 -07002068 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2069 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002070
Jens Axboe3529d8c2019-12-19 18:24:38 -07002071 if (!req->io)
2072 return 0;
2073
2074 io = req->io;
2075 io->rw.iov = io->rw.fast_iov;
2076 req->io = NULL;
2077 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2078 req->io = io;
2079 if (ret < 0)
2080 return ret;
2081
2082 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2083 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002084}
2085
Pavel Begunkov267bc902019-11-07 01:41:08 +03002086static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002087 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002088{
2089 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002090 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002091 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002092 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002093 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002094
Jens Axboe3529d8c2019-12-19 18:24:38 -07002095 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002096 if (ret < 0)
2097 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002098
Jens Axboefd6c2e42019-12-18 12:19:41 -07002099 /* Ensure we clear previously set non-block flag */
2100 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002101 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002102
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002103 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002104 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002105 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002106 req->result = io_size;
2107
2108 /*
2109 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2110 * we know to async punt it even if it was opened O_NONBLOCK
2111 */
2112 if (force_nonblock && !io_file_supports_async(req->file)) {
2113 req->flags |= REQ_F_MUST_PUNT;
2114 goto copy_iov;
2115 }
2116
Jens Axboe10d59342019-12-09 20:16:22 -07002117 /* file path doesn't support NOWAIT for non-direct_IO */
2118 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2119 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002120 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002121
Jens Axboe31b51512019-01-18 22:56:34 -07002122 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002123 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002124 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002125 ssize_t ret2;
2126
Jens Axboe2b188cc2019-01-07 10:46:33 -07002127 /*
2128 * Open-code file_start_write here to grab freeze protection,
2129 * which will be released by another thread in
2130 * io_complete_rw(). Fool lockdep by telling it the lock got
2131 * released so that it doesn't complain about the held lock when
2132 * we return to userspace.
2133 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002134 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002135 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002136 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002137 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002138 SB_FREEZE_WRITE);
2139 }
2140 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002141
Jens Axboe9adbd452019-12-20 08:45:55 -07002142 if (req->file->f_op->write_iter)
2143 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002144 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002145 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002146 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002147 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002148 } else {
2149copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002150 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002151 inline_vecs, &iter);
2152 if (ret)
2153 goto out_free;
2154 return -EAGAIN;
2155 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002156 }
Jens Axboe31b51512019-01-18 22:56:34 -07002157out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002158 if (!io_wq_current_is_worker())
2159 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002160 return ret;
2161}
2162
2163/*
2164 * IORING_OP_NOP just posts a completion event, nothing else.
2165 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002166static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002167{
2168 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002169
Jens Axboedef596e2019-01-09 08:59:42 -07002170 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2171 return -EINVAL;
2172
Jens Axboe78e19bb2019-11-06 15:21:34 -07002173 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002174 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002175 return 0;
2176}
2177
Jens Axboe3529d8c2019-12-19 18:24:38 -07002178static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002179{
Jens Axboe6b063142019-01-10 22:13:58 -07002180 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002181
Jens Axboe09bb8392019-03-13 12:39:28 -06002182 if (!req->file)
2183 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002184
Jens Axboe6b063142019-01-10 22:13:58 -07002185 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002186 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002187 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002188 return -EINVAL;
2189
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002190 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2191 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2192 return -EINVAL;
2193
2194 req->sync.off = READ_ONCE(sqe->off);
2195 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002196 return 0;
2197}
2198
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002199static bool io_req_cancelled(struct io_kiocb *req)
2200{
2201 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2202 req_set_fail_links(req);
2203 io_cqring_add_event(req, -ECANCELED);
2204 io_put_req(req);
2205 return true;
2206 }
2207
2208 return false;
2209}
2210
Jens Axboe78912932020-01-14 22:09:06 -07002211static void io_link_work_cb(struct io_wq_work **workptr)
2212{
2213 struct io_wq_work *work = *workptr;
2214 struct io_kiocb *link = work->data;
2215
2216 io_queue_linked_timeout(link);
2217 work->func = io_wq_submit_work;
2218}
2219
2220static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2221{
2222 struct io_kiocb *link;
2223
2224 io_prep_async_work(nxt, &link);
2225 *workptr = &nxt->work;
2226 if (link) {
2227 nxt->work.flags |= IO_WQ_WORK_CB;
2228 nxt->work.func = io_link_work_cb;
2229 nxt->work.data = link;
2230 }
2231}
2232
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002233static void io_fsync_finish(struct io_wq_work **workptr)
2234{
2235 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2236 loff_t end = req->sync.off + req->sync.len;
2237 struct io_kiocb *nxt = NULL;
2238 int ret;
2239
2240 if (io_req_cancelled(req))
2241 return;
2242
Jens Axboe9adbd452019-12-20 08:45:55 -07002243 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002244 end > 0 ? end : LLONG_MAX,
2245 req->sync.flags & IORING_FSYNC_DATASYNC);
2246 if (ret < 0)
2247 req_set_fail_links(req);
2248 io_cqring_add_event(req, ret);
2249 io_put_req_find_next(req, &nxt);
2250 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002251 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002252}
2253
Jens Axboefc4df992019-12-10 14:38:45 -07002254static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2255 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002256{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002257 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002258
2259 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002260 if (force_nonblock) {
2261 io_put_req(req);
2262 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002263 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002264 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002265
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002266 work = old_work = &req->work;
2267 io_fsync_finish(&work);
2268 if (work && work != old_work)
2269 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002270 return 0;
2271}
2272
Jens Axboed63d1b52019-12-10 10:38:56 -07002273static void io_fallocate_finish(struct io_wq_work **workptr)
2274{
2275 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2276 struct io_kiocb *nxt = NULL;
2277 int ret;
2278
2279 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2280 req->sync.len);
2281 if (ret < 0)
2282 req_set_fail_links(req);
2283 io_cqring_add_event(req, ret);
2284 io_put_req_find_next(req, &nxt);
2285 if (nxt)
2286 io_wq_assign_next(workptr, nxt);
2287}
2288
2289static int io_fallocate_prep(struct io_kiocb *req,
2290 const struct io_uring_sqe *sqe)
2291{
2292 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2293 return -EINVAL;
2294
2295 req->sync.off = READ_ONCE(sqe->off);
2296 req->sync.len = READ_ONCE(sqe->addr);
2297 req->sync.mode = READ_ONCE(sqe->len);
2298 return 0;
2299}
2300
2301static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2302 bool force_nonblock)
2303{
2304 struct io_wq_work *work, *old_work;
2305
2306 /* fallocate always requiring blocking context */
2307 if (force_nonblock) {
2308 io_put_req(req);
2309 req->work.func = io_fallocate_finish;
2310 return -EAGAIN;
2311 }
2312
2313 work = old_work = &req->work;
2314 io_fallocate_finish(&work);
2315 if (work && work != old_work)
2316 *nxt = container_of(work, struct io_kiocb, work);
2317
2318 return 0;
2319}
2320
Jens Axboe15b71ab2019-12-11 11:20:36 -07002321static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2322{
2323 int ret;
2324
2325 if (sqe->ioprio || sqe->buf_index)
2326 return -EINVAL;
2327
2328 req->open.dfd = READ_ONCE(sqe->fd);
2329 req->open.mode = READ_ONCE(sqe->len);
2330 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2331 req->open.flags = READ_ONCE(sqe->open_flags);
2332
2333 req->open.filename = getname(req->open.fname);
2334 if (IS_ERR(req->open.filename)) {
2335 ret = PTR_ERR(req->open.filename);
2336 req->open.filename = NULL;
2337 return ret;
2338 }
2339
2340 return 0;
2341}
2342
2343static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2344 bool force_nonblock)
2345{
2346 struct open_flags op;
2347 struct open_how how;
2348 struct file *file;
2349 int ret;
2350
2351 if (force_nonblock) {
2352 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2353 return -EAGAIN;
2354 }
2355
2356 how = build_open_how(req->open.flags, req->open.mode);
2357 ret = build_open_flags(&how, &op);
2358 if (ret)
2359 goto err;
2360
2361 ret = get_unused_fd_flags(how.flags);
2362 if (ret < 0)
2363 goto err;
2364
2365 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2366 if (IS_ERR(file)) {
2367 put_unused_fd(ret);
2368 ret = PTR_ERR(file);
2369 } else {
2370 fsnotify_open(file);
2371 fd_install(ret, file);
2372 }
2373err:
2374 putname(req->open.filename);
2375 if (ret < 0)
2376 req_set_fail_links(req);
2377 io_cqring_add_event(req, ret);
2378 io_put_req_find_next(req, nxt);
2379 return 0;
2380}
2381
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002382static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2383{
2384 unsigned lookup_flags;
2385 int ret;
2386
2387 if (sqe->ioprio || sqe->buf_index)
2388 return -EINVAL;
2389
2390 req->open.dfd = READ_ONCE(sqe->fd);
2391 req->open.mask = READ_ONCE(sqe->len);
2392 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2393 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2394 req->open.flags = READ_ONCE(sqe->statx_flags);
2395
2396 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.flags))
2397 return -EINVAL;
2398
2399 req->open.filename = getname_flags(req->open.fname, lookup_flags, NULL);
2400 if (IS_ERR(req->open.filename)) {
2401 ret = PTR_ERR(req->open.filename);
2402 req->open.filename = NULL;
2403 return ret;
2404 }
2405
2406 return 0;
2407}
2408
2409static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2410 bool force_nonblock)
2411{
2412 struct io_open *ctx = &req->open;
2413 unsigned lookup_flags;
2414 struct path path;
2415 struct kstat stat;
2416 int ret;
2417
2418 if (force_nonblock)
2419 return -EAGAIN;
2420
2421 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->flags))
2422 return -EINVAL;
2423
2424retry:
2425 /* filename_lookup() drops it, keep a reference */
2426 ctx->filename->refcnt++;
2427
2428 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2429 NULL);
2430 if (ret)
2431 goto err;
2432
2433 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->flags);
2434 path_put(&path);
2435 if (retry_estale(ret, lookup_flags)) {
2436 lookup_flags |= LOOKUP_REVAL;
2437 goto retry;
2438 }
2439 if (!ret)
2440 ret = cp_statx(&stat, ctx->buffer);
2441err:
2442 putname(ctx->filename);
2443 if (ret < 0)
2444 req_set_fail_links(req);
2445 io_cqring_add_event(req, ret);
2446 io_put_req_find_next(req, nxt);
2447 return 0;
2448}
2449
Jens Axboeb5dba592019-12-11 14:02:38 -07002450static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2451{
2452 /*
2453 * If we queue this for async, it must not be cancellable. That would
2454 * leave the 'file' in an undeterminate state.
2455 */
2456 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2457
2458 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2459 sqe->rw_flags || sqe->buf_index)
2460 return -EINVAL;
2461 if (sqe->flags & IOSQE_FIXED_FILE)
2462 return -EINVAL;
2463
2464 req->close.fd = READ_ONCE(sqe->fd);
2465 if (req->file->f_op == &io_uring_fops ||
2466 req->close.fd == req->ring_fd)
2467 return -EBADF;
2468
2469 return 0;
2470}
2471
2472static void io_close_finish(struct io_wq_work **workptr)
2473{
2474 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2475 struct io_kiocb *nxt = NULL;
2476
2477 /* Invoked with files, we need to do the close */
2478 if (req->work.files) {
2479 int ret;
2480
2481 ret = filp_close(req->close.put_file, req->work.files);
2482 if (ret < 0) {
2483 req_set_fail_links(req);
2484 }
2485 io_cqring_add_event(req, ret);
2486 }
2487
2488 fput(req->close.put_file);
2489
2490 /* we bypassed the re-issue, drop the submission reference */
2491 io_put_req(req);
2492 io_put_req_find_next(req, &nxt);
2493 if (nxt)
2494 io_wq_assign_next(workptr, nxt);
2495}
2496
2497static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2498 bool force_nonblock)
2499{
2500 int ret;
2501
2502 req->close.put_file = NULL;
2503 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2504 if (ret < 0)
2505 return ret;
2506
2507 /* if the file has a flush method, be safe and punt to async */
2508 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2509 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2510 goto eagain;
2511 }
2512
2513 /*
2514 * No ->flush(), safely close from here and just punt the
2515 * fput() to async context.
2516 */
2517 ret = filp_close(req->close.put_file, current->files);
2518
2519 if (ret < 0)
2520 req_set_fail_links(req);
2521 io_cqring_add_event(req, ret);
2522
2523 if (io_wq_current_is_worker()) {
2524 struct io_wq_work *old_work, *work;
2525
2526 old_work = work = &req->work;
2527 io_close_finish(&work);
2528 if (work && work != old_work)
2529 *nxt = container_of(work, struct io_kiocb, work);
2530 return 0;
2531 }
2532
2533eagain:
2534 req->work.func = io_close_finish;
2535 return -EAGAIN;
2536}
2537
Jens Axboe3529d8c2019-12-19 18:24:38 -07002538static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002539{
2540 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002541
2542 if (!req->file)
2543 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002544
2545 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2546 return -EINVAL;
2547 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2548 return -EINVAL;
2549
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002550 req->sync.off = READ_ONCE(sqe->off);
2551 req->sync.len = READ_ONCE(sqe->len);
2552 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002553 return 0;
2554}
2555
2556static void io_sync_file_range_finish(struct io_wq_work **workptr)
2557{
2558 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2559 struct io_kiocb *nxt = NULL;
2560 int ret;
2561
2562 if (io_req_cancelled(req))
2563 return;
2564
Jens Axboe9adbd452019-12-20 08:45:55 -07002565 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002566 req->sync.flags);
2567 if (ret < 0)
2568 req_set_fail_links(req);
2569 io_cqring_add_event(req, ret);
2570 io_put_req_find_next(req, &nxt);
2571 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002572 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002573}
2574
Jens Axboefc4df992019-12-10 14:38:45 -07002575static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002576 bool force_nonblock)
2577{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002578 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002579
2580 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002581 if (force_nonblock) {
2582 io_put_req(req);
2583 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002584 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002585 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002586
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002587 work = old_work = &req->work;
2588 io_sync_file_range_finish(&work);
2589 if (work && work != old_work)
2590 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002591 return 0;
2592}
2593
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002594#if defined(CONFIG_NET)
2595static void io_sendrecv_async(struct io_wq_work **workptr)
2596{
2597 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2598 struct iovec *iov = NULL;
2599
2600 if (req->io->rw.iov != req->io->rw.fast_iov)
2601 iov = req->io->msg.iov;
2602 io_wq_submit_work(workptr);
2603 kfree(iov);
2604}
2605#endif
2606
Jens Axboe3529d8c2019-12-19 18:24:38 -07002607static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002608{
Jens Axboe03b12302019-12-02 18:50:25 -07002609#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002610 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002611 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002612
Jens Axboee47293f2019-12-20 08:58:21 -07002613 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2614 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe3529d8c2019-12-19 18:24:38 -07002615
2616 if (!io)
2617 return 0;
2618
Jens Axboed9688562019-12-09 19:35:20 -07002619 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002620 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002621 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002622#else
Jens Axboee47293f2019-12-20 08:58:21 -07002623 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002624#endif
2625}
2626
Jens Axboefc4df992019-12-10 14:38:45 -07002627static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2628 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002629{
2630#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002631 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002632 struct socket *sock;
2633 int ret;
2634
2635 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2636 return -EINVAL;
2637
2638 sock = sock_from_file(req->file, &ret);
2639 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002640 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002641 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002642 unsigned flags;
2643
Jens Axboe03b12302019-12-02 18:50:25 -07002644 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002645 kmsg = &req->io->msg;
2646 kmsg->msg.msg_name = &addr;
2647 /* if iov is set, it's allocated already */
2648 if (!kmsg->iov)
2649 kmsg->iov = kmsg->fast_iov;
2650 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002651 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002652 struct io_sr_msg *sr = &req->sr_msg;
2653
Jens Axboe0b416c32019-12-15 10:57:46 -07002654 kmsg = &io.msg;
2655 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002656
2657 io.msg.iov = io.msg.fast_iov;
2658 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2659 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002660 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002661 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002662 }
2663
Jens Axboee47293f2019-12-20 08:58:21 -07002664 flags = req->sr_msg.msg_flags;
2665 if (flags & MSG_DONTWAIT)
2666 req->flags |= REQ_F_NOWAIT;
2667 else if (force_nonblock)
2668 flags |= MSG_DONTWAIT;
2669
Jens Axboe0b416c32019-12-15 10:57:46 -07002670 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002671 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002672 if (req->io)
2673 return -EAGAIN;
2674 if (io_alloc_async_ctx(req))
2675 return -ENOMEM;
2676 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2677 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002678 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002679 }
2680 if (ret == -ERESTARTSYS)
2681 ret = -EINTR;
2682 }
2683
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002684 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002685 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002686 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002687 if (ret < 0)
2688 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002689 io_put_req_find_next(req, nxt);
2690 return 0;
2691#else
2692 return -EOPNOTSUPP;
2693#endif
2694}
2695
Jens Axboe3529d8c2019-12-19 18:24:38 -07002696static int io_recvmsg_prep(struct io_kiocb *req,
2697 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002698{
2699#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002700 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002701 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002702
Jens Axboe3529d8c2019-12-19 18:24:38 -07002703 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2704 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2705
2706 if (!io)
Jens Axboe06b76d42019-12-19 14:44:26 -07002707 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002708
Jens Axboed9688562019-12-09 19:35:20 -07002709 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002710 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002711 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002712#else
Jens Axboee47293f2019-12-20 08:58:21 -07002713 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002714#endif
2715}
2716
Jens Axboefc4df992019-12-10 14:38:45 -07002717static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2718 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002719{
2720#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002721 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002722 struct socket *sock;
2723 int ret;
2724
2725 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2726 return -EINVAL;
2727
2728 sock = sock_from_file(req->file, &ret);
2729 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002730 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002731 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002732 unsigned flags;
2733
Jens Axboe03b12302019-12-02 18:50:25 -07002734 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002735 kmsg = &req->io->msg;
2736 kmsg->msg.msg_name = &addr;
2737 /* if iov is set, it's allocated already */
2738 if (!kmsg->iov)
2739 kmsg->iov = kmsg->fast_iov;
2740 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002741 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002742 struct io_sr_msg *sr = &req->sr_msg;
2743
Jens Axboe0b416c32019-12-15 10:57:46 -07002744 kmsg = &io.msg;
2745 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002746
2747 io.msg.iov = io.msg.fast_iov;
2748 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
2749 sr->msg_flags, &io.msg.uaddr,
2750 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002751 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002752 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002753 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002754
Jens Axboee47293f2019-12-20 08:58:21 -07002755 flags = req->sr_msg.msg_flags;
2756 if (flags & MSG_DONTWAIT)
2757 req->flags |= REQ_F_NOWAIT;
2758 else if (force_nonblock)
2759 flags |= MSG_DONTWAIT;
2760
2761 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
2762 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002763 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002764 if (req->io)
2765 return -EAGAIN;
2766 if (io_alloc_async_ctx(req))
2767 return -ENOMEM;
2768 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2769 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002770 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002771 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002772 if (ret == -ERESTARTSYS)
2773 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002774 }
2775
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002776 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002777 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002778 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002779 if (ret < 0)
2780 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002781 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002782 return 0;
2783#else
2784 return -EOPNOTSUPP;
2785#endif
2786}
2787
Jens Axboe3529d8c2019-12-19 18:24:38 -07002788static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002789{
2790#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002791 struct io_accept *accept = &req->accept;
2792
Jens Axboe17f2fe32019-10-17 14:42:58 -06002793 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2794 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002795 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002796 return -EINVAL;
2797
Jens Axboed55e5f52019-12-11 16:12:15 -07002798 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2799 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002800 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002801 return 0;
2802#else
2803 return -EOPNOTSUPP;
2804#endif
2805}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002806
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002807#if defined(CONFIG_NET)
2808static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2809 bool force_nonblock)
2810{
2811 struct io_accept *accept = &req->accept;
2812 unsigned file_flags;
2813 int ret;
2814
2815 file_flags = force_nonblock ? O_NONBLOCK : 0;
2816 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2817 accept->addr_len, accept->flags);
2818 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002819 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002820 if (ret == -ERESTARTSYS)
2821 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002822 if (ret < 0)
2823 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002824 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002825 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002826 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002827}
2828
2829static void io_accept_finish(struct io_wq_work **workptr)
2830{
2831 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2832 struct io_kiocb *nxt = NULL;
2833
2834 if (io_req_cancelled(req))
2835 return;
2836 __io_accept(req, &nxt, false);
2837 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002838 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002839}
2840#endif
2841
2842static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2843 bool force_nonblock)
2844{
2845#if defined(CONFIG_NET)
2846 int ret;
2847
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002848 ret = __io_accept(req, nxt, force_nonblock);
2849 if (ret == -EAGAIN && force_nonblock) {
2850 req->work.func = io_accept_finish;
2851 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2852 io_put_req(req);
2853 return -EAGAIN;
2854 }
2855 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002856#else
2857 return -EOPNOTSUPP;
2858#endif
2859}
2860
Jens Axboe3529d8c2019-12-19 18:24:38 -07002861static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07002862{
2863#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002864 struct io_connect *conn = &req->connect;
2865 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07002866
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002867 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2868 return -EINVAL;
2869 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2870 return -EINVAL;
2871
Jens Axboe3529d8c2019-12-19 18:24:38 -07002872 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2873 conn->addr_len = READ_ONCE(sqe->addr2);
2874
2875 if (!io)
2876 return 0;
2877
2878 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002879 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002880#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002881 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07002882#endif
2883}
2884
Jens Axboefc4df992019-12-10 14:38:45 -07002885static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
2886 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07002887{
2888#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07002889 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002890 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002891 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002892
Jens Axboef499a022019-12-02 16:28:46 -07002893 if (req->io) {
2894 io = req->io;
2895 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002896 ret = move_addr_to_kernel(req->connect.addr,
2897 req->connect.addr_len,
2898 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002899 if (ret)
2900 goto out;
2901 io = &__io;
2902 }
2903
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002904 file_flags = force_nonblock ? O_NONBLOCK : 0;
2905
2906 ret = __sys_connect_file(req->file, &io->connect.address,
2907 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002908 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002909 if (req->io)
2910 return -EAGAIN;
2911 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07002912 ret = -ENOMEM;
2913 goto out;
2914 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002915 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07002916 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002917 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002918 if (ret == -ERESTARTSYS)
2919 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002920out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002921 if (ret < 0)
2922 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002923 io_cqring_add_event(req, ret);
2924 io_put_req_find_next(req, nxt);
2925 return 0;
2926#else
2927 return -EOPNOTSUPP;
2928#endif
2929}
2930
Jens Axboe221c5eb2019-01-17 09:41:58 -07002931static void io_poll_remove_one(struct io_kiocb *req)
2932{
2933 struct io_poll_iocb *poll = &req->poll;
2934
2935 spin_lock(&poll->head->lock);
2936 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07002937 if (!list_empty(&poll->wait.entry)) {
2938 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002939 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002940 }
2941 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002942 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002943}
2944
2945static void io_poll_remove_all(struct io_ring_ctx *ctx)
2946{
Jens Axboe78076bb2019-12-04 19:56:40 -07002947 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002948 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07002949 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002950
2951 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002952 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2953 struct hlist_head *list;
2954
2955 list = &ctx->cancel_hash[i];
2956 hlist_for_each_entry_safe(req, tmp, list, hash_node)
2957 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002958 }
2959 spin_unlock_irq(&ctx->completion_lock);
2960}
2961
Jens Axboe47f46762019-11-09 17:43:02 -07002962static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2963{
Jens Axboe78076bb2019-12-04 19:56:40 -07002964 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07002965 struct io_kiocb *req;
2966
Jens Axboe78076bb2019-12-04 19:56:40 -07002967 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2968 hlist_for_each_entry(req, list, hash_node) {
2969 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002970 io_poll_remove_one(req);
2971 return 0;
2972 }
Jens Axboe47f46762019-11-09 17:43:02 -07002973 }
2974
2975 return -ENOENT;
2976}
2977
Jens Axboe3529d8c2019-12-19 18:24:38 -07002978static int io_poll_remove_prep(struct io_kiocb *req,
2979 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002980{
Jens Axboe221c5eb2019-01-17 09:41:58 -07002981 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2982 return -EINVAL;
2983 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2984 sqe->poll_events)
2985 return -EINVAL;
2986
Jens Axboe0969e782019-12-17 18:40:57 -07002987 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07002988 return 0;
2989}
2990
2991/*
2992 * Find a running poll command that matches one specified in sqe->addr,
2993 * and remove it if found.
2994 */
2995static int io_poll_remove(struct io_kiocb *req)
2996{
2997 struct io_ring_ctx *ctx = req->ctx;
2998 u64 addr;
2999 int ret;
3000
Jens Axboe0969e782019-12-17 18:40:57 -07003001 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003002 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003003 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003004 spin_unlock_irq(&ctx->completion_lock);
3005
Jens Axboe78e19bb2019-11-06 15:21:34 -07003006 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003007 if (ret < 0)
3008 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003009 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003010 return 0;
3011}
3012
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003013static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003014{
Jackie Liua197f662019-11-08 08:09:12 -07003015 struct io_ring_ctx *ctx = req->ctx;
3016
Jens Axboe8c838782019-03-12 15:48:16 -06003017 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003018 if (error)
3019 io_cqring_fill_event(req, error);
3020 else
3021 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003022 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003023}
3024
Jens Axboe561fb042019-10-24 07:25:42 -06003025static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003026{
Jens Axboe561fb042019-10-24 07:25:42 -06003027 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003028 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3029 struct io_poll_iocb *poll = &req->poll;
3030 struct poll_table_struct pt = { ._key = poll->events };
3031 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003032 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003033 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003034 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003035
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003036 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003037 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003038 ret = -ECANCELED;
3039 } else if (READ_ONCE(poll->canceled)) {
3040 ret = -ECANCELED;
3041 }
Jens Axboe561fb042019-10-24 07:25:42 -06003042
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003043 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003044 mask = vfs_poll(poll->file, &pt) & poll->events;
3045
3046 /*
3047 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3048 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3049 * synchronize with them. In the cancellation case the list_del_init
3050 * itself is not actually needed, but harmless so we keep it in to
3051 * avoid further branches in the fast path.
3052 */
3053 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003054 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003055 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003056 spin_unlock_irq(&ctx->completion_lock);
3057 return;
3058 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003059 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003060 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003061 spin_unlock_irq(&ctx->completion_lock);
3062
Jens Axboe8c838782019-03-12 15:48:16 -06003063 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003064
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003065 if (ret < 0)
3066 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003067 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003068 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003069 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003070}
3071
3072static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3073 void *key)
3074{
Jens Axboee9444752019-11-26 15:02:04 -07003075 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003076 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3077 struct io_ring_ctx *ctx = req->ctx;
3078 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06003079 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003080
3081 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003082 if (mask && !(mask & poll->events))
3083 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003084
Jens Axboe392edb42019-12-09 17:52:20 -07003085 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003086
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003087 /*
3088 * Run completion inline if we can. We're using trylock here because
3089 * we are violating the completion_lock -> poll wq lock ordering.
3090 * If we have a link timeout we're going to need the completion_lock
3091 * for finalizing the request, mark us as having grabbed that already.
3092 */
Jens Axboe8c838782019-03-12 15:48:16 -06003093 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboe78076bb2019-12-04 19:56:40 -07003094 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003095 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003096 req->flags |= REQ_F_COMP_LOCKED;
3097 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003098 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3099
3100 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06003101 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003102 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003103 }
3104
Jens Axboe221c5eb2019-01-17 09:41:58 -07003105 return 1;
3106}
3107
3108struct io_poll_table {
3109 struct poll_table_struct pt;
3110 struct io_kiocb *req;
3111 int error;
3112};
3113
3114static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3115 struct poll_table_struct *p)
3116{
3117 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3118
3119 if (unlikely(pt->req->poll.head)) {
3120 pt->error = -EINVAL;
3121 return;
3122 }
3123
3124 pt->error = 0;
3125 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003126 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003127}
3128
Jens Axboeeac406c2019-11-14 12:09:58 -07003129static void io_poll_req_insert(struct io_kiocb *req)
3130{
3131 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003132 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003133
Jens Axboe78076bb2019-12-04 19:56:40 -07003134 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3135 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003136}
3137
Jens Axboe3529d8c2019-12-19 18:24:38 -07003138static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003139{
3140 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003141 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003142
3143 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3144 return -EINVAL;
3145 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3146 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003147 if (!poll->file)
3148 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003149
Jens Axboe221c5eb2019-01-17 09:41:58 -07003150 events = READ_ONCE(sqe->poll_events);
3151 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003152 return 0;
3153}
3154
3155static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3156{
3157 struct io_poll_iocb *poll = &req->poll;
3158 struct io_ring_ctx *ctx = req->ctx;
3159 struct io_poll_table ipt;
3160 bool cancel = false;
3161 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003162
3163 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003164 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003165
Jens Axboe221c5eb2019-01-17 09:41:58 -07003166 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003167 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003168 poll->canceled = false;
3169
3170 ipt.pt._qproc = io_poll_queue_proc;
3171 ipt.pt._key = poll->events;
3172 ipt.req = req;
3173 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3174
3175 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003176 INIT_LIST_HEAD(&poll->wait.entry);
3177 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3178 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003179
Jens Axboe36703242019-07-25 10:20:18 -06003180 INIT_LIST_HEAD(&req->list);
3181
Jens Axboe221c5eb2019-01-17 09:41:58 -07003182 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003183
3184 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003185 if (likely(poll->head)) {
3186 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003187 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003188 if (ipt.error)
3189 cancel = true;
3190 ipt.error = 0;
3191 mask = 0;
3192 }
3193 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003194 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003195 else if (cancel)
3196 WRITE_ONCE(poll->canceled, true);
3197 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003198 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003199 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003200 }
Jens Axboe8c838782019-03-12 15:48:16 -06003201 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003202 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003203 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003204 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003205 spin_unlock_irq(&ctx->completion_lock);
3206
Jens Axboe8c838782019-03-12 15:48:16 -06003207 if (mask) {
3208 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003209 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003210 }
Jens Axboe8c838782019-03-12 15:48:16 -06003211 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003212}
3213
Jens Axboe5262f562019-09-17 12:26:57 -06003214static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3215{
Jens Axboead8a48a2019-11-15 08:49:11 -07003216 struct io_timeout_data *data = container_of(timer,
3217 struct io_timeout_data, timer);
3218 struct io_kiocb *req = data->req;
3219 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003220 unsigned long flags;
3221
Jens Axboe5262f562019-09-17 12:26:57 -06003222 atomic_inc(&ctx->cq_timeouts);
3223
3224 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003225 /*
Jens Axboe11365042019-10-16 09:08:32 -06003226 * We could be racing with timeout deletion. If the list is empty,
3227 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003228 */
Jens Axboe842f9612019-10-29 12:34:10 -06003229 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003230 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003231
Jens Axboe11365042019-10-16 09:08:32 -06003232 /*
3233 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003234 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003235 * pointer will be increased, otherwise other timeout reqs may
3236 * return in advance without waiting for enough wait_nr.
3237 */
3238 prev = req;
3239 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3240 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003241 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003242 }
Jens Axboe842f9612019-10-29 12:34:10 -06003243
Jens Axboe78e19bb2019-11-06 15:21:34 -07003244 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003245 io_commit_cqring(ctx);
3246 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3247
3248 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003249 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003250 io_put_req(req);
3251 return HRTIMER_NORESTART;
3252}
3253
Jens Axboe47f46762019-11-09 17:43:02 -07003254static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3255{
3256 struct io_kiocb *req;
3257 int ret = -ENOENT;
3258
3259 list_for_each_entry(req, &ctx->timeout_list, list) {
3260 if (user_data == req->user_data) {
3261 list_del_init(&req->list);
3262 ret = 0;
3263 break;
3264 }
3265 }
3266
3267 if (ret == -ENOENT)
3268 return ret;
3269
Jens Axboe2d283902019-12-04 11:08:05 -07003270 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003271 if (ret == -1)
3272 return -EALREADY;
3273
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003274 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003275 io_cqring_fill_event(req, -ECANCELED);
3276 io_put_req(req);
3277 return 0;
3278}
3279
Jens Axboe3529d8c2019-12-19 18:24:38 -07003280static int io_timeout_remove_prep(struct io_kiocb *req,
3281 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003282{
Jens Axboeb29472e2019-12-17 18:50:29 -07003283 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3284 return -EINVAL;
3285 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3286 return -EINVAL;
3287
3288 req->timeout.addr = READ_ONCE(sqe->addr);
3289 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3290 if (req->timeout.flags)
3291 return -EINVAL;
3292
Jens Axboeb29472e2019-12-17 18:50:29 -07003293 return 0;
3294}
3295
Jens Axboe11365042019-10-16 09:08:32 -06003296/*
3297 * Remove or update an existing timeout command
3298 */
Jens Axboefc4df992019-12-10 14:38:45 -07003299static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003300{
3301 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003302 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003303
Jens Axboe11365042019-10-16 09:08:32 -06003304 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003305 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003306
Jens Axboe47f46762019-11-09 17:43:02 -07003307 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003308 io_commit_cqring(ctx);
3309 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003310 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003311 if (ret < 0)
3312 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003313 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003314 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003315}
3316
Jens Axboe3529d8c2019-12-19 18:24:38 -07003317static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003318 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003319{
Jens Axboead8a48a2019-11-15 08:49:11 -07003320 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003321 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003322
Jens Axboead8a48a2019-11-15 08:49:11 -07003323 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003324 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003325 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003326 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003327 if (sqe->off && is_timeout_link)
3328 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003329 flags = READ_ONCE(sqe->timeout_flags);
3330 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003331 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003332
Jens Axboe26a61672019-12-20 09:02:01 -07003333 req->timeout.count = READ_ONCE(sqe->off);
3334
Jens Axboe3529d8c2019-12-19 18:24:38 -07003335 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003336 return -ENOMEM;
3337
3338 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003339 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003340 req->flags |= REQ_F_TIMEOUT;
3341
3342 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003343 return -EFAULT;
3344
Jens Axboe11365042019-10-16 09:08:32 -06003345 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003346 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003347 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003348 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003349
Jens Axboead8a48a2019-11-15 08:49:11 -07003350 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3351 return 0;
3352}
3353
Jens Axboefc4df992019-12-10 14:38:45 -07003354static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003355{
3356 unsigned count;
3357 struct io_ring_ctx *ctx = req->ctx;
3358 struct io_timeout_data *data;
3359 struct list_head *entry;
3360 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003361
Jens Axboe2d283902019-12-04 11:08:05 -07003362 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003363
Jens Axboe5262f562019-09-17 12:26:57 -06003364 /*
3365 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003366 * timeout event to be satisfied. If it isn't set, then this is
3367 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003368 */
Jens Axboe26a61672019-12-20 09:02:01 -07003369 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003370 if (!count) {
3371 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3372 spin_lock_irq(&ctx->completion_lock);
3373 entry = ctx->timeout_list.prev;
3374 goto add;
3375 }
Jens Axboe5262f562019-09-17 12:26:57 -06003376
3377 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003378 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003379
3380 /*
3381 * Insertion sort, ensuring the first entry in the list is always
3382 * the one we need first.
3383 */
Jens Axboe5262f562019-09-17 12:26:57 -06003384 spin_lock_irq(&ctx->completion_lock);
3385 list_for_each_prev(entry, &ctx->timeout_list) {
3386 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003387 unsigned nxt_sq_head;
3388 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003389 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003390
Jens Axboe93bd25b2019-11-11 23:34:31 -07003391 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3392 continue;
3393
yangerkun5da0fb12019-10-15 21:59:29 +08003394 /*
3395 * Since cached_sq_head + count - 1 can overflow, use type long
3396 * long to store it.
3397 */
3398 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003399 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3400 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003401
3402 /*
3403 * cached_sq_head may overflow, and it will never overflow twice
3404 * once there is some timeout req still be valid.
3405 */
3406 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003407 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003408
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003409 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003410 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003411
3412 /*
3413 * Sequence of reqs after the insert one and itself should
3414 * be adjusted because each timeout req consumes a slot.
3415 */
3416 span++;
3417 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003418 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003419 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003420add:
Jens Axboe5262f562019-09-17 12:26:57 -06003421 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003422 data->timer.function = io_timeout_fn;
3423 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003424 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003425 return 0;
3426}
3427
Jens Axboe62755e32019-10-28 21:49:21 -06003428static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003429{
Jens Axboe62755e32019-10-28 21:49:21 -06003430 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003431
Jens Axboe62755e32019-10-28 21:49:21 -06003432 return req->user_data == (unsigned long) data;
3433}
3434
Jens Axboee977d6d2019-11-05 12:39:45 -07003435static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003436{
Jens Axboe62755e32019-10-28 21:49:21 -06003437 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003438 int ret = 0;
3439
Jens Axboe62755e32019-10-28 21:49:21 -06003440 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3441 switch (cancel_ret) {
3442 case IO_WQ_CANCEL_OK:
3443 ret = 0;
3444 break;
3445 case IO_WQ_CANCEL_RUNNING:
3446 ret = -EALREADY;
3447 break;
3448 case IO_WQ_CANCEL_NOTFOUND:
3449 ret = -ENOENT;
3450 break;
3451 }
3452
Jens Axboee977d6d2019-11-05 12:39:45 -07003453 return ret;
3454}
3455
Jens Axboe47f46762019-11-09 17:43:02 -07003456static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3457 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003458 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003459{
3460 unsigned long flags;
3461 int ret;
3462
3463 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3464 if (ret != -ENOENT) {
3465 spin_lock_irqsave(&ctx->completion_lock, flags);
3466 goto done;
3467 }
3468
3469 spin_lock_irqsave(&ctx->completion_lock, flags);
3470 ret = io_timeout_cancel(ctx, sqe_addr);
3471 if (ret != -ENOENT)
3472 goto done;
3473 ret = io_poll_cancel(ctx, sqe_addr);
3474done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003475 if (!ret)
3476 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003477 io_cqring_fill_event(req, ret);
3478 io_commit_cqring(ctx);
3479 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3480 io_cqring_ev_posted(ctx);
3481
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003482 if (ret < 0)
3483 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003484 io_put_req_find_next(req, nxt);
3485}
3486
Jens Axboe3529d8c2019-12-19 18:24:38 -07003487static int io_async_cancel_prep(struct io_kiocb *req,
3488 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003489{
Jens Axboefbf23842019-12-17 18:45:56 -07003490 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003491 return -EINVAL;
3492 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3493 sqe->cancel_flags)
3494 return -EINVAL;
3495
Jens Axboefbf23842019-12-17 18:45:56 -07003496 req->cancel.addr = READ_ONCE(sqe->addr);
3497 return 0;
3498}
3499
3500static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3501{
3502 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003503
3504 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003505 return 0;
3506}
3507
Jens Axboe05f3fb32019-12-09 11:22:50 -07003508static int io_files_update_prep(struct io_kiocb *req,
3509 const struct io_uring_sqe *sqe)
3510{
3511 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3512 return -EINVAL;
3513
3514 req->files_update.offset = READ_ONCE(sqe->off);
3515 req->files_update.nr_args = READ_ONCE(sqe->len);
3516 if (!req->files_update.nr_args)
3517 return -EINVAL;
3518 req->files_update.arg = READ_ONCE(sqe->addr);
3519 return 0;
3520}
3521
3522static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3523{
3524 struct io_ring_ctx *ctx = req->ctx;
3525 struct io_uring_files_update up;
3526 int ret;
3527
3528 if (force_nonblock) {
3529 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3530 return -EAGAIN;
3531 }
3532
3533 up.offset = req->files_update.offset;
3534 up.fds = req->files_update.arg;
3535
3536 mutex_lock(&ctx->uring_lock);
3537 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3538 mutex_unlock(&ctx->uring_lock);
3539
3540 if (ret < 0)
3541 req_set_fail_links(req);
3542 io_cqring_add_event(req, ret);
3543 io_put_req(req);
3544 return 0;
3545}
3546
Jens Axboe3529d8c2019-12-19 18:24:38 -07003547static int io_req_defer_prep(struct io_kiocb *req,
3548 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003549{
Jens Axboee7815732019-12-17 19:45:06 -07003550 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003551
Jens Axboed625c6e2019-12-17 19:53:05 -07003552 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003553 case IORING_OP_NOP:
3554 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003555 case IORING_OP_READV:
3556 case IORING_OP_READ_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003557 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003558 break;
3559 case IORING_OP_WRITEV:
3560 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003561 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003562 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003563 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003564 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003565 break;
3566 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003567 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003568 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003569 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003570 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003571 break;
3572 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003573 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003574 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003575 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003576 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003577 break;
3578 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003579 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003580 break;
Jens Axboef499a022019-12-02 16:28:46 -07003581 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003582 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003583 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003584 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003585 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003586 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003587 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003588 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003589 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003590 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003591 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003592 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003593 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003594 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003595 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003596 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003597 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003598 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003599 case IORING_OP_FALLOCATE:
3600 ret = io_fallocate_prep(req, sqe);
3601 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003602 case IORING_OP_OPENAT:
3603 ret = io_openat_prep(req, sqe);
3604 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003605 case IORING_OP_CLOSE:
3606 ret = io_close_prep(req, sqe);
3607 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003608 case IORING_OP_FILES_UPDATE:
3609 ret = io_files_update_prep(req, sqe);
3610 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003611 case IORING_OP_STATX:
3612 ret = io_statx_prep(req, sqe);
3613 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003614 default:
Jens Axboee7815732019-12-17 19:45:06 -07003615 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3616 req->opcode);
3617 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003618 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003619 }
3620
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003621 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003622}
3623
Jens Axboe3529d8c2019-12-19 18:24:38 -07003624static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003625{
Jackie Liua197f662019-11-08 08:09:12 -07003626 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003627 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003628
Bob Liu9d858b22019-11-13 18:06:25 +08003629 /* Still need defer if there is pending req in defer list. */
3630 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003631 return 0;
3632
Jens Axboe3529d8c2019-12-19 18:24:38 -07003633 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003634 return -EAGAIN;
3635
Jens Axboe3529d8c2019-12-19 18:24:38 -07003636 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003637 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003638 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003639
Jens Axboede0617e2019-04-06 21:51:27 -06003640 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003641 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003642 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003643 return 0;
3644 }
3645
Jens Axboe915967f2019-11-21 09:01:20 -07003646 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003647 list_add_tail(&req->list, &ctx->defer_list);
3648 spin_unlock_irq(&ctx->completion_lock);
3649 return -EIOCBQUEUED;
3650}
3651
Jens Axboe3529d8c2019-12-19 18:24:38 -07003652static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3653 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003654{
Jackie Liua197f662019-11-08 08:09:12 -07003655 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07003656 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003657
Jens Axboed625c6e2019-12-17 19:53:05 -07003658 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003659 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003660 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003661 break;
3662 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003663 case IORING_OP_READ_FIXED:
3664 if (sqe) {
3665 ret = io_read_prep(req, sqe, force_nonblock);
3666 if (ret < 0)
3667 break;
3668 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003669 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003670 break;
3671 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07003672 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003673 if (sqe) {
3674 ret = io_write_prep(req, sqe, force_nonblock);
3675 if (ret < 0)
3676 break;
3677 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003678 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003679 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003680 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003681 if (sqe) {
3682 ret = io_prep_fsync(req, sqe);
3683 if (ret < 0)
3684 break;
3685 }
Jens Axboefc4df992019-12-10 14:38:45 -07003686 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003687 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003688 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003689 if (sqe) {
3690 ret = io_poll_add_prep(req, sqe);
3691 if (ret)
3692 break;
3693 }
Jens Axboefc4df992019-12-10 14:38:45 -07003694 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003695 break;
3696 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003697 if (sqe) {
3698 ret = io_poll_remove_prep(req, sqe);
3699 if (ret < 0)
3700 break;
3701 }
Jens Axboefc4df992019-12-10 14:38:45 -07003702 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003703 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003704 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003705 if (sqe) {
3706 ret = io_prep_sfr(req, sqe);
3707 if (ret < 0)
3708 break;
3709 }
Jens Axboefc4df992019-12-10 14:38:45 -07003710 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003711 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003712 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003713 if (sqe) {
3714 ret = io_sendmsg_prep(req, sqe);
3715 if (ret < 0)
3716 break;
3717 }
Jens Axboefc4df992019-12-10 14:38:45 -07003718 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003719 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003720 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003721 if (sqe) {
3722 ret = io_recvmsg_prep(req, sqe);
3723 if (ret)
3724 break;
3725 }
Jens Axboefc4df992019-12-10 14:38:45 -07003726 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003727 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003728 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003729 if (sqe) {
3730 ret = io_timeout_prep(req, sqe, false);
3731 if (ret)
3732 break;
3733 }
Jens Axboefc4df992019-12-10 14:38:45 -07003734 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003735 break;
Jens Axboe11365042019-10-16 09:08:32 -06003736 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003737 if (sqe) {
3738 ret = io_timeout_remove_prep(req, sqe);
3739 if (ret)
3740 break;
3741 }
Jens Axboefc4df992019-12-10 14:38:45 -07003742 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003743 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003744 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003745 if (sqe) {
3746 ret = io_accept_prep(req, sqe);
3747 if (ret)
3748 break;
3749 }
Jens Axboefc4df992019-12-10 14:38:45 -07003750 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003751 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003752 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003753 if (sqe) {
3754 ret = io_connect_prep(req, sqe);
3755 if (ret)
3756 break;
3757 }
Jens Axboefc4df992019-12-10 14:38:45 -07003758 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003759 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003760 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003761 if (sqe) {
3762 ret = io_async_cancel_prep(req, sqe);
3763 if (ret)
3764 break;
3765 }
Jens Axboefc4df992019-12-10 14:38:45 -07003766 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003767 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003768 case IORING_OP_FALLOCATE:
3769 if (sqe) {
3770 ret = io_fallocate_prep(req, sqe);
3771 if (ret)
3772 break;
3773 }
3774 ret = io_fallocate(req, nxt, force_nonblock);
3775 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003776 case IORING_OP_OPENAT:
3777 if (sqe) {
3778 ret = io_openat_prep(req, sqe);
3779 if (ret)
3780 break;
3781 }
3782 ret = io_openat(req, nxt, force_nonblock);
3783 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003784 case IORING_OP_CLOSE:
3785 if (sqe) {
3786 ret = io_close_prep(req, sqe);
3787 if (ret)
3788 break;
3789 }
3790 ret = io_close(req, nxt, force_nonblock);
3791 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003792 case IORING_OP_FILES_UPDATE:
3793 if (sqe) {
3794 ret = io_files_update_prep(req, sqe);
3795 if (ret)
3796 break;
3797 }
3798 ret = io_files_update(req, force_nonblock);
3799 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003800 case IORING_OP_STATX:
3801 if (sqe) {
3802 ret = io_statx_prep(req, sqe);
3803 if (ret)
3804 break;
3805 }
3806 ret = io_statx(req, nxt, force_nonblock);
3807 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003808 default:
3809 ret = -EINVAL;
3810 break;
3811 }
3812
Jens Axboedef596e2019-01-09 08:59:42 -07003813 if (ret)
3814 return ret;
3815
3816 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07003817 const bool in_async = io_wq_current_is_worker();
3818
Jens Axboe9e645e112019-05-10 16:07:28 -06003819 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07003820 return -EAGAIN;
3821
Jens Axboe11ba8202020-01-15 21:51:17 -07003822 /* workqueue context doesn't hold uring_lock, grab it now */
3823 if (in_async)
3824 mutex_lock(&ctx->uring_lock);
3825
Jens Axboedef596e2019-01-09 08:59:42 -07003826 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07003827
3828 if (in_async)
3829 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07003830 }
3831
3832 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003833}
3834
Jens Axboe561fb042019-10-24 07:25:42 -06003835static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003836{
Jens Axboe561fb042019-10-24 07:25:42 -06003837 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003838 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003839 struct io_kiocb *nxt = NULL;
3840 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003841
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003842 /* if NO_CANCEL is set, we must still run the work */
3843 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
3844 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003845 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003846 }
Jens Axboe31b51512019-01-18 22:56:34 -07003847
Jens Axboe561fb042019-10-24 07:25:42 -06003848 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003849 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3850 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003851 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003852 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003853 /*
3854 * We can get EAGAIN for polled IO even though we're
3855 * forcing a sync submission from here, since we can't
3856 * wait for request slots on the block side.
3857 */
3858 if (ret != -EAGAIN)
3859 break;
3860 cond_resched();
3861 } while (1);
3862 }
Jens Axboe31b51512019-01-18 22:56:34 -07003863
Jens Axboe561fb042019-10-24 07:25:42 -06003864 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003865 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003866
Jens Axboe561fb042019-10-24 07:25:42 -06003867 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003868 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003869 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003870 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003871 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003872
Jens Axboe561fb042019-10-24 07:25:42 -06003873 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07003874 if (!ret && nxt)
3875 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07003876}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003877
Jens Axboe15b71ab2019-12-11 11:20:36 -07003878static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06003879{
Jens Axboed3656342019-12-18 09:50:26 -07003880 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07003881 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07003882 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
3883 return 0;
3884 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06003885}
3886
Jens Axboe65e19f52019-10-26 07:20:21 -06003887static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3888 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003889{
Jens Axboe65e19f52019-10-26 07:20:21 -06003890 struct fixed_file_table *table;
3891
Jens Axboe05f3fb32019-12-09 11:22:50 -07003892 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
3893 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06003894}
3895
Jens Axboe3529d8c2019-12-19 18:24:38 -07003896static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
3897 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06003898{
Jackie Liua197f662019-11-08 08:09:12 -07003899 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003900 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07003901 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06003902
Jens Axboe3529d8c2019-12-19 18:24:38 -07003903 flags = READ_ONCE(sqe->flags);
3904 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003905
Jackie Liu4fe2c962019-09-09 20:50:40 +08003906 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003907 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003908
Jens Axboed3656342019-12-18 09:50:26 -07003909 if (!io_req_needs_file(req, fd))
3910 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06003911
3912 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07003913 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003914 (unsigned) fd >= ctx->nr_user_files))
3915 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003916 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003917 req->file = io_file_from_index(ctx, fd);
3918 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003919 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003920 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003921 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06003922 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003923 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003924 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003925 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003926 req->file = io_file_get(state, fd);
3927 if (unlikely(!req->file))
3928 return -EBADF;
3929 }
3930
3931 return 0;
3932}
3933
Jackie Liua197f662019-11-08 08:09:12 -07003934static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003935{
Jens Axboefcb323c2019-10-24 12:39:47 -06003936 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003937 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003938
Jens Axboeb5dba592019-12-11 14:02:38 -07003939 if (!req->ring_file)
3940 return -EBADF;
3941
Jens Axboefcb323c2019-10-24 12:39:47 -06003942 rcu_read_lock();
3943 spin_lock_irq(&ctx->inflight_lock);
3944 /*
3945 * We use the f_ops->flush() handler to ensure that we can flush
3946 * out work accessing these files if the fd is closed. Check if
3947 * the fd has changed since we started down this path, and disallow
3948 * this operation if it has.
3949 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003950 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003951 list_add(&req->inflight_entry, &ctx->inflight_list);
3952 req->flags |= REQ_F_INFLIGHT;
3953 req->work.files = current->files;
3954 ret = 0;
3955 }
3956 spin_unlock_irq(&ctx->inflight_lock);
3957 rcu_read_unlock();
3958
3959 return ret;
3960}
3961
Jens Axboe2665abf2019-11-05 12:40:47 -07003962static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3963{
Jens Axboead8a48a2019-11-15 08:49:11 -07003964 struct io_timeout_data *data = container_of(timer,
3965 struct io_timeout_data, timer);
3966 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003967 struct io_ring_ctx *ctx = req->ctx;
3968 struct io_kiocb *prev = NULL;
3969 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003970
3971 spin_lock_irqsave(&ctx->completion_lock, flags);
3972
3973 /*
3974 * We don't expect the list to be empty, that will only happen if we
3975 * race with the completion of the linked work.
3976 */
Pavel Begunkov44932332019-12-05 16:16:35 +03003977 if (!list_empty(&req->link_list)) {
3978 prev = list_entry(req->link_list.prev, struct io_kiocb,
3979 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003980 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03003981 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003982 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3983 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003984 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003985 }
3986
3987 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3988
3989 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003990 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003991 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3992 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003993 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003994 } else {
3995 io_cqring_add_event(req, -ETIME);
3996 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003997 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003998 return HRTIMER_NORESTART;
3999}
4000
Jens Axboead8a48a2019-11-15 08:49:11 -07004001static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004002{
Jens Axboe76a46e02019-11-10 23:34:16 -07004003 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004004
Jens Axboe76a46e02019-11-10 23:34:16 -07004005 /*
4006 * If the list is now empty, then our linked request finished before
4007 * we got a chance to setup the timer
4008 */
4009 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004010 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004011 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004012
Jens Axboead8a48a2019-11-15 08:49:11 -07004013 data->timer.function = io_link_timeout_fn;
4014 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4015 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004016 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004017 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004018
Jens Axboe2665abf2019-11-05 12:40:47 -07004019 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004020 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004021}
4022
Jens Axboead8a48a2019-11-15 08:49:11 -07004023static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004024{
4025 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004026
Jens Axboe2665abf2019-11-05 12:40:47 -07004027 if (!(req->flags & REQ_F_LINK))
4028 return NULL;
4029
Pavel Begunkov44932332019-12-05 16:16:35 +03004030 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4031 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004032 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004033 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004034
Jens Axboe76a46e02019-11-10 23:34:16 -07004035 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004036 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004037}
4038
Jens Axboe3529d8c2019-12-19 18:24:38 -07004039static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004040{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004041 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004042 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004043 int ret;
4044
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004045again:
4046 linked_timeout = io_prep_linked_timeout(req);
4047
Jens Axboe3529d8c2019-12-19 18:24:38 -07004048 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004049
4050 /*
4051 * We async punt it if the file wasn't marked NOWAIT, or if the file
4052 * doesn't support non-blocking read/write attempts
4053 */
4054 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4055 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004056 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4057 ret = io_grab_files(req);
4058 if (ret)
4059 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004060 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004061
4062 /*
4063 * Queued up for async execution, worker will release
4064 * submit reference when the iocb is actually submitted.
4065 */
4066 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004067 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004068 }
Jens Axboee65ef562019-03-12 10:16:44 -06004069
Jens Axboefcb323c2019-10-24 12:39:47 -06004070err:
Jens Axboee65ef562019-03-12 10:16:44 -06004071 /* drop submission reference */
4072 io_put_req(req);
4073
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004074 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004075 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004076 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004077 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004078 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004079 }
4080
Jens Axboee65ef562019-03-12 10:16:44 -06004081 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004082 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004083 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004084 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004085 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004086 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004087done_req:
4088 if (nxt) {
4089 req = nxt;
4090 nxt = NULL;
4091 goto again;
4092 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004093}
4094
Jens Axboe3529d8c2019-12-19 18:24:38 -07004095static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004096{
4097 int ret;
4098
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004099 if (unlikely(req->ctx->drain_next)) {
4100 req->flags |= REQ_F_IO_DRAIN;
4101 req->ctx->drain_next = false;
4102 }
4103 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
4104
Jens Axboe3529d8c2019-12-19 18:24:38 -07004105 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004106 if (ret) {
4107 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004108 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004109 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004110 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004111 }
Jens Axboece35a472019-12-17 08:04:44 -07004112 } else if ((req->flags & REQ_F_FORCE_ASYNC) &&
4113 !io_wq_current_is_worker()) {
4114 /*
4115 * Never try inline submit of IOSQE_ASYNC is set, go straight
4116 * to async execution.
4117 */
4118 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4119 io_queue_async_work(req);
4120 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004121 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004122 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004123}
4124
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004125static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004126{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004127 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004128 io_cqring_add_event(req, -ECANCELED);
4129 io_double_put_req(req);
4130 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004131 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004132}
4133
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004134#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004135 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004136
Jens Axboe3529d8c2019-12-19 18:24:38 -07004137static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4138 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004139{
Jackie Liua197f662019-11-08 08:09:12 -07004140 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004141 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004142 int ret;
4143
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004144 sqe_flags = READ_ONCE(sqe->flags);
4145
Jens Axboe9e645e112019-05-10 16:07:28 -06004146 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004147 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004148 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004149 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004150 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004151 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004152 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004153
Jens Axboe3529d8c2019-12-19 18:24:38 -07004154 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004155 if (unlikely(ret)) {
4156err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004157 io_cqring_add_event(req, ret);
4158 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004159 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004160 }
4161
Jens Axboe9e645e112019-05-10 16:07:28 -06004162 /*
4163 * If we already have a head request, queue this one for async
4164 * submittal once the head completes. If we don't have a head but
4165 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4166 * submitted sync once the chain is complete. If none of those
4167 * conditions are true (normal request), then just queue it.
4168 */
4169 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004170 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004171
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004172 if (sqe_flags & IOSQE_IO_DRAIN)
Pavel Begunkov9d763772019-12-17 02:22:07 +03004173 head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004174
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004175 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004176 req->flags |= REQ_F_HARDLINK;
4177
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004178 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004179 ret = -EAGAIN;
4180 goto err_req;
4181 }
4182
Jens Axboe3529d8c2019-12-19 18:24:38 -07004183 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004184 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004185 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004186 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004187 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004188 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004189 trace_io_uring_link(ctx, req, head);
4190 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004191
4192 /* last request of a link, enqueue the link */
4193 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4194 io_queue_link_head(head);
4195 *link = NULL;
4196 }
4197 } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004198 req->flags |= REQ_F_LINK;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004199 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004200 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004201
Jens Axboe9e645e112019-05-10 16:07:28 -06004202 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004203 ret = io_req_defer_prep(req, sqe);
4204 if (ret)
4205 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004206 *link = req;
4207 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004208 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004209 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004210
4211 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004212}
4213
Jens Axboe9a56a232019-01-09 09:06:50 -07004214/*
4215 * Batched submission is done, ensure local IO is flushed out.
4216 */
4217static void io_submit_state_end(struct io_submit_state *state)
4218{
4219 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004220 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004221 if (state->free_reqs)
4222 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4223 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004224}
4225
4226/*
4227 * Start submission side cache.
4228 */
4229static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004230 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004231{
4232 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004233 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004234 state->file = NULL;
4235 state->ios_left = max_ios;
4236}
4237
Jens Axboe2b188cc2019-01-07 10:46:33 -07004238static void io_commit_sqring(struct io_ring_ctx *ctx)
4239{
Hristo Venev75b28af2019-08-26 17:23:46 +00004240 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004241
Hristo Venev75b28af2019-08-26 17:23:46 +00004242 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004243 /*
4244 * Ensure any loads from the SQEs are done at this point,
4245 * since once we write the new head, the application could
4246 * write new data to them.
4247 */
Hristo Venev75b28af2019-08-26 17:23:46 +00004248 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004249 }
4250}
4251
4252/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004253 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004254 * that is mapped by userspace. This means that care needs to be taken to
4255 * ensure that reads are stable, as we cannot rely on userspace always
4256 * being a good citizen. If members of the sqe are validated and then later
4257 * used, it's important that those reads are done through READ_ONCE() to
4258 * prevent a re-load down the line.
4259 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004260static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4261 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004262{
Hristo Venev75b28af2019-08-26 17:23:46 +00004263 struct io_rings *rings = ctx->rings;
4264 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004265 unsigned head;
4266
4267 /*
4268 * The cached sq head (or cq tail) serves two purposes:
4269 *
4270 * 1) allows us to batch the cost of updating the user visible
4271 * head updates.
4272 * 2) allows the kernel side to track the head on its own, even
4273 * though the application is the one updating it.
4274 */
4275 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02004276 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004277 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004278 return false;
4279
Hristo Venev75b28af2019-08-26 17:23:46 +00004280 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004281 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004282 /*
4283 * All io need record the previous position, if LINK vs DARIN,
4284 * it can be used to mark the position of the first IO in the
4285 * link list.
4286 */
4287 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004288 *sqe_ptr = &ctx->sq_sqes[head];
4289 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4290 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004291 ctx->cached_sq_head++;
4292 return true;
4293 }
4294
4295 /* drop invalid entries */
4296 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004297 ctx->cached_sq_dropped++;
4298 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004299 return false;
4300}
4301
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004302static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004303 struct file *ring_file, int ring_fd,
4304 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004305{
4306 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004307 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004308 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004309 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004310
Jens Axboec4a2ed72019-11-21 21:01:26 -07004311 /* if we have a backlog and couldn't flush it all, return BUSY */
4312 if (!list_empty(&ctx->cq_overflow_list) &&
4313 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004314 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004315
4316 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004317 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004318 statep = &state;
4319 }
4320
4321 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004322 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004323 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004324
Pavel Begunkov196be952019-11-07 01:41:06 +03004325 req = io_get_req(ctx, statep);
4326 if (unlikely(!req)) {
4327 if (!submitted)
4328 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004329 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004330 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004331 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03004332 __io_free_req(req);
4333 break;
4334 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004335
Jens Axboed3656342019-12-18 09:50:26 -07004336 /* will complete beyond this point, count as submitted */
4337 submitted++;
4338
4339 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4340 io_cqring_add_event(req, -EINVAL);
4341 io_double_put_req(req);
4342 break;
4343 }
4344
4345 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004346 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4347 if (!mm_fault) {
4348 use_mm(ctx->sqo_mm);
4349 *mm = ctx->sqo_mm;
4350 }
4351 }
4352
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004353 req->ring_file = ring_file;
4354 req->ring_fd = ring_fd;
4355 req->has_user = *mm != NULL;
4356 req->in_async = async;
4357 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07004358 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004359 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004360 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004361 }
4362
Jens Axboe9e645e112019-05-10 16:07:28 -06004363 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004364 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004365 if (statep)
4366 io_submit_state_end(&state);
4367
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004368 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4369 io_commit_sqring(ctx);
4370
Jens Axboe6c271ce2019-01-10 11:22:30 -07004371 return submitted;
4372}
4373
4374static int io_sq_thread(void *data)
4375{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004376 struct io_ring_ctx *ctx = data;
4377 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004378 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004379 mm_segment_t old_fs;
4380 DEFINE_WAIT(wait);
4381 unsigned inflight;
4382 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004383 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004384
Jens Axboe206aefd2019-11-07 18:27:42 -07004385 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004386
Jens Axboe6c271ce2019-01-10 11:22:30 -07004387 old_fs = get_fs();
4388 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004389 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004390
Jens Axboec1edbf52019-11-10 16:56:04 -07004391 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004392 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004393 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004394
4395 if (inflight) {
4396 unsigned nr_events = 0;
4397
4398 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004399 /*
4400 * inflight is the count of the maximum possible
4401 * entries we submitted, but it can be smaller
4402 * if we dropped some of them. If we don't have
4403 * poll entries available, then we know that we
4404 * have nothing left to poll for. Reset the
4405 * inflight count to zero in that case.
4406 */
4407 mutex_lock(&ctx->uring_lock);
4408 if (!list_empty(&ctx->poll_list))
4409 __io_iopoll_check(ctx, &nr_events, 0);
4410 else
4411 inflight = 0;
4412 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004413 } else {
4414 /*
4415 * Normal IO, just pretend everything completed.
4416 * We don't have to poll completions for that.
4417 */
4418 nr_events = inflight;
4419 }
4420
4421 inflight -= nr_events;
4422 if (!inflight)
4423 timeout = jiffies + ctx->sq_thread_idle;
4424 }
4425
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004426 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004427
4428 /*
4429 * If submit got -EBUSY, flag us as needing the application
4430 * to enter the kernel to reap and flush events.
4431 */
4432 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004433 /*
4434 * We're polling. If we're within the defined idle
4435 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004436 * to sleep. The exception is if we got EBUSY doing
4437 * more IO, we should wait for the application to
4438 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004439 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004440 if (inflight ||
4441 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004442 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004443 continue;
4444 }
4445
4446 /*
4447 * Drop cur_mm before scheduling, we can't hold it for
4448 * long periods (or over schedule()). Do this before
4449 * adding ourselves to the waitqueue, as the unuse/drop
4450 * may sleep.
4451 */
4452 if (cur_mm) {
4453 unuse_mm(cur_mm);
4454 mmput(cur_mm);
4455 cur_mm = NULL;
4456 }
4457
4458 prepare_to_wait(&ctx->sqo_wait, &wait,
4459 TASK_INTERRUPTIBLE);
4460
4461 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004462 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004463 /* make sure to read SQ tail after writing flags */
4464 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004465
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004466 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004467 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004468 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004469 finish_wait(&ctx->sqo_wait, &wait);
4470 break;
4471 }
4472 if (signal_pending(current))
4473 flush_signals(current);
4474 schedule();
4475 finish_wait(&ctx->sqo_wait, &wait);
4476
Hristo Venev75b28af2019-08-26 17:23:46 +00004477 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004478 continue;
4479 }
4480 finish_wait(&ctx->sqo_wait, &wait);
4481
Hristo Venev75b28af2019-08-26 17:23:46 +00004482 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004483 }
4484
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004485 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004486 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004487 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004488 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004489 if (ret > 0)
4490 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004491 }
4492
4493 set_fs(old_fs);
4494 if (cur_mm) {
4495 unuse_mm(cur_mm);
4496 mmput(cur_mm);
4497 }
Jens Axboe181e4482019-11-25 08:52:30 -07004498 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004499
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004500 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004501
Jens Axboe6c271ce2019-01-10 11:22:30 -07004502 return 0;
4503}
4504
Jens Axboebda52162019-09-24 13:47:15 -06004505struct io_wait_queue {
4506 struct wait_queue_entry wq;
4507 struct io_ring_ctx *ctx;
4508 unsigned to_wait;
4509 unsigned nr_timeouts;
4510};
4511
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004512static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004513{
4514 struct io_ring_ctx *ctx = iowq->ctx;
4515
4516 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004517 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004518 * started waiting. For timeouts, we always want to return to userspace,
4519 * regardless of event count.
4520 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004521 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004522 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4523}
4524
4525static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4526 int wake_flags, void *key)
4527{
4528 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4529 wq);
4530
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004531 /* use noflush == true, as we can't safely rely on locking context */
4532 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004533 return -1;
4534
4535 return autoremove_wake_function(curr, mode, wake_flags, key);
4536}
4537
Jens Axboe2b188cc2019-01-07 10:46:33 -07004538/*
4539 * Wait until events become available, if we don't already have some. The
4540 * application must reap them itself, as they reside on the shared cq ring.
4541 */
4542static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4543 const sigset_t __user *sig, size_t sigsz)
4544{
Jens Axboebda52162019-09-24 13:47:15 -06004545 struct io_wait_queue iowq = {
4546 .wq = {
4547 .private = current,
4548 .func = io_wake_function,
4549 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4550 },
4551 .ctx = ctx,
4552 .to_wait = min_events,
4553 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004554 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004555 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004556
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004557 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004558 return 0;
4559
4560 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004561#ifdef CONFIG_COMPAT
4562 if (in_compat_syscall())
4563 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004564 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004565 else
4566#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004567 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004568
Jens Axboe2b188cc2019-01-07 10:46:33 -07004569 if (ret)
4570 return ret;
4571 }
4572
Jens Axboebda52162019-09-24 13:47:15 -06004573 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004574 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004575 do {
4576 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4577 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004578 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004579 break;
4580 schedule();
4581 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004582 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004583 break;
4584 }
4585 } while (1);
4586 finish_wait(&ctx->wait, &iowq.wq);
4587
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004588 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004589
Hristo Venev75b28af2019-08-26 17:23:46 +00004590 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004591}
4592
Jens Axboe6b063142019-01-10 22:13:58 -07004593static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4594{
4595#if defined(CONFIG_UNIX)
4596 if (ctx->ring_sock) {
4597 struct sock *sock = ctx->ring_sock->sk;
4598 struct sk_buff *skb;
4599
4600 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4601 kfree_skb(skb);
4602 }
4603#else
4604 int i;
4605
Jens Axboe65e19f52019-10-26 07:20:21 -06004606 for (i = 0; i < ctx->nr_user_files; i++) {
4607 struct file *file;
4608
4609 file = io_file_from_index(ctx, i);
4610 if (file)
4611 fput(file);
4612 }
Jens Axboe6b063142019-01-10 22:13:58 -07004613#endif
4614}
4615
Jens Axboe05f3fb32019-12-09 11:22:50 -07004616static void io_file_ref_kill(struct percpu_ref *ref)
4617{
4618 struct fixed_file_data *data;
4619
4620 data = container_of(ref, struct fixed_file_data, refs);
4621 complete(&data->done);
4622}
4623
Jens Axboe6b063142019-01-10 22:13:58 -07004624static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4625{
Jens Axboe05f3fb32019-12-09 11:22:50 -07004626 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06004627 unsigned nr_tables, i;
4628
Jens Axboe05f3fb32019-12-09 11:22:50 -07004629 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07004630 return -ENXIO;
4631
Jens Axboe05f3fb32019-12-09 11:22:50 -07004632 /* protect against inflight atomic switch, which drops the ref */
4633 flush_work(&data->ref_work);
4634 percpu_ref_get(&data->refs);
4635 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
4636 wait_for_completion(&data->done);
4637 percpu_ref_put(&data->refs);
4638 percpu_ref_exit(&data->refs);
4639
Jens Axboe6b063142019-01-10 22:13:58 -07004640 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004641 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4642 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004643 kfree(data->table[i].files);
4644 kfree(data->table);
4645 kfree(data);
4646 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004647 ctx->nr_user_files = 0;
4648 return 0;
4649}
4650
Jens Axboe6c271ce2019-01-10 11:22:30 -07004651static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4652{
4653 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004654 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004655 /*
4656 * The park is a bit of a work-around, without it we get
4657 * warning spews on shutdown with SQPOLL set and affinity
4658 * set to a single CPU.
4659 */
Jens Axboe06058632019-04-13 09:26:03 -06004660 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004661 kthread_stop(ctx->sqo_thread);
4662 ctx->sqo_thread = NULL;
4663 }
4664}
4665
Jens Axboe6b063142019-01-10 22:13:58 -07004666static void io_finish_async(struct io_ring_ctx *ctx)
4667{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004668 io_sq_thread_stop(ctx);
4669
Jens Axboe561fb042019-10-24 07:25:42 -06004670 if (ctx->io_wq) {
4671 io_wq_destroy(ctx->io_wq);
4672 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004673 }
4674}
4675
4676#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07004677/*
4678 * Ensure the UNIX gc is aware of our file set, so we are certain that
4679 * the io_uring can be safely unregistered on process exit, even if we have
4680 * loops in the file referencing.
4681 */
4682static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4683{
4684 struct sock *sk = ctx->ring_sock->sk;
4685 struct scm_fp_list *fpl;
4686 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004687 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004688
4689 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4690 unsigned long inflight = ctx->user->unix_inflight + nr;
4691
4692 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4693 return -EMFILE;
4694 }
4695
4696 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4697 if (!fpl)
4698 return -ENOMEM;
4699
4700 skb = alloc_skb(0, GFP_KERNEL);
4701 if (!skb) {
4702 kfree(fpl);
4703 return -ENOMEM;
4704 }
4705
4706 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004707
Jens Axboe08a45172019-10-03 08:11:03 -06004708 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004709 fpl->user = get_uid(ctx->user);
4710 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004711 struct file *file = io_file_from_index(ctx, i + offset);
4712
4713 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004714 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004715 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004716 unix_inflight(fpl->user, fpl->fp[nr_files]);
4717 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004718 }
4719
Jens Axboe08a45172019-10-03 08:11:03 -06004720 if (nr_files) {
4721 fpl->max = SCM_MAX_FD;
4722 fpl->count = nr_files;
4723 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004724 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06004725 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4726 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004727
Jens Axboe08a45172019-10-03 08:11:03 -06004728 for (i = 0; i < nr_files; i++)
4729 fput(fpl->fp[i]);
4730 } else {
4731 kfree_skb(skb);
4732 kfree(fpl);
4733 }
Jens Axboe6b063142019-01-10 22:13:58 -07004734
4735 return 0;
4736}
4737
4738/*
4739 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4740 * causes regular reference counting to break down. We rely on the UNIX
4741 * garbage collection to take care of this problem for us.
4742 */
4743static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4744{
4745 unsigned left, total;
4746 int ret = 0;
4747
4748 total = 0;
4749 left = ctx->nr_user_files;
4750 while (left) {
4751 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004752
4753 ret = __io_sqe_files_scm(ctx, this_files, total);
4754 if (ret)
4755 break;
4756 left -= this_files;
4757 total += this_files;
4758 }
4759
4760 if (!ret)
4761 return 0;
4762
4763 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004764 struct file *file = io_file_from_index(ctx, total);
4765
4766 if (file)
4767 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004768 total++;
4769 }
4770
4771 return ret;
4772}
4773#else
4774static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4775{
4776 return 0;
4777}
4778#endif
4779
Jens Axboe65e19f52019-10-26 07:20:21 -06004780static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4781 unsigned nr_files)
4782{
4783 int i;
4784
4785 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004786 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004787 unsigned this_files;
4788
4789 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4790 table->files = kcalloc(this_files, sizeof(struct file *),
4791 GFP_KERNEL);
4792 if (!table->files)
4793 break;
4794 nr_files -= this_files;
4795 }
4796
4797 if (i == nr_tables)
4798 return 0;
4799
4800 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004801 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004802 kfree(table->files);
4803 }
4804 return 1;
4805}
4806
Jens Axboe05f3fb32019-12-09 11:22:50 -07004807static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06004808{
4809#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06004810 struct sock *sock = ctx->ring_sock->sk;
4811 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4812 struct sk_buff *skb;
4813 int i;
4814
4815 __skb_queue_head_init(&list);
4816
4817 /*
4818 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4819 * remove this entry and rearrange the file array.
4820 */
4821 skb = skb_dequeue(head);
4822 while (skb) {
4823 struct scm_fp_list *fp;
4824
4825 fp = UNIXCB(skb).fp;
4826 for (i = 0; i < fp->count; i++) {
4827 int left;
4828
4829 if (fp->fp[i] != file)
4830 continue;
4831
4832 unix_notinflight(fp->user, fp->fp[i]);
4833 left = fp->count - 1 - i;
4834 if (left) {
4835 memmove(&fp->fp[i], &fp->fp[i + 1],
4836 left * sizeof(struct file *));
4837 }
4838 fp->count--;
4839 if (!fp->count) {
4840 kfree_skb(skb);
4841 skb = NULL;
4842 } else {
4843 __skb_queue_tail(&list, skb);
4844 }
4845 fput(file);
4846 file = NULL;
4847 break;
4848 }
4849
4850 if (!file)
4851 break;
4852
4853 __skb_queue_tail(&list, skb);
4854
4855 skb = skb_dequeue(head);
4856 }
4857
4858 if (skb_peek(&list)) {
4859 spin_lock_irq(&head->lock);
4860 while ((skb = __skb_dequeue(&list)) != NULL)
4861 __skb_queue_tail(head, skb);
4862 spin_unlock_irq(&head->lock);
4863 }
4864#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07004865 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06004866#endif
4867}
4868
Jens Axboe05f3fb32019-12-09 11:22:50 -07004869struct io_file_put {
4870 struct llist_node llist;
4871 struct file *file;
4872 struct completion *done;
4873};
4874
4875static void io_ring_file_ref_switch(struct work_struct *work)
4876{
4877 struct io_file_put *pfile, *tmp;
4878 struct fixed_file_data *data;
4879 struct llist_node *node;
4880
4881 data = container_of(work, struct fixed_file_data, ref_work);
4882
4883 while ((node = llist_del_all(&data->put_llist)) != NULL) {
4884 llist_for_each_entry_safe(pfile, tmp, node, llist) {
4885 io_ring_file_put(data->ctx, pfile->file);
4886 if (pfile->done)
4887 complete(pfile->done);
4888 else
4889 kfree(pfile);
4890 }
4891 }
4892
4893 percpu_ref_get(&data->refs);
4894 percpu_ref_switch_to_percpu(&data->refs);
4895}
4896
4897static void io_file_data_ref_zero(struct percpu_ref *ref)
4898{
4899 struct fixed_file_data *data;
4900
4901 data = container_of(ref, struct fixed_file_data, refs);
4902
4903 /* we can't safely switch from inside this context, punt to wq */
4904 queue_work(system_wq, &data->ref_work);
4905}
4906
4907static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
4908 unsigned nr_args)
4909{
4910 __s32 __user *fds = (__s32 __user *) arg;
4911 unsigned nr_tables;
4912 struct file *file;
4913 int fd, ret = 0;
4914 unsigned i;
4915
4916 if (ctx->file_data)
4917 return -EBUSY;
4918 if (!nr_args)
4919 return -EINVAL;
4920 if (nr_args > IORING_MAX_FIXED_FILES)
4921 return -EMFILE;
4922
4923 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
4924 if (!ctx->file_data)
4925 return -ENOMEM;
4926 ctx->file_data->ctx = ctx;
4927 init_completion(&ctx->file_data->done);
4928
4929 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
4930 ctx->file_data->table = kcalloc(nr_tables,
4931 sizeof(struct fixed_file_table),
4932 GFP_KERNEL);
4933 if (!ctx->file_data->table) {
4934 kfree(ctx->file_data);
4935 ctx->file_data = NULL;
4936 return -ENOMEM;
4937 }
4938
4939 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
4940 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
4941 kfree(ctx->file_data->table);
4942 kfree(ctx->file_data);
4943 ctx->file_data = NULL;
4944 return -ENOMEM;
4945 }
4946 ctx->file_data->put_llist.first = NULL;
4947 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
4948
4949 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
4950 percpu_ref_exit(&ctx->file_data->refs);
4951 kfree(ctx->file_data->table);
4952 kfree(ctx->file_data);
4953 ctx->file_data = NULL;
4954 return -ENOMEM;
4955 }
4956
4957 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
4958 struct fixed_file_table *table;
4959 unsigned index;
4960
4961 ret = -EFAULT;
4962 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
4963 break;
4964 /* allow sparse sets */
4965 if (fd == -1) {
4966 ret = 0;
4967 continue;
4968 }
4969
4970 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
4971 index = i & IORING_FILE_TABLE_MASK;
4972 file = fget(fd);
4973
4974 ret = -EBADF;
4975 if (!file)
4976 break;
4977
4978 /*
4979 * Don't allow io_uring instances to be registered. If UNIX
4980 * isn't enabled, then this causes a reference cycle and this
4981 * instance can never get freed. If UNIX is enabled we'll
4982 * handle it just fine, but there's still no point in allowing
4983 * a ring fd as it doesn't support regular read/write anyway.
4984 */
4985 if (file->f_op == &io_uring_fops) {
4986 fput(file);
4987 break;
4988 }
4989 ret = 0;
4990 table->files[index] = file;
4991 }
4992
4993 if (ret) {
4994 for (i = 0; i < ctx->nr_user_files; i++) {
4995 file = io_file_from_index(ctx, i);
4996 if (file)
4997 fput(file);
4998 }
4999 for (i = 0; i < nr_tables; i++)
5000 kfree(ctx->file_data->table[i].files);
5001
5002 kfree(ctx->file_data->table);
5003 kfree(ctx->file_data);
5004 ctx->file_data = NULL;
5005 ctx->nr_user_files = 0;
5006 return ret;
5007 }
5008
5009 ret = io_sqe_files_scm(ctx);
5010 if (ret)
5011 io_sqe_files_unregister(ctx);
5012
5013 return ret;
5014}
5015
Jens Axboec3a31e62019-10-03 13:59:56 -06005016static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5017 int index)
5018{
5019#if defined(CONFIG_UNIX)
5020 struct sock *sock = ctx->ring_sock->sk;
5021 struct sk_buff_head *head = &sock->sk_receive_queue;
5022 struct sk_buff *skb;
5023
5024 /*
5025 * See if we can merge this file into an existing skb SCM_RIGHTS
5026 * file set. If there's no room, fall back to allocating a new skb
5027 * and filling it in.
5028 */
5029 spin_lock_irq(&head->lock);
5030 skb = skb_peek(head);
5031 if (skb) {
5032 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5033
5034 if (fpl->count < SCM_MAX_FD) {
5035 __skb_unlink(skb, head);
5036 spin_unlock_irq(&head->lock);
5037 fpl->fp[fpl->count] = get_file(file);
5038 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5039 fpl->count++;
5040 spin_lock_irq(&head->lock);
5041 __skb_queue_head(head, skb);
5042 } else {
5043 skb = NULL;
5044 }
5045 }
5046 spin_unlock_irq(&head->lock);
5047
5048 if (skb) {
5049 fput(file);
5050 return 0;
5051 }
5052
5053 return __io_sqe_files_scm(ctx, 1, index);
5054#else
5055 return 0;
5056#endif
5057}
5058
Jens Axboe05f3fb32019-12-09 11:22:50 -07005059static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005060{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005061 struct fixed_file_data *data;
5062
5063 data = container_of(ref, struct fixed_file_data, refs);
5064 clear_bit(FFD_F_ATOMIC, &data->state);
5065}
5066
5067static bool io_queue_file_removal(struct fixed_file_data *data,
5068 struct file *file)
5069{
5070 struct io_file_put *pfile, pfile_stack;
5071 DECLARE_COMPLETION_ONSTACK(done);
5072
5073 /*
5074 * If we fail allocating the struct we need for doing async reomval
5075 * of this file, just punt to sync and wait for it.
5076 */
5077 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5078 if (!pfile) {
5079 pfile = &pfile_stack;
5080 pfile->done = &done;
5081 }
5082
5083 pfile->file = file;
5084 llist_add(&pfile->llist, &data->put_llist);
5085
5086 if (pfile == &pfile_stack) {
5087 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5088 percpu_ref_put(&data->refs);
5089 percpu_ref_switch_to_atomic(&data->refs,
5090 io_atomic_switch);
5091 }
5092 wait_for_completion(&done);
5093 flush_work(&data->ref_work);
5094 return false;
5095 }
5096
5097 return true;
5098}
5099
5100static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5101 struct io_uring_files_update *up,
5102 unsigned nr_args)
5103{
5104 struct fixed_file_data *data = ctx->file_data;
5105 bool ref_switch = false;
5106 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005107 __s32 __user *fds;
5108 int fd, i, err;
5109 __u32 done;
5110
Jens Axboe05f3fb32019-12-09 11:22:50 -07005111 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005112 return -EOVERFLOW;
5113 if (done > ctx->nr_user_files)
5114 return -EINVAL;
5115
5116 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005117 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005118 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005119 struct fixed_file_table *table;
5120 unsigned index;
5121
Jens Axboec3a31e62019-10-03 13:59:56 -06005122 err = 0;
5123 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5124 err = -EFAULT;
5125 break;
5126 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005127 i = array_index_nospec(up->offset, ctx->nr_user_files);
5128 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005129 index = i & IORING_FILE_TABLE_MASK;
5130 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005131 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005132 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005133 if (io_queue_file_removal(data, file))
5134 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005135 }
5136 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005137 file = fget(fd);
5138 if (!file) {
5139 err = -EBADF;
5140 break;
5141 }
5142 /*
5143 * Don't allow io_uring instances to be registered. If
5144 * UNIX isn't enabled, then this causes a reference
5145 * cycle and this instance can never get freed. If UNIX
5146 * is enabled we'll handle it just fine, but there's
5147 * still no point in allowing a ring fd as it doesn't
5148 * support regular read/write anyway.
5149 */
5150 if (file->f_op == &io_uring_fops) {
5151 fput(file);
5152 err = -EBADF;
5153 break;
5154 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005155 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005156 err = io_sqe_file_register(ctx, file, i);
5157 if (err)
5158 break;
5159 }
5160 nr_args--;
5161 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005162 up->offset++;
5163 }
5164
5165 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5166 percpu_ref_put(&data->refs);
5167 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005168 }
5169
5170 return done ? done : err;
5171}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005172static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5173 unsigned nr_args)
5174{
5175 struct io_uring_files_update up;
5176
5177 if (!ctx->file_data)
5178 return -ENXIO;
5179 if (!nr_args)
5180 return -EINVAL;
5181 if (copy_from_user(&up, arg, sizeof(up)))
5182 return -EFAULT;
5183 if (up.resv)
5184 return -EINVAL;
5185
5186 return __io_sqe_files_update(ctx, &up, nr_args);
5187}
Jens Axboec3a31e62019-10-03 13:59:56 -06005188
Jens Axboe7d723062019-11-12 22:31:31 -07005189static void io_put_work(struct io_wq_work *work)
5190{
5191 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5192
5193 io_put_req(req);
5194}
5195
5196static void io_get_work(struct io_wq_work *work)
5197{
5198 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5199
5200 refcount_inc(&req->refs);
5201}
5202
Jens Axboe6c271ce2019-01-10 11:22:30 -07005203static int io_sq_offload_start(struct io_ring_ctx *ctx,
5204 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005205{
Jens Axboe576a3472019-11-25 08:49:20 -07005206 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005207 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005208 int ret;
5209
Jens Axboe6c271ce2019-01-10 11:22:30 -07005210 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005211 mmgrab(current->mm);
5212 ctx->sqo_mm = current->mm;
5213
Jens Axboe6c271ce2019-01-10 11:22:30 -07005214 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005215 ret = -EPERM;
5216 if (!capable(CAP_SYS_ADMIN))
5217 goto err;
5218
Jens Axboe917257d2019-04-13 09:28:55 -06005219 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5220 if (!ctx->sq_thread_idle)
5221 ctx->sq_thread_idle = HZ;
5222
Jens Axboe6c271ce2019-01-10 11:22:30 -07005223 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005224 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005225
Jens Axboe917257d2019-04-13 09:28:55 -06005226 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005227 if (cpu >= nr_cpu_ids)
5228 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005229 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005230 goto err;
5231
Jens Axboe6c271ce2019-01-10 11:22:30 -07005232 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5233 ctx, cpu,
5234 "io_uring-sq");
5235 } else {
5236 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5237 "io_uring-sq");
5238 }
5239 if (IS_ERR(ctx->sqo_thread)) {
5240 ret = PTR_ERR(ctx->sqo_thread);
5241 ctx->sqo_thread = NULL;
5242 goto err;
5243 }
5244 wake_up_process(ctx->sqo_thread);
5245 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5246 /* Can't have SQ_AFF without SQPOLL */
5247 ret = -EINVAL;
5248 goto err;
5249 }
5250
Jens Axboe576a3472019-11-25 08:49:20 -07005251 data.mm = ctx->sqo_mm;
5252 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005253 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005254 data.get_work = io_get_work;
5255 data.put_work = io_put_work;
5256
Jens Axboe561fb042019-10-24 07:25:42 -06005257 /* Do QD, or 4 * CPUS, whatever is smallest */
5258 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005259 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005260 if (IS_ERR(ctx->io_wq)) {
5261 ret = PTR_ERR(ctx->io_wq);
5262 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005263 goto err;
5264 }
5265
5266 return 0;
5267err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005268 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005269 mmdrop(ctx->sqo_mm);
5270 ctx->sqo_mm = NULL;
5271 return ret;
5272}
5273
5274static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5275{
5276 atomic_long_sub(nr_pages, &user->locked_vm);
5277}
5278
5279static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5280{
5281 unsigned long page_limit, cur_pages, new_pages;
5282
5283 /* Don't allow more pages than we can safely lock */
5284 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5285
5286 do {
5287 cur_pages = atomic_long_read(&user->locked_vm);
5288 new_pages = cur_pages + nr_pages;
5289 if (new_pages > page_limit)
5290 return -ENOMEM;
5291 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5292 new_pages) != cur_pages);
5293
5294 return 0;
5295}
5296
5297static void io_mem_free(void *ptr)
5298{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005299 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005300
Mark Rutland52e04ef2019-04-30 17:30:21 +01005301 if (!ptr)
5302 return;
5303
5304 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005305 if (put_page_testzero(page))
5306 free_compound_page(page);
5307}
5308
5309static void *io_mem_alloc(size_t size)
5310{
5311 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5312 __GFP_NORETRY;
5313
5314 return (void *) __get_free_pages(gfp_flags, get_order(size));
5315}
5316
Hristo Venev75b28af2019-08-26 17:23:46 +00005317static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5318 size_t *sq_offset)
5319{
5320 struct io_rings *rings;
5321 size_t off, sq_array_size;
5322
5323 off = struct_size(rings, cqes, cq_entries);
5324 if (off == SIZE_MAX)
5325 return SIZE_MAX;
5326
5327#ifdef CONFIG_SMP
5328 off = ALIGN(off, SMP_CACHE_BYTES);
5329 if (off == 0)
5330 return SIZE_MAX;
5331#endif
5332
5333 sq_array_size = array_size(sizeof(u32), sq_entries);
5334 if (sq_array_size == SIZE_MAX)
5335 return SIZE_MAX;
5336
5337 if (check_add_overflow(off, sq_array_size, &off))
5338 return SIZE_MAX;
5339
5340 if (sq_offset)
5341 *sq_offset = off;
5342
5343 return off;
5344}
5345
Jens Axboe2b188cc2019-01-07 10:46:33 -07005346static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5347{
Hristo Venev75b28af2019-08-26 17:23:46 +00005348 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005349
Hristo Venev75b28af2019-08-26 17:23:46 +00005350 pages = (size_t)1 << get_order(
5351 rings_size(sq_entries, cq_entries, NULL));
5352 pages += (size_t)1 << get_order(
5353 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005354
Hristo Venev75b28af2019-08-26 17:23:46 +00005355 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005356}
5357
Jens Axboeedafcce2019-01-09 09:16:05 -07005358static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5359{
5360 int i, j;
5361
5362 if (!ctx->user_bufs)
5363 return -ENXIO;
5364
5365 for (i = 0; i < ctx->nr_user_bufs; i++) {
5366 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5367
5368 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005369 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005370
5371 if (ctx->account_mem)
5372 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005373 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005374 imu->nr_bvecs = 0;
5375 }
5376
5377 kfree(ctx->user_bufs);
5378 ctx->user_bufs = NULL;
5379 ctx->nr_user_bufs = 0;
5380 return 0;
5381}
5382
5383static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5384 void __user *arg, unsigned index)
5385{
5386 struct iovec __user *src;
5387
5388#ifdef CONFIG_COMPAT
5389 if (ctx->compat) {
5390 struct compat_iovec __user *ciovs;
5391 struct compat_iovec ciov;
5392
5393 ciovs = (struct compat_iovec __user *) arg;
5394 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5395 return -EFAULT;
5396
Jens Axboed55e5f52019-12-11 16:12:15 -07005397 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005398 dst->iov_len = ciov.iov_len;
5399 return 0;
5400 }
5401#endif
5402 src = (struct iovec __user *) arg;
5403 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5404 return -EFAULT;
5405 return 0;
5406}
5407
5408static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5409 unsigned nr_args)
5410{
5411 struct vm_area_struct **vmas = NULL;
5412 struct page **pages = NULL;
5413 int i, j, got_pages = 0;
5414 int ret = -EINVAL;
5415
5416 if (ctx->user_bufs)
5417 return -EBUSY;
5418 if (!nr_args || nr_args > UIO_MAXIOV)
5419 return -EINVAL;
5420
5421 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5422 GFP_KERNEL);
5423 if (!ctx->user_bufs)
5424 return -ENOMEM;
5425
5426 for (i = 0; i < nr_args; i++) {
5427 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5428 unsigned long off, start, end, ubuf;
5429 int pret, nr_pages;
5430 struct iovec iov;
5431 size_t size;
5432
5433 ret = io_copy_iov(ctx, &iov, arg, i);
5434 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005435 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005436
5437 /*
5438 * Don't impose further limits on the size and buffer
5439 * constraints here, we'll -EINVAL later when IO is
5440 * submitted if they are wrong.
5441 */
5442 ret = -EFAULT;
5443 if (!iov.iov_base || !iov.iov_len)
5444 goto err;
5445
5446 /* arbitrary limit, but we need something */
5447 if (iov.iov_len > SZ_1G)
5448 goto err;
5449
5450 ubuf = (unsigned long) iov.iov_base;
5451 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5452 start = ubuf >> PAGE_SHIFT;
5453 nr_pages = end - start;
5454
5455 if (ctx->account_mem) {
5456 ret = io_account_mem(ctx->user, nr_pages);
5457 if (ret)
5458 goto err;
5459 }
5460
5461 ret = 0;
5462 if (!pages || nr_pages > got_pages) {
5463 kfree(vmas);
5464 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005465 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005466 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005467 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005468 sizeof(struct vm_area_struct *),
5469 GFP_KERNEL);
5470 if (!pages || !vmas) {
5471 ret = -ENOMEM;
5472 if (ctx->account_mem)
5473 io_unaccount_mem(ctx->user, nr_pages);
5474 goto err;
5475 }
5476 got_pages = nr_pages;
5477 }
5478
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005479 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005480 GFP_KERNEL);
5481 ret = -ENOMEM;
5482 if (!imu->bvec) {
5483 if (ctx->account_mem)
5484 io_unaccount_mem(ctx->user, nr_pages);
5485 goto err;
5486 }
5487
5488 ret = 0;
5489 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005490 pret = get_user_pages(ubuf, nr_pages,
5491 FOLL_WRITE | FOLL_LONGTERM,
5492 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005493 if (pret == nr_pages) {
5494 /* don't support file backed memory */
5495 for (j = 0; j < nr_pages; j++) {
5496 struct vm_area_struct *vma = vmas[j];
5497
5498 if (vma->vm_file &&
5499 !is_file_hugepages(vma->vm_file)) {
5500 ret = -EOPNOTSUPP;
5501 break;
5502 }
5503 }
5504 } else {
5505 ret = pret < 0 ? pret : -EFAULT;
5506 }
5507 up_read(&current->mm->mmap_sem);
5508 if (ret) {
5509 /*
5510 * if we did partial map, or found file backed vmas,
5511 * release any pages we did get
5512 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005513 if (pret > 0)
5514 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005515 if (ctx->account_mem)
5516 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005517 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005518 goto err;
5519 }
5520
5521 off = ubuf & ~PAGE_MASK;
5522 size = iov.iov_len;
5523 for (j = 0; j < nr_pages; j++) {
5524 size_t vec_len;
5525
5526 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5527 imu->bvec[j].bv_page = pages[j];
5528 imu->bvec[j].bv_len = vec_len;
5529 imu->bvec[j].bv_offset = off;
5530 off = 0;
5531 size -= vec_len;
5532 }
5533 /* store original address for later verification */
5534 imu->ubuf = ubuf;
5535 imu->len = iov.iov_len;
5536 imu->nr_bvecs = nr_pages;
5537
5538 ctx->nr_user_bufs++;
5539 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005540 kvfree(pages);
5541 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005542 return 0;
5543err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005544 kvfree(pages);
5545 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005546 io_sqe_buffer_unregister(ctx);
5547 return ret;
5548}
5549
Jens Axboe9b402842019-04-11 11:45:41 -06005550static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5551{
5552 __s32 __user *fds = arg;
5553 int fd;
5554
5555 if (ctx->cq_ev_fd)
5556 return -EBUSY;
5557
5558 if (copy_from_user(&fd, fds, sizeof(*fds)))
5559 return -EFAULT;
5560
5561 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5562 if (IS_ERR(ctx->cq_ev_fd)) {
5563 int ret = PTR_ERR(ctx->cq_ev_fd);
5564 ctx->cq_ev_fd = NULL;
5565 return ret;
5566 }
5567
5568 return 0;
5569}
5570
5571static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5572{
5573 if (ctx->cq_ev_fd) {
5574 eventfd_ctx_put(ctx->cq_ev_fd);
5575 ctx->cq_ev_fd = NULL;
5576 return 0;
5577 }
5578
5579 return -ENXIO;
5580}
5581
Jens Axboe2b188cc2019-01-07 10:46:33 -07005582static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5583{
Jens Axboe6b063142019-01-10 22:13:58 -07005584 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005585 if (ctx->sqo_mm)
5586 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005587
5588 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005589 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005590 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005591 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005592
Jens Axboe2b188cc2019-01-07 10:46:33 -07005593#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005594 if (ctx->ring_sock) {
5595 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005596 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005597 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005598#endif
5599
Hristo Venev75b28af2019-08-26 17:23:46 +00005600 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005601 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005602
5603 percpu_ref_exit(&ctx->refs);
5604 if (ctx->account_mem)
5605 io_unaccount_mem(ctx->user,
5606 ring_pages(ctx->sq_entries, ctx->cq_entries));
5607 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07005608 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07005609 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07005610 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07005611 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005612 kfree(ctx);
5613}
5614
5615static __poll_t io_uring_poll(struct file *file, poll_table *wait)
5616{
5617 struct io_ring_ctx *ctx = file->private_data;
5618 __poll_t mask = 0;
5619
5620 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02005621 /*
5622 * synchronizes with barrier from wq_has_sleeper call in
5623 * io_commit_cqring
5624 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005625 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00005626 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
5627 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005628 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08005629 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005630 mask |= EPOLLIN | EPOLLRDNORM;
5631
5632 return mask;
5633}
5634
5635static int io_uring_fasync(int fd, struct file *file, int on)
5636{
5637 struct io_ring_ctx *ctx = file->private_data;
5638
5639 return fasync_helper(fd, file, on, &ctx->cq_fasync);
5640}
5641
5642static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
5643{
5644 mutex_lock(&ctx->uring_lock);
5645 percpu_ref_kill(&ctx->refs);
5646 mutex_unlock(&ctx->uring_lock);
5647
Jens Axboe5262f562019-09-17 12:26:57 -06005648 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005649 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06005650
5651 if (ctx->io_wq)
5652 io_wq_cancel_all(ctx->io_wq);
5653
Jens Axboedef596e2019-01-09 08:59:42 -07005654 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07005655 /* if we failed setting up the ctx, we might not have any rings */
5656 if (ctx->rings)
5657 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07005658 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005659 io_ring_ctx_free(ctx);
5660}
5661
5662static int io_uring_release(struct inode *inode, struct file *file)
5663{
5664 struct io_ring_ctx *ctx = file->private_data;
5665
5666 file->private_data = NULL;
5667 io_ring_ctx_wait_and_kill(ctx);
5668 return 0;
5669}
5670
Jens Axboefcb323c2019-10-24 12:39:47 -06005671static void io_uring_cancel_files(struct io_ring_ctx *ctx,
5672 struct files_struct *files)
5673{
5674 struct io_kiocb *req;
5675 DEFINE_WAIT(wait);
5676
5677 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07005678 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06005679
5680 spin_lock_irq(&ctx->inflight_lock);
5681 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07005682 if (req->work.files != files)
5683 continue;
5684 /* req is being completed, ignore */
5685 if (!refcount_inc_not_zero(&req->refs))
5686 continue;
5687 cancel_req = req;
5688 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06005689 }
Jens Axboe768134d2019-11-10 20:30:53 -07005690 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005691 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07005692 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06005693 spin_unlock_irq(&ctx->inflight_lock);
5694
Jens Axboe768134d2019-11-10 20:30:53 -07005695 /* We need to keep going until we don't find a matching req */
5696 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005697 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005698
5699 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5700 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005701 schedule();
5702 }
Jens Axboe768134d2019-11-10 20:30:53 -07005703 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005704}
5705
5706static int io_uring_flush(struct file *file, void *data)
5707{
5708 struct io_ring_ctx *ctx = file->private_data;
5709
5710 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005711 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5712 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005713 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005714 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005715 return 0;
5716}
5717
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005718static void *io_uring_validate_mmap_request(struct file *file,
5719 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005720{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005721 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005722 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005723 struct page *page;
5724 void *ptr;
5725
5726 switch (offset) {
5727 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005728 case IORING_OFF_CQ_RING:
5729 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005730 break;
5731 case IORING_OFF_SQES:
5732 ptr = ctx->sq_sqes;
5733 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005734 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005735 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005736 }
5737
5738 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005739 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005740 return ERR_PTR(-EINVAL);
5741
5742 return ptr;
5743}
5744
5745#ifdef CONFIG_MMU
5746
5747static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5748{
5749 size_t sz = vma->vm_end - vma->vm_start;
5750 unsigned long pfn;
5751 void *ptr;
5752
5753 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5754 if (IS_ERR(ptr))
5755 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005756
5757 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5758 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5759}
5760
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005761#else /* !CONFIG_MMU */
5762
5763static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5764{
5765 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5766}
5767
5768static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5769{
5770 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5771}
5772
5773static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5774 unsigned long addr, unsigned long len,
5775 unsigned long pgoff, unsigned long flags)
5776{
5777 void *ptr;
5778
5779 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5780 if (IS_ERR(ptr))
5781 return PTR_ERR(ptr);
5782
5783 return (unsigned long) ptr;
5784}
5785
5786#endif /* !CONFIG_MMU */
5787
Jens Axboe2b188cc2019-01-07 10:46:33 -07005788SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5789 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5790 size_t, sigsz)
5791{
5792 struct io_ring_ctx *ctx;
5793 long ret = -EBADF;
5794 int submitted = 0;
5795 struct fd f;
5796
Jens Axboe6c271ce2019-01-10 11:22:30 -07005797 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005798 return -EINVAL;
5799
5800 f = fdget(fd);
5801 if (!f.file)
5802 return -EBADF;
5803
5804 ret = -EOPNOTSUPP;
5805 if (f.file->f_op != &io_uring_fops)
5806 goto out_fput;
5807
5808 ret = -ENXIO;
5809 ctx = f.file->private_data;
5810 if (!percpu_ref_tryget(&ctx->refs))
5811 goto out_fput;
5812
Jens Axboe6c271ce2019-01-10 11:22:30 -07005813 /*
5814 * For SQ polling, the thread will do all submissions and completions.
5815 * Just return the requested submit count, and wake the thread if
5816 * we were asked to.
5817 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005818 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005819 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005820 if (!list_empty_careful(&ctx->cq_overflow_list))
5821 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005822 if (flags & IORING_ENTER_SQ_WAKEUP)
5823 wake_up(&ctx->sqo_wait);
5824 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005825 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005826 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005827
Jens Axboe44d28272020-01-16 19:00:24 -07005828 if (current->mm != ctx->sqo_mm ||
5829 current_cred() != ctx->creds) {
5830 ret = -EPERM;
5831 goto out;
5832 }
5833
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005834 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005835 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005836 /* already have mm, so io_submit_sqes() won't try to grab it */
5837 cur_mm = ctx->sqo_mm;
5838 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
5839 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005840 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005841
5842 if (submitted != to_submit)
5843 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005844 }
5845 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07005846 unsigned nr_events = 0;
5847
Jens Axboe2b188cc2019-01-07 10:46:33 -07005848 min_complete = min(min_complete, ctx->cq_entries);
5849
Jens Axboedef596e2019-01-09 08:59:42 -07005850 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07005851 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07005852 } else {
5853 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
5854 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005855 }
5856
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005857out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03005858 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005859out_fput:
5860 fdput(f);
5861 return submitted ? submitted : ret;
5862}
5863
5864static const struct file_operations io_uring_fops = {
5865 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06005866 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07005867 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005868#ifndef CONFIG_MMU
5869 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
5870 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
5871#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07005872 .poll = io_uring_poll,
5873 .fasync = io_uring_fasync,
5874};
5875
5876static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
5877 struct io_uring_params *p)
5878{
Hristo Venev75b28af2019-08-26 17:23:46 +00005879 struct io_rings *rings;
5880 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005881
Hristo Venev75b28af2019-08-26 17:23:46 +00005882 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
5883 if (size == SIZE_MAX)
5884 return -EOVERFLOW;
5885
5886 rings = io_mem_alloc(size);
5887 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005888 return -ENOMEM;
5889
Hristo Venev75b28af2019-08-26 17:23:46 +00005890 ctx->rings = rings;
5891 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
5892 rings->sq_ring_mask = p->sq_entries - 1;
5893 rings->cq_ring_mask = p->cq_entries - 1;
5894 rings->sq_ring_entries = p->sq_entries;
5895 rings->cq_ring_entries = p->cq_entries;
5896 ctx->sq_mask = rings->sq_ring_mask;
5897 ctx->cq_mask = rings->cq_ring_mask;
5898 ctx->sq_entries = rings->sq_ring_entries;
5899 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005900
5901 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07005902 if (size == SIZE_MAX) {
5903 io_mem_free(ctx->rings);
5904 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005905 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07005906 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005907
5908 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07005909 if (!ctx->sq_sqes) {
5910 io_mem_free(ctx->rings);
5911 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005912 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07005913 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005914
Jens Axboe2b188cc2019-01-07 10:46:33 -07005915 return 0;
5916}
5917
5918/*
5919 * Allocate an anonymous fd, this is what constitutes the application
5920 * visible backing of an io_uring instance. The application mmaps this
5921 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
5922 * we have to tie this fd to a socket for file garbage collection purposes.
5923 */
5924static int io_uring_get_fd(struct io_ring_ctx *ctx)
5925{
5926 struct file *file;
5927 int ret;
5928
5929#if defined(CONFIG_UNIX)
5930 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
5931 &ctx->ring_sock);
5932 if (ret)
5933 return ret;
5934#endif
5935
5936 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
5937 if (ret < 0)
5938 goto err;
5939
5940 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
5941 O_RDWR | O_CLOEXEC);
5942 if (IS_ERR(file)) {
5943 put_unused_fd(ret);
5944 ret = PTR_ERR(file);
5945 goto err;
5946 }
5947
5948#if defined(CONFIG_UNIX)
5949 ctx->ring_sock->file = file;
5950#endif
5951 fd_install(ret, file);
5952 return ret;
5953err:
5954#if defined(CONFIG_UNIX)
5955 sock_release(ctx->ring_sock);
5956 ctx->ring_sock = NULL;
5957#endif
5958 return ret;
5959}
5960
5961static int io_uring_create(unsigned entries, struct io_uring_params *p)
5962{
5963 struct user_struct *user = NULL;
5964 struct io_ring_ctx *ctx;
5965 bool account_mem;
5966 int ret;
5967
5968 if (!entries || entries > IORING_MAX_ENTRIES)
5969 return -EINVAL;
5970
5971 /*
5972 * Use twice as many entries for the CQ ring. It's possible for the
5973 * application to drive a higher depth than the size of the SQ ring,
5974 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005975 * some flexibility in overcommitting a bit. If the application has
5976 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5977 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005978 */
5979 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005980 if (p->flags & IORING_SETUP_CQSIZE) {
5981 /*
5982 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5983 * to a power-of-two, if it isn't already. We do NOT impose
5984 * any cq vs sq ring sizing.
5985 */
5986 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5987 return -EINVAL;
5988 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5989 } else {
5990 p->cq_entries = 2 * p->sq_entries;
5991 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005992
5993 user = get_uid(current_user());
5994 account_mem = !capable(CAP_IPC_LOCK);
5995
5996 if (account_mem) {
5997 ret = io_account_mem(user,
5998 ring_pages(p->sq_entries, p->cq_entries));
5999 if (ret) {
6000 free_uid(user);
6001 return ret;
6002 }
6003 }
6004
6005 ctx = io_ring_ctx_alloc(p);
6006 if (!ctx) {
6007 if (account_mem)
6008 io_unaccount_mem(user, ring_pages(p->sq_entries,
6009 p->cq_entries));
6010 free_uid(user);
6011 return -ENOMEM;
6012 }
6013 ctx->compat = in_compat_syscall();
6014 ctx->account_mem = account_mem;
6015 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006016 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006017
6018 ret = io_allocate_scq_urings(ctx, p);
6019 if (ret)
6020 goto err;
6021
Jens Axboe6c271ce2019-01-10 11:22:30 -07006022 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006023 if (ret)
6024 goto err;
6025
Jens Axboe2b188cc2019-01-07 10:46:33 -07006026 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006027 p->sq_off.head = offsetof(struct io_rings, sq.head);
6028 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6029 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6030 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6031 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6032 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6033 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006034
6035 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006036 p->cq_off.head = offsetof(struct io_rings, cq.head);
6037 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6038 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6039 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6040 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6041 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006042
Jens Axboe044c1ab2019-10-28 09:15:33 -06006043 /*
6044 * Install ring fd as the very last thing, so we don't risk someone
6045 * having closed it before we finish setup
6046 */
6047 ret = io_uring_get_fd(ctx);
6048 if (ret < 0)
6049 goto err;
6050
Jens Axboeda8c9692019-12-02 18:51:26 -07006051 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
6052 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006053 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006054 return ret;
6055err:
6056 io_ring_ctx_wait_and_kill(ctx);
6057 return ret;
6058}
6059
6060/*
6061 * Sets up an aio uring context, and returns the fd. Applications asks for a
6062 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6063 * params structure passed in.
6064 */
6065static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6066{
6067 struct io_uring_params p;
6068 long ret;
6069 int i;
6070
6071 if (copy_from_user(&p, params, sizeof(p)))
6072 return -EFAULT;
6073 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6074 if (p.resv[i])
6075 return -EINVAL;
6076 }
6077
Jens Axboe6c271ce2019-01-10 11:22:30 -07006078 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06006079 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006080 return -EINVAL;
6081
6082 ret = io_uring_create(entries, &p);
6083 if (ret < 0)
6084 return ret;
6085
6086 if (copy_to_user(params, &p, sizeof(p)))
6087 return -EFAULT;
6088
6089 return ret;
6090}
6091
6092SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6093 struct io_uring_params __user *, params)
6094{
6095 return io_uring_setup(entries, params);
6096}
6097
Jens Axboeedafcce2019-01-09 09:16:05 -07006098static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6099 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006100 __releases(ctx->uring_lock)
6101 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006102{
6103 int ret;
6104
Jens Axboe35fa71a2019-04-22 10:23:23 -06006105 /*
6106 * We're inside the ring mutex, if the ref is already dying, then
6107 * someone else killed the ctx or is already going through
6108 * io_uring_register().
6109 */
6110 if (percpu_ref_is_dying(&ctx->refs))
6111 return -ENXIO;
6112
Jens Axboe05f3fb32019-12-09 11:22:50 -07006113 if (opcode != IORING_UNREGISTER_FILES &&
6114 opcode != IORING_REGISTER_FILES_UPDATE) {
6115 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006116
Jens Axboe05f3fb32019-12-09 11:22:50 -07006117 /*
6118 * Drop uring mutex before waiting for references to exit. If
6119 * another thread is currently inside io_uring_enter() it might
6120 * need to grab the uring_lock to make progress. If we hold it
6121 * here across the drain wait, then we can deadlock. It's safe
6122 * to drop the mutex here, since no new references will come in
6123 * after we've killed the percpu ref.
6124 */
6125 mutex_unlock(&ctx->uring_lock);
6126 wait_for_completion(&ctx->completions[0]);
6127 mutex_lock(&ctx->uring_lock);
6128 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006129
6130 switch (opcode) {
6131 case IORING_REGISTER_BUFFERS:
6132 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6133 break;
6134 case IORING_UNREGISTER_BUFFERS:
6135 ret = -EINVAL;
6136 if (arg || nr_args)
6137 break;
6138 ret = io_sqe_buffer_unregister(ctx);
6139 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006140 case IORING_REGISTER_FILES:
6141 ret = io_sqe_files_register(ctx, arg, nr_args);
6142 break;
6143 case IORING_UNREGISTER_FILES:
6144 ret = -EINVAL;
6145 if (arg || nr_args)
6146 break;
6147 ret = io_sqe_files_unregister(ctx);
6148 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006149 case IORING_REGISTER_FILES_UPDATE:
6150 ret = io_sqe_files_update(ctx, arg, nr_args);
6151 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006152 case IORING_REGISTER_EVENTFD:
6153 ret = -EINVAL;
6154 if (nr_args != 1)
6155 break;
6156 ret = io_eventfd_register(ctx, arg);
6157 break;
6158 case IORING_UNREGISTER_EVENTFD:
6159 ret = -EINVAL;
6160 if (arg || nr_args)
6161 break;
6162 ret = io_eventfd_unregister(ctx);
6163 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006164 default:
6165 ret = -EINVAL;
6166 break;
6167 }
6168
Jens Axboe05f3fb32019-12-09 11:22:50 -07006169
6170 if (opcode != IORING_UNREGISTER_FILES &&
6171 opcode != IORING_REGISTER_FILES_UPDATE) {
6172 /* bring the ctx back to life */
6173 reinit_completion(&ctx->completions[0]);
6174 percpu_ref_reinit(&ctx->refs);
6175 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006176 return ret;
6177}
6178
6179SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6180 void __user *, arg, unsigned int, nr_args)
6181{
6182 struct io_ring_ctx *ctx;
6183 long ret = -EBADF;
6184 struct fd f;
6185
6186 f = fdget(fd);
6187 if (!f.file)
6188 return -EBADF;
6189
6190 ret = -EOPNOTSUPP;
6191 if (f.file->f_op != &io_uring_fops)
6192 goto out_fput;
6193
6194 ctx = f.file->private_data;
6195
6196 mutex_lock(&ctx->uring_lock);
6197 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6198 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006199 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6200 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006201out_fput:
6202 fdput(f);
6203 return ret;
6204}
6205
Jens Axboe2b188cc2019-01-07 10:46:33 -07006206static int __init io_uring_init(void)
6207{
Jens Axboed3656342019-12-18 09:50:26 -07006208 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006209 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6210 return 0;
6211};
6212__initcall(io_uring_init);