blob: 4325068324b7d0cef66afdf9593623ed76257f35 [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;
382 umode_t mode;
383 const char __user *fname;
384 struct filename *filename;
385 int flags;
386};
387
Jens Axboe05f3fb32019-12-09 11:22:50 -0700388struct io_files_update {
389 struct file *file;
390 u64 arg;
391 u32 nr_args;
392 u32 offset;
393};
394
Jens Axboef499a022019-12-02 16:28:46 -0700395struct io_async_connect {
396 struct sockaddr_storage address;
397};
398
Jens Axboe03b12302019-12-02 18:50:25 -0700399struct io_async_msghdr {
400 struct iovec fast_iov[UIO_FASTIOV];
401 struct iovec *iov;
402 struct sockaddr __user *uaddr;
403 struct msghdr msg;
404};
405
Jens Axboef67676d2019-12-02 11:03:47 -0700406struct io_async_rw {
407 struct iovec fast_iov[UIO_FASTIOV];
408 struct iovec *iov;
409 ssize_t nr_segs;
410 ssize_t size;
411};
412
Jens Axboe15b71ab2019-12-11 11:20:36 -0700413struct io_async_open {
414 struct filename *filename;
415};
416
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700417struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700418 union {
419 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700420 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700421 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700422 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700423 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700424 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700425};
426
Jens Axboe09bb8392019-03-13 12:39:28 -0600427/*
428 * NOTE! Each of the iocb union members has the file pointer
429 * as the first entry in their struct definition. So you can
430 * access the file pointer through any of the sub-structs,
431 * or directly as just 'ki_filp' in this struct.
432 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700433struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700434 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600435 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700436 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700437 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700438 struct io_accept accept;
439 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700440 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700441 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700442 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700443 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700444 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700445 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700446 struct io_files_update files_update;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700447 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700448
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700449 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300450 struct file *ring_file;
451 int ring_fd;
452 bool has_user;
453 bool in_async;
454 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700455 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700456
457 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700458 union {
459 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700460 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700461 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600462 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700463 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700464 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200465#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700466#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700467#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700468#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200469#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
470#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600471#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700472#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800473#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300474#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600475#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600476#define REQ_F_ISREG 2048 /* regular file */
477#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700478#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800479#define REQ_F_INFLIGHT 16384 /* on inflight list */
480#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700481#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700482 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600483 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600484 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700485
Jens Axboefcb323c2019-10-24 12:39:47 -0600486 struct list_head inflight_entry;
487
Jens Axboe561fb042019-10-24 07:25:42 -0600488 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700489};
490
491#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700492#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700493
Jens Axboe9a56a232019-01-09 09:06:50 -0700494struct io_submit_state {
495 struct blk_plug plug;
496
497 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700498 * io_kiocb alloc cache
499 */
500 void *reqs[IO_IOPOLL_BATCH];
501 unsigned int free_reqs;
502 unsigned int cur_req;
503
504 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700505 * File reference cache
506 */
507 struct file *file;
508 unsigned int fd;
509 unsigned int has_refs;
510 unsigned int used_refs;
511 unsigned int ios_left;
512};
513
Jens Axboe561fb042019-10-24 07:25:42 -0600514static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700515static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800516static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800517static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700518static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700519static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700520static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
521static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700522static int __io_sqe_files_update(struct io_ring_ctx *ctx,
523 struct io_uring_files_update *ip,
524 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600525
Jens Axboe2b188cc2019-01-07 10:46:33 -0700526static struct kmem_cache *req_cachep;
527
528static const struct file_operations io_uring_fops;
529
530struct sock *io_uring_get_socket(struct file *file)
531{
532#if defined(CONFIG_UNIX)
533 if (file->f_op == &io_uring_fops) {
534 struct io_ring_ctx *ctx = file->private_data;
535
536 return ctx->ring_sock->sk;
537 }
538#endif
539 return NULL;
540}
541EXPORT_SYMBOL(io_uring_get_socket);
542
543static void io_ring_ctx_ref_free(struct percpu_ref *ref)
544{
545 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
546
Jens Axboe206aefd2019-11-07 18:27:42 -0700547 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700548}
549
550static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
551{
552 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700553 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700554
555 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
556 if (!ctx)
557 return NULL;
558
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700559 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
560 if (!ctx->fallback_req)
561 goto err;
562
Jens Axboe206aefd2019-11-07 18:27:42 -0700563 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
564 if (!ctx->completions)
565 goto err;
566
Jens Axboe78076bb2019-12-04 19:56:40 -0700567 /*
568 * Use 5 bits less than the max cq entries, that should give us around
569 * 32 entries per hash list if totally full and uniformly spread.
570 */
571 hash_bits = ilog2(p->cq_entries);
572 hash_bits -= 5;
573 if (hash_bits <= 0)
574 hash_bits = 1;
575 ctx->cancel_hash_bits = hash_bits;
576 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
577 GFP_KERNEL);
578 if (!ctx->cancel_hash)
579 goto err;
580 __hash_init(ctx->cancel_hash, 1U << hash_bits);
581
Roman Gushchin21482892019-05-07 10:01:48 -0700582 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700583 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
584 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700585
586 ctx->flags = p->flags;
587 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700588 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700589 init_completion(&ctx->completions[0]);
590 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700591 mutex_init(&ctx->uring_lock);
592 init_waitqueue_head(&ctx->wait);
593 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700594 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600595 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600596 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600597 init_waitqueue_head(&ctx->inflight_wait);
598 spin_lock_init(&ctx->inflight_lock);
599 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700600 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700601err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700602 if (ctx->fallback_req)
603 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700604 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700605 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700606 kfree(ctx);
607 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608}
609
Bob Liu9d858b22019-11-13 18:06:25 +0800610static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600611{
Jackie Liua197f662019-11-08 08:09:12 -0700612 struct io_ring_ctx *ctx = req->ctx;
613
Jens Axboe498ccd92019-10-25 10:04:25 -0600614 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
615 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600616}
617
Bob Liu9d858b22019-11-13 18:06:25 +0800618static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600619{
Bob Liu9d858b22019-11-13 18:06:25 +0800620 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
621 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600622
Bob Liu9d858b22019-11-13 18:06:25 +0800623 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600624}
625
626static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600627{
628 struct io_kiocb *req;
629
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600630 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800631 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600632 list_del_init(&req->list);
633 return req;
634 }
635
636 return NULL;
637}
638
Jens Axboe5262f562019-09-17 12:26:57 -0600639static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
640{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600641 struct io_kiocb *req;
642
643 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700644 if (req) {
645 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
646 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800647 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700648 list_del_init(&req->list);
649 return req;
650 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600651 }
652
653 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600654}
655
Jens Axboede0617e2019-04-06 21:51:27 -0600656static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700657{
Hristo Venev75b28af2019-08-26 17:23:46 +0000658 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700659
Hristo Venev75b28af2019-08-26 17:23:46 +0000660 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700661 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000662 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700663
Jens Axboe2b188cc2019-01-07 10:46:33 -0700664 if (wq_has_sleeper(&ctx->cq_wait)) {
665 wake_up_interruptible(&ctx->cq_wait);
666 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
667 }
668 }
669}
670
Jens Axboed625c6e2019-12-17 19:53:05 -0700671static inline bool io_req_needs_user(struct io_kiocb *req)
Jens Axboe18d9be12019-09-10 09:13:05 -0600672{
Jens Axboed625c6e2019-12-17 19:53:05 -0700673 return !(req->opcode == IORING_OP_READ_FIXED ||
674 req->opcode == IORING_OP_WRITE_FIXED);
Jens Axboe561fb042019-10-24 07:25:42 -0600675}
676
Jens Axboe94ae5e72019-11-14 19:39:52 -0700677static inline bool io_prep_async_work(struct io_kiocb *req,
678 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600679{
680 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600681
Jens Axboe3529d8c2019-12-19 18:24:38 -0700682 switch (req->opcode) {
683 case IORING_OP_WRITEV:
684 case IORING_OP_WRITE_FIXED:
685 /* only regular files should be hashed for writes */
686 if (req->flags & REQ_F_ISREG)
687 do_hashed = true;
688 /* fall-through */
689 case IORING_OP_READV:
690 case IORING_OP_READ_FIXED:
691 case IORING_OP_SENDMSG:
692 case IORING_OP_RECVMSG:
693 case IORING_OP_ACCEPT:
694 case IORING_OP_POLL_ADD:
695 case IORING_OP_CONNECT:
696 /*
697 * We know REQ_F_ISREG is not set on some of these
698 * opcodes, but this enables us to keep the check in
699 * just one place.
700 */
701 if (!(req->flags & REQ_F_ISREG))
702 req->work.flags |= IO_WQ_WORK_UNBOUND;
703 break;
Jens Axboe54a91f32019-09-10 09:15:04 -0600704 }
Jens Axboe3529d8c2019-12-19 18:24:38 -0700705 if (io_req_needs_user(req))
706 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600707
Jens Axboe94ae5e72019-11-14 19:39:52 -0700708 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600709 return do_hashed;
710}
711
Jackie Liua197f662019-11-08 08:09:12 -0700712static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600713{
Jackie Liua197f662019-11-08 08:09:12 -0700714 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700715 struct io_kiocb *link;
716 bool do_hashed;
717
718 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600719
720 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
721 req->flags);
722 if (!do_hashed) {
723 io_wq_enqueue(ctx->io_wq, &req->work);
724 } else {
725 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
726 file_inode(req->file));
727 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700728
729 if (link)
730 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600731}
732
Jens Axboe5262f562019-09-17 12:26:57 -0600733static void io_kill_timeout(struct io_kiocb *req)
734{
735 int ret;
736
Jens Axboe2d283902019-12-04 11:08:05 -0700737 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600738 if (ret != -1) {
739 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600740 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700741 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800742 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600743 }
744}
745
746static void io_kill_timeouts(struct io_ring_ctx *ctx)
747{
748 struct io_kiocb *req, *tmp;
749
750 spin_lock_irq(&ctx->completion_lock);
751 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
752 io_kill_timeout(req);
753 spin_unlock_irq(&ctx->completion_lock);
754}
755
Jens Axboede0617e2019-04-06 21:51:27 -0600756static void io_commit_cqring(struct io_ring_ctx *ctx)
757{
758 struct io_kiocb *req;
759
Jens Axboe5262f562019-09-17 12:26:57 -0600760 while ((req = io_get_timeout_req(ctx)) != NULL)
761 io_kill_timeout(req);
762
Jens Axboede0617e2019-04-06 21:51:27 -0600763 __io_commit_cqring(ctx);
764
765 while ((req = io_get_deferred_req(ctx)) != NULL) {
766 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700767 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600768 }
769}
770
Jens Axboe2b188cc2019-01-07 10:46:33 -0700771static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
772{
Hristo Venev75b28af2019-08-26 17:23:46 +0000773 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700774 unsigned tail;
775
776 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200777 /*
778 * writes to the cq entry need to come after reading head; the
779 * control dependency is enough as we're using WRITE_ONCE to
780 * fill the cq entry
781 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000782 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700783 return NULL;
784
785 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000786 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700787}
788
Jens Axboe8c838782019-03-12 15:48:16 -0600789static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
790{
791 if (waitqueue_active(&ctx->wait))
792 wake_up(&ctx->wait);
793 if (waitqueue_active(&ctx->sqo_wait))
794 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600795 if (ctx->cq_ev_fd)
796 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600797}
798
Jens Axboec4a2ed72019-11-21 21:01:26 -0700799/* Returns true if there are no backlogged entries after the flush */
800static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700801{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700802 struct io_rings *rings = ctx->rings;
803 struct io_uring_cqe *cqe;
804 struct io_kiocb *req;
805 unsigned long flags;
806 LIST_HEAD(list);
807
808 if (!force) {
809 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700810 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700811 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
812 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700813 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700814 }
815
816 spin_lock_irqsave(&ctx->completion_lock, flags);
817
818 /* if force is set, the ring is going away. always drop after that */
819 if (force)
820 ctx->cq_overflow_flushed = true;
821
Jens Axboec4a2ed72019-11-21 21:01:26 -0700822 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700823 while (!list_empty(&ctx->cq_overflow_list)) {
824 cqe = io_get_cqring(ctx);
825 if (!cqe && !force)
826 break;
827
828 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
829 list);
830 list_move(&req->list, &list);
831 if (cqe) {
832 WRITE_ONCE(cqe->user_data, req->user_data);
833 WRITE_ONCE(cqe->res, req->result);
834 WRITE_ONCE(cqe->flags, 0);
835 } else {
836 WRITE_ONCE(ctx->rings->cq_overflow,
837 atomic_inc_return(&ctx->cached_cq_overflow));
838 }
839 }
840
841 io_commit_cqring(ctx);
842 spin_unlock_irqrestore(&ctx->completion_lock, flags);
843 io_cqring_ev_posted(ctx);
844
845 while (!list_empty(&list)) {
846 req = list_first_entry(&list, struct io_kiocb, list);
847 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800848 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700849 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700850
851 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700852}
853
Jens Axboe78e19bb2019-11-06 15:21:34 -0700854static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700855{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700856 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700857 struct io_uring_cqe *cqe;
858
Jens Axboe78e19bb2019-11-06 15:21:34 -0700859 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700860
Jens Axboe2b188cc2019-01-07 10:46:33 -0700861 /*
862 * If we can't get a cq entry, userspace overflowed the
863 * submission (by quite a lot). Increment the overflow count in
864 * the ring.
865 */
866 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700867 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700868 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700869 WRITE_ONCE(cqe->res, res);
870 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700871 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700872 WRITE_ONCE(ctx->rings->cq_overflow,
873 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700874 } else {
875 refcount_inc(&req->refs);
876 req->result = res;
877 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700878 }
879}
880
Jens Axboe78e19bb2019-11-06 15:21:34 -0700881static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700882{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700883 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700884 unsigned long flags;
885
886 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700887 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700888 io_commit_cqring(ctx);
889 spin_unlock_irqrestore(&ctx->completion_lock, flags);
890
Jens Axboe8c838782019-03-12 15:48:16 -0600891 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700892}
893
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700894static inline bool io_is_fallback_req(struct io_kiocb *req)
895{
896 return req == (struct io_kiocb *)
897 ((unsigned long) req->ctx->fallback_req & ~1UL);
898}
899
900static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
901{
902 struct io_kiocb *req;
903
904 req = ctx->fallback_req;
905 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
906 return req;
907
908 return NULL;
909}
910
Jens Axboe2579f912019-01-09 09:10:43 -0700911static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
912 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700913{
Jens Axboefd6fab22019-03-14 16:30:06 -0600914 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700915 struct io_kiocb *req;
916
917 if (!percpu_ref_tryget(&ctx->refs))
918 return NULL;
919
Jens Axboe2579f912019-01-09 09:10:43 -0700920 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600921 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700922 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700923 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700924 } else if (!state->free_reqs) {
925 size_t sz;
926 int ret;
927
928 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600929 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
930
931 /*
932 * Bulk alloc is all-or-nothing. If we fail to get a batch,
933 * retry single alloc to be on the safe side.
934 */
935 if (unlikely(ret <= 0)) {
936 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
937 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700938 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600939 ret = 1;
940 }
Jens Axboe2579f912019-01-09 09:10:43 -0700941 state->free_reqs = ret - 1;
942 state->cur_req = 1;
943 req = state->reqs[0];
944 } else {
945 req = state->reqs[state->cur_req];
946 state->free_reqs--;
947 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700948 }
949
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700950got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700951 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300952 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600953 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700954 req->ctx = ctx;
955 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600956 /* one is dropped after submission, the other at completion */
957 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600958 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600959 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700960 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700961fallback:
962 req = io_get_fallback_req(ctx);
963 if (req)
964 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300965 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700966 return NULL;
967}
968
Jens Axboedef596e2019-01-09 08:59:42 -0700969static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
970{
971 if (*nr) {
972 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300973 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700974 percpu_ref_put_many(&ctx->file_data->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700975 *nr = 0;
976 }
977}
978
Jens Axboe9e645e112019-05-10 16:07:28 -0600979static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700980{
Jens Axboefcb323c2019-10-24 12:39:47 -0600981 struct io_ring_ctx *ctx = req->ctx;
982
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700983 if (req->io)
984 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700985 if (req->file) {
986 if (req->flags & REQ_F_FIXED_FILE)
987 percpu_ref_put(&ctx->file_data->refs);
988 else
989 fput(req->file);
990 }
Jens Axboefcb323c2019-10-24 12:39:47 -0600991 if (req->flags & REQ_F_INFLIGHT) {
992 unsigned long flags;
993
994 spin_lock_irqsave(&ctx->inflight_lock, flags);
995 list_del(&req->inflight_entry);
996 if (waitqueue_active(&ctx->inflight_wait))
997 wake_up(&ctx->inflight_wait);
998 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
999 }
1000 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001001 if (likely(!io_is_fallback_req(req)))
1002 kmem_cache_free(req_cachep, req);
1003 else
1004 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001005}
1006
Jackie Liua197f662019-11-08 08:09:12 -07001007static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001008{
Jackie Liua197f662019-11-08 08:09:12 -07001009 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001010 int ret;
1011
Jens Axboe2d283902019-12-04 11:08:05 -07001012 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001013 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001014 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001015 io_commit_cqring(ctx);
1016 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001017 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001018 return true;
1019 }
1020
1021 return false;
1022}
1023
Jens Axboeba816ad2019-09-28 11:36:45 -06001024static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001025{
Jens Axboe2665abf2019-11-05 12:40:47 -07001026 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001027 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001028
Jens Axboe4d7dd462019-11-20 13:03:52 -07001029 /* Already got next link */
1030 if (req->flags & REQ_F_LINK_NEXT)
1031 return;
1032
Jens Axboe9e645e112019-05-10 16:07:28 -06001033 /*
1034 * The list should never be empty when we are called here. But could
1035 * potentially happen if the chain is messed up, check to be on the
1036 * safe side.
1037 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001038 while (!list_empty(&req->link_list)) {
1039 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1040 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001041
Pavel Begunkov44932332019-12-05 16:16:35 +03001042 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1043 (nxt->flags & REQ_F_TIMEOUT))) {
1044 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001045 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001046 req->flags &= ~REQ_F_LINK_TIMEOUT;
1047 continue;
1048 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001049
Pavel Begunkov44932332019-12-05 16:16:35 +03001050 list_del_init(&req->link_list);
1051 if (!list_empty(&nxt->link_list))
1052 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001053 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001054 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001055 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001056
Jens Axboe4d7dd462019-11-20 13:03:52 -07001057 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001058 if (wake_ev)
1059 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001060}
1061
1062/*
1063 * Called if REQ_F_LINK is set, and we fail the head request
1064 */
1065static void io_fail_links(struct io_kiocb *req)
1066{
Jens Axboe2665abf2019-11-05 12:40:47 -07001067 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001068 unsigned long flags;
1069
1070 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001071
1072 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001073 struct io_kiocb *link = list_first_entry(&req->link_list,
1074 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001075
Pavel Begunkov44932332019-12-05 16:16:35 +03001076 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001077 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001078
1079 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001080 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001081 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001082 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001083 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001084 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001085 }
Jens Axboe5d960722019-11-19 15:31:28 -07001086 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001087 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001088
1089 io_commit_cqring(ctx);
1090 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1091 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001092}
1093
Jens Axboe4d7dd462019-11-20 13:03:52 -07001094static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001095{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001096 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001097 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001098
Jens Axboe9e645e112019-05-10 16:07:28 -06001099 /*
1100 * If LINK is set, we have dependent requests in this chain. If we
1101 * didn't fail this request, queue the first one up, moving any other
1102 * dependencies to the next request. In case of failure, fail the rest
1103 * of the chain.
1104 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001105 if (req->flags & REQ_F_FAIL_LINK) {
1106 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001107 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1108 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001109 struct io_ring_ctx *ctx = req->ctx;
1110 unsigned long flags;
1111
1112 /*
1113 * If this is a timeout link, we could be racing with the
1114 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001115 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001116 */
1117 spin_lock_irqsave(&ctx->completion_lock, flags);
1118 io_req_link_next(req, nxt);
1119 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1120 } else {
1121 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001122 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001123}
Jens Axboe9e645e112019-05-10 16:07:28 -06001124
Jackie Liuc69f8db2019-11-09 11:00:08 +08001125static void io_free_req(struct io_kiocb *req)
1126{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001127 struct io_kiocb *nxt = NULL;
1128
1129 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001130 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001131
1132 if (nxt)
1133 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001134}
1135
Jens Axboeba816ad2019-09-28 11:36:45 -06001136/*
1137 * Drop reference to request, return next in chain (if there is one) if this
1138 * was the last reference to this request.
1139 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001140__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001141static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001142{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001143 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001144
Jens Axboee65ef562019-03-12 10:16:44 -06001145 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001146 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001147}
1148
Jens Axboe2b188cc2019-01-07 10:46:33 -07001149static void io_put_req(struct io_kiocb *req)
1150{
Jens Axboedef596e2019-01-09 08:59:42 -07001151 if (refcount_dec_and_test(&req->refs))
1152 io_free_req(req);
1153}
1154
Jens Axboe978db572019-11-14 22:39:04 -07001155/*
1156 * Must only be used if we don't need to care about links, usually from
1157 * within the completion handling itself.
1158 */
1159static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001160{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001161 /* drop both submit and complete references */
1162 if (refcount_sub_and_test(2, &req->refs))
1163 __io_free_req(req);
1164}
1165
Jens Axboe978db572019-11-14 22:39:04 -07001166static void io_double_put_req(struct io_kiocb *req)
1167{
1168 /* drop both submit and complete references */
1169 if (refcount_sub_and_test(2, &req->refs))
1170 io_free_req(req);
1171}
1172
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001173static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001174{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001175 struct io_rings *rings = ctx->rings;
1176
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001177 /*
1178 * noflush == true is from the waitqueue handler, just ensure we wake
1179 * up the task, and the next invocation will flush the entries. We
1180 * cannot safely to it from here.
1181 */
1182 if (noflush && !list_empty(&ctx->cq_overflow_list))
1183 return -1U;
1184
1185 io_cqring_overflow_flush(ctx, false);
1186
Jens Axboea3a0e432019-08-20 11:03:11 -06001187 /* See comment at the top of this file */
1188 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001189 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001190}
1191
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001192static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1193{
1194 struct io_rings *rings = ctx->rings;
1195
1196 /* make sure SQ entry isn't read before tail */
1197 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1198}
1199
Jens Axboedef596e2019-01-09 08:59:42 -07001200/*
1201 * Find and free completed poll iocbs
1202 */
1203static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1204 struct list_head *done)
1205{
1206 void *reqs[IO_IOPOLL_BATCH];
1207 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001208 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001209
Jens Axboe09bb8392019-03-13 12:39:28 -06001210 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001211 while (!list_empty(done)) {
1212 req = list_first_entry(done, struct io_kiocb, list);
1213 list_del(&req->list);
1214
Jens Axboe78e19bb2019-11-06 15:21:34 -07001215 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001216 (*nr_events)++;
1217
Jens Axboe09bb8392019-03-13 12:39:28 -06001218 if (refcount_dec_and_test(&req->refs)) {
1219 /* If we're not using fixed files, we have to pair the
1220 * completion part with the file put. Use regular
1221 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001222 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001223 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001224 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1225 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1226 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001227 reqs[to_free++] = req;
1228 if (to_free == ARRAY_SIZE(reqs))
1229 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001230 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001231 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001232 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001233 }
Jens Axboedef596e2019-01-09 08:59:42 -07001234 }
Jens Axboedef596e2019-01-09 08:59:42 -07001235
Jens Axboe09bb8392019-03-13 12:39:28 -06001236 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001237 io_free_req_many(ctx, reqs, &to_free);
1238}
1239
1240static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1241 long min)
1242{
1243 struct io_kiocb *req, *tmp;
1244 LIST_HEAD(done);
1245 bool spin;
1246 int ret;
1247
1248 /*
1249 * Only spin for completions if we don't have multiple devices hanging
1250 * off our complete list, and we're under the requested amount.
1251 */
1252 spin = !ctx->poll_multi_file && *nr_events < min;
1253
1254 ret = 0;
1255 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001256 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001257
1258 /*
1259 * Move completed entries to our local list. If we find a
1260 * request that requires polling, break out and complete
1261 * the done list first, if we have entries there.
1262 */
1263 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1264 list_move_tail(&req->list, &done);
1265 continue;
1266 }
1267 if (!list_empty(&done))
1268 break;
1269
1270 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1271 if (ret < 0)
1272 break;
1273
1274 if (ret && spin)
1275 spin = false;
1276 ret = 0;
1277 }
1278
1279 if (!list_empty(&done))
1280 io_iopoll_complete(ctx, nr_events, &done);
1281
1282 return ret;
1283}
1284
1285/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001286 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001287 * non-spinning poll check - we'll still enter the driver poll loop, but only
1288 * as a non-spinning completion check.
1289 */
1290static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1291 long min)
1292{
Jens Axboe08f54392019-08-21 22:19:11 -06001293 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001294 int ret;
1295
1296 ret = io_do_iopoll(ctx, nr_events, min);
1297 if (ret < 0)
1298 return ret;
1299 if (!min || *nr_events >= min)
1300 return 0;
1301 }
1302
1303 return 1;
1304}
1305
1306/*
1307 * We can't just wait for polled events to come to us, we have to actively
1308 * find and complete them.
1309 */
1310static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1311{
1312 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1313 return;
1314
1315 mutex_lock(&ctx->uring_lock);
1316 while (!list_empty(&ctx->poll_list)) {
1317 unsigned int nr_events = 0;
1318
1319 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001320
1321 /*
1322 * Ensure we allow local-to-the-cpu processing to take place,
1323 * in this case we need to ensure that we reap all events.
1324 */
1325 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001326 }
1327 mutex_unlock(&ctx->uring_lock);
1328}
1329
Jens Axboe2b2ed972019-10-25 10:06:15 -06001330static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1331 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001332{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001333 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001334
1335 do {
1336 int tmin = 0;
1337
Jens Axboe500f9fb2019-08-19 12:15:59 -06001338 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001339 * Don't enter poll loop if we already have events pending.
1340 * If we do, we can potentially be spinning for commands that
1341 * already triggered a CQE (eg in error).
1342 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001343 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001344 break;
1345
1346 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001347 * If a submit got punted to a workqueue, we can have the
1348 * application entering polling for a command before it gets
1349 * issued. That app will hold the uring_lock for the duration
1350 * of the poll right here, so we need to take a breather every
1351 * now and then to ensure that the issue has a chance to add
1352 * the poll to the issued list. Otherwise we can spin here
1353 * forever, while the workqueue is stuck trying to acquire the
1354 * very same mutex.
1355 */
1356 if (!(++iters & 7)) {
1357 mutex_unlock(&ctx->uring_lock);
1358 mutex_lock(&ctx->uring_lock);
1359 }
1360
Jens Axboedef596e2019-01-09 08:59:42 -07001361 if (*nr_events < min)
1362 tmin = min - *nr_events;
1363
1364 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1365 if (ret <= 0)
1366 break;
1367 ret = 0;
1368 } while (min && !*nr_events && !need_resched());
1369
Jens Axboe2b2ed972019-10-25 10:06:15 -06001370 return ret;
1371}
1372
1373static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1374 long min)
1375{
1376 int ret;
1377
1378 /*
1379 * We disallow the app entering submit/complete with polling, but we
1380 * still need to lock the ring to prevent racing with polled issue
1381 * that got punted to a workqueue.
1382 */
1383 mutex_lock(&ctx->uring_lock);
1384 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001385 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001386 return ret;
1387}
1388
Jens Axboe491381ce2019-10-17 09:20:46 -06001389static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001390{
Jens Axboe491381ce2019-10-17 09:20:46 -06001391 /*
1392 * Tell lockdep we inherited freeze protection from submission
1393 * thread.
1394 */
1395 if (req->flags & REQ_F_ISREG) {
1396 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001397
Jens Axboe491381ce2019-10-17 09:20:46 -06001398 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001399 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001400 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001401}
1402
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001403static inline void req_set_fail_links(struct io_kiocb *req)
1404{
1405 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1406 req->flags |= REQ_F_FAIL_LINK;
1407}
1408
Jens Axboeba816ad2019-09-28 11:36:45 -06001409static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001410{
Jens Axboe9adbd452019-12-20 08:45:55 -07001411 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001412
Jens Axboe491381ce2019-10-17 09:20:46 -06001413 if (kiocb->ki_flags & IOCB_WRITE)
1414 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001415
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001416 if (res != req->result)
1417 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001418 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001419}
1420
1421static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1422{
Jens Axboe9adbd452019-12-20 08:45:55 -07001423 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001424
1425 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001426 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001427}
1428
Jens Axboeba816ad2019-09-28 11:36:45 -06001429static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1430{
Jens Axboe9adbd452019-12-20 08:45:55 -07001431 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001432 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001433
1434 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001435 io_put_req_find_next(req, &nxt);
1436
1437 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001438}
1439
Jens Axboedef596e2019-01-09 08:59:42 -07001440static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1441{
Jens Axboe9adbd452019-12-20 08:45:55 -07001442 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001443
Jens Axboe491381ce2019-10-17 09:20:46 -06001444 if (kiocb->ki_flags & IOCB_WRITE)
1445 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001446
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001447 if (res != req->result)
1448 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001449 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001450 if (res != -EAGAIN)
1451 req->flags |= REQ_F_IOPOLL_COMPLETED;
1452}
1453
1454/*
1455 * After the iocb has been issued, it's safe to be found on the poll list.
1456 * Adding the kiocb to the list AFTER submission ensures that we don't
1457 * find it from a io_iopoll_getevents() thread before the issuer is done
1458 * accessing the kiocb cookie.
1459 */
1460static void io_iopoll_req_issued(struct io_kiocb *req)
1461{
1462 struct io_ring_ctx *ctx = req->ctx;
1463
1464 /*
1465 * Track whether we have multiple files in our lists. This will impact
1466 * how we do polling eventually, not spinning if we're on potentially
1467 * different devices.
1468 */
1469 if (list_empty(&ctx->poll_list)) {
1470 ctx->poll_multi_file = false;
1471 } else if (!ctx->poll_multi_file) {
1472 struct io_kiocb *list_req;
1473
1474 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1475 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001476 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001477 ctx->poll_multi_file = true;
1478 }
1479
1480 /*
1481 * For fast devices, IO may have already completed. If it has, add
1482 * it to the front so we find it first.
1483 */
1484 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1485 list_add(&req->list, &ctx->poll_list);
1486 else
1487 list_add_tail(&req->list, &ctx->poll_list);
1488}
1489
Jens Axboe3d6770f2019-04-13 11:50:54 -06001490static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001491{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001492 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001493 int diff = state->has_refs - state->used_refs;
1494
1495 if (diff)
1496 fput_many(state->file, diff);
1497 state->file = NULL;
1498 }
1499}
1500
1501/*
1502 * Get as many references to a file as we have IOs left in this submission,
1503 * assuming most submissions are for one file, or at least that each file
1504 * has more than one submission.
1505 */
1506static struct file *io_file_get(struct io_submit_state *state, int fd)
1507{
1508 if (!state)
1509 return fget(fd);
1510
1511 if (state->file) {
1512 if (state->fd == fd) {
1513 state->used_refs++;
1514 state->ios_left--;
1515 return state->file;
1516 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001517 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001518 }
1519 state->file = fget_many(fd, state->ios_left);
1520 if (!state->file)
1521 return NULL;
1522
1523 state->fd = fd;
1524 state->has_refs = state->ios_left;
1525 state->used_refs = 1;
1526 state->ios_left--;
1527 return state->file;
1528}
1529
Jens Axboe2b188cc2019-01-07 10:46:33 -07001530/*
1531 * If we tracked the file through the SCM inflight mechanism, we could support
1532 * any file. For now, just ensure that anything potentially problematic is done
1533 * inline.
1534 */
1535static bool io_file_supports_async(struct file *file)
1536{
1537 umode_t mode = file_inode(file)->i_mode;
1538
Jens Axboe10d59342019-12-09 20:16:22 -07001539 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001540 return true;
1541 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1542 return true;
1543
1544 return false;
1545}
1546
Jens Axboe3529d8c2019-12-19 18:24:38 -07001547static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1548 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001549{
Jens Axboedef596e2019-01-09 08:59:42 -07001550 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001551 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001552 unsigned ioprio;
1553 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001554
Jens Axboe09bb8392019-03-13 12:39:28 -06001555 if (!req->file)
1556 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001557
Jens Axboe491381ce2019-10-17 09:20:46 -06001558 if (S_ISREG(file_inode(req->file)->i_mode))
1559 req->flags |= REQ_F_ISREG;
1560
Jens Axboe2b188cc2019-01-07 10:46:33 -07001561 kiocb->ki_pos = READ_ONCE(sqe->off);
1562 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1563 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1564
1565 ioprio = READ_ONCE(sqe->ioprio);
1566 if (ioprio) {
1567 ret = ioprio_check_cap(ioprio);
1568 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001569 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001570
1571 kiocb->ki_ioprio = ioprio;
1572 } else
1573 kiocb->ki_ioprio = get_current_ioprio();
1574
1575 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1576 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001577 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001578
1579 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001580 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1581 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001582 req->flags |= REQ_F_NOWAIT;
1583
1584 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001585 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001586
Jens Axboedef596e2019-01-09 08:59:42 -07001587 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001588 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1589 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001590 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001591
Jens Axboedef596e2019-01-09 08:59:42 -07001592 kiocb->ki_flags |= IOCB_HIPRI;
1593 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001594 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001595 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001596 if (kiocb->ki_flags & IOCB_HIPRI)
1597 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001598 kiocb->ki_complete = io_complete_rw;
1599 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001600
Jens Axboe3529d8c2019-12-19 18:24:38 -07001601 req->rw.addr = READ_ONCE(sqe->addr);
1602 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001603 /* we own ->private, reuse it for the buffer index */
1604 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001605 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001606 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001607}
1608
1609static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1610{
1611 switch (ret) {
1612 case -EIOCBQUEUED:
1613 break;
1614 case -ERESTARTSYS:
1615 case -ERESTARTNOINTR:
1616 case -ERESTARTNOHAND:
1617 case -ERESTART_RESTARTBLOCK:
1618 /*
1619 * We can't just restart the syscall, since previously
1620 * submitted sqes may already be in progress. Just fail this
1621 * IO with EINTR.
1622 */
1623 ret = -EINTR;
1624 /* fall through */
1625 default:
1626 kiocb->ki_complete(kiocb, ret, 0);
1627 }
1628}
1629
Jens Axboeba816ad2019-09-28 11:36:45 -06001630static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1631 bool in_async)
1632{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001633 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001634 *nxt = __io_complete_rw(kiocb, ret);
1635 else
1636 io_rw_done(kiocb, ret);
1637}
1638
Jens Axboe9adbd452019-12-20 08:45:55 -07001639static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001640 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001641{
Jens Axboe9adbd452019-12-20 08:45:55 -07001642 struct io_ring_ctx *ctx = req->ctx;
1643 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001644 struct io_mapped_ubuf *imu;
1645 unsigned index, buf_index;
1646 size_t offset;
1647 u64 buf_addr;
1648
1649 /* attempt to use fixed buffers without having provided iovecs */
1650 if (unlikely(!ctx->user_bufs))
1651 return -EFAULT;
1652
Jens Axboe9adbd452019-12-20 08:45:55 -07001653 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001654 if (unlikely(buf_index >= ctx->nr_user_bufs))
1655 return -EFAULT;
1656
1657 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1658 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001659 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001660
1661 /* overflow */
1662 if (buf_addr + len < buf_addr)
1663 return -EFAULT;
1664 /* not inside the mapped region */
1665 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1666 return -EFAULT;
1667
1668 /*
1669 * May not be a start of buffer, set size appropriately
1670 * and advance us to the beginning.
1671 */
1672 offset = buf_addr - imu->ubuf;
1673 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001674
1675 if (offset) {
1676 /*
1677 * Don't use iov_iter_advance() here, as it's really slow for
1678 * using the latter parts of a big fixed buffer - it iterates
1679 * over each segment manually. We can cheat a bit here, because
1680 * we know that:
1681 *
1682 * 1) it's a BVEC iter, we set it up
1683 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1684 * first and last bvec
1685 *
1686 * So just find our index, and adjust the iterator afterwards.
1687 * If the offset is within the first bvec (or the whole first
1688 * bvec, just use iov_iter_advance(). This makes it easier
1689 * since we can just skip the first segment, which may not
1690 * be PAGE_SIZE aligned.
1691 */
1692 const struct bio_vec *bvec = imu->bvec;
1693
1694 if (offset <= bvec->bv_len) {
1695 iov_iter_advance(iter, offset);
1696 } else {
1697 unsigned long seg_skip;
1698
1699 /* skip first vec */
1700 offset -= bvec->bv_len;
1701 seg_skip = 1 + (offset >> PAGE_SHIFT);
1702
1703 iter->bvec = bvec + seg_skip;
1704 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001705 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001706 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001707 }
1708 }
1709
Jens Axboe5e559562019-11-13 16:12:46 -07001710 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001711}
1712
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001713static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1714 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001715{
Jens Axboe9adbd452019-12-20 08:45:55 -07001716 void __user *buf = u64_to_user_ptr(req->rw.addr);
1717 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001718 u8 opcode;
1719
Jens Axboed625c6e2019-12-17 19:53:05 -07001720 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001721 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001722 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001723 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001724 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001725
Jens Axboe9adbd452019-12-20 08:45:55 -07001726 /* buffer index only valid with fixed read/write */
1727 if (req->rw.kiocb.private)
1728 return -EINVAL;
1729
Jens Axboef67676d2019-12-02 11:03:47 -07001730 if (req->io) {
1731 struct io_async_rw *iorw = &req->io->rw;
1732
1733 *iovec = iorw->iov;
1734 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1735 if (iorw->iov == iorw->fast_iov)
1736 *iovec = NULL;
1737 return iorw->size;
1738 }
1739
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001740 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001741 return -EFAULT;
1742
1743#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001744 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001745 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1746 iovec, iter);
1747#endif
1748
1749 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1750}
1751
Jens Axboe32960612019-09-23 11:05:34 -06001752/*
1753 * For files that don't have ->read_iter() and ->write_iter(), handle them
1754 * by looping over ->read() or ->write() manually.
1755 */
1756static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1757 struct iov_iter *iter)
1758{
1759 ssize_t ret = 0;
1760
1761 /*
1762 * Don't support polled IO through this interface, and we can't
1763 * support non-blocking either. For the latter, this just causes
1764 * the kiocb to be handled from an async context.
1765 */
1766 if (kiocb->ki_flags & IOCB_HIPRI)
1767 return -EOPNOTSUPP;
1768 if (kiocb->ki_flags & IOCB_NOWAIT)
1769 return -EAGAIN;
1770
1771 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001772 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001773 ssize_t nr;
1774
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001775 if (!iov_iter_is_bvec(iter)) {
1776 iovec = iov_iter_iovec(iter);
1777 } else {
1778 /* fixed buffers import bvec */
1779 iovec.iov_base = kmap(iter->bvec->bv_page)
1780 + iter->iov_offset;
1781 iovec.iov_len = min(iter->count,
1782 iter->bvec->bv_len - iter->iov_offset);
1783 }
1784
Jens Axboe32960612019-09-23 11:05:34 -06001785 if (rw == READ) {
1786 nr = file->f_op->read(file, iovec.iov_base,
1787 iovec.iov_len, &kiocb->ki_pos);
1788 } else {
1789 nr = file->f_op->write(file, iovec.iov_base,
1790 iovec.iov_len, &kiocb->ki_pos);
1791 }
1792
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001793 if (iov_iter_is_bvec(iter))
1794 kunmap(iter->bvec->bv_page);
1795
Jens Axboe32960612019-09-23 11:05:34 -06001796 if (nr < 0) {
1797 if (!ret)
1798 ret = nr;
1799 break;
1800 }
1801 ret += nr;
1802 if (nr != iovec.iov_len)
1803 break;
1804 iov_iter_advance(iter, nr);
1805 }
1806
1807 return ret;
1808}
1809
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001810static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07001811 struct iovec *iovec, struct iovec *fast_iov,
1812 struct iov_iter *iter)
1813{
1814 req->io->rw.nr_segs = iter->nr_segs;
1815 req->io->rw.size = io_size;
1816 req->io->rw.iov = iovec;
1817 if (!req->io->rw.iov) {
1818 req->io->rw.iov = req->io->rw.fast_iov;
1819 memcpy(req->io->rw.iov, fast_iov,
1820 sizeof(struct iovec) * iter->nr_segs);
1821 }
1822}
1823
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001824static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07001825{
1826 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07001827 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001828}
1829
1830static void io_rw_async(struct io_wq_work **workptr)
1831{
1832 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1833 struct iovec *iov = NULL;
1834
1835 if (req->io->rw.iov != req->io->rw.fast_iov)
1836 iov = req->io->rw.iov;
1837 io_wq_submit_work(workptr);
1838 kfree(iov);
1839}
1840
1841static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
1842 struct iovec *iovec, struct iovec *fast_iov,
1843 struct iov_iter *iter)
1844{
Jens Axboe74566df2020-01-13 19:23:24 -07001845 if (req->opcode == IORING_OP_READ_FIXED ||
1846 req->opcode == IORING_OP_WRITE_FIXED)
1847 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001848 if (!req->io && io_alloc_async_ctx(req))
1849 return -ENOMEM;
1850
1851 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
1852 req->work.func = io_rw_async;
1853 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001854}
1855
Jens Axboe3529d8c2019-12-19 18:24:38 -07001856static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1857 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07001858{
Jens Axboe3529d8c2019-12-19 18:24:38 -07001859 struct io_async_ctx *io;
1860 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07001861 ssize_t ret;
1862
Jens Axboe3529d8c2019-12-19 18:24:38 -07001863 ret = io_prep_rw(req, sqe, force_nonblock);
1864 if (ret)
1865 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07001866
Jens Axboe3529d8c2019-12-19 18:24:38 -07001867 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1868 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07001869
Jens Axboe3529d8c2019-12-19 18:24:38 -07001870 if (!req->io)
1871 return 0;
1872
1873 io = req->io;
1874 io->rw.iov = io->rw.fast_iov;
1875 req->io = NULL;
1876 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
1877 req->io = io;
1878 if (ret < 0)
1879 return ret;
1880
1881 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
1882 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001883}
1884
Pavel Begunkov267bc902019-11-07 01:41:08 +03001885static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001886 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001887{
1888 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001889 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001890 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001891 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001892 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001893
Jens Axboe3529d8c2019-12-19 18:24:38 -07001894 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07001895 if (ret < 0)
1896 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001897
Jens Axboefd6c2e42019-12-18 12:19:41 -07001898 /* Ensure we clear previously set non-block flag */
1899 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001900 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001901
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08001902 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001903 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001904 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001905 req->result = io_size;
1906
1907 /*
1908 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1909 * we know to async punt it even if it was opened O_NONBLOCK
1910 */
Jens Axboe9adbd452019-12-20 08:45:55 -07001911 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07001912 req->flags |= REQ_F_MUST_PUNT;
1913 goto copy_iov;
1914 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001915
Jens Axboe31b51512019-01-18 22:56:34 -07001916 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07001917 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001918 if (!ret) {
1919 ssize_t ret2;
1920
Jens Axboe9adbd452019-12-20 08:45:55 -07001921 if (req->file->f_op->read_iter)
1922 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001923 else
Jens Axboe9adbd452019-12-20 08:45:55 -07001924 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001925
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001926 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07001927 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001928 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001929 } else {
1930copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001931 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001932 inline_vecs, &iter);
1933 if (ret)
1934 goto out_free;
1935 return -EAGAIN;
1936 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001937 }
Jens Axboef67676d2019-12-02 11:03:47 -07001938out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001939 if (!io_wq_current_is_worker())
1940 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001941 return ret;
1942}
1943
Jens Axboe3529d8c2019-12-19 18:24:38 -07001944static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1945 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07001946{
Jens Axboe3529d8c2019-12-19 18:24:38 -07001947 struct io_async_ctx *io;
1948 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07001949 ssize_t ret;
1950
Jens Axboe3529d8c2019-12-19 18:24:38 -07001951 ret = io_prep_rw(req, sqe, force_nonblock);
1952 if (ret)
1953 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07001954
Jens Axboe3529d8c2019-12-19 18:24:38 -07001955 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
1956 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07001957
Jens Axboe3529d8c2019-12-19 18:24:38 -07001958 if (!req->io)
1959 return 0;
1960
1961 io = req->io;
1962 io->rw.iov = io->rw.fast_iov;
1963 req->io = NULL;
1964 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
1965 req->io = io;
1966 if (ret < 0)
1967 return ret;
1968
1969 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
1970 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001971}
1972
Pavel Begunkov267bc902019-11-07 01:41:08 +03001973static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001974 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001975{
1976 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001977 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001978 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001979 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001980 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001981
Jens Axboe3529d8c2019-12-19 18:24:38 -07001982 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07001983 if (ret < 0)
1984 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001985
Jens Axboefd6c2e42019-12-18 12:19:41 -07001986 /* Ensure we clear previously set non-block flag */
1987 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001988 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001989
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08001990 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001991 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001992 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001993 req->result = io_size;
1994
1995 /*
1996 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1997 * we know to async punt it even if it was opened O_NONBLOCK
1998 */
1999 if (force_nonblock && !io_file_supports_async(req->file)) {
2000 req->flags |= REQ_F_MUST_PUNT;
2001 goto copy_iov;
2002 }
2003
Jens Axboe10d59342019-12-09 20:16:22 -07002004 /* file path doesn't support NOWAIT for non-direct_IO */
2005 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2006 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002007 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002008
Jens Axboe31b51512019-01-18 22:56:34 -07002009 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002010 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002011 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002012 ssize_t ret2;
2013
Jens Axboe2b188cc2019-01-07 10:46:33 -07002014 /*
2015 * Open-code file_start_write here to grab freeze protection,
2016 * which will be released by another thread in
2017 * io_complete_rw(). Fool lockdep by telling it the lock got
2018 * released so that it doesn't complain about the held lock when
2019 * we return to userspace.
2020 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002021 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002022 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002023 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002024 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002025 SB_FREEZE_WRITE);
2026 }
2027 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002028
Jens Axboe9adbd452019-12-20 08:45:55 -07002029 if (req->file->f_op->write_iter)
2030 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002031 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002032 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002033 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002034 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002035 } else {
2036copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002037 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002038 inline_vecs, &iter);
2039 if (ret)
2040 goto out_free;
2041 return -EAGAIN;
2042 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002043 }
Jens Axboe31b51512019-01-18 22:56:34 -07002044out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002045 if (!io_wq_current_is_worker())
2046 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002047 return ret;
2048}
2049
2050/*
2051 * IORING_OP_NOP just posts a completion event, nothing else.
2052 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002053static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002054{
2055 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002056
Jens Axboedef596e2019-01-09 08:59:42 -07002057 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2058 return -EINVAL;
2059
Jens Axboe78e19bb2019-11-06 15:21:34 -07002060 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002061 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002062 return 0;
2063}
2064
Jens Axboe3529d8c2019-12-19 18:24:38 -07002065static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002066{
Jens Axboe6b063142019-01-10 22:13:58 -07002067 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002068
Jens Axboe09bb8392019-03-13 12:39:28 -06002069 if (!req->file)
2070 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002071
Jens Axboe6b063142019-01-10 22:13:58 -07002072 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002073 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002074 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002075 return -EINVAL;
2076
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002077 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2078 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2079 return -EINVAL;
2080
2081 req->sync.off = READ_ONCE(sqe->off);
2082 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002083 return 0;
2084}
2085
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002086static bool io_req_cancelled(struct io_kiocb *req)
2087{
2088 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2089 req_set_fail_links(req);
2090 io_cqring_add_event(req, -ECANCELED);
2091 io_put_req(req);
2092 return true;
2093 }
2094
2095 return false;
2096}
2097
Jens Axboe78912932020-01-14 22:09:06 -07002098static void io_link_work_cb(struct io_wq_work **workptr)
2099{
2100 struct io_wq_work *work = *workptr;
2101 struct io_kiocb *link = work->data;
2102
2103 io_queue_linked_timeout(link);
2104 work->func = io_wq_submit_work;
2105}
2106
2107static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2108{
2109 struct io_kiocb *link;
2110
2111 io_prep_async_work(nxt, &link);
2112 *workptr = &nxt->work;
2113 if (link) {
2114 nxt->work.flags |= IO_WQ_WORK_CB;
2115 nxt->work.func = io_link_work_cb;
2116 nxt->work.data = link;
2117 }
2118}
2119
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002120static void io_fsync_finish(struct io_wq_work **workptr)
2121{
2122 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2123 loff_t end = req->sync.off + req->sync.len;
2124 struct io_kiocb *nxt = NULL;
2125 int ret;
2126
2127 if (io_req_cancelled(req))
2128 return;
2129
Jens Axboe9adbd452019-12-20 08:45:55 -07002130 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002131 end > 0 ? end : LLONG_MAX,
2132 req->sync.flags & IORING_FSYNC_DATASYNC);
2133 if (ret < 0)
2134 req_set_fail_links(req);
2135 io_cqring_add_event(req, ret);
2136 io_put_req_find_next(req, &nxt);
2137 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002138 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002139}
2140
Jens Axboefc4df992019-12-10 14:38:45 -07002141static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2142 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002143{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002144 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002145
2146 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002147 if (force_nonblock) {
2148 io_put_req(req);
2149 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002150 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002151 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002152
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002153 work = old_work = &req->work;
2154 io_fsync_finish(&work);
2155 if (work && work != old_work)
2156 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002157 return 0;
2158}
2159
Jens Axboed63d1b52019-12-10 10:38:56 -07002160static void io_fallocate_finish(struct io_wq_work **workptr)
2161{
2162 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2163 struct io_kiocb *nxt = NULL;
2164 int ret;
2165
2166 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2167 req->sync.len);
2168 if (ret < 0)
2169 req_set_fail_links(req);
2170 io_cqring_add_event(req, ret);
2171 io_put_req_find_next(req, &nxt);
2172 if (nxt)
2173 io_wq_assign_next(workptr, nxt);
2174}
2175
2176static int io_fallocate_prep(struct io_kiocb *req,
2177 const struct io_uring_sqe *sqe)
2178{
2179 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2180 return -EINVAL;
2181
2182 req->sync.off = READ_ONCE(sqe->off);
2183 req->sync.len = READ_ONCE(sqe->addr);
2184 req->sync.mode = READ_ONCE(sqe->len);
2185 return 0;
2186}
2187
2188static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2189 bool force_nonblock)
2190{
2191 struct io_wq_work *work, *old_work;
2192
2193 /* fallocate always requiring blocking context */
2194 if (force_nonblock) {
2195 io_put_req(req);
2196 req->work.func = io_fallocate_finish;
2197 return -EAGAIN;
2198 }
2199
2200 work = old_work = &req->work;
2201 io_fallocate_finish(&work);
2202 if (work && work != old_work)
2203 *nxt = container_of(work, struct io_kiocb, work);
2204
2205 return 0;
2206}
2207
Jens Axboe15b71ab2019-12-11 11:20:36 -07002208static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2209{
2210 int ret;
2211
2212 if (sqe->ioprio || sqe->buf_index)
2213 return -EINVAL;
2214
2215 req->open.dfd = READ_ONCE(sqe->fd);
2216 req->open.mode = READ_ONCE(sqe->len);
2217 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2218 req->open.flags = READ_ONCE(sqe->open_flags);
2219
2220 req->open.filename = getname(req->open.fname);
2221 if (IS_ERR(req->open.filename)) {
2222 ret = PTR_ERR(req->open.filename);
2223 req->open.filename = NULL;
2224 return ret;
2225 }
2226
2227 return 0;
2228}
2229
2230static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2231 bool force_nonblock)
2232{
2233 struct open_flags op;
2234 struct open_how how;
2235 struct file *file;
2236 int ret;
2237
2238 if (force_nonblock) {
2239 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2240 return -EAGAIN;
2241 }
2242
2243 how = build_open_how(req->open.flags, req->open.mode);
2244 ret = build_open_flags(&how, &op);
2245 if (ret)
2246 goto err;
2247
2248 ret = get_unused_fd_flags(how.flags);
2249 if (ret < 0)
2250 goto err;
2251
2252 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2253 if (IS_ERR(file)) {
2254 put_unused_fd(ret);
2255 ret = PTR_ERR(file);
2256 } else {
2257 fsnotify_open(file);
2258 fd_install(ret, file);
2259 }
2260err:
2261 putname(req->open.filename);
2262 if (ret < 0)
2263 req_set_fail_links(req);
2264 io_cqring_add_event(req, ret);
2265 io_put_req_find_next(req, nxt);
2266 return 0;
2267}
2268
Jens Axboeb5dba592019-12-11 14:02:38 -07002269static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2270{
2271 /*
2272 * If we queue this for async, it must not be cancellable. That would
2273 * leave the 'file' in an undeterminate state.
2274 */
2275 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2276
2277 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2278 sqe->rw_flags || sqe->buf_index)
2279 return -EINVAL;
2280 if (sqe->flags & IOSQE_FIXED_FILE)
2281 return -EINVAL;
2282
2283 req->close.fd = READ_ONCE(sqe->fd);
2284 if (req->file->f_op == &io_uring_fops ||
2285 req->close.fd == req->ring_fd)
2286 return -EBADF;
2287
2288 return 0;
2289}
2290
2291static void io_close_finish(struct io_wq_work **workptr)
2292{
2293 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2294 struct io_kiocb *nxt = NULL;
2295
2296 /* Invoked with files, we need to do the close */
2297 if (req->work.files) {
2298 int ret;
2299
2300 ret = filp_close(req->close.put_file, req->work.files);
2301 if (ret < 0) {
2302 req_set_fail_links(req);
2303 }
2304 io_cqring_add_event(req, ret);
2305 }
2306
2307 fput(req->close.put_file);
2308
2309 /* we bypassed the re-issue, drop the submission reference */
2310 io_put_req(req);
2311 io_put_req_find_next(req, &nxt);
2312 if (nxt)
2313 io_wq_assign_next(workptr, nxt);
2314}
2315
2316static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2317 bool force_nonblock)
2318{
2319 int ret;
2320
2321 req->close.put_file = NULL;
2322 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2323 if (ret < 0)
2324 return ret;
2325
2326 /* if the file has a flush method, be safe and punt to async */
2327 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2328 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2329 goto eagain;
2330 }
2331
2332 /*
2333 * No ->flush(), safely close from here and just punt the
2334 * fput() to async context.
2335 */
2336 ret = filp_close(req->close.put_file, current->files);
2337
2338 if (ret < 0)
2339 req_set_fail_links(req);
2340 io_cqring_add_event(req, ret);
2341
2342 if (io_wq_current_is_worker()) {
2343 struct io_wq_work *old_work, *work;
2344
2345 old_work = work = &req->work;
2346 io_close_finish(&work);
2347 if (work && work != old_work)
2348 *nxt = container_of(work, struct io_kiocb, work);
2349 return 0;
2350 }
2351
2352eagain:
2353 req->work.func = io_close_finish;
2354 return -EAGAIN;
2355}
2356
Jens Axboe3529d8c2019-12-19 18:24:38 -07002357static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002358{
2359 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002360
2361 if (!req->file)
2362 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002363
2364 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2365 return -EINVAL;
2366 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2367 return -EINVAL;
2368
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002369 req->sync.off = READ_ONCE(sqe->off);
2370 req->sync.len = READ_ONCE(sqe->len);
2371 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002372 return 0;
2373}
2374
2375static void io_sync_file_range_finish(struct io_wq_work **workptr)
2376{
2377 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2378 struct io_kiocb *nxt = NULL;
2379 int ret;
2380
2381 if (io_req_cancelled(req))
2382 return;
2383
Jens Axboe9adbd452019-12-20 08:45:55 -07002384 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002385 req->sync.flags);
2386 if (ret < 0)
2387 req_set_fail_links(req);
2388 io_cqring_add_event(req, ret);
2389 io_put_req_find_next(req, &nxt);
2390 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002391 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002392}
2393
Jens Axboefc4df992019-12-10 14:38:45 -07002394static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002395 bool force_nonblock)
2396{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002397 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002398
2399 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002400 if (force_nonblock) {
2401 io_put_req(req);
2402 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002403 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002404 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002405
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002406 work = old_work = &req->work;
2407 io_sync_file_range_finish(&work);
2408 if (work && work != old_work)
2409 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002410 return 0;
2411}
2412
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002413#if defined(CONFIG_NET)
2414static void io_sendrecv_async(struct io_wq_work **workptr)
2415{
2416 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2417 struct iovec *iov = NULL;
2418
2419 if (req->io->rw.iov != req->io->rw.fast_iov)
2420 iov = req->io->msg.iov;
2421 io_wq_submit_work(workptr);
2422 kfree(iov);
2423}
2424#endif
2425
Jens Axboe3529d8c2019-12-19 18:24:38 -07002426static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002427{
Jens Axboe03b12302019-12-02 18:50:25 -07002428#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002429 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002430 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002431
Jens Axboee47293f2019-12-20 08:58:21 -07002432 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2433 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe3529d8c2019-12-19 18:24:38 -07002434
2435 if (!io)
2436 return 0;
2437
Jens Axboed9688562019-12-09 19:35:20 -07002438 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002439 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002440 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002441#else
Jens Axboee47293f2019-12-20 08:58:21 -07002442 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002443#endif
2444}
2445
Jens Axboefc4df992019-12-10 14:38:45 -07002446static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2447 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002448{
2449#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002450 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002451 struct socket *sock;
2452 int ret;
2453
2454 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2455 return -EINVAL;
2456
2457 sock = sock_from_file(req->file, &ret);
2458 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002459 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002460 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002461 unsigned flags;
2462
Jens Axboe03b12302019-12-02 18:50:25 -07002463 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002464 kmsg = &req->io->msg;
2465 kmsg->msg.msg_name = &addr;
2466 /* if iov is set, it's allocated already */
2467 if (!kmsg->iov)
2468 kmsg->iov = kmsg->fast_iov;
2469 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002470 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002471 struct io_sr_msg *sr = &req->sr_msg;
2472
Jens Axboe0b416c32019-12-15 10:57:46 -07002473 kmsg = &io.msg;
2474 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002475
2476 io.msg.iov = io.msg.fast_iov;
2477 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2478 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002479 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002480 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002481 }
2482
Jens Axboee47293f2019-12-20 08:58:21 -07002483 flags = req->sr_msg.msg_flags;
2484 if (flags & MSG_DONTWAIT)
2485 req->flags |= REQ_F_NOWAIT;
2486 else if (force_nonblock)
2487 flags |= MSG_DONTWAIT;
2488
Jens Axboe0b416c32019-12-15 10:57:46 -07002489 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002490 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002491 if (req->io)
2492 return -EAGAIN;
2493 if (io_alloc_async_ctx(req))
2494 return -ENOMEM;
2495 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2496 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002497 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002498 }
2499 if (ret == -ERESTARTSYS)
2500 ret = -EINTR;
2501 }
2502
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002503 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002504 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002505 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002506 if (ret < 0)
2507 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002508 io_put_req_find_next(req, nxt);
2509 return 0;
2510#else
2511 return -EOPNOTSUPP;
2512#endif
2513}
2514
Jens Axboe3529d8c2019-12-19 18:24:38 -07002515static int io_recvmsg_prep(struct io_kiocb *req,
2516 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002517{
2518#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002519 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002520 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002521
Jens Axboe3529d8c2019-12-19 18:24:38 -07002522 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2523 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2524
2525 if (!io)
Jens Axboe06b76d42019-12-19 14:44:26 -07002526 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002527
Jens Axboed9688562019-12-09 19:35:20 -07002528 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002529 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002530 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002531#else
Jens Axboee47293f2019-12-20 08:58:21 -07002532 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002533#endif
2534}
2535
Jens Axboefc4df992019-12-10 14:38:45 -07002536static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2537 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002538{
2539#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002540 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002541 struct socket *sock;
2542 int ret;
2543
2544 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2545 return -EINVAL;
2546
2547 sock = sock_from_file(req->file, &ret);
2548 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002549 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002550 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002551 unsigned flags;
2552
Jens Axboe03b12302019-12-02 18:50:25 -07002553 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002554 kmsg = &req->io->msg;
2555 kmsg->msg.msg_name = &addr;
2556 /* if iov is set, it's allocated already */
2557 if (!kmsg->iov)
2558 kmsg->iov = kmsg->fast_iov;
2559 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002560 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002561 struct io_sr_msg *sr = &req->sr_msg;
2562
Jens Axboe0b416c32019-12-15 10:57:46 -07002563 kmsg = &io.msg;
2564 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002565
2566 io.msg.iov = io.msg.fast_iov;
2567 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
2568 sr->msg_flags, &io.msg.uaddr,
2569 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002570 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002571 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002572 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002573
Jens Axboee47293f2019-12-20 08:58:21 -07002574 flags = req->sr_msg.msg_flags;
2575 if (flags & MSG_DONTWAIT)
2576 req->flags |= REQ_F_NOWAIT;
2577 else if (force_nonblock)
2578 flags |= MSG_DONTWAIT;
2579
2580 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
2581 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002582 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002583 if (req->io)
2584 return -EAGAIN;
2585 if (io_alloc_async_ctx(req))
2586 return -ENOMEM;
2587 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2588 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002589 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002590 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002591 if (ret == -ERESTARTSYS)
2592 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002593 }
2594
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002595 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002596 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002597 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002598 if (ret < 0)
2599 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002600 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002601 return 0;
2602#else
2603 return -EOPNOTSUPP;
2604#endif
2605}
2606
Jens Axboe3529d8c2019-12-19 18:24:38 -07002607static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002608{
2609#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002610 struct io_accept *accept = &req->accept;
2611
Jens Axboe17f2fe32019-10-17 14:42:58 -06002612 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2613 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002614 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002615 return -EINVAL;
2616
Jens Axboed55e5f52019-12-11 16:12:15 -07002617 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2618 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002619 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002620 return 0;
2621#else
2622 return -EOPNOTSUPP;
2623#endif
2624}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002625
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002626#if defined(CONFIG_NET)
2627static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2628 bool force_nonblock)
2629{
2630 struct io_accept *accept = &req->accept;
2631 unsigned file_flags;
2632 int ret;
2633
2634 file_flags = force_nonblock ? O_NONBLOCK : 0;
2635 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2636 accept->addr_len, accept->flags);
2637 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002638 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002639 if (ret == -ERESTARTSYS)
2640 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002641 if (ret < 0)
2642 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002643 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002644 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002645 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002646}
2647
2648static void io_accept_finish(struct io_wq_work **workptr)
2649{
2650 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2651 struct io_kiocb *nxt = NULL;
2652
2653 if (io_req_cancelled(req))
2654 return;
2655 __io_accept(req, &nxt, false);
2656 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002657 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002658}
2659#endif
2660
2661static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2662 bool force_nonblock)
2663{
2664#if defined(CONFIG_NET)
2665 int ret;
2666
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002667 ret = __io_accept(req, nxt, force_nonblock);
2668 if (ret == -EAGAIN && force_nonblock) {
2669 req->work.func = io_accept_finish;
2670 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2671 io_put_req(req);
2672 return -EAGAIN;
2673 }
2674 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002675#else
2676 return -EOPNOTSUPP;
2677#endif
2678}
2679
Jens Axboe3529d8c2019-12-19 18:24:38 -07002680static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07002681{
2682#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002683 struct io_connect *conn = &req->connect;
2684 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07002685
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002686 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2687 return -EINVAL;
2688 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2689 return -EINVAL;
2690
Jens Axboe3529d8c2019-12-19 18:24:38 -07002691 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2692 conn->addr_len = READ_ONCE(sqe->addr2);
2693
2694 if (!io)
2695 return 0;
2696
2697 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002698 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002699#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002700 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07002701#endif
2702}
2703
Jens Axboefc4df992019-12-10 14:38:45 -07002704static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
2705 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07002706{
2707#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07002708 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002709 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002710 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002711
Jens Axboef499a022019-12-02 16:28:46 -07002712 if (req->io) {
2713 io = req->io;
2714 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002715 ret = move_addr_to_kernel(req->connect.addr,
2716 req->connect.addr_len,
2717 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002718 if (ret)
2719 goto out;
2720 io = &__io;
2721 }
2722
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002723 file_flags = force_nonblock ? O_NONBLOCK : 0;
2724
2725 ret = __sys_connect_file(req->file, &io->connect.address,
2726 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002727 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002728 if (req->io)
2729 return -EAGAIN;
2730 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07002731 ret = -ENOMEM;
2732 goto out;
2733 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002734 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07002735 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002736 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002737 if (ret == -ERESTARTSYS)
2738 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002739out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002740 if (ret < 0)
2741 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002742 io_cqring_add_event(req, ret);
2743 io_put_req_find_next(req, nxt);
2744 return 0;
2745#else
2746 return -EOPNOTSUPP;
2747#endif
2748}
2749
Jens Axboe221c5eb2019-01-17 09:41:58 -07002750static void io_poll_remove_one(struct io_kiocb *req)
2751{
2752 struct io_poll_iocb *poll = &req->poll;
2753
2754 spin_lock(&poll->head->lock);
2755 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07002756 if (!list_empty(&poll->wait.entry)) {
2757 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002758 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002759 }
2760 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002761 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002762}
2763
2764static void io_poll_remove_all(struct io_ring_ctx *ctx)
2765{
Jens Axboe78076bb2019-12-04 19:56:40 -07002766 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002767 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07002768 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002769
2770 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002771 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2772 struct hlist_head *list;
2773
2774 list = &ctx->cancel_hash[i];
2775 hlist_for_each_entry_safe(req, tmp, list, hash_node)
2776 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002777 }
2778 spin_unlock_irq(&ctx->completion_lock);
2779}
2780
Jens Axboe47f46762019-11-09 17:43:02 -07002781static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2782{
Jens Axboe78076bb2019-12-04 19:56:40 -07002783 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07002784 struct io_kiocb *req;
2785
Jens Axboe78076bb2019-12-04 19:56:40 -07002786 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2787 hlist_for_each_entry(req, list, hash_node) {
2788 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002789 io_poll_remove_one(req);
2790 return 0;
2791 }
Jens Axboe47f46762019-11-09 17:43:02 -07002792 }
2793
2794 return -ENOENT;
2795}
2796
Jens Axboe3529d8c2019-12-19 18:24:38 -07002797static int io_poll_remove_prep(struct io_kiocb *req,
2798 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002799{
Jens Axboe221c5eb2019-01-17 09:41:58 -07002800 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2801 return -EINVAL;
2802 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2803 sqe->poll_events)
2804 return -EINVAL;
2805
Jens Axboe0969e782019-12-17 18:40:57 -07002806 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07002807 return 0;
2808}
2809
2810/*
2811 * Find a running poll command that matches one specified in sqe->addr,
2812 * and remove it if found.
2813 */
2814static int io_poll_remove(struct io_kiocb *req)
2815{
2816 struct io_ring_ctx *ctx = req->ctx;
2817 u64 addr;
2818 int ret;
2819
Jens Axboe0969e782019-12-17 18:40:57 -07002820 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002821 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07002822 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002823 spin_unlock_irq(&ctx->completion_lock);
2824
Jens Axboe78e19bb2019-11-06 15:21:34 -07002825 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002826 if (ret < 0)
2827 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002828 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002829 return 0;
2830}
2831
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002832static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002833{
Jackie Liua197f662019-11-08 08:09:12 -07002834 struct io_ring_ctx *ctx = req->ctx;
2835
Jens Axboe8c838782019-03-12 15:48:16 -06002836 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002837 if (error)
2838 io_cqring_fill_event(req, error);
2839 else
2840 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002841 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002842}
2843
Jens Axboe561fb042019-10-24 07:25:42 -06002844static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002845{
Jens Axboe561fb042019-10-24 07:25:42 -06002846 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002847 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2848 struct io_poll_iocb *poll = &req->poll;
2849 struct poll_table_struct pt = { ._key = poll->events };
2850 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002851 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002852 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002853 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002854
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002855 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002856 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002857 ret = -ECANCELED;
2858 } else if (READ_ONCE(poll->canceled)) {
2859 ret = -ECANCELED;
2860 }
Jens Axboe561fb042019-10-24 07:25:42 -06002861
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002862 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002863 mask = vfs_poll(poll->file, &pt) & poll->events;
2864
2865 /*
2866 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2867 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2868 * synchronize with them. In the cancellation case the list_del_init
2869 * itself is not actually needed, but harmless so we keep it in to
2870 * avoid further branches in the fast path.
2871 */
2872 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002873 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07002874 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002875 spin_unlock_irq(&ctx->completion_lock);
2876 return;
2877 }
Jens Axboe78076bb2019-12-04 19:56:40 -07002878 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002879 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002880 spin_unlock_irq(&ctx->completion_lock);
2881
Jens Axboe8c838782019-03-12 15:48:16 -06002882 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002883
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002884 if (ret < 0)
2885 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002886 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002887 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002888 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002889}
2890
2891static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2892 void *key)
2893{
Jens Axboee9444752019-11-26 15:02:04 -07002894 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002895 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2896 struct io_ring_ctx *ctx = req->ctx;
2897 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002898 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002899
2900 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002901 if (mask && !(mask & poll->events))
2902 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002903
Jens Axboe392edb42019-12-09 17:52:20 -07002904 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002905
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002906 /*
2907 * Run completion inline if we can. We're using trylock here because
2908 * we are violating the completion_lock -> poll wq lock ordering.
2909 * If we have a link timeout we're going to need the completion_lock
2910 * for finalizing the request, mark us as having grabbed that already.
2911 */
Jens Axboe8c838782019-03-12 15:48:16 -06002912 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboe78076bb2019-12-04 19:56:40 -07002913 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002914 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002915 req->flags |= REQ_F_COMP_LOCKED;
2916 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002917 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2918
2919 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002920 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002921 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002922 }
2923
Jens Axboe221c5eb2019-01-17 09:41:58 -07002924 return 1;
2925}
2926
2927struct io_poll_table {
2928 struct poll_table_struct pt;
2929 struct io_kiocb *req;
2930 int error;
2931};
2932
2933static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2934 struct poll_table_struct *p)
2935{
2936 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2937
2938 if (unlikely(pt->req->poll.head)) {
2939 pt->error = -EINVAL;
2940 return;
2941 }
2942
2943 pt->error = 0;
2944 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07002945 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002946}
2947
Jens Axboeeac406c2019-11-14 12:09:58 -07002948static void io_poll_req_insert(struct io_kiocb *req)
2949{
2950 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07002951 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07002952
Jens Axboe78076bb2019-12-04 19:56:40 -07002953 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
2954 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07002955}
2956
Jens Axboe3529d8c2019-12-19 18:24:38 -07002957static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002958{
2959 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002960 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002961
2962 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2963 return -EINVAL;
2964 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2965 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002966 if (!poll->file)
2967 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002968
Jens Axboe221c5eb2019-01-17 09:41:58 -07002969 events = READ_ONCE(sqe->poll_events);
2970 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07002971 return 0;
2972}
2973
2974static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
2975{
2976 struct io_poll_iocb *poll = &req->poll;
2977 struct io_ring_ctx *ctx = req->ctx;
2978 struct io_poll_table ipt;
2979 bool cancel = false;
2980 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07002981
2982 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07002983 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002984
Jens Axboe221c5eb2019-01-17 09:41:58 -07002985 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002986 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002987 poll->canceled = false;
2988
2989 ipt.pt._qproc = io_poll_queue_proc;
2990 ipt.pt._key = poll->events;
2991 ipt.req = req;
2992 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2993
2994 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07002995 INIT_LIST_HEAD(&poll->wait.entry);
2996 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2997 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002998
Jens Axboe36703242019-07-25 10:20:18 -06002999 INIT_LIST_HEAD(&req->list);
3000
Jens Axboe221c5eb2019-01-17 09:41:58 -07003001 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003002
3003 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003004 if (likely(poll->head)) {
3005 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003006 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003007 if (ipt.error)
3008 cancel = true;
3009 ipt.error = 0;
3010 mask = 0;
3011 }
3012 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003013 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003014 else if (cancel)
3015 WRITE_ONCE(poll->canceled, true);
3016 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003017 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003018 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003019 }
Jens Axboe8c838782019-03-12 15:48:16 -06003020 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003021 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003022 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003023 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003024 spin_unlock_irq(&ctx->completion_lock);
3025
Jens Axboe8c838782019-03-12 15:48:16 -06003026 if (mask) {
3027 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003028 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003029 }
Jens Axboe8c838782019-03-12 15:48:16 -06003030 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003031}
3032
Jens Axboe5262f562019-09-17 12:26:57 -06003033static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3034{
Jens Axboead8a48a2019-11-15 08:49:11 -07003035 struct io_timeout_data *data = container_of(timer,
3036 struct io_timeout_data, timer);
3037 struct io_kiocb *req = data->req;
3038 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003039 unsigned long flags;
3040
Jens Axboe5262f562019-09-17 12:26:57 -06003041 atomic_inc(&ctx->cq_timeouts);
3042
3043 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003044 /*
Jens Axboe11365042019-10-16 09:08:32 -06003045 * We could be racing with timeout deletion. If the list is empty,
3046 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003047 */
Jens Axboe842f9612019-10-29 12:34:10 -06003048 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003049 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003050
Jens Axboe11365042019-10-16 09:08:32 -06003051 /*
3052 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003053 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003054 * pointer will be increased, otherwise other timeout reqs may
3055 * return in advance without waiting for enough wait_nr.
3056 */
3057 prev = req;
3058 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3059 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003060 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003061 }
Jens Axboe842f9612019-10-29 12:34:10 -06003062
Jens Axboe78e19bb2019-11-06 15:21:34 -07003063 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003064 io_commit_cqring(ctx);
3065 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3066
3067 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003068 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003069 io_put_req(req);
3070 return HRTIMER_NORESTART;
3071}
3072
Jens Axboe47f46762019-11-09 17:43:02 -07003073static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3074{
3075 struct io_kiocb *req;
3076 int ret = -ENOENT;
3077
3078 list_for_each_entry(req, &ctx->timeout_list, list) {
3079 if (user_data == req->user_data) {
3080 list_del_init(&req->list);
3081 ret = 0;
3082 break;
3083 }
3084 }
3085
3086 if (ret == -ENOENT)
3087 return ret;
3088
Jens Axboe2d283902019-12-04 11:08:05 -07003089 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003090 if (ret == -1)
3091 return -EALREADY;
3092
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003093 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003094 io_cqring_fill_event(req, -ECANCELED);
3095 io_put_req(req);
3096 return 0;
3097}
3098
Jens Axboe3529d8c2019-12-19 18:24:38 -07003099static int io_timeout_remove_prep(struct io_kiocb *req,
3100 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003101{
Jens Axboeb29472e2019-12-17 18:50:29 -07003102 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3103 return -EINVAL;
3104 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3105 return -EINVAL;
3106
3107 req->timeout.addr = READ_ONCE(sqe->addr);
3108 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3109 if (req->timeout.flags)
3110 return -EINVAL;
3111
Jens Axboeb29472e2019-12-17 18:50:29 -07003112 return 0;
3113}
3114
Jens Axboe11365042019-10-16 09:08:32 -06003115/*
3116 * Remove or update an existing timeout command
3117 */
Jens Axboefc4df992019-12-10 14:38:45 -07003118static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003119{
3120 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003121 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003122
Jens Axboe11365042019-10-16 09:08:32 -06003123 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003124 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003125
Jens Axboe47f46762019-11-09 17:43:02 -07003126 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003127 io_commit_cqring(ctx);
3128 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003129 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003130 if (ret < 0)
3131 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003132 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003133 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003134}
3135
Jens Axboe3529d8c2019-12-19 18:24:38 -07003136static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003137 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003138{
Jens Axboead8a48a2019-11-15 08:49:11 -07003139 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003140 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003141
Jens Axboead8a48a2019-11-15 08:49:11 -07003142 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003143 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003144 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003145 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003146 if (sqe->off && is_timeout_link)
3147 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003148 flags = READ_ONCE(sqe->timeout_flags);
3149 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003150 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003151
Jens Axboe26a61672019-12-20 09:02:01 -07003152 req->timeout.count = READ_ONCE(sqe->off);
3153
Jens Axboe3529d8c2019-12-19 18:24:38 -07003154 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003155 return -ENOMEM;
3156
3157 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003158 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003159 req->flags |= REQ_F_TIMEOUT;
3160
3161 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003162 return -EFAULT;
3163
Jens Axboe11365042019-10-16 09:08:32 -06003164 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003165 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003166 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003167 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003168
Jens Axboead8a48a2019-11-15 08:49:11 -07003169 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3170 return 0;
3171}
3172
Jens Axboefc4df992019-12-10 14:38:45 -07003173static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003174{
3175 unsigned count;
3176 struct io_ring_ctx *ctx = req->ctx;
3177 struct io_timeout_data *data;
3178 struct list_head *entry;
3179 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003180
Jens Axboe2d283902019-12-04 11:08:05 -07003181 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003182
Jens Axboe5262f562019-09-17 12:26:57 -06003183 /*
3184 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003185 * timeout event to be satisfied. If it isn't set, then this is
3186 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003187 */
Jens Axboe26a61672019-12-20 09:02:01 -07003188 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003189 if (!count) {
3190 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3191 spin_lock_irq(&ctx->completion_lock);
3192 entry = ctx->timeout_list.prev;
3193 goto add;
3194 }
Jens Axboe5262f562019-09-17 12:26:57 -06003195
3196 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003197 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003198
3199 /*
3200 * Insertion sort, ensuring the first entry in the list is always
3201 * the one we need first.
3202 */
Jens Axboe5262f562019-09-17 12:26:57 -06003203 spin_lock_irq(&ctx->completion_lock);
3204 list_for_each_prev(entry, &ctx->timeout_list) {
3205 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003206 unsigned nxt_sq_head;
3207 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003208 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003209
Jens Axboe93bd25b2019-11-11 23:34:31 -07003210 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3211 continue;
3212
yangerkun5da0fb12019-10-15 21:59:29 +08003213 /*
3214 * Since cached_sq_head + count - 1 can overflow, use type long
3215 * long to store it.
3216 */
3217 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003218 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3219 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003220
3221 /*
3222 * cached_sq_head may overflow, and it will never overflow twice
3223 * once there is some timeout req still be valid.
3224 */
3225 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003226 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003227
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003228 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003229 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003230
3231 /*
3232 * Sequence of reqs after the insert one and itself should
3233 * be adjusted because each timeout req consumes a slot.
3234 */
3235 span++;
3236 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003237 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003238 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003239add:
Jens Axboe5262f562019-09-17 12:26:57 -06003240 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003241 data->timer.function = io_timeout_fn;
3242 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003243 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003244 return 0;
3245}
3246
Jens Axboe62755e32019-10-28 21:49:21 -06003247static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003248{
Jens Axboe62755e32019-10-28 21:49:21 -06003249 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003250
Jens Axboe62755e32019-10-28 21:49:21 -06003251 return req->user_data == (unsigned long) data;
3252}
3253
Jens Axboee977d6d2019-11-05 12:39:45 -07003254static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003255{
Jens Axboe62755e32019-10-28 21:49:21 -06003256 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003257 int ret = 0;
3258
Jens Axboe62755e32019-10-28 21:49:21 -06003259 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3260 switch (cancel_ret) {
3261 case IO_WQ_CANCEL_OK:
3262 ret = 0;
3263 break;
3264 case IO_WQ_CANCEL_RUNNING:
3265 ret = -EALREADY;
3266 break;
3267 case IO_WQ_CANCEL_NOTFOUND:
3268 ret = -ENOENT;
3269 break;
3270 }
3271
Jens Axboee977d6d2019-11-05 12:39:45 -07003272 return ret;
3273}
3274
Jens Axboe47f46762019-11-09 17:43:02 -07003275static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3276 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003277 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003278{
3279 unsigned long flags;
3280 int ret;
3281
3282 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3283 if (ret != -ENOENT) {
3284 spin_lock_irqsave(&ctx->completion_lock, flags);
3285 goto done;
3286 }
3287
3288 spin_lock_irqsave(&ctx->completion_lock, flags);
3289 ret = io_timeout_cancel(ctx, sqe_addr);
3290 if (ret != -ENOENT)
3291 goto done;
3292 ret = io_poll_cancel(ctx, sqe_addr);
3293done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003294 if (!ret)
3295 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003296 io_cqring_fill_event(req, ret);
3297 io_commit_cqring(ctx);
3298 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3299 io_cqring_ev_posted(ctx);
3300
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003301 if (ret < 0)
3302 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003303 io_put_req_find_next(req, nxt);
3304}
3305
Jens Axboe3529d8c2019-12-19 18:24:38 -07003306static int io_async_cancel_prep(struct io_kiocb *req,
3307 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003308{
Jens Axboefbf23842019-12-17 18:45:56 -07003309 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003310 return -EINVAL;
3311 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3312 sqe->cancel_flags)
3313 return -EINVAL;
3314
Jens Axboefbf23842019-12-17 18:45:56 -07003315 req->cancel.addr = READ_ONCE(sqe->addr);
3316 return 0;
3317}
3318
3319static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3320{
3321 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003322
3323 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003324 return 0;
3325}
3326
Jens Axboe05f3fb32019-12-09 11:22:50 -07003327static int io_files_update_prep(struct io_kiocb *req,
3328 const struct io_uring_sqe *sqe)
3329{
3330 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3331 return -EINVAL;
3332
3333 req->files_update.offset = READ_ONCE(sqe->off);
3334 req->files_update.nr_args = READ_ONCE(sqe->len);
3335 if (!req->files_update.nr_args)
3336 return -EINVAL;
3337 req->files_update.arg = READ_ONCE(sqe->addr);
3338 return 0;
3339}
3340
3341static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3342{
3343 struct io_ring_ctx *ctx = req->ctx;
3344 struct io_uring_files_update up;
3345 int ret;
3346
3347 if (force_nonblock) {
3348 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3349 return -EAGAIN;
3350 }
3351
3352 up.offset = req->files_update.offset;
3353 up.fds = req->files_update.arg;
3354
3355 mutex_lock(&ctx->uring_lock);
3356 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3357 mutex_unlock(&ctx->uring_lock);
3358
3359 if (ret < 0)
3360 req_set_fail_links(req);
3361 io_cqring_add_event(req, ret);
3362 io_put_req(req);
3363 return 0;
3364}
3365
Jens Axboe3529d8c2019-12-19 18:24:38 -07003366static int io_req_defer_prep(struct io_kiocb *req,
3367 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003368{
Jens Axboee7815732019-12-17 19:45:06 -07003369 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003370
Jens Axboed625c6e2019-12-17 19:53:05 -07003371 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003372 case IORING_OP_NOP:
3373 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003374 case IORING_OP_READV:
3375 case IORING_OP_READ_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003376 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003377 break;
3378 case IORING_OP_WRITEV:
3379 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003380 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003381 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003382 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003383 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003384 break;
3385 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003386 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003387 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003388 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003389 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003390 break;
3391 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003392 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003393 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003394 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003395 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003396 break;
3397 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003398 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003399 break;
Jens Axboef499a022019-12-02 16:28:46 -07003400 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003401 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003402 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003403 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003404 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003405 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003406 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003407 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003408 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003409 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003410 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003411 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003412 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003413 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003414 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003415 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003416 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003417 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003418 case IORING_OP_FALLOCATE:
3419 ret = io_fallocate_prep(req, sqe);
3420 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003421 case IORING_OP_OPENAT:
3422 ret = io_openat_prep(req, sqe);
3423 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003424 case IORING_OP_CLOSE:
3425 ret = io_close_prep(req, sqe);
3426 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003427 case IORING_OP_FILES_UPDATE:
3428 ret = io_files_update_prep(req, sqe);
3429 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003430 default:
Jens Axboee7815732019-12-17 19:45:06 -07003431 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3432 req->opcode);
3433 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003434 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003435 }
3436
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003437 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003438}
3439
Jens Axboe3529d8c2019-12-19 18:24:38 -07003440static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003441{
Jackie Liua197f662019-11-08 08:09:12 -07003442 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003443 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003444
Bob Liu9d858b22019-11-13 18:06:25 +08003445 /* Still need defer if there is pending req in defer list. */
3446 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003447 return 0;
3448
Jens Axboe3529d8c2019-12-19 18:24:38 -07003449 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003450 return -EAGAIN;
3451
Jens Axboe3529d8c2019-12-19 18:24:38 -07003452 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003453 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003454 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003455
Jens Axboede0617e2019-04-06 21:51:27 -06003456 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003457 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003458 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003459 return 0;
3460 }
3461
Jens Axboe915967f2019-11-21 09:01:20 -07003462 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003463 list_add_tail(&req->list, &ctx->defer_list);
3464 spin_unlock_irq(&ctx->completion_lock);
3465 return -EIOCBQUEUED;
3466}
3467
Jens Axboe3529d8c2019-12-19 18:24:38 -07003468static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3469 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003470{
Jackie Liua197f662019-11-08 08:09:12 -07003471 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07003472 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003473
Jens Axboed625c6e2019-12-17 19:53:05 -07003474 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003475 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003476 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003477 break;
3478 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003479 case IORING_OP_READ_FIXED:
3480 if (sqe) {
3481 ret = io_read_prep(req, sqe, force_nonblock);
3482 if (ret < 0)
3483 break;
3484 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003485 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003486 break;
3487 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07003488 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003489 if (sqe) {
3490 ret = io_write_prep(req, sqe, force_nonblock);
3491 if (ret < 0)
3492 break;
3493 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003494 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003495 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003496 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003497 if (sqe) {
3498 ret = io_prep_fsync(req, sqe);
3499 if (ret < 0)
3500 break;
3501 }
Jens Axboefc4df992019-12-10 14:38:45 -07003502 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003503 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003504 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003505 if (sqe) {
3506 ret = io_poll_add_prep(req, sqe);
3507 if (ret)
3508 break;
3509 }
Jens Axboefc4df992019-12-10 14:38:45 -07003510 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003511 break;
3512 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003513 if (sqe) {
3514 ret = io_poll_remove_prep(req, sqe);
3515 if (ret < 0)
3516 break;
3517 }
Jens Axboefc4df992019-12-10 14:38:45 -07003518 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003519 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003520 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003521 if (sqe) {
3522 ret = io_prep_sfr(req, sqe);
3523 if (ret < 0)
3524 break;
3525 }
Jens Axboefc4df992019-12-10 14:38:45 -07003526 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003527 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003528 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003529 if (sqe) {
3530 ret = io_sendmsg_prep(req, sqe);
3531 if (ret < 0)
3532 break;
3533 }
Jens Axboefc4df992019-12-10 14:38:45 -07003534 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003535 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003536 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003537 if (sqe) {
3538 ret = io_recvmsg_prep(req, sqe);
3539 if (ret)
3540 break;
3541 }
Jens Axboefc4df992019-12-10 14:38:45 -07003542 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003543 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003544 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003545 if (sqe) {
3546 ret = io_timeout_prep(req, sqe, false);
3547 if (ret)
3548 break;
3549 }
Jens Axboefc4df992019-12-10 14:38:45 -07003550 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003551 break;
Jens Axboe11365042019-10-16 09:08:32 -06003552 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003553 if (sqe) {
3554 ret = io_timeout_remove_prep(req, sqe);
3555 if (ret)
3556 break;
3557 }
Jens Axboefc4df992019-12-10 14:38:45 -07003558 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003559 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003560 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003561 if (sqe) {
3562 ret = io_accept_prep(req, sqe);
3563 if (ret)
3564 break;
3565 }
Jens Axboefc4df992019-12-10 14:38:45 -07003566 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003567 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003568 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003569 if (sqe) {
3570 ret = io_connect_prep(req, sqe);
3571 if (ret)
3572 break;
3573 }
Jens Axboefc4df992019-12-10 14:38:45 -07003574 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003575 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003576 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003577 if (sqe) {
3578 ret = io_async_cancel_prep(req, sqe);
3579 if (ret)
3580 break;
3581 }
Jens Axboefc4df992019-12-10 14:38:45 -07003582 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003583 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003584 case IORING_OP_FALLOCATE:
3585 if (sqe) {
3586 ret = io_fallocate_prep(req, sqe);
3587 if (ret)
3588 break;
3589 }
3590 ret = io_fallocate(req, nxt, force_nonblock);
3591 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003592 case IORING_OP_OPENAT:
3593 if (sqe) {
3594 ret = io_openat_prep(req, sqe);
3595 if (ret)
3596 break;
3597 }
3598 ret = io_openat(req, nxt, force_nonblock);
3599 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003600 case IORING_OP_CLOSE:
3601 if (sqe) {
3602 ret = io_close_prep(req, sqe);
3603 if (ret)
3604 break;
3605 }
3606 ret = io_close(req, nxt, force_nonblock);
3607 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003608 case IORING_OP_FILES_UPDATE:
3609 if (sqe) {
3610 ret = io_files_update_prep(req, sqe);
3611 if (ret)
3612 break;
3613 }
3614 ret = io_files_update(req, force_nonblock);
3615 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003616 default:
3617 ret = -EINVAL;
3618 break;
3619 }
3620
Jens Axboedef596e2019-01-09 08:59:42 -07003621 if (ret)
3622 return ret;
3623
3624 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07003625 const bool in_async = io_wq_current_is_worker();
3626
Jens Axboe9e645e112019-05-10 16:07:28 -06003627 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07003628 return -EAGAIN;
3629
Jens Axboe11ba8202020-01-15 21:51:17 -07003630 /* workqueue context doesn't hold uring_lock, grab it now */
3631 if (in_async)
3632 mutex_lock(&ctx->uring_lock);
3633
Jens Axboedef596e2019-01-09 08:59:42 -07003634 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07003635
3636 if (in_async)
3637 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07003638 }
3639
3640 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003641}
3642
Jens Axboe561fb042019-10-24 07:25:42 -06003643static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003644{
Jens Axboe561fb042019-10-24 07:25:42 -06003645 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003646 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003647 struct io_kiocb *nxt = NULL;
3648 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003649
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003650 /* if NO_CANCEL is set, we must still run the work */
3651 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
3652 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003653 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07003654 }
Jens Axboe31b51512019-01-18 22:56:34 -07003655
Jens Axboe561fb042019-10-24 07:25:42 -06003656 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003657 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3658 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003659 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003660 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003661 /*
3662 * We can get EAGAIN for polled IO even though we're
3663 * forcing a sync submission from here, since we can't
3664 * wait for request slots on the block side.
3665 */
3666 if (ret != -EAGAIN)
3667 break;
3668 cond_resched();
3669 } while (1);
3670 }
Jens Axboe31b51512019-01-18 22:56:34 -07003671
Jens Axboe561fb042019-10-24 07:25:42 -06003672 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003673 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003674
Jens Axboe561fb042019-10-24 07:25:42 -06003675 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003676 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003677 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003678 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003679 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003680
Jens Axboe561fb042019-10-24 07:25:42 -06003681 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07003682 if (!ret && nxt)
3683 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07003684}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003685
Jens Axboe9e3aa612019-12-11 15:55:43 -07003686static bool io_req_op_valid(int op)
3687{
3688 return op >= IORING_OP_NOP && op < IORING_OP_LAST;
3689}
3690
Jens Axboe15b71ab2019-12-11 11:20:36 -07003691static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06003692{
Jens Axboed625c6e2019-12-17 19:53:05 -07003693 switch (req->opcode) {
Jens Axboe09bb8392019-03-13 12:39:28 -06003694 case IORING_OP_NOP:
3695 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03003696 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03003697 case IORING_OP_TIMEOUT_REMOVE:
3698 case IORING_OP_ASYNC_CANCEL:
3699 case IORING_OP_LINK_TIMEOUT:
Jens Axboe9e3aa612019-12-11 15:55:43 -07003700 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003701 case IORING_OP_OPENAT:
3702 return fd != -1;
Jens Axboe09bb8392019-03-13 12:39:28 -06003703 default:
Jens Axboed625c6e2019-12-17 19:53:05 -07003704 if (io_req_op_valid(req->opcode))
Jens Axboe9e3aa612019-12-11 15:55:43 -07003705 return 1;
3706 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003707 }
3708}
3709
Jens Axboe65e19f52019-10-26 07:20:21 -06003710static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3711 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003712{
Jens Axboe65e19f52019-10-26 07:20:21 -06003713 struct fixed_file_table *table;
3714
Jens Axboe05f3fb32019-12-09 11:22:50 -07003715 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
3716 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06003717}
3718
Jens Axboe3529d8c2019-12-19 18:24:38 -07003719static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
3720 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06003721{
Jackie Liua197f662019-11-08 08:09:12 -07003722 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003723 unsigned flags;
Jens Axboe9e3aa612019-12-11 15:55:43 -07003724 int fd, ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003725
Jens Axboe3529d8c2019-12-19 18:24:38 -07003726 flags = READ_ONCE(sqe->flags);
3727 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003728
Jackie Liu4fe2c962019-09-09 20:50:40 +08003729 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003730 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003731
Jens Axboe15b71ab2019-12-11 11:20:36 -07003732 ret = io_req_needs_file(req, fd);
Jens Axboe9e3aa612019-12-11 15:55:43 -07003733 if (ret <= 0)
3734 return ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003735
3736 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07003737 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003738 (unsigned) fd >= ctx->nr_user_files))
3739 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003740 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003741 req->file = io_file_from_index(ctx, fd);
3742 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003743 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003744 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003745 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06003746 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003747 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003748 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003749 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003750 req->file = io_file_get(state, fd);
3751 if (unlikely(!req->file))
3752 return -EBADF;
3753 }
3754
3755 return 0;
3756}
3757
Jackie Liua197f662019-11-08 08:09:12 -07003758static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003759{
Jens Axboefcb323c2019-10-24 12:39:47 -06003760 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003761 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003762
Jens Axboeb5dba592019-12-11 14:02:38 -07003763 if (!req->ring_file)
3764 return -EBADF;
3765
Jens Axboefcb323c2019-10-24 12:39:47 -06003766 rcu_read_lock();
3767 spin_lock_irq(&ctx->inflight_lock);
3768 /*
3769 * We use the f_ops->flush() handler to ensure that we can flush
3770 * out work accessing these files if the fd is closed. Check if
3771 * the fd has changed since we started down this path, and disallow
3772 * this operation if it has.
3773 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003774 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003775 list_add(&req->inflight_entry, &ctx->inflight_list);
3776 req->flags |= REQ_F_INFLIGHT;
3777 req->work.files = current->files;
3778 ret = 0;
3779 }
3780 spin_unlock_irq(&ctx->inflight_lock);
3781 rcu_read_unlock();
3782
3783 return ret;
3784}
3785
Jens Axboe2665abf2019-11-05 12:40:47 -07003786static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3787{
Jens Axboead8a48a2019-11-15 08:49:11 -07003788 struct io_timeout_data *data = container_of(timer,
3789 struct io_timeout_data, timer);
3790 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003791 struct io_ring_ctx *ctx = req->ctx;
3792 struct io_kiocb *prev = NULL;
3793 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003794
3795 spin_lock_irqsave(&ctx->completion_lock, flags);
3796
3797 /*
3798 * We don't expect the list to be empty, that will only happen if we
3799 * race with the completion of the linked work.
3800 */
Pavel Begunkov44932332019-12-05 16:16:35 +03003801 if (!list_empty(&req->link_list)) {
3802 prev = list_entry(req->link_list.prev, struct io_kiocb,
3803 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003804 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03003805 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003806 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3807 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003808 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003809 }
3810
3811 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3812
3813 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003814 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003815 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3816 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003817 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003818 } else {
3819 io_cqring_add_event(req, -ETIME);
3820 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003821 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003822 return HRTIMER_NORESTART;
3823}
3824
Jens Axboead8a48a2019-11-15 08:49:11 -07003825static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003826{
Jens Axboe76a46e02019-11-10 23:34:16 -07003827 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07003828
Jens Axboe76a46e02019-11-10 23:34:16 -07003829 /*
3830 * If the list is now empty, then our linked request finished before
3831 * we got a chance to setup the timer
3832 */
3833 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03003834 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07003835 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003836
Jens Axboead8a48a2019-11-15 08:49:11 -07003837 data->timer.function = io_link_timeout_fn;
3838 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3839 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07003840 }
Jens Axboe76a46e02019-11-10 23:34:16 -07003841 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07003842
Jens Axboe2665abf2019-11-05 12:40:47 -07003843 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07003844 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003845}
3846
Jens Axboead8a48a2019-11-15 08:49:11 -07003847static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003848{
3849 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003850
Jens Axboe2665abf2019-11-05 12:40:47 -07003851 if (!(req->flags & REQ_F_LINK))
3852 return NULL;
3853
Pavel Begunkov44932332019-12-05 16:16:35 +03003854 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
3855 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07003856 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07003857 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003858
Jens Axboe76a46e02019-11-10 23:34:16 -07003859 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07003860 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07003861}
3862
Jens Axboe3529d8c2019-12-19 18:24:38 -07003863static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003864{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003865 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003866 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003867 int ret;
3868
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003869again:
3870 linked_timeout = io_prep_linked_timeout(req);
3871
Jens Axboe3529d8c2019-12-19 18:24:38 -07003872 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06003873
3874 /*
3875 * We async punt it if the file wasn't marked NOWAIT, or if the file
3876 * doesn't support non-blocking read/write attempts
3877 */
3878 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3879 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003880 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3881 ret = io_grab_files(req);
3882 if (ret)
3883 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003884 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003885
3886 /*
3887 * Queued up for async execution, worker will release
3888 * submit reference when the iocb is actually submitted.
3889 */
3890 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003891 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003892 }
Jens Axboee65ef562019-03-12 10:16:44 -06003893
Jens Axboefcb323c2019-10-24 12:39:47 -06003894err:
Jens Axboee65ef562019-03-12 10:16:44 -06003895 /* drop submission reference */
3896 io_put_req(req);
3897
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003898 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003899 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003900 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003901 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003902 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003903 }
3904
Jens Axboee65ef562019-03-12 10:16:44 -06003905 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003906 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003907 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003908 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003909 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003910 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003911done_req:
3912 if (nxt) {
3913 req = nxt;
3914 nxt = NULL;
3915 goto again;
3916 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003917}
3918
Jens Axboe3529d8c2019-12-19 18:24:38 -07003919static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003920{
3921 int ret;
3922
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003923 if (unlikely(req->ctx->drain_next)) {
3924 req->flags |= REQ_F_IO_DRAIN;
3925 req->ctx->drain_next = false;
3926 }
3927 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3928
Jens Axboe3529d8c2019-12-19 18:24:38 -07003929 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003930 if (ret) {
3931 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003932 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003933 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003934 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003935 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003936 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07003937 __io_queue_sqe(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003938}
3939
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003940static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003941{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003942 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003943 io_cqring_add_event(req, -ECANCELED);
3944 io_double_put_req(req);
3945 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07003946 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003947}
3948
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003949#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
3950 IOSQE_IO_HARDLINK)
Jens Axboe9e645e112019-05-10 16:07:28 -06003951
Jens Axboe3529d8c2019-12-19 18:24:38 -07003952static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3953 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003954{
Jackie Liua197f662019-11-08 08:09:12 -07003955 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003956 int ret;
3957
3958 /* enforce forwards compatibility on users */
Jens Axboe3529d8c2019-12-19 18:24:38 -07003959 if (unlikely(sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003960 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003961 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003962 }
3963
Jens Axboe3529d8c2019-12-19 18:24:38 -07003964 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06003965 if (unlikely(ret)) {
3966err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003967 io_cqring_add_event(req, ret);
3968 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003969 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06003970 }
3971
Jens Axboe9e645e112019-05-10 16:07:28 -06003972 /*
3973 * If we already have a head request, queue this one for async
3974 * submittal once the head completes. If we don't have a head but
3975 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3976 * submitted sync once the chain is complete. If none of those
3977 * conditions are true (normal request), then just queue it.
3978 */
3979 if (*link) {
3980 struct io_kiocb *prev = *link;
3981
Jens Axboe3529d8c2019-12-19 18:24:38 -07003982 if (sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003983 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3984
Jens Axboe3529d8c2019-12-19 18:24:38 -07003985 if (sqe->flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003986 req->flags |= REQ_F_HARDLINK;
3987
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003988 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003989 ret = -EAGAIN;
3990 goto err_req;
3991 }
3992
Jens Axboe3529d8c2019-12-19 18:24:38 -07003993 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07003994 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003995 /* fail even hard links since we don't submit */
Jens Axboe2d283902019-12-04 11:08:05 -07003996 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07003997 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07003998 }
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003999 trace_io_uring_link(ctx, req, prev);
Pavel Begunkov44932332019-12-05 16:16:35 +03004000 list_add_tail(&req->link_list, &prev->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004001 } else if (sqe->flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004002 req->flags |= REQ_F_LINK;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004003 if (sqe->flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004004 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004005
Jens Axboe9e645e112019-05-10 16:07:28 -06004006 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004007 ret = io_req_defer_prep(req, sqe);
4008 if (ret)
4009 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004010 *link = req;
4011 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004012 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004013 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004014
4015 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004016}
4017
Jens Axboe9a56a232019-01-09 09:06:50 -07004018/*
4019 * Batched submission is done, ensure local IO is flushed out.
4020 */
4021static void io_submit_state_end(struct io_submit_state *state)
4022{
4023 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004024 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004025 if (state->free_reqs)
4026 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4027 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004028}
4029
4030/*
4031 * Start submission side cache.
4032 */
4033static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004034 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004035{
4036 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004037 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004038 state->file = NULL;
4039 state->ios_left = max_ios;
4040}
4041
Jens Axboe2b188cc2019-01-07 10:46:33 -07004042static void io_commit_sqring(struct io_ring_ctx *ctx)
4043{
Hristo Venev75b28af2019-08-26 17:23:46 +00004044 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004045
Hristo Venev75b28af2019-08-26 17:23:46 +00004046 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004047 /*
4048 * Ensure any loads from the SQEs are done at this point,
4049 * since once we write the new head, the application could
4050 * write new data to them.
4051 */
Hristo Venev75b28af2019-08-26 17:23:46 +00004052 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004053 }
4054}
4055
4056/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004057 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004058 * that is mapped by userspace. This means that care needs to be taken to
4059 * ensure that reads are stable, as we cannot rely on userspace always
4060 * being a good citizen. If members of the sqe are validated and then later
4061 * used, it's important that those reads are done through READ_ONCE() to
4062 * prevent a re-load down the line.
4063 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004064static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4065 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004066{
Hristo Venev75b28af2019-08-26 17:23:46 +00004067 struct io_rings *rings = ctx->rings;
4068 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004069 unsigned head;
4070
4071 /*
4072 * The cached sq head (or cq tail) serves two purposes:
4073 *
4074 * 1) allows us to batch the cost of updating the user visible
4075 * head updates.
4076 * 2) allows the kernel side to track the head on its own, even
4077 * though the application is the one updating it.
4078 */
4079 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02004080 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004081 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004082 return false;
4083
Hristo Venev75b28af2019-08-26 17:23:46 +00004084 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004085 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004086 /*
4087 * All io need record the previous position, if LINK vs DARIN,
4088 * it can be used to mark the position of the first IO in the
4089 * link list.
4090 */
4091 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004092 *sqe_ptr = &ctx->sq_sqes[head];
4093 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4094 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004095 ctx->cached_sq_head++;
4096 return true;
4097 }
4098
4099 /* drop invalid entries */
4100 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004101 ctx->cached_sq_dropped++;
4102 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004103 return false;
4104}
4105
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004106static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004107 struct file *ring_file, int ring_fd,
4108 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004109{
4110 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004111 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004112 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004113 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004114
Jens Axboec4a2ed72019-11-21 21:01:26 -07004115 /* if we have a backlog and couldn't flush it all, return BUSY */
4116 if (!list_empty(&ctx->cq_overflow_list) &&
4117 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004118 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004119
4120 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004121 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004122 statep = &state;
4123 }
4124
4125 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004126 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004127 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03004128 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004129
Pavel Begunkov196be952019-11-07 01:41:06 +03004130 req = io_get_req(ctx, statep);
4131 if (unlikely(!req)) {
4132 if (!submitted)
4133 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004134 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004135 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004136 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03004137 __io_free_req(req);
4138 break;
4139 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004140
Jens Axboed625c6e2019-12-17 19:53:05 -07004141 if (io_req_needs_user(req) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004142 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4143 if (!mm_fault) {
4144 use_mm(ctx->sqo_mm);
4145 *mm = ctx->sqo_mm;
4146 }
4147 }
4148
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004149 submitted++;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004150 sqe_flags = sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03004151
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004152 req->ring_file = ring_file;
4153 req->ring_fd = ring_fd;
4154 req->has_user = *mm != NULL;
4155 req->in_async = async;
4156 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07004157 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004158 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004159 break;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03004160 /*
4161 * If previous wasn't linked and we have a linked command,
4162 * that's the end of the chain. Submit the previous link.
4163 */
Pavel Begunkovffbb8d62019-12-17 20:57:05 +03004164 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004165 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03004166 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004167 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004168 }
4169
Jens Axboe9e645e112019-05-10 16:07:28 -06004170 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004171 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004172 if (statep)
4173 io_submit_state_end(&state);
4174
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004175 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4176 io_commit_sqring(ctx);
4177
Jens Axboe6c271ce2019-01-10 11:22:30 -07004178 return submitted;
4179}
4180
4181static int io_sq_thread(void *data)
4182{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004183 struct io_ring_ctx *ctx = data;
4184 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004185 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004186 mm_segment_t old_fs;
4187 DEFINE_WAIT(wait);
4188 unsigned inflight;
4189 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004190 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004191
Jens Axboe206aefd2019-11-07 18:27:42 -07004192 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004193
Jens Axboe6c271ce2019-01-10 11:22:30 -07004194 old_fs = get_fs();
4195 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004196 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004197
Jens Axboec1edbf52019-11-10 16:56:04 -07004198 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004199 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004200 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004201
4202 if (inflight) {
4203 unsigned nr_events = 0;
4204
4205 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004206 /*
4207 * inflight is the count of the maximum possible
4208 * entries we submitted, but it can be smaller
4209 * if we dropped some of them. If we don't have
4210 * poll entries available, then we know that we
4211 * have nothing left to poll for. Reset the
4212 * inflight count to zero in that case.
4213 */
4214 mutex_lock(&ctx->uring_lock);
4215 if (!list_empty(&ctx->poll_list))
4216 __io_iopoll_check(ctx, &nr_events, 0);
4217 else
4218 inflight = 0;
4219 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004220 } else {
4221 /*
4222 * Normal IO, just pretend everything completed.
4223 * We don't have to poll completions for that.
4224 */
4225 nr_events = inflight;
4226 }
4227
4228 inflight -= nr_events;
4229 if (!inflight)
4230 timeout = jiffies + ctx->sq_thread_idle;
4231 }
4232
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004233 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004234
4235 /*
4236 * If submit got -EBUSY, flag us as needing the application
4237 * to enter the kernel to reap and flush events.
4238 */
4239 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004240 /*
4241 * We're polling. If we're within the defined idle
4242 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004243 * to sleep. The exception is if we got EBUSY doing
4244 * more IO, we should wait for the application to
4245 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004246 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004247 if (inflight ||
4248 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004249 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004250 continue;
4251 }
4252
4253 /*
4254 * Drop cur_mm before scheduling, we can't hold it for
4255 * long periods (or over schedule()). Do this before
4256 * adding ourselves to the waitqueue, as the unuse/drop
4257 * may sleep.
4258 */
4259 if (cur_mm) {
4260 unuse_mm(cur_mm);
4261 mmput(cur_mm);
4262 cur_mm = NULL;
4263 }
4264
4265 prepare_to_wait(&ctx->sqo_wait, &wait,
4266 TASK_INTERRUPTIBLE);
4267
4268 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004269 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004270 /* make sure to read SQ tail after writing flags */
4271 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004272
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004273 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004274 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004275 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004276 finish_wait(&ctx->sqo_wait, &wait);
4277 break;
4278 }
4279 if (signal_pending(current))
4280 flush_signals(current);
4281 schedule();
4282 finish_wait(&ctx->sqo_wait, &wait);
4283
Hristo Venev75b28af2019-08-26 17:23:46 +00004284 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004285 continue;
4286 }
4287 finish_wait(&ctx->sqo_wait, &wait);
4288
Hristo Venev75b28af2019-08-26 17:23:46 +00004289 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004290 }
4291
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004292 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004293 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004294 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004295 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004296 if (ret > 0)
4297 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004298 }
4299
4300 set_fs(old_fs);
4301 if (cur_mm) {
4302 unuse_mm(cur_mm);
4303 mmput(cur_mm);
4304 }
Jens Axboe181e4482019-11-25 08:52:30 -07004305 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004306
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004307 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004308
Jens Axboe6c271ce2019-01-10 11:22:30 -07004309 return 0;
4310}
4311
Jens Axboebda52162019-09-24 13:47:15 -06004312struct io_wait_queue {
4313 struct wait_queue_entry wq;
4314 struct io_ring_ctx *ctx;
4315 unsigned to_wait;
4316 unsigned nr_timeouts;
4317};
4318
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004319static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004320{
4321 struct io_ring_ctx *ctx = iowq->ctx;
4322
4323 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004324 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004325 * started waiting. For timeouts, we always want to return to userspace,
4326 * regardless of event count.
4327 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004328 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004329 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4330}
4331
4332static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4333 int wake_flags, void *key)
4334{
4335 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4336 wq);
4337
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004338 /* use noflush == true, as we can't safely rely on locking context */
4339 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004340 return -1;
4341
4342 return autoremove_wake_function(curr, mode, wake_flags, key);
4343}
4344
Jens Axboe2b188cc2019-01-07 10:46:33 -07004345/*
4346 * Wait until events become available, if we don't already have some. The
4347 * application must reap them itself, as they reside on the shared cq ring.
4348 */
4349static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4350 const sigset_t __user *sig, size_t sigsz)
4351{
Jens Axboebda52162019-09-24 13:47:15 -06004352 struct io_wait_queue iowq = {
4353 .wq = {
4354 .private = current,
4355 .func = io_wake_function,
4356 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4357 },
4358 .ctx = ctx,
4359 .to_wait = min_events,
4360 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004361 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004362 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004363
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004364 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004365 return 0;
4366
4367 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004368#ifdef CONFIG_COMPAT
4369 if (in_compat_syscall())
4370 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004371 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004372 else
4373#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004374 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004375
Jens Axboe2b188cc2019-01-07 10:46:33 -07004376 if (ret)
4377 return ret;
4378 }
4379
Jens Axboebda52162019-09-24 13:47:15 -06004380 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004381 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004382 do {
4383 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4384 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004385 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004386 break;
4387 schedule();
4388 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004389 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004390 break;
4391 }
4392 } while (1);
4393 finish_wait(&ctx->wait, &iowq.wq);
4394
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004395 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004396
Hristo Venev75b28af2019-08-26 17:23:46 +00004397 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004398}
4399
Jens Axboe6b063142019-01-10 22:13:58 -07004400static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4401{
4402#if defined(CONFIG_UNIX)
4403 if (ctx->ring_sock) {
4404 struct sock *sock = ctx->ring_sock->sk;
4405 struct sk_buff *skb;
4406
4407 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4408 kfree_skb(skb);
4409 }
4410#else
4411 int i;
4412
Jens Axboe65e19f52019-10-26 07:20:21 -06004413 for (i = 0; i < ctx->nr_user_files; i++) {
4414 struct file *file;
4415
4416 file = io_file_from_index(ctx, i);
4417 if (file)
4418 fput(file);
4419 }
Jens Axboe6b063142019-01-10 22:13:58 -07004420#endif
4421}
4422
Jens Axboe05f3fb32019-12-09 11:22:50 -07004423static void io_file_ref_kill(struct percpu_ref *ref)
4424{
4425 struct fixed_file_data *data;
4426
4427 data = container_of(ref, struct fixed_file_data, refs);
4428 complete(&data->done);
4429}
4430
Jens Axboe6b063142019-01-10 22:13:58 -07004431static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4432{
Jens Axboe05f3fb32019-12-09 11:22:50 -07004433 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06004434 unsigned nr_tables, i;
4435
Jens Axboe05f3fb32019-12-09 11:22:50 -07004436 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07004437 return -ENXIO;
4438
Jens Axboe05f3fb32019-12-09 11:22:50 -07004439 /* protect against inflight atomic switch, which drops the ref */
4440 flush_work(&data->ref_work);
4441 percpu_ref_get(&data->refs);
4442 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
4443 wait_for_completion(&data->done);
4444 percpu_ref_put(&data->refs);
4445 percpu_ref_exit(&data->refs);
4446
Jens Axboe6b063142019-01-10 22:13:58 -07004447 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004448 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4449 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004450 kfree(data->table[i].files);
4451 kfree(data->table);
4452 kfree(data);
4453 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004454 ctx->nr_user_files = 0;
4455 return 0;
4456}
4457
Jens Axboe6c271ce2019-01-10 11:22:30 -07004458static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4459{
4460 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004461 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004462 /*
4463 * The park is a bit of a work-around, without it we get
4464 * warning spews on shutdown with SQPOLL set and affinity
4465 * set to a single CPU.
4466 */
Jens Axboe06058632019-04-13 09:26:03 -06004467 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004468 kthread_stop(ctx->sqo_thread);
4469 ctx->sqo_thread = NULL;
4470 }
4471}
4472
Jens Axboe6b063142019-01-10 22:13:58 -07004473static void io_finish_async(struct io_ring_ctx *ctx)
4474{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004475 io_sq_thread_stop(ctx);
4476
Jens Axboe561fb042019-10-24 07:25:42 -06004477 if (ctx->io_wq) {
4478 io_wq_destroy(ctx->io_wq);
4479 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004480 }
4481}
4482
4483#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07004484/*
4485 * Ensure the UNIX gc is aware of our file set, so we are certain that
4486 * the io_uring can be safely unregistered on process exit, even if we have
4487 * loops in the file referencing.
4488 */
4489static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4490{
4491 struct sock *sk = ctx->ring_sock->sk;
4492 struct scm_fp_list *fpl;
4493 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004494 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004495
4496 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4497 unsigned long inflight = ctx->user->unix_inflight + nr;
4498
4499 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4500 return -EMFILE;
4501 }
4502
4503 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4504 if (!fpl)
4505 return -ENOMEM;
4506
4507 skb = alloc_skb(0, GFP_KERNEL);
4508 if (!skb) {
4509 kfree(fpl);
4510 return -ENOMEM;
4511 }
4512
4513 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004514
Jens Axboe08a45172019-10-03 08:11:03 -06004515 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004516 fpl->user = get_uid(ctx->user);
4517 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004518 struct file *file = io_file_from_index(ctx, i + offset);
4519
4520 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004521 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004522 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004523 unix_inflight(fpl->user, fpl->fp[nr_files]);
4524 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004525 }
4526
Jens Axboe08a45172019-10-03 08:11:03 -06004527 if (nr_files) {
4528 fpl->max = SCM_MAX_FD;
4529 fpl->count = nr_files;
4530 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004531 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06004532 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4533 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004534
Jens Axboe08a45172019-10-03 08:11:03 -06004535 for (i = 0; i < nr_files; i++)
4536 fput(fpl->fp[i]);
4537 } else {
4538 kfree_skb(skb);
4539 kfree(fpl);
4540 }
Jens Axboe6b063142019-01-10 22:13:58 -07004541
4542 return 0;
4543}
4544
4545/*
4546 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4547 * causes regular reference counting to break down. We rely on the UNIX
4548 * garbage collection to take care of this problem for us.
4549 */
4550static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4551{
4552 unsigned left, total;
4553 int ret = 0;
4554
4555 total = 0;
4556 left = ctx->nr_user_files;
4557 while (left) {
4558 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004559
4560 ret = __io_sqe_files_scm(ctx, this_files, total);
4561 if (ret)
4562 break;
4563 left -= this_files;
4564 total += this_files;
4565 }
4566
4567 if (!ret)
4568 return 0;
4569
4570 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004571 struct file *file = io_file_from_index(ctx, total);
4572
4573 if (file)
4574 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004575 total++;
4576 }
4577
4578 return ret;
4579}
4580#else
4581static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4582{
4583 return 0;
4584}
4585#endif
4586
Jens Axboe65e19f52019-10-26 07:20:21 -06004587static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4588 unsigned nr_files)
4589{
4590 int i;
4591
4592 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004593 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004594 unsigned this_files;
4595
4596 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4597 table->files = kcalloc(this_files, sizeof(struct file *),
4598 GFP_KERNEL);
4599 if (!table->files)
4600 break;
4601 nr_files -= this_files;
4602 }
4603
4604 if (i == nr_tables)
4605 return 0;
4606
4607 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004608 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004609 kfree(table->files);
4610 }
4611 return 1;
4612}
4613
Jens Axboe05f3fb32019-12-09 11:22:50 -07004614static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06004615{
4616#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06004617 struct sock *sock = ctx->ring_sock->sk;
4618 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4619 struct sk_buff *skb;
4620 int i;
4621
4622 __skb_queue_head_init(&list);
4623
4624 /*
4625 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4626 * remove this entry and rearrange the file array.
4627 */
4628 skb = skb_dequeue(head);
4629 while (skb) {
4630 struct scm_fp_list *fp;
4631
4632 fp = UNIXCB(skb).fp;
4633 for (i = 0; i < fp->count; i++) {
4634 int left;
4635
4636 if (fp->fp[i] != file)
4637 continue;
4638
4639 unix_notinflight(fp->user, fp->fp[i]);
4640 left = fp->count - 1 - i;
4641 if (left) {
4642 memmove(&fp->fp[i], &fp->fp[i + 1],
4643 left * sizeof(struct file *));
4644 }
4645 fp->count--;
4646 if (!fp->count) {
4647 kfree_skb(skb);
4648 skb = NULL;
4649 } else {
4650 __skb_queue_tail(&list, skb);
4651 }
4652 fput(file);
4653 file = NULL;
4654 break;
4655 }
4656
4657 if (!file)
4658 break;
4659
4660 __skb_queue_tail(&list, skb);
4661
4662 skb = skb_dequeue(head);
4663 }
4664
4665 if (skb_peek(&list)) {
4666 spin_lock_irq(&head->lock);
4667 while ((skb = __skb_dequeue(&list)) != NULL)
4668 __skb_queue_tail(head, skb);
4669 spin_unlock_irq(&head->lock);
4670 }
4671#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07004672 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06004673#endif
4674}
4675
Jens Axboe05f3fb32019-12-09 11:22:50 -07004676struct io_file_put {
4677 struct llist_node llist;
4678 struct file *file;
4679 struct completion *done;
4680};
4681
4682static void io_ring_file_ref_switch(struct work_struct *work)
4683{
4684 struct io_file_put *pfile, *tmp;
4685 struct fixed_file_data *data;
4686 struct llist_node *node;
4687
4688 data = container_of(work, struct fixed_file_data, ref_work);
4689
4690 while ((node = llist_del_all(&data->put_llist)) != NULL) {
4691 llist_for_each_entry_safe(pfile, tmp, node, llist) {
4692 io_ring_file_put(data->ctx, pfile->file);
4693 if (pfile->done)
4694 complete(pfile->done);
4695 else
4696 kfree(pfile);
4697 }
4698 }
4699
4700 percpu_ref_get(&data->refs);
4701 percpu_ref_switch_to_percpu(&data->refs);
4702}
4703
4704static void io_file_data_ref_zero(struct percpu_ref *ref)
4705{
4706 struct fixed_file_data *data;
4707
4708 data = container_of(ref, struct fixed_file_data, refs);
4709
4710 /* we can't safely switch from inside this context, punt to wq */
4711 queue_work(system_wq, &data->ref_work);
4712}
4713
4714static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
4715 unsigned nr_args)
4716{
4717 __s32 __user *fds = (__s32 __user *) arg;
4718 unsigned nr_tables;
4719 struct file *file;
4720 int fd, ret = 0;
4721 unsigned i;
4722
4723 if (ctx->file_data)
4724 return -EBUSY;
4725 if (!nr_args)
4726 return -EINVAL;
4727 if (nr_args > IORING_MAX_FIXED_FILES)
4728 return -EMFILE;
4729
4730 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
4731 if (!ctx->file_data)
4732 return -ENOMEM;
4733 ctx->file_data->ctx = ctx;
4734 init_completion(&ctx->file_data->done);
4735
4736 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
4737 ctx->file_data->table = kcalloc(nr_tables,
4738 sizeof(struct fixed_file_table),
4739 GFP_KERNEL);
4740 if (!ctx->file_data->table) {
4741 kfree(ctx->file_data);
4742 ctx->file_data = NULL;
4743 return -ENOMEM;
4744 }
4745
4746 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
4747 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
4748 kfree(ctx->file_data->table);
4749 kfree(ctx->file_data);
4750 ctx->file_data = NULL;
4751 return -ENOMEM;
4752 }
4753 ctx->file_data->put_llist.first = NULL;
4754 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
4755
4756 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
4757 percpu_ref_exit(&ctx->file_data->refs);
4758 kfree(ctx->file_data->table);
4759 kfree(ctx->file_data);
4760 ctx->file_data = NULL;
4761 return -ENOMEM;
4762 }
4763
4764 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
4765 struct fixed_file_table *table;
4766 unsigned index;
4767
4768 ret = -EFAULT;
4769 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
4770 break;
4771 /* allow sparse sets */
4772 if (fd == -1) {
4773 ret = 0;
4774 continue;
4775 }
4776
4777 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
4778 index = i & IORING_FILE_TABLE_MASK;
4779 file = fget(fd);
4780
4781 ret = -EBADF;
4782 if (!file)
4783 break;
4784
4785 /*
4786 * Don't allow io_uring instances to be registered. If UNIX
4787 * isn't enabled, then this causes a reference cycle and this
4788 * instance can never get freed. If UNIX is enabled we'll
4789 * handle it just fine, but there's still no point in allowing
4790 * a ring fd as it doesn't support regular read/write anyway.
4791 */
4792 if (file->f_op == &io_uring_fops) {
4793 fput(file);
4794 break;
4795 }
4796 ret = 0;
4797 table->files[index] = file;
4798 }
4799
4800 if (ret) {
4801 for (i = 0; i < ctx->nr_user_files; i++) {
4802 file = io_file_from_index(ctx, i);
4803 if (file)
4804 fput(file);
4805 }
4806 for (i = 0; i < nr_tables; i++)
4807 kfree(ctx->file_data->table[i].files);
4808
4809 kfree(ctx->file_data->table);
4810 kfree(ctx->file_data);
4811 ctx->file_data = NULL;
4812 ctx->nr_user_files = 0;
4813 return ret;
4814 }
4815
4816 ret = io_sqe_files_scm(ctx);
4817 if (ret)
4818 io_sqe_files_unregister(ctx);
4819
4820 return ret;
4821}
4822
Jens Axboec3a31e62019-10-03 13:59:56 -06004823static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4824 int index)
4825{
4826#if defined(CONFIG_UNIX)
4827 struct sock *sock = ctx->ring_sock->sk;
4828 struct sk_buff_head *head = &sock->sk_receive_queue;
4829 struct sk_buff *skb;
4830
4831 /*
4832 * See if we can merge this file into an existing skb SCM_RIGHTS
4833 * file set. If there's no room, fall back to allocating a new skb
4834 * and filling it in.
4835 */
4836 spin_lock_irq(&head->lock);
4837 skb = skb_peek(head);
4838 if (skb) {
4839 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4840
4841 if (fpl->count < SCM_MAX_FD) {
4842 __skb_unlink(skb, head);
4843 spin_unlock_irq(&head->lock);
4844 fpl->fp[fpl->count] = get_file(file);
4845 unix_inflight(fpl->user, fpl->fp[fpl->count]);
4846 fpl->count++;
4847 spin_lock_irq(&head->lock);
4848 __skb_queue_head(head, skb);
4849 } else {
4850 skb = NULL;
4851 }
4852 }
4853 spin_unlock_irq(&head->lock);
4854
4855 if (skb) {
4856 fput(file);
4857 return 0;
4858 }
4859
4860 return __io_sqe_files_scm(ctx, 1, index);
4861#else
4862 return 0;
4863#endif
4864}
4865
Jens Axboe05f3fb32019-12-09 11:22:50 -07004866static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06004867{
Jens Axboe05f3fb32019-12-09 11:22:50 -07004868 struct fixed_file_data *data;
4869
4870 data = container_of(ref, struct fixed_file_data, refs);
4871 clear_bit(FFD_F_ATOMIC, &data->state);
4872}
4873
4874static bool io_queue_file_removal(struct fixed_file_data *data,
4875 struct file *file)
4876{
4877 struct io_file_put *pfile, pfile_stack;
4878 DECLARE_COMPLETION_ONSTACK(done);
4879
4880 /*
4881 * If we fail allocating the struct we need for doing async reomval
4882 * of this file, just punt to sync and wait for it.
4883 */
4884 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
4885 if (!pfile) {
4886 pfile = &pfile_stack;
4887 pfile->done = &done;
4888 }
4889
4890 pfile->file = file;
4891 llist_add(&pfile->llist, &data->put_llist);
4892
4893 if (pfile == &pfile_stack) {
4894 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
4895 percpu_ref_put(&data->refs);
4896 percpu_ref_switch_to_atomic(&data->refs,
4897 io_atomic_switch);
4898 }
4899 wait_for_completion(&done);
4900 flush_work(&data->ref_work);
4901 return false;
4902 }
4903
4904 return true;
4905}
4906
4907static int __io_sqe_files_update(struct io_ring_ctx *ctx,
4908 struct io_uring_files_update *up,
4909 unsigned nr_args)
4910{
4911 struct fixed_file_data *data = ctx->file_data;
4912 bool ref_switch = false;
4913 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06004914 __s32 __user *fds;
4915 int fd, i, err;
4916 __u32 done;
4917
Jens Axboe05f3fb32019-12-09 11:22:50 -07004918 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06004919 return -EOVERFLOW;
4920 if (done > ctx->nr_user_files)
4921 return -EINVAL;
4922
4923 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004924 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06004925 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004926 struct fixed_file_table *table;
4927 unsigned index;
4928
Jens Axboec3a31e62019-10-03 13:59:56 -06004929 err = 0;
4930 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
4931 err = -EFAULT;
4932 break;
4933 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07004934 i = array_index_nospec(up->offset, ctx->nr_user_files);
4935 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06004936 index = i & IORING_FILE_TABLE_MASK;
4937 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004938 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06004939 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004940 if (io_queue_file_removal(data, file))
4941 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06004942 }
4943 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06004944 file = fget(fd);
4945 if (!file) {
4946 err = -EBADF;
4947 break;
4948 }
4949 /*
4950 * Don't allow io_uring instances to be registered. If
4951 * UNIX isn't enabled, then this causes a reference
4952 * cycle and this instance can never get freed. If UNIX
4953 * is enabled we'll handle it just fine, but there's
4954 * still no point in allowing a ring fd as it doesn't
4955 * support regular read/write anyway.
4956 */
4957 if (file->f_op == &io_uring_fops) {
4958 fput(file);
4959 err = -EBADF;
4960 break;
4961 }
Jens Axboe65e19f52019-10-26 07:20:21 -06004962 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06004963 err = io_sqe_file_register(ctx, file, i);
4964 if (err)
4965 break;
4966 }
4967 nr_args--;
4968 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004969 up->offset++;
4970 }
4971
4972 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
4973 percpu_ref_put(&data->refs);
4974 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06004975 }
4976
4977 return done ? done : err;
4978}
Jens Axboe05f3fb32019-12-09 11:22:50 -07004979static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
4980 unsigned nr_args)
4981{
4982 struct io_uring_files_update up;
4983
4984 if (!ctx->file_data)
4985 return -ENXIO;
4986 if (!nr_args)
4987 return -EINVAL;
4988 if (copy_from_user(&up, arg, sizeof(up)))
4989 return -EFAULT;
4990 if (up.resv)
4991 return -EINVAL;
4992
4993 return __io_sqe_files_update(ctx, &up, nr_args);
4994}
Jens Axboec3a31e62019-10-03 13:59:56 -06004995
Jens Axboe7d723062019-11-12 22:31:31 -07004996static void io_put_work(struct io_wq_work *work)
4997{
4998 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4999
5000 io_put_req(req);
5001}
5002
5003static void io_get_work(struct io_wq_work *work)
5004{
5005 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5006
5007 refcount_inc(&req->refs);
5008}
5009
Jens Axboe6c271ce2019-01-10 11:22:30 -07005010static int io_sq_offload_start(struct io_ring_ctx *ctx,
5011 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005012{
Jens Axboe576a3472019-11-25 08:49:20 -07005013 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005014 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005015 int ret;
5016
Jens Axboe6c271ce2019-01-10 11:22:30 -07005017 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005018 mmgrab(current->mm);
5019 ctx->sqo_mm = current->mm;
5020
Jens Axboe6c271ce2019-01-10 11:22:30 -07005021 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005022 ret = -EPERM;
5023 if (!capable(CAP_SYS_ADMIN))
5024 goto err;
5025
Jens Axboe917257d2019-04-13 09:28:55 -06005026 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5027 if (!ctx->sq_thread_idle)
5028 ctx->sq_thread_idle = HZ;
5029
Jens Axboe6c271ce2019-01-10 11:22:30 -07005030 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005031 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005032
Jens Axboe917257d2019-04-13 09:28:55 -06005033 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005034 if (cpu >= nr_cpu_ids)
5035 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005036 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005037 goto err;
5038
Jens Axboe6c271ce2019-01-10 11:22:30 -07005039 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5040 ctx, cpu,
5041 "io_uring-sq");
5042 } else {
5043 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5044 "io_uring-sq");
5045 }
5046 if (IS_ERR(ctx->sqo_thread)) {
5047 ret = PTR_ERR(ctx->sqo_thread);
5048 ctx->sqo_thread = NULL;
5049 goto err;
5050 }
5051 wake_up_process(ctx->sqo_thread);
5052 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5053 /* Can't have SQ_AFF without SQPOLL */
5054 ret = -EINVAL;
5055 goto err;
5056 }
5057
Jens Axboe576a3472019-11-25 08:49:20 -07005058 data.mm = ctx->sqo_mm;
5059 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005060 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005061 data.get_work = io_get_work;
5062 data.put_work = io_put_work;
5063
Jens Axboe561fb042019-10-24 07:25:42 -06005064 /* Do QD, or 4 * CPUS, whatever is smallest */
5065 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005066 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005067 if (IS_ERR(ctx->io_wq)) {
5068 ret = PTR_ERR(ctx->io_wq);
5069 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005070 goto err;
5071 }
5072
5073 return 0;
5074err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005075 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005076 mmdrop(ctx->sqo_mm);
5077 ctx->sqo_mm = NULL;
5078 return ret;
5079}
5080
5081static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5082{
5083 atomic_long_sub(nr_pages, &user->locked_vm);
5084}
5085
5086static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5087{
5088 unsigned long page_limit, cur_pages, new_pages;
5089
5090 /* Don't allow more pages than we can safely lock */
5091 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5092
5093 do {
5094 cur_pages = atomic_long_read(&user->locked_vm);
5095 new_pages = cur_pages + nr_pages;
5096 if (new_pages > page_limit)
5097 return -ENOMEM;
5098 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5099 new_pages) != cur_pages);
5100
5101 return 0;
5102}
5103
5104static void io_mem_free(void *ptr)
5105{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005106 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005107
Mark Rutland52e04ef2019-04-30 17:30:21 +01005108 if (!ptr)
5109 return;
5110
5111 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005112 if (put_page_testzero(page))
5113 free_compound_page(page);
5114}
5115
5116static void *io_mem_alloc(size_t size)
5117{
5118 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5119 __GFP_NORETRY;
5120
5121 return (void *) __get_free_pages(gfp_flags, get_order(size));
5122}
5123
Hristo Venev75b28af2019-08-26 17:23:46 +00005124static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5125 size_t *sq_offset)
5126{
5127 struct io_rings *rings;
5128 size_t off, sq_array_size;
5129
5130 off = struct_size(rings, cqes, cq_entries);
5131 if (off == SIZE_MAX)
5132 return SIZE_MAX;
5133
5134#ifdef CONFIG_SMP
5135 off = ALIGN(off, SMP_CACHE_BYTES);
5136 if (off == 0)
5137 return SIZE_MAX;
5138#endif
5139
5140 sq_array_size = array_size(sizeof(u32), sq_entries);
5141 if (sq_array_size == SIZE_MAX)
5142 return SIZE_MAX;
5143
5144 if (check_add_overflow(off, sq_array_size, &off))
5145 return SIZE_MAX;
5146
5147 if (sq_offset)
5148 *sq_offset = off;
5149
5150 return off;
5151}
5152
Jens Axboe2b188cc2019-01-07 10:46:33 -07005153static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5154{
Hristo Venev75b28af2019-08-26 17:23:46 +00005155 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005156
Hristo Venev75b28af2019-08-26 17:23:46 +00005157 pages = (size_t)1 << get_order(
5158 rings_size(sq_entries, cq_entries, NULL));
5159 pages += (size_t)1 << get_order(
5160 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005161
Hristo Venev75b28af2019-08-26 17:23:46 +00005162 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005163}
5164
Jens Axboeedafcce2019-01-09 09:16:05 -07005165static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5166{
5167 int i, j;
5168
5169 if (!ctx->user_bufs)
5170 return -ENXIO;
5171
5172 for (i = 0; i < ctx->nr_user_bufs; i++) {
5173 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5174
5175 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005176 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005177
5178 if (ctx->account_mem)
5179 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005180 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005181 imu->nr_bvecs = 0;
5182 }
5183
5184 kfree(ctx->user_bufs);
5185 ctx->user_bufs = NULL;
5186 ctx->nr_user_bufs = 0;
5187 return 0;
5188}
5189
5190static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5191 void __user *arg, unsigned index)
5192{
5193 struct iovec __user *src;
5194
5195#ifdef CONFIG_COMPAT
5196 if (ctx->compat) {
5197 struct compat_iovec __user *ciovs;
5198 struct compat_iovec ciov;
5199
5200 ciovs = (struct compat_iovec __user *) arg;
5201 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5202 return -EFAULT;
5203
Jens Axboed55e5f52019-12-11 16:12:15 -07005204 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005205 dst->iov_len = ciov.iov_len;
5206 return 0;
5207 }
5208#endif
5209 src = (struct iovec __user *) arg;
5210 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5211 return -EFAULT;
5212 return 0;
5213}
5214
5215static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5216 unsigned nr_args)
5217{
5218 struct vm_area_struct **vmas = NULL;
5219 struct page **pages = NULL;
5220 int i, j, got_pages = 0;
5221 int ret = -EINVAL;
5222
5223 if (ctx->user_bufs)
5224 return -EBUSY;
5225 if (!nr_args || nr_args > UIO_MAXIOV)
5226 return -EINVAL;
5227
5228 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5229 GFP_KERNEL);
5230 if (!ctx->user_bufs)
5231 return -ENOMEM;
5232
5233 for (i = 0; i < nr_args; i++) {
5234 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5235 unsigned long off, start, end, ubuf;
5236 int pret, nr_pages;
5237 struct iovec iov;
5238 size_t size;
5239
5240 ret = io_copy_iov(ctx, &iov, arg, i);
5241 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005242 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005243
5244 /*
5245 * Don't impose further limits on the size and buffer
5246 * constraints here, we'll -EINVAL later when IO is
5247 * submitted if they are wrong.
5248 */
5249 ret = -EFAULT;
5250 if (!iov.iov_base || !iov.iov_len)
5251 goto err;
5252
5253 /* arbitrary limit, but we need something */
5254 if (iov.iov_len > SZ_1G)
5255 goto err;
5256
5257 ubuf = (unsigned long) iov.iov_base;
5258 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5259 start = ubuf >> PAGE_SHIFT;
5260 nr_pages = end - start;
5261
5262 if (ctx->account_mem) {
5263 ret = io_account_mem(ctx->user, nr_pages);
5264 if (ret)
5265 goto err;
5266 }
5267
5268 ret = 0;
5269 if (!pages || nr_pages > got_pages) {
5270 kfree(vmas);
5271 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005272 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005273 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005274 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005275 sizeof(struct vm_area_struct *),
5276 GFP_KERNEL);
5277 if (!pages || !vmas) {
5278 ret = -ENOMEM;
5279 if (ctx->account_mem)
5280 io_unaccount_mem(ctx->user, nr_pages);
5281 goto err;
5282 }
5283 got_pages = nr_pages;
5284 }
5285
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005286 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005287 GFP_KERNEL);
5288 ret = -ENOMEM;
5289 if (!imu->bvec) {
5290 if (ctx->account_mem)
5291 io_unaccount_mem(ctx->user, nr_pages);
5292 goto err;
5293 }
5294
5295 ret = 0;
5296 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005297 pret = get_user_pages(ubuf, nr_pages,
5298 FOLL_WRITE | FOLL_LONGTERM,
5299 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005300 if (pret == nr_pages) {
5301 /* don't support file backed memory */
5302 for (j = 0; j < nr_pages; j++) {
5303 struct vm_area_struct *vma = vmas[j];
5304
5305 if (vma->vm_file &&
5306 !is_file_hugepages(vma->vm_file)) {
5307 ret = -EOPNOTSUPP;
5308 break;
5309 }
5310 }
5311 } else {
5312 ret = pret < 0 ? pret : -EFAULT;
5313 }
5314 up_read(&current->mm->mmap_sem);
5315 if (ret) {
5316 /*
5317 * if we did partial map, or found file backed vmas,
5318 * release any pages we did get
5319 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005320 if (pret > 0)
5321 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005322 if (ctx->account_mem)
5323 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005324 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005325 goto err;
5326 }
5327
5328 off = ubuf & ~PAGE_MASK;
5329 size = iov.iov_len;
5330 for (j = 0; j < nr_pages; j++) {
5331 size_t vec_len;
5332
5333 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5334 imu->bvec[j].bv_page = pages[j];
5335 imu->bvec[j].bv_len = vec_len;
5336 imu->bvec[j].bv_offset = off;
5337 off = 0;
5338 size -= vec_len;
5339 }
5340 /* store original address for later verification */
5341 imu->ubuf = ubuf;
5342 imu->len = iov.iov_len;
5343 imu->nr_bvecs = nr_pages;
5344
5345 ctx->nr_user_bufs++;
5346 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005347 kvfree(pages);
5348 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005349 return 0;
5350err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005351 kvfree(pages);
5352 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005353 io_sqe_buffer_unregister(ctx);
5354 return ret;
5355}
5356
Jens Axboe9b402842019-04-11 11:45:41 -06005357static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5358{
5359 __s32 __user *fds = arg;
5360 int fd;
5361
5362 if (ctx->cq_ev_fd)
5363 return -EBUSY;
5364
5365 if (copy_from_user(&fd, fds, sizeof(*fds)))
5366 return -EFAULT;
5367
5368 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5369 if (IS_ERR(ctx->cq_ev_fd)) {
5370 int ret = PTR_ERR(ctx->cq_ev_fd);
5371 ctx->cq_ev_fd = NULL;
5372 return ret;
5373 }
5374
5375 return 0;
5376}
5377
5378static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5379{
5380 if (ctx->cq_ev_fd) {
5381 eventfd_ctx_put(ctx->cq_ev_fd);
5382 ctx->cq_ev_fd = NULL;
5383 return 0;
5384 }
5385
5386 return -ENXIO;
5387}
5388
Jens Axboe2b188cc2019-01-07 10:46:33 -07005389static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5390{
Jens Axboe6b063142019-01-10 22:13:58 -07005391 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005392 if (ctx->sqo_mm)
5393 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005394
5395 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005396 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005397 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005398 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005399
Jens Axboe2b188cc2019-01-07 10:46:33 -07005400#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005401 if (ctx->ring_sock) {
5402 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005403 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005404 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005405#endif
5406
Hristo Venev75b28af2019-08-26 17:23:46 +00005407 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005408 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005409
5410 percpu_ref_exit(&ctx->refs);
5411 if (ctx->account_mem)
5412 io_unaccount_mem(ctx->user,
5413 ring_pages(ctx->sq_entries, ctx->cq_entries));
5414 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07005415 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07005416 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07005417 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07005418 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005419 kfree(ctx);
5420}
5421
5422static __poll_t io_uring_poll(struct file *file, poll_table *wait)
5423{
5424 struct io_ring_ctx *ctx = file->private_data;
5425 __poll_t mask = 0;
5426
5427 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02005428 /*
5429 * synchronizes with barrier from wq_has_sleeper call in
5430 * io_commit_cqring
5431 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005432 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00005433 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
5434 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005435 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08005436 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005437 mask |= EPOLLIN | EPOLLRDNORM;
5438
5439 return mask;
5440}
5441
5442static int io_uring_fasync(int fd, struct file *file, int on)
5443{
5444 struct io_ring_ctx *ctx = file->private_data;
5445
5446 return fasync_helper(fd, file, on, &ctx->cq_fasync);
5447}
5448
5449static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
5450{
5451 mutex_lock(&ctx->uring_lock);
5452 percpu_ref_kill(&ctx->refs);
5453 mutex_unlock(&ctx->uring_lock);
5454
Jens Axboe5262f562019-09-17 12:26:57 -06005455 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005456 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06005457
5458 if (ctx->io_wq)
5459 io_wq_cancel_all(ctx->io_wq);
5460
Jens Axboedef596e2019-01-09 08:59:42 -07005461 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07005462 /* if we failed setting up the ctx, we might not have any rings */
5463 if (ctx->rings)
5464 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07005465 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005466 io_ring_ctx_free(ctx);
5467}
5468
5469static int io_uring_release(struct inode *inode, struct file *file)
5470{
5471 struct io_ring_ctx *ctx = file->private_data;
5472
5473 file->private_data = NULL;
5474 io_ring_ctx_wait_and_kill(ctx);
5475 return 0;
5476}
5477
Jens Axboefcb323c2019-10-24 12:39:47 -06005478static void io_uring_cancel_files(struct io_ring_ctx *ctx,
5479 struct files_struct *files)
5480{
5481 struct io_kiocb *req;
5482 DEFINE_WAIT(wait);
5483
5484 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07005485 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06005486
5487 spin_lock_irq(&ctx->inflight_lock);
5488 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07005489 if (req->work.files != files)
5490 continue;
5491 /* req is being completed, ignore */
5492 if (!refcount_inc_not_zero(&req->refs))
5493 continue;
5494 cancel_req = req;
5495 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06005496 }
Jens Axboe768134d2019-11-10 20:30:53 -07005497 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005498 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07005499 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06005500 spin_unlock_irq(&ctx->inflight_lock);
5501
Jens Axboe768134d2019-11-10 20:30:53 -07005502 /* We need to keep going until we don't find a matching req */
5503 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005504 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005505
5506 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5507 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005508 schedule();
5509 }
Jens Axboe768134d2019-11-10 20:30:53 -07005510 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005511}
5512
5513static int io_uring_flush(struct file *file, void *data)
5514{
5515 struct io_ring_ctx *ctx = file->private_data;
5516
5517 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005518 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5519 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005520 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005521 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005522 return 0;
5523}
5524
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005525static void *io_uring_validate_mmap_request(struct file *file,
5526 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005527{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005528 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005529 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005530 struct page *page;
5531 void *ptr;
5532
5533 switch (offset) {
5534 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005535 case IORING_OFF_CQ_RING:
5536 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005537 break;
5538 case IORING_OFF_SQES:
5539 ptr = ctx->sq_sqes;
5540 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005541 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005542 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005543 }
5544
5545 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005546 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005547 return ERR_PTR(-EINVAL);
5548
5549 return ptr;
5550}
5551
5552#ifdef CONFIG_MMU
5553
5554static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5555{
5556 size_t sz = vma->vm_end - vma->vm_start;
5557 unsigned long pfn;
5558 void *ptr;
5559
5560 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5561 if (IS_ERR(ptr))
5562 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005563
5564 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5565 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5566}
5567
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005568#else /* !CONFIG_MMU */
5569
5570static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5571{
5572 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5573}
5574
5575static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5576{
5577 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5578}
5579
5580static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5581 unsigned long addr, unsigned long len,
5582 unsigned long pgoff, unsigned long flags)
5583{
5584 void *ptr;
5585
5586 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5587 if (IS_ERR(ptr))
5588 return PTR_ERR(ptr);
5589
5590 return (unsigned long) ptr;
5591}
5592
5593#endif /* !CONFIG_MMU */
5594
Jens Axboe2b188cc2019-01-07 10:46:33 -07005595SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5596 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5597 size_t, sigsz)
5598{
5599 struct io_ring_ctx *ctx;
5600 long ret = -EBADF;
5601 int submitted = 0;
5602 struct fd f;
5603
Jens Axboe6c271ce2019-01-10 11:22:30 -07005604 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005605 return -EINVAL;
5606
5607 f = fdget(fd);
5608 if (!f.file)
5609 return -EBADF;
5610
5611 ret = -EOPNOTSUPP;
5612 if (f.file->f_op != &io_uring_fops)
5613 goto out_fput;
5614
5615 ret = -ENXIO;
5616 ctx = f.file->private_data;
5617 if (!percpu_ref_tryget(&ctx->refs))
5618 goto out_fput;
5619
Jens Axboe6c271ce2019-01-10 11:22:30 -07005620 /*
5621 * For SQ polling, the thread will do all submissions and completions.
5622 * Just return the requested submit count, and wake the thread if
5623 * we were asked to.
5624 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005625 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005626 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005627 if (!list_empty_careful(&ctx->cq_overflow_list))
5628 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005629 if (flags & IORING_ENTER_SQ_WAKEUP)
5630 wake_up(&ctx->sqo_wait);
5631 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005632 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005633 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005634
Jens Axboe44d28272020-01-16 19:00:24 -07005635 if (current->mm != ctx->sqo_mm ||
5636 current_cred() != ctx->creds) {
5637 ret = -EPERM;
5638 goto out;
5639 }
5640
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005641 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005642 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005643 /* already have mm, so io_submit_sqes() won't try to grab it */
5644 cur_mm = ctx->sqo_mm;
5645 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
5646 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005647 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005648
5649 if (submitted != to_submit)
5650 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005651 }
5652 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07005653 unsigned nr_events = 0;
5654
Jens Axboe2b188cc2019-01-07 10:46:33 -07005655 min_complete = min(min_complete, ctx->cq_entries);
5656
Jens Axboedef596e2019-01-09 08:59:42 -07005657 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07005658 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07005659 } else {
5660 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
5661 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005662 }
5663
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005664out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03005665 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005666out_fput:
5667 fdput(f);
5668 return submitted ? submitted : ret;
5669}
5670
5671static const struct file_operations io_uring_fops = {
5672 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06005673 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07005674 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005675#ifndef CONFIG_MMU
5676 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
5677 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
5678#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07005679 .poll = io_uring_poll,
5680 .fasync = io_uring_fasync,
5681};
5682
5683static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
5684 struct io_uring_params *p)
5685{
Hristo Venev75b28af2019-08-26 17:23:46 +00005686 struct io_rings *rings;
5687 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005688
Hristo Venev75b28af2019-08-26 17:23:46 +00005689 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
5690 if (size == SIZE_MAX)
5691 return -EOVERFLOW;
5692
5693 rings = io_mem_alloc(size);
5694 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005695 return -ENOMEM;
5696
Hristo Venev75b28af2019-08-26 17:23:46 +00005697 ctx->rings = rings;
5698 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
5699 rings->sq_ring_mask = p->sq_entries - 1;
5700 rings->cq_ring_mask = p->cq_entries - 1;
5701 rings->sq_ring_entries = p->sq_entries;
5702 rings->cq_ring_entries = p->cq_entries;
5703 ctx->sq_mask = rings->sq_ring_mask;
5704 ctx->cq_mask = rings->cq_ring_mask;
5705 ctx->sq_entries = rings->sq_ring_entries;
5706 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005707
5708 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07005709 if (size == SIZE_MAX) {
5710 io_mem_free(ctx->rings);
5711 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005712 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07005713 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005714
5715 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07005716 if (!ctx->sq_sqes) {
5717 io_mem_free(ctx->rings);
5718 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005719 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07005720 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005721
Jens Axboe2b188cc2019-01-07 10:46:33 -07005722 return 0;
5723}
5724
5725/*
5726 * Allocate an anonymous fd, this is what constitutes the application
5727 * visible backing of an io_uring instance. The application mmaps this
5728 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
5729 * we have to tie this fd to a socket for file garbage collection purposes.
5730 */
5731static int io_uring_get_fd(struct io_ring_ctx *ctx)
5732{
5733 struct file *file;
5734 int ret;
5735
5736#if defined(CONFIG_UNIX)
5737 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
5738 &ctx->ring_sock);
5739 if (ret)
5740 return ret;
5741#endif
5742
5743 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
5744 if (ret < 0)
5745 goto err;
5746
5747 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
5748 O_RDWR | O_CLOEXEC);
5749 if (IS_ERR(file)) {
5750 put_unused_fd(ret);
5751 ret = PTR_ERR(file);
5752 goto err;
5753 }
5754
5755#if defined(CONFIG_UNIX)
5756 ctx->ring_sock->file = file;
5757#endif
5758 fd_install(ret, file);
5759 return ret;
5760err:
5761#if defined(CONFIG_UNIX)
5762 sock_release(ctx->ring_sock);
5763 ctx->ring_sock = NULL;
5764#endif
5765 return ret;
5766}
5767
5768static int io_uring_create(unsigned entries, struct io_uring_params *p)
5769{
5770 struct user_struct *user = NULL;
5771 struct io_ring_ctx *ctx;
5772 bool account_mem;
5773 int ret;
5774
5775 if (!entries || entries > IORING_MAX_ENTRIES)
5776 return -EINVAL;
5777
5778 /*
5779 * Use twice as many entries for the CQ ring. It's possible for the
5780 * application to drive a higher depth than the size of the SQ ring,
5781 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005782 * some flexibility in overcommitting a bit. If the application has
5783 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5784 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005785 */
5786 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005787 if (p->flags & IORING_SETUP_CQSIZE) {
5788 /*
5789 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5790 * to a power-of-two, if it isn't already. We do NOT impose
5791 * any cq vs sq ring sizing.
5792 */
5793 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5794 return -EINVAL;
5795 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5796 } else {
5797 p->cq_entries = 2 * p->sq_entries;
5798 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005799
5800 user = get_uid(current_user());
5801 account_mem = !capable(CAP_IPC_LOCK);
5802
5803 if (account_mem) {
5804 ret = io_account_mem(user,
5805 ring_pages(p->sq_entries, p->cq_entries));
5806 if (ret) {
5807 free_uid(user);
5808 return ret;
5809 }
5810 }
5811
5812 ctx = io_ring_ctx_alloc(p);
5813 if (!ctx) {
5814 if (account_mem)
5815 io_unaccount_mem(user, ring_pages(p->sq_entries,
5816 p->cq_entries));
5817 free_uid(user);
5818 return -ENOMEM;
5819 }
5820 ctx->compat = in_compat_syscall();
5821 ctx->account_mem = account_mem;
5822 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07005823 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07005824
5825 ret = io_allocate_scq_urings(ctx, p);
5826 if (ret)
5827 goto err;
5828
Jens Axboe6c271ce2019-01-10 11:22:30 -07005829 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005830 if (ret)
5831 goto err;
5832
Jens Axboe2b188cc2019-01-07 10:46:33 -07005833 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005834 p->sq_off.head = offsetof(struct io_rings, sq.head);
5835 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5836 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5837 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5838 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5839 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5840 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005841
5842 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005843 p->cq_off.head = offsetof(struct io_rings, cq.head);
5844 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5845 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5846 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5847 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5848 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06005849
Jens Axboe044c1ab2019-10-28 09:15:33 -06005850 /*
5851 * Install ring fd as the very last thing, so we don't risk someone
5852 * having closed it before we finish setup
5853 */
5854 ret = io_uring_get_fd(ctx);
5855 if (ret < 0)
5856 goto err;
5857
Jens Axboeda8c9692019-12-02 18:51:26 -07005858 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
5859 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005860 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005861 return ret;
5862err:
5863 io_ring_ctx_wait_and_kill(ctx);
5864 return ret;
5865}
5866
5867/*
5868 * Sets up an aio uring context, and returns the fd. Applications asks for a
5869 * ring size, we return the actual sq/cq ring sizes (among other things) in the
5870 * params structure passed in.
5871 */
5872static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5873{
5874 struct io_uring_params p;
5875 long ret;
5876 int i;
5877
5878 if (copy_from_user(&p, params, sizeof(p)))
5879 return -EFAULT;
5880 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5881 if (p.resv[i])
5882 return -EINVAL;
5883 }
5884
Jens Axboe6c271ce2019-01-10 11:22:30 -07005885 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06005886 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005887 return -EINVAL;
5888
5889 ret = io_uring_create(entries, &p);
5890 if (ret < 0)
5891 return ret;
5892
5893 if (copy_to_user(params, &p, sizeof(p)))
5894 return -EFAULT;
5895
5896 return ret;
5897}
5898
5899SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5900 struct io_uring_params __user *, params)
5901{
5902 return io_uring_setup(entries, params);
5903}
5904
Jens Axboeedafcce2019-01-09 09:16:05 -07005905static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
5906 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06005907 __releases(ctx->uring_lock)
5908 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07005909{
5910 int ret;
5911
Jens Axboe35fa71a2019-04-22 10:23:23 -06005912 /*
5913 * We're inside the ring mutex, if the ref is already dying, then
5914 * someone else killed the ctx or is already going through
5915 * io_uring_register().
5916 */
5917 if (percpu_ref_is_dying(&ctx->refs))
5918 return -ENXIO;
5919
Jens Axboe05f3fb32019-12-09 11:22:50 -07005920 if (opcode != IORING_UNREGISTER_FILES &&
5921 opcode != IORING_REGISTER_FILES_UPDATE) {
5922 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06005923
Jens Axboe05f3fb32019-12-09 11:22:50 -07005924 /*
5925 * Drop uring mutex before waiting for references to exit. If
5926 * another thread is currently inside io_uring_enter() it might
5927 * need to grab the uring_lock to make progress. If we hold it
5928 * here across the drain wait, then we can deadlock. It's safe
5929 * to drop the mutex here, since no new references will come in
5930 * after we've killed the percpu ref.
5931 */
5932 mutex_unlock(&ctx->uring_lock);
5933 wait_for_completion(&ctx->completions[0]);
5934 mutex_lock(&ctx->uring_lock);
5935 }
Jens Axboeedafcce2019-01-09 09:16:05 -07005936
5937 switch (opcode) {
5938 case IORING_REGISTER_BUFFERS:
5939 ret = io_sqe_buffer_register(ctx, arg, nr_args);
5940 break;
5941 case IORING_UNREGISTER_BUFFERS:
5942 ret = -EINVAL;
5943 if (arg || nr_args)
5944 break;
5945 ret = io_sqe_buffer_unregister(ctx);
5946 break;
Jens Axboe6b063142019-01-10 22:13:58 -07005947 case IORING_REGISTER_FILES:
5948 ret = io_sqe_files_register(ctx, arg, nr_args);
5949 break;
5950 case IORING_UNREGISTER_FILES:
5951 ret = -EINVAL;
5952 if (arg || nr_args)
5953 break;
5954 ret = io_sqe_files_unregister(ctx);
5955 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06005956 case IORING_REGISTER_FILES_UPDATE:
5957 ret = io_sqe_files_update(ctx, arg, nr_args);
5958 break;
Jens Axboe9b402842019-04-11 11:45:41 -06005959 case IORING_REGISTER_EVENTFD:
5960 ret = -EINVAL;
5961 if (nr_args != 1)
5962 break;
5963 ret = io_eventfd_register(ctx, arg);
5964 break;
5965 case IORING_UNREGISTER_EVENTFD:
5966 ret = -EINVAL;
5967 if (arg || nr_args)
5968 break;
5969 ret = io_eventfd_unregister(ctx);
5970 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07005971 default:
5972 ret = -EINVAL;
5973 break;
5974 }
5975
Jens Axboe05f3fb32019-12-09 11:22:50 -07005976
5977 if (opcode != IORING_UNREGISTER_FILES &&
5978 opcode != IORING_REGISTER_FILES_UPDATE) {
5979 /* bring the ctx back to life */
5980 reinit_completion(&ctx->completions[0]);
5981 percpu_ref_reinit(&ctx->refs);
5982 }
Jens Axboeedafcce2019-01-09 09:16:05 -07005983 return ret;
5984}
5985
5986SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
5987 void __user *, arg, unsigned int, nr_args)
5988{
5989 struct io_ring_ctx *ctx;
5990 long ret = -EBADF;
5991 struct fd f;
5992
5993 f = fdget(fd);
5994 if (!f.file)
5995 return -EBADF;
5996
5997 ret = -EOPNOTSUPP;
5998 if (f.file->f_op != &io_uring_fops)
5999 goto out_fput;
6000
6001 ctx = f.file->private_data;
6002
6003 mutex_lock(&ctx->uring_lock);
6004 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6005 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006006 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6007 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006008out_fput:
6009 fdput(f);
6010 return ret;
6011}
6012
Jens Axboe2b188cc2019-01-07 10:46:33 -07006013static int __init io_uring_init(void)
6014{
6015 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6016 return 0;
6017};
6018__initcall(io_uring_init);