blob: 2c036972930fee5ee0218abb50b063350c0775c2 [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
YueHaibing96fd84d2020-01-07 22:22:44 +08001163 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001164 if (req->file) {
1165 if (req->flags & REQ_F_FIXED_FILE)
1166 percpu_ref_put(&ctx->file_data->refs);
1167 else
1168 fput(req->file);
1169 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001170}
1171
1172static void __io_free_req(struct io_kiocb *req)
1173{
1174 __io_req_aux_free(req);
1175
Jens Axboefcb323c2019-10-24 12:39:47 -06001176 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001177 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001178 unsigned long flags;
1179
1180 spin_lock_irqsave(&ctx->inflight_lock, flags);
1181 list_del(&req->inflight_entry);
1182 if (waitqueue_active(&ctx->inflight_wait))
1183 wake_up(&ctx->inflight_wait);
1184 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1185 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001186
1187 percpu_ref_put(&req->ctx->refs);
1188 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001189}
1190
Jens Axboec6ca97b302019-12-28 12:11:08 -07001191struct req_batch {
1192 void *reqs[IO_IOPOLL_BATCH];
1193 int to_free;
1194 int need_iter;
1195};
1196
1197static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1198{
1199 if (!rb->to_free)
1200 return;
1201 if (rb->need_iter) {
1202 int i, inflight = 0;
1203 unsigned long flags;
1204
1205 for (i = 0; i < rb->to_free; i++) {
1206 struct io_kiocb *req = rb->reqs[i];
1207
1208 if (req->flags & REQ_F_FIXED_FILE)
1209 req->file = NULL;
1210 if (req->flags & REQ_F_INFLIGHT)
1211 inflight++;
1212 else
1213 rb->reqs[i] = NULL;
1214 __io_req_aux_free(req);
1215 }
1216 if (!inflight)
1217 goto do_free;
1218
1219 spin_lock_irqsave(&ctx->inflight_lock, flags);
1220 for (i = 0; i < rb->to_free; i++) {
1221 struct io_kiocb *req = rb->reqs[i];
1222
1223 if (req) {
1224 list_del(&req->inflight_entry);
1225 if (!--inflight)
1226 break;
1227 }
1228 }
1229 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1230
1231 if (waitqueue_active(&ctx->inflight_wait))
1232 wake_up(&ctx->inflight_wait);
1233 }
1234do_free:
1235 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1236 percpu_ref_put_many(&ctx->refs, rb->to_free);
1237 percpu_ref_put_many(&ctx->file_data->refs, rb->to_free);
1238 rb->to_free = rb->need_iter = 0;
1239}
1240
Jackie Liua197f662019-11-08 08:09:12 -07001241static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001242{
Jackie Liua197f662019-11-08 08:09:12 -07001243 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001244 int ret;
1245
Jens Axboe2d283902019-12-04 11:08:05 -07001246 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001247 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001248 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001249 io_commit_cqring(ctx);
1250 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001251 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001252 return true;
1253 }
1254
1255 return false;
1256}
1257
Jens Axboeba816ad2019-09-28 11:36:45 -06001258static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001259{
Jens Axboe2665abf2019-11-05 12:40:47 -07001260 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001261 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001262
Jens Axboe4d7dd462019-11-20 13:03:52 -07001263 /* Already got next link */
1264 if (req->flags & REQ_F_LINK_NEXT)
1265 return;
1266
Jens Axboe9e645e112019-05-10 16:07:28 -06001267 /*
1268 * The list should never be empty when we are called here. But could
1269 * potentially happen if the chain is messed up, check to be on the
1270 * safe side.
1271 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001272 while (!list_empty(&req->link_list)) {
1273 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1274 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001275
Pavel Begunkov44932332019-12-05 16:16:35 +03001276 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1277 (nxt->flags & REQ_F_TIMEOUT))) {
1278 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001279 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001280 req->flags &= ~REQ_F_LINK_TIMEOUT;
1281 continue;
1282 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001283
Pavel Begunkov44932332019-12-05 16:16:35 +03001284 list_del_init(&req->link_list);
1285 if (!list_empty(&nxt->link_list))
1286 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001287 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001288 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001289 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001290
Jens Axboe4d7dd462019-11-20 13:03:52 -07001291 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001292 if (wake_ev)
1293 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001294}
1295
1296/*
1297 * Called if REQ_F_LINK is set, and we fail the head request
1298 */
1299static void io_fail_links(struct io_kiocb *req)
1300{
Jens Axboe2665abf2019-11-05 12:40:47 -07001301 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001302 unsigned long flags;
1303
1304 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001305
1306 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001307 struct io_kiocb *link = list_first_entry(&req->link_list,
1308 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001309
Pavel Begunkov44932332019-12-05 16:16:35 +03001310 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001311 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001312
1313 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001314 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001315 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001316 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001317 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001318 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001319 }
Jens Axboe5d960722019-11-19 15:31:28 -07001320 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001321 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001322
1323 io_commit_cqring(ctx);
1324 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1325 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001326}
1327
Jens Axboe4d7dd462019-11-20 13:03:52 -07001328static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001329{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001330 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001331 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001332
Jens Axboe9e645e112019-05-10 16:07:28 -06001333 /*
1334 * If LINK is set, we have dependent requests in this chain. If we
1335 * didn't fail this request, queue the first one up, moving any other
1336 * dependencies to the next request. In case of failure, fail the rest
1337 * of the chain.
1338 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001339 if (req->flags & REQ_F_FAIL_LINK) {
1340 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001341 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1342 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001343 struct io_ring_ctx *ctx = req->ctx;
1344 unsigned long flags;
1345
1346 /*
1347 * If this is a timeout link, we could be racing with the
1348 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001349 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001350 */
1351 spin_lock_irqsave(&ctx->completion_lock, flags);
1352 io_req_link_next(req, nxt);
1353 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1354 } else {
1355 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001356 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001357}
Jens Axboe9e645e112019-05-10 16:07:28 -06001358
Jackie Liuc69f8db2019-11-09 11:00:08 +08001359static void io_free_req(struct io_kiocb *req)
1360{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001361 struct io_kiocb *nxt = NULL;
1362
1363 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001364 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001365
1366 if (nxt)
1367 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001368}
1369
Jens Axboeba816ad2019-09-28 11:36:45 -06001370/*
1371 * Drop reference to request, return next in chain (if there is one) if this
1372 * was the last reference to this request.
1373 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001374__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001375static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001376{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001377 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001378
Jens Axboee65ef562019-03-12 10:16:44 -06001379 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001380 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001381}
1382
Jens Axboe2b188cc2019-01-07 10:46:33 -07001383static void io_put_req(struct io_kiocb *req)
1384{
Jens Axboedef596e2019-01-09 08:59:42 -07001385 if (refcount_dec_and_test(&req->refs))
1386 io_free_req(req);
1387}
1388
Jens Axboe978db572019-11-14 22:39:04 -07001389/*
1390 * Must only be used if we don't need to care about links, usually from
1391 * within the completion handling itself.
1392 */
1393static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001394{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001395 /* drop both submit and complete references */
1396 if (refcount_sub_and_test(2, &req->refs))
1397 __io_free_req(req);
1398}
1399
Jens Axboe978db572019-11-14 22:39:04 -07001400static void io_double_put_req(struct io_kiocb *req)
1401{
1402 /* drop both submit and complete references */
1403 if (refcount_sub_and_test(2, &req->refs))
1404 io_free_req(req);
1405}
1406
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001407static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001408{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001409 struct io_rings *rings = ctx->rings;
1410
Jens Axboead3eb2c2019-12-18 17:12:20 -07001411 if (test_bit(0, &ctx->cq_check_overflow)) {
1412 /*
1413 * noflush == true is from the waitqueue handler, just ensure
1414 * we wake up the task, and the next invocation will flush the
1415 * entries. We cannot safely to it from here.
1416 */
1417 if (noflush && !list_empty(&ctx->cq_overflow_list))
1418 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001419
Jens Axboead3eb2c2019-12-18 17:12:20 -07001420 io_cqring_overflow_flush(ctx, false);
1421 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001422
Jens Axboea3a0e432019-08-20 11:03:11 -06001423 /* See comment at the top of this file */
1424 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001425 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001426}
1427
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001428static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1429{
1430 struct io_rings *rings = ctx->rings;
1431
1432 /* make sure SQ entry isn't read before tail */
1433 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1434}
1435
Jens Axboe8237e042019-12-28 10:48:22 -07001436static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001437{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001438 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1439 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001440
Jens Axboec6ca97b302019-12-28 12:11:08 -07001441 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1442 rb->need_iter++;
1443
1444 rb->reqs[rb->to_free++] = req;
1445 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1446 io_free_req_many(req->ctx, rb);
1447 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001448}
1449
Jens Axboedef596e2019-01-09 08:59:42 -07001450/*
1451 * Find and free completed poll iocbs
1452 */
1453static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1454 struct list_head *done)
1455{
Jens Axboe8237e042019-12-28 10:48:22 -07001456 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001457 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001458
Jens Axboec6ca97b302019-12-28 12:11:08 -07001459 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001460 while (!list_empty(done)) {
1461 req = list_first_entry(done, struct io_kiocb, list);
1462 list_del(&req->list);
1463
Jens Axboe78e19bb2019-11-06 15:21:34 -07001464 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001465 (*nr_events)++;
1466
Jens Axboe8237e042019-12-28 10:48:22 -07001467 if (refcount_dec_and_test(&req->refs) &&
1468 !io_req_multi_free(&rb, req))
1469 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001470 }
Jens Axboedef596e2019-01-09 08:59:42 -07001471
Jens Axboe09bb8392019-03-13 12:39:28 -06001472 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001473 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001474}
1475
1476static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1477 long min)
1478{
1479 struct io_kiocb *req, *tmp;
1480 LIST_HEAD(done);
1481 bool spin;
1482 int ret;
1483
1484 /*
1485 * Only spin for completions if we don't have multiple devices hanging
1486 * off our complete list, and we're under the requested amount.
1487 */
1488 spin = !ctx->poll_multi_file && *nr_events < min;
1489
1490 ret = 0;
1491 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001492 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001493
1494 /*
1495 * Move completed entries to our local list. If we find a
1496 * request that requires polling, break out and complete
1497 * the done list first, if we have entries there.
1498 */
1499 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1500 list_move_tail(&req->list, &done);
1501 continue;
1502 }
1503 if (!list_empty(&done))
1504 break;
1505
1506 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1507 if (ret < 0)
1508 break;
1509
1510 if (ret && spin)
1511 spin = false;
1512 ret = 0;
1513 }
1514
1515 if (!list_empty(&done))
1516 io_iopoll_complete(ctx, nr_events, &done);
1517
1518 return ret;
1519}
1520
1521/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001522 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001523 * non-spinning poll check - we'll still enter the driver poll loop, but only
1524 * as a non-spinning completion check.
1525 */
1526static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1527 long min)
1528{
Jens Axboe08f54392019-08-21 22:19:11 -06001529 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001530 int ret;
1531
1532 ret = io_do_iopoll(ctx, nr_events, min);
1533 if (ret < 0)
1534 return ret;
1535 if (!min || *nr_events >= min)
1536 return 0;
1537 }
1538
1539 return 1;
1540}
1541
1542/*
1543 * We can't just wait for polled events to come to us, we have to actively
1544 * find and complete them.
1545 */
1546static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1547{
1548 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1549 return;
1550
1551 mutex_lock(&ctx->uring_lock);
1552 while (!list_empty(&ctx->poll_list)) {
1553 unsigned int nr_events = 0;
1554
1555 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001556
1557 /*
1558 * Ensure we allow local-to-the-cpu processing to take place,
1559 * in this case we need to ensure that we reap all events.
1560 */
1561 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001562 }
1563 mutex_unlock(&ctx->uring_lock);
1564}
1565
Jens Axboe2b2ed972019-10-25 10:06:15 -06001566static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1567 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001568{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001569 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001570
1571 do {
1572 int tmin = 0;
1573
Jens Axboe500f9fb2019-08-19 12:15:59 -06001574 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001575 * Don't enter poll loop if we already have events pending.
1576 * If we do, we can potentially be spinning for commands that
1577 * already triggered a CQE (eg in error).
1578 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001579 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001580 break;
1581
1582 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001583 * If a submit got punted to a workqueue, we can have the
1584 * application entering polling for a command before it gets
1585 * issued. That app will hold the uring_lock for the duration
1586 * of the poll right here, so we need to take a breather every
1587 * now and then to ensure that the issue has a chance to add
1588 * the poll to the issued list. Otherwise we can spin here
1589 * forever, while the workqueue is stuck trying to acquire the
1590 * very same mutex.
1591 */
1592 if (!(++iters & 7)) {
1593 mutex_unlock(&ctx->uring_lock);
1594 mutex_lock(&ctx->uring_lock);
1595 }
1596
Jens Axboedef596e2019-01-09 08:59:42 -07001597 if (*nr_events < min)
1598 tmin = min - *nr_events;
1599
1600 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1601 if (ret <= 0)
1602 break;
1603 ret = 0;
1604 } while (min && !*nr_events && !need_resched());
1605
Jens Axboe2b2ed972019-10-25 10:06:15 -06001606 return ret;
1607}
1608
1609static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1610 long min)
1611{
1612 int ret;
1613
1614 /*
1615 * We disallow the app entering submit/complete with polling, but we
1616 * still need to lock the ring to prevent racing with polled issue
1617 * that got punted to a workqueue.
1618 */
1619 mutex_lock(&ctx->uring_lock);
1620 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001621 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001622 return ret;
1623}
1624
Jens Axboe491381ce2019-10-17 09:20:46 -06001625static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001626{
Jens Axboe491381ce2019-10-17 09:20:46 -06001627 /*
1628 * Tell lockdep we inherited freeze protection from submission
1629 * thread.
1630 */
1631 if (req->flags & REQ_F_ISREG) {
1632 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001633
Jens Axboe491381ce2019-10-17 09:20:46 -06001634 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001635 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001636 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001637}
1638
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001639static inline void req_set_fail_links(struct io_kiocb *req)
1640{
1641 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1642 req->flags |= REQ_F_FAIL_LINK;
1643}
1644
Jens Axboeba816ad2019-09-28 11:36:45 -06001645static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001646{
Jens Axboe9adbd452019-12-20 08:45:55 -07001647 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001648
Jens Axboe491381ce2019-10-17 09:20:46 -06001649 if (kiocb->ki_flags & IOCB_WRITE)
1650 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001651
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001652 if (res != req->result)
1653 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001654 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001655}
1656
1657static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1658{
Jens Axboe9adbd452019-12-20 08:45:55 -07001659 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001660
1661 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001662 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001663}
1664
Jens Axboeba816ad2019-09-28 11:36:45 -06001665static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1666{
Jens Axboe9adbd452019-12-20 08:45:55 -07001667 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001668 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001669
1670 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001671 io_put_req_find_next(req, &nxt);
1672
1673 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674}
1675
Jens Axboedef596e2019-01-09 08:59:42 -07001676static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1677{
Jens Axboe9adbd452019-12-20 08:45:55 -07001678 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001679
Jens Axboe491381ce2019-10-17 09:20:46 -06001680 if (kiocb->ki_flags & IOCB_WRITE)
1681 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001682
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001683 if (res != req->result)
1684 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001685 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001686 if (res != -EAGAIN)
1687 req->flags |= REQ_F_IOPOLL_COMPLETED;
1688}
1689
1690/*
1691 * After the iocb has been issued, it's safe to be found on the poll list.
1692 * Adding the kiocb to the list AFTER submission ensures that we don't
1693 * find it from a io_iopoll_getevents() thread before the issuer is done
1694 * accessing the kiocb cookie.
1695 */
1696static void io_iopoll_req_issued(struct io_kiocb *req)
1697{
1698 struct io_ring_ctx *ctx = req->ctx;
1699
1700 /*
1701 * Track whether we have multiple files in our lists. This will impact
1702 * how we do polling eventually, not spinning if we're on potentially
1703 * different devices.
1704 */
1705 if (list_empty(&ctx->poll_list)) {
1706 ctx->poll_multi_file = false;
1707 } else if (!ctx->poll_multi_file) {
1708 struct io_kiocb *list_req;
1709
1710 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1711 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001712 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001713 ctx->poll_multi_file = true;
1714 }
1715
1716 /*
1717 * For fast devices, IO may have already completed. If it has, add
1718 * it to the front so we find it first.
1719 */
1720 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1721 list_add(&req->list, &ctx->poll_list);
1722 else
1723 list_add_tail(&req->list, &ctx->poll_list);
1724}
1725
Jens Axboe3d6770f2019-04-13 11:50:54 -06001726static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001727{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001728 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001729 int diff = state->has_refs - state->used_refs;
1730
1731 if (diff)
1732 fput_many(state->file, diff);
1733 state->file = NULL;
1734 }
1735}
1736
1737/*
1738 * Get as many references to a file as we have IOs left in this submission,
1739 * assuming most submissions are for one file, or at least that each file
1740 * has more than one submission.
1741 */
1742static struct file *io_file_get(struct io_submit_state *state, int fd)
1743{
1744 if (!state)
1745 return fget(fd);
1746
1747 if (state->file) {
1748 if (state->fd == fd) {
1749 state->used_refs++;
1750 state->ios_left--;
1751 return state->file;
1752 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001753 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001754 }
1755 state->file = fget_many(fd, state->ios_left);
1756 if (!state->file)
1757 return NULL;
1758
1759 state->fd = fd;
1760 state->has_refs = state->ios_left;
1761 state->used_refs = 1;
1762 state->ios_left--;
1763 return state->file;
1764}
1765
Jens Axboe2b188cc2019-01-07 10:46:33 -07001766/*
1767 * If we tracked the file through the SCM inflight mechanism, we could support
1768 * any file. For now, just ensure that anything potentially problematic is done
1769 * inline.
1770 */
1771static bool io_file_supports_async(struct file *file)
1772{
1773 umode_t mode = file_inode(file)->i_mode;
1774
Jens Axboe10d59342019-12-09 20:16:22 -07001775 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001776 return true;
1777 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1778 return true;
1779
1780 return false;
1781}
1782
Jens Axboe3529d8c2019-12-19 18:24:38 -07001783static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1784 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001785{
Jens Axboedef596e2019-01-09 08:59:42 -07001786 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001787 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001788 unsigned ioprio;
1789 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001790
Jens Axboe09bb8392019-03-13 12:39:28 -06001791 if (!req->file)
1792 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001793
Jens Axboe491381ce2019-10-17 09:20:46 -06001794 if (S_ISREG(file_inode(req->file)->i_mode))
1795 req->flags |= REQ_F_ISREG;
1796
Jens Axboe2b188cc2019-01-07 10:46:33 -07001797 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001798 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1799 req->flags |= REQ_F_CUR_POS;
1800 kiocb->ki_pos = req->file->f_pos;
1801 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001802 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1803 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1804
1805 ioprio = READ_ONCE(sqe->ioprio);
1806 if (ioprio) {
1807 ret = ioprio_check_cap(ioprio);
1808 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001809 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001810
1811 kiocb->ki_ioprio = ioprio;
1812 } else
1813 kiocb->ki_ioprio = get_current_ioprio();
1814
1815 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1816 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001817 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001818
1819 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001820 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1821 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001822 req->flags |= REQ_F_NOWAIT;
1823
1824 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001825 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001826
Jens Axboedef596e2019-01-09 08:59:42 -07001827 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001828 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1829 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001830 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001831
Jens Axboedef596e2019-01-09 08:59:42 -07001832 kiocb->ki_flags |= IOCB_HIPRI;
1833 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001834 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001835 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001836 if (kiocb->ki_flags & IOCB_HIPRI)
1837 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001838 kiocb->ki_complete = io_complete_rw;
1839 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001840
Jens Axboe3529d8c2019-12-19 18:24:38 -07001841 req->rw.addr = READ_ONCE(sqe->addr);
1842 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001843 /* we own ->private, reuse it for the buffer index */
1844 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001845 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001846 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001847}
1848
1849static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1850{
1851 switch (ret) {
1852 case -EIOCBQUEUED:
1853 break;
1854 case -ERESTARTSYS:
1855 case -ERESTARTNOINTR:
1856 case -ERESTARTNOHAND:
1857 case -ERESTART_RESTARTBLOCK:
1858 /*
1859 * We can't just restart the syscall, since previously
1860 * submitted sqes may already be in progress. Just fail this
1861 * IO with EINTR.
1862 */
1863 ret = -EINTR;
1864 /* fall through */
1865 default:
1866 kiocb->ki_complete(kiocb, ret, 0);
1867 }
1868}
1869
Jens Axboeba816ad2019-09-28 11:36:45 -06001870static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1871 bool in_async)
1872{
Jens Axboeba042912019-12-25 16:33:42 -07001873 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1874
1875 if (req->flags & REQ_F_CUR_POS)
1876 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001877 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001878 *nxt = __io_complete_rw(kiocb, ret);
1879 else
1880 io_rw_done(kiocb, ret);
1881}
1882
Jens Axboe9adbd452019-12-20 08:45:55 -07001883static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001884 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001885{
Jens Axboe9adbd452019-12-20 08:45:55 -07001886 struct io_ring_ctx *ctx = req->ctx;
1887 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001888 struct io_mapped_ubuf *imu;
1889 unsigned index, buf_index;
1890 size_t offset;
1891 u64 buf_addr;
1892
1893 /* attempt to use fixed buffers without having provided iovecs */
1894 if (unlikely(!ctx->user_bufs))
1895 return -EFAULT;
1896
Jens Axboe9adbd452019-12-20 08:45:55 -07001897 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001898 if (unlikely(buf_index >= ctx->nr_user_bufs))
1899 return -EFAULT;
1900
1901 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1902 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001903 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001904
1905 /* overflow */
1906 if (buf_addr + len < buf_addr)
1907 return -EFAULT;
1908 /* not inside the mapped region */
1909 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1910 return -EFAULT;
1911
1912 /*
1913 * May not be a start of buffer, set size appropriately
1914 * and advance us to the beginning.
1915 */
1916 offset = buf_addr - imu->ubuf;
1917 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001918
1919 if (offset) {
1920 /*
1921 * Don't use iov_iter_advance() here, as it's really slow for
1922 * using the latter parts of a big fixed buffer - it iterates
1923 * over each segment manually. We can cheat a bit here, because
1924 * we know that:
1925 *
1926 * 1) it's a BVEC iter, we set it up
1927 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1928 * first and last bvec
1929 *
1930 * So just find our index, and adjust the iterator afterwards.
1931 * If the offset is within the first bvec (or the whole first
1932 * bvec, just use iov_iter_advance(). This makes it easier
1933 * since we can just skip the first segment, which may not
1934 * be PAGE_SIZE aligned.
1935 */
1936 const struct bio_vec *bvec = imu->bvec;
1937
1938 if (offset <= bvec->bv_len) {
1939 iov_iter_advance(iter, offset);
1940 } else {
1941 unsigned long seg_skip;
1942
1943 /* skip first vec */
1944 offset -= bvec->bv_len;
1945 seg_skip = 1 + (offset >> PAGE_SHIFT);
1946
1947 iter->bvec = bvec + seg_skip;
1948 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001949 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001950 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001951 }
1952 }
1953
Jens Axboe5e559562019-11-13 16:12:46 -07001954 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001955}
1956
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001957static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1958 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001959{
Jens Axboe9adbd452019-12-20 08:45:55 -07001960 void __user *buf = u64_to_user_ptr(req->rw.addr);
1961 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001962 u8 opcode;
1963
Jens Axboed625c6e2019-12-17 19:53:05 -07001964 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001965 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001966 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001967 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001968 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001969
Jens Axboe9adbd452019-12-20 08:45:55 -07001970 /* buffer index only valid with fixed read/write */
1971 if (req->rw.kiocb.private)
1972 return -EINVAL;
1973
Jens Axboe3a6820f2019-12-22 15:19:35 -07001974 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1975 ssize_t ret;
1976 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1977 *iovec = NULL;
1978 return ret;
1979 }
1980
Jens Axboef67676d2019-12-02 11:03:47 -07001981 if (req->io) {
1982 struct io_async_rw *iorw = &req->io->rw;
1983
1984 *iovec = iorw->iov;
1985 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1986 if (iorw->iov == iorw->fast_iov)
1987 *iovec = NULL;
1988 return iorw->size;
1989 }
1990
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001991 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001992 return -EFAULT;
1993
1994#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001995 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001996 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1997 iovec, iter);
1998#endif
1999
2000 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2001}
2002
Jens Axboe32960612019-09-23 11:05:34 -06002003/*
2004 * For files that don't have ->read_iter() and ->write_iter(), handle them
2005 * by looping over ->read() or ->write() manually.
2006 */
2007static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2008 struct iov_iter *iter)
2009{
2010 ssize_t ret = 0;
2011
2012 /*
2013 * Don't support polled IO through this interface, and we can't
2014 * support non-blocking either. For the latter, this just causes
2015 * the kiocb to be handled from an async context.
2016 */
2017 if (kiocb->ki_flags & IOCB_HIPRI)
2018 return -EOPNOTSUPP;
2019 if (kiocb->ki_flags & IOCB_NOWAIT)
2020 return -EAGAIN;
2021
2022 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002023 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002024 ssize_t nr;
2025
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002026 if (!iov_iter_is_bvec(iter)) {
2027 iovec = iov_iter_iovec(iter);
2028 } else {
2029 /* fixed buffers import bvec */
2030 iovec.iov_base = kmap(iter->bvec->bv_page)
2031 + iter->iov_offset;
2032 iovec.iov_len = min(iter->count,
2033 iter->bvec->bv_len - iter->iov_offset);
2034 }
2035
Jens Axboe32960612019-09-23 11:05:34 -06002036 if (rw == READ) {
2037 nr = file->f_op->read(file, iovec.iov_base,
2038 iovec.iov_len, &kiocb->ki_pos);
2039 } else {
2040 nr = file->f_op->write(file, iovec.iov_base,
2041 iovec.iov_len, &kiocb->ki_pos);
2042 }
2043
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002044 if (iov_iter_is_bvec(iter))
2045 kunmap(iter->bvec->bv_page);
2046
Jens Axboe32960612019-09-23 11:05:34 -06002047 if (nr < 0) {
2048 if (!ret)
2049 ret = nr;
2050 break;
2051 }
2052 ret += nr;
2053 if (nr != iovec.iov_len)
2054 break;
2055 iov_iter_advance(iter, nr);
2056 }
2057
2058 return ret;
2059}
2060
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002061static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002062 struct iovec *iovec, struct iovec *fast_iov,
2063 struct iov_iter *iter)
2064{
2065 req->io->rw.nr_segs = iter->nr_segs;
2066 req->io->rw.size = io_size;
2067 req->io->rw.iov = iovec;
2068 if (!req->io->rw.iov) {
2069 req->io->rw.iov = req->io->rw.fast_iov;
2070 memcpy(req->io->rw.iov, fast_iov,
2071 sizeof(struct iovec) * iter->nr_segs);
2072 }
2073}
2074
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002075static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002076{
Jens Axboed3656342019-12-18 09:50:26 -07002077 if (!io_op_defs[req->opcode].async_ctx)
2078 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002079 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002080 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002081}
2082
2083static void io_rw_async(struct io_wq_work **workptr)
2084{
2085 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2086 struct iovec *iov = NULL;
2087
2088 if (req->io->rw.iov != req->io->rw.fast_iov)
2089 iov = req->io->rw.iov;
2090 io_wq_submit_work(workptr);
2091 kfree(iov);
2092}
2093
2094static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2095 struct iovec *iovec, struct iovec *fast_iov,
2096 struct iov_iter *iter)
2097{
Jens Axboe74566df2020-01-13 19:23:24 -07002098 if (req->opcode == IORING_OP_READ_FIXED ||
2099 req->opcode == IORING_OP_WRITE_FIXED)
2100 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002101 if (!req->io && io_alloc_async_ctx(req))
2102 return -ENOMEM;
2103
2104 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2105 req->work.func = io_rw_async;
2106 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002107}
2108
Jens Axboe3529d8c2019-12-19 18:24:38 -07002109static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2110 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002111{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002112 struct io_async_ctx *io;
2113 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002114 ssize_t ret;
2115
Jens Axboe3529d8c2019-12-19 18:24:38 -07002116 ret = io_prep_rw(req, sqe, force_nonblock);
2117 if (ret)
2118 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002119
Jens Axboe3529d8c2019-12-19 18:24:38 -07002120 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2121 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002122
Jens Axboe3529d8c2019-12-19 18:24:38 -07002123 if (!req->io)
2124 return 0;
2125
2126 io = req->io;
2127 io->rw.iov = io->rw.fast_iov;
2128 req->io = NULL;
2129 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2130 req->io = io;
2131 if (ret < 0)
2132 return ret;
2133
2134 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2135 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002136}
2137
Pavel Begunkov267bc902019-11-07 01:41:08 +03002138static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002139 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002140{
2141 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002142 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002143 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002144 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002145 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002146
Jens Axboe3529d8c2019-12-19 18:24:38 -07002147 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002148 if (ret < 0)
2149 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002150
Jens Axboefd6c2e42019-12-18 12:19:41 -07002151 /* Ensure we clear previously set non-block flag */
2152 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002153 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002154
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002155 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002156 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002157 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002158 req->result = io_size;
2159
2160 /*
2161 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2162 * we know to async punt it even if it was opened O_NONBLOCK
2163 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002164 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002165 req->flags |= REQ_F_MUST_PUNT;
2166 goto copy_iov;
2167 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002168
Jens Axboe31b51512019-01-18 22:56:34 -07002169 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002170 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002171 if (!ret) {
2172 ssize_t ret2;
2173
Jens Axboe9adbd452019-12-20 08:45:55 -07002174 if (req->file->f_op->read_iter)
2175 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002176 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002177 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002178
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002179 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002180 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002181 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002182 } else {
2183copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002184 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002185 inline_vecs, &iter);
2186 if (ret)
2187 goto out_free;
2188 return -EAGAIN;
2189 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002190 }
Jens Axboef67676d2019-12-02 11:03:47 -07002191out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002192 if (!io_wq_current_is_worker())
2193 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002194 return ret;
2195}
2196
Jens Axboe3529d8c2019-12-19 18:24:38 -07002197static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2198 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002199{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002200 struct io_async_ctx *io;
2201 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002202 ssize_t ret;
2203
Jens Axboe3529d8c2019-12-19 18:24:38 -07002204 ret = io_prep_rw(req, sqe, force_nonblock);
2205 if (ret)
2206 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002207
Jens Axboe3529d8c2019-12-19 18:24:38 -07002208 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2209 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002210
Jens Axboe3529d8c2019-12-19 18:24:38 -07002211 if (!req->io)
2212 return 0;
2213
2214 io = req->io;
2215 io->rw.iov = io->rw.fast_iov;
2216 req->io = NULL;
2217 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2218 req->io = io;
2219 if (ret < 0)
2220 return ret;
2221
2222 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2223 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002224}
2225
Pavel Begunkov267bc902019-11-07 01:41:08 +03002226static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002227 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002228{
2229 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002230 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002231 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002232 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002233 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002234
Jens Axboe3529d8c2019-12-19 18:24:38 -07002235 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002236 if (ret < 0)
2237 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002238
Jens Axboefd6c2e42019-12-18 12:19:41 -07002239 /* Ensure we clear previously set non-block flag */
2240 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002241 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002242
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002243 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002244 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002245 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002246 req->result = io_size;
2247
2248 /*
2249 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2250 * we know to async punt it even if it was opened O_NONBLOCK
2251 */
2252 if (force_nonblock && !io_file_supports_async(req->file)) {
2253 req->flags |= REQ_F_MUST_PUNT;
2254 goto copy_iov;
2255 }
2256
Jens Axboe10d59342019-12-09 20:16:22 -07002257 /* file path doesn't support NOWAIT for non-direct_IO */
2258 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2259 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002260 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002261
Jens Axboe31b51512019-01-18 22:56:34 -07002262 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002263 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002264 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002265 ssize_t ret2;
2266
Jens Axboe2b188cc2019-01-07 10:46:33 -07002267 /*
2268 * Open-code file_start_write here to grab freeze protection,
2269 * which will be released by another thread in
2270 * io_complete_rw(). Fool lockdep by telling it the lock got
2271 * released so that it doesn't complain about the held lock when
2272 * we return to userspace.
2273 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002274 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002275 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002276 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002277 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002278 SB_FREEZE_WRITE);
2279 }
2280 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002281
Jens Axboe9adbd452019-12-20 08:45:55 -07002282 if (req->file->f_op->write_iter)
2283 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002284 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002285 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002286 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002287 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002288 } else {
2289copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002290 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002291 inline_vecs, &iter);
2292 if (ret)
2293 goto out_free;
2294 return -EAGAIN;
2295 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002296 }
Jens Axboe31b51512019-01-18 22:56:34 -07002297out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002298 if (!io_wq_current_is_worker())
2299 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002300 return ret;
2301}
2302
2303/*
2304 * IORING_OP_NOP just posts a completion event, nothing else.
2305 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002306static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002307{
2308 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002309
Jens Axboedef596e2019-01-09 08:59:42 -07002310 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2311 return -EINVAL;
2312
Jens Axboe78e19bb2019-11-06 15:21:34 -07002313 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002314 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002315 return 0;
2316}
2317
Jens Axboe3529d8c2019-12-19 18:24:38 -07002318static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002319{
Jens Axboe6b063142019-01-10 22:13:58 -07002320 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002321
Jens Axboe09bb8392019-03-13 12:39:28 -06002322 if (!req->file)
2323 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002324
Jens Axboe6b063142019-01-10 22:13:58 -07002325 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002326 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002327 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002328 return -EINVAL;
2329
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002330 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2331 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2332 return -EINVAL;
2333
2334 req->sync.off = READ_ONCE(sqe->off);
2335 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002336 return 0;
2337}
2338
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002339static bool io_req_cancelled(struct io_kiocb *req)
2340{
2341 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2342 req_set_fail_links(req);
2343 io_cqring_add_event(req, -ECANCELED);
2344 io_put_req(req);
2345 return true;
2346 }
2347
2348 return false;
2349}
2350
Jens Axboe78912932020-01-14 22:09:06 -07002351static void io_link_work_cb(struct io_wq_work **workptr)
2352{
2353 struct io_wq_work *work = *workptr;
2354 struct io_kiocb *link = work->data;
2355
2356 io_queue_linked_timeout(link);
2357 work->func = io_wq_submit_work;
2358}
2359
2360static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2361{
2362 struct io_kiocb *link;
2363
2364 io_prep_async_work(nxt, &link);
2365 *workptr = &nxt->work;
2366 if (link) {
2367 nxt->work.flags |= IO_WQ_WORK_CB;
2368 nxt->work.func = io_link_work_cb;
2369 nxt->work.data = link;
2370 }
2371}
2372
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002373static void io_fsync_finish(struct io_wq_work **workptr)
2374{
2375 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2376 loff_t end = req->sync.off + req->sync.len;
2377 struct io_kiocb *nxt = NULL;
2378 int ret;
2379
2380 if (io_req_cancelled(req))
2381 return;
2382
Jens Axboe9adbd452019-12-20 08:45:55 -07002383 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002384 end > 0 ? end : LLONG_MAX,
2385 req->sync.flags & IORING_FSYNC_DATASYNC);
2386 if (ret < 0)
2387 req_set_fail_links(req);
2388 io_cqring_add_event(req, ret);
2389 io_put_req_find_next(req, &nxt);
2390 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002391 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002392}
2393
Jens Axboefc4df992019-12-10 14:38:45 -07002394static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2395 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002396{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002397 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002398
2399 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002400 if (force_nonblock) {
2401 io_put_req(req);
2402 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002403 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002404 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002405
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002406 work = old_work = &req->work;
2407 io_fsync_finish(&work);
2408 if (work && work != old_work)
2409 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002410 return 0;
2411}
2412
Jens Axboed63d1b52019-12-10 10:38:56 -07002413static void io_fallocate_finish(struct io_wq_work **workptr)
2414{
2415 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2416 struct io_kiocb *nxt = NULL;
2417 int ret;
2418
2419 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2420 req->sync.len);
2421 if (ret < 0)
2422 req_set_fail_links(req);
2423 io_cqring_add_event(req, ret);
2424 io_put_req_find_next(req, &nxt);
2425 if (nxt)
2426 io_wq_assign_next(workptr, nxt);
2427}
2428
2429static int io_fallocate_prep(struct io_kiocb *req,
2430 const struct io_uring_sqe *sqe)
2431{
2432 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2433 return -EINVAL;
2434
2435 req->sync.off = READ_ONCE(sqe->off);
2436 req->sync.len = READ_ONCE(sqe->addr);
2437 req->sync.mode = READ_ONCE(sqe->len);
2438 return 0;
2439}
2440
2441static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2442 bool force_nonblock)
2443{
2444 struct io_wq_work *work, *old_work;
2445
2446 /* fallocate always requiring blocking context */
2447 if (force_nonblock) {
2448 io_put_req(req);
2449 req->work.func = io_fallocate_finish;
2450 return -EAGAIN;
2451 }
2452
2453 work = old_work = &req->work;
2454 io_fallocate_finish(&work);
2455 if (work && work != old_work)
2456 *nxt = container_of(work, struct io_kiocb, work);
2457
2458 return 0;
2459}
2460
Jens Axboe15b71ab2019-12-11 11:20:36 -07002461static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2462{
2463 int ret;
2464
2465 if (sqe->ioprio || sqe->buf_index)
2466 return -EINVAL;
2467
2468 req->open.dfd = READ_ONCE(sqe->fd);
2469 req->open.mode = READ_ONCE(sqe->len);
2470 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2471 req->open.flags = READ_ONCE(sqe->open_flags);
2472
2473 req->open.filename = getname(req->open.fname);
2474 if (IS_ERR(req->open.filename)) {
2475 ret = PTR_ERR(req->open.filename);
2476 req->open.filename = NULL;
2477 return ret;
2478 }
2479
2480 return 0;
2481}
2482
2483static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2484 bool force_nonblock)
2485{
2486 struct open_flags op;
2487 struct open_how how;
2488 struct file *file;
2489 int ret;
2490
2491 if (force_nonblock) {
2492 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2493 return -EAGAIN;
2494 }
2495
2496 how = build_open_how(req->open.flags, req->open.mode);
2497 ret = build_open_flags(&how, &op);
2498 if (ret)
2499 goto err;
2500
2501 ret = get_unused_fd_flags(how.flags);
2502 if (ret < 0)
2503 goto err;
2504
2505 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2506 if (IS_ERR(file)) {
2507 put_unused_fd(ret);
2508 ret = PTR_ERR(file);
2509 } else {
2510 fsnotify_open(file);
2511 fd_install(ret, file);
2512 }
2513err:
2514 putname(req->open.filename);
2515 if (ret < 0)
2516 req_set_fail_links(req);
2517 io_cqring_add_event(req, ret);
2518 io_put_req_find_next(req, nxt);
2519 return 0;
2520}
2521
Jens Axboec1ca7572019-12-25 22:18:28 -07002522static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2523{
2524#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2525 if (sqe->ioprio || sqe->buf_index || sqe->off)
2526 return -EINVAL;
2527
2528 req->madvise.addr = READ_ONCE(sqe->addr);
2529 req->madvise.len = READ_ONCE(sqe->len);
2530 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2531 return 0;
2532#else
2533 return -EOPNOTSUPP;
2534#endif
2535}
2536
2537static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2538 bool force_nonblock)
2539{
2540#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2541 struct io_madvise *ma = &req->madvise;
2542 int ret;
2543
2544 if (force_nonblock)
2545 return -EAGAIN;
2546
2547 ret = do_madvise(ma->addr, ma->len, ma->advice);
2548 if (ret < 0)
2549 req_set_fail_links(req);
2550 io_cqring_add_event(req, ret);
2551 io_put_req_find_next(req, nxt);
2552 return 0;
2553#else
2554 return -EOPNOTSUPP;
2555#endif
2556}
2557
Jens Axboe4840e412019-12-25 22:03:45 -07002558static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2559{
2560 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2561 return -EINVAL;
2562
2563 req->fadvise.offset = READ_ONCE(sqe->off);
2564 req->fadvise.len = READ_ONCE(sqe->len);
2565 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2566 return 0;
2567}
2568
2569static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2570 bool force_nonblock)
2571{
2572 struct io_fadvise *fa = &req->fadvise;
2573 int ret;
2574
2575 /* DONTNEED may block, others _should_ not */
2576 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2577 return -EAGAIN;
2578
2579 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2580 if (ret < 0)
2581 req_set_fail_links(req);
2582 io_cqring_add_event(req, ret);
2583 io_put_req_find_next(req, nxt);
2584 return 0;
2585}
2586
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002587static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2588{
2589 unsigned lookup_flags;
2590 int ret;
2591
2592 if (sqe->ioprio || sqe->buf_index)
2593 return -EINVAL;
2594
2595 req->open.dfd = READ_ONCE(sqe->fd);
2596 req->open.mask = READ_ONCE(sqe->len);
2597 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2598 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2599 req->open.flags = READ_ONCE(sqe->statx_flags);
2600
2601 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.flags))
2602 return -EINVAL;
2603
2604 req->open.filename = getname_flags(req->open.fname, lookup_flags, NULL);
2605 if (IS_ERR(req->open.filename)) {
2606 ret = PTR_ERR(req->open.filename);
2607 req->open.filename = NULL;
2608 return ret;
2609 }
2610
2611 return 0;
2612}
2613
2614static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2615 bool force_nonblock)
2616{
2617 struct io_open *ctx = &req->open;
2618 unsigned lookup_flags;
2619 struct path path;
2620 struct kstat stat;
2621 int ret;
2622
2623 if (force_nonblock)
2624 return -EAGAIN;
2625
2626 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->flags))
2627 return -EINVAL;
2628
2629retry:
2630 /* filename_lookup() drops it, keep a reference */
2631 ctx->filename->refcnt++;
2632
2633 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2634 NULL);
2635 if (ret)
2636 goto err;
2637
2638 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->flags);
2639 path_put(&path);
2640 if (retry_estale(ret, lookup_flags)) {
2641 lookup_flags |= LOOKUP_REVAL;
2642 goto retry;
2643 }
2644 if (!ret)
2645 ret = cp_statx(&stat, ctx->buffer);
2646err:
2647 putname(ctx->filename);
2648 if (ret < 0)
2649 req_set_fail_links(req);
2650 io_cqring_add_event(req, ret);
2651 io_put_req_find_next(req, nxt);
2652 return 0;
2653}
2654
Jens Axboeb5dba592019-12-11 14:02:38 -07002655static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2656{
2657 /*
2658 * If we queue this for async, it must not be cancellable. That would
2659 * leave the 'file' in an undeterminate state.
2660 */
2661 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2662
2663 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2664 sqe->rw_flags || sqe->buf_index)
2665 return -EINVAL;
2666 if (sqe->flags & IOSQE_FIXED_FILE)
2667 return -EINVAL;
2668
2669 req->close.fd = READ_ONCE(sqe->fd);
2670 if (req->file->f_op == &io_uring_fops ||
2671 req->close.fd == req->ring_fd)
2672 return -EBADF;
2673
2674 return 0;
2675}
2676
2677static void io_close_finish(struct io_wq_work **workptr)
2678{
2679 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2680 struct io_kiocb *nxt = NULL;
2681
2682 /* Invoked with files, we need to do the close */
2683 if (req->work.files) {
2684 int ret;
2685
2686 ret = filp_close(req->close.put_file, req->work.files);
2687 if (ret < 0) {
2688 req_set_fail_links(req);
2689 }
2690 io_cqring_add_event(req, ret);
2691 }
2692
2693 fput(req->close.put_file);
2694
2695 /* we bypassed the re-issue, drop the submission reference */
2696 io_put_req(req);
2697 io_put_req_find_next(req, &nxt);
2698 if (nxt)
2699 io_wq_assign_next(workptr, nxt);
2700}
2701
2702static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2703 bool force_nonblock)
2704{
2705 int ret;
2706
2707 req->close.put_file = NULL;
2708 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2709 if (ret < 0)
2710 return ret;
2711
2712 /* if the file has a flush method, be safe and punt to async */
2713 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2714 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2715 goto eagain;
2716 }
2717
2718 /*
2719 * No ->flush(), safely close from here and just punt the
2720 * fput() to async context.
2721 */
2722 ret = filp_close(req->close.put_file, current->files);
2723
2724 if (ret < 0)
2725 req_set_fail_links(req);
2726 io_cqring_add_event(req, ret);
2727
2728 if (io_wq_current_is_worker()) {
2729 struct io_wq_work *old_work, *work;
2730
2731 old_work = work = &req->work;
2732 io_close_finish(&work);
2733 if (work && work != old_work)
2734 *nxt = container_of(work, struct io_kiocb, work);
2735 return 0;
2736 }
2737
2738eagain:
2739 req->work.func = io_close_finish;
2740 return -EAGAIN;
2741}
2742
Jens Axboe3529d8c2019-12-19 18:24:38 -07002743static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002744{
2745 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002746
2747 if (!req->file)
2748 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002749
2750 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2751 return -EINVAL;
2752 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2753 return -EINVAL;
2754
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002755 req->sync.off = READ_ONCE(sqe->off);
2756 req->sync.len = READ_ONCE(sqe->len);
2757 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002758 return 0;
2759}
2760
2761static void io_sync_file_range_finish(struct io_wq_work **workptr)
2762{
2763 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2764 struct io_kiocb *nxt = NULL;
2765 int ret;
2766
2767 if (io_req_cancelled(req))
2768 return;
2769
Jens Axboe9adbd452019-12-20 08:45:55 -07002770 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002771 req->sync.flags);
2772 if (ret < 0)
2773 req_set_fail_links(req);
2774 io_cqring_add_event(req, ret);
2775 io_put_req_find_next(req, &nxt);
2776 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002777 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002778}
2779
Jens Axboefc4df992019-12-10 14:38:45 -07002780static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002781 bool force_nonblock)
2782{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002783 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002784
2785 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002786 if (force_nonblock) {
2787 io_put_req(req);
2788 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002789 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002790 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002791
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002792 work = old_work = &req->work;
2793 io_sync_file_range_finish(&work);
2794 if (work && work != old_work)
2795 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002796 return 0;
2797}
2798
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002799#if defined(CONFIG_NET)
2800static void io_sendrecv_async(struct io_wq_work **workptr)
2801{
2802 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2803 struct iovec *iov = NULL;
2804
2805 if (req->io->rw.iov != req->io->rw.fast_iov)
2806 iov = req->io->msg.iov;
2807 io_wq_submit_work(workptr);
2808 kfree(iov);
2809}
2810#endif
2811
Jens Axboe3529d8c2019-12-19 18:24:38 -07002812static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002813{
Jens Axboe03b12302019-12-02 18:50:25 -07002814#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002815 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002816 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002817
Jens Axboee47293f2019-12-20 08:58:21 -07002818 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2819 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002820 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002821
Jens Axboefddafac2020-01-04 20:19:44 -07002822 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002823 return 0;
2824
Jens Axboed9688562019-12-09 19:35:20 -07002825 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002826 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002827 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002828#else
Jens Axboee47293f2019-12-20 08:58:21 -07002829 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002830#endif
2831}
2832
Jens Axboefc4df992019-12-10 14:38:45 -07002833static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2834 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002835{
2836#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002837 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002838 struct socket *sock;
2839 int ret;
2840
2841 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2842 return -EINVAL;
2843
2844 sock = sock_from_file(req->file, &ret);
2845 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002846 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002847 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002848 unsigned flags;
2849
Jens Axboe03b12302019-12-02 18:50:25 -07002850 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002851 kmsg = &req->io->msg;
2852 kmsg->msg.msg_name = &addr;
2853 /* if iov is set, it's allocated already */
2854 if (!kmsg->iov)
2855 kmsg->iov = kmsg->fast_iov;
2856 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002857 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002858 struct io_sr_msg *sr = &req->sr_msg;
2859
Jens Axboe0b416c32019-12-15 10:57:46 -07002860 kmsg = &io.msg;
2861 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002862
2863 io.msg.iov = io.msg.fast_iov;
2864 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2865 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002866 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002867 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002868 }
2869
Jens Axboee47293f2019-12-20 08:58:21 -07002870 flags = req->sr_msg.msg_flags;
2871 if (flags & MSG_DONTWAIT)
2872 req->flags |= REQ_F_NOWAIT;
2873 else if (force_nonblock)
2874 flags |= MSG_DONTWAIT;
2875
Jens Axboe0b416c32019-12-15 10:57:46 -07002876 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002877 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002878 if (req->io)
2879 return -EAGAIN;
2880 if (io_alloc_async_ctx(req))
2881 return -ENOMEM;
2882 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2883 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002884 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002885 }
2886 if (ret == -ERESTARTSYS)
2887 ret = -EINTR;
2888 }
2889
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002890 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002891 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002892 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002893 if (ret < 0)
2894 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002895 io_put_req_find_next(req, nxt);
2896 return 0;
2897#else
2898 return -EOPNOTSUPP;
2899#endif
2900}
2901
Jens Axboefddafac2020-01-04 20:19:44 -07002902static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2903 bool force_nonblock)
2904{
2905#if defined(CONFIG_NET)
2906 struct socket *sock;
2907 int ret;
2908
2909 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2910 return -EINVAL;
2911
2912 sock = sock_from_file(req->file, &ret);
2913 if (sock) {
2914 struct io_sr_msg *sr = &req->sr_msg;
2915 struct msghdr msg;
2916 struct iovec iov;
2917 unsigned flags;
2918
2919 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
2920 &msg.msg_iter);
2921 if (ret)
2922 return ret;
2923
2924 msg.msg_name = NULL;
2925 msg.msg_control = NULL;
2926 msg.msg_controllen = 0;
2927 msg.msg_namelen = 0;
2928
2929 flags = req->sr_msg.msg_flags;
2930 if (flags & MSG_DONTWAIT)
2931 req->flags |= REQ_F_NOWAIT;
2932 else if (force_nonblock)
2933 flags |= MSG_DONTWAIT;
2934
2935 ret = __sys_sendmsg_sock(sock, &msg, flags);
2936 if (force_nonblock && ret == -EAGAIN)
2937 return -EAGAIN;
2938 if (ret == -ERESTARTSYS)
2939 ret = -EINTR;
2940 }
2941
2942 io_cqring_add_event(req, ret);
2943 if (ret < 0)
2944 req_set_fail_links(req);
2945 io_put_req_find_next(req, nxt);
2946 return 0;
2947#else
2948 return -EOPNOTSUPP;
2949#endif
2950}
2951
Jens Axboe3529d8c2019-12-19 18:24:38 -07002952static int io_recvmsg_prep(struct io_kiocb *req,
2953 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002954{
2955#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002956 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002957 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002958
Jens Axboe3529d8c2019-12-19 18:24:38 -07002959 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2960 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2961
Jens Axboefddafac2020-01-04 20:19:44 -07002962 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07002963 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002964
Jens Axboed9688562019-12-09 19:35:20 -07002965 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002966 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002967 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002968#else
Jens Axboee47293f2019-12-20 08:58:21 -07002969 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002970#endif
2971}
2972
Jens Axboefc4df992019-12-10 14:38:45 -07002973static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2974 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002975{
2976#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002977 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002978 struct socket *sock;
2979 int ret;
2980
2981 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2982 return -EINVAL;
2983
2984 sock = sock_from_file(req->file, &ret);
2985 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002986 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002987 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002988 unsigned flags;
2989
Jens Axboe03b12302019-12-02 18:50:25 -07002990 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002991 kmsg = &req->io->msg;
2992 kmsg->msg.msg_name = &addr;
2993 /* if iov is set, it's allocated already */
2994 if (!kmsg->iov)
2995 kmsg->iov = kmsg->fast_iov;
2996 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002997 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002998 struct io_sr_msg *sr = &req->sr_msg;
2999
Jens Axboe0b416c32019-12-15 10:57:46 -07003000 kmsg = &io.msg;
3001 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003002
3003 io.msg.iov = io.msg.fast_iov;
3004 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3005 sr->msg_flags, &io.msg.uaddr,
3006 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003007 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003008 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003009 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003010
Jens Axboee47293f2019-12-20 08:58:21 -07003011 flags = req->sr_msg.msg_flags;
3012 if (flags & MSG_DONTWAIT)
3013 req->flags |= REQ_F_NOWAIT;
3014 else if (force_nonblock)
3015 flags |= MSG_DONTWAIT;
3016
3017 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3018 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003019 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003020 if (req->io)
3021 return -EAGAIN;
3022 if (io_alloc_async_ctx(req))
3023 return -ENOMEM;
3024 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3025 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003026 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003027 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003028 if (ret == -ERESTARTSYS)
3029 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003030 }
3031
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003032 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003033 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003034 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003035 if (ret < 0)
3036 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003037 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003038 return 0;
3039#else
3040 return -EOPNOTSUPP;
3041#endif
3042}
3043
Jens Axboefddafac2020-01-04 20:19:44 -07003044static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3045 bool force_nonblock)
3046{
3047#if defined(CONFIG_NET)
3048 struct socket *sock;
3049 int ret;
3050
3051 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3052 return -EINVAL;
3053
3054 sock = sock_from_file(req->file, &ret);
3055 if (sock) {
3056 struct io_sr_msg *sr = &req->sr_msg;
3057 struct msghdr msg;
3058 struct iovec iov;
3059 unsigned flags;
3060
3061 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3062 &msg.msg_iter);
3063 if (ret)
3064 return ret;
3065
3066 msg.msg_name = NULL;
3067 msg.msg_control = NULL;
3068 msg.msg_controllen = 0;
3069 msg.msg_namelen = 0;
3070 msg.msg_iocb = NULL;
3071 msg.msg_flags = 0;
3072
3073 flags = req->sr_msg.msg_flags;
3074 if (flags & MSG_DONTWAIT)
3075 req->flags |= REQ_F_NOWAIT;
3076 else if (force_nonblock)
3077 flags |= MSG_DONTWAIT;
3078
3079 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3080 if (force_nonblock && ret == -EAGAIN)
3081 return -EAGAIN;
3082 if (ret == -ERESTARTSYS)
3083 ret = -EINTR;
3084 }
3085
3086 io_cqring_add_event(req, ret);
3087 if (ret < 0)
3088 req_set_fail_links(req);
3089 io_put_req_find_next(req, nxt);
3090 return 0;
3091#else
3092 return -EOPNOTSUPP;
3093#endif
3094}
3095
3096
Jens Axboe3529d8c2019-12-19 18:24:38 -07003097static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003098{
3099#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003100 struct io_accept *accept = &req->accept;
3101
Jens Axboe17f2fe32019-10-17 14:42:58 -06003102 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3103 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003104 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003105 return -EINVAL;
3106
Jens Axboed55e5f52019-12-11 16:12:15 -07003107 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3108 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003109 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003110 return 0;
3111#else
3112 return -EOPNOTSUPP;
3113#endif
3114}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003115
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003116#if defined(CONFIG_NET)
3117static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3118 bool force_nonblock)
3119{
3120 struct io_accept *accept = &req->accept;
3121 unsigned file_flags;
3122 int ret;
3123
3124 file_flags = force_nonblock ? O_NONBLOCK : 0;
3125 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3126 accept->addr_len, accept->flags);
3127 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003128 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003129 if (ret == -ERESTARTSYS)
3130 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003131 if (ret < 0)
3132 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003133 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003134 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003135 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003136}
3137
3138static void io_accept_finish(struct io_wq_work **workptr)
3139{
3140 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3141 struct io_kiocb *nxt = NULL;
3142
3143 if (io_req_cancelled(req))
3144 return;
3145 __io_accept(req, &nxt, false);
3146 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003147 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003148}
3149#endif
3150
3151static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3152 bool force_nonblock)
3153{
3154#if defined(CONFIG_NET)
3155 int ret;
3156
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003157 ret = __io_accept(req, nxt, force_nonblock);
3158 if (ret == -EAGAIN && force_nonblock) {
3159 req->work.func = io_accept_finish;
3160 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3161 io_put_req(req);
3162 return -EAGAIN;
3163 }
3164 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003165#else
3166 return -EOPNOTSUPP;
3167#endif
3168}
3169
Jens Axboe3529d8c2019-12-19 18:24:38 -07003170static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003171{
3172#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003173 struct io_connect *conn = &req->connect;
3174 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003175
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003176 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3177 return -EINVAL;
3178 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3179 return -EINVAL;
3180
Jens Axboe3529d8c2019-12-19 18:24:38 -07003181 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3182 conn->addr_len = READ_ONCE(sqe->addr2);
3183
3184 if (!io)
3185 return 0;
3186
3187 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003188 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003189#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003190 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003191#endif
3192}
3193
Jens Axboefc4df992019-12-10 14:38:45 -07003194static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3195 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003196{
3197#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003198 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003199 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003200 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003201
Jens Axboef499a022019-12-02 16:28:46 -07003202 if (req->io) {
3203 io = req->io;
3204 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003205 ret = move_addr_to_kernel(req->connect.addr,
3206 req->connect.addr_len,
3207 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003208 if (ret)
3209 goto out;
3210 io = &__io;
3211 }
3212
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003213 file_flags = force_nonblock ? O_NONBLOCK : 0;
3214
3215 ret = __sys_connect_file(req->file, &io->connect.address,
3216 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003217 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003218 if (req->io)
3219 return -EAGAIN;
3220 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003221 ret = -ENOMEM;
3222 goto out;
3223 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003224 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003225 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003226 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003227 if (ret == -ERESTARTSYS)
3228 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003229out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003230 if (ret < 0)
3231 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003232 io_cqring_add_event(req, ret);
3233 io_put_req_find_next(req, nxt);
3234 return 0;
3235#else
3236 return -EOPNOTSUPP;
3237#endif
3238}
3239
Jens Axboe221c5eb2019-01-17 09:41:58 -07003240static void io_poll_remove_one(struct io_kiocb *req)
3241{
3242 struct io_poll_iocb *poll = &req->poll;
3243
3244 spin_lock(&poll->head->lock);
3245 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003246 if (!list_empty(&poll->wait.entry)) {
3247 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003248 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003249 }
3250 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003251 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003252}
3253
3254static void io_poll_remove_all(struct io_ring_ctx *ctx)
3255{
Jens Axboe78076bb2019-12-04 19:56:40 -07003256 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003257 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003258 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003259
3260 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003261 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3262 struct hlist_head *list;
3263
3264 list = &ctx->cancel_hash[i];
3265 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3266 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003267 }
3268 spin_unlock_irq(&ctx->completion_lock);
3269}
3270
Jens Axboe47f46762019-11-09 17:43:02 -07003271static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3272{
Jens Axboe78076bb2019-12-04 19:56:40 -07003273 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003274 struct io_kiocb *req;
3275
Jens Axboe78076bb2019-12-04 19:56:40 -07003276 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3277 hlist_for_each_entry(req, list, hash_node) {
3278 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003279 io_poll_remove_one(req);
3280 return 0;
3281 }
Jens Axboe47f46762019-11-09 17:43:02 -07003282 }
3283
3284 return -ENOENT;
3285}
3286
Jens Axboe3529d8c2019-12-19 18:24:38 -07003287static int io_poll_remove_prep(struct io_kiocb *req,
3288 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003289{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003290 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3291 return -EINVAL;
3292 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3293 sqe->poll_events)
3294 return -EINVAL;
3295
Jens Axboe0969e782019-12-17 18:40:57 -07003296 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003297 return 0;
3298}
3299
3300/*
3301 * Find a running poll command that matches one specified in sqe->addr,
3302 * and remove it if found.
3303 */
3304static int io_poll_remove(struct io_kiocb *req)
3305{
3306 struct io_ring_ctx *ctx = req->ctx;
3307 u64 addr;
3308 int ret;
3309
Jens Axboe0969e782019-12-17 18:40:57 -07003310 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003311 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003312 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003313 spin_unlock_irq(&ctx->completion_lock);
3314
Jens Axboe78e19bb2019-11-06 15:21:34 -07003315 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003316 if (ret < 0)
3317 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003318 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003319 return 0;
3320}
3321
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003322static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003323{
Jackie Liua197f662019-11-08 08:09:12 -07003324 struct io_ring_ctx *ctx = req->ctx;
3325
Jens Axboe8c838782019-03-12 15:48:16 -06003326 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003327 if (error)
3328 io_cqring_fill_event(req, error);
3329 else
3330 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003331 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003332}
3333
Jens Axboe561fb042019-10-24 07:25:42 -06003334static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003335{
Jens Axboe561fb042019-10-24 07:25:42 -06003336 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003337 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3338 struct io_poll_iocb *poll = &req->poll;
3339 struct poll_table_struct pt = { ._key = poll->events };
3340 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003341 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003342 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003343 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003344
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003345 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003346 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003347 ret = -ECANCELED;
3348 } else if (READ_ONCE(poll->canceled)) {
3349 ret = -ECANCELED;
3350 }
Jens Axboe561fb042019-10-24 07:25:42 -06003351
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003352 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003353 mask = vfs_poll(poll->file, &pt) & poll->events;
3354
3355 /*
3356 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3357 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3358 * synchronize with them. In the cancellation case the list_del_init
3359 * itself is not actually needed, but harmless so we keep it in to
3360 * avoid further branches in the fast path.
3361 */
3362 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003363 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003364 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003365 spin_unlock_irq(&ctx->completion_lock);
3366 return;
3367 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003368 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003369 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003370 spin_unlock_irq(&ctx->completion_lock);
3371
Jens Axboe8c838782019-03-12 15:48:16 -06003372 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003373
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003374 if (ret < 0)
3375 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003376 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003377 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003378 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003379}
3380
Jens Axboee94f1412019-12-19 12:06:02 -07003381static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3382{
Jens Axboee94f1412019-12-19 12:06:02 -07003383 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003384 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003385
Jens Axboec6ca97b302019-12-28 12:11:08 -07003386 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003387 spin_lock_irq(&ctx->completion_lock);
3388 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3389 hash_del(&req->hash_node);
3390 io_poll_complete(req, req->result, 0);
3391
Jens Axboe8237e042019-12-28 10:48:22 -07003392 if (refcount_dec_and_test(&req->refs) &&
3393 !io_req_multi_free(&rb, req)) {
3394 req->flags |= REQ_F_COMP_LOCKED;
3395 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003396 }
3397 }
3398 spin_unlock_irq(&ctx->completion_lock);
3399
3400 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003401 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003402}
3403
3404static void io_poll_flush(struct io_wq_work **workptr)
3405{
3406 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3407 struct llist_node *nodes;
3408
3409 nodes = llist_del_all(&req->ctx->poll_llist);
3410 if (nodes)
3411 __io_poll_flush(req->ctx, nodes);
3412}
3413
Jens Axboe221c5eb2019-01-17 09:41:58 -07003414static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3415 void *key)
3416{
Jens Axboee9444752019-11-26 15:02:04 -07003417 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003418 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3419 struct io_ring_ctx *ctx = req->ctx;
3420 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003421
3422 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003423 if (mask && !(mask & poll->events))
3424 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003425
Jens Axboe392edb42019-12-09 17:52:20 -07003426 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003427
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003428 /*
3429 * Run completion inline if we can. We're using trylock here because
3430 * we are violating the completion_lock -> poll wq lock ordering.
3431 * If we have a link timeout we're going to need the completion_lock
3432 * for finalizing the request, mark us as having grabbed that already.
3433 */
Jens Axboee94f1412019-12-19 12:06:02 -07003434 if (mask) {
3435 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003436
Jens Axboee94f1412019-12-19 12:06:02 -07003437 if (llist_empty(&ctx->poll_llist) &&
3438 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3439 hash_del(&req->hash_node);
3440 io_poll_complete(req, mask, 0);
3441 req->flags |= REQ_F_COMP_LOCKED;
3442 io_put_req(req);
3443 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3444
3445 io_cqring_ev_posted(ctx);
3446 req = NULL;
3447 } else {
3448 req->result = mask;
3449 req->llist_node.next = NULL;
3450 /* if the list wasn't empty, we're done */
3451 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3452 req = NULL;
3453 else
3454 req->work.func = io_poll_flush;
3455 }
Jens Axboe8c838782019-03-12 15:48:16 -06003456 }
Jens Axboee94f1412019-12-19 12:06:02 -07003457 if (req)
3458 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003459
Jens Axboe221c5eb2019-01-17 09:41:58 -07003460 return 1;
3461}
3462
3463struct io_poll_table {
3464 struct poll_table_struct pt;
3465 struct io_kiocb *req;
3466 int error;
3467};
3468
3469static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3470 struct poll_table_struct *p)
3471{
3472 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3473
3474 if (unlikely(pt->req->poll.head)) {
3475 pt->error = -EINVAL;
3476 return;
3477 }
3478
3479 pt->error = 0;
3480 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003481 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003482}
3483
Jens Axboeeac406c2019-11-14 12:09:58 -07003484static void io_poll_req_insert(struct io_kiocb *req)
3485{
3486 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003487 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003488
Jens Axboe78076bb2019-12-04 19:56:40 -07003489 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3490 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003491}
3492
Jens Axboe3529d8c2019-12-19 18:24:38 -07003493static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003494{
3495 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003496 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003497
3498 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3499 return -EINVAL;
3500 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3501 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003502 if (!poll->file)
3503 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003504
Jens Axboe221c5eb2019-01-17 09:41:58 -07003505 events = READ_ONCE(sqe->poll_events);
3506 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003507 return 0;
3508}
3509
3510static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3511{
3512 struct io_poll_iocb *poll = &req->poll;
3513 struct io_ring_ctx *ctx = req->ctx;
3514 struct io_poll_table ipt;
3515 bool cancel = false;
3516 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003517
3518 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003519 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003520
Jens Axboe221c5eb2019-01-17 09:41:58 -07003521 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003522 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003523 poll->canceled = false;
3524
3525 ipt.pt._qproc = io_poll_queue_proc;
3526 ipt.pt._key = poll->events;
3527 ipt.req = req;
3528 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3529
3530 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003531 INIT_LIST_HEAD(&poll->wait.entry);
3532 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3533 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003534
Jens Axboe36703242019-07-25 10:20:18 -06003535 INIT_LIST_HEAD(&req->list);
3536
Jens Axboe221c5eb2019-01-17 09:41:58 -07003537 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003538
3539 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003540 if (likely(poll->head)) {
3541 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003542 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003543 if (ipt.error)
3544 cancel = true;
3545 ipt.error = 0;
3546 mask = 0;
3547 }
3548 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003549 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003550 else if (cancel)
3551 WRITE_ONCE(poll->canceled, true);
3552 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003553 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003554 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003555 }
Jens Axboe8c838782019-03-12 15:48:16 -06003556 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003557 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003558 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003559 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003560 spin_unlock_irq(&ctx->completion_lock);
3561
Jens Axboe8c838782019-03-12 15:48:16 -06003562 if (mask) {
3563 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003564 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003565 }
Jens Axboe8c838782019-03-12 15:48:16 -06003566 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003567}
3568
Jens Axboe5262f562019-09-17 12:26:57 -06003569static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3570{
Jens Axboead8a48a2019-11-15 08:49:11 -07003571 struct io_timeout_data *data = container_of(timer,
3572 struct io_timeout_data, timer);
3573 struct io_kiocb *req = data->req;
3574 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003575 unsigned long flags;
3576
Jens Axboe5262f562019-09-17 12:26:57 -06003577 atomic_inc(&ctx->cq_timeouts);
3578
3579 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003580 /*
Jens Axboe11365042019-10-16 09:08:32 -06003581 * We could be racing with timeout deletion. If the list is empty,
3582 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003583 */
Jens Axboe842f9612019-10-29 12:34:10 -06003584 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003585 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003586
Jens Axboe11365042019-10-16 09:08:32 -06003587 /*
3588 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003589 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003590 * pointer will be increased, otherwise other timeout reqs may
3591 * return in advance without waiting for enough wait_nr.
3592 */
3593 prev = req;
3594 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3595 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003596 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003597 }
Jens Axboe842f9612019-10-29 12:34:10 -06003598
Jens Axboe78e19bb2019-11-06 15:21:34 -07003599 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003600 io_commit_cqring(ctx);
3601 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3602
3603 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003604 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003605 io_put_req(req);
3606 return HRTIMER_NORESTART;
3607}
3608
Jens Axboe47f46762019-11-09 17:43:02 -07003609static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3610{
3611 struct io_kiocb *req;
3612 int ret = -ENOENT;
3613
3614 list_for_each_entry(req, &ctx->timeout_list, list) {
3615 if (user_data == req->user_data) {
3616 list_del_init(&req->list);
3617 ret = 0;
3618 break;
3619 }
3620 }
3621
3622 if (ret == -ENOENT)
3623 return ret;
3624
Jens Axboe2d283902019-12-04 11:08:05 -07003625 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003626 if (ret == -1)
3627 return -EALREADY;
3628
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003629 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003630 io_cqring_fill_event(req, -ECANCELED);
3631 io_put_req(req);
3632 return 0;
3633}
3634
Jens Axboe3529d8c2019-12-19 18:24:38 -07003635static int io_timeout_remove_prep(struct io_kiocb *req,
3636 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003637{
Jens Axboeb29472e2019-12-17 18:50:29 -07003638 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3639 return -EINVAL;
3640 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3641 return -EINVAL;
3642
3643 req->timeout.addr = READ_ONCE(sqe->addr);
3644 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3645 if (req->timeout.flags)
3646 return -EINVAL;
3647
Jens Axboeb29472e2019-12-17 18:50:29 -07003648 return 0;
3649}
3650
Jens Axboe11365042019-10-16 09:08:32 -06003651/*
3652 * Remove or update an existing timeout command
3653 */
Jens Axboefc4df992019-12-10 14:38:45 -07003654static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003655{
3656 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003657 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003658
Jens Axboe11365042019-10-16 09:08:32 -06003659 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003660 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003661
Jens Axboe47f46762019-11-09 17:43:02 -07003662 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003663 io_commit_cqring(ctx);
3664 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003665 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003666 if (ret < 0)
3667 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003668 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003669 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003670}
3671
Jens Axboe3529d8c2019-12-19 18:24:38 -07003672static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003673 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003674{
Jens Axboead8a48a2019-11-15 08:49:11 -07003675 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003676 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003677
Jens Axboead8a48a2019-11-15 08:49:11 -07003678 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003679 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003680 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003681 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003682 if (sqe->off && is_timeout_link)
3683 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003684 flags = READ_ONCE(sqe->timeout_flags);
3685 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003686 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003687
Jens Axboe26a61672019-12-20 09:02:01 -07003688 req->timeout.count = READ_ONCE(sqe->off);
3689
Jens Axboe3529d8c2019-12-19 18:24:38 -07003690 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003691 return -ENOMEM;
3692
3693 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003694 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003695 req->flags |= REQ_F_TIMEOUT;
3696
3697 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003698 return -EFAULT;
3699
Jens Axboe11365042019-10-16 09:08:32 -06003700 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003701 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003702 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003703 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003704
Jens Axboead8a48a2019-11-15 08:49:11 -07003705 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3706 return 0;
3707}
3708
Jens Axboefc4df992019-12-10 14:38:45 -07003709static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003710{
3711 unsigned count;
3712 struct io_ring_ctx *ctx = req->ctx;
3713 struct io_timeout_data *data;
3714 struct list_head *entry;
3715 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003716
Jens Axboe2d283902019-12-04 11:08:05 -07003717 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003718
Jens Axboe5262f562019-09-17 12:26:57 -06003719 /*
3720 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003721 * timeout event to be satisfied. If it isn't set, then this is
3722 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003723 */
Jens Axboe26a61672019-12-20 09:02:01 -07003724 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003725 if (!count) {
3726 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3727 spin_lock_irq(&ctx->completion_lock);
3728 entry = ctx->timeout_list.prev;
3729 goto add;
3730 }
Jens Axboe5262f562019-09-17 12:26:57 -06003731
3732 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003733 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003734
3735 /*
3736 * Insertion sort, ensuring the first entry in the list is always
3737 * the one we need first.
3738 */
Jens Axboe5262f562019-09-17 12:26:57 -06003739 spin_lock_irq(&ctx->completion_lock);
3740 list_for_each_prev(entry, &ctx->timeout_list) {
3741 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003742 unsigned nxt_sq_head;
3743 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003744 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003745
Jens Axboe93bd25b2019-11-11 23:34:31 -07003746 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3747 continue;
3748
yangerkun5da0fb12019-10-15 21:59:29 +08003749 /*
3750 * Since cached_sq_head + count - 1 can overflow, use type long
3751 * long to store it.
3752 */
3753 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003754 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3755 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003756
3757 /*
3758 * cached_sq_head may overflow, and it will never overflow twice
3759 * once there is some timeout req still be valid.
3760 */
3761 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003762 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003763
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003764 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003765 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003766
3767 /*
3768 * Sequence of reqs after the insert one and itself should
3769 * be adjusted because each timeout req consumes a slot.
3770 */
3771 span++;
3772 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003773 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003774 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003775add:
Jens Axboe5262f562019-09-17 12:26:57 -06003776 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003777 data->timer.function = io_timeout_fn;
3778 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003779 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003780 return 0;
3781}
3782
Jens Axboe62755e32019-10-28 21:49:21 -06003783static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003784{
Jens Axboe62755e32019-10-28 21:49:21 -06003785 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003786
Jens Axboe62755e32019-10-28 21:49:21 -06003787 return req->user_data == (unsigned long) data;
3788}
3789
Jens Axboee977d6d2019-11-05 12:39:45 -07003790static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003791{
Jens Axboe62755e32019-10-28 21:49:21 -06003792 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003793 int ret = 0;
3794
Jens Axboe62755e32019-10-28 21:49:21 -06003795 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3796 switch (cancel_ret) {
3797 case IO_WQ_CANCEL_OK:
3798 ret = 0;
3799 break;
3800 case IO_WQ_CANCEL_RUNNING:
3801 ret = -EALREADY;
3802 break;
3803 case IO_WQ_CANCEL_NOTFOUND:
3804 ret = -ENOENT;
3805 break;
3806 }
3807
Jens Axboee977d6d2019-11-05 12:39:45 -07003808 return ret;
3809}
3810
Jens Axboe47f46762019-11-09 17:43:02 -07003811static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3812 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003813 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003814{
3815 unsigned long flags;
3816 int ret;
3817
3818 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3819 if (ret != -ENOENT) {
3820 spin_lock_irqsave(&ctx->completion_lock, flags);
3821 goto done;
3822 }
3823
3824 spin_lock_irqsave(&ctx->completion_lock, flags);
3825 ret = io_timeout_cancel(ctx, sqe_addr);
3826 if (ret != -ENOENT)
3827 goto done;
3828 ret = io_poll_cancel(ctx, sqe_addr);
3829done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003830 if (!ret)
3831 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003832 io_cqring_fill_event(req, ret);
3833 io_commit_cqring(ctx);
3834 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3835 io_cqring_ev_posted(ctx);
3836
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003837 if (ret < 0)
3838 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003839 io_put_req_find_next(req, nxt);
3840}
3841
Jens Axboe3529d8c2019-12-19 18:24:38 -07003842static int io_async_cancel_prep(struct io_kiocb *req,
3843 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003844{
Jens Axboefbf23842019-12-17 18:45:56 -07003845 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003846 return -EINVAL;
3847 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3848 sqe->cancel_flags)
3849 return -EINVAL;
3850
Jens Axboefbf23842019-12-17 18:45:56 -07003851 req->cancel.addr = READ_ONCE(sqe->addr);
3852 return 0;
3853}
3854
3855static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3856{
3857 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003858
3859 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003860 return 0;
3861}
3862
Jens Axboe05f3fb32019-12-09 11:22:50 -07003863static int io_files_update_prep(struct io_kiocb *req,
3864 const struct io_uring_sqe *sqe)
3865{
3866 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3867 return -EINVAL;
3868
3869 req->files_update.offset = READ_ONCE(sqe->off);
3870 req->files_update.nr_args = READ_ONCE(sqe->len);
3871 if (!req->files_update.nr_args)
3872 return -EINVAL;
3873 req->files_update.arg = READ_ONCE(sqe->addr);
3874 return 0;
3875}
3876
3877static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3878{
3879 struct io_ring_ctx *ctx = req->ctx;
3880 struct io_uring_files_update up;
3881 int ret;
3882
3883 if (force_nonblock) {
3884 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3885 return -EAGAIN;
3886 }
3887
3888 up.offset = req->files_update.offset;
3889 up.fds = req->files_update.arg;
3890
3891 mutex_lock(&ctx->uring_lock);
3892 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3893 mutex_unlock(&ctx->uring_lock);
3894
3895 if (ret < 0)
3896 req_set_fail_links(req);
3897 io_cqring_add_event(req, ret);
3898 io_put_req(req);
3899 return 0;
3900}
3901
Jens Axboe3529d8c2019-12-19 18:24:38 -07003902static int io_req_defer_prep(struct io_kiocb *req,
3903 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003904{
Jens Axboee7815732019-12-17 19:45:06 -07003905 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003906
Jens Axboed625c6e2019-12-17 19:53:05 -07003907 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003908 case IORING_OP_NOP:
3909 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003910 case IORING_OP_READV:
3911 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003912 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003913 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003914 break;
3915 case IORING_OP_WRITEV:
3916 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003917 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003918 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003919 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003920 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003921 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003922 break;
3923 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003924 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003925 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003926 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003927 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003928 break;
3929 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003930 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003931 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003932 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003933 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003934 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003935 break;
3936 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003937 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003938 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003939 break;
Jens Axboef499a022019-12-02 16:28:46 -07003940 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003941 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003942 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003943 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003944 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003945 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003946 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003947 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003948 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003949 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003950 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003951 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003952 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003953 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003954 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003955 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003956 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003957 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003958 case IORING_OP_FALLOCATE:
3959 ret = io_fallocate_prep(req, sqe);
3960 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003961 case IORING_OP_OPENAT:
3962 ret = io_openat_prep(req, sqe);
3963 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003964 case IORING_OP_CLOSE:
3965 ret = io_close_prep(req, sqe);
3966 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003967 case IORING_OP_FILES_UPDATE:
3968 ret = io_files_update_prep(req, sqe);
3969 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003970 case IORING_OP_STATX:
3971 ret = io_statx_prep(req, sqe);
3972 break;
Jens Axboe4840e412019-12-25 22:03:45 -07003973 case IORING_OP_FADVISE:
3974 ret = io_fadvise_prep(req, sqe);
3975 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07003976 case IORING_OP_MADVISE:
3977 ret = io_madvise_prep(req, sqe);
3978 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003979 default:
Jens Axboee7815732019-12-17 19:45:06 -07003980 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3981 req->opcode);
3982 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003983 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003984 }
3985
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003986 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003987}
3988
Jens Axboe3529d8c2019-12-19 18:24:38 -07003989static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003990{
Jackie Liua197f662019-11-08 08:09:12 -07003991 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003992 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003993
Bob Liu9d858b22019-11-13 18:06:25 +08003994 /* Still need defer if there is pending req in defer list. */
3995 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003996 return 0;
3997
Jens Axboe3529d8c2019-12-19 18:24:38 -07003998 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003999 return -EAGAIN;
4000
Jens Axboe3529d8c2019-12-19 18:24:38 -07004001 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004002 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004003 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004004
Jens Axboede0617e2019-04-06 21:51:27 -06004005 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004006 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004007 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004008 return 0;
4009 }
4010
Jens Axboe915967f2019-11-21 09:01:20 -07004011 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004012 list_add_tail(&req->list, &ctx->defer_list);
4013 spin_unlock_irq(&ctx->completion_lock);
4014 return -EIOCBQUEUED;
4015}
4016
Jens Axboe3529d8c2019-12-19 18:24:38 -07004017static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4018 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004019{
Jackie Liua197f662019-11-08 08:09:12 -07004020 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004021 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004022
Jens Axboed625c6e2019-12-17 19:53:05 -07004023 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004024 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004025 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004026 break;
4027 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004028 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004029 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004030 if (sqe) {
4031 ret = io_read_prep(req, sqe, force_nonblock);
4032 if (ret < 0)
4033 break;
4034 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004035 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004036 break;
4037 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004038 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004039 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004040 if (sqe) {
4041 ret = io_write_prep(req, sqe, force_nonblock);
4042 if (ret < 0)
4043 break;
4044 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004045 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004046 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004047 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004048 if (sqe) {
4049 ret = io_prep_fsync(req, sqe);
4050 if (ret < 0)
4051 break;
4052 }
Jens Axboefc4df992019-12-10 14:38:45 -07004053 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004054 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004055 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004056 if (sqe) {
4057 ret = io_poll_add_prep(req, sqe);
4058 if (ret)
4059 break;
4060 }
Jens Axboefc4df992019-12-10 14:38:45 -07004061 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004062 break;
4063 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004064 if (sqe) {
4065 ret = io_poll_remove_prep(req, sqe);
4066 if (ret < 0)
4067 break;
4068 }
Jens Axboefc4df992019-12-10 14:38:45 -07004069 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004070 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004071 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004072 if (sqe) {
4073 ret = io_prep_sfr(req, sqe);
4074 if (ret < 0)
4075 break;
4076 }
Jens Axboefc4df992019-12-10 14:38:45 -07004077 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004078 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004079 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004080 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004081 if (sqe) {
4082 ret = io_sendmsg_prep(req, sqe);
4083 if (ret < 0)
4084 break;
4085 }
Jens Axboefddafac2020-01-04 20:19:44 -07004086 if (req->opcode == IORING_OP_SENDMSG)
4087 ret = io_sendmsg(req, nxt, force_nonblock);
4088 else
4089 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004090 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004091 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004092 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004093 if (sqe) {
4094 ret = io_recvmsg_prep(req, sqe);
4095 if (ret)
4096 break;
4097 }
Jens Axboefddafac2020-01-04 20:19:44 -07004098 if (req->opcode == IORING_OP_RECVMSG)
4099 ret = io_recvmsg(req, nxt, force_nonblock);
4100 else
4101 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004102 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004103 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004104 if (sqe) {
4105 ret = io_timeout_prep(req, sqe, false);
4106 if (ret)
4107 break;
4108 }
Jens Axboefc4df992019-12-10 14:38:45 -07004109 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004110 break;
Jens Axboe11365042019-10-16 09:08:32 -06004111 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004112 if (sqe) {
4113 ret = io_timeout_remove_prep(req, sqe);
4114 if (ret)
4115 break;
4116 }
Jens Axboefc4df992019-12-10 14:38:45 -07004117 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004118 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004119 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004120 if (sqe) {
4121 ret = io_accept_prep(req, sqe);
4122 if (ret)
4123 break;
4124 }
Jens Axboefc4df992019-12-10 14:38:45 -07004125 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004126 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004127 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004128 if (sqe) {
4129 ret = io_connect_prep(req, sqe);
4130 if (ret)
4131 break;
4132 }
Jens Axboefc4df992019-12-10 14:38:45 -07004133 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004134 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004135 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004136 if (sqe) {
4137 ret = io_async_cancel_prep(req, sqe);
4138 if (ret)
4139 break;
4140 }
Jens Axboefc4df992019-12-10 14:38:45 -07004141 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004142 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004143 case IORING_OP_FALLOCATE:
4144 if (sqe) {
4145 ret = io_fallocate_prep(req, sqe);
4146 if (ret)
4147 break;
4148 }
4149 ret = io_fallocate(req, nxt, force_nonblock);
4150 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004151 case IORING_OP_OPENAT:
4152 if (sqe) {
4153 ret = io_openat_prep(req, sqe);
4154 if (ret)
4155 break;
4156 }
4157 ret = io_openat(req, nxt, force_nonblock);
4158 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004159 case IORING_OP_CLOSE:
4160 if (sqe) {
4161 ret = io_close_prep(req, sqe);
4162 if (ret)
4163 break;
4164 }
4165 ret = io_close(req, nxt, force_nonblock);
4166 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004167 case IORING_OP_FILES_UPDATE:
4168 if (sqe) {
4169 ret = io_files_update_prep(req, sqe);
4170 if (ret)
4171 break;
4172 }
4173 ret = io_files_update(req, force_nonblock);
4174 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004175 case IORING_OP_STATX:
4176 if (sqe) {
4177 ret = io_statx_prep(req, sqe);
4178 if (ret)
4179 break;
4180 }
4181 ret = io_statx(req, nxt, force_nonblock);
4182 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004183 case IORING_OP_FADVISE:
4184 if (sqe) {
4185 ret = io_fadvise_prep(req, sqe);
4186 if (ret)
4187 break;
4188 }
4189 ret = io_fadvise(req, nxt, force_nonblock);
4190 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004191 case IORING_OP_MADVISE:
4192 if (sqe) {
4193 ret = io_madvise_prep(req, sqe);
4194 if (ret)
4195 break;
4196 }
4197 ret = io_madvise(req, nxt, force_nonblock);
4198 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004199 default:
4200 ret = -EINVAL;
4201 break;
4202 }
4203
Jens Axboedef596e2019-01-09 08:59:42 -07004204 if (ret)
4205 return ret;
4206
4207 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004208 const bool in_async = io_wq_current_is_worker();
4209
Jens Axboe9e645e112019-05-10 16:07:28 -06004210 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004211 return -EAGAIN;
4212
Jens Axboe11ba8202020-01-15 21:51:17 -07004213 /* workqueue context doesn't hold uring_lock, grab it now */
4214 if (in_async)
4215 mutex_lock(&ctx->uring_lock);
4216
Jens Axboedef596e2019-01-09 08:59:42 -07004217 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004218
4219 if (in_async)
4220 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004221 }
4222
4223 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004224}
4225
Jens Axboe561fb042019-10-24 07:25:42 -06004226static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004227{
Jens Axboe561fb042019-10-24 07:25:42 -06004228 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004229 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004230 struct io_kiocb *nxt = NULL;
4231 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004232
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004233 /* if NO_CANCEL is set, we must still run the work */
4234 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4235 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004236 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004237 }
Jens Axboe31b51512019-01-18 22:56:34 -07004238
Jens Axboe561fb042019-10-24 07:25:42 -06004239 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004240 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4241 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004242 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004243 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004244 /*
4245 * We can get EAGAIN for polled IO even though we're
4246 * forcing a sync submission from here, since we can't
4247 * wait for request slots on the block side.
4248 */
4249 if (ret != -EAGAIN)
4250 break;
4251 cond_resched();
4252 } while (1);
4253 }
Jens Axboe31b51512019-01-18 22:56:34 -07004254
Jens Axboe561fb042019-10-24 07:25:42 -06004255 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004256 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004257
Jens Axboe561fb042019-10-24 07:25:42 -06004258 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004259 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004260 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004261 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004262 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004263
Jens Axboe561fb042019-10-24 07:25:42 -06004264 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004265 if (!ret && nxt)
4266 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004267}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004268
Jens Axboe15b71ab2019-12-11 11:20:36 -07004269static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004270{
Jens Axboed3656342019-12-18 09:50:26 -07004271 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004272 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004273 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4274 return 0;
4275 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004276}
4277
Jens Axboe65e19f52019-10-26 07:20:21 -06004278static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4279 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004280{
Jens Axboe65e19f52019-10-26 07:20:21 -06004281 struct fixed_file_table *table;
4282
Jens Axboe05f3fb32019-12-09 11:22:50 -07004283 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4284 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004285}
4286
Jens Axboe3529d8c2019-12-19 18:24:38 -07004287static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4288 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004289{
Jackie Liua197f662019-11-08 08:09:12 -07004290 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004291 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004292 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004293
Jens Axboe3529d8c2019-12-19 18:24:38 -07004294 flags = READ_ONCE(sqe->flags);
4295 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004296
Jackie Liu4fe2c962019-09-09 20:50:40 +08004297 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06004298 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06004299
Jens Axboed3656342019-12-18 09:50:26 -07004300 if (!io_req_needs_file(req, fd))
4301 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004302
4303 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004304 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004305 (unsigned) fd >= ctx->nr_user_files))
4306 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004307 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004308 req->file = io_file_from_index(ctx, fd);
4309 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004310 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004311 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004312 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004313 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004314 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004315 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004316 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004317 req->file = io_file_get(state, fd);
4318 if (unlikely(!req->file))
4319 return -EBADF;
4320 }
4321
4322 return 0;
4323}
4324
Jackie Liua197f662019-11-08 08:09:12 -07004325static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004326{
Jens Axboefcb323c2019-10-24 12:39:47 -06004327 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004328 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004329
Jens Axboeb5dba592019-12-11 14:02:38 -07004330 if (!req->ring_file)
4331 return -EBADF;
4332
Jens Axboefcb323c2019-10-24 12:39:47 -06004333 rcu_read_lock();
4334 spin_lock_irq(&ctx->inflight_lock);
4335 /*
4336 * We use the f_ops->flush() handler to ensure that we can flush
4337 * out work accessing these files if the fd is closed. Check if
4338 * the fd has changed since we started down this path, and disallow
4339 * this operation if it has.
4340 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004341 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004342 list_add(&req->inflight_entry, &ctx->inflight_list);
4343 req->flags |= REQ_F_INFLIGHT;
4344 req->work.files = current->files;
4345 ret = 0;
4346 }
4347 spin_unlock_irq(&ctx->inflight_lock);
4348 rcu_read_unlock();
4349
4350 return ret;
4351}
4352
Jens Axboe2665abf2019-11-05 12:40:47 -07004353static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4354{
Jens Axboead8a48a2019-11-15 08:49:11 -07004355 struct io_timeout_data *data = container_of(timer,
4356 struct io_timeout_data, timer);
4357 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004358 struct io_ring_ctx *ctx = req->ctx;
4359 struct io_kiocb *prev = NULL;
4360 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004361
4362 spin_lock_irqsave(&ctx->completion_lock, flags);
4363
4364 /*
4365 * We don't expect the list to be empty, that will only happen if we
4366 * race with the completion of the linked work.
4367 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004368 if (!list_empty(&req->link_list)) {
4369 prev = list_entry(req->link_list.prev, struct io_kiocb,
4370 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004371 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004372 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004373 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4374 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004375 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004376 }
4377
4378 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4379
4380 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004381 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004382 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4383 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004384 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004385 } else {
4386 io_cqring_add_event(req, -ETIME);
4387 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004388 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004389 return HRTIMER_NORESTART;
4390}
4391
Jens Axboead8a48a2019-11-15 08:49:11 -07004392static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004393{
Jens Axboe76a46e02019-11-10 23:34:16 -07004394 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004395
Jens Axboe76a46e02019-11-10 23:34:16 -07004396 /*
4397 * If the list is now empty, then our linked request finished before
4398 * we got a chance to setup the timer
4399 */
4400 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004401 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004402 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004403
Jens Axboead8a48a2019-11-15 08:49:11 -07004404 data->timer.function = io_link_timeout_fn;
4405 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4406 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004407 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004408 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004409
Jens Axboe2665abf2019-11-05 12:40:47 -07004410 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004411 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004412}
4413
Jens Axboead8a48a2019-11-15 08:49:11 -07004414static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004415{
4416 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004417
Jens Axboe2665abf2019-11-05 12:40:47 -07004418 if (!(req->flags & REQ_F_LINK))
4419 return NULL;
4420
Pavel Begunkov44932332019-12-05 16:16:35 +03004421 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4422 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004423 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004424 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004425
Jens Axboe76a46e02019-11-10 23:34:16 -07004426 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004427 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004428}
4429
Jens Axboe3529d8c2019-12-19 18:24:38 -07004430static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004431{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004432 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004433 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004434 int ret;
4435
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004436again:
4437 linked_timeout = io_prep_linked_timeout(req);
4438
Jens Axboe3529d8c2019-12-19 18:24:38 -07004439 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004440
4441 /*
4442 * We async punt it if the file wasn't marked NOWAIT, or if the file
4443 * doesn't support non-blocking read/write attempts
4444 */
4445 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4446 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004447 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4448 ret = io_grab_files(req);
4449 if (ret)
4450 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004451 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004452
4453 /*
4454 * Queued up for async execution, worker will release
4455 * submit reference when the iocb is actually submitted.
4456 */
4457 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004458 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004459 }
Jens Axboee65ef562019-03-12 10:16:44 -06004460
Jens Axboefcb323c2019-10-24 12:39:47 -06004461err:
Jens Axboee65ef562019-03-12 10:16:44 -06004462 /* drop submission reference */
4463 io_put_req(req);
4464
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004465 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004466 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004467 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004468 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004469 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004470 }
4471
Jens Axboee65ef562019-03-12 10:16:44 -06004472 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004473 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004474 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004475 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004476 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004477 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004478done_req:
4479 if (nxt) {
4480 req = nxt;
4481 nxt = NULL;
4482 goto again;
4483 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004484}
4485
Jens Axboe3529d8c2019-12-19 18:24:38 -07004486static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004487{
4488 int ret;
4489
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004490 if (unlikely(req->ctx->drain_next)) {
4491 req->flags |= REQ_F_IO_DRAIN;
4492 req->ctx->drain_next = false;
4493 }
4494 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
4495
Jens Axboe3529d8c2019-12-19 18:24:38 -07004496 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004497 if (ret) {
4498 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004499 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004500 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004501 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004502 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004503 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboece35a472019-12-17 08:04:44 -07004504 /*
4505 * Never try inline submit of IOSQE_ASYNC is set, go straight
4506 * to async execution.
4507 */
4508 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4509 io_queue_async_work(req);
4510 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004511 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004512 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004513}
4514
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004515static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004516{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004517 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004518 io_cqring_add_event(req, -ECANCELED);
4519 io_double_put_req(req);
4520 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004521 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004522}
4523
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004524#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004525 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004526
Jens Axboe3529d8c2019-12-19 18:24:38 -07004527static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4528 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004529{
Jackie Liua197f662019-11-08 08:09:12 -07004530 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004531 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004532 int ret;
4533
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004534 sqe_flags = READ_ONCE(sqe->flags);
4535
Jens Axboe9e645e112019-05-10 16:07:28 -06004536 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004537 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004538 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004539 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004540 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004541 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004542 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004543
Jens Axboe3529d8c2019-12-19 18:24:38 -07004544 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004545 if (unlikely(ret)) {
4546err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004547 io_cqring_add_event(req, ret);
4548 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004549 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004550 }
4551
Jens Axboe9e645e112019-05-10 16:07:28 -06004552 /*
4553 * If we already have a head request, queue this one for async
4554 * submittal once the head completes. If we don't have a head but
4555 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4556 * submitted sync once the chain is complete. If none of those
4557 * conditions are true (normal request), then just queue it.
4558 */
4559 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004560 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004561
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004562 if (sqe_flags & IOSQE_IO_DRAIN)
Pavel Begunkov9d763772019-12-17 02:22:07 +03004563 head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004564
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004565 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004566 req->flags |= REQ_F_HARDLINK;
4567
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004568 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004569 ret = -EAGAIN;
4570 goto err_req;
4571 }
4572
Jens Axboe3529d8c2019-12-19 18:24:38 -07004573 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004574 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004575 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004576 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004577 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004578 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004579 trace_io_uring_link(ctx, req, head);
4580 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004581
4582 /* last request of a link, enqueue the link */
4583 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4584 io_queue_link_head(head);
4585 *link = NULL;
4586 }
4587 } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004588 req->flags |= REQ_F_LINK;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004589 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004590 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004591
Jens Axboe9e645e112019-05-10 16:07:28 -06004592 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004593 ret = io_req_defer_prep(req, sqe);
4594 if (ret)
4595 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004596 *link = req;
4597 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004598 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004599 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004600
4601 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004602}
4603
Jens Axboe9a56a232019-01-09 09:06:50 -07004604/*
4605 * Batched submission is done, ensure local IO is flushed out.
4606 */
4607static void io_submit_state_end(struct io_submit_state *state)
4608{
4609 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004610 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004611 if (state->free_reqs)
4612 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4613 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004614}
4615
4616/*
4617 * Start submission side cache.
4618 */
4619static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004620 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004621{
4622 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004623 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004624 state->file = NULL;
4625 state->ios_left = max_ios;
4626}
4627
Jens Axboe2b188cc2019-01-07 10:46:33 -07004628static void io_commit_sqring(struct io_ring_ctx *ctx)
4629{
Hristo Venev75b28af2019-08-26 17:23:46 +00004630 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004631
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004632 /*
4633 * Ensure any loads from the SQEs are done at this point,
4634 * since once we write the new head, the application could
4635 * write new data to them.
4636 */
4637 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004638}
4639
4640/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004641 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004642 * that is mapped by userspace. This means that care needs to be taken to
4643 * ensure that reads are stable, as we cannot rely on userspace always
4644 * being a good citizen. If members of the sqe are validated and then later
4645 * used, it's important that those reads are done through READ_ONCE() to
4646 * prevent a re-load down the line.
4647 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004648static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4649 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004650{
Hristo Venev75b28af2019-08-26 17:23:46 +00004651 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004652 unsigned head;
4653
4654 /*
4655 * The cached sq head (or cq tail) serves two purposes:
4656 *
4657 * 1) allows us to batch the cost of updating the user visible
4658 * head updates.
4659 * 2) allows the kernel side to track the head on its own, even
4660 * though the application is the one updating it.
4661 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004662 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004663 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004664 /*
4665 * All io need record the previous position, if LINK vs DARIN,
4666 * it can be used to mark the position of the first IO in the
4667 * link list.
4668 */
4669 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004670 *sqe_ptr = &ctx->sq_sqes[head];
4671 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4672 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004673 ctx->cached_sq_head++;
4674 return true;
4675 }
4676
4677 /* drop invalid entries */
4678 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004679 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004680 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004681 return false;
4682}
4683
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004684static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004685 struct file *ring_file, int ring_fd,
4686 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004687{
4688 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004689 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004690 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004691 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004692
Jens Axboec4a2ed72019-11-21 21:01:26 -07004693 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004694 if (test_bit(0, &ctx->sq_check_overflow)) {
4695 if (!list_empty(&ctx->cq_overflow_list) &&
4696 !io_cqring_overflow_flush(ctx, false))
4697 return -EBUSY;
4698 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004699
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004700 /* make sure SQ entry isn't read before tail */
4701 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004702
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004703 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4704 return -EAGAIN;
4705
Jens Axboe6c271ce2019-01-10 11:22:30 -07004706 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004707 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004708 statep = &state;
4709 }
4710
4711 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004712 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004713 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004714
Pavel Begunkov196be952019-11-07 01:41:06 +03004715 req = io_get_req(ctx, statep);
4716 if (unlikely(!req)) {
4717 if (!submitted)
4718 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004719 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004720 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004721 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004722 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004723 break;
4724 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004725
Jens Axboed3656342019-12-18 09:50:26 -07004726 /* will complete beyond this point, count as submitted */
4727 submitted++;
4728
4729 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4730 io_cqring_add_event(req, -EINVAL);
4731 io_double_put_req(req);
4732 break;
4733 }
4734
4735 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004736 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4737 if (!mm_fault) {
4738 use_mm(ctx->sqo_mm);
4739 *mm = ctx->sqo_mm;
4740 }
4741 }
4742
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004743 req->ring_file = ring_file;
4744 req->ring_fd = ring_fd;
4745 req->has_user = *mm != NULL;
4746 req->in_async = async;
4747 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07004748 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004749 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004750 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004751 }
4752
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004753 if (submitted != nr)
4754 percpu_ref_put_many(&ctx->refs, nr - submitted);
Jens Axboe9e645e112019-05-10 16:07:28 -06004755 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004756 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004757 if (statep)
4758 io_submit_state_end(&state);
4759
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004760 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4761 io_commit_sqring(ctx);
4762
Jens Axboe6c271ce2019-01-10 11:22:30 -07004763 return submitted;
4764}
4765
4766static int io_sq_thread(void *data)
4767{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004768 struct io_ring_ctx *ctx = data;
4769 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004770 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004771 mm_segment_t old_fs;
4772 DEFINE_WAIT(wait);
4773 unsigned inflight;
4774 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004775 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004776
Jens Axboe206aefd2019-11-07 18:27:42 -07004777 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004778
Jens Axboe6c271ce2019-01-10 11:22:30 -07004779 old_fs = get_fs();
4780 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004781 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004782
Jens Axboec1edbf52019-11-10 16:56:04 -07004783 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004784 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004785 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004786
4787 if (inflight) {
4788 unsigned nr_events = 0;
4789
4790 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004791 /*
4792 * inflight is the count of the maximum possible
4793 * entries we submitted, but it can be smaller
4794 * if we dropped some of them. If we don't have
4795 * poll entries available, then we know that we
4796 * have nothing left to poll for. Reset the
4797 * inflight count to zero in that case.
4798 */
4799 mutex_lock(&ctx->uring_lock);
4800 if (!list_empty(&ctx->poll_list))
4801 __io_iopoll_check(ctx, &nr_events, 0);
4802 else
4803 inflight = 0;
4804 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004805 } else {
4806 /*
4807 * Normal IO, just pretend everything completed.
4808 * We don't have to poll completions for that.
4809 */
4810 nr_events = inflight;
4811 }
4812
4813 inflight -= nr_events;
4814 if (!inflight)
4815 timeout = jiffies + ctx->sq_thread_idle;
4816 }
4817
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004818 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004819
4820 /*
4821 * If submit got -EBUSY, flag us as needing the application
4822 * to enter the kernel to reap and flush events.
4823 */
4824 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004825 /*
4826 * We're polling. If we're within the defined idle
4827 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004828 * to sleep. The exception is if we got EBUSY doing
4829 * more IO, we should wait for the application to
4830 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004831 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004832 if (inflight ||
4833 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004834 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004835 continue;
4836 }
4837
4838 /*
4839 * Drop cur_mm before scheduling, we can't hold it for
4840 * long periods (or over schedule()). Do this before
4841 * adding ourselves to the waitqueue, as the unuse/drop
4842 * may sleep.
4843 */
4844 if (cur_mm) {
4845 unuse_mm(cur_mm);
4846 mmput(cur_mm);
4847 cur_mm = NULL;
4848 }
4849
4850 prepare_to_wait(&ctx->sqo_wait, &wait,
4851 TASK_INTERRUPTIBLE);
4852
4853 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004854 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004855 /* make sure to read SQ tail after writing flags */
4856 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004857
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004858 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004859 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004860 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004861 finish_wait(&ctx->sqo_wait, &wait);
4862 break;
4863 }
4864 if (signal_pending(current))
4865 flush_signals(current);
4866 schedule();
4867 finish_wait(&ctx->sqo_wait, &wait);
4868
Hristo Venev75b28af2019-08-26 17:23:46 +00004869 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004870 continue;
4871 }
4872 finish_wait(&ctx->sqo_wait, &wait);
4873
Hristo Venev75b28af2019-08-26 17:23:46 +00004874 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004875 }
4876
Jens Axboe8a4955f2019-12-09 14:52:35 -07004877 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004878 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004879 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004880 if (ret > 0)
4881 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004882 }
4883
4884 set_fs(old_fs);
4885 if (cur_mm) {
4886 unuse_mm(cur_mm);
4887 mmput(cur_mm);
4888 }
Jens Axboe181e4482019-11-25 08:52:30 -07004889 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004890
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004891 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004892
Jens Axboe6c271ce2019-01-10 11:22:30 -07004893 return 0;
4894}
4895
Jens Axboebda52162019-09-24 13:47:15 -06004896struct io_wait_queue {
4897 struct wait_queue_entry wq;
4898 struct io_ring_ctx *ctx;
4899 unsigned to_wait;
4900 unsigned nr_timeouts;
4901};
4902
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004903static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004904{
4905 struct io_ring_ctx *ctx = iowq->ctx;
4906
4907 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004908 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004909 * started waiting. For timeouts, we always want to return to userspace,
4910 * regardless of event count.
4911 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004912 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004913 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4914}
4915
4916static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4917 int wake_flags, void *key)
4918{
4919 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4920 wq);
4921
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004922 /* use noflush == true, as we can't safely rely on locking context */
4923 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004924 return -1;
4925
4926 return autoremove_wake_function(curr, mode, wake_flags, key);
4927}
4928
Jens Axboe2b188cc2019-01-07 10:46:33 -07004929/*
4930 * Wait until events become available, if we don't already have some. The
4931 * application must reap them itself, as they reside on the shared cq ring.
4932 */
4933static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4934 const sigset_t __user *sig, size_t sigsz)
4935{
Jens Axboebda52162019-09-24 13:47:15 -06004936 struct io_wait_queue iowq = {
4937 .wq = {
4938 .private = current,
4939 .func = io_wake_function,
4940 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4941 },
4942 .ctx = ctx,
4943 .to_wait = min_events,
4944 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004945 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004946 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004947
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004948 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004949 return 0;
4950
4951 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004952#ifdef CONFIG_COMPAT
4953 if (in_compat_syscall())
4954 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004955 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004956 else
4957#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004958 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004959
Jens Axboe2b188cc2019-01-07 10:46:33 -07004960 if (ret)
4961 return ret;
4962 }
4963
Jens Axboebda52162019-09-24 13:47:15 -06004964 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004965 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004966 do {
4967 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4968 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004969 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004970 break;
4971 schedule();
4972 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004973 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004974 break;
4975 }
4976 } while (1);
4977 finish_wait(&ctx->wait, &iowq.wq);
4978
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004979 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004980
Hristo Venev75b28af2019-08-26 17:23:46 +00004981 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004982}
4983
Jens Axboe6b063142019-01-10 22:13:58 -07004984static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4985{
4986#if defined(CONFIG_UNIX)
4987 if (ctx->ring_sock) {
4988 struct sock *sock = ctx->ring_sock->sk;
4989 struct sk_buff *skb;
4990
4991 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4992 kfree_skb(skb);
4993 }
4994#else
4995 int i;
4996
Jens Axboe65e19f52019-10-26 07:20:21 -06004997 for (i = 0; i < ctx->nr_user_files; i++) {
4998 struct file *file;
4999
5000 file = io_file_from_index(ctx, i);
5001 if (file)
5002 fput(file);
5003 }
Jens Axboe6b063142019-01-10 22:13:58 -07005004#endif
5005}
5006
Jens Axboe05f3fb32019-12-09 11:22:50 -07005007static void io_file_ref_kill(struct percpu_ref *ref)
5008{
5009 struct fixed_file_data *data;
5010
5011 data = container_of(ref, struct fixed_file_data, refs);
5012 complete(&data->done);
5013}
5014
Jens Axboe6b063142019-01-10 22:13:58 -07005015static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5016{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005017 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005018 unsigned nr_tables, i;
5019
Jens Axboe05f3fb32019-12-09 11:22:50 -07005020 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005021 return -ENXIO;
5022
Jens Axboe05f3fb32019-12-09 11:22:50 -07005023 /* protect against inflight atomic switch, which drops the ref */
5024 flush_work(&data->ref_work);
5025 percpu_ref_get(&data->refs);
5026 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5027 wait_for_completion(&data->done);
5028 percpu_ref_put(&data->refs);
5029 percpu_ref_exit(&data->refs);
5030
Jens Axboe6b063142019-01-10 22:13:58 -07005031 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005032 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5033 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005034 kfree(data->table[i].files);
5035 kfree(data->table);
5036 kfree(data);
5037 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005038 ctx->nr_user_files = 0;
5039 return 0;
5040}
5041
Jens Axboe6c271ce2019-01-10 11:22:30 -07005042static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5043{
5044 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005045 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005046 /*
5047 * The park is a bit of a work-around, without it we get
5048 * warning spews on shutdown with SQPOLL set and affinity
5049 * set to a single CPU.
5050 */
Jens Axboe06058632019-04-13 09:26:03 -06005051 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005052 kthread_stop(ctx->sqo_thread);
5053 ctx->sqo_thread = NULL;
5054 }
5055}
5056
Jens Axboe6b063142019-01-10 22:13:58 -07005057static void io_finish_async(struct io_ring_ctx *ctx)
5058{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005059 io_sq_thread_stop(ctx);
5060
Jens Axboe561fb042019-10-24 07:25:42 -06005061 if (ctx->io_wq) {
5062 io_wq_destroy(ctx->io_wq);
5063 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005064 }
5065}
5066
5067#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005068/*
5069 * Ensure the UNIX gc is aware of our file set, so we are certain that
5070 * the io_uring can be safely unregistered on process exit, even if we have
5071 * loops in the file referencing.
5072 */
5073static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5074{
5075 struct sock *sk = ctx->ring_sock->sk;
5076 struct scm_fp_list *fpl;
5077 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005078 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005079
5080 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5081 unsigned long inflight = ctx->user->unix_inflight + nr;
5082
5083 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5084 return -EMFILE;
5085 }
5086
5087 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5088 if (!fpl)
5089 return -ENOMEM;
5090
5091 skb = alloc_skb(0, GFP_KERNEL);
5092 if (!skb) {
5093 kfree(fpl);
5094 return -ENOMEM;
5095 }
5096
5097 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005098
Jens Axboe08a45172019-10-03 08:11:03 -06005099 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005100 fpl->user = get_uid(ctx->user);
5101 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005102 struct file *file = io_file_from_index(ctx, i + offset);
5103
5104 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005105 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005106 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005107 unix_inflight(fpl->user, fpl->fp[nr_files]);
5108 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005109 }
5110
Jens Axboe08a45172019-10-03 08:11:03 -06005111 if (nr_files) {
5112 fpl->max = SCM_MAX_FD;
5113 fpl->count = nr_files;
5114 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005115 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005116 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5117 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005118
Jens Axboe08a45172019-10-03 08:11:03 -06005119 for (i = 0; i < nr_files; i++)
5120 fput(fpl->fp[i]);
5121 } else {
5122 kfree_skb(skb);
5123 kfree(fpl);
5124 }
Jens Axboe6b063142019-01-10 22:13:58 -07005125
5126 return 0;
5127}
5128
5129/*
5130 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5131 * causes regular reference counting to break down. We rely on the UNIX
5132 * garbage collection to take care of this problem for us.
5133 */
5134static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5135{
5136 unsigned left, total;
5137 int ret = 0;
5138
5139 total = 0;
5140 left = ctx->nr_user_files;
5141 while (left) {
5142 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005143
5144 ret = __io_sqe_files_scm(ctx, this_files, total);
5145 if (ret)
5146 break;
5147 left -= this_files;
5148 total += this_files;
5149 }
5150
5151 if (!ret)
5152 return 0;
5153
5154 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005155 struct file *file = io_file_from_index(ctx, total);
5156
5157 if (file)
5158 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005159 total++;
5160 }
5161
5162 return ret;
5163}
5164#else
5165static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5166{
5167 return 0;
5168}
5169#endif
5170
Jens Axboe65e19f52019-10-26 07:20:21 -06005171static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5172 unsigned nr_files)
5173{
5174 int i;
5175
5176 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005177 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005178 unsigned this_files;
5179
5180 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5181 table->files = kcalloc(this_files, sizeof(struct file *),
5182 GFP_KERNEL);
5183 if (!table->files)
5184 break;
5185 nr_files -= this_files;
5186 }
5187
5188 if (i == nr_tables)
5189 return 0;
5190
5191 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005192 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005193 kfree(table->files);
5194 }
5195 return 1;
5196}
5197
Jens Axboe05f3fb32019-12-09 11:22:50 -07005198static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005199{
5200#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005201 struct sock *sock = ctx->ring_sock->sk;
5202 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5203 struct sk_buff *skb;
5204 int i;
5205
5206 __skb_queue_head_init(&list);
5207
5208 /*
5209 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5210 * remove this entry and rearrange the file array.
5211 */
5212 skb = skb_dequeue(head);
5213 while (skb) {
5214 struct scm_fp_list *fp;
5215
5216 fp = UNIXCB(skb).fp;
5217 for (i = 0; i < fp->count; i++) {
5218 int left;
5219
5220 if (fp->fp[i] != file)
5221 continue;
5222
5223 unix_notinflight(fp->user, fp->fp[i]);
5224 left = fp->count - 1 - i;
5225 if (left) {
5226 memmove(&fp->fp[i], &fp->fp[i + 1],
5227 left * sizeof(struct file *));
5228 }
5229 fp->count--;
5230 if (!fp->count) {
5231 kfree_skb(skb);
5232 skb = NULL;
5233 } else {
5234 __skb_queue_tail(&list, skb);
5235 }
5236 fput(file);
5237 file = NULL;
5238 break;
5239 }
5240
5241 if (!file)
5242 break;
5243
5244 __skb_queue_tail(&list, skb);
5245
5246 skb = skb_dequeue(head);
5247 }
5248
5249 if (skb_peek(&list)) {
5250 spin_lock_irq(&head->lock);
5251 while ((skb = __skb_dequeue(&list)) != NULL)
5252 __skb_queue_tail(head, skb);
5253 spin_unlock_irq(&head->lock);
5254 }
5255#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005256 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005257#endif
5258}
5259
Jens Axboe05f3fb32019-12-09 11:22:50 -07005260struct io_file_put {
5261 struct llist_node llist;
5262 struct file *file;
5263 struct completion *done;
5264};
5265
5266static void io_ring_file_ref_switch(struct work_struct *work)
5267{
5268 struct io_file_put *pfile, *tmp;
5269 struct fixed_file_data *data;
5270 struct llist_node *node;
5271
5272 data = container_of(work, struct fixed_file_data, ref_work);
5273
5274 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5275 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5276 io_ring_file_put(data->ctx, pfile->file);
5277 if (pfile->done)
5278 complete(pfile->done);
5279 else
5280 kfree(pfile);
5281 }
5282 }
5283
5284 percpu_ref_get(&data->refs);
5285 percpu_ref_switch_to_percpu(&data->refs);
5286}
5287
5288static void io_file_data_ref_zero(struct percpu_ref *ref)
5289{
5290 struct fixed_file_data *data;
5291
5292 data = container_of(ref, struct fixed_file_data, refs);
5293
5294 /* we can't safely switch from inside this context, punt to wq */
5295 queue_work(system_wq, &data->ref_work);
5296}
5297
5298static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5299 unsigned nr_args)
5300{
5301 __s32 __user *fds = (__s32 __user *) arg;
5302 unsigned nr_tables;
5303 struct file *file;
5304 int fd, ret = 0;
5305 unsigned i;
5306
5307 if (ctx->file_data)
5308 return -EBUSY;
5309 if (!nr_args)
5310 return -EINVAL;
5311 if (nr_args > IORING_MAX_FIXED_FILES)
5312 return -EMFILE;
5313
5314 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5315 if (!ctx->file_data)
5316 return -ENOMEM;
5317 ctx->file_data->ctx = ctx;
5318 init_completion(&ctx->file_data->done);
5319
5320 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5321 ctx->file_data->table = kcalloc(nr_tables,
5322 sizeof(struct fixed_file_table),
5323 GFP_KERNEL);
5324 if (!ctx->file_data->table) {
5325 kfree(ctx->file_data);
5326 ctx->file_data = NULL;
5327 return -ENOMEM;
5328 }
5329
5330 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5331 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5332 kfree(ctx->file_data->table);
5333 kfree(ctx->file_data);
5334 ctx->file_data = NULL;
5335 return -ENOMEM;
5336 }
5337 ctx->file_data->put_llist.first = NULL;
5338 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5339
5340 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5341 percpu_ref_exit(&ctx->file_data->refs);
5342 kfree(ctx->file_data->table);
5343 kfree(ctx->file_data);
5344 ctx->file_data = NULL;
5345 return -ENOMEM;
5346 }
5347
5348 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5349 struct fixed_file_table *table;
5350 unsigned index;
5351
5352 ret = -EFAULT;
5353 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5354 break;
5355 /* allow sparse sets */
5356 if (fd == -1) {
5357 ret = 0;
5358 continue;
5359 }
5360
5361 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5362 index = i & IORING_FILE_TABLE_MASK;
5363 file = fget(fd);
5364
5365 ret = -EBADF;
5366 if (!file)
5367 break;
5368
5369 /*
5370 * Don't allow io_uring instances to be registered. If UNIX
5371 * isn't enabled, then this causes a reference cycle and this
5372 * instance can never get freed. If UNIX is enabled we'll
5373 * handle it just fine, but there's still no point in allowing
5374 * a ring fd as it doesn't support regular read/write anyway.
5375 */
5376 if (file->f_op == &io_uring_fops) {
5377 fput(file);
5378 break;
5379 }
5380 ret = 0;
5381 table->files[index] = file;
5382 }
5383
5384 if (ret) {
5385 for (i = 0; i < ctx->nr_user_files; i++) {
5386 file = io_file_from_index(ctx, i);
5387 if (file)
5388 fput(file);
5389 }
5390 for (i = 0; i < nr_tables; i++)
5391 kfree(ctx->file_data->table[i].files);
5392
5393 kfree(ctx->file_data->table);
5394 kfree(ctx->file_data);
5395 ctx->file_data = NULL;
5396 ctx->nr_user_files = 0;
5397 return ret;
5398 }
5399
5400 ret = io_sqe_files_scm(ctx);
5401 if (ret)
5402 io_sqe_files_unregister(ctx);
5403
5404 return ret;
5405}
5406
Jens Axboec3a31e62019-10-03 13:59:56 -06005407static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5408 int index)
5409{
5410#if defined(CONFIG_UNIX)
5411 struct sock *sock = ctx->ring_sock->sk;
5412 struct sk_buff_head *head = &sock->sk_receive_queue;
5413 struct sk_buff *skb;
5414
5415 /*
5416 * See if we can merge this file into an existing skb SCM_RIGHTS
5417 * file set. If there's no room, fall back to allocating a new skb
5418 * and filling it in.
5419 */
5420 spin_lock_irq(&head->lock);
5421 skb = skb_peek(head);
5422 if (skb) {
5423 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5424
5425 if (fpl->count < SCM_MAX_FD) {
5426 __skb_unlink(skb, head);
5427 spin_unlock_irq(&head->lock);
5428 fpl->fp[fpl->count] = get_file(file);
5429 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5430 fpl->count++;
5431 spin_lock_irq(&head->lock);
5432 __skb_queue_head(head, skb);
5433 } else {
5434 skb = NULL;
5435 }
5436 }
5437 spin_unlock_irq(&head->lock);
5438
5439 if (skb) {
5440 fput(file);
5441 return 0;
5442 }
5443
5444 return __io_sqe_files_scm(ctx, 1, index);
5445#else
5446 return 0;
5447#endif
5448}
5449
Jens Axboe05f3fb32019-12-09 11:22:50 -07005450static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005451{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005452 struct fixed_file_data *data;
5453
5454 data = container_of(ref, struct fixed_file_data, refs);
5455 clear_bit(FFD_F_ATOMIC, &data->state);
5456}
5457
5458static bool io_queue_file_removal(struct fixed_file_data *data,
5459 struct file *file)
5460{
5461 struct io_file_put *pfile, pfile_stack;
5462 DECLARE_COMPLETION_ONSTACK(done);
5463
5464 /*
5465 * If we fail allocating the struct we need for doing async reomval
5466 * of this file, just punt to sync and wait for it.
5467 */
5468 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5469 if (!pfile) {
5470 pfile = &pfile_stack;
5471 pfile->done = &done;
5472 }
5473
5474 pfile->file = file;
5475 llist_add(&pfile->llist, &data->put_llist);
5476
5477 if (pfile == &pfile_stack) {
5478 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5479 percpu_ref_put(&data->refs);
5480 percpu_ref_switch_to_atomic(&data->refs,
5481 io_atomic_switch);
5482 }
5483 wait_for_completion(&done);
5484 flush_work(&data->ref_work);
5485 return false;
5486 }
5487
5488 return true;
5489}
5490
5491static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5492 struct io_uring_files_update *up,
5493 unsigned nr_args)
5494{
5495 struct fixed_file_data *data = ctx->file_data;
5496 bool ref_switch = false;
5497 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005498 __s32 __user *fds;
5499 int fd, i, err;
5500 __u32 done;
5501
Jens Axboe05f3fb32019-12-09 11:22:50 -07005502 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005503 return -EOVERFLOW;
5504 if (done > ctx->nr_user_files)
5505 return -EINVAL;
5506
5507 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005508 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005509 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005510 struct fixed_file_table *table;
5511 unsigned index;
5512
Jens Axboec3a31e62019-10-03 13:59:56 -06005513 err = 0;
5514 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5515 err = -EFAULT;
5516 break;
5517 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005518 i = array_index_nospec(up->offset, ctx->nr_user_files);
5519 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005520 index = i & IORING_FILE_TABLE_MASK;
5521 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005522 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005523 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005524 if (io_queue_file_removal(data, file))
5525 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005526 }
5527 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005528 file = fget(fd);
5529 if (!file) {
5530 err = -EBADF;
5531 break;
5532 }
5533 /*
5534 * Don't allow io_uring instances to be registered. If
5535 * UNIX isn't enabled, then this causes a reference
5536 * cycle and this instance can never get freed. If UNIX
5537 * is enabled we'll handle it just fine, but there's
5538 * still no point in allowing a ring fd as it doesn't
5539 * support regular read/write anyway.
5540 */
5541 if (file->f_op == &io_uring_fops) {
5542 fput(file);
5543 err = -EBADF;
5544 break;
5545 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005546 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005547 err = io_sqe_file_register(ctx, file, i);
5548 if (err)
5549 break;
5550 }
5551 nr_args--;
5552 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005553 up->offset++;
5554 }
5555
5556 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5557 percpu_ref_put(&data->refs);
5558 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005559 }
5560
5561 return done ? done : err;
5562}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005563static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5564 unsigned nr_args)
5565{
5566 struct io_uring_files_update up;
5567
5568 if (!ctx->file_data)
5569 return -ENXIO;
5570 if (!nr_args)
5571 return -EINVAL;
5572 if (copy_from_user(&up, arg, sizeof(up)))
5573 return -EFAULT;
5574 if (up.resv)
5575 return -EINVAL;
5576
5577 return __io_sqe_files_update(ctx, &up, nr_args);
5578}
Jens Axboec3a31e62019-10-03 13:59:56 -06005579
Jens Axboe7d723062019-11-12 22:31:31 -07005580static void io_put_work(struct io_wq_work *work)
5581{
5582 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5583
5584 io_put_req(req);
5585}
5586
5587static void io_get_work(struct io_wq_work *work)
5588{
5589 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5590
5591 refcount_inc(&req->refs);
5592}
5593
Jens Axboe6c271ce2019-01-10 11:22:30 -07005594static int io_sq_offload_start(struct io_ring_ctx *ctx,
5595 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005596{
Jens Axboe576a3472019-11-25 08:49:20 -07005597 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005598 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005599 int ret;
5600
Jens Axboe6c271ce2019-01-10 11:22:30 -07005601 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005602 mmgrab(current->mm);
5603 ctx->sqo_mm = current->mm;
5604
Jens Axboe6c271ce2019-01-10 11:22:30 -07005605 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005606 ret = -EPERM;
5607 if (!capable(CAP_SYS_ADMIN))
5608 goto err;
5609
Jens Axboe917257d2019-04-13 09:28:55 -06005610 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5611 if (!ctx->sq_thread_idle)
5612 ctx->sq_thread_idle = HZ;
5613
Jens Axboe6c271ce2019-01-10 11:22:30 -07005614 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005615 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005616
Jens Axboe917257d2019-04-13 09:28:55 -06005617 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005618 if (cpu >= nr_cpu_ids)
5619 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005620 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005621 goto err;
5622
Jens Axboe6c271ce2019-01-10 11:22:30 -07005623 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5624 ctx, cpu,
5625 "io_uring-sq");
5626 } else {
5627 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5628 "io_uring-sq");
5629 }
5630 if (IS_ERR(ctx->sqo_thread)) {
5631 ret = PTR_ERR(ctx->sqo_thread);
5632 ctx->sqo_thread = NULL;
5633 goto err;
5634 }
5635 wake_up_process(ctx->sqo_thread);
5636 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5637 /* Can't have SQ_AFF without SQPOLL */
5638 ret = -EINVAL;
5639 goto err;
5640 }
5641
Jens Axboe576a3472019-11-25 08:49:20 -07005642 data.mm = ctx->sqo_mm;
5643 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005644 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005645 data.get_work = io_get_work;
5646 data.put_work = io_put_work;
5647
Jens Axboe561fb042019-10-24 07:25:42 -06005648 /* Do QD, or 4 * CPUS, whatever is smallest */
5649 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005650 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005651 if (IS_ERR(ctx->io_wq)) {
5652 ret = PTR_ERR(ctx->io_wq);
5653 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005654 goto err;
5655 }
5656
5657 return 0;
5658err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005659 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005660 mmdrop(ctx->sqo_mm);
5661 ctx->sqo_mm = NULL;
5662 return ret;
5663}
5664
5665static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5666{
5667 atomic_long_sub(nr_pages, &user->locked_vm);
5668}
5669
5670static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5671{
5672 unsigned long page_limit, cur_pages, new_pages;
5673
5674 /* Don't allow more pages than we can safely lock */
5675 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5676
5677 do {
5678 cur_pages = atomic_long_read(&user->locked_vm);
5679 new_pages = cur_pages + nr_pages;
5680 if (new_pages > page_limit)
5681 return -ENOMEM;
5682 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5683 new_pages) != cur_pages);
5684
5685 return 0;
5686}
5687
5688static void io_mem_free(void *ptr)
5689{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005690 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005691
Mark Rutland52e04ef2019-04-30 17:30:21 +01005692 if (!ptr)
5693 return;
5694
5695 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005696 if (put_page_testzero(page))
5697 free_compound_page(page);
5698}
5699
5700static void *io_mem_alloc(size_t size)
5701{
5702 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5703 __GFP_NORETRY;
5704
5705 return (void *) __get_free_pages(gfp_flags, get_order(size));
5706}
5707
Hristo Venev75b28af2019-08-26 17:23:46 +00005708static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5709 size_t *sq_offset)
5710{
5711 struct io_rings *rings;
5712 size_t off, sq_array_size;
5713
5714 off = struct_size(rings, cqes, cq_entries);
5715 if (off == SIZE_MAX)
5716 return SIZE_MAX;
5717
5718#ifdef CONFIG_SMP
5719 off = ALIGN(off, SMP_CACHE_BYTES);
5720 if (off == 0)
5721 return SIZE_MAX;
5722#endif
5723
5724 sq_array_size = array_size(sizeof(u32), sq_entries);
5725 if (sq_array_size == SIZE_MAX)
5726 return SIZE_MAX;
5727
5728 if (check_add_overflow(off, sq_array_size, &off))
5729 return SIZE_MAX;
5730
5731 if (sq_offset)
5732 *sq_offset = off;
5733
5734 return off;
5735}
5736
Jens Axboe2b188cc2019-01-07 10:46:33 -07005737static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5738{
Hristo Venev75b28af2019-08-26 17:23:46 +00005739 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005740
Hristo Venev75b28af2019-08-26 17:23:46 +00005741 pages = (size_t)1 << get_order(
5742 rings_size(sq_entries, cq_entries, NULL));
5743 pages += (size_t)1 << get_order(
5744 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005745
Hristo Venev75b28af2019-08-26 17:23:46 +00005746 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005747}
5748
Jens Axboeedafcce2019-01-09 09:16:05 -07005749static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5750{
5751 int i, j;
5752
5753 if (!ctx->user_bufs)
5754 return -ENXIO;
5755
5756 for (i = 0; i < ctx->nr_user_bufs; i++) {
5757 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5758
5759 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005760 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005761
5762 if (ctx->account_mem)
5763 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005764 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005765 imu->nr_bvecs = 0;
5766 }
5767
5768 kfree(ctx->user_bufs);
5769 ctx->user_bufs = NULL;
5770 ctx->nr_user_bufs = 0;
5771 return 0;
5772}
5773
5774static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5775 void __user *arg, unsigned index)
5776{
5777 struct iovec __user *src;
5778
5779#ifdef CONFIG_COMPAT
5780 if (ctx->compat) {
5781 struct compat_iovec __user *ciovs;
5782 struct compat_iovec ciov;
5783
5784 ciovs = (struct compat_iovec __user *) arg;
5785 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5786 return -EFAULT;
5787
Jens Axboed55e5f52019-12-11 16:12:15 -07005788 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005789 dst->iov_len = ciov.iov_len;
5790 return 0;
5791 }
5792#endif
5793 src = (struct iovec __user *) arg;
5794 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5795 return -EFAULT;
5796 return 0;
5797}
5798
5799static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5800 unsigned nr_args)
5801{
5802 struct vm_area_struct **vmas = NULL;
5803 struct page **pages = NULL;
5804 int i, j, got_pages = 0;
5805 int ret = -EINVAL;
5806
5807 if (ctx->user_bufs)
5808 return -EBUSY;
5809 if (!nr_args || nr_args > UIO_MAXIOV)
5810 return -EINVAL;
5811
5812 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5813 GFP_KERNEL);
5814 if (!ctx->user_bufs)
5815 return -ENOMEM;
5816
5817 for (i = 0; i < nr_args; i++) {
5818 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5819 unsigned long off, start, end, ubuf;
5820 int pret, nr_pages;
5821 struct iovec iov;
5822 size_t size;
5823
5824 ret = io_copy_iov(ctx, &iov, arg, i);
5825 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005826 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005827
5828 /*
5829 * Don't impose further limits on the size and buffer
5830 * constraints here, we'll -EINVAL later when IO is
5831 * submitted if they are wrong.
5832 */
5833 ret = -EFAULT;
5834 if (!iov.iov_base || !iov.iov_len)
5835 goto err;
5836
5837 /* arbitrary limit, but we need something */
5838 if (iov.iov_len > SZ_1G)
5839 goto err;
5840
5841 ubuf = (unsigned long) iov.iov_base;
5842 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5843 start = ubuf >> PAGE_SHIFT;
5844 nr_pages = end - start;
5845
5846 if (ctx->account_mem) {
5847 ret = io_account_mem(ctx->user, nr_pages);
5848 if (ret)
5849 goto err;
5850 }
5851
5852 ret = 0;
5853 if (!pages || nr_pages > got_pages) {
5854 kfree(vmas);
5855 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005856 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005857 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005858 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005859 sizeof(struct vm_area_struct *),
5860 GFP_KERNEL);
5861 if (!pages || !vmas) {
5862 ret = -ENOMEM;
5863 if (ctx->account_mem)
5864 io_unaccount_mem(ctx->user, nr_pages);
5865 goto err;
5866 }
5867 got_pages = nr_pages;
5868 }
5869
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005870 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005871 GFP_KERNEL);
5872 ret = -ENOMEM;
5873 if (!imu->bvec) {
5874 if (ctx->account_mem)
5875 io_unaccount_mem(ctx->user, nr_pages);
5876 goto err;
5877 }
5878
5879 ret = 0;
5880 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005881 pret = get_user_pages(ubuf, nr_pages,
5882 FOLL_WRITE | FOLL_LONGTERM,
5883 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005884 if (pret == nr_pages) {
5885 /* don't support file backed memory */
5886 for (j = 0; j < nr_pages; j++) {
5887 struct vm_area_struct *vma = vmas[j];
5888
5889 if (vma->vm_file &&
5890 !is_file_hugepages(vma->vm_file)) {
5891 ret = -EOPNOTSUPP;
5892 break;
5893 }
5894 }
5895 } else {
5896 ret = pret < 0 ? pret : -EFAULT;
5897 }
5898 up_read(&current->mm->mmap_sem);
5899 if (ret) {
5900 /*
5901 * if we did partial map, or found file backed vmas,
5902 * release any pages we did get
5903 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005904 if (pret > 0)
5905 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005906 if (ctx->account_mem)
5907 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005908 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005909 goto err;
5910 }
5911
5912 off = ubuf & ~PAGE_MASK;
5913 size = iov.iov_len;
5914 for (j = 0; j < nr_pages; j++) {
5915 size_t vec_len;
5916
5917 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5918 imu->bvec[j].bv_page = pages[j];
5919 imu->bvec[j].bv_len = vec_len;
5920 imu->bvec[j].bv_offset = off;
5921 off = 0;
5922 size -= vec_len;
5923 }
5924 /* store original address for later verification */
5925 imu->ubuf = ubuf;
5926 imu->len = iov.iov_len;
5927 imu->nr_bvecs = nr_pages;
5928
5929 ctx->nr_user_bufs++;
5930 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005931 kvfree(pages);
5932 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005933 return 0;
5934err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005935 kvfree(pages);
5936 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005937 io_sqe_buffer_unregister(ctx);
5938 return ret;
5939}
5940
Jens Axboe9b402842019-04-11 11:45:41 -06005941static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5942{
5943 __s32 __user *fds = arg;
5944 int fd;
5945
5946 if (ctx->cq_ev_fd)
5947 return -EBUSY;
5948
5949 if (copy_from_user(&fd, fds, sizeof(*fds)))
5950 return -EFAULT;
5951
5952 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5953 if (IS_ERR(ctx->cq_ev_fd)) {
5954 int ret = PTR_ERR(ctx->cq_ev_fd);
5955 ctx->cq_ev_fd = NULL;
5956 return ret;
5957 }
5958
5959 return 0;
5960}
5961
5962static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5963{
5964 if (ctx->cq_ev_fd) {
5965 eventfd_ctx_put(ctx->cq_ev_fd);
5966 ctx->cq_ev_fd = NULL;
5967 return 0;
5968 }
5969
5970 return -ENXIO;
5971}
5972
Jens Axboe2b188cc2019-01-07 10:46:33 -07005973static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5974{
Jens Axboe6b063142019-01-10 22:13:58 -07005975 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005976 if (ctx->sqo_mm)
5977 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005978
5979 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005980 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005981 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005982 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005983
Jens Axboe2b188cc2019-01-07 10:46:33 -07005984#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005985 if (ctx->ring_sock) {
5986 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005987 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005988 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005989#endif
5990
Hristo Venev75b28af2019-08-26 17:23:46 +00005991 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005992 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005993
5994 percpu_ref_exit(&ctx->refs);
5995 if (ctx->account_mem)
5996 io_unaccount_mem(ctx->user,
5997 ring_pages(ctx->sq_entries, ctx->cq_entries));
5998 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07005999 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006000 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006001 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006002 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006003 kfree(ctx);
6004}
6005
6006static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6007{
6008 struct io_ring_ctx *ctx = file->private_data;
6009 __poll_t mask = 0;
6010
6011 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006012 /*
6013 * synchronizes with barrier from wq_has_sleeper call in
6014 * io_commit_cqring
6015 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006016 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006017 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6018 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006019 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006020 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006021 mask |= EPOLLIN | EPOLLRDNORM;
6022
6023 return mask;
6024}
6025
6026static int io_uring_fasync(int fd, struct file *file, int on)
6027{
6028 struct io_ring_ctx *ctx = file->private_data;
6029
6030 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6031}
6032
6033static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6034{
6035 mutex_lock(&ctx->uring_lock);
6036 percpu_ref_kill(&ctx->refs);
6037 mutex_unlock(&ctx->uring_lock);
6038
Jens Axboe5262f562019-09-17 12:26:57 -06006039 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006040 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006041
6042 if (ctx->io_wq)
6043 io_wq_cancel_all(ctx->io_wq);
6044
Jens Axboedef596e2019-01-09 08:59:42 -07006045 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006046 /* if we failed setting up the ctx, we might not have any rings */
6047 if (ctx->rings)
6048 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07006049 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006050 io_ring_ctx_free(ctx);
6051}
6052
6053static int io_uring_release(struct inode *inode, struct file *file)
6054{
6055 struct io_ring_ctx *ctx = file->private_data;
6056
6057 file->private_data = NULL;
6058 io_ring_ctx_wait_and_kill(ctx);
6059 return 0;
6060}
6061
Jens Axboefcb323c2019-10-24 12:39:47 -06006062static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6063 struct files_struct *files)
6064{
6065 struct io_kiocb *req;
6066 DEFINE_WAIT(wait);
6067
6068 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006069 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006070
6071 spin_lock_irq(&ctx->inflight_lock);
6072 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006073 if (req->work.files != files)
6074 continue;
6075 /* req is being completed, ignore */
6076 if (!refcount_inc_not_zero(&req->refs))
6077 continue;
6078 cancel_req = req;
6079 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006080 }
Jens Axboe768134d2019-11-10 20:30:53 -07006081 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006082 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006083 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006084 spin_unlock_irq(&ctx->inflight_lock);
6085
Jens Axboe768134d2019-11-10 20:30:53 -07006086 /* We need to keep going until we don't find a matching req */
6087 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006088 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006089
6090 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6091 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006092 schedule();
6093 }
Jens Axboe768134d2019-11-10 20:30:53 -07006094 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006095}
6096
6097static int io_uring_flush(struct file *file, void *data)
6098{
6099 struct io_ring_ctx *ctx = file->private_data;
6100
6101 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006102 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6103 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006104 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006105 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006106 return 0;
6107}
6108
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006109static void *io_uring_validate_mmap_request(struct file *file,
6110 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006111{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006112 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006113 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006114 struct page *page;
6115 void *ptr;
6116
6117 switch (offset) {
6118 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006119 case IORING_OFF_CQ_RING:
6120 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006121 break;
6122 case IORING_OFF_SQES:
6123 ptr = ctx->sq_sqes;
6124 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006125 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006126 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006127 }
6128
6129 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006130 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006131 return ERR_PTR(-EINVAL);
6132
6133 return ptr;
6134}
6135
6136#ifdef CONFIG_MMU
6137
6138static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6139{
6140 size_t sz = vma->vm_end - vma->vm_start;
6141 unsigned long pfn;
6142 void *ptr;
6143
6144 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6145 if (IS_ERR(ptr))
6146 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006147
6148 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6149 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6150}
6151
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006152#else /* !CONFIG_MMU */
6153
6154static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6155{
6156 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6157}
6158
6159static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6160{
6161 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6162}
6163
6164static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6165 unsigned long addr, unsigned long len,
6166 unsigned long pgoff, unsigned long flags)
6167{
6168 void *ptr;
6169
6170 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6171 if (IS_ERR(ptr))
6172 return PTR_ERR(ptr);
6173
6174 return (unsigned long) ptr;
6175}
6176
6177#endif /* !CONFIG_MMU */
6178
Jens Axboe2b188cc2019-01-07 10:46:33 -07006179SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6180 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6181 size_t, sigsz)
6182{
6183 struct io_ring_ctx *ctx;
6184 long ret = -EBADF;
6185 int submitted = 0;
6186 struct fd f;
6187
Jens Axboe6c271ce2019-01-10 11:22:30 -07006188 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006189 return -EINVAL;
6190
6191 f = fdget(fd);
6192 if (!f.file)
6193 return -EBADF;
6194
6195 ret = -EOPNOTSUPP;
6196 if (f.file->f_op != &io_uring_fops)
6197 goto out_fput;
6198
6199 ret = -ENXIO;
6200 ctx = f.file->private_data;
6201 if (!percpu_ref_tryget(&ctx->refs))
6202 goto out_fput;
6203
Jens Axboe6c271ce2019-01-10 11:22:30 -07006204 /*
6205 * For SQ polling, the thread will do all submissions and completions.
6206 * Just return the requested submit count, and wake the thread if
6207 * we were asked to.
6208 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006209 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006210 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006211 if (!list_empty_careful(&ctx->cq_overflow_list))
6212 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006213 if (flags & IORING_ENTER_SQ_WAKEUP)
6214 wake_up(&ctx->sqo_wait);
6215 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006216 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006217 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006218
Jens Axboe44d28272020-01-16 19:00:24 -07006219 if (current->mm != ctx->sqo_mm ||
6220 current_cred() != ctx->creds) {
6221 ret = -EPERM;
6222 goto out;
6223 }
6224
Jens Axboe2b188cc2019-01-07 10:46:33 -07006225 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006226 /* already have mm, so io_submit_sqes() won't try to grab it */
6227 cur_mm = ctx->sqo_mm;
6228 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6229 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006230 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006231
6232 if (submitted != to_submit)
6233 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006234 }
6235 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006236 unsigned nr_events = 0;
6237
Jens Axboe2b188cc2019-01-07 10:46:33 -07006238 min_complete = min(min_complete, ctx->cq_entries);
6239
Jens Axboedef596e2019-01-09 08:59:42 -07006240 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006241 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006242 } else {
6243 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6244 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006245 }
6246
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006247out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006248 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006249out_fput:
6250 fdput(f);
6251 return submitted ? submitted : ret;
6252}
6253
6254static const struct file_operations io_uring_fops = {
6255 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006256 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006257 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006258#ifndef CONFIG_MMU
6259 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6260 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6261#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006262 .poll = io_uring_poll,
6263 .fasync = io_uring_fasync,
6264};
6265
6266static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6267 struct io_uring_params *p)
6268{
Hristo Venev75b28af2019-08-26 17:23:46 +00006269 struct io_rings *rings;
6270 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006271
Hristo Venev75b28af2019-08-26 17:23:46 +00006272 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6273 if (size == SIZE_MAX)
6274 return -EOVERFLOW;
6275
6276 rings = io_mem_alloc(size);
6277 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006278 return -ENOMEM;
6279
Hristo Venev75b28af2019-08-26 17:23:46 +00006280 ctx->rings = rings;
6281 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6282 rings->sq_ring_mask = p->sq_entries - 1;
6283 rings->cq_ring_mask = p->cq_entries - 1;
6284 rings->sq_ring_entries = p->sq_entries;
6285 rings->cq_ring_entries = p->cq_entries;
6286 ctx->sq_mask = rings->sq_ring_mask;
6287 ctx->cq_mask = rings->cq_ring_mask;
6288 ctx->sq_entries = rings->sq_ring_entries;
6289 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006290
6291 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006292 if (size == SIZE_MAX) {
6293 io_mem_free(ctx->rings);
6294 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006295 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006296 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006297
6298 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006299 if (!ctx->sq_sqes) {
6300 io_mem_free(ctx->rings);
6301 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006302 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006303 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006304
Jens Axboe2b188cc2019-01-07 10:46:33 -07006305 return 0;
6306}
6307
6308/*
6309 * Allocate an anonymous fd, this is what constitutes the application
6310 * visible backing of an io_uring instance. The application mmaps this
6311 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6312 * we have to tie this fd to a socket for file garbage collection purposes.
6313 */
6314static int io_uring_get_fd(struct io_ring_ctx *ctx)
6315{
6316 struct file *file;
6317 int ret;
6318
6319#if defined(CONFIG_UNIX)
6320 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6321 &ctx->ring_sock);
6322 if (ret)
6323 return ret;
6324#endif
6325
6326 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6327 if (ret < 0)
6328 goto err;
6329
6330 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6331 O_RDWR | O_CLOEXEC);
6332 if (IS_ERR(file)) {
6333 put_unused_fd(ret);
6334 ret = PTR_ERR(file);
6335 goto err;
6336 }
6337
6338#if defined(CONFIG_UNIX)
6339 ctx->ring_sock->file = file;
6340#endif
6341 fd_install(ret, file);
6342 return ret;
6343err:
6344#if defined(CONFIG_UNIX)
6345 sock_release(ctx->ring_sock);
6346 ctx->ring_sock = NULL;
6347#endif
6348 return ret;
6349}
6350
6351static int io_uring_create(unsigned entries, struct io_uring_params *p)
6352{
6353 struct user_struct *user = NULL;
6354 struct io_ring_ctx *ctx;
6355 bool account_mem;
6356 int ret;
6357
Jens Axboe8110c1a2019-12-28 15:39:54 -07006358 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006359 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006360 if (entries > IORING_MAX_ENTRIES) {
6361 if (!(p->flags & IORING_SETUP_CLAMP))
6362 return -EINVAL;
6363 entries = IORING_MAX_ENTRIES;
6364 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006365
6366 /*
6367 * Use twice as many entries for the CQ ring. It's possible for the
6368 * application to drive a higher depth than the size of the SQ ring,
6369 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006370 * some flexibility in overcommitting a bit. If the application has
6371 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6372 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006373 */
6374 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006375 if (p->flags & IORING_SETUP_CQSIZE) {
6376 /*
6377 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6378 * to a power-of-two, if it isn't already. We do NOT impose
6379 * any cq vs sq ring sizing.
6380 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006381 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006382 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006383 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6384 if (!(p->flags & IORING_SETUP_CLAMP))
6385 return -EINVAL;
6386 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6387 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006388 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6389 } else {
6390 p->cq_entries = 2 * p->sq_entries;
6391 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006392
6393 user = get_uid(current_user());
6394 account_mem = !capable(CAP_IPC_LOCK);
6395
6396 if (account_mem) {
6397 ret = io_account_mem(user,
6398 ring_pages(p->sq_entries, p->cq_entries));
6399 if (ret) {
6400 free_uid(user);
6401 return ret;
6402 }
6403 }
6404
6405 ctx = io_ring_ctx_alloc(p);
6406 if (!ctx) {
6407 if (account_mem)
6408 io_unaccount_mem(user, ring_pages(p->sq_entries,
6409 p->cq_entries));
6410 free_uid(user);
6411 return -ENOMEM;
6412 }
6413 ctx->compat = in_compat_syscall();
6414 ctx->account_mem = account_mem;
6415 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006416 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006417
6418 ret = io_allocate_scq_urings(ctx, p);
6419 if (ret)
6420 goto err;
6421
Jens Axboe6c271ce2019-01-10 11:22:30 -07006422 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006423 if (ret)
6424 goto err;
6425
Jens Axboe2b188cc2019-01-07 10:46:33 -07006426 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006427 p->sq_off.head = offsetof(struct io_rings, sq.head);
6428 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6429 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6430 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6431 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6432 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6433 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006434
6435 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006436 p->cq_off.head = offsetof(struct io_rings, cq.head);
6437 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6438 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6439 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6440 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6441 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006442
Jens Axboe044c1ab2019-10-28 09:15:33 -06006443 /*
6444 * Install ring fd as the very last thing, so we don't risk someone
6445 * having closed it before we finish setup
6446 */
6447 ret = io_uring_get_fd(ctx);
6448 if (ret < 0)
6449 goto err;
6450
Jens Axboeda8c9692019-12-02 18:51:26 -07006451 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboeba042912019-12-25 16:33:42 -07006452 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006453 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006454 return ret;
6455err:
6456 io_ring_ctx_wait_and_kill(ctx);
6457 return ret;
6458}
6459
6460/*
6461 * Sets up an aio uring context, and returns the fd. Applications asks for a
6462 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6463 * params structure passed in.
6464 */
6465static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6466{
6467 struct io_uring_params p;
6468 long ret;
6469 int i;
6470
6471 if (copy_from_user(&p, params, sizeof(p)))
6472 return -EFAULT;
6473 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6474 if (p.resv[i])
6475 return -EINVAL;
6476 }
6477
Jens Axboe6c271ce2019-01-10 11:22:30 -07006478 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006479 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
6480 IORING_SETUP_CLAMP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006481 return -EINVAL;
6482
6483 ret = io_uring_create(entries, &p);
6484 if (ret < 0)
6485 return ret;
6486
6487 if (copy_to_user(params, &p, sizeof(p)))
6488 return -EFAULT;
6489
6490 return ret;
6491}
6492
6493SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6494 struct io_uring_params __user *, params)
6495{
6496 return io_uring_setup(entries, params);
6497}
6498
Jens Axboeedafcce2019-01-09 09:16:05 -07006499static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6500 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006501 __releases(ctx->uring_lock)
6502 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006503{
6504 int ret;
6505
Jens Axboe35fa71a2019-04-22 10:23:23 -06006506 /*
6507 * We're inside the ring mutex, if the ref is already dying, then
6508 * someone else killed the ctx or is already going through
6509 * io_uring_register().
6510 */
6511 if (percpu_ref_is_dying(&ctx->refs))
6512 return -ENXIO;
6513
Jens Axboe05f3fb32019-12-09 11:22:50 -07006514 if (opcode != IORING_UNREGISTER_FILES &&
6515 opcode != IORING_REGISTER_FILES_UPDATE) {
6516 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006517
Jens Axboe05f3fb32019-12-09 11:22:50 -07006518 /*
6519 * Drop uring mutex before waiting for references to exit. If
6520 * another thread is currently inside io_uring_enter() it might
6521 * need to grab the uring_lock to make progress. If we hold it
6522 * here across the drain wait, then we can deadlock. It's safe
6523 * to drop the mutex here, since no new references will come in
6524 * after we've killed the percpu ref.
6525 */
6526 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006527 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006528 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006529 if (ret) {
6530 percpu_ref_resurrect(&ctx->refs);
6531 ret = -EINTR;
6532 goto out;
6533 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006534 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006535
6536 switch (opcode) {
6537 case IORING_REGISTER_BUFFERS:
6538 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6539 break;
6540 case IORING_UNREGISTER_BUFFERS:
6541 ret = -EINVAL;
6542 if (arg || nr_args)
6543 break;
6544 ret = io_sqe_buffer_unregister(ctx);
6545 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006546 case IORING_REGISTER_FILES:
6547 ret = io_sqe_files_register(ctx, arg, nr_args);
6548 break;
6549 case IORING_UNREGISTER_FILES:
6550 ret = -EINVAL;
6551 if (arg || nr_args)
6552 break;
6553 ret = io_sqe_files_unregister(ctx);
6554 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006555 case IORING_REGISTER_FILES_UPDATE:
6556 ret = io_sqe_files_update(ctx, arg, nr_args);
6557 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006558 case IORING_REGISTER_EVENTFD:
6559 ret = -EINVAL;
6560 if (nr_args != 1)
6561 break;
6562 ret = io_eventfd_register(ctx, arg);
6563 break;
6564 case IORING_UNREGISTER_EVENTFD:
6565 ret = -EINVAL;
6566 if (arg || nr_args)
6567 break;
6568 ret = io_eventfd_unregister(ctx);
6569 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006570 default:
6571 ret = -EINVAL;
6572 break;
6573 }
6574
Jens Axboe05f3fb32019-12-09 11:22:50 -07006575
6576 if (opcode != IORING_UNREGISTER_FILES &&
6577 opcode != IORING_REGISTER_FILES_UPDATE) {
6578 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006579 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07006580out:
6581 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006582 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006583 return ret;
6584}
6585
6586SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6587 void __user *, arg, unsigned int, nr_args)
6588{
6589 struct io_ring_ctx *ctx;
6590 long ret = -EBADF;
6591 struct fd f;
6592
6593 f = fdget(fd);
6594 if (!f.file)
6595 return -EBADF;
6596
6597 ret = -EOPNOTSUPP;
6598 if (f.file->f_op != &io_uring_fops)
6599 goto out_fput;
6600
6601 ctx = f.file->private_data;
6602
6603 mutex_lock(&ctx->uring_lock);
6604 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6605 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006606 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6607 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006608out_fput:
6609 fdput(f);
6610 return ret;
6611}
6612
Jens Axboe2b188cc2019-01-07 10:46:33 -07006613static int __init io_uring_init(void)
6614{
Jens Axboed3656342019-12-18 09:50:26 -07006615 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006616 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6617 return 0;
6618};
6619__initcall(io_uring_init);