blob: 27fefb52910c7f3262fefc710d26b6b05d146277 [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 Axboe2b188cc2019-01-07 10:46:33 -070072
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020073#define CREATE_TRACE_POINTS
74#include <trace/events/io_uring.h>
75
Jens Axboe2b188cc2019-01-07 10:46:33 -070076#include <uapi/linux/io_uring.h>
77
78#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060079#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070080
Daniel Xu5277dea2019-09-14 14:23:45 -070081#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060082#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060083
84/*
85 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
86 */
87#define IORING_FILE_TABLE_SHIFT 9
88#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
89#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
90#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070091
92struct io_uring {
93 u32 head ____cacheline_aligned_in_smp;
94 u32 tail ____cacheline_aligned_in_smp;
95};
96
Stefan Bühler1e84b972019-04-24 23:54:16 +020097/*
Hristo Venev75b28af2019-08-26 17:23:46 +000098 * This data is shared with the application through the mmap at offsets
99 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200100 *
101 * The offsets to the member fields are published through struct
102 * io_sqring_offsets when calling io_uring_setup.
103 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000104struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 /*
106 * Head and tail offsets into the ring; the offsets need to be
107 * masked to get valid indices.
108 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000109 * The kernel controls head of the sq ring and the tail of the cq ring,
110 * and the application controls tail of the sq ring and the head of the
111 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000115 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200116 * ring_entries - 1)
117 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 u32 sq_ring_mask, cq_ring_mask;
119 /* Ring sizes (constant, power of 2) */
120 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 /*
122 * Number of invalid entries dropped by the kernel due to
123 * invalid index stored in array
124 *
125 * Written by the kernel, shouldn't be modified by the
126 * application (i.e. get number of "new events" by comparing to
127 * cached value).
128 *
129 * After a new SQ head value was read by the application this
130 * counter includes all submissions that were dropped reaching
131 * the new SQ head (and possibly more).
132 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 /*
135 * Runtime flags
136 *
137 * Written by the kernel, shouldn't be modified by the
138 * application.
139 *
140 * The application needs a full memory barrier before checking
141 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
145 * Number of completion events lost because the queue was full;
146 * this should be avoided by the application by making sure
147 * there are not more requests pending thatn there is space in
148 * the completion queue.
149 *
150 * Written by the kernel, shouldn't be modified by the
151 * application (i.e. get number of "new events" by comparing to
152 * cached value).
153 *
154 * As completion events come in out of order this counter is not
155 * ordered with any other data.
156 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000157 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 /*
159 * Ring buffer of completion events.
160 *
161 * The kernel writes completion events fresh every time they are
162 * produced, so the application is allowed to modify pending
163 * entries.
164 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000165 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700166};
167
Jens Axboeedafcce2019-01-09 09:16:05 -0700168struct io_mapped_ubuf {
169 u64 ubuf;
170 size_t len;
171 struct bio_vec *bvec;
172 unsigned int nr_bvecs;
173};
174
Jens Axboe65e19f52019-10-26 07:20:21 -0600175struct fixed_file_table {
176 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700177};
178
Jens Axboe2b188cc2019-01-07 10:46:33 -0700179struct io_ring_ctx {
180 struct {
181 struct percpu_ref refs;
182 } ____cacheline_aligned_in_smp;
183
184 struct {
185 unsigned int flags;
186 bool compat;
187 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700188 bool cq_overflow_flushed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700189
Hristo Venev75b28af2019-08-26 17:23:46 +0000190 /*
191 * Ring buffer of indices into array of io_uring_sqe, which is
192 * mmapped by the application using the IORING_OFF_SQES offset.
193 *
194 * This indirection could e.g. be used to assign fixed
195 * io_uring_sqe entries to operations and only submit them to
196 * the queue when needed.
197 *
198 * The kernel modifies neither the indices array nor the entries
199 * array.
200 */
201 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700202 unsigned cached_sq_head;
203 unsigned sq_entries;
204 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700205 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600206 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700207 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600209
210 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600211 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700212 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700213
Jens Axboefcb323c2019-10-24 12:39:47 -0600214 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700215 } ____cacheline_aligned_in_smp;
216
Hristo Venev75b28af2019-08-26 17:23:46 +0000217 struct io_rings *rings;
218
Jens Axboe2b188cc2019-01-07 10:46:33 -0700219 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600220 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 struct task_struct *sqo_thread; /* if using sq thread polling */
222 struct mm_struct *sqo_mm;
223 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700224
Jens Axboe6b063142019-01-10 22:13:58 -0700225 /*
226 * If used, fixed file set. Writers must ensure that ->refs is dead,
227 * readers must ensure that ->refs is alive as long as the file* is
228 * used. Only updated through io_uring_register(2).
229 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600230 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700231 unsigned nr_user_files;
232
Jens Axboeedafcce2019-01-09 09:16:05 -0700233 /* if used, fixed mapped user buffers */
234 unsigned nr_user_bufs;
235 struct io_mapped_ubuf *user_bufs;
236
Jens Axboe2b188cc2019-01-07 10:46:33 -0700237 struct user_struct *user;
238
Jens Axboe206aefd2019-11-07 18:27:42 -0700239 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
240 struct completion *completions;
241
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700242 /* if all else fails... */
243 struct io_kiocb *fallback_req;
244
Jens Axboe206aefd2019-11-07 18:27:42 -0700245#if defined(CONFIG_UNIX)
246 struct socket *ring_sock;
247#endif
248
249 struct {
250 unsigned cached_cq_tail;
251 unsigned cq_entries;
252 unsigned cq_mask;
253 atomic_t cq_timeouts;
254 struct wait_queue_head cq_wait;
255 struct fasync_struct *cq_fasync;
256 struct eventfd_ctx *cq_ev_fd;
257 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258
259 struct {
260 struct mutex uring_lock;
261 wait_queue_head_t wait;
262 } ____cacheline_aligned_in_smp;
263
264 struct {
265 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700266 bool poll_multi_file;
267 /*
268 * ->poll_list is protected by the ctx->uring_lock for
269 * io_uring instances that don't use IORING_SETUP_SQPOLL.
270 * For SQPOLL, only the single threaded io_sq_thread() will
271 * manipulate the list, hence no extra locking is needed there.
272 */
273 struct list_head poll_list;
Jens Axboeeac406c2019-11-14 12:09:58 -0700274 struct rb_root cancel_tree;
Jens Axboefcb323c2019-10-24 12:39:47 -0600275
276 spinlock_t inflight_lock;
277 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700278 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279};
280
281struct sqe_submit {
282 const struct io_uring_sqe *sqe;
Jens Axboefcb323c2019-10-24 12:39:47 -0600283 struct file *ring_file;
284 int ring_fd;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800285 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700286 bool has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +0800287 bool in_async;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700288 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700289};
290
Jens Axboe09bb8392019-03-13 12:39:28 -0600291/*
292 * First field must be the file pointer in all the
293 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
294 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700295struct io_poll_iocb {
296 struct file *file;
297 struct wait_queue_head *head;
298 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600299 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700300 bool canceled;
301 struct wait_queue_entry wait;
302};
303
Jens Axboead8a48a2019-11-15 08:49:11 -0700304struct io_timeout_data {
305 struct io_kiocb *req;
306 struct hrtimer timer;
307 struct timespec64 ts;
308 enum hrtimer_mode mode;
309};
310
Jens Axboe5262f562019-09-17 12:26:57 -0600311struct io_timeout {
312 struct file *file;
Jens Axboead8a48a2019-11-15 08:49:11 -0700313 struct io_timeout_data *data;
Jens Axboe5262f562019-09-17 12:26:57 -0600314};
315
Jens Axboe09bb8392019-03-13 12:39:28 -0600316/*
317 * NOTE! Each of the iocb union members has the file pointer
318 * as the first entry in their struct definition. So you can
319 * access the file pointer through any of the sub-structs,
320 * or directly as just 'ki_filp' in this struct.
321 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700322struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700323 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600324 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700325 struct kiocb rw;
326 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600327 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700328 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700329
330 struct sqe_submit submit;
331
332 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700333 union {
334 struct list_head list;
335 struct rb_node rb_node;
336 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600337 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700338 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700339 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200340#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700341#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700342#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700343#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200344#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
345#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600346#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700347#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800348#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800349#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600350#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600351#define REQ_F_ISREG 2048 /* regular file */
352#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700353#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800354#define REQ_F_INFLIGHT 16384 /* on inflight list */
355#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe94ae5e72019-11-14 19:39:52 -0700356#define REQ_F_FREE_SQE 65536 /* free sqe if not async queued */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700357 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600358 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600359 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700360
Jens Axboefcb323c2019-10-24 12:39:47 -0600361 struct list_head inflight_entry;
362
Jens Axboe561fb042019-10-24 07:25:42 -0600363 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700364};
365
366#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700367#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700368
Jens Axboe9a56a232019-01-09 09:06:50 -0700369struct io_submit_state {
370 struct blk_plug plug;
371
372 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700373 * io_kiocb alloc cache
374 */
375 void *reqs[IO_IOPOLL_BATCH];
376 unsigned int free_reqs;
377 unsigned int cur_req;
378
379 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700380 * File reference cache
381 */
382 struct file *file;
383 unsigned int fd;
384 unsigned int has_refs;
385 unsigned int used_refs;
386 unsigned int ios_left;
387};
388
Jens Axboe561fb042019-10-24 07:25:42 -0600389static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700390static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800391static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800392static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700393static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700394static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700395static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
396static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600397
Jens Axboe2b188cc2019-01-07 10:46:33 -0700398static struct kmem_cache *req_cachep;
399
400static const struct file_operations io_uring_fops;
401
402struct sock *io_uring_get_socket(struct file *file)
403{
404#if defined(CONFIG_UNIX)
405 if (file->f_op == &io_uring_fops) {
406 struct io_ring_ctx *ctx = file->private_data;
407
408 return ctx->ring_sock->sk;
409 }
410#endif
411 return NULL;
412}
413EXPORT_SYMBOL(io_uring_get_socket);
414
415static void io_ring_ctx_ref_free(struct percpu_ref *ref)
416{
417 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
418
Jens Axboe206aefd2019-11-07 18:27:42 -0700419 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700420}
421
422static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
423{
424 struct io_ring_ctx *ctx;
425
426 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
427 if (!ctx)
428 return NULL;
429
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700430 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
431 if (!ctx->fallback_req)
432 goto err;
433
Jens Axboe206aefd2019-11-07 18:27:42 -0700434 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
435 if (!ctx->completions)
436 goto err;
437
Roman Gushchin21482892019-05-07 10:01:48 -0700438 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700439 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
440 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700441
442 ctx->flags = p->flags;
443 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700444 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700445 init_completion(&ctx->completions[0]);
446 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700447 mutex_init(&ctx->uring_lock);
448 init_waitqueue_head(&ctx->wait);
449 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700450 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboeeac406c2019-11-14 12:09:58 -0700451 ctx->cancel_tree = RB_ROOT;
Jens Axboede0617e2019-04-06 21:51:27 -0600452 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600453 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600454 init_waitqueue_head(&ctx->inflight_wait);
455 spin_lock_init(&ctx->inflight_lock);
456 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700457 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700458err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700459 if (ctx->fallback_req)
460 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700461 kfree(ctx->completions);
462 kfree(ctx);
463 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700464}
465
Bob Liu9d858b22019-11-13 18:06:25 +0800466static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600467{
Jackie Liua197f662019-11-08 08:09:12 -0700468 struct io_ring_ctx *ctx = req->ctx;
469
Jens Axboe498ccd92019-10-25 10:04:25 -0600470 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
471 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600472}
473
Bob Liu9d858b22019-11-13 18:06:25 +0800474static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600475{
Bob Liu9d858b22019-11-13 18:06:25 +0800476 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
477 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600478
Bob Liu9d858b22019-11-13 18:06:25 +0800479 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600480}
481
482static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600483{
484 struct io_kiocb *req;
485
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600486 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800487 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600488 list_del_init(&req->list);
489 return req;
490 }
491
492 return NULL;
493}
494
Jens Axboe5262f562019-09-17 12:26:57 -0600495static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
496{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600497 struct io_kiocb *req;
498
499 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700500 if (req) {
501 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
502 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800503 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700504 list_del_init(&req->list);
505 return req;
506 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600507 }
508
509 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600510}
511
Jens Axboede0617e2019-04-06 21:51:27 -0600512static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700513{
Hristo Venev75b28af2019-08-26 17:23:46 +0000514 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700515
Hristo Venev75b28af2019-08-26 17:23:46 +0000516 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700517 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000518 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700519
Jens Axboe2b188cc2019-01-07 10:46:33 -0700520 if (wq_has_sleeper(&ctx->cq_wait)) {
521 wake_up_interruptible(&ctx->cq_wait);
522 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
523 }
524 }
525}
526
Jens Axboe561fb042019-10-24 07:25:42 -0600527static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600528{
Jens Axboe561fb042019-10-24 07:25:42 -0600529 u8 opcode = READ_ONCE(sqe->opcode);
530
531 return !(opcode == IORING_OP_READ_FIXED ||
532 opcode == IORING_OP_WRITE_FIXED);
533}
534
Jens Axboe94ae5e72019-11-14 19:39:52 -0700535static inline bool io_prep_async_work(struct io_kiocb *req,
536 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600537{
538 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600539
Jens Axboe6cc47d12019-09-18 11:18:23 -0600540 if (req->submit.sqe) {
541 switch (req->submit.sqe->opcode) {
542 case IORING_OP_WRITEV:
543 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600544 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700545 /* fall-through */
546 case IORING_OP_READV:
547 case IORING_OP_READ_FIXED:
548 case IORING_OP_SENDMSG:
549 case IORING_OP_RECVMSG:
550 case IORING_OP_ACCEPT:
551 case IORING_OP_POLL_ADD:
552 /*
553 * We know REQ_F_ISREG is not set on some of these
554 * opcodes, but this enables us to keep the check in
555 * just one place.
556 */
557 if (!(req->flags & REQ_F_ISREG))
558 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600559 break;
560 }
Jens Axboe561fb042019-10-24 07:25:42 -0600561 if (io_sqe_needs_user(req->submit.sqe))
562 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600563 }
564
Jens Axboe94ae5e72019-11-14 19:39:52 -0700565 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600566 return do_hashed;
567}
568
Jackie Liua197f662019-11-08 08:09:12 -0700569static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600570{
Jackie Liua197f662019-11-08 08:09:12 -0700571 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700572 struct io_kiocb *link;
573 bool do_hashed;
574
575 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600576
577 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
578 req->flags);
579 if (!do_hashed) {
580 io_wq_enqueue(ctx->io_wq, &req->work);
581 } else {
582 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
583 file_inode(req->file));
584 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700585
586 if (link)
587 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600588}
589
Jens Axboe5262f562019-09-17 12:26:57 -0600590static void io_kill_timeout(struct io_kiocb *req)
591{
592 int ret;
593
Jens Axboead8a48a2019-11-15 08:49:11 -0700594 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600595 if (ret != -1) {
596 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600597 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700598 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800599 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600600 }
601}
602
603static void io_kill_timeouts(struct io_ring_ctx *ctx)
604{
605 struct io_kiocb *req, *tmp;
606
607 spin_lock_irq(&ctx->completion_lock);
608 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
609 io_kill_timeout(req);
610 spin_unlock_irq(&ctx->completion_lock);
611}
612
Jens Axboede0617e2019-04-06 21:51:27 -0600613static void io_commit_cqring(struct io_ring_ctx *ctx)
614{
615 struct io_kiocb *req;
616
Jens Axboe5262f562019-09-17 12:26:57 -0600617 while ((req = io_get_timeout_req(ctx)) != NULL)
618 io_kill_timeout(req);
619
Jens Axboede0617e2019-04-06 21:51:27 -0600620 __io_commit_cqring(ctx);
621
622 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800623 if (req->flags & REQ_F_SHADOW_DRAIN) {
624 /* Just for drain, free it. */
625 __io_free_req(req);
626 continue;
627 }
Jens Axboede0617e2019-04-06 21:51:27 -0600628 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700629 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600630 }
631}
632
Jens Axboe2b188cc2019-01-07 10:46:33 -0700633static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
634{
Hristo Venev75b28af2019-08-26 17:23:46 +0000635 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700636 unsigned tail;
637
638 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200639 /*
640 * writes to the cq entry need to come after reading head; the
641 * control dependency is enough as we're using WRITE_ONCE to
642 * fill the cq entry
643 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000644 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700645 return NULL;
646
647 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000648 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700649}
650
Jens Axboe8c838782019-03-12 15:48:16 -0600651static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
652{
653 if (waitqueue_active(&ctx->wait))
654 wake_up(&ctx->wait);
655 if (waitqueue_active(&ctx->sqo_wait))
656 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600657 if (ctx->cq_ev_fd)
658 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600659}
660
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700661static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700662{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700663 struct io_rings *rings = ctx->rings;
664 struct io_uring_cqe *cqe;
665 struct io_kiocb *req;
666 unsigned long flags;
667 LIST_HEAD(list);
668
669 if (!force) {
670 if (list_empty_careful(&ctx->cq_overflow_list))
671 return;
672 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
673 rings->cq_ring_entries))
674 return;
675 }
676
677 spin_lock_irqsave(&ctx->completion_lock, flags);
678
679 /* if force is set, the ring is going away. always drop after that */
680 if (force)
681 ctx->cq_overflow_flushed = true;
682
683 while (!list_empty(&ctx->cq_overflow_list)) {
684 cqe = io_get_cqring(ctx);
685 if (!cqe && !force)
686 break;
687
688 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
689 list);
690 list_move(&req->list, &list);
691 if (cqe) {
692 WRITE_ONCE(cqe->user_data, req->user_data);
693 WRITE_ONCE(cqe->res, req->result);
694 WRITE_ONCE(cqe->flags, 0);
695 } else {
696 WRITE_ONCE(ctx->rings->cq_overflow,
697 atomic_inc_return(&ctx->cached_cq_overflow));
698 }
699 }
700
701 io_commit_cqring(ctx);
702 spin_unlock_irqrestore(&ctx->completion_lock, flags);
703 io_cqring_ev_posted(ctx);
704
705 while (!list_empty(&list)) {
706 req = list_first_entry(&list, struct io_kiocb, list);
707 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800708 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700709 }
710}
711
Jens Axboe78e19bb2019-11-06 15:21:34 -0700712static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700713{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700714 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700715 struct io_uring_cqe *cqe;
716
Jens Axboe78e19bb2019-11-06 15:21:34 -0700717 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700718
Jens Axboe2b188cc2019-01-07 10:46:33 -0700719 /*
720 * If we can't get a cq entry, userspace overflowed the
721 * submission (by quite a lot). Increment the overflow count in
722 * the ring.
723 */
724 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700725 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700726 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700727 WRITE_ONCE(cqe->res, res);
728 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700729 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700730 WRITE_ONCE(ctx->rings->cq_overflow,
731 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700732 } else {
733 refcount_inc(&req->refs);
734 req->result = res;
735 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700736 }
737}
738
Jens Axboe78e19bb2019-11-06 15:21:34 -0700739static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700740{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700741 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700742 unsigned long flags;
743
744 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700745 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700746 io_commit_cqring(ctx);
747 spin_unlock_irqrestore(&ctx->completion_lock, flags);
748
Jens Axboe8c838782019-03-12 15:48:16 -0600749 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700750}
751
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700752static inline bool io_is_fallback_req(struct io_kiocb *req)
753{
754 return req == (struct io_kiocb *)
755 ((unsigned long) req->ctx->fallback_req & ~1UL);
756}
757
758static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
759{
760 struct io_kiocb *req;
761
762 req = ctx->fallback_req;
763 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
764 return req;
765
766 return NULL;
767}
768
Jens Axboe2579f912019-01-09 09:10:43 -0700769static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
770 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700771{
Jens Axboefd6fab22019-03-14 16:30:06 -0600772 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700773 struct io_kiocb *req;
774
775 if (!percpu_ref_tryget(&ctx->refs))
776 return NULL;
777
Jens Axboe2579f912019-01-09 09:10:43 -0700778 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600779 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700780 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700781 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700782 } else if (!state->free_reqs) {
783 size_t sz;
784 int ret;
785
786 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600787 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
788
789 /*
790 * Bulk alloc is all-or-nothing. If we fail to get a batch,
791 * retry single alloc to be on the safe side.
792 */
793 if (unlikely(ret <= 0)) {
794 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
795 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700796 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600797 ret = 1;
798 }
Jens Axboe2579f912019-01-09 09:10:43 -0700799 state->free_reqs = ret - 1;
800 state->cur_req = 1;
801 req = state->reqs[0];
802 } else {
803 req = state->reqs[state->cur_req];
804 state->free_reqs--;
805 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700806 }
807
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700808got_it:
Jens Axboe60c112b2019-06-21 10:20:18 -0600809 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700810 req->ctx = ctx;
811 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600812 /* one is dropped after submission, the other at completion */
813 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600814 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600815 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700816 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700817fallback:
818 req = io_get_fallback_req(ctx);
819 if (req)
820 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300821 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700822 return NULL;
823}
824
Jens Axboedef596e2019-01-09 08:59:42 -0700825static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
826{
827 if (*nr) {
828 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300829 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700830 *nr = 0;
831 }
832}
833
Jens Axboe9e645e112019-05-10 16:07:28 -0600834static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700835{
Jens Axboefcb323c2019-10-24 12:39:47 -0600836 struct io_ring_ctx *ctx = req->ctx;
837
Pavel Begunkovbbad27b2019-11-19 23:32:47 +0300838 if (req->flags & REQ_F_FREE_SQE)
839 kfree(req->submit.sqe);
Jens Axboe09bb8392019-03-13 12:39:28 -0600840 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
841 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600842 if (req->flags & REQ_F_INFLIGHT) {
843 unsigned long flags;
844
845 spin_lock_irqsave(&ctx->inflight_lock, flags);
846 list_del(&req->inflight_entry);
847 if (waitqueue_active(&ctx->inflight_wait))
848 wake_up(&ctx->inflight_wait);
849 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
850 }
Jens Axboead8a48a2019-11-15 08:49:11 -0700851 if (req->flags & REQ_F_TIMEOUT)
852 kfree(req->timeout.data);
Jens Axboefcb323c2019-10-24 12:39:47 -0600853 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700854 if (likely(!io_is_fallback_req(req)))
855 kmem_cache_free(req_cachep, req);
856 else
857 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600858}
859
Jackie Liua197f662019-11-08 08:09:12 -0700860static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600861{
Jackie Liua197f662019-11-08 08:09:12 -0700862 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700863 int ret;
864
Jens Axboead8a48a2019-11-15 08:49:11 -0700865 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700866 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700867 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700868 io_commit_cqring(ctx);
869 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800870 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700871 return true;
872 }
873
874 return false;
875}
876
Jens Axboeba816ad2019-09-28 11:36:45 -0600877static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600878{
Jens Axboe2665abf2019-11-05 12:40:47 -0700879 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600880 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700881 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600882
Jens Axboe4d7dd462019-11-20 13:03:52 -0700883 /* Already got next link */
884 if (req->flags & REQ_F_LINK_NEXT)
885 return;
886
Jens Axboe9e645e112019-05-10 16:07:28 -0600887 /*
888 * The list should never be empty when we are called here. But could
889 * potentially happen if the chain is messed up, check to be on the
890 * safe side.
891 */
892 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700893 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700894 list_del_init(&nxt->list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700895
896 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
897 (nxt->flags & REQ_F_TIMEOUT)) {
898 wake_ev |= io_link_cancel_timeout(nxt);
899 nxt = list_first_entry_or_null(&req->link_list,
900 struct io_kiocb, list);
901 req->flags &= ~REQ_F_LINK_TIMEOUT;
902 continue;
903 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600904 if (!list_empty(&req->link_list)) {
905 INIT_LIST_HEAD(&nxt->link_list);
906 list_splice(&req->link_list, &nxt->link_list);
907 nxt->flags |= REQ_F_LINK;
908 }
909
Jens Axboeba816ad2019-09-28 11:36:45 -0600910 /*
911 * If we're in async work, we can continue processing the chain
912 * in this context instead of having to queue up new async work.
913 */
Jens Axboe94ae5e72019-11-14 19:39:52 -0700914 if (nxt) {
915 if (nxtptr && io_wq_current_is_worker())
916 *nxtptr = nxt;
917 else
918 io_queue_async_work(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700919 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700920 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600921 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700922
Jens Axboe4d7dd462019-11-20 13:03:52 -0700923 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700924 if (wake_ev)
925 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600926}
927
928/*
929 * Called if REQ_F_LINK is set, and we fail the head request
930 */
931static void io_fail_links(struct io_kiocb *req)
932{
Jens Axboe2665abf2019-11-05 12:40:47 -0700933 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600934 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700935 unsigned long flags;
936
937 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600938
939 while (!list_empty(&req->link_list)) {
940 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700941 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600942
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200943 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700944
945 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
946 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700947 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700948 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700949 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700950 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700951 }
Jens Axboe5d960722019-11-19 15:31:28 -0700952 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600953 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700954
955 io_commit_cqring(ctx);
956 spin_unlock_irqrestore(&ctx->completion_lock, flags);
957 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600958}
959
Jens Axboe4d7dd462019-11-20 13:03:52 -0700960static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600961{
Jens Axboe4d7dd462019-11-20 13:03:52 -0700962 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -0700963 return;
Jens Axboe2665abf2019-11-05 12:40:47 -0700964
Jens Axboe9e645e112019-05-10 16:07:28 -0600965 /*
966 * If LINK is set, we have dependent requests in this chain. If we
967 * didn't fail this request, queue the first one up, moving any other
968 * dependencies to the next request. In case of failure, fail the rest
969 * of the chain.
970 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700971 if (req->flags & REQ_F_FAIL_LINK) {
972 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700973 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
974 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700975 struct io_ring_ctx *ctx = req->ctx;
976 unsigned long flags;
977
978 /*
979 * If this is a timeout link, we could be racing with the
980 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700981 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700982 */
983 spin_lock_irqsave(&ctx->completion_lock, flags);
984 io_req_link_next(req, nxt);
985 spin_unlock_irqrestore(&ctx->completion_lock, flags);
986 } else {
987 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600988 }
Jens Axboe4d7dd462019-11-20 13:03:52 -0700989}
Jens Axboe9e645e112019-05-10 16:07:28 -0600990
Jens Axboe4d7dd462019-11-20 13:03:52 -0700991static void io_free_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
992{
993 io_req_find_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600994 __io_free_req(req);
995}
996
Jackie Liuc69f8db2019-11-09 11:00:08 +0800997static void io_free_req(struct io_kiocb *req)
998{
999 io_free_req_find_next(req, NULL);
1000}
1001
Jens Axboeba816ad2019-09-28 11:36:45 -06001002/*
1003 * Drop reference to request, return next in chain (if there is one) if this
1004 * was the last reference to this request.
1005 */
Jackie Liuec9c02a2019-11-08 23:50:36 +08001006static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001007{
Jens Axboeba816ad2019-09-28 11:36:45 -06001008 struct io_kiocb *nxt = NULL;
1009
Jens Axboe4d7dd462019-11-20 13:03:52 -07001010 io_req_find_next(req, &nxt);
1011
Jens Axboee65ef562019-03-12 10:16:44 -06001012 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001013 __io_free_req(req);
Jens Axboeba816ad2019-09-28 11:36:45 -06001014
Jens Axboeba816ad2019-09-28 11:36:45 -06001015 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -06001016 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -06001017 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -06001018 else
Jackie Liua197f662019-11-08 08:09:12 -07001019 io_queue_async_work(nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -06001020 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001021}
1022
Jens Axboe2b188cc2019-01-07 10:46:33 -07001023static void io_put_req(struct io_kiocb *req)
1024{
Jens Axboedef596e2019-01-09 08:59:42 -07001025 if (refcount_dec_and_test(&req->refs))
1026 io_free_req(req);
1027}
1028
Jens Axboe978db572019-11-14 22:39:04 -07001029/*
1030 * Must only be used if we don't need to care about links, usually from
1031 * within the completion handling itself.
1032 */
1033static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001034{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001035 /* drop both submit and complete references */
1036 if (refcount_sub_and_test(2, &req->refs))
1037 __io_free_req(req);
1038}
1039
Jens Axboe978db572019-11-14 22:39:04 -07001040static void io_double_put_req(struct io_kiocb *req)
1041{
1042 /* drop both submit and complete references */
1043 if (refcount_sub_and_test(2, &req->refs))
1044 io_free_req(req);
1045}
1046
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001047static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001048{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001049 struct io_rings *rings = ctx->rings;
1050
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001051 /*
1052 * noflush == true is from the waitqueue handler, just ensure we wake
1053 * up the task, and the next invocation will flush the entries. We
1054 * cannot safely to it from here.
1055 */
1056 if (noflush && !list_empty(&ctx->cq_overflow_list))
1057 return -1U;
1058
1059 io_cqring_overflow_flush(ctx, false);
1060
Jens Axboea3a0e432019-08-20 11:03:11 -06001061 /* See comment at the top of this file */
1062 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001063 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001064}
1065
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001066static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1067{
1068 struct io_rings *rings = ctx->rings;
1069
1070 /* make sure SQ entry isn't read before tail */
1071 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1072}
1073
Jens Axboedef596e2019-01-09 08:59:42 -07001074/*
1075 * Find and free completed poll iocbs
1076 */
1077static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1078 struct list_head *done)
1079{
1080 void *reqs[IO_IOPOLL_BATCH];
1081 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001082 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001083
Jens Axboe09bb8392019-03-13 12:39:28 -06001084 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001085 while (!list_empty(done)) {
1086 req = list_first_entry(done, struct io_kiocb, list);
1087 list_del(&req->list);
1088
Jens Axboe78e19bb2019-11-06 15:21:34 -07001089 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001090 (*nr_events)++;
1091
Jens Axboe09bb8392019-03-13 12:39:28 -06001092 if (refcount_dec_and_test(&req->refs)) {
1093 /* If we're not using fixed files, we have to pair the
1094 * completion part with the file put. Use regular
1095 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001096 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001097 */
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03001098 if (((req->flags &
1099 (REQ_F_FIXED_FILE|REQ_F_LINK|REQ_F_FREE_SQE)) ==
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001100 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001101 reqs[to_free++] = req;
1102 if (to_free == ARRAY_SIZE(reqs))
1103 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001104 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001105 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001106 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001107 }
Jens Axboedef596e2019-01-09 08:59:42 -07001108 }
Jens Axboedef596e2019-01-09 08:59:42 -07001109
Jens Axboe09bb8392019-03-13 12:39:28 -06001110 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001111 io_free_req_many(ctx, reqs, &to_free);
1112}
1113
1114static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1115 long min)
1116{
1117 struct io_kiocb *req, *tmp;
1118 LIST_HEAD(done);
1119 bool spin;
1120 int ret;
1121
1122 /*
1123 * Only spin for completions if we don't have multiple devices hanging
1124 * off our complete list, and we're under the requested amount.
1125 */
1126 spin = !ctx->poll_multi_file && *nr_events < min;
1127
1128 ret = 0;
1129 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1130 struct kiocb *kiocb = &req->rw;
1131
1132 /*
1133 * Move completed entries to our local list. If we find a
1134 * request that requires polling, break out and complete
1135 * the done list first, if we have entries there.
1136 */
1137 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1138 list_move_tail(&req->list, &done);
1139 continue;
1140 }
1141 if (!list_empty(&done))
1142 break;
1143
1144 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1145 if (ret < 0)
1146 break;
1147
1148 if (ret && spin)
1149 spin = false;
1150 ret = 0;
1151 }
1152
1153 if (!list_empty(&done))
1154 io_iopoll_complete(ctx, nr_events, &done);
1155
1156 return ret;
1157}
1158
1159/*
1160 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1161 * non-spinning poll check - we'll still enter the driver poll loop, but only
1162 * as a non-spinning completion check.
1163 */
1164static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1165 long min)
1166{
Jens Axboe08f54392019-08-21 22:19:11 -06001167 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001168 int ret;
1169
1170 ret = io_do_iopoll(ctx, nr_events, min);
1171 if (ret < 0)
1172 return ret;
1173 if (!min || *nr_events >= min)
1174 return 0;
1175 }
1176
1177 return 1;
1178}
1179
1180/*
1181 * We can't just wait for polled events to come to us, we have to actively
1182 * find and complete them.
1183 */
1184static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1185{
1186 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1187 return;
1188
1189 mutex_lock(&ctx->uring_lock);
1190 while (!list_empty(&ctx->poll_list)) {
1191 unsigned int nr_events = 0;
1192
1193 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001194
1195 /*
1196 * Ensure we allow local-to-the-cpu processing to take place,
1197 * in this case we need to ensure that we reap all events.
1198 */
1199 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001200 }
1201 mutex_unlock(&ctx->uring_lock);
1202}
1203
Jens Axboe2b2ed972019-10-25 10:06:15 -06001204static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1205 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001206{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001207 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001208
1209 do {
1210 int tmin = 0;
1211
Jens Axboe500f9fb2019-08-19 12:15:59 -06001212 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001213 * Don't enter poll loop if we already have events pending.
1214 * If we do, we can potentially be spinning for commands that
1215 * already triggered a CQE (eg in error).
1216 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001217 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001218 break;
1219
1220 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001221 * If a submit got punted to a workqueue, we can have the
1222 * application entering polling for a command before it gets
1223 * issued. That app will hold the uring_lock for the duration
1224 * of the poll right here, so we need to take a breather every
1225 * now and then to ensure that the issue has a chance to add
1226 * the poll to the issued list. Otherwise we can spin here
1227 * forever, while the workqueue is stuck trying to acquire the
1228 * very same mutex.
1229 */
1230 if (!(++iters & 7)) {
1231 mutex_unlock(&ctx->uring_lock);
1232 mutex_lock(&ctx->uring_lock);
1233 }
1234
Jens Axboedef596e2019-01-09 08:59:42 -07001235 if (*nr_events < min)
1236 tmin = min - *nr_events;
1237
1238 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1239 if (ret <= 0)
1240 break;
1241 ret = 0;
1242 } while (min && !*nr_events && !need_resched());
1243
Jens Axboe2b2ed972019-10-25 10:06:15 -06001244 return ret;
1245}
1246
1247static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1248 long min)
1249{
1250 int ret;
1251
1252 /*
1253 * We disallow the app entering submit/complete with polling, but we
1254 * still need to lock the ring to prevent racing with polled issue
1255 * that got punted to a workqueue.
1256 */
1257 mutex_lock(&ctx->uring_lock);
1258 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001259 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001260 return ret;
1261}
1262
Jens Axboe491381ce2019-10-17 09:20:46 -06001263static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001264{
Jens Axboe491381ce2019-10-17 09:20:46 -06001265 /*
1266 * Tell lockdep we inherited freeze protection from submission
1267 * thread.
1268 */
1269 if (req->flags & REQ_F_ISREG) {
1270 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001271
Jens Axboe491381ce2019-10-17 09:20:46 -06001272 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001273 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001274 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001275}
1276
Jens Axboeba816ad2019-09-28 11:36:45 -06001277static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001278{
1279 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1280
Jens Axboe491381ce2019-10-17 09:20:46 -06001281 if (kiocb->ki_flags & IOCB_WRITE)
1282 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001283
Jens Axboe9e645e112019-05-10 16:07:28 -06001284 if ((req->flags & REQ_F_LINK) && res != req->result)
1285 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001286 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001287}
1288
1289static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1290{
1291 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1292
1293 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001294 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001295}
1296
Jens Axboeba816ad2019-09-28 11:36:45 -06001297static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1298{
1299 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001300 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001301
1302 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001303 io_put_req_find_next(req, &nxt);
1304
1305 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001306}
1307
Jens Axboedef596e2019-01-09 08:59:42 -07001308static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1309{
1310 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1311
Jens Axboe491381ce2019-10-17 09:20:46 -06001312 if (kiocb->ki_flags & IOCB_WRITE)
1313 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001314
Jens Axboe9e645e112019-05-10 16:07:28 -06001315 if ((req->flags & REQ_F_LINK) && res != req->result)
1316 req->flags |= REQ_F_FAIL_LINK;
1317 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001318 if (res != -EAGAIN)
1319 req->flags |= REQ_F_IOPOLL_COMPLETED;
1320}
1321
1322/*
1323 * After the iocb has been issued, it's safe to be found on the poll list.
1324 * Adding the kiocb to the list AFTER submission ensures that we don't
1325 * find it from a io_iopoll_getevents() thread before the issuer is done
1326 * accessing the kiocb cookie.
1327 */
1328static void io_iopoll_req_issued(struct io_kiocb *req)
1329{
1330 struct io_ring_ctx *ctx = req->ctx;
1331
1332 /*
1333 * Track whether we have multiple files in our lists. This will impact
1334 * how we do polling eventually, not spinning if we're on potentially
1335 * different devices.
1336 */
1337 if (list_empty(&ctx->poll_list)) {
1338 ctx->poll_multi_file = false;
1339 } else if (!ctx->poll_multi_file) {
1340 struct io_kiocb *list_req;
1341
1342 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1343 list);
1344 if (list_req->rw.ki_filp != req->rw.ki_filp)
1345 ctx->poll_multi_file = true;
1346 }
1347
1348 /*
1349 * For fast devices, IO may have already completed. If it has, add
1350 * it to the front so we find it first.
1351 */
1352 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1353 list_add(&req->list, &ctx->poll_list);
1354 else
1355 list_add_tail(&req->list, &ctx->poll_list);
1356}
1357
Jens Axboe3d6770f2019-04-13 11:50:54 -06001358static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001359{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001360 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001361 int diff = state->has_refs - state->used_refs;
1362
1363 if (diff)
1364 fput_many(state->file, diff);
1365 state->file = NULL;
1366 }
1367}
1368
1369/*
1370 * Get as many references to a file as we have IOs left in this submission,
1371 * assuming most submissions are for one file, or at least that each file
1372 * has more than one submission.
1373 */
1374static struct file *io_file_get(struct io_submit_state *state, int fd)
1375{
1376 if (!state)
1377 return fget(fd);
1378
1379 if (state->file) {
1380 if (state->fd == fd) {
1381 state->used_refs++;
1382 state->ios_left--;
1383 return state->file;
1384 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001385 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001386 }
1387 state->file = fget_many(fd, state->ios_left);
1388 if (!state->file)
1389 return NULL;
1390
1391 state->fd = fd;
1392 state->has_refs = state->ios_left;
1393 state->used_refs = 1;
1394 state->ios_left--;
1395 return state->file;
1396}
1397
Jens Axboe2b188cc2019-01-07 10:46:33 -07001398/*
1399 * If we tracked the file through the SCM inflight mechanism, we could support
1400 * any file. For now, just ensure that anything potentially problematic is done
1401 * inline.
1402 */
1403static bool io_file_supports_async(struct file *file)
1404{
1405 umode_t mode = file_inode(file)->i_mode;
1406
1407 if (S_ISBLK(mode) || S_ISCHR(mode))
1408 return true;
1409 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1410 return true;
1411
1412 return false;
1413}
1414
Pavel Begunkov267bc902019-11-07 01:41:08 +03001415static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001416{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001417 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001418 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001419 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001420 unsigned ioprio;
1421 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001422
Jens Axboe09bb8392019-03-13 12:39:28 -06001423 if (!req->file)
1424 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001425
Jens Axboe491381ce2019-10-17 09:20:46 -06001426 if (S_ISREG(file_inode(req->file)->i_mode))
1427 req->flags |= REQ_F_ISREG;
1428
1429 /*
1430 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1431 * we know to async punt it even if it was opened O_NONBLOCK
1432 */
1433 if (force_nonblock && !io_file_supports_async(req->file)) {
1434 req->flags |= REQ_F_MUST_PUNT;
1435 return -EAGAIN;
1436 }
Jens Axboe6b063142019-01-10 22:13:58 -07001437
Jens Axboe2b188cc2019-01-07 10:46:33 -07001438 kiocb->ki_pos = READ_ONCE(sqe->off);
1439 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1440 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1441
1442 ioprio = READ_ONCE(sqe->ioprio);
1443 if (ioprio) {
1444 ret = ioprio_check_cap(ioprio);
1445 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001446 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001447
1448 kiocb->ki_ioprio = ioprio;
1449 } else
1450 kiocb->ki_ioprio = get_current_ioprio();
1451
1452 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1453 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001454 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001455
1456 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001457 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1458 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001459 req->flags |= REQ_F_NOWAIT;
1460
1461 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001462 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001463
Jens Axboedef596e2019-01-09 08:59:42 -07001464 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001465 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1466 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001467 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001468
Jens Axboedef596e2019-01-09 08:59:42 -07001469 kiocb->ki_flags |= IOCB_HIPRI;
1470 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001471 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001472 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001473 if (kiocb->ki_flags & IOCB_HIPRI)
1474 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001475 kiocb->ki_complete = io_complete_rw;
1476 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001477 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001478}
1479
1480static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1481{
1482 switch (ret) {
1483 case -EIOCBQUEUED:
1484 break;
1485 case -ERESTARTSYS:
1486 case -ERESTARTNOINTR:
1487 case -ERESTARTNOHAND:
1488 case -ERESTART_RESTARTBLOCK:
1489 /*
1490 * We can't just restart the syscall, since previously
1491 * submitted sqes may already be in progress. Just fail this
1492 * IO with EINTR.
1493 */
1494 ret = -EINTR;
1495 /* fall through */
1496 default:
1497 kiocb->ki_complete(kiocb, ret, 0);
1498 }
1499}
1500
Jens Axboeba816ad2019-09-28 11:36:45 -06001501static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1502 bool in_async)
1503{
1504 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1505 *nxt = __io_complete_rw(kiocb, ret);
1506 else
1507 io_rw_done(kiocb, ret);
1508}
1509
Jens Axboeedafcce2019-01-09 09:16:05 -07001510static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1511 const struct io_uring_sqe *sqe,
1512 struct iov_iter *iter)
1513{
1514 size_t len = READ_ONCE(sqe->len);
1515 struct io_mapped_ubuf *imu;
1516 unsigned index, buf_index;
1517 size_t offset;
1518 u64 buf_addr;
1519
1520 /* attempt to use fixed buffers without having provided iovecs */
1521 if (unlikely(!ctx->user_bufs))
1522 return -EFAULT;
1523
1524 buf_index = READ_ONCE(sqe->buf_index);
1525 if (unlikely(buf_index >= ctx->nr_user_bufs))
1526 return -EFAULT;
1527
1528 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1529 imu = &ctx->user_bufs[index];
1530 buf_addr = READ_ONCE(sqe->addr);
1531
1532 /* overflow */
1533 if (buf_addr + len < buf_addr)
1534 return -EFAULT;
1535 /* not inside the mapped region */
1536 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1537 return -EFAULT;
1538
1539 /*
1540 * May not be a start of buffer, set size appropriately
1541 * and advance us to the beginning.
1542 */
1543 offset = buf_addr - imu->ubuf;
1544 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001545
1546 if (offset) {
1547 /*
1548 * Don't use iov_iter_advance() here, as it's really slow for
1549 * using the latter parts of a big fixed buffer - it iterates
1550 * over each segment manually. We can cheat a bit here, because
1551 * we know that:
1552 *
1553 * 1) it's a BVEC iter, we set it up
1554 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1555 * first and last bvec
1556 *
1557 * So just find our index, and adjust the iterator afterwards.
1558 * If the offset is within the first bvec (or the whole first
1559 * bvec, just use iov_iter_advance(). This makes it easier
1560 * since we can just skip the first segment, which may not
1561 * be PAGE_SIZE aligned.
1562 */
1563 const struct bio_vec *bvec = imu->bvec;
1564
1565 if (offset <= bvec->bv_len) {
1566 iov_iter_advance(iter, offset);
1567 } else {
1568 unsigned long seg_skip;
1569
1570 /* skip first vec */
1571 offset -= bvec->bv_len;
1572 seg_skip = 1 + (offset >> PAGE_SHIFT);
1573
1574 iter->bvec = bvec + seg_skip;
1575 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001576 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001577 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001578 }
1579 }
1580
Jens Axboe5e559562019-11-13 16:12:46 -07001581 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001582}
1583
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001584static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1585 const struct sqe_submit *s, struct iovec **iovec,
1586 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001587{
1588 const struct io_uring_sqe *sqe = s->sqe;
1589 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1590 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001591 u8 opcode;
1592
1593 /*
1594 * We're reading ->opcode for the second time, but the first read
1595 * doesn't care whether it's _FIXED or not, so it doesn't matter
1596 * whether ->opcode changes concurrently. The first read does care
1597 * about whether it is a READ or a WRITE, so we don't trust this read
1598 * for that purpose and instead let the caller pass in the read/write
1599 * flag.
1600 */
1601 opcode = READ_ONCE(sqe->opcode);
1602 if (opcode == IORING_OP_READ_FIXED ||
1603 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001604 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001605 *iovec = NULL;
1606 return ret;
1607 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001608
1609 if (!s->has_user)
1610 return -EFAULT;
1611
1612#ifdef CONFIG_COMPAT
1613 if (ctx->compat)
1614 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1615 iovec, iter);
1616#endif
1617
1618 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1619}
1620
Jens Axboe32960612019-09-23 11:05:34 -06001621/*
1622 * For files that don't have ->read_iter() and ->write_iter(), handle them
1623 * by looping over ->read() or ->write() manually.
1624 */
1625static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1626 struct iov_iter *iter)
1627{
1628 ssize_t ret = 0;
1629
1630 /*
1631 * Don't support polled IO through this interface, and we can't
1632 * support non-blocking either. For the latter, this just causes
1633 * the kiocb to be handled from an async context.
1634 */
1635 if (kiocb->ki_flags & IOCB_HIPRI)
1636 return -EOPNOTSUPP;
1637 if (kiocb->ki_flags & IOCB_NOWAIT)
1638 return -EAGAIN;
1639
1640 while (iov_iter_count(iter)) {
1641 struct iovec iovec = iov_iter_iovec(iter);
1642 ssize_t nr;
1643
1644 if (rw == READ) {
1645 nr = file->f_op->read(file, iovec.iov_base,
1646 iovec.iov_len, &kiocb->ki_pos);
1647 } else {
1648 nr = file->f_op->write(file, iovec.iov_base,
1649 iovec.iov_len, &kiocb->ki_pos);
1650 }
1651
1652 if (nr < 0) {
1653 if (!ret)
1654 ret = nr;
1655 break;
1656 }
1657 ret += nr;
1658 if (nr != iovec.iov_len)
1659 break;
1660 iov_iter_advance(iter, nr);
1661 }
1662
1663 return ret;
1664}
1665
Pavel Begunkov267bc902019-11-07 01:41:08 +03001666static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001667 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001668{
1669 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1670 struct kiocb *kiocb = &req->rw;
1671 struct iov_iter iter;
1672 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001673 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001674 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001675
Pavel Begunkov267bc902019-11-07 01:41:08 +03001676 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001677 if (ret)
1678 return ret;
1679 file = kiocb->ki_filp;
1680
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001682 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001683
Pavel Begunkov267bc902019-11-07 01:41:08 +03001684 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001685 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001686 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001687
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001688 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001689 if (req->flags & REQ_F_LINK)
1690 req->result = read_size;
1691
Jens Axboe31b51512019-01-18 22:56:34 -07001692 iov_count = iov_iter_count(&iter);
1693 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001694 if (!ret) {
1695 ssize_t ret2;
1696
Jens Axboe32960612019-09-23 11:05:34 -06001697 if (file->f_op->read_iter)
1698 ret2 = call_read_iter(file, kiocb, &iter);
1699 else
1700 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1701
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001702 /*
1703 * In case of a short read, punt to async. This can happen
1704 * if we have data partially cached. Alternatively we can
1705 * return the short read, in which case the application will
1706 * need to issue another SQE and wait for it. That SQE will
1707 * need async punt anyway, so it's more efficient to do it
1708 * here.
1709 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001710 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1711 (req->flags & REQ_F_ISREG) &&
1712 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001713 ret2 = -EAGAIN;
1714 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001715 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001716 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001717 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001718 ret = -EAGAIN;
1719 }
1720 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001721 return ret;
1722}
1723
Pavel Begunkov267bc902019-11-07 01:41:08 +03001724static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001725 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001726{
1727 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1728 struct kiocb *kiocb = &req->rw;
1729 struct iov_iter iter;
1730 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001731 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001732 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001733
Pavel Begunkov267bc902019-11-07 01:41:08 +03001734 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735 if (ret)
1736 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737
Jens Axboe2b188cc2019-01-07 10:46:33 -07001738 file = kiocb->ki_filp;
1739 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001740 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001741
Pavel Begunkov267bc902019-11-07 01:41:08 +03001742 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001743 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001744 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001745
Jens Axboe9e645e112019-05-10 16:07:28 -06001746 if (req->flags & REQ_F_LINK)
1747 req->result = ret;
1748
Jens Axboe31b51512019-01-18 22:56:34 -07001749 iov_count = iov_iter_count(&iter);
1750
1751 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001752 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001753 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001754
1755 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001756 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001757 ssize_t ret2;
1758
Jens Axboe2b188cc2019-01-07 10:46:33 -07001759 /*
1760 * Open-code file_start_write here to grab freeze protection,
1761 * which will be released by another thread in
1762 * io_complete_rw(). Fool lockdep by telling it the lock got
1763 * released so that it doesn't complain about the held lock when
1764 * we return to userspace.
1765 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001766 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001767 __sb_start_write(file_inode(file)->i_sb,
1768 SB_FREEZE_WRITE, true);
1769 __sb_writers_release(file_inode(file)->i_sb,
1770 SB_FREEZE_WRITE);
1771 }
1772 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001773
Jens Axboe32960612019-09-23 11:05:34 -06001774 if (file->f_op->write_iter)
1775 ret2 = call_write_iter(file, kiocb, &iter);
1776 else
1777 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001778 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001779 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001780 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001781 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001782 }
Jens Axboe31b51512019-01-18 22:56:34 -07001783out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001784 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001785 return ret;
1786}
1787
1788/*
1789 * IORING_OP_NOP just posts a completion event, nothing else.
1790 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001791static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001792{
1793 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001794
Jens Axboedef596e2019-01-09 08:59:42 -07001795 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1796 return -EINVAL;
1797
Jens Axboe78e19bb2019-11-06 15:21:34 -07001798 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001799 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001800 return 0;
1801}
1802
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001803static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1804{
Jens Axboe6b063142019-01-10 22:13:58 -07001805 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001806
Jens Axboe09bb8392019-03-13 12:39:28 -06001807 if (!req->file)
1808 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001809
Jens Axboe6b063142019-01-10 22:13:58 -07001810 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001811 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001812 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001813 return -EINVAL;
1814
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001815 return 0;
1816}
1817
1818static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001819 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001820{
1821 loff_t sqe_off = READ_ONCE(sqe->off);
1822 loff_t sqe_len = READ_ONCE(sqe->len);
1823 loff_t end = sqe_off + sqe_len;
1824 unsigned fsync_flags;
1825 int ret;
1826
1827 fsync_flags = READ_ONCE(sqe->fsync_flags);
1828 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1829 return -EINVAL;
1830
1831 ret = io_prep_fsync(req, sqe);
1832 if (ret)
1833 return ret;
1834
1835 /* fsync always requires a blocking context */
1836 if (force_nonblock)
1837 return -EAGAIN;
1838
1839 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1840 end > 0 ? end : LLONG_MAX,
1841 fsync_flags & IORING_FSYNC_DATASYNC);
1842
Jens Axboe9e645e112019-05-10 16:07:28 -06001843 if (ret < 0 && (req->flags & REQ_F_LINK))
1844 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001845 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001846 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001847 return 0;
1848}
1849
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001850static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1851{
1852 struct io_ring_ctx *ctx = req->ctx;
1853 int ret = 0;
1854
1855 if (!req->file)
1856 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001857
1858 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1859 return -EINVAL;
1860 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1861 return -EINVAL;
1862
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001863 return ret;
1864}
1865
1866static int io_sync_file_range(struct io_kiocb *req,
1867 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001868 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001869 bool force_nonblock)
1870{
1871 loff_t sqe_off;
1872 loff_t sqe_len;
1873 unsigned flags;
1874 int ret;
1875
1876 ret = io_prep_sfr(req, sqe);
1877 if (ret)
1878 return ret;
1879
1880 /* sync_file_range always requires a blocking context */
1881 if (force_nonblock)
1882 return -EAGAIN;
1883
1884 sqe_off = READ_ONCE(sqe->off);
1885 sqe_len = READ_ONCE(sqe->len);
1886 flags = READ_ONCE(sqe->sync_range_flags);
1887
1888 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1889
Jens Axboe9e645e112019-05-10 16:07:28 -06001890 if (ret < 0 && (req->flags & REQ_F_LINK))
1891 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001892 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001893 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001894 return 0;
1895}
1896
Jens Axboe0fa03c62019-04-19 13:34:07 -06001897#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001898static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001899 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001900 long (*fn)(struct socket *, struct user_msghdr __user *,
1901 unsigned int))
1902{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001903 struct socket *sock;
1904 int ret;
1905
1906 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1907 return -EINVAL;
1908
1909 sock = sock_from_file(req->file, &ret);
1910 if (sock) {
1911 struct user_msghdr __user *msg;
1912 unsigned flags;
1913
1914 flags = READ_ONCE(sqe->msg_flags);
1915 if (flags & MSG_DONTWAIT)
1916 req->flags |= REQ_F_NOWAIT;
1917 else if (force_nonblock)
1918 flags |= MSG_DONTWAIT;
1919
1920 msg = (struct user_msghdr __user *) (unsigned long)
1921 READ_ONCE(sqe->addr);
1922
Jens Axboeaa1fa282019-04-19 13:38:09 -06001923 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001924 if (force_nonblock && ret == -EAGAIN)
1925 return ret;
1926 }
1927
Jens Axboe78e19bb2019-11-06 15:21:34 -07001928 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001929 if (ret < 0 && (req->flags & REQ_F_LINK))
1930 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001931 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001932 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001933}
1934#endif
1935
1936static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001937 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001938{
1939#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001940 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1941 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001942#else
1943 return -EOPNOTSUPP;
1944#endif
1945}
1946
1947static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001948 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001949{
1950#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001951 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1952 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001953#else
1954 return -EOPNOTSUPP;
1955#endif
1956}
1957
Jens Axboe17f2fe32019-10-17 14:42:58 -06001958static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1959 struct io_kiocb **nxt, bool force_nonblock)
1960{
1961#if defined(CONFIG_NET)
1962 struct sockaddr __user *addr;
1963 int __user *addr_len;
1964 unsigned file_flags;
1965 int flags, ret;
1966
1967 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1968 return -EINVAL;
1969 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1970 return -EINVAL;
1971
1972 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1973 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1974 flags = READ_ONCE(sqe->accept_flags);
1975 file_flags = force_nonblock ? O_NONBLOCK : 0;
1976
1977 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1978 if (ret == -EAGAIN && force_nonblock) {
1979 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1980 return -EAGAIN;
1981 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001982 if (ret == -ERESTARTSYS)
1983 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001984 if (ret < 0 && (req->flags & REQ_F_LINK))
1985 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001986 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001987 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001988 return 0;
1989#else
1990 return -EOPNOTSUPP;
1991#endif
1992}
1993
Jens Axboeeac406c2019-11-14 12:09:58 -07001994static inline void io_poll_remove_req(struct io_kiocb *req)
1995{
1996 if (!RB_EMPTY_NODE(&req->rb_node)) {
1997 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
1998 RB_CLEAR_NODE(&req->rb_node);
1999 }
2000}
2001
Jens Axboe221c5eb2019-01-17 09:41:58 -07002002static void io_poll_remove_one(struct io_kiocb *req)
2003{
2004 struct io_poll_iocb *poll = &req->poll;
2005
2006 spin_lock(&poll->head->lock);
2007 WRITE_ONCE(poll->canceled, true);
2008 if (!list_empty(&poll->wait.entry)) {
2009 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002010 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002011 }
2012 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002013 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002014}
2015
2016static void io_poll_remove_all(struct io_ring_ctx *ctx)
2017{
Jens Axboeeac406c2019-11-14 12:09:58 -07002018 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002019 struct io_kiocb *req;
2020
2021 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002022 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2023 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002024 io_poll_remove_one(req);
2025 }
2026 spin_unlock_irq(&ctx->completion_lock);
2027}
2028
Jens Axboe47f46762019-11-09 17:43:02 -07002029static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2030{
Jens Axboeeac406c2019-11-14 12:09:58 -07002031 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002032 struct io_kiocb *req;
2033
Jens Axboeeac406c2019-11-14 12:09:58 -07002034 p = ctx->cancel_tree.rb_node;
2035 while (p) {
2036 parent = p;
2037 req = rb_entry(parent, struct io_kiocb, rb_node);
2038 if (sqe_addr < req->user_data) {
2039 p = p->rb_left;
2040 } else if (sqe_addr > req->user_data) {
2041 p = p->rb_right;
2042 } else {
2043 io_poll_remove_one(req);
2044 return 0;
2045 }
Jens Axboe47f46762019-11-09 17:43:02 -07002046 }
2047
2048 return -ENOENT;
2049}
2050
Jens Axboe221c5eb2019-01-17 09:41:58 -07002051/*
2052 * Find a running poll command that matches one specified in sqe->addr,
2053 * and remove it if found.
2054 */
2055static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2056{
2057 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002058 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002059
2060 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2061 return -EINVAL;
2062 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2063 sqe->poll_events)
2064 return -EINVAL;
2065
2066 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002067 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002068 spin_unlock_irq(&ctx->completion_lock);
2069
Jens Axboe78e19bb2019-11-06 15:21:34 -07002070 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002071 if (ret < 0 && (req->flags & REQ_F_LINK))
2072 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002073 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002074 return 0;
2075}
2076
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002077static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002078{
Jackie Liua197f662019-11-08 08:09:12 -07002079 struct io_ring_ctx *ctx = req->ctx;
2080
Jens Axboe8c838782019-03-12 15:48:16 -06002081 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002082 if (error)
2083 io_cqring_fill_event(req, error);
2084 else
2085 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002086 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002087}
2088
Jens Axboe561fb042019-10-24 07:25:42 -06002089static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002090{
Jens Axboe561fb042019-10-24 07:25:42 -06002091 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002092 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2093 struct io_poll_iocb *poll = &req->poll;
2094 struct poll_table_struct pt = { ._key = poll->events };
2095 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002096 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002097 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002098 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002099
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002100 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002101 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002102 ret = -ECANCELED;
2103 } else if (READ_ONCE(poll->canceled)) {
2104 ret = -ECANCELED;
2105 }
Jens Axboe561fb042019-10-24 07:25:42 -06002106
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002107 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002108 mask = vfs_poll(poll->file, &pt) & poll->events;
2109
2110 /*
2111 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2112 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2113 * synchronize with them. In the cancellation case the list_del_init
2114 * itself is not actually needed, but harmless so we keep it in to
2115 * avoid further branches in the fast path.
2116 */
2117 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002118 if (!mask && ret != -ECANCELED) {
Jens Axboe221c5eb2019-01-17 09:41:58 -07002119 add_wait_queue(poll->head, &poll->wait);
2120 spin_unlock_irq(&ctx->completion_lock);
2121 return;
2122 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002123 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002124 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002125 spin_unlock_irq(&ctx->completion_lock);
2126
Jens Axboe8c838782019-03-12 15:48:16 -06002127 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002128
Jens Axboefba38c22019-11-18 12:27:57 -07002129 if (ret < 0 && req->flags & REQ_F_LINK)
2130 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002131 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002132 if (nxt)
2133 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002134}
2135
2136static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2137 void *key)
2138{
2139 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2140 wait);
2141 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2142 struct io_ring_ctx *ctx = req->ctx;
2143 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002144 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002145
2146 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002147 if (mask && !(mask & poll->events))
2148 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002149
2150 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002151
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002152 /*
2153 * Run completion inline if we can. We're using trylock here because
2154 * we are violating the completion_lock -> poll wq lock ordering.
2155 * If we have a link timeout we're going to need the completion_lock
2156 * for finalizing the request, mark us as having grabbed that already.
2157 */
Jens Axboe8c838782019-03-12 15:48:16 -06002158 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002159 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002160 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002161 req->flags |= REQ_F_COMP_LOCKED;
2162 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002163 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2164
2165 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002166 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002167 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002168 }
2169
Jens Axboe221c5eb2019-01-17 09:41:58 -07002170 return 1;
2171}
2172
2173struct io_poll_table {
2174 struct poll_table_struct pt;
2175 struct io_kiocb *req;
2176 int error;
2177};
2178
2179static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2180 struct poll_table_struct *p)
2181{
2182 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2183
2184 if (unlikely(pt->req->poll.head)) {
2185 pt->error = -EINVAL;
2186 return;
2187 }
2188
2189 pt->error = 0;
2190 pt->req->poll.head = head;
2191 add_wait_queue(head, &pt->req->poll.wait);
2192}
2193
Jens Axboeeac406c2019-11-14 12:09:58 -07002194static void io_poll_req_insert(struct io_kiocb *req)
2195{
2196 struct io_ring_ctx *ctx = req->ctx;
2197 struct rb_node **p = &ctx->cancel_tree.rb_node;
2198 struct rb_node *parent = NULL;
2199 struct io_kiocb *tmp;
2200
2201 while (*p) {
2202 parent = *p;
2203 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2204 if (req->user_data < tmp->user_data)
2205 p = &(*p)->rb_left;
2206 else
2207 p = &(*p)->rb_right;
2208 }
2209 rb_link_node(&req->rb_node, parent, p);
2210 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2211}
2212
Jens Axboe89723d02019-11-05 15:32:58 -07002213static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2214 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002215{
2216 struct io_poll_iocb *poll = &req->poll;
2217 struct io_ring_ctx *ctx = req->ctx;
2218 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002219 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002220 __poll_t mask;
2221 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002222
2223 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2224 return -EINVAL;
2225 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2226 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002227 if (!poll->file)
2228 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002229
Jens Axboe6cc47d12019-09-18 11:18:23 -06002230 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002231 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002232 events = READ_ONCE(sqe->poll_events);
2233 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002234 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002235
Jens Axboe221c5eb2019-01-17 09:41:58 -07002236 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002237 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002238 poll->canceled = false;
2239
2240 ipt.pt._qproc = io_poll_queue_proc;
2241 ipt.pt._key = poll->events;
2242 ipt.req = req;
2243 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2244
2245 /* initialized the list so that we can do list_empty checks */
2246 INIT_LIST_HEAD(&poll->wait.entry);
2247 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2248
Jens Axboe36703242019-07-25 10:20:18 -06002249 INIT_LIST_HEAD(&req->list);
2250
Jens Axboe221c5eb2019-01-17 09:41:58 -07002251 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002252
2253 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002254 if (likely(poll->head)) {
2255 spin_lock(&poll->head->lock);
2256 if (unlikely(list_empty(&poll->wait.entry))) {
2257 if (ipt.error)
2258 cancel = true;
2259 ipt.error = 0;
2260 mask = 0;
2261 }
2262 if (mask || ipt.error)
2263 list_del_init(&poll->wait.entry);
2264 else if (cancel)
2265 WRITE_ONCE(poll->canceled, true);
2266 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002267 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002268 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002269 }
Jens Axboe8c838782019-03-12 15:48:16 -06002270 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002271 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002272 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002273 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002274 spin_unlock_irq(&ctx->completion_lock);
2275
Jens Axboe8c838782019-03-12 15:48:16 -06002276 if (mask) {
2277 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002278 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002279 }
Jens Axboe8c838782019-03-12 15:48:16 -06002280 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002281}
2282
Jens Axboe5262f562019-09-17 12:26:57 -06002283static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2284{
Jens Axboead8a48a2019-11-15 08:49:11 -07002285 struct io_timeout_data *data = container_of(timer,
2286 struct io_timeout_data, timer);
2287 struct io_kiocb *req = data->req;
2288 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002289 unsigned long flags;
2290
Jens Axboe5262f562019-09-17 12:26:57 -06002291 atomic_inc(&ctx->cq_timeouts);
2292
2293 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002294 /*
Jens Axboe11365042019-10-16 09:08:32 -06002295 * We could be racing with timeout deletion. If the list is empty,
2296 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002297 */
Jens Axboe842f9612019-10-29 12:34:10 -06002298 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002299 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002300
Jens Axboe11365042019-10-16 09:08:32 -06002301 /*
2302 * Adjust the reqs sequence before the current one because it
2303 * will consume a slot in the cq_ring and the the cq_tail
2304 * pointer will be increased, otherwise other timeout reqs may
2305 * return in advance without waiting for enough wait_nr.
2306 */
2307 prev = req;
2308 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2309 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002310 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002311 }
Jens Axboe842f9612019-10-29 12:34:10 -06002312
Jens Axboe78e19bb2019-11-06 15:21:34 -07002313 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002314 io_commit_cqring(ctx);
2315 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2316
2317 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002318 if (req->flags & REQ_F_LINK)
2319 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002320 io_put_req(req);
2321 return HRTIMER_NORESTART;
2322}
2323
Jens Axboe47f46762019-11-09 17:43:02 -07002324static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2325{
2326 struct io_kiocb *req;
2327 int ret = -ENOENT;
2328
2329 list_for_each_entry(req, &ctx->timeout_list, list) {
2330 if (user_data == req->user_data) {
2331 list_del_init(&req->list);
2332 ret = 0;
2333 break;
2334 }
2335 }
2336
2337 if (ret == -ENOENT)
2338 return ret;
2339
Jens Axboead8a48a2019-11-15 08:49:11 -07002340 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002341 if (ret == -1)
2342 return -EALREADY;
2343
Jens Axboefba38c22019-11-18 12:27:57 -07002344 if (req->flags & REQ_F_LINK)
2345 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002346 io_cqring_fill_event(req, -ECANCELED);
2347 io_put_req(req);
2348 return 0;
2349}
2350
Jens Axboe11365042019-10-16 09:08:32 -06002351/*
2352 * Remove or update an existing timeout command
2353 */
2354static int io_timeout_remove(struct io_kiocb *req,
2355 const struct io_uring_sqe *sqe)
2356{
2357 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002358 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002359 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002360
2361 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2362 return -EINVAL;
2363 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2364 return -EINVAL;
2365 flags = READ_ONCE(sqe->timeout_flags);
2366 if (flags)
2367 return -EINVAL;
2368
Jens Axboe11365042019-10-16 09:08:32 -06002369 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002370 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002371
Jens Axboe47f46762019-11-09 17:43:02 -07002372 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002373 io_commit_cqring(ctx);
2374 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002375 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002376 if (ret < 0 && req->flags & REQ_F_LINK)
2377 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002378 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002379 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002380}
2381
Jens Axboead8a48a2019-11-15 08:49:11 -07002382static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002383{
Jens Axboead8a48a2019-11-15 08:49:11 -07002384 const struct io_uring_sqe *sqe = req->submit.sqe;
2385 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002386 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002387
Jens Axboead8a48a2019-11-15 08:49:11 -07002388 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002389 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002390 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002391 return -EINVAL;
2392 flags = READ_ONCE(sqe->timeout_flags);
2393 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002394 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002395
Jens Axboead8a48a2019-11-15 08:49:11 -07002396 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2397 if (!data)
2398 return -ENOMEM;
2399 data->req = req;
2400 req->timeout.data = data;
2401 req->flags |= REQ_F_TIMEOUT;
2402
2403 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002404 return -EFAULT;
2405
Jens Axboe11365042019-10-16 09:08:32 -06002406 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002407 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002408 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002409 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002410
Jens Axboead8a48a2019-11-15 08:49:11 -07002411 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2412 return 0;
2413}
2414
2415static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2416{
2417 unsigned count;
2418 struct io_ring_ctx *ctx = req->ctx;
2419 struct io_timeout_data *data;
2420 struct list_head *entry;
2421 unsigned span = 0;
2422 int ret;
2423
2424 ret = io_timeout_setup(req);
2425 /* common setup allows flags (like links) set, we don't */
2426 if (!ret && sqe->flags)
2427 ret = -EINVAL;
2428 if (ret)
2429 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002430
Jens Axboe5262f562019-09-17 12:26:57 -06002431 /*
2432 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002433 * timeout event to be satisfied. If it isn't set, then this is
2434 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002435 */
2436 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002437 if (!count) {
2438 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2439 spin_lock_irq(&ctx->completion_lock);
2440 entry = ctx->timeout_list.prev;
2441 goto add;
2442 }
Jens Axboe5262f562019-09-17 12:26:57 -06002443
2444 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002445 /* reuse it to store the count */
2446 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002447
2448 /*
2449 * Insertion sort, ensuring the first entry in the list is always
2450 * the one we need first.
2451 */
Jens Axboe5262f562019-09-17 12:26:57 -06002452 spin_lock_irq(&ctx->completion_lock);
2453 list_for_each_prev(entry, &ctx->timeout_list) {
2454 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002455 unsigned nxt_sq_head;
2456 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002457
Jens Axboe93bd25b2019-11-11 23:34:31 -07002458 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2459 continue;
2460
yangerkun5da0fb12019-10-15 21:59:29 +08002461 /*
2462 * Since cached_sq_head + count - 1 can overflow, use type long
2463 * long to store it.
2464 */
2465 tmp = (long long)ctx->cached_sq_head + count - 1;
2466 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2467 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2468
2469 /*
2470 * cached_sq_head may overflow, and it will never overflow twice
2471 * once there is some timeout req still be valid.
2472 */
2473 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002474 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002475
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002476 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002477 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002478
2479 /*
2480 * Sequence of reqs after the insert one and itself should
2481 * be adjusted because each timeout req consumes a slot.
2482 */
2483 span++;
2484 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002485 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002486 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002487add:
Jens Axboe5262f562019-09-17 12:26:57 -06002488 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002489 data = req->timeout.data;
2490 data->timer.function = io_timeout_fn;
2491 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002492 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002493 return 0;
2494}
2495
Jens Axboe62755e32019-10-28 21:49:21 -06002496static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002497{
Jens Axboe62755e32019-10-28 21:49:21 -06002498 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002499
Jens Axboe62755e32019-10-28 21:49:21 -06002500 return req->user_data == (unsigned long) data;
2501}
2502
Jens Axboee977d6d2019-11-05 12:39:45 -07002503static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002504{
Jens Axboe62755e32019-10-28 21:49:21 -06002505 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002506 int ret = 0;
2507
Jens Axboe62755e32019-10-28 21:49:21 -06002508 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2509 switch (cancel_ret) {
2510 case IO_WQ_CANCEL_OK:
2511 ret = 0;
2512 break;
2513 case IO_WQ_CANCEL_RUNNING:
2514 ret = -EALREADY;
2515 break;
2516 case IO_WQ_CANCEL_NOTFOUND:
2517 ret = -ENOENT;
2518 break;
2519 }
2520
Jens Axboee977d6d2019-11-05 12:39:45 -07002521 return ret;
2522}
2523
Jens Axboe47f46762019-11-09 17:43:02 -07002524static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2525 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002526 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002527{
2528 unsigned long flags;
2529 int ret;
2530
2531 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2532 if (ret != -ENOENT) {
2533 spin_lock_irqsave(&ctx->completion_lock, flags);
2534 goto done;
2535 }
2536
2537 spin_lock_irqsave(&ctx->completion_lock, flags);
2538 ret = io_timeout_cancel(ctx, sqe_addr);
2539 if (ret != -ENOENT)
2540 goto done;
2541 ret = io_poll_cancel(ctx, sqe_addr);
2542done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002543 if (!ret)
2544 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002545 io_cqring_fill_event(req, ret);
2546 io_commit_cqring(ctx);
2547 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2548 io_cqring_ev_posted(ctx);
2549
2550 if (ret < 0 && (req->flags & REQ_F_LINK))
2551 req->flags |= REQ_F_FAIL_LINK;
2552 io_put_req_find_next(req, nxt);
2553}
2554
Jens Axboee977d6d2019-11-05 12:39:45 -07002555static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2556 struct io_kiocb **nxt)
2557{
2558 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002559
2560 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2561 return -EINVAL;
2562 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2563 sqe->cancel_flags)
2564 return -EINVAL;
2565
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002566 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002567 return 0;
2568}
2569
Jackie Liua197f662019-11-08 08:09:12 -07002570static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002571{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002572 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002573 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002574 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002575
Bob Liu9d858b22019-11-13 18:06:25 +08002576 /* Still need defer if there is pending req in defer list. */
2577 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002578 return 0;
2579
2580 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2581 if (!sqe_copy)
2582 return -EAGAIN;
2583
2584 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002585 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002586 spin_unlock_irq(&ctx->completion_lock);
2587 kfree(sqe_copy);
2588 return 0;
2589 }
2590
2591 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002592 req->flags |= REQ_F_FREE_SQE;
Jens Axboede0617e2019-04-06 21:51:27 -06002593 req->submit.sqe = sqe_copy;
2594
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002595 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002596 list_add_tail(&req->list, &ctx->defer_list);
2597 spin_unlock_irq(&ctx->completion_lock);
2598 return -EIOCBQUEUED;
2599}
2600
Jackie Liua197f662019-11-08 08:09:12 -07002601static int __io_submit_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2602 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002603{
Jens Axboee0c5c572019-03-12 10:18:47 -06002604 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002605 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002606 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002607
2608 opcode = READ_ONCE(s->sqe->opcode);
2609 switch (opcode) {
2610 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002611 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002612 break;
2613 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002614 if (unlikely(s->sqe->buf_index))
2615 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002616 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002617 break;
2618 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002619 if (unlikely(s->sqe->buf_index))
2620 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002621 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002622 break;
2623 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002624 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002625 break;
2626 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002627 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002628 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002629 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002630 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002631 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002632 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002633 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002634 break;
2635 case IORING_OP_POLL_REMOVE:
2636 ret = io_poll_remove(req, s->sqe);
2637 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002638 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002639 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002640 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002641 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002642 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002643 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002644 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002645 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002646 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002647 case IORING_OP_TIMEOUT:
2648 ret = io_timeout(req, s->sqe);
2649 break;
Jens Axboe11365042019-10-16 09:08:32 -06002650 case IORING_OP_TIMEOUT_REMOVE:
2651 ret = io_timeout_remove(req, s->sqe);
2652 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002653 case IORING_OP_ACCEPT:
2654 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2655 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002656 case IORING_OP_ASYNC_CANCEL:
2657 ret = io_async_cancel(req, s->sqe, nxt);
2658 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002659 default:
2660 ret = -EINVAL;
2661 break;
2662 }
2663
Jens Axboedef596e2019-01-09 08:59:42 -07002664 if (ret)
2665 return ret;
2666
2667 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002668 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002669 return -EAGAIN;
2670
2671 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002672 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002673 mutex_lock(&ctx->uring_lock);
2674 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002675 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002676 mutex_unlock(&ctx->uring_lock);
2677 }
2678
2679 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002680}
2681
Jens Axboeb76da702019-11-20 13:05:32 -07002682static void io_link_work_cb(struct io_wq_work **workptr)
2683{
2684 struct io_wq_work *work = *workptr;
2685 struct io_kiocb *link = work->data;
2686
2687 io_queue_linked_timeout(link);
2688 work->func = io_wq_submit_work;
2689}
2690
Jens Axboe561fb042019-10-24 07:25:42 -06002691static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002692{
Jens Axboe561fb042019-10-24 07:25:42 -06002693 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002694 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002695 struct sqe_submit *s = &req->submit;
Jens Axboe561fb042019-10-24 07:25:42 -06002696 struct io_kiocb *nxt = NULL;
2697 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002698
Jens Axboe561fb042019-10-24 07:25:42 -06002699 /* Ensure we clear previously set non-block flag */
2700 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002701
Jens Axboe561fb042019-10-24 07:25:42 -06002702 if (work->flags & IO_WQ_WORK_CANCEL)
2703 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002704
Jens Axboe561fb042019-10-24 07:25:42 -06002705 if (!ret) {
2706 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2707 s->in_async = true;
2708 do {
Jackie Liua197f662019-11-08 08:09:12 -07002709 ret = __io_submit_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002710 /*
2711 * We can get EAGAIN for polled IO even though we're
2712 * forcing a sync submission from here, since we can't
2713 * wait for request slots on the block side.
2714 */
2715 if (ret != -EAGAIN)
2716 break;
2717 cond_resched();
2718 } while (1);
2719 }
Jens Axboe31b51512019-01-18 22:56:34 -07002720
Jens Axboe561fb042019-10-24 07:25:42 -06002721 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002722 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002723
Jens Axboe561fb042019-10-24 07:25:42 -06002724 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002725 if (req->flags & REQ_F_LINK)
2726 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002727 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002728 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002729 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002730
Jens Axboe561fb042019-10-24 07:25:42 -06002731 /* if a dependent link is ready, pass it back */
2732 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002733 struct io_kiocb *link;
2734
2735 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06002736 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07002737 if (link) {
2738 nxt->work.flags |= IO_WQ_WORK_CB;
2739 nxt->work.func = io_link_work_cb;
2740 nxt->work.data = link;
2741 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002742 }
Jens Axboe31b51512019-01-18 22:56:34 -07002743}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002744
Jens Axboe09bb8392019-03-13 12:39:28 -06002745static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2746{
2747 int op = READ_ONCE(sqe->opcode);
2748
2749 switch (op) {
2750 case IORING_OP_NOP:
2751 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002752 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002753 case IORING_OP_TIMEOUT_REMOVE:
2754 case IORING_OP_ASYNC_CANCEL:
2755 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002756 return false;
2757 default:
2758 return true;
2759 }
2760}
2761
Jens Axboe65e19f52019-10-26 07:20:21 -06002762static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2763 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002764{
Jens Axboe65e19f52019-10-26 07:20:21 -06002765 struct fixed_file_table *table;
2766
2767 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2768 return table->files[index & IORING_FILE_TABLE_MASK];
2769}
2770
Jackie Liua197f662019-11-08 08:09:12 -07002771static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002772{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002773 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002774 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002775 unsigned flags;
2776 int fd;
2777
2778 flags = READ_ONCE(s->sqe->flags);
2779 fd = READ_ONCE(s->sqe->fd);
2780
Jackie Liu4fe2c962019-09-09 20:50:40 +08002781 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002782 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002783 /*
2784 * All io need record the previous position, if LINK vs DARIN,
2785 * it can be used to mark the position of the first IO in the
2786 * link list.
2787 */
2788 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002789
Jens Axboe60c112b2019-06-21 10:20:18 -06002790 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002791 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002792
2793 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002794 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002795 (unsigned) fd >= ctx->nr_user_files))
2796 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002797 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002798 req->file = io_file_from_index(ctx, fd);
2799 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002800 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002801 req->flags |= REQ_F_FIXED_FILE;
2802 } else {
2803 if (s->needs_fixed_file)
2804 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002805 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002806 req->file = io_file_get(state, fd);
2807 if (unlikely(!req->file))
2808 return -EBADF;
2809 }
2810
2811 return 0;
2812}
2813
Jackie Liua197f662019-11-08 08:09:12 -07002814static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002815{
Jens Axboefcb323c2019-10-24 12:39:47 -06002816 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002817 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002818
2819 rcu_read_lock();
2820 spin_lock_irq(&ctx->inflight_lock);
2821 /*
2822 * We use the f_ops->flush() handler to ensure that we can flush
2823 * out work accessing these files if the fd is closed. Check if
2824 * the fd has changed since we started down this path, and disallow
2825 * this operation if it has.
2826 */
2827 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2828 list_add(&req->inflight_entry, &ctx->inflight_list);
2829 req->flags |= REQ_F_INFLIGHT;
2830 req->work.files = current->files;
2831 ret = 0;
2832 }
2833 spin_unlock_irq(&ctx->inflight_lock);
2834 rcu_read_unlock();
2835
2836 return ret;
2837}
2838
Jens Axboe2665abf2019-11-05 12:40:47 -07002839static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2840{
Jens Axboead8a48a2019-11-15 08:49:11 -07002841 struct io_timeout_data *data = container_of(timer,
2842 struct io_timeout_data, timer);
2843 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002844 struct io_ring_ctx *ctx = req->ctx;
2845 struct io_kiocb *prev = NULL;
2846 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002847
2848 spin_lock_irqsave(&ctx->completion_lock, flags);
2849
2850 /*
2851 * We don't expect the list to be empty, that will only happen if we
2852 * race with the completion of the linked work.
2853 */
2854 if (!list_empty(&req->list)) {
2855 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07002856 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002857 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07002858 prev->flags &= ~REQ_F_LINK_TIMEOUT;
2859 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07002860 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002861 }
2862
2863 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2864
2865 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07002866 if (prev->flags & REQ_F_LINK)
2867 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002868 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2869 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07002870 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002871 } else {
2872 io_cqring_add_event(req, -ETIME);
2873 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002874 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002875 return HRTIMER_NORESTART;
2876}
2877
Jens Axboead8a48a2019-11-15 08:49:11 -07002878static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002879{
Jens Axboe76a46e02019-11-10 23:34:16 -07002880 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002881
Jens Axboe76a46e02019-11-10 23:34:16 -07002882 /*
2883 * If the list is now empty, then our linked request finished before
2884 * we got a chance to setup the timer
2885 */
2886 spin_lock_irq(&ctx->completion_lock);
2887 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002888 struct io_timeout_data *data = req->timeout.data;
2889
Jens Axboead8a48a2019-11-15 08:49:11 -07002890 data->timer.function = io_link_timeout_fn;
2891 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2892 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002893 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002894 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002895
Jens Axboe2665abf2019-11-05 12:40:47 -07002896 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002897 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002898}
2899
Jens Axboead8a48a2019-11-15 08:49:11 -07002900static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002901{
2902 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002903
Jens Axboe2665abf2019-11-05 12:40:47 -07002904 if (!(req->flags & REQ_F_LINK))
2905 return NULL;
2906
2907 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002908 if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT)
2909 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002910
Jens Axboe76a46e02019-11-10 23:34:16 -07002911 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07002912 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002913}
2914
Jens Axboe0e0702d2019-11-14 21:42:10 -07002915static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002916{
Jens Axboe94ae5e72019-11-14 19:39:52 -07002917 struct io_kiocb *nxt = io_prep_linked_timeout(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002918 int ret;
2919
Jackie Liua197f662019-11-08 08:09:12 -07002920 ret = __io_submit_sqe(req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002921
2922 /*
2923 * We async punt it if the file wasn't marked NOWAIT, or if the file
2924 * doesn't support non-blocking read/write attempts
2925 */
2926 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2927 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002928 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002929 struct io_uring_sqe *sqe_copy;
2930
Jackie Liu954dab12019-09-18 10:37:52 +08002931 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002932 if (!sqe_copy)
2933 goto err;
Jens Axboee65ef562019-03-12 10:16:44 -06002934
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002935 s->sqe = sqe_copy;
2936 req->flags |= REQ_F_FREE_SQE;
2937
2938 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2939 ret = io_grab_files(req);
2940 if (ret)
2941 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002942 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002943
2944 /*
2945 * Queued up for async execution, worker will release
2946 * submit reference when the iocb is actually submitted.
2947 */
2948 io_queue_async_work(req);
2949 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002950 }
Jens Axboee65ef562019-03-12 10:16:44 -06002951
Jens Axboefcb323c2019-10-24 12:39:47 -06002952err:
Jens Axboee65ef562019-03-12 10:16:44 -06002953 /* drop submission reference */
2954 io_put_req(req);
2955
Jens Axboe76a46e02019-11-10 23:34:16 -07002956 if (nxt) {
2957 if (!ret)
Jens Axboead8a48a2019-11-15 08:49:11 -07002958 io_queue_linked_timeout(nxt);
Jens Axboe76a46e02019-11-10 23:34:16 -07002959 else
2960 io_put_req(nxt);
2961 }
2962
Jens Axboee65ef562019-03-12 10:16:44 -06002963 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002964 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002965 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002966 if (req->flags & REQ_F_LINK)
2967 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002968 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002969 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002970}
2971
Jens Axboe0e0702d2019-11-14 21:42:10 -07002972static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002973{
2974 int ret;
2975
Jackie Liua197f662019-11-08 08:09:12 -07002976 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002977 if (ret) {
2978 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002979 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03002980 if (req->flags & REQ_F_LINK)
2981 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002982 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002983 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07002984 } else
2985 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002986}
2987
Jens Axboe0e0702d2019-11-14 21:42:10 -07002988static void io_queue_link_head(struct io_kiocb *req, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002989{
2990 int ret;
2991 int need_submit = false;
Jackie Liua197f662019-11-08 08:09:12 -07002992 struct io_ring_ctx *ctx = req->ctx;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002993
Jens Axboe94ae5e72019-11-14 19:39:52 -07002994 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
2995 ret = -ECANCELED;
2996 goto err;
2997 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07002998 if (!shadow) {
2999 io_queue_sqe(req);
3000 return;
3001 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08003002
3003 /*
3004 * Mark the first IO in link list as DRAIN, let all the following
3005 * IOs enter the defer list. all IO needs to be completed before link
3006 * list.
3007 */
3008 req->flags |= REQ_F_IO_DRAIN;
Jackie Liua197f662019-11-08 08:09:12 -07003009 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003010 if (ret) {
3011 if (ret != -EIOCBQUEUED) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003012err:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003013 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03003014 if (req->flags & REQ_F_LINK)
3015 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003016 io_double_put_req(req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07003017 if (shadow)
3018 __io_free_req(shadow);
Jens Axboe0e0702d2019-11-14 21:42:10 -07003019 return;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003020 }
3021 } else {
3022 /*
3023 * If ret == 0 means that all IOs in front of link io are
3024 * running done. let's queue link head.
3025 */
3026 need_submit = true;
3027 }
3028
3029 /* Insert shadow req to defer_list, blocking next IOs */
3030 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003031 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003032 list_add_tail(&shadow->list, &ctx->defer_list);
3033 spin_unlock_irq(&ctx->completion_lock);
3034
3035 if (need_submit)
Jens Axboe0e0702d2019-11-14 21:42:10 -07003036 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003037}
3038
Jens Axboe9e645e112019-05-10 16:07:28 -06003039#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3040
Jackie Liua197f662019-11-08 08:09:12 -07003041static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3042 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003043{
Pavel Begunkov267bc902019-11-07 01:41:08 +03003044 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07003045 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003046 int ret;
3047
Jens Axboe78e19bb2019-11-06 15:21:34 -07003048 req->user_data = s->sqe->user_data;
3049
Jens Axboe9e645e112019-05-10 16:07:28 -06003050 /* enforce forwards compatibility on users */
3051 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
3052 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003053 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003054 }
3055
Jackie Liua197f662019-11-08 08:09:12 -07003056 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003057 if (unlikely(ret)) {
3058err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003059 io_cqring_add_event(req, ret);
3060 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003061 return;
3062 }
3063
Jens Axboe9e645e112019-05-10 16:07:28 -06003064 /*
3065 * If we already have a head request, queue this one for async
3066 * submittal once the head completes. If we don't have a head but
3067 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3068 * submitted sync once the chain is complete. If none of those
3069 * conditions are true (normal request), then just queue it.
3070 */
3071 if (*link) {
3072 struct io_kiocb *prev = *link;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003073 struct io_uring_sqe *sqe_copy;
Jens Axboe9e645e112019-05-10 16:07:28 -06003074
Jens Axboe94ae5e72019-11-14 19:39:52 -07003075 if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
3076 ret = io_timeout_setup(req);
3077 /* common setup allows offset being set, we don't */
3078 if (!ret && s->sqe->off)
3079 ret = -EINVAL;
3080 if (ret) {
3081 prev->flags |= REQ_F_FAIL_LINK;
3082 goto err_req;
3083 }
3084 }
3085
Jens Axboe9e645e112019-05-10 16:07:28 -06003086 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
3087 if (!sqe_copy) {
3088 ret = -EAGAIN;
3089 goto err_req;
3090 }
3091
3092 s->sqe = sqe_copy;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003093 req->flags |= REQ_F_FREE_SQE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003094 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003095 list_add_tail(&req->list, &prev->link_list);
3096 } else if (s->sqe->flags & IOSQE_IO_LINK) {
3097 req->flags |= REQ_F_LINK;
3098
Jens Axboe9e645e112019-05-10 16:07:28 -06003099 INIT_LIST_HEAD(&req->link_list);
3100 *link = req;
3101 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003102 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003103 }
3104}
3105
Jens Axboe9a56a232019-01-09 09:06:50 -07003106/*
3107 * Batched submission is done, ensure local IO is flushed out.
3108 */
3109static void io_submit_state_end(struct io_submit_state *state)
3110{
3111 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003112 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003113 if (state->free_reqs)
3114 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3115 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003116}
3117
3118/*
3119 * Start submission side cache.
3120 */
3121static void io_submit_state_start(struct io_submit_state *state,
3122 struct io_ring_ctx *ctx, unsigned max_ios)
3123{
3124 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003125 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003126 state->file = NULL;
3127 state->ios_left = max_ios;
3128}
3129
Jens Axboe2b188cc2019-01-07 10:46:33 -07003130static void io_commit_sqring(struct io_ring_ctx *ctx)
3131{
Hristo Venev75b28af2019-08-26 17:23:46 +00003132 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003133
Hristo Venev75b28af2019-08-26 17:23:46 +00003134 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003135 /*
3136 * Ensure any loads from the SQEs are done at this point,
3137 * since once we write the new head, the application could
3138 * write new data to them.
3139 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003140 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003141 }
3142}
3143
3144/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003145 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3146 * that is mapped by userspace. This means that care needs to be taken to
3147 * ensure that reads are stable, as we cannot rely on userspace always
3148 * being a good citizen. If members of the sqe are validated and then later
3149 * used, it's important that those reads are done through READ_ONCE() to
3150 * prevent a re-load down the line.
3151 */
3152static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
3153{
Hristo Venev75b28af2019-08-26 17:23:46 +00003154 struct io_rings *rings = ctx->rings;
3155 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003156 unsigned head;
3157
3158 /*
3159 * The cached sq head (or cq tail) serves two purposes:
3160 *
3161 * 1) allows us to batch the cost of updating the user visible
3162 * head updates.
3163 * 2) allows the kernel side to track the head on its own, even
3164 * though the application is the one updating it.
3165 */
3166 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003167 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00003168 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003169 return false;
3170
Hristo Venev75b28af2019-08-26 17:23:46 +00003171 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003172 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003173 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003174 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003175 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003176 ctx->cached_sq_head++;
3177 return true;
3178 }
3179
3180 /* drop invalid entries */
3181 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003182 ctx->cached_sq_dropped++;
3183 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003184 return false;
3185}
3186
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003187static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003188 struct file *ring_file, int ring_fd,
3189 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003190{
3191 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003192 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003193 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003194 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003195 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003196
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003197 if (!list_empty(&ctx->cq_overflow_list)) {
3198 io_cqring_overflow_flush(ctx, false);
3199 return -EBUSY;
3200 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003201
3202 if (nr > IO_PLUG_THRESHOLD) {
3203 io_submit_state_start(&state, ctx, nr);
3204 statep = &state;
3205 }
3206
3207 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003208 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003209 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003210
Pavel Begunkov196be952019-11-07 01:41:06 +03003211 req = io_get_req(ctx, statep);
3212 if (unlikely(!req)) {
3213 if (!submitted)
3214 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003215 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003216 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003217 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003218 __io_free_req(req);
3219 break;
3220 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003221
Pavel Begunkov50585b92019-11-07 01:41:07 +03003222 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003223 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3224 if (!mm_fault) {
3225 use_mm(ctx->sqo_mm);
3226 *mm = ctx->sqo_mm;
3227 }
3228 }
3229
Pavel Begunkov50585b92019-11-07 01:41:07 +03003230 sqe_flags = req->submit.sqe->flags;
3231
3232 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08003233 if (!shadow_req) {
3234 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08003235 if (unlikely(!shadow_req))
3236 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003237 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
3238 refcount_dec(&shadow_req->refs);
3239 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003240 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003241 }
3242
Jackie Liua1041c22019-09-18 17:25:52 +08003243out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03003244 req->submit.ring_file = ring_file;
3245 req->submit.ring_fd = ring_fd;
3246 req->submit.has_user = *mm != NULL;
3247 req->submit.in_async = async;
3248 req->submit.needs_fixed_file = async;
3249 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3250 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003251 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003252 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003253
3254 /*
3255 * If previous wasn't linked and we have a linked command,
3256 * that's the end of the chain. Submit the previous link.
3257 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003258 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Jackie Liua197f662019-11-08 08:09:12 -07003259 io_queue_link_head(link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003260 link = NULL;
3261 shadow_req = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003262 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003263 }
3264
Jens Axboe9e645e112019-05-10 16:07:28 -06003265 if (link)
Jackie Liua197f662019-11-08 08:09:12 -07003266 io_queue_link_head(link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003267 if (statep)
3268 io_submit_state_end(&state);
3269
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003270 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3271 io_commit_sqring(ctx);
3272
Jens Axboe6c271ce2019-01-10 11:22:30 -07003273 return submitted;
3274}
3275
3276static int io_sq_thread(void *data)
3277{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003278 struct io_ring_ctx *ctx = data;
3279 struct mm_struct *cur_mm = NULL;
3280 mm_segment_t old_fs;
3281 DEFINE_WAIT(wait);
3282 unsigned inflight;
3283 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003284 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003285
Jens Axboe206aefd2019-11-07 18:27:42 -07003286 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003287
Jens Axboe6c271ce2019-01-10 11:22:30 -07003288 old_fs = get_fs();
3289 set_fs(USER_DS);
3290
Jens Axboec1edbf52019-11-10 16:56:04 -07003291 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003292 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003293 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003294
3295 if (inflight) {
3296 unsigned nr_events = 0;
3297
3298 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003299 /*
3300 * inflight is the count of the maximum possible
3301 * entries we submitted, but it can be smaller
3302 * if we dropped some of them. If we don't have
3303 * poll entries available, then we know that we
3304 * have nothing left to poll for. Reset the
3305 * inflight count to zero in that case.
3306 */
3307 mutex_lock(&ctx->uring_lock);
3308 if (!list_empty(&ctx->poll_list))
3309 __io_iopoll_check(ctx, &nr_events, 0);
3310 else
3311 inflight = 0;
3312 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003313 } else {
3314 /*
3315 * Normal IO, just pretend everything completed.
3316 * We don't have to poll completions for that.
3317 */
3318 nr_events = inflight;
3319 }
3320
3321 inflight -= nr_events;
3322 if (!inflight)
3323 timeout = jiffies + ctx->sq_thread_idle;
3324 }
3325
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003326 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003327
3328 /*
3329 * If submit got -EBUSY, flag us as needing the application
3330 * to enter the kernel to reap and flush events.
3331 */
3332 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003333 /*
3334 * We're polling. If we're within the defined idle
3335 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003336 * to sleep. The exception is if we got EBUSY doing
3337 * more IO, we should wait for the application to
3338 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003339 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003340 if (inflight ||
3341 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003342 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003343 continue;
3344 }
3345
3346 /*
3347 * Drop cur_mm before scheduling, we can't hold it for
3348 * long periods (or over schedule()). Do this before
3349 * adding ourselves to the waitqueue, as the unuse/drop
3350 * may sleep.
3351 */
3352 if (cur_mm) {
3353 unuse_mm(cur_mm);
3354 mmput(cur_mm);
3355 cur_mm = NULL;
3356 }
3357
3358 prepare_to_wait(&ctx->sqo_wait, &wait,
3359 TASK_INTERRUPTIBLE);
3360
3361 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003362 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003363 /* make sure to read SQ tail after writing flags */
3364 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003365
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003366 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003367 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003368 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003369 finish_wait(&ctx->sqo_wait, &wait);
3370 break;
3371 }
3372 if (signal_pending(current))
3373 flush_signals(current);
3374 schedule();
3375 finish_wait(&ctx->sqo_wait, &wait);
3376
Hristo Venev75b28af2019-08-26 17:23:46 +00003377 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003378 continue;
3379 }
3380 finish_wait(&ctx->sqo_wait, &wait);
3381
Hristo Venev75b28af2019-08-26 17:23:46 +00003382 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003383 }
3384
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003385 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003386 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3387 if (ret > 0)
3388 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003389 }
3390
3391 set_fs(old_fs);
3392 if (cur_mm) {
3393 unuse_mm(cur_mm);
3394 mmput(cur_mm);
3395 }
Jens Axboe06058632019-04-13 09:26:03 -06003396
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003397 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003398
Jens Axboe6c271ce2019-01-10 11:22:30 -07003399 return 0;
3400}
3401
Jens Axboebda52162019-09-24 13:47:15 -06003402struct io_wait_queue {
3403 struct wait_queue_entry wq;
3404 struct io_ring_ctx *ctx;
3405 unsigned to_wait;
3406 unsigned nr_timeouts;
3407};
3408
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003409static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003410{
3411 struct io_ring_ctx *ctx = iowq->ctx;
3412
3413 /*
3414 * Wake up if we have enough events, or if a timeout occured since we
3415 * started waiting. For timeouts, we always want to return to userspace,
3416 * regardless of event count.
3417 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003418 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003419 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3420}
3421
3422static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3423 int wake_flags, void *key)
3424{
3425 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3426 wq);
3427
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003428 /* use noflush == true, as we can't safely rely on locking context */
3429 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003430 return -1;
3431
3432 return autoremove_wake_function(curr, mode, wake_flags, key);
3433}
3434
Jens Axboe2b188cc2019-01-07 10:46:33 -07003435/*
3436 * Wait until events become available, if we don't already have some. The
3437 * application must reap them itself, as they reside on the shared cq ring.
3438 */
3439static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3440 const sigset_t __user *sig, size_t sigsz)
3441{
Jens Axboebda52162019-09-24 13:47:15 -06003442 struct io_wait_queue iowq = {
3443 .wq = {
3444 .private = current,
3445 .func = io_wake_function,
3446 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3447 },
3448 .ctx = ctx,
3449 .to_wait = min_events,
3450 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003451 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003452 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003453
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003454 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003455 return 0;
3456
3457 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003458#ifdef CONFIG_COMPAT
3459 if (in_compat_syscall())
3460 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003461 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003462 else
3463#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003464 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003465
Jens Axboe2b188cc2019-01-07 10:46:33 -07003466 if (ret)
3467 return ret;
3468 }
3469
Jens Axboebda52162019-09-24 13:47:15 -06003470 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003471 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003472 do {
3473 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3474 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003475 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003476 break;
3477 schedule();
3478 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003479 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003480 break;
3481 }
3482 } while (1);
3483 finish_wait(&ctx->wait, &iowq.wq);
3484
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003485 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003486
Hristo Venev75b28af2019-08-26 17:23:46 +00003487 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003488}
3489
Jens Axboe6b063142019-01-10 22:13:58 -07003490static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3491{
3492#if defined(CONFIG_UNIX)
3493 if (ctx->ring_sock) {
3494 struct sock *sock = ctx->ring_sock->sk;
3495 struct sk_buff *skb;
3496
3497 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3498 kfree_skb(skb);
3499 }
3500#else
3501 int i;
3502
Jens Axboe65e19f52019-10-26 07:20:21 -06003503 for (i = 0; i < ctx->nr_user_files; i++) {
3504 struct file *file;
3505
3506 file = io_file_from_index(ctx, i);
3507 if (file)
3508 fput(file);
3509 }
Jens Axboe6b063142019-01-10 22:13:58 -07003510#endif
3511}
3512
3513static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3514{
Jens Axboe65e19f52019-10-26 07:20:21 -06003515 unsigned nr_tables, i;
3516
3517 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003518 return -ENXIO;
3519
3520 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003521 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3522 for (i = 0; i < nr_tables; i++)
3523 kfree(ctx->file_table[i].files);
3524 kfree(ctx->file_table);
3525 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003526 ctx->nr_user_files = 0;
3527 return 0;
3528}
3529
Jens Axboe6c271ce2019-01-10 11:22:30 -07003530static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3531{
3532 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003533 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003534 /*
3535 * The park is a bit of a work-around, without it we get
3536 * warning spews on shutdown with SQPOLL set and affinity
3537 * set to a single CPU.
3538 */
Jens Axboe06058632019-04-13 09:26:03 -06003539 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003540 kthread_stop(ctx->sqo_thread);
3541 ctx->sqo_thread = NULL;
3542 }
3543}
3544
Jens Axboe6b063142019-01-10 22:13:58 -07003545static void io_finish_async(struct io_ring_ctx *ctx)
3546{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003547 io_sq_thread_stop(ctx);
3548
Jens Axboe561fb042019-10-24 07:25:42 -06003549 if (ctx->io_wq) {
3550 io_wq_destroy(ctx->io_wq);
3551 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003552 }
3553}
3554
3555#if defined(CONFIG_UNIX)
3556static void io_destruct_skb(struct sk_buff *skb)
3557{
3558 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3559
Jens Axboe561fb042019-10-24 07:25:42 -06003560 if (ctx->io_wq)
3561 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003562
Jens Axboe6b063142019-01-10 22:13:58 -07003563 unix_destruct_scm(skb);
3564}
3565
3566/*
3567 * Ensure the UNIX gc is aware of our file set, so we are certain that
3568 * the io_uring can be safely unregistered on process exit, even if we have
3569 * loops in the file referencing.
3570 */
3571static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3572{
3573 struct sock *sk = ctx->ring_sock->sk;
3574 struct scm_fp_list *fpl;
3575 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003576 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003577
3578 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3579 unsigned long inflight = ctx->user->unix_inflight + nr;
3580
3581 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3582 return -EMFILE;
3583 }
3584
3585 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3586 if (!fpl)
3587 return -ENOMEM;
3588
3589 skb = alloc_skb(0, GFP_KERNEL);
3590 if (!skb) {
3591 kfree(fpl);
3592 return -ENOMEM;
3593 }
3594
3595 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003596
Jens Axboe08a45172019-10-03 08:11:03 -06003597 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003598 fpl->user = get_uid(ctx->user);
3599 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003600 struct file *file = io_file_from_index(ctx, i + offset);
3601
3602 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003603 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003604 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003605 unix_inflight(fpl->user, fpl->fp[nr_files]);
3606 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003607 }
3608
Jens Axboe08a45172019-10-03 08:11:03 -06003609 if (nr_files) {
3610 fpl->max = SCM_MAX_FD;
3611 fpl->count = nr_files;
3612 UNIXCB(skb).fp = fpl;
3613 skb->destructor = io_destruct_skb;
3614 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3615 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003616
Jens Axboe08a45172019-10-03 08:11:03 -06003617 for (i = 0; i < nr_files; i++)
3618 fput(fpl->fp[i]);
3619 } else {
3620 kfree_skb(skb);
3621 kfree(fpl);
3622 }
Jens Axboe6b063142019-01-10 22:13:58 -07003623
3624 return 0;
3625}
3626
3627/*
3628 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3629 * causes regular reference counting to break down. We rely on the UNIX
3630 * garbage collection to take care of this problem for us.
3631 */
3632static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3633{
3634 unsigned left, total;
3635 int ret = 0;
3636
3637 total = 0;
3638 left = ctx->nr_user_files;
3639 while (left) {
3640 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003641
3642 ret = __io_sqe_files_scm(ctx, this_files, total);
3643 if (ret)
3644 break;
3645 left -= this_files;
3646 total += this_files;
3647 }
3648
3649 if (!ret)
3650 return 0;
3651
3652 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003653 struct file *file = io_file_from_index(ctx, total);
3654
3655 if (file)
3656 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003657 total++;
3658 }
3659
3660 return ret;
3661}
3662#else
3663static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3664{
3665 return 0;
3666}
3667#endif
3668
Jens Axboe65e19f52019-10-26 07:20:21 -06003669static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3670 unsigned nr_files)
3671{
3672 int i;
3673
3674 for (i = 0; i < nr_tables; i++) {
3675 struct fixed_file_table *table = &ctx->file_table[i];
3676 unsigned this_files;
3677
3678 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3679 table->files = kcalloc(this_files, sizeof(struct file *),
3680 GFP_KERNEL);
3681 if (!table->files)
3682 break;
3683 nr_files -= this_files;
3684 }
3685
3686 if (i == nr_tables)
3687 return 0;
3688
3689 for (i = 0; i < nr_tables; i++) {
3690 struct fixed_file_table *table = &ctx->file_table[i];
3691 kfree(table->files);
3692 }
3693 return 1;
3694}
3695
Jens Axboe6b063142019-01-10 22:13:58 -07003696static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3697 unsigned nr_args)
3698{
3699 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003700 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003701 int fd, ret = 0;
3702 unsigned i;
3703
Jens Axboe65e19f52019-10-26 07:20:21 -06003704 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003705 return -EBUSY;
3706 if (!nr_args)
3707 return -EINVAL;
3708 if (nr_args > IORING_MAX_FIXED_FILES)
3709 return -EMFILE;
3710
Jens Axboe65e19f52019-10-26 07:20:21 -06003711 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3712 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3713 GFP_KERNEL);
3714 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003715 return -ENOMEM;
3716
Jens Axboe65e19f52019-10-26 07:20:21 -06003717 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3718 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003719 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003720 return -ENOMEM;
3721 }
3722
Jens Axboe08a45172019-10-03 08:11:03 -06003723 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003724 struct fixed_file_table *table;
3725 unsigned index;
3726
Jens Axboe6b063142019-01-10 22:13:58 -07003727 ret = -EFAULT;
3728 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3729 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003730 /* allow sparse sets */
3731 if (fd == -1) {
3732 ret = 0;
3733 continue;
3734 }
Jens Axboe6b063142019-01-10 22:13:58 -07003735
Jens Axboe65e19f52019-10-26 07:20:21 -06003736 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3737 index = i & IORING_FILE_TABLE_MASK;
3738 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003739
3740 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003741 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003742 break;
3743 /*
3744 * Don't allow io_uring instances to be registered. If UNIX
3745 * isn't enabled, then this causes a reference cycle and this
3746 * instance can never get freed. If UNIX is enabled we'll
3747 * handle it just fine, but there's still no point in allowing
3748 * a ring fd as it doesn't support regular read/write anyway.
3749 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003750 if (table->files[index]->f_op == &io_uring_fops) {
3751 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003752 break;
3753 }
Jens Axboe6b063142019-01-10 22:13:58 -07003754 ret = 0;
3755 }
3756
3757 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003758 for (i = 0; i < ctx->nr_user_files; i++) {
3759 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003760
Jens Axboe65e19f52019-10-26 07:20:21 -06003761 file = io_file_from_index(ctx, i);
3762 if (file)
3763 fput(file);
3764 }
3765 for (i = 0; i < nr_tables; i++)
3766 kfree(ctx->file_table[i].files);
3767
3768 kfree(ctx->file_table);
3769 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003770 ctx->nr_user_files = 0;
3771 return ret;
3772 }
3773
3774 ret = io_sqe_files_scm(ctx);
3775 if (ret)
3776 io_sqe_files_unregister(ctx);
3777
3778 return ret;
3779}
3780
Jens Axboec3a31e62019-10-03 13:59:56 -06003781static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3782{
3783#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003784 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003785 struct sock *sock = ctx->ring_sock->sk;
3786 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3787 struct sk_buff *skb;
3788 int i;
3789
3790 __skb_queue_head_init(&list);
3791
3792 /*
3793 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3794 * remove this entry and rearrange the file array.
3795 */
3796 skb = skb_dequeue(head);
3797 while (skb) {
3798 struct scm_fp_list *fp;
3799
3800 fp = UNIXCB(skb).fp;
3801 for (i = 0; i < fp->count; i++) {
3802 int left;
3803
3804 if (fp->fp[i] != file)
3805 continue;
3806
3807 unix_notinflight(fp->user, fp->fp[i]);
3808 left = fp->count - 1 - i;
3809 if (left) {
3810 memmove(&fp->fp[i], &fp->fp[i + 1],
3811 left * sizeof(struct file *));
3812 }
3813 fp->count--;
3814 if (!fp->count) {
3815 kfree_skb(skb);
3816 skb = NULL;
3817 } else {
3818 __skb_queue_tail(&list, skb);
3819 }
3820 fput(file);
3821 file = NULL;
3822 break;
3823 }
3824
3825 if (!file)
3826 break;
3827
3828 __skb_queue_tail(&list, skb);
3829
3830 skb = skb_dequeue(head);
3831 }
3832
3833 if (skb_peek(&list)) {
3834 spin_lock_irq(&head->lock);
3835 while ((skb = __skb_dequeue(&list)) != NULL)
3836 __skb_queue_tail(head, skb);
3837 spin_unlock_irq(&head->lock);
3838 }
3839#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003840 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003841#endif
3842}
3843
3844static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3845 int index)
3846{
3847#if defined(CONFIG_UNIX)
3848 struct sock *sock = ctx->ring_sock->sk;
3849 struct sk_buff_head *head = &sock->sk_receive_queue;
3850 struct sk_buff *skb;
3851
3852 /*
3853 * See if we can merge this file into an existing skb SCM_RIGHTS
3854 * file set. If there's no room, fall back to allocating a new skb
3855 * and filling it in.
3856 */
3857 spin_lock_irq(&head->lock);
3858 skb = skb_peek(head);
3859 if (skb) {
3860 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3861
3862 if (fpl->count < SCM_MAX_FD) {
3863 __skb_unlink(skb, head);
3864 spin_unlock_irq(&head->lock);
3865 fpl->fp[fpl->count] = get_file(file);
3866 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3867 fpl->count++;
3868 spin_lock_irq(&head->lock);
3869 __skb_queue_head(head, skb);
3870 } else {
3871 skb = NULL;
3872 }
3873 }
3874 spin_unlock_irq(&head->lock);
3875
3876 if (skb) {
3877 fput(file);
3878 return 0;
3879 }
3880
3881 return __io_sqe_files_scm(ctx, 1, index);
3882#else
3883 return 0;
3884#endif
3885}
3886
3887static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3888 unsigned nr_args)
3889{
3890 struct io_uring_files_update up;
3891 __s32 __user *fds;
3892 int fd, i, err;
3893 __u32 done;
3894
Jens Axboe65e19f52019-10-26 07:20:21 -06003895 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003896 return -ENXIO;
3897 if (!nr_args)
3898 return -EINVAL;
3899 if (copy_from_user(&up, arg, sizeof(up)))
3900 return -EFAULT;
3901 if (check_add_overflow(up.offset, nr_args, &done))
3902 return -EOVERFLOW;
3903 if (done > ctx->nr_user_files)
3904 return -EINVAL;
3905
3906 done = 0;
3907 fds = (__s32 __user *) up.fds;
3908 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003909 struct fixed_file_table *table;
3910 unsigned index;
3911
Jens Axboec3a31e62019-10-03 13:59:56 -06003912 err = 0;
3913 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3914 err = -EFAULT;
3915 break;
3916 }
3917 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003918 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3919 index = i & IORING_FILE_TABLE_MASK;
3920 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003921 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003922 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003923 }
3924 if (fd != -1) {
3925 struct file *file;
3926
3927 file = fget(fd);
3928 if (!file) {
3929 err = -EBADF;
3930 break;
3931 }
3932 /*
3933 * Don't allow io_uring instances to be registered. If
3934 * UNIX isn't enabled, then this causes a reference
3935 * cycle and this instance can never get freed. If UNIX
3936 * is enabled we'll handle it just fine, but there's
3937 * still no point in allowing a ring fd as it doesn't
3938 * support regular read/write anyway.
3939 */
3940 if (file->f_op == &io_uring_fops) {
3941 fput(file);
3942 err = -EBADF;
3943 break;
3944 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003945 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003946 err = io_sqe_file_register(ctx, file, i);
3947 if (err)
3948 break;
3949 }
3950 nr_args--;
3951 done++;
3952 up.offset++;
3953 }
3954
3955 return done ? done : err;
3956}
3957
Jens Axboe7d723062019-11-12 22:31:31 -07003958static void io_put_work(struct io_wq_work *work)
3959{
3960 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3961
3962 io_put_req(req);
3963}
3964
3965static void io_get_work(struct io_wq_work *work)
3966{
3967 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3968
3969 refcount_inc(&req->refs);
3970}
3971
Jens Axboe6c271ce2019-01-10 11:22:30 -07003972static int io_sq_offload_start(struct io_ring_ctx *ctx,
3973 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003974{
Jens Axboe561fb042019-10-24 07:25:42 -06003975 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003976 int ret;
3977
Jens Axboe6c271ce2019-01-10 11:22:30 -07003978 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003979 mmgrab(current->mm);
3980 ctx->sqo_mm = current->mm;
3981
Jens Axboe6c271ce2019-01-10 11:22:30 -07003982 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003983 ret = -EPERM;
3984 if (!capable(CAP_SYS_ADMIN))
3985 goto err;
3986
Jens Axboe917257d2019-04-13 09:28:55 -06003987 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3988 if (!ctx->sq_thread_idle)
3989 ctx->sq_thread_idle = HZ;
3990
Jens Axboe6c271ce2019-01-10 11:22:30 -07003991 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003992 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003993
Jens Axboe917257d2019-04-13 09:28:55 -06003994 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003995 if (cpu >= nr_cpu_ids)
3996 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003997 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003998 goto err;
3999
Jens Axboe6c271ce2019-01-10 11:22:30 -07004000 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
4001 ctx, cpu,
4002 "io_uring-sq");
4003 } else {
4004 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4005 "io_uring-sq");
4006 }
4007 if (IS_ERR(ctx->sqo_thread)) {
4008 ret = PTR_ERR(ctx->sqo_thread);
4009 ctx->sqo_thread = NULL;
4010 goto err;
4011 }
4012 wake_up_process(ctx->sqo_thread);
4013 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4014 /* Can't have SQ_AFF without SQPOLL */
4015 ret = -EINVAL;
4016 goto err;
4017 }
4018
Jens Axboe561fb042019-10-24 07:25:42 -06004019 /* Do QD, or 4 * CPUS, whatever is smallest */
4020 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe7d723062019-11-12 22:31:31 -07004021 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user,
4022 io_get_work, io_put_work);
Jens Axboe975c99a52019-10-30 08:42:56 -06004023 if (IS_ERR(ctx->io_wq)) {
4024 ret = PTR_ERR(ctx->io_wq);
4025 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004026 goto err;
4027 }
4028
4029 return 0;
4030err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004031 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004032 mmdrop(ctx->sqo_mm);
4033 ctx->sqo_mm = NULL;
4034 return ret;
4035}
4036
4037static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4038{
4039 atomic_long_sub(nr_pages, &user->locked_vm);
4040}
4041
4042static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4043{
4044 unsigned long page_limit, cur_pages, new_pages;
4045
4046 /* Don't allow more pages than we can safely lock */
4047 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4048
4049 do {
4050 cur_pages = atomic_long_read(&user->locked_vm);
4051 new_pages = cur_pages + nr_pages;
4052 if (new_pages > page_limit)
4053 return -ENOMEM;
4054 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4055 new_pages) != cur_pages);
4056
4057 return 0;
4058}
4059
4060static void io_mem_free(void *ptr)
4061{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004062 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004063
Mark Rutland52e04ef2019-04-30 17:30:21 +01004064 if (!ptr)
4065 return;
4066
4067 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004068 if (put_page_testzero(page))
4069 free_compound_page(page);
4070}
4071
4072static void *io_mem_alloc(size_t size)
4073{
4074 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4075 __GFP_NORETRY;
4076
4077 return (void *) __get_free_pages(gfp_flags, get_order(size));
4078}
4079
Hristo Venev75b28af2019-08-26 17:23:46 +00004080static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4081 size_t *sq_offset)
4082{
4083 struct io_rings *rings;
4084 size_t off, sq_array_size;
4085
4086 off = struct_size(rings, cqes, cq_entries);
4087 if (off == SIZE_MAX)
4088 return SIZE_MAX;
4089
4090#ifdef CONFIG_SMP
4091 off = ALIGN(off, SMP_CACHE_BYTES);
4092 if (off == 0)
4093 return SIZE_MAX;
4094#endif
4095
4096 sq_array_size = array_size(sizeof(u32), sq_entries);
4097 if (sq_array_size == SIZE_MAX)
4098 return SIZE_MAX;
4099
4100 if (check_add_overflow(off, sq_array_size, &off))
4101 return SIZE_MAX;
4102
4103 if (sq_offset)
4104 *sq_offset = off;
4105
4106 return off;
4107}
4108
Jens Axboe2b188cc2019-01-07 10:46:33 -07004109static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4110{
Hristo Venev75b28af2019-08-26 17:23:46 +00004111 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004112
Hristo Venev75b28af2019-08-26 17:23:46 +00004113 pages = (size_t)1 << get_order(
4114 rings_size(sq_entries, cq_entries, NULL));
4115 pages += (size_t)1 << get_order(
4116 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004117
Hristo Venev75b28af2019-08-26 17:23:46 +00004118 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004119}
4120
Jens Axboeedafcce2019-01-09 09:16:05 -07004121static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4122{
4123 int i, j;
4124
4125 if (!ctx->user_bufs)
4126 return -ENXIO;
4127
4128 for (i = 0; i < ctx->nr_user_bufs; i++) {
4129 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4130
4131 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004132 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004133
4134 if (ctx->account_mem)
4135 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004136 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004137 imu->nr_bvecs = 0;
4138 }
4139
4140 kfree(ctx->user_bufs);
4141 ctx->user_bufs = NULL;
4142 ctx->nr_user_bufs = 0;
4143 return 0;
4144}
4145
4146static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4147 void __user *arg, unsigned index)
4148{
4149 struct iovec __user *src;
4150
4151#ifdef CONFIG_COMPAT
4152 if (ctx->compat) {
4153 struct compat_iovec __user *ciovs;
4154 struct compat_iovec ciov;
4155
4156 ciovs = (struct compat_iovec __user *) arg;
4157 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4158 return -EFAULT;
4159
4160 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4161 dst->iov_len = ciov.iov_len;
4162 return 0;
4163 }
4164#endif
4165 src = (struct iovec __user *) arg;
4166 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4167 return -EFAULT;
4168 return 0;
4169}
4170
4171static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4172 unsigned nr_args)
4173{
4174 struct vm_area_struct **vmas = NULL;
4175 struct page **pages = NULL;
4176 int i, j, got_pages = 0;
4177 int ret = -EINVAL;
4178
4179 if (ctx->user_bufs)
4180 return -EBUSY;
4181 if (!nr_args || nr_args > UIO_MAXIOV)
4182 return -EINVAL;
4183
4184 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4185 GFP_KERNEL);
4186 if (!ctx->user_bufs)
4187 return -ENOMEM;
4188
4189 for (i = 0; i < nr_args; i++) {
4190 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4191 unsigned long off, start, end, ubuf;
4192 int pret, nr_pages;
4193 struct iovec iov;
4194 size_t size;
4195
4196 ret = io_copy_iov(ctx, &iov, arg, i);
4197 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004198 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004199
4200 /*
4201 * Don't impose further limits on the size and buffer
4202 * constraints here, we'll -EINVAL later when IO is
4203 * submitted if they are wrong.
4204 */
4205 ret = -EFAULT;
4206 if (!iov.iov_base || !iov.iov_len)
4207 goto err;
4208
4209 /* arbitrary limit, but we need something */
4210 if (iov.iov_len > SZ_1G)
4211 goto err;
4212
4213 ubuf = (unsigned long) iov.iov_base;
4214 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4215 start = ubuf >> PAGE_SHIFT;
4216 nr_pages = end - start;
4217
4218 if (ctx->account_mem) {
4219 ret = io_account_mem(ctx->user, nr_pages);
4220 if (ret)
4221 goto err;
4222 }
4223
4224 ret = 0;
4225 if (!pages || nr_pages > got_pages) {
4226 kfree(vmas);
4227 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004228 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004229 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004230 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004231 sizeof(struct vm_area_struct *),
4232 GFP_KERNEL);
4233 if (!pages || !vmas) {
4234 ret = -ENOMEM;
4235 if (ctx->account_mem)
4236 io_unaccount_mem(ctx->user, nr_pages);
4237 goto err;
4238 }
4239 got_pages = nr_pages;
4240 }
4241
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004242 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004243 GFP_KERNEL);
4244 ret = -ENOMEM;
4245 if (!imu->bvec) {
4246 if (ctx->account_mem)
4247 io_unaccount_mem(ctx->user, nr_pages);
4248 goto err;
4249 }
4250
4251 ret = 0;
4252 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004253 pret = get_user_pages(ubuf, nr_pages,
4254 FOLL_WRITE | FOLL_LONGTERM,
4255 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004256 if (pret == nr_pages) {
4257 /* don't support file backed memory */
4258 for (j = 0; j < nr_pages; j++) {
4259 struct vm_area_struct *vma = vmas[j];
4260
4261 if (vma->vm_file &&
4262 !is_file_hugepages(vma->vm_file)) {
4263 ret = -EOPNOTSUPP;
4264 break;
4265 }
4266 }
4267 } else {
4268 ret = pret < 0 ? pret : -EFAULT;
4269 }
4270 up_read(&current->mm->mmap_sem);
4271 if (ret) {
4272 /*
4273 * if we did partial map, or found file backed vmas,
4274 * release any pages we did get
4275 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004276 if (pret > 0)
4277 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004278 if (ctx->account_mem)
4279 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004280 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004281 goto err;
4282 }
4283
4284 off = ubuf & ~PAGE_MASK;
4285 size = iov.iov_len;
4286 for (j = 0; j < nr_pages; j++) {
4287 size_t vec_len;
4288
4289 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4290 imu->bvec[j].bv_page = pages[j];
4291 imu->bvec[j].bv_len = vec_len;
4292 imu->bvec[j].bv_offset = off;
4293 off = 0;
4294 size -= vec_len;
4295 }
4296 /* store original address for later verification */
4297 imu->ubuf = ubuf;
4298 imu->len = iov.iov_len;
4299 imu->nr_bvecs = nr_pages;
4300
4301 ctx->nr_user_bufs++;
4302 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004303 kvfree(pages);
4304 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004305 return 0;
4306err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004307 kvfree(pages);
4308 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004309 io_sqe_buffer_unregister(ctx);
4310 return ret;
4311}
4312
Jens Axboe9b402842019-04-11 11:45:41 -06004313static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4314{
4315 __s32 __user *fds = arg;
4316 int fd;
4317
4318 if (ctx->cq_ev_fd)
4319 return -EBUSY;
4320
4321 if (copy_from_user(&fd, fds, sizeof(*fds)))
4322 return -EFAULT;
4323
4324 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4325 if (IS_ERR(ctx->cq_ev_fd)) {
4326 int ret = PTR_ERR(ctx->cq_ev_fd);
4327 ctx->cq_ev_fd = NULL;
4328 return ret;
4329 }
4330
4331 return 0;
4332}
4333
4334static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4335{
4336 if (ctx->cq_ev_fd) {
4337 eventfd_ctx_put(ctx->cq_ev_fd);
4338 ctx->cq_ev_fd = NULL;
4339 return 0;
4340 }
4341
4342 return -ENXIO;
4343}
4344
Jens Axboe2b188cc2019-01-07 10:46:33 -07004345static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4346{
Jens Axboe6b063142019-01-10 22:13:58 -07004347 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004348 if (ctx->sqo_mm)
4349 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004350
4351 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004352 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004353 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004354 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004355
Jens Axboe2b188cc2019-01-07 10:46:33 -07004356#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004357 if (ctx->ring_sock) {
4358 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004359 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004360 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004361#endif
4362
Hristo Venev75b28af2019-08-26 17:23:46 +00004363 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004364 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004365
4366 percpu_ref_exit(&ctx->refs);
4367 if (ctx->account_mem)
4368 io_unaccount_mem(ctx->user,
4369 ring_pages(ctx->sq_entries, ctx->cq_entries));
4370 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004371 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004372 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004373 kfree(ctx);
4374}
4375
4376static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4377{
4378 struct io_ring_ctx *ctx = file->private_data;
4379 __poll_t mask = 0;
4380
4381 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004382 /*
4383 * synchronizes with barrier from wq_has_sleeper call in
4384 * io_commit_cqring
4385 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004386 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004387 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4388 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004389 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004390 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004391 mask |= EPOLLIN | EPOLLRDNORM;
4392
4393 return mask;
4394}
4395
4396static int io_uring_fasync(int fd, struct file *file, int on)
4397{
4398 struct io_ring_ctx *ctx = file->private_data;
4399
4400 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4401}
4402
4403static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4404{
4405 mutex_lock(&ctx->uring_lock);
4406 percpu_ref_kill(&ctx->refs);
4407 mutex_unlock(&ctx->uring_lock);
4408
Jens Axboe5262f562019-09-17 12:26:57 -06004409 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004410 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004411
4412 if (ctx->io_wq)
4413 io_wq_cancel_all(ctx->io_wq);
4414
Jens Axboedef596e2019-01-09 08:59:42 -07004415 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004416 /* if we failed setting up the ctx, we might not have any rings */
4417 if (ctx->rings)
4418 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004419 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004420 io_ring_ctx_free(ctx);
4421}
4422
4423static int io_uring_release(struct inode *inode, struct file *file)
4424{
4425 struct io_ring_ctx *ctx = file->private_data;
4426
4427 file->private_data = NULL;
4428 io_ring_ctx_wait_and_kill(ctx);
4429 return 0;
4430}
4431
Jens Axboefcb323c2019-10-24 12:39:47 -06004432static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4433 struct files_struct *files)
4434{
4435 struct io_kiocb *req;
4436 DEFINE_WAIT(wait);
4437
4438 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004439 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004440
4441 spin_lock_irq(&ctx->inflight_lock);
4442 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004443 if (req->work.files != files)
4444 continue;
4445 /* req is being completed, ignore */
4446 if (!refcount_inc_not_zero(&req->refs))
4447 continue;
4448 cancel_req = req;
4449 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004450 }
Jens Axboe768134d2019-11-10 20:30:53 -07004451 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004452 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004453 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004454 spin_unlock_irq(&ctx->inflight_lock);
4455
Jens Axboe768134d2019-11-10 20:30:53 -07004456 /* We need to keep going until we don't find a matching req */
4457 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004458 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004459
4460 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4461 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004462 schedule();
4463 }
Jens Axboe768134d2019-11-10 20:30:53 -07004464 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004465}
4466
4467static int io_uring_flush(struct file *file, void *data)
4468{
4469 struct io_ring_ctx *ctx = file->private_data;
4470
4471 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004472 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4473 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004474 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004475 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004476 return 0;
4477}
4478
Jens Axboe2b188cc2019-01-07 10:46:33 -07004479static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4480{
4481 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4482 unsigned long sz = vma->vm_end - vma->vm_start;
4483 struct io_ring_ctx *ctx = file->private_data;
4484 unsigned long pfn;
4485 struct page *page;
4486 void *ptr;
4487
4488 switch (offset) {
4489 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004490 case IORING_OFF_CQ_RING:
4491 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004492 break;
4493 case IORING_OFF_SQES:
4494 ptr = ctx->sq_sqes;
4495 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004496 default:
4497 return -EINVAL;
4498 }
4499
4500 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004501 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004502 return -EINVAL;
4503
4504 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4505 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4506}
4507
4508SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4509 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4510 size_t, sigsz)
4511{
4512 struct io_ring_ctx *ctx;
4513 long ret = -EBADF;
4514 int submitted = 0;
4515 struct fd f;
4516
Jens Axboe6c271ce2019-01-10 11:22:30 -07004517 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004518 return -EINVAL;
4519
4520 f = fdget(fd);
4521 if (!f.file)
4522 return -EBADF;
4523
4524 ret = -EOPNOTSUPP;
4525 if (f.file->f_op != &io_uring_fops)
4526 goto out_fput;
4527
4528 ret = -ENXIO;
4529 ctx = f.file->private_data;
4530 if (!percpu_ref_tryget(&ctx->refs))
4531 goto out_fput;
4532
Jens Axboe6c271ce2019-01-10 11:22:30 -07004533 /*
4534 * For SQ polling, the thread will do all submissions and completions.
4535 * Just return the requested submit count, and wake the thread if
4536 * we were asked to.
4537 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004538 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004539 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004540 if (!list_empty_careful(&ctx->cq_overflow_list))
4541 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004542 if (flags & IORING_ENTER_SQ_WAKEUP)
4543 wake_up(&ctx->sqo_wait);
4544 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004545 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004546 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004547
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004548 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004549 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004550 /* already have mm, so io_submit_sqes() won't try to grab it */
4551 cur_mm = ctx->sqo_mm;
4552 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4553 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004554 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004555 }
4556 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004557 unsigned nr_events = 0;
4558
Jens Axboe2b188cc2019-01-07 10:46:33 -07004559 min_complete = min(min_complete, ctx->cq_entries);
4560
Jens Axboedef596e2019-01-09 08:59:42 -07004561 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004562 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004563 } else {
4564 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4565 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004566 }
4567
Pavel Begunkov6805b322019-10-08 02:18:42 +03004568 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004569out_fput:
4570 fdput(f);
4571 return submitted ? submitted : ret;
4572}
4573
4574static const struct file_operations io_uring_fops = {
4575 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004576 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004577 .mmap = io_uring_mmap,
4578 .poll = io_uring_poll,
4579 .fasync = io_uring_fasync,
4580};
4581
4582static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4583 struct io_uring_params *p)
4584{
Hristo Venev75b28af2019-08-26 17:23:46 +00004585 struct io_rings *rings;
4586 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004587
Hristo Venev75b28af2019-08-26 17:23:46 +00004588 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4589 if (size == SIZE_MAX)
4590 return -EOVERFLOW;
4591
4592 rings = io_mem_alloc(size);
4593 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004594 return -ENOMEM;
4595
Hristo Venev75b28af2019-08-26 17:23:46 +00004596 ctx->rings = rings;
4597 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4598 rings->sq_ring_mask = p->sq_entries - 1;
4599 rings->cq_ring_mask = p->cq_entries - 1;
4600 rings->sq_ring_entries = p->sq_entries;
4601 rings->cq_ring_entries = p->cq_entries;
4602 ctx->sq_mask = rings->sq_ring_mask;
4603 ctx->cq_mask = rings->cq_ring_mask;
4604 ctx->sq_entries = rings->sq_ring_entries;
4605 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004606
4607 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004608 if (size == SIZE_MAX) {
4609 io_mem_free(ctx->rings);
4610 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004611 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004612 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004613
4614 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004615 if (!ctx->sq_sqes) {
4616 io_mem_free(ctx->rings);
4617 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004618 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004619 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004620
Jens Axboe2b188cc2019-01-07 10:46:33 -07004621 return 0;
4622}
4623
4624/*
4625 * Allocate an anonymous fd, this is what constitutes the application
4626 * visible backing of an io_uring instance. The application mmaps this
4627 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4628 * we have to tie this fd to a socket for file garbage collection purposes.
4629 */
4630static int io_uring_get_fd(struct io_ring_ctx *ctx)
4631{
4632 struct file *file;
4633 int ret;
4634
4635#if defined(CONFIG_UNIX)
4636 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4637 &ctx->ring_sock);
4638 if (ret)
4639 return ret;
4640#endif
4641
4642 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4643 if (ret < 0)
4644 goto err;
4645
4646 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4647 O_RDWR | O_CLOEXEC);
4648 if (IS_ERR(file)) {
4649 put_unused_fd(ret);
4650 ret = PTR_ERR(file);
4651 goto err;
4652 }
4653
4654#if defined(CONFIG_UNIX)
4655 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004656 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004657#endif
4658 fd_install(ret, file);
4659 return ret;
4660err:
4661#if defined(CONFIG_UNIX)
4662 sock_release(ctx->ring_sock);
4663 ctx->ring_sock = NULL;
4664#endif
4665 return ret;
4666}
4667
4668static int io_uring_create(unsigned entries, struct io_uring_params *p)
4669{
4670 struct user_struct *user = NULL;
4671 struct io_ring_ctx *ctx;
4672 bool account_mem;
4673 int ret;
4674
4675 if (!entries || entries > IORING_MAX_ENTRIES)
4676 return -EINVAL;
4677
4678 /*
4679 * Use twice as many entries for the CQ ring. It's possible for the
4680 * application to drive a higher depth than the size of the SQ ring,
4681 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004682 * some flexibility in overcommitting a bit. If the application has
4683 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4684 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004685 */
4686 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004687 if (p->flags & IORING_SETUP_CQSIZE) {
4688 /*
4689 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4690 * to a power-of-two, if it isn't already. We do NOT impose
4691 * any cq vs sq ring sizing.
4692 */
4693 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4694 return -EINVAL;
4695 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4696 } else {
4697 p->cq_entries = 2 * p->sq_entries;
4698 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004699
4700 user = get_uid(current_user());
4701 account_mem = !capable(CAP_IPC_LOCK);
4702
4703 if (account_mem) {
4704 ret = io_account_mem(user,
4705 ring_pages(p->sq_entries, p->cq_entries));
4706 if (ret) {
4707 free_uid(user);
4708 return ret;
4709 }
4710 }
4711
4712 ctx = io_ring_ctx_alloc(p);
4713 if (!ctx) {
4714 if (account_mem)
4715 io_unaccount_mem(user, ring_pages(p->sq_entries,
4716 p->cq_entries));
4717 free_uid(user);
4718 return -ENOMEM;
4719 }
4720 ctx->compat = in_compat_syscall();
4721 ctx->account_mem = account_mem;
4722 ctx->user = user;
4723
4724 ret = io_allocate_scq_urings(ctx, p);
4725 if (ret)
4726 goto err;
4727
Jens Axboe6c271ce2019-01-10 11:22:30 -07004728 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004729 if (ret)
4730 goto err;
4731
Jens Axboe2b188cc2019-01-07 10:46:33 -07004732 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004733 p->sq_off.head = offsetof(struct io_rings, sq.head);
4734 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4735 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4736 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4737 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4738 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4739 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004740
4741 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004742 p->cq_off.head = offsetof(struct io_rings, cq.head);
4743 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4744 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4745 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4746 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4747 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004748
Jens Axboe044c1ab2019-10-28 09:15:33 -06004749 /*
4750 * Install ring fd as the very last thing, so we don't risk someone
4751 * having closed it before we finish setup
4752 */
4753 ret = io_uring_get_fd(ctx);
4754 if (ret < 0)
4755 goto err;
4756
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004757 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004758 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004759 return ret;
4760err:
4761 io_ring_ctx_wait_and_kill(ctx);
4762 return ret;
4763}
4764
4765/*
4766 * Sets up an aio uring context, and returns the fd. Applications asks for a
4767 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4768 * params structure passed in.
4769 */
4770static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4771{
4772 struct io_uring_params p;
4773 long ret;
4774 int i;
4775
4776 if (copy_from_user(&p, params, sizeof(p)))
4777 return -EFAULT;
4778 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4779 if (p.resv[i])
4780 return -EINVAL;
4781 }
4782
Jens Axboe6c271ce2019-01-10 11:22:30 -07004783 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004784 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004785 return -EINVAL;
4786
4787 ret = io_uring_create(entries, &p);
4788 if (ret < 0)
4789 return ret;
4790
4791 if (copy_to_user(params, &p, sizeof(p)))
4792 return -EFAULT;
4793
4794 return ret;
4795}
4796
4797SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4798 struct io_uring_params __user *, params)
4799{
4800 return io_uring_setup(entries, params);
4801}
4802
Jens Axboeedafcce2019-01-09 09:16:05 -07004803static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4804 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004805 __releases(ctx->uring_lock)
4806 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004807{
4808 int ret;
4809
Jens Axboe35fa71a2019-04-22 10:23:23 -06004810 /*
4811 * We're inside the ring mutex, if the ref is already dying, then
4812 * someone else killed the ctx or is already going through
4813 * io_uring_register().
4814 */
4815 if (percpu_ref_is_dying(&ctx->refs))
4816 return -ENXIO;
4817
Jens Axboeedafcce2019-01-09 09:16:05 -07004818 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004819
4820 /*
4821 * Drop uring mutex before waiting for references to exit. If another
4822 * thread is currently inside io_uring_enter() it might need to grab
4823 * the uring_lock to make progress. If we hold it here across the drain
4824 * wait, then we can deadlock. It's safe to drop the mutex here, since
4825 * no new references will come in after we've killed the percpu ref.
4826 */
4827 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004828 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004829 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004830
4831 switch (opcode) {
4832 case IORING_REGISTER_BUFFERS:
4833 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4834 break;
4835 case IORING_UNREGISTER_BUFFERS:
4836 ret = -EINVAL;
4837 if (arg || nr_args)
4838 break;
4839 ret = io_sqe_buffer_unregister(ctx);
4840 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004841 case IORING_REGISTER_FILES:
4842 ret = io_sqe_files_register(ctx, arg, nr_args);
4843 break;
4844 case IORING_UNREGISTER_FILES:
4845 ret = -EINVAL;
4846 if (arg || nr_args)
4847 break;
4848 ret = io_sqe_files_unregister(ctx);
4849 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004850 case IORING_REGISTER_FILES_UPDATE:
4851 ret = io_sqe_files_update(ctx, arg, nr_args);
4852 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004853 case IORING_REGISTER_EVENTFD:
4854 ret = -EINVAL;
4855 if (nr_args != 1)
4856 break;
4857 ret = io_eventfd_register(ctx, arg);
4858 break;
4859 case IORING_UNREGISTER_EVENTFD:
4860 ret = -EINVAL;
4861 if (arg || nr_args)
4862 break;
4863 ret = io_eventfd_unregister(ctx);
4864 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004865 default:
4866 ret = -EINVAL;
4867 break;
4868 }
4869
4870 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004871 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004872 percpu_ref_reinit(&ctx->refs);
4873 return ret;
4874}
4875
4876SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4877 void __user *, arg, unsigned int, nr_args)
4878{
4879 struct io_ring_ctx *ctx;
4880 long ret = -EBADF;
4881 struct fd f;
4882
4883 f = fdget(fd);
4884 if (!f.file)
4885 return -EBADF;
4886
4887 ret = -EOPNOTSUPP;
4888 if (f.file->f_op != &io_uring_fops)
4889 goto out_fput;
4890
4891 ctx = f.file->private_data;
4892
4893 mutex_lock(&ctx->uring_lock);
4894 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4895 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004896 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4897 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004898out_fput:
4899 fdput(f);
4900 return ret;
4901}
4902
Jens Axboe2b188cc2019-01-07 10:46:33 -07004903static int __init io_uring_init(void)
4904{
4905 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4906 return 0;
4907};
4908__initcall(io_uring_init);