blob: 9ca12b900b425f1a470d0b7ec970c849fffdf147 [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 Axboef499a022019-12-02 16:28:46 -0700411struct io_async_connect {
412 struct sockaddr_storage address;
413};
414
Jens Axboe03b12302019-12-02 18:50:25 -0700415struct io_async_msghdr {
416 struct iovec fast_iov[UIO_FASTIOV];
417 struct iovec *iov;
418 struct sockaddr __user *uaddr;
419 struct msghdr msg;
420};
421
Jens Axboef67676d2019-12-02 11:03:47 -0700422struct io_async_rw {
423 struct iovec fast_iov[UIO_FASTIOV];
424 struct iovec *iov;
425 ssize_t nr_segs;
426 ssize_t size;
427};
428
Jens Axboe15b71ab2019-12-11 11:20:36 -0700429struct io_async_open {
430 struct filename *filename;
431};
432
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700433struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700434 union {
435 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700436 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700437 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700438 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700439 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700440 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700441};
442
Jens Axboe09bb8392019-03-13 12:39:28 -0600443/*
444 * NOTE! Each of the iocb union members has the file pointer
445 * as the first entry in their struct definition. So you can
446 * access the file pointer through any of the sub-structs,
447 * or directly as just 'ki_filp' in this struct.
448 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700449struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700450 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600451 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700452 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700453 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700454 struct io_accept accept;
455 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700456 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700457 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700458 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700459 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700460 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700461 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700462 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700463 struct io_fadvise fadvise;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700464 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700465
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700466 struct io_async_ctx *io;
Jens Axboee94f1412019-12-19 12:06:02 -0700467 union {
468 /*
469 * ring_file is only used in the submission path, and
470 * llist_node is only used for poll deferred completions
471 */
472 struct file *ring_file;
473 struct llist_node llist_node;
474 };
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300475 int ring_fd;
476 bool has_user;
477 bool in_async;
478 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700479 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700480
481 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700482 union {
483 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700484 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700485 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600486 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700487 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700488 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200489#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700490#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700491#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700492#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200493#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
494#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600495#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700496#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800497#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300498#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600499#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600500#define REQ_F_ISREG 2048 /* regular file */
501#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700502#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800503#define REQ_F_INFLIGHT 16384 /* on inflight list */
504#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700505#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboece35a472019-12-17 08:04:44 -0700506#define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */
Jens Axboeba042912019-12-25 16:33:42 -0700507#define REQ_F_CUR_POS 262144 /* read/write uses file position */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700508 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600509 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600510 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700511
Jens Axboefcb323c2019-10-24 12:39:47 -0600512 struct list_head inflight_entry;
513
Jens Axboe561fb042019-10-24 07:25:42 -0600514 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700515};
516
517#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700518#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700519
Jens Axboe9a56a232019-01-09 09:06:50 -0700520struct io_submit_state {
521 struct blk_plug plug;
522
523 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700524 * io_kiocb alloc cache
525 */
526 void *reqs[IO_IOPOLL_BATCH];
527 unsigned int free_reqs;
528 unsigned int cur_req;
529
530 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700531 * File reference cache
532 */
533 struct file *file;
534 unsigned int fd;
535 unsigned int has_refs;
536 unsigned int used_refs;
537 unsigned int ios_left;
538};
539
Jens Axboed3656342019-12-18 09:50:26 -0700540struct io_op_def {
541 /* needs req->io allocated for deferral/async */
542 unsigned async_ctx : 1;
543 /* needs current->mm setup, does mm access */
544 unsigned needs_mm : 1;
545 /* needs req->file assigned */
546 unsigned needs_file : 1;
547 /* needs req->file assigned IFF fd is >= 0 */
548 unsigned fd_non_neg : 1;
549 /* hash wq insertion if file is a regular file */
550 unsigned hash_reg_file : 1;
551 /* unbound wq insertion if file is a non-regular file */
552 unsigned unbound_nonreg_file : 1;
553};
554
555static const struct io_op_def io_op_defs[] = {
556 {
557 /* IORING_OP_NOP */
558 },
559 {
560 /* IORING_OP_READV */
561 .async_ctx = 1,
562 .needs_mm = 1,
563 .needs_file = 1,
564 .unbound_nonreg_file = 1,
565 },
566 {
567 /* IORING_OP_WRITEV */
568 .async_ctx = 1,
569 .needs_mm = 1,
570 .needs_file = 1,
571 .hash_reg_file = 1,
572 .unbound_nonreg_file = 1,
573 },
574 {
575 /* IORING_OP_FSYNC */
576 .needs_file = 1,
577 },
578 {
579 /* IORING_OP_READ_FIXED */
580 .needs_file = 1,
581 .unbound_nonreg_file = 1,
582 },
583 {
584 /* IORING_OP_WRITE_FIXED */
585 .needs_file = 1,
586 .hash_reg_file = 1,
587 .unbound_nonreg_file = 1,
588 },
589 {
590 /* IORING_OP_POLL_ADD */
591 .needs_file = 1,
592 .unbound_nonreg_file = 1,
593 },
594 {
595 /* IORING_OP_POLL_REMOVE */
596 },
597 {
598 /* IORING_OP_SYNC_FILE_RANGE */
599 .needs_file = 1,
600 },
601 {
602 /* IORING_OP_SENDMSG */
603 .async_ctx = 1,
604 .needs_mm = 1,
605 .needs_file = 1,
606 .unbound_nonreg_file = 1,
607 },
608 {
609 /* IORING_OP_RECVMSG */
610 .async_ctx = 1,
611 .needs_mm = 1,
612 .needs_file = 1,
613 .unbound_nonreg_file = 1,
614 },
615 {
616 /* IORING_OP_TIMEOUT */
617 .async_ctx = 1,
618 .needs_mm = 1,
619 },
620 {
621 /* IORING_OP_TIMEOUT_REMOVE */
622 },
623 {
624 /* IORING_OP_ACCEPT */
625 .needs_mm = 1,
626 .needs_file = 1,
627 .unbound_nonreg_file = 1,
628 },
629 {
630 /* IORING_OP_ASYNC_CANCEL */
631 },
632 {
633 /* IORING_OP_LINK_TIMEOUT */
634 .async_ctx = 1,
635 .needs_mm = 1,
636 },
637 {
638 /* IORING_OP_CONNECT */
639 .async_ctx = 1,
640 .needs_mm = 1,
641 .needs_file = 1,
642 .unbound_nonreg_file = 1,
643 },
644 {
645 /* IORING_OP_FALLOCATE */
646 .needs_file = 1,
647 },
648 {
649 /* IORING_OP_OPENAT */
650 .needs_file = 1,
651 .fd_non_neg = 1,
652 },
653 {
654 /* IORING_OP_CLOSE */
655 .needs_file = 1,
656 },
657 {
658 /* IORING_OP_FILES_UPDATE */
659 .needs_mm = 1,
660 },
661 {
662 /* IORING_OP_STATX */
663 .needs_mm = 1,
664 .needs_file = 1,
665 .fd_non_neg = 1,
666 },
Jens Axboe3a6820f2019-12-22 15:19:35 -0700667 {
668 /* IORING_OP_READ */
669 .needs_mm = 1,
670 .needs_file = 1,
671 .unbound_nonreg_file = 1,
672 },
673 {
674 /* IORING_OP_WRITE */
675 .needs_mm = 1,
676 .needs_file = 1,
677 .unbound_nonreg_file = 1,
678 },
Jens Axboe4840e412019-12-25 22:03:45 -0700679 {
680 /* IORING_OP_FADVISE */
681 .needs_file = 1,
682 },
Jens Axboed3656342019-12-18 09:50:26 -0700683};
684
Jens Axboe561fb042019-10-24 07:25:42 -0600685static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700686static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800687static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700688static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700689static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
690static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700691static int __io_sqe_files_update(struct io_ring_ctx *ctx,
692 struct io_uring_files_update *ip,
693 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600694
Jens Axboe2b188cc2019-01-07 10:46:33 -0700695static struct kmem_cache *req_cachep;
696
697static const struct file_operations io_uring_fops;
698
699struct sock *io_uring_get_socket(struct file *file)
700{
701#if defined(CONFIG_UNIX)
702 if (file->f_op == &io_uring_fops) {
703 struct io_ring_ctx *ctx = file->private_data;
704
705 return ctx->ring_sock->sk;
706 }
707#endif
708 return NULL;
709}
710EXPORT_SYMBOL(io_uring_get_socket);
711
712static void io_ring_ctx_ref_free(struct percpu_ref *ref)
713{
714 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
715
Jens Axboe206aefd2019-11-07 18:27:42 -0700716 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700717}
718
719static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
720{
721 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700722 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700723
724 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
725 if (!ctx)
726 return NULL;
727
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700728 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
729 if (!ctx->fallback_req)
730 goto err;
731
Jens Axboe206aefd2019-11-07 18:27:42 -0700732 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
733 if (!ctx->completions)
734 goto err;
735
Jens Axboe78076bb2019-12-04 19:56:40 -0700736 /*
737 * Use 5 bits less than the max cq entries, that should give us around
738 * 32 entries per hash list if totally full and uniformly spread.
739 */
740 hash_bits = ilog2(p->cq_entries);
741 hash_bits -= 5;
742 if (hash_bits <= 0)
743 hash_bits = 1;
744 ctx->cancel_hash_bits = hash_bits;
745 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
746 GFP_KERNEL);
747 if (!ctx->cancel_hash)
748 goto err;
749 __hash_init(ctx->cancel_hash, 1U << hash_bits);
750
Roman Gushchin21482892019-05-07 10:01:48 -0700751 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700752 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
753 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700754
755 ctx->flags = p->flags;
756 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700757 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700758 init_completion(&ctx->completions[0]);
759 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700760 mutex_init(&ctx->uring_lock);
761 init_waitqueue_head(&ctx->wait);
762 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700763 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700764 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600765 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600766 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600767 init_waitqueue_head(&ctx->inflight_wait);
768 spin_lock_init(&ctx->inflight_lock);
769 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700770 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700771err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700772 if (ctx->fallback_req)
773 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700774 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700775 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700776 kfree(ctx);
777 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700778}
779
Bob Liu9d858b22019-11-13 18:06:25 +0800780static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600781{
Jackie Liua197f662019-11-08 08:09:12 -0700782 struct io_ring_ctx *ctx = req->ctx;
783
Jens Axboe498ccd92019-10-25 10:04:25 -0600784 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
785 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600786}
787
Bob Liu9d858b22019-11-13 18:06:25 +0800788static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600789{
Bob Liu9d858b22019-11-13 18:06:25 +0800790 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
791 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600792
Bob Liu9d858b22019-11-13 18:06:25 +0800793 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600794}
795
796static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600797{
798 struct io_kiocb *req;
799
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600800 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800801 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600802 list_del_init(&req->list);
803 return req;
804 }
805
806 return NULL;
807}
808
Jens Axboe5262f562019-09-17 12:26:57 -0600809static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
810{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600811 struct io_kiocb *req;
812
813 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700814 if (req) {
815 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
816 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800817 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700818 list_del_init(&req->list);
819 return req;
820 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600821 }
822
823 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600824}
825
Jens Axboede0617e2019-04-06 21:51:27 -0600826static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700827{
Hristo Venev75b28af2019-08-26 17:23:46 +0000828 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700829
Hristo Venev75b28af2019-08-26 17:23:46 +0000830 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700831 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000832 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700833
Jens Axboe2b188cc2019-01-07 10:46:33 -0700834 if (wq_has_sleeper(&ctx->cq_wait)) {
835 wake_up_interruptible(&ctx->cq_wait);
836 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
837 }
838 }
839}
840
Jens Axboe94ae5e72019-11-14 19:39:52 -0700841static inline bool io_prep_async_work(struct io_kiocb *req,
842 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600843{
Jens Axboed3656342019-12-18 09:50:26 -0700844 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600845 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600846
Jens Axboed3656342019-12-18 09:50:26 -0700847 if (req->flags & REQ_F_ISREG) {
848 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700849 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700850 } else {
851 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700852 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600853 }
Jens Axboed3656342019-12-18 09:50:26 -0700854 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700855 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600856
Jens Axboe94ae5e72019-11-14 19:39:52 -0700857 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600858 return do_hashed;
859}
860
Jackie Liua197f662019-11-08 08:09:12 -0700861static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600862{
Jackie Liua197f662019-11-08 08:09:12 -0700863 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700864 struct io_kiocb *link;
865 bool do_hashed;
866
867 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600868
869 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
870 req->flags);
871 if (!do_hashed) {
872 io_wq_enqueue(ctx->io_wq, &req->work);
873 } else {
874 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
875 file_inode(req->file));
876 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700877
878 if (link)
879 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600880}
881
Jens Axboe5262f562019-09-17 12:26:57 -0600882static void io_kill_timeout(struct io_kiocb *req)
883{
884 int ret;
885
Jens Axboe2d283902019-12-04 11:08:05 -0700886 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600887 if (ret != -1) {
888 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600889 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700890 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800891 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600892 }
893}
894
895static void io_kill_timeouts(struct io_ring_ctx *ctx)
896{
897 struct io_kiocb *req, *tmp;
898
899 spin_lock_irq(&ctx->completion_lock);
900 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
901 io_kill_timeout(req);
902 spin_unlock_irq(&ctx->completion_lock);
903}
904
Jens Axboede0617e2019-04-06 21:51:27 -0600905static void io_commit_cqring(struct io_ring_ctx *ctx)
906{
907 struct io_kiocb *req;
908
Jens Axboe5262f562019-09-17 12:26:57 -0600909 while ((req = io_get_timeout_req(ctx)) != NULL)
910 io_kill_timeout(req);
911
Jens Axboede0617e2019-04-06 21:51:27 -0600912 __io_commit_cqring(ctx);
913
914 while ((req = io_get_deferred_req(ctx)) != NULL) {
915 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700916 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600917 }
918}
919
Jens Axboe2b188cc2019-01-07 10:46:33 -0700920static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
921{
Hristo Venev75b28af2019-08-26 17:23:46 +0000922 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700923 unsigned tail;
924
925 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200926 /*
927 * writes to the cq entry need to come after reading head; the
928 * control dependency is enough as we're using WRITE_ONCE to
929 * fill the cq entry
930 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000931 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700932 return NULL;
933
934 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000935 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700936}
937
Jens Axboe8c838782019-03-12 15:48:16 -0600938static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
939{
940 if (waitqueue_active(&ctx->wait))
941 wake_up(&ctx->wait);
942 if (waitqueue_active(&ctx->sqo_wait))
943 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600944 if (ctx->cq_ev_fd)
945 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600946}
947
Jens Axboec4a2ed72019-11-21 21:01:26 -0700948/* Returns true if there are no backlogged entries after the flush */
949static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700950{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700951 struct io_rings *rings = ctx->rings;
952 struct io_uring_cqe *cqe;
953 struct io_kiocb *req;
954 unsigned long flags;
955 LIST_HEAD(list);
956
957 if (!force) {
958 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700959 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700960 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
961 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700962 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700963 }
964
965 spin_lock_irqsave(&ctx->completion_lock, flags);
966
967 /* if force is set, the ring is going away. always drop after that */
968 if (force)
969 ctx->cq_overflow_flushed = true;
970
Jens Axboec4a2ed72019-11-21 21:01:26 -0700971 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700972 while (!list_empty(&ctx->cq_overflow_list)) {
973 cqe = io_get_cqring(ctx);
974 if (!cqe && !force)
975 break;
976
977 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
978 list);
979 list_move(&req->list, &list);
980 if (cqe) {
981 WRITE_ONCE(cqe->user_data, req->user_data);
982 WRITE_ONCE(cqe->res, req->result);
983 WRITE_ONCE(cqe->flags, 0);
984 } else {
985 WRITE_ONCE(ctx->rings->cq_overflow,
986 atomic_inc_return(&ctx->cached_cq_overflow));
987 }
988 }
989
990 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -0700991 if (cqe) {
992 clear_bit(0, &ctx->sq_check_overflow);
993 clear_bit(0, &ctx->cq_check_overflow);
994 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700995 spin_unlock_irqrestore(&ctx->completion_lock, flags);
996 io_cqring_ev_posted(ctx);
997
998 while (!list_empty(&list)) {
999 req = list_first_entry(&list, struct io_kiocb, list);
1000 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001001 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001002 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001003
1004 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001005}
1006
Jens Axboe78e19bb2019-11-06 15:21:34 -07001007static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001008{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001009 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001010 struct io_uring_cqe *cqe;
1011
Jens Axboe78e19bb2019-11-06 15:21:34 -07001012 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001013
Jens Axboe2b188cc2019-01-07 10:46:33 -07001014 /*
1015 * If we can't get a cq entry, userspace overflowed the
1016 * submission (by quite a lot). Increment the overflow count in
1017 * the ring.
1018 */
1019 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001020 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001021 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001022 WRITE_ONCE(cqe->res, res);
1023 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001024 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001025 WRITE_ONCE(ctx->rings->cq_overflow,
1026 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001027 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001028 if (list_empty(&ctx->cq_overflow_list)) {
1029 set_bit(0, &ctx->sq_check_overflow);
1030 set_bit(0, &ctx->cq_check_overflow);
1031 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001032 refcount_inc(&req->refs);
1033 req->result = res;
1034 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001035 }
1036}
1037
Jens Axboe78e19bb2019-11-06 15:21:34 -07001038static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001039{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001040 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001041 unsigned long flags;
1042
1043 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001044 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001045 io_commit_cqring(ctx);
1046 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1047
Jens Axboe8c838782019-03-12 15:48:16 -06001048 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049}
1050
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001051static inline bool io_is_fallback_req(struct io_kiocb *req)
1052{
1053 return req == (struct io_kiocb *)
1054 ((unsigned long) req->ctx->fallback_req & ~1UL);
1055}
1056
1057static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1058{
1059 struct io_kiocb *req;
1060
1061 req = ctx->fallback_req;
1062 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1063 return req;
1064
1065 return NULL;
1066}
1067
Jens Axboe2579f912019-01-09 09:10:43 -07001068static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1069 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001070{
Jens Axboefd6fab22019-03-14 16:30:06 -06001071 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001072 struct io_kiocb *req;
1073
1074 if (!percpu_ref_tryget(&ctx->refs))
1075 return NULL;
1076
Jens Axboe2579f912019-01-09 09:10:43 -07001077 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001078 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001079 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001080 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001081 } else if (!state->free_reqs) {
1082 size_t sz;
1083 int ret;
1084
1085 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001086 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1087
1088 /*
1089 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1090 * retry single alloc to be on the safe side.
1091 */
1092 if (unlikely(ret <= 0)) {
1093 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1094 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001095 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001096 ret = 1;
1097 }
Jens Axboe2579f912019-01-09 09:10:43 -07001098 state->free_reqs = ret - 1;
1099 state->cur_req = 1;
1100 req = state->reqs[0];
1101 } else {
1102 req = state->reqs[state->cur_req];
1103 state->free_reqs--;
1104 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001105 }
1106
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001107got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001108 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001109 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001110 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001111 req->ctx = ctx;
1112 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001113 /* one is dropped after submission, the other at completion */
1114 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001115 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001116 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001117 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001118fallback:
1119 req = io_get_fallback_req(ctx);
1120 if (req)
1121 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001122 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001123 return NULL;
1124}
1125
Jens Axboedef596e2019-01-09 08:59:42 -07001126static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
1127{
1128 if (*nr) {
1129 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +03001130 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001131 percpu_ref_put_many(&ctx->file_data->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -07001132 *nr = 0;
1133 }
1134}
1135
Jens Axboe9e645e112019-05-10 16:07:28 -06001136static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001137{
Jens Axboefcb323c2019-10-24 12:39:47 -06001138 struct io_ring_ctx *ctx = req->ctx;
1139
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001140 if (req->io)
1141 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001142 if (req->file) {
1143 if (req->flags & REQ_F_FIXED_FILE)
1144 percpu_ref_put(&ctx->file_data->refs);
1145 else
1146 fput(req->file);
1147 }
Jens Axboefcb323c2019-10-24 12:39:47 -06001148 if (req->flags & REQ_F_INFLIGHT) {
1149 unsigned long flags;
1150
1151 spin_lock_irqsave(&ctx->inflight_lock, flags);
1152 list_del(&req->inflight_entry);
1153 if (waitqueue_active(&ctx->inflight_wait))
1154 wake_up(&ctx->inflight_wait);
1155 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1156 }
1157 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001158 if (likely(!io_is_fallback_req(req)))
1159 kmem_cache_free(req_cachep, req);
1160 else
1161 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -06001162}
1163
Jackie Liua197f662019-11-08 08:09:12 -07001164static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001165{
Jackie Liua197f662019-11-08 08:09:12 -07001166 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001167 int ret;
1168
Jens Axboe2d283902019-12-04 11:08:05 -07001169 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001170 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001171 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001172 io_commit_cqring(ctx);
1173 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001174 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001175 return true;
1176 }
1177
1178 return false;
1179}
1180
Jens Axboeba816ad2019-09-28 11:36:45 -06001181static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001182{
Jens Axboe2665abf2019-11-05 12:40:47 -07001183 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001184 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001185
Jens Axboe4d7dd462019-11-20 13:03:52 -07001186 /* Already got next link */
1187 if (req->flags & REQ_F_LINK_NEXT)
1188 return;
1189
Jens Axboe9e645e112019-05-10 16:07:28 -06001190 /*
1191 * The list should never be empty when we are called here. But could
1192 * potentially happen if the chain is messed up, check to be on the
1193 * safe side.
1194 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001195 while (!list_empty(&req->link_list)) {
1196 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1197 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001198
Pavel Begunkov44932332019-12-05 16:16:35 +03001199 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1200 (nxt->flags & REQ_F_TIMEOUT))) {
1201 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001202 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001203 req->flags &= ~REQ_F_LINK_TIMEOUT;
1204 continue;
1205 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001206
Pavel Begunkov44932332019-12-05 16:16:35 +03001207 list_del_init(&req->link_list);
1208 if (!list_empty(&nxt->link_list))
1209 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001210 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001211 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001212 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001213
Jens Axboe4d7dd462019-11-20 13:03:52 -07001214 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001215 if (wake_ev)
1216 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001217}
1218
1219/*
1220 * Called if REQ_F_LINK is set, and we fail the head request
1221 */
1222static void io_fail_links(struct io_kiocb *req)
1223{
Jens Axboe2665abf2019-11-05 12:40:47 -07001224 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001225 unsigned long flags;
1226
1227 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001228
1229 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001230 struct io_kiocb *link = list_first_entry(&req->link_list,
1231 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001232
Pavel Begunkov44932332019-12-05 16:16:35 +03001233 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001234 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001235
1236 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001237 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001238 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001239 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001240 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001241 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001242 }
Jens Axboe5d960722019-11-19 15:31:28 -07001243 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001244 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001245
1246 io_commit_cqring(ctx);
1247 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1248 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001249}
1250
Jens Axboe4d7dd462019-11-20 13:03:52 -07001251static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001252{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001253 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001254 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001255
Jens Axboe9e645e112019-05-10 16:07:28 -06001256 /*
1257 * If LINK is set, we have dependent requests in this chain. If we
1258 * didn't fail this request, queue the first one up, moving any other
1259 * dependencies to the next request. In case of failure, fail the rest
1260 * of the chain.
1261 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001262 if (req->flags & REQ_F_FAIL_LINK) {
1263 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001264 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1265 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001266 struct io_ring_ctx *ctx = req->ctx;
1267 unsigned long flags;
1268
1269 /*
1270 * If this is a timeout link, we could be racing with the
1271 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001272 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001273 */
1274 spin_lock_irqsave(&ctx->completion_lock, flags);
1275 io_req_link_next(req, nxt);
1276 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1277 } else {
1278 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001279 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001280}
Jens Axboe9e645e112019-05-10 16:07:28 -06001281
Jackie Liuc69f8db2019-11-09 11:00:08 +08001282static void io_free_req(struct io_kiocb *req)
1283{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001284 struct io_kiocb *nxt = NULL;
1285
1286 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001287 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001288
1289 if (nxt)
1290 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001291}
1292
Jens Axboeba816ad2019-09-28 11:36:45 -06001293/*
1294 * Drop reference to request, return next in chain (if there is one) if this
1295 * was the last reference to this request.
1296 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001297__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001298static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001299{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001300 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001301
Jens Axboee65ef562019-03-12 10:16:44 -06001302 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001303 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001304}
1305
Jens Axboe2b188cc2019-01-07 10:46:33 -07001306static void io_put_req(struct io_kiocb *req)
1307{
Jens Axboedef596e2019-01-09 08:59:42 -07001308 if (refcount_dec_and_test(&req->refs))
1309 io_free_req(req);
1310}
1311
Jens Axboe978db572019-11-14 22:39:04 -07001312/*
1313 * Must only be used if we don't need to care about links, usually from
1314 * within the completion handling itself.
1315 */
1316static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001317{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001318 /* drop both submit and complete references */
1319 if (refcount_sub_and_test(2, &req->refs))
1320 __io_free_req(req);
1321}
1322
Jens Axboe978db572019-11-14 22:39:04 -07001323static void io_double_put_req(struct io_kiocb *req)
1324{
1325 /* drop both submit and complete references */
1326 if (refcount_sub_and_test(2, &req->refs))
1327 io_free_req(req);
1328}
1329
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001330static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001331{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001332 struct io_rings *rings = ctx->rings;
1333
Jens Axboead3eb2c2019-12-18 17:12:20 -07001334 if (test_bit(0, &ctx->cq_check_overflow)) {
1335 /*
1336 * noflush == true is from the waitqueue handler, just ensure
1337 * we wake up the task, and the next invocation will flush the
1338 * entries. We cannot safely to it from here.
1339 */
1340 if (noflush && !list_empty(&ctx->cq_overflow_list))
1341 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001342
Jens Axboead3eb2c2019-12-18 17:12:20 -07001343 io_cqring_overflow_flush(ctx, false);
1344 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001345
Jens Axboea3a0e432019-08-20 11:03:11 -06001346 /* See comment at the top of this file */
1347 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001348 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001349}
1350
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001351static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1352{
1353 struct io_rings *rings = ctx->rings;
1354
1355 /* make sure SQ entry isn't read before tail */
1356 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1357}
1358
Jens Axboee94f1412019-12-19 12:06:02 -07001359static inline bool io_req_multi_free(struct io_kiocb *req)
1360{
1361 /*
1362 * If we're not using fixed files, we have to pair the completion part
1363 * with the file put. Use regular completions for those, only batch
1364 * free for fixed file and non-linked commands.
1365 */
1366 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) == REQ_F_FIXED_FILE)
1367 && !io_is_fallback_req(req) && !req->io)
1368 return true;
1369
1370 return false;
1371}
1372
Jens Axboedef596e2019-01-09 08:59:42 -07001373/*
1374 * Find and free completed poll iocbs
1375 */
1376static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1377 struct list_head *done)
1378{
1379 void *reqs[IO_IOPOLL_BATCH];
1380 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001381 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001382
Jens Axboe09bb8392019-03-13 12:39:28 -06001383 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001384 while (!list_empty(done)) {
1385 req = list_first_entry(done, struct io_kiocb, list);
1386 list_del(&req->list);
1387
Jens Axboe78e19bb2019-11-06 15:21:34 -07001388 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001389 (*nr_events)++;
1390
Jens Axboe09bb8392019-03-13 12:39:28 -06001391 if (refcount_dec_and_test(&req->refs)) {
Jens Axboee94f1412019-12-19 12:06:02 -07001392 if (io_req_multi_free(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001393 reqs[to_free++] = req;
1394 if (to_free == ARRAY_SIZE(reqs))
1395 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001396 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001397 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001398 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001399 }
Jens Axboedef596e2019-01-09 08:59:42 -07001400 }
Jens Axboedef596e2019-01-09 08:59:42 -07001401
Jens Axboe09bb8392019-03-13 12:39:28 -06001402 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001403 io_free_req_many(ctx, reqs, &to_free);
1404}
1405
1406static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1407 long min)
1408{
1409 struct io_kiocb *req, *tmp;
1410 LIST_HEAD(done);
1411 bool spin;
1412 int ret;
1413
1414 /*
1415 * Only spin for completions if we don't have multiple devices hanging
1416 * off our complete list, and we're under the requested amount.
1417 */
1418 spin = !ctx->poll_multi_file && *nr_events < min;
1419
1420 ret = 0;
1421 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001422 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001423
1424 /*
1425 * Move completed entries to our local list. If we find a
1426 * request that requires polling, break out and complete
1427 * the done list first, if we have entries there.
1428 */
1429 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1430 list_move_tail(&req->list, &done);
1431 continue;
1432 }
1433 if (!list_empty(&done))
1434 break;
1435
1436 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1437 if (ret < 0)
1438 break;
1439
1440 if (ret && spin)
1441 spin = false;
1442 ret = 0;
1443 }
1444
1445 if (!list_empty(&done))
1446 io_iopoll_complete(ctx, nr_events, &done);
1447
1448 return ret;
1449}
1450
1451/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001452 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001453 * non-spinning poll check - we'll still enter the driver poll loop, but only
1454 * as a non-spinning completion check.
1455 */
1456static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1457 long min)
1458{
Jens Axboe08f54392019-08-21 22:19:11 -06001459 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001460 int ret;
1461
1462 ret = io_do_iopoll(ctx, nr_events, min);
1463 if (ret < 0)
1464 return ret;
1465 if (!min || *nr_events >= min)
1466 return 0;
1467 }
1468
1469 return 1;
1470}
1471
1472/*
1473 * We can't just wait for polled events to come to us, we have to actively
1474 * find and complete them.
1475 */
1476static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1477{
1478 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1479 return;
1480
1481 mutex_lock(&ctx->uring_lock);
1482 while (!list_empty(&ctx->poll_list)) {
1483 unsigned int nr_events = 0;
1484
1485 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001486
1487 /*
1488 * Ensure we allow local-to-the-cpu processing to take place,
1489 * in this case we need to ensure that we reap all events.
1490 */
1491 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001492 }
1493 mutex_unlock(&ctx->uring_lock);
1494}
1495
Jens Axboe2b2ed972019-10-25 10:06:15 -06001496static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1497 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001498{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001499 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001500
1501 do {
1502 int tmin = 0;
1503
Jens Axboe500f9fb2019-08-19 12:15:59 -06001504 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001505 * Don't enter poll loop if we already have events pending.
1506 * If we do, we can potentially be spinning for commands that
1507 * already triggered a CQE (eg in error).
1508 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001509 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001510 break;
1511
1512 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001513 * If a submit got punted to a workqueue, we can have the
1514 * application entering polling for a command before it gets
1515 * issued. That app will hold the uring_lock for the duration
1516 * of the poll right here, so we need to take a breather every
1517 * now and then to ensure that the issue has a chance to add
1518 * the poll to the issued list. Otherwise we can spin here
1519 * forever, while the workqueue is stuck trying to acquire the
1520 * very same mutex.
1521 */
1522 if (!(++iters & 7)) {
1523 mutex_unlock(&ctx->uring_lock);
1524 mutex_lock(&ctx->uring_lock);
1525 }
1526
Jens Axboedef596e2019-01-09 08:59:42 -07001527 if (*nr_events < min)
1528 tmin = min - *nr_events;
1529
1530 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1531 if (ret <= 0)
1532 break;
1533 ret = 0;
1534 } while (min && !*nr_events && !need_resched());
1535
Jens Axboe2b2ed972019-10-25 10:06:15 -06001536 return ret;
1537}
1538
1539static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1540 long min)
1541{
1542 int ret;
1543
1544 /*
1545 * We disallow the app entering submit/complete with polling, but we
1546 * still need to lock the ring to prevent racing with polled issue
1547 * that got punted to a workqueue.
1548 */
1549 mutex_lock(&ctx->uring_lock);
1550 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001551 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001552 return ret;
1553}
1554
Jens Axboe491381ce2019-10-17 09:20:46 -06001555static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001556{
Jens Axboe491381ce2019-10-17 09:20:46 -06001557 /*
1558 * Tell lockdep we inherited freeze protection from submission
1559 * thread.
1560 */
1561 if (req->flags & REQ_F_ISREG) {
1562 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001563
Jens Axboe491381ce2019-10-17 09:20:46 -06001564 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001565 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001566 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001567}
1568
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001569static inline void req_set_fail_links(struct io_kiocb *req)
1570{
1571 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1572 req->flags |= REQ_F_FAIL_LINK;
1573}
1574
Jens Axboeba816ad2019-09-28 11:36:45 -06001575static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001576{
Jens Axboe9adbd452019-12-20 08:45:55 -07001577 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001578
Jens Axboe491381ce2019-10-17 09:20:46 -06001579 if (kiocb->ki_flags & IOCB_WRITE)
1580 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001581
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001582 if (res != req->result)
1583 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001584 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001585}
1586
1587static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1588{
Jens Axboe9adbd452019-12-20 08:45:55 -07001589 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001590
1591 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001592 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001593}
1594
Jens Axboeba816ad2019-09-28 11:36:45 -06001595static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1596{
Jens Axboe9adbd452019-12-20 08:45:55 -07001597 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001598 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001599
1600 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001601 io_put_req_find_next(req, &nxt);
1602
1603 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001604}
1605
Jens Axboedef596e2019-01-09 08:59:42 -07001606static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1607{
Jens Axboe9adbd452019-12-20 08:45:55 -07001608 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001609
Jens Axboe491381ce2019-10-17 09:20:46 -06001610 if (kiocb->ki_flags & IOCB_WRITE)
1611 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001612
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001613 if (res != req->result)
1614 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001615 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001616 if (res != -EAGAIN)
1617 req->flags |= REQ_F_IOPOLL_COMPLETED;
1618}
1619
1620/*
1621 * After the iocb has been issued, it's safe to be found on the poll list.
1622 * Adding the kiocb to the list AFTER submission ensures that we don't
1623 * find it from a io_iopoll_getevents() thread before the issuer is done
1624 * accessing the kiocb cookie.
1625 */
1626static void io_iopoll_req_issued(struct io_kiocb *req)
1627{
1628 struct io_ring_ctx *ctx = req->ctx;
1629
1630 /*
1631 * Track whether we have multiple files in our lists. This will impact
1632 * how we do polling eventually, not spinning if we're on potentially
1633 * different devices.
1634 */
1635 if (list_empty(&ctx->poll_list)) {
1636 ctx->poll_multi_file = false;
1637 } else if (!ctx->poll_multi_file) {
1638 struct io_kiocb *list_req;
1639
1640 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1641 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001642 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001643 ctx->poll_multi_file = true;
1644 }
1645
1646 /*
1647 * For fast devices, IO may have already completed. If it has, add
1648 * it to the front so we find it first.
1649 */
1650 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1651 list_add(&req->list, &ctx->poll_list);
1652 else
1653 list_add_tail(&req->list, &ctx->poll_list);
1654}
1655
Jens Axboe3d6770f2019-04-13 11:50:54 -06001656static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001657{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001658 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001659 int diff = state->has_refs - state->used_refs;
1660
1661 if (diff)
1662 fput_many(state->file, diff);
1663 state->file = NULL;
1664 }
1665}
1666
1667/*
1668 * Get as many references to a file as we have IOs left in this submission,
1669 * assuming most submissions are for one file, or at least that each file
1670 * has more than one submission.
1671 */
1672static struct file *io_file_get(struct io_submit_state *state, int fd)
1673{
1674 if (!state)
1675 return fget(fd);
1676
1677 if (state->file) {
1678 if (state->fd == fd) {
1679 state->used_refs++;
1680 state->ios_left--;
1681 return state->file;
1682 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001683 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001684 }
1685 state->file = fget_many(fd, state->ios_left);
1686 if (!state->file)
1687 return NULL;
1688
1689 state->fd = fd;
1690 state->has_refs = state->ios_left;
1691 state->used_refs = 1;
1692 state->ios_left--;
1693 return state->file;
1694}
1695
Jens Axboe2b188cc2019-01-07 10:46:33 -07001696/*
1697 * If we tracked the file through the SCM inflight mechanism, we could support
1698 * any file. For now, just ensure that anything potentially problematic is done
1699 * inline.
1700 */
1701static bool io_file_supports_async(struct file *file)
1702{
1703 umode_t mode = file_inode(file)->i_mode;
1704
Jens Axboe10d59342019-12-09 20:16:22 -07001705 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001706 return true;
1707 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1708 return true;
1709
1710 return false;
1711}
1712
Jens Axboe3529d8c2019-12-19 18:24:38 -07001713static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1714 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001715{
Jens Axboedef596e2019-01-09 08:59:42 -07001716 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001717 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001718 unsigned ioprio;
1719 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001720
Jens Axboe09bb8392019-03-13 12:39:28 -06001721 if (!req->file)
1722 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001723
Jens Axboe491381ce2019-10-17 09:20:46 -06001724 if (S_ISREG(file_inode(req->file)->i_mode))
1725 req->flags |= REQ_F_ISREG;
1726
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001728 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1729 req->flags |= REQ_F_CUR_POS;
1730 kiocb->ki_pos = req->file->f_pos;
1731 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1733 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1734
1735 ioprio = READ_ONCE(sqe->ioprio);
1736 if (ioprio) {
1737 ret = ioprio_check_cap(ioprio);
1738 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001739 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001740
1741 kiocb->ki_ioprio = ioprio;
1742 } else
1743 kiocb->ki_ioprio = get_current_ioprio();
1744
1745 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1746 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001747 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001748
1749 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001750 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1751 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001752 req->flags |= REQ_F_NOWAIT;
1753
1754 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001755 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001756
Jens Axboedef596e2019-01-09 08:59:42 -07001757 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001758 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1759 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001760 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001761
Jens Axboedef596e2019-01-09 08:59:42 -07001762 kiocb->ki_flags |= IOCB_HIPRI;
1763 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001764 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001765 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001766 if (kiocb->ki_flags & IOCB_HIPRI)
1767 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001768 kiocb->ki_complete = io_complete_rw;
1769 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001770
Jens Axboe3529d8c2019-12-19 18:24:38 -07001771 req->rw.addr = READ_ONCE(sqe->addr);
1772 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001773 /* we own ->private, reuse it for the buffer index */
1774 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001775 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001776 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001777}
1778
1779static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1780{
1781 switch (ret) {
1782 case -EIOCBQUEUED:
1783 break;
1784 case -ERESTARTSYS:
1785 case -ERESTARTNOINTR:
1786 case -ERESTARTNOHAND:
1787 case -ERESTART_RESTARTBLOCK:
1788 /*
1789 * We can't just restart the syscall, since previously
1790 * submitted sqes may already be in progress. Just fail this
1791 * IO with EINTR.
1792 */
1793 ret = -EINTR;
1794 /* fall through */
1795 default:
1796 kiocb->ki_complete(kiocb, ret, 0);
1797 }
1798}
1799
Jens Axboeba816ad2019-09-28 11:36:45 -06001800static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1801 bool in_async)
1802{
Jens Axboeba042912019-12-25 16:33:42 -07001803 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1804
1805 if (req->flags & REQ_F_CUR_POS)
1806 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001807 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001808 *nxt = __io_complete_rw(kiocb, ret);
1809 else
1810 io_rw_done(kiocb, ret);
1811}
1812
Jens Axboe9adbd452019-12-20 08:45:55 -07001813static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001814 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001815{
Jens Axboe9adbd452019-12-20 08:45:55 -07001816 struct io_ring_ctx *ctx = req->ctx;
1817 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001818 struct io_mapped_ubuf *imu;
1819 unsigned index, buf_index;
1820 size_t offset;
1821 u64 buf_addr;
1822
1823 /* attempt to use fixed buffers without having provided iovecs */
1824 if (unlikely(!ctx->user_bufs))
1825 return -EFAULT;
1826
Jens Axboe9adbd452019-12-20 08:45:55 -07001827 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001828 if (unlikely(buf_index >= ctx->nr_user_bufs))
1829 return -EFAULT;
1830
1831 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1832 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001833 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001834
1835 /* overflow */
1836 if (buf_addr + len < buf_addr)
1837 return -EFAULT;
1838 /* not inside the mapped region */
1839 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1840 return -EFAULT;
1841
1842 /*
1843 * May not be a start of buffer, set size appropriately
1844 * and advance us to the beginning.
1845 */
1846 offset = buf_addr - imu->ubuf;
1847 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001848
1849 if (offset) {
1850 /*
1851 * Don't use iov_iter_advance() here, as it's really slow for
1852 * using the latter parts of a big fixed buffer - it iterates
1853 * over each segment manually. We can cheat a bit here, because
1854 * we know that:
1855 *
1856 * 1) it's a BVEC iter, we set it up
1857 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1858 * first and last bvec
1859 *
1860 * So just find our index, and adjust the iterator afterwards.
1861 * If the offset is within the first bvec (or the whole first
1862 * bvec, just use iov_iter_advance(). This makes it easier
1863 * since we can just skip the first segment, which may not
1864 * be PAGE_SIZE aligned.
1865 */
1866 const struct bio_vec *bvec = imu->bvec;
1867
1868 if (offset <= bvec->bv_len) {
1869 iov_iter_advance(iter, offset);
1870 } else {
1871 unsigned long seg_skip;
1872
1873 /* skip first vec */
1874 offset -= bvec->bv_len;
1875 seg_skip = 1 + (offset >> PAGE_SHIFT);
1876
1877 iter->bvec = bvec + seg_skip;
1878 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001879 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001880 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001881 }
1882 }
1883
Jens Axboe5e559562019-11-13 16:12:46 -07001884 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001885}
1886
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001887static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1888 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001889{
Jens Axboe9adbd452019-12-20 08:45:55 -07001890 void __user *buf = u64_to_user_ptr(req->rw.addr);
1891 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001892 u8 opcode;
1893
Jens Axboed625c6e2019-12-17 19:53:05 -07001894 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001895 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001896 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001897 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001898 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899
Jens Axboe9adbd452019-12-20 08:45:55 -07001900 /* buffer index only valid with fixed read/write */
1901 if (req->rw.kiocb.private)
1902 return -EINVAL;
1903
Jens Axboe3a6820f2019-12-22 15:19:35 -07001904 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1905 ssize_t ret;
1906 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1907 *iovec = NULL;
1908 return ret;
1909 }
1910
Jens Axboef67676d2019-12-02 11:03:47 -07001911 if (req->io) {
1912 struct io_async_rw *iorw = &req->io->rw;
1913
1914 *iovec = iorw->iov;
1915 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1916 if (iorw->iov == iorw->fast_iov)
1917 *iovec = NULL;
1918 return iorw->size;
1919 }
1920
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001921 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001922 return -EFAULT;
1923
1924#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001925 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001926 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1927 iovec, iter);
1928#endif
1929
1930 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1931}
1932
Jens Axboe32960612019-09-23 11:05:34 -06001933/*
1934 * For files that don't have ->read_iter() and ->write_iter(), handle them
1935 * by looping over ->read() or ->write() manually.
1936 */
1937static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1938 struct iov_iter *iter)
1939{
1940 ssize_t ret = 0;
1941
1942 /*
1943 * Don't support polled IO through this interface, and we can't
1944 * support non-blocking either. For the latter, this just causes
1945 * the kiocb to be handled from an async context.
1946 */
1947 if (kiocb->ki_flags & IOCB_HIPRI)
1948 return -EOPNOTSUPP;
1949 if (kiocb->ki_flags & IOCB_NOWAIT)
1950 return -EAGAIN;
1951
1952 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001953 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001954 ssize_t nr;
1955
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001956 if (!iov_iter_is_bvec(iter)) {
1957 iovec = iov_iter_iovec(iter);
1958 } else {
1959 /* fixed buffers import bvec */
1960 iovec.iov_base = kmap(iter->bvec->bv_page)
1961 + iter->iov_offset;
1962 iovec.iov_len = min(iter->count,
1963 iter->bvec->bv_len - iter->iov_offset);
1964 }
1965
Jens Axboe32960612019-09-23 11:05:34 -06001966 if (rw == READ) {
1967 nr = file->f_op->read(file, iovec.iov_base,
1968 iovec.iov_len, &kiocb->ki_pos);
1969 } else {
1970 nr = file->f_op->write(file, iovec.iov_base,
1971 iovec.iov_len, &kiocb->ki_pos);
1972 }
1973
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001974 if (iov_iter_is_bvec(iter))
1975 kunmap(iter->bvec->bv_page);
1976
Jens Axboe32960612019-09-23 11:05:34 -06001977 if (nr < 0) {
1978 if (!ret)
1979 ret = nr;
1980 break;
1981 }
1982 ret += nr;
1983 if (nr != iovec.iov_len)
1984 break;
1985 iov_iter_advance(iter, nr);
1986 }
1987
1988 return ret;
1989}
1990
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001991static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07001992 struct iovec *iovec, struct iovec *fast_iov,
1993 struct iov_iter *iter)
1994{
1995 req->io->rw.nr_segs = iter->nr_segs;
1996 req->io->rw.size = io_size;
1997 req->io->rw.iov = iovec;
1998 if (!req->io->rw.iov) {
1999 req->io->rw.iov = req->io->rw.fast_iov;
2000 memcpy(req->io->rw.iov, fast_iov,
2001 sizeof(struct iovec) * iter->nr_segs);
2002 }
2003}
2004
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002005static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002006{
Jens Axboed3656342019-12-18 09:50:26 -07002007 if (!io_op_defs[req->opcode].async_ctx)
2008 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002009 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002010 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002011}
2012
2013static void io_rw_async(struct io_wq_work **workptr)
2014{
2015 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2016 struct iovec *iov = NULL;
2017
2018 if (req->io->rw.iov != req->io->rw.fast_iov)
2019 iov = req->io->rw.iov;
2020 io_wq_submit_work(workptr);
2021 kfree(iov);
2022}
2023
2024static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2025 struct iovec *iovec, struct iovec *fast_iov,
2026 struct iov_iter *iter)
2027{
Jens Axboe74566df2020-01-13 19:23:24 -07002028 if (req->opcode == IORING_OP_READ_FIXED ||
2029 req->opcode == IORING_OP_WRITE_FIXED)
2030 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002031 if (!req->io && io_alloc_async_ctx(req))
2032 return -ENOMEM;
2033
2034 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2035 req->work.func = io_rw_async;
2036 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002037}
2038
Jens Axboe3529d8c2019-12-19 18:24:38 -07002039static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2040 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002041{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002042 struct io_async_ctx *io;
2043 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002044 ssize_t ret;
2045
Jens Axboe3529d8c2019-12-19 18:24:38 -07002046 ret = io_prep_rw(req, sqe, force_nonblock);
2047 if (ret)
2048 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002049
Jens Axboe3529d8c2019-12-19 18:24:38 -07002050 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2051 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002052
Jens Axboe3529d8c2019-12-19 18:24:38 -07002053 if (!req->io)
2054 return 0;
2055
2056 io = req->io;
2057 io->rw.iov = io->rw.fast_iov;
2058 req->io = NULL;
2059 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2060 req->io = io;
2061 if (ret < 0)
2062 return ret;
2063
2064 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2065 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002066}
2067
Pavel Begunkov267bc902019-11-07 01:41:08 +03002068static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002069 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002070{
2071 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002072 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002073 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002074 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002075 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002076
Jens Axboe3529d8c2019-12-19 18:24:38 -07002077 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002078 if (ret < 0)
2079 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002080
Jens Axboefd6c2e42019-12-18 12:19:41 -07002081 /* Ensure we clear previously set non-block flag */
2082 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002083 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002084
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002085 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002086 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002087 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002088 req->result = io_size;
2089
2090 /*
2091 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2092 * we know to async punt it even if it was opened O_NONBLOCK
2093 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002094 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002095 req->flags |= REQ_F_MUST_PUNT;
2096 goto copy_iov;
2097 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002098
Jens Axboe31b51512019-01-18 22:56:34 -07002099 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002100 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002101 if (!ret) {
2102 ssize_t ret2;
2103
Jens Axboe9adbd452019-12-20 08:45:55 -07002104 if (req->file->f_op->read_iter)
2105 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002106 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002107 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002108
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002109 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002110 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002111 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002112 } else {
2113copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002114 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002115 inline_vecs, &iter);
2116 if (ret)
2117 goto out_free;
2118 return -EAGAIN;
2119 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002120 }
Jens Axboef67676d2019-12-02 11:03:47 -07002121out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002122 if (!io_wq_current_is_worker())
2123 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002124 return ret;
2125}
2126
Jens Axboe3529d8c2019-12-19 18:24:38 -07002127static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2128 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002129{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002130 struct io_async_ctx *io;
2131 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002132 ssize_t ret;
2133
Jens Axboe3529d8c2019-12-19 18:24:38 -07002134 ret = io_prep_rw(req, sqe, force_nonblock);
2135 if (ret)
2136 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002137
Jens Axboe3529d8c2019-12-19 18:24:38 -07002138 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2139 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002140
Jens Axboe3529d8c2019-12-19 18:24:38 -07002141 if (!req->io)
2142 return 0;
2143
2144 io = req->io;
2145 io->rw.iov = io->rw.fast_iov;
2146 req->io = NULL;
2147 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2148 req->io = io;
2149 if (ret < 0)
2150 return ret;
2151
2152 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2153 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002154}
2155
Pavel Begunkov267bc902019-11-07 01:41:08 +03002156static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002157 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002158{
2159 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002160 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002161 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002162 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002163 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002164
Jens Axboe3529d8c2019-12-19 18:24:38 -07002165 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002166 if (ret < 0)
2167 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002168
Jens Axboefd6c2e42019-12-18 12:19:41 -07002169 /* Ensure we clear previously set non-block flag */
2170 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002171 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002172
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002173 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002174 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002175 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002176 req->result = io_size;
2177
2178 /*
2179 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2180 * we know to async punt it even if it was opened O_NONBLOCK
2181 */
2182 if (force_nonblock && !io_file_supports_async(req->file)) {
2183 req->flags |= REQ_F_MUST_PUNT;
2184 goto copy_iov;
2185 }
2186
Jens Axboe10d59342019-12-09 20:16:22 -07002187 /* file path doesn't support NOWAIT for non-direct_IO */
2188 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2189 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002190 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002191
Jens Axboe31b51512019-01-18 22:56:34 -07002192 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002193 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002194 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002195 ssize_t ret2;
2196
Jens Axboe2b188cc2019-01-07 10:46:33 -07002197 /*
2198 * Open-code file_start_write here to grab freeze protection,
2199 * which will be released by another thread in
2200 * io_complete_rw(). Fool lockdep by telling it the lock got
2201 * released so that it doesn't complain about the held lock when
2202 * we return to userspace.
2203 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002204 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002205 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002206 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002207 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002208 SB_FREEZE_WRITE);
2209 }
2210 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002211
Jens Axboe9adbd452019-12-20 08:45:55 -07002212 if (req->file->f_op->write_iter)
2213 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002214 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002215 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002216 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002217 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002218 } else {
2219copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002220 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002221 inline_vecs, &iter);
2222 if (ret)
2223 goto out_free;
2224 return -EAGAIN;
2225 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002226 }
Jens Axboe31b51512019-01-18 22:56:34 -07002227out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002228 if (!io_wq_current_is_worker())
2229 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002230 return ret;
2231}
2232
2233/*
2234 * IORING_OP_NOP just posts a completion event, nothing else.
2235 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002236static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002237{
2238 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002239
Jens Axboedef596e2019-01-09 08:59:42 -07002240 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2241 return -EINVAL;
2242
Jens Axboe78e19bb2019-11-06 15:21:34 -07002243 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002244 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002245 return 0;
2246}
2247
Jens Axboe3529d8c2019-12-19 18:24:38 -07002248static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002249{
Jens Axboe6b063142019-01-10 22:13:58 -07002250 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002251
Jens Axboe09bb8392019-03-13 12:39:28 -06002252 if (!req->file)
2253 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002254
Jens Axboe6b063142019-01-10 22:13:58 -07002255 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002256 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002257 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002258 return -EINVAL;
2259
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002260 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2261 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2262 return -EINVAL;
2263
2264 req->sync.off = READ_ONCE(sqe->off);
2265 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002266 return 0;
2267}
2268
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002269static bool io_req_cancelled(struct io_kiocb *req)
2270{
2271 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2272 req_set_fail_links(req);
2273 io_cqring_add_event(req, -ECANCELED);
2274 io_put_req(req);
2275 return true;
2276 }
2277
2278 return false;
2279}
2280
Jens Axboe78912932020-01-14 22:09:06 -07002281static void io_link_work_cb(struct io_wq_work **workptr)
2282{
2283 struct io_wq_work *work = *workptr;
2284 struct io_kiocb *link = work->data;
2285
2286 io_queue_linked_timeout(link);
2287 work->func = io_wq_submit_work;
2288}
2289
2290static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2291{
2292 struct io_kiocb *link;
2293
2294 io_prep_async_work(nxt, &link);
2295 *workptr = &nxt->work;
2296 if (link) {
2297 nxt->work.flags |= IO_WQ_WORK_CB;
2298 nxt->work.func = io_link_work_cb;
2299 nxt->work.data = link;
2300 }
2301}
2302
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002303static void io_fsync_finish(struct io_wq_work **workptr)
2304{
2305 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2306 loff_t end = req->sync.off + req->sync.len;
2307 struct io_kiocb *nxt = NULL;
2308 int ret;
2309
2310 if (io_req_cancelled(req))
2311 return;
2312
Jens Axboe9adbd452019-12-20 08:45:55 -07002313 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002314 end > 0 ? end : LLONG_MAX,
2315 req->sync.flags & IORING_FSYNC_DATASYNC);
2316 if (ret < 0)
2317 req_set_fail_links(req);
2318 io_cqring_add_event(req, ret);
2319 io_put_req_find_next(req, &nxt);
2320 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002321 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002322}
2323
Jens Axboefc4df992019-12-10 14:38:45 -07002324static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2325 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002326{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002327 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002328
2329 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002330 if (force_nonblock) {
2331 io_put_req(req);
2332 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002333 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002334 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002335
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002336 work = old_work = &req->work;
2337 io_fsync_finish(&work);
2338 if (work && work != old_work)
2339 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002340 return 0;
2341}
2342
Jens Axboed63d1b52019-12-10 10:38:56 -07002343static void io_fallocate_finish(struct io_wq_work **workptr)
2344{
2345 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2346 struct io_kiocb *nxt = NULL;
2347 int ret;
2348
2349 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2350 req->sync.len);
2351 if (ret < 0)
2352 req_set_fail_links(req);
2353 io_cqring_add_event(req, ret);
2354 io_put_req_find_next(req, &nxt);
2355 if (nxt)
2356 io_wq_assign_next(workptr, nxt);
2357}
2358
2359static int io_fallocate_prep(struct io_kiocb *req,
2360 const struct io_uring_sqe *sqe)
2361{
2362 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2363 return -EINVAL;
2364
2365 req->sync.off = READ_ONCE(sqe->off);
2366 req->sync.len = READ_ONCE(sqe->addr);
2367 req->sync.mode = READ_ONCE(sqe->len);
2368 return 0;
2369}
2370
2371static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2372 bool force_nonblock)
2373{
2374 struct io_wq_work *work, *old_work;
2375
2376 /* fallocate always requiring blocking context */
2377 if (force_nonblock) {
2378 io_put_req(req);
2379 req->work.func = io_fallocate_finish;
2380 return -EAGAIN;
2381 }
2382
2383 work = old_work = &req->work;
2384 io_fallocate_finish(&work);
2385 if (work && work != old_work)
2386 *nxt = container_of(work, struct io_kiocb, work);
2387
2388 return 0;
2389}
2390
Jens Axboe15b71ab2019-12-11 11:20:36 -07002391static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2392{
2393 int ret;
2394
2395 if (sqe->ioprio || sqe->buf_index)
2396 return -EINVAL;
2397
2398 req->open.dfd = READ_ONCE(sqe->fd);
2399 req->open.mode = READ_ONCE(sqe->len);
2400 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2401 req->open.flags = READ_ONCE(sqe->open_flags);
2402
2403 req->open.filename = getname(req->open.fname);
2404 if (IS_ERR(req->open.filename)) {
2405 ret = PTR_ERR(req->open.filename);
2406 req->open.filename = NULL;
2407 return ret;
2408 }
2409
2410 return 0;
2411}
2412
2413static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2414 bool force_nonblock)
2415{
2416 struct open_flags op;
2417 struct open_how how;
2418 struct file *file;
2419 int ret;
2420
2421 if (force_nonblock) {
2422 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2423 return -EAGAIN;
2424 }
2425
2426 how = build_open_how(req->open.flags, req->open.mode);
2427 ret = build_open_flags(&how, &op);
2428 if (ret)
2429 goto err;
2430
2431 ret = get_unused_fd_flags(how.flags);
2432 if (ret < 0)
2433 goto err;
2434
2435 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2436 if (IS_ERR(file)) {
2437 put_unused_fd(ret);
2438 ret = PTR_ERR(file);
2439 } else {
2440 fsnotify_open(file);
2441 fd_install(ret, file);
2442 }
2443err:
2444 putname(req->open.filename);
2445 if (ret < 0)
2446 req_set_fail_links(req);
2447 io_cqring_add_event(req, ret);
2448 io_put_req_find_next(req, nxt);
2449 return 0;
2450}
2451
Jens Axboe4840e412019-12-25 22:03:45 -07002452static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2453{
2454 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2455 return -EINVAL;
2456
2457 req->fadvise.offset = READ_ONCE(sqe->off);
2458 req->fadvise.len = READ_ONCE(sqe->len);
2459 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2460 return 0;
2461}
2462
2463static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2464 bool force_nonblock)
2465{
2466 struct io_fadvise *fa = &req->fadvise;
2467 int ret;
2468
2469 /* DONTNEED may block, others _should_ not */
2470 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2471 return -EAGAIN;
2472
2473 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2474 if (ret < 0)
2475 req_set_fail_links(req);
2476 io_cqring_add_event(req, ret);
2477 io_put_req_find_next(req, nxt);
2478 return 0;
2479}
2480
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002481static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2482{
2483 unsigned lookup_flags;
2484 int ret;
2485
2486 if (sqe->ioprio || sqe->buf_index)
2487 return -EINVAL;
2488
2489 req->open.dfd = READ_ONCE(sqe->fd);
2490 req->open.mask = READ_ONCE(sqe->len);
2491 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2492 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2493 req->open.flags = READ_ONCE(sqe->statx_flags);
2494
2495 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.flags))
2496 return -EINVAL;
2497
2498 req->open.filename = getname_flags(req->open.fname, lookup_flags, NULL);
2499 if (IS_ERR(req->open.filename)) {
2500 ret = PTR_ERR(req->open.filename);
2501 req->open.filename = NULL;
2502 return ret;
2503 }
2504
2505 return 0;
2506}
2507
2508static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2509 bool force_nonblock)
2510{
2511 struct io_open *ctx = &req->open;
2512 unsigned lookup_flags;
2513 struct path path;
2514 struct kstat stat;
2515 int ret;
2516
2517 if (force_nonblock)
2518 return -EAGAIN;
2519
2520 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->flags))
2521 return -EINVAL;
2522
2523retry:
2524 /* filename_lookup() drops it, keep a reference */
2525 ctx->filename->refcnt++;
2526
2527 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2528 NULL);
2529 if (ret)
2530 goto err;
2531
2532 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->flags);
2533 path_put(&path);
2534 if (retry_estale(ret, lookup_flags)) {
2535 lookup_flags |= LOOKUP_REVAL;
2536 goto retry;
2537 }
2538 if (!ret)
2539 ret = cp_statx(&stat, ctx->buffer);
2540err:
2541 putname(ctx->filename);
2542 if (ret < 0)
2543 req_set_fail_links(req);
2544 io_cqring_add_event(req, ret);
2545 io_put_req_find_next(req, nxt);
2546 return 0;
2547}
2548
Jens Axboeb5dba592019-12-11 14:02:38 -07002549static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2550{
2551 /*
2552 * If we queue this for async, it must not be cancellable. That would
2553 * leave the 'file' in an undeterminate state.
2554 */
2555 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2556
2557 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2558 sqe->rw_flags || sqe->buf_index)
2559 return -EINVAL;
2560 if (sqe->flags & IOSQE_FIXED_FILE)
2561 return -EINVAL;
2562
2563 req->close.fd = READ_ONCE(sqe->fd);
2564 if (req->file->f_op == &io_uring_fops ||
2565 req->close.fd == req->ring_fd)
2566 return -EBADF;
2567
2568 return 0;
2569}
2570
2571static void io_close_finish(struct io_wq_work **workptr)
2572{
2573 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2574 struct io_kiocb *nxt = NULL;
2575
2576 /* Invoked with files, we need to do the close */
2577 if (req->work.files) {
2578 int ret;
2579
2580 ret = filp_close(req->close.put_file, req->work.files);
2581 if (ret < 0) {
2582 req_set_fail_links(req);
2583 }
2584 io_cqring_add_event(req, ret);
2585 }
2586
2587 fput(req->close.put_file);
2588
2589 /* we bypassed the re-issue, drop the submission reference */
2590 io_put_req(req);
2591 io_put_req_find_next(req, &nxt);
2592 if (nxt)
2593 io_wq_assign_next(workptr, nxt);
2594}
2595
2596static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2597 bool force_nonblock)
2598{
2599 int ret;
2600
2601 req->close.put_file = NULL;
2602 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2603 if (ret < 0)
2604 return ret;
2605
2606 /* if the file has a flush method, be safe and punt to async */
2607 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2608 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2609 goto eagain;
2610 }
2611
2612 /*
2613 * No ->flush(), safely close from here and just punt the
2614 * fput() to async context.
2615 */
2616 ret = filp_close(req->close.put_file, current->files);
2617
2618 if (ret < 0)
2619 req_set_fail_links(req);
2620 io_cqring_add_event(req, ret);
2621
2622 if (io_wq_current_is_worker()) {
2623 struct io_wq_work *old_work, *work;
2624
2625 old_work = work = &req->work;
2626 io_close_finish(&work);
2627 if (work && work != old_work)
2628 *nxt = container_of(work, struct io_kiocb, work);
2629 return 0;
2630 }
2631
2632eagain:
2633 req->work.func = io_close_finish;
2634 return -EAGAIN;
2635}
2636
Jens Axboe3529d8c2019-12-19 18:24:38 -07002637static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002638{
2639 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002640
2641 if (!req->file)
2642 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002643
2644 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2645 return -EINVAL;
2646 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2647 return -EINVAL;
2648
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002649 req->sync.off = READ_ONCE(sqe->off);
2650 req->sync.len = READ_ONCE(sqe->len);
2651 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002652 return 0;
2653}
2654
2655static void io_sync_file_range_finish(struct io_wq_work **workptr)
2656{
2657 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2658 struct io_kiocb *nxt = NULL;
2659 int ret;
2660
2661 if (io_req_cancelled(req))
2662 return;
2663
Jens Axboe9adbd452019-12-20 08:45:55 -07002664 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002665 req->sync.flags);
2666 if (ret < 0)
2667 req_set_fail_links(req);
2668 io_cqring_add_event(req, ret);
2669 io_put_req_find_next(req, &nxt);
2670 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002671 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002672}
2673
Jens Axboefc4df992019-12-10 14:38:45 -07002674static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002675 bool force_nonblock)
2676{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002677 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002678
2679 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002680 if (force_nonblock) {
2681 io_put_req(req);
2682 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002683 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002684 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002685
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002686 work = old_work = &req->work;
2687 io_sync_file_range_finish(&work);
2688 if (work && work != old_work)
2689 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002690 return 0;
2691}
2692
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002693#if defined(CONFIG_NET)
2694static void io_sendrecv_async(struct io_wq_work **workptr)
2695{
2696 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2697 struct iovec *iov = NULL;
2698
2699 if (req->io->rw.iov != req->io->rw.fast_iov)
2700 iov = req->io->msg.iov;
2701 io_wq_submit_work(workptr);
2702 kfree(iov);
2703}
2704#endif
2705
Jens Axboe3529d8c2019-12-19 18:24:38 -07002706static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002707{
Jens Axboe03b12302019-12-02 18:50:25 -07002708#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002709 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002710 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002711
Jens Axboee47293f2019-12-20 08:58:21 -07002712 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2713 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe3529d8c2019-12-19 18:24:38 -07002714
2715 if (!io)
2716 return 0;
2717
Jens Axboed9688562019-12-09 19:35:20 -07002718 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002719 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002720 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002721#else
Jens Axboee47293f2019-12-20 08:58:21 -07002722 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002723#endif
2724}
2725
Jens Axboefc4df992019-12-10 14:38:45 -07002726static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2727 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002728{
2729#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002730 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002731 struct socket *sock;
2732 int ret;
2733
2734 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2735 return -EINVAL;
2736
2737 sock = sock_from_file(req->file, &ret);
2738 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002739 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002740 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002741 unsigned flags;
2742
Jens Axboe03b12302019-12-02 18:50:25 -07002743 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002744 kmsg = &req->io->msg;
2745 kmsg->msg.msg_name = &addr;
2746 /* if iov is set, it's allocated already */
2747 if (!kmsg->iov)
2748 kmsg->iov = kmsg->fast_iov;
2749 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002750 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002751 struct io_sr_msg *sr = &req->sr_msg;
2752
Jens Axboe0b416c32019-12-15 10:57:46 -07002753 kmsg = &io.msg;
2754 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002755
2756 io.msg.iov = io.msg.fast_iov;
2757 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2758 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002759 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002760 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002761 }
2762
Jens Axboee47293f2019-12-20 08:58:21 -07002763 flags = req->sr_msg.msg_flags;
2764 if (flags & MSG_DONTWAIT)
2765 req->flags |= REQ_F_NOWAIT;
2766 else if (force_nonblock)
2767 flags |= MSG_DONTWAIT;
2768
Jens Axboe0b416c32019-12-15 10:57:46 -07002769 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002770 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002771 if (req->io)
2772 return -EAGAIN;
2773 if (io_alloc_async_ctx(req))
2774 return -ENOMEM;
2775 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2776 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002777 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002778 }
2779 if (ret == -ERESTARTSYS)
2780 ret = -EINTR;
2781 }
2782
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002783 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002784 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002785 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002786 if (ret < 0)
2787 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002788 io_put_req_find_next(req, nxt);
2789 return 0;
2790#else
2791 return -EOPNOTSUPP;
2792#endif
2793}
2794
Jens Axboe3529d8c2019-12-19 18:24:38 -07002795static int io_recvmsg_prep(struct io_kiocb *req,
2796 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002797{
2798#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002799 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002800 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002801
Jens Axboe3529d8c2019-12-19 18:24:38 -07002802 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2803 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2804
2805 if (!io)
Jens Axboe06b76d42019-12-19 14:44:26 -07002806 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002807
Jens Axboed9688562019-12-09 19:35:20 -07002808 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002809 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002810 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002811#else
Jens Axboee47293f2019-12-20 08:58:21 -07002812 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002813#endif
2814}
2815
Jens Axboefc4df992019-12-10 14:38:45 -07002816static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2817 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002818{
2819#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002820 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002821 struct socket *sock;
2822 int ret;
2823
2824 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2825 return -EINVAL;
2826
2827 sock = sock_from_file(req->file, &ret);
2828 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002829 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002830 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002831 unsigned flags;
2832
Jens Axboe03b12302019-12-02 18:50:25 -07002833 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002834 kmsg = &req->io->msg;
2835 kmsg->msg.msg_name = &addr;
2836 /* if iov is set, it's allocated already */
2837 if (!kmsg->iov)
2838 kmsg->iov = kmsg->fast_iov;
2839 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002840 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002841 struct io_sr_msg *sr = &req->sr_msg;
2842
Jens Axboe0b416c32019-12-15 10:57:46 -07002843 kmsg = &io.msg;
2844 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002845
2846 io.msg.iov = io.msg.fast_iov;
2847 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
2848 sr->msg_flags, &io.msg.uaddr,
2849 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002850 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002851 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002852 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002853
Jens Axboee47293f2019-12-20 08:58:21 -07002854 flags = req->sr_msg.msg_flags;
2855 if (flags & MSG_DONTWAIT)
2856 req->flags |= REQ_F_NOWAIT;
2857 else if (force_nonblock)
2858 flags |= MSG_DONTWAIT;
2859
2860 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
2861 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002862 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002863 if (req->io)
2864 return -EAGAIN;
2865 if (io_alloc_async_ctx(req))
2866 return -ENOMEM;
2867 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2868 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002869 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002870 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002871 if (ret == -ERESTARTSYS)
2872 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002873 }
2874
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002875 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002876 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002877 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002878 if (ret < 0)
2879 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002880 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002881 return 0;
2882#else
2883 return -EOPNOTSUPP;
2884#endif
2885}
2886
Jens Axboe3529d8c2019-12-19 18:24:38 -07002887static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002888{
2889#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002890 struct io_accept *accept = &req->accept;
2891
Jens Axboe17f2fe32019-10-17 14:42:58 -06002892 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2893 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002894 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002895 return -EINVAL;
2896
Jens Axboed55e5f52019-12-11 16:12:15 -07002897 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2898 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002899 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002900 return 0;
2901#else
2902 return -EOPNOTSUPP;
2903#endif
2904}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002905
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002906#if defined(CONFIG_NET)
2907static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2908 bool force_nonblock)
2909{
2910 struct io_accept *accept = &req->accept;
2911 unsigned file_flags;
2912 int ret;
2913
2914 file_flags = force_nonblock ? O_NONBLOCK : 0;
2915 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2916 accept->addr_len, accept->flags);
2917 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002918 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002919 if (ret == -ERESTARTSYS)
2920 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002921 if (ret < 0)
2922 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002923 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002924 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002925 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002926}
2927
2928static void io_accept_finish(struct io_wq_work **workptr)
2929{
2930 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2931 struct io_kiocb *nxt = NULL;
2932
2933 if (io_req_cancelled(req))
2934 return;
2935 __io_accept(req, &nxt, false);
2936 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002937 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002938}
2939#endif
2940
2941static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2942 bool force_nonblock)
2943{
2944#if defined(CONFIG_NET)
2945 int ret;
2946
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002947 ret = __io_accept(req, nxt, force_nonblock);
2948 if (ret == -EAGAIN && force_nonblock) {
2949 req->work.func = io_accept_finish;
2950 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2951 io_put_req(req);
2952 return -EAGAIN;
2953 }
2954 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002955#else
2956 return -EOPNOTSUPP;
2957#endif
2958}
2959
Jens Axboe3529d8c2019-12-19 18:24:38 -07002960static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07002961{
2962#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002963 struct io_connect *conn = &req->connect;
2964 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07002965
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002966 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2967 return -EINVAL;
2968 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2969 return -EINVAL;
2970
Jens Axboe3529d8c2019-12-19 18:24:38 -07002971 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2972 conn->addr_len = READ_ONCE(sqe->addr2);
2973
2974 if (!io)
2975 return 0;
2976
2977 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002978 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002979#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002980 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07002981#endif
2982}
2983
Jens Axboefc4df992019-12-10 14:38:45 -07002984static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
2985 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07002986{
2987#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07002988 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002989 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002990 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002991
Jens Axboef499a022019-12-02 16:28:46 -07002992 if (req->io) {
2993 io = req->io;
2994 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002995 ret = move_addr_to_kernel(req->connect.addr,
2996 req->connect.addr_len,
2997 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002998 if (ret)
2999 goto out;
3000 io = &__io;
3001 }
3002
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003003 file_flags = force_nonblock ? O_NONBLOCK : 0;
3004
3005 ret = __sys_connect_file(req->file, &io->connect.address,
3006 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003007 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003008 if (req->io)
3009 return -EAGAIN;
3010 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003011 ret = -ENOMEM;
3012 goto out;
3013 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003014 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003015 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003016 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003017 if (ret == -ERESTARTSYS)
3018 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003019out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003020 if (ret < 0)
3021 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003022 io_cqring_add_event(req, ret);
3023 io_put_req_find_next(req, nxt);
3024 return 0;
3025#else
3026 return -EOPNOTSUPP;
3027#endif
3028}
3029
Jens Axboe221c5eb2019-01-17 09:41:58 -07003030static void io_poll_remove_one(struct io_kiocb *req)
3031{
3032 struct io_poll_iocb *poll = &req->poll;
3033
3034 spin_lock(&poll->head->lock);
3035 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003036 if (!list_empty(&poll->wait.entry)) {
3037 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003038 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003039 }
3040 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003041 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003042}
3043
3044static void io_poll_remove_all(struct io_ring_ctx *ctx)
3045{
Jens Axboe78076bb2019-12-04 19:56:40 -07003046 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003047 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003048 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003049
3050 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003051 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3052 struct hlist_head *list;
3053
3054 list = &ctx->cancel_hash[i];
3055 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3056 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003057 }
3058 spin_unlock_irq(&ctx->completion_lock);
3059}
3060
Jens Axboe47f46762019-11-09 17:43:02 -07003061static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3062{
Jens Axboe78076bb2019-12-04 19:56:40 -07003063 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003064 struct io_kiocb *req;
3065
Jens Axboe78076bb2019-12-04 19:56:40 -07003066 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3067 hlist_for_each_entry(req, list, hash_node) {
3068 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003069 io_poll_remove_one(req);
3070 return 0;
3071 }
Jens Axboe47f46762019-11-09 17:43:02 -07003072 }
3073
3074 return -ENOENT;
3075}
3076
Jens Axboe3529d8c2019-12-19 18:24:38 -07003077static int io_poll_remove_prep(struct io_kiocb *req,
3078 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003079{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003080 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3081 return -EINVAL;
3082 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3083 sqe->poll_events)
3084 return -EINVAL;
3085
Jens Axboe0969e782019-12-17 18:40:57 -07003086 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003087 return 0;
3088}
3089
3090/*
3091 * Find a running poll command that matches one specified in sqe->addr,
3092 * and remove it if found.
3093 */
3094static int io_poll_remove(struct io_kiocb *req)
3095{
3096 struct io_ring_ctx *ctx = req->ctx;
3097 u64 addr;
3098 int ret;
3099
Jens Axboe0969e782019-12-17 18:40:57 -07003100 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003101 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003102 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003103 spin_unlock_irq(&ctx->completion_lock);
3104
Jens Axboe78e19bb2019-11-06 15:21:34 -07003105 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003106 if (ret < 0)
3107 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003108 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003109 return 0;
3110}
3111
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003112static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003113{
Jackie Liua197f662019-11-08 08:09:12 -07003114 struct io_ring_ctx *ctx = req->ctx;
3115
Jens Axboe8c838782019-03-12 15:48:16 -06003116 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003117 if (error)
3118 io_cqring_fill_event(req, error);
3119 else
3120 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003121 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003122}
3123
Jens Axboe561fb042019-10-24 07:25:42 -06003124static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003125{
Jens Axboe561fb042019-10-24 07:25:42 -06003126 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003127 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3128 struct io_poll_iocb *poll = &req->poll;
3129 struct poll_table_struct pt = { ._key = poll->events };
3130 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003131 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003132 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003133 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003134
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003135 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003136 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003137 ret = -ECANCELED;
3138 } else if (READ_ONCE(poll->canceled)) {
3139 ret = -ECANCELED;
3140 }
Jens Axboe561fb042019-10-24 07:25:42 -06003141
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003142 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003143 mask = vfs_poll(poll->file, &pt) & poll->events;
3144
3145 /*
3146 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3147 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3148 * synchronize with them. In the cancellation case the list_del_init
3149 * itself is not actually needed, but harmless so we keep it in to
3150 * avoid further branches in the fast path.
3151 */
3152 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003153 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003154 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003155 spin_unlock_irq(&ctx->completion_lock);
3156 return;
3157 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003158 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003159 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003160 spin_unlock_irq(&ctx->completion_lock);
3161
Jens Axboe8c838782019-03-12 15:48:16 -06003162 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003163
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003164 if (ret < 0)
3165 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003166 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003167 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003168 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003169}
3170
Jens Axboee94f1412019-12-19 12:06:02 -07003171static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3172{
3173 void *reqs[IO_IOPOLL_BATCH];
3174 struct io_kiocb *req, *tmp;
3175 int to_free = 0;
3176
3177 spin_lock_irq(&ctx->completion_lock);
3178 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3179 hash_del(&req->hash_node);
3180 io_poll_complete(req, req->result, 0);
3181
3182 if (refcount_dec_and_test(&req->refs)) {
3183 if (io_req_multi_free(req)) {
3184 reqs[to_free++] = req;
3185 if (to_free == ARRAY_SIZE(reqs))
3186 io_free_req_many(ctx, reqs, &to_free);
3187 } else {
3188 req->flags |= REQ_F_COMP_LOCKED;
3189 io_free_req(req);
3190 }
3191 }
3192 }
3193 spin_unlock_irq(&ctx->completion_lock);
3194
3195 io_cqring_ev_posted(ctx);
3196 io_free_req_many(ctx, reqs, &to_free);
3197}
3198
3199static void io_poll_flush(struct io_wq_work **workptr)
3200{
3201 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3202 struct llist_node *nodes;
3203
3204 nodes = llist_del_all(&req->ctx->poll_llist);
3205 if (nodes)
3206 __io_poll_flush(req->ctx, nodes);
3207}
3208
Jens Axboe221c5eb2019-01-17 09:41:58 -07003209static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3210 void *key)
3211{
Jens Axboee9444752019-11-26 15:02:04 -07003212 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003213 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3214 struct io_ring_ctx *ctx = req->ctx;
3215 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003216
3217 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003218 if (mask && !(mask & poll->events))
3219 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003220
Jens Axboe392edb42019-12-09 17:52:20 -07003221 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003222
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003223 /*
3224 * Run completion inline if we can. We're using trylock here because
3225 * we are violating the completion_lock -> poll wq lock ordering.
3226 * If we have a link timeout we're going to need the completion_lock
3227 * for finalizing the request, mark us as having grabbed that already.
3228 */
Jens Axboee94f1412019-12-19 12:06:02 -07003229 if (mask) {
3230 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003231
Jens Axboee94f1412019-12-19 12:06:02 -07003232 if (llist_empty(&ctx->poll_llist) &&
3233 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3234 hash_del(&req->hash_node);
3235 io_poll_complete(req, mask, 0);
3236 req->flags |= REQ_F_COMP_LOCKED;
3237 io_put_req(req);
3238 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3239
3240 io_cqring_ev_posted(ctx);
3241 req = NULL;
3242 } else {
3243 req->result = mask;
3244 req->llist_node.next = NULL;
3245 /* if the list wasn't empty, we're done */
3246 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3247 req = NULL;
3248 else
3249 req->work.func = io_poll_flush;
3250 }
Jens Axboe8c838782019-03-12 15:48:16 -06003251 }
Jens Axboee94f1412019-12-19 12:06:02 -07003252 if (req)
3253 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003254
Jens Axboe221c5eb2019-01-17 09:41:58 -07003255 return 1;
3256}
3257
3258struct io_poll_table {
3259 struct poll_table_struct pt;
3260 struct io_kiocb *req;
3261 int error;
3262};
3263
3264static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3265 struct poll_table_struct *p)
3266{
3267 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3268
3269 if (unlikely(pt->req->poll.head)) {
3270 pt->error = -EINVAL;
3271 return;
3272 }
3273
3274 pt->error = 0;
3275 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003276 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003277}
3278
Jens Axboeeac406c2019-11-14 12:09:58 -07003279static void io_poll_req_insert(struct io_kiocb *req)
3280{
3281 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003282 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003283
Jens Axboe78076bb2019-12-04 19:56:40 -07003284 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3285 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003286}
3287
Jens Axboe3529d8c2019-12-19 18:24:38 -07003288static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003289{
3290 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003291 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003292
3293 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3294 return -EINVAL;
3295 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3296 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003297 if (!poll->file)
3298 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003299
Jens Axboe221c5eb2019-01-17 09:41:58 -07003300 events = READ_ONCE(sqe->poll_events);
3301 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003302 return 0;
3303}
3304
3305static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3306{
3307 struct io_poll_iocb *poll = &req->poll;
3308 struct io_ring_ctx *ctx = req->ctx;
3309 struct io_poll_table ipt;
3310 bool cancel = false;
3311 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003312
3313 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003314 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003315
Jens Axboe221c5eb2019-01-17 09:41:58 -07003316 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003317 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003318 poll->canceled = false;
3319
3320 ipt.pt._qproc = io_poll_queue_proc;
3321 ipt.pt._key = poll->events;
3322 ipt.req = req;
3323 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3324
3325 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003326 INIT_LIST_HEAD(&poll->wait.entry);
3327 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3328 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003329
Jens Axboe36703242019-07-25 10:20:18 -06003330 INIT_LIST_HEAD(&req->list);
3331
Jens Axboe221c5eb2019-01-17 09:41:58 -07003332 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003333
3334 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003335 if (likely(poll->head)) {
3336 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003337 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003338 if (ipt.error)
3339 cancel = true;
3340 ipt.error = 0;
3341 mask = 0;
3342 }
3343 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003344 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003345 else if (cancel)
3346 WRITE_ONCE(poll->canceled, true);
3347 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003348 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003349 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003350 }
Jens Axboe8c838782019-03-12 15:48:16 -06003351 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003352 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003353 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003354 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003355 spin_unlock_irq(&ctx->completion_lock);
3356
Jens Axboe8c838782019-03-12 15:48:16 -06003357 if (mask) {
3358 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003359 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003360 }
Jens Axboe8c838782019-03-12 15:48:16 -06003361 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003362}
3363
Jens Axboe5262f562019-09-17 12:26:57 -06003364static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3365{
Jens Axboead8a48a2019-11-15 08:49:11 -07003366 struct io_timeout_data *data = container_of(timer,
3367 struct io_timeout_data, timer);
3368 struct io_kiocb *req = data->req;
3369 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003370 unsigned long flags;
3371
Jens Axboe5262f562019-09-17 12:26:57 -06003372 atomic_inc(&ctx->cq_timeouts);
3373
3374 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003375 /*
Jens Axboe11365042019-10-16 09:08:32 -06003376 * We could be racing with timeout deletion. If the list is empty,
3377 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003378 */
Jens Axboe842f9612019-10-29 12:34:10 -06003379 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003380 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003381
Jens Axboe11365042019-10-16 09:08:32 -06003382 /*
3383 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003384 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003385 * pointer will be increased, otherwise other timeout reqs may
3386 * return in advance without waiting for enough wait_nr.
3387 */
3388 prev = req;
3389 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3390 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003391 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003392 }
Jens Axboe842f9612019-10-29 12:34:10 -06003393
Jens Axboe78e19bb2019-11-06 15:21:34 -07003394 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003395 io_commit_cqring(ctx);
3396 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3397
3398 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003399 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003400 io_put_req(req);
3401 return HRTIMER_NORESTART;
3402}
3403
Jens Axboe47f46762019-11-09 17:43:02 -07003404static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3405{
3406 struct io_kiocb *req;
3407 int ret = -ENOENT;
3408
3409 list_for_each_entry(req, &ctx->timeout_list, list) {
3410 if (user_data == req->user_data) {
3411 list_del_init(&req->list);
3412 ret = 0;
3413 break;
3414 }
3415 }
3416
3417 if (ret == -ENOENT)
3418 return ret;
3419
Jens Axboe2d283902019-12-04 11:08:05 -07003420 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003421 if (ret == -1)
3422 return -EALREADY;
3423
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003424 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003425 io_cqring_fill_event(req, -ECANCELED);
3426 io_put_req(req);
3427 return 0;
3428}
3429
Jens Axboe3529d8c2019-12-19 18:24:38 -07003430static int io_timeout_remove_prep(struct io_kiocb *req,
3431 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003432{
Jens Axboeb29472e2019-12-17 18:50:29 -07003433 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3434 return -EINVAL;
3435 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3436 return -EINVAL;
3437
3438 req->timeout.addr = READ_ONCE(sqe->addr);
3439 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3440 if (req->timeout.flags)
3441 return -EINVAL;
3442
Jens Axboeb29472e2019-12-17 18:50:29 -07003443 return 0;
3444}
3445
Jens Axboe11365042019-10-16 09:08:32 -06003446/*
3447 * Remove or update an existing timeout command
3448 */
Jens Axboefc4df992019-12-10 14:38:45 -07003449static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003450{
3451 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003452 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003453
Jens Axboe11365042019-10-16 09:08:32 -06003454 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003455 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003456
Jens Axboe47f46762019-11-09 17:43:02 -07003457 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003458 io_commit_cqring(ctx);
3459 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003460 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003461 if (ret < 0)
3462 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003463 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003464 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003465}
3466
Jens Axboe3529d8c2019-12-19 18:24:38 -07003467static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003468 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003469{
Jens Axboead8a48a2019-11-15 08:49:11 -07003470 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003471 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003472
Jens Axboead8a48a2019-11-15 08:49:11 -07003473 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003474 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003475 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003476 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003477 if (sqe->off && is_timeout_link)
3478 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003479 flags = READ_ONCE(sqe->timeout_flags);
3480 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003481 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003482
Jens Axboe26a61672019-12-20 09:02:01 -07003483 req->timeout.count = READ_ONCE(sqe->off);
3484
Jens Axboe3529d8c2019-12-19 18:24:38 -07003485 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003486 return -ENOMEM;
3487
3488 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003489 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003490 req->flags |= REQ_F_TIMEOUT;
3491
3492 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003493 return -EFAULT;
3494
Jens Axboe11365042019-10-16 09:08:32 -06003495 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003496 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003497 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003498 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003499
Jens Axboead8a48a2019-11-15 08:49:11 -07003500 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3501 return 0;
3502}
3503
Jens Axboefc4df992019-12-10 14:38:45 -07003504static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003505{
3506 unsigned count;
3507 struct io_ring_ctx *ctx = req->ctx;
3508 struct io_timeout_data *data;
3509 struct list_head *entry;
3510 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003511
Jens Axboe2d283902019-12-04 11:08:05 -07003512 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003513
Jens Axboe5262f562019-09-17 12:26:57 -06003514 /*
3515 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003516 * timeout event to be satisfied. If it isn't set, then this is
3517 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003518 */
Jens Axboe26a61672019-12-20 09:02:01 -07003519 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003520 if (!count) {
3521 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3522 spin_lock_irq(&ctx->completion_lock);
3523 entry = ctx->timeout_list.prev;
3524 goto add;
3525 }
Jens Axboe5262f562019-09-17 12:26:57 -06003526
3527 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003528 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003529
3530 /*
3531 * Insertion sort, ensuring the first entry in the list is always
3532 * the one we need first.
3533 */
Jens Axboe5262f562019-09-17 12:26:57 -06003534 spin_lock_irq(&ctx->completion_lock);
3535 list_for_each_prev(entry, &ctx->timeout_list) {
3536 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003537 unsigned nxt_sq_head;
3538 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003539 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003540
Jens Axboe93bd25b2019-11-11 23:34:31 -07003541 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3542 continue;
3543
yangerkun5da0fb12019-10-15 21:59:29 +08003544 /*
3545 * Since cached_sq_head + count - 1 can overflow, use type long
3546 * long to store it.
3547 */
3548 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003549 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3550 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003551
3552 /*
3553 * cached_sq_head may overflow, and it will never overflow twice
3554 * once there is some timeout req still be valid.
3555 */
3556 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003557 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003558
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003559 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003560 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003561
3562 /*
3563 * Sequence of reqs after the insert one and itself should
3564 * be adjusted because each timeout req consumes a slot.
3565 */
3566 span++;
3567 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003568 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003569 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003570add:
Jens Axboe5262f562019-09-17 12:26:57 -06003571 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003572 data->timer.function = io_timeout_fn;
3573 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003574 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003575 return 0;
3576}
3577
Jens Axboe62755e32019-10-28 21:49:21 -06003578static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003579{
Jens Axboe62755e32019-10-28 21:49:21 -06003580 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003581
Jens Axboe62755e32019-10-28 21:49:21 -06003582 return req->user_data == (unsigned long) data;
3583}
3584
Jens Axboee977d6d2019-11-05 12:39:45 -07003585static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003586{
Jens Axboe62755e32019-10-28 21:49:21 -06003587 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003588 int ret = 0;
3589
Jens Axboe62755e32019-10-28 21:49:21 -06003590 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3591 switch (cancel_ret) {
3592 case IO_WQ_CANCEL_OK:
3593 ret = 0;
3594 break;
3595 case IO_WQ_CANCEL_RUNNING:
3596 ret = -EALREADY;
3597 break;
3598 case IO_WQ_CANCEL_NOTFOUND:
3599 ret = -ENOENT;
3600 break;
3601 }
3602
Jens Axboee977d6d2019-11-05 12:39:45 -07003603 return ret;
3604}
3605
Jens Axboe47f46762019-11-09 17:43:02 -07003606static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3607 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003608 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003609{
3610 unsigned long flags;
3611 int ret;
3612
3613 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3614 if (ret != -ENOENT) {
3615 spin_lock_irqsave(&ctx->completion_lock, flags);
3616 goto done;
3617 }
3618
3619 spin_lock_irqsave(&ctx->completion_lock, flags);
3620 ret = io_timeout_cancel(ctx, sqe_addr);
3621 if (ret != -ENOENT)
3622 goto done;
3623 ret = io_poll_cancel(ctx, sqe_addr);
3624done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003625 if (!ret)
3626 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003627 io_cqring_fill_event(req, ret);
3628 io_commit_cqring(ctx);
3629 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3630 io_cqring_ev_posted(ctx);
3631
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003632 if (ret < 0)
3633 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003634 io_put_req_find_next(req, nxt);
3635}
3636
Jens Axboe3529d8c2019-12-19 18:24:38 -07003637static int io_async_cancel_prep(struct io_kiocb *req,
3638 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003639{
Jens Axboefbf23842019-12-17 18:45:56 -07003640 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003641 return -EINVAL;
3642 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3643 sqe->cancel_flags)
3644 return -EINVAL;
3645
Jens Axboefbf23842019-12-17 18:45:56 -07003646 req->cancel.addr = READ_ONCE(sqe->addr);
3647 return 0;
3648}
3649
3650static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3651{
3652 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003653
3654 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003655 return 0;
3656}
3657
Jens Axboe05f3fb32019-12-09 11:22:50 -07003658static int io_files_update_prep(struct io_kiocb *req,
3659 const struct io_uring_sqe *sqe)
3660{
3661 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3662 return -EINVAL;
3663
3664 req->files_update.offset = READ_ONCE(sqe->off);
3665 req->files_update.nr_args = READ_ONCE(sqe->len);
3666 if (!req->files_update.nr_args)
3667 return -EINVAL;
3668 req->files_update.arg = READ_ONCE(sqe->addr);
3669 return 0;
3670}
3671
3672static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3673{
3674 struct io_ring_ctx *ctx = req->ctx;
3675 struct io_uring_files_update up;
3676 int ret;
3677
3678 if (force_nonblock) {
3679 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3680 return -EAGAIN;
3681 }
3682
3683 up.offset = req->files_update.offset;
3684 up.fds = req->files_update.arg;
3685
3686 mutex_lock(&ctx->uring_lock);
3687 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3688 mutex_unlock(&ctx->uring_lock);
3689
3690 if (ret < 0)
3691 req_set_fail_links(req);
3692 io_cqring_add_event(req, ret);
3693 io_put_req(req);
3694 return 0;
3695}
3696
Jens Axboe3529d8c2019-12-19 18:24:38 -07003697static int io_req_defer_prep(struct io_kiocb *req,
3698 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003699{
Jens Axboee7815732019-12-17 19:45:06 -07003700 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003701
Jens Axboed625c6e2019-12-17 19:53:05 -07003702 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003703 case IORING_OP_NOP:
3704 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003705 case IORING_OP_READV:
3706 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003707 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003708 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003709 break;
3710 case IORING_OP_WRITEV:
3711 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003712 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003713 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003714 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003715 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003716 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003717 break;
3718 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003719 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003720 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003721 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003722 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003723 break;
3724 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003725 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003726 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003727 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003728 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003729 break;
3730 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003731 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003732 break;
Jens Axboef499a022019-12-02 16:28:46 -07003733 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003734 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003735 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003736 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003737 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003738 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003739 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003740 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003741 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003742 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003743 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003744 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003745 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003746 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003747 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003748 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003749 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003750 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003751 case IORING_OP_FALLOCATE:
3752 ret = io_fallocate_prep(req, sqe);
3753 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003754 case IORING_OP_OPENAT:
3755 ret = io_openat_prep(req, sqe);
3756 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003757 case IORING_OP_CLOSE:
3758 ret = io_close_prep(req, sqe);
3759 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003760 case IORING_OP_FILES_UPDATE:
3761 ret = io_files_update_prep(req, sqe);
3762 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003763 case IORING_OP_STATX:
3764 ret = io_statx_prep(req, sqe);
3765 break;
Jens Axboe4840e412019-12-25 22:03:45 -07003766 case IORING_OP_FADVISE:
3767 ret = io_fadvise_prep(req, sqe);
3768 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003769 default:
Jens Axboee7815732019-12-17 19:45:06 -07003770 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3771 req->opcode);
3772 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003773 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003774 }
3775
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003776 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003777}
3778
Jens Axboe3529d8c2019-12-19 18:24:38 -07003779static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003780{
Jackie Liua197f662019-11-08 08:09:12 -07003781 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003782 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003783
Bob Liu9d858b22019-11-13 18:06:25 +08003784 /* Still need defer if there is pending req in defer list. */
3785 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003786 return 0;
3787
Jens Axboe3529d8c2019-12-19 18:24:38 -07003788 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003789 return -EAGAIN;
3790
Jens Axboe3529d8c2019-12-19 18:24:38 -07003791 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003792 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003793 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003794
Jens Axboede0617e2019-04-06 21:51:27 -06003795 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003796 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003797 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003798 return 0;
3799 }
3800
Jens Axboe915967f2019-11-21 09:01:20 -07003801 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003802 list_add_tail(&req->list, &ctx->defer_list);
3803 spin_unlock_irq(&ctx->completion_lock);
3804 return -EIOCBQUEUED;
3805}
3806
Jens Axboe3529d8c2019-12-19 18:24:38 -07003807static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3808 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003809{
Jackie Liua197f662019-11-08 08:09:12 -07003810 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07003811 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003812
Jens Axboed625c6e2019-12-17 19:53:05 -07003813 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003814 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003815 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003816 break;
3817 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003818 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003819 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003820 if (sqe) {
3821 ret = io_read_prep(req, sqe, force_nonblock);
3822 if (ret < 0)
3823 break;
3824 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003825 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003826 break;
3827 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07003828 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003829 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003830 if (sqe) {
3831 ret = io_write_prep(req, sqe, force_nonblock);
3832 if (ret < 0)
3833 break;
3834 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003835 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003836 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003837 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003838 if (sqe) {
3839 ret = io_prep_fsync(req, sqe);
3840 if (ret < 0)
3841 break;
3842 }
Jens Axboefc4df992019-12-10 14:38:45 -07003843 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003844 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003845 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003846 if (sqe) {
3847 ret = io_poll_add_prep(req, sqe);
3848 if (ret)
3849 break;
3850 }
Jens Axboefc4df992019-12-10 14:38:45 -07003851 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003852 break;
3853 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003854 if (sqe) {
3855 ret = io_poll_remove_prep(req, sqe);
3856 if (ret < 0)
3857 break;
3858 }
Jens Axboefc4df992019-12-10 14:38:45 -07003859 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003860 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003861 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003862 if (sqe) {
3863 ret = io_prep_sfr(req, sqe);
3864 if (ret < 0)
3865 break;
3866 }
Jens Axboefc4df992019-12-10 14:38:45 -07003867 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003868 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003869 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003870 if (sqe) {
3871 ret = io_sendmsg_prep(req, sqe);
3872 if (ret < 0)
3873 break;
3874 }
Jens Axboefc4df992019-12-10 14:38:45 -07003875 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003876 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003877 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003878 if (sqe) {
3879 ret = io_recvmsg_prep(req, sqe);
3880 if (ret)
3881 break;
3882 }
Jens Axboefc4df992019-12-10 14:38:45 -07003883 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003884 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003885 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003886 if (sqe) {
3887 ret = io_timeout_prep(req, sqe, false);
3888 if (ret)
3889 break;
3890 }
Jens Axboefc4df992019-12-10 14:38:45 -07003891 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003892 break;
Jens Axboe11365042019-10-16 09:08:32 -06003893 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003894 if (sqe) {
3895 ret = io_timeout_remove_prep(req, sqe);
3896 if (ret)
3897 break;
3898 }
Jens Axboefc4df992019-12-10 14:38:45 -07003899 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003900 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003901 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003902 if (sqe) {
3903 ret = io_accept_prep(req, sqe);
3904 if (ret)
3905 break;
3906 }
Jens Axboefc4df992019-12-10 14:38:45 -07003907 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003908 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003909 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003910 if (sqe) {
3911 ret = io_connect_prep(req, sqe);
3912 if (ret)
3913 break;
3914 }
Jens Axboefc4df992019-12-10 14:38:45 -07003915 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003916 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003917 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003918 if (sqe) {
3919 ret = io_async_cancel_prep(req, sqe);
3920 if (ret)
3921 break;
3922 }
Jens Axboefc4df992019-12-10 14:38:45 -07003923 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003924 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003925 case IORING_OP_FALLOCATE:
3926 if (sqe) {
3927 ret = io_fallocate_prep(req, sqe);
3928 if (ret)
3929 break;
3930 }
3931 ret = io_fallocate(req, nxt, force_nonblock);
3932 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003933 case IORING_OP_OPENAT:
3934 if (sqe) {
3935 ret = io_openat_prep(req, sqe);
3936 if (ret)
3937 break;
3938 }
3939 ret = io_openat(req, nxt, force_nonblock);
3940 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003941 case IORING_OP_CLOSE:
3942 if (sqe) {
3943 ret = io_close_prep(req, sqe);
3944 if (ret)
3945 break;
3946 }
3947 ret = io_close(req, nxt, force_nonblock);
3948 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003949 case IORING_OP_FILES_UPDATE:
3950 if (sqe) {
3951 ret = io_files_update_prep(req, sqe);
3952 if (ret)
3953 break;
3954 }
3955 ret = io_files_update(req, force_nonblock);
3956 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003957 case IORING_OP_STATX:
3958 if (sqe) {
3959 ret = io_statx_prep(req, sqe);
3960 if (ret)
3961 break;
3962 }
3963 ret = io_statx(req, nxt, force_nonblock);
3964 break;
Jens Axboe4840e412019-12-25 22:03:45 -07003965 case IORING_OP_FADVISE:
3966 if (sqe) {
3967 ret = io_fadvise_prep(req, sqe);
3968 if (ret)
3969 break;
3970 }
3971 ret = io_fadvise(req, nxt, force_nonblock);
3972 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003973 default:
3974 ret = -EINVAL;
3975 break;
3976 }
3977
Jens Axboedef596e2019-01-09 08:59:42 -07003978 if (ret)
3979 return ret;
3980
3981 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07003982 const bool in_async = io_wq_current_is_worker();
3983
Jens Axboe9e645e112019-05-10 16:07:28 -06003984 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07003985 return -EAGAIN;
3986
Jens Axboe11ba8202020-01-15 21:51:17 -07003987 /* workqueue context doesn't hold uring_lock, grab it now */
3988 if (in_async)
3989 mutex_lock(&ctx->uring_lock);
3990
Jens Axboedef596e2019-01-09 08:59:42 -07003991 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07003992
3993 if (in_async)
3994 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07003995 }
3996
3997 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003998}
3999
Jens Axboe561fb042019-10-24 07:25:42 -06004000static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004001{
Jens Axboe561fb042019-10-24 07:25:42 -06004002 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004003 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004004 struct io_kiocb *nxt = NULL;
4005 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004006
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004007 /* if NO_CANCEL is set, we must still run the work */
4008 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4009 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004010 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004011 }
Jens Axboe31b51512019-01-18 22:56:34 -07004012
Jens Axboe561fb042019-10-24 07:25:42 -06004013 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004014 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4015 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004016 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004017 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004018 /*
4019 * We can get EAGAIN for polled IO even though we're
4020 * forcing a sync submission from here, since we can't
4021 * wait for request slots on the block side.
4022 */
4023 if (ret != -EAGAIN)
4024 break;
4025 cond_resched();
4026 } while (1);
4027 }
Jens Axboe31b51512019-01-18 22:56:34 -07004028
Jens Axboe561fb042019-10-24 07:25:42 -06004029 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004030 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004031
Jens Axboe561fb042019-10-24 07:25:42 -06004032 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004033 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004034 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004035 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004036 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004037
Jens Axboe561fb042019-10-24 07:25:42 -06004038 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004039 if (!ret && nxt)
4040 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004041}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004042
Jens Axboe15b71ab2019-12-11 11:20:36 -07004043static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004044{
Jens Axboed3656342019-12-18 09:50:26 -07004045 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004046 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004047 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4048 return 0;
4049 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004050}
4051
Jens Axboe65e19f52019-10-26 07:20:21 -06004052static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4053 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004054{
Jens Axboe65e19f52019-10-26 07:20:21 -06004055 struct fixed_file_table *table;
4056
Jens Axboe05f3fb32019-12-09 11:22:50 -07004057 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4058 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004059}
4060
Jens Axboe3529d8c2019-12-19 18:24:38 -07004061static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4062 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004063{
Jackie Liua197f662019-11-08 08:09:12 -07004064 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004065 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004066 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004067
Jens Axboe3529d8c2019-12-19 18:24:38 -07004068 flags = READ_ONCE(sqe->flags);
4069 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004070
Jackie Liu4fe2c962019-09-09 20:50:40 +08004071 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06004072 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06004073
Jens Axboed3656342019-12-18 09:50:26 -07004074 if (!io_req_needs_file(req, fd))
4075 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004076
4077 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004078 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004079 (unsigned) fd >= ctx->nr_user_files))
4080 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004081 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004082 req->file = io_file_from_index(ctx, fd);
4083 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004084 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004085 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004086 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004087 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004088 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004089 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004090 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004091 req->file = io_file_get(state, fd);
4092 if (unlikely(!req->file))
4093 return -EBADF;
4094 }
4095
4096 return 0;
4097}
4098
Jackie Liua197f662019-11-08 08:09:12 -07004099static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004100{
Jens Axboefcb323c2019-10-24 12:39:47 -06004101 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004102 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004103
Jens Axboeb5dba592019-12-11 14:02:38 -07004104 if (!req->ring_file)
4105 return -EBADF;
4106
Jens Axboefcb323c2019-10-24 12:39:47 -06004107 rcu_read_lock();
4108 spin_lock_irq(&ctx->inflight_lock);
4109 /*
4110 * We use the f_ops->flush() handler to ensure that we can flush
4111 * out work accessing these files if the fd is closed. Check if
4112 * the fd has changed since we started down this path, and disallow
4113 * this operation if it has.
4114 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004115 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004116 list_add(&req->inflight_entry, &ctx->inflight_list);
4117 req->flags |= REQ_F_INFLIGHT;
4118 req->work.files = current->files;
4119 ret = 0;
4120 }
4121 spin_unlock_irq(&ctx->inflight_lock);
4122 rcu_read_unlock();
4123
4124 return ret;
4125}
4126
Jens Axboe2665abf2019-11-05 12:40:47 -07004127static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4128{
Jens Axboead8a48a2019-11-15 08:49:11 -07004129 struct io_timeout_data *data = container_of(timer,
4130 struct io_timeout_data, timer);
4131 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004132 struct io_ring_ctx *ctx = req->ctx;
4133 struct io_kiocb *prev = NULL;
4134 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004135
4136 spin_lock_irqsave(&ctx->completion_lock, flags);
4137
4138 /*
4139 * We don't expect the list to be empty, that will only happen if we
4140 * race with the completion of the linked work.
4141 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004142 if (!list_empty(&req->link_list)) {
4143 prev = list_entry(req->link_list.prev, struct io_kiocb,
4144 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004145 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004146 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004147 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4148 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004149 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004150 }
4151
4152 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4153
4154 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004155 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004156 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4157 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004158 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004159 } else {
4160 io_cqring_add_event(req, -ETIME);
4161 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004162 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004163 return HRTIMER_NORESTART;
4164}
4165
Jens Axboead8a48a2019-11-15 08:49:11 -07004166static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004167{
Jens Axboe76a46e02019-11-10 23:34:16 -07004168 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004169
Jens Axboe76a46e02019-11-10 23:34:16 -07004170 /*
4171 * If the list is now empty, then our linked request finished before
4172 * we got a chance to setup the timer
4173 */
4174 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004175 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004176 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004177
Jens Axboead8a48a2019-11-15 08:49:11 -07004178 data->timer.function = io_link_timeout_fn;
4179 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4180 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004181 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004182 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004183
Jens Axboe2665abf2019-11-05 12:40:47 -07004184 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004185 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004186}
4187
Jens Axboead8a48a2019-11-15 08:49:11 -07004188static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004189{
4190 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004191
Jens Axboe2665abf2019-11-05 12:40:47 -07004192 if (!(req->flags & REQ_F_LINK))
4193 return NULL;
4194
Pavel Begunkov44932332019-12-05 16:16:35 +03004195 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4196 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004197 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004198 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004199
Jens Axboe76a46e02019-11-10 23:34:16 -07004200 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004201 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004202}
4203
Jens Axboe3529d8c2019-12-19 18:24:38 -07004204static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004205{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004206 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004207 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004208 int ret;
4209
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004210again:
4211 linked_timeout = io_prep_linked_timeout(req);
4212
Jens Axboe3529d8c2019-12-19 18:24:38 -07004213 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004214
4215 /*
4216 * We async punt it if the file wasn't marked NOWAIT, or if the file
4217 * doesn't support non-blocking read/write attempts
4218 */
4219 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4220 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004221 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4222 ret = io_grab_files(req);
4223 if (ret)
4224 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004225 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004226
4227 /*
4228 * Queued up for async execution, worker will release
4229 * submit reference when the iocb is actually submitted.
4230 */
4231 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004232 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004233 }
Jens Axboee65ef562019-03-12 10:16:44 -06004234
Jens Axboefcb323c2019-10-24 12:39:47 -06004235err:
Jens Axboee65ef562019-03-12 10:16:44 -06004236 /* drop submission reference */
4237 io_put_req(req);
4238
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004239 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004240 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004241 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004242 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004243 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004244 }
4245
Jens Axboee65ef562019-03-12 10:16:44 -06004246 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004247 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004248 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004249 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004250 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004251 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004252done_req:
4253 if (nxt) {
4254 req = nxt;
4255 nxt = NULL;
4256 goto again;
4257 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004258}
4259
Jens Axboe3529d8c2019-12-19 18:24:38 -07004260static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004261{
4262 int ret;
4263
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004264 if (unlikely(req->ctx->drain_next)) {
4265 req->flags |= REQ_F_IO_DRAIN;
4266 req->ctx->drain_next = false;
4267 }
4268 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
4269
Jens Axboe3529d8c2019-12-19 18:24:38 -07004270 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004271 if (ret) {
4272 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004273 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004274 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004275 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004276 }
Jens Axboece35a472019-12-17 08:04:44 -07004277 } else if ((req->flags & REQ_F_FORCE_ASYNC) &&
4278 !io_wq_current_is_worker()) {
4279 /*
4280 * Never try inline submit of IOSQE_ASYNC is set, go straight
4281 * to async execution.
4282 */
4283 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4284 io_queue_async_work(req);
4285 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004286 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004287 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004288}
4289
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004290static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004291{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004292 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004293 io_cqring_add_event(req, -ECANCELED);
4294 io_double_put_req(req);
4295 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004296 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004297}
4298
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004299#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004300 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004301
Jens Axboe3529d8c2019-12-19 18:24:38 -07004302static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4303 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004304{
Jackie Liua197f662019-11-08 08:09:12 -07004305 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004306 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004307 int ret;
4308
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004309 sqe_flags = READ_ONCE(sqe->flags);
4310
Jens Axboe9e645e112019-05-10 16:07:28 -06004311 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004312 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004313 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004314 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004315 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004316 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004317 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004318
Jens Axboe3529d8c2019-12-19 18:24:38 -07004319 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004320 if (unlikely(ret)) {
4321err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004322 io_cqring_add_event(req, ret);
4323 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004324 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004325 }
4326
Jens Axboe9e645e112019-05-10 16:07:28 -06004327 /*
4328 * If we already have a head request, queue this one for async
4329 * submittal once the head completes. If we don't have a head but
4330 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4331 * submitted sync once the chain is complete. If none of those
4332 * conditions are true (normal request), then just queue it.
4333 */
4334 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004335 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004336
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004337 if (sqe_flags & IOSQE_IO_DRAIN)
Pavel Begunkov9d763772019-12-17 02:22:07 +03004338 head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004339
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004340 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004341 req->flags |= REQ_F_HARDLINK;
4342
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004343 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004344 ret = -EAGAIN;
4345 goto err_req;
4346 }
4347
Jens Axboe3529d8c2019-12-19 18:24:38 -07004348 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004349 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004350 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004351 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004352 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004353 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004354 trace_io_uring_link(ctx, req, head);
4355 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004356
4357 /* last request of a link, enqueue the link */
4358 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4359 io_queue_link_head(head);
4360 *link = NULL;
4361 }
4362 } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004363 req->flags |= REQ_F_LINK;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004364 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004365 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004366
Jens Axboe9e645e112019-05-10 16:07:28 -06004367 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004368 ret = io_req_defer_prep(req, sqe);
4369 if (ret)
4370 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004371 *link = req;
4372 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004373 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004374 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004375
4376 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004377}
4378
Jens Axboe9a56a232019-01-09 09:06:50 -07004379/*
4380 * Batched submission is done, ensure local IO is flushed out.
4381 */
4382static void io_submit_state_end(struct io_submit_state *state)
4383{
4384 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004385 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004386 if (state->free_reqs)
4387 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4388 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004389}
4390
4391/*
4392 * Start submission side cache.
4393 */
4394static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004395 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004396{
4397 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004398 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004399 state->file = NULL;
4400 state->ios_left = max_ios;
4401}
4402
Jens Axboe2b188cc2019-01-07 10:46:33 -07004403static void io_commit_sqring(struct io_ring_ctx *ctx)
4404{
Hristo Venev75b28af2019-08-26 17:23:46 +00004405 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004406
Hristo Venev75b28af2019-08-26 17:23:46 +00004407 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004408 /*
4409 * Ensure any loads from the SQEs are done at this point,
4410 * since once we write the new head, the application could
4411 * write new data to them.
4412 */
Hristo Venev75b28af2019-08-26 17:23:46 +00004413 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004414 }
4415}
4416
4417/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004418 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004419 * that is mapped by userspace. This means that care needs to be taken to
4420 * ensure that reads are stable, as we cannot rely on userspace always
4421 * being a good citizen. If members of the sqe are validated and then later
4422 * used, it's important that those reads are done through READ_ONCE() to
4423 * prevent a re-load down the line.
4424 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004425static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4426 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004427{
Hristo Venev75b28af2019-08-26 17:23:46 +00004428 struct io_rings *rings = ctx->rings;
4429 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004430 unsigned head;
4431
4432 /*
4433 * The cached sq head (or cq tail) serves two purposes:
4434 *
4435 * 1) allows us to batch the cost of updating the user visible
4436 * head updates.
4437 * 2) allows the kernel side to track the head on its own, even
4438 * though the application is the one updating it.
4439 */
4440 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02004441 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004442 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004443 return false;
4444
Hristo Venev75b28af2019-08-26 17:23:46 +00004445 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004446 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004447 /*
4448 * All io need record the previous position, if LINK vs DARIN,
4449 * it can be used to mark the position of the first IO in the
4450 * link list.
4451 */
4452 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004453 *sqe_ptr = &ctx->sq_sqes[head];
4454 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4455 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004456 ctx->cached_sq_head++;
4457 return true;
4458 }
4459
4460 /* drop invalid entries */
4461 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004462 ctx->cached_sq_dropped++;
4463 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004464 return false;
4465}
4466
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004467static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004468 struct file *ring_file, int ring_fd,
4469 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004470{
4471 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004472 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004473 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004474 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004475
Jens Axboec4a2ed72019-11-21 21:01:26 -07004476 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004477 if (test_bit(0, &ctx->sq_check_overflow)) {
4478 if (!list_empty(&ctx->cq_overflow_list) &&
4479 !io_cqring_overflow_flush(ctx, false))
4480 return -EBUSY;
4481 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004482
4483 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004484 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004485 statep = &state;
4486 }
4487
4488 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004489 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004490 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004491
Pavel Begunkov196be952019-11-07 01:41:06 +03004492 req = io_get_req(ctx, statep);
4493 if (unlikely(!req)) {
4494 if (!submitted)
4495 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004496 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004497 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004498 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03004499 __io_free_req(req);
4500 break;
4501 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004502
Jens Axboed3656342019-12-18 09:50:26 -07004503 /* will complete beyond this point, count as submitted */
4504 submitted++;
4505
4506 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4507 io_cqring_add_event(req, -EINVAL);
4508 io_double_put_req(req);
4509 break;
4510 }
4511
4512 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004513 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4514 if (!mm_fault) {
4515 use_mm(ctx->sqo_mm);
4516 *mm = ctx->sqo_mm;
4517 }
4518 }
4519
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004520 req->ring_file = ring_file;
4521 req->ring_fd = ring_fd;
4522 req->has_user = *mm != NULL;
4523 req->in_async = async;
4524 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07004525 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004526 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004527 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004528 }
4529
Jens Axboe9e645e112019-05-10 16:07:28 -06004530 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004531 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004532 if (statep)
4533 io_submit_state_end(&state);
4534
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004535 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4536 io_commit_sqring(ctx);
4537
Jens Axboe6c271ce2019-01-10 11:22:30 -07004538 return submitted;
4539}
4540
4541static int io_sq_thread(void *data)
4542{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004543 struct io_ring_ctx *ctx = data;
4544 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004545 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004546 mm_segment_t old_fs;
4547 DEFINE_WAIT(wait);
4548 unsigned inflight;
4549 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004550 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004551
Jens Axboe206aefd2019-11-07 18:27:42 -07004552 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004553
Jens Axboe6c271ce2019-01-10 11:22:30 -07004554 old_fs = get_fs();
4555 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004556 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004557
Jens Axboec1edbf52019-11-10 16:56:04 -07004558 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004559 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004560 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004561
4562 if (inflight) {
4563 unsigned nr_events = 0;
4564
4565 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004566 /*
4567 * inflight is the count of the maximum possible
4568 * entries we submitted, but it can be smaller
4569 * if we dropped some of them. If we don't have
4570 * poll entries available, then we know that we
4571 * have nothing left to poll for. Reset the
4572 * inflight count to zero in that case.
4573 */
4574 mutex_lock(&ctx->uring_lock);
4575 if (!list_empty(&ctx->poll_list))
4576 __io_iopoll_check(ctx, &nr_events, 0);
4577 else
4578 inflight = 0;
4579 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004580 } else {
4581 /*
4582 * Normal IO, just pretend everything completed.
4583 * We don't have to poll completions for that.
4584 */
4585 nr_events = inflight;
4586 }
4587
4588 inflight -= nr_events;
4589 if (!inflight)
4590 timeout = jiffies + ctx->sq_thread_idle;
4591 }
4592
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004593 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004594
4595 /*
4596 * If submit got -EBUSY, flag us as needing the application
4597 * to enter the kernel to reap and flush events.
4598 */
4599 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004600 /*
4601 * We're polling. If we're within the defined idle
4602 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004603 * to sleep. The exception is if we got EBUSY doing
4604 * more IO, we should wait for the application to
4605 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004606 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004607 if (inflight ||
4608 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004609 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004610 continue;
4611 }
4612
4613 /*
4614 * Drop cur_mm before scheduling, we can't hold it for
4615 * long periods (or over schedule()). Do this before
4616 * adding ourselves to the waitqueue, as the unuse/drop
4617 * may sleep.
4618 */
4619 if (cur_mm) {
4620 unuse_mm(cur_mm);
4621 mmput(cur_mm);
4622 cur_mm = NULL;
4623 }
4624
4625 prepare_to_wait(&ctx->sqo_wait, &wait,
4626 TASK_INTERRUPTIBLE);
4627
4628 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004629 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004630 /* make sure to read SQ tail after writing flags */
4631 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004632
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004633 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004634 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004635 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004636 finish_wait(&ctx->sqo_wait, &wait);
4637 break;
4638 }
4639 if (signal_pending(current))
4640 flush_signals(current);
4641 schedule();
4642 finish_wait(&ctx->sqo_wait, &wait);
4643
Hristo Venev75b28af2019-08-26 17:23:46 +00004644 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004645 continue;
4646 }
4647 finish_wait(&ctx->sqo_wait, &wait);
4648
Hristo Venev75b28af2019-08-26 17:23:46 +00004649 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004650 }
4651
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004652 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004653 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004654 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004655 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004656 if (ret > 0)
4657 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004658 }
4659
4660 set_fs(old_fs);
4661 if (cur_mm) {
4662 unuse_mm(cur_mm);
4663 mmput(cur_mm);
4664 }
Jens Axboe181e4482019-11-25 08:52:30 -07004665 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004666
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004667 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004668
Jens Axboe6c271ce2019-01-10 11:22:30 -07004669 return 0;
4670}
4671
Jens Axboebda52162019-09-24 13:47:15 -06004672struct io_wait_queue {
4673 struct wait_queue_entry wq;
4674 struct io_ring_ctx *ctx;
4675 unsigned to_wait;
4676 unsigned nr_timeouts;
4677};
4678
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004679static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004680{
4681 struct io_ring_ctx *ctx = iowq->ctx;
4682
4683 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004684 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004685 * started waiting. For timeouts, we always want to return to userspace,
4686 * regardless of event count.
4687 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004688 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004689 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4690}
4691
4692static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4693 int wake_flags, void *key)
4694{
4695 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4696 wq);
4697
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004698 /* use noflush == true, as we can't safely rely on locking context */
4699 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004700 return -1;
4701
4702 return autoremove_wake_function(curr, mode, wake_flags, key);
4703}
4704
Jens Axboe2b188cc2019-01-07 10:46:33 -07004705/*
4706 * Wait until events become available, if we don't already have some. The
4707 * application must reap them itself, as they reside on the shared cq ring.
4708 */
4709static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4710 const sigset_t __user *sig, size_t sigsz)
4711{
Jens Axboebda52162019-09-24 13:47:15 -06004712 struct io_wait_queue iowq = {
4713 .wq = {
4714 .private = current,
4715 .func = io_wake_function,
4716 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4717 },
4718 .ctx = ctx,
4719 .to_wait = min_events,
4720 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004721 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004722 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004723
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004724 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004725 return 0;
4726
4727 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004728#ifdef CONFIG_COMPAT
4729 if (in_compat_syscall())
4730 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004731 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004732 else
4733#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004734 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004735
Jens Axboe2b188cc2019-01-07 10:46:33 -07004736 if (ret)
4737 return ret;
4738 }
4739
Jens Axboebda52162019-09-24 13:47:15 -06004740 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004741 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004742 do {
4743 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4744 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004745 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004746 break;
4747 schedule();
4748 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004749 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004750 break;
4751 }
4752 } while (1);
4753 finish_wait(&ctx->wait, &iowq.wq);
4754
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004755 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004756
Hristo Venev75b28af2019-08-26 17:23:46 +00004757 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004758}
4759
Jens Axboe6b063142019-01-10 22:13:58 -07004760static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4761{
4762#if defined(CONFIG_UNIX)
4763 if (ctx->ring_sock) {
4764 struct sock *sock = ctx->ring_sock->sk;
4765 struct sk_buff *skb;
4766
4767 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4768 kfree_skb(skb);
4769 }
4770#else
4771 int i;
4772
Jens Axboe65e19f52019-10-26 07:20:21 -06004773 for (i = 0; i < ctx->nr_user_files; i++) {
4774 struct file *file;
4775
4776 file = io_file_from_index(ctx, i);
4777 if (file)
4778 fput(file);
4779 }
Jens Axboe6b063142019-01-10 22:13:58 -07004780#endif
4781}
4782
Jens Axboe05f3fb32019-12-09 11:22:50 -07004783static void io_file_ref_kill(struct percpu_ref *ref)
4784{
4785 struct fixed_file_data *data;
4786
4787 data = container_of(ref, struct fixed_file_data, refs);
4788 complete(&data->done);
4789}
4790
Jens Axboe6b063142019-01-10 22:13:58 -07004791static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4792{
Jens Axboe05f3fb32019-12-09 11:22:50 -07004793 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06004794 unsigned nr_tables, i;
4795
Jens Axboe05f3fb32019-12-09 11:22:50 -07004796 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07004797 return -ENXIO;
4798
Jens Axboe05f3fb32019-12-09 11:22:50 -07004799 /* protect against inflight atomic switch, which drops the ref */
4800 flush_work(&data->ref_work);
4801 percpu_ref_get(&data->refs);
4802 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
4803 wait_for_completion(&data->done);
4804 percpu_ref_put(&data->refs);
4805 percpu_ref_exit(&data->refs);
4806
Jens Axboe6b063142019-01-10 22:13:58 -07004807 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004808 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4809 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004810 kfree(data->table[i].files);
4811 kfree(data->table);
4812 kfree(data);
4813 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004814 ctx->nr_user_files = 0;
4815 return 0;
4816}
4817
Jens Axboe6c271ce2019-01-10 11:22:30 -07004818static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4819{
4820 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004821 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004822 /*
4823 * The park is a bit of a work-around, without it we get
4824 * warning spews on shutdown with SQPOLL set and affinity
4825 * set to a single CPU.
4826 */
Jens Axboe06058632019-04-13 09:26:03 -06004827 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004828 kthread_stop(ctx->sqo_thread);
4829 ctx->sqo_thread = NULL;
4830 }
4831}
4832
Jens Axboe6b063142019-01-10 22:13:58 -07004833static void io_finish_async(struct io_ring_ctx *ctx)
4834{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004835 io_sq_thread_stop(ctx);
4836
Jens Axboe561fb042019-10-24 07:25:42 -06004837 if (ctx->io_wq) {
4838 io_wq_destroy(ctx->io_wq);
4839 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004840 }
4841}
4842
4843#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07004844/*
4845 * Ensure the UNIX gc is aware of our file set, so we are certain that
4846 * the io_uring can be safely unregistered on process exit, even if we have
4847 * loops in the file referencing.
4848 */
4849static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4850{
4851 struct sock *sk = ctx->ring_sock->sk;
4852 struct scm_fp_list *fpl;
4853 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004854 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004855
4856 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4857 unsigned long inflight = ctx->user->unix_inflight + nr;
4858
4859 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4860 return -EMFILE;
4861 }
4862
4863 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4864 if (!fpl)
4865 return -ENOMEM;
4866
4867 skb = alloc_skb(0, GFP_KERNEL);
4868 if (!skb) {
4869 kfree(fpl);
4870 return -ENOMEM;
4871 }
4872
4873 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004874
Jens Axboe08a45172019-10-03 08:11:03 -06004875 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004876 fpl->user = get_uid(ctx->user);
4877 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004878 struct file *file = io_file_from_index(ctx, i + offset);
4879
4880 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004881 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004882 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004883 unix_inflight(fpl->user, fpl->fp[nr_files]);
4884 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004885 }
4886
Jens Axboe08a45172019-10-03 08:11:03 -06004887 if (nr_files) {
4888 fpl->max = SCM_MAX_FD;
4889 fpl->count = nr_files;
4890 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004891 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06004892 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4893 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004894
Jens Axboe08a45172019-10-03 08:11:03 -06004895 for (i = 0; i < nr_files; i++)
4896 fput(fpl->fp[i]);
4897 } else {
4898 kfree_skb(skb);
4899 kfree(fpl);
4900 }
Jens Axboe6b063142019-01-10 22:13:58 -07004901
4902 return 0;
4903}
4904
4905/*
4906 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4907 * causes regular reference counting to break down. We rely on the UNIX
4908 * garbage collection to take care of this problem for us.
4909 */
4910static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4911{
4912 unsigned left, total;
4913 int ret = 0;
4914
4915 total = 0;
4916 left = ctx->nr_user_files;
4917 while (left) {
4918 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004919
4920 ret = __io_sqe_files_scm(ctx, this_files, total);
4921 if (ret)
4922 break;
4923 left -= this_files;
4924 total += this_files;
4925 }
4926
4927 if (!ret)
4928 return 0;
4929
4930 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004931 struct file *file = io_file_from_index(ctx, total);
4932
4933 if (file)
4934 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004935 total++;
4936 }
4937
4938 return ret;
4939}
4940#else
4941static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4942{
4943 return 0;
4944}
4945#endif
4946
Jens Axboe65e19f52019-10-26 07:20:21 -06004947static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4948 unsigned nr_files)
4949{
4950 int i;
4951
4952 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004953 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004954 unsigned this_files;
4955
4956 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4957 table->files = kcalloc(this_files, sizeof(struct file *),
4958 GFP_KERNEL);
4959 if (!table->files)
4960 break;
4961 nr_files -= this_files;
4962 }
4963
4964 if (i == nr_tables)
4965 return 0;
4966
4967 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004968 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06004969 kfree(table->files);
4970 }
4971 return 1;
4972}
4973
Jens Axboe05f3fb32019-12-09 11:22:50 -07004974static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06004975{
4976#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06004977 struct sock *sock = ctx->ring_sock->sk;
4978 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4979 struct sk_buff *skb;
4980 int i;
4981
4982 __skb_queue_head_init(&list);
4983
4984 /*
4985 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4986 * remove this entry and rearrange the file array.
4987 */
4988 skb = skb_dequeue(head);
4989 while (skb) {
4990 struct scm_fp_list *fp;
4991
4992 fp = UNIXCB(skb).fp;
4993 for (i = 0; i < fp->count; i++) {
4994 int left;
4995
4996 if (fp->fp[i] != file)
4997 continue;
4998
4999 unix_notinflight(fp->user, fp->fp[i]);
5000 left = fp->count - 1 - i;
5001 if (left) {
5002 memmove(&fp->fp[i], &fp->fp[i + 1],
5003 left * sizeof(struct file *));
5004 }
5005 fp->count--;
5006 if (!fp->count) {
5007 kfree_skb(skb);
5008 skb = NULL;
5009 } else {
5010 __skb_queue_tail(&list, skb);
5011 }
5012 fput(file);
5013 file = NULL;
5014 break;
5015 }
5016
5017 if (!file)
5018 break;
5019
5020 __skb_queue_tail(&list, skb);
5021
5022 skb = skb_dequeue(head);
5023 }
5024
5025 if (skb_peek(&list)) {
5026 spin_lock_irq(&head->lock);
5027 while ((skb = __skb_dequeue(&list)) != NULL)
5028 __skb_queue_tail(head, skb);
5029 spin_unlock_irq(&head->lock);
5030 }
5031#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005032 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005033#endif
5034}
5035
Jens Axboe05f3fb32019-12-09 11:22:50 -07005036struct io_file_put {
5037 struct llist_node llist;
5038 struct file *file;
5039 struct completion *done;
5040};
5041
5042static void io_ring_file_ref_switch(struct work_struct *work)
5043{
5044 struct io_file_put *pfile, *tmp;
5045 struct fixed_file_data *data;
5046 struct llist_node *node;
5047
5048 data = container_of(work, struct fixed_file_data, ref_work);
5049
5050 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5051 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5052 io_ring_file_put(data->ctx, pfile->file);
5053 if (pfile->done)
5054 complete(pfile->done);
5055 else
5056 kfree(pfile);
5057 }
5058 }
5059
5060 percpu_ref_get(&data->refs);
5061 percpu_ref_switch_to_percpu(&data->refs);
5062}
5063
5064static void io_file_data_ref_zero(struct percpu_ref *ref)
5065{
5066 struct fixed_file_data *data;
5067
5068 data = container_of(ref, struct fixed_file_data, refs);
5069
5070 /* we can't safely switch from inside this context, punt to wq */
5071 queue_work(system_wq, &data->ref_work);
5072}
5073
5074static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5075 unsigned nr_args)
5076{
5077 __s32 __user *fds = (__s32 __user *) arg;
5078 unsigned nr_tables;
5079 struct file *file;
5080 int fd, ret = 0;
5081 unsigned i;
5082
5083 if (ctx->file_data)
5084 return -EBUSY;
5085 if (!nr_args)
5086 return -EINVAL;
5087 if (nr_args > IORING_MAX_FIXED_FILES)
5088 return -EMFILE;
5089
5090 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5091 if (!ctx->file_data)
5092 return -ENOMEM;
5093 ctx->file_data->ctx = ctx;
5094 init_completion(&ctx->file_data->done);
5095
5096 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5097 ctx->file_data->table = kcalloc(nr_tables,
5098 sizeof(struct fixed_file_table),
5099 GFP_KERNEL);
5100 if (!ctx->file_data->table) {
5101 kfree(ctx->file_data);
5102 ctx->file_data = NULL;
5103 return -ENOMEM;
5104 }
5105
5106 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5107 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5108 kfree(ctx->file_data->table);
5109 kfree(ctx->file_data);
5110 ctx->file_data = NULL;
5111 return -ENOMEM;
5112 }
5113 ctx->file_data->put_llist.first = NULL;
5114 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5115
5116 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5117 percpu_ref_exit(&ctx->file_data->refs);
5118 kfree(ctx->file_data->table);
5119 kfree(ctx->file_data);
5120 ctx->file_data = NULL;
5121 return -ENOMEM;
5122 }
5123
5124 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5125 struct fixed_file_table *table;
5126 unsigned index;
5127
5128 ret = -EFAULT;
5129 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5130 break;
5131 /* allow sparse sets */
5132 if (fd == -1) {
5133 ret = 0;
5134 continue;
5135 }
5136
5137 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5138 index = i & IORING_FILE_TABLE_MASK;
5139 file = fget(fd);
5140
5141 ret = -EBADF;
5142 if (!file)
5143 break;
5144
5145 /*
5146 * Don't allow io_uring instances to be registered. If UNIX
5147 * isn't enabled, then this causes a reference cycle and this
5148 * instance can never get freed. If UNIX is enabled we'll
5149 * handle it just fine, but there's still no point in allowing
5150 * a ring fd as it doesn't support regular read/write anyway.
5151 */
5152 if (file->f_op == &io_uring_fops) {
5153 fput(file);
5154 break;
5155 }
5156 ret = 0;
5157 table->files[index] = file;
5158 }
5159
5160 if (ret) {
5161 for (i = 0; i < ctx->nr_user_files; i++) {
5162 file = io_file_from_index(ctx, i);
5163 if (file)
5164 fput(file);
5165 }
5166 for (i = 0; i < nr_tables; i++)
5167 kfree(ctx->file_data->table[i].files);
5168
5169 kfree(ctx->file_data->table);
5170 kfree(ctx->file_data);
5171 ctx->file_data = NULL;
5172 ctx->nr_user_files = 0;
5173 return ret;
5174 }
5175
5176 ret = io_sqe_files_scm(ctx);
5177 if (ret)
5178 io_sqe_files_unregister(ctx);
5179
5180 return ret;
5181}
5182
Jens Axboec3a31e62019-10-03 13:59:56 -06005183static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5184 int index)
5185{
5186#if defined(CONFIG_UNIX)
5187 struct sock *sock = ctx->ring_sock->sk;
5188 struct sk_buff_head *head = &sock->sk_receive_queue;
5189 struct sk_buff *skb;
5190
5191 /*
5192 * See if we can merge this file into an existing skb SCM_RIGHTS
5193 * file set. If there's no room, fall back to allocating a new skb
5194 * and filling it in.
5195 */
5196 spin_lock_irq(&head->lock);
5197 skb = skb_peek(head);
5198 if (skb) {
5199 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5200
5201 if (fpl->count < SCM_MAX_FD) {
5202 __skb_unlink(skb, head);
5203 spin_unlock_irq(&head->lock);
5204 fpl->fp[fpl->count] = get_file(file);
5205 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5206 fpl->count++;
5207 spin_lock_irq(&head->lock);
5208 __skb_queue_head(head, skb);
5209 } else {
5210 skb = NULL;
5211 }
5212 }
5213 spin_unlock_irq(&head->lock);
5214
5215 if (skb) {
5216 fput(file);
5217 return 0;
5218 }
5219
5220 return __io_sqe_files_scm(ctx, 1, index);
5221#else
5222 return 0;
5223#endif
5224}
5225
Jens Axboe05f3fb32019-12-09 11:22:50 -07005226static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005227{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005228 struct fixed_file_data *data;
5229
5230 data = container_of(ref, struct fixed_file_data, refs);
5231 clear_bit(FFD_F_ATOMIC, &data->state);
5232}
5233
5234static bool io_queue_file_removal(struct fixed_file_data *data,
5235 struct file *file)
5236{
5237 struct io_file_put *pfile, pfile_stack;
5238 DECLARE_COMPLETION_ONSTACK(done);
5239
5240 /*
5241 * If we fail allocating the struct we need for doing async reomval
5242 * of this file, just punt to sync and wait for it.
5243 */
5244 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5245 if (!pfile) {
5246 pfile = &pfile_stack;
5247 pfile->done = &done;
5248 }
5249
5250 pfile->file = file;
5251 llist_add(&pfile->llist, &data->put_llist);
5252
5253 if (pfile == &pfile_stack) {
5254 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5255 percpu_ref_put(&data->refs);
5256 percpu_ref_switch_to_atomic(&data->refs,
5257 io_atomic_switch);
5258 }
5259 wait_for_completion(&done);
5260 flush_work(&data->ref_work);
5261 return false;
5262 }
5263
5264 return true;
5265}
5266
5267static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5268 struct io_uring_files_update *up,
5269 unsigned nr_args)
5270{
5271 struct fixed_file_data *data = ctx->file_data;
5272 bool ref_switch = false;
5273 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005274 __s32 __user *fds;
5275 int fd, i, err;
5276 __u32 done;
5277
Jens Axboe05f3fb32019-12-09 11:22:50 -07005278 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005279 return -EOVERFLOW;
5280 if (done > ctx->nr_user_files)
5281 return -EINVAL;
5282
5283 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005284 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005285 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005286 struct fixed_file_table *table;
5287 unsigned index;
5288
Jens Axboec3a31e62019-10-03 13:59:56 -06005289 err = 0;
5290 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5291 err = -EFAULT;
5292 break;
5293 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005294 i = array_index_nospec(up->offset, ctx->nr_user_files);
5295 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005296 index = i & IORING_FILE_TABLE_MASK;
5297 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005298 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005299 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005300 if (io_queue_file_removal(data, file))
5301 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005302 }
5303 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005304 file = fget(fd);
5305 if (!file) {
5306 err = -EBADF;
5307 break;
5308 }
5309 /*
5310 * Don't allow io_uring instances to be registered. If
5311 * UNIX isn't enabled, then this causes a reference
5312 * cycle and this instance can never get freed. If UNIX
5313 * is enabled we'll handle it just fine, but there's
5314 * still no point in allowing a ring fd as it doesn't
5315 * support regular read/write anyway.
5316 */
5317 if (file->f_op == &io_uring_fops) {
5318 fput(file);
5319 err = -EBADF;
5320 break;
5321 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005322 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005323 err = io_sqe_file_register(ctx, file, i);
5324 if (err)
5325 break;
5326 }
5327 nr_args--;
5328 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005329 up->offset++;
5330 }
5331
5332 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5333 percpu_ref_put(&data->refs);
5334 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005335 }
5336
5337 return done ? done : err;
5338}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005339static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5340 unsigned nr_args)
5341{
5342 struct io_uring_files_update up;
5343
5344 if (!ctx->file_data)
5345 return -ENXIO;
5346 if (!nr_args)
5347 return -EINVAL;
5348 if (copy_from_user(&up, arg, sizeof(up)))
5349 return -EFAULT;
5350 if (up.resv)
5351 return -EINVAL;
5352
5353 return __io_sqe_files_update(ctx, &up, nr_args);
5354}
Jens Axboec3a31e62019-10-03 13:59:56 -06005355
Jens Axboe7d723062019-11-12 22:31:31 -07005356static void io_put_work(struct io_wq_work *work)
5357{
5358 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5359
5360 io_put_req(req);
5361}
5362
5363static void io_get_work(struct io_wq_work *work)
5364{
5365 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5366
5367 refcount_inc(&req->refs);
5368}
5369
Jens Axboe6c271ce2019-01-10 11:22:30 -07005370static int io_sq_offload_start(struct io_ring_ctx *ctx,
5371 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005372{
Jens Axboe576a3472019-11-25 08:49:20 -07005373 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005374 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005375 int ret;
5376
Jens Axboe6c271ce2019-01-10 11:22:30 -07005377 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005378 mmgrab(current->mm);
5379 ctx->sqo_mm = current->mm;
5380
Jens Axboe6c271ce2019-01-10 11:22:30 -07005381 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005382 ret = -EPERM;
5383 if (!capable(CAP_SYS_ADMIN))
5384 goto err;
5385
Jens Axboe917257d2019-04-13 09:28:55 -06005386 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5387 if (!ctx->sq_thread_idle)
5388 ctx->sq_thread_idle = HZ;
5389
Jens Axboe6c271ce2019-01-10 11:22:30 -07005390 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005391 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005392
Jens Axboe917257d2019-04-13 09:28:55 -06005393 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005394 if (cpu >= nr_cpu_ids)
5395 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005396 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005397 goto err;
5398
Jens Axboe6c271ce2019-01-10 11:22:30 -07005399 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5400 ctx, cpu,
5401 "io_uring-sq");
5402 } else {
5403 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5404 "io_uring-sq");
5405 }
5406 if (IS_ERR(ctx->sqo_thread)) {
5407 ret = PTR_ERR(ctx->sqo_thread);
5408 ctx->sqo_thread = NULL;
5409 goto err;
5410 }
5411 wake_up_process(ctx->sqo_thread);
5412 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5413 /* Can't have SQ_AFF without SQPOLL */
5414 ret = -EINVAL;
5415 goto err;
5416 }
5417
Jens Axboe576a3472019-11-25 08:49:20 -07005418 data.mm = ctx->sqo_mm;
5419 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005420 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005421 data.get_work = io_get_work;
5422 data.put_work = io_put_work;
5423
Jens Axboe561fb042019-10-24 07:25:42 -06005424 /* Do QD, or 4 * CPUS, whatever is smallest */
5425 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005426 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005427 if (IS_ERR(ctx->io_wq)) {
5428 ret = PTR_ERR(ctx->io_wq);
5429 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005430 goto err;
5431 }
5432
5433 return 0;
5434err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005435 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005436 mmdrop(ctx->sqo_mm);
5437 ctx->sqo_mm = NULL;
5438 return ret;
5439}
5440
5441static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5442{
5443 atomic_long_sub(nr_pages, &user->locked_vm);
5444}
5445
5446static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5447{
5448 unsigned long page_limit, cur_pages, new_pages;
5449
5450 /* Don't allow more pages than we can safely lock */
5451 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5452
5453 do {
5454 cur_pages = atomic_long_read(&user->locked_vm);
5455 new_pages = cur_pages + nr_pages;
5456 if (new_pages > page_limit)
5457 return -ENOMEM;
5458 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5459 new_pages) != cur_pages);
5460
5461 return 0;
5462}
5463
5464static void io_mem_free(void *ptr)
5465{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005466 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005467
Mark Rutland52e04ef2019-04-30 17:30:21 +01005468 if (!ptr)
5469 return;
5470
5471 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005472 if (put_page_testzero(page))
5473 free_compound_page(page);
5474}
5475
5476static void *io_mem_alloc(size_t size)
5477{
5478 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5479 __GFP_NORETRY;
5480
5481 return (void *) __get_free_pages(gfp_flags, get_order(size));
5482}
5483
Hristo Venev75b28af2019-08-26 17:23:46 +00005484static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5485 size_t *sq_offset)
5486{
5487 struct io_rings *rings;
5488 size_t off, sq_array_size;
5489
5490 off = struct_size(rings, cqes, cq_entries);
5491 if (off == SIZE_MAX)
5492 return SIZE_MAX;
5493
5494#ifdef CONFIG_SMP
5495 off = ALIGN(off, SMP_CACHE_BYTES);
5496 if (off == 0)
5497 return SIZE_MAX;
5498#endif
5499
5500 sq_array_size = array_size(sizeof(u32), sq_entries);
5501 if (sq_array_size == SIZE_MAX)
5502 return SIZE_MAX;
5503
5504 if (check_add_overflow(off, sq_array_size, &off))
5505 return SIZE_MAX;
5506
5507 if (sq_offset)
5508 *sq_offset = off;
5509
5510 return off;
5511}
5512
Jens Axboe2b188cc2019-01-07 10:46:33 -07005513static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5514{
Hristo Venev75b28af2019-08-26 17:23:46 +00005515 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005516
Hristo Venev75b28af2019-08-26 17:23:46 +00005517 pages = (size_t)1 << get_order(
5518 rings_size(sq_entries, cq_entries, NULL));
5519 pages += (size_t)1 << get_order(
5520 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005521
Hristo Venev75b28af2019-08-26 17:23:46 +00005522 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005523}
5524
Jens Axboeedafcce2019-01-09 09:16:05 -07005525static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5526{
5527 int i, j;
5528
5529 if (!ctx->user_bufs)
5530 return -ENXIO;
5531
5532 for (i = 0; i < ctx->nr_user_bufs; i++) {
5533 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5534
5535 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005536 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005537
5538 if (ctx->account_mem)
5539 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005540 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005541 imu->nr_bvecs = 0;
5542 }
5543
5544 kfree(ctx->user_bufs);
5545 ctx->user_bufs = NULL;
5546 ctx->nr_user_bufs = 0;
5547 return 0;
5548}
5549
5550static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5551 void __user *arg, unsigned index)
5552{
5553 struct iovec __user *src;
5554
5555#ifdef CONFIG_COMPAT
5556 if (ctx->compat) {
5557 struct compat_iovec __user *ciovs;
5558 struct compat_iovec ciov;
5559
5560 ciovs = (struct compat_iovec __user *) arg;
5561 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5562 return -EFAULT;
5563
Jens Axboed55e5f52019-12-11 16:12:15 -07005564 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005565 dst->iov_len = ciov.iov_len;
5566 return 0;
5567 }
5568#endif
5569 src = (struct iovec __user *) arg;
5570 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5571 return -EFAULT;
5572 return 0;
5573}
5574
5575static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5576 unsigned nr_args)
5577{
5578 struct vm_area_struct **vmas = NULL;
5579 struct page **pages = NULL;
5580 int i, j, got_pages = 0;
5581 int ret = -EINVAL;
5582
5583 if (ctx->user_bufs)
5584 return -EBUSY;
5585 if (!nr_args || nr_args > UIO_MAXIOV)
5586 return -EINVAL;
5587
5588 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5589 GFP_KERNEL);
5590 if (!ctx->user_bufs)
5591 return -ENOMEM;
5592
5593 for (i = 0; i < nr_args; i++) {
5594 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5595 unsigned long off, start, end, ubuf;
5596 int pret, nr_pages;
5597 struct iovec iov;
5598 size_t size;
5599
5600 ret = io_copy_iov(ctx, &iov, arg, i);
5601 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005602 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005603
5604 /*
5605 * Don't impose further limits on the size and buffer
5606 * constraints here, we'll -EINVAL later when IO is
5607 * submitted if they are wrong.
5608 */
5609 ret = -EFAULT;
5610 if (!iov.iov_base || !iov.iov_len)
5611 goto err;
5612
5613 /* arbitrary limit, but we need something */
5614 if (iov.iov_len > SZ_1G)
5615 goto err;
5616
5617 ubuf = (unsigned long) iov.iov_base;
5618 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5619 start = ubuf >> PAGE_SHIFT;
5620 nr_pages = end - start;
5621
5622 if (ctx->account_mem) {
5623 ret = io_account_mem(ctx->user, nr_pages);
5624 if (ret)
5625 goto err;
5626 }
5627
5628 ret = 0;
5629 if (!pages || nr_pages > got_pages) {
5630 kfree(vmas);
5631 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005632 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005633 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005634 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005635 sizeof(struct vm_area_struct *),
5636 GFP_KERNEL);
5637 if (!pages || !vmas) {
5638 ret = -ENOMEM;
5639 if (ctx->account_mem)
5640 io_unaccount_mem(ctx->user, nr_pages);
5641 goto err;
5642 }
5643 got_pages = nr_pages;
5644 }
5645
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005646 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005647 GFP_KERNEL);
5648 ret = -ENOMEM;
5649 if (!imu->bvec) {
5650 if (ctx->account_mem)
5651 io_unaccount_mem(ctx->user, nr_pages);
5652 goto err;
5653 }
5654
5655 ret = 0;
5656 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005657 pret = get_user_pages(ubuf, nr_pages,
5658 FOLL_WRITE | FOLL_LONGTERM,
5659 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005660 if (pret == nr_pages) {
5661 /* don't support file backed memory */
5662 for (j = 0; j < nr_pages; j++) {
5663 struct vm_area_struct *vma = vmas[j];
5664
5665 if (vma->vm_file &&
5666 !is_file_hugepages(vma->vm_file)) {
5667 ret = -EOPNOTSUPP;
5668 break;
5669 }
5670 }
5671 } else {
5672 ret = pret < 0 ? pret : -EFAULT;
5673 }
5674 up_read(&current->mm->mmap_sem);
5675 if (ret) {
5676 /*
5677 * if we did partial map, or found file backed vmas,
5678 * release any pages we did get
5679 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005680 if (pret > 0)
5681 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005682 if (ctx->account_mem)
5683 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005684 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005685 goto err;
5686 }
5687
5688 off = ubuf & ~PAGE_MASK;
5689 size = iov.iov_len;
5690 for (j = 0; j < nr_pages; j++) {
5691 size_t vec_len;
5692
5693 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5694 imu->bvec[j].bv_page = pages[j];
5695 imu->bvec[j].bv_len = vec_len;
5696 imu->bvec[j].bv_offset = off;
5697 off = 0;
5698 size -= vec_len;
5699 }
5700 /* store original address for later verification */
5701 imu->ubuf = ubuf;
5702 imu->len = iov.iov_len;
5703 imu->nr_bvecs = nr_pages;
5704
5705 ctx->nr_user_bufs++;
5706 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005707 kvfree(pages);
5708 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005709 return 0;
5710err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005711 kvfree(pages);
5712 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005713 io_sqe_buffer_unregister(ctx);
5714 return ret;
5715}
5716
Jens Axboe9b402842019-04-11 11:45:41 -06005717static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5718{
5719 __s32 __user *fds = arg;
5720 int fd;
5721
5722 if (ctx->cq_ev_fd)
5723 return -EBUSY;
5724
5725 if (copy_from_user(&fd, fds, sizeof(*fds)))
5726 return -EFAULT;
5727
5728 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5729 if (IS_ERR(ctx->cq_ev_fd)) {
5730 int ret = PTR_ERR(ctx->cq_ev_fd);
5731 ctx->cq_ev_fd = NULL;
5732 return ret;
5733 }
5734
5735 return 0;
5736}
5737
5738static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5739{
5740 if (ctx->cq_ev_fd) {
5741 eventfd_ctx_put(ctx->cq_ev_fd);
5742 ctx->cq_ev_fd = NULL;
5743 return 0;
5744 }
5745
5746 return -ENXIO;
5747}
5748
Jens Axboe2b188cc2019-01-07 10:46:33 -07005749static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5750{
Jens Axboe6b063142019-01-10 22:13:58 -07005751 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005752 if (ctx->sqo_mm)
5753 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005754
5755 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005756 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005757 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005758 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005759
Jens Axboe2b188cc2019-01-07 10:46:33 -07005760#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005761 if (ctx->ring_sock) {
5762 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005763 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005764 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005765#endif
5766
Hristo Venev75b28af2019-08-26 17:23:46 +00005767 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005768 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005769
5770 percpu_ref_exit(&ctx->refs);
5771 if (ctx->account_mem)
5772 io_unaccount_mem(ctx->user,
5773 ring_pages(ctx->sq_entries, ctx->cq_entries));
5774 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07005775 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07005776 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07005777 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07005778 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005779 kfree(ctx);
5780}
5781
5782static __poll_t io_uring_poll(struct file *file, poll_table *wait)
5783{
5784 struct io_ring_ctx *ctx = file->private_data;
5785 __poll_t mask = 0;
5786
5787 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02005788 /*
5789 * synchronizes with barrier from wq_has_sleeper call in
5790 * io_commit_cqring
5791 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005792 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00005793 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
5794 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005795 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08005796 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005797 mask |= EPOLLIN | EPOLLRDNORM;
5798
5799 return mask;
5800}
5801
5802static int io_uring_fasync(int fd, struct file *file, int on)
5803{
5804 struct io_ring_ctx *ctx = file->private_data;
5805
5806 return fasync_helper(fd, file, on, &ctx->cq_fasync);
5807}
5808
5809static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
5810{
5811 mutex_lock(&ctx->uring_lock);
5812 percpu_ref_kill(&ctx->refs);
5813 mutex_unlock(&ctx->uring_lock);
5814
Jens Axboe5262f562019-09-17 12:26:57 -06005815 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005816 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06005817
5818 if (ctx->io_wq)
5819 io_wq_cancel_all(ctx->io_wq);
5820
Jens Axboedef596e2019-01-09 08:59:42 -07005821 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07005822 /* if we failed setting up the ctx, we might not have any rings */
5823 if (ctx->rings)
5824 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07005825 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005826 io_ring_ctx_free(ctx);
5827}
5828
5829static int io_uring_release(struct inode *inode, struct file *file)
5830{
5831 struct io_ring_ctx *ctx = file->private_data;
5832
5833 file->private_data = NULL;
5834 io_ring_ctx_wait_and_kill(ctx);
5835 return 0;
5836}
5837
Jens Axboefcb323c2019-10-24 12:39:47 -06005838static void io_uring_cancel_files(struct io_ring_ctx *ctx,
5839 struct files_struct *files)
5840{
5841 struct io_kiocb *req;
5842 DEFINE_WAIT(wait);
5843
5844 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07005845 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06005846
5847 spin_lock_irq(&ctx->inflight_lock);
5848 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07005849 if (req->work.files != files)
5850 continue;
5851 /* req is being completed, ignore */
5852 if (!refcount_inc_not_zero(&req->refs))
5853 continue;
5854 cancel_req = req;
5855 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06005856 }
Jens Axboe768134d2019-11-10 20:30:53 -07005857 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005858 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07005859 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06005860 spin_unlock_irq(&ctx->inflight_lock);
5861
Jens Axboe768134d2019-11-10 20:30:53 -07005862 /* We need to keep going until we don't find a matching req */
5863 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005864 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005865
5866 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5867 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005868 schedule();
5869 }
Jens Axboe768134d2019-11-10 20:30:53 -07005870 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005871}
5872
5873static int io_uring_flush(struct file *file, void *data)
5874{
5875 struct io_ring_ctx *ctx = file->private_data;
5876
5877 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005878 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5879 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005880 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005881 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005882 return 0;
5883}
5884
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005885static void *io_uring_validate_mmap_request(struct file *file,
5886 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005887{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005888 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005889 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005890 struct page *page;
5891 void *ptr;
5892
5893 switch (offset) {
5894 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005895 case IORING_OFF_CQ_RING:
5896 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005897 break;
5898 case IORING_OFF_SQES:
5899 ptr = ctx->sq_sqes;
5900 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005901 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005902 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005903 }
5904
5905 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005906 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005907 return ERR_PTR(-EINVAL);
5908
5909 return ptr;
5910}
5911
5912#ifdef CONFIG_MMU
5913
5914static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5915{
5916 size_t sz = vma->vm_end - vma->vm_start;
5917 unsigned long pfn;
5918 void *ptr;
5919
5920 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5921 if (IS_ERR(ptr))
5922 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005923
5924 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5925 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5926}
5927
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005928#else /* !CONFIG_MMU */
5929
5930static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5931{
5932 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5933}
5934
5935static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5936{
5937 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5938}
5939
5940static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5941 unsigned long addr, unsigned long len,
5942 unsigned long pgoff, unsigned long flags)
5943{
5944 void *ptr;
5945
5946 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5947 if (IS_ERR(ptr))
5948 return PTR_ERR(ptr);
5949
5950 return (unsigned long) ptr;
5951}
5952
5953#endif /* !CONFIG_MMU */
5954
Jens Axboe2b188cc2019-01-07 10:46:33 -07005955SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5956 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5957 size_t, sigsz)
5958{
5959 struct io_ring_ctx *ctx;
5960 long ret = -EBADF;
5961 int submitted = 0;
5962 struct fd f;
5963
Jens Axboe6c271ce2019-01-10 11:22:30 -07005964 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005965 return -EINVAL;
5966
5967 f = fdget(fd);
5968 if (!f.file)
5969 return -EBADF;
5970
5971 ret = -EOPNOTSUPP;
5972 if (f.file->f_op != &io_uring_fops)
5973 goto out_fput;
5974
5975 ret = -ENXIO;
5976 ctx = f.file->private_data;
5977 if (!percpu_ref_tryget(&ctx->refs))
5978 goto out_fput;
5979
Jens Axboe6c271ce2019-01-10 11:22:30 -07005980 /*
5981 * For SQ polling, the thread will do all submissions and completions.
5982 * Just return the requested submit count, and wake the thread if
5983 * we were asked to.
5984 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005985 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005986 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005987 if (!list_empty_careful(&ctx->cq_overflow_list))
5988 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005989 if (flags & IORING_ENTER_SQ_WAKEUP)
5990 wake_up(&ctx->sqo_wait);
5991 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005992 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005993 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005994
Jens Axboe44d28272020-01-16 19:00:24 -07005995 if (current->mm != ctx->sqo_mm ||
5996 current_cred() != ctx->creds) {
5997 ret = -EPERM;
5998 goto out;
5999 }
6000
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006001 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006002 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006003 /* already have mm, so io_submit_sqes() won't try to grab it */
6004 cur_mm = ctx->sqo_mm;
6005 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6006 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006007 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006008
6009 if (submitted != to_submit)
6010 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006011 }
6012 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006013 unsigned nr_events = 0;
6014
Jens Axboe2b188cc2019-01-07 10:46:33 -07006015 min_complete = min(min_complete, ctx->cq_entries);
6016
Jens Axboedef596e2019-01-09 08:59:42 -07006017 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006018 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006019 } else {
6020 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6021 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006022 }
6023
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006024out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006025 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006026out_fput:
6027 fdput(f);
6028 return submitted ? submitted : ret;
6029}
6030
6031static const struct file_operations io_uring_fops = {
6032 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006033 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006034 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006035#ifndef CONFIG_MMU
6036 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6037 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6038#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006039 .poll = io_uring_poll,
6040 .fasync = io_uring_fasync,
6041};
6042
6043static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6044 struct io_uring_params *p)
6045{
Hristo Venev75b28af2019-08-26 17:23:46 +00006046 struct io_rings *rings;
6047 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006048
Hristo Venev75b28af2019-08-26 17:23:46 +00006049 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6050 if (size == SIZE_MAX)
6051 return -EOVERFLOW;
6052
6053 rings = io_mem_alloc(size);
6054 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006055 return -ENOMEM;
6056
Hristo Venev75b28af2019-08-26 17:23:46 +00006057 ctx->rings = rings;
6058 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6059 rings->sq_ring_mask = p->sq_entries - 1;
6060 rings->cq_ring_mask = p->cq_entries - 1;
6061 rings->sq_ring_entries = p->sq_entries;
6062 rings->cq_ring_entries = p->cq_entries;
6063 ctx->sq_mask = rings->sq_ring_mask;
6064 ctx->cq_mask = rings->cq_ring_mask;
6065 ctx->sq_entries = rings->sq_ring_entries;
6066 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006067
6068 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006069 if (size == SIZE_MAX) {
6070 io_mem_free(ctx->rings);
6071 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006072 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006073 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006074
6075 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006076 if (!ctx->sq_sqes) {
6077 io_mem_free(ctx->rings);
6078 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006079 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006080 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006081
Jens Axboe2b188cc2019-01-07 10:46:33 -07006082 return 0;
6083}
6084
6085/*
6086 * Allocate an anonymous fd, this is what constitutes the application
6087 * visible backing of an io_uring instance. The application mmaps this
6088 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6089 * we have to tie this fd to a socket for file garbage collection purposes.
6090 */
6091static int io_uring_get_fd(struct io_ring_ctx *ctx)
6092{
6093 struct file *file;
6094 int ret;
6095
6096#if defined(CONFIG_UNIX)
6097 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6098 &ctx->ring_sock);
6099 if (ret)
6100 return ret;
6101#endif
6102
6103 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6104 if (ret < 0)
6105 goto err;
6106
6107 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6108 O_RDWR | O_CLOEXEC);
6109 if (IS_ERR(file)) {
6110 put_unused_fd(ret);
6111 ret = PTR_ERR(file);
6112 goto err;
6113 }
6114
6115#if defined(CONFIG_UNIX)
6116 ctx->ring_sock->file = file;
6117#endif
6118 fd_install(ret, file);
6119 return ret;
6120err:
6121#if defined(CONFIG_UNIX)
6122 sock_release(ctx->ring_sock);
6123 ctx->ring_sock = NULL;
6124#endif
6125 return ret;
6126}
6127
6128static int io_uring_create(unsigned entries, struct io_uring_params *p)
6129{
6130 struct user_struct *user = NULL;
6131 struct io_ring_ctx *ctx;
6132 bool account_mem;
6133 int ret;
6134
6135 if (!entries || entries > IORING_MAX_ENTRIES)
6136 return -EINVAL;
6137
6138 /*
6139 * Use twice as many entries for the CQ ring. It's possible for the
6140 * application to drive a higher depth than the size of the SQ ring,
6141 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006142 * some flexibility in overcommitting a bit. If the application has
6143 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6144 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006145 */
6146 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006147 if (p->flags & IORING_SETUP_CQSIZE) {
6148 /*
6149 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6150 * to a power-of-two, if it isn't already. We do NOT impose
6151 * any cq vs sq ring sizing.
6152 */
6153 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
6154 return -EINVAL;
6155 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6156 } else {
6157 p->cq_entries = 2 * p->sq_entries;
6158 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006159
6160 user = get_uid(current_user());
6161 account_mem = !capable(CAP_IPC_LOCK);
6162
6163 if (account_mem) {
6164 ret = io_account_mem(user,
6165 ring_pages(p->sq_entries, p->cq_entries));
6166 if (ret) {
6167 free_uid(user);
6168 return ret;
6169 }
6170 }
6171
6172 ctx = io_ring_ctx_alloc(p);
6173 if (!ctx) {
6174 if (account_mem)
6175 io_unaccount_mem(user, ring_pages(p->sq_entries,
6176 p->cq_entries));
6177 free_uid(user);
6178 return -ENOMEM;
6179 }
6180 ctx->compat = in_compat_syscall();
6181 ctx->account_mem = account_mem;
6182 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006183 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006184
6185 ret = io_allocate_scq_urings(ctx, p);
6186 if (ret)
6187 goto err;
6188
Jens Axboe6c271ce2019-01-10 11:22:30 -07006189 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006190 if (ret)
6191 goto err;
6192
Jens Axboe2b188cc2019-01-07 10:46:33 -07006193 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006194 p->sq_off.head = offsetof(struct io_rings, sq.head);
6195 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6196 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6197 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6198 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6199 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6200 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006201
6202 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006203 p->cq_off.head = offsetof(struct io_rings, cq.head);
6204 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6205 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6206 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6207 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6208 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006209
Jens Axboe044c1ab2019-10-28 09:15:33 -06006210 /*
6211 * Install ring fd as the very last thing, so we don't risk someone
6212 * having closed it before we finish setup
6213 */
6214 ret = io_uring_get_fd(ctx);
6215 if (ret < 0)
6216 goto err;
6217
Jens Axboeda8c9692019-12-02 18:51:26 -07006218 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboeba042912019-12-25 16:33:42 -07006219 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006220 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006221 return ret;
6222err:
6223 io_ring_ctx_wait_and_kill(ctx);
6224 return ret;
6225}
6226
6227/*
6228 * Sets up an aio uring context, and returns the fd. Applications asks for a
6229 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6230 * params structure passed in.
6231 */
6232static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6233{
6234 struct io_uring_params p;
6235 long ret;
6236 int i;
6237
6238 if (copy_from_user(&p, params, sizeof(p)))
6239 return -EFAULT;
6240 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6241 if (p.resv[i])
6242 return -EINVAL;
6243 }
6244
Jens Axboe6c271ce2019-01-10 11:22:30 -07006245 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06006246 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006247 return -EINVAL;
6248
6249 ret = io_uring_create(entries, &p);
6250 if (ret < 0)
6251 return ret;
6252
6253 if (copy_to_user(params, &p, sizeof(p)))
6254 return -EFAULT;
6255
6256 return ret;
6257}
6258
6259SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6260 struct io_uring_params __user *, params)
6261{
6262 return io_uring_setup(entries, params);
6263}
6264
Jens Axboeedafcce2019-01-09 09:16:05 -07006265static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6266 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006267 __releases(ctx->uring_lock)
6268 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006269{
6270 int ret;
6271
Jens Axboe35fa71a2019-04-22 10:23:23 -06006272 /*
6273 * We're inside the ring mutex, if the ref is already dying, then
6274 * someone else killed the ctx or is already going through
6275 * io_uring_register().
6276 */
6277 if (percpu_ref_is_dying(&ctx->refs))
6278 return -ENXIO;
6279
Jens Axboe05f3fb32019-12-09 11:22:50 -07006280 if (opcode != IORING_UNREGISTER_FILES &&
6281 opcode != IORING_REGISTER_FILES_UPDATE) {
6282 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006283
Jens Axboe05f3fb32019-12-09 11:22:50 -07006284 /*
6285 * Drop uring mutex before waiting for references to exit. If
6286 * another thread is currently inside io_uring_enter() it might
6287 * need to grab the uring_lock to make progress. If we hold it
6288 * here across the drain wait, then we can deadlock. It's safe
6289 * to drop the mutex here, since no new references will come in
6290 * after we've killed the percpu ref.
6291 */
6292 mutex_unlock(&ctx->uring_lock);
6293 wait_for_completion(&ctx->completions[0]);
6294 mutex_lock(&ctx->uring_lock);
6295 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006296
6297 switch (opcode) {
6298 case IORING_REGISTER_BUFFERS:
6299 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6300 break;
6301 case IORING_UNREGISTER_BUFFERS:
6302 ret = -EINVAL;
6303 if (arg || nr_args)
6304 break;
6305 ret = io_sqe_buffer_unregister(ctx);
6306 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006307 case IORING_REGISTER_FILES:
6308 ret = io_sqe_files_register(ctx, arg, nr_args);
6309 break;
6310 case IORING_UNREGISTER_FILES:
6311 ret = -EINVAL;
6312 if (arg || nr_args)
6313 break;
6314 ret = io_sqe_files_unregister(ctx);
6315 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006316 case IORING_REGISTER_FILES_UPDATE:
6317 ret = io_sqe_files_update(ctx, arg, nr_args);
6318 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006319 case IORING_REGISTER_EVENTFD:
6320 ret = -EINVAL;
6321 if (nr_args != 1)
6322 break;
6323 ret = io_eventfd_register(ctx, arg);
6324 break;
6325 case IORING_UNREGISTER_EVENTFD:
6326 ret = -EINVAL;
6327 if (arg || nr_args)
6328 break;
6329 ret = io_eventfd_unregister(ctx);
6330 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006331 default:
6332 ret = -EINVAL;
6333 break;
6334 }
6335
Jens Axboe05f3fb32019-12-09 11:22:50 -07006336
6337 if (opcode != IORING_UNREGISTER_FILES &&
6338 opcode != IORING_REGISTER_FILES_UPDATE) {
6339 /* bring the ctx back to life */
6340 reinit_completion(&ctx->completions[0]);
6341 percpu_ref_reinit(&ctx->refs);
6342 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006343 return ret;
6344}
6345
6346SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6347 void __user *, arg, unsigned int, nr_args)
6348{
6349 struct io_ring_ctx *ctx;
6350 long ret = -EBADF;
6351 struct fd f;
6352
6353 f = fdget(fd);
6354 if (!f.file)
6355 return -EBADF;
6356
6357 ret = -EOPNOTSUPP;
6358 if (f.file->f_op != &io_uring_fops)
6359 goto out_fput;
6360
6361 ctx = f.file->private_data;
6362
6363 mutex_lock(&ctx->uring_lock);
6364 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6365 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006366 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6367 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006368out_fput:
6369 fdput(f);
6370 return ret;
6371}
6372
Jens Axboe2b188cc2019-01-07 10:46:33 -07006373static int __init io_uring_init(void)
6374{
Jens Axboed3656342019-12-18 09:50:26 -07006375 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006376 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6377 return 0;
6378};
6379__initcall(io_uring_init);