blob: 55bc890f863d5e70404edf6806efdcab30664b9e [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 */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200343#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
344#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600345#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700346#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800347#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800348#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600349#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600350#define REQ_F_ISREG 2048 /* regular file */
351#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700352#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800353#define REQ_F_INFLIGHT 16384 /* on inflight list */
354#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe94ae5e72019-11-14 19:39:52 -0700355#define REQ_F_FREE_SQE 65536 /* free sqe if not async queued */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700356 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600357 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600358 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700359
Jens Axboefcb323c2019-10-24 12:39:47 -0600360 struct list_head inflight_entry;
361
Jens Axboe561fb042019-10-24 07:25:42 -0600362 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700363};
364
365#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700366#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700367
Jens Axboe9a56a232019-01-09 09:06:50 -0700368struct io_submit_state {
369 struct blk_plug plug;
370
371 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700372 * io_kiocb alloc cache
373 */
374 void *reqs[IO_IOPOLL_BATCH];
375 unsigned int free_reqs;
376 unsigned int cur_req;
377
378 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700379 * File reference cache
380 */
381 struct file *file;
382 unsigned int fd;
383 unsigned int has_refs;
384 unsigned int used_refs;
385 unsigned int ios_left;
386};
387
Jens Axboe561fb042019-10-24 07:25:42 -0600388static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700389static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800390static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800391static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700392static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700393static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700394static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
395static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600396
Jens Axboe2b188cc2019-01-07 10:46:33 -0700397static struct kmem_cache *req_cachep;
398
399static const struct file_operations io_uring_fops;
400
401struct sock *io_uring_get_socket(struct file *file)
402{
403#if defined(CONFIG_UNIX)
404 if (file->f_op == &io_uring_fops) {
405 struct io_ring_ctx *ctx = file->private_data;
406
407 return ctx->ring_sock->sk;
408 }
409#endif
410 return NULL;
411}
412EXPORT_SYMBOL(io_uring_get_socket);
413
414static void io_ring_ctx_ref_free(struct percpu_ref *ref)
415{
416 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
417
Jens Axboe206aefd2019-11-07 18:27:42 -0700418 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700419}
420
421static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
422{
423 struct io_ring_ctx *ctx;
424
425 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
426 if (!ctx)
427 return NULL;
428
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700429 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
430 if (!ctx->fallback_req)
431 goto err;
432
Jens Axboe206aefd2019-11-07 18:27:42 -0700433 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
434 if (!ctx->completions)
435 goto err;
436
Roman Gushchin21482892019-05-07 10:01:48 -0700437 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700438 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
439 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700440
441 ctx->flags = p->flags;
442 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700443 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700444 init_completion(&ctx->completions[0]);
445 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700446 mutex_init(&ctx->uring_lock);
447 init_waitqueue_head(&ctx->wait);
448 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700449 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboeeac406c2019-11-14 12:09:58 -0700450 ctx->cancel_tree = RB_ROOT;
Jens Axboede0617e2019-04-06 21:51:27 -0600451 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600452 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600453 init_waitqueue_head(&ctx->inflight_wait);
454 spin_lock_init(&ctx->inflight_lock);
455 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700456 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700457err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700458 if (ctx->fallback_req)
459 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700460 kfree(ctx->completions);
461 kfree(ctx);
462 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700463}
464
Bob Liu9d858b22019-11-13 18:06:25 +0800465static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600466{
Jackie Liua197f662019-11-08 08:09:12 -0700467 struct io_ring_ctx *ctx = req->ctx;
468
Jens Axboe498ccd92019-10-25 10:04:25 -0600469 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
470 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600471}
472
Bob Liu9d858b22019-11-13 18:06:25 +0800473static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600474{
Bob Liu9d858b22019-11-13 18:06:25 +0800475 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
476 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600477
Bob Liu9d858b22019-11-13 18:06:25 +0800478 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600479}
480
481static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600482{
483 struct io_kiocb *req;
484
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600485 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800486 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600487 list_del_init(&req->list);
488 return req;
489 }
490
491 return NULL;
492}
493
Jens Axboe5262f562019-09-17 12:26:57 -0600494static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
495{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600496 struct io_kiocb *req;
497
498 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700499 if (req) {
500 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
501 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800502 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700503 list_del_init(&req->list);
504 return req;
505 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600506 }
507
508 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600509}
510
Jens Axboede0617e2019-04-06 21:51:27 -0600511static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700512{
Hristo Venev75b28af2019-08-26 17:23:46 +0000513 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700514
Hristo Venev75b28af2019-08-26 17:23:46 +0000515 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700516 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000517 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700518
Jens Axboe2b188cc2019-01-07 10:46:33 -0700519 if (wq_has_sleeper(&ctx->cq_wait)) {
520 wake_up_interruptible(&ctx->cq_wait);
521 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
522 }
523 }
524}
525
Jens Axboe561fb042019-10-24 07:25:42 -0600526static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600527{
Jens Axboe561fb042019-10-24 07:25:42 -0600528 u8 opcode = READ_ONCE(sqe->opcode);
529
530 return !(opcode == IORING_OP_READ_FIXED ||
531 opcode == IORING_OP_WRITE_FIXED);
532}
533
Jens Axboe94ae5e72019-11-14 19:39:52 -0700534static inline bool io_prep_async_work(struct io_kiocb *req,
535 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600536{
537 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600538
Jens Axboe6cc47d12019-09-18 11:18:23 -0600539 if (req->submit.sqe) {
540 switch (req->submit.sqe->opcode) {
541 case IORING_OP_WRITEV:
542 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600543 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700544 /* fall-through */
545 case IORING_OP_READV:
546 case IORING_OP_READ_FIXED:
547 case IORING_OP_SENDMSG:
548 case IORING_OP_RECVMSG:
549 case IORING_OP_ACCEPT:
550 case IORING_OP_POLL_ADD:
551 /*
552 * We know REQ_F_ISREG is not set on some of these
553 * opcodes, but this enables us to keep the check in
554 * just one place.
555 */
556 if (!(req->flags & REQ_F_ISREG))
557 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600558 break;
559 }
Jens Axboe561fb042019-10-24 07:25:42 -0600560 if (io_sqe_needs_user(req->submit.sqe))
561 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600562 }
563
Jens Axboe94ae5e72019-11-14 19:39:52 -0700564 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600565 return do_hashed;
566}
567
Jackie Liua197f662019-11-08 08:09:12 -0700568static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600569{
Jackie Liua197f662019-11-08 08:09:12 -0700570 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700571 struct io_kiocb *link;
572 bool do_hashed;
573
574 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600575
576 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
577 req->flags);
578 if (!do_hashed) {
579 io_wq_enqueue(ctx->io_wq, &req->work);
580 } else {
581 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
582 file_inode(req->file));
583 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700584
585 if (link)
586 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600587}
588
Jens Axboe5262f562019-09-17 12:26:57 -0600589static void io_kill_timeout(struct io_kiocb *req)
590{
591 int ret;
592
Jens Axboead8a48a2019-11-15 08:49:11 -0700593 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600594 if (ret != -1) {
595 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600596 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700597 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800598 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600599 }
600}
601
602static void io_kill_timeouts(struct io_ring_ctx *ctx)
603{
604 struct io_kiocb *req, *tmp;
605
606 spin_lock_irq(&ctx->completion_lock);
607 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
608 io_kill_timeout(req);
609 spin_unlock_irq(&ctx->completion_lock);
610}
611
Jens Axboede0617e2019-04-06 21:51:27 -0600612static void io_commit_cqring(struct io_ring_ctx *ctx)
613{
614 struct io_kiocb *req;
615
Jens Axboe5262f562019-09-17 12:26:57 -0600616 while ((req = io_get_timeout_req(ctx)) != NULL)
617 io_kill_timeout(req);
618
Jens Axboede0617e2019-04-06 21:51:27 -0600619 __io_commit_cqring(ctx);
620
621 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800622 if (req->flags & REQ_F_SHADOW_DRAIN) {
623 /* Just for drain, free it. */
624 __io_free_req(req);
625 continue;
626 }
Jens Axboede0617e2019-04-06 21:51:27 -0600627 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700628 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600629 }
630}
631
Jens Axboe2b188cc2019-01-07 10:46:33 -0700632static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
633{
Hristo Venev75b28af2019-08-26 17:23:46 +0000634 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700635 unsigned tail;
636
637 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200638 /*
639 * writes to the cq entry need to come after reading head; the
640 * control dependency is enough as we're using WRITE_ONCE to
641 * fill the cq entry
642 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000643 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700644 return NULL;
645
646 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000647 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700648}
649
Jens Axboe8c838782019-03-12 15:48:16 -0600650static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
651{
652 if (waitqueue_active(&ctx->wait))
653 wake_up(&ctx->wait);
654 if (waitqueue_active(&ctx->sqo_wait))
655 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600656 if (ctx->cq_ev_fd)
657 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600658}
659
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700660static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700661{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700662 struct io_rings *rings = ctx->rings;
663 struct io_uring_cqe *cqe;
664 struct io_kiocb *req;
665 unsigned long flags;
666 LIST_HEAD(list);
667
668 if (!force) {
669 if (list_empty_careful(&ctx->cq_overflow_list))
670 return;
671 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
672 rings->cq_ring_entries))
673 return;
674 }
675
676 spin_lock_irqsave(&ctx->completion_lock, flags);
677
678 /* if force is set, the ring is going away. always drop after that */
679 if (force)
680 ctx->cq_overflow_flushed = true;
681
682 while (!list_empty(&ctx->cq_overflow_list)) {
683 cqe = io_get_cqring(ctx);
684 if (!cqe && !force)
685 break;
686
687 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
688 list);
689 list_move(&req->list, &list);
690 if (cqe) {
691 WRITE_ONCE(cqe->user_data, req->user_data);
692 WRITE_ONCE(cqe->res, req->result);
693 WRITE_ONCE(cqe->flags, 0);
694 } else {
695 WRITE_ONCE(ctx->rings->cq_overflow,
696 atomic_inc_return(&ctx->cached_cq_overflow));
697 }
698 }
699
700 io_commit_cqring(ctx);
701 spin_unlock_irqrestore(&ctx->completion_lock, flags);
702 io_cqring_ev_posted(ctx);
703
704 while (!list_empty(&list)) {
705 req = list_first_entry(&list, struct io_kiocb, list);
706 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800707 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700708 }
709}
710
Jens Axboe78e19bb2019-11-06 15:21:34 -0700711static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700712{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700713 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700714 struct io_uring_cqe *cqe;
715
Jens Axboe78e19bb2019-11-06 15:21:34 -0700716 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700717
Jens Axboe2b188cc2019-01-07 10:46:33 -0700718 /*
719 * If we can't get a cq entry, userspace overflowed the
720 * submission (by quite a lot). Increment the overflow count in
721 * the ring.
722 */
723 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700724 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700725 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700726 WRITE_ONCE(cqe->res, res);
727 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700728 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700729 WRITE_ONCE(ctx->rings->cq_overflow,
730 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700731 } else {
732 refcount_inc(&req->refs);
733 req->result = res;
734 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700735 }
736}
737
Jens Axboe78e19bb2019-11-06 15:21:34 -0700738static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700739{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700740 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700741 unsigned long flags;
742
743 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700744 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700745 io_commit_cqring(ctx);
746 spin_unlock_irqrestore(&ctx->completion_lock, flags);
747
Jens Axboe8c838782019-03-12 15:48:16 -0600748 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700749}
750
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700751static inline bool io_is_fallback_req(struct io_kiocb *req)
752{
753 return req == (struct io_kiocb *)
754 ((unsigned long) req->ctx->fallback_req & ~1UL);
755}
756
757static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
758{
759 struct io_kiocb *req;
760
761 req = ctx->fallback_req;
762 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
763 return req;
764
765 return NULL;
766}
767
Jens Axboe2579f912019-01-09 09:10:43 -0700768static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
769 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700770{
Jens Axboefd6fab22019-03-14 16:30:06 -0600771 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700772 struct io_kiocb *req;
773
774 if (!percpu_ref_tryget(&ctx->refs))
775 return NULL;
776
Jens Axboe2579f912019-01-09 09:10:43 -0700777 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600778 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700779 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700780 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700781 } else if (!state->free_reqs) {
782 size_t sz;
783 int ret;
784
785 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600786 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
787
788 /*
789 * Bulk alloc is all-or-nothing. If we fail to get a batch,
790 * retry single alloc to be on the safe side.
791 */
792 if (unlikely(ret <= 0)) {
793 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
794 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700795 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600796 ret = 1;
797 }
Jens Axboe2579f912019-01-09 09:10:43 -0700798 state->free_reqs = ret - 1;
799 state->cur_req = 1;
800 req = state->reqs[0];
801 } else {
802 req = state->reqs[state->cur_req];
803 state->free_reqs--;
804 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700805 }
806
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700807got_it:
Jens Axboe60c112b2019-06-21 10:20:18 -0600808 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700809 req->ctx = ctx;
810 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600811 /* one is dropped after submission, the other at completion */
812 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600813 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600814 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700815 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700816fallback:
817 req = io_get_fallback_req(ctx);
818 if (req)
819 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300820 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700821 return NULL;
822}
823
Jens Axboedef596e2019-01-09 08:59:42 -0700824static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
825{
826 if (*nr) {
827 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300828 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700829 *nr = 0;
830 }
831}
832
Jens Axboe9e645e112019-05-10 16:07:28 -0600833static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700834{
Jens Axboefcb323c2019-10-24 12:39:47 -0600835 struct io_ring_ctx *ctx = req->ctx;
836
Pavel Begunkovbbad27b2019-11-19 23:32:47 +0300837 if (req->flags & REQ_F_FREE_SQE)
838 kfree(req->submit.sqe);
Jens Axboe09bb8392019-03-13 12:39:28 -0600839 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
840 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600841 if (req->flags & REQ_F_INFLIGHT) {
842 unsigned long flags;
843
844 spin_lock_irqsave(&ctx->inflight_lock, flags);
845 list_del(&req->inflight_entry);
846 if (waitqueue_active(&ctx->inflight_wait))
847 wake_up(&ctx->inflight_wait);
848 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
849 }
Jens Axboead8a48a2019-11-15 08:49:11 -0700850 if (req->flags & REQ_F_TIMEOUT)
851 kfree(req->timeout.data);
Jens Axboefcb323c2019-10-24 12:39:47 -0600852 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700853 if (likely(!io_is_fallback_req(req)))
854 kmem_cache_free(req_cachep, req);
855 else
856 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600857}
858
Jackie Liua197f662019-11-08 08:09:12 -0700859static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600860{
Jackie Liua197f662019-11-08 08:09:12 -0700861 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700862 int ret;
863
Jens Axboead8a48a2019-11-15 08:49:11 -0700864 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700865 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700866 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700867 io_commit_cqring(ctx);
868 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800869 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700870 return true;
871 }
872
873 return false;
874}
875
Jens Axboeba816ad2019-09-28 11:36:45 -0600876static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600877{
Jens Axboe2665abf2019-11-05 12:40:47 -0700878 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600879 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700880 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600881
882 /*
883 * The list should never be empty when we are called here. But could
884 * potentially happen if the chain is messed up, check to be on the
885 * safe side.
886 */
887 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700888 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700889 list_del_init(&nxt->list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700890
891 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
892 (nxt->flags & REQ_F_TIMEOUT)) {
893 wake_ev |= io_link_cancel_timeout(nxt);
894 nxt = list_first_entry_or_null(&req->link_list,
895 struct io_kiocb, list);
896 req->flags &= ~REQ_F_LINK_TIMEOUT;
897 continue;
898 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600899 if (!list_empty(&req->link_list)) {
900 INIT_LIST_HEAD(&nxt->link_list);
901 list_splice(&req->link_list, &nxt->link_list);
902 nxt->flags |= REQ_F_LINK;
903 }
904
Jens Axboeba816ad2019-09-28 11:36:45 -0600905 /*
906 * If we're in async work, we can continue processing the chain
907 * in this context instead of having to queue up new async work.
908 */
Jens Axboe94ae5e72019-11-14 19:39:52 -0700909 if (nxt) {
910 if (nxtptr && io_wq_current_is_worker())
911 *nxtptr = nxt;
912 else
913 io_queue_async_work(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700914 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700915 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600916 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700917
918 if (wake_ev)
919 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600920}
921
922/*
923 * Called if REQ_F_LINK is set, and we fail the head request
924 */
925static void io_fail_links(struct io_kiocb *req)
926{
Jens Axboe2665abf2019-11-05 12:40:47 -0700927 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600928 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700929 unsigned long flags;
930
931 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600932
933 while (!list_empty(&req->link_list)) {
934 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700935 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600936
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200937 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700938
939 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
940 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700941 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700942 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700943 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700944 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700945 }
Jens Axboe5d960722019-11-19 15:31:28 -0700946 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600947 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700948
949 io_commit_cqring(ctx);
950 spin_unlock_irqrestore(&ctx->completion_lock, flags);
951 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600952}
953
Jackie Liuc69f8db2019-11-09 11:00:08 +0800954static void io_free_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600955{
Jens Axboe2665abf2019-11-05 12:40:47 -0700956 if (likely(!(req->flags & REQ_F_LINK))) {
957 __io_free_req(req);
958 return;
959 }
960
Jens Axboe9e645e112019-05-10 16:07:28 -0600961 /*
962 * If LINK is set, we have dependent requests in this chain. If we
963 * didn't fail this request, queue the first one up, moving any other
964 * dependencies to the next request. In case of failure, fail the rest
965 * of the chain.
966 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700967 if (req->flags & REQ_F_FAIL_LINK) {
968 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700969 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
970 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700971 struct io_ring_ctx *ctx = req->ctx;
972 unsigned long flags;
973
974 /*
975 * If this is a timeout link, we could be racing with the
976 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700977 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700978 */
979 spin_lock_irqsave(&ctx->completion_lock, flags);
980 io_req_link_next(req, nxt);
981 spin_unlock_irqrestore(&ctx->completion_lock, flags);
982 } else {
983 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600984 }
985
986 __io_free_req(req);
987}
988
Jackie Liuc69f8db2019-11-09 11:00:08 +0800989static void io_free_req(struct io_kiocb *req)
990{
991 io_free_req_find_next(req, NULL);
992}
993
Jens Axboeba816ad2019-09-28 11:36:45 -0600994/*
995 * Drop reference to request, return next in chain (if there is one) if this
996 * was the last reference to this request.
997 */
Jackie Liuec9c02a2019-11-08 23:50:36 +0800998static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -0600999{
Jens Axboeba816ad2019-09-28 11:36:45 -06001000 struct io_kiocb *nxt = NULL;
1001
Jens Axboee65ef562019-03-12 10:16:44 -06001002 if (refcount_dec_and_test(&req->refs))
Jackie Liuc69f8db2019-11-09 11:00:08 +08001003 io_free_req_find_next(req, &nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -06001004
Jens Axboeba816ad2019-09-28 11:36:45 -06001005 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -06001006 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -06001007 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -06001008 else
Jackie Liua197f662019-11-08 08:09:12 -07001009 io_queue_async_work(nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -06001010 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001011}
1012
Jens Axboe2b188cc2019-01-07 10:46:33 -07001013static void io_put_req(struct io_kiocb *req)
1014{
Jens Axboedef596e2019-01-09 08:59:42 -07001015 if (refcount_dec_and_test(&req->refs))
1016 io_free_req(req);
1017}
1018
Jens Axboe978db572019-11-14 22:39:04 -07001019/*
1020 * Must only be used if we don't need to care about links, usually from
1021 * within the completion handling itself.
1022 */
1023static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001024{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001025 /* drop both submit and complete references */
1026 if (refcount_sub_and_test(2, &req->refs))
1027 __io_free_req(req);
1028}
1029
Jens Axboe978db572019-11-14 22:39:04 -07001030static void io_double_put_req(struct io_kiocb *req)
1031{
1032 /* drop both submit and complete references */
1033 if (refcount_sub_and_test(2, &req->refs))
1034 io_free_req(req);
1035}
1036
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001037static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001038{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001039 struct io_rings *rings = ctx->rings;
1040
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001041 /*
1042 * noflush == true is from the waitqueue handler, just ensure we wake
1043 * up the task, and the next invocation will flush the entries. We
1044 * cannot safely to it from here.
1045 */
1046 if (noflush && !list_empty(&ctx->cq_overflow_list))
1047 return -1U;
1048
1049 io_cqring_overflow_flush(ctx, false);
1050
Jens Axboea3a0e432019-08-20 11:03:11 -06001051 /* See comment at the top of this file */
1052 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001053 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001054}
1055
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001056static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1057{
1058 struct io_rings *rings = ctx->rings;
1059
1060 /* make sure SQ entry isn't read before tail */
1061 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1062}
1063
Jens Axboedef596e2019-01-09 08:59:42 -07001064/*
1065 * Find and free completed poll iocbs
1066 */
1067static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1068 struct list_head *done)
1069{
1070 void *reqs[IO_IOPOLL_BATCH];
1071 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001072 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001073
Jens Axboe09bb8392019-03-13 12:39:28 -06001074 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001075 while (!list_empty(done)) {
1076 req = list_first_entry(done, struct io_kiocb, list);
1077 list_del(&req->list);
1078
Jens Axboe78e19bb2019-11-06 15:21:34 -07001079 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001080 (*nr_events)++;
1081
Jens Axboe09bb8392019-03-13 12:39:28 -06001082 if (refcount_dec_and_test(&req->refs)) {
1083 /* If we're not using fixed files, we have to pair the
1084 * completion part with the file put. Use regular
1085 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001086 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001087 */
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03001088 if (((req->flags &
1089 (REQ_F_FIXED_FILE|REQ_F_LINK|REQ_F_FREE_SQE)) ==
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001090 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001091 reqs[to_free++] = req;
1092 if (to_free == ARRAY_SIZE(reqs))
1093 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001094 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001095 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001096 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001097 }
Jens Axboedef596e2019-01-09 08:59:42 -07001098 }
Jens Axboedef596e2019-01-09 08:59:42 -07001099
Jens Axboe09bb8392019-03-13 12:39:28 -06001100 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001101 io_free_req_many(ctx, reqs, &to_free);
1102}
1103
1104static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1105 long min)
1106{
1107 struct io_kiocb *req, *tmp;
1108 LIST_HEAD(done);
1109 bool spin;
1110 int ret;
1111
1112 /*
1113 * Only spin for completions if we don't have multiple devices hanging
1114 * off our complete list, and we're under the requested amount.
1115 */
1116 spin = !ctx->poll_multi_file && *nr_events < min;
1117
1118 ret = 0;
1119 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1120 struct kiocb *kiocb = &req->rw;
1121
1122 /*
1123 * Move completed entries to our local list. If we find a
1124 * request that requires polling, break out and complete
1125 * the done list first, if we have entries there.
1126 */
1127 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1128 list_move_tail(&req->list, &done);
1129 continue;
1130 }
1131 if (!list_empty(&done))
1132 break;
1133
1134 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1135 if (ret < 0)
1136 break;
1137
1138 if (ret && spin)
1139 spin = false;
1140 ret = 0;
1141 }
1142
1143 if (!list_empty(&done))
1144 io_iopoll_complete(ctx, nr_events, &done);
1145
1146 return ret;
1147}
1148
1149/*
1150 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1151 * non-spinning poll check - we'll still enter the driver poll loop, but only
1152 * as a non-spinning completion check.
1153 */
1154static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1155 long min)
1156{
Jens Axboe08f54392019-08-21 22:19:11 -06001157 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001158 int ret;
1159
1160 ret = io_do_iopoll(ctx, nr_events, min);
1161 if (ret < 0)
1162 return ret;
1163 if (!min || *nr_events >= min)
1164 return 0;
1165 }
1166
1167 return 1;
1168}
1169
1170/*
1171 * We can't just wait for polled events to come to us, we have to actively
1172 * find and complete them.
1173 */
1174static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1175{
1176 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1177 return;
1178
1179 mutex_lock(&ctx->uring_lock);
1180 while (!list_empty(&ctx->poll_list)) {
1181 unsigned int nr_events = 0;
1182
1183 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001184
1185 /*
1186 * Ensure we allow local-to-the-cpu processing to take place,
1187 * in this case we need to ensure that we reap all events.
1188 */
1189 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001190 }
1191 mutex_unlock(&ctx->uring_lock);
1192}
1193
Jens Axboe2b2ed972019-10-25 10:06:15 -06001194static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1195 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001196{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001197 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001198
1199 do {
1200 int tmin = 0;
1201
Jens Axboe500f9fb2019-08-19 12:15:59 -06001202 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001203 * Don't enter poll loop if we already have events pending.
1204 * If we do, we can potentially be spinning for commands that
1205 * already triggered a CQE (eg in error).
1206 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001207 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001208 break;
1209
1210 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001211 * If a submit got punted to a workqueue, we can have the
1212 * application entering polling for a command before it gets
1213 * issued. That app will hold the uring_lock for the duration
1214 * of the poll right here, so we need to take a breather every
1215 * now and then to ensure that the issue has a chance to add
1216 * the poll to the issued list. Otherwise we can spin here
1217 * forever, while the workqueue is stuck trying to acquire the
1218 * very same mutex.
1219 */
1220 if (!(++iters & 7)) {
1221 mutex_unlock(&ctx->uring_lock);
1222 mutex_lock(&ctx->uring_lock);
1223 }
1224
Jens Axboedef596e2019-01-09 08:59:42 -07001225 if (*nr_events < min)
1226 tmin = min - *nr_events;
1227
1228 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1229 if (ret <= 0)
1230 break;
1231 ret = 0;
1232 } while (min && !*nr_events && !need_resched());
1233
Jens Axboe2b2ed972019-10-25 10:06:15 -06001234 return ret;
1235}
1236
1237static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1238 long min)
1239{
1240 int ret;
1241
1242 /*
1243 * We disallow the app entering submit/complete with polling, but we
1244 * still need to lock the ring to prevent racing with polled issue
1245 * that got punted to a workqueue.
1246 */
1247 mutex_lock(&ctx->uring_lock);
1248 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001249 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001250 return ret;
1251}
1252
Jens Axboe491381ce2019-10-17 09:20:46 -06001253static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001254{
Jens Axboe491381ce2019-10-17 09:20:46 -06001255 /*
1256 * Tell lockdep we inherited freeze protection from submission
1257 * thread.
1258 */
1259 if (req->flags & REQ_F_ISREG) {
1260 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001261
Jens Axboe491381ce2019-10-17 09:20:46 -06001262 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001263 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001264 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001265}
1266
Jens Axboeba816ad2019-09-28 11:36:45 -06001267static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001268{
1269 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1270
Jens Axboe491381ce2019-10-17 09:20:46 -06001271 if (kiocb->ki_flags & IOCB_WRITE)
1272 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001273
Jens Axboe9e645e112019-05-10 16:07:28 -06001274 if ((req->flags & REQ_F_LINK) && res != req->result)
1275 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001276 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001277}
1278
1279static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1280{
1281 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1282
1283 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001284 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001285}
1286
Jens Axboeba816ad2019-09-28 11:36:45 -06001287static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1288{
1289 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001290 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001291
1292 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001293 io_put_req_find_next(req, &nxt);
1294
1295 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001296}
1297
Jens Axboedef596e2019-01-09 08:59:42 -07001298static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1299{
1300 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1301
Jens Axboe491381ce2019-10-17 09:20:46 -06001302 if (kiocb->ki_flags & IOCB_WRITE)
1303 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001304
Jens Axboe9e645e112019-05-10 16:07:28 -06001305 if ((req->flags & REQ_F_LINK) && res != req->result)
1306 req->flags |= REQ_F_FAIL_LINK;
1307 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001308 if (res != -EAGAIN)
1309 req->flags |= REQ_F_IOPOLL_COMPLETED;
1310}
1311
1312/*
1313 * After the iocb has been issued, it's safe to be found on the poll list.
1314 * Adding the kiocb to the list AFTER submission ensures that we don't
1315 * find it from a io_iopoll_getevents() thread before the issuer is done
1316 * accessing the kiocb cookie.
1317 */
1318static void io_iopoll_req_issued(struct io_kiocb *req)
1319{
1320 struct io_ring_ctx *ctx = req->ctx;
1321
1322 /*
1323 * Track whether we have multiple files in our lists. This will impact
1324 * how we do polling eventually, not spinning if we're on potentially
1325 * different devices.
1326 */
1327 if (list_empty(&ctx->poll_list)) {
1328 ctx->poll_multi_file = false;
1329 } else if (!ctx->poll_multi_file) {
1330 struct io_kiocb *list_req;
1331
1332 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1333 list);
1334 if (list_req->rw.ki_filp != req->rw.ki_filp)
1335 ctx->poll_multi_file = true;
1336 }
1337
1338 /*
1339 * For fast devices, IO may have already completed. If it has, add
1340 * it to the front so we find it first.
1341 */
1342 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1343 list_add(&req->list, &ctx->poll_list);
1344 else
1345 list_add_tail(&req->list, &ctx->poll_list);
1346}
1347
Jens Axboe3d6770f2019-04-13 11:50:54 -06001348static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001349{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001350 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001351 int diff = state->has_refs - state->used_refs;
1352
1353 if (diff)
1354 fput_many(state->file, diff);
1355 state->file = NULL;
1356 }
1357}
1358
1359/*
1360 * Get as many references to a file as we have IOs left in this submission,
1361 * assuming most submissions are for one file, or at least that each file
1362 * has more than one submission.
1363 */
1364static struct file *io_file_get(struct io_submit_state *state, int fd)
1365{
1366 if (!state)
1367 return fget(fd);
1368
1369 if (state->file) {
1370 if (state->fd == fd) {
1371 state->used_refs++;
1372 state->ios_left--;
1373 return state->file;
1374 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001375 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001376 }
1377 state->file = fget_many(fd, state->ios_left);
1378 if (!state->file)
1379 return NULL;
1380
1381 state->fd = fd;
1382 state->has_refs = state->ios_left;
1383 state->used_refs = 1;
1384 state->ios_left--;
1385 return state->file;
1386}
1387
Jens Axboe2b188cc2019-01-07 10:46:33 -07001388/*
1389 * If we tracked the file through the SCM inflight mechanism, we could support
1390 * any file. For now, just ensure that anything potentially problematic is done
1391 * inline.
1392 */
1393static bool io_file_supports_async(struct file *file)
1394{
1395 umode_t mode = file_inode(file)->i_mode;
1396
1397 if (S_ISBLK(mode) || S_ISCHR(mode))
1398 return true;
1399 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1400 return true;
1401
1402 return false;
1403}
1404
Pavel Begunkov267bc902019-11-07 01:41:08 +03001405static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001406{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001407 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001408 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001409 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001410 unsigned ioprio;
1411 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001412
Jens Axboe09bb8392019-03-13 12:39:28 -06001413 if (!req->file)
1414 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001415
Jens Axboe491381ce2019-10-17 09:20:46 -06001416 if (S_ISREG(file_inode(req->file)->i_mode))
1417 req->flags |= REQ_F_ISREG;
1418
1419 /*
1420 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1421 * we know to async punt it even if it was opened O_NONBLOCK
1422 */
1423 if (force_nonblock && !io_file_supports_async(req->file)) {
1424 req->flags |= REQ_F_MUST_PUNT;
1425 return -EAGAIN;
1426 }
Jens Axboe6b063142019-01-10 22:13:58 -07001427
Jens Axboe2b188cc2019-01-07 10:46:33 -07001428 kiocb->ki_pos = READ_ONCE(sqe->off);
1429 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1430 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1431
1432 ioprio = READ_ONCE(sqe->ioprio);
1433 if (ioprio) {
1434 ret = ioprio_check_cap(ioprio);
1435 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001436 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001437
1438 kiocb->ki_ioprio = ioprio;
1439 } else
1440 kiocb->ki_ioprio = get_current_ioprio();
1441
1442 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1443 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001444 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001445
1446 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001447 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1448 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001449 req->flags |= REQ_F_NOWAIT;
1450
1451 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001452 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001453
Jens Axboedef596e2019-01-09 08:59:42 -07001454 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001455 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1456 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001457 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001458
Jens Axboedef596e2019-01-09 08:59:42 -07001459 kiocb->ki_flags |= IOCB_HIPRI;
1460 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001461 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001462 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001463 if (kiocb->ki_flags & IOCB_HIPRI)
1464 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001465 kiocb->ki_complete = io_complete_rw;
1466 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001467 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001468}
1469
1470static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1471{
1472 switch (ret) {
1473 case -EIOCBQUEUED:
1474 break;
1475 case -ERESTARTSYS:
1476 case -ERESTARTNOINTR:
1477 case -ERESTARTNOHAND:
1478 case -ERESTART_RESTARTBLOCK:
1479 /*
1480 * We can't just restart the syscall, since previously
1481 * submitted sqes may already be in progress. Just fail this
1482 * IO with EINTR.
1483 */
1484 ret = -EINTR;
1485 /* fall through */
1486 default:
1487 kiocb->ki_complete(kiocb, ret, 0);
1488 }
1489}
1490
Jens Axboeba816ad2019-09-28 11:36:45 -06001491static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1492 bool in_async)
1493{
1494 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1495 *nxt = __io_complete_rw(kiocb, ret);
1496 else
1497 io_rw_done(kiocb, ret);
1498}
1499
Jens Axboeedafcce2019-01-09 09:16:05 -07001500static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1501 const struct io_uring_sqe *sqe,
1502 struct iov_iter *iter)
1503{
1504 size_t len = READ_ONCE(sqe->len);
1505 struct io_mapped_ubuf *imu;
1506 unsigned index, buf_index;
1507 size_t offset;
1508 u64 buf_addr;
1509
1510 /* attempt to use fixed buffers without having provided iovecs */
1511 if (unlikely(!ctx->user_bufs))
1512 return -EFAULT;
1513
1514 buf_index = READ_ONCE(sqe->buf_index);
1515 if (unlikely(buf_index >= ctx->nr_user_bufs))
1516 return -EFAULT;
1517
1518 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1519 imu = &ctx->user_bufs[index];
1520 buf_addr = READ_ONCE(sqe->addr);
1521
1522 /* overflow */
1523 if (buf_addr + len < buf_addr)
1524 return -EFAULT;
1525 /* not inside the mapped region */
1526 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1527 return -EFAULT;
1528
1529 /*
1530 * May not be a start of buffer, set size appropriately
1531 * and advance us to the beginning.
1532 */
1533 offset = buf_addr - imu->ubuf;
1534 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001535
1536 if (offset) {
1537 /*
1538 * Don't use iov_iter_advance() here, as it's really slow for
1539 * using the latter parts of a big fixed buffer - it iterates
1540 * over each segment manually. We can cheat a bit here, because
1541 * we know that:
1542 *
1543 * 1) it's a BVEC iter, we set it up
1544 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1545 * first and last bvec
1546 *
1547 * So just find our index, and adjust the iterator afterwards.
1548 * If the offset is within the first bvec (or the whole first
1549 * bvec, just use iov_iter_advance(). This makes it easier
1550 * since we can just skip the first segment, which may not
1551 * be PAGE_SIZE aligned.
1552 */
1553 const struct bio_vec *bvec = imu->bvec;
1554
1555 if (offset <= bvec->bv_len) {
1556 iov_iter_advance(iter, offset);
1557 } else {
1558 unsigned long seg_skip;
1559
1560 /* skip first vec */
1561 offset -= bvec->bv_len;
1562 seg_skip = 1 + (offset >> PAGE_SHIFT);
1563
1564 iter->bvec = bvec + seg_skip;
1565 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001566 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001567 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001568 }
1569 }
1570
Jens Axboe5e559562019-11-13 16:12:46 -07001571 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001572}
1573
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001574static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1575 const struct sqe_submit *s, struct iovec **iovec,
1576 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001577{
1578 const struct io_uring_sqe *sqe = s->sqe;
1579 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1580 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001581 u8 opcode;
1582
1583 /*
1584 * We're reading ->opcode for the second time, but the first read
1585 * doesn't care whether it's _FIXED or not, so it doesn't matter
1586 * whether ->opcode changes concurrently. The first read does care
1587 * about whether it is a READ or a WRITE, so we don't trust this read
1588 * for that purpose and instead let the caller pass in the read/write
1589 * flag.
1590 */
1591 opcode = READ_ONCE(sqe->opcode);
1592 if (opcode == IORING_OP_READ_FIXED ||
1593 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001594 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001595 *iovec = NULL;
1596 return ret;
1597 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001598
1599 if (!s->has_user)
1600 return -EFAULT;
1601
1602#ifdef CONFIG_COMPAT
1603 if (ctx->compat)
1604 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1605 iovec, iter);
1606#endif
1607
1608 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1609}
1610
Jens Axboe32960612019-09-23 11:05:34 -06001611/*
1612 * For files that don't have ->read_iter() and ->write_iter(), handle them
1613 * by looping over ->read() or ->write() manually.
1614 */
1615static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1616 struct iov_iter *iter)
1617{
1618 ssize_t ret = 0;
1619
1620 /*
1621 * Don't support polled IO through this interface, and we can't
1622 * support non-blocking either. For the latter, this just causes
1623 * the kiocb to be handled from an async context.
1624 */
1625 if (kiocb->ki_flags & IOCB_HIPRI)
1626 return -EOPNOTSUPP;
1627 if (kiocb->ki_flags & IOCB_NOWAIT)
1628 return -EAGAIN;
1629
1630 while (iov_iter_count(iter)) {
1631 struct iovec iovec = iov_iter_iovec(iter);
1632 ssize_t nr;
1633
1634 if (rw == READ) {
1635 nr = file->f_op->read(file, iovec.iov_base,
1636 iovec.iov_len, &kiocb->ki_pos);
1637 } else {
1638 nr = file->f_op->write(file, iovec.iov_base,
1639 iovec.iov_len, &kiocb->ki_pos);
1640 }
1641
1642 if (nr < 0) {
1643 if (!ret)
1644 ret = nr;
1645 break;
1646 }
1647 ret += nr;
1648 if (nr != iovec.iov_len)
1649 break;
1650 iov_iter_advance(iter, nr);
1651 }
1652
1653 return ret;
1654}
1655
Pavel Begunkov267bc902019-11-07 01:41:08 +03001656static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001657 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001658{
1659 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1660 struct kiocb *kiocb = &req->rw;
1661 struct iov_iter iter;
1662 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001663 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001664 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001665
Pavel Begunkov267bc902019-11-07 01:41:08 +03001666 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001667 if (ret)
1668 return ret;
1669 file = kiocb->ki_filp;
1670
Jens Axboe2b188cc2019-01-07 10:46:33 -07001671 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001672 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001673
Pavel Begunkov267bc902019-11-07 01:41:08 +03001674 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001675 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001676 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001677
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001678 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001679 if (req->flags & REQ_F_LINK)
1680 req->result = read_size;
1681
Jens Axboe31b51512019-01-18 22:56:34 -07001682 iov_count = iov_iter_count(&iter);
1683 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001684 if (!ret) {
1685 ssize_t ret2;
1686
Jens Axboe32960612019-09-23 11:05:34 -06001687 if (file->f_op->read_iter)
1688 ret2 = call_read_iter(file, kiocb, &iter);
1689 else
1690 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1691
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001692 /*
1693 * In case of a short read, punt to async. This can happen
1694 * if we have data partially cached. Alternatively we can
1695 * return the short read, in which case the application will
1696 * need to issue another SQE and wait for it. That SQE will
1697 * need async punt anyway, so it's more efficient to do it
1698 * here.
1699 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001700 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1701 (req->flags & REQ_F_ISREG) &&
1702 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001703 ret2 = -EAGAIN;
1704 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001705 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001706 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001707 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708 ret = -EAGAIN;
1709 }
1710 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001711 return ret;
1712}
1713
Pavel Begunkov267bc902019-11-07 01:41:08 +03001714static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001715 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001716{
1717 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1718 struct kiocb *kiocb = &req->rw;
1719 struct iov_iter iter;
1720 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001721 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001722 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001723
Pavel Begunkov267bc902019-11-07 01:41:08 +03001724 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001725 if (ret)
1726 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727
Jens Axboe2b188cc2019-01-07 10:46:33 -07001728 file = kiocb->ki_filp;
1729 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001730 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001731
Pavel Begunkov267bc902019-11-07 01:41:08 +03001732 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001733 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001734 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735
Jens Axboe9e645e112019-05-10 16:07:28 -06001736 if (req->flags & REQ_F_LINK)
1737 req->result = ret;
1738
Jens Axboe31b51512019-01-18 22:56:34 -07001739 iov_count = iov_iter_count(&iter);
1740
1741 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001742 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001743 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001744
1745 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001746 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001747 ssize_t ret2;
1748
Jens Axboe2b188cc2019-01-07 10:46:33 -07001749 /*
1750 * Open-code file_start_write here to grab freeze protection,
1751 * which will be released by another thread in
1752 * io_complete_rw(). Fool lockdep by telling it the lock got
1753 * released so that it doesn't complain about the held lock when
1754 * we return to userspace.
1755 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001756 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001757 __sb_start_write(file_inode(file)->i_sb,
1758 SB_FREEZE_WRITE, true);
1759 __sb_writers_release(file_inode(file)->i_sb,
1760 SB_FREEZE_WRITE);
1761 }
1762 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001763
Jens Axboe32960612019-09-23 11:05:34 -06001764 if (file->f_op->write_iter)
1765 ret2 = call_write_iter(file, kiocb, &iter);
1766 else
1767 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001768 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001769 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001770 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001771 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001772 }
Jens Axboe31b51512019-01-18 22:56:34 -07001773out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001774 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001775 return ret;
1776}
1777
1778/*
1779 * IORING_OP_NOP just posts a completion event, nothing else.
1780 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001781static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001782{
1783 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001784
Jens Axboedef596e2019-01-09 08:59:42 -07001785 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1786 return -EINVAL;
1787
Jens Axboe78e19bb2019-11-06 15:21:34 -07001788 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001789 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001790 return 0;
1791}
1792
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001793static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1794{
Jens Axboe6b063142019-01-10 22:13:58 -07001795 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001796
Jens Axboe09bb8392019-03-13 12:39:28 -06001797 if (!req->file)
1798 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001799
Jens Axboe6b063142019-01-10 22:13:58 -07001800 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001801 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001802 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001803 return -EINVAL;
1804
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001805 return 0;
1806}
1807
1808static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001809 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001810{
1811 loff_t sqe_off = READ_ONCE(sqe->off);
1812 loff_t sqe_len = READ_ONCE(sqe->len);
1813 loff_t end = sqe_off + sqe_len;
1814 unsigned fsync_flags;
1815 int ret;
1816
1817 fsync_flags = READ_ONCE(sqe->fsync_flags);
1818 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1819 return -EINVAL;
1820
1821 ret = io_prep_fsync(req, sqe);
1822 if (ret)
1823 return ret;
1824
1825 /* fsync always requires a blocking context */
1826 if (force_nonblock)
1827 return -EAGAIN;
1828
1829 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1830 end > 0 ? end : LLONG_MAX,
1831 fsync_flags & IORING_FSYNC_DATASYNC);
1832
Jens Axboe9e645e112019-05-10 16:07:28 -06001833 if (ret < 0 && (req->flags & REQ_F_LINK))
1834 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001835 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001836 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001837 return 0;
1838}
1839
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001840static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1841{
1842 struct io_ring_ctx *ctx = req->ctx;
1843 int ret = 0;
1844
1845 if (!req->file)
1846 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001847
1848 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1849 return -EINVAL;
1850 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1851 return -EINVAL;
1852
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001853 return ret;
1854}
1855
1856static int io_sync_file_range(struct io_kiocb *req,
1857 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001858 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001859 bool force_nonblock)
1860{
1861 loff_t sqe_off;
1862 loff_t sqe_len;
1863 unsigned flags;
1864 int ret;
1865
1866 ret = io_prep_sfr(req, sqe);
1867 if (ret)
1868 return ret;
1869
1870 /* sync_file_range always requires a blocking context */
1871 if (force_nonblock)
1872 return -EAGAIN;
1873
1874 sqe_off = READ_ONCE(sqe->off);
1875 sqe_len = READ_ONCE(sqe->len);
1876 flags = READ_ONCE(sqe->sync_range_flags);
1877
1878 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1879
Jens Axboe9e645e112019-05-10 16:07:28 -06001880 if (ret < 0 && (req->flags & REQ_F_LINK))
1881 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001882 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001883 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001884 return 0;
1885}
1886
Jens Axboe0fa03c62019-04-19 13:34:07 -06001887#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001888static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001889 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001890 long (*fn)(struct socket *, struct user_msghdr __user *,
1891 unsigned int))
1892{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001893 struct socket *sock;
1894 int ret;
1895
1896 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1897 return -EINVAL;
1898
1899 sock = sock_from_file(req->file, &ret);
1900 if (sock) {
1901 struct user_msghdr __user *msg;
1902 unsigned flags;
1903
1904 flags = READ_ONCE(sqe->msg_flags);
1905 if (flags & MSG_DONTWAIT)
1906 req->flags |= REQ_F_NOWAIT;
1907 else if (force_nonblock)
1908 flags |= MSG_DONTWAIT;
1909
1910 msg = (struct user_msghdr __user *) (unsigned long)
1911 READ_ONCE(sqe->addr);
1912
Jens Axboeaa1fa282019-04-19 13:38:09 -06001913 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001914 if (force_nonblock && ret == -EAGAIN)
1915 return ret;
1916 }
1917
Jens Axboe78e19bb2019-11-06 15:21:34 -07001918 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001919 if (ret < 0 && (req->flags & REQ_F_LINK))
1920 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001921 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001922 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001923}
1924#endif
1925
1926static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001927 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001928{
1929#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001930 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1931 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001932#else
1933 return -EOPNOTSUPP;
1934#endif
1935}
1936
1937static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001938 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001939{
1940#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001941 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1942 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001943#else
1944 return -EOPNOTSUPP;
1945#endif
1946}
1947
Jens Axboe17f2fe32019-10-17 14:42:58 -06001948static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1949 struct io_kiocb **nxt, bool force_nonblock)
1950{
1951#if defined(CONFIG_NET)
1952 struct sockaddr __user *addr;
1953 int __user *addr_len;
1954 unsigned file_flags;
1955 int flags, ret;
1956
1957 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1958 return -EINVAL;
1959 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1960 return -EINVAL;
1961
1962 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1963 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1964 flags = READ_ONCE(sqe->accept_flags);
1965 file_flags = force_nonblock ? O_NONBLOCK : 0;
1966
1967 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1968 if (ret == -EAGAIN && force_nonblock) {
1969 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1970 return -EAGAIN;
1971 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001972 if (ret == -ERESTARTSYS)
1973 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001974 if (ret < 0 && (req->flags & REQ_F_LINK))
1975 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001976 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001977 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001978 return 0;
1979#else
1980 return -EOPNOTSUPP;
1981#endif
1982}
1983
Jens Axboeeac406c2019-11-14 12:09:58 -07001984static inline void io_poll_remove_req(struct io_kiocb *req)
1985{
1986 if (!RB_EMPTY_NODE(&req->rb_node)) {
1987 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
1988 RB_CLEAR_NODE(&req->rb_node);
1989 }
1990}
1991
Jens Axboe221c5eb2019-01-17 09:41:58 -07001992static void io_poll_remove_one(struct io_kiocb *req)
1993{
1994 struct io_poll_iocb *poll = &req->poll;
1995
1996 spin_lock(&poll->head->lock);
1997 WRITE_ONCE(poll->canceled, true);
1998 if (!list_empty(&poll->wait.entry)) {
1999 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002000 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002001 }
2002 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002003 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002004}
2005
2006static void io_poll_remove_all(struct io_ring_ctx *ctx)
2007{
Jens Axboeeac406c2019-11-14 12:09:58 -07002008 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002009 struct io_kiocb *req;
2010
2011 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002012 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2013 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002014 io_poll_remove_one(req);
2015 }
2016 spin_unlock_irq(&ctx->completion_lock);
2017}
2018
Jens Axboe47f46762019-11-09 17:43:02 -07002019static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2020{
Jens Axboeeac406c2019-11-14 12:09:58 -07002021 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002022 struct io_kiocb *req;
2023
Jens Axboeeac406c2019-11-14 12:09:58 -07002024 p = ctx->cancel_tree.rb_node;
2025 while (p) {
2026 parent = p;
2027 req = rb_entry(parent, struct io_kiocb, rb_node);
2028 if (sqe_addr < req->user_data) {
2029 p = p->rb_left;
2030 } else if (sqe_addr > req->user_data) {
2031 p = p->rb_right;
2032 } else {
2033 io_poll_remove_one(req);
2034 return 0;
2035 }
Jens Axboe47f46762019-11-09 17:43:02 -07002036 }
2037
2038 return -ENOENT;
2039}
2040
Jens Axboe221c5eb2019-01-17 09:41:58 -07002041/*
2042 * Find a running poll command that matches one specified in sqe->addr,
2043 * and remove it if found.
2044 */
2045static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2046{
2047 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002048 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002049
2050 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2051 return -EINVAL;
2052 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2053 sqe->poll_events)
2054 return -EINVAL;
2055
2056 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002057 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002058 spin_unlock_irq(&ctx->completion_lock);
2059
Jens Axboe78e19bb2019-11-06 15:21:34 -07002060 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002061 if (ret < 0 && (req->flags & REQ_F_LINK))
2062 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002063 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002064 return 0;
2065}
2066
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002067static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002068{
Jackie Liua197f662019-11-08 08:09:12 -07002069 struct io_ring_ctx *ctx = req->ctx;
2070
Jens Axboe8c838782019-03-12 15:48:16 -06002071 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002072 if (error)
2073 io_cqring_fill_event(req, error);
2074 else
2075 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002076 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002077}
2078
Jens Axboe561fb042019-10-24 07:25:42 -06002079static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002080{
Jens Axboe561fb042019-10-24 07:25:42 -06002081 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002082 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2083 struct io_poll_iocb *poll = &req->poll;
2084 struct poll_table_struct pt = { ._key = poll->events };
2085 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002086 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002087 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002088 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002089
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002090 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002091 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002092 ret = -ECANCELED;
2093 } else if (READ_ONCE(poll->canceled)) {
2094 ret = -ECANCELED;
2095 }
Jens Axboe561fb042019-10-24 07:25:42 -06002096
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002097 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002098 mask = vfs_poll(poll->file, &pt) & poll->events;
2099
2100 /*
2101 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2102 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2103 * synchronize with them. In the cancellation case the list_del_init
2104 * itself is not actually needed, but harmless so we keep it in to
2105 * avoid further branches in the fast path.
2106 */
2107 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002108 if (!mask && ret != -ECANCELED) {
Jens Axboe221c5eb2019-01-17 09:41:58 -07002109 add_wait_queue(poll->head, &poll->wait);
2110 spin_unlock_irq(&ctx->completion_lock);
2111 return;
2112 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002113 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002114 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002115 spin_unlock_irq(&ctx->completion_lock);
2116
Jens Axboe8c838782019-03-12 15:48:16 -06002117 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002118
Jens Axboefba38c22019-11-18 12:27:57 -07002119 if (ret < 0 && req->flags & REQ_F_LINK)
2120 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002121 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002122 if (nxt)
2123 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002124}
2125
2126static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2127 void *key)
2128{
2129 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2130 wait);
2131 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2132 struct io_ring_ctx *ctx = req->ctx;
2133 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002134 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002135
2136 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002137 if (mask && !(mask & poll->events))
2138 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002139
2140 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002141
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002142 /*
2143 * Run completion inline if we can. We're using trylock here because
2144 * we are violating the completion_lock -> poll wq lock ordering.
2145 * If we have a link timeout we're going to need the completion_lock
2146 * for finalizing the request, mark us as having grabbed that already.
2147 */
Jens Axboe8c838782019-03-12 15:48:16 -06002148 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002149 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002150 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002151 req->flags |= REQ_F_COMP_LOCKED;
2152 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002153 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2154
2155 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002156 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002157 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002158 }
2159
Jens Axboe221c5eb2019-01-17 09:41:58 -07002160 return 1;
2161}
2162
2163struct io_poll_table {
2164 struct poll_table_struct pt;
2165 struct io_kiocb *req;
2166 int error;
2167};
2168
2169static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2170 struct poll_table_struct *p)
2171{
2172 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2173
2174 if (unlikely(pt->req->poll.head)) {
2175 pt->error = -EINVAL;
2176 return;
2177 }
2178
2179 pt->error = 0;
2180 pt->req->poll.head = head;
2181 add_wait_queue(head, &pt->req->poll.wait);
2182}
2183
Jens Axboeeac406c2019-11-14 12:09:58 -07002184static void io_poll_req_insert(struct io_kiocb *req)
2185{
2186 struct io_ring_ctx *ctx = req->ctx;
2187 struct rb_node **p = &ctx->cancel_tree.rb_node;
2188 struct rb_node *parent = NULL;
2189 struct io_kiocb *tmp;
2190
2191 while (*p) {
2192 parent = *p;
2193 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2194 if (req->user_data < tmp->user_data)
2195 p = &(*p)->rb_left;
2196 else
2197 p = &(*p)->rb_right;
2198 }
2199 rb_link_node(&req->rb_node, parent, p);
2200 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2201}
2202
Jens Axboe89723d02019-11-05 15:32:58 -07002203static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2204 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002205{
2206 struct io_poll_iocb *poll = &req->poll;
2207 struct io_ring_ctx *ctx = req->ctx;
2208 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002209 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002210 __poll_t mask;
2211 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002212
2213 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2214 return -EINVAL;
2215 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2216 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002217 if (!poll->file)
2218 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002219
Jens Axboe6cc47d12019-09-18 11:18:23 -06002220 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002221 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002222 events = READ_ONCE(sqe->poll_events);
2223 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002224 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002225
Jens Axboe221c5eb2019-01-17 09:41:58 -07002226 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002227 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002228 poll->canceled = false;
2229
2230 ipt.pt._qproc = io_poll_queue_proc;
2231 ipt.pt._key = poll->events;
2232 ipt.req = req;
2233 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2234
2235 /* initialized the list so that we can do list_empty checks */
2236 INIT_LIST_HEAD(&poll->wait.entry);
2237 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2238
Jens Axboe36703242019-07-25 10:20:18 -06002239 INIT_LIST_HEAD(&req->list);
2240
Jens Axboe221c5eb2019-01-17 09:41:58 -07002241 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002242
2243 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002244 if (likely(poll->head)) {
2245 spin_lock(&poll->head->lock);
2246 if (unlikely(list_empty(&poll->wait.entry))) {
2247 if (ipt.error)
2248 cancel = true;
2249 ipt.error = 0;
2250 mask = 0;
2251 }
2252 if (mask || ipt.error)
2253 list_del_init(&poll->wait.entry);
2254 else if (cancel)
2255 WRITE_ONCE(poll->canceled, true);
2256 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002257 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002258 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002259 }
Jens Axboe8c838782019-03-12 15:48:16 -06002260 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002261 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002262 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002263 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002264 spin_unlock_irq(&ctx->completion_lock);
2265
Jens Axboe8c838782019-03-12 15:48:16 -06002266 if (mask) {
2267 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002268 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002269 }
Jens Axboe8c838782019-03-12 15:48:16 -06002270 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002271}
2272
Jens Axboe5262f562019-09-17 12:26:57 -06002273static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2274{
Jens Axboead8a48a2019-11-15 08:49:11 -07002275 struct io_timeout_data *data = container_of(timer,
2276 struct io_timeout_data, timer);
2277 struct io_kiocb *req = data->req;
2278 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002279 unsigned long flags;
2280
Jens Axboe5262f562019-09-17 12:26:57 -06002281 atomic_inc(&ctx->cq_timeouts);
2282
2283 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002284 /*
Jens Axboe11365042019-10-16 09:08:32 -06002285 * We could be racing with timeout deletion. If the list is empty,
2286 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002287 */
Jens Axboe842f9612019-10-29 12:34:10 -06002288 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002289 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002290
Jens Axboe11365042019-10-16 09:08:32 -06002291 /*
2292 * Adjust the reqs sequence before the current one because it
2293 * will consume a slot in the cq_ring and the the cq_tail
2294 * pointer will be increased, otherwise other timeout reqs may
2295 * return in advance without waiting for enough wait_nr.
2296 */
2297 prev = req;
2298 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2299 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002300 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002301 }
Jens Axboe842f9612019-10-29 12:34:10 -06002302
Jens Axboe78e19bb2019-11-06 15:21:34 -07002303 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002304 io_commit_cqring(ctx);
2305 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2306
2307 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002308 if (req->flags & REQ_F_LINK)
2309 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002310 io_put_req(req);
2311 return HRTIMER_NORESTART;
2312}
2313
Jens Axboe47f46762019-11-09 17:43:02 -07002314static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2315{
2316 struct io_kiocb *req;
2317 int ret = -ENOENT;
2318
2319 list_for_each_entry(req, &ctx->timeout_list, list) {
2320 if (user_data == req->user_data) {
2321 list_del_init(&req->list);
2322 ret = 0;
2323 break;
2324 }
2325 }
2326
2327 if (ret == -ENOENT)
2328 return ret;
2329
Jens Axboead8a48a2019-11-15 08:49:11 -07002330 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002331 if (ret == -1)
2332 return -EALREADY;
2333
Jens Axboefba38c22019-11-18 12:27:57 -07002334 if (req->flags & REQ_F_LINK)
2335 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002336 io_cqring_fill_event(req, -ECANCELED);
2337 io_put_req(req);
2338 return 0;
2339}
2340
Jens Axboe11365042019-10-16 09:08:32 -06002341/*
2342 * Remove or update an existing timeout command
2343 */
2344static int io_timeout_remove(struct io_kiocb *req,
2345 const struct io_uring_sqe *sqe)
2346{
2347 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002348 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002349 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002350
2351 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2352 return -EINVAL;
2353 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2354 return -EINVAL;
2355 flags = READ_ONCE(sqe->timeout_flags);
2356 if (flags)
2357 return -EINVAL;
2358
Jens Axboe11365042019-10-16 09:08:32 -06002359 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002360 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002361
Jens Axboe47f46762019-11-09 17:43:02 -07002362 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002363 io_commit_cqring(ctx);
2364 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002365 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002366 if (ret < 0 && req->flags & REQ_F_LINK)
2367 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002368 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002369 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002370}
2371
Jens Axboead8a48a2019-11-15 08:49:11 -07002372static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002373{
Jens Axboead8a48a2019-11-15 08:49:11 -07002374 const struct io_uring_sqe *sqe = req->submit.sqe;
2375 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002376 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002377
Jens Axboead8a48a2019-11-15 08:49:11 -07002378 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002379 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002380 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002381 return -EINVAL;
2382 flags = READ_ONCE(sqe->timeout_flags);
2383 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002384 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002385
Jens Axboead8a48a2019-11-15 08:49:11 -07002386 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2387 if (!data)
2388 return -ENOMEM;
2389 data->req = req;
2390 req->timeout.data = data;
2391 req->flags |= REQ_F_TIMEOUT;
2392
2393 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002394 return -EFAULT;
2395
Jens Axboe11365042019-10-16 09:08:32 -06002396 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002397 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002398 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002399 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002400
Jens Axboead8a48a2019-11-15 08:49:11 -07002401 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2402 return 0;
2403}
2404
2405static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2406{
2407 unsigned count;
2408 struct io_ring_ctx *ctx = req->ctx;
2409 struct io_timeout_data *data;
2410 struct list_head *entry;
2411 unsigned span = 0;
2412 int ret;
2413
2414 ret = io_timeout_setup(req);
2415 /* common setup allows flags (like links) set, we don't */
2416 if (!ret && sqe->flags)
2417 ret = -EINVAL;
2418 if (ret)
2419 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002420
Jens Axboe5262f562019-09-17 12:26:57 -06002421 /*
2422 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002423 * timeout event to be satisfied. If it isn't set, then this is
2424 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002425 */
2426 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002427 if (!count) {
2428 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2429 spin_lock_irq(&ctx->completion_lock);
2430 entry = ctx->timeout_list.prev;
2431 goto add;
2432 }
Jens Axboe5262f562019-09-17 12:26:57 -06002433
2434 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002435 /* reuse it to store the count */
2436 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002437
2438 /*
2439 * Insertion sort, ensuring the first entry in the list is always
2440 * the one we need first.
2441 */
Jens Axboe5262f562019-09-17 12:26:57 -06002442 spin_lock_irq(&ctx->completion_lock);
2443 list_for_each_prev(entry, &ctx->timeout_list) {
2444 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002445 unsigned nxt_sq_head;
2446 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002447
Jens Axboe93bd25b2019-11-11 23:34:31 -07002448 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2449 continue;
2450
yangerkun5da0fb12019-10-15 21:59:29 +08002451 /*
2452 * Since cached_sq_head + count - 1 can overflow, use type long
2453 * long to store it.
2454 */
2455 tmp = (long long)ctx->cached_sq_head + count - 1;
2456 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2457 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2458
2459 /*
2460 * cached_sq_head may overflow, and it will never overflow twice
2461 * once there is some timeout req still be valid.
2462 */
2463 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002464 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002465
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002466 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002467 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002468
2469 /*
2470 * Sequence of reqs after the insert one and itself should
2471 * be adjusted because each timeout req consumes a slot.
2472 */
2473 span++;
2474 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002475 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002476 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002477add:
Jens Axboe5262f562019-09-17 12:26:57 -06002478 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002479 data = req->timeout.data;
2480 data->timer.function = io_timeout_fn;
2481 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002482 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002483 return 0;
2484}
2485
Jens Axboe62755e32019-10-28 21:49:21 -06002486static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002487{
Jens Axboe62755e32019-10-28 21:49:21 -06002488 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002489
Jens Axboe62755e32019-10-28 21:49:21 -06002490 return req->user_data == (unsigned long) data;
2491}
2492
Jens Axboee977d6d2019-11-05 12:39:45 -07002493static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002494{
Jens Axboe62755e32019-10-28 21:49:21 -06002495 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002496 int ret = 0;
2497
Jens Axboe62755e32019-10-28 21:49:21 -06002498 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2499 switch (cancel_ret) {
2500 case IO_WQ_CANCEL_OK:
2501 ret = 0;
2502 break;
2503 case IO_WQ_CANCEL_RUNNING:
2504 ret = -EALREADY;
2505 break;
2506 case IO_WQ_CANCEL_NOTFOUND:
2507 ret = -ENOENT;
2508 break;
2509 }
2510
Jens Axboee977d6d2019-11-05 12:39:45 -07002511 return ret;
2512}
2513
Jens Axboe47f46762019-11-09 17:43:02 -07002514static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2515 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002516 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002517{
2518 unsigned long flags;
2519 int ret;
2520
2521 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2522 if (ret != -ENOENT) {
2523 spin_lock_irqsave(&ctx->completion_lock, flags);
2524 goto done;
2525 }
2526
2527 spin_lock_irqsave(&ctx->completion_lock, flags);
2528 ret = io_timeout_cancel(ctx, sqe_addr);
2529 if (ret != -ENOENT)
2530 goto done;
2531 ret = io_poll_cancel(ctx, sqe_addr);
2532done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002533 if (!ret)
2534 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002535 io_cqring_fill_event(req, ret);
2536 io_commit_cqring(ctx);
2537 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2538 io_cqring_ev_posted(ctx);
2539
2540 if (ret < 0 && (req->flags & REQ_F_LINK))
2541 req->flags |= REQ_F_FAIL_LINK;
2542 io_put_req_find_next(req, nxt);
2543}
2544
Jens Axboee977d6d2019-11-05 12:39:45 -07002545static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2546 struct io_kiocb **nxt)
2547{
2548 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002549
2550 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2551 return -EINVAL;
2552 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2553 sqe->cancel_flags)
2554 return -EINVAL;
2555
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002556 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002557 return 0;
2558}
2559
Jackie Liua197f662019-11-08 08:09:12 -07002560static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002561{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002562 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002563 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002564 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002565
Bob Liu9d858b22019-11-13 18:06:25 +08002566 /* Still need defer if there is pending req in defer list. */
2567 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002568 return 0;
2569
2570 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2571 if (!sqe_copy)
2572 return -EAGAIN;
2573
2574 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002575 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002576 spin_unlock_irq(&ctx->completion_lock);
2577 kfree(sqe_copy);
2578 return 0;
2579 }
2580
2581 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002582 req->flags |= REQ_F_FREE_SQE;
Jens Axboede0617e2019-04-06 21:51:27 -06002583 req->submit.sqe = sqe_copy;
2584
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002585 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002586 list_add_tail(&req->list, &ctx->defer_list);
2587 spin_unlock_irq(&ctx->completion_lock);
2588 return -EIOCBQUEUED;
2589}
2590
Jackie Liua197f662019-11-08 08:09:12 -07002591static int __io_submit_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2592 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002593{
Jens Axboee0c5c572019-03-12 10:18:47 -06002594 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002595 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002596 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002597
2598 opcode = READ_ONCE(s->sqe->opcode);
2599 switch (opcode) {
2600 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002601 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002602 break;
2603 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002604 if (unlikely(s->sqe->buf_index))
2605 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002606 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002607 break;
2608 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002609 if (unlikely(s->sqe->buf_index))
2610 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002611 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002612 break;
2613 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002614 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002615 break;
2616 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002617 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002618 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002619 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002620 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002621 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002622 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002623 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002624 break;
2625 case IORING_OP_POLL_REMOVE:
2626 ret = io_poll_remove(req, s->sqe);
2627 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002628 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002629 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002630 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002631 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002632 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002633 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002634 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002635 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002636 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002637 case IORING_OP_TIMEOUT:
2638 ret = io_timeout(req, s->sqe);
2639 break;
Jens Axboe11365042019-10-16 09:08:32 -06002640 case IORING_OP_TIMEOUT_REMOVE:
2641 ret = io_timeout_remove(req, s->sqe);
2642 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002643 case IORING_OP_ACCEPT:
2644 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2645 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002646 case IORING_OP_ASYNC_CANCEL:
2647 ret = io_async_cancel(req, s->sqe, nxt);
2648 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002649 default:
2650 ret = -EINVAL;
2651 break;
2652 }
2653
Jens Axboedef596e2019-01-09 08:59:42 -07002654 if (ret)
2655 return ret;
2656
2657 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002658 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002659 return -EAGAIN;
2660
2661 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002662 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002663 mutex_lock(&ctx->uring_lock);
2664 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002665 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002666 mutex_unlock(&ctx->uring_lock);
2667 }
2668
2669 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002670}
2671
Jens Axboe561fb042019-10-24 07:25:42 -06002672static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002673{
Jens Axboe561fb042019-10-24 07:25:42 -06002674 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002675 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002676 struct sqe_submit *s = &req->submit;
Jens Axboe561fb042019-10-24 07:25:42 -06002677 struct io_kiocb *nxt = NULL;
2678 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002679
Jens Axboe561fb042019-10-24 07:25:42 -06002680 /* Ensure we clear previously set non-block flag */
2681 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002682
Jens Axboe561fb042019-10-24 07:25:42 -06002683 if (work->flags & IO_WQ_WORK_CANCEL)
2684 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002685
Jens Axboe561fb042019-10-24 07:25:42 -06002686 if (!ret) {
2687 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2688 s->in_async = true;
2689 do {
Jackie Liua197f662019-11-08 08:09:12 -07002690 ret = __io_submit_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002691 /*
2692 * We can get EAGAIN for polled IO even though we're
2693 * forcing a sync submission from here, since we can't
2694 * wait for request slots on the block side.
2695 */
2696 if (ret != -EAGAIN)
2697 break;
2698 cond_resched();
2699 } while (1);
2700 }
Jens Axboe31b51512019-01-18 22:56:34 -07002701
Jens Axboe561fb042019-10-24 07:25:42 -06002702 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002703 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002704
Jens Axboe561fb042019-10-24 07:25:42 -06002705 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002706 if (req->flags & REQ_F_LINK)
2707 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002708 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002709 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002710 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002711
Jens Axboe561fb042019-10-24 07:25:42 -06002712 /* if a dependent link is ready, pass it back */
2713 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002714 struct io_kiocb *link;
2715
2716 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06002717 *workptr = &nxt->work;
Jens Axboe94ae5e72019-11-14 19:39:52 -07002718 if (link)
2719 io_queue_linked_timeout(link);
Jens Axboeedafcce2019-01-09 09:16:05 -07002720 }
Jens Axboe31b51512019-01-18 22:56:34 -07002721}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002722
Jens Axboe09bb8392019-03-13 12:39:28 -06002723static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2724{
2725 int op = READ_ONCE(sqe->opcode);
2726
2727 switch (op) {
2728 case IORING_OP_NOP:
2729 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002730 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002731 case IORING_OP_TIMEOUT_REMOVE:
2732 case IORING_OP_ASYNC_CANCEL:
2733 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002734 return false;
2735 default:
2736 return true;
2737 }
2738}
2739
Jens Axboe65e19f52019-10-26 07:20:21 -06002740static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2741 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002742{
Jens Axboe65e19f52019-10-26 07:20:21 -06002743 struct fixed_file_table *table;
2744
2745 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2746 return table->files[index & IORING_FILE_TABLE_MASK];
2747}
2748
Jackie Liua197f662019-11-08 08:09:12 -07002749static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002750{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002751 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002752 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002753 unsigned flags;
2754 int fd;
2755
2756 flags = READ_ONCE(s->sqe->flags);
2757 fd = READ_ONCE(s->sqe->fd);
2758
Jackie Liu4fe2c962019-09-09 20:50:40 +08002759 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002760 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002761 /*
2762 * All io need record the previous position, if LINK vs DARIN,
2763 * it can be used to mark the position of the first IO in the
2764 * link list.
2765 */
2766 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002767
Jens Axboe60c112b2019-06-21 10:20:18 -06002768 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002769 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002770
2771 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002772 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002773 (unsigned) fd >= ctx->nr_user_files))
2774 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002775 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002776 req->file = io_file_from_index(ctx, fd);
2777 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002778 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002779 req->flags |= REQ_F_FIXED_FILE;
2780 } else {
2781 if (s->needs_fixed_file)
2782 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002783 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002784 req->file = io_file_get(state, fd);
2785 if (unlikely(!req->file))
2786 return -EBADF;
2787 }
2788
2789 return 0;
2790}
2791
Jackie Liua197f662019-11-08 08:09:12 -07002792static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002793{
Jens Axboefcb323c2019-10-24 12:39:47 -06002794 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002795 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002796
2797 rcu_read_lock();
2798 spin_lock_irq(&ctx->inflight_lock);
2799 /*
2800 * We use the f_ops->flush() handler to ensure that we can flush
2801 * out work accessing these files if the fd is closed. Check if
2802 * the fd has changed since we started down this path, and disallow
2803 * this operation if it has.
2804 */
2805 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2806 list_add(&req->inflight_entry, &ctx->inflight_list);
2807 req->flags |= REQ_F_INFLIGHT;
2808 req->work.files = current->files;
2809 ret = 0;
2810 }
2811 spin_unlock_irq(&ctx->inflight_lock);
2812 rcu_read_unlock();
2813
2814 return ret;
2815}
2816
Jens Axboe2665abf2019-11-05 12:40:47 -07002817static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2818{
Jens Axboead8a48a2019-11-15 08:49:11 -07002819 struct io_timeout_data *data = container_of(timer,
2820 struct io_timeout_data, timer);
2821 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002822 struct io_ring_ctx *ctx = req->ctx;
2823 struct io_kiocb *prev = NULL;
2824 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002825
2826 spin_lock_irqsave(&ctx->completion_lock, flags);
2827
2828 /*
2829 * We don't expect the list to be empty, that will only happen if we
2830 * race with the completion of the linked work.
2831 */
2832 if (!list_empty(&req->list)) {
2833 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07002834 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002835 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07002836 prev->flags &= ~REQ_F_LINK_TIMEOUT;
2837 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07002838 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002839 }
2840
2841 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2842
2843 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07002844 if (prev->flags & REQ_F_LINK)
2845 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002846 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2847 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07002848 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002849 } else {
2850 io_cqring_add_event(req, -ETIME);
2851 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002852 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002853 return HRTIMER_NORESTART;
2854}
2855
Jens Axboead8a48a2019-11-15 08:49:11 -07002856static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002857{
Jens Axboe76a46e02019-11-10 23:34:16 -07002858 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002859
Jens Axboe76a46e02019-11-10 23:34:16 -07002860 /*
2861 * If the list is now empty, then our linked request finished before
2862 * we got a chance to setup the timer
2863 */
2864 spin_lock_irq(&ctx->completion_lock);
2865 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002866 struct io_timeout_data *data = req->timeout.data;
2867
Jens Axboead8a48a2019-11-15 08:49:11 -07002868 data->timer.function = io_link_timeout_fn;
2869 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2870 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002871 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002872 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002873
Jens Axboe2665abf2019-11-05 12:40:47 -07002874 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002875 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002876}
2877
Jens Axboead8a48a2019-11-15 08:49:11 -07002878static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002879{
2880 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002881
Jens Axboe2665abf2019-11-05 12:40:47 -07002882 if (!(req->flags & REQ_F_LINK))
2883 return NULL;
2884
2885 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002886 if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT)
2887 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002888
Jens Axboe76a46e02019-11-10 23:34:16 -07002889 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07002890 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002891}
2892
Jens Axboe0e0702d2019-11-14 21:42:10 -07002893static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002894{
Jens Axboe94ae5e72019-11-14 19:39:52 -07002895 struct io_kiocb *nxt = io_prep_linked_timeout(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002896 int ret;
2897
Jackie Liua197f662019-11-08 08:09:12 -07002898 ret = __io_submit_sqe(req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002899
2900 /*
2901 * We async punt it if the file wasn't marked NOWAIT, or if the file
2902 * doesn't support non-blocking read/write attempts
2903 */
2904 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2905 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002906 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002907 struct io_uring_sqe *sqe_copy;
2908
Jackie Liu954dab12019-09-18 10:37:52 +08002909 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002910 if (!sqe_copy)
2911 goto err;
Jens Axboee65ef562019-03-12 10:16:44 -06002912
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002913 s->sqe = sqe_copy;
2914 req->flags |= REQ_F_FREE_SQE;
2915
2916 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2917 ret = io_grab_files(req);
2918 if (ret)
2919 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002920 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002921
2922 /*
2923 * Queued up for async execution, worker will release
2924 * submit reference when the iocb is actually submitted.
2925 */
2926 io_queue_async_work(req);
2927 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002928 }
Jens Axboee65ef562019-03-12 10:16:44 -06002929
Jens Axboefcb323c2019-10-24 12:39:47 -06002930err:
Jens Axboee65ef562019-03-12 10:16:44 -06002931 /* drop submission reference */
2932 io_put_req(req);
2933
Jens Axboe76a46e02019-11-10 23:34:16 -07002934 if (nxt) {
2935 if (!ret)
Jens Axboead8a48a2019-11-15 08:49:11 -07002936 io_queue_linked_timeout(nxt);
Jens Axboe76a46e02019-11-10 23:34:16 -07002937 else
2938 io_put_req(nxt);
2939 }
2940
Jens Axboee65ef562019-03-12 10:16:44 -06002941 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002942 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002943 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002944 if (req->flags & REQ_F_LINK)
2945 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002946 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002947 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002948}
2949
Jens Axboe0e0702d2019-11-14 21:42:10 -07002950static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002951{
2952 int ret;
2953
Jackie Liua197f662019-11-08 08:09:12 -07002954 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002955 if (ret) {
2956 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002957 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03002958 if (req->flags & REQ_F_LINK)
2959 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002960 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002961 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07002962 } else
2963 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002964}
2965
Jens Axboe0e0702d2019-11-14 21:42:10 -07002966static void io_queue_link_head(struct io_kiocb *req, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002967{
2968 int ret;
2969 int need_submit = false;
Jackie Liua197f662019-11-08 08:09:12 -07002970 struct io_ring_ctx *ctx = req->ctx;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002971
Jens Axboe94ae5e72019-11-14 19:39:52 -07002972 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
2973 ret = -ECANCELED;
2974 goto err;
2975 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07002976 if (!shadow) {
2977 io_queue_sqe(req);
2978 return;
2979 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08002980
2981 /*
2982 * Mark the first IO in link list as DRAIN, let all the following
2983 * IOs enter the defer list. all IO needs to be completed before link
2984 * list.
2985 */
2986 req->flags |= REQ_F_IO_DRAIN;
Jackie Liua197f662019-11-08 08:09:12 -07002987 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002988 if (ret) {
2989 if (ret != -EIOCBQUEUED) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002990err:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002991 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03002992 if (req->flags & REQ_F_LINK)
2993 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002994 io_double_put_req(req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07002995 if (shadow)
2996 __io_free_req(shadow);
Jens Axboe0e0702d2019-11-14 21:42:10 -07002997 return;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002998 }
2999 } else {
3000 /*
3001 * If ret == 0 means that all IOs in front of link io are
3002 * running done. let's queue link head.
3003 */
3004 need_submit = true;
3005 }
3006
3007 /* Insert shadow req to defer_list, blocking next IOs */
3008 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003009 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003010 list_add_tail(&shadow->list, &ctx->defer_list);
3011 spin_unlock_irq(&ctx->completion_lock);
3012
3013 if (need_submit)
Jens Axboe0e0702d2019-11-14 21:42:10 -07003014 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003015}
3016
Jens Axboe9e645e112019-05-10 16:07:28 -06003017#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3018
Jackie Liua197f662019-11-08 08:09:12 -07003019static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3020 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003021{
Pavel Begunkov267bc902019-11-07 01:41:08 +03003022 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07003023 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003024 int ret;
3025
Jens Axboe78e19bb2019-11-06 15:21:34 -07003026 req->user_data = s->sqe->user_data;
3027
Jens Axboe9e645e112019-05-10 16:07:28 -06003028 /* enforce forwards compatibility on users */
3029 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
3030 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003031 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003032 }
3033
Jackie Liua197f662019-11-08 08:09:12 -07003034 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003035 if (unlikely(ret)) {
3036err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003037 io_cqring_add_event(req, ret);
3038 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003039 return;
3040 }
3041
Jens Axboe9e645e112019-05-10 16:07:28 -06003042 /*
3043 * If we already have a head request, queue this one for async
3044 * submittal once the head completes. If we don't have a head but
3045 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3046 * submitted sync once the chain is complete. If none of those
3047 * conditions are true (normal request), then just queue it.
3048 */
3049 if (*link) {
3050 struct io_kiocb *prev = *link;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003051 struct io_uring_sqe *sqe_copy;
Jens Axboe9e645e112019-05-10 16:07:28 -06003052
Jens Axboe94ae5e72019-11-14 19:39:52 -07003053 if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
3054 ret = io_timeout_setup(req);
3055 /* common setup allows offset being set, we don't */
3056 if (!ret && s->sqe->off)
3057 ret = -EINVAL;
3058 if (ret) {
3059 prev->flags |= REQ_F_FAIL_LINK;
3060 goto err_req;
3061 }
3062 }
3063
Jens Axboe9e645e112019-05-10 16:07:28 -06003064 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
3065 if (!sqe_copy) {
3066 ret = -EAGAIN;
3067 goto err_req;
3068 }
3069
3070 s->sqe = sqe_copy;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003071 req->flags |= REQ_F_FREE_SQE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003072 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003073 list_add_tail(&req->list, &prev->link_list);
3074 } else if (s->sqe->flags & IOSQE_IO_LINK) {
3075 req->flags |= REQ_F_LINK;
3076
Jens Axboe9e645e112019-05-10 16:07:28 -06003077 INIT_LIST_HEAD(&req->link_list);
3078 *link = req;
3079 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003080 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003081 }
3082}
3083
Jens Axboe9a56a232019-01-09 09:06:50 -07003084/*
3085 * Batched submission is done, ensure local IO is flushed out.
3086 */
3087static void io_submit_state_end(struct io_submit_state *state)
3088{
3089 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003090 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003091 if (state->free_reqs)
3092 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3093 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003094}
3095
3096/*
3097 * Start submission side cache.
3098 */
3099static void io_submit_state_start(struct io_submit_state *state,
3100 struct io_ring_ctx *ctx, unsigned max_ios)
3101{
3102 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003103 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003104 state->file = NULL;
3105 state->ios_left = max_ios;
3106}
3107
Jens Axboe2b188cc2019-01-07 10:46:33 -07003108static void io_commit_sqring(struct io_ring_ctx *ctx)
3109{
Hristo Venev75b28af2019-08-26 17:23:46 +00003110 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003111
Hristo Venev75b28af2019-08-26 17:23:46 +00003112 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003113 /*
3114 * Ensure any loads from the SQEs are done at this point,
3115 * since once we write the new head, the application could
3116 * write new data to them.
3117 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003118 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003119 }
3120}
3121
3122/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003123 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3124 * that is mapped by userspace. This means that care needs to be taken to
3125 * ensure that reads are stable, as we cannot rely on userspace always
3126 * being a good citizen. If members of the sqe are validated and then later
3127 * used, it's important that those reads are done through READ_ONCE() to
3128 * prevent a re-load down the line.
3129 */
3130static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
3131{
Hristo Venev75b28af2019-08-26 17:23:46 +00003132 struct io_rings *rings = ctx->rings;
3133 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003134 unsigned head;
3135
3136 /*
3137 * The cached sq head (or cq tail) serves two purposes:
3138 *
3139 * 1) allows us to batch the cost of updating the user visible
3140 * head updates.
3141 * 2) allows the kernel side to track the head on its own, even
3142 * though the application is the one updating it.
3143 */
3144 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003145 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00003146 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003147 return false;
3148
Hristo Venev75b28af2019-08-26 17:23:46 +00003149 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003150 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003151 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003152 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003153 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003154 ctx->cached_sq_head++;
3155 return true;
3156 }
3157
3158 /* drop invalid entries */
3159 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003160 ctx->cached_sq_dropped++;
3161 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003162 return false;
3163}
3164
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003165static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003166 struct file *ring_file, int ring_fd,
3167 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003168{
3169 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003170 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003171 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003172 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003173 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003174
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003175 if (!list_empty(&ctx->cq_overflow_list)) {
3176 io_cqring_overflow_flush(ctx, false);
3177 return -EBUSY;
3178 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003179
3180 if (nr > IO_PLUG_THRESHOLD) {
3181 io_submit_state_start(&state, ctx, nr);
3182 statep = &state;
3183 }
3184
3185 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003186 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003187 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003188
Pavel Begunkov196be952019-11-07 01:41:06 +03003189 req = io_get_req(ctx, statep);
3190 if (unlikely(!req)) {
3191 if (!submitted)
3192 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003193 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003194 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003195 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003196 __io_free_req(req);
3197 break;
3198 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003199
Pavel Begunkov50585b92019-11-07 01:41:07 +03003200 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003201 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3202 if (!mm_fault) {
3203 use_mm(ctx->sqo_mm);
3204 *mm = ctx->sqo_mm;
3205 }
3206 }
3207
Pavel Begunkov50585b92019-11-07 01:41:07 +03003208 sqe_flags = req->submit.sqe->flags;
3209
3210 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08003211 if (!shadow_req) {
3212 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08003213 if (unlikely(!shadow_req))
3214 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003215 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
3216 refcount_dec(&shadow_req->refs);
3217 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003218 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003219 }
3220
Jackie Liua1041c22019-09-18 17:25:52 +08003221out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03003222 req->submit.ring_file = ring_file;
3223 req->submit.ring_fd = ring_fd;
3224 req->submit.has_user = *mm != NULL;
3225 req->submit.in_async = async;
3226 req->submit.needs_fixed_file = async;
3227 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3228 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003229 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003230 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003231
3232 /*
3233 * If previous wasn't linked and we have a linked command,
3234 * that's the end of the chain. Submit the previous link.
3235 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003236 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Jackie Liua197f662019-11-08 08:09:12 -07003237 io_queue_link_head(link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003238 link = NULL;
3239 shadow_req = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003240 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003241 }
3242
Jens Axboe9e645e112019-05-10 16:07:28 -06003243 if (link)
Jackie Liua197f662019-11-08 08:09:12 -07003244 io_queue_link_head(link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003245 if (statep)
3246 io_submit_state_end(&state);
3247
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003248 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3249 io_commit_sqring(ctx);
3250
Jens Axboe6c271ce2019-01-10 11:22:30 -07003251 return submitted;
3252}
3253
3254static int io_sq_thread(void *data)
3255{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003256 struct io_ring_ctx *ctx = data;
3257 struct mm_struct *cur_mm = NULL;
3258 mm_segment_t old_fs;
3259 DEFINE_WAIT(wait);
3260 unsigned inflight;
3261 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003262 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003263
Jens Axboe206aefd2019-11-07 18:27:42 -07003264 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003265
Jens Axboe6c271ce2019-01-10 11:22:30 -07003266 old_fs = get_fs();
3267 set_fs(USER_DS);
3268
Jens Axboec1edbf52019-11-10 16:56:04 -07003269 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003270 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003271 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003272
3273 if (inflight) {
3274 unsigned nr_events = 0;
3275
3276 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003277 /*
3278 * inflight is the count of the maximum possible
3279 * entries we submitted, but it can be smaller
3280 * if we dropped some of them. If we don't have
3281 * poll entries available, then we know that we
3282 * have nothing left to poll for. Reset the
3283 * inflight count to zero in that case.
3284 */
3285 mutex_lock(&ctx->uring_lock);
3286 if (!list_empty(&ctx->poll_list))
3287 __io_iopoll_check(ctx, &nr_events, 0);
3288 else
3289 inflight = 0;
3290 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003291 } else {
3292 /*
3293 * Normal IO, just pretend everything completed.
3294 * We don't have to poll completions for that.
3295 */
3296 nr_events = inflight;
3297 }
3298
3299 inflight -= nr_events;
3300 if (!inflight)
3301 timeout = jiffies + ctx->sq_thread_idle;
3302 }
3303
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003304 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003305
3306 /*
3307 * If submit got -EBUSY, flag us as needing the application
3308 * to enter the kernel to reap and flush events.
3309 */
3310 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003311 /*
3312 * We're polling. If we're within the defined idle
3313 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003314 * to sleep. The exception is if we got EBUSY doing
3315 * more IO, we should wait for the application to
3316 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003317 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003318 if (inflight ||
3319 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003320 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003321 continue;
3322 }
3323
3324 /*
3325 * Drop cur_mm before scheduling, we can't hold it for
3326 * long periods (or over schedule()). Do this before
3327 * adding ourselves to the waitqueue, as the unuse/drop
3328 * may sleep.
3329 */
3330 if (cur_mm) {
3331 unuse_mm(cur_mm);
3332 mmput(cur_mm);
3333 cur_mm = NULL;
3334 }
3335
3336 prepare_to_wait(&ctx->sqo_wait, &wait,
3337 TASK_INTERRUPTIBLE);
3338
3339 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003340 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003341 /* make sure to read SQ tail after writing flags */
3342 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003343
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003344 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003345 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003346 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003347 finish_wait(&ctx->sqo_wait, &wait);
3348 break;
3349 }
3350 if (signal_pending(current))
3351 flush_signals(current);
3352 schedule();
3353 finish_wait(&ctx->sqo_wait, &wait);
3354
Hristo Venev75b28af2019-08-26 17:23:46 +00003355 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003356 continue;
3357 }
3358 finish_wait(&ctx->sqo_wait, &wait);
3359
Hristo Venev75b28af2019-08-26 17:23:46 +00003360 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003361 }
3362
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003363 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003364 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3365 if (ret > 0)
3366 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003367 }
3368
3369 set_fs(old_fs);
3370 if (cur_mm) {
3371 unuse_mm(cur_mm);
3372 mmput(cur_mm);
3373 }
Jens Axboe06058632019-04-13 09:26:03 -06003374
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003375 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003376
Jens Axboe6c271ce2019-01-10 11:22:30 -07003377 return 0;
3378}
3379
Jens Axboebda52162019-09-24 13:47:15 -06003380struct io_wait_queue {
3381 struct wait_queue_entry wq;
3382 struct io_ring_ctx *ctx;
3383 unsigned to_wait;
3384 unsigned nr_timeouts;
3385};
3386
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003387static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003388{
3389 struct io_ring_ctx *ctx = iowq->ctx;
3390
3391 /*
3392 * Wake up if we have enough events, or if a timeout occured since we
3393 * started waiting. For timeouts, we always want to return to userspace,
3394 * regardless of event count.
3395 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003396 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003397 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3398}
3399
3400static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3401 int wake_flags, void *key)
3402{
3403 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3404 wq);
3405
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003406 /* use noflush == true, as we can't safely rely on locking context */
3407 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003408 return -1;
3409
3410 return autoremove_wake_function(curr, mode, wake_flags, key);
3411}
3412
Jens Axboe2b188cc2019-01-07 10:46:33 -07003413/*
3414 * Wait until events become available, if we don't already have some. The
3415 * application must reap them itself, as they reside on the shared cq ring.
3416 */
3417static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3418 const sigset_t __user *sig, size_t sigsz)
3419{
Jens Axboebda52162019-09-24 13:47:15 -06003420 struct io_wait_queue iowq = {
3421 .wq = {
3422 .private = current,
3423 .func = io_wake_function,
3424 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3425 },
3426 .ctx = ctx,
3427 .to_wait = min_events,
3428 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003429 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003430 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003431
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003432 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003433 return 0;
3434
3435 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003436#ifdef CONFIG_COMPAT
3437 if (in_compat_syscall())
3438 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003439 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003440 else
3441#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003442 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003443
Jens Axboe2b188cc2019-01-07 10:46:33 -07003444 if (ret)
3445 return ret;
3446 }
3447
Jens Axboebda52162019-09-24 13:47:15 -06003448 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003449 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003450 do {
3451 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3452 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003453 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003454 break;
3455 schedule();
3456 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003457 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003458 break;
3459 }
3460 } while (1);
3461 finish_wait(&ctx->wait, &iowq.wq);
3462
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003463 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003464
Hristo Venev75b28af2019-08-26 17:23:46 +00003465 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003466}
3467
Jens Axboe6b063142019-01-10 22:13:58 -07003468static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3469{
3470#if defined(CONFIG_UNIX)
3471 if (ctx->ring_sock) {
3472 struct sock *sock = ctx->ring_sock->sk;
3473 struct sk_buff *skb;
3474
3475 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3476 kfree_skb(skb);
3477 }
3478#else
3479 int i;
3480
Jens Axboe65e19f52019-10-26 07:20:21 -06003481 for (i = 0; i < ctx->nr_user_files; i++) {
3482 struct file *file;
3483
3484 file = io_file_from_index(ctx, i);
3485 if (file)
3486 fput(file);
3487 }
Jens Axboe6b063142019-01-10 22:13:58 -07003488#endif
3489}
3490
3491static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3492{
Jens Axboe65e19f52019-10-26 07:20:21 -06003493 unsigned nr_tables, i;
3494
3495 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003496 return -ENXIO;
3497
3498 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003499 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3500 for (i = 0; i < nr_tables; i++)
3501 kfree(ctx->file_table[i].files);
3502 kfree(ctx->file_table);
3503 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003504 ctx->nr_user_files = 0;
3505 return 0;
3506}
3507
Jens Axboe6c271ce2019-01-10 11:22:30 -07003508static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3509{
3510 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003511 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003512 /*
3513 * The park is a bit of a work-around, without it we get
3514 * warning spews on shutdown with SQPOLL set and affinity
3515 * set to a single CPU.
3516 */
Jens Axboe06058632019-04-13 09:26:03 -06003517 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003518 kthread_stop(ctx->sqo_thread);
3519 ctx->sqo_thread = NULL;
3520 }
3521}
3522
Jens Axboe6b063142019-01-10 22:13:58 -07003523static void io_finish_async(struct io_ring_ctx *ctx)
3524{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003525 io_sq_thread_stop(ctx);
3526
Jens Axboe561fb042019-10-24 07:25:42 -06003527 if (ctx->io_wq) {
3528 io_wq_destroy(ctx->io_wq);
3529 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003530 }
3531}
3532
3533#if defined(CONFIG_UNIX)
3534static void io_destruct_skb(struct sk_buff *skb)
3535{
3536 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3537
Jens Axboe561fb042019-10-24 07:25:42 -06003538 if (ctx->io_wq)
3539 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003540
Jens Axboe6b063142019-01-10 22:13:58 -07003541 unix_destruct_scm(skb);
3542}
3543
3544/*
3545 * Ensure the UNIX gc is aware of our file set, so we are certain that
3546 * the io_uring can be safely unregistered on process exit, even if we have
3547 * loops in the file referencing.
3548 */
3549static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3550{
3551 struct sock *sk = ctx->ring_sock->sk;
3552 struct scm_fp_list *fpl;
3553 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003554 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003555
3556 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3557 unsigned long inflight = ctx->user->unix_inflight + nr;
3558
3559 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3560 return -EMFILE;
3561 }
3562
3563 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3564 if (!fpl)
3565 return -ENOMEM;
3566
3567 skb = alloc_skb(0, GFP_KERNEL);
3568 if (!skb) {
3569 kfree(fpl);
3570 return -ENOMEM;
3571 }
3572
3573 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003574
Jens Axboe08a45172019-10-03 08:11:03 -06003575 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003576 fpl->user = get_uid(ctx->user);
3577 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003578 struct file *file = io_file_from_index(ctx, i + offset);
3579
3580 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003581 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003582 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003583 unix_inflight(fpl->user, fpl->fp[nr_files]);
3584 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003585 }
3586
Jens Axboe08a45172019-10-03 08:11:03 -06003587 if (nr_files) {
3588 fpl->max = SCM_MAX_FD;
3589 fpl->count = nr_files;
3590 UNIXCB(skb).fp = fpl;
3591 skb->destructor = io_destruct_skb;
3592 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3593 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003594
Jens Axboe08a45172019-10-03 08:11:03 -06003595 for (i = 0; i < nr_files; i++)
3596 fput(fpl->fp[i]);
3597 } else {
3598 kfree_skb(skb);
3599 kfree(fpl);
3600 }
Jens Axboe6b063142019-01-10 22:13:58 -07003601
3602 return 0;
3603}
3604
3605/*
3606 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3607 * causes regular reference counting to break down. We rely on the UNIX
3608 * garbage collection to take care of this problem for us.
3609 */
3610static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3611{
3612 unsigned left, total;
3613 int ret = 0;
3614
3615 total = 0;
3616 left = ctx->nr_user_files;
3617 while (left) {
3618 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003619
3620 ret = __io_sqe_files_scm(ctx, this_files, total);
3621 if (ret)
3622 break;
3623 left -= this_files;
3624 total += this_files;
3625 }
3626
3627 if (!ret)
3628 return 0;
3629
3630 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003631 struct file *file = io_file_from_index(ctx, total);
3632
3633 if (file)
3634 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003635 total++;
3636 }
3637
3638 return ret;
3639}
3640#else
3641static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3642{
3643 return 0;
3644}
3645#endif
3646
Jens Axboe65e19f52019-10-26 07:20:21 -06003647static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3648 unsigned nr_files)
3649{
3650 int i;
3651
3652 for (i = 0; i < nr_tables; i++) {
3653 struct fixed_file_table *table = &ctx->file_table[i];
3654 unsigned this_files;
3655
3656 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3657 table->files = kcalloc(this_files, sizeof(struct file *),
3658 GFP_KERNEL);
3659 if (!table->files)
3660 break;
3661 nr_files -= this_files;
3662 }
3663
3664 if (i == nr_tables)
3665 return 0;
3666
3667 for (i = 0; i < nr_tables; i++) {
3668 struct fixed_file_table *table = &ctx->file_table[i];
3669 kfree(table->files);
3670 }
3671 return 1;
3672}
3673
Jens Axboe6b063142019-01-10 22:13:58 -07003674static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3675 unsigned nr_args)
3676{
3677 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003678 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003679 int fd, ret = 0;
3680 unsigned i;
3681
Jens Axboe65e19f52019-10-26 07:20:21 -06003682 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003683 return -EBUSY;
3684 if (!nr_args)
3685 return -EINVAL;
3686 if (nr_args > IORING_MAX_FIXED_FILES)
3687 return -EMFILE;
3688
Jens Axboe65e19f52019-10-26 07:20:21 -06003689 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3690 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3691 GFP_KERNEL);
3692 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003693 return -ENOMEM;
3694
Jens Axboe65e19f52019-10-26 07:20:21 -06003695 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3696 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003697 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003698 return -ENOMEM;
3699 }
3700
Jens Axboe08a45172019-10-03 08:11:03 -06003701 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003702 struct fixed_file_table *table;
3703 unsigned index;
3704
Jens Axboe6b063142019-01-10 22:13:58 -07003705 ret = -EFAULT;
3706 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3707 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003708 /* allow sparse sets */
3709 if (fd == -1) {
3710 ret = 0;
3711 continue;
3712 }
Jens Axboe6b063142019-01-10 22:13:58 -07003713
Jens Axboe65e19f52019-10-26 07:20:21 -06003714 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3715 index = i & IORING_FILE_TABLE_MASK;
3716 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003717
3718 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003719 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003720 break;
3721 /*
3722 * Don't allow io_uring instances to be registered. If UNIX
3723 * isn't enabled, then this causes a reference cycle and this
3724 * instance can never get freed. If UNIX is enabled we'll
3725 * handle it just fine, but there's still no point in allowing
3726 * a ring fd as it doesn't support regular read/write anyway.
3727 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003728 if (table->files[index]->f_op == &io_uring_fops) {
3729 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003730 break;
3731 }
Jens Axboe6b063142019-01-10 22:13:58 -07003732 ret = 0;
3733 }
3734
3735 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003736 for (i = 0; i < ctx->nr_user_files; i++) {
3737 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003738
Jens Axboe65e19f52019-10-26 07:20:21 -06003739 file = io_file_from_index(ctx, i);
3740 if (file)
3741 fput(file);
3742 }
3743 for (i = 0; i < nr_tables; i++)
3744 kfree(ctx->file_table[i].files);
3745
3746 kfree(ctx->file_table);
3747 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003748 ctx->nr_user_files = 0;
3749 return ret;
3750 }
3751
3752 ret = io_sqe_files_scm(ctx);
3753 if (ret)
3754 io_sqe_files_unregister(ctx);
3755
3756 return ret;
3757}
3758
Jens Axboec3a31e62019-10-03 13:59:56 -06003759static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3760{
3761#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003762 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003763 struct sock *sock = ctx->ring_sock->sk;
3764 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3765 struct sk_buff *skb;
3766 int i;
3767
3768 __skb_queue_head_init(&list);
3769
3770 /*
3771 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3772 * remove this entry and rearrange the file array.
3773 */
3774 skb = skb_dequeue(head);
3775 while (skb) {
3776 struct scm_fp_list *fp;
3777
3778 fp = UNIXCB(skb).fp;
3779 for (i = 0; i < fp->count; i++) {
3780 int left;
3781
3782 if (fp->fp[i] != file)
3783 continue;
3784
3785 unix_notinflight(fp->user, fp->fp[i]);
3786 left = fp->count - 1 - i;
3787 if (left) {
3788 memmove(&fp->fp[i], &fp->fp[i + 1],
3789 left * sizeof(struct file *));
3790 }
3791 fp->count--;
3792 if (!fp->count) {
3793 kfree_skb(skb);
3794 skb = NULL;
3795 } else {
3796 __skb_queue_tail(&list, skb);
3797 }
3798 fput(file);
3799 file = NULL;
3800 break;
3801 }
3802
3803 if (!file)
3804 break;
3805
3806 __skb_queue_tail(&list, skb);
3807
3808 skb = skb_dequeue(head);
3809 }
3810
3811 if (skb_peek(&list)) {
3812 spin_lock_irq(&head->lock);
3813 while ((skb = __skb_dequeue(&list)) != NULL)
3814 __skb_queue_tail(head, skb);
3815 spin_unlock_irq(&head->lock);
3816 }
3817#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003818 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003819#endif
3820}
3821
3822static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3823 int index)
3824{
3825#if defined(CONFIG_UNIX)
3826 struct sock *sock = ctx->ring_sock->sk;
3827 struct sk_buff_head *head = &sock->sk_receive_queue;
3828 struct sk_buff *skb;
3829
3830 /*
3831 * See if we can merge this file into an existing skb SCM_RIGHTS
3832 * file set. If there's no room, fall back to allocating a new skb
3833 * and filling it in.
3834 */
3835 spin_lock_irq(&head->lock);
3836 skb = skb_peek(head);
3837 if (skb) {
3838 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3839
3840 if (fpl->count < SCM_MAX_FD) {
3841 __skb_unlink(skb, head);
3842 spin_unlock_irq(&head->lock);
3843 fpl->fp[fpl->count] = get_file(file);
3844 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3845 fpl->count++;
3846 spin_lock_irq(&head->lock);
3847 __skb_queue_head(head, skb);
3848 } else {
3849 skb = NULL;
3850 }
3851 }
3852 spin_unlock_irq(&head->lock);
3853
3854 if (skb) {
3855 fput(file);
3856 return 0;
3857 }
3858
3859 return __io_sqe_files_scm(ctx, 1, index);
3860#else
3861 return 0;
3862#endif
3863}
3864
3865static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3866 unsigned nr_args)
3867{
3868 struct io_uring_files_update up;
3869 __s32 __user *fds;
3870 int fd, i, err;
3871 __u32 done;
3872
Jens Axboe65e19f52019-10-26 07:20:21 -06003873 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003874 return -ENXIO;
3875 if (!nr_args)
3876 return -EINVAL;
3877 if (copy_from_user(&up, arg, sizeof(up)))
3878 return -EFAULT;
3879 if (check_add_overflow(up.offset, nr_args, &done))
3880 return -EOVERFLOW;
3881 if (done > ctx->nr_user_files)
3882 return -EINVAL;
3883
3884 done = 0;
3885 fds = (__s32 __user *) up.fds;
3886 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003887 struct fixed_file_table *table;
3888 unsigned index;
3889
Jens Axboec3a31e62019-10-03 13:59:56 -06003890 err = 0;
3891 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3892 err = -EFAULT;
3893 break;
3894 }
3895 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003896 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3897 index = i & IORING_FILE_TABLE_MASK;
3898 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003899 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003900 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003901 }
3902 if (fd != -1) {
3903 struct file *file;
3904
3905 file = fget(fd);
3906 if (!file) {
3907 err = -EBADF;
3908 break;
3909 }
3910 /*
3911 * Don't allow io_uring instances to be registered. If
3912 * UNIX isn't enabled, then this causes a reference
3913 * cycle and this instance can never get freed. If UNIX
3914 * is enabled we'll handle it just fine, but there's
3915 * still no point in allowing a ring fd as it doesn't
3916 * support regular read/write anyway.
3917 */
3918 if (file->f_op == &io_uring_fops) {
3919 fput(file);
3920 err = -EBADF;
3921 break;
3922 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003923 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003924 err = io_sqe_file_register(ctx, file, i);
3925 if (err)
3926 break;
3927 }
3928 nr_args--;
3929 done++;
3930 up.offset++;
3931 }
3932
3933 return done ? done : err;
3934}
3935
Jens Axboe7d723062019-11-12 22:31:31 -07003936static void io_put_work(struct io_wq_work *work)
3937{
3938 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3939
3940 io_put_req(req);
3941}
3942
3943static void io_get_work(struct io_wq_work *work)
3944{
3945 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3946
3947 refcount_inc(&req->refs);
3948}
3949
Jens Axboe6c271ce2019-01-10 11:22:30 -07003950static int io_sq_offload_start(struct io_ring_ctx *ctx,
3951 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003952{
Jens Axboe561fb042019-10-24 07:25:42 -06003953 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003954 int ret;
3955
Jens Axboe6c271ce2019-01-10 11:22:30 -07003956 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003957 mmgrab(current->mm);
3958 ctx->sqo_mm = current->mm;
3959
Jens Axboe6c271ce2019-01-10 11:22:30 -07003960 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003961 ret = -EPERM;
3962 if (!capable(CAP_SYS_ADMIN))
3963 goto err;
3964
Jens Axboe917257d2019-04-13 09:28:55 -06003965 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3966 if (!ctx->sq_thread_idle)
3967 ctx->sq_thread_idle = HZ;
3968
Jens Axboe6c271ce2019-01-10 11:22:30 -07003969 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003970 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003971
Jens Axboe917257d2019-04-13 09:28:55 -06003972 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003973 if (cpu >= nr_cpu_ids)
3974 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003975 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003976 goto err;
3977
Jens Axboe6c271ce2019-01-10 11:22:30 -07003978 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3979 ctx, cpu,
3980 "io_uring-sq");
3981 } else {
3982 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3983 "io_uring-sq");
3984 }
3985 if (IS_ERR(ctx->sqo_thread)) {
3986 ret = PTR_ERR(ctx->sqo_thread);
3987 ctx->sqo_thread = NULL;
3988 goto err;
3989 }
3990 wake_up_process(ctx->sqo_thread);
3991 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3992 /* Can't have SQ_AFF without SQPOLL */
3993 ret = -EINVAL;
3994 goto err;
3995 }
3996
Jens Axboe561fb042019-10-24 07:25:42 -06003997 /* Do QD, or 4 * CPUS, whatever is smallest */
3998 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe7d723062019-11-12 22:31:31 -07003999 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user,
4000 io_get_work, io_put_work);
Jens Axboe975c99a52019-10-30 08:42:56 -06004001 if (IS_ERR(ctx->io_wq)) {
4002 ret = PTR_ERR(ctx->io_wq);
4003 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004004 goto err;
4005 }
4006
4007 return 0;
4008err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004009 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004010 mmdrop(ctx->sqo_mm);
4011 ctx->sqo_mm = NULL;
4012 return ret;
4013}
4014
4015static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4016{
4017 atomic_long_sub(nr_pages, &user->locked_vm);
4018}
4019
4020static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4021{
4022 unsigned long page_limit, cur_pages, new_pages;
4023
4024 /* Don't allow more pages than we can safely lock */
4025 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4026
4027 do {
4028 cur_pages = atomic_long_read(&user->locked_vm);
4029 new_pages = cur_pages + nr_pages;
4030 if (new_pages > page_limit)
4031 return -ENOMEM;
4032 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4033 new_pages) != cur_pages);
4034
4035 return 0;
4036}
4037
4038static void io_mem_free(void *ptr)
4039{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004040 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004041
Mark Rutland52e04ef2019-04-30 17:30:21 +01004042 if (!ptr)
4043 return;
4044
4045 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004046 if (put_page_testzero(page))
4047 free_compound_page(page);
4048}
4049
4050static void *io_mem_alloc(size_t size)
4051{
4052 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4053 __GFP_NORETRY;
4054
4055 return (void *) __get_free_pages(gfp_flags, get_order(size));
4056}
4057
Hristo Venev75b28af2019-08-26 17:23:46 +00004058static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4059 size_t *sq_offset)
4060{
4061 struct io_rings *rings;
4062 size_t off, sq_array_size;
4063
4064 off = struct_size(rings, cqes, cq_entries);
4065 if (off == SIZE_MAX)
4066 return SIZE_MAX;
4067
4068#ifdef CONFIG_SMP
4069 off = ALIGN(off, SMP_CACHE_BYTES);
4070 if (off == 0)
4071 return SIZE_MAX;
4072#endif
4073
4074 sq_array_size = array_size(sizeof(u32), sq_entries);
4075 if (sq_array_size == SIZE_MAX)
4076 return SIZE_MAX;
4077
4078 if (check_add_overflow(off, sq_array_size, &off))
4079 return SIZE_MAX;
4080
4081 if (sq_offset)
4082 *sq_offset = off;
4083
4084 return off;
4085}
4086
Jens Axboe2b188cc2019-01-07 10:46:33 -07004087static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4088{
Hristo Venev75b28af2019-08-26 17:23:46 +00004089 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004090
Hristo Venev75b28af2019-08-26 17:23:46 +00004091 pages = (size_t)1 << get_order(
4092 rings_size(sq_entries, cq_entries, NULL));
4093 pages += (size_t)1 << get_order(
4094 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004095
Hristo Venev75b28af2019-08-26 17:23:46 +00004096 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004097}
4098
Jens Axboeedafcce2019-01-09 09:16:05 -07004099static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4100{
4101 int i, j;
4102
4103 if (!ctx->user_bufs)
4104 return -ENXIO;
4105
4106 for (i = 0; i < ctx->nr_user_bufs; i++) {
4107 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4108
4109 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004110 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004111
4112 if (ctx->account_mem)
4113 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004114 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004115 imu->nr_bvecs = 0;
4116 }
4117
4118 kfree(ctx->user_bufs);
4119 ctx->user_bufs = NULL;
4120 ctx->nr_user_bufs = 0;
4121 return 0;
4122}
4123
4124static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4125 void __user *arg, unsigned index)
4126{
4127 struct iovec __user *src;
4128
4129#ifdef CONFIG_COMPAT
4130 if (ctx->compat) {
4131 struct compat_iovec __user *ciovs;
4132 struct compat_iovec ciov;
4133
4134 ciovs = (struct compat_iovec __user *) arg;
4135 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4136 return -EFAULT;
4137
4138 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4139 dst->iov_len = ciov.iov_len;
4140 return 0;
4141 }
4142#endif
4143 src = (struct iovec __user *) arg;
4144 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4145 return -EFAULT;
4146 return 0;
4147}
4148
4149static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4150 unsigned nr_args)
4151{
4152 struct vm_area_struct **vmas = NULL;
4153 struct page **pages = NULL;
4154 int i, j, got_pages = 0;
4155 int ret = -EINVAL;
4156
4157 if (ctx->user_bufs)
4158 return -EBUSY;
4159 if (!nr_args || nr_args > UIO_MAXIOV)
4160 return -EINVAL;
4161
4162 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4163 GFP_KERNEL);
4164 if (!ctx->user_bufs)
4165 return -ENOMEM;
4166
4167 for (i = 0; i < nr_args; i++) {
4168 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4169 unsigned long off, start, end, ubuf;
4170 int pret, nr_pages;
4171 struct iovec iov;
4172 size_t size;
4173
4174 ret = io_copy_iov(ctx, &iov, arg, i);
4175 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004176 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004177
4178 /*
4179 * Don't impose further limits on the size and buffer
4180 * constraints here, we'll -EINVAL later when IO is
4181 * submitted if they are wrong.
4182 */
4183 ret = -EFAULT;
4184 if (!iov.iov_base || !iov.iov_len)
4185 goto err;
4186
4187 /* arbitrary limit, but we need something */
4188 if (iov.iov_len > SZ_1G)
4189 goto err;
4190
4191 ubuf = (unsigned long) iov.iov_base;
4192 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4193 start = ubuf >> PAGE_SHIFT;
4194 nr_pages = end - start;
4195
4196 if (ctx->account_mem) {
4197 ret = io_account_mem(ctx->user, nr_pages);
4198 if (ret)
4199 goto err;
4200 }
4201
4202 ret = 0;
4203 if (!pages || nr_pages > got_pages) {
4204 kfree(vmas);
4205 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004206 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004207 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004208 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004209 sizeof(struct vm_area_struct *),
4210 GFP_KERNEL);
4211 if (!pages || !vmas) {
4212 ret = -ENOMEM;
4213 if (ctx->account_mem)
4214 io_unaccount_mem(ctx->user, nr_pages);
4215 goto err;
4216 }
4217 got_pages = nr_pages;
4218 }
4219
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004220 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004221 GFP_KERNEL);
4222 ret = -ENOMEM;
4223 if (!imu->bvec) {
4224 if (ctx->account_mem)
4225 io_unaccount_mem(ctx->user, nr_pages);
4226 goto err;
4227 }
4228
4229 ret = 0;
4230 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004231 pret = get_user_pages(ubuf, nr_pages,
4232 FOLL_WRITE | FOLL_LONGTERM,
4233 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004234 if (pret == nr_pages) {
4235 /* don't support file backed memory */
4236 for (j = 0; j < nr_pages; j++) {
4237 struct vm_area_struct *vma = vmas[j];
4238
4239 if (vma->vm_file &&
4240 !is_file_hugepages(vma->vm_file)) {
4241 ret = -EOPNOTSUPP;
4242 break;
4243 }
4244 }
4245 } else {
4246 ret = pret < 0 ? pret : -EFAULT;
4247 }
4248 up_read(&current->mm->mmap_sem);
4249 if (ret) {
4250 /*
4251 * if we did partial map, or found file backed vmas,
4252 * release any pages we did get
4253 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004254 if (pret > 0)
4255 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004256 if (ctx->account_mem)
4257 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004258 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004259 goto err;
4260 }
4261
4262 off = ubuf & ~PAGE_MASK;
4263 size = iov.iov_len;
4264 for (j = 0; j < nr_pages; j++) {
4265 size_t vec_len;
4266
4267 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4268 imu->bvec[j].bv_page = pages[j];
4269 imu->bvec[j].bv_len = vec_len;
4270 imu->bvec[j].bv_offset = off;
4271 off = 0;
4272 size -= vec_len;
4273 }
4274 /* store original address for later verification */
4275 imu->ubuf = ubuf;
4276 imu->len = iov.iov_len;
4277 imu->nr_bvecs = nr_pages;
4278
4279 ctx->nr_user_bufs++;
4280 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004281 kvfree(pages);
4282 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004283 return 0;
4284err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004285 kvfree(pages);
4286 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004287 io_sqe_buffer_unregister(ctx);
4288 return ret;
4289}
4290
Jens Axboe9b402842019-04-11 11:45:41 -06004291static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4292{
4293 __s32 __user *fds = arg;
4294 int fd;
4295
4296 if (ctx->cq_ev_fd)
4297 return -EBUSY;
4298
4299 if (copy_from_user(&fd, fds, sizeof(*fds)))
4300 return -EFAULT;
4301
4302 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4303 if (IS_ERR(ctx->cq_ev_fd)) {
4304 int ret = PTR_ERR(ctx->cq_ev_fd);
4305 ctx->cq_ev_fd = NULL;
4306 return ret;
4307 }
4308
4309 return 0;
4310}
4311
4312static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4313{
4314 if (ctx->cq_ev_fd) {
4315 eventfd_ctx_put(ctx->cq_ev_fd);
4316 ctx->cq_ev_fd = NULL;
4317 return 0;
4318 }
4319
4320 return -ENXIO;
4321}
4322
Jens Axboe2b188cc2019-01-07 10:46:33 -07004323static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4324{
Jens Axboe6b063142019-01-10 22:13:58 -07004325 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004326 if (ctx->sqo_mm)
4327 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004328
4329 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004330 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004331 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004332 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004333
Jens Axboe2b188cc2019-01-07 10:46:33 -07004334#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004335 if (ctx->ring_sock) {
4336 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004337 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004338 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004339#endif
4340
Hristo Venev75b28af2019-08-26 17:23:46 +00004341 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004342 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004343
4344 percpu_ref_exit(&ctx->refs);
4345 if (ctx->account_mem)
4346 io_unaccount_mem(ctx->user,
4347 ring_pages(ctx->sq_entries, ctx->cq_entries));
4348 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004349 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004350 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004351 kfree(ctx);
4352}
4353
4354static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4355{
4356 struct io_ring_ctx *ctx = file->private_data;
4357 __poll_t mask = 0;
4358
4359 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004360 /*
4361 * synchronizes with barrier from wq_has_sleeper call in
4362 * io_commit_cqring
4363 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004364 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004365 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4366 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004367 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004368 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004369 mask |= EPOLLIN | EPOLLRDNORM;
4370
4371 return mask;
4372}
4373
4374static int io_uring_fasync(int fd, struct file *file, int on)
4375{
4376 struct io_ring_ctx *ctx = file->private_data;
4377
4378 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4379}
4380
4381static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4382{
4383 mutex_lock(&ctx->uring_lock);
4384 percpu_ref_kill(&ctx->refs);
4385 mutex_unlock(&ctx->uring_lock);
4386
Jens Axboe5262f562019-09-17 12:26:57 -06004387 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004388 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004389
4390 if (ctx->io_wq)
4391 io_wq_cancel_all(ctx->io_wq);
4392
Jens Axboedef596e2019-01-09 08:59:42 -07004393 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004394 /* if we failed setting up the ctx, we might not have any rings */
4395 if (ctx->rings)
4396 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004397 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004398 io_ring_ctx_free(ctx);
4399}
4400
4401static int io_uring_release(struct inode *inode, struct file *file)
4402{
4403 struct io_ring_ctx *ctx = file->private_data;
4404
4405 file->private_data = NULL;
4406 io_ring_ctx_wait_and_kill(ctx);
4407 return 0;
4408}
4409
Jens Axboefcb323c2019-10-24 12:39:47 -06004410static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4411 struct files_struct *files)
4412{
4413 struct io_kiocb *req;
4414 DEFINE_WAIT(wait);
4415
4416 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004417 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004418
4419 spin_lock_irq(&ctx->inflight_lock);
4420 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004421 if (req->work.files != files)
4422 continue;
4423 /* req is being completed, ignore */
4424 if (!refcount_inc_not_zero(&req->refs))
4425 continue;
4426 cancel_req = req;
4427 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004428 }
Jens Axboe768134d2019-11-10 20:30:53 -07004429 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004430 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004431 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004432 spin_unlock_irq(&ctx->inflight_lock);
4433
Jens Axboe768134d2019-11-10 20:30:53 -07004434 /* We need to keep going until we don't find a matching req */
4435 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004436 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004437
4438 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4439 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004440 schedule();
4441 }
Jens Axboe768134d2019-11-10 20:30:53 -07004442 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004443}
4444
4445static int io_uring_flush(struct file *file, void *data)
4446{
4447 struct io_ring_ctx *ctx = file->private_data;
4448
4449 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004450 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4451 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004452 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004453 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004454 return 0;
4455}
4456
Jens Axboe2b188cc2019-01-07 10:46:33 -07004457static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4458{
4459 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4460 unsigned long sz = vma->vm_end - vma->vm_start;
4461 struct io_ring_ctx *ctx = file->private_data;
4462 unsigned long pfn;
4463 struct page *page;
4464 void *ptr;
4465
4466 switch (offset) {
4467 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004468 case IORING_OFF_CQ_RING:
4469 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004470 break;
4471 case IORING_OFF_SQES:
4472 ptr = ctx->sq_sqes;
4473 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004474 default:
4475 return -EINVAL;
4476 }
4477
4478 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004479 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004480 return -EINVAL;
4481
4482 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4483 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4484}
4485
4486SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4487 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4488 size_t, sigsz)
4489{
4490 struct io_ring_ctx *ctx;
4491 long ret = -EBADF;
4492 int submitted = 0;
4493 struct fd f;
4494
Jens Axboe6c271ce2019-01-10 11:22:30 -07004495 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004496 return -EINVAL;
4497
4498 f = fdget(fd);
4499 if (!f.file)
4500 return -EBADF;
4501
4502 ret = -EOPNOTSUPP;
4503 if (f.file->f_op != &io_uring_fops)
4504 goto out_fput;
4505
4506 ret = -ENXIO;
4507 ctx = f.file->private_data;
4508 if (!percpu_ref_tryget(&ctx->refs))
4509 goto out_fput;
4510
Jens Axboe6c271ce2019-01-10 11:22:30 -07004511 /*
4512 * For SQ polling, the thread will do all submissions and completions.
4513 * Just return the requested submit count, and wake the thread if
4514 * we were asked to.
4515 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004516 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004517 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004518 if (!list_empty_careful(&ctx->cq_overflow_list))
4519 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004520 if (flags & IORING_ENTER_SQ_WAKEUP)
4521 wake_up(&ctx->sqo_wait);
4522 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004523 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004524 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004525
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004526 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004527 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004528 /* already have mm, so io_submit_sqes() won't try to grab it */
4529 cur_mm = ctx->sqo_mm;
4530 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4531 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004532 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004533 }
4534 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004535 unsigned nr_events = 0;
4536
Jens Axboe2b188cc2019-01-07 10:46:33 -07004537 min_complete = min(min_complete, ctx->cq_entries);
4538
Jens Axboedef596e2019-01-09 08:59:42 -07004539 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004540 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004541 } else {
4542 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4543 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004544 }
4545
Pavel Begunkov6805b322019-10-08 02:18:42 +03004546 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004547out_fput:
4548 fdput(f);
4549 return submitted ? submitted : ret;
4550}
4551
4552static const struct file_operations io_uring_fops = {
4553 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004554 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004555 .mmap = io_uring_mmap,
4556 .poll = io_uring_poll,
4557 .fasync = io_uring_fasync,
4558};
4559
4560static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4561 struct io_uring_params *p)
4562{
Hristo Venev75b28af2019-08-26 17:23:46 +00004563 struct io_rings *rings;
4564 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004565
Hristo Venev75b28af2019-08-26 17:23:46 +00004566 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4567 if (size == SIZE_MAX)
4568 return -EOVERFLOW;
4569
4570 rings = io_mem_alloc(size);
4571 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004572 return -ENOMEM;
4573
Hristo Venev75b28af2019-08-26 17:23:46 +00004574 ctx->rings = rings;
4575 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4576 rings->sq_ring_mask = p->sq_entries - 1;
4577 rings->cq_ring_mask = p->cq_entries - 1;
4578 rings->sq_ring_entries = p->sq_entries;
4579 rings->cq_ring_entries = p->cq_entries;
4580 ctx->sq_mask = rings->sq_ring_mask;
4581 ctx->cq_mask = rings->cq_ring_mask;
4582 ctx->sq_entries = rings->sq_ring_entries;
4583 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004584
4585 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004586 if (size == SIZE_MAX) {
4587 io_mem_free(ctx->rings);
4588 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004589 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004590 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004591
4592 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004593 if (!ctx->sq_sqes) {
4594 io_mem_free(ctx->rings);
4595 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004596 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004597 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004598
Jens Axboe2b188cc2019-01-07 10:46:33 -07004599 return 0;
4600}
4601
4602/*
4603 * Allocate an anonymous fd, this is what constitutes the application
4604 * visible backing of an io_uring instance. The application mmaps this
4605 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4606 * we have to tie this fd to a socket for file garbage collection purposes.
4607 */
4608static int io_uring_get_fd(struct io_ring_ctx *ctx)
4609{
4610 struct file *file;
4611 int ret;
4612
4613#if defined(CONFIG_UNIX)
4614 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4615 &ctx->ring_sock);
4616 if (ret)
4617 return ret;
4618#endif
4619
4620 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4621 if (ret < 0)
4622 goto err;
4623
4624 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4625 O_RDWR | O_CLOEXEC);
4626 if (IS_ERR(file)) {
4627 put_unused_fd(ret);
4628 ret = PTR_ERR(file);
4629 goto err;
4630 }
4631
4632#if defined(CONFIG_UNIX)
4633 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004634 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004635#endif
4636 fd_install(ret, file);
4637 return ret;
4638err:
4639#if defined(CONFIG_UNIX)
4640 sock_release(ctx->ring_sock);
4641 ctx->ring_sock = NULL;
4642#endif
4643 return ret;
4644}
4645
4646static int io_uring_create(unsigned entries, struct io_uring_params *p)
4647{
4648 struct user_struct *user = NULL;
4649 struct io_ring_ctx *ctx;
4650 bool account_mem;
4651 int ret;
4652
4653 if (!entries || entries > IORING_MAX_ENTRIES)
4654 return -EINVAL;
4655
4656 /*
4657 * Use twice as many entries for the CQ ring. It's possible for the
4658 * application to drive a higher depth than the size of the SQ ring,
4659 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004660 * some flexibility in overcommitting a bit. If the application has
4661 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4662 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004663 */
4664 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004665 if (p->flags & IORING_SETUP_CQSIZE) {
4666 /*
4667 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4668 * to a power-of-two, if it isn't already. We do NOT impose
4669 * any cq vs sq ring sizing.
4670 */
4671 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4672 return -EINVAL;
4673 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4674 } else {
4675 p->cq_entries = 2 * p->sq_entries;
4676 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004677
4678 user = get_uid(current_user());
4679 account_mem = !capable(CAP_IPC_LOCK);
4680
4681 if (account_mem) {
4682 ret = io_account_mem(user,
4683 ring_pages(p->sq_entries, p->cq_entries));
4684 if (ret) {
4685 free_uid(user);
4686 return ret;
4687 }
4688 }
4689
4690 ctx = io_ring_ctx_alloc(p);
4691 if (!ctx) {
4692 if (account_mem)
4693 io_unaccount_mem(user, ring_pages(p->sq_entries,
4694 p->cq_entries));
4695 free_uid(user);
4696 return -ENOMEM;
4697 }
4698 ctx->compat = in_compat_syscall();
4699 ctx->account_mem = account_mem;
4700 ctx->user = user;
4701
4702 ret = io_allocate_scq_urings(ctx, p);
4703 if (ret)
4704 goto err;
4705
Jens Axboe6c271ce2019-01-10 11:22:30 -07004706 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004707 if (ret)
4708 goto err;
4709
Jens Axboe2b188cc2019-01-07 10:46:33 -07004710 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004711 p->sq_off.head = offsetof(struct io_rings, sq.head);
4712 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4713 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4714 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4715 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4716 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4717 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004718
4719 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004720 p->cq_off.head = offsetof(struct io_rings, cq.head);
4721 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4722 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4723 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4724 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4725 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004726
Jens Axboe044c1ab2019-10-28 09:15:33 -06004727 /*
4728 * Install ring fd as the very last thing, so we don't risk someone
4729 * having closed it before we finish setup
4730 */
4731 ret = io_uring_get_fd(ctx);
4732 if (ret < 0)
4733 goto err;
4734
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004735 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004736 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004737 return ret;
4738err:
4739 io_ring_ctx_wait_and_kill(ctx);
4740 return ret;
4741}
4742
4743/*
4744 * Sets up an aio uring context, and returns the fd. Applications asks for a
4745 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4746 * params structure passed in.
4747 */
4748static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4749{
4750 struct io_uring_params p;
4751 long ret;
4752 int i;
4753
4754 if (copy_from_user(&p, params, sizeof(p)))
4755 return -EFAULT;
4756 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4757 if (p.resv[i])
4758 return -EINVAL;
4759 }
4760
Jens Axboe6c271ce2019-01-10 11:22:30 -07004761 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004762 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004763 return -EINVAL;
4764
4765 ret = io_uring_create(entries, &p);
4766 if (ret < 0)
4767 return ret;
4768
4769 if (copy_to_user(params, &p, sizeof(p)))
4770 return -EFAULT;
4771
4772 return ret;
4773}
4774
4775SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4776 struct io_uring_params __user *, params)
4777{
4778 return io_uring_setup(entries, params);
4779}
4780
Jens Axboeedafcce2019-01-09 09:16:05 -07004781static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4782 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004783 __releases(ctx->uring_lock)
4784 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004785{
4786 int ret;
4787
Jens Axboe35fa71a2019-04-22 10:23:23 -06004788 /*
4789 * We're inside the ring mutex, if the ref is already dying, then
4790 * someone else killed the ctx or is already going through
4791 * io_uring_register().
4792 */
4793 if (percpu_ref_is_dying(&ctx->refs))
4794 return -ENXIO;
4795
Jens Axboeedafcce2019-01-09 09:16:05 -07004796 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004797
4798 /*
4799 * Drop uring mutex before waiting for references to exit. If another
4800 * thread is currently inside io_uring_enter() it might need to grab
4801 * the uring_lock to make progress. If we hold it here across the drain
4802 * wait, then we can deadlock. It's safe to drop the mutex here, since
4803 * no new references will come in after we've killed the percpu ref.
4804 */
4805 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004806 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004807 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004808
4809 switch (opcode) {
4810 case IORING_REGISTER_BUFFERS:
4811 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4812 break;
4813 case IORING_UNREGISTER_BUFFERS:
4814 ret = -EINVAL;
4815 if (arg || nr_args)
4816 break;
4817 ret = io_sqe_buffer_unregister(ctx);
4818 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004819 case IORING_REGISTER_FILES:
4820 ret = io_sqe_files_register(ctx, arg, nr_args);
4821 break;
4822 case IORING_UNREGISTER_FILES:
4823 ret = -EINVAL;
4824 if (arg || nr_args)
4825 break;
4826 ret = io_sqe_files_unregister(ctx);
4827 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004828 case IORING_REGISTER_FILES_UPDATE:
4829 ret = io_sqe_files_update(ctx, arg, nr_args);
4830 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004831 case IORING_REGISTER_EVENTFD:
4832 ret = -EINVAL;
4833 if (nr_args != 1)
4834 break;
4835 ret = io_eventfd_register(ctx, arg);
4836 break;
4837 case IORING_UNREGISTER_EVENTFD:
4838 ret = -EINVAL;
4839 if (arg || nr_args)
4840 break;
4841 ret = io_eventfd_unregister(ctx);
4842 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004843 default:
4844 ret = -EINVAL;
4845 break;
4846 }
4847
4848 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004849 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004850 percpu_ref_reinit(&ctx->refs);
4851 return ret;
4852}
4853
4854SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4855 void __user *, arg, unsigned int, nr_args)
4856{
4857 struct io_ring_ctx *ctx;
4858 long ret = -EBADF;
4859 struct fd f;
4860
4861 f = fdget(fd);
4862 if (!f.file)
4863 return -EBADF;
4864
4865 ret = -EOPNOTSUPP;
4866 if (f.file->f_op != &io_uring_fops)
4867 goto out_fput;
4868
4869 ctx = f.file->private_data;
4870
4871 mutex_lock(&ctx->uring_lock);
4872 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4873 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004874 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4875 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004876out_fput:
4877 fdput(f);
4878 return ret;
4879}
4880
Jens Axboe2b188cc2019-01-07 10:46:33 -07004881static int __init io_uring_init(void)
4882{
4883 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4884 return 0;
4885};
4886__initcall(io_uring_init);