blob: edf072b6cb8f7648c6e03a6dcd55e8eee4c27e1b [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 Axboe4840e412019-12-25 22:03:45 -070075#include <linux/fadvise.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070076
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020077#define CREATE_TRACE_POINTS
78#include <trace/events/io_uring.h>
79
Jens Axboe2b188cc2019-01-07 10:46:33 -070080#include <uapi/linux/io_uring.h>
81
82#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060083#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
Daniel Xu5277dea2019-09-14 14:23:45 -070085#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060086#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060087
88/*
89 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
90 */
91#define IORING_FILE_TABLE_SHIFT 9
92#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
93#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
94#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070095
96struct io_uring {
97 u32 head ____cacheline_aligned_in_smp;
98 u32 tail ____cacheline_aligned_in_smp;
99};
100
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000102 * This data is shared with the application through the mmap at offsets
103 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200104 *
105 * The offsets to the member fields are published through struct
106 * io_sqring_offsets when calling io_uring_setup.
107 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000108struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200109 /*
110 * Head and tail offsets into the ring; the offsets need to be
111 * masked to get valid indices.
112 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 * The kernel controls head of the sq ring and the tail of the cq ring,
114 * and the application controls tail of the sq ring and the head of the
115 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200116 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000117 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120 * ring_entries - 1)
121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 u32 sq_ring_mask, cq_ring_mask;
123 /* Ring sizes (constant, power of 2) */
124 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 /*
126 * Number of invalid entries dropped by the kernel due to
127 * invalid index stored in array
128 *
129 * Written by the kernel, shouldn't be modified by the
130 * application (i.e. get number of "new events" by comparing to
131 * cached value).
132 *
133 * After a new SQ head value was read by the application this
134 * counter includes all submissions that were dropped reaching
135 * the new SQ head (and possibly more).
136 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000137 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200138 /*
139 * Runtime flags
140 *
141 * Written by the kernel, shouldn't be modified by the
142 * application.
143 *
144 * The application needs a full memory barrier before checking
145 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
146 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000147 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200148 /*
149 * Number of completion events lost because the queue was full;
150 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800151 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200152 * the completion queue.
153 *
154 * Written by the kernel, shouldn't be modified by the
155 * application (i.e. get number of "new events" by comparing to
156 * cached value).
157 *
158 * As completion events come in out of order this counter is not
159 * ordered with any other data.
160 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000161 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 /*
163 * Ring buffer of completion events.
164 *
165 * The kernel writes completion events fresh every time they are
166 * produced, so the application is allowed to modify pending
167 * entries.
168 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000169 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700170};
171
Jens Axboeedafcce2019-01-09 09:16:05 -0700172struct io_mapped_ubuf {
173 u64 ubuf;
174 size_t len;
175 struct bio_vec *bvec;
176 unsigned int nr_bvecs;
177};
178
Jens Axboe65e19f52019-10-26 07:20:21 -0600179struct fixed_file_table {
180 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700181};
182
Jens Axboe05f3fb32019-12-09 11:22:50 -0700183enum {
184 FFD_F_ATOMIC,
185};
186
187struct fixed_file_data {
188 struct fixed_file_table *table;
189 struct io_ring_ctx *ctx;
190
191 struct percpu_ref refs;
192 struct llist_head put_llist;
193 unsigned long state;
194 struct work_struct ref_work;
195 struct completion done;
196};
197
Jens Axboe2b188cc2019-01-07 10:46:33 -0700198struct io_ring_ctx {
199 struct {
200 struct percpu_ref refs;
201 } ____cacheline_aligned_in_smp;
202
203 struct {
204 unsigned int flags;
205 bool compat;
206 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700207 bool cq_overflow_flushed;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300208 bool drain_next;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700209
Hristo Venev75b28af2019-08-26 17:23:46 +0000210 /*
211 * Ring buffer of indices into array of io_uring_sqe, which is
212 * mmapped by the application using the IORING_OFF_SQES offset.
213 *
214 * This indirection could e.g. be used to assign fixed
215 * io_uring_sqe entries to operations and only submit them to
216 * the queue when needed.
217 *
218 * The kernel modifies neither the indices array nor the entries
219 * array.
220 */
221 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222 unsigned cached_sq_head;
223 unsigned sq_entries;
224 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700225 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600226 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700227 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700228 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600229
230 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600231 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700232 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700233
Jens Axboefcb323c2019-10-24 12:39:47 -0600234 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700235 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700236 } ____cacheline_aligned_in_smp;
237
Hristo Venev75b28af2019-08-26 17:23:46 +0000238 struct io_rings *rings;
239
Jens Axboe2b188cc2019-01-07 10:46:33 -0700240 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600241 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700242 struct task_struct *sqo_thread; /* if using sq thread polling */
243 struct mm_struct *sqo_mm;
244 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700245
Jens Axboe6b063142019-01-10 22:13:58 -0700246 /*
247 * If used, fixed file set. Writers must ensure that ->refs is dead,
248 * readers must ensure that ->refs is alive as long as the file* is
249 * used. Only updated through io_uring_register(2).
250 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700251 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700252 unsigned nr_user_files;
253
Jens Axboeedafcce2019-01-09 09:16:05 -0700254 /* if used, fixed mapped user buffers */
255 unsigned nr_user_bufs;
256 struct io_mapped_ubuf *user_bufs;
257
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258 struct user_struct *user;
259
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700260 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700261
Jens Axboe206aefd2019-11-07 18:27:42 -0700262 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
263 struct completion *completions;
264
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700265 /* if all else fails... */
266 struct io_kiocb *fallback_req;
267
Jens Axboe206aefd2019-11-07 18:27:42 -0700268#if defined(CONFIG_UNIX)
269 struct socket *ring_sock;
270#endif
271
272 struct {
273 unsigned cached_cq_tail;
274 unsigned cq_entries;
275 unsigned cq_mask;
276 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700277 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700278 struct wait_queue_head cq_wait;
279 struct fasync_struct *cq_fasync;
280 struct eventfd_ctx *cq_ev_fd;
281 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700282
283 struct {
284 struct mutex uring_lock;
285 wait_queue_head_t wait;
286 } ____cacheline_aligned_in_smp;
287
288 struct {
289 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700290 struct llist_head poll_llist;
291
Jens Axboedef596e2019-01-09 08:59:42 -0700292 /*
293 * ->poll_list is protected by the ctx->uring_lock for
294 * io_uring instances that don't use IORING_SETUP_SQPOLL.
295 * For SQPOLL, only the single threaded io_sq_thread() will
296 * manipulate the list, hence no extra locking is needed there.
297 */
298 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700299 struct hlist_head *cancel_hash;
300 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700301 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600302
303 spinlock_t inflight_lock;
304 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700305 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700306};
307
Jens Axboe09bb8392019-03-13 12:39:28 -0600308/*
309 * First field must be the file pointer in all the
310 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
311 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700312struct io_poll_iocb {
313 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700314 union {
315 struct wait_queue_head *head;
316 u64 addr;
317 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700318 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600319 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700320 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700321 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700322};
323
Jens Axboeb5dba592019-12-11 14:02:38 -0700324struct io_close {
325 struct file *file;
326 struct file *put_file;
327 int fd;
328};
329
Jens Axboead8a48a2019-11-15 08:49:11 -0700330struct io_timeout_data {
331 struct io_kiocb *req;
332 struct hrtimer timer;
333 struct timespec64 ts;
334 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300335 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700336};
337
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700338struct io_accept {
339 struct file *file;
340 struct sockaddr __user *addr;
341 int __user *addr_len;
342 int flags;
343};
344
345struct io_sync {
346 struct file *file;
347 loff_t len;
348 loff_t off;
349 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700350 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700351};
352
Jens Axboefbf23842019-12-17 18:45:56 -0700353struct io_cancel {
354 struct file *file;
355 u64 addr;
356};
357
Jens Axboeb29472e2019-12-17 18:50:29 -0700358struct io_timeout {
359 struct file *file;
360 u64 addr;
361 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700362 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700363};
364
Jens Axboe9adbd452019-12-20 08:45:55 -0700365struct io_rw {
366 /* NOTE: kiocb has the file as the first member, so don't do it here */
367 struct kiocb kiocb;
368 u64 addr;
369 u64 len;
370};
371
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700372struct io_connect {
373 struct file *file;
374 struct sockaddr __user *addr;
375 int addr_len;
376};
377
Jens Axboee47293f2019-12-20 08:58:21 -0700378struct io_sr_msg {
379 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700380 union {
381 struct user_msghdr __user *msg;
382 void __user *buf;
383 };
Jens Axboee47293f2019-12-20 08:58:21 -0700384 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700385 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700386};
387
Jens Axboe15b71ab2019-12-11 11:20:36 -0700388struct io_open {
389 struct file *file;
390 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700391 union {
392 umode_t mode;
393 unsigned mask;
394 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700395 const char __user *fname;
396 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700397 struct statx __user *buffer;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700398 int flags;
399};
400
Jens Axboe05f3fb32019-12-09 11:22:50 -0700401struct io_files_update {
402 struct file *file;
403 u64 arg;
404 u32 nr_args;
405 u32 offset;
406};
407
Jens Axboe4840e412019-12-25 22:03:45 -0700408struct io_fadvise {
409 struct file *file;
410 u64 offset;
411 u32 len;
412 u32 advice;
413};
414
Jens Axboec1ca7572019-12-25 22:18:28 -0700415struct io_madvise {
416 struct file *file;
417 u64 addr;
418 u32 len;
419 u32 advice;
420};
421
Jens Axboef499a022019-12-02 16:28:46 -0700422struct io_async_connect {
423 struct sockaddr_storage address;
424};
425
Jens Axboe03b12302019-12-02 18:50:25 -0700426struct io_async_msghdr {
427 struct iovec fast_iov[UIO_FASTIOV];
428 struct iovec *iov;
429 struct sockaddr __user *uaddr;
430 struct msghdr msg;
431};
432
Jens Axboef67676d2019-12-02 11:03:47 -0700433struct io_async_rw {
434 struct iovec fast_iov[UIO_FASTIOV];
435 struct iovec *iov;
436 ssize_t nr_segs;
437 ssize_t size;
438};
439
Jens Axboe15b71ab2019-12-11 11:20:36 -0700440struct io_async_open {
441 struct filename *filename;
442};
443
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700444struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700445 union {
446 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700447 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700448 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700449 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700450 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700451 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700452};
453
Jens Axboe09bb8392019-03-13 12:39:28 -0600454/*
455 * NOTE! Each of the iocb union members has the file pointer
456 * as the first entry in their struct definition. So you can
457 * access the file pointer through any of the sub-structs,
458 * or directly as just 'ki_filp' in this struct.
459 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700460struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700461 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600462 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700463 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700464 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700465 struct io_accept accept;
466 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700467 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700468 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700469 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700470 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700471 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700472 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700473 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700474 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700475 struct io_madvise madvise;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700476 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700477
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700478 struct io_async_ctx *io;
Jens Axboee94f1412019-12-19 12:06:02 -0700479 union {
480 /*
481 * ring_file is only used in the submission path, and
482 * llist_node is only used for poll deferred completions
483 */
484 struct file *ring_file;
485 struct llist_node llist_node;
486 };
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300487 int ring_fd;
488 bool has_user;
489 bool in_async;
490 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700491 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700492
493 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700494 union {
495 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700496 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700497 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600498 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700499 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700500 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200501#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700502#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700503#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700504#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200505#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
506#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600507#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700508#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800509#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300510#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600511#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600512#define REQ_F_ISREG 2048 /* regular file */
513#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700514#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800515#define REQ_F_INFLIGHT 16384 /* on inflight list */
516#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700517#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboece35a472019-12-17 08:04:44 -0700518#define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */
Jens Axboeba042912019-12-25 16:33:42 -0700519#define REQ_F_CUR_POS 262144 /* read/write uses file position */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700520 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600521 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600522 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700523
Jens Axboefcb323c2019-10-24 12:39:47 -0600524 struct list_head inflight_entry;
525
Jens Axboe561fb042019-10-24 07:25:42 -0600526 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700527};
528
529#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700530#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700531
Jens Axboe9a56a232019-01-09 09:06:50 -0700532struct io_submit_state {
533 struct blk_plug plug;
534
535 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700536 * io_kiocb alloc cache
537 */
538 void *reqs[IO_IOPOLL_BATCH];
539 unsigned int free_reqs;
540 unsigned int cur_req;
541
542 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700543 * File reference cache
544 */
545 struct file *file;
546 unsigned int fd;
547 unsigned int has_refs;
548 unsigned int used_refs;
549 unsigned int ios_left;
550};
551
Jens Axboed3656342019-12-18 09:50:26 -0700552struct io_op_def {
553 /* needs req->io allocated for deferral/async */
554 unsigned async_ctx : 1;
555 /* needs current->mm setup, does mm access */
556 unsigned needs_mm : 1;
557 /* needs req->file assigned */
558 unsigned needs_file : 1;
559 /* needs req->file assigned IFF fd is >= 0 */
560 unsigned fd_non_neg : 1;
561 /* hash wq insertion if file is a regular file */
562 unsigned hash_reg_file : 1;
563 /* unbound wq insertion if file is a non-regular file */
564 unsigned unbound_nonreg_file : 1;
565};
566
567static const struct io_op_def io_op_defs[] = {
568 {
569 /* IORING_OP_NOP */
570 },
571 {
572 /* IORING_OP_READV */
573 .async_ctx = 1,
574 .needs_mm = 1,
575 .needs_file = 1,
576 .unbound_nonreg_file = 1,
577 },
578 {
579 /* IORING_OP_WRITEV */
580 .async_ctx = 1,
581 .needs_mm = 1,
582 .needs_file = 1,
583 .hash_reg_file = 1,
584 .unbound_nonreg_file = 1,
585 },
586 {
587 /* IORING_OP_FSYNC */
588 .needs_file = 1,
589 },
590 {
591 /* IORING_OP_READ_FIXED */
592 .needs_file = 1,
593 .unbound_nonreg_file = 1,
594 },
595 {
596 /* IORING_OP_WRITE_FIXED */
597 .needs_file = 1,
598 .hash_reg_file = 1,
599 .unbound_nonreg_file = 1,
600 },
601 {
602 /* IORING_OP_POLL_ADD */
603 .needs_file = 1,
604 .unbound_nonreg_file = 1,
605 },
606 {
607 /* IORING_OP_POLL_REMOVE */
608 },
609 {
610 /* IORING_OP_SYNC_FILE_RANGE */
611 .needs_file = 1,
612 },
613 {
614 /* IORING_OP_SENDMSG */
615 .async_ctx = 1,
616 .needs_mm = 1,
617 .needs_file = 1,
618 .unbound_nonreg_file = 1,
619 },
620 {
621 /* IORING_OP_RECVMSG */
622 .async_ctx = 1,
623 .needs_mm = 1,
624 .needs_file = 1,
625 .unbound_nonreg_file = 1,
626 },
627 {
628 /* IORING_OP_TIMEOUT */
629 .async_ctx = 1,
630 .needs_mm = 1,
631 },
632 {
633 /* IORING_OP_TIMEOUT_REMOVE */
634 },
635 {
636 /* IORING_OP_ACCEPT */
637 .needs_mm = 1,
638 .needs_file = 1,
639 .unbound_nonreg_file = 1,
640 },
641 {
642 /* IORING_OP_ASYNC_CANCEL */
643 },
644 {
645 /* IORING_OP_LINK_TIMEOUT */
646 .async_ctx = 1,
647 .needs_mm = 1,
648 },
649 {
650 /* IORING_OP_CONNECT */
651 .async_ctx = 1,
652 .needs_mm = 1,
653 .needs_file = 1,
654 .unbound_nonreg_file = 1,
655 },
656 {
657 /* IORING_OP_FALLOCATE */
658 .needs_file = 1,
659 },
660 {
661 /* IORING_OP_OPENAT */
662 .needs_file = 1,
663 .fd_non_neg = 1,
664 },
665 {
666 /* IORING_OP_CLOSE */
667 .needs_file = 1,
668 },
669 {
670 /* IORING_OP_FILES_UPDATE */
671 .needs_mm = 1,
672 },
673 {
674 /* IORING_OP_STATX */
675 .needs_mm = 1,
676 .needs_file = 1,
677 .fd_non_neg = 1,
678 },
Jens Axboe3a6820f2019-12-22 15:19:35 -0700679 {
680 /* IORING_OP_READ */
681 .needs_mm = 1,
682 .needs_file = 1,
683 .unbound_nonreg_file = 1,
684 },
685 {
686 /* IORING_OP_WRITE */
687 .needs_mm = 1,
688 .needs_file = 1,
689 .unbound_nonreg_file = 1,
690 },
Jens Axboe4840e412019-12-25 22:03:45 -0700691 {
692 /* IORING_OP_FADVISE */
693 .needs_file = 1,
694 },
Jens Axboec1ca7572019-12-25 22:18:28 -0700695 {
696 /* IORING_OP_MADVISE */
697 .needs_mm = 1,
698 },
Jens Axboefddafac2020-01-04 20:19:44 -0700699 {
700 /* IORING_OP_SEND */
701 .needs_mm = 1,
702 .needs_file = 1,
703 .unbound_nonreg_file = 1,
704 },
705 {
706 /* IORING_OP_RECV */
707 .needs_mm = 1,
708 .needs_file = 1,
709 .unbound_nonreg_file = 1,
710 },
Jens Axboed3656342019-12-18 09:50:26 -0700711};
712
Jens Axboe561fb042019-10-24 07:25:42 -0600713static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700714static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800715static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700716static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700717static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
718static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700719static int __io_sqe_files_update(struct io_ring_ctx *ctx,
720 struct io_uring_files_update *ip,
721 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600722
Jens Axboe2b188cc2019-01-07 10:46:33 -0700723static struct kmem_cache *req_cachep;
724
725static const struct file_operations io_uring_fops;
726
727struct sock *io_uring_get_socket(struct file *file)
728{
729#if defined(CONFIG_UNIX)
730 if (file->f_op == &io_uring_fops) {
731 struct io_ring_ctx *ctx = file->private_data;
732
733 return ctx->ring_sock->sk;
734 }
735#endif
736 return NULL;
737}
738EXPORT_SYMBOL(io_uring_get_socket);
739
740static void io_ring_ctx_ref_free(struct percpu_ref *ref)
741{
742 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
743
Jens Axboe206aefd2019-11-07 18:27:42 -0700744 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700745}
746
747static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
748{
749 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700750 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700751
752 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
753 if (!ctx)
754 return NULL;
755
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700756 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
757 if (!ctx->fallback_req)
758 goto err;
759
Jens Axboe206aefd2019-11-07 18:27:42 -0700760 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
761 if (!ctx->completions)
762 goto err;
763
Jens Axboe78076bb2019-12-04 19:56:40 -0700764 /*
765 * Use 5 bits less than the max cq entries, that should give us around
766 * 32 entries per hash list if totally full and uniformly spread.
767 */
768 hash_bits = ilog2(p->cq_entries);
769 hash_bits -= 5;
770 if (hash_bits <= 0)
771 hash_bits = 1;
772 ctx->cancel_hash_bits = hash_bits;
773 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
774 GFP_KERNEL);
775 if (!ctx->cancel_hash)
776 goto err;
777 __hash_init(ctx->cancel_hash, 1U << hash_bits);
778
Roman Gushchin21482892019-05-07 10:01:48 -0700779 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700780 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
781 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700782
783 ctx->flags = p->flags;
784 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700785 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700786 init_completion(&ctx->completions[0]);
787 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700788 mutex_init(&ctx->uring_lock);
789 init_waitqueue_head(&ctx->wait);
790 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700791 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700792 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600793 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600794 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600795 init_waitqueue_head(&ctx->inflight_wait);
796 spin_lock_init(&ctx->inflight_lock);
797 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700798 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700799err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700800 if (ctx->fallback_req)
801 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700802 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700803 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700804 kfree(ctx);
805 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700806}
807
Bob Liu9d858b22019-11-13 18:06:25 +0800808static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600809{
Jackie Liua197f662019-11-08 08:09:12 -0700810 struct io_ring_ctx *ctx = req->ctx;
811
Jens Axboe498ccd92019-10-25 10:04:25 -0600812 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
813 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600814}
815
Bob Liu9d858b22019-11-13 18:06:25 +0800816static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600817{
Bob Liu9d858b22019-11-13 18:06:25 +0800818 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
819 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600820
Bob Liu9d858b22019-11-13 18:06:25 +0800821 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600822}
823
824static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600825{
826 struct io_kiocb *req;
827
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600828 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800829 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600830 list_del_init(&req->list);
831 return req;
832 }
833
834 return NULL;
835}
836
Jens Axboe5262f562019-09-17 12:26:57 -0600837static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
838{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600839 struct io_kiocb *req;
840
841 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700842 if (req) {
843 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
844 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800845 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700846 list_del_init(&req->list);
847 return req;
848 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600849 }
850
851 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600852}
853
Jens Axboede0617e2019-04-06 21:51:27 -0600854static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700855{
Hristo Venev75b28af2019-08-26 17:23:46 +0000856 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700857
Hristo Venev75b28af2019-08-26 17:23:46 +0000858 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700859 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000860 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700861
Jens Axboe2b188cc2019-01-07 10:46:33 -0700862 if (wq_has_sleeper(&ctx->cq_wait)) {
863 wake_up_interruptible(&ctx->cq_wait);
864 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
865 }
866 }
867}
868
Jens Axboe94ae5e72019-11-14 19:39:52 -0700869static inline bool io_prep_async_work(struct io_kiocb *req,
870 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600871{
Jens Axboed3656342019-12-18 09:50:26 -0700872 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600873 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600874
Jens Axboed3656342019-12-18 09:50:26 -0700875 if (req->flags & REQ_F_ISREG) {
876 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700877 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700878 } else {
879 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700880 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600881 }
Jens Axboed3656342019-12-18 09:50:26 -0700882 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700883 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600884
Jens Axboe94ae5e72019-11-14 19:39:52 -0700885 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600886 return do_hashed;
887}
888
Jackie Liua197f662019-11-08 08:09:12 -0700889static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600890{
Jackie Liua197f662019-11-08 08:09:12 -0700891 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700892 struct io_kiocb *link;
893 bool do_hashed;
894
895 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600896
897 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
898 req->flags);
899 if (!do_hashed) {
900 io_wq_enqueue(ctx->io_wq, &req->work);
901 } else {
902 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
903 file_inode(req->file));
904 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700905
906 if (link)
907 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600908}
909
Jens Axboe5262f562019-09-17 12:26:57 -0600910static void io_kill_timeout(struct io_kiocb *req)
911{
912 int ret;
913
Jens Axboe2d283902019-12-04 11:08:05 -0700914 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600915 if (ret != -1) {
916 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600917 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700918 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800919 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600920 }
921}
922
923static void io_kill_timeouts(struct io_ring_ctx *ctx)
924{
925 struct io_kiocb *req, *tmp;
926
927 spin_lock_irq(&ctx->completion_lock);
928 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
929 io_kill_timeout(req);
930 spin_unlock_irq(&ctx->completion_lock);
931}
932
Jens Axboede0617e2019-04-06 21:51:27 -0600933static void io_commit_cqring(struct io_ring_ctx *ctx)
934{
935 struct io_kiocb *req;
936
Jens Axboe5262f562019-09-17 12:26:57 -0600937 while ((req = io_get_timeout_req(ctx)) != NULL)
938 io_kill_timeout(req);
939
Jens Axboede0617e2019-04-06 21:51:27 -0600940 __io_commit_cqring(ctx);
941
942 while ((req = io_get_deferred_req(ctx)) != NULL) {
943 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700944 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600945 }
946}
947
Jens Axboe2b188cc2019-01-07 10:46:33 -0700948static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
949{
Hristo Venev75b28af2019-08-26 17:23:46 +0000950 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700951 unsigned tail;
952
953 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200954 /*
955 * writes to the cq entry need to come after reading head; the
956 * control dependency is enough as we're using WRITE_ONCE to
957 * fill the cq entry
958 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000959 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700960 return NULL;
961
962 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000963 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700964}
965
Jens Axboe8c838782019-03-12 15:48:16 -0600966static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
967{
968 if (waitqueue_active(&ctx->wait))
969 wake_up(&ctx->wait);
970 if (waitqueue_active(&ctx->sqo_wait))
971 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600972 if (ctx->cq_ev_fd)
973 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600974}
975
Jens Axboec4a2ed72019-11-21 21:01:26 -0700976/* Returns true if there are no backlogged entries after the flush */
977static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700978{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700979 struct io_rings *rings = ctx->rings;
980 struct io_uring_cqe *cqe;
981 struct io_kiocb *req;
982 unsigned long flags;
983 LIST_HEAD(list);
984
985 if (!force) {
986 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700987 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700988 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
989 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700990 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700991 }
992
993 spin_lock_irqsave(&ctx->completion_lock, flags);
994
995 /* if force is set, the ring is going away. always drop after that */
996 if (force)
997 ctx->cq_overflow_flushed = true;
998
Jens Axboec4a2ed72019-11-21 21:01:26 -0700999 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001000 while (!list_empty(&ctx->cq_overflow_list)) {
1001 cqe = io_get_cqring(ctx);
1002 if (!cqe && !force)
1003 break;
1004
1005 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1006 list);
1007 list_move(&req->list, &list);
1008 if (cqe) {
1009 WRITE_ONCE(cqe->user_data, req->user_data);
1010 WRITE_ONCE(cqe->res, req->result);
1011 WRITE_ONCE(cqe->flags, 0);
1012 } else {
1013 WRITE_ONCE(ctx->rings->cq_overflow,
1014 atomic_inc_return(&ctx->cached_cq_overflow));
1015 }
1016 }
1017
1018 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001019 if (cqe) {
1020 clear_bit(0, &ctx->sq_check_overflow);
1021 clear_bit(0, &ctx->cq_check_overflow);
1022 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001023 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1024 io_cqring_ev_posted(ctx);
1025
1026 while (!list_empty(&list)) {
1027 req = list_first_entry(&list, struct io_kiocb, list);
1028 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001029 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001030 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001031
1032 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001033}
1034
Jens Axboe78e19bb2019-11-06 15:21:34 -07001035static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001036{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001037 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001038 struct io_uring_cqe *cqe;
1039
Jens Axboe78e19bb2019-11-06 15:21:34 -07001040 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001041
Jens Axboe2b188cc2019-01-07 10:46:33 -07001042 /*
1043 * If we can't get a cq entry, userspace overflowed the
1044 * submission (by quite a lot). Increment the overflow count in
1045 * the ring.
1046 */
1047 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001048 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001049 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001050 WRITE_ONCE(cqe->res, res);
1051 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001052 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001053 WRITE_ONCE(ctx->rings->cq_overflow,
1054 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001055 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001056 if (list_empty(&ctx->cq_overflow_list)) {
1057 set_bit(0, &ctx->sq_check_overflow);
1058 set_bit(0, &ctx->cq_check_overflow);
1059 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001060 refcount_inc(&req->refs);
1061 req->result = res;
1062 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001063 }
1064}
1065
Jens Axboe78e19bb2019-11-06 15:21:34 -07001066static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001067{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001068 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001069 unsigned long flags;
1070
1071 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001072 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001073 io_commit_cqring(ctx);
1074 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1075
Jens Axboe8c838782019-03-12 15:48:16 -06001076 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001077}
1078
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001079static inline bool io_is_fallback_req(struct io_kiocb *req)
1080{
1081 return req == (struct io_kiocb *)
1082 ((unsigned long) req->ctx->fallback_req & ~1UL);
1083}
1084
1085static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1086{
1087 struct io_kiocb *req;
1088
1089 req = ctx->fallback_req;
1090 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1091 return req;
1092
1093 return NULL;
1094}
1095
Jens Axboe2579f912019-01-09 09:10:43 -07001096static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1097 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001098{
Jens Axboefd6fab22019-03-14 16:30:06 -06001099 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001100 struct io_kiocb *req;
1101
Jens Axboe2579f912019-01-09 09:10:43 -07001102 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001103 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001104 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001105 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001106 } else if (!state->free_reqs) {
1107 size_t sz;
1108 int ret;
1109
1110 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001111 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1112
1113 /*
1114 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1115 * retry single alloc to be on the safe side.
1116 */
1117 if (unlikely(ret <= 0)) {
1118 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1119 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001120 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001121 ret = 1;
1122 }
Jens Axboe2579f912019-01-09 09:10:43 -07001123 state->free_reqs = ret - 1;
1124 state->cur_req = 1;
1125 req = state->reqs[0];
1126 } else {
1127 req = state->reqs[state->cur_req];
1128 state->free_reqs--;
1129 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001130 }
1131
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001132got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001133 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001134 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001135 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001136 req->ctx = ctx;
1137 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001138 /* one is dropped after submission, the other at completion */
1139 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001140 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001141 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001142 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001143fallback:
1144 req = io_get_fallback_req(ctx);
1145 if (req)
1146 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001147 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001148 return NULL;
1149}
1150
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001151static void __io_req_do_free(struct io_kiocb *req)
1152{
1153 if (likely(!io_is_fallback_req(req)))
1154 kmem_cache_free(req_cachep, req);
1155 else
1156 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1157}
1158
Jens Axboec6ca97b302019-12-28 12:11:08 -07001159static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001160{
Jens Axboefcb323c2019-10-24 12:39:47 -06001161 struct io_ring_ctx *ctx = req->ctx;
1162
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001163 if (req->io)
1164 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001165 if (req->file) {
1166 if (req->flags & REQ_F_FIXED_FILE)
1167 percpu_ref_put(&ctx->file_data->refs);
1168 else
1169 fput(req->file);
1170 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001171}
1172
1173static void __io_free_req(struct io_kiocb *req)
1174{
1175 __io_req_aux_free(req);
1176
Jens Axboefcb323c2019-10-24 12:39:47 -06001177 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001178 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001179 unsigned long flags;
1180
1181 spin_lock_irqsave(&ctx->inflight_lock, flags);
1182 list_del(&req->inflight_entry);
1183 if (waitqueue_active(&ctx->inflight_wait))
1184 wake_up(&ctx->inflight_wait);
1185 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1186 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001187
1188 percpu_ref_put(&req->ctx->refs);
1189 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001190}
1191
Jens Axboec6ca97b302019-12-28 12:11:08 -07001192struct req_batch {
1193 void *reqs[IO_IOPOLL_BATCH];
1194 int to_free;
1195 int need_iter;
1196};
1197
1198static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1199{
1200 if (!rb->to_free)
1201 return;
1202 if (rb->need_iter) {
1203 int i, inflight = 0;
1204 unsigned long flags;
1205
1206 for (i = 0; i < rb->to_free; i++) {
1207 struct io_kiocb *req = rb->reqs[i];
1208
1209 if (req->flags & REQ_F_FIXED_FILE)
1210 req->file = NULL;
1211 if (req->flags & REQ_F_INFLIGHT)
1212 inflight++;
1213 else
1214 rb->reqs[i] = NULL;
1215 __io_req_aux_free(req);
1216 }
1217 if (!inflight)
1218 goto do_free;
1219
1220 spin_lock_irqsave(&ctx->inflight_lock, flags);
1221 for (i = 0; i < rb->to_free; i++) {
1222 struct io_kiocb *req = rb->reqs[i];
1223
1224 if (req) {
1225 list_del(&req->inflight_entry);
1226 if (!--inflight)
1227 break;
1228 }
1229 }
1230 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1231
1232 if (waitqueue_active(&ctx->inflight_wait))
1233 wake_up(&ctx->inflight_wait);
1234 }
1235do_free:
1236 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1237 percpu_ref_put_many(&ctx->refs, rb->to_free);
1238 percpu_ref_put_many(&ctx->file_data->refs, rb->to_free);
1239 rb->to_free = rb->need_iter = 0;
1240}
1241
Jackie Liua197f662019-11-08 08:09:12 -07001242static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001243{
Jackie Liua197f662019-11-08 08:09:12 -07001244 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001245 int ret;
1246
Jens Axboe2d283902019-12-04 11:08:05 -07001247 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001248 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001249 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001250 io_commit_cqring(ctx);
1251 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001252 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001253 return true;
1254 }
1255
1256 return false;
1257}
1258
Jens Axboeba816ad2019-09-28 11:36:45 -06001259static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001260{
Jens Axboe2665abf2019-11-05 12:40:47 -07001261 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001262 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001263
Jens Axboe4d7dd462019-11-20 13:03:52 -07001264 /* Already got next link */
1265 if (req->flags & REQ_F_LINK_NEXT)
1266 return;
1267
Jens Axboe9e645e112019-05-10 16:07:28 -06001268 /*
1269 * The list should never be empty when we are called here. But could
1270 * potentially happen if the chain is messed up, check to be on the
1271 * safe side.
1272 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001273 while (!list_empty(&req->link_list)) {
1274 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1275 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001276
Pavel Begunkov44932332019-12-05 16:16:35 +03001277 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1278 (nxt->flags & REQ_F_TIMEOUT))) {
1279 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001280 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001281 req->flags &= ~REQ_F_LINK_TIMEOUT;
1282 continue;
1283 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001284
Pavel Begunkov44932332019-12-05 16:16:35 +03001285 list_del_init(&req->link_list);
1286 if (!list_empty(&nxt->link_list))
1287 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001288 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001289 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001290 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001291
Jens Axboe4d7dd462019-11-20 13:03:52 -07001292 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001293 if (wake_ev)
1294 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001295}
1296
1297/*
1298 * Called if REQ_F_LINK is set, and we fail the head request
1299 */
1300static void io_fail_links(struct io_kiocb *req)
1301{
Jens Axboe2665abf2019-11-05 12:40:47 -07001302 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001303 unsigned long flags;
1304
1305 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001306
1307 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001308 struct io_kiocb *link = list_first_entry(&req->link_list,
1309 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001310
Pavel Begunkov44932332019-12-05 16:16:35 +03001311 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001312 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001313
1314 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001315 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001316 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001317 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001318 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001319 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001320 }
Jens Axboe5d960722019-11-19 15:31:28 -07001321 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001322 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001323
1324 io_commit_cqring(ctx);
1325 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1326 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001327}
1328
Jens Axboe4d7dd462019-11-20 13:03:52 -07001329static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001330{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001331 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001332 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001333
Jens Axboe9e645e112019-05-10 16:07:28 -06001334 /*
1335 * If LINK is set, we have dependent requests in this chain. If we
1336 * didn't fail this request, queue the first one up, moving any other
1337 * dependencies to the next request. In case of failure, fail the rest
1338 * of the chain.
1339 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001340 if (req->flags & REQ_F_FAIL_LINK) {
1341 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001342 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1343 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001344 struct io_ring_ctx *ctx = req->ctx;
1345 unsigned long flags;
1346
1347 /*
1348 * If this is a timeout link, we could be racing with the
1349 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001350 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001351 */
1352 spin_lock_irqsave(&ctx->completion_lock, flags);
1353 io_req_link_next(req, nxt);
1354 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1355 } else {
1356 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001357 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001358}
Jens Axboe9e645e112019-05-10 16:07:28 -06001359
Jackie Liuc69f8db2019-11-09 11:00:08 +08001360static void io_free_req(struct io_kiocb *req)
1361{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001362 struct io_kiocb *nxt = NULL;
1363
1364 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001365 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001366
1367 if (nxt)
1368 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001369}
1370
Jens Axboeba816ad2019-09-28 11:36:45 -06001371/*
1372 * Drop reference to request, return next in chain (if there is one) if this
1373 * was the last reference to this request.
1374 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001375__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001376static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001377{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001378 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001379
Jens Axboee65ef562019-03-12 10:16:44 -06001380 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001381 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001382}
1383
Jens Axboe2b188cc2019-01-07 10:46:33 -07001384static void io_put_req(struct io_kiocb *req)
1385{
Jens Axboedef596e2019-01-09 08:59:42 -07001386 if (refcount_dec_and_test(&req->refs))
1387 io_free_req(req);
1388}
1389
Jens Axboe978db572019-11-14 22:39:04 -07001390/*
1391 * Must only be used if we don't need to care about links, usually from
1392 * within the completion handling itself.
1393 */
1394static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001395{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001396 /* drop both submit and complete references */
1397 if (refcount_sub_and_test(2, &req->refs))
1398 __io_free_req(req);
1399}
1400
Jens Axboe978db572019-11-14 22:39:04 -07001401static void io_double_put_req(struct io_kiocb *req)
1402{
1403 /* drop both submit and complete references */
1404 if (refcount_sub_and_test(2, &req->refs))
1405 io_free_req(req);
1406}
1407
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001408static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001409{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001410 struct io_rings *rings = ctx->rings;
1411
Jens Axboead3eb2c2019-12-18 17:12:20 -07001412 if (test_bit(0, &ctx->cq_check_overflow)) {
1413 /*
1414 * noflush == true is from the waitqueue handler, just ensure
1415 * we wake up the task, and the next invocation will flush the
1416 * entries. We cannot safely to it from here.
1417 */
1418 if (noflush && !list_empty(&ctx->cq_overflow_list))
1419 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001420
Jens Axboead3eb2c2019-12-18 17:12:20 -07001421 io_cqring_overflow_flush(ctx, false);
1422 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001423
Jens Axboea3a0e432019-08-20 11:03:11 -06001424 /* See comment at the top of this file */
1425 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001426 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001427}
1428
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001429static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1430{
1431 struct io_rings *rings = ctx->rings;
1432
1433 /* make sure SQ entry isn't read before tail */
1434 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1435}
1436
Jens Axboe8237e042019-12-28 10:48:22 -07001437static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001438{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001439 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1440 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001441
Jens Axboec6ca97b302019-12-28 12:11:08 -07001442 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1443 rb->need_iter++;
1444
1445 rb->reqs[rb->to_free++] = req;
1446 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1447 io_free_req_many(req->ctx, rb);
1448 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001449}
1450
Jens Axboedef596e2019-01-09 08:59:42 -07001451/*
1452 * Find and free completed poll iocbs
1453 */
1454static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1455 struct list_head *done)
1456{
Jens Axboe8237e042019-12-28 10:48:22 -07001457 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001458 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001459
Jens Axboec6ca97b302019-12-28 12:11:08 -07001460 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001461 while (!list_empty(done)) {
1462 req = list_first_entry(done, struct io_kiocb, list);
1463 list_del(&req->list);
1464
Jens Axboe78e19bb2019-11-06 15:21:34 -07001465 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001466 (*nr_events)++;
1467
Jens Axboe8237e042019-12-28 10:48:22 -07001468 if (refcount_dec_and_test(&req->refs) &&
1469 !io_req_multi_free(&rb, req))
1470 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001471 }
Jens Axboedef596e2019-01-09 08:59:42 -07001472
Jens Axboe09bb8392019-03-13 12:39:28 -06001473 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001474 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001475}
1476
1477static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1478 long min)
1479{
1480 struct io_kiocb *req, *tmp;
1481 LIST_HEAD(done);
1482 bool spin;
1483 int ret;
1484
1485 /*
1486 * Only spin for completions if we don't have multiple devices hanging
1487 * off our complete list, and we're under the requested amount.
1488 */
1489 spin = !ctx->poll_multi_file && *nr_events < min;
1490
1491 ret = 0;
1492 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001493 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001494
1495 /*
1496 * Move completed entries to our local list. If we find a
1497 * request that requires polling, break out and complete
1498 * the done list first, if we have entries there.
1499 */
1500 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1501 list_move_tail(&req->list, &done);
1502 continue;
1503 }
1504 if (!list_empty(&done))
1505 break;
1506
1507 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1508 if (ret < 0)
1509 break;
1510
1511 if (ret && spin)
1512 spin = false;
1513 ret = 0;
1514 }
1515
1516 if (!list_empty(&done))
1517 io_iopoll_complete(ctx, nr_events, &done);
1518
1519 return ret;
1520}
1521
1522/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001523 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001524 * non-spinning poll check - we'll still enter the driver poll loop, but only
1525 * as a non-spinning completion check.
1526 */
1527static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1528 long min)
1529{
Jens Axboe08f54392019-08-21 22:19:11 -06001530 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001531 int ret;
1532
1533 ret = io_do_iopoll(ctx, nr_events, min);
1534 if (ret < 0)
1535 return ret;
1536 if (!min || *nr_events >= min)
1537 return 0;
1538 }
1539
1540 return 1;
1541}
1542
1543/*
1544 * We can't just wait for polled events to come to us, we have to actively
1545 * find and complete them.
1546 */
1547static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1548{
1549 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1550 return;
1551
1552 mutex_lock(&ctx->uring_lock);
1553 while (!list_empty(&ctx->poll_list)) {
1554 unsigned int nr_events = 0;
1555
1556 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001557
1558 /*
1559 * Ensure we allow local-to-the-cpu processing to take place,
1560 * in this case we need to ensure that we reap all events.
1561 */
1562 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001563 }
1564 mutex_unlock(&ctx->uring_lock);
1565}
1566
Jens Axboe2b2ed972019-10-25 10:06:15 -06001567static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1568 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001569{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001570 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001571
1572 do {
1573 int tmin = 0;
1574
Jens Axboe500f9fb2019-08-19 12:15:59 -06001575 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001576 * Don't enter poll loop if we already have events pending.
1577 * If we do, we can potentially be spinning for commands that
1578 * already triggered a CQE (eg in error).
1579 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001580 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001581 break;
1582
1583 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001584 * If a submit got punted to a workqueue, we can have the
1585 * application entering polling for a command before it gets
1586 * issued. That app will hold the uring_lock for the duration
1587 * of the poll right here, so we need to take a breather every
1588 * now and then to ensure that the issue has a chance to add
1589 * the poll to the issued list. Otherwise we can spin here
1590 * forever, while the workqueue is stuck trying to acquire the
1591 * very same mutex.
1592 */
1593 if (!(++iters & 7)) {
1594 mutex_unlock(&ctx->uring_lock);
1595 mutex_lock(&ctx->uring_lock);
1596 }
1597
Jens Axboedef596e2019-01-09 08:59:42 -07001598 if (*nr_events < min)
1599 tmin = min - *nr_events;
1600
1601 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1602 if (ret <= 0)
1603 break;
1604 ret = 0;
1605 } while (min && !*nr_events && !need_resched());
1606
Jens Axboe2b2ed972019-10-25 10:06:15 -06001607 return ret;
1608}
1609
1610static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1611 long min)
1612{
1613 int ret;
1614
1615 /*
1616 * We disallow the app entering submit/complete with polling, but we
1617 * still need to lock the ring to prevent racing with polled issue
1618 * that got punted to a workqueue.
1619 */
1620 mutex_lock(&ctx->uring_lock);
1621 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001622 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001623 return ret;
1624}
1625
Jens Axboe491381ce2019-10-17 09:20:46 -06001626static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001627{
Jens Axboe491381ce2019-10-17 09:20:46 -06001628 /*
1629 * Tell lockdep we inherited freeze protection from submission
1630 * thread.
1631 */
1632 if (req->flags & REQ_F_ISREG) {
1633 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001634
Jens Axboe491381ce2019-10-17 09:20:46 -06001635 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001636 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001637 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001638}
1639
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001640static inline void req_set_fail_links(struct io_kiocb *req)
1641{
1642 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1643 req->flags |= REQ_F_FAIL_LINK;
1644}
1645
Jens Axboeba816ad2019-09-28 11:36:45 -06001646static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001647{
Jens Axboe9adbd452019-12-20 08:45:55 -07001648 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001649
Jens Axboe491381ce2019-10-17 09:20:46 -06001650 if (kiocb->ki_flags & IOCB_WRITE)
1651 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001652
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001653 if (res != req->result)
1654 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001655 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001656}
1657
1658static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1659{
Jens Axboe9adbd452019-12-20 08:45:55 -07001660 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001661
1662 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001663 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001664}
1665
Jens Axboeba816ad2019-09-28 11:36:45 -06001666static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1667{
Jens Axboe9adbd452019-12-20 08:45:55 -07001668 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001669 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001670
1671 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001672 io_put_req_find_next(req, &nxt);
1673
1674 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001675}
1676
Jens Axboedef596e2019-01-09 08:59:42 -07001677static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1678{
Jens Axboe9adbd452019-12-20 08:45:55 -07001679 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001680
Jens Axboe491381ce2019-10-17 09:20:46 -06001681 if (kiocb->ki_flags & IOCB_WRITE)
1682 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001683
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001684 if (res != req->result)
1685 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001686 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001687 if (res != -EAGAIN)
1688 req->flags |= REQ_F_IOPOLL_COMPLETED;
1689}
1690
1691/*
1692 * After the iocb has been issued, it's safe to be found on the poll list.
1693 * Adding the kiocb to the list AFTER submission ensures that we don't
1694 * find it from a io_iopoll_getevents() thread before the issuer is done
1695 * accessing the kiocb cookie.
1696 */
1697static void io_iopoll_req_issued(struct io_kiocb *req)
1698{
1699 struct io_ring_ctx *ctx = req->ctx;
1700
1701 /*
1702 * Track whether we have multiple files in our lists. This will impact
1703 * how we do polling eventually, not spinning if we're on potentially
1704 * different devices.
1705 */
1706 if (list_empty(&ctx->poll_list)) {
1707 ctx->poll_multi_file = false;
1708 } else if (!ctx->poll_multi_file) {
1709 struct io_kiocb *list_req;
1710
1711 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1712 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001713 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001714 ctx->poll_multi_file = true;
1715 }
1716
1717 /*
1718 * For fast devices, IO may have already completed. If it has, add
1719 * it to the front so we find it first.
1720 */
1721 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1722 list_add(&req->list, &ctx->poll_list);
1723 else
1724 list_add_tail(&req->list, &ctx->poll_list);
1725}
1726
Jens Axboe3d6770f2019-04-13 11:50:54 -06001727static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001728{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001729 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001730 int diff = state->has_refs - state->used_refs;
1731
1732 if (diff)
1733 fput_many(state->file, diff);
1734 state->file = NULL;
1735 }
1736}
1737
1738/*
1739 * Get as many references to a file as we have IOs left in this submission,
1740 * assuming most submissions are for one file, or at least that each file
1741 * has more than one submission.
1742 */
1743static struct file *io_file_get(struct io_submit_state *state, int fd)
1744{
1745 if (!state)
1746 return fget(fd);
1747
1748 if (state->file) {
1749 if (state->fd == fd) {
1750 state->used_refs++;
1751 state->ios_left--;
1752 return state->file;
1753 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001754 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001755 }
1756 state->file = fget_many(fd, state->ios_left);
1757 if (!state->file)
1758 return NULL;
1759
1760 state->fd = fd;
1761 state->has_refs = state->ios_left;
1762 state->used_refs = 1;
1763 state->ios_left--;
1764 return state->file;
1765}
1766
Jens Axboe2b188cc2019-01-07 10:46:33 -07001767/*
1768 * If we tracked the file through the SCM inflight mechanism, we could support
1769 * any file. For now, just ensure that anything potentially problematic is done
1770 * inline.
1771 */
1772static bool io_file_supports_async(struct file *file)
1773{
1774 umode_t mode = file_inode(file)->i_mode;
1775
Jens Axboe10d59342019-12-09 20:16:22 -07001776 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001777 return true;
1778 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1779 return true;
1780
1781 return false;
1782}
1783
Jens Axboe3529d8c2019-12-19 18:24:38 -07001784static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1785 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001786{
Jens Axboedef596e2019-01-09 08:59:42 -07001787 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001788 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001789 unsigned ioprio;
1790 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001791
Jens Axboe09bb8392019-03-13 12:39:28 -06001792 if (!req->file)
1793 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001794
Jens Axboe491381ce2019-10-17 09:20:46 -06001795 if (S_ISREG(file_inode(req->file)->i_mode))
1796 req->flags |= REQ_F_ISREG;
1797
Jens Axboe2b188cc2019-01-07 10:46:33 -07001798 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001799 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1800 req->flags |= REQ_F_CUR_POS;
1801 kiocb->ki_pos = req->file->f_pos;
1802 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001803 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1804 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1805
1806 ioprio = READ_ONCE(sqe->ioprio);
1807 if (ioprio) {
1808 ret = ioprio_check_cap(ioprio);
1809 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001810 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001811
1812 kiocb->ki_ioprio = ioprio;
1813 } else
1814 kiocb->ki_ioprio = get_current_ioprio();
1815
1816 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1817 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001818 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001819
1820 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001821 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1822 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001823 req->flags |= REQ_F_NOWAIT;
1824
1825 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001826 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001827
Jens Axboedef596e2019-01-09 08:59:42 -07001828 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001829 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1830 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001831 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001832
Jens Axboedef596e2019-01-09 08:59:42 -07001833 kiocb->ki_flags |= IOCB_HIPRI;
1834 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001835 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001836 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001837 if (kiocb->ki_flags & IOCB_HIPRI)
1838 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001839 kiocb->ki_complete = io_complete_rw;
1840 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001841
Jens Axboe3529d8c2019-12-19 18:24:38 -07001842 req->rw.addr = READ_ONCE(sqe->addr);
1843 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001844 /* we own ->private, reuse it for the buffer index */
1845 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001846 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001847 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001848}
1849
1850static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1851{
1852 switch (ret) {
1853 case -EIOCBQUEUED:
1854 break;
1855 case -ERESTARTSYS:
1856 case -ERESTARTNOINTR:
1857 case -ERESTARTNOHAND:
1858 case -ERESTART_RESTARTBLOCK:
1859 /*
1860 * We can't just restart the syscall, since previously
1861 * submitted sqes may already be in progress. Just fail this
1862 * IO with EINTR.
1863 */
1864 ret = -EINTR;
1865 /* fall through */
1866 default:
1867 kiocb->ki_complete(kiocb, ret, 0);
1868 }
1869}
1870
Jens Axboeba816ad2019-09-28 11:36:45 -06001871static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1872 bool in_async)
1873{
Jens Axboeba042912019-12-25 16:33:42 -07001874 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1875
1876 if (req->flags & REQ_F_CUR_POS)
1877 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001878 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001879 *nxt = __io_complete_rw(kiocb, ret);
1880 else
1881 io_rw_done(kiocb, ret);
1882}
1883
Jens Axboe9adbd452019-12-20 08:45:55 -07001884static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001885 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001886{
Jens Axboe9adbd452019-12-20 08:45:55 -07001887 struct io_ring_ctx *ctx = req->ctx;
1888 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001889 struct io_mapped_ubuf *imu;
1890 unsigned index, buf_index;
1891 size_t offset;
1892 u64 buf_addr;
1893
1894 /* attempt to use fixed buffers without having provided iovecs */
1895 if (unlikely(!ctx->user_bufs))
1896 return -EFAULT;
1897
Jens Axboe9adbd452019-12-20 08:45:55 -07001898 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001899 if (unlikely(buf_index >= ctx->nr_user_bufs))
1900 return -EFAULT;
1901
1902 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1903 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001904 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001905
1906 /* overflow */
1907 if (buf_addr + len < buf_addr)
1908 return -EFAULT;
1909 /* not inside the mapped region */
1910 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1911 return -EFAULT;
1912
1913 /*
1914 * May not be a start of buffer, set size appropriately
1915 * and advance us to the beginning.
1916 */
1917 offset = buf_addr - imu->ubuf;
1918 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001919
1920 if (offset) {
1921 /*
1922 * Don't use iov_iter_advance() here, as it's really slow for
1923 * using the latter parts of a big fixed buffer - it iterates
1924 * over each segment manually. We can cheat a bit here, because
1925 * we know that:
1926 *
1927 * 1) it's a BVEC iter, we set it up
1928 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1929 * first and last bvec
1930 *
1931 * So just find our index, and adjust the iterator afterwards.
1932 * If the offset is within the first bvec (or the whole first
1933 * bvec, just use iov_iter_advance(). This makes it easier
1934 * since we can just skip the first segment, which may not
1935 * be PAGE_SIZE aligned.
1936 */
1937 const struct bio_vec *bvec = imu->bvec;
1938
1939 if (offset <= bvec->bv_len) {
1940 iov_iter_advance(iter, offset);
1941 } else {
1942 unsigned long seg_skip;
1943
1944 /* skip first vec */
1945 offset -= bvec->bv_len;
1946 seg_skip = 1 + (offset >> PAGE_SHIFT);
1947
1948 iter->bvec = bvec + seg_skip;
1949 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001950 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001951 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001952 }
1953 }
1954
Jens Axboe5e559562019-11-13 16:12:46 -07001955 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001956}
1957
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001958static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1959 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001960{
Jens Axboe9adbd452019-12-20 08:45:55 -07001961 void __user *buf = u64_to_user_ptr(req->rw.addr);
1962 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001963 u8 opcode;
1964
Jens Axboed625c6e2019-12-17 19:53:05 -07001965 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001966 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001967 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001968 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001969 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001970
Jens Axboe9adbd452019-12-20 08:45:55 -07001971 /* buffer index only valid with fixed read/write */
1972 if (req->rw.kiocb.private)
1973 return -EINVAL;
1974
Jens Axboe3a6820f2019-12-22 15:19:35 -07001975 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1976 ssize_t ret;
1977 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1978 *iovec = NULL;
1979 return ret;
1980 }
1981
Jens Axboef67676d2019-12-02 11:03:47 -07001982 if (req->io) {
1983 struct io_async_rw *iorw = &req->io->rw;
1984
1985 *iovec = iorw->iov;
1986 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1987 if (iorw->iov == iorw->fast_iov)
1988 *iovec = NULL;
1989 return iorw->size;
1990 }
1991
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001992 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001993 return -EFAULT;
1994
1995#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001996 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001997 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1998 iovec, iter);
1999#endif
2000
2001 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2002}
2003
Jens Axboe32960612019-09-23 11:05:34 -06002004/*
2005 * For files that don't have ->read_iter() and ->write_iter(), handle them
2006 * by looping over ->read() or ->write() manually.
2007 */
2008static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2009 struct iov_iter *iter)
2010{
2011 ssize_t ret = 0;
2012
2013 /*
2014 * Don't support polled IO through this interface, and we can't
2015 * support non-blocking either. For the latter, this just causes
2016 * the kiocb to be handled from an async context.
2017 */
2018 if (kiocb->ki_flags & IOCB_HIPRI)
2019 return -EOPNOTSUPP;
2020 if (kiocb->ki_flags & IOCB_NOWAIT)
2021 return -EAGAIN;
2022
2023 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002024 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002025 ssize_t nr;
2026
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002027 if (!iov_iter_is_bvec(iter)) {
2028 iovec = iov_iter_iovec(iter);
2029 } else {
2030 /* fixed buffers import bvec */
2031 iovec.iov_base = kmap(iter->bvec->bv_page)
2032 + iter->iov_offset;
2033 iovec.iov_len = min(iter->count,
2034 iter->bvec->bv_len - iter->iov_offset);
2035 }
2036
Jens Axboe32960612019-09-23 11:05:34 -06002037 if (rw == READ) {
2038 nr = file->f_op->read(file, iovec.iov_base,
2039 iovec.iov_len, &kiocb->ki_pos);
2040 } else {
2041 nr = file->f_op->write(file, iovec.iov_base,
2042 iovec.iov_len, &kiocb->ki_pos);
2043 }
2044
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002045 if (iov_iter_is_bvec(iter))
2046 kunmap(iter->bvec->bv_page);
2047
Jens Axboe32960612019-09-23 11:05:34 -06002048 if (nr < 0) {
2049 if (!ret)
2050 ret = nr;
2051 break;
2052 }
2053 ret += nr;
2054 if (nr != iovec.iov_len)
2055 break;
2056 iov_iter_advance(iter, nr);
2057 }
2058
2059 return ret;
2060}
2061
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002062static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002063 struct iovec *iovec, struct iovec *fast_iov,
2064 struct iov_iter *iter)
2065{
2066 req->io->rw.nr_segs = iter->nr_segs;
2067 req->io->rw.size = io_size;
2068 req->io->rw.iov = iovec;
2069 if (!req->io->rw.iov) {
2070 req->io->rw.iov = req->io->rw.fast_iov;
2071 memcpy(req->io->rw.iov, fast_iov,
2072 sizeof(struct iovec) * iter->nr_segs);
2073 }
2074}
2075
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002076static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002077{
Jens Axboed3656342019-12-18 09:50:26 -07002078 if (!io_op_defs[req->opcode].async_ctx)
2079 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002080 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002081 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002082}
2083
2084static void io_rw_async(struct io_wq_work **workptr)
2085{
2086 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2087 struct iovec *iov = NULL;
2088
2089 if (req->io->rw.iov != req->io->rw.fast_iov)
2090 iov = req->io->rw.iov;
2091 io_wq_submit_work(workptr);
2092 kfree(iov);
2093}
2094
2095static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2096 struct iovec *iovec, struct iovec *fast_iov,
2097 struct iov_iter *iter)
2098{
Jens Axboe74566df2020-01-13 19:23:24 -07002099 if (req->opcode == IORING_OP_READ_FIXED ||
2100 req->opcode == IORING_OP_WRITE_FIXED)
2101 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002102 if (!req->io && io_alloc_async_ctx(req))
2103 return -ENOMEM;
2104
2105 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2106 req->work.func = io_rw_async;
2107 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002108}
2109
Jens Axboe3529d8c2019-12-19 18:24:38 -07002110static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2111 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002112{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002113 struct io_async_ctx *io;
2114 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002115 ssize_t ret;
2116
Jens Axboe3529d8c2019-12-19 18:24:38 -07002117 ret = io_prep_rw(req, sqe, force_nonblock);
2118 if (ret)
2119 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002120
Jens Axboe3529d8c2019-12-19 18:24:38 -07002121 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2122 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002123
Jens Axboe3529d8c2019-12-19 18:24:38 -07002124 if (!req->io)
2125 return 0;
2126
2127 io = req->io;
2128 io->rw.iov = io->rw.fast_iov;
2129 req->io = NULL;
2130 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2131 req->io = io;
2132 if (ret < 0)
2133 return ret;
2134
2135 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2136 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002137}
2138
Pavel Begunkov267bc902019-11-07 01:41:08 +03002139static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002140 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002141{
2142 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002143 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002144 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002145 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002146 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002147
Jens Axboe3529d8c2019-12-19 18:24:38 -07002148 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002149 if (ret < 0)
2150 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002151
Jens Axboefd6c2e42019-12-18 12:19:41 -07002152 /* Ensure we clear previously set non-block flag */
2153 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002154 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002155
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002156 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002157 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002158 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002159 req->result = io_size;
2160
2161 /*
2162 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2163 * we know to async punt it even if it was opened O_NONBLOCK
2164 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002165 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002166 req->flags |= REQ_F_MUST_PUNT;
2167 goto copy_iov;
2168 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002169
Jens Axboe31b51512019-01-18 22:56:34 -07002170 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002171 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002172 if (!ret) {
2173 ssize_t ret2;
2174
Jens Axboe9adbd452019-12-20 08:45:55 -07002175 if (req->file->f_op->read_iter)
2176 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002177 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002178 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002179
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002180 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002181 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002182 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002183 } else {
2184copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002185 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002186 inline_vecs, &iter);
2187 if (ret)
2188 goto out_free;
2189 return -EAGAIN;
2190 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002191 }
Jens Axboef67676d2019-12-02 11:03:47 -07002192out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002193 if (!io_wq_current_is_worker())
2194 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002195 return ret;
2196}
2197
Jens Axboe3529d8c2019-12-19 18:24:38 -07002198static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2199 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002200{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002201 struct io_async_ctx *io;
2202 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002203 ssize_t ret;
2204
Jens Axboe3529d8c2019-12-19 18:24:38 -07002205 ret = io_prep_rw(req, sqe, force_nonblock);
2206 if (ret)
2207 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002208
Jens Axboe3529d8c2019-12-19 18:24:38 -07002209 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2210 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002211
Jens Axboe3529d8c2019-12-19 18:24:38 -07002212 if (!req->io)
2213 return 0;
2214
2215 io = req->io;
2216 io->rw.iov = io->rw.fast_iov;
2217 req->io = NULL;
2218 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2219 req->io = io;
2220 if (ret < 0)
2221 return ret;
2222
2223 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2224 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002225}
2226
Pavel Begunkov267bc902019-11-07 01:41:08 +03002227static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002228 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002229{
2230 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002231 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002232 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002233 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002234 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002235
Jens Axboe3529d8c2019-12-19 18:24:38 -07002236 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002237 if (ret < 0)
2238 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002239
Jens Axboefd6c2e42019-12-18 12:19:41 -07002240 /* Ensure we clear previously set non-block flag */
2241 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002242 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002243
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002244 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002245 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002246 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002247 req->result = io_size;
2248
2249 /*
2250 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2251 * we know to async punt it even if it was opened O_NONBLOCK
2252 */
2253 if (force_nonblock && !io_file_supports_async(req->file)) {
2254 req->flags |= REQ_F_MUST_PUNT;
2255 goto copy_iov;
2256 }
2257
Jens Axboe10d59342019-12-09 20:16:22 -07002258 /* file path doesn't support NOWAIT for non-direct_IO */
2259 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2260 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002261 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002262
Jens Axboe31b51512019-01-18 22:56:34 -07002263 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002264 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002265 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002266 ssize_t ret2;
2267
Jens Axboe2b188cc2019-01-07 10:46:33 -07002268 /*
2269 * Open-code file_start_write here to grab freeze protection,
2270 * which will be released by another thread in
2271 * io_complete_rw(). Fool lockdep by telling it the lock got
2272 * released so that it doesn't complain about the held lock when
2273 * we return to userspace.
2274 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002275 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002276 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002277 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002278 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002279 SB_FREEZE_WRITE);
2280 }
2281 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002282
Jens Axboe9adbd452019-12-20 08:45:55 -07002283 if (req->file->f_op->write_iter)
2284 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002285 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002286 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002287 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002288 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002289 } else {
2290copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002291 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002292 inline_vecs, &iter);
2293 if (ret)
2294 goto out_free;
2295 return -EAGAIN;
2296 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002297 }
Jens Axboe31b51512019-01-18 22:56:34 -07002298out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002299 if (!io_wq_current_is_worker())
2300 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002301 return ret;
2302}
2303
2304/*
2305 * IORING_OP_NOP just posts a completion event, nothing else.
2306 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002307static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002308{
2309 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002310
Jens Axboedef596e2019-01-09 08:59:42 -07002311 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2312 return -EINVAL;
2313
Jens Axboe78e19bb2019-11-06 15:21:34 -07002314 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002315 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002316 return 0;
2317}
2318
Jens Axboe3529d8c2019-12-19 18:24:38 -07002319static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002320{
Jens Axboe6b063142019-01-10 22:13:58 -07002321 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002322
Jens Axboe09bb8392019-03-13 12:39:28 -06002323 if (!req->file)
2324 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002325
Jens Axboe6b063142019-01-10 22:13:58 -07002326 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002327 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002328 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002329 return -EINVAL;
2330
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002331 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2332 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2333 return -EINVAL;
2334
2335 req->sync.off = READ_ONCE(sqe->off);
2336 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002337 return 0;
2338}
2339
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002340static bool io_req_cancelled(struct io_kiocb *req)
2341{
2342 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2343 req_set_fail_links(req);
2344 io_cqring_add_event(req, -ECANCELED);
2345 io_put_req(req);
2346 return true;
2347 }
2348
2349 return false;
2350}
2351
Jens Axboe78912932020-01-14 22:09:06 -07002352static void io_link_work_cb(struct io_wq_work **workptr)
2353{
2354 struct io_wq_work *work = *workptr;
2355 struct io_kiocb *link = work->data;
2356
2357 io_queue_linked_timeout(link);
2358 work->func = io_wq_submit_work;
2359}
2360
2361static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2362{
2363 struct io_kiocb *link;
2364
2365 io_prep_async_work(nxt, &link);
2366 *workptr = &nxt->work;
2367 if (link) {
2368 nxt->work.flags |= IO_WQ_WORK_CB;
2369 nxt->work.func = io_link_work_cb;
2370 nxt->work.data = link;
2371 }
2372}
2373
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002374static void io_fsync_finish(struct io_wq_work **workptr)
2375{
2376 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2377 loff_t end = req->sync.off + req->sync.len;
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 = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002385 end > 0 ? end : LLONG_MAX,
2386 req->sync.flags & IORING_FSYNC_DATASYNC);
2387 if (ret < 0)
2388 req_set_fail_links(req);
2389 io_cqring_add_event(req, ret);
2390 io_put_req_find_next(req, &nxt);
2391 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002392 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002393}
2394
Jens Axboefc4df992019-12-10 14:38:45 -07002395static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2396 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002397{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002398 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002399
2400 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002401 if (force_nonblock) {
2402 io_put_req(req);
2403 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002404 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002405 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002406
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002407 work = old_work = &req->work;
2408 io_fsync_finish(&work);
2409 if (work && work != old_work)
2410 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002411 return 0;
2412}
2413
Jens Axboed63d1b52019-12-10 10:38:56 -07002414static void io_fallocate_finish(struct io_wq_work **workptr)
2415{
2416 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2417 struct io_kiocb *nxt = NULL;
2418 int ret;
2419
2420 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2421 req->sync.len);
2422 if (ret < 0)
2423 req_set_fail_links(req);
2424 io_cqring_add_event(req, ret);
2425 io_put_req_find_next(req, &nxt);
2426 if (nxt)
2427 io_wq_assign_next(workptr, nxt);
2428}
2429
2430static int io_fallocate_prep(struct io_kiocb *req,
2431 const struct io_uring_sqe *sqe)
2432{
2433 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2434 return -EINVAL;
2435
2436 req->sync.off = READ_ONCE(sqe->off);
2437 req->sync.len = READ_ONCE(sqe->addr);
2438 req->sync.mode = READ_ONCE(sqe->len);
2439 return 0;
2440}
2441
2442static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2443 bool force_nonblock)
2444{
2445 struct io_wq_work *work, *old_work;
2446
2447 /* fallocate always requiring blocking context */
2448 if (force_nonblock) {
2449 io_put_req(req);
2450 req->work.func = io_fallocate_finish;
2451 return -EAGAIN;
2452 }
2453
2454 work = old_work = &req->work;
2455 io_fallocate_finish(&work);
2456 if (work && work != old_work)
2457 *nxt = container_of(work, struct io_kiocb, work);
2458
2459 return 0;
2460}
2461
Jens Axboe15b71ab2019-12-11 11:20:36 -07002462static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2463{
2464 int ret;
2465
2466 if (sqe->ioprio || sqe->buf_index)
2467 return -EINVAL;
2468
2469 req->open.dfd = READ_ONCE(sqe->fd);
2470 req->open.mode = READ_ONCE(sqe->len);
2471 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2472 req->open.flags = READ_ONCE(sqe->open_flags);
2473
2474 req->open.filename = getname(req->open.fname);
2475 if (IS_ERR(req->open.filename)) {
2476 ret = PTR_ERR(req->open.filename);
2477 req->open.filename = NULL;
2478 return ret;
2479 }
2480
2481 return 0;
2482}
2483
2484static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2485 bool force_nonblock)
2486{
2487 struct open_flags op;
2488 struct open_how how;
2489 struct file *file;
2490 int ret;
2491
2492 if (force_nonblock) {
2493 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2494 return -EAGAIN;
2495 }
2496
2497 how = build_open_how(req->open.flags, req->open.mode);
2498 ret = build_open_flags(&how, &op);
2499 if (ret)
2500 goto err;
2501
2502 ret = get_unused_fd_flags(how.flags);
2503 if (ret < 0)
2504 goto err;
2505
2506 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2507 if (IS_ERR(file)) {
2508 put_unused_fd(ret);
2509 ret = PTR_ERR(file);
2510 } else {
2511 fsnotify_open(file);
2512 fd_install(ret, file);
2513 }
2514err:
2515 putname(req->open.filename);
2516 if (ret < 0)
2517 req_set_fail_links(req);
2518 io_cqring_add_event(req, ret);
2519 io_put_req_find_next(req, nxt);
2520 return 0;
2521}
2522
Jens Axboec1ca7572019-12-25 22:18:28 -07002523static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2524{
2525#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2526 if (sqe->ioprio || sqe->buf_index || sqe->off)
2527 return -EINVAL;
2528
2529 req->madvise.addr = READ_ONCE(sqe->addr);
2530 req->madvise.len = READ_ONCE(sqe->len);
2531 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2532 return 0;
2533#else
2534 return -EOPNOTSUPP;
2535#endif
2536}
2537
2538static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2539 bool force_nonblock)
2540{
2541#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2542 struct io_madvise *ma = &req->madvise;
2543 int ret;
2544
2545 if (force_nonblock)
2546 return -EAGAIN;
2547
2548 ret = do_madvise(ma->addr, ma->len, ma->advice);
2549 if (ret < 0)
2550 req_set_fail_links(req);
2551 io_cqring_add_event(req, ret);
2552 io_put_req_find_next(req, nxt);
2553 return 0;
2554#else
2555 return -EOPNOTSUPP;
2556#endif
2557}
2558
Jens Axboe4840e412019-12-25 22:03:45 -07002559static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2560{
2561 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2562 return -EINVAL;
2563
2564 req->fadvise.offset = READ_ONCE(sqe->off);
2565 req->fadvise.len = READ_ONCE(sqe->len);
2566 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2567 return 0;
2568}
2569
2570static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2571 bool force_nonblock)
2572{
2573 struct io_fadvise *fa = &req->fadvise;
2574 int ret;
2575
2576 /* DONTNEED may block, others _should_ not */
2577 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2578 return -EAGAIN;
2579
2580 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2581 if (ret < 0)
2582 req_set_fail_links(req);
2583 io_cqring_add_event(req, ret);
2584 io_put_req_find_next(req, nxt);
2585 return 0;
2586}
2587
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002588static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2589{
2590 unsigned lookup_flags;
2591 int ret;
2592
2593 if (sqe->ioprio || sqe->buf_index)
2594 return -EINVAL;
2595
2596 req->open.dfd = READ_ONCE(sqe->fd);
2597 req->open.mask = READ_ONCE(sqe->len);
2598 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2599 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2600 req->open.flags = READ_ONCE(sqe->statx_flags);
2601
2602 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.flags))
2603 return -EINVAL;
2604
2605 req->open.filename = getname_flags(req->open.fname, lookup_flags, NULL);
2606 if (IS_ERR(req->open.filename)) {
2607 ret = PTR_ERR(req->open.filename);
2608 req->open.filename = NULL;
2609 return ret;
2610 }
2611
2612 return 0;
2613}
2614
2615static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2616 bool force_nonblock)
2617{
2618 struct io_open *ctx = &req->open;
2619 unsigned lookup_flags;
2620 struct path path;
2621 struct kstat stat;
2622 int ret;
2623
2624 if (force_nonblock)
2625 return -EAGAIN;
2626
2627 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->flags))
2628 return -EINVAL;
2629
2630retry:
2631 /* filename_lookup() drops it, keep a reference */
2632 ctx->filename->refcnt++;
2633
2634 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2635 NULL);
2636 if (ret)
2637 goto err;
2638
2639 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->flags);
2640 path_put(&path);
2641 if (retry_estale(ret, lookup_flags)) {
2642 lookup_flags |= LOOKUP_REVAL;
2643 goto retry;
2644 }
2645 if (!ret)
2646 ret = cp_statx(&stat, ctx->buffer);
2647err:
2648 putname(ctx->filename);
2649 if (ret < 0)
2650 req_set_fail_links(req);
2651 io_cqring_add_event(req, ret);
2652 io_put_req_find_next(req, nxt);
2653 return 0;
2654}
2655
Jens Axboeb5dba592019-12-11 14:02:38 -07002656static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2657{
2658 /*
2659 * If we queue this for async, it must not be cancellable. That would
2660 * leave the 'file' in an undeterminate state.
2661 */
2662 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2663
2664 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2665 sqe->rw_flags || sqe->buf_index)
2666 return -EINVAL;
2667 if (sqe->flags & IOSQE_FIXED_FILE)
2668 return -EINVAL;
2669
2670 req->close.fd = READ_ONCE(sqe->fd);
2671 if (req->file->f_op == &io_uring_fops ||
2672 req->close.fd == req->ring_fd)
2673 return -EBADF;
2674
2675 return 0;
2676}
2677
2678static void io_close_finish(struct io_wq_work **workptr)
2679{
2680 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2681 struct io_kiocb *nxt = NULL;
2682
2683 /* Invoked with files, we need to do the close */
2684 if (req->work.files) {
2685 int ret;
2686
2687 ret = filp_close(req->close.put_file, req->work.files);
2688 if (ret < 0) {
2689 req_set_fail_links(req);
2690 }
2691 io_cqring_add_event(req, ret);
2692 }
2693
2694 fput(req->close.put_file);
2695
2696 /* we bypassed the re-issue, drop the submission reference */
2697 io_put_req(req);
2698 io_put_req_find_next(req, &nxt);
2699 if (nxt)
2700 io_wq_assign_next(workptr, nxt);
2701}
2702
2703static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2704 bool force_nonblock)
2705{
2706 int ret;
2707
2708 req->close.put_file = NULL;
2709 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2710 if (ret < 0)
2711 return ret;
2712
2713 /* if the file has a flush method, be safe and punt to async */
2714 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2715 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2716 goto eagain;
2717 }
2718
2719 /*
2720 * No ->flush(), safely close from here and just punt the
2721 * fput() to async context.
2722 */
2723 ret = filp_close(req->close.put_file, current->files);
2724
2725 if (ret < 0)
2726 req_set_fail_links(req);
2727 io_cqring_add_event(req, ret);
2728
2729 if (io_wq_current_is_worker()) {
2730 struct io_wq_work *old_work, *work;
2731
2732 old_work = work = &req->work;
2733 io_close_finish(&work);
2734 if (work && work != old_work)
2735 *nxt = container_of(work, struct io_kiocb, work);
2736 return 0;
2737 }
2738
2739eagain:
2740 req->work.func = io_close_finish;
2741 return -EAGAIN;
2742}
2743
Jens Axboe3529d8c2019-12-19 18:24:38 -07002744static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002745{
2746 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002747
2748 if (!req->file)
2749 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002750
2751 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2752 return -EINVAL;
2753 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2754 return -EINVAL;
2755
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002756 req->sync.off = READ_ONCE(sqe->off);
2757 req->sync.len = READ_ONCE(sqe->len);
2758 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002759 return 0;
2760}
2761
2762static void io_sync_file_range_finish(struct io_wq_work **workptr)
2763{
2764 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2765 struct io_kiocb *nxt = NULL;
2766 int ret;
2767
2768 if (io_req_cancelled(req))
2769 return;
2770
Jens Axboe9adbd452019-12-20 08:45:55 -07002771 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002772 req->sync.flags);
2773 if (ret < 0)
2774 req_set_fail_links(req);
2775 io_cqring_add_event(req, ret);
2776 io_put_req_find_next(req, &nxt);
2777 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002778 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002779}
2780
Jens Axboefc4df992019-12-10 14:38:45 -07002781static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002782 bool force_nonblock)
2783{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002784 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002785
2786 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002787 if (force_nonblock) {
2788 io_put_req(req);
2789 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002790 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002791 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002792
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002793 work = old_work = &req->work;
2794 io_sync_file_range_finish(&work);
2795 if (work && work != old_work)
2796 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002797 return 0;
2798}
2799
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002800#if defined(CONFIG_NET)
2801static void io_sendrecv_async(struct io_wq_work **workptr)
2802{
2803 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2804 struct iovec *iov = NULL;
2805
2806 if (req->io->rw.iov != req->io->rw.fast_iov)
2807 iov = req->io->msg.iov;
2808 io_wq_submit_work(workptr);
2809 kfree(iov);
2810}
2811#endif
2812
Jens Axboe3529d8c2019-12-19 18:24:38 -07002813static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002814{
Jens Axboe03b12302019-12-02 18:50:25 -07002815#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002816 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002817 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002818
Jens Axboee47293f2019-12-20 08:58:21 -07002819 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2820 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002821 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002822
Jens Axboefddafac2020-01-04 20:19:44 -07002823 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002824 return 0;
2825
Jens Axboed9688562019-12-09 19:35:20 -07002826 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002827 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002828 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002829#else
Jens Axboee47293f2019-12-20 08:58:21 -07002830 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002831#endif
2832}
2833
Jens Axboefc4df992019-12-10 14:38:45 -07002834static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2835 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002836{
2837#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002838 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002839 struct socket *sock;
2840 int ret;
2841
2842 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2843 return -EINVAL;
2844
2845 sock = sock_from_file(req->file, &ret);
2846 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002847 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002848 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002849 unsigned flags;
2850
Jens Axboe03b12302019-12-02 18:50:25 -07002851 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002852 kmsg = &req->io->msg;
2853 kmsg->msg.msg_name = &addr;
2854 /* if iov is set, it's allocated already */
2855 if (!kmsg->iov)
2856 kmsg->iov = kmsg->fast_iov;
2857 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002858 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002859 struct io_sr_msg *sr = &req->sr_msg;
2860
Jens Axboe0b416c32019-12-15 10:57:46 -07002861 kmsg = &io.msg;
2862 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002863
2864 io.msg.iov = io.msg.fast_iov;
2865 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2866 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002867 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002868 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002869 }
2870
Jens Axboee47293f2019-12-20 08:58:21 -07002871 flags = req->sr_msg.msg_flags;
2872 if (flags & MSG_DONTWAIT)
2873 req->flags |= REQ_F_NOWAIT;
2874 else if (force_nonblock)
2875 flags |= MSG_DONTWAIT;
2876
Jens Axboe0b416c32019-12-15 10:57:46 -07002877 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002878 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002879 if (req->io)
2880 return -EAGAIN;
2881 if (io_alloc_async_ctx(req))
2882 return -ENOMEM;
2883 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2884 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002885 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002886 }
2887 if (ret == -ERESTARTSYS)
2888 ret = -EINTR;
2889 }
2890
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002891 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002892 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002893 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002894 if (ret < 0)
2895 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002896 io_put_req_find_next(req, nxt);
2897 return 0;
2898#else
2899 return -EOPNOTSUPP;
2900#endif
2901}
2902
Jens Axboefddafac2020-01-04 20:19:44 -07002903static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2904 bool force_nonblock)
2905{
2906#if defined(CONFIG_NET)
2907 struct socket *sock;
2908 int ret;
2909
2910 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2911 return -EINVAL;
2912
2913 sock = sock_from_file(req->file, &ret);
2914 if (sock) {
2915 struct io_sr_msg *sr = &req->sr_msg;
2916 struct msghdr msg;
2917 struct iovec iov;
2918 unsigned flags;
2919
2920 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
2921 &msg.msg_iter);
2922 if (ret)
2923 return ret;
2924
2925 msg.msg_name = NULL;
2926 msg.msg_control = NULL;
2927 msg.msg_controllen = 0;
2928 msg.msg_namelen = 0;
2929
2930 flags = req->sr_msg.msg_flags;
2931 if (flags & MSG_DONTWAIT)
2932 req->flags |= REQ_F_NOWAIT;
2933 else if (force_nonblock)
2934 flags |= MSG_DONTWAIT;
2935
2936 ret = __sys_sendmsg_sock(sock, &msg, flags);
2937 if (force_nonblock && ret == -EAGAIN)
2938 return -EAGAIN;
2939 if (ret == -ERESTARTSYS)
2940 ret = -EINTR;
2941 }
2942
2943 io_cqring_add_event(req, ret);
2944 if (ret < 0)
2945 req_set_fail_links(req);
2946 io_put_req_find_next(req, nxt);
2947 return 0;
2948#else
2949 return -EOPNOTSUPP;
2950#endif
2951}
2952
Jens Axboe3529d8c2019-12-19 18:24:38 -07002953static int io_recvmsg_prep(struct io_kiocb *req,
2954 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002955{
2956#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002957 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002958 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002959
Jens Axboe3529d8c2019-12-19 18:24:38 -07002960 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2961 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2962
Jens Axboefddafac2020-01-04 20:19:44 -07002963 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07002964 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002965
Jens Axboed9688562019-12-09 19:35:20 -07002966 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002967 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002968 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002969#else
Jens Axboee47293f2019-12-20 08:58:21 -07002970 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002971#endif
2972}
2973
Jens Axboefc4df992019-12-10 14:38:45 -07002974static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2975 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002976{
2977#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002978 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002979 struct socket *sock;
2980 int ret;
2981
2982 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2983 return -EINVAL;
2984
2985 sock = sock_from_file(req->file, &ret);
2986 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002987 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002988 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002989 unsigned flags;
2990
Jens Axboe03b12302019-12-02 18:50:25 -07002991 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002992 kmsg = &req->io->msg;
2993 kmsg->msg.msg_name = &addr;
2994 /* if iov is set, it's allocated already */
2995 if (!kmsg->iov)
2996 kmsg->iov = kmsg->fast_iov;
2997 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002998 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002999 struct io_sr_msg *sr = &req->sr_msg;
3000
Jens Axboe0b416c32019-12-15 10:57:46 -07003001 kmsg = &io.msg;
3002 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003003
3004 io.msg.iov = io.msg.fast_iov;
3005 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3006 sr->msg_flags, &io.msg.uaddr,
3007 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003008 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003009 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003010 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003011
Jens Axboee47293f2019-12-20 08:58:21 -07003012 flags = req->sr_msg.msg_flags;
3013 if (flags & MSG_DONTWAIT)
3014 req->flags |= REQ_F_NOWAIT;
3015 else if (force_nonblock)
3016 flags |= MSG_DONTWAIT;
3017
3018 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3019 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003020 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003021 if (req->io)
3022 return -EAGAIN;
3023 if (io_alloc_async_ctx(req))
3024 return -ENOMEM;
3025 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3026 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003027 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003028 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003029 if (ret == -ERESTARTSYS)
3030 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003031 }
3032
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003033 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003034 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003035 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003036 if (ret < 0)
3037 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003038 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003039 return 0;
3040#else
3041 return -EOPNOTSUPP;
3042#endif
3043}
3044
Jens Axboefddafac2020-01-04 20:19:44 -07003045static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3046 bool force_nonblock)
3047{
3048#if defined(CONFIG_NET)
3049 struct socket *sock;
3050 int ret;
3051
3052 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3053 return -EINVAL;
3054
3055 sock = sock_from_file(req->file, &ret);
3056 if (sock) {
3057 struct io_sr_msg *sr = &req->sr_msg;
3058 struct msghdr msg;
3059 struct iovec iov;
3060 unsigned flags;
3061
3062 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3063 &msg.msg_iter);
3064 if (ret)
3065 return ret;
3066
3067 msg.msg_name = NULL;
3068 msg.msg_control = NULL;
3069 msg.msg_controllen = 0;
3070 msg.msg_namelen = 0;
3071 msg.msg_iocb = NULL;
3072 msg.msg_flags = 0;
3073
3074 flags = req->sr_msg.msg_flags;
3075 if (flags & MSG_DONTWAIT)
3076 req->flags |= REQ_F_NOWAIT;
3077 else if (force_nonblock)
3078 flags |= MSG_DONTWAIT;
3079
3080 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3081 if (force_nonblock && ret == -EAGAIN)
3082 return -EAGAIN;
3083 if (ret == -ERESTARTSYS)
3084 ret = -EINTR;
3085 }
3086
3087 io_cqring_add_event(req, ret);
3088 if (ret < 0)
3089 req_set_fail_links(req);
3090 io_put_req_find_next(req, nxt);
3091 return 0;
3092#else
3093 return -EOPNOTSUPP;
3094#endif
3095}
3096
3097
Jens Axboe3529d8c2019-12-19 18:24:38 -07003098static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003099{
3100#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003101 struct io_accept *accept = &req->accept;
3102
Jens Axboe17f2fe32019-10-17 14:42:58 -06003103 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3104 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003105 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003106 return -EINVAL;
3107
Jens Axboed55e5f52019-12-11 16:12:15 -07003108 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3109 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003110 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003111 return 0;
3112#else
3113 return -EOPNOTSUPP;
3114#endif
3115}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003116
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003117#if defined(CONFIG_NET)
3118static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3119 bool force_nonblock)
3120{
3121 struct io_accept *accept = &req->accept;
3122 unsigned file_flags;
3123 int ret;
3124
3125 file_flags = force_nonblock ? O_NONBLOCK : 0;
3126 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3127 accept->addr_len, accept->flags);
3128 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003129 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003130 if (ret == -ERESTARTSYS)
3131 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003132 if (ret < 0)
3133 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003134 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003135 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003136 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003137}
3138
3139static void io_accept_finish(struct io_wq_work **workptr)
3140{
3141 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3142 struct io_kiocb *nxt = NULL;
3143
3144 if (io_req_cancelled(req))
3145 return;
3146 __io_accept(req, &nxt, false);
3147 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003148 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003149}
3150#endif
3151
3152static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3153 bool force_nonblock)
3154{
3155#if defined(CONFIG_NET)
3156 int ret;
3157
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003158 ret = __io_accept(req, nxt, force_nonblock);
3159 if (ret == -EAGAIN && force_nonblock) {
3160 req->work.func = io_accept_finish;
3161 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3162 io_put_req(req);
3163 return -EAGAIN;
3164 }
3165 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003166#else
3167 return -EOPNOTSUPP;
3168#endif
3169}
3170
Jens Axboe3529d8c2019-12-19 18:24:38 -07003171static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003172{
3173#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003174 struct io_connect *conn = &req->connect;
3175 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003176
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003177 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3178 return -EINVAL;
3179 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3180 return -EINVAL;
3181
Jens Axboe3529d8c2019-12-19 18:24:38 -07003182 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3183 conn->addr_len = READ_ONCE(sqe->addr2);
3184
3185 if (!io)
3186 return 0;
3187
3188 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003189 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003190#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003191 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003192#endif
3193}
3194
Jens Axboefc4df992019-12-10 14:38:45 -07003195static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3196 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003197{
3198#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003199 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003200 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003201 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003202
Jens Axboef499a022019-12-02 16:28:46 -07003203 if (req->io) {
3204 io = req->io;
3205 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003206 ret = move_addr_to_kernel(req->connect.addr,
3207 req->connect.addr_len,
3208 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003209 if (ret)
3210 goto out;
3211 io = &__io;
3212 }
3213
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003214 file_flags = force_nonblock ? O_NONBLOCK : 0;
3215
3216 ret = __sys_connect_file(req->file, &io->connect.address,
3217 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003218 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003219 if (req->io)
3220 return -EAGAIN;
3221 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003222 ret = -ENOMEM;
3223 goto out;
3224 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003225 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003226 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003227 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003228 if (ret == -ERESTARTSYS)
3229 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003230out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003231 if (ret < 0)
3232 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003233 io_cqring_add_event(req, ret);
3234 io_put_req_find_next(req, nxt);
3235 return 0;
3236#else
3237 return -EOPNOTSUPP;
3238#endif
3239}
3240
Jens Axboe221c5eb2019-01-17 09:41:58 -07003241static void io_poll_remove_one(struct io_kiocb *req)
3242{
3243 struct io_poll_iocb *poll = &req->poll;
3244
3245 spin_lock(&poll->head->lock);
3246 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003247 if (!list_empty(&poll->wait.entry)) {
3248 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003249 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003250 }
3251 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003252 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003253}
3254
3255static void io_poll_remove_all(struct io_ring_ctx *ctx)
3256{
Jens Axboe78076bb2019-12-04 19:56:40 -07003257 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003258 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003259 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003260
3261 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003262 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3263 struct hlist_head *list;
3264
3265 list = &ctx->cancel_hash[i];
3266 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3267 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003268 }
3269 spin_unlock_irq(&ctx->completion_lock);
3270}
3271
Jens Axboe47f46762019-11-09 17:43:02 -07003272static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3273{
Jens Axboe78076bb2019-12-04 19:56:40 -07003274 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003275 struct io_kiocb *req;
3276
Jens Axboe78076bb2019-12-04 19:56:40 -07003277 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3278 hlist_for_each_entry(req, list, hash_node) {
3279 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003280 io_poll_remove_one(req);
3281 return 0;
3282 }
Jens Axboe47f46762019-11-09 17:43:02 -07003283 }
3284
3285 return -ENOENT;
3286}
3287
Jens Axboe3529d8c2019-12-19 18:24:38 -07003288static int io_poll_remove_prep(struct io_kiocb *req,
3289 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003290{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003291 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3292 return -EINVAL;
3293 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3294 sqe->poll_events)
3295 return -EINVAL;
3296
Jens Axboe0969e782019-12-17 18:40:57 -07003297 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003298 return 0;
3299}
3300
3301/*
3302 * Find a running poll command that matches one specified in sqe->addr,
3303 * and remove it if found.
3304 */
3305static int io_poll_remove(struct io_kiocb *req)
3306{
3307 struct io_ring_ctx *ctx = req->ctx;
3308 u64 addr;
3309 int ret;
3310
Jens Axboe0969e782019-12-17 18:40:57 -07003311 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003312 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003313 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003314 spin_unlock_irq(&ctx->completion_lock);
3315
Jens Axboe78e19bb2019-11-06 15:21:34 -07003316 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003317 if (ret < 0)
3318 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003319 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003320 return 0;
3321}
3322
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003323static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003324{
Jackie Liua197f662019-11-08 08:09:12 -07003325 struct io_ring_ctx *ctx = req->ctx;
3326
Jens Axboe8c838782019-03-12 15:48:16 -06003327 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003328 if (error)
3329 io_cqring_fill_event(req, error);
3330 else
3331 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003332 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003333}
3334
Jens Axboe561fb042019-10-24 07:25:42 -06003335static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003336{
Jens Axboe561fb042019-10-24 07:25:42 -06003337 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003338 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3339 struct io_poll_iocb *poll = &req->poll;
3340 struct poll_table_struct pt = { ._key = poll->events };
3341 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003342 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003343 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003344 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003345
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003346 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003347 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003348 ret = -ECANCELED;
3349 } else if (READ_ONCE(poll->canceled)) {
3350 ret = -ECANCELED;
3351 }
Jens Axboe561fb042019-10-24 07:25:42 -06003352
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003353 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003354 mask = vfs_poll(poll->file, &pt) & poll->events;
3355
3356 /*
3357 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3358 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3359 * synchronize with them. In the cancellation case the list_del_init
3360 * itself is not actually needed, but harmless so we keep it in to
3361 * avoid further branches in the fast path.
3362 */
3363 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003364 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003365 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003366 spin_unlock_irq(&ctx->completion_lock);
3367 return;
3368 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003369 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003370 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003371 spin_unlock_irq(&ctx->completion_lock);
3372
Jens Axboe8c838782019-03-12 15:48:16 -06003373 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003374
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003375 if (ret < 0)
3376 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003377 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003378 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003379 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003380}
3381
Jens Axboee94f1412019-12-19 12:06:02 -07003382static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3383{
Jens Axboee94f1412019-12-19 12:06:02 -07003384 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003385 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003386
Jens Axboec6ca97b302019-12-28 12:11:08 -07003387 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003388 spin_lock_irq(&ctx->completion_lock);
3389 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3390 hash_del(&req->hash_node);
3391 io_poll_complete(req, req->result, 0);
3392
Jens Axboe8237e042019-12-28 10:48:22 -07003393 if (refcount_dec_and_test(&req->refs) &&
3394 !io_req_multi_free(&rb, req)) {
3395 req->flags |= REQ_F_COMP_LOCKED;
3396 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003397 }
3398 }
3399 spin_unlock_irq(&ctx->completion_lock);
3400
3401 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003402 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003403}
3404
3405static void io_poll_flush(struct io_wq_work **workptr)
3406{
3407 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3408 struct llist_node *nodes;
3409
3410 nodes = llist_del_all(&req->ctx->poll_llist);
3411 if (nodes)
3412 __io_poll_flush(req->ctx, nodes);
3413}
3414
Jens Axboe221c5eb2019-01-17 09:41:58 -07003415static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3416 void *key)
3417{
Jens Axboee9444752019-11-26 15:02:04 -07003418 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003419 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3420 struct io_ring_ctx *ctx = req->ctx;
3421 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003422
3423 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003424 if (mask && !(mask & poll->events))
3425 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003426
Jens Axboe392edb42019-12-09 17:52:20 -07003427 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003428
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003429 /*
3430 * Run completion inline if we can. We're using trylock here because
3431 * we are violating the completion_lock -> poll wq lock ordering.
3432 * If we have a link timeout we're going to need the completion_lock
3433 * for finalizing the request, mark us as having grabbed that already.
3434 */
Jens Axboee94f1412019-12-19 12:06:02 -07003435 if (mask) {
3436 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003437
Jens Axboee94f1412019-12-19 12:06:02 -07003438 if (llist_empty(&ctx->poll_llist) &&
3439 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3440 hash_del(&req->hash_node);
3441 io_poll_complete(req, mask, 0);
3442 req->flags |= REQ_F_COMP_LOCKED;
3443 io_put_req(req);
3444 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3445
3446 io_cqring_ev_posted(ctx);
3447 req = NULL;
3448 } else {
3449 req->result = mask;
3450 req->llist_node.next = NULL;
3451 /* if the list wasn't empty, we're done */
3452 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3453 req = NULL;
3454 else
3455 req->work.func = io_poll_flush;
3456 }
Jens Axboe8c838782019-03-12 15:48:16 -06003457 }
Jens Axboee94f1412019-12-19 12:06:02 -07003458 if (req)
3459 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003460
Jens Axboe221c5eb2019-01-17 09:41:58 -07003461 return 1;
3462}
3463
3464struct io_poll_table {
3465 struct poll_table_struct pt;
3466 struct io_kiocb *req;
3467 int error;
3468};
3469
3470static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3471 struct poll_table_struct *p)
3472{
3473 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3474
3475 if (unlikely(pt->req->poll.head)) {
3476 pt->error = -EINVAL;
3477 return;
3478 }
3479
3480 pt->error = 0;
3481 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003482 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003483}
3484
Jens Axboeeac406c2019-11-14 12:09:58 -07003485static void io_poll_req_insert(struct io_kiocb *req)
3486{
3487 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003488 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003489
Jens Axboe78076bb2019-12-04 19:56:40 -07003490 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3491 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003492}
3493
Jens Axboe3529d8c2019-12-19 18:24:38 -07003494static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003495{
3496 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003497 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003498
3499 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3500 return -EINVAL;
3501 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3502 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003503 if (!poll->file)
3504 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003505
Jens Axboe221c5eb2019-01-17 09:41:58 -07003506 events = READ_ONCE(sqe->poll_events);
3507 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003508 return 0;
3509}
3510
3511static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3512{
3513 struct io_poll_iocb *poll = &req->poll;
3514 struct io_ring_ctx *ctx = req->ctx;
3515 struct io_poll_table ipt;
3516 bool cancel = false;
3517 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003518
3519 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003520 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003521
Jens Axboe221c5eb2019-01-17 09:41:58 -07003522 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003523 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003524 poll->canceled = false;
3525
3526 ipt.pt._qproc = io_poll_queue_proc;
3527 ipt.pt._key = poll->events;
3528 ipt.req = req;
3529 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3530
3531 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003532 INIT_LIST_HEAD(&poll->wait.entry);
3533 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3534 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003535
Jens Axboe36703242019-07-25 10:20:18 -06003536 INIT_LIST_HEAD(&req->list);
3537
Jens Axboe221c5eb2019-01-17 09:41:58 -07003538 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003539
3540 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003541 if (likely(poll->head)) {
3542 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003543 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003544 if (ipt.error)
3545 cancel = true;
3546 ipt.error = 0;
3547 mask = 0;
3548 }
3549 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003550 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003551 else if (cancel)
3552 WRITE_ONCE(poll->canceled, true);
3553 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003554 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003555 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003556 }
Jens Axboe8c838782019-03-12 15:48:16 -06003557 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003558 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003559 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003560 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003561 spin_unlock_irq(&ctx->completion_lock);
3562
Jens Axboe8c838782019-03-12 15:48:16 -06003563 if (mask) {
3564 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003565 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003566 }
Jens Axboe8c838782019-03-12 15:48:16 -06003567 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003568}
3569
Jens Axboe5262f562019-09-17 12:26:57 -06003570static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3571{
Jens Axboead8a48a2019-11-15 08:49:11 -07003572 struct io_timeout_data *data = container_of(timer,
3573 struct io_timeout_data, timer);
3574 struct io_kiocb *req = data->req;
3575 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003576 unsigned long flags;
3577
Jens Axboe5262f562019-09-17 12:26:57 -06003578 atomic_inc(&ctx->cq_timeouts);
3579
3580 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003581 /*
Jens Axboe11365042019-10-16 09:08:32 -06003582 * We could be racing with timeout deletion. If the list is empty,
3583 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003584 */
Jens Axboe842f9612019-10-29 12:34:10 -06003585 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003586 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003587
Jens Axboe11365042019-10-16 09:08:32 -06003588 /*
3589 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003590 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003591 * pointer will be increased, otherwise other timeout reqs may
3592 * return in advance without waiting for enough wait_nr.
3593 */
3594 prev = req;
3595 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3596 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003597 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003598 }
Jens Axboe842f9612019-10-29 12:34:10 -06003599
Jens Axboe78e19bb2019-11-06 15:21:34 -07003600 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003601 io_commit_cqring(ctx);
3602 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3603
3604 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003605 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003606 io_put_req(req);
3607 return HRTIMER_NORESTART;
3608}
3609
Jens Axboe47f46762019-11-09 17:43:02 -07003610static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3611{
3612 struct io_kiocb *req;
3613 int ret = -ENOENT;
3614
3615 list_for_each_entry(req, &ctx->timeout_list, list) {
3616 if (user_data == req->user_data) {
3617 list_del_init(&req->list);
3618 ret = 0;
3619 break;
3620 }
3621 }
3622
3623 if (ret == -ENOENT)
3624 return ret;
3625
Jens Axboe2d283902019-12-04 11:08:05 -07003626 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003627 if (ret == -1)
3628 return -EALREADY;
3629
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003630 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003631 io_cqring_fill_event(req, -ECANCELED);
3632 io_put_req(req);
3633 return 0;
3634}
3635
Jens Axboe3529d8c2019-12-19 18:24:38 -07003636static int io_timeout_remove_prep(struct io_kiocb *req,
3637 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003638{
Jens Axboeb29472e2019-12-17 18:50:29 -07003639 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3640 return -EINVAL;
3641 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3642 return -EINVAL;
3643
3644 req->timeout.addr = READ_ONCE(sqe->addr);
3645 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3646 if (req->timeout.flags)
3647 return -EINVAL;
3648
Jens Axboeb29472e2019-12-17 18:50:29 -07003649 return 0;
3650}
3651
Jens Axboe11365042019-10-16 09:08:32 -06003652/*
3653 * Remove or update an existing timeout command
3654 */
Jens Axboefc4df992019-12-10 14:38:45 -07003655static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003656{
3657 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003658 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003659
Jens Axboe11365042019-10-16 09:08:32 -06003660 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003661 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003662
Jens Axboe47f46762019-11-09 17:43:02 -07003663 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003664 io_commit_cqring(ctx);
3665 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003666 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003667 if (ret < 0)
3668 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003669 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003670 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003671}
3672
Jens Axboe3529d8c2019-12-19 18:24:38 -07003673static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003674 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003675{
Jens Axboead8a48a2019-11-15 08:49:11 -07003676 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003677 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003678
Jens Axboead8a48a2019-11-15 08:49:11 -07003679 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003680 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003681 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003682 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003683 if (sqe->off && is_timeout_link)
3684 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003685 flags = READ_ONCE(sqe->timeout_flags);
3686 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003687 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003688
Jens Axboe26a61672019-12-20 09:02:01 -07003689 req->timeout.count = READ_ONCE(sqe->off);
3690
Jens Axboe3529d8c2019-12-19 18:24:38 -07003691 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003692 return -ENOMEM;
3693
3694 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003695 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003696 req->flags |= REQ_F_TIMEOUT;
3697
3698 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003699 return -EFAULT;
3700
Jens Axboe11365042019-10-16 09:08:32 -06003701 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003702 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003703 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003704 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003705
Jens Axboead8a48a2019-11-15 08:49:11 -07003706 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3707 return 0;
3708}
3709
Jens Axboefc4df992019-12-10 14:38:45 -07003710static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003711{
3712 unsigned count;
3713 struct io_ring_ctx *ctx = req->ctx;
3714 struct io_timeout_data *data;
3715 struct list_head *entry;
3716 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003717
Jens Axboe2d283902019-12-04 11:08:05 -07003718 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003719
Jens Axboe5262f562019-09-17 12:26:57 -06003720 /*
3721 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003722 * timeout event to be satisfied. If it isn't set, then this is
3723 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003724 */
Jens Axboe26a61672019-12-20 09:02:01 -07003725 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003726 if (!count) {
3727 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3728 spin_lock_irq(&ctx->completion_lock);
3729 entry = ctx->timeout_list.prev;
3730 goto add;
3731 }
Jens Axboe5262f562019-09-17 12:26:57 -06003732
3733 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003734 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003735
3736 /*
3737 * Insertion sort, ensuring the first entry in the list is always
3738 * the one we need first.
3739 */
Jens Axboe5262f562019-09-17 12:26:57 -06003740 spin_lock_irq(&ctx->completion_lock);
3741 list_for_each_prev(entry, &ctx->timeout_list) {
3742 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003743 unsigned nxt_sq_head;
3744 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003745 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003746
Jens Axboe93bd25b2019-11-11 23:34:31 -07003747 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3748 continue;
3749
yangerkun5da0fb12019-10-15 21:59:29 +08003750 /*
3751 * Since cached_sq_head + count - 1 can overflow, use type long
3752 * long to store it.
3753 */
3754 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003755 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3756 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003757
3758 /*
3759 * cached_sq_head may overflow, and it will never overflow twice
3760 * once there is some timeout req still be valid.
3761 */
3762 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003763 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003764
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003765 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003766 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003767
3768 /*
3769 * Sequence of reqs after the insert one and itself should
3770 * be adjusted because each timeout req consumes a slot.
3771 */
3772 span++;
3773 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003774 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003775 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003776add:
Jens Axboe5262f562019-09-17 12:26:57 -06003777 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003778 data->timer.function = io_timeout_fn;
3779 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003780 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003781 return 0;
3782}
3783
Jens Axboe62755e32019-10-28 21:49:21 -06003784static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003785{
Jens Axboe62755e32019-10-28 21:49:21 -06003786 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003787
Jens Axboe62755e32019-10-28 21:49:21 -06003788 return req->user_data == (unsigned long) data;
3789}
3790
Jens Axboee977d6d2019-11-05 12:39:45 -07003791static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003792{
Jens Axboe62755e32019-10-28 21:49:21 -06003793 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003794 int ret = 0;
3795
Jens Axboe62755e32019-10-28 21:49:21 -06003796 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3797 switch (cancel_ret) {
3798 case IO_WQ_CANCEL_OK:
3799 ret = 0;
3800 break;
3801 case IO_WQ_CANCEL_RUNNING:
3802 ret = -EALREADY;
3803 break;
3804 case IO_WQ_CANCEL_NOTFOUND:
3805 ret = -ENOENT;
3806 break;
3807 }
3808
Jens Axboee977d6d2019-11-05 12:39:45 -07003809 return ret;
3810}
3811
Jens Axboe47f46762019-11-09 17:43:02 -07003812static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3813 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003814 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003815{
3816 unsigned long flags;
3817 int ret;
3818
3819 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3820 if (ret != -ENOENT) {
3821 spin_lock_irqsave(&ctx->completion_lock, flags);
3822 goto done;
3823 }
3824
3825 spin_lock_irqsave(&ctx->completion_lock, flags);
3826 ret = io_timeout_cancel(ctx, sqe_addr);
3827 if (ret != -ENOENT)
3828 goto done;
3829 ret = io_poll_cancel(ctx, sqe_addr);
3830done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003831 if (!ret)
3832 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003833 io_cqring_fill_event(req, ret);
3834 io_commit_cqring(ctx);
3835 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3836 io_cqring_ev_posted(ctx);
3837
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003838 if (ret < 0)
3839 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003840 io_put_req_find_next(req, nxt);
3841}
3842
Jens Axboe3529d8c2019-12-19 18:24:38 -07003843static int io_async_cancel_prep(struct io_kiocb *req,
3844 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003845{
Jens Axboefbf23842019-12-17 18:45:56 -07003846 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003847 return -EINVAL;
3848 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3849 sqe->cancel_flags)
3850 return -EINVAL;
3851
Jens Axboefbf23842019-12-17 18:45:56 -07003852 req->cancel.addr = READ_ONCE(sqe->addr);
3853 return 0;
3854}
3855
3856static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3857{
3858 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003859
3860 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003861 return 0;
3862}
3863
Jens Axboe05f3fb32019-12-09 11:22:50 -07003864static int io_files_update_prep(struct io_kiocb *req,
3865 const struct io_uring_sqe *sqe)
3866{
3867 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3868 return -EINVAL;
3869
3870 req->files_update.offset = READ_ONCE(sqe->off);
3871 req->files_update.nr_args = READ_ONCE(sqe->len);
3872 if (!req->files_update.nr_args)
3873 return -EINVAL;
3874 req->files_update.arg = READ_ONCE(sqe->addr);
3875 return 0;
3876}
3877
3878static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3879{
3880 struct io_ring_ctx *ctx = req->ctx;
3881 struct io_uring_files_update up;
3882 int ret;
3883
3884 if (force_nonblock) {
3885 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3886 return -EAGAIN;
3887 }
3888
3889 up.offset = req->files_update.offset;
3890 up.fds = req->files_update.arg;
3891
3892 mutex_lock(&ctx->uring_lock);
3893 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3894 mutex_unlock(&ctx->uring_lock);
3895
3896 if (ret < 0)
3897 req_set_fail_links(req);
3898 io_cqring_add_event(req, ret);
3899 io_put_req(req);
3900 return 0;
3901}
3902
Jens Axboe3529d8c2019-12-19 18:24:38 -07003903static int io_req_defer_prep(struct io_kiocb *req,
3904 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003905{
Jens Axboee7815732019-12-17 19:45:06 -07003906 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003907
Jens Axboed625c6e2019-12-17 19:53:05 -07003908 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003909 case IORING_OP_NOP:
3910 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003911 case IORING_OP_READV:
3912 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003913 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003914 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003915 break;
3916 case IORING_OP_WRITEV:
3917 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003918 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003919 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003920 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003921 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003922 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003923 break;
3924 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003925 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003926 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003927 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003928 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003929 break;
3930 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003931 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003932 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003933 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003934 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003935 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003936 break;
3937 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003938 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003939 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003940 break;
Jens Axboef499a022019-12-02 16:28:46 -07003941 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003942 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003943 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003944 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003945 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003946 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003947 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003948 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003949 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003950 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003951 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003952 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003953 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003954 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003955 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003956 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003957 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003958 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003959 case IORING_OP_FALLOCATE:
3960 ret = io_fallocate_prep(req, sqe);
3961 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003962 case IORING_OP_OPENAT:
3963 ret = io_openat_prep(req, sqe);
3964 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003965 case IORING_OP_CLOSE:
3966 ret = io_close_prep(req, sqe);
3967 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003968 case IORING_OP_FILES_UPDATE:
3969 ret = io_files_update_prep(req, sqe);
3970 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003971 case IORING_OP_STATX:
3972 ret = io_statx_prep(req, sqe);
3973 break;
Jens Axboe4840e412019-12-25 22:03:45 -07003974 case IORING_OP_FADVISE:
3975 ret = io_fadvise_prep(req, sqe);
3976 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07003977 case IORING_OP_MADVISE:
3978 ret = io_madvise_prep(req, sqe);
3979 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003980 default:
Jens Axboee7815732019-12-17 19:45:06 -07003981 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3982 req->opcode);
3983 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003984 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003985 }
3986
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003987 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003988}
3989
Jens Axboe3529d8c2019-12-19 18:24:38 -07003990static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003991{
Jackie Liua197f662019-11-08 08:09:12 -07003992 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003993 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003994
Bob Liu9d858b22019-11-13 18:06:25 +08003995 /* Still need defer if there is pending req in defer list. */
3996 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003997 return 0;
3998
Jens Axboe3529d8c2019-12-19 18:24:38 -07003999 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004000 return -EAGAIN;
4001
Jens Axboe3529d8c2019-12-19 18:24:38 -07004002 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004003 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004004 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004005
Jens Axboede0617e2019-04-06 21:51:27 -06004006 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004007 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004008 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004009 return 0;
4010 }
4011
Jens Axboe915967f2019-11-21 09:01:20 -07004012 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004013 list_add_tail(&req->list, &ctx->defer_list);
4014 spin_unlock_irq(&ctx->completion_lock);
4015 return -EIOCBQUEUED;
4016}
4017
Jens Axboe3529d8c2019-12-19 18:24:38 -07004018static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4019 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004020{
Jackie Liua197f662019-11-08 08:09:12 -07004021 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004022 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004023
Jens Axboed625c6e2019-12-17 19:53:05 -07004024 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004025 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004026 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004027 break;
4028 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004029 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004030 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004031 if (sqe) {
4032 ret = io_read_prep(req, sqe, force_nonblock);
4033 if (ret < 0)
4034 break;
4035 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004036 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004037 break;
4038 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004039 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004040 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004041 if (sqe) {
4042 ret = io_write_prep(req, sqe, force_nonblock);
4043 if (ret < 0)
4044 break;
4045 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004046 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004047 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004048 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004049 if (sqe) {
4050 ret = io_prep_fsync(req, sqe);
4051 if (ret < 0)
4052 break;
4053 }
Jens Axboefc4df992019-12-10 14:38:45 -07004054 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004055 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004056 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004057 if (sqe) {
4058 ret = io_poll_add_prep(req, sqe);
4059 if (ret)
4060 break;
4061 }
Jens Axboefc4df992019-12-10 14:38:45 -07004062 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004063 break;
4064 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004065 if (sqe) {
4066 ret = io_poll_remove_prep(req, sqe);
4067 if (ret < 0)
4068 break;
4069 }
Jens Axboefc4df992019-12-10 14:38:45 -07004070 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004071 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004072 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004073 if (sqe) {
4074 ret = io_prep_sfr(req, sqe);
4075 if (ret < 0)
4076 break;
4077 }
Jens Axboefc4df992019-12-10 14:38:45 -07004078 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004079 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004080 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004081 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004082 if (sqe) {
4083 ret = io_sendmsg_prep(req, sqe);
4084 if (ret < 0)
4085 break;
4086 }
Jens Axboefddafac2020-01-04 20:19:44 -07004087 if (req->opcode == IORING_OP_SENDMSG)
4088 ret = io_sendmsg(req, nxt, force_nonblock);
4089 else
4090 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004091 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004092 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004093 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004094 if (sqe) {
4095 ret = io_recvmsg_prep(req, sqe);
4096 if (ret)
4097 break;
4098 }
Jens Axboefddafac2020-01-04 20:19:44 -07004099 if (req->opcode == IORING_OP_RECVMSG)
4100 ret = io_recvmsg(req, nxt, force_nonblock);
4101 else
4102 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004103 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004104 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004105 if (sqe) {
4106 ret = io_timeout_prep(req, sqe, false);
4107 if (ret)
4108 break;
4109 }
Jens Axboefc4df992019-12-10 14:38:45 -07004110 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004111 break;
Jens Axboe11365042019-10-16 09:08:32 -06004112 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004113 if (sqe) {
4114 ret = io_timeout_remove_prep(req, sqe);
4115 if (ret)
4116 break;
4117 }
Jens Axboefc4df992019-12-10 14:38:45 -07004118 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004119 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004120 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004121 if (sqe) {
4122 ret = io_accept_prep(req, sqe);
4123 if (ret)
4124 break;
4125 }
Jens Axboefc4df992019-12-10 14:38:45 -07004126 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004127 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004128 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004129 if (sqe) {
4130 ret = io_connect_prep(req, sqe);
4131 if (ret)
4132 break;
4133 }
Jens Axboefc4df992019-12-10 14:38:45 -07004134 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004135 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004136 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004137 if (sqe) {
4138 ret = io_async_cancel_prep(req, sqe);
4139 if (ret)
4140 break;
4141 }
Jens Axboefc4df992019-12-10 14:38:45 -07004142 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004143 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004144 case IORING_OP_FALLOCATE:
4145 if (sqe) {
4146 ret = io_fallocate_prep(req, sqe);
4147 if (ret)
4148 break;
4149 }
4150 ret = io_fallocate(req, nxt, force_nonblock);
4151 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004152 case IORING_OP_OPENAT:
4153 if (sqe) {
4154 ret = io_openat_prep(req, sqe);
4155 if (ret)
4156 break;
4157 }
4158 ret = io_openat(req, nxt, force_nonblock);
4159 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004160 case IORING_OP_CLOSE:
4161 if (sqe) {
4162 ret = io_close_prep(req, sqe);
4163 if (ret)
4164 break;
4165 }
4166 ret = io_close(req, nxt, force_nonblock);
4167 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004168 case IORING_OP_FILES_UPDATE:
4169 if (sqe) {
4170 ret = io_files_update_prep(req, sqe);
4171 if (ret)
4172 break;
4173 }
4174 ret = io_files_update(req, force_nonblock);
4175 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004176 case IORING_OP_STATX:
4177 if (sqe) {
4178 ret = io_statx_prep(req, sqe);
4179 if (ret)
4180 break;
4181 }
4182 ret = io_statx(req, nxt, force_nonblock);
4183 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004184 case IORING_OP_FADVISE:
4185 if (sqe) {
4186 ret = io_fadvise_prep(req, sqe);
4187 if (ret)
4188 break;
4189 }
4190 ret = io_fadvise(req, nxt, force_nonblock);
4191 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004192 case IORING_OP_MADVISE:
4193 if (sqe) {
4194 ret = io_madvise_prep(req, sqe);
4195 if (ret)
4196 break;
4197 }
4198 ret = io_madvise(req, nxt, force_nonblock);
4199 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004200 default:
4201 ret = -EINVAL;
4202 break;
4203 }
4204
Jens Axboedef596e2019-01-09 08:59:42 -07004205 if (ret)
4206 return ret;
4207
4208 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004209 const bool in_async = io_wq_current_is_worker();
4210
Jens Axboe9e645e112019-05-10 16:07:28 -06004211 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004212 return -EAGAIN;
4213
Jens Axboe11ba8202020-01-15 21:51:17 -07004214 /* workqueue context doesn't hold uring_lock, grab it now */
4215 if (in_async)
4216 mutex_lock(&ctx->uring_lock);
4217
Jens Axboedef596e2019-01-09 08:59:42 -07004218 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004219
4220 if (in_async)
4221 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004222 }
4223
4224 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004225}
4226
Jens Axboe561fb042019-10-24 07:25:42 -06004227static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004228{
Jens Axboe561fb042019-10-24 07:25:42 -06004229 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004230 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004231 struct io_kiocb *nxt = NULL;
4232 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004233
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004234 /* if NO_CANCEL is set, we must still run the work */
4235 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4236 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004237 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004238 }
Jens Axboe31b51512019-01-18 22:56:34 -07004239
Jens Axboe561fb042019-10-24 07:25:42 -06004240 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004241 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4242 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004243 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004244 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004245 /*
4246 * We can get EAGAIN for polled IO even though we're
4247 * forcing a sync submission from here, since we can't
4248 * wait for request slots on the block side.
4249 */
4250 if (ret != -EAGAIN)
4251 break;
4252 cond_resched();
4253 } while (1);
4254 }
Jens Axboe31b51512019-01-18 22:56:34 -07004255
Jens Axboe561fb042019-10-24 07:25:42 -06004256 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004257 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004258
Jens Axboe561fb042019-10-24 07:25:42 -06004259 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004260 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004261 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004262 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004263 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004264
Jens Axboe561fb042019-10-24 07:25:42 -06004265 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004266 if (!ret && nxt)
4267 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004268}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004269
Jens Axboe15b71ab2019-12-11 11:20:36 -07004270static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004271{
Jens Axboed3656342019-12-18 09:50:26 -07004272 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004273 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004274 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4275 return 0;
4276 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004277}
4278
Jens Axboe65e19f52019-10-26 07:20:21 -06004279static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4280 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004281{
Jens Axboe65e19f52019-10-26 07:20:21 -06004282 struct fixed_file_table *table;
4283
Jens Axboe05f3fb32019-12-09 11:22:50 -07004284 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4285 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004286}
4287
Jens Axboe3529d8c2019-12-19 18:24:38 -07004288static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4289 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004290{
Jackie Liua197f662019-11-08 08:09:12 -07004291 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004292 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004293 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004294
Jens Axboe3529d8c2019-12-19 18:24:38 -07004295 flags = READ_ONCE(sqe->flags);
4296 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004297
Jackie Liu4fe2c962019-09-09 20:50:40 +08004298 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06004299 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06004300
Jens Axboed3656342019-12-18 09:50:26 -07004301 if (!io_req_needs_file(req, fd))
4302 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004303
4304 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004305 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004306 (unsigned) fd >= ctx->nr_user_files))
4307 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004308 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004309 req->file = io_file_from_index(ctx, fd);
4310 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004311 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004312 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004313 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004314 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004315 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004316 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004317 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004318 req->file = io_file_get(state, fd);
4319 if (unlikely(!req->file))
4320 return -EBADF;
4321 }
4322
4323 return 0;
4324}
4325
Jackie Liua197f662019-11-08 08:09:12 -07004326static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004327{
Jens Axboefcb323c2019-10-24 12:39:47 -06004328 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004329 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004330
Jens Axboeb5dba592019-12-11 14:02:38 -07004331 if (!req->ring_file)
4332 return -EBADF;
4333
Jens Axboefcb323c2019-10-24 12:39:47 -06004334 rcu_read_lock();
4335 spin_lock_irq(&ctx->inflight_lock);
4336 /*
4337 * We use the f_ops->flush() handler to ensure that we can flush
4338 * out work accessing these files if the fd is closed. Check if
4339 * the fd has changed since we started down this path, and disallow
4340 * this operation if it has.
4341 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004342 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004343 list_add(&req->inflight_entry, &ctx->inflight_list);
4344 req->flags |= REQ_F_INFLIGHT;
4345 req->work.files = current->files;
4346 ret = 0;
4347 }
4348 spin_unlock_irq(&ctx->inflight_lock);
4349 rcu_read_unlock();
4350
4351 return ret;
4352}
4353
Jens Axboe2665abf2019-11-05 12:40:47 -07004354static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4355{
Jens Axboead8a48a2019-11-15 08:49:11 -07004356 struct io_timeout_data *data = container_of(timer,
4357 struct io_timeout_data, timer);
4358 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004359 struct io_ring_ctx *ctx = req->ctx;
4360 struct io_kiocb *prev = NULL;
4361 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004362
4363 spin_lock_irqsave(&ctx->completion_lock, flags);
4364
4365 /*
4366 * We don't expect the list to be empty, that will only happen if we
4367 * race with the completion of the linked work.
4368 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004369 if (!list_empty(&req->link_list)) {
4370 prev = list_entry(req->link_list.prev, struct io_kiocb,
4371 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004372 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004373 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004374 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4375 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004376 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004377 }
4378
4379 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4380
4381 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004382 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004383 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4384 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004385 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004386 } else {
4387 io_cqring_add_event(req, -ETIME);
4388 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004389 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004390 return HRTIMER_NORESTART;
4391}
4392
Jens Axboead8a48a2019-11-15 08:49:11 -07004393static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004394{
Jens Axboe76a46e02019-11-10 23:34:16 -07004395 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004396
Jens Axboe76a46e02019-11-10 23:34:16 -07004397 /*
4398 * If the list is now empty, then our linked request finished before
4399 * we got a chance to setup the timer
4400 */
4401 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004402 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004403 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004404
Jens Axboead8a48a2019-11-15 08:49:11 -07004405 data->timer.function = io_link_timeout_fn;
4406 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4407 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004408 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004409 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004410
Jens Axboe2665abf2019-11-05 12:40:47 -07004411 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004412 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004413}
4414
Jens Axboead8a48a2019-11-15 08:49:11 -07004415static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004416{
4417 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004418
Jens Axboe2665abf2019-11-05 12:40:47 -07004419 if (!(req->flags & REQ_F_LINK))
4420 return NULL;
4421
Pavel Begunkov44932332019-12-05 16:16:35 +03004422 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4423 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004424 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004425 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004426
Jens Axboe76a46e02019-11-10 23:34:16 -07004427 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004428 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004429}
4430
Jens Axboe3529d8c2019-12-19 18:24:38 -07004431static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004432{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004433 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004434 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004435 int ret;
4436
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004437again:
4438 linked_timeout = io_prep_linked_timeout(req);
4439
Jens Axboe3529d8c2019-12-19 18:24:38 -07004440 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004441
4442 /*
4443 * We async punt it if the file wasn't marked NOWAIT, or if the file
4444 * doesn't support non-blocking read/write attempts
4445 */
4446 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4447 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004448 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4449 ret = io_grab_files(req);
4450 if (ret)
4451 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004452 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004453
4454 /*
4455 * Queued up for async execution, worker will release
4456 * submit reference when the iocb is actually submitted.
4457 */
4458 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004459 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004460 }
Jens Axboee65ef562019-03-12 10:16:44 -06004461
Jens Axboefcb323c2019-10-24 12:39:47 -06004462err:
Jens Axboee65ef562019-03-12 10:16:44 -06004463 /* drop submission reference */
4464 io_put_req(req);
4465
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004466 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004467 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004468 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004469 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004470 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004471 }
4472
Jens Axboee65ef562019-03-12 10:16:44 -06004473 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004474 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004475 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004476 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004477 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004478 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004479done_req:
4480 if (nxt) {
4481 req = nxt;
4482 nxt = NULL;
4483 goto again;
4484 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004485}
4486
Jens Axboe3529d8c2019-12-19 18:24:38 -07004487static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004488{
4489 int ret;
4490
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004491 if (unlikely(req->ctx->drain_next)) {
4492 req->flags |= REQ_F_IO_DRAIN;
4493 req->ctx->drain_next = false;
4494 }
4495 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
4496
Jens Axboe3529d8c2019-12-19 18:24:38 -07004497 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004498 if (ret) {
4499 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004500 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004501 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004502 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004503 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004504 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboece35a472019-12-17 08:04:44 -07004505 /*
4506 * Never try inline submit of IOSQE_ASYNC is set, go straight
4507 * to async execution.
4508 */
4509 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4510 io_queue_async_work(req);
4511 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004512 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004513 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004514}
4515
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004516static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004517{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004518 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004519 io_cqring_add_event(req, -ECANCELED);
4520 io_double_put_req(req);
4521 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004522 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004523}
4524
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004525#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004526 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004527
Jens Axboe3529d8c2019-12-19 18:24:38 -07004528static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4529 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004530{
Jackie Liua197f662019-11-08 08:09:12 -07004531 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004532 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004533 int ret;
4534
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004535 sqe_flags = READ_ONCE(sqe->flags);
4536
Jens Axboe9e645e112019-05-10 16:07:28 -06004537 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004538 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004539 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004540 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004541 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004542 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004543 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004544
Jens Axboe3529d8c2019-12-19 18:24:38 -07004545 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004546 if (unlikely(ret)) {
4547err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004548 io_cqring_add_event(req, ret);
4549 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004550 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004551 }
4552
Jens Axboe9e645e112019-05-10 16:07:28 -06004553 /*
4554 * If we already have a head request, queue this one for async
4555 * submittal once the head completes. If we don't have a head but
4556 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4557 * submitted sync once the chain is complete. If none of those
4558 * conditions are true (normal request), then just queue it.
4559 */
4560 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004561 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004562
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004563 if (sqe_flags & IOSQE_IO_DRAIN)
Pavel Begunkov9d763772019-12-17 02:22:07 +03004564 head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004565
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004566 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004567 req->flags |= REQ_F_HARDLINK;
4568
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004569 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004570 ret = -EAGAIN;
4571 goto err_req;
4572 }
4573
Jens Axboe3529d8c2019-12-19 18:24:38 -07004574 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004575 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004576 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004577 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004578 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004579 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004580 trace_io_uring_link(ctx, req, head);
4581 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004582
4583 /* last request of a link, enqueue the link */
4584 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4585 io_queue_link_head(head);
4586 *link = NULL;
4587 }
4588 } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004589 req->flags |= REQ_F_LINK;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004590 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004591 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004592
Jens Axboe9e645e112019-05-10 16:07:28 -06004593 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004594 ret = io_req_defer_prep(req, sqe);
4595 if (ret)
4596 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004597 *link = req;
4598 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004599 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004600 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004601
4602 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004603}
4604
Jens Axboe9a56a232019-01-09 09:06:50 -07004605/*
4606 * Batched submission is done, ensure local IO is flushed out.
4607 */
4608static void io_submit_state_end(struct io_submit_state *state)
4609{
4610 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004611 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004612 if (state->free_reqs)
4613 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4614 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004615}
4616
4617/*
4618 * Start submission side cache.
4619 */
4620static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004621 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004622{
4623 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004624 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004625 state->file = NULL;
4626 state->ios_left = max_ios;
4627}
4628
Jens Axboe2b188cc2019-01-07 10:46:33 -07004629static void io_commit_sqring(struct io_ring_ctx *ctx)
4630{
Hristo Venev75b28af2019-08-26 17:23:46 +00004631 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004632
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004633 /*
4634 * Ensure any loads from the SQEs are done at this point,
4635 * since once we write the new head, the application could
4636 * write new data to them.
4637 */
4638 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004639}
4640
4641/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004642 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004643 * that is mapped by userspace. This means that care needs to be taken to
4644 * ensure that reads are stable, as we cannot rely on userspace always
4645 * being a good citizen. If members of the sqe are validated and then later
4646 * used, it's important that those reads are done through READ_ONCE() to
4647 * prevent a re-load down the line.
4648 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004649static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4650 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004651{
Hristo Venev75b28af2019-08-26 17:23:46 +00004652 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004653 unsigned head;
4654
4655 /*
4656 * The cached sq head (or cq tail) serves two purposes:
4657 *
4658 * 1) allows us to batch the cost of updating the user visible
4659 * head updates.
4660 * 2) allows the kernel side to track the head on its own, even
4661 * though the application is the one updating it.
4662 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004663 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004664 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004665 /*
4666 * All io need record the previous position, if LINK vs DARIN,
4667 * it can be used to mark the position of the first IO in the
4668 * link list.
4669 */
4670 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004671 *sqe_ptr = &ctx->sq_sqes[head];
4672 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4673 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004674 ctx->cached_sq_head++;
4675 return true;
4676 }
4677
4678 /* drop invalid entries */
4679 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004680 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004681 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004682 return false;
4683}
4684
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004685static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004686 struct file *ring_file, int ring_fd,
4687 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004688{
4689 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004690 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004691 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004692 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004693
Jens Axboec4a2ed72019-11-21 21:01:26 -07004694 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004695 if (test_bit(0, &ctx->sq_check_overflow)) {
4696 if (!list_empty(&ctx->cq_overflow_list) &&
4697 !io_cqring_overflow_flush(ctx, false))
4698 return -EBUSY;
4699 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004700
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004701 /* make sure SQ entry isn't read before tail */
4702 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004703
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004704 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4705 return -EAGAIN;
4706
Jens Axboe6c271ce2019-01-10 11:22:30 -07004707 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004708 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004709 statep = &state;
4710 }
4711
4712 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004713 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004714 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004715
Pavel Begunkov196be952019-11-07 01:41:06 +03004716 req = io_get_req(ctx, statep);
4717 if (unlikely(!req)) {
4718 if (!submitted)
4719 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004720 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004721 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004722 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004723 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004724 break;
4725 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004726
Jens Axboed3656342019-12-18 09:50:26 -07004727 /* will complete beyond this point, count as submitted */
4728 submitted++;
4729
4730 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4731 io_cqring_add_event(req, -EINVAL);
4732 io_double_put_req(req);
4733 break;
4734 }
4735
4736 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004737 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4738 if (!mm_fault) {
4739 use_mm(ctx->sqo_mm);
4740 *mm = ctx->sqo_mm;
4741 }
4742 }
4743
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004744 req->ring_file = ring_file;
4745 req->ring_fd = ring_fd;
4746 req->has_user = *mm != NULL;
4747 req->in_async = async;
4748 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07004749 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004750 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004751 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004752 }
4753
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004754 if (submitted != nr)
4755 percpu_ref_put_many(&ctx->refs, nr - submitted);
Jens Axboe9e645e112019-05-10 16:07:28 -06004756 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004757 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004758 if (statep)
4759 io_submit_state_end(&state);
4760
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004761 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4762 io_commit_sqring(ctx);
4763
Jens Axboe6c271ce2019-01-10 11:22:30 -07004764 return submitted;
4765}
4766
4767static int io_sq_thread(void *data)
4768{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004769 struct io_ring_ctx *ctx = data;
4770 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004771 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004772 mm_segment_t old_fs;
4773 DEFINE_WAIT(wait);
4774 unsigned inflight;
4775 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004776 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004777
Jens Axboe206aefd2019-11-07 18:27:42 -07004778 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004779
Jens Axboe6c271ce2019-01-10 11:22:30 -07004780 old_fs = get_fs();
4781 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004782 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004783
Jens Axboec1edbf52019-11-10 16:56:04 -07004784 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004785 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004786 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004787
4788 if (inflight) {
4789 unsigned nr_events = 0;
4790
4791 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004792 /*
4793 * inflight is the count of the maximum possible
4794 * entries we submitted, but it can be smaller
4795 * if we dropped some of them. If we don't have
4796 * poll entries available, then we know that we
4797 * have nothing left to poll for. Reset the
4798 * inflight count to zero in that case.
4799 */
4800 mutex_lock(&ctx->uring_lock);
4801 if (!list_empty(&ctx->poll_list))
4802 __io_iopoll_check(ctx, &nr_events, 0);
4803 else
4804 inflight = 0;
4805 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004806 } else {
4807 /*
4808 * Normal IO, just pretend everything completed.
4809 * We don't have to poll completions for that.
4810 */
4811 nr_events = inflight;
4812 }
4813
4814 inflight -= nr_events;
4815 if (!inflight)
4816 timeout = jiffies + ctx->sq_thread_idle;
4817 }
4818
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004819 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004820
4821 /*
4822 * If submit got -EBUSY, flag us as needing the application
4823 * to enter the kernel to reap and flush events.
4824 */
4825 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004826 /*
4827 * We're polling. If we're within the defined idle
4828 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004829 * to sleep. The exception is if we got EBUSY doing
4830 * more IO, we should wait for the application to
4831 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004832 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004833 if (inflight ||
4834 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004835 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004836 continue;
4837 }
4838
4839 /*
4840 * Drop cur_mm before scheduling, we can't hold it for
4841 * long periods (or over schedule()). Do this before
4842 * adding ourselves to the waitqueue, as the unuse/drop
4843 * may sleep.
4844 */
4845 if (cur_mm) {
4846 unuse_mm(cur_mm);
4847 mmput(cur_mm);
4848 cur_mm = NULL;
4849 }
4850
4851 prepare_to_wait(&ctx->sqo_wait, &wait,
4852 TASK_INTERRUPTIBLE);
4853
4854 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004855 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004856 /* make sure to read SQ tail after writing flags */
4857 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004858
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004859 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004860 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004861 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004862 finish_wait(&ctx->sqo_wait, &wait);
4863 break;
4864 }
4865 if (signal_pending(current))
4866 flush_signals(current);
4867 schedule();
4868 finish_wait(&ctx->sqo_wait, &wait);
4869
Hristo Venev75b28af2019-08-26 17:23:46 +00004870 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004871 continue;
4872 }
4873 finish_wait(&ctx->sqo_wait, &wait);
4874
Hristo Venev75b28af2019-08-26 17:23:46 +00004875 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004876 }
4877
Jens Axboe8a4955f2019-12-09 14:52:35 -07004878 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004879 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004880 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004881 if (ret > 0)
4882 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004883 }
4884
4885 set_fs(old_fs);
4886 if (cur_mm) {
4887 unuse_mm(cur_mm);
4888 mmput(cur_mm);
4889 }
Jens Axboe181e4482019-11-25 08:52:30 -07004890 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004891
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004892 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004893
Jens Axboe6c271ce2019-01-10 11:22:30 -07004894 return 0;
4895}
4896
Jens Axboebda52162019-09-24 13:47:15 -06004897struct io_wait_queue {
4898 struct wait_queue_entry wq;
4899 struct io_ring_ctx *ctx;
4900 unsigned to_wait;
4901 unsigned nr_timeouts;
4902};
4903
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004904static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004905{
4906 struct io_ring_ctx *ctx = iowq->ctx;
4907
4908 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004909 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004910 * started waiting. For timeouts, we always want to return to userspace,
4911 * regardless of event count.
4912 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004913 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004914 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4915}
4916
4917static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4918 int wake_flags, void *key)
4919{
4920 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4921 wq);
4922
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004923 /* use noflush == true, as we can't safely rely on locking context */
4924 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004925 return -1;
4926
4927 return autoremove_wake_function(curr, mode, wake_flags, key);
4928}
4929
Jens Axboe2b188cc2019-01-07 10:46:33 -07004930/*
4931 * Wait until events become available, if we don't already have some. The
4932 * application must reap them itself, as they reside on the shared cq ring.
4933 */
4934static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4935 const sigset_t __user *sig, size_t sigsz)
4936{
Jens Axboebda52162019-09-24 13:47:15 -06004937 struct io_wait_queue iowq = {
4938 .wq = {
4939 .private = current,
4940 .func = io_wake_function,
4941 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4942 },
4943 .ctx = ctx,
4944 .to_wait = min_events,
4945 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004946 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004947 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004948
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004949 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004950 return 0;
4951
4952 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004953#ifdef CONFIG_COMPAT
4954 if (in_compat_syscall())
4955 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004956 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004957 else
4958#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004959 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004960
Jens Axboe2b188cc2019-01-07 10:46:33 -07004961 if (ret)
4962 return ret;
4963 }
4964
Jens Axboebda52162019-09-24 13:47:15 -06004965 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004966 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004967 do {
4968 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4969 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004970 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004971 break;
4972 schedule();
4973 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004974 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004975 break;
4976 }
4977 } while (1);
4978 finish_wait(&ctx->wait, &iowq.wq);
4979
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004980 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004981
Hristo Venev75b28af2019-08-26 17:23:46 +00004982 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004983}
4984
Jens Axboe6b063142019-01-10 22:13:58 -07004985static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4986{
4987#if defined(CONFIG_UNIX)
4988 if (ctx->ring_sock) {
4989 struct sock *sock = ctx->ring_sock->sk;
4990 struct sk_buff *skb;
4991
4992 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4993 kfree_skb(skb);
4994 }
4995#else
4996 int i;
4997
Jens Axboe65e19f52019-10-26 07:20:21 -06004998 for (i = 0; i < ctx->nr_user_files; i++) {
4999 struct file *file;
5000
5001 file = io_file_from_index(ctx, i);
5002 if (file)
5003 fput(file);
5004 }
Jens Axboe6b063142019-01-10 22:13:58 -07005005#endif
5006}
5007
Jens Axboe05f3fb32019-12-09 11:22:50 -07005008static void io_file_ref_kill(struct percpu_ref *ref)
5009{
5010 struct fixed_file_data *data;
5011
5012 data = container_of(ref, struct fixed_file_data, refs);
5013 complete(&data->done);
5014}
5015
Jens Axboe6b063142019-01-10 22:13:58 -07005016static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5017{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005018 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005019 unsigned nr_tables, i;
5020
Jens Axboe05f3fb32019-12-09 11:22:50 -07005021 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005022 return -ENXIO;
5023
Jens Axboe05f3fb32019-12-09 11:22:50 -07005024 /* protect against inflight atomic switch, which drops the ref */
5025 flush_work(&data->ref_work);
5026 percpu_ref_get(&data->refs);
5027 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5028 wait_for_completion(&data->done);
5029 percpu_ref_put(&data->refs);
5030 percpu_ref_exit(&data->refs);
5031
Jens Axboe6b063142019-01-10 22:13:58 -07005032 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005033 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5034 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005035 kfree(data->table[i].files);
5036 kfree(data->table);
5037 kfree(data);
5038 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005039 ctx->nr_user_files = 0;
5040 return 0;
5041}
5042
Jens Axboe6c271ce2019-01-10 11:22:30 -07005043static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5044{
5045 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005046 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005047 /*
5048 * The park is a bit of a work-around, without it we get
5049 * warning spews on shutdown with SQPOLL set and affinity
5050 * set to a single CPU.
5051 */
Jens Axboe06058632019-04-13 09:26:03 -06005052 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005053 kthread_stop(ctx->sqo_thread);
5054 ctx->sqo_thread = NULL;
5055 }
5056}
5057
Jens Axboe6b063142019-01-10 22:13:58 -07005058static void io_finish_async(struct io_ring_ctx *ctx)
5059{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005060 io_sq_thread_stop(ctx);
5061
Jens Axboe561fb042019-10-24 07:25:42 -06005062 if (ctx->io_wq) {
5063 io_wq_destroy(ctx->io_wq);
5064 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005065 }
5066}
5067
5068#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005069/*
5070 * Ensure the UNIX gc is aware of our file set, so we are certain that
5071 * the io_uring can be safely unregistered on process exit, even if we have
5072 * loops in the file referencing.
5073 */
5074static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5075{
5076 struct sock *sk = ctx->ring_sock->sk;
5077 struct scm_fp_list *fpl;
5078 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005079 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005080
5081 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5082 unsigned long inflight = ctx->user->unix_inflight + nr;
5083
5084 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5085 return -EMFILE;
5086 }
5087
5088 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5089 if (!fpl)
5090 return -ENOMEM;
5091
5092 skb = alloc_skb(0, GFP_KERNEL);
5093 if (!skb) {
5094 kfree(fpl);
5095 return -ENOMEM;
5096 }
5097
5098 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005099
Jens Axboe08a45172019-10-03 08:11:03 -06005100 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005101 fpl->user = get_uid(ctx->user);
5102 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005103 struct file *file = io_file_from_index(ctx, i + offset);
5104
5105 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005106 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005107 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005108 unix_inflight(fpl->user, fpl->fp[nr_files]);
5109 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005110 }
5111
Jens Axboe08a45172019-10-03 08:11:03 -06005112 if (nr_files) {
5113 fpl->max = SCM_MAX_FD;
5114 fpl->count = nr_files;
5115 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005116 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005117 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5118 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005119
Jens Axboe08a45172019-10-03 08:11:03 -06005120 for (i = 0; i < nr_files; i++)
5121 fput(fpl->fp[i]);
5122 } else {
5123 kfree_skb(skb);
5124 kfree(fpl);
5125 }
Jens Axboe6b063142019-01-10 22:13:58 -07005126
5127 return 0;
5128}
5129
5130/*
5131 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5132 * causes regular reference counting to break down. We rely on the UNIX
5133 * garbage collection to take care of this problem for us.
5134 */
5135static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5136{
5137 unsigned left, total;
5138 int ret = 0;
5139
5140 total = 0;
5141 left = ctx->nr_user_files;
5142 while (left) {
5143 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005144
5145 ret = __io_sqe_files_scm(ctx, this_files, total);
5146 if (ret)
5147 break;
5148 left -= this_files;
5149 total += this_files;
5150 }
5151
5152 if (!ret)
5153 return 0;
5154
5155 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005156 struct file *file = io_file_from_index(ctx, total);
5157
5158 if (file)
5159 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005160 total++;
5161 }
5162
5163 return ret;
5164}
5165#else
5166static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5167{
5168 return 0;
5169}
5170#endif
5171
Jens Axboe65e19f52019-10-26 07:20:21 -06005172static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5173 unsigned nr_files)
5174{
5175 int i;
5176
5177 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005178 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005179 unsigned this_files;
5180
5181 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5182 table->files = kcalloc(this_files, sizeof(struct file *),
5183 GFP_KERNEL);
5184 if (!table->files)
5185 break;
5186 nr_files -= this_files;
5187 }
5188
5189 if (i == nr_tables)
5190 return 0;
5191
5192 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005193 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005194 kfree(table->files);
5195 }
5196 return 1;
5197}
5198
Jens Axboe05f3fb32019-12-09 11:22:50 -07005199static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005200{
5201#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005202 struct sock *sock = ctx->ring_sock->sk;
5203 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5204 struct sk_buff *skb;
5205 int i;
5206
5207 __skb_queue_head_init(&list);
5208
5209 /*
5210 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5211 * remove this entry and rearrange the file array.
5212 */
5213 skb = skb_dequeue(head);
5214 while (skb) {
5215 struct scm_fp_list *fp;
5216
5217 fp = UNIXCB(skb).fp;
5218 for (i = 0; i < fp->count; i++) {
5219 int left;
5220
5221 if (fp->fp[i] != file)
5222 continue;
5223
5224 unix_notinflight(fp->user, fp->fp[i]);
5225 left = fp->count - 1 - i;
5226 if (left) {
5227 memmove(&fp->fp[i], &fp->fp[i + 1],
5228 left * sizeof(struct file *));
5229 }
5230 fp->count--;
5231 if (!fp->count) {
5232 kfree_skb(skb);
5233 skb = NULL;
5234 } else {
5235 __skb_queue_tail(&list, skb);
5236 }
5237 fput(file);
5238 file = NULL;
5239 break;
5240 }
5241
5242 if (!file)
5243 break;
5244
5245 __skb_queue_tail(&list, skb);
5246
5247 skb = skb_dequeue(head);
5248 }
5249
5250 if (skb_peek(&list)) {
5251 spin_lock_irq(&head->lock);
5252 while ((skb = __skb_dequeue(&list)) != NULL)
5253 __skb_queue_tail(head, skb);
5254 spin_unlock_irq(&head->lock);
5255 }
5256#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005257 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005258#endif
5259}
5260
Jens Axboe05f3fb32019-12-09 11:22:50 -07005261struct io_file_put {
5262 struct llist_node llist;
5263 struct file *file;
5264 struct completion *done;
5265};
5266
5267static void io_ring_file_ref_switch(struct work_struct *work)
5268{
5269 struct io_file_put *pfile, *tmp;
5270 struct fixed_file_data *data;
5271 struct llist_node *node;
5272
5273 data = container_of(work, struct fixed_file_data, ref_work);
5274
5275 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5276 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5277 io_ring_file_put(data->ctx, pfile->file);
5278 if (pfile->done)
5279 complete(pfile->done);
5280 else
5281 kfree(pfile);
5282 }
5283 }
5284
5285 percpu_ref_get(&data->refs);
5286 percpu_ref_switch_to_percpu(&data->refs);
5287}
5288
5289static void io_file_data_ref_zero(struct percpu_ref *ref)
5290{
5291 struct fixed_file_data *data;
5292
5293 data = container_of(ref, struct fixed_file_data, refs);
5294
5295 /* we can't safely switch from inside this context, punt to wq */
5296 queue_work(system_wq, &data->ref_work);
5297}
5298
5299static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5300 unsigned nr_args)
5301{
5302 __s32 __user *fds = (__s32 __user *) arg;
5303 unsigned nr_tables;
5304 struct file *file;
5305 int fd, ret = 0;
5306 unsigned i;
5307
5308 if (ctx->file_data)
5309 return -EBUSY;
5310 if (!nr_args)
5311 return -EINVAL;
5312 if (nr_args > IORING_MAX_FIXED_FILES)
5313 return -EMFILE;
5314
5315 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5316 if (!ctx->file_data)
5317 return -ENOMEM;
5318 ctx->file_data->ctx = ctx;
5319 init_completion(&ctx->file_data->done);
5320
5321 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5322 ctx->file_data->table = kcalloc(nr_tables,
5323 sizeof(struct fixed_file_table),
5324 GFP_KERNEL);
5325 if (!ctx->file_data->table) {
5326 kfree(ctx->file_data);
5327 ctx->file_data = NULL;
5328 return -ENOMEM;
5329 }
5330
5331 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5332 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5333 kfree(ctx->file_data->table);
5334 kfree(ctx->file_data);
5335 ctx->file_data = NULL;
5336 return -ENOMEM;
5337 }
5338 ctx->file_data->put_llist.first = NULL;
5339 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5340
5341 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5342 percpu_ref_exit(&ctx->file_data->refs);
5343 kfree(ctx->file_data->table);
5344 kfree(ctx->file_data);
5345 ctx->file_data = NULL;
5346 return -ENOMEM;
5347 }
5348
5349 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5350 struct fixed_file_table *table;
5351 unsigned index;
5352
5353 ret = -EFAULT;
5354 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5355 break;
5356 /* allow sparse sets */
5357 if (fd == -1) {
5358 ret = 0;
5359 continue;
5360 }
5361
5362 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5363 index = i & IORING_FILE_TABLE_MASK;
5364 file = fget(fd);
5365
5366 ret = -EBADF;
5367 if (!file)
5368 break;
5369
5370 /*
5371 * Don't allow io_uring instances to be registered. If UNIX
5372 * isn't enabled, then this causes a reference cycle and this
5373 * instance can never get freed. If UNIX is enabled we'll
5374 * handle it just fine, but there's still no point in allowing
5375 * a ring fd as it doesn't support regular read/write anyway.
5376 */
5377 if (file->f_op == &io_uring_fops) {
5378 fput(file);
5379 break;
5380 }
5381 ret = 0;
5382 table->files[index] = file;
5383 }
5384
5385 if (ret) {
5386 for (i = 0; i < ctx->nr_user_files; i++) {
5387 file = io_file_from_index(ctx, i);
5388 if (file)
5389 fput(file);
5390 }
5391 for (i = 0; i < nr_tables; i++)
5392 kfree(ctx->file_data->table[i].files);
5393
5394 kfree(ctx->file_data->table);
5395 kfree(ctx->file_data);
5396 ctx->file_data = NULL;
5397 ctx->nr_user_files = 0;
5398 return ret;
5399 }
5400
5401 ret = io_sqe_files_scm(ctx);
5402 if (ret)
5403 io_sqe_files_unregister(ctx);
5404
5405 return ret;
5406}
5407
Jens Axboec3a31e62019-10-03 13:59:56 -06005408static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5409 int index)
5410{
5411#if defined(CONFIG_UNIX)
5412 struct sock *sock = ctx->ring_sock->sk;
5413 struct sk_buff_head *head = &sock->sk_receive_queue;
5414 struct sk_buff *skb;
5415
5416 /*
5417 * See if we can merge this file into an existing skb SCM_RIGHTS
5418 * file set. If there's no room, fall back to allocating a new skb
5419 * and filling it in.
5420 */
5421 spin_lock_irq(&head->lock);
5422 skb = skb_peek(head);
5423 if (skb) {
5424 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5425
5426 if (fpl->count < SCM_MAX_FD) {
5427 __skb_unlink(skb, head);
5428 spin_unlock_irq(&head->lock);
5429 fpl->fp[fpl->count] = get_file(file);
5430 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5431 fpl->count++;
5432 spin_lock_irq(&head->lock);
5433 __skb_queue_head(head, skb);
5434 } else {
5435 skb = NULL;
5436 }
5437 }
5438 spin_unlock_irq(&head->lock);
5439
5440 if (skb) {
5441 fput(file);
5442 return 0;
5443 }
5444
5445 return __io_sqe_files_scm(ctx, 1, index);
5446#else
5447 return 0;
5448#endif
5449}
5450
Jens Axboe05f3fb32019-12-09 11:22:50 -07005451static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005452{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005453 struct fixed_file_data *data;
5454
5455 data = container_of(ref, struct fixed_file_data, refs);
5456 clear_bit(FFD_F_ATOMIC, &data->state);
5457}
5458
5459static bool io_queue_file_removal(struct fixed_file_data *data,
5460 struct file *file)
5461{
5462 struct io_file_put *pfile, pfile_stack;
5463 DECLARE_COMPLETION_ONSTACK(done);
5464
5465 /*
5466 * If we fail allocating the struct we need for doing async reomval
5467 * of this file, just punt to sync and wait for it.
5468 */
5469 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5470 if (!pfile) {
5471 pfile = &pfile_stack;
5472 pfile->done = &done;
5473 }
5474
5475 pfile->file = file;
5476 llist_add(&pfile->llist, &data->put_llist);
5477
5478 if (pfile == &pfile_stack) {
5479 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5480 percpu_ref_put(&data->refs);
5481 percpu_ref_switch_to_atomic(&data->refs,
5482 io_atomic_switch);
5483 }
5484 wait_for_completion(&done);
5485 flush_work(&data->ref_work);
5486 return false;
5487 }
5488
5489 return true;
5490}
5491
5492static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5493 struct io_uring_files_update *up,
5494 unsigned nr_args)
5495{
5496 struct fixed_file_data *data = ctx->file_data;
5497 bool ref_switch = false;
5498 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005499 __s32 __user *fds;
5500 int fd, i, err;
5501 __u32 done;
5502
Jens Axboe05f3fb32019-12-09 11:22:50 -07005503 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005504 return -EOVERFLOW;
5505 if (done > ctx->nr_user_files)
5506 return -EINVAL;
5507
5508 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005509 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005510 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005511 struct fixed_file_table *table;
5512 unsigned index;
5513
Jens Axboec3a31e62019-10-03 13:59:56 -06005514 err = 0;
5515 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5516 err = -EFAULT;
5517 break;
5518 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005519 i = array_index_nospec(up->offset, ctx->nr_user_files);
5520 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005521 index = i & IORING_FILE_TABLE_MASK;
5522 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005523 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005524 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005525 if (io_queue_file_removal(data, file))
5526 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005527 }
5528 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005529 file = fget(fd);
5530 if (!file) {
5531 err = -EBADF;
5532 break;
5533 }
5534 /*
5535 * Don't allow io_uring instances to be registered. If
5536 * UNIX isn't enabled, then this causes a reference
5537 * cycle and this instance can never get freed. If UNIX
5538 * is enabled we'll handle it just fine, but there's
5539 * still no point in allowing a ring fd as it doesn't
5540 * support regular read/write anyway.
5541 */
5542 if (file->f_op == &io_uring_fops) {
5543 fput(file);
5544 err = -EBADF;
5545 break;
5546 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005547 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005548 err = io_sqe_file_register(ctx, file, i);
5549 if (err)
5550 break;
5551 }
5552 nr_args--;
5553 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005554 up->offset++;
5555 }
5556
5557 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5558 percpu_ref_put(&data->refs);
5559 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005560 }
5561
5562 return done ? done : err;
5563}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005564static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5565 unsigned nr_args)
5566{
5567 struct io_uring_files_update up;
5568
5569 if (!ctx->file_data)
5570 return -ENXIO;
5571 if (!nr_args)
5572 return -EINVAL;
5573 if (copy_from_user(&up, arg, sizeof(up)))
5574 return -EFAULT;
5575 if (up.resv)
5576 return -EINVAL;
5577
5578 return __io_sqe_files_update(ctx, &up, nr_args);
5579}
Jens Axboec3a31e62019-10-03 13:59:56 -06005580
Jens Axboe7d723062019-11-12 22:31:31 -07005581static void io_put_work(struct io_wq_work *work)
5582{
5583 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5584
5585 io_put_req(req);
5586}
5587
5588static void io_get_work(struct io_wq_work *work)
5589{
5590 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5591
5592 refcount_inc(&req->refs);
5593}
5594
Jens Axboe6c271ce2019-01-10 11:22:30 -07005595static int io_sq_offload_start(struct io_ring_ctx *ctx,
5596 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005597{
Jens Axboe576a3472019-11-25 08:49:20 -07005598 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005599 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005600 int ret;
5601
Jens Axboe6c271ce2019-01-10 11:22:30 -07005602 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005603 mmgrab(current->mm);
5604 ctx->sqo_mm = current->mm;
5605
Jens Axboe6c271ce2019-01-10 11:22:30 -07005606 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005607 ret = -EPERM;
5608 if (!capable(CAP_SYS_ADMIN))
5609 goto err;
5610
Jens Axboe917257d2019-04-13 09:28:55 -06005611 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5612 if (!ctx->sq_thread_idle)
5613 ctx->sq_thread_idle = HZ;
5614
Jens Axboe6c271ce2019-01-10 11:22:30 -07005615 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005616 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005617
Jens Axboe917257d2019-04-13 09:28:55 -06005618 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005619 if (cpu >= nr_cpu_ids)
5620 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005621 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005622 goto err;
5623
Jens Axboe6c271ce2019-01-10 11:22:30 -07005624 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5625 ctx, cpu,
5626 "io_uring-sq");
5627 } else {
5628 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5629 "io_uring-sq");
5630 }
5631 if (IS_ERR(ctx->sqo_thread)) {
5632 ret = PTR_ERR(ctx->sqo_thread);
5633 ctx->sqo_thread = NULL;
5634 goto err;
5635 }
5636 wake_up_process(ctx->sqo_thread);
5637 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5638 /* Can't have SQ_AFF without SQPOLL */
5639 ret = -EINVAL;
5640 goto err;
5641 }
5642
Jens Axboe576a3472019-11-25 08:49:20 -07005643 data.mm = ctx->sqo_mm;
5644 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005645 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005646 data.get_work = io_get_work;
5647 data.put_work = io_put_work;
5648
Jens Axboe561fb042019-10-24 07:25:42 -06005649 /* Do QD, or 4 * CPUS, whatever is smallest */
5650 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005651 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005652 if (IS_ERR(ctx->io_wq)) {
5653 ret = PTR_ERR(ctx->io_wq);
5654 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005655 goto err;
5656 }
5657
5658 return 0;
5659err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005660 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005661 mmdrop(ctx->sqo_mm);
5662 ctx->sqo_mm = NULL;
5663 return ret;
5664}
5665
5666static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5667{
5668 atomic_long_sub(nr_pages, &user->locked_vm);
5669}
5670
5671static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5672{
5673 unsigned long page_limit, cur_pages, new_pages;
5674
5675 /* Don't allow more pages than we can safely lock */
5676 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5677
5678 do {
5679 cur_pages = atomic_long_read(&user->locked_vm);
5680 new_pages = cur_pages + nr_pages;
5681 if (new_pages > page_limit)
5682 return -ENOMEM;
5683 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5684 new_pages) != cur_pages);
5685
5686 return 0;
5687}
5688
5689static void io_mem_free(void *ptr)
5690{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005691 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005692
Mark Rutland52e04ef2019-04-30 17:30:21 +01005693 if (!ptr)
5694 return;
5695
5696 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005697 if (put_page_testzero(page))
5698 free_compound_page(page);
5699}
5700
5701static void *io_mem_alloc(size_t size)
5702{
5703 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5704 __GFP_NORETRY;
5705
5706 return (void *) __get_free_pages(gfp_flags, get_order(size));
5707}
5708
Hristo Venev75b28af2019-08-26 17:23:46 +00005709static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5710 size_t *sq_offset)
5711{
5712 struct io_rings *rings;
5713 size_t off, sq_array_size;
5714
5715 off = struct_size(rings, cqes, cq_entries);
5716 if (off == SIZE_MAX)
5717 return SIZE_MAX;
5718
5719#ifdef CONFIG_SMP
5720 off = ALIGN(off, SMP_CACHE_BYTES);
5721 if (off == 0)
5722 return SIZE_MAX;
5723#endif
5724
5725 sq_array_size = array_size(sizeof(u32), sq_entries);
5726 if (sq_array_size == SIZE_MAX)
5727 return SIZE_MAX;
5728
5729 if (check_add_overflow(off, sq_array_size, &off))
5730 return SIZE_MAX;
5731
5732 if (sq_offset)
5733 *sq_offset = off;
5734
5735 return off;
5736}
5737
Jens Axboe2b188cc2019-01-07 10:46:33 -07005738static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5739{
Hristo Venev75b28af2019-08-26 17:23:46 +00005740 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005741
Hristo Venev75b28af2019-08-26 17:23:46 +00005742 pages = (size_t)1 << get_order(
5743 rings_size(sq_entries, cq_entries, NULL));
5744 pages += (size_t)1 << get_order(
5745 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005746
Hristo Venev75b28af2019-08-26 17:23:46 +00005747 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005748}
5749
Jens Axboeedafcce2019-01-09 09:16:05 -07005750static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5751{
5752 int i, j;
5753
5754 if (!ctx->user_bufs)
5755 return -ENXIO;
5756
5757 for (i = 0; i < ctx->nr_user_bufs; i++) {
5758 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5759
5760 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005761 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005762
5763 if (ctx->account_mem)
5764 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005765 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005766 imu->nr_bvecs = 0;
5767 }
5768
5769 kfree(ctx->user_bufs);
5770 ctx->user_bufs = NULL;
5771 ctx->nr_user_bufs = 0;
5772 return 0;
5773}
5774
5775static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5776 void __user *arg, unsigned index)
5777{
5778 struct iovec __user *src;
5779
5780#ifdef CONFIG_COMPAT
5781 if (ctx->compat) {
5782 struct compat_iovec __user *ciovs;
5783 struct compat_iovec ciov;
5784
5785 ciovs = (struct compat_iovec __user *) arg;
5786 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5787 return -EFAULT;
5788
Jens Axboed55e5f52019-12-11 16:12:15 -07005789 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005790 dst->iov_len = ciov.iov_len;
5791 return 0;
5792 }
5793#endif
5794 src = (struct iovec __user *) arg;
5795 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5796 return -EFAULT;
5797 return 0;
5798}
5799
5800static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5801 unsigned nr_args)
5802{
5803 struct vm_area_struct **vmas = NULL;
5804 struct page **pages = NULL;
5805 int i, j, got_pages = 0;
5806 int ret = -EINVAL;
5807
5808 if (ctx->user_bufs)
5809 return -EBUSY;
5810 if (!nr_args || nr_args > UIO_MAXIOV)
5811 return -EINVAL;
5812
5813 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5814 GFP_KERNEL);
5815 if (!ctx->user_bufs)
5816 return -ENOMEM;
5817
5818 for (i = 0; i < nr_args; i++) {
5819 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5820 unsigned long off, start, end, ubuf;
5821 int pret, nr_pages;
5822 struct iovec iov;
5823 size_t size;
5824
5825 ret = io_copy_iov(ctx, &iov, arg, i);
5826 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005827 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005828
5829 /*
5830 * Don't impose further limits on the size and buffer
5831 * constraints here, we'll -EINVAL later when IO is
5832 * submitted if they are wrong.
5833 */
5834 ret = -EFAULT;
5835 if (!iov.iov_base || !iov.iov_len)
5836 goto err;
5837
5838 /* arbitrary limit, but we need something */
5839 if (iov.iov_len > SZ_1G)
5840 goto err;
5841
5842 ubuf = (unsigned long) iov.iov_base;
5843 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5844 start = ubuf >> PAGE_SHIFT;
5845 nr_pages = end - start;
5846
5847 if (ctx->account_mem) {
5848 ret = io_account_mem(ctx->user, nr_pages);
5849 if (ret)
5850 goto err;
5851 }
5852
5853 ret = 0;
5854 if (!pages || nr_pages > got_pages) {
5855 kfree(vmas);
5856 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005857 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005858 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005859 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005860 sizeof(struct vm_area_struct *),
5861 GFP_KERNEL);
5862 if (!pages || !vmas) {
5863 ret = -ENOMEM;
5864 if (ctx->account_mem)
5865 io_unaccount_mem(ctx->user, nr_pages);
5866 goto err;
5867 }
5868 got_pages = nr_pages;
5869 }
5870
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005871 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005872 GFP_KERNEL);
5873 ret = -ENOMEM;
5874 if (!imu->bvec) {
5875 if (ctx->account_mem)
5876 io_unaccount_mem(ctx->user, nr_pages);
5877 goto err;
5878 }
5879
5880 ret = 0;
5881 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005882 pret = get_user_pages(ubuf, nr_pages,
5883 FOLL_WRITE | FOLL_LONGTERM,
5884 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005885 if (pret == nr_pages) {
5886 /* don't support file backed memory */
5887 for (j = 0; j < nr_pages; j++) {
5888 struct vm_area_struct *vma = vmas[j];
5889
5890 if (vma->vm_file &&
5891 !is_file_hugepages(vma->vm_file)) {
5892 ret = -EOPNOTSUPP;
5893 break;
5894 }
5895 }
5896 } else {
5897 ret = pret < 0 ? pret : -EFAULT;
5898 }
5899 up_read(&current->mm->mmap_sem);
5900 if (ret) {
5901 /*
5902 * if we did partial map, or found file backed vmas,
5903 * release any pages we did get
5904 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005905 if (pret > 0)
5906 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005907 if (ctx->account_mem)
5908 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005909 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005910 goto err;
5911 }
5912
5913 off = ubuf & ~PAGE_MASK;
5914 size = iov.iov_len;
5915 for (j = 0; j < nr_pages; j++) {
5916 size_t vec_len;
5917
5918 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5919 imu->bvec[j].bv_page = pages[j];
5920 imu->bvec[j].bv_len = vec_len;
5921 imu->bvec[j].bv_offset = off;
5922 off = 0;
5923 size -= vec_len;
5924 }
5925 /* store original address for later verification */
5926 imu->ubuf = ubuf;
5927 imu->len = iov.iov_len;
5928 imu->nr_bvecs = nr_pages;
5929
5930 ctx->nr_user_bufs++;
5931 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005932 kvfree(pages);
5933 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005934 return 0;
5935err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005936 kvfree(pages);
5937 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005938 io_sqe_buffer_unregister(ctx);
5939 return ret;
5940}
5941
Jens Axboe9b402842019-04-11 11:45:41 -06005942static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5943{
5944 __s32 __user *fds = arg;
5945 int fd;
5946
5947 if (ctx->cq_ev_fd)
5948 return -EBUSY;
5949
5950 if (copy_from_user(&fd, fds, sizeof(*fds)))
5951 return -EFAULT;
5952
5953 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5954 if (IS_ERR(ctx->cq_ev_fd)) {
5955 int ret = PTR_ERR(ctx->cq_ev_fd);
5956 ctx->cq_ev_fd = NULL;
5957 return ret;
5958 }
5959
5960 return 0;
5961}
5962
5963static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5964{
5965 if (ctx->cq_ev_fd) {
5966 eventfd_ctx_put(ctx->cq_ev_fd);
5967 ctx->cq_ev_fd = NULL;
5968 return 0;
5969 }
5970
5971 return -ENXIO;
5972}
5973
Jens Axboe2b188cc2019-01-07 10:46:33 -07005974static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5975{
Jens Axboe6b063142019-01-10 22:13:58 -07005976 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005977 if (ctx->sqo_mm)
5978 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005979
5980 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005981 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005982 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005983 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005984
Jens Axboe2b188cc2019-01-07 10:46:33 -07005985#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005986 if (ctx->ring_sock) {
5987 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005988 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005989 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005990#endif
5991
Hristo Venev75b28af2019-08-26 17:23:46 +00005992 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005993 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005994
5995 percpu_ref_exit(&ctx->refs);
5996 if (ctx->account_mem)
5997 io_unaccount_mem(ctx->user,
5998 ring_pages(ctx->sq_entries, ctx->cq_entries));
5999 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006000 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006001 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006002 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006003 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006004 kfree(ctx);
6005}
6006
6007static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6008{
6009 struct io_ring_ctx *ctx = file->private_data;
6010 __poll_t mask = 0;
6011
6012 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006013 /*
6014 * synchronizes with barrier from wq_has_sleeper call in
6015 * io_commit_cqring
6016 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006017 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006018 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6019 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006020 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006021 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006022 mask |= EPOLLIN | EPOLLRDNORM;
6023
6024 return mask;
6025}
6026
6027static int io_uring_fasync(int fd, struct file *file, int on)
6028{
6029 struct io_ring_ctx *ctx = file->private_data;
6030
6031 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6032}
6033
6034static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6035{
6036 mutex_lock(&ctx->uring_lock);
6037 percpu_ref_kill(&ctx->refs);
6038 mutex_unlock(&ctx->uring_lock);
6039
Jens Axboe5262f562019-09-17 12:26:57 -06006040 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006041 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006042
6043 if (ctx->io_wq)
6044 io_wq_cancel_all(ctx->io_wq);
6045
Jens Axboedef596e2019-01-09 08:59:42 -07006046 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006047 /* if we failed setting up the ctx, we might not have any rings */
6048 if (ctx->rings)
6049 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07006050 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006051 io_ring_ctx_free(ctx);
6052}
6053
6054static int io_uring_release(struct inode *inode, struct file *file)
6055{
6056 struct io_ring_ctx *ctx = file->private_data;
6057
6058 file->private_data = NULL;
6059 io_ring_ctx_wait_and_kill(ctx);
6060 return 0;
6061}
6062
Jens Axboefcb323c2019-10-24 12:39:47 -06006063static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6064 struct files_struct *files)
6065{
6066 struct io_kiocb *req;
6067 DEFINE_WAIT(wait);
6068
6069 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006070 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006071
6072 spin_lock_irq(&ctx->inflight_lock);
6073 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006074 if (req->work.files != files)
6075 continue;
6076 /* req is being completed, ignore */
6077 if (!refcount_inc_not_zero(&req->refs))
6078 continue;
6079 cancel_req = req;
6080 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006081 }
Jens Axboe768134d2019-11-10 20:30:53 -07006082 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006083 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006084 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006085 spin_unlock_irq(&ctx->inflight_lock);
6086
Jens Axboe768134d2019-11-10 20:30:53 -07006087 /* We need to keep going until we don't find a matching req */
6088 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006089 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006090
6091 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6092 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006093 schedule();
6094 }
Jens Axboe768134d2019-11-10 20:30:53 -07006095 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006096}
6097
6098static int io_uring_flush(struct file *file, void *data)
6099{
6100 struct io_ring_ctx *ctx = file->private_data;
6101
6102 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006103 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6104 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006105 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006106 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006107 return 0;
6108}
6109
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006110static void *io_uring_validate_mmap_request(struct file *file,
6111 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006112{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006113 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006114 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006115 struct page *page;
6116 void *ptr;
6117
6118 switch (offset) {
6119 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006120 case IORING_OFF_CQ_RING:
6121 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006122 break;
6123 case IORING_OFF_SQES:
6124 ptr = ctx->sq_sqes;
6125 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006126 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006127 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006128 }
6129
6130 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006131 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006132 return ERR_PTR(-EINVAL);
6133
6134 return ptr;
6135}
6136
6137#ifdef CONFIG_MMU
6138
6139static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6140{
6141 size_t sz = vma->vm_end - vma->vm_start;
6142 unsigned long pfn;
6143 void *ptr;
6144
6145 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6146 if (IS_ERR(ptr))
6147 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006148
6149 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6150 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6151}
6152
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006153#else /* !CONFIG_MMU */
6154
6155static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6156{
6157 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6158}
6159
6160static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6161{
6162 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6163}
6164
6165static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6166 unsigned long addr, unsigned long len,
6167 unsigned long pgoff, unsigned long flags)
6168{
6169 void *ptr;
6170
6171 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6172 if (IS_ERR(ptr))
6173 return PTR_ERR(ptr);
6174
6175 return (unsigned long) ptr;
6176}
6177
6178#endif /* !CONFIG_MMU */
6179
Jens Axboe2b188cc2019-01-07 10:46:33 -07006180SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6181 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6182 size_t, sigsz)
6183{
6184 struct io_ring_ctx *ctx;
6185 long ret = -EBADF;
6186 int submitted = 0;
6187 struct fd f;
6188
Jens Axboe6c271ce2019-01-10 11:22:30 -07006189 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006190 return -EINVAL;
6191
6192 f = fdget(fd);
6193 if (!f.file)
6194 return -EBADF;
6195
6196 ret = -EOPNOTSUPP;
6197 if (f.file->f_op != &io_uring_fops)
6198 goto out_fput;
6199
6200 ret = -ENXIO;
6201 ctx = f.file->private_data;
6202 if (!percpu_ref_tryget(&ctx->refs))
6203 goto out_fput;
6204
Jens Axboe6c271ce2019-01-10 11:22:30 -07006205 /*
6206 * For SQ polling, the thread will do all submissions and completions.
6207 * Just return the requested submit count, and wake the thread if
6208 * we were asked to.
6209 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006210 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006211 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006212 if (!list_empty_careful(&ctx->cq_overflow_list))
6213 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006214 if (flags & IORING_ENTER_SQ_WAKEUP)
6215 wake_up(&ctx->sqo_wait);
6216 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006217 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006218 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006219
Jens Axboe44d28272020-01-16 19:00:24 -07006220 if (current->mm != ctx->sqo_mm ||
6221 current_cred() != ctx->creds) {
6222 ret = -EPERM;
6223 goto out;
6224 }
6225
Jens Axboe2b188cc2019-01-07 10:46:33 -07006226 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006227 /* already have mm, so io_submit_sqes() won't try to grab it */
6228 cur_mm = ctx->sqo_mm;
6229 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6230 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006231 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006232
6233 if (submitted != to_submit)
6234 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006235 }
6236 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006237 unsigned nr_events = 0;
6238
Jens Axboe2b188cc2019-01-07 10:46:33 -07006239 min_complete = min(min_complete, ctx->cq_entries);
6240
Jens Axboedef596e2019-01-09 08:59:42 -07006241 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006242 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006243 } else {
6244 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6245 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006246 }
6247
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006248out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006249 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006250out_fput:
6251 fdput(f);
6252 return submitted ? submitted : ret;
6253}
6254
6255static const struct file_operations io_uring_fops = {
6256 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006257 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006258 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006259#ifndef CONFIG_MMU
6260 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6261 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6262#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006263 .poll = io_uring_poll,
6264 .fasync = io_uring_fasync,
6265};
6266
6267static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6268 struct io_uring_params *p)
6269{
Hristo Venev75b28af2019-08-26 17:23:46 +00006270 struct io_rings *rings;
6271 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006272
Hristo Venev75b28af2019-08-26 17:23:46 +00006273 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6274 if (size == SIZE_MAX)
6275 return -EOVERFLOW;
6276
6277 rings = io_mem_alloc(size);
6278 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006279 return -ENOMEM;
6280
Hristo Venev75b28af2019-08-26 17:23:46 +00006281 ctx->rings = rings;
6282 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6283 rings->sq_ring_mask = p->sq_entries - 1;
6284 rings->cq_ring_mask = p->cq_entries - 1;
6285 rings->sq_ring_entries = p->sq_entries;
6286 rings->cq_ring_entries = p->cq_entries;
6287 ctx->sq_mask = rings->sq_ring_mask;
6288 ctx->cq_mask = rings->cq_ring_mask;
6289 ctx->sq_entries = rings->sq_ring_entries;
6290 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006291
6292 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006293 if (size == SIZE_MAX) {
6294 io_mem_free(ctx->rings);
6295 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006296 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006297 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006298
6299 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006300 if (!ctx->sq_sqes) {
6301 io_mem_free(ctx->rings);
6302 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006303 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006304 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006305
Jens Axboe2b188cc2019-01-07 10:46:33 -07006306 return 0;
6307}
6308
6309/*
6310 * Allocate an anonymous fd, this is what constitutes the application
6311 * visible backing of an io_uring instance. The application mmaps this
6312 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6313 * we have to tie this fd to a socket for file garbage collection purposes.
6314 */
6315static int io_uring_get_fd(struct io_ring_ctx *ctx)
6316{
6317 struct file *file;
6318 int ret;
6319
6320#if defined(CONFIG_UNIX)
6321 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6322 &ctx->ring_sock);
6323 if (ret)
6324 return ret;
6325#endif
6326
6327 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6328 if (ret < 0)
6329 goto err;
6330
6331 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6332 O_RDWR | O_CLOEXEC);
6333 if (IS_ERR(file)) {
6334 put_unused_fd(ret);
6335 ret = PTR_ERR(file);
6336 goto err;
6337 }
6338
6339#if defined(CONFIG_UNIX)
6340 ctx->ring_sock->file = file;
6341#endif
6342 fd_install(ret, file);
6343 return ret;
6344err:
6345#if defined(CONFIG_UNIX)
6346 sock_release(ctx->ring_sock);
6347 ctx->ring_sock = NULL;
6348#endif
6349 return ret;
6350}
6351
6352static int io_uring_create(unsigned entries, struct io_uring_params *p)
6353{
6354 struct user_struct *user = NULL;
6355 struct io_ring_ctx *ctx;
6356 bool account_mem;
6357 int ret;
6358
Jens Axboe8110c1a2019-12-28 15:39:54 -07006359 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006360 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006361 if (entries > IORING_MAX_ENTRIES) {
6362 if (!(p->flags & IORING_SETUP_CLAMP))
6363 return -EINVAL;
6364 entries = IORING_MAX_ENTRIES;
6365 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006366
6367 /*
6368 * Use twice as many entries for the CQ ring. It's possible for the
6369 * application to drive a higher depth than the size of the SQ ring,
6370 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006371 * some flexibility in overcommitting a bit. If the application has
6372 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6373 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006374 */
6375 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006376 if (p->flags & IORING_SETUP_CQSIZE) {
6377 /*
6378 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6379 * to a power-of-two, if it isn't already. We do NOT impose
6380 * any cq vs sq ring sizing.
6381 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006382 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006383 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006384 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6385 if (!(p->flags & IORING_SETUP_CLAMP))
6386 return -EINVAL;
6387 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6388 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006389 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6390 } else {
6391 p->cq_entries = 2 * p->sq_entries;
6392 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006393
6394 user = get_uid(current_user());
6395 account_mem = !capable(CAP_IPC_LOCK);
6396
6397 if (account_mem) {
6398 ret = io_account_mem(user,
6399 ring_pages(p->sq_entries, p->cq_entries));
6400 if (ret) {
6401 free_uid(user);
6402 return ret;
6403 }
6404 }
6405
6406 ctx = io_ring_ctx_alloc(p);
6407 if (!ctx) {
6408 if (account_mem)
6409 io_unaccount_mem(user, ring_pages(p->sq_entries,
6410 p->cq_entries));
6411 free_uid(user);
6412 return -ENOMEM;
6413 }
6414 ctx->compat = in_compat_syscall();
6415 ctx->account_mem = account_mem;
6416 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006417 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006418
6419 ret = io_allocate_scq_urings(ctx, p);
6420 if (ret)
6421 goto err;
6422
Jens Axboe6c271ce2019-01-10 11:22:30 -07006423 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006424 if (ret)
6425 goto err;
6426
Jens Axboe2b188cc2019-01-07 10:46:33 -07006427 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006428 p->sq_off.head = offsetof(struct io_rings, sq.head);
6429 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6430 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6431 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6432 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6433 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6434 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006435
6436 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006437 p->cq_off.head = offsetof(struct io_rings, cq.head);
6438 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6439 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6440 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6441 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6442 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006443
Jens Axboe044c1ab2019-10-28 09:15:33 -06006444 /*
6445 * Install ring fd as the very last thing, so we don't risk someone
6446 * having closed it before we finish setup
6447 */
6448 ret = io_uring_get_fd(ctx);
6449 if (ret < 0)
6450 goto err;
6451
Jens Axboeda8c9692019-12-02 18:51:26 -07006452 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboeba042912019-12-25 16:33:42 -07006453 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006454 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006455 return ret;
6456err:
6457 io_ring_ctx_wait_and_kill(ctx);
6458 return ret;
6459}
6460
6461/*
6462 * Sets up an aio uring context, and returns the fd. Applications asks for a
6463 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6464 * params structure passed in.
6465 */
6466static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6467{
6468 struct io_uring_params p;
6469 long ret;
6470 int i;
6471
6472 if (copy_from_user(&p, params, sizeof(p)))
6473 return -EFAULT;
6474 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6475 if (p.resv[i])
6476 return -EINVAL;
6477 }
6478
Jens Axboe6c271ce2019-01-10 11:22:30 -07006479 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006480 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
6481 IORING_SETUP_CLAMP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006482 return -EINVAL;
6483
6484 ret = io_uring_create(entries, &p);
6485 if (ret < 0)
6486 return ret;
6487
6488 if (copy_to_user(params, &p, sizeof(p)))
6489 return -EFAULT;
6490
6491 return ret;
6492}
6493
6494SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6495 struct io_uring_params __user *, params)
6496{
6497 return io_uring_setup(entries, params);
6498}
6499
Jens Axboeedafcce2019-01-09 09:16:05 -07006500static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6501 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006502 __releases(ctx->uring_lock)
6503 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006504{
6505 int ret;
6506
Jens Axboe35fa71a2019-04-22 10:23:23 -06006507 /*
6508 * We're inside the ring mutex, if the ref is already dying, then
6509 * someone else killed the ctx or is already going through
6510 * io_uring_register().
6511 */
6512 if (percpu_ref_is_dying(&ctx->refs))
6513 return -ENXIO;
6514
Jens Axboe05f3fb32019-12-09 11:22:50 -07006515 if (opcode != IORING_UNREGISTER_FILES &&
6516 opcode != IORING_REGISTER_FILES_UPDATE) {
6517 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006518
Jens Axboe05f3fb32019-12-09 11:22:50 -07006519 /*
6520 * Drop uring mutex before waiting for references to exit. If
6521 * another thread is currently inside io_uring_enter() it might
6522 * need to grab the uring_lock to make progress. If we hold it
6523 * here across the drain wait, then we can deadlock. It's safe
6524 * to drop the mutex here, since no new references will come in
6525 * after we've killed the percpu ref.
6526 */
6527 mutex_unlock(&ctx->uring_lock);
6528 wait_for_completion(&ctx->completions[0]);
6529 mutex_lock(&ctx->uring_lock);
6530 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006531
6532 switch (opcode) {
6533 case IORING_REGISTER_BUFFERS:
6534 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6535 break;
6536 case IORING_UNREGISTER_BUFFERS:
6537 ret = -EINVAL;
6538 if (arg || nr_args)
6539 break;
6540 ret = io_sqe_buffer_unregister(ctx);
6541 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006542 case IORING_REGISTER_FILES:
6543 ret = io_sqe_files_register(ctx, arg, nr_args);
6544 break;
6545 case IORING_UNREGISTER_FILES:
6546 ret = -EINVAL;
6547 if (arg || nr_args)
6548 break;
6549 ret = io_sqe_files_unregister(ctx);
6550 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006551 case IORING_REGISTER_FILES_UPDATE:
6552 ret = io_sqe_files_update(ctx, arg, nr_args);
6553 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006554 case IORING_REGISTER_EVENTFD:
6555 ret = -EINVAL;
6556 if (nr_args != 1)
6557 break;
6558 ret = io_eventfd_register(ctx, arg);
6559 break;
6560 case IORING_UNREGISTER_EVENTFD:
6561 ret = -EINVAL;
6562 if (arg || nr_args)
6563 break;
6564 ret = io_eventfd_unregister(ctx);
6565 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006566 default:
6567 ret = -EINVAL;
6568 break;
6569 }
6570
Jens Axboe05f3fb32019-12-09 11:22:50 -07006571
6572 if (opcode != IORING_UNREGISTER_FILES &&
6573 opcode != IORING_REGISTER_FILES_UPDATE) {
6574 /* bring the ctx back to life */
6575 reinit_completion(&ctx->completions[0]);
6576 percpu_ref_reinit(&ctx->refs);
6577 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006578 return ret;
6579}
6580
6581SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6582 void __user *, arg, unsigned int, nr_args)
6583{
6584 struct io_ring_ctx *ctx;
6585 long ret = -EBADF;
6586 struct fd f;
6587
6588 f = fdget(fd);
6589 if (!f.file)
6590 return -EBADF;
6591
6592 ret = -EOPNOTSUPP;
6593 if (f.file->f_op != &io_uring_fops)
6594 goto out_fput;
6595
6596 ctx = f.file->private_data;
6597
6598 mutex_lock(&ctx->uring_lock);
6599 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6600 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006601 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6602 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006603out_fput:
6604 fdput(f);
6605 return ret;
6606}
6607
Jens Axboe2b188cc2019-01-07 10:46:33 -07006608static int __init io_uring_init(void)
6609{
Jens Axboed3656342019-12-18 09:50:26 -07006610 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006611 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6612 return 0;
6613};
6614__initcall(io_uring_init);