blob: 17a199c99c7cca46be5f30246e83ba3a7c7f4c46 [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;
380 struct user_msghdr __user *msg;
381 int msg_flags;
382};
383
Jens Axboe15b71ab2019-12-11 11:20:36 -0700384struct io_open {
385 struct file *file;
386 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700387 union {
388 umode_t mode;
389 unsigned mask;
390 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700391 const char __user *fname;
392 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700393 struct statx __user *buffer;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700394 int flags;
395};
396
Jens Axboe05f3fb32019-12-09 11:22:50 -0700397struct io_files_update {
398 struct file *file;
399 u64 arg;
400 u32 nr_args;
401 u32 offset;
402};
403
Jens Axboe4840e412019-12-25 22:03:45 -0700404struct io_fadvise {
405 struct file *file;
406 u64 offset;
407 u32 len;
408 u32 advice;
409};
410
Jens Axboec1ca7572019-12-25 22:18:28 -0700411struct io_madvise {
412 struct file *file;
413 u64 addr;
414 u32 len;
415 u32 advice;
416};
417
Jens Axboef499a022019-12-02 16:28:46 -0700418struct io_async_connect {
419 struct sockaddr_storage address;
420};
421
Jens Axboe03b12302019-12-02 18:50:25 -0700422struct io_async_msghdr {
423 struct iovec fast_iov[UIO_FASTIOV];
424 struct iovec *iov;
425 struct sockaddr __user *uaddr;
426 struct msghdr msg;
427};
428
Jens Axboef67676d2019-12-02 11:03:47 -0700429struct io_async_rw {
430 struct iovec fast_iov[UIO_FASTIOV];
431 struct iovec *iov;
432 ssize_t nr_segs;
433 ssize_t size;
434};
435
Jens Axboe15b71ab2019-12-11 11:20:36 -0700436struct io_async_open {
437 struct filename *filename;
438};
439
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700440struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700441 union {
442 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700443 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700444 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700445 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700446 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700447 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700448};
449
Jens Axboe09bb8392019-03-13 12:39:28 -0600450/*
451 * NOTE! Each of the iocb union members has the file pointer
452 * as the first entry in their struct definition. So you can
453 * access the file pointer through any of the sub-structs,
454 * or directly as just 'ki_filp' in this struct.
455 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700456struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700457 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600458 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700459 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700460 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700461 struct io_accept accept;
462 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700463 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700464 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700465 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700466 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700467 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700468 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700469 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700470 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700471 struct io_madvise madvise;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700472 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700473
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700474 struct io_async_ctx *io;
Jens Axboee94f1412019-12-19 12:06:02 -0700475 union {
476 /*
477 * ring_file is only used in the submission path, and
478 * llist_node is only used for poll deferred completions
479 */
480 struct file *ring_file;
481 struct llist_node llist_node;
482 };
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300483 int ring_fd;
484 bool has_user;
485 bool in_async;
486 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700487 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700488
489 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700490 union {
491 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700492 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700493 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600494 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700495 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700496 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200497#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700498#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700499#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700500#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200501#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
502#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600503#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700504#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800505#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300506#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600507#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600508#define REQ_F_ISREG 2048 /* regular file */
509#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700510#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800511#define REQ_F_INFLIGHT 16384 /* on inflight list */
512#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700513#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboece35a472019-12-17 08:04:44 -0700514#define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */
Jens Axboeba042912019-12-25 16:33:42 -0700515#define REQ_F_CUR_POS 262144 /* read/write uses file position */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700516 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600517 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600518 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700519
Jens Axboefcb323c2019-10-24 12:39:47 -0600520 struct list_head inflight_entry;
521
Jens Axboe561fb042019-10-24 07:25:42 -0600522 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700523};
524
525#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700526#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700527
Jens Axboe9a56a232019-01-09 09:06:50 -0700528struct io_submit_state {
529 struct blk_plug plug;
530
531 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700532 * io_kiocb alloc cache
533 */
534 void *reqs[IO_IOPOLL_BATCH];
535 unsigned int free_reqs;
536 unsigned int cur_req;
537
538 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700539 * File reference cache
540 */
541 struct file *file;
542 unsigned int fd;
543 unsigned int has_refs;
544 unsigned int used_refs;
545 unsigned int ios_left;
546};
547
Jens Axboed3656342019-12-18 09:50:26 -0700548struct io_op_def {
549 /* needs req->io allocated for deferral/async */
550 unsigned async_ctx : 1;
551 /* needs current->mm setup, does mm access */
552 unsigned needs_mm : 1;
553 /* needs req->file assigned */
554 unsigned needs_file : 1;
555 /* needs req->file assigned IFF fd is >= 0 */
556 unsigned fd_non_neg : 1;
557 /* hash wq insertion if file is a regular file */
558 unsigned hash_reg_file : 1;
559 /* unbound wq insertion if file is a non-regular file */
560 unsigned unbound_nonreg_file : 1;
561};
562
563static const struct io_op_def io_op_defs[] = {
564 {
565 /* IORING_OP_NOP */
566 },
567 {
568 /* IORING_OP_READV */
569 .async_ctx = 1,
570 .needs_mm = 1,
571 .needs_file = 1,
572 .unbound_nonreg_file = 1,
573 },
574 {
575 /* IORING_OP_WRITEV */
576 .async_ctx = 1,
577 .needs_mm = 1,
578 .needs_file = 1,
579 .hash_reg_file = 1,
580 .unbound_nonreg_file = 1,
581 },
582 {
583 /* IORING_OP_FSYNC */
584 .needs_file = 1,
585 },
586 {
587 /* IORING_OP_READ_FIXED */
588 .needs_file = 1,
589 .unbound_nonreg_file = 1,
590 },
591 {
592 /* IORING_OP_WRITE_FIXED */
593 .needs_file = 1,
594 .hash_reg_file = 1,
595 .unbound_nonreg_file = 1,
596 },
597 {
598 /* IORING_OP_POLL_ADD */
599 .needs_file = 1,
600 .unbound_nonreg_file = 1,
601 },
602 {
603 /* IORING_OP_POLL_REMOVE */
604 },
605 {
606 /* IORING_OP_SYNC_FILE_RANGE */
607 .needs_file = 1,
608 },
609 {
610 /* IORING_OP_SENDMSG */
611 .async_ctx = 1,
612 .needs_mm = 1,
613 .needs_file = 1,
614 .unbound_nonreg_file = 1,
615 },
616 {
617 /* IORING_OP_RECVMSG */
618 .async_ctx = 1,
619 .needs_mm = 1,
620 .needs_file = 1,
621 .unbound_nonreg_file = 1,
622 },
623 {
624 /* IORING_OP_TIMEOUT */
625 .async_ctx = 1,
626 .needs_mm = 1,
627 },
628 {
629 /* IORING_OP_TIMEOUT_REMOVE */
630 },
631 {
632 /* IORING_OP_ACCEPT */
633 .needs_mm = 1,
634 .needs_file = 1,
635 .unbound_nonreg_file = 1,
636 },
637 {
638 /* IORING_OP_ASYNC_CANCEL */
639 },
640 {
641 /* IORING_OP_LINK_TIMEOUT */
642 .async_ctx = 1,
643 .needs_mm = 1,
644 },
645 {
646 /* IORING_OP_CONNECT */
647 .async_ctx = 1,
648 .needs_mm = 1,
649 .needs_file = 1,
650 .unbound_nonreg_file = 1,
651 },
652 {
653 /* IORING_OP_FALLOCATE */
654 .needs_file = 1,
655 },
656 {
657 /* IORING_OP_OPENAT */
658 .needs_file = 1,
659 .fd_non_neg = 1,
660 },
661 {
662 /* IORING_OP_CLOSE */
663 .needs_file = 1,
664 },
665 {
666 /* IORING_OP_FILES_UPDATE */
667 .needs_mm = 1,
668 },
669 {
670 /* IORING_OP_STATX */
671 .needs_mm = 1,
672 .needs_file = 1,
673 .fd_non_neg = 1,
674 },
Jens Axboe3a6820f2019-12-22 15:19:35 -0700675 {
676 /* IORING_OP_READ */
677 .needs_mm = 1,
678 .needs_file = 1,
679 .unbound_nonreg_file = 1,
680 },
681 {
682 /* IORING_OP_WRITE */
683 .needs_mm = 1,
684 .needs_file = 1,
685 .unbound_nonreg_file = 1,
686 },
Jens Axboe4840e412019-12-25 22:03:45 -0700687 {
688 /* IORING_OP_FADVISE */
689 .needs_file = 1,
690 },
Jens Axboec1ca7572019-12-25 22:18:28 -0700691 {
692 /* IORING_OP_MADVISE */
693 .needs_mm = 1,
694 },
Jens Axboed3656342019-12-18 09:50:26 -0700695};
696
Jens Axboe561fb042019-10-24 07:25:42 -0600697static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700698static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800699static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700700static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700701static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
702static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700703static int __io_sqe_files_update(struct io_ring_ctx *ctx,
704 struct io_uring_files_update *ip,
705 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600706
Jens Axboe2b188cc2019-01-07 10:46:33 -0700707static struct kmem_cache *req_cachep;
708
709static const struct file_operations io_uring_fops;
710
711struct sock *io_uring_get_socket(struct file *file)
712{
713#if defined(CONFIG_UNIX)
714 if (file->f_op == &io_uring_fops) {
715 struct io_ring_ctx *ctx = file->private_data;
716
717 return ctx->ring_sock->sk;
718 }
719#endif
720 return NULL;
721}
722EXPORT_SYMBOL(io_uring_get_socket);
723
724static void io_ring_ctx_ref_free(struct percpu_ref *ref)
725{
726 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
727
Jens Axboe206aefd2019-11-07 18:27:42 -0700728 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700729}
730
731static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
732{
733 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700734 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700735
736 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
737 if (!ctx)
738 return NULL;
739
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700740 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
741 if (!ctx->fallback_req)
742 goto err;
743
Jens Axboe206aefd2019-11-07 18:27:42 -0700744 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
745 if (!ctx->completions)
746 goto err;
747
Jens Axboe78076bb2019-12-04 19:56:40 -0700748 /*
749 * Use 5 bits less than the max cq entries, that should give us around
750 * 32 entries per hash list if totally full and uniformly spread.
751 */
752 hash_bits = ilog2(p->cq_entries);
753 hash_bits -= 5;
754 if (hash_bits <= 0)
755 hash_bits = 1;
756 ctx->cancel_hash_bits = hash_bits;
757 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
758 GFP_KERNEL);
759 if (!ctx->cancel_hash)
760 goto err;
761 __hash_init(ctx->cancel_hash, 1U << hash_bits);
762
Roman Gushchin21482892019-05-07 10:01:48 -0700763 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700764 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
765 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700766
767 ctx->flags = p->flags;
768 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700769 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700770 init_completion(&ctx->completions[0]);
771 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700772 mutex_init(&ctx->uring_lock);
773 init_waitqueue_head(&ctx->wait);
774 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700775 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700776 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600777 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600778 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600779 init_waitqueue_head(&ctx->inflight_wait);
780 spin_lock_init(&ctx->inflight_lock);
781 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700782 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700783err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700784 if (ctx->fallback_req)
785 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700786 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700787 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700788 kfree(ctx);
789 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700790}
791
Bob Liu9d858b22019-11-13 18:06:25 +0800792static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600793{
Jackie Liua197f662019-11-08 08:09:12 -0700794 struct io_ring_ctx *ctx = req->ctx;
795
Jens Axboe498ccd92019-10-25 10:04:25 -0600796 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
797 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600798}
799
Bob Liu9d858b22019-11-13 18:06:25 +0800800static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600801{
Bob Liu9d858b22019-11-13 18:06:25 +0800802 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
803 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600804
Bob Liu9d858b22019-11-13 18:06:25 +0800805 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600806}
807
808static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600809{
810 struct io_kiocb *req;
811
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600812 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800813 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600814 list_del_init(&req->list);
815 return req;
816 }
817
818 return NULL;
819}
820
Jens Axboe5262f562019-09-17 12:26:57 -0600821static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
822{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600823 struct io_kiocb *req;
824
825 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700826 if (req) {
827 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
828 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800829 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700830 list_del_init(&req->list);
831 return req;
832 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600833 }
834
835 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600836}
837
Jens Axboede0617e2019-04-06 21:51:27 -0600838static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700839{
Hristo Venev75b28af2019-08-26 17:23:46 +0000840 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700841
Hristo Venev75b28af2019-08-26 17:23:46 +0000842 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700843 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000844 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700845
Jens Axboe2b188cc2019-01-07 10:46:33 -0700846 if (wq_has_sleeper(&ctx->cq_wait)) {
847 wake_up_interruptible(&ctx->cq_wait);
848 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
849 }
850 }
851}
852
Jens Axboe94ae5e72019-11-14 19:39:52 -0700853static inline bool io_prep_async_work(struct io_kiocb *req,
854 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600855{
Jens Axboed3656342019-12-18 09:50:26 -0700856 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600857 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600858
Jens Axboed3656342019-12-18 09:50:26 -0700859 if (req->flags & REQ_F_ISREG) {
860 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700861 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700862 } else {
863 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700864 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600865 }
Jens Axboed3656342019-12-18 09:50:26 -0700866 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700867 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600868
Jens Axboe94ae5e72019-11-14 19:39:52 -0700869 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600870 return do_hashed;
871}
872
Jackie Liua197f662019-11-08 08:09:12 -0700873static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600874{
Jackie Liua197f662019-11-08 08:09:12 -0700875 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700876 struct io_kiocb *link;
877 bool do_hashed;
878
879 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600880
881 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
882 req->flags);
883 if (!do_hashed) {
884 io_wq_enqueue(ctx->io_wq, &req->work);
885 } else {
886 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
887 file_inode(req->file));
888 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700889
890 if (link)
891 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600892}
893
Jens Axboe5262f562019-09-17 12:26:57 -0600894static void io_kill_timeout(struct io_kiocb *req)
895{
896 int ret;
897
Jens Axboe2d283902019-12-04 11:08:05 -0700898 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600899 if (ret != -1) {
900 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600901 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700902 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800903 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600904 }
905}
906
907static void io_kill_timeouts(struct io_ring_ctx *ctx)
908{
909 struct io_kiocb *req, *tmp;
910
911 spin_lock_irq(&ctx->completion_lock);
912 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
913 io_kill_timeout(req);
914 spin_unlock_irq(&ctx->completion_lock);
915}
916
Jens Axboede0617e2019-04-06 21:51:27 -0600917static void io_commit_cqring(struct io_ring_ctx *ctx)
918{
919 struct io_kiocb *req;
920
Jens Axboe5262f562019-09-17 12:26:57 -0600921 while ((req = io_get_timeout_req(ctx)) != NULL)
922 io_kill_timeout(req);
923
Jens Axboede0617e2019-04-06 21:51:27 -0600924 __io_commit_cqring(ctx);
925
926 while ((req = io_get_deferred_req(ctx)) != NULL) {
927 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700928 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600929 }
930}
931
Jens Axboe2b188cc2019-01-07 10:46:33 -0700932static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
933{
Hristo Venev75b28af2019-08-26 17:23:46 +0000934 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700935 unsigned tail;
936
937 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200938 /*
939 * writes to the cq entry need to come after reading head; the
940 * control dependency is enough as we're using WRITE_ONCE to
941 * fill the cq entry
942 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000943 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700944 return NULL;
945
946 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000947 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700948}
949
Jens Axboe8c838782019-03-12 15:48:16 -0600950static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
951{
952 if (waitqueue_active(&ctx->wait))
953 wake_up(&ctx->wait);
954 if (waitqueue_active(&ctx->sqo_wait))
955 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600956 if (ctx->cq_ev_fd)
957 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600958}
959
Jens Axboec4a2ed72019-11-21 21:01:26 -0700960/* Returns true if there are no backlogged entries after the flush */
961static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700962{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700963 struct io_rings *rings = ctx->rings;
964 struct io_uring_cqe *cqe;
965 struct io_kiocb *req;
966 unsigned long flags;
967 LIST_HEAD(list);
968
969 if (!force) {
970 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700971 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700972 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
973 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700974 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700975 }
976
977 spin_lock_irqsave(&ctx->completion_lock, flags);
978
979 /* if force is set, the ring is going away. always drop after that */
980 if (force)
981 ctx->cq_overflow_flushed = true;
982
Jens Axboec4a2ed72019-11-21 21:01:26 -0700983 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700984 while (!list_empty(&ctx->cq_overflow_list)) {
985 cqe = io_get_cqring(ctx);
986 if (!cqe && !force)
987 break;
988
989 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
990 list);
991 list_move(&req->list, &list);
992 if (cqe) {
993 WRITE_ONCE(cqe->user_data, req->user_data);
994 WRITE_ONCE(cqe->res, req->result);
995 WRITE_ONCE(cqe->flags, 0);
996 } else {
997 WRITE_ONCE(ctx->rings->cq_overflow,
998 atomic_inc_return(&ctx->cached_cq_overflow));
999 }
1000 }
1001
1002 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001003 if (cqe) {
1004 clear_bit(0, &ctx->sq_check_overflow);
1005 clear_bit(0, &ctx->cq_check_overflow);
1006 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001007 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1008 io_cqring_ev_posted(ctx);
1009
1010 while (!list_empty(&list)) {
1011 req = list_first_entry(&list, struct io_kiocb, list);
1012 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001013 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001014 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001015
1016 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001017}
1018
Jens Axboe78e19bb2019-11-06 15:21:34 -07001019static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001020{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001021 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001022 struct io_uring_cqe *cqe;
1023
Jens Axboe78e19bb2019-11-06 15:21:34 -07001024 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001025
Jens Axboe2b188cc2019-01-07 10:46:33 -07001026 /*
1027 * If we can't get a cq entry, userspace overflowed the
1028 * submission (by quite a lot). Increment the overflow count in
1029 * the ring.
1030 */
1031 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001032 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001033 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001034 WRITE_ONCE(cqe->res, res);
1035 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001036 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001037 WRITE_ONCE(ctx->rings->cq_overflow,
1038 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001039 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001040 if (list_empty(&ctx->cq_overflow_list)) {
1041 set_bit(0, &ctx->sq_check_overflow);
1042 set_bit(0, &ctx->cq_check_overflow);
1043 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001044 refcount_inc(&req->refs);
1045 req->result = res;
1046 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047 }
1048}
1049
Jens Axboe78e19bb2019-11-06 15:21:34 -07001050static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001051{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001052 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001053 unsigned long flags;
1054
1055 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001056 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001057 io_commit_cqring(ctx);
1058 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1059
Jens Axboe8c838782019-03-12 15:48:16 -06001060 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001061}
1062
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001063static inline bool io_is_fallback_req(struct io_kiocb *req)
1064{
1065 return req == (struct io_kiocb *)
1066 ((unsigned long) req->ctx->fallback_req & ~1UL);
1067}
1068
1069static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1070{
1071 struct io_kiocb *req;
1072
1073 req = ctx->fallback_req;
1074 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1075 return req;
1076
1077 return NULL;
1078}
1079
Jens Axboe2579f912019-01-09 09:10:43 -07001080static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1081 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001082{
Jens Axboefd6fab22019-03-14 16:30:06 -06001083 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084 struct io_kiocb *req;
1085
1086 if (!percpu_ref_tryget(&ctx->refs))
1087 return NULL;
1088
Jens Axboe2579f912019-01-09 09:10:43 -07001089 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001090 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001091 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001092 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001093 } else if (!state->free_reqs) {
1094 size_t sz;
1095 int ret;
1096
1097 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001098 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1099
1100 /*
1101 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1102 * retry single alloc to be on the safe side.
1103 */
1104 if (unlikely(ret <= 0)) {
1105 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1106 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001107 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001108 ret = 1;
1109 }
Jens Axboe2579f912019-01-09 09:10:43 -07001110 state->free_reqs = ret - 1;
1111 state->cur_req = 1;
1112 req = state->reqs[0];
1113 } else {
1114 req = state->reqs[state->cur_req];
1115 state->free_reqs--;
1116 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001117 }
1118
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001119got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001120 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001121 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001122 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001123 req->ctx = ctx;
1124 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001125 /* one is dropped after submission, the other at completion */
1126 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001127 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001128 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001129 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001130fallback:
1131 req = io_get_fallback_req(ctx);
1132 if (req)
1133 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001134 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001135 return NULL;
1136}
1137
Jens Axboedef596e2019-01-09 08:59:42 -07001138static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
1139{
1140 if (*nr) {
1141 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +03001142 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001143 percpu_ref_put_many(&ctx->file_data->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -07001144 *nr = 0;
1145 }
1146}
1147
Jens Axboe9e645e112019-05-10 16:07:28 -06001148static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001149{
Jens Axboefcb323c2019-10-24 12:39:47 -06001150 struct io_ring_ctx *ctx = req->ctx;
1151
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001152 if (req->io)
1153 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001154 if (req->file) {
1155 if (req->flags & REQ_F_FIXED_FILE)
1156 percpu_ref_put(&ctx->file_data->refs);
1157 else
1158 fput(req->file);
1159 }
Jens Axboefcb323c2019-10-24 12:39:47 -06001160 if (req->flags & REQ_F_INFLIGHT) {
1161 unsigned long flags;
1162
1163 spin_lock_irqsave(&ctx->inflight_lock, flags);
1164 list_del(&req->inflight_entry);
1165 if (waitqueue_active(&ctx->inflight_wait))
1166 wake_up(&ctx->inflight_wait);
1167 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1168 }
1169 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001170 if (likely(!io_is_fallback_req(req)))
1171 kmem_cache_free(req_cachep, req);
1172 else
1173 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001174}
1175
Jackie Liua197f662019-11-08 08:09:12 -07001176static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001177{
Jackie Liua197f662019-11-08 08:09:12 -07001178 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001179 int ret;
1180
Jens Axboe2d283902019-12-04 11:08:05 -07001181 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001182 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001183 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001184 io_commit_cqring(ctx);
1185 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001186 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001187 return true;
1188 }
1189
1190 return false;
1191}
1192
Jens Axboeba816ad2019-09-28 11:36:45 -06001193static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001194{
Jens Axboe2665abf2019-11-05 12:40:47 -07001195 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001196 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001197
Jens Axboe4d7dd462019-11-20 13:03:52 -07001198 /* Already got next link */
1199 if (req->flags & REQ_F_LINK_NEXT)
1200 return;
1201
Jens Axboe9e645e112019-05-10 16:07:28 -06001202 /*
1203 * The list should never be empty when we are called here. But could
1204 * potentially happen if the chain is messed up, check to be on the
1205 * safe side.
1206 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001207 while (!list_empty(&req->link_list)) {
1208 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1209 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001210
Pavel Begunkov44932332019-12-05 16:16:35 +03001211 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1212 (nxt->flags & REQ_F_TIMEOUT))) {
1213 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001214 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001215 req->flags &= ~REQ_F_LINK_TIMEOUT;
1216 continue;
1217 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001218
Pavel Begunkov44932332019-12-05 16:16:35 +03001219 list_del_init(&req->link_list);
1220 if (!list_empty(&nxt->link_list))
1221 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001222 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001223 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001224 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001225
Jens Axboe4d7dd462019-11-20 13:03:52 -07001226 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001227 if (wake_ev)
1228 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001229}
1230
1231/*
1232 * Called if REQ_F_LINK is set, and we fail the head request
1233 */
1234static void io_fail_links(struct io_kiocb *req)
1235{
Jens Axboe2665abf2019-11-05 12:40:47 -07001236 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001237 unsigned long flags;
1238
1239 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001240
1241 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001242 struct io_kiocb *link = list_first_entry(&req->link_list,
1243 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001244
Pavel Begunkov44932332019-12-05 16:16:35 +03001245 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001246 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001247
1248 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001249 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001250 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001251 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001252 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001253 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001254 }
Jens Axboe5d960722019-11-19 15:31:28 -07001255 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001256 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001257
1258 io_commit_cqring(ctx);
1259 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1260 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001261}
1262
Jens Axboe4d7dd462019-11-20 13:03:52 -07001263static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001264{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001265 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001266 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001267
Jens Axboe9e645e112019-05-10 16:07:28 -06001268 /*
1269 * If LINK is set, we have dependent requests in this chain. If we
1270 * didn't fail this request, queue the first one up, moving any other
1271 * dependencies to the next request. In case of failure, fail the rest
1272 * of the chain.
1273 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001274 if (req->flags & REQ_F_FAIL_LINK) {
1275 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001276 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1277 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001278 struct io_ring_ctx *ctx = req->ctx;
1279 unsigned long flags;
1280
1281 /*
1282 * If this is a timeout link, we could be racing with the
1283 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001284 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001285 */
1286 spin_lock_irqsave(&ctx->completion_lock, flags);
1287 io_req_link_next(req, nxt);
1288 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1289 } else {
1290 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001291 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001292}
Jens Axboe9e645e112019-05-10 16:07:28 -06001293
Jackie Liuc69f8db2019-11-09 11:00:08 +08001294static void io_free_req(struct io_kiocb *req)
1295{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001296 struct io_kiocb *nxt = NULL;
1297
1298 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001299 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001300
1301 if (nxt)
1302 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001303}
1304
Jens Axboeba816ad2019-09-28 11:36:45 -06001305/*
1306 * Drop reference to request, return next in chain (if there is one) if this
1307 * was the last reference to this request.
1308 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001309__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001310static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001311{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001312 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001313
Jens Axboee65ef562019-03-12 10:16:44 -06001314 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001315 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001316}
1317
Jens Axboe2b188cc2019-01-07 10:46:33 -07001318static void io_put_req(struct io_kiocb *req)
1319{
Jens Axboedef596e2019-01-09 08:59:42 -07001320 if (refcount_dec_and_test(&req->refs))
1321 io_free_req(req);
1322}
1323
Jens Axboe978db572019-11-14 22:39:04 -07001324/*
1325 * Must only be used if we don't need to care about links, usually from
1326 * within the completion handling itself.
1327 */
1328static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001329{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001330 /* drop both submit and complete references */
1331 if (refcount_sub_and_test(2, &req->refs))
1332 __io_free_req(req);
1333}
1334
Jens Axboe978db572019-11-14 22:39:04 -07001335static void io_double_put_req(struct io_kiocb *req)
1336{
1337 /* drop both submit and complete references */
1338 if (refcount_sub_and_test(2, &req->refs))
1339 io_free_req(req);
1340}
1341
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001342static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001343{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001344 struct io_rings *rings = ctx->rings;
1345
Jens Axboead3eb2c2019-12-18 17:12:20 -07001346 if (test_bit(0, &ctx->cq_check_overflow)) {
1347 /*
1348 * noflush == true is from the waitqueue handler, just ensure
1349 * we wake up the task, and the next invocation will flush the
1350 * entries. We cannot safely to it from here.
1351 */
1352 if (noflush && !list_empty(&ctx->cq_overflow_list))
1353 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001354
Jens Axboead3eb2c2019-12-18 17:12:20 -07001355 io_cqring_overflow_flush(ctx, false);
1356 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001357
Jens Axboea3a0e432019-08-20 11:03:11 -06001358 /* See comment at the top of this file */
1359 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001360 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001361}
1362
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001363static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1364{
1365 struct io_rings *rings = ctx->rings;
1366
1367 /* make sure SQ entry isn't read before tail */
1368 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1369}
1370
Jens Axboee94f1412019-12-19 12:06:02 -07001371static inline bool io_req_multi_free(struct io_kiocb *req)
1372{
1373 /*
1374 * If we're not using fixed files, we have to pair the completion part
1375 * with the file put. Use regular completions for those, only batch
1376 * free for fixed file and non-linked commands.
1377 */
1378 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) == REQ_F_FIXED_FILE)
1379 && !io_is_fallback_req(req) && !req->io)
1380 return true;
1381
1382 return false;
1383}
1384
Jens Axboedef596e2019-01-09 08:59:42 -07001385/*
1386 * Find and free completed poll iocbs
1387 */
1388static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1389 struct list_head *done)
1390{
1391 void *reqs[IO_IOPOLL_BATCH];
1392 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001393 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001394
Jens Axboe09bb8392019-03-13 12:39:28 -06001395 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001396 while (!list_empty(done)) {
1397 req = list_first_entry(done, struct io_kiocb, list);
1398 list_del(&req->list);
1399
Jens Axboe78e19bb2019-11-06 15:21:34 -07001400 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001401 (*nr_events)++;
1402
Jens Axboe09bb8392019-03-13 12:39:28 -06001403 if (refcount_dec_and_test(&req->refs)) {
Jens Axboee94f1412019-12-19 12:06:02 -07001404 if (io_req_multi_free(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001405 reqs[to_free++] = req;
1406 if (to_free == ARRAY_SIZE(reqs))
1407 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001408 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001409 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001410 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001411 }
Jens Axboedef596e2019-01-09 08:59:42 -07001412 }
Jens Axboedef596e2019-01-09 08:59:42 -07001413
Jens Axboe09bb8392019-03-13 12:39:28 -06001414 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001415 io_free_req_many(ctx, reqs, &to_free);
1416}
1417
1418static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1419 long min)
1420{
1421 struct io_kiocb *req, *tmp;
1422 LIST_HEAD(done);
1423 bool spin;
1424 int ret;
1425
1426 /*
1427 * Only spin for completions if we don't have multiple devices hanging
1428 * off our complete list, and we're under the requested amount.
1429 */
1430 spin = !ctx->poll_multi_file && *nr_events < min;
1431
1432 ret = 0;
1433 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001434 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001435
1436 /*
1437 * Move completed entries to our local list. If we find a
1438 * request that requires polling, break out and complete
1439 * the done list first, if we have entries there.
1440 */
1441 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1442 list_move_tail(&req->list, &done);
1443 continue;
1444 }
1445 if (!list_empty(&done))
1446 break;
1447
1448 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1449 if (ret < 0)
1450 break;
1451
1452 if (ret && spin)
1453 spin = false;
1454 ret = 0;
1455 }
1456
1457 if (!list_empty(&done))
1458 io_iopoll_complete(ctx, nr_events, &done);
1459
1460 return ret;
1461}
1462
1463/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001464 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001465 * non-spinning poll check - we'll still enter the driver poll loop, but only
1466 * as a non-spinning completion check.
1467 */
1468static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1469 long min)
1470{
Jens Axboe08f54392019-08-21 22:19:11 -06001471 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001472 int ret;
1473
1474 ret = io_do_iopoll(ctx, nr_events, min);
1475 if (ret < 0)
1476 return ret;
1477 if (!min || *nr_events >= min)
1478 return 0;
1479 }
1480
1481 return 1;
1482}
1483
1484/*
1485 * We can't just wait for polled events to come to us, we have to actively
1486 * find and complete them.
1487 */
1488static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1489{
1490 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1491 return;
1492
1493 mutex_lock(&ctx->uring_lock);
1494 while (!list_empty(&ctx->poll_list)) {
1495 unsigned int nr_events = 0;
1496
1497 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001498
1499 /*
1500 * Ensure we allow local-to-the-cpu processing to take place,
1501 * in this case we need to ensure that we reap all events.
1502 */
1503 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001504 }
1505 mutex_unlock(&ctx->uring_lock);
1506}
1507
Jens Axboe2b2ed972019-10-25 10:06:15 -06001508static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1509 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001510{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001511 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001512
1513 do {
1514 int tmin = 0;
1515
Jens Axboe500f9fb2019-08-19 12:15:59 -06001516 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001517 * Don't enter poll loop if we already have events pending.
1518 * If we do, we can potentially be spinning for commands that
1519 * already triggered a CQE (eg in error).
1520 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001521 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001522 break;
1523
1524 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001525 * If a submit got punted to a workqueue, we can have the
1526 * application entering polling for a command before it gets
1527 * issued. That app will hold the uring_lock for the duration
1528 * of the poll right here, so we need to take a breather every
1529 * now and then to ensure that the issue has a chance to add
1530 * the poll to the issued list. Otherwise we can spin here
1531 * forever, while the workqueue is stuck trying to acquire the
1532 * very same mutex.
1533 */
1534 if (!(++iters & 7)) {
1535 mutex_unlock(&ctx->uring_lock);
1536 mutex_lock(&ctx->uring_lock);
1537 }
1538
Jens Axboedef596e2019-01-09 08:59:42 -07001539 if (*nr_events < min)
1540 tmin = min - *nr_events;
1541
1542 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1543 if (ret <= 0)
1544 break;
1545 ret = 0;
1546 } while (min && !*nr_events && !need_resched());
1547
Jens Axboe2b2ed972019-10-25 10:06:15 -06001548 return ret;
1549}
1550
1551static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1552 long min)
1553{
1554 int ret;
1555
1556 /*
1557 * We disallow the app entering submit/complete with polling, but we
1558 * still need to lock the ring to prevent racing with polled issue
1559 * that got punted to a workqueue.
1560 */
1561 mutex_lock(&ctx->uring_lock);
1562 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001563 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001564 return ret;
1565}
1566
Jens Axboe491381ce2019-10-17 09:20:46 -06001567static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001568{
Jens Axboe491381ce2019-10-17 09:20:46 -06001569 /*
1570 * Tell lockdep we inherited freeze protection from submission
1571 * thread.
1572 */
1573 if (req->flags & REQ_F_ISREG) {
1574 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001575
Jens Axboe491381ce2019-10-17 09:20:46 -06001576 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001577 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001578 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001579}
1580
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001581static inline void req_set_fail_links(struct io_kiocb *req)
1582{
1583 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1584 req->flags |= REQ_F_FAIL_LINK;
1585}
1586
Jens Axboeba816ad2019-09-28 11:36:45 -06001587static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001588{
Jens Axboe9adbd452019-12-20 08:45:55 -07001589 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001590
Jens Axboe491381ce2019-10-17 09:20:46 -06001591 if (kiocb->ki_flags & IOCB_WRITE)
1592 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001593
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001594 if (res != req->result)
1595 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001596 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001597}
1598
1599static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1600{
Jens Axboe9adbd452019-12-20 08:45:55 -07001601 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001602
1603 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001604 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001605}
1606
Jens Axboeba816ad2019-09-28 11:36:45 -06001607static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1608{
Jens Axboe9adbd452019-12-20 08:45:55 -07001609 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001610 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001611
1612 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001613 io_put_req_find_next(req, &nxt);
1614
1615 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001616}
1617
Jens Axboedef596e2019-01-09 08:59:42 -07001618static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1619{
Jens Axboe9adbd452019-12-20 08:45:55 -07001620 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001621
Jens Axboe491381ce2019-10-17 09:20:46 -06001622 if (kiocb->ki_flags & IOCB_WRITE)
1623 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001624
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001625 if (res != req->result)
1626 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001627 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001628 if (res != -EAGAIN)
1629 req->flags |= REQ_F_IOPOLL_COMPLETED;
1630}
1631
1632/*
1633 * After the iocb has been issued, it's safe to be found on the poll list.
1634 * Adding the kiocb to the list AFTER submission ensures that we don't
1635 * find it from a io_iopoll_getevents() thread before the issuer is done
1636 * accessing the kiocb cookie.
1637 */
1638static void io_iopoll_req_issued(struct io_kiocb *req)
1639{
1640 struct io_ring_ctx *ctx = req->ctx;
1641
1642 /*
1643 * Track whether we have multiple files in our lists. This will impact
1644 * how we do polling eventually, not spinning if we're on potentially
1645 * different devices.
1646 */
1647 if (list_empty(&ctx->poll_list)) {
1648 ctx->poll_multi_file = false;
1649 } else if (!ctx->poll_multi_file) {
1650 struct io_kiocb *list_req;
1651
1652 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1653 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001654 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001655 ctx->poll_multi_file = true;
1656 }
1657
1658 /*
1659 * For fast devices, IO may have already completed. If it has, add
1660 * it to the front so we find it first.
1661 */
1662 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1663 list_add(&req->list, &ctx->poll_list);
1664 else
1665 list_add_tail(&req->list, &ctx->poll_list);
1666}
1667
Jens Axboe3d6770f2019-04-13 11:50:54 -06001668static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001669{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001670 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001671 int diff = state->has_refs - state->used_refs;
1672
1673 if (diff)
1674 fput_many(state->file, diff);
1675 state->file = NULL;
1676 }
1677}
1678
1679/*
1680 * Get as many references to a file as we have IOs left in this submission,
1681 * assuming most submissions are for one file, or at least that each file
1682 * has more than one submission.
1683 */
1684static struct file *io_file_get(struct io_submit_state *state, int fd)
1685{
1686 if (!state)
1687 return fget(fd);
1688
1689 if (state->file) {
1690 if (state->fd == fd) {
1691 state->used_refs++;
1692 state->ios_left--;
1693 return state->file;
1694 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001695 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001696 }
1697 state->file = fget_many(fd, state->ios_left);
1698 if (!state->file)
1699 return NULL;
1700
1701 state->fd = fd;
1702 state->has_refs = state->ios_left;
1703 state->used_refs = 1;
1704 state->ios_left--;
1705 return state->file;
1706}
1707
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708/*
1709 * If we tracked the file through the SCM inflight mechanism, we could support
1710 * any file. For now, just ensure that anything potentially problematic is done
1711 * inline.
1712 */
1713static bool io_file_supports_async(struct file *file)
1714{
1715 umode_t mode = file_inode(file)->i_mode;
1716
Jens Axboe10d59342019-12-09 20:16:22 -07001717 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001718 return true;
1719 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1720 return true;
1721
1722 return false;
1723}
1724
Jens Axboe3529d8c2019-12-19 18:24:38 -07001725static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1726 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727{
Jens Axboedef596e2019-01-09 08:59:42 -07001728 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001729 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001730 unsigned ioprio;
1731 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732
Jens Axboe09bb8392019-03-13 12:39:28 -06001733 if (!req->file)
1734 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735
Jens Axboe491381ce2019-10-17 09:20:46 -06001736 if (S_ISREG(file_inode(req->file)->i_mode))
1737 req->flags |= REQ_F_ISREG;
1738
Jens Axboe2b188cc2019-01-07 10:46:33 -07001739 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001740 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1741 req->flags |= REQ_F_CUR_POS;
1742 kiocb->ki_pos = req->file->f_pos;
1743 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001744 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1745 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1746
1747 ioprio = READ_ONCE(sqe->ioprio);
1748 if (ioprio) {
1749 ret = ioprio_check_cap(ioprio);
1750 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001751 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001752
1753 kiocb->ki_ioprio = ioprio;
1754 } else
1755 kiocb->ki_ioprio = get_current_ioprio();
1756
1757 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1758 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001759 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001760
1761 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001762 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1763 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001764 req->flags |= REQ_F_NOWAIT;
1765
1766 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001767 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001768
Jens Axboedef596e2019-01-09 08:59:42 -07001769 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001770 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1771 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001772 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001773
Jens Axboedef596e2019-01-09 08:59:42 -07001774 kiocb->ki_flags |= IOCB_HIPRI;
1775 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001776 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001777 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001778 if (kiocb->ki_flags & IOCB_HIPRI)
1779 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001780 kiocb->ki_complete = io_complete_rw;
1781 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001782
Jens Axboe3529d8c2019-12-19 18:24:38 -07001783 req->rw.addr = READ_ONCE(sqe->addr);
1784 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001785 /* we own ->private, reuse it for the buffer index */
1786 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001787 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001788 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001789}
1790
1791static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1792{
1793 switch (ret) {
1794 case -EIOCBQUEUED:
1795 break;
1796 case -ERESTARTSYS:
1797 case -ERESTARTNOINTR:
1798 case -ERESTARTNOHAND:
1799 case -ERESTART_RESTARTBLOCK:
1800 /*
1801 * We can't just restart the syscall, since previously
1802 * submitted sqes may already be in progress. Just fail this
1803 * IO with EINTR.
1804 */
1805 ret = -EINTR;
1806 /* fall through */
1807 default:
1808 kiocb->ki_complete(kiocb, ret, 0);
1809 }
1810}
1811
Jens Axboeba816ad2019-09-28 11:36:45 -06001812static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1813 bool in_async)
1814{
Jens Axboeba042912019-12-25 16:33:42 -07001815 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1816
1817 if (req->flags & REQ_F_CUR_POS)
1818 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001819 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001820 *nxt = __io_complete_rw(kiocb, ret);
1821 else
1822 io_rw_done(kiocb, ret);
1823}
1824
Jens Axboe9adbd452019-12-20 08:45:55 -07001825static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001826 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001827{
Jens Axboe9adbd452019-12-20 08:45:55 -07001828 struct io_ring_ctx *ctx = req->ctx;
1829 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001830 struct io_mapped_ubuf *imu;
1831 unsigned index, buf_index;
1832 size_t offset;
1833 u64 buf_addr;
1834
1835 /* attempt to use fixed buffers without having provided iovecs */
1836 if (unlikely(!ctx->user_bufs))
1837 return -EFAULT;
1838
Jens Axboe9adbd452019-12-20 08:45:55 -07001839 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001840 if (unlikely(buf_index >= ctx->nr_user_bufs))
1841 return -EFAULT;
1842
1843 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1844 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001845 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001846
1847 /* overflow */
1848 if (buf_addr + len < buf_addr)
1849 return -EFAULT;
1850 /* not inside the mapped region */
1851 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1852 return -EFAULT;
1853
1854 /*
1855 * May not be a start of buffer, set size appropriately
1856 * and advance us to the beginning.
1857 */
1858 offset = buf_addr - imu->ubuf;
1859 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001860
1861 if (offset) {
1862 /*
1863 * Don't use iov_iter_advance() here, as it's really slow for
1864 * using the latter parts of a big fixed buffer - it iterates
1865 * over each segment manually. We can cheat a bit here, because
1866 * we know that:
1867 *
1868 * 1) it's a BVEC iter, we set it up
1869 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1870 * first and last bvec
1871 *
1872 * So just find our index, and adjust the iterator afterwards.
1873 * If the offset is within the first bvec (or the whole first
1874 * bvec, just use iov_iter_advance(). This makes it easier
1875 * since we can just skip the first segment, which may not
1876 * be PAGE_SIZE aligned.
1877 */
1878 const struct bio_vec *bvec = imu->bvec;
1879
1880 if (offset <= bvec->bv_len) {
1881 iov_iter_advance(iter, offset);
1882 } else {
1883 unsigned long seg_skip;
1884
1885 /* skip first vec */
1886 offset -= bvec->bv_len;
1887 seg_skip = 1 + (offset >> PAGE_SHIFT);
1888
1889 iter->bvec = bvec + seg_skip;
1890 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001891 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001892 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001893 }
1894 }
1895
Jens Axboe5e559562019-11-13 16:12:46 -07001896 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001897}
1898
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001899static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1900 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001901{
Jens Axboe9adbd452019-12-20 08:45:55 -07001902 void __user *buf = u64_to_user_ptr(req->rw.addr);
1903 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001904 u8 opcode;
1905
Jens Axboed625c6e2019-12-17 19:53:05 -07001906 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001907 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001908 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001909 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001910 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001911
Jens Axboe9adbd452019-12-20 08:45:55 -07001912 /* buffer index only valid with fixed read/write */
1913 if (req->rw.kiocb.private)
1914 return -EINVAL;
1915
Jens Axboe3a6820f2019-12-22 15:19:35 -07001916 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1917 ssize_t ret;
1918 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1919 *iovec = NULL;
1920 return ret;
1921 }
1922
Jens Axboef67676d2019-12-02 11:03:47 -07001923 if (req->io) {
1924 struct io_async_rw *iorw = &req->io->rw;
1925
1926 *iovec = iorw->iov;
1927 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1928 if (iorw->iov == iorw->fast_iov)
1929 *iovec = NULL;
1930 return iorw->size;
1931 }
1932
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001933 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001934 return -EFAULT;
1935
1936#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001937 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001938 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1939 iovec, iter);
1940#endif
1941
1942 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1943}
1944
Jens Axboe32960612019-09-23 11:05:34 -06001945/*
1946 * For files that don't have ->read_iter() and ->write_iter(), handle them
1947 * by looping over ->read() or ->write() manually.
1948 */
1949static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1950 struct iov_iter *iter)
1951{
1952 ssize_t ret = 0;
1953
1954 /*
1955 * Don't support polled IO through this interface, and we can't
1956 * support non-blocking either. For the latter, this just causes
1957 * the kiocb to be handled from an async context.
1958 */
1959 if (kiocb->ki_flags & IOCB_HIPRI)
1960 return -EOPNOTSUPP;
1961 if (kiocb->ki_flags & IOCB_NOWAIT)
1962 return -EAGAIN;
1963
1964 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001965 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001966 ssize_t nr;
1967
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001968 if (!iov_iter_is_bvec(iter)) {
1969 iovec = iov_iter_iovec(iter);
1970 } else {
1971 /* fixed buffers import bvec */
1972 iovec.iov_base = kmap(iter->bvec->bv_page)
1973 + iter->iov_offset;
1974 iovec.iov_len = min(iter->count,
1975 iter->bvec->bv_len - iter->iov_offset);
1976 }
1977
Jens Axboe32960612019-09-23 11:05:34 -06001978 if (rw == READ) {
1979 nr = file->f_op->read(file, iovec.iov_base,
1980 iovec.iov_len, &kiocb->ki_pos);
1981 } else {
1982 nr = file->f_op->write(file, iovec.iov_base,
1983 iovec.iov_len, &kiocb->ki_pos);
1984 }
1985
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001986 if (iov_iter_is_bvec(iter))
1987 kunmap(iter->bvec->bv_page);
1988
Jens Axboe32960612019-09-23 11:05:34 -06001989 if (nr < 0) {
1990 if (!ret)
1991 ret = nr;
1992 break;
1993 }
1994 ret += nr;
1995 if (nr != iovec.iov_len)
1996 break;
1997 iov_iter_advance(iter, nr);
1998 }
1999
2000 return ret;
2001}
2002
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002003static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002004 struct iovec *iovec, struct iovec *fast_iov,
2005 struct iov_iter *iter)
2006{
2007 req->io->rw.nr_segs = iter->nr_segs;
2008 req->io->rw.size = io_size;
2009 req->io->rw.iov = iovec;
2010 if (!req->io->rw.iov) {
2011 req->io->rw.iov = req->io->rw.fast_iov;
2012 memcpy(req->io->rw.iov, fast_iov,
2013 sizeof(struct iovec) * iter->nr_segs);
2014 }
2015}
2016
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002017static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002018{
Jens Axboed3656342019-12-18 09:50:26 -07002019 if (!io_op_defs[req->opcode].async_ctx)
2020 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002021 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002022 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002023}
2024
2025static void io_rw_async(struct io_wq_work **workptr)
2026{
2027 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2028 struct iovec *iov = NULL;
2029
2030 if (req->io->rw.iov != req->io->rw.fast_iov)
2031 iov = req->io->rw.iov;
2032 io_wq_submit_work(workptr);
2033 kfree(iov);
2034}
2035
2036static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2037 struct iovec *iovec, struct iovec *fast_iov,
2038 struct iov_iter *iter)
2039{
Jens Axboe74566df2020-01-13 19:23:24 -07002040 if (req->opcode == IORING_OP_READ_FIXED ||
2041 req->opcode == IORING_OP_WRITE_FIXED)
2042 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002043 if (!req->io && io_alloc_async_ctx(req))
2044 return -ENOMEM;
2045
2046 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2047 req->work.func = io_rw_async;
2048 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002049}
2050
Jens Axboe3529d8c2019-12-19 18:24:38 -07002051static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2052 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002053{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002054 struct io_async_ctx *io;
2055 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002056 ssize_t ret;
2057
Jens Axboe3529d8c2019-12-19 18:24:38 -07002058 ret = io_prep_rw(req, sqe, force_nonblock);
2059 if (ret)
2060 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002061
Jens Axboe3529d8c2019-12-19 18:24:38 -07002062 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2063 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002064
Jens Axboe3529d8c2019-12-19 18:24:38 -07002065 if (!req->io)
2066 return 0;
2067
2068 io = req->io;
2069 io->rw.iov = io->rw.fast_iov;
2070 req->io = NULL;
2071 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2072 req->io = io;
2073 if (ret < 0)
2074 return ret;
2075
2076 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2077 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002078}
2079
Pavel Begunkov267bc902019-11-07 01:41:08 +03002080static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002081 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002082{
2083 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002084 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002085 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002086 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002087 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002088
Jens Axboe3529d8c2019-12-19 18:24:38 -07002089 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002090 if (ret < 0)
2091 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002092
Jens Axboefd6c2e42019-12-18 12:19:41 -07002093 /* Ensure we clear previously set non-block flag */
2094 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002095 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002096
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002097 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002098 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002099 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002100 req->result = io_size;
2101
2102 /*
2103 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2104 * we know to async punt it even if it was opened O_NONBLOCK
2105 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002106 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002107 req->flags |= REQ_F_MUST_PUNT;
2108 goto copy_iov;
2109 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002110
Jens Axboe31b51512019-01-18 22:56:34 -07002111 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002112 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002113 if (!ret) {
2114 ssize_t ret2;
2115
Jens Axboe9adbd452019-12-20 08:45:55 -07002116 if (req->file->f_op->read_iter)
2117 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002118 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002119 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002120
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002121 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002122 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002123 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002124 } else {
2125copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002126 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002127 inline_vecs, &iter);
2128 if (ret)
2129 goto out_free;
2130 return -EAGAIN;
2131 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002132 }
Jens Axboef67676d2019-12-02 11:03:47 -07002133out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002134 if (!io_wq_current_is_worker())
2135 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002136 return ret;
2137}
2138
Jens Axboe3529d8c2019-12-19 18:24:38 -07002139static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2140 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002141{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002142 struct io_async_ctx *io;
2143 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002144 ssize_t ret;
2145
Jens Axboe3529d8c2019-12-19 18:24:38 -07002146 ret = io_prep_rw(req, sqe, force_nonblock);
2147 if (ret)
2148 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002149
Jens Axboe3529d8c2019-12-19 18:24:38 -07002150 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2151 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002152
Jens Axboe3529d8c2019-12-19 18:24:38 -07002153 if (!req->io)
2154 return 0;
2155
2156 io = req->io;
2157 io->rw.iov = io->rw.fast_iov;
2158 req->io = NULL;
2159 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2160 req->io = io;
2161 if (ret < 0)
2162 return ret;
2163
2164 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2165 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002166}
2167
Pavel Begunkov267bc902019-11-07 01:41:08 +03002168static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002169 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002170{
2171 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002172 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002173 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002174 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002175 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002176
Jens Axboe3529d8c2019-12-19 18:24:38 -07002177 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002178 if (ret < 0)
2179 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002180
Jens Axboefd6c2e42019-12-18 12:19:41 -07002181 /* Ensure we clear previously set non-block flag */
2182 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002183 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002184
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002185 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002186 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002187 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002188 req->result = io_size;
2189
2190 /*
2191 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2192 * we know to async punt it even if it was opened O_NONBLOCK
2193 */
2194 if (force_nonblock && !io_file_supports_async(req->file)) {
2195 req->flags |= REQ_F_MUST_PUNT;
2196 goto copy_iov;
2197 }
2198
Jens Axboe10d59342019-12-09 20:16:22 -07002199 /* file path doesn't support NOWAIT for non-direct_IO */
2200 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2201 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002202 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002203
Jens Axboe31b51512019-01-18 22:56:34 -07002204 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002205 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002206 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002207 ssize_t ret2;
2208
Jens Axboe2b188cc2019-01-07 10:46:33 -07002209 /*
2210 * Open-code file_start_write here to grab freeze protection,
2211 * which will be released by another thread in
2212 * io_complete_rw(). Fool lockdep by telling it the lock got
2213 * released so that it doesn't complain about the held lock when
2214 * we return to userspace.
2215 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002216 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002217 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002218 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002219 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002220 SB_FREEZE_WRITE);
2221 }
2222 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002223
Jens Axboe9adbd452019-12-20 08:45:55 -07002224 if (req->file->f_op->write_iter)
2225 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002226 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002227 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002228 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002229 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002230 } else {
2231copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002232 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002233 inline_vecs, &iter);
2234 if (ret)
2235 goto out_free;
2236 return -EAGAIN;
2237 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002238 }
Jens Axboe31b51512019-01-18 22:56:34 -07002239out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002240 if (!io_wq_current_is_worker())
2241 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002242 return ret;
2243}
2244
2245/*
2246 * IORING_OP_NOP just posts a completion event, nothing else.
2247 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002248static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002249{
2250 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002251
Jens Axboedef596e2019-01-09 08:59:42 -07002252 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2253 return -EINVAL;
2254
Jens Axboe78e19bb2019-11-06 15:21:34 -07002255 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002256 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002257 return 0;
2258}
2259
Jens Axboe3529d8c2019-12-19 18:24:38 -07002260static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002261{
Jens Axboe6b063142019-01-10 22:13:58 -07002262 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002263
Jens Axboe09bb8392019-03-13 12:39:28 -06002264 if (!req->file)
2265 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002266
Jens Axboe6b063142019-01-10 22:13:58 -07002267 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002268 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002269 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002270 return -EINVAL;
2271
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002272 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2273 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2274 return -EINVAL;
2275
2276 req->sync.off = READ_ONCE(sqe->off);
2277 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002278 return 0;
2279}
2280
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002281static bool io_req_cancelled(struct io_kiocb *req)
2282{
2283 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2284 req_set_fail_links(req);
2285 io_cqring_add_event(req, -ECANCELED);
2286 io_put_req(req);
2287 return true;
2288 }
2289
2290 return false;
2291}
2292
Jens Axboe78912932020-01-14 22:09:06 -07002293static void io_link_work_cb(struct io_wq_work **workptr)
2294{
2295 struct io_wq_work *work = *workptr;
2296 struct io_kiocb *link = work->data;
2297
2298 io_queue_linked_timeout(link);
2299 work->func = io_wq_submit_work;
2300}
2301
2302static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2303{
2304 struct io_kiocb *link;
2305
2306 io_prep_async_work(nxt, &link);
2307 *workptr = &nxt->work;
2308 if (link) {
2309 nxt->work.flags |= IO_WQ_WORK_CB;
2310 nxt->work.func = io_link_work_cb;
2311 nxt->work.data = link;
2312 }
2313}
2314
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002315static void io_fsync_finish(struct io_wq_work **workptr)
2316{
2317 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2318 loff_t end = req->sync.off + req->sync.len;
2319 struct io_kiocb *nxt = NULL;
2320 int ret;
2321
2322 if (io_req_cancelled(req))
2323 return;
2324
Jens Axboe9adbd452019-12-20 08:45:55 -07002325 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002326 end > 0 ? end : LLONG_MAX,
2327 req->sync.flags & IORING_FSYNC_DATASYNC);
2328 if (ret < 0)
2329 req_set_fail_links(req);
2330 io_cqring_add_event(req, ret);
2331 io_put_req_find_next(req, &nxt);
2332 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002333 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002334}
2335
Jens Axboefc4df992019-12-10 14:38:45 -07002336static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2337 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002338{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002339 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002340
2341 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002342 if (force_nonblock) {
2343 io_put_req(req);
2344 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002345 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002346 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002347
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002348 work = old_work = &req->work;
2349 io_fsync_finish(&work);
2350 if (work && work != old_work)
2351 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002352 return 0;
2353}
2354
Jens Axboed63d1b52019-12-10 10:38:56 -07002355static void io_fallocate_finish(struct io_wq_work **workptr)
2356{
2357 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2358 struct io_kiocb *nxt = NULL;
2359 int ret;
2360
2361 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2362 req->sync.len);
2363 if (ret < 0)
2364 req_set_fail_links(req);
2365 io_cqring_add_event(req, ret);
2366 io_put_req_find_next(req, &nxt);
2367 if (nxt)
2368 io_wq_assign_next(workptr, nxt);
2369}
2370
2371static int io_fallocate_prep(struct io_kiocb *req,
2372 const struct io_uring_sqe *sqe)
2373{
2374 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2375 return -EINVAL;
2376
2377 req->sync.off = READ_ONCE(sqe->off);
2378 req->sync.len = READ_ONCE(sqe->addr);
2379 req->sync.mode = READ_ONCE(sqe->len);
2380 return 0;
2381}
2382
2383static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2384 bool force_nonblock)
2385{
2386 struct io_wq_work *work, *old_work;
2387
2388 /* fallocate always requiring blocking context */
2389 if (force_nonblock) {
2390 io_put_req(req);
2391 req->work.func = io_fallocate_finish;
2392 return -EAGAIN;
2393 }
2394
2395 work = old_work = &req->work;
2396 io_fallocate_finish(&work);
2397 if (work && work != old_work)
2398 *nxt = container_of(work, struct io_kiocb, work);
2399
2400 return 0;
2401}
2402
Jens Axboe15b71ab2019-12-11 11:20:36 -07002403static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2404{
2405 int ret;
2406
2407 if (sqe->ioprio || sqe->buf_index)
2408 return -EINVAL;
2409
2410 req->open.dfd = READ_ONCE(sqe->fd);
2411 req->open.mode = READ_ONCE(sqe->len);
2412 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2413 req->open.flags = READ_ONCE(sqe->open_flags);
2414
2415 req->open.filename = getname(req->open.fname);
2416 if (IS_ERR(req->open.filename)) {
2417 ret = PTR_ERR(req->open.filename);
2418 req->open.filename = NULL;
2419 return ret;
2420 }
2421
2422 return 0;
2423}
2424
2425static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2426 bool force_nonblock)
2427{
2428 struct open_flags op;
2429 struct open_how how;
2430 struct file *file;
2431 int ret;
2432
2433 if (force_nonblock) {
2434 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2435 return -EAGAIN;
2436 }
2437
2438 how = build_open_how(req->open.flags, req->open.mode);
2439 ret = build_open_flags(&how, &op);
2440 if (ret)
2441 goto err;
2442
2443 ret = get_unused_fd_flags(how.flags);
2444 if (ret < 0)
2445 goto err;
2446
2447 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2448 if (IS_ERR(file)) {
2449 put_unused_fd(ret);
2450 ret = PTR_ERR(file);
2451 } else {
2452 fsnotify_open(file);
2453 fd_install(ret, file);
2454 }
2455err:
2456 putname(req->open.filename);
2457 if (ret < 0)
2458 req_set_fail_links(req);
2459 io_cqring_add_event(req, ret);
2460 io_put_req_find_next(req, nxt);
2461 return 0;
2462}
2463
Jens Axboec1ca7572019-12-25 22:18:28 -07002464static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2465{
2466#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2467 if (sqe->ioprio || sqe->buf_index || sqe->off)
2468 return -EINVAL;
2469
2470 req->madvise.addr = READ_ONCE(sqe->addr);
2471 req->madvise.len = READ_ONCE(sqe->len);
2472 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2473 return 0;
2474#else
2475 return -EOPNOTSUPP;
2476#endif
2477}
2478
2479static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2480 bool force_nonblock)
2481{
2482#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2483 struct io_madvise *ma = &req->madvise;
2484 int ret;
2485
2486 if (force_nonblock)
2487 return -EAGAIN;
2488
2489 ret = do_madvise(ma->addr, ma->len, ma->advice);
2490 if (ret < 0)
2491 req_set_fail_links(req);
2492 io_cqring_add_event(req, ret);
2493 io_put_req_find_next(req, nxt);
2494 return 0;
2495#else
2496 return -EOPNOTSUPP;
2497#endif
2498}
2499
Jens Axboe4840e412019-12-25 22:03:45 -07002500static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2501{
2502 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2503 return -EINVAL;
2504
2505 req->fadvise.offset = READ_ONCE(sqe->off);
2506 req->fadvise.len = READ_ONCE(sqe->len);
2507 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2508 return 0;
2509}
2510
2511static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2512 bool force_nonblock)
2513{
2514 struct io_fadvise *fa = &req->fadvise;
2515 int ret;
2516
2517 /* DONTNEED may block, others _should_ not */
2518 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2519 return -EAGAIN;
2520
2521 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2522 if (ret < 0)
2523 req_set_fail_links(req);
2524 io_cqring_add_event(req, ret);
2525 io_put_req_find_next(req, nxt);
2526 return 0;
2527}
2528
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002529static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2530{
2531 unsigned lookup_flags;
2532 int ret;
2533
2534 if (sqe->ioprio || sqe->buf_index)
2535 return -EINVAL;
2536
2537 req->open.dfd = READ_ONCE(sqe->fd);
2538 req->open.mask = READ_ONCE(sqe->len);
2539 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2540 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2541 req->open.flags = READ_ONCE(sqe->statx_flags);
2542
2543 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.flags))
2544 return -EINVAL;
2545
2546 req->open.filename = getname_flags(req->open.fname, lookup_flags, NULL);
2547 if (IS_ERR(req->open.filename)) {
2548 ret = PTR_ERR(req->open.filename);
2549 req->open.filename = NULL;
2550 return ret;
2551 }
2552
2553 return 0;
2554}
2555
2556static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2557 bool force_nonblock)
2558{
2559 struct io_open *ctx = &req->open;
2560 unsigned lookup_flags;
2561 struct path path;
2562 struct kstat stat;
2563 int ret;
2564
2565 if (force_nonblock)
2566 return -EAGAIN;
2567
2568 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->flags))
2569 return -EINVAL;
2570
2571retry:
2572 /* filename_lookup() drops it, keep a reference */
2573 ctx->filename->refcnt++;
2574
2575 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2576 NULL);
2577 if (ret)
2578 goto err;
2579
2580 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->flags);
2581 path_put(&path);
2582 if (retry_estale(ret, lookup_flags)) {
2583 lookup_flags |= LOOKUP_REVAL;
2584 goto retry;
2585 }
2586 if (!ret)
2587 ret = cp_statx(&stat, ctx->buffer);
2588err:
2589 putname(ctx->filename);
2590 if (ret < 0)
2591 req_set_fail_links(req);
2592 io_cqring_add_event(req, ret);
2593 io_put_req_find_next(req, nxt);
2594 return 0;
2595}
2596
Jens Axboeb5dba592019-12-11 14:02:38 -07002597static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2598{
2599 /*
2600 * If we queue this for async, it must not be cancellable. That would
2601 * leave the 'file' in an undeterminate state.
2602 */
2603 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2604
2605 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2606 sqe->rw_flags || sqe->buf_index)
2607 return -EINVAL;
2608 if (sqe->flags & IOSQE_FIXED_FILE)
2609 return -EINVAL;
2610
2611 req->close.fd = READ_ONCE(sqe->fd);
2612 if (req->file->f_op == &io_uring_fops ||
2613 req->close.fd == req->ring_fd)
2614 return -EBADF;
2615
2616 return 0;
2617}
2618
2619static void io_close_finish(struct io_wq_work **workptr)
2620{
2621 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2622 struct io_kiocb *nxt = NULL;
2623
2624 /* Invoked with files, we need to do the close */
2625 if (req->work.files) {
2626 int ret;
2627
2628 ret = filp_close(req->close.put_file, req->work.files);
2629 if (ret < 0) {
2630 req_set_fail_links(req);
2631 }
2632 io_cqring_add_event(req, ret);
2633 }
2634
2635 fput(req->close.put_file);
2636
2637 /* we bypassed the re-issue, drop the submission reference */
2638 io_put_req(req);
2639 io_put_req_find_next(req, &nxt);
2640 if (nxt)
2641 io_wq_assign_next(workptr, nxt);
2642}
2643
2644static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2645 bool force_nonblock)
2646{
2647 int ret;
2648
2649 req->close.put_file = NULL;
2650 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2651 if (ret < 0)
2652 return ret;
2653
2654 /* if the file has a flush method, be safe and punt to async */
2655 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2656 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2657 goto eagain;
2658 }
2659
2660 /*
2661 * No ->flush(), safely close from here and just punt the
2662 * fput() to async context.
2663 */
2664 ret = filp_close(req->close.put_file, current->files);
2665
2666 if (ret < 0)
2667 req_set_fail_links(req);
2668 io_cqring_add_event(req, ret);
2669
2670 if (io_wq_current_is_worker()) {
2671 struct io_wq_work *old_work, *work;
2672
2673 old_work = work = &req->work;
2674 io_close_finish(&work);
2675 if (work && work != old_work)
2676 *nxt = container_of(work, struct io_kiocb, work);
2677 return 0;
2678 }
2679
2680eagain:
2681 req->work.func = io_close_finish;
2682 return -EAGAIN;
2683}
2684
Jens Axboe3529d8c2019-12-19 18:24:38 -07002685static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002686{
2687 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002688
2689 if (!req->file)
2690 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002691
2692 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2693 return -EINVAL;
2694 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2695 return -EINVAL;
2696
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002697 req->sync.off = READ_ONCE(sqe->off);
2698 req->sync.len = READ_ONCE(sqe->len);
2699 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002700 return 0;
2701}
2702
2703static void io_sync_file_range_finish(struct io_wq_work **workptr)
2704{
2705 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2706 struct io_kiocb *nxt = NULL;
2707 int ret;
2708
2709 if (io_req_cancelled(req))
2710 return;
2711
Jens Axboe9adbd452019-12-20 08:45:55 -07002712 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002713 req->sync.flags);
2714 if (ret < 0)
2715 req_set_fail_links(req);
2716 io_cqring_add_event(req, ret);
2717 io_put_req_find_next(req, &nxt);
2718 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002719 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002720}
2721
Jens Axboefc4df992019-12-10 14:38:45 -07002722static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002723 bool force_nonblock)
2724{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002725 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002726
2727 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002728 if (force_nonblock) {
2729 io_put_req(req);
2730 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002731 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002732 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002733
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002734 work = old_work = &req->work;
2735 io_sync_file_range_finish(&work);
2736 if (work && work != old_work)
2737 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002738 return 0;
2739}
2740
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002741#if defined(CONFIG_NET)
2742static void io_sendrecv_async(struct io_wq_work **workptr)
2743{
2744 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2745 struct iovec *iov = NULL;
2746
2747 if (req->io->rw.iov != req->io->rw.fast_iov)
2748 iov = req->io->msg.iov;
2749 io_wq_submit_work(workptr);
2750 kfree(iov);
2751}
2752#endif
2753
Jens Axboe3529d8c2019-12-19 18:24:38 -07002754static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002755{
Jens Axboe03b12302019-12-02 18:50:25 -07002756#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002757 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002758 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002759
Jens Axboee47293f2019-12-20 08:58:21 -07002760 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2761 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe3529d8c2019-12-19 18:24:38 -07002762
2763 if (!io)
2764 return 0;
2765
Jens Axboed9688562019-12-09 19:35:20 -07002766 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002767 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002768 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002769#else
Jens Axboee47293f2019-12-20 08:58:21 -07002770 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002771#endif
2772}
2773
Jens Axboefc4df992019-12-10 14:38:45 -07002774static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2775 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002776{
2777#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002778 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002779 struct socket *sock;
2780 int ret;
2781
2782 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2783 return -EINVAL;
2784
2785 sock = sock_from_file(req->file, &ret);
2786 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002787 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002788 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002789 unsigned flags;
2790
Jens Axboe03b12302019-12-02 18:50:25 -07002791 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002792 kmsg = &req->io->msg;
2793 kmsg->msg.msg_name = &addr;
2794 /* if iov is set, it's allocated already */
2795 if (!kmsg->iov)
2796 kmsg->iov = kmsg->fast_iov;
2797 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002798 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002799 struct io_sr_msg *sr = &req->sr_msg;
2800
Jens Axboe0b416c32019-12-15 10:57:46 -07002801 kmsg = &io.msg;
2802 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002803
2804 io.msg.iov = io.msg.fast_iov;
2805 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2806 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002807 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002808 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002809 }
2810
Jens Axboee47293f2019-12-20 08:58:21 -07002811 flags = req->sr_msg.msg_flags;
2812 if (flags & MSG_DONTWAIT)
2813 req->flags |= REQ_F_NOWAIT;
2814 else if (force_nonblock)
2815 flags |= MSG_DONTWAIT;
2816
Jens Axboe0b416c32019-12-15 10:57:46 -07002817 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002818 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002819 if (req->io)
2820 return -EAGAIN;
2821 if (io_alloc_async_ctx(req))
2822 return -ENOMEM;
2823 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2824 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002825 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002826 }
2827 if (ret == -ERESTARTSYS)
2828 ret = -EINTR;
2829 }
2830
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002831 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002832 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002833 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002834 if (ret < 0)
2835 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002836 io_put_req_find_next(req, nxt);
2837 return 0;
2838#else
2839 return -EOPNOTSUPP;
2840#endif
2841}
2842
Jens Axboe3529d8c2019-12-19 18:24:38 -07002843static int io_recvmsg_prep(struct io_kiocb *req,
2844 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002845{
2846#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002847 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002848 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002849
Jens Axboe3529d8c2019-12-19 18:24:38 -07002850 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2851 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2852
2853 if (!io)
Jens Axboe06b76d42019-12-19 14:44:26 -07002854 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002855
Jens Axboed9688562019-12-09 19:35:20 -07002856 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002857 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002858 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002859#else
Jens Axboee47293f2019-12-20 08:58:21 -07002860 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002861#endif
2862}
2863
Jens Axboefc4df992019-12-10 14:38:45 -07002864static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2865 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002866{
2867#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002868 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002869 struct socket *sock;
2870 int ret;
2871
2872 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2873 return -EINVAL;
2874
2875 sock = sock_from_file(req->file, &ret);
2876 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002877 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002878 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002879 unsigned flags;
2880
Jens Axboe03b12302019-12-02 18:50:25 -07002881 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002882 kmsg = &req->io->msg;
2883 kmsg->msg.msg_name = &addr;
2884 /* if iov is set, it's allocated already */
2885 if (!kmsg->iov)
2886 kmsg->iov = kmsg->fast_iov;
2887 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002888 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002889 struct io_sr_msg *sr = &req->sr_msg;
2890
Jens Axboe0b416c32019-12-15 10:57:46 -07002891 kmsg = &io.msg;
2892 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002893
2894 io.msg.iov = io.msg.fast_iov;
2895 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
2896 sr->msg_flags, &io.msg.uaddr,
2897 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002898 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002899 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002900 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002901
Jens Axboee47293f2019-12-20 08:58:21 -07002902 flags = req->sr_msg.msg_flags;
2903 if (flags & MSG_DONTWAIT)
2904 req->flags |= REQ_F_NOWAIT;
2905 else if (force_nonblock)
2906 flags |= MSG_DONTWAIT;
2907
2908 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
2909 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002910 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002911 if (req->io)
2912 return -EAGAIN;
2913 if (io_alloc_async_ctx(req))
2914 return -ENOMEM;
2915 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2916 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002917 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002918 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002919 if (ret == -ERESTARTSYS)
2920 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002921 }
2922
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002923 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002924 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002925 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002926 if (ret < 0)
2927 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002928 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002929 return 0;
2930#else
2931 return -EOPNOTSUPP;
2932#endif
2933}
2934
Jens Axboe3529d8c2019-12-19 18:24:38 -07002935static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002936{
2937#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002938 struct io_accept *accept = &req->accept;
2939
Jens Axboe17f2fe32019-10-17 14:42:58 -06002940 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2941 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002942 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002943 return -EINVAL;
2944
Jens Axboed55e5f52019-12-11 16:12:15 -07002945 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2946 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002947 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002948 return 0;
2949#else
2950 return -EOPNOTSUPP;
2951#endif
2952}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002953
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002954#if defined(CONFIG_NET)
2955static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2956 bool force_nonblock)
2957{
2958 struct io_accept *accept = &req->accept;
2959 unsigned file_flags;
2960 int ret;
2961
2962 file_flags = force_nonblock ? O_NONBLOCK : 0;
2963 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2964 accept->addr_len, accept->flags);
2965 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002966 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002967 if (ret == -ERESTARTSYS)
2968 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002969 if (ret < 0)
2970 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002971 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002972 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002973 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002974}
2975
2976static void io_accept_finish(struct io_wq_work **workptr)
2977{
2978 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2979 struct io_kiocb *nxt = NULL;
2980
2981 if (io_req_cancelled(req))
2982 return;
2983 __io_accept(req, &nxt, false);
2984 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002985 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002986}
2987#endif
2988
2989static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2990 bool force_nonblock)
2991{
2992#if defined(CONFIG_NET)
2993 int ret;
2994
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002995 ret = __io_accept(req, nxt, force_nonblock);
2996 if (ret == -EAGAIN && force_nonblock) {
2997 req->work.func = io_accept_finish;
2998 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2999 io_put_req(req);
3000 return -EAGAIN;
3001 }
3002 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003003#else
3004 return -EOPNOTSUPP;
3005#endif
3006}
3007
Jens Axboe3529d8c2019-12-19 18:24:38 -07003008static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003009{
3010#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003011 struct io_connect *conn = &req->connect;
3012 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003013
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003014 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3015 return -EINVAL;
3016 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3017 return -EINVAL;
3018
Jens Axboe3529d8c2019-12-19 18:24:38 -07003019 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3020 conn->addr_len = READ_ONCE(sqe->addr2);
3021
3022 if (!io)
3023 return 0;
3024
3025 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003026 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003027#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003028 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003029#endif
3030}
3031
Jens Axboefc4df992019-12-10 14:38:45 -07003032static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3033 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003034{
3035#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003036 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003037 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003038 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003039
Jens Axboef499a022019-12-02 16:28:46 -07003040 if (req->io) {
3041 io = req->io;
3042 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003043 ret = move_addr_to_kernel(req->connect.addr,
3044 req->connect.addr_len,
3045 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003046 if (ret)
3047 goto out;
3048 io = &__io;
3049 }
3050
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003051 file_flags = force_nonblock ? O_NONBLOCK : 0;
3052
3053 ret = __sys_connect_file(req->file, &io->connect.address,
3054 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003055 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003056 if (req->io)
3057 return -EAGAIN;
3058 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003059 ret = -ENOMEM;
3060 goto out;
3061 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003062 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003063 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003064 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003065 if (ret == -ERESTARTSYS)
3066 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003067out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003068 if (ret < 0)
3069 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003070 io_cqring_add_event(req, ret);
3071 io_put_req_find_next(req, nxt);
3072 return 0;
3073#else
3074 return -EOPNOTSUPP;
3075#endif
3076}
3077
Jens Axboe221c5eb2019-01-17 09:41:58 -07003078static void io_poll_remove_one(struct io_kiocb *req)
3079{
3080 struct io_poll_iocb *poll = &req->poll;
3081
3082 spin_lock(&poll->head->lock);
3083 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003084 if (!list_empty(&poll->wait.entry)) {
3085 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003086 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003087 }
3088 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003089 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003090}
3091
3092static void io_poll_remove_all(struct io_ring_ctx *ctx)
3093{
Jens Axboe78076bb2019-12-04 19:56:40 -07003094 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003095 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003096 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003097
3098 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003099 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3100 struct hlist_head *list;
3101
3102 list = &ctx->cancel_hash[i];
3103 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3104 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003105 }
3106 spin_unlock_irq(&ctx->completion_lock);
3107}
3108
Jens Axboe47f46762019-11-09 17:43:02 -07003109static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3110{
Jens Axboe78076bb2019-12-04 19:56:40 -07003111 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003112 struct io_kiocb *req;
3113
Jens Axboe78076bb2019-12-04 19:56:40 -07003114 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3115 hlist_for_each_entry(req, list, hash_node) {
3116 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003117 io_poll_remove_one(req);
3118 return 0;
3119 }
Jens Axboe47f46762019-11-09 17:43:02 -07003120 }
3121
3122 return -ENOENT;
3123}
3124
Jens Axboe3529d8c2019-12-19 18:24:38 -07003125static int io_poll_remove_prep(struct io_kiocb *req,
3126 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003127{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003128 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3129 return -EINVAL;
3130 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3131 sqe->poll_events)
3132 return -EINVAL;
3133
Jens Axboe0969e782019-12-17 18:40:57 -07003134 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003135 return 0;
3136}
3137
3138/*
3139 * Find a running poll command that matches one specified in sqe->addr,
3140 * and remove it if found.
3141 */
3142static int io_poll_remove(struct io_kiocb *req)
3143{
3144 struct io_ring_ctx *ctx = req->ctx;
3145 u64 addr;
3146 int ret;
3147
Jens Axboe0969e782019-12-17 18:40:57 -07003148 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003149 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003150 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003151 spin_unlock_irq(&ctx->completion_lock);
3152
Jens Axboe78e19bb2019-11-06 15:21:34 -07003153 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003154 if (ret < 0)
3155 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003156 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003157 return 0;
3158}
3159
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003160static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003161{
Jackie Liua197f662019-11-08 08:09:12 -07003162 struct io_ring_ctx *ctx = req->ctx;
3163
Jens Axboe8c838782019-03-12 15:48:16 -06003164 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003165 if (error)
3166 io_cqring_fill_event(req, error);
3167 else
3168 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003169 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003170}
3171
Jens Axboe561fb042019-10-24 07:25:42 -06003172static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003173{
Jens Axboe561fb042019-10-24 07:25:42 -06003174 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003175 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3176 struct io_poll_iocb *poll = &req->poll;
3177 struct poll_table_struct pt = { ._key = poll->events };
3178 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003179 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003180 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003181 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003182
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003183 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003184 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003185 ret = -ECANCELED;
3186 } else if (READ_ONCE(poll->canceled)) {
3187 ret = -ECANCELED;
3188 }
Jens Axboe561fb042019-10-24 07:25:42 -06003189
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003190 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003191 mask = vfs_poll(poll->file, &pt) & poll->events;
3192
3193 /*
3194 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3195 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3196 * synchronize with them. In the cancellation case the list_del_init
3197 * itself is not actually needed, but harmless so we keep it in to
3198 * avoid further branches in the fast path.
3199 */
3200 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003201 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003202 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003203 spin_unlock_irq(&ctx->completion_lock);
3204 return;
3205 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003206 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003207 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003208 spin_unlock_irq(&ctx->completion_lock);
3209
Jens Axboe8c838782019-03-12 15:48:16 -06003210 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003211
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003212 if (ret < 0)
3213 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003214 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003215 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003216 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003217}
3218
Jens Axboee94f1412019-12-19 12:06:02 -07003219static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3220{
3221 void *reqs[IO_IOPOLL_BATCH];
3222 struct io_kiocb *req, *tmp;
3223 int to_free = 0;
3224
3225 spin_lock_irq(&ctx->completion_lock);
3226 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3227 hash_del(&req->hash_node);
3228 io_poll_complete(req, req->result, 0);
3229
3230 if (refcount_dec_and_test(&req->refs)) {
3231 if (io_req_multi_free(req)) {
3232 reqs[to_free++] = req;
3233 if (to_free == ARRAY_SIZE(reqs))
3234 io_free_req_many(ctx, reqs, &to_free);
3235 } else {
3236 req->flags |= REQ_F_COMP_LOCKED;
3237 io_free_req(req);
3238 }
3239 }
3240 }
3241 spin_unlock_irq(&ctx->completion_lock);
3242
3243 io_cqring_ev_posted(ctx);
3244 io_free_req_many(ctx, reqs, &to_free);
3245}
3246
3247static void io_poll_flush(struct io_wq_work **workptr)
3248{
3249 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3250 struct llist_node *nodes;
3251
3252 nodes = llist_del_all(&req->ctx->poll_llist);
3253 if (nodes)
3254 __io_poll_flush(req->ctx, nodes);
3255}
3256
Jens Axboe221c5eb2019-01-17 09:41:58 -07003257static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3258 void *key)
3259{
Jens Axboee9444752019-11-26 15:02:04 -07003260 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003261 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3262 struct io_ring_ctx *ctx = req->ctx;
3263 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003264
3265 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003266 if (mask && !(mask & poll->events))
3267 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003268
Jens Axboe392edb42019-12-09 17:52:20 -07003269 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003270
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003271 /*
3272 * Run completion inline if we can. We're using trylock here because
3273 * we are violating the completion_lock -> poll wq lock ordering.
3274 * If we have a link timeout we're going to need the completion_lock
3275 * for finalizing the request, mark us as having grabbed that already.
3276 */
Jens Axboee94f1412019-12-19 12:06:02 -07003277 if (mask) {
3278 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003279
Jens Axboee94f1412019-12-19 12:06:02 -07003280 if (llist_empty(&ctx->poll_llist) &&
3281 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3282 hash_del(&req->hash_node);
3283 io_poll_complete(req, mask, 0);
3284 req->flags |= REQ_F_COMP_LOCKED;
3285 io_put_req(req);
3286 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3287
3288 io_cqring_ev_posted(ctx);
3289 req = NULL;
3290 } else {
3291 req->result = mask;
3292 req->llist_node.next = NULL;
3293 /* if the list wasn't empty, we're done */
3294 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3295 req = NULL;
3296 else
3297 req->work.func = io_poll_flush;
3298 }
Jens Axboe8c838782019-03-12 15:48:16 -06003299 }
Jens Axboee94f1412019-12-19 12:06:02 -07003300 if (req)
3301 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003302
Jens Axboe221c5eb2019-01-17 09:41:58 -07003303 return 1;
3304}
3305
3306struct io_poll_table {
3307 struct poll_table_struct pt;
3308 struct io_kiocb *req;
3309 int error;
3310};
3311
3312static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3313 struct poll_table_struct *p)
3314{
3315 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3316
3317 if (unlikely(pt->req->poll.head)) {
3318 pt->error = -EINVAL;
3319 return;
3320 }
3321
3322 pt->error = 0;
3323 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003324 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003325}
3326
Jens Axboeeac406c2019-11-14 12:09:58 -07003327static void io_poll_req_insert(struct io_kiocb *req)
3328{
3329 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003330 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003331
Jens Axboe78076bb2019-12-04 19:56:40 -07003332 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3333 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003334}
3335
Jens Axboe3529d8c2019-12-19 18:24:38 -07003336static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003337{
3338 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003339 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003340
3341 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3342 return -EINVAL;
3343 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3344 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003345 if (!poll->file)
3346 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003347
Jens Axboe221c5eb2019-01-17 09:41:58 -07003348 events = READ_ONCE(sqe->poll_events);
3349 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003350 return 0;
3351}
3352
3353static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3354{
3355 struct io_poll_iocb *poll = &req->poll;
3356 struct io_ring_ctx *ctx = req->ctx;
3357 struct io_poll_table ipt;
3358 bool cancel = false;
3359 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003360
3361 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003362 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003363
Jens Axboe221c5eb2019-01-17 09:41:58 -07003364 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003365 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003366 poll->canceled = false;
3367
3368 ipt.pt._qproc = io_poll_queue_proc;
3369 ipt.pt._key = poll->events;
3370 ipt.req = req;
3371 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3372
3373 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003374 INIT_LIST_HEAD(&poll->wait.entry);
3375 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3376 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003377
Jens Axboe36703242019-07-25 10:20:18 -06003378 INIT_LIST_HEAD(&req->list);
3379
Jens Axboe221c5eb2019-01-17 09:41:58 -07003380 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003381
3382 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003383 if (likely(poll->head)) {
3384 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003385 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003386 if (ipt.error)
3387 cancel = true;
3388 ipt.error = 0;
3389 mask = 0;
3390 }
3391 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003392 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003393 else if (cancel)
3394 WRITE_ONCE(poll->canceled, true);
3395 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003396 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003397 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003398 }
Jens Axboe8c838782019-03-12 15:48:16 -06003399 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003400 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003401 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003402 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003403 spin_unlock_irq(&ctx->completion_lock);
3404
Jens Axboe8c838782019-03-12 15:48:16 -06003405 if (mask) {
3406 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003407 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003408 }
Jens Axboe8c838782019-03-12 15:48:16 -06003409 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003410}
3411
Jens Axboe5262f562019-09-17 12:26:57 -06003412static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3413{
Jens Axboead8a48a2019-11-15 08:49:11 -07003414 struct io_timeout_data *data = container_of(timer,
3415 struct io_timeout_data, timer);
3416 struct io_kiocb *req = data->req;
3417 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003418 unsigned long flags;
3419
Jens Axboe5262f562019-09-17 12:26:57 -06003420 atomic_inc(&ctx->cq_timeouts);
3421
3422 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003423 /*
Jens Axboe11365042019-10-16 09:08:32 -06003424 * We could be racing with timeout deletion. If the list is empty,
3425 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003426 */
Jens Axboe842f9612019-10-29 12:34:10 -06003427 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003428 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003429
Jens Axboe11365042019-10-16 09:08:32 -06003430 /*
3431 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003432 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003433 * pointer will be increased, otherwise other timeout reqs may
3434 * return in advance without waiting for enough wait_nr.
3435 */
3436 prev = req;
3437 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3438 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003439 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003440 }
Jens Axboe842f9612019-10-29 12:34:10 -06003441
Jens Axboe78e19bb2019-11-06 15:21:34 -07003442 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003443 io_commit_cqring(ctx);
3444 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3445
3446 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003447 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003448 io_put_req(req);
3449 return HRTIMER_NORESTART;
3450}
3451
Jens Axboe47f46762019-11-09 17:43:02 -07003452static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3453{
3454 struct io_kiocb *req;
3455 int ret = -ENOENT;
3456
3457 list_for_each_entry(req, &ctx->timeout_list, list) {
3458 if (user_data == req->user_data) {
3459 list_del_init(&req->list);
3460 ret = 0;
3461 break;
3462 }
3463 }
3464
3465 if (ret == -ENOENT)
3466 return ret;
3467
Jens Axboe2d283902019-12-04 11:08:05 -07003468 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003469 if (ret == -1)
3470 return -EALREADY;
3471
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003472 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003473 io_cqring_fill_event(req, -ECANCELED);
3474 io_put_req(req);
3475 return 0;
3476}
3477
Jens Axboe3529d8c2019-12-19 18:24:38 -07003478static int io_timeout_remove_prep(struct io_kiocb *req,
3479 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003480{
Jens Axboeb29472e2019-12-17 18:50:29 -07003481 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3482 return -EINVAL;
3483 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3484 return -EINVAL;
3485
3486 req->timeout.addr = READ_ONCE(sqe->addr);
3487 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3488 if (req->timeout.flags)
3489 return -EINVAL;
3490
Jens Axboeb29472e2019-12-17 18:50:29 -07003491 return 0;
3492}
3493
Jens Axboe11365042019-10-16 09:08:32 -06003494/*
3495 * Remove or update an existing timeout command
3496 */
Jens Axboefc4df992019-12-10 14:38:45 -07003497static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003498{
3499 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003500 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003501
Jens Axboe11365042019-10-16 09:08:32 -06003502 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003503 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003504
Jens Axboe47f46762019-11-09 17:43:02 -07003505 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003506 io_commit_cqring(ctx);
3507 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003508 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003509 if (ret < 0)
3510 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003511 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003512 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003513}
3514
Jens Axboe3529d8c2019-12-19 18:24:38 -07003515static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003516 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003517{
Jens Axboead8a48a2019-11-15 08:49:11 -07003518 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003519 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003520
Jens Axboead8a48a2019-11-15 08:49:11 -07003521 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003522 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003523 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003524 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003525 if (sqe->off && is_timeout_link)
3526 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003527 flags = READ_ONCE(sqe->timeout_flags);
3528 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003529 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003530
Jens Axboe26a61672019-12-20 09:02:01 -07003531 req->timeout.count = READ_ONCE(sqe->off);
3532
Jens Axboe3529d8c2019-12-19 18:24:38 -07003533 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003534 return -ENOMEM;
3535
3536 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003537 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003538 req->flags |= REQ_F_TIMEOUT;
3539
3540 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003541 return -EFAULT;
3542
Jens Axboe11365042019-10-16 09:08:32 -06003543 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003544 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003545 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003546 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003547
Jens Axboead8a48a2019-11-15 08:49:11 -07003548 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3549 return 0;
3550}
3551
Jens Axboefc4df992019-12-10 14:38:45 -07003552static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003553{
3554 unsigned count;
3555 struct io_ring_ctx *ctx = req->ctx;
3556 struct io_timeout_data *data;
3557 struct list_head *entry;
3558 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003559
Jens Axboe2d283902019-12-04 11:08:05 -07003560 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003561
Jens Axboe5262f562019-09-17 12:26:57 -06003562 /*
3563 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003564 * timeout event to be satisfied. If it isn't set, then this is
3565 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003566 */
Jens Axboe26a61672019-12-20 09:02:01 -07003567 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003568 if (!count) {
3569 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3570 spin_lock_irq(&ctx->completion_lock);
3571 entry = ctx->timeout_list.prev;
3572 goto add;
3573 }
Jens Axboe5262f562019-09-17 12:26:57 -06003574
3575 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003576 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003577
3578 /*
3579 * Insertion sort, ensuring the first entry in the list is always
3580 * the one we need first.
3581 */
Jens Axboe5262f562019-09-17 12:26:57 -06003582 spin_lock_irq(&ctx->completion_lock);
3583 list_for_each_prev(entry, &ctx->timeout_list) {
3584 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003585 unsigned nxt_sq_head;
3586 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003587 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003588
Jens Axboe93bd25b2019-11-11 23:34:31 -07003589 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3590 continue;
3591
yangerkun5da0fb12019-10-15 21:59:29 +08003592 /*
3593 * Since cached_sq_head + count - 1 can overflow, use type long
3594 * long to store it.
3595 */
3596 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003597 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3598 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003599
3600 /*
3601 * cached_sq_head may overflow, and it will never overflow twice
3602 * once there is some timeout req still be valid.
3603 */
3604 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003605 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003606
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003607 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003608 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003609
3610 /*
3611 * Sequence of reqs after the insert one and itself should
3612 * be adjusted because each timeout req consumes a slot.
3613 */
3614 span++;
3615 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003616 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003617 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003618add:
Jens Axboe5262f562019-09-17 12:26:57 -06003619 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003620 data->timer.function = io_timeout_fn;
3621 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003622 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003623 return 0;
3624}
3625
Jens Axboe62755e32019-10-28 21:49:21 -06003626static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003627{
Jens Axboe62755e32019-10-28 21:49:21 -06003628 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003629
Jens Axboe62755e32019-10-28 21:49:21 -06003630 return req->user_data == (unsigned long) data;
3631}
3632
Jens Axboee977d6d2019-11-05 12:39:45 -07003633static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003634{
Jens Axboe62755e32019-10-28 21:49:21 -06003635 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003636 int ret = 0;
3637
Jens Axboe62755e32019-10-28 21:49:21 -06003638 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3639 switch (cancel_ret) {
3640 case IO_WQ_CANCEL_OK:
3641 ret = 0;
3642 break;
3643 case IO_WQ_CANCEL_RUNNING:
3644 ret = -EALREADY;
3645 break;
3646 case IO_WQ_CANCEL_NOTFOUND:
3647 ret = -ENOENT;
3648 break;
3649 }
3650
Jens Axboee977d6d2019-11-05 12:39:45 -07003651 return ret;
3652}
3653
Jens Axboe47f46762019-11-09 17:43:02 -07003654static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3655 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003656 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003657{
3658 unsigned long flags;
3659 int ret;
3660
3661 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3662 if (ret != -ENOENT) {
3663 spin_lock_irqsave(&ctx->completion_lock, flags);
3664 goto done;
3665 }
3666
3667 spin_lock_irqsave(&ctx->completion_lock, flags);
3668 ret = io_timeout_cancel(ctx, sqe_addr);
3669 if (ret != -ENOENT)
3670 goto done;
3671 ret = io_poll_cancel(ctx, sqe_addr);
3672done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003673 if (!ret)
3674 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003675 io_cqring_fill_event(req, ret);
3676 io_commit_cqring(ctx);
3677 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3678 io_cqring_ev_posted(ctx);
3679
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003680 if (ret < 0)
3681 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003682 io_put_req_find_next(req, nxt);
3683}
3684
Jens Axboe3529d8c2019-12-19 18:24:38 -07003685static int io_async_cancel_prep(struct io_kiocb *req,
3686 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003687{
Jens Axboefbf23842019-12-17 18:45:56 -07003688 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003689 return -EINVAL;
3690 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3691 sqe->cancel_flags)
3692 return -EINVAL;
3693
Jens Axboefbf23842019-12-17 18:45:56 -07003694 req->cancel.addr = READ_ONCE(sqe->addr);
3695 return 0;
3696}
3697
3698static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3699{
3700 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003701
3702 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003703 return 0;
3704}
3705
Jens Axboe05f3fb32019-12-09 11:22:50 -07003706static int io_files_update_prep(struct io_kiocb *req,
3707 const struct io_uring_sqe *sqe)
3708{
3709 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3710 return -EINVAL;
3711
3712 req->files_update.offset = READ_ONCE(sqe->off);
3713 req->files_update.nr_args = READ_ONCE(sqe->len);
3714 if (!req->files_update.nr_args)
3715 return -EINVAL;
3716 req->files_update.arg = READ_ONCE(sqe->addr);
3717 return 0;
3718}
3719
3720static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3721{
3722 struct io_ring_ctx *ctx = req->ctx;
3723 struct io_uring_files_update up;
3724 int ret;
3725
3726 if (force_nonblock) {
3727 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3728 return -EAGAIN;
3729 }
3730
3731 up.offset = req->files_update.offset;
3732 up.fds = req->files_update.arg;
3733
3734 mutex_lock(&ctx->uring_lock);
3735 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3736 mutex_unlock(&ctx->uring_lock);
3737
3738 if (ret < 0)
3739 req_set_fail_links(req);
3740 io_cqring_add_event(req, ret);
3741 io_put_req(req);
3742 return 0;
3743}
3744
Jens Axboe3529d8c2019-12-19 18:24:38 -07003745static int io_req_defer_prep(struct io_kiocb *req,
3746 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003747{
Jens Axboee7815732019-12-17 19:45:06 -07003748 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003749
Jens Axboed625c6e2019-12-17 19:53:05 -07003750 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003751 case IORING_OP_NOP:
3752 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003753 case IORING_OP_READV:
3754 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003755 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003756 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003757 break;
3758 case IORING_OP_WRITEV:
3759 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003760 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003761 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003762 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003763 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003764 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003765 break;
3766 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003767 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003768 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003769 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003770 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003771 break;
3772 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003773 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003774 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003775 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003776 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003777 break;
3778 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003779 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003780 break;
Jens Axboef499a022019-12-02 16:28:46 -07003781 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003782 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003783 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003784 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003785 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003786 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003787 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003788 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003789 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003790 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003791 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003792 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003793 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003794 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003795 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003796 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003797 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003798 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003799 case IORING_OP_FALLOCATE:
3800 ret = io_fallocate_prep(req, sqe);
3801 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003802 case IORING_OP_OPENAT:
3803 ret = io_openat_prep(req, sqe);
3804 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003805 case IORING_OP_CLOSE:
3806 ret = io_close_prep(req, sqe);
3807 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003808 case IORING_OP_FILES_UPDATE:
3809 ret = io_files_update_prep(req, sqe);
3810 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003811 case IORING_OP_STATX:
3812 ret = io_statx_prep(req, sqe);
3813 break;
Jens Axboe4840e412019-12-25 22:03:45 -07003814 case IORING_OP_FADVISE:
3815 ret = io_fadvise_prep(req, sqe);
3816 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07003817 case IORING_OP_MADVISE:
3818 ret = io_madvise_prep(req, sqe);
3819 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003820 default:
Jens Axboee7815732019-12-17 19:45:06 -07003821 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3822 req->opcode);
3823 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003824 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003825 }
3826
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003827 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003828}
3829
Jens Axboe3529d8c2019-12-19 18:24:38 -07003830static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003831{
Jackie Liua197f662019-11-08 08:09:12 -07003832 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003833 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003834
Bob Liu9d858b22019-11-13 18:06:25 +08003835 /* Still need defer if there is pending req in defer list. */
3836 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003837 return 0;
3838
Jens Axboe3529d8c2019-12-19 18:24:38 -07003839 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003840 return -EAGAIN;
3841
Jens Axboe3529d8c2019-12-19 18:24:38 -07003842 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003843 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003844 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003845
Jens Axboede0617e2019-04-06 21:51:27 -06003846 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003847 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003848 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003849 return 0;
3850 }
3851
Jens Axboe915967f2019-11-21 09:01:20 -07003852 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003853 list_add_tail(&req->list, &ctx->defer_list);
3854 spin_unlock_irq(&ctx->completion_lock);
3855 return -EIOCBQUEUED;
3856}
3857
Jens Axboe3529d8c2019-12-19 18:24:38 -07003858static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3859 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003860{
Jackie Liua197f662019-11-08 08:09:12 -07003861 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07003862 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003863
Jens Axboed625c6e2019-12-17 19:53:05 -07003864 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003865 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003866 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003867 break;
3868 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003869 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003870 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003871 if (sqe) {
3872 ret = io_read_prep(req, sqe, force_nonblock);
3873 if (ret < 0)
3874 break;
3875 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003876 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003877 break;
3878 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07003879 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003880 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003881 if (sqe) {
3882 ret = io_write_prep(req, sqe, force_nonblock);
3883 if (ret < 0)
3884 break;
3885 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003886 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003887 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003888 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003889 if (sqe) {
3890 ret = io_prep_fsync(req, sqe);
3891 if (ret < 0)
3892 break;
3893 }
Jens Axboefc4df992019-12-10 14:38:45 -07003894 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003895 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003896 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003897 if (sqe) {
3898 ret = io_poll_add_prep(req, sqe);
3899 if (ret)
3900 break;
3901 }
Jens Axboefc4df992019-12-10 14:38:45 -07003902 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003903 break;
3904 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003905 if (sqe) {
3906 ret = io_poll_remove_prep(req, sqe);
3907 if (ret < 0)
3908 break;
3909 }
Jens Axboefc4df992019-12-10 14:38:45 -07003910 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003911 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003912 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003913 if (sqe) {
3914 ret = io_prep_sfr(req, sqe);
3915 if (ret < 0)
3916 break;
3917 }
Jens Axboefc4df992019-12-10 14:38:45 -07003918 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003919 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003920 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003921 if (sqe) {
3922 ret = io_sendmsg_prep(req, sqe);
3923 if (ret < 0)
3924 break;
3925 }
Jens Axboefc4df992019-12-10 14:38:45 -07003926 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003927 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003928 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003929 if (sqe) {
3930 ret = io_recvmsg_prep(req, sqe);
3931 if (ret)
3932 break;
3933 }
Jens Axboefc4df992019-12-10 14:38:45 -07003934 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003935 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003936 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003937 if (sqe) {
3938 ret = io_timeout_prep(req, sqe, false);
3939 if (ret)
3940 break;
3941 }
Jens Axboefc4df992019-12-10 14:38:45 -07003942 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003943 break;
Jens Axboe11365042019-10-16 09:08:32 -06003944 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003945 if (sqe) {
3946 ret = io_timeout_remove_prep(req, sqe);
3947 if (ret)
3948 break;
3949 }
Jens Axboefc4df992019-12-10 14:38:45 -07003950 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003951 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003952 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003953 if (sqe) {
3954 ret = io_accept_prep(req, sqe);
3955 if (ret)
3956 break;
3957 }
Jens Axboefc4df992019-12-10 14:38:45 -07003958 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003959 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003960 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003961 if (sqe) {
3962 ret = io_connect_prep(req, sqe);
3963 if (ret)
3964 break;
3965 }
Jens Axboefc4df992019-12-10 14:38:45 -07003966 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003967 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003968 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003969 if (sqe) {
3970 ret = io_async_cancel_prep(req, sqe);
3971 if (ret)
3972 break;
3973 }
Jens Axboefc4df992019-12-10 14:38:45 -07003974 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003975 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003976 case IORING_OP_FALLOCATE:
3977 if (sqe) {
3978 ret = io_fallocate_prep(req, sqe);
3979 if (ret)
3980 break;
3981 }
3982 ret = io_fallocate(req, nxt, force_nonblock);
3983 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003984 case IORING_OP_OPENAT:
3985 if (sqe) {
3986 ret = io_openat_prep(req, sqe);
3987 if (ret)
3988 break;
3989 }
3990 ret = io_openat(req, nxt, force_nonblock);
3991 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003992 case IORING_OP_CLOSE:
3993 if (sqe) {
3994 ret = io_close_prep(req, sqe);
3995 if (ret)
3996 break;
3997 }
3998 ret = io_close(req, nxt, force_nonblock);
3999 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004000 case IORING_OP_FILES_UPDATE:
4001 if (sqe) {
4002 ret = io_files_update_prep(req, sqe);
4003 if (ret)
4004 break;
4005 }
4006 ret = io_files_update(req, force_nonblock);
4007 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004008 case IORING_OP_STATX:
4009 if (sqe) {
4010 ret = io_statx_prep(req, sqe);
4011 if (ret)
4012 break;
4013 }
4014 ret = io_statx(req, nxt, force_nonblock);
4015 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004016 case IORING_OP_FADVISE:
4017 if (sqe) {
4018 ret = io_fadvise_prep(req, sqe);
4019 if (ret)
4020 break;
4021 }
4022 ret = io_fadvise(req, nxt, force_nonblock);
4023 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004024 case IORING_OP_MADVISE:
4025 if (sqe) {
4026 ret = io_madvise_prep(req, sqe);
4027 if (ret)
4028 break;
4029 }
4030 ret = io_madvise(req, nxt, force_nonblock);
4031 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004032 default:
4033 ret = -EINVAL;
4034 break;
4035 }
4036
Jens Axboedef596e2019-01-09 08:59:42 -07004037 if (ret)
4038 return ret;
4039
4040 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004041 const bool in_async = io_wq_current_is_worker();
4042
Jens Axboe9e645e112019-05-10 16:07:28 -06004043 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004044 return -EAGAIN;
4045
Jens Axboe11ba8202020-01-15 21:51:17 -07004046 /* workqueue context doesn't hold uring_lock, grab it now */
4047 if (in_async)
4048 mutex_lock(&ctx->uring_lock);
4049
Jens Axboedef596e2019-01-09 08:59:42 -07004050 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004051
4052 if (in_async)
4053 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004054 }
4055
4056 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004057}
4058
Jens Axboe561fb042019-10-24 07:25:42 -06004059static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004060{
Jens Axboe561fb042019-10-24 07:25:42 -06004061 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004062 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004063 struct io_kiocb *nxt = NULL;
4064 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004065
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004066 /* if NO_CANCEL is set, we must still run the work */
4067 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4068 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004069 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004070 }
Jens Axboe31b51512019-01-18 22:56:34 -07004071
Jens Axboe561fb042019-10-24 07:25:42 -06004072 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004073 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4074 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004075 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004076 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004077 /*
4078 * We can get EAGAIN for polled IO even though we're
4079 * forcing a sync submission from here, since we can't
4080 * wait for request slots on the block side.
4081 */
4082 if (ret != -EAGAIN)
4083 break;
4084 cond_resched();
4085 } while (1);
4086 }
Jens Axboe31b51512019-01-18 22:56:34 -07004087
Jens Axboe561fb042019-10-24 07:25:42 -06004088 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004089 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004090
Jens Axboe561fb042019-10-24 07:25:42 -06004091 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004092 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004093 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004094 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004095 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004096
Jens Axboe561fb042019-10-24 07:25:42 -06004097 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004098 if (!ret && nxt)
4099 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004100}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004101
Jens Axboe15b71ab2019-12-11 11:20:36 -07004102static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004103{
Jens Axboed3656342019-12-18 09:50:26 -07004104 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004105 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004106 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4107 return 0;
4108 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004109}
4110
Jens Axboe65e19f52019-10-26 07:20:21 -06004111static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4112 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004113{
Jens Axboe65e19f52019-10-26 07:20:21 -06004114 struct fixed_file_table *table;
4115
Jens Axboe05f3fb32019-12-09 11:22:50 -07004116 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4117 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004118}
4119
Jens Axboe3529d8c2019-12-19 18:24:38 -07004120static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4121 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004122{
Jackie Liua197f662019-11-08 08:09:12 -07004123 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004124 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004125 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004126
Jens Axboe3529d8c2019-12-19 18:24:38 -07004127 flags = READ_ONCE(sqe->flags);
4128 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004129
Jackie Liu4fe2c962019-09-09 20:50:40 +08004130 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06004131 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06004132
Jens Axboed3656342019-12-18 09:50:26 -07004133 if (!io_req_needs_file(req, fd))
4134 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004135
4136 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004137 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004138 (unsigned) fd >= ctx->nr_user_files))
4139 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004140 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004141 req->file = io_file_from_index(ctx, fd);
4142 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004143 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004144 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004145 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004146 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004147 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004148 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004149 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004150 req->file = io_file_get(state, fd);
4151 if (unlikely(!req->file))
4152 return -EBADF;
4153 }
4154
4155 return 0;
4156}
4157
Jackie Liua197f662019-11-08 08:09:12 -07004158static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004159{
Jens Axboefcb323c2019-10-24 12:39:47 -06004160 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004161 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004162
Jens Axboeb5dba592019-12-11 14:02:38 -07004163 if (!req->ring_file)
4164 return -EBADF;
4165
Jens Axboefcb323c2019-10-24 12:39:47 -06004166 rcu_read_lock();
4167 spin_lock_irq(&ctx->inflight_lock);
4168 /*
4169 * We use the f_ops->flush() handler to ensure that we can flush
4170 * out work accessing these files if the fd is closed. Check if
4171 * the fd has changed since we started down this path, and disallow
4172 * this operation if it has.
4173 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004174 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004175 list_add(&req->inflight_entry, &ctx->inflight_list);
4176 req->flags |= REQ_F_INFLIGHT;
4177 req->work.files = current->files;
4178 ret = 0;
4179 }
4180 spin_unlock_irq(&ctx->inflight_lock);
4181 rcu_read_unlock();
4182
4183 return ret;
4184}
4185
Jens Axboe2665abf2019-11-05 12:40:47 -07004186static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4187{
Jens Axboead8a48a2019-11-15 08:49:11 -07004188 struct io_timeout_data *data = container_of(timer,
4189 struct io_timeout_data, timer);
4190 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004191 struct io_ring_ctx *ctx = req->ctx;
4192 struct io_kiocb *prev = NULL;
4193 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004194
4195 spin_lock_irqsave(&ctx->completion_lock, flags);
4196
4197 /*
4198 * We don't expect the list to be empty, that will only happen if we
4199 * race with the completion of the linked work.
4200 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004201 if (!list_empty(&req->link_list)) {
4202 prev = list_entry(req->link_list.prev, struct io_kiocb,
4203 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004204 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004205 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004206 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4207 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004208 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004209 }
4210
4211 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4212
4213 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004214 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004215 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4216 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004217 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004218 } else {
4219 io_cqring_add_event(req, -ETIME);
4220 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004221 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004222 return HRTIMER_NORESTART;
4223}
4224
Jens Axboead8a48a2019-11-15 08:49:11 -07004225static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004226{
Jens Axboe76a46e02019-11-10 23:34:16 -07004227 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004228
Jens Axboe76a46e02019-11-10 23:34:16 -07004229 /*
4230 * If the list is now empty, then our linked request finished before
4231 * we got a chance to setup the timer
4232 */
4233 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004234 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004235 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004236
Jens Axboead8a48a2019-11-15 08:49:11 -07004237 data->timer.function = io_link_timeout_fn;
4238 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4239 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004240 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004241 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004242
Jens Axboe2665abf2019-11-05 12:40:47 -07004243 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004244 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004245}
4246
Jens Axboead8a48a2019-11-15 08:49:11 -07004247static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004248{
4249 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004250
Jens Axboe2665abf2019-11-05 12:40:47 -07004251 if (!(req->flags & REQ_F_LINK))
4252 return NULL;
4253
Pavel Begunkov44932332019-12-05 16:16:35 +03004254 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4255 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004256 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004257 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004258
Jens Axboe76a46e02019-11-10 23:34:16 -07004259 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004260 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004261}
4262
Jens Axboe3529d8c2019-12-19 18:24:38 -07004263static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004264{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004265 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004266 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004267 int ret;
4268
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004269again:
4270 linked_timeout = io_prep_linked_timeout(req);
4271
Jens Axboe3529d8c2019-12-19 18:24:38 -07004272 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004273
4274 /*
4275 * We async punt it if the file wasn't marked NOWAIT, or if the file
4276 * doesn't support non-blocking read/write attempts
4277 */
4278 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4279 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004280 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4281 ret = io_grab_files(req);
4282 if (ret)
4283 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004284 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004285
4286 /*
4287 * Queued up for async execution, worker will release
4288 * submit reference when the iocb is actually submitted.
4289 */
4290 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004291 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004292 }
Jens Axboee65ef562019-03-12 10:16:44 -06004293
Jens Axboefcb323c2019-10-24 12:39:47 -06004294err:
Jens Axboee65ef562019-03-12 10:16:44 -06004295 /* drop submission reference */
4296 io_put_req(req);
4297
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004298 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004299 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004300 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004301 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004302 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004303 }
4304
Jens Axboee65ef562019-03-12 10:16:44 -06004305 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004306 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004307 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004308 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004309 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004310 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004311done_req:
4312 if (nxt) {
4313 req = nxt;
4314 nxt = NULL;
4315 goto again;
4316 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004317}
4318
Jens Axboe3529d8c2019-12-19 18:24:38 -07004319static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004320{
4321 int ret;
4322
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004323 if (unlikely(req->ctx->drain_next)) {
4324 req->flags |= REQ_F_IO_DRAIN;
4325 req->ctx->drain_next = false;
4326 }
4327 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
4328
Jens Axboe3529d8c2019-12-19 18:24:38 -07004329 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004330 if (ret) {
4331 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004332 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004333 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004334 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004335 }
Jens Axboece35a472019-12-17 08:04:44 -07004336 } else if ((req->flags & REQ_F_FORCE_ASYNC) &&
4337 !io_wq_current_is_worker()) {
4338 /*
4339 * Never try inline submit of IOSQE_ASYNC is set, go straight
4340 * to async execution.
4341 */
4342 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4343 io_queue_async_work(req);
4344 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004345 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004346 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004347}
4348
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004349static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004350{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004351 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004352 io_cqring_add_event(req, -ECANCELED);
4353 io_double_put_req(req);
4354 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004355 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004356}
4357
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004358#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004359 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004360
Jens Axboe3529d8c2019-12-19 18:24:38 -07004361static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4362 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004363{
Jackie Liua197f662019-11-08 08:09:12 -07004364 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004365 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004366 int ret;
4367
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004368 sqe_flags = READ_ONCE(sqe->flags);
4369
Jens Axboe9e645e112019-05-10 16:07:28 -06004370 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004371 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004372 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004373 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004374 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004375 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004376 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004377
Jens Axboe3529d8c2019-12-19 18:24:38 -07004378 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004379 if (unlikely(ret)) {
4380err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004381 io_cqring_add_event(req, ret);
4382 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004383 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004384 }
4385
Jens Axboe9e645e112019-05-10 16:07:28 -06004386 /*
4387 * If we already have a head request, queue this one for async
4388 * submittal once the head completes. If we don't have a head but
4389 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4390 * submitted sync once the chain is complete. If none of those
4391 * conditions are true (normal request), then just queue it.
4392 */
4393 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004394 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004395
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004396 if (sqe_flags & IOSQE_IO_DRAIN)
Pavel Begunkov9d763772019-12-17 02:22:07 +03004397 head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004398
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004399 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004400 req->flags |= REQ_F_HARDLINK;
4401
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004402 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004403 ret = -EAGAIN;
4404 goto err_req;
4405 }
4406
Jens Axboe3529d8c2019-12-19 18:24:38 -07004407 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004408 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004409 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004410 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004411 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004412 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004413 trace_io_uring_link(ctx, req, head);
4414 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004415
4416 /* last request of a link, enqueue the link */
4417 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4418 io_queue_link_head(head);
4419 *link = NULL;
4420 }
4421 } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004422 req->flags |= REQ_F_LINK;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004423 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004424 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004425
Jens Axboe9e645e112019-05-10 16:07:28 -06004426 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004427 ret = io_req_defer_prep(req, sqe);
4428 if (ret)
4429 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004430 *link = req;
4431 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004432 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004433 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004434
4435 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004436}
4437
Jens Axboe9a56a232019-01-09 09:06:50 -07004438/*
4439 * Batched submission is done, ensure local IO is flushed out.
4440 */
4441static void io_submit_state_end(struct io_submit_state *state)
4442{
4443 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004444 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004445 if (state->free_reqs)
4446 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4447 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004448}
4449
4450/*
4451 * Start submission side cache.
4452 */
4453static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004454 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004455{
4456 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004457 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004458 state->file = NULL;
4459 state->ios_left = max_ios;
4460}
4461
Jens Axboe2b188cc2019-01-07 10:46:33 -07004462static void io_commit_sqring(struct io_ring_ctx *ctx)
4463{
Hristo Venev75b28af2019-08-26 17:23:46 +00004464 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004465
Hristo Venev75b28af2019-08-26 17:23:46 +00004466 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004467 /*
4468 * Ensure any loads from the SQEs are done at this point,
4469 * since once we write the new head, the application could
4470 * write new data to them.
4471 */
Hristo Venev75b28af2019-08-26 17:23:46 +00004472 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004473 }
4474}
4475
4476/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004477 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004478 * that is mapped by userspace. This means that care needs to be taken to
4479 * ensure that reads are stable, as we cannot rely on userspace always
4480 * being a good citizen. If members of the sqe are validated and then later
4481 * used, it's important that those reads are done through READ_ONCE() to
4482 * prevent a re-load down the line.
4483 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004484static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4485 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004486{
Hristo Venev75b28af2019-08-26 17:23:46 +00004487 struct io_rings *rings = ctx->rings;
4488 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004489 unsigned head;
4490
4491 /*
4492 * The cached sq head (or cq tail) serves two purposes:
4493 *
4494 * 1) allows us to batch the cost of updating the user visible
4495 * head updates.
4496 * 2) allows the kernel side to track the head on its own, even
4497 * though the application is the one updating it.
4498 */
4499 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02004500 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004501 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004502 return false;
4503
Hristo Venev75b28af2019-08-26 17:23:46 +00004504 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004505 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004506 /*
4507 * All io need record the previous position, if LINK vs DARIN,
4508 * it can be used to mark the position of the first IO in the
4509 * link list.
4510 */
4511 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004512 *sqe_ptr = &ctx->sq_sqes[head];
4513 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4514 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004515 ctx->cached_sq_head++;
4516 return true;
4517 }
4518
4519 /* drop invalid entries */
4520 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004521 ctx->cached_sq_dropped++;
4522 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004523 return false;
4524}
4525
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004526static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004527 struct file *ring_file, int ring_fd,
4528 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004529{
4530 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004531 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004532 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004533 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004534
Jens Axboec4a2ed72019-11-21 21:01:26 -07004535 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004536 if (test_bit(0, &ctx->sq_check_overflow)) {
4537 if (!list_empty(&ctx->cq_overflow_list) &&
4538 !io_cqring_overflow_flush(ctx, false))
4539 return -EBUSY;
4540 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004541
4542 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004543 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004544 statep = &state;
4545 }
4546
4547 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004548 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004549 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004550
Pavel Begunkov196be952019-11-07 01:41:06 +03004551 req = io_get_req(ctx, statep);
4552 if (unlikely(!req)) {
4553 if (!submitted)
4554 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004555 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004556 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004557 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03004558 __io_free_req(req);
4559 break;
4560 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004561
Jens Axboed3656342019-12-18 09:50:26 -07004562 /* will complete beyond this point, count as submitted */
4563 submitted++;
4564
4565 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4566 io_cqring_add_event(req, -EINVAL);
4567 io_double_put_req(req);
4568 break;
4569 }
4570
4571 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004572 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4573 if (!mm_fault) {
4574 use_mm(ctx->sqo_mm);
4575 *mm = ctx->sqo_mm;
4576 }
4577 }
4578
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004579 req->ring_file = ring_file;
4580 req->ring_fd = ring_fd;
4581 req->has_user = *mm != NULL;
4582 req->in_async = async;
4583 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07004584 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004585 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004586 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004587 }
4588
Jens Axboe9e645e112019-05-10 16:07:28 -06004589 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004590 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004591 if (statep)
4592 io_submit_state_end(&state);
4593
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004594 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4595 io_commit_sqring(ctx);
4596
Jens Axboe6c271ce2019-01-10 11:22:30 -07004597 return submitted;
4598}
4599
4600static int io_sq_thread(void *data)
4601{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004602 struct io_ring_ctx *ctx = data;
4603 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004604 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004605 mm_segment_t old_fs;
4606 DEFINE_WAIT(wait);
4607 unsigned inflight;
4608 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004609 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004610
Jens Axboe206aefd2019-11-07 18:27:42 -07004611 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004612
Jens Axboe6c271ce2019-01-10 11:22:30 -07004613 old_fs = get_fs();
4614 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004615 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004616
Jens Axboec1edbf52019-11-10 16:56:04 -07004617 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004618 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004619 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004620
4621 if (inflight) {
4622 unsigned nr_events = 0;
4623
4624 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004625 /*
4626 * inflight is the count of the maximum possible
4627 * entries we submitted, but it can be smaller
4628 * if we dropped some of them. If we don't have
4629 * poll entries available, then we know that we
4630 * have nothing left to poll for. Reset the
4631 * inflight count to zero in that case.
4632 */
4633 mutex_lock(&ctx->uring_lock);
4634 if (!list_empty(&ctx->poll_list))
4635 __io_iopoll_check(ctx, &nr_events, 0);
4636 else
4637 inflight = 0;
4638 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004639 } else {
4640 /*
4641 * Normal IO, just pretend everything completed.
4642 * We don't have to poll completions for that.
4643 */
4644 nr_events = inflight;
4645 }
4646
4647 inflight -= nr_events;
4648 if (!inflight)
4649 timeout = jiffies + ctx->sq_thread_idle;
4650 }
4651
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004652 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004653
4654 /*
4655 * If submit got -EBUSY, flag us as needing the application
4656 * to enter the kernel to reap and flush events.
4657 */
4658 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004659 /*
4660 * We're polling. If we're within the defined idle
4661 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004662 * to sleep. The exception is if we got EBUSY doing
4663 * more IO, we should wait for the application to
4664 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004665 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004666 if (inflight ||
4667 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004668 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004669 continue;
4670 }
4671
4672 /*
4673 * Drop cur_mm before scheduling, we can't hold it for
4674 * long periods (or over schedule()). Do this before
4675 * adding ourselves to the waitqueue, as the unuse/drop
4676 * may sleep.
4677 */
4678 if (cur_mm) {
4679 unuse_mm(cur_mm);
4680 mmput(cur_mm);
4681 cur_mm = NULL;
4682 }
4683
4684 prepare_to_wait(&ctx->sqo_wait, &wait,
4685 TASK_INTERRUPTIBLE);
4686
4687 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004688 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004689 /* make sure to read SQ tail after writing flags */
4690 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004691
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004692 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004693 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004694 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004695 finish_wait(&ctx->sqo_wait, &wait);
4696 break;
4697 }
4698 if (signal_pending(current))
4699 flush_signals(current);
4700 schedule();
4701 finish_wait(&ctx->sqo_wait, &wait);
4702
Hristo Venev75b28af2019-08-26 17:23:46 +00004703 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004704 continue;
4705 }
4706 finish_wait(&ctx->sqo_wait, &wait);
4707
Hristo Venev75b28af2019-08-26 17:23:46 +00004708 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004709 }
4710
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004711 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004712 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004713 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004714 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004715 if (ret > 0)
4716 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004717 }
4718
4719 set_fs(old_fs);
4720 if (cur_mm) {
4721 unuse_mm(cur_mm);
4722 mmput(cur_mm);
4723 }
Jens Axboe181e4482019-11-25 08:52:30 -07004724 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004725
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004726 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004727
Jens Axboe6c271ce2019-01-10 11:22:30 -07004728 return 0;
4729}
4730
Jens Axboebda52162019-09-24 13:47:15 -06004731struct io_wait_queue {
4732 struct wait_queue_entry wq;
4733 struct io_ring_ctx *ctx;
4734 unsigned to_wait;
4735 unsigned nr_timeouts;
4736};
4737
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004738static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004739{
4740 struct io_ring_ctx *ctx = iowq->ctx;
4741
4742 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004743 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004744 * started waiting. For timeouts, we always want to return to userspace,
4745 * regardless of event count.
4746 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004747 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004748 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4749}
4750
4751static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4752 int wake_flags, void *key)
4753{
4754 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4755 wq);
4756
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004757 /* use noflush == true, as we can't safely rely on locking context */
4758 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004759 return -1;
4760
4761 return autoremove_wake_function(curr, mode, wake_flags, key);
4762}
4763
Jens Axboe2b188cc2019-01-07 10:46:33 -07004764/*
4765 * Wait until events become available, if we don't already have some. The
4766 * application must reap them itself, as they reside on the shared cq ring.
4767 */
4768static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4769 const sigset_t __user *sig, size_t sigsz)
4770{
Jens Axboebda52162019-09-24 13:47:15 -06004771 struct io_wait_queue iowq = {
4772 .wq = {
4773 .private = current,
4774 .func = io_wake_function,
4775 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4776 },
4777 .ctx = ctx,
4778 .to_wait = min_events,
4779 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004780 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004781 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004782
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004783 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004784 return 0;
4785
4786 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004787#ifdef CONFIG_COMPAT
4788 if (in_compat_syscall())
4789 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004790 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004791 else
4792#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004793 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004794
Jens Axboe2b188cc2019-01-07 10:46:33 -07004795 if (ret)
4796 return ret;
4797 }
4798
Jens Axboebda52162019-09-24 13:47:15 -06004799 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004800 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004801 do {
4802 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4803 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004804 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004805 break;
4806 schedule();
4807 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004808 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004809 break;
4810 }
4811 } while (1);
4812 finish_wait(&ctx->wait, &iowq.wq);
4813
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004814 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004815
Hristo Venev75b28af2019-08-26 17:23:46 +00004816 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004817}
4818
Jens Axboe6b063142019-01-10 22:13:58 -07004819static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4820{
4821#if defined(CONFIG_UNIX)
4822 if (ctx->ring_sock) {
4823 struct sock *sock = ctx->ring_sock->sk;
4824 struct sk_buff *skb;
4825
4826 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4827 kfree_skb(skb);
4828 }
4829#else
4830 int i;
4831
Jens Axboe65e19f52019-10-26 07:20:21 -06004832 for (i = 0; i < ctx->nr_user_files; i++) {
4833 struct file *file;
4834
4835 file = io_file_from_index(ctx, i);
4836 if (file)
4837 fput(file);
4838 }
Jens Axboe6b063142019-01-10 22:13:58 -07004839#endif
4840}
4841
Jens Axboe05f3fb32019-12-09 11:22:50 -07004842static void io_file_ref_kill(struct percpu_ref *ref)
4843{
4844 struct fixed_file_data *data;
4845
4846 data = container_of(ref, struct fixed_file_data, refs);
4847 complete(&data->done);
4848}
4849
Jens Axboe6b063142019-01-10 22:13:58 -07004850static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4851{
Jens Axboe05f3fb32019-12-09 11:22:50 -07004852 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06004853 unsigned nr_tables, i;
4854
Jens Axboe05f3fb32019-12-09 11:22:50 -07004855 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07004856 return -ENXIO;
4857
Jens Axboe05f3fb32019-12-09 11:22:50 -07004858 /* protect against inflight atomic switch, which drops the ref */
4859 flush_work(&data->ref_work);
4860 percpu_ref_get(&data->refs);
4861 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
4862 wait_for_completion(&data->done);
4863 percpu_ref_put(&data->refs);
4864 percpu_ref_exit(&data->refs);
4865
Jens Axboe6b063142019-01-10 22:13:58 -07004866 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004867 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4868 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004869 kfree(data->table[i].files);
4870 kfree(data->table);
4871 kfree(data);
4872 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004873 ctx->nr_user_files = 0;
4874 return 0;
4875}
4876
Jens Axboe6c271ce2019-01-10 11:22:30 -07004877static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4878{
4879 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004880 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004881 /*
4882 * The park is a bit of a work-around, without it we get
4883 * warning spews on shutdown with SQPOLL set and affinity
4884 * set to a single CPU.
4885 */
Jens Axboe06058632019-04-13 09:26:03 -06004886 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004887 kthread_stop(ctx->sqo_thread);
4888 ctx->sqo_thread = NULL;
4889 }
4890}
4891
Jens Axboe6b063142019-01-10 22:13:58 -07004892static void io_finish_async(struct io_ring_ctx *ctx)
4893{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004894 io_sq_thread_stop(ctx);
4895
Jens Axboe561fb042019-10-24 07:25:42 -06004896 if (ctx->io_wq) {
4897 io_wq_destroy(ctx->io_wq);
4898 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004899 }
4900}
4901
4902#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07004903/*
4904 * Ensure the UNIX gc is aware of our file set, so we are certain that
4905 * the io_uring can be safely unregistered on process exit, even if we have
4906 * loops in the file referencing.
4907 */
4908static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4909{
4910 struct sock *sk = ctx->ring_sock->sk;
4911 struct scm_fp_list *fpl;
4912 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004913 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004914
4915 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4916 unsigned long inflight = ctx->user->unix_inflight + nr;
4917
4918 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4919 return -EMFILE;
4920 }
4921
4922 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4923 if (!fpl)
4924 return -ENOMEM;
4925
4926 skb = alloc_skb(0, GFP_KERNEL);
4927 if (!skb) {
4928 kfree(fpl);
4929 return -ENOMEM;
4930 }
4931
4932 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004933
Jens Axboe08a45172019-10-03 08:11:03 -06004934 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004935 fpl->user = get_uid(ctx->user);
4936 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004937 struct file *file = io_file_from_index(ctx, i + offset);
4938
4939 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004940 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004941 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004942 unix_inflight(fpl->user, fpl->fp[nr_files]);
4943 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004944 }
4945
Jens Axboe08a45172019-10-03 08:11:03 -06004946 if (nr_files) {
4947 fpl->max = SCM_MAX_FD;
4948 fpl->count = nr_files;
4949 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004950 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06004951 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4952 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004953
Jens Axboe08a45172019-10-03 08:11:03 -06004954 for (i = 0; i < nr_files; i++)
4955 fput(fpl->fp[i]);
4956 } else {
4957 kfree_skb(skb);
4958 kfree(fpl);
4959 }
Jens Axboe6b063142019-01-10 22:13:58 -07004960
4961 return 0;
4962}
4963
4964/*
4965 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4966 * causes regular reference counting to break down. We rely on the UNIX
4967 * garbage collection to take care of this problem for us.
4968 */
4969static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4970{
4971 unsigned left, total;
4972 int ret = 0;
4973
4974 total = 0;
4975 left = ctx->nr_user_files;
4976 while (left) {
4977 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004978
4979 ret = __io_sqe_files_scm(ctx, this_files, total);
4980 if (ret)
4981 break;
4982 left -= this_files;
4983 total += this_files;
4984 }
4985
4986 if (!ret)
4987 return 0;
4988
4989 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004990 struct file *file = io_file_from_index(ctx, total);
4991
4992 if (file)
4993 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004994 total++;
4995 }
4996
4997 return ret;
4998}
4999#else
5000static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5001{
5002 return 0;
5003}
5004#endif
5005
Jens Axboe65e19f52019-10-26 07:20:21 -06005006static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5007 unsigned nr_files)
5008{
5009 int i;
5010
5011 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005012 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005013 unsigned this_files;
5014
5015 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5016 table->files = kcalloc(this_files, sizeof(struct file *),
5017 GFP_KERNEL);
5018 if (!table->files)
5019 break;
5020 nr_files -= this_files;
5021 }
5022
5023 if (i == nr_tables)
5024 return 0;
5025
5026 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005027 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005028 kfree(table->files);
5029 }
5030 return 1;
5031}
5032
Jens Axboe05f3fb32019-12-09 11:22:50 -07005033static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005034{
5035#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005036 struct sock *sock = ctx->ring_sock->sk;
5037 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5038 struct sk_buff *skb;
5039 int i;
5040
5041 __skb_queue_head_init(&list);
5042
5043 /*
5044 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5045 * remove this entry and rearrange the file array.
5046 */
5047 skb = skb_dequeue(head);
5048 while (skb) {
5049 struct scm_fp_list *fp;
5050
5051 fp = UNIXCB(skb).fp;
5052 for (i = 0; i < fp->count; i++) {
5053 int left;
5054
5055 if (fp->fp[i] != file)
5056 continue;
5057
5058 unix_notinflight(fp->user, fp->fp[i]);
5059 left = fp->count - 1 - i;
5060 if (left) {
5061 memmove(&fp->fp[i], &fp->fp[i + 1],
5062 left * sizeof(struct file *));
5063 }
5064 fp->count--;
5065 if (!fp->count) {
5066 kfree_skb(skb);
5067 skb = NULL;
5068 } else {
5069 __skb_queue_tail(&list, skb);
5070 }
5071 fput(file);
5072 file = NULL;
5073 break;
5074 }
5075
5076 if (!file)
5077 break;
5078
5079 __skb_queue_tail(&list, skb);
5080
5081 skb = skb_dequeue(head);
5082 }
5083
5084 if (skb_peek(&list)) {
5085 spin_lock_irq(&head->lock);
5086 while ((skb = __skb_dequeue(&list)) != NULL)
5087 __skb_queue_tail(head, skb);
5088 spin_unlock_irq(&head->lock);
5089 }
5090#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005091 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005092#endif
5093}
5094
Jens Axboe05f3fb32019-12-09 11:22:50 -07005095struct io_file_put {
5096 struct llist_node llist;
5097 struct file *file;
5098 struct completion *done;
5099};
5100
5101static void io_ring_file_ref_switch(struct work_struct *work)
5102{
5103 struct io_file_put *pfile, *tmp;
5104 struct fixed_file_data *data;
5105 struct llist_node *node;
5106
5107 data = container_of(work, struct fixed_file_data, ref_work);
5108
5109 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5110 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5111 io_ring_file_put(data->ctx, pfile->file);
5112 if (pfile->done)
5113 complete(pfile->done);
5114 else
5115 kfree(pfile);
5116 }
5117 }
5118
5119 percpu_ref_get(&data->refs);
5120 percpu_ref_switch_to_percpu(&data->refs);
5121}
5122
5123static void io_file_data_ref_zero(struct percpu_ref *ref)
5124{
5125 struct fixed_file_data *data;
5126
5127 data = container_of(ref, struct fixed_file_data, refs);
5128
5129 /* we can't safely switch from inside this context, punt to wq */
5130 queue_work(system_wq, &data->ref_work);
5131}
5132
5133static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5134 unsigned nr_args)
5135{
5136 __s32 __user *fds = (__s32 __user *) arg;
5137 unsigned nr_tables;
5138 struct file *file;
5139 int fd, ret = 0;
5140 unsigned i;
5141
5142 if (ctx->file_data)
5143 return -EBUSY;
5144 if (!nr_args)
5145 return -EINVAL;
5146 if (nr_args > IORING_MAX_FIXED_FILES)
5147 return -EMFILE;
5148
5149 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5150 if (!ctx->file_data)
5151 return -ENOMEM;
5152 ctx->file_data->ctx = ctx;
5153 init_completion(&ctx->file_data->done);
5154
5155 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5156 ctx->file_data->table = kcalloc(nr_tables,
5157 sizeof(struct fixed_file_table),
5158 GFP_KERNEL);
5159 if (!ctx->file_data->table) {
5160 kfree(ctx->file_data);
5161 ctx->file_data = NULL;
5162 return -ENOMEM;
5163 }
5164
5165 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5166 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5167 kfree(ctx->file_data->table);
5168 kfree(ctx->file_data);
5169 ctx->file_data = NULL;
5170 return -ENOMEM;
5171 }
5172 ctx->file_data->put_llist.first = NULL;
5173 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5174
5175 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5176 percpu_ref_exit(&ctx->file_data->refs);
5177 kfree(ctx->file_data->table);
5178 kfree(ctx->file_data);
5179 ctx->file_data = NULL;
5180 return -ENOMEM;
5181 }
5182
5183 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5184 struct fixed_file_table *table;
5185 unsigned index;
5186
5187 ret = -EFAULT;
5188 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5189 break;
5190 /* allow sparse sets */
5191 if (fd == -1) {
5192 ret = 0;
5193 continue;
5194 }
5195
5196 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5197 index = i & IORING_FILE_TABLE_MASK;
5198 file = fget(fd);
5199
5200 ret = -EBADF;
5201 if (!file)
5202 break;
5203
5204 /*
5205 * Don't allow io_uring instances to be registered. If UNIX
5206 * isn't enabled, then this causes a reference cycle and this
5207 * instance can never get freed. If UNIX is enabled we'll
5208 * handle it just fine, but there's still no point in allowing
5209 * a ring fd as it doesn't support regular read/write anyway.
5210 */
5211 if (file->f_op == &io_uring_fops) {
5212 fput(file);
5213 break;
5214 }
5215 ret = 0;
5216 table->files[index] = file;
5217 }
5218
5219 if (ret) {
5220 for (i = 0; i < ctx->nr_user_files; i++) {
5221 file = io_file_from_index(ctx, i);
5222 if (file)
5223 fput(file);
5224 }
5225 for (i = 0; i < nr_tables; i++)
5226 kfree(ctx->file_data->table[i].files);
5227
5228 kfree(ctx->file_data->table);
5229 kfree(ctx->file_data);
5230 ctx->file_data = NULL;
5231 ctx->nr_user_files = 0;
5232 return ret;
5233 }
5234
5235 ret = io_sqe_files_scm(ctx);
5236 if (ret)
5237 io_sqe_files_unregister(ctx);
5238
5239 return ret;
5240}
5241
Jens Axboec3a31e62019-10-03 13:59:56 -06005242static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5243 int index)
5244{
5245#if defined(CONFIG_UNIX)
5246 struct sock *sock = ctx->ring_sock->sk;
5247 struct sk_buff_head *head = &sock->sk_receive_queue;
5248 struct sk_buff *skb;
5249
5250 /*
5251 * See if we can merge this file into an existing skb SCM_RIGHTS
5252 * file set. If there's no room, fall back to allocating a new skb
5253 * and filling it in.
5254 */
5255 spin_lock_irq(&head->lock);
5256 skb = skb_peek(head);
5257 if (skb) {
5258 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5259
5260 if (fpl->count < SCM_MAX_FD) {
5261 __skb_unlink(skb, head);
5262 spin_unlock_irq(&head->lock);
5263 fpl->fp[fpl->count] = get_file(file);
5264 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5265 fpl->count++;
5266 spin_lock_irq(&head->lock);
5267 __skb_queue_head(head, skb);
5268 } else {
5269 skb = NULL;
5270 }
5271 }
5272 spin_unlock_irq(&head->lock);
5273
5274 if (skb) {
5275 fput(file);
5276 return 0;
5277 }
5278
5279 return __io_sqe_files_scm(ctx, 1, index);
5280#else
5281 return 0;
5282#endif
5283}
5284
Jens Axboe05f3fb32019-12-09 11:22:50 -07005285static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005286{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005287 struct fixed_file_data *data;
5288
5289 data = container_of(ref, struct fixed_file_data, refs);
5290 clear_bit(FFD_F_ATOMIC, &data->state);
5291}
5292
5293static bool io_queue_file_removal(struct fixed_file_data *data,
5294 struct file *file)
5295{
5296 struct io_file_put *pfile, pfile_stack;
5297 DECLARE_COMPLETION_ONSTACK(done);
5298
5299 /*
5300 * If we fail allocating the struct we need for doing async reomval
5301 * of this file, just punt to sync and wait for it.
5302 */
5303 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5304 if (!pfile) {
5305 pfile = &pfile_stack;
5306 pfile->done = &done;
5307 }
5308
5309 pfile->file = file;
5310 llist_add(&pfile->llist, &data->put_llist);
5311
5312 if (pfile == &pfile_stack) {
5313 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5314 percpu_ref_put(&data->refs);
5315 percpu_ref_switch_to_atomic(&data->refs,
5316 io_atomic_switch);
5317 }
5318 wait_for_completion(&done);
5319 flush_work(&data->ref_work);
5320 return false;
5321 }
5322
5323 return true;
5324}
5325
5326static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5327 struct io_uring_files_update *up,
5328 unsigned nr_args)
5329{
5330 struct fixed_file_data *data = ctx->file_data;
5331 bool ref_switch = false;
5332 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005333 __s32 __user *fds;
5334 int fd, i, err;
5335 __u32 done;
5336
Jens Axboe05f3fb32019-12-09 11:22:50 -07005337 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005338 return -EOVERFLOW;
5339 if (done > ctx->nr_user_files)
5340 return -EINVAL;
5341
5342 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005343 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005344 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005345 struct fixed_file_table *table;
5346 unsigned index;
5347
Jens Axboec3a31e62019-10-03 13:59:56 -06005348 err = 0;
5349 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5350 err = -EFAULT;
5351 break;
5352 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005353 i = array_index_nospec(up->offset, ctx->nr_user_files);
5354 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005355 index = i & IORING_FILE_TABLE_MASK;
5356 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005357 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005358 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005359 if (io_queue_file_removal(data, file))
5360 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005361 }
5362 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005363 file = fget(fd);
5364 if (!file) {
5365 err = -EBADF;
5366 break;
5367 }
5368 /*
5369 * Don't allow io_uring instances to be registered. If
5370 * UNIX isn't enabled, then this causes a reference
5371 * cycle and this instance can never get freed. If UNIX
5372 * is enabled we'll handle it just fine, but there's
5373 * still no point in allowing a ring fd as it doesn't
5374 * support regular read/write anyway.
5375 */
5376 if (file->f_op == &io_uring_fops) {
5377 fput(file);
5378 err = -EBADF;
5379 break;
5380 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005381 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005382 err = io_sqe_file_register(ctx, file, i);
5383 if (err)
5384 break;
5385 }
5386 nr_args--;
5387 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005388 up->offset++;
5389 }
5390
5391 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5392 percpu_ref_put(&data->refs);
5393 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005394 }
5395
5396 return done ? done : err;
5397}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005398static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5399 unsigned nr_args)
5400{
5401 struct io_uring_files_update up;
5402
5403 if (!ctx->file_data)
5404 return -ENXIO;
5405 if (!nr_args)
5406 return -EINVAL;
5407 if (copy_from_user(&up, arg, sizeof(up)))
5408 return -EFAULT;
5409 if (up.resv)
5410 return -EINVAL;
5411
5412 return __io_sqe_files_update(ctx, &up, nr_args);
5413}
Jens Axboec3a31e62019-10-03 13:59:56 -06005414
Jens Axboe7d723062019-11-12 22:31:31 -07005415static void io_put_work(struct io_wq_work *work)
5416{
5417 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5418
5419 io_put_req(req);
5420}
5421
5422static void io_get_work(struct io_wq_work *work)
5423{
5424 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5425
5426 refcount_inc(&req->refs);
5427}
5428
Jens Axboe6c271ce2019-01-10 11:22:30 -07005429static int io_sq_offload_start(struct io_ring_ctx *ctx,
5430 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005431{
Jens Axboe576a3472019-11-25 08:49:20 -07005432 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005433 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005434 int ret;
5435
Jens Axboe6c271ce2019-01-10 11:22:30 -07005436 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005437 mmgrab(current->mm);
5438 ctx->sqo_mm = current->mm;
5439
Jens Axboe6c271ce2019-01-10 11:22:30 -07005440 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005441 ret = -EPERM;
5442 if (!capable(CAP_SYS_ADMIN))
5443 goto err;
5444
Jens Axboe917257d2019-04-13 09:28:55 -06005445 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5446 if (!ctx->sq_thread_idle)
5447 ctx->sq_thread_idle = HZ;
5448
Jens Axboe6c271ce2019-01-10 11:22:30 -07005449 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005450 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005451
Jens Axboe917257d2019-04-13 09:28:55 -06005452 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005453 if (cpu >= nr_cpu_ids)
5454 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005455 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005456 goto err;
5457
Jens Axboe6c271ce2019-01-10 11:22:30 -07005458 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5459 ctx, cpu,
5460 "io_uring-sq");
5461 } else {
5462 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5463 "io_uring-sq");
5464 }
5465 if (IS_ERR(ctx->sqo_thread)) {
5466 ret = PTR_ERR(ctx->sqo_thread);
5467 ctx->sqo_thread = NULL;
5468 goto err;
5469 }
5470 wake_up_process(ctx->sqo_thread);
5471 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5472 /* Can't have SQ_AFF without SQPOLL */
5473 ret = -EINVAL;
5474 goto err;
5475 }
5476
Jens Axboe576a3472019-11-25 08:49:20 -07005477 data.mm = ctx->sqo_mm;
5478 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005479 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005480 data.get_work = io_get_work;
5481 data.put_work = io_put_work;
5482
Jens Axboe561fb042019-10-24 07:25:42 -06005483 /* Do QD, or 4 * CPUS, whatever is smallest */
5484 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005485 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005486 if (IS_ERR(ctx->io_wq)) {
5487 ret = PTR_ERR(ctx->io_wq);
5488 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005489 goto err;
5490 }
5491
5492 return 0;
5493err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005494 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005495 mmdrop(ctx->sqo_mm);
5496 ctx->sqo_mm = NULL;
5497 return ret;
5498}
5499
5500static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5501{
5502 atomic_long_sub(nr_pages, &user->locked_vm);
5503}
5504
5505static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5506{
5507 unsigned long page_limit, cur_pages, new_pages;
5508
5509 /* Don't allow more pages than we can safely lock */
5510 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5511
5512 do {
5513 cur_pages = atomic_long_read(&user->locked_vm);
5514 new_pages = cur_pages + nr_pages;
5515 if (new_pages > page_limit)
5516 return -ENOMEM;
5517 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5518 new_pages) != cur_pages);
5519
5520 return 0;
5521}
5522
5523static void io_mem_free(void *ptr)
5524{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005525 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005526
Mark Rutland52e04ef2019-04-30 17:30:21 +01005527 if (!ptr)
5528 return;
5529
5530 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005531 if (put_page_testzero(page))
5532 free_compound_page(page);
5533}
5534
5535static void *io_mem_alloc(size_t size)
5536{
5537 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5538 __GFP_NORETRY;
5539
5540 return (void *) __get_free_pages(gfp_flags, get_order(size));
5541}
5542
Hristo Venev75b28af2019-08-26 17:23:46 +00005543static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5544 size_t *sq_offset)
5545{
5546 struct io_rings *rings;
5547 size_t off, sq_array_size;
5548
5549 off = struct_size(rings, cqes, cq_entries);
5550 if (off == SIZE_MAX)
5551 return SIZE_MAX;
5552
5553#ifdef CONFIG_SMP
5554 off = ALIGN(off, SMP_CACHE_BYTES);
5555 if (off == 0)
5556 return SIZE_MAX;
5557#endif
5558
5559 sq_array_size = array_size(sizeof(u32), sq_entries);
5560 if (sq_array_size == SIZE_MAX)
5561 return SIZE_MAX;
5562
5563 if (check_add_overflow(off, sq_array_size, &off))
5564 return SIZE_MAX;
5565
5566 if (sq_offset)
5567 *sq_offset = off;
5568
5569 return off;
5570}
5571
Jens Axboe2b188cc2019-01-07 10:46:33 -07005572static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5573{
Hristo Venev75b28af2019-08-26 17:23:46 +00005574 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005575
Hristo Venev75b28af2019-08-26 17:23:46 +00005576 pages = (size_t)1 << get_order(
5577 rings_size(sq_entries, cq_entries, NULL));
5578 pages += (size_t)1 << get_order(
5579 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005580
Hristo Venev75b28af2019-08-26 17:23:46 +00005581 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005582}
5583
Jens Axboeedafcce2019-01-09 09:16:05 -07005584static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5585{
5586 int i, j;
5587
5588 if (!ctx->user_bufs)
5589 return -ENXIO;
5590
5591 for (i = 0; i < ctx->nr_user_bufs; i++) {
5592 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5593
5594 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005595 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005596
5597 if (ctx->account_mem)
5598 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005599 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005600 imu->nr_bvecs = 0;
5601 }
5602
5603 kfree(ctx->user_bufs);
5604 ctx->user_bufs = NULL;
5605 ctx->nr_user_bufs = 0;
5606 return 0;
5607}
5608
5609static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5610 void __user *arg, unsigned index)
5611{
5612 struct iovec __user *src;
5613
5614#ifdef CONFIG_COMPAT
5615 if (ctx->compat) {
5616 struct compat_iovec __user *ciovs;
5617 struct compat_iovec ciov;
5618
5619 ciovs = (struct compat_iovec __user *) arg;
5620 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5621 return -EFAULT;
5622
Jens Axboed55e5f52019-12-11 16:12:15 -07005623 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005624 dst->iov_len = ciov.iov_len;
5625 return 0;
5626 }
5627#endif
5628 src = (struct iovec __user *) arg;
5629 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5630 return -EFAULT;
5631 return 0;
5632}
5633
5634static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5635 unsigned nr_args)
5636{
5637 struct vm_area_struct **vmas = NULL;
5638 struct page **pages = NULL;
5639 int i, j, got_pages = 0;
5640 int ret = -EINVAL;
5641
5642 if (ctx->user_bufs)
5643 return -EBUSY;
5644 if (!nr_args || nr_args > UIO_MAXIOV)
5645 return -EINVAL;
5646
5647 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5648 GFP_KERNEL);
5649 if (!ctx->user_bufs)
5650 return -ENOMEM;
5651
5652 for (i = 0; i < nr_args; i++) {
5653 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5654 unsigned long off, start, end, ubuf;
5655 int pret, nr_pages;
5656 struct iovec iov;
5657 size_t size;
5658
5659 ret = io_copy_iov(ctx, &iov, arg, i);
5660 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005661 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005662
5663 /*
5664 * Don't impose further limits on the size and buffer
5665 * constraints here, we'll -EINVAL later when IO is
5666 * submitted if they are wrong.
5667 */
5668 ret = -EFAULT;
5669 if (!iov.iov_base || !iov.iov_len)
5670 goto err;
5671
5672 /* arbitrary limit, but we need something */
5673 if (iov.iov_len > SZ_1G)
5674 goto err;
5675
5676 ubuf = (unsigned long) iov.iov_base;
5677 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5678 start = ubuf >> PAGE_SHIFT;
5679 nr_pages = end - start;
5680
5681 if (ctx->account_mem) {
5682 ret = io_account_mem(ctx->user, nr_pages);
5683 if (ret)
5684 goto err;
5685 }
5686
5687 ret = 0;
5688 if (!pages || nr_pages > got_pages) {
5689 kfree(vmas);
5690 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005691 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005692 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005693 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005694 sizeof(struct vm_area_struct *),
5695 GFP_KERNEL);
5696 if (!pages || !vmas) {
5697 ret = -ENOMEM;
5698 if (ctx->account_mem)
5699 io_unaccount_mem(ctx->user, nr_pages);
5700 goto err;
5701 }
5702 got_pages = nr_pages;
5703 }
5704
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005705 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005706 GFP_KERNEL);
5707 ret = -ENOMEM;
5708 if (!imu->bvec) {
5709 if (ctx->account_mem)
5710 io_unaccount_mem(ctx->user, nr_pages);
5711 goto err;
5712 }
5713
5714 ret = 0;
5715 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005716 pret = get_user_pages(ubuf, nr_pages,
5717 FOLL_WRITE | FOLL_LONGTERM,
5718 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005719 if (pret == nr_pages) {
5720 /* don't support file backed memory */
5721 for (j = 0; j < nr_pages; j++) {
5722 struct vm_area_struct *vma = vmas[j];
5723
5724 if (vma->vm_file &&
5725 !is_file_hugepages(vma->vm_file)) {
5726 ret = -EOPNOTSUPP;
5727 break;
5728 }
5729 }
5730 } else {
5731 ret = pret < 0 ? pret : -EFAULT;
5732 }
5733 up_read(&current->mm->mmap_sem);
5734 if (ret) {
5735 /*
5736 * if we did partial map, or found file backed vmas,
5737 * release any pages we did get
5738 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005739 if (pret > 0)
5740 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005741 if (ctx->account_mem)
5742 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005743 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005744 goto err;
5745 }
5746
5747 off = ubuf & ~PAGE_MASK;
5748 size = iov.iov_len;
5749 for (j = 0; j < nr_pages; j++) {
5750 size_t vec_len;
5751
5752 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5753 imu->bvec[j].bv_page = pages[j];
5754 imu->bvec[j].bv_len = vec_len;
5755 imu->bvec[j].bv_offset = off;
5756 off = 0;
5757 size -= vec_len;
5758 }
5759 /* store original address for later verification */
5760 imu->ubuf = ubuf;
5761 imu->len = iov.iov_len;
5762 imu->nr_bvecs = nr_pages;
5763
5764 ctx->nr_user_bufs++;
5765 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005766 kvfree(pages);
5767 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005768 return 0;
5769err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005770 kvfree(pages);
5771 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005772 io_sqe_buffer_unregister(ctx);
5773 return ret;
5774}
5775
Jens Axboe9b402842019-04-11 11:45:41 -06005776static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5777{
5778 __s32 __user *fds = arg;
5779 int fd;
5780
5781 if (ctx->cq_ev_fd)
5782 return -EBUSY;
5783
5784 if (copy_from_user(&fd, fds, sizeof(*fds)))
5785 return -EFAULT;
5786
5787 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5788 if (IS_ERR(ctx->cq_ev_fd)) {
5789 int ret = PTR_ERR(ctx->cq_ev_fd);
5790 ctx->cq_ev_fd = NULL;
5791 return ret;
5792 }
5793
5794 return 0;
5795}
5796
5797static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5798{
5799 if (ctx->cq_ev_fd) {
5800 eventfd_ctx_put(ctx->cq_ev_fd);
5801 ctx->cq_ev_fd = NULL;
5802 return 0;
5803 }
5804
5805 return -ENXIO;
5806}
5807
Jens Axboe2b188cc2019-01-07 10:46:33 -07005808static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5809{
Jens Axboe6b063142019-01-10 22:13:58 -07005810 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005811 if (ctx->sqo_mm)
5812 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005813
5814 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005815 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005816 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005817 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005818
Jens Axboe2b188cc2019-01-07 10:46:33 -07005819#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005820 if (ctx->ring_sock) {
5821 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005822 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005823 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005824#endif
5825
Hristo Venev75b28af2019-08-26 17:23:46 +00005826 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005827 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005828
5829 percpu_ref_exit(&ctx->refs);
5830 if (ctx->account_mem)
5831 io_unaccount_mem(ctx->user,
5832 ring_pages(ctx->sq_entries, ctx->cq_entries));
5833 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07005834 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07005835 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07005836 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07005837 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005838 kfree(ctx);
5839}
5840
5841static __poll_t io_uring_poll(struct file *file, poll_table *wait)
5842{
5843 struct io_ring_ctx *ctx = file->private_data;
5844 __poll_t mask = 0;
5845
5846 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02005847 /*
5848 * synchronizes with barrier from wq_has_sleeper call in
5849 * io_commit_cqring
5850 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005851 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00005852 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
5853 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005854 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08005855 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005856 mask |= EPOLLIN | EPOLLRDNORM;
5857
5858 return mask;
5859}
5860
5861static int io_uring_fasync(int fd, struct file *file, int on)
5862{
5863 struct io_ring_ctx *ctx = file->private_data;
5864
5865 return fasync_helper(fd, file, on, &ctx->cq_fasync);
5866}
5867
5868static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
5869{
5870 mutex_lock(&ctx->uring_lock);
5871 percpu_ref_kill(&ctx->refs);
5872 mutex_unlock(&ctx->uring_lock);
5873
Jens Axboe5262f562019-09-17 12:26:57 -06005874 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005875 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06005876
5877 if (ctx->io_wq)
5878 io_wq_cancel_all(ctx->io_wq);
5879
Jens Axboedef596e2019-01-09 08:59:42 -07005880 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07005881 /* if we failed setting up the ctx, we might not have any rings */
5882 if (ctx->rings)
5883 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07005884 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005885 io_ring_ctx_free(ctx);
5886}
5887
5888static int io_uring_release(struct inode *inode, struct file *file)
5889{
5890 struct io_ring_ctx *ctx = file->private_data;
5891
5892 file->private_data = NULL;
5893 io_ring_ctx_wait_and_kill(ctx);
5894 return 0;
5895}
5896
Jens Axboefcb323c2019-10-24 12:39:47 -06005897static void io_uring_cancel_files(struct io_ring_ctx *ctx,
5898 struct files_struct *files)
5899{
5900 struct io_kiocb *req;
5901 DEFINE_WAIT(wait);
5902
5903 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07005904 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06005905
5906 spin_lock_irq(&ctx->inflight_lock);
5907 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07005908 if (req->work.files != files)
5909 continue;
5910 /* req is being completed, ignore */
5911 if (!refcount_inc_not_zero(&req->refs))
5912 continue;
5913 cancel_req = req;
5914 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06005915 }
Jens Axboe768134d2019-11-10 20:30:53 -07005916 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005917 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07005918 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06005919 spin_unlock_irq(&ctx->inflight_lock);
5920
Jens Axboe768134d2019-11-10 20:30:53 -07005921 /* We need to keep going until we don't find a matching req */
5922 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005923 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005924
5925 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5926 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005927 schedule();
5928 }
Jens Axboe768134d2019-11-10 20:30:53 -07005929 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005930}
5931
5932static int io_uring_flush(struct file *file, void *data)
5933{
5934 struct io_ring_ctx *ctx = file->private_data;
5935
5936 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005937 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5938 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005939 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005940 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005941 return 0;
5942}
5943
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005944static void *io_uring_validate_mmap_request(struct file *file,
5945 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005946{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005947 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005948 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005949 struct page *page;
5950 void *ptr;
5951
5952 switch (offset) {
5953 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005954 case IORING_OFF_CQ_RING:
5955 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005956 break;
5957 case IORING_OFF_SQES:
5958 ptr = ctx->sq_sqes;
5959 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005960 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005961 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005962 }
5963
5964 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005965 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005966 return ERR_PTR(-EINVAL);
5967
5968 return ptr;
5969}
5970
5971#ifdef CONFIG_MMU
5972
5973static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5974{
5975 size_t sz = vma->vm_end - vma->vm_start;
5976 unsigned long pfn;
5977 void *ptr;
5978
5979 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5980 if (IS_ERR(ptr))
5981 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005982
5983 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5984 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5985}
5986
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005987#else /* !CONFIG_MMU */
5988
5989static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5990{
5991 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5992}
5993
5994static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5995{
5996 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5997}
5998
5999static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6000 unsigned long addr, unsigned long len,
6001 unsigned long pgoff, unsigned long flags)
6002{
6003 void *ptr;
6004
6005 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6006 if (IS_ERR(ptr))
6007 return PTR_ERR(ptr);
6008
6009 return (unsigned long) ptr;
6010}
6011
6012#endif /* !CONFIG_MMU */
6013
Jens Axboe2b188cc2019-01-07 10:46:33 -07006014SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6015 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6016 size_t, sigsz)
6017{
6018 struct io_ring_ctx *ctx;
6019 long ret = -EBADF;
6020 int submitted = 0;
6021 struct fd f;
6022
Jens Axboe6c271ce2019-01-10 11:22:30 -07006023 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006024 return -EINVAL;
6025
6026 f = fdget(fd);
6027 if (!f.file)
6028 return -EBADF;
6029
6030 ret = -EOPNOTSUPP;
6031 if (f.file->f_op != &io_uring_fops)
6032 goto out_fput;
6033
6034 ret = -ENXIO;
6035 ctx = f.file->private_data;
6036 if (!percpu_ref_tryget(&ctx->refs))
6037 goto out_fput;
6038
Jens Axboe6c271ce2019-01-10 11:22:30 -07006039 /*
6040 * For SQ polling, the thread will do all submissions and completions.
6041 * Just return the requested submit count, and wake the thread if
6042 * we were asked to.
6043 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006044 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006045 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006046 if (!list_empty_careful(&ctx->cq_overflow_list))
6047 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006048 if (flags & IORING_ENTER_SQ_WAKEUP)
6049 wake_up(&ctx->sqo_wait);
6050 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006051 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006052 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006053
Jens Axboe44d28272020-01-16 19:00:24 -07006054 if (current->mm != ctx->sqo_mm ||
6055 current_cred() != ctx->creds) {
6056 ret = -EPERM;
6057 goto out;
6058 }
6059
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006060 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006061 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006062 /* already have mm, so io_submit_sqes() won't try to grab it */
6063 cur_mm = ctx->sqo_mm;
6064 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6065 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006066 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006067
6068 if (submitted != to_submit)
6069 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006070 }
6071 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006072 unsigned nr_events = 0;
6073
Jens Axboe2b188cc2019-01-07 10:46:33 -07006074 min_complete = min(min_complete, ctx->cq_entries);
6075
Jens Axboedef596e2019-01-09 08:59:42 -07006076 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006077 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006078 } else {
6079 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6080 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006081 }
6082
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006083out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006084 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006085out_fput:
6086 fdput(f);
6087 return submitted ? submitted : ret;
6088}
6089
6090static const struct file_operations io_uring_fops = {
6091 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006092 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006093 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006094#ifndef CONFIG_MMU
6095 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6096 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6097#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006098 .poll = io_uring_poll,
6099 .fasync = io_uring_fasync,
6100};
6101
6102static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6103 struct io_uring_params *p)
6104{
Hristo Venev75b28af2019-08-26 17:23:46 +00006105 struct io_rings *rings;
6106 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006107
Hristo Venev75b28af2019-08-26 17:23:46 +00006108 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6109 if (size == SIZE_MAX)
6110 return -EOVERFLOW;
6111
6112 rings = io_mem_alloc(size);
6113 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006114 return -ENOMEM;
6115
Hristo Venev75b28af2019-08-26 17:23:46 +00006116 ctx->rings = rings;
6117 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6118 rings->sq_ring_mask = p->sq_entries - 1;
6119 rings->cq_ring_mask = p->cq_entries - 1;
6120 rings->sq_ring_entries = p->sq_entries;
6121 rings->cq_ring_entries = p->cq_entries;
6122 ctx->sq_mask = rings->sq_ring_mask;
6123 ctx->cq_mask = rings->cq_ring_mask;
6124 ctx->sq_entries = rings->sq_ring_entries;
6125 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006126
6127 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006128 if (size == SIZE_MAX) {
6129 io_mem_free(ctx->rings);
6130 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006131 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006132 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006133
6134 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006135 if (!ctx->sq_sqes) {
6136 io_mem_free(ctx->rings);
6137 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006138 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006139 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006140
Jens Axboe2b188cc2019-01-07 10:46:33 -07006141 return 0;
6142}
6143
6144/*
6145 * Allocate an anonymous fd, this is what constitutes the application
6146 * visible backing of an io_uring instance. The application mmaps this
6147 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6148 * we have to tie this fd to a socket for file garbage collection purposes.
6149 */
6150static int io_uring_get_fd(struct io_ring_ctx *ctx)
6151{
6152 struct file *file;
6153 int ret;
6154
6155#if defined(CONFIG_UNIX)
6156 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6157 &ctx->ring_sock);
6158 if (ret)
6159 return ret;
6160#endif
6161
6162 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6163 if (ret < 0)
6164 goto err;
6165
6166 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6167 O_RDWR | O_CLOEXEC);
6168 if (IS_ERR(file)) {
6169 put_unused_fd(ret);
6170 ret = PTR_ERR(file);
6171 goto err;
6172 }
6173
6174#if defined(CONFIG_UNIX)
6175 ctx->ring_sock->file = file;
6176#endif
6177 fd_install(ret, file);
6178 return ret;
6179err:
6180#if defined(CONFIG_UNIX)
6181 sock_release(ctx->ring_sock);
6182 ctx->ring_sock = NULL;
6183#endif
6184 return ret;
6185}
6186
6187static int io_uring_create(unsigned entries, struct io_uring_params *p)
6188{
6189 struct user_struct *user = NULL;
6190 struct io_ring_ctx *ctx;
6191 bool account_mem;
6192 int ret;
6193
6194 if (!entries || entries > IORING_MAX_ENTRIES)
6195 return -EINVAL;
6196
6197 /*
6198 * Use twice as many entries for the CQ ring. It's possible for the
6199 * application to drive a higher depth than the size of the SQ ring,
6200 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006201 * some flexibility in overcommitting a bit. If the application has
6202 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6203 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006204 */
6205 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006206 if (p->flags & IORING_SETUP_CQSIZE) {
6207 /*
6208 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6209 * to a power-of-two, if it isn't already. We do NOT impose
6210 * any cq vs sq ring sizing.
6211 */
6212 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
6213 return -EINVAL;
6214 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6215 } else {
6216 p->cq_entries = 2 * p->sq_entries;
6217 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006218
6219 user = get_uid(current_user());
6220 account_mem = !capable(CAP_IPC_LOCK);
6221
6222 if (account_mem) {
6223 ret = io_account_mem(user,
6224 ring_pages(p->sq_entries, p->cq_entries));
6225 if (ret) {
6226 free_uid(user);
6227 return ret;
6228 }
6229 }
6230
6231 ctx = io_ring_ctx_alloc(p);
6232 if (!ctx) {
6233 if (account_mem)
6234 io_unaccount_mem(user, ring_pages(p->sq_entries,
6235 p->cq_entries));
6236 free_uid(user);
6237 return -ENOMEM;
6238 }
6239 ctx->compat = in_compat_syscall();
6240 ctx->account_mem = account_mem;
6241 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006242 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006243
6244 ret = io_allocate_scq_urings(ctx, p);
6245 if (ret)
6246 goto err;
6247
Jens Axboe6c271ce2019-01-10 11:22:30 -07006248 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006249 if (ret)
6250 goto err;
6251
Jens Axboe2b188cc2019-01-07 10:46:33 -07006252 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006253 p->sq_off.head = offsetof(struct io_rings, sq.head);
6254 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6255 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6256 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6257 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6258 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6259 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006260
6261 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006262 p->cq_off.head = offsetof(struct io_rings, cq.head);
6263 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6264 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6265 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6266 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6267 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006268
Jens Axboe044c1ab2019-10-28 09:15:33 -06006269 /*
6270 * Install ring fd as the very last thing, so we don't risk someone
6271 * having closed it before we finish setup
6272 */
6273 ret = io_uring_get_fd(ctx);
6274 if (ret < 0)
6275 goto err;
6276
Jens Axboeda8c9692019-12-02 18:51:26 -07006277 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboeba042912019-12-25 16:33:42 -07006278 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006279 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006280 return ret;
6281err:
6282 io_ring_ctx_wait_and_kill(ctx);
6283 return ret;
6284}
6285
6286/*
6287 * Sets up an aio uring context, and returns the fd. Applications asks for a
6288 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6289 * params structure passed in.
6290 */
6291static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6292{
6293 struct io_uring_params p;
6294 long ret;
6295 int i;
6296
6297 if (copy_from_user(&p, params, sizeof(p)))
6298 return -EFAULT;
6299 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6300 if (p.resv[i])
6301 return -EINVAL;
6302 }
6303
Jens Axboe6c271ce2019-01-10 11:22:30 -07006304 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06006305 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006306 return -EINVAL;
6307
6308 ret = io_uring_create(entries, &p);
6309 if (ret < 0)
6310 return ret;
6311
6312 if (copy_to_user(params, &p, sizeof(p)))
6313 return -EFAULT;
6314
6315 return ret;
6316}
6317
6318SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6319 struct io_uring_params __user *, params)
6320{
6321 return io_uring_setup(entries, params);
6322}
6323
Jens Axboeedafcce2019-01-09 09:16:05 -07006324static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6325 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006326 __releases(ctx->uring_lock)
6327 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006328{
6329 int ret;
6330
Jens Axboe35fa71a2019-04-22 10:23:23 -06006331 /*
6332 * We're inside the ring mutex, if the ref is already dying, then
6333 * someone else killed the ctx or is already going through
6334 * io_uring_register().
6335 */
6336 if (percpu_ref_is_dying(&ctx->refs))
6337 return -ENXIO;
6338
Jens Axboe05f3fb32019-12-09 11:22:50 -07006339 if (opcode != IORING_UNREGISTER_FILES &&
6340 opcode != IORING_REGISTER_FILES_UPDATE) {
6341 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006342
Jens Axboe05f3fb32019-12-09 11:22:50 -07006343 /*
6344 * Drop uring mutex before waiting for references to exit. If
6345 * another thread is currently inside io_uring_enter() it might
6346 * need to grab the uring_lock to make progress. If we hold it
6347 * here across the drain wait, then we can deadlock. It's safe
6348 * to drop the mutex here, since no new references will come in
6349 * after we've killed the percpu ref.
6350 */
6351 mutex_unlock(&ctx->uring_lock);
6352 wait_for_completion(&ctx->completions[0]);
6353 mutex_lock(&ctx->uring_lock);
6354 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006355
6356 switch (opcode) {
6357 case IORING_REGISTER_BUFFERS:
6358 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6359 break;
6360 case IORING_UNREGISTER_BUFFERS:
6361 ret = -EINVAL;
6362 if (arg || nr_args)
6363 break;
6364 ret = io_sqe_buffer_unregister(ctx);
6365 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006366 case IORING_REGISTER_FILES:
6367 ret = io_sqe_files_register(ctx, arg, nr_args);
6368 break;
6369 case IORING_UNREGISTER_FILES:
6370 ret = -EINVAL;
6371 if (arg || nr_args)
6372 break;
6373 ret = io_sqe_files_unregister(ctx);
6374 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006375 case IORING_REGISTER_FILES_UPDATE:
6376 ret = io_sqe_files_update(ctx, arg, nr_args);
6377 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006378 case IORING_REGISTER_EVENTFD:
6379 ret = -EINVAL;
6380 if (nr_args != 1)
6381 break;
6382 ret = io_eventfd_register(ctx, arg);
6383 break;
6384 case IORING_UNREGISTER_EVENTFD:
6385 ret = -EINVAL;
6386 if (arg || nr_args)
6387 break;
6388 ret = io_eventfd_unregister(ctx);
6389 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006390 default:
6391 ret = -EINVAL;
6392 break;
6393 }
6394
Jens Axboe05f3fb32019-12-09 11:22:50 -07006395
6396 if (opcode != IORING_UNREGISTER_FILES &&
6397 opcode != IORING_REGISTER_FILES_UPDATE) {
6398 /* bring the ctx back to life */
6399 reinit_completion(&ctx->completions[0]);
6400 percpu_ref_reinit(&ctx->refs);
6401 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006402 return ret;
6403}
6404
6405SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6406 void __user *, arg, unsigned int, nr_args)
6407{
6408 struct io_ring_ctx *ctx;
6409 long ret = -EBADF;
6410 struct fd f;
6411
6412 f = fdget(fd);
6413 if (!f.file)
6414 return -EBADF;
6415
6416 ret = -EOPNOTSUPP;
6417 if (f.file->f_op != &io_uring_fops)
6418 goto out_fput;
6419
6420 ctx = f.file->private_data;
6421
6422 mutex_lock(&ctx->uring_lock);
6423 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6424 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006425 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6426 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006427out_fput:
6428 fdput(f);
6429 return ret;
6430}
6431
Jens Axboe2b188cc2019-01-07 10:46:33 -07006432static int __init io_uring_init(void)
6433{
Jens Axboed3656342019-12-18 09:50:26 -07006434 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006435 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6436 return 0;
6437};
6438__initcall(io_uring_init);