blob: 40c351a9ed269d9f580a2209250bcc380721dee6 [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
Jens Axboe09bb8392019-03-13 12:39:28 -0600837 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
838 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600839 if (req->flags & REQ_F_INFLIGHT) {
840 unsigned long flags;
841
842 spin_lock_irqsave(&ctx->inflight_lock, flags);
843 list_del(&req->inflight_entry);
844 if (waitqueue_active(&ctx->inflight_wait))
845 wake_up(&ctx->inflight_wait);
846 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
847 }
Jens Axboead8a48a2019-11-15 08:49:11 -0700848 if (req->flags & REQ_F_TIMEOUT)
849 kfree(req->timeout.data);
Jens Axboefcb323c2019-10-24 12:39:47 -0600850 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700851 if (likely(!io_is_fallback_req(req)))
852 kmem_cache_free(req_cachep, req);
853 else
854 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600855}
856
Jackie Liua197f662019-11-08 08:09:12 -0700857static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600858{
Jackie Liua197f662019-11-08 08:09:12 -0700859 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700860 int ret;
861
Jens Axboead8a48a2019-11-15 08:49:11 -0700862 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700863 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700864 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700865 io_commit_cqring(ctx);
866 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800867 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700868 return true;
869 }
870
871 return false;
872}
873
Jens Axboeba816ad2019-09-28 11:36:45 -0600874static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600875{
Jens Axboe2665abf2019-11-05 12:40:47 -0700876 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600877 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700878 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600879
880 /*
881 * The list should never be empty when we are called here. But could
882 * potentially happen if the chain is messed up, check to be on the
883 * safe side.
884 */
885 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700886 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700887 list_del_init(&nxt->list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700888
889 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
890 (nxt->flags & REQ_F_TIMEOUT)) {
891 wake_ev |= io_link_cancel_timeout(nxt);
892 nxt = list_first_entry_or_null(&req->link_list,
893 struct io_kiocb, list);
894 req->flags &= ~REQ_F_LINK_TIMEOUT;
895 continue;
896 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600897 if (!list_empty(&req->link_list)) {
898 INIT_LIST_HEAD(&nxt->link_list);
899 list_splice(&req->link_list, &nxt->link_list);
900 nxt->flags |= REQ_F_LINK;
901 }
902
Jens Axboeba816ad2019-09-28 11:36:45 -0600903 /*
904 * If we're in async work, we can continue processing the chain
905 * in this context instead of having to queue up new async work.
906 */
Jens Axboe94ae5e72019-11-14 19:39:52 -0700907 if (nxt) {
908 if (nxtptr && io_wq_current_is_worker())
909 *nxtptr = nxt;
910 else
911 io_queue_async_work(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700912 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700913 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600914 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700915
916 if (wake_ev)
917 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600918}
919
920/*
921 * Called if REQ_F_LINK is set, and we fail the head request
922 */
923static void io_fail_links(struct io_kiocb *req)
924{
Jens Axboe2665abf2019-11-05 12:40:47 -0700925 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600926 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700927 unsigned long flags;
928
929 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600930
931 while (!list_empty(&req->link_list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -0700932 const struct io_uring_sqe *sqe_to_free = NULL;
933
Jens Axboe9e645e112019-05-10 16:07:28 -0600934 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
Jens Axboe94ae5e72019-11-14 19:39:52 -0700939 if (link->flags & REQ_F_FREE_SQE)
940 sqe_to_free = link->submit.sqe;
941
Jens Axboe2665abf2019-11-05 12:40:47 -0700942 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
943 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700944 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700945 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700946 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700947 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700948 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700949 kfree(sqe_to_free);
Jens Axboe9e645e112019-05-10 16:07:28 -0600950 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700951
952 io_commit_cqring(ctx);
953 spin_unlock_irqrestore(&ctx->completion_lock, flags);
954 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600955}
956
Jackie Liuc69f8db2019-11-09 11:00:08 +0800957static void io_free_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600958{
Jens Axboe2665abf2019-11-05 12:40:47 -0700959 if (likely(!(req->flags & REQ_F_LINK))) {
960 __io_free_req(req);
961 return;
962 }
963
Jens Axboe9e645e112019-05-10 16:07:28 -0600964 /*
965 * If LINK is set, we have dependent requests in this chain. If we
966 * didn't fail this request, queue the first one up, moving any other
967 * dependencies to the next request. In case of failure, fail the rest
968 * of the chain.
969 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700970 if (req->flags & REQ_F_FAIL_LINK) {
971 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700972 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
973 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700974 struct io_ring_ctx *ctx = req->ctx;
975 unsigned long flags;
976
977 /*
978 * If this is a timeout link, we could be racing with the
979 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700980 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700981 */
982 spin_lock_irqsave(&ctx->completion_lock, flags);
983 io_req_link_next(req, nxt);
984 spin_unlock_irqrestore(&ctx->completion_lock, flags);
985 } else {
986 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600987 }
988
989 __io_free_req(req);
990}
991
Jackie Liuc69f8db2019-11-09 11:00:08 +0800992static void io_free_req(struct io_kiocb *req)
993{
994 io_free_req_find_next(req, NULL);
995}
996
Jens Axboeba816ad2019-09-28 11:36:45 -0600997/*
998 * Drop reference to request, return next in chain (if there is one) if this
999 * was the last reference to this request.
1000 */
Jackie Liuec9c02a2019-11-08 23:50:36 +08001001static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001002{
Jens Axboeba816ad2019-09-28 11:36:45 -06001003 struct io_kiocb *nxt = NULL;
1004
Jens Axboee65ef562019-03-12 10:16:44 -06001005 if (refcount_dec_and_test(&req->refs))
Jackie Liuc69f8db2019-11-09 11:00:08 +08001006 io_free_req_find_next(req, &nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -06001007
Jens Axboeba816ad2019-09-28 11:36:45 -06001008 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -06001009 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -06001010 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -06001011 else
Jackie Liua197f662019-11-08 08:09:12 -07001012 io_queue_async_work(nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -06001013 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001014}
1015
Jens Axboe2b188cc2019-01-07 10:46:33 -07001016static void io_put_req(struct io_kiocb *req)
1017{
Jens Axboedef596e2019-01-09 08:59:42 -07001018 if (refcount_dec_and_test(&req->refs))
1019 io_free_req(req);
1020}
1021
Jens Axboe978db572019-11-14 22:39:04 -07001022/*
1023 * Must only be used if we don't need to care about links, usually from
1024 * within the completion handling itself.
1025 */
1026static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001027{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001028 /* drop both submit and complete references */
1029 if (refcount_sub_and_test(2, &req->refs))
1030 __io_free_req(req);
1031}
1032
Jens Axboe978db572019-11-14 22:39:04 -07001033static void io_double_put_req(struct io_kiocb *req)
1034{
1035 /* drop both submit and complete references */
1036 if (refcount_sub_and_test(2, &req->refs))
1037 io_free_req(req);
1038}
1039
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001040static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001041{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001042 struct io_rings *rings = ctx->rings;
1043
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001044 /*
1045 * noflush == true is from the waitqueue handler, just ensure we wake
1046 * up the task, and the next invocation will flush the entries. We
1047 * cannot safely to it from here.
1048 */
1049 if (noflush && !list_empty(&ctx->cq_overflow_list))
1050 return -1U;
1051
1052 io_cqring_overflow_flush(ctx, false);
1053
Jens Axboea3a0e432019-08-20 11:03:11 -06001054 /* See comment at the top of this file */
1055 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001056 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001057}
1058
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001059static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1060{
1061 struct io_rings *rings = ctx->rings;
1062
1063 /* make sure SQ entry isn't read before tail */
1064 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1065}
1066
Jens Axboedef596e2019-01-09 08:59:42 -07001067/*
1068 * Find and free completed poll iocbs
1069 */
1070static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1071 struct list_head *done)
1072{
1073 void *reqs[IO_IOPOLL_BATCH];
1074 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001075 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001076
Jens Axboe09bb8392019-03-13 12:39:28 -06001077 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001078 while (!list_empty(done)) {
1079 req = list_first_entry(done, struct io_kiocb, list);
1080 list_del(&req->list);
1081
Jens Axboe78e19bb2019-11-06 15:21:34 -07001082 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001083 (*nr_events)++;
1084
Jens Axboe09bb8392019-03-13 12:39:28 -06001085 if (refcount_dec_and_test(&req->refs)) {
1086 /* If we're not using fixed files, we have to pair the
1087 * completion part with the file put. Use regular
1088 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001089 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001090 */
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001091 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1092 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001093 reqs[to_free++] = req;
1094 if (to_free == ARRAY_SIZE(reqs))
1095 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001096 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001097 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001098 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001099 }
Jens Axboedef596e2019-01-09 08:59:42 -07001100 }
Jens Axboedef596e2019-01-09 08:59:42 -07001101
Jens Axboe09bb8392019-03-13 12:39:28 -06001102 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001103 io_free_req_many(ctx, reqs, &to_free);
1104}
1105
1106static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1107 long min)
1108{
1109 struct io_kiocb *req, *tmp;
1110 LIST_HEAD(done);
1111 bool spin;
1112 int ret;
1113
1114 /*
1115 * Only spin for completions if we don't have multiple devices hanging
1116 * off our complete list, and we're under the requested amount.
1117 */
1118 spin = !ctx->poll_multi_file && *nr_events < min;
1119
1120 ret = 0;
1121 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1122 struct kiocb *kiocb = &req->rw;
1123
1124 /*
1125 * Move completed entries to our local list. If we find a
1126 * request that requires polling, break out and complete
1127 * the done list first, if we have entries there.
1128 */
1129 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1130 list_move_tail(&req->list, &done);
1131 continue;
1132 }
1133 if (!list_empty(&done))
1134 break;
1135
1136 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1137 if (ret < 0)
1138 break;
1139
1140 if (ret && spin)
1141 spin = false;
1142 ret = 0;
1143 }
1144
1145 if (!list_empty(&done))
1146 io_iopoll_complete(ctx, nr_events, &done);
1147
1148 return ret;
1149}
1150
1151/*
1152 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1153 * non-spinning poll check - we'll still enter the driver poll loop, but only
1154 * as a non-spinning completion check.
1155 */
1156static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1157 long min)
1158{
Jens Axboe08f54392019-08-21 22:19:11 -06001159 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001160 int ret;
1161
1162 ret = io_do_iopoll(ctx, nr_events, min);
1163 if (ret < 0)
1164 return ret;
1165 if (!min || *nr_events >= min)
1166 return 0;
1167 }
1168
1169 return 1;
1170}
1171
1172/*
1173 * We can't just wait for polled events to come to us, we have to actively
1174 * find and complete them.
1175 */
1176static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1177{
1178 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1179 return;
1180
1181 mutex_lock(&ctx->uring_lock);
1182 while (!list_empty(&ctx->poll_list)) {
1183 unsigned int nr_events = 0;
1184
1185 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001186
1187 /*
1188 * Ensure we allow local-to-the-cpu processing to take place,
1189 * in this case we need to ensure that we reap all events.
1190 */
1191 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001192 }
1193 mutex_unlock(&ctx->uring_lock);
1194}
1195
Jens Axboe2b2ed972019-10-25 10:06:15 -06001196static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1197 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001198{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001199 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001200
1201 do {
1202 int tmin = 0;
1203
Jens Axboe500f9fb2019-08-19 12:15:59 -06001204 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001205 * Don't enter poll loop if we already have events pending.
1206 * If we do, we can potentially be spinning for commands that
1207 * already triggered a CQE (eg in error).
1208 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001209 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001210 break;
1211
1212 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001213 * If a submit got punted to a workqueue, we can have the
1214 * application entering polling for a command before it gets
1215 * issued. That app will hold the uring_lock for the duration
1216 * of the poll right here, so we need to take a breather every
1217 * now and then to ensure that the issue has a chance to add
1218 * the poll to the issued list. Otherwise we can spin here
1219 * forever, while the workqueue is stuck trying to acquire the
1220 * very same mutex.
1221 */
1222 if (!(++iters & 7)) {
1223 mutex_unlock(&ctx->uring_lock);
1224 mutex_lock(&ctx->uring_lock);
1225 }
1226
Jens Axboedef596e2019-01-09 08:59:42 -07001227 if (*nr_events < min)
1228 tmin = min - *nr_events;
1229
1230 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1231 if (ret <= 0)
1232 break;
1233 ret = 0;
1234 } while (min && !*nr_events && !need_resched());
1235
Jens Axboe2b2ed972019-10-25 10:06:15 -06001236 return ret;
1237}
1238
1239static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1240 long min)
1241{
1242 int ret;
1243
1244 /*
1245 * We disallow the app entering submit/complete with polling, but we
1246 * still need to lock the ring to prevent racing with polled issue
1247 * that got punted to a workqueue.
1248 */
1249 mutex_lock(&ctx->uring_lock);
1250 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001251 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001252 return ret;
1253}
1254
Jens Axboe491381ce2019-10-17 09:20:46 -06001255static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001256{
Jens Axboe491381ce2019-10-17 09:20:46 -06001257 /*
1258 * Tell lockdep we inherited freeze protection from submission
1259 * thread.
1260 */
1261 if (req->flags & REQ_F_ISREG) {
1262 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001263
Jens Axboe491381ce2019-10-17 09:20:46 -06001264 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001265 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001266 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001267}
1268
Jens Axboeba816ad2019-09-28 11:36:45 -06001269static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270{
1271 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1272
Jens Axboe491381ce2019-10-17 09:20:46 -06001273 if (kiocb->ki_flags & IOCB_WRITE)
1274 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001275
Jens Axboe9e645e112019-05-10 16:07:28 -06001276 if ((req->flags & REQ_F_LINK) && res != req->result)
1277 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001278 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001279}
1280
1281static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1282{
1283 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1284
1285 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001286 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001287}
1288
Jens Axboeba816ad2019-09-28 11:36:45 -06001289static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1290{
1291 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001292 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001293
1294 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001295 io_put_req_find_next(req, &nxt);
1296
1297 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001298}
1299
Jens Axboedef596e2019-01-09 08:59:42 -07001300static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1301{
1302 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1303
Jens Axboe491381ce2019-10-17 09:20:46 -06001304 if (kiocb->ki_flags & IOCB_WRITE)
1305 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001306
Jens Axboe9e645e112019-05-10 16:07:28 -06001307 if ((req->flags & REQ_F_LINK) && res != req->result)
1308 req->flags |= REQ_F_FAIL_LINK;
1309 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001310 if (res != -EAGAIN)
1311 req->flags |= REQ_F_IOPOLL_COMPLETED;
1312}
1313
1314/*
1315 * After the iocb has been issued, it's safe to be found on the poll list.
1316 * Adding the kiocb to the list AFTER submission ensures that we don't
1317 * find it from a io_iopoll_getevents() thread before the issuer is done
1318 * accessing the kiocb cookie.
1319 */
1320static void io_iopoll_req_issued(struct io_kiocb *req)
1321{
1322 struct io_ring_ctx *ctx = req->ctx;
1323
1324 /*
1325 * Track whether we have multiple files in our lists. This will impact
1326 * how we do polling eventually, not spinning if we're on potentially
1327 * different devices.
1328 */
1329 if (list_empty(&ctx->poll_list)) {
1330 ctx->poll_multi_file = false;
1331 } else if (!ctx->poll_multi_file) {
1332 struct io_kiocb *list_req;
1333
1334 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1335 list);
1336 if (list_req->rw.ki_filp != req->rw.ki_filp)
1337 ctx->poll_multi_file = true;
1338 }
1339
1340 /*
1341 * For fast devices, IO may have already completed. If it has, add
1342 * it to the front so we find it first.
1343 */
1344 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1345 list_add(&req->list, &ctx->poll_list);
1346 else
1347 list_add_tail(&req->list, &ctx->poll_list);
1348}
1349
Jens Axboe3d6770f2019-04-13 11:50:54 -06001350static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001351{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001352 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001353 int diff = state->has_refs - state->used_refs;
1354
1355 if (diff)
1356 fput_many(state->file, diff);
1357 state->file = NULL;
1358 }
1359}
1360
1361/*
1362 * Get as many references to a file as we have IOs left in this submission,
1363 * assuming most submissions are for one file, or at least that each file
1364 * has more than one submission.
1365 */
1366static struct file *io_file_get(struct io_submit_state *state, int fd)
1367{
1368 if (!state)
1369 return fget(fd);
1370
1371 if (state->file) {
1372 if (state->fd == fd) {
1373 state->used_refs++;
1374 state->ios_left--;
1375 return state->file;
1376 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001377 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001378 }
1379 state->file = fget_many(fd, state->ios_left);
1380 if (!state->file)
1381 return NULL;
1382
1383 state->fd = fd;
1384 state->has_refs = state->ios_left;
1385 state->used_refs = 1;
1386 state->ios_left--;
1387 return state->file;
1388}
1389
Jens Axboe2b188cc2019-01-07 10:46:33 -07001390/*
1391 * If we tracked the file through the SCM inflight mechanism, we could support
1392 * any file. For now, just ensure that anything potentially problematic is done
1393 * inline.
1394 */
1395static bool io_file_supports_async(struct file *file)
1396{
1397 umode_t mode = file_inode(file)->i_mode;
1398
1399 if (S_ISBLK(mode) || S_ISCHR(mode))
1400 return true;
1401 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1402 return true;
1403
1404 return false;
1405}
1406
Pavel Begunkov267bc902019-11-07 01:41:08 +03001407static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001408{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001409 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001410 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001411 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001412 unsigned ioprio;
1413 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001414
Jens Axboe09bb8392019-03-13 12:39:28 -06001415 if (!req->file)
1416 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001417
Jens Axboe491381ce2019-10-17 09:20:46 -06001418 if (S_ISREG(file_inode(req->file)->i_mode))
1419 req->flags |= REQ_F_ISREG;
1420
1421 /*
1422 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1423 * we know to async punt it even if it was opened O_NONBLOCK
1424 */
1425 if (force_nonblock && !io_file_supports_async(req->file)) {
1426 req->flags |= REQ_F_MUST_PUNT;
1427 return -EAGAIN;
1428 }
Jens Axboe6b063142019-01-10 22:13:58 -07001429
Jens Axboe2b188cc2019-01-07 10:46:33 -07001430 kiocb->ki_pos = READ_ONCE(sqe->off);
1431 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1432 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1433
1434 ioprio = READ_ONCE(sqe->ioprio);
1435 if (ioprio) {
1436 ret = ioprio_check_cap(ioprio);
1437 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001438 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001439
1440 kiocb->ki_ioprio = ioprio;
1441 } else
1442 kiocb->ki_ioprio = get_current_ioprio();
1443
1444 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1445 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001446 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001447
1448 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001449 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1450 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001451 req->flags |= REQ_F_NOWAIT;
1452
1453 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001454 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001455
Jens Axboedef596e2019-01-09 08:59:42 -07001456 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001457 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1458 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001459 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001460
Jens Axboedef596e2019-01-09 08:59:42 -07001461 kiocb->ki_flags |= IOCB_HIPRI;
1462 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001463 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001464 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001465 if (kiocb->ki_flags & IOCB_HIPRI)
1466 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001467 kiocb->ki_complete = io_complete_rw;
1468 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001469 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001470}
1471
1472static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1473{
1474 switch (ret) {
1475 case -EIOCBQUEUED:
1476 break;
1477 case -ERESTARTSYS:
1478 case -ERESTARTNOINTR:
1479 case -ERESTARTNOHAND:
1480 case -ERESTART_RESTARTBLOCK:
1481 /*
1482 * We can't just restart the syscall, since previously
1483 * submitted sqes may already be in progress. Just fail this
1484 * IO with EINTR.
1485 */
1486 ret = -EINTR;
1487 /* fall through */
1488 default:
1489 kiocb->ki_complete(kiocb, ret, 0);
1490 }
1491}
1492
Jens Axboeba816ad2019-09-28 11:36:45 -06001493static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1494 bool in_async)
1495{
1496 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1497 *nxt = __io_complete_rw(kiocb, ret);
1498 else
1499 io_rw_done(kiocb, ret);
1500}
1501
Jens Axboeedafcce2019-01-09 09:16:05 -07001502static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1503 const struct io_uring_sqe *sqe,
1504 struct iov_iter *iter)
1505{
1506 size_t len = READ_ONCE(sqe->len);
1507 struct io_mapped_ubuf *imu;
1508 unsigned index, buf_index;
1509 size_t offset;
1510 u64 buf_addr;
1511
1512 /* attempt to use fixed buffers without having provided iovecs */
1513 if (unlikely(!ctx->user_bufs))
1514 return -EFAULT;
1515
1516 buf_index = READ_ONCE(sqe->buf_index);
1517 if (unlikely(buf_index >= ctx->nr_user_bufs))
1518 return -EFAULT;
1519
1520 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1521 imu = &ctx->user_bufs[index];
1522 buf_addr = READ_ONCE(sqe->addr);
1523
1524 /* overflow */
1525 if (buf_addr + len < buf_addr)
1526 return -EFAULT;
1527 /* not inside the mapped region */
1528 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1529 return -EFAULT;
1530
1531 /*
1532 * May not be a start of buffer, set size appropriately
1533 * and advance us to the beginning.
1534 */
1535 offset = buf_addr - imu->ubuf;
1536 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001537
1538 if (offset) {
1539 /*
1540 * Don't use iov_iter_advance() here, as it's really slow for
1541 * using the latter parts of a big fixed buffer - it iterates
1542 * over each segment manually. We can cheat a bit here, because
1543 * we know that:
1544 *
1545 * 1) it's a BVEC iter, we set it up
1546 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1547 * first and last bvec
1548 *
1549 * So just find our index, and adjust the iterator afterwards.
1550 * If the offset is within the first bvec (or the whole first
1551 * bvec, just use iov_iter_advance(). This makes it easier
1552 * since we can just skip the first segment, which may not
1553 * be PAGE_SIZE aligned.
1554 */
1555 const struct bio_vec *bvec = imu->bvec;
1556
1557 if (offset <= bvec->bv_len) {
1558 iov_iter_advance(iter, offset);
1559 } else {
1560 unsigned long seg_skip;
1561
1562 /* skip first vec */
1563 offset -= bvec->bv_len;
1564 seg_skip = 1 + (offset >> PAGE_SHIFT);
1565
1566 iter->bvec = bvec + seg_skip;
1567 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001568 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001569 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001570 }
1571 }
1572
Jens Axboe5e559562019-11-13 16:12:46 -07001573 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001574}
1575
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001576static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1577 const struct sqe_submit *s, struct iovec **iovec,
1578 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001579{
1580 const struct io_uring_sqe *sqe = s->sqe;
1581 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1582 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001583 u8 opcode;
1584
1585 /*
1586 * We're reading ->opcode for the second time, but the first read
1587 * doesn't care whether it's _FIXED or not, so it doesn't matter
1588 * whether ->opcode changes concurrently. The first read does care
1589 * about whether it is a READ or a WRITE, so we don't trust this read
1590 * for that purpose and instead let the caller pass in the read/write
1591 * flag.
1592 */
1593 opcode = READ_ONCE(sqe->opcode);
1594 if (opcode == IORING_OP_READ_FIXED ||
1595 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001596 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001597 *iovec = NULL;
1598 return ret;
1599 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001600
1601 if (!s->has_user)
1602 return -EFAULT;
1603
1604#ifdef CONFIG_COMPAT
1605 if (ctx->compat)
1606 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1607 iovec, iter);
1608#endif
1609
1610 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1611}
1612
Jens Axboe32960612019-09-23 11:05:34 -06001613/*
1614 * For files that don't have ->read_iter() and ->write_iter(), handle them
1615 * by looping over ->read() or ->write() manually.
1616 */
1617static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1618 struct iov_iter *iter)
1619{
1620 ssize_t ret = 0;
1621
1622 /*
1623 * Don't support polled IO through this interface, and we can't
1624 * support non-blocking either. For the latter, this just causes
1625 * the kiocb to be handled from an async context.
1626 */
1627 if (kiocb->ki_flags & IOCB_HIPRI)
1628 return -EOPNOTSUPP;
1629 if (kiocb->ki_flags & IOCB_NOWAIT)
1630 return -EAGAIN;
1631
1632 while (iov_iter_count(iter)) {
1633 struct iovec iovec = iov_iter_iovec(iter);
1634 ssize_t nr;
1635
1636 if (rw == READ) {
1637 nr = file->f_op->read(file, iovec.iov_base,
1638 iovec.iov_len, &kiocb->ki_pos);
1639 } else {
1640 nr = file->f_op->write(file, iovec.iov_base,
1641 iovec.iov_len, &kiocb->ki_pos);
1642 }
1643
1644 if (nr < 0) {
1645 if (!ret)
1646 ret = nr;
1647 break;
1648 }
1649 ret += nr;
1650 if (nr != iovec.iov_len)
1651 break;
1652 iov_iter_advance(iter, nr);
1653 }
1654
1655 return ret;
1656}
1657
Pavel Begunkov267bc902019-11-07 01:41:08 +03001658static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001659 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001660{
1661 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1662 struct kiocb *kiocb = &req->rw;
1663 struct iov_iter iter;
1664 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001665 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001666 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001667
Pavel Begunkov267bc902019-11-07 01:41:08 +03001668 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001669 if (ret)
1670 return ret;
1671 file = kiocb->ki_filp;
1672
Jens Axboe2b188cc2019-01-07 10:46:33 -07001673 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001674 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001675
Pavel Begunkov267bc902019-11-07 01:41:08 +03001676 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001677 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001678 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001679
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001680 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001681 if (req->flags & REQ_F_LINK)
1682 req->result = read_size;
1683
Jens Axboe31b51512019-01-18 22:56:34 -07001684 iov_count = iov_iter_count(&iter);
1685 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001686 if (!ret) {
1687 ssize_t ret2;
1688
Jens Axboe32960612019-09-23 11:05:34 -06001689 if (file->f_op->read_iter)
1690 ret2 = call_read_iter(file, kiocb, &iter);
1691 else
1692 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1693
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001694 /*
1695 * In case of a short read, punt to async. This can happen
1696 * if we have data partially cached. Alternatively we can
1697 * return the short read, in which case the application will
1698 * need to issue another SQE and wait for it. That SQE will
1699 * need async punt anyway, so it's more efficient to do it
1700 * here.
1701 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001702 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1703 (req->flags & REQ_F_ISREG) &&
1704 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001705 ret2 = -EAGAIN;
1706 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001707 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001708 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001709 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001710 ret = -EAGAIN;
1711 }
1712 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001713 return ret;
1714}
1715
Pavel Begunkov267bc902019-11-07 01:41:08 +03001716static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001717 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001718{
1719 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1720 struct kiocb *kiocb = &req->rw;
1721 struct iov_iter iter;
1722 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001723 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001724 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001725
Pavel Begunkov267bc902019-11-07 01:41:08 +03001726 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727 if (ret)
1728 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001729
Jens Axboe2b188cc2019-01-07 10:46:33 -07001730 file = kiocb->ki_filp;
1731 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001732 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001733
Pavel Begunkov267bc902019-11-07 01:41:08 +03001734 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001735 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001736 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737
Jens Axboe9e645e112019-05-10 16:07:28 -06001738 if (req->flags & REQ_F_LINK)
1739 req->result = ret;
1740
Jens Axboe31b51512019-01-18 22:56:34 -07001741 iov_count = iov_iter_count(&iter);
1742
1743 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001744 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001745 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001746
1747 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001748 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001749 ssize_t ret2;
1750
Jens Axboe2b188cc2019-01-07 10:46:33 -07001751 /*
1752 * Open-code file_start_write here to grab freeze protection,
1753 * which will be released by another thread in
1754 * io_complete_rw(). Fool lockdep by telling it the lock got
1755 * released so that it doesn't complain about the held lock when
1756 * we return to userspace.
1757 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001758 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001759 __sb_start_write(file_inode(file)->i_sb,
1760 SB_FREEZE_WRITE, true);
1761 __sb_writers_release(file_inode(file)->i_sb,
1762 SB_FREEZE_WRITE);
1763 }
1764 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001765
Jens Axboe32960612019-09-23 11:05:34 -06001766 if (file->f_op->write_iter)
1767 ret2 = call_write_iter(file, kiocb, &iter);
1768 else
1769 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001770 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001771 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001772 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001773 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001774 }
Jens Axboe31b51512019-01-18 22:56:34 -07001775out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001776 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001777 return ret;
1778}
1779
1780/*
1781 * IORING_OP_NOP just posts a completion event, nothing else.
1782 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001783static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001784{
1785 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001786
Jens Axboedef596e2019-01-09 08:59:42 -07001787 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1788 return -EINVAL;
1789
Jens Axboe78e19bb2019-11-06 15:21:34 -07001790 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001791 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001792 return 0;
1793}
1794
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001795static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1796{
Jens Axboe6b063142019-01-10 22:13:58 -07001797 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001798
Jens Axboe09bb8392019-03-13 12:39:28 -06001799 if (!req->file)
1800 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001801
Jens Axboe6b063142019-01-10 22:13:58 -07001802 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001803 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001804 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001805 return -EINVAL;
1806
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001807 return 0;
1808}
1809
1810static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001811 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001812{
1813 loff_t sqe_off = READ_ONCE(sqe->off);
1814 loff_t sqe_len = READ_ONCE(sqe->len);
1815 loff_t end = sqe_off + sqe_len;
1816 unsigned fsync_flags;
1817 int ret;
1818
1819 fsync_flags = READ_ONCE(sqe->fsync_flags);
1820 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1821 return -EINVAL;
1822
1823 ret = io_prep_fsync(req, sqe);
1824 if (ret)
1825 return ret;
1826
1827 /* fsync always requires a blocking context */
1828 if (force_nonblock)
1829 return -EAGAIN;
1830
1831 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1832 end > 0 ? end : LLONG_MAX,
1833 fsync_flags & IORING_FSYNC_DATASYNC);
1834
Jens Axboe9e645e112019-05-10 16:07:28 -06001835 if (ret < 0 && (req->flags & REQ_F_LINK))
1836 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001837 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001838 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001839 return 0;
1840}
1841
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001842static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1843{
1844 struct io_ring_ctx *ctx = req->ctx;
1845 int ret = 0;
1846
1847 if (!req->file)
1848 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001849
1850 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1851 return -EINVAL;
1852 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1853 return -EINVAL;
1854
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001855 return ret;
1856}
1857
1858static int io_sync_file_range(struct io_kiocb *req,
1859 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001860 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001861 bool force_nonblock)
1862{
1863 loff_t sqe_off;
1864 loff_t sqe_len;
1865 unsigned flags;
1866 int ret;
1867
1868 ret = io_prep_sfr(req, sqe);
1869 if (ret)
1870 return ret;
1871
1872 /* sync_file_range always requires a blocking context */
1873 if (force_nonblock)
1874 return -EAGAIN;
1875
1876 sqe_off = READ_ONCE(sqe->off);
1877 sqe_len = READ_ONCE(sqe->len);
1878 flags = READ_ONCE(sqe->sync_range_flags);
1879
1880 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1881
Jens Axboe9e645e112019-05-10 16:07:28 -06001882 if (ret < 0 && (req->flags & REQ_F_LINK))
1883 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001884 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001885 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001886 return 0;
1887}
1888
Jens Axboe0fa03c62019-04-19 13:34:07 -06001889#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001890static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001891 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001892 long (*fn)(struct socket *, struct user_msghdr __user *,
1893 unsigned int))
1894{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001895 struct socket *sock;
1896 int ret;
1897
1898 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1899 return -EINVAL;
1900
1901 sock = sock_from_file(req->file, &ret);
1902 if (sock) {
1903 struct user_msghdr __user *msg;
1904 unsigned flags;
1905
1906 flags = READ_ONCE(sqe->msg_flags);
1907 if (flags & MSG_DONTWAIT)
1908 req->flags |= REQ_F_NOWAIT;
1909 else if (force_nonblock)
1910 flags |= MSG_DONTWAIT;
1911
1912 msg = (struct user_msghdr __user *) (unsigned long)
1913 READ_ONCE(sqe->addr);
1914
Jens Axboeaa1fa282019-04-19 13:38:09 -06001915 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001916 if (force_nonblock && ret == -EAGAIN)
1917 return ret;
1918 }
1919
Jens Axboe78e19bb2019-11-06 15:21:34 -07001920 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001921 if (ret < 0 && (req->flags & REQ_F_LINK))
1922 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001923 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001924 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001925}
1926#endif
1927
1928static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001929 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001930{
1931#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001932 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1933 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001934#else
1935 return -EOPNOTSUPP;
1936#endif
1937}
1938
1939static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001940 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001941{
1942#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001943 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1944 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001945#else
1946 return -EOPNOTSUPP;
1947#endif
1948}
1949
Jens Axboe17f2fe32019-10-17 14:42:58 -06001950static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1951 struct io_kiocb **nxt, bool force_nonblock)
1952{
1953#if defined(CONFIG_NET)
1954 struct sockaddr __user *addr;
1955 int __user *addr_len;
1956 unsigned file_flags;
1957 int flags, ret;
1958
1959 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1960 return -EINVAL;
1961 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1962 return -EINVAL;
1963
1964 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1965 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1966 flags = READ_ONCE(sqe->accept_flags);
1967 file_flags = force_nonblock ? O_NONBLOCK : 0;
1968
1969 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1970 if (ret == -EAGAIN && force_nonblock) {
1971 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1972 return -EAGAIN;
1973 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001974 if (ret == -ERESTARTSYS)
1975 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001976 if (ret < 0 && (req->flags & REQ_F_LINK))
1977 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001978 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001979 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001980 return 0;
1981#else
1982 return -EOPNOTSUPP;
1983#endif
1984}
1985
Jens Axboeeac406c2019-11-14 12:09:58 -07001986static inline void io_poll_remove_req(struct io_kiocb *req)
1987{
1988 if (!RB_EMPTY_NODE(&req->rb_node)) {
1989 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
1990 RB_CLEAR_NODE(&req->rb_node);
1991 }
1992}
1993
Jens Axboe221c5eb2019-01-17 09:41:58 -07001994static void io_poll_remove_one(struct io_kiocb *req)
1995{
1996 struct io_poll_iocb *poll = &req->poll;
1997
1998 spin_lock(&poll->head->lock);
1999 WRITE_ONCE(poll->canceled, true);
2000 if (!list_empty(&poll->wait.entry)) {
2001 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002002 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002003 }
2004 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002005 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002006}
2007
2008static void io_poll_remove_all(struct io_ring_ctx *ctx)
2009{
Jens Axboeeac406c2019-11-14 12:09:58 -07002010 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002011 struct io_kiocb *req;
2012
2013 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002014 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2015 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002016 io_poll_remove_one(req);
2017 }
2018 spin_unlock_irq(&ctx->completion_lock);
2019}
2020
Jens Axboe47f46762019-11-09 17:43:02 -07002021static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2022{
Jens Axboeeac406c2019-11-14 12:09:58 -07002023 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002024 struct io_kiocb *req;
2025
Jens Axboeeac406c2019-11-14 12:09:58 -07002026 p = ctx->cancel_tree.rb_node;
2027 while (p) {
2028 parent = p;
2029 req = rb_entry(parent, struct io_kiocb, rb_node);
2030 if (sqe_addr < req->user_data) {
2031 p = p->rb_left;
2032 } else if (sqe_addr > req->user_data) {
2033 p = p->rb_right;
2034 } else {
2035 io_poll_remove_one(req);
2036 return 0;
2037 }
Jens Axboe47f46762019-11-09 17:43:02 -07002038 }
2039
2040 return -ENOENT;
2041}
2042
Jens Axboe221c5eb2019-01-17 09:41:58 -07002043/*
2044 * Find a running poll command that matches one specified in sqe->addr,
2045 * and remove it if found.
2046 */
2047static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2048{
2049 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002050 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002051
2052 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2053 return -EINVAL;
2054 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2055 sqe->poll_events)
2056 return -EINVAL;
2057
2058 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002059 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002060 spin_unlock_irq(&ctx->completion_lock);
2061
Jens Axboe78e19bb2019-11-06 15:21:34 -07002062 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002063 if (ret < 0 && (req->flags & REQ_F_LINK))
2064 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002065 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002066 return 0;
2067}
2068
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002069static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002070{
Jackie Liua197f662019-11-08 08:09:12 -07002071 struct io_ring_ctx *ctx = req->ctx;
2072
Jens Axboe8c838782019-03-12 15:48:16 -06002073 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002074 if (error)
2075 io_cqring_fill_event(req, error);
2076 else
2077 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002078 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002079}
2080
Jens Axboe561fb042019-10-24 07:25:42 -06002081static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002082{
Jens Axboe561fb042019-10-24 07:25:42 -06002083 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002084 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2085 struct io_poll_iocb *poll = &req->poll;
2086 struct poll_table_struct pt = { ._key = poll->events };
2087 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002088 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002089 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002090 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002091
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002092 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002093 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002094 ret = -ECANCELED;
2095 } else if (READ_ONCE(poll->canceled)) {
2096 ret = -ECANCELED;
2097 }
Jens Axboe561fb042019-10-24 07:25:42 -06002098
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002099 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002100 mask = vfs_poll(poll->file, &pt) & poll->events;
2101
2102 /*
2103 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2104 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2105 * synchronize with them. In the cancellation case the list_del_init
2106 * itself is not actually needed, but harmless so we keep it in to
2107 * avoid further branches in the fast path.
2108 */
2109 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002110 if (!mask && ret != -ECANCELED) {
Jens Axboe221c5eb2019-01-17 09:41:58 -07002111 add_wait_queue(poll->head, &poll->wait);
2112 spin_unlock_irq(&ctx->completion_lock);
2113 return;
2114 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002115 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002116 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002117 spin_unlock_irq(&ctx->completion_lock);
2118
Jens Axboe8c838782019-03-12 15:48:16 -06002119 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002120
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
2334 io_cqring_fill_event(req, -ECANCELED);
2335 io_put_req(req);
2336 return 0;
2337}
2338
Jens Axboe11365042019-10-16 09:08:32 -06002339/*
2340 * Remove or update an existing timeout command
2341 */
2342static int io_timeout_remove(struct io_kiocb *req,
2343 const struct io_uring_sqe *sqe)
2344{
2345 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002346 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002347 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002348
2349 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2350 return -EINVAL;
2351 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2352 return -EINVAL;
2353 flags = READ_ONCE(sqe->timeout_flags);
2354 if (flags)
2355 return -EINVAL;
2356
Jens Axboe11365042019-10-16 09:08:32 -06002357 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002358 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002359
Jens Axboe47f46762019-11-09 17:43:02 -07002360 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002361 io_commit_cqring(ctx);
2362 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002363 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002364 if (ret < 0 && req->flags & REQ_F_LINK)
2365 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002366 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002367 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002368}
2369
Jens Axboead8a48a2019-11-15 08:49:11 -07002370static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002371{
Jens Axboead8a48a2019-11-15 08:49:11 -07002372 const struct io_uring_sqe *sqe = req->submit.sqe;
2373 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002374 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002375
Jens Axboead8a48a2019-11-15 08:49:11 -07002376 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002377 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002378 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002379 return -EINVAL;
2380 flags = READ_ONCE(sqe->timeout_flags);
2381 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002382 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002383
Jens Axboead8a48a2019-11-15 08:49:11 -07002384 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2385 if (!data)
2386 return -ENOMEM;
2387 data->req = req;
2388 req->timeout.data = data;
2389 req->flags |= REQ_F_TIMEOUT;
2390
2391 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002392 return -EFAULT;
2393
Jens Axboe11365042019-10-16 09:08:32 -06002394 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002395 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002396 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002397 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002398
Jens Axboead8a48a2019-11-15 08:49:11 -07002399 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2400 return 0;
2401}
2402
2403static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2404{
2405 unsigned count;
2406 struct io_ring_ctx *ctx = req->ctx;
2407 struct io_timeout_data *data;
2408 struct list_head *entry;
2409 unsigned span = 0;
2410 int ret;
2411
2412 ret = io_timeout_setup(req);
2413 /* common setup allows flags (like links) set, we don't */
2414 if (!ret && sqe->flags)
2415 ret = -EINVAL;
2416 if (ret)
2417 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002418
Jens Axboe5262f562019-09-17 12:26:57 -06002419 /*
2420 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002421 * timeout event to be satisfied. If it isn't set, then this is
2422 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002423 */
2424 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002425 if (!count) {
2426 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2427 spin_lock_irq(&ctx->completion_lock);
2428 entry = ctx->timeout_list.prev;
2429 goto add;
2430 }
Jens Axboe5262f562019-09-17 12:26:57 -06002431
2432 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002433 /* reuse it to store the count */
2434 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002435
2436 /*
2437 * Insertion sort, ensuring the first entry in the list is always
2438 * the one we need first.
2439 */
Jens Axboe5262f562019-09-17 12:26:57 -06002440 spin_lock_irq(&ctx->completion_lock);
2441 list_for_each_prev(entry, &ctx->timeout_list) {
2442 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002443 unsigned nxt_sq_head;
2444 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002445
Jens Axboe93bd25b2019-11-11 23:34:31 -07002446 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2447 continue;
2448
yangerkun5da0fb12019-10-15 21:59:29 +08002449 /*
2450 * Since cached_sq_head + count - 1 can overflow, use type long
2451 * long to store it.
2452 */
2453 tmp = (long long)ctx->cached_sq_head + count - 1;
2454 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2455 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2456
2457 /*
2458 * cached_sq_head may overflow, and it will never overflow twice
2459 * once there is some timeout req still be valid.
2460 */
2461 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002462 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002463
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002464 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002465 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002466
2467 /*
2468 * Sequence of reqs after the insert one and itself should
2469 * be adjusted because each timeout req consumes a slot.
2470 */
2471 span++;
2472 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002473 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002474 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002475add:
Jens Axboe5262f562019-09-17 12:26:57 -06002476 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002477 data = req->timeout.data;
2478 data->timer.function = io_timeout_fn;
2479 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002480 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002481 return 0;
2482}
2483
Jens Axboe62755e32019-10-28 21:49:21 -06002484static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002485{
Jens Axboe62755e32019-10-28 21:49:21 -06002486 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002487
Jens Axboe62755e32019-10-28 21:49:21 -06002488 return req->user_data == (unsigned long) data;
2489}
2490
Jens Axboee977d6d2019-11-05 12:39:45 -07002491static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002492{
Jens Axboe62755e32019-10-28 21:49:21 -06002493 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002494 int ret = 0;
2495
Jens Axboe62755e32019-10-28 21:49:21 -06002496 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2497 switch (cancel_ret) {
2498 case IO_WQ_CANCEL_OK:
2499 ret = 0;
2500 break;
2501 case IO_WQ_CANCEL_RUNNING:
2502 ret = -EALREADY;
2503 break;
2504 case IO_WQ_CANCEL_NOTFOUND:
2505 ret = -ENOENT;
2506 break;
2507 }
2508
Jens Axboee977d6d2019-11-05 12:39:45 -07002509 return ret;
2510}
2511
Jens Axboe47f46762019-11-09 17:43:02 -07002512static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2513 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002514 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002515{
2516 unsigned long flags;
2517 int ret;
2518
2519 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2520 if (ret != -ENOENT) {
2521 spin_lock_irqsave(&ctx->completion_lock, flags);
2522 goto done;
2523 }
2524
2525 spin_lock_irqsave(&ctx->completion_lock, flags);
2526 ret = io_timeout_cancel(ctx, sqe_addr);
2527 if (ret != -ENOENT)
2528 goto done;
2529 ret = io_poll_cancel(ctx, sqe_addr);
2530done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002531 if (!ret)
2532 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002533 io_cqring_fill_event(req, ret);
2534 io_commit_cqring(ctx);
2535 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2536 io_cqring_ev_posted(ctx);
2537
2538 if (ret < 0 && (req->flags & REQ_F_LINK))
2539 req->flags |= REQ_F_FAIL_LINK;
2540 io_put_req_find_next(req, nxt);
2541}
2542
Jens Axboee977d6d2019-11-05 12:39:45 -07002543static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2544 struct io_kiocb **nxt)
2545{
2546 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002547
2548 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2549 return -EINVAL;
2550 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2551 sqe->cancel_flags)
2552 return -EINVAL;
2553
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002554 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002555 return 0;
2556}
2557
Jackie Liua197f662019-11-08 08:09:12 -07002558static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002559{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002560 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002561 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002562 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002563
Bob Liu9d858b22019-11-13 18:06:25 +08002564 /* Still need defer if there is pending req in defer list. */
2565 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002566 return 0;
2567
2568 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2569 if (!sqe_copy)
2570 return -EAGAIN;
2571
2572 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002573 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002574 spin_unlock_irq(&ctx->completion_lock);
2575 kfree(sqe_copy);
2576 return 0;
2577 }
2578
2579 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2580 req->submit.sqe = sqe_copy;
2581
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002582 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002583 list_add_tail(&req->list, &ctx->defer_list);
2584 spin_unlock_irq(&ctx->completion_lock);
2585 return -EIOCBQUEUED;
2586}
2587
Jackie Liua197f662019-11-08 08:09:12 -07002588static int __io_submit_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2589 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002590{
Jens Axboee0c5c572019-03-12 10:18:47 -06002591 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002592 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002593 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002594
2595 opcode = READ_ONCE(s->sqe->opcode);
2596 switch (opcode) {
2597 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002598 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002599 break;
2600 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002601 if (unlikely(s->sqe->buf_index))
2602 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002603 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002604 break;
2605 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002606 if (unlikely(s->sqe->buf_index))
2607 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002608 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002609 break;
2610 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002611 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002612 break;
2613 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002614 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002615 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002616 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002617 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002618 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002619 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002620 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002621 break;
2622 case IORING_OP_POLL_REMOVE:
2623 ret = io_poll_remove(req, s->sqe);
2624 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002625 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002626 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002627 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002628 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002629 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002630 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002631 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002632 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002633 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002634 case IORING_OP_TIMEOUT:
2635 ret = io_timeout(req, s->sqe);
2636 break;
Jens Axboe11365042019-10-16 09:08:32 -06002637 case IORING_OP_TIMEOUT_REMOVE:
2638 ret = io_timeout_remove(req, s->sqe);
2639 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002640 case IORING_OP_ACCEPT:
2641 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2642 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002643 case IORING_OP_ASYNC_CANCEL:
2644 ret = io_async_cancel(req, s->sqe, nxt);
2645 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002646 default:
2647 ret = -EINVAL;
2648 break;
2649 }
2650
Jens Axboedef596e2019-01-09 08:59:42 -07002651 if (ret)
2652 return ret;
2653
2654 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002655 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002656 return -EAGAIN;
2657
2658 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002659 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002660 mutex_lock(&ctx->uring_lock);
2661 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002662 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002663 mutex_unlock(&ctx->uring_lock);
2664 }
2665
2666 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002667}
2668
Jens Axboe561fb042019-10-24 07:25:42 -06002669static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002670{
Jens Axboe561fb042019-10-24 07:25:42 -06002671 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002672 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002673 struct sqe_submit *s = &req->submit;
2674 const struct io_uring_sqe *sqe = s->sqe;
2675 struct io_kiocb *nxt = NULL;
2676 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002677
Jens Axboe561fb042019-10-24 07:25:42 -06002678 /* Ensure we clear previously set non-block flag */
2679 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002680
Jens Axboe561fb042019-10-24 07:25:42 -06002681 if (work->flags & IO_WQ_WORK_CANCEL)
2682 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002683
Jens Axboe561fb042019-10-24 07:25:42 -06002684 if (!ret) {
2685 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2686 s->in_async = true;
2687 do {
Jackie Liua197f662019-11-08 08:09:12 -07002688 ret = __io_submit_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002689 /*
2690 * We can get EAGAIN for polled IO even though we're
2691 * forcing a sync submission from here, since we can't
2692 * wait for request slots on the block side.
2693 */
2694 if (ret != -EAGAIN)
2695 break;
2696 cond_resched();
2697 } while (1);
2698 }
Jens Axboe31b51512019-01-18 22:56:34 -07002699
Jens Axboe561fb042019-10-24 07:25:42 -06002700 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002701 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002702
Jens Axboe561fb042019-10-24 07:25:42 -06002703 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002704 if (req->flags & REQ_F_LINK)
2705 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002706 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002707 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002708 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002709
Jens Axboe561fb042019-10-24 07:25:42 -06002710 /* async context always use a copy of the sqe */
2711 kfree(sqe);
2712
2713 /* if a dependent link is ready, pass it back */
2714 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002715 struct io_kiocb *link;
2716
2717 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06002718 *workptr = &nxt->work;
Jens Axboe94ae5e72019-11-14 19:39:52 -07002719 if (link)
2720 io_queue_linked_timeout(link);
Jens Axboeedafcce2019-01-09 09:16:05 -07002721 }
Jens Axboe31b51512019-01-18 22:56:34 -07002722}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002723
Jens Axboe09bb8392019-03-13 12:39:28 -06002724static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2725{
2726 int op = READ_ONCE(sqe->opcode);
2727
2728 switch (op) {
2729 case IORING_OP_NOP:
2730 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002731 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002732 case IORING_OP_TIMEOUT_REMOVE:
2733 case IORING_OP_ASYNC_CANCEL:
2734 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002735 return false;
2736 default:
2737 return true;
2738 }
2739}
2740
Jens Axboe65e19f52019-10-26 07:20:21 -06002741static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2742 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002743{
Jens Axboe65e19f52019-10-26 07:20:21 -06002744 struct fixed_file_table *table;
2745
2746 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2747 return table->files[index & IORING_FILE_TABLE_MASK];
2748}
2749
Jackie Liua197f662019-11-08 08:09:12 -07002750static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002751{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002752 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002753 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002754 unsigned flags;
2755 int fd;
2756
2757 flags = READ_ONCE(s->sqe->flags);
2758 fd = READ_ONCE(s->sqe->fd);
2759
Jackie Liu4fe2c962019-09-09 20:50:40 +08002760 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002761 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002762 /*
2763 * All io need record the previous position, if LINK vs DARIN,
2764 * it can be used to mark the position of the first IO in the
2765 * link list.
2766 */
2767 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002768
Jens Axboe60c112b2019-06-21 10:20:18 -06002769 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002770 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002771
2772 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002773 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002774 (unsigned) fd >= ctx->nr_user_files))
2775 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002776 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002777 req->file = io_file_from_index(ctx, fd);
2778 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002779 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002780 req->flags |= REQ_F_FIXED_FILE;
2781 } else {
2782 if (s->needs_fixed_file)
2783 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002784 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002785 req->file = io_file_get(state, fd);
2786 if (unlikely(!req->file))
2787 return -EBADF;
2788 }
2789
2790 return 0;
2791}
2792
Jackie Liua197f662019-11-08 08:09:12 -07002793static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002794{
Jens Axboefcb323c2019-10-24 12:39:47 -06002795 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002796 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002797
2798 rcu_read_lock();
2799 spin_lock_irq(&ctx->inflight_lock);
2800 /*
2801 * We use the f_ops->flush() handler to ensure that we can flush
2802 * out work accessing these files if the fd is closed. Check if
2803 * the fd has changed since we started down this path, and disallow
2804 * this operation if it has.
2805 */
2806 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2807 list_add(&req->inflight_entry, &ctx->inflight_list);
2808 req->flags |= REQ_F_INFLIGHT;
2809 req->work.files = current->files;
2810 ret = 0;
2811 }
2812 spin_unlock_irq(&ctx->inflight_lock);
2813 rcu_read_unlock();
2814
2815 return ret;
2816}
2817
Jens Axboe2665abf2019-11-05 12:40:47 -07002818static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2819{
Jens Axboead8a48a2019-11-15 08:49:11 -07002820 struct io_timeout_data *data = container_of(timer,
2821 struct io_timeout_data, timer);
2822 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002823 struct io_ring_ctx *ctx = req->ctx;
2824 struct io_kiocb *prev = NULL;
2825 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002826
2827 spin_lock_irqsave(&ctx->completion_lock, flags);
2828
2829 /*
2830 * We don't expect the list to be empty, that will only happen if we
2831 * race with the completion of the linked work.
2832 */
2833 if (!list_empty(&req->list)) {
2834 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002835 if (refcount_inc_not_zero(&prev->refs))
2836 list_del_init(&req->list);
2837 else
2838 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002839 }
2840
2841 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2842
2843 if (prev) {
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002844 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2845 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07002846 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002847 } else {
2848 io_cqring_add_event(req, -ETIME);
2849 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002850 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002851 return HRTIMER_NORESTART;
2852}
2853
Jens Axboead8a48a2019-11-15 08:49:11 -07002854static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002855{
Jens Axboe76a46e02019-11-10 23:34:16 -07002856 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002857
Jens Axboe76a46e02019-11-10 23:34:16 -07002858 /*
2859 * If the list is now empty, then our linked request finished before
2860 * we got a chance to setup the timer
2861 */
2862 spin_lock_irq(&ctx->completion_lock);
2863 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002864 struct io_timeout_data *data = req->timeout.data;
2865
Jens Axboead8a48a2019-11-15 08:49:11 -07002866 data->timer.function = io_link_timeout_fn;
2867 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2868 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002869 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002870 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002871
Jens Axboe2665abf2019-11-05 12:40:47 -07002872 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002873 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002874}
2875
Jens Axboead8a48a2019-11-15 08:49:11 -07002876static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002877{
2878 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002879
Jens Axboe2665abf2019-11-05 12:40:47 -07002880 if (!(req->flags & REQ_F_LINK))
2881 return NULL;
2882
2883 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002884 if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT)
2885 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002886
Jens Axboe76a46e02019-11-10 23:34:16 -07002887 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07002888 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002889}
2890
Jens Axboe0e0702d2019-11-14 21:42:10 -07002891static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002892{
Jens Axboe94ae5e72019-11-14 19:39:52 -07002893 struct io_kiocb *nxt = io_prep_linked_timeout(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002894 int ret;
2895
Jackie Liua197f662019-11-08 08:09:12 -07002896 ret = __io_submit_sqe(req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002897
2898 /*
2899 * We async punt it if the file wasn't marked NOWAIT, or if the file
2900 * doesn't support non-blocking read/write attempts
2901 */
2902 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2903 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002904 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002905 struct io_uring_sqe *sqe_copy;
2906
Jackie Liu954dab12019-09-18 10:37:52 +08002907 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002908 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002909 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002910 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
Jackie Liua197f662019-11-08 08:09:12 -07002911 ret = io_grab_files(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06002912 if (ret) {
2913 kfree(sqe_copy);
2914 goto err;
2915 }
Jens Axboe31b51512019-01-18 22:56:34 -07002916 }
Jens Axboee65ef562019-03-12 10:16:44 -06002917
2918 /*
2919 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002920 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002921 */
Jackie Liua197f662019-11-08 08:09:12 -07002922 io_queue_async_work(req);
Jens Axboe0e0702d2019-11-14 21:42:10 -07002923 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002924 }
2925 }
Jens Axboee65ef562019-03-12 10:16:44 -06002926
Jens Axboefcb323c2019-10-24 12:39:47 -06002927err:
Jens Axboee65ef562019-03-12 10:16:44 -06002928 /* drop submission reference */
2929 io_put_req(req);
2930
Jens Axboe76a46e02019-11-10 23:34:16 -07002931 if (nxt) {
2932 if (!ret)
Jens Axboead8a48a2019-11-15 08:49:11 -07002933 io_queue_linked_timeout(nxt);
Jens Axboe76a46e02019-11-10 23:34:16 -07002934 else
2935 io_put_req(nxt);
2936 }
2937
Jens Axboee65ef562019-03-12 10:16:44 -06002938 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002939 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002940 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002941 if (req->flags & REQ_F_LINK)
2942 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002943 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002944 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002945}
2946
Jens Axboe0e0702d2019-11-14 21:42:10 -07002947static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002948{
2949 int ret;
2950
Jackie Liua197f662019-11-08 08:09:12 -07002951 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002952 if (ret) {
2953 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002954 io_cqring_add_event(req, ret);
2955 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002956 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07002957 } else
2958 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002959}
2960
Jens Axboe0e0702d2019-11-14 21:42:10 -07002961static void io_queue_link_head(struct io_kiocb *req, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002962{
2963 int ret;
2964 int need_submit = false;
Jackie Liua197f662019-11-08 08:09:12 -07002965 struct io_ring_ctx *ctx = req->ctx;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002966
Jens Axboe94ae5e72019-11-14 19:39:52 -07002967 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
2968 ret = -ECANCELED;
2969 goto err;
2970 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07002971 if (!shadow) {
2972 io_queue_sqe(req);
2973 return;
2974 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08002975
2976 /*
2977 * Mark the first IO in link list as DRAIN, let all the following
2978 * IOs enter the defer list. all IO needs to be completed before link
2979 * list.
2980 */
2981 req->flags |= REQ_F_IO_DRAIN;
Jackie Liua197f662019-11-08 08:09:12 -07002982 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002983 if (ret) {
2984 if (ret != -EIOCBQUEUED) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002985err:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002986 io_cqring_add_event(req, ret);
2987 io_double_put_req(req);
Jens Axboe94ae5e72019-11-14 19:39:52 -07002988 if (shadow)
2989 __io_free_req(shadow);
Jens Axboe0e0702d2019-11-14 21:42:10 -07002990 return;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002991 }
2992 } else {
2993 /*
2994 * If ret == 0 means that all IOs in front of link io are
2995 * running done. let's queue link head.
2996 */
2997 need_submit = true;
2998 }
2999
3000 /* Insert shadow req to defer_list, blocking next IOs */
3001 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003002 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003003 list_add_tail(&shadow->list, &ctx->defer_list);
3004 spin_unlock_irq(&ctx->completion_lock);
3005
3006 if (need_submit)
Jens Axboe0e0702d2019-11-14 21:42:10 -07003007 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003008}
3009
Jens Axboe9e645e112019-05-10 16:07:28 -06003010#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3011
Jackie Liua197f662019-11-08 08:09:12 -07003012static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3013 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003014{
3015 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03003016 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07003017 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003018 int ret;
3019
Jens Axboe78e19bb2019-11-06 15:21:34 -07003020 req->user_data = s->sqe->user_data;
3021
Jens Axboe9e645e112019-05-10 16:07:28 -06003022 /* enforce forwards compatibility on users */
3023 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
3024 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003025 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003026 }
3027
Jackie Liua197f662019-11-08 08:09:12 -07003028 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003029 if (unlikely(ret)) {
3030err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003031 io_cqring_add_event(req, ret);
3032 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003033 return;
3034 }
3035
Jens Axboe9e645e112019-05-10 16:07:28 -06003036 /*
3037 * If we already have a head request, queue this one for async
3038 * submittal once the head completes. If we don't have a head but
3039 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3040 * submitted sync once the chain is complete. If none of those
3041 * conditions are true (normal request), then just queue it.
3042 */
3043 if (*link) {
3044 struct io_kiocb *prev = *link;
3045
Jens Axboe94ae5e72019-11-14 19:39:52 -07003046 if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
3047 ret = io_timeout_setup(req);
3048 /* common setup allows offset being set, we don't */
3049 if (!ret && s->sqe->off)
3050 ret = -EINVAL;
3051 if (ret) {
3052 prev->flags |= REQ_F_FAIL_LINK;
3053 goto err_req;
3054 }
3055 }
3056
Jens Axboe9e645e112019-05-10 16:07:28 -06003057 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
3058 if (!sqe_copy) {
3059 ret = -EAGAIN;
3060 goto err_req;
3061 }
3062
3063 s->sqe = sqe_copy;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003064 req->flags |= REQ_F_FREE_SQE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003065 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003066 list_add_tail(&req->list, &prev->link_list);
3067 } else if (s->sqe->flags & IOSQE_IO_LINK) {
3068 req->flags |= REQ_F_LINK;
3069
Jens Axboe9e645e112019-05-10 16:07:28 -06003070 INIT_LIST_HEAD(&req->link_list);
3071 *link = req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003072 } else if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
3073 /* Only valid as a linked SQE */
3074 ret = -EINVAL;
3075 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003076 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003077 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003078 }
3079}
3080
Jens Axboe9a56a232019-01-09 09:06:50 -07003081/*
3082 * Batched submission is done, ensure local IO is flushed out.
3083 */
3084static void io_submit_state_end(struct io_submit_state *state)
3085{
3086 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003087 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003088 if (state->free_reqs)
3089 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3090 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003091}
3092
3093/*
3094 * Start submission side cache.
3095 */
3096static void io_submit_state_start(struct io_submit_state *state,
3097 struct io_ring_ctx *ctx, unsigned max_ios)
3098{
3099 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003100 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003101 state->file = NULL;
3102 state->ios_left = max_ios;
3103}
3104
Jens Axboe2b188cc2019-01-07 10:46:33 -07003105static void io_commit_sqring(struct io_ring_ctx *ctx)
3106{
Hristo Venev75b28af2019-08-26 17:23:46 +00003107 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003108
Hristo Venev75b28af2019-08-26 17:23:46 +00003109 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003110 /*
3111 * Ensure any loads from the SQEs are done at this point,
3112 * since once we write the new head, the application could
3113 * write new data to them.
3114 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003115 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003116 }
3117}
3118
3119/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003120 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3121 * that is mapped by userspace. This means that care needs to be taken to
3122 * ensure that reads are stable, as we cannot rely on userspace always
3123 * being a good citizen. If members of the sqe are validated and then later
3124 * used, it's important that those reads are done through READ_ONCE() to
3125 * prevent a re-load down the line.
3126 */
3127static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
3128{
Hristo Venev75b28af2019-08-26 17:23:46 +00003129 struct io_rings *rings = ctx->rings;
3130 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003131 unsigned head;
3132
3133 /*
3134 * The cached sq head (or cq tail) serves two purposes:
3135 *
3136 * 1) allows us to batch the cost of updating the user visible
3137 * head updates.
3138 * 2) allows the kernel side to track the head on its own, even
3139 * though the application is the one updating it.
3140 */
3141 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003142 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00003143 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003144 return false;
3145
Hristo Venev75b28af2019-08-26 17:23:46 +00003146 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003147 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003148 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003149 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003150 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003151 ctx->cached_sq_head++;
3152 return true;
3153 }
3154
3155 /* drop invalid entries */
3156 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003157 ctx->cached_sq_dropped++;
3158 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003159 return false;
3160}
3161
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003162static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003163 struct file *ring_file, int ring_fd,
3164 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003165{
3166 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003167 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003168 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003169 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003170 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003171
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003172 if (!list_empty(&ctx->cq_overflow_list)) {
3173 io_cqring_overflow_flush(ctx, false);
3174 return -EBUSY;
3175 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003176
3177 if (nr > IO_PLUG_THRESHOLD) {
3178 io_submit_state_start(&state, ctx, nr);
3179 statep = &state;
3180 }
3181
3182 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003183 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003184 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003185
Pavel Begunkov196be952019-11-07 01:41:06 +03003186 req = io_get_req(ctx, statep);
3187 if (unlikely(!req)) {
3188 if (!submitted)
3189 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003190 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003191 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003192 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003193 __io_free_req(req);
3194 break;
3195 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003196
Pavel Begunkov50585b92019-11-07 01:41:07 +03003197 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003198 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3199 if (!mm_fault) {
3200 use_mm(ctx->sqo_mm);
3201 *mm = ctx->sqo_mm;
3202 }
3203 }
3204
Pavel Begunkov50585b92019-11-07 01:41:07 +03003205 sqe_flags = req->submit.sqe->flags;
3206
3207 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08003208 if (!shadow_req) {
3209 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08003210 if (unlikely(!shadow_req))
3211 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003212 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
3213 refcount_dec(&shadow_req->refs);
3214 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003215 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003216 }
3217
Jackie Liua1041c22019-09-18 17:25:52 +08003218out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03003219 req->submit.ring_file = ring_file;
3220 req->submit.ring_fd = ring_fd;
3221 req->submit.has_user = *mm != NULL;
3222 req->submit.in_async = async;
3223 req->submit.needs_fixed_file = async;
3224 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3225 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003226 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003227 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003228
3229 /*
3230 * If previous wasn't linked and we have a linked command,
3231 * that's the end of the chain. Submit the previous link.
3232 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003233 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Jackie Liua197f662019-11-08 08:09:12 -07003234 io_queue_link_head(link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003235 link = NULL;
3236 shadow_req = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003237 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003238 }
3239
Jens Axboe9e645e112019-05-10 16:07:28 -06003240 if (link)
Jackie Liua197f662019-11-08 08:09:12 -07003241 io_queue_link_head(link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003242 if (statep)
3243 io_submit_state_end(&state);
3244
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003245 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3246 io_commit_sqring(ctx);
3247
Jens Axboe6c271ce2019-01-10 11:22:30 -07003248 return submitted;
3249}
3250
3251static int io_sq_thread(void *data)
3252{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003253 struct io_ring_ctx *ctx = data;
3254 struct mm_struct *cur_mm = NULL;
3255 mm_segment_t old_fs;
3256 DEFINE_WAIT(wait);
3257 unsigned inflight;
3258 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003259 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003260
Jens Axboe206aefd2019-11-07 18:27:42 -07003261 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003262
Jens Axboe6c271ce2019-01-10 11:22:30 -07003263 old_fs = get_fs();
3264 set_fs(USER_DS);
3265
Jens Axboec1edbf52019-11-10 16:56:04 -07003266 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003267 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003268 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003269
3270 if (inflight) {
3271 unsigned nr_events = 0;
3272
3273 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003274 /*
3275 * inflight is the count of the maximum possible
3276 * entries we submitted, but it can be smaller
3277 * if we dropped some of them. If we don't have
3278 * poll entries available, then we know that we
3279 * have nothing left to poll for. Reset the
3280 * inflight count to zero in that case.
3281 */
3282 mutex_lock(&ctx->uring_lock);
3283 if (!list_empty(&ctx->poll_list))
3284 __io_iopoll_check(ctx, &nr_events, 0);
3285 else
3286 inflight = 0;
3287 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003288 } else {
3289 /*
3290 * Normal IO, just pretend everything completed.
3291 * We don't have to poll completions for that.
3292 */
3293 nr_events = inflight;
3294 }
3295
3296 inflight -= nr_events;
3297 if (!inflight)
3298 timeout = jiffies + ctx->sq_thread_idle;
3299 }
3300
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003301 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003302
3303 /*
3304 * If submit got -EBUSY, flag us as needing the application
3305 * to enter the kernel to reap and flush events.
3306 */
3307 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003308 /*
3309 * We're polling. If we're within the defined idle
3310 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003311 * to sleep. The exception is if we got EBUSY doing
3312 * more IO, we should wait for the application to
3313 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003314 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003315 if (inflight ||
3316 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003317 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003318 continue;
3319 }
3320
3321 /*
3322 * Drop cur_mm before scheduling, we can't hold it for
3323 * long periods (or over schedule()). Do this before
3324 * adding ourselves to the waitqueue, as the unuse/drop
3325 * may sleep.
3326 */
3327 if (cur_mm) {
3328 unuse_mm(cur_mm);
3329 mmput(cur_mm);
3330 cur_mm = NULL;
3331 }
3332
3333 prepare_to_wait(&ctx->sqo_wait, &wait,
3334 TASK_INTERRUPTIBLE);
3335
3336 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003337 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003338 /* make sure to read SQ tail after writing flags */
3339 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003340
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003341 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003342 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003343 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003344 finish_wait(&ctx->sqo_wait, &wait);
3345 break;
3346 }
3347 if (signal_pending(current))
3348 flush_signals(current);
3349 schedule();
3350 finish_wait(&ctx->sqo_wait, &wait);
3351
Hristo Venev75b28af2019-08-26 17:23:46 +00003352 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003353 continue;
3354 }
3355 finish_wait(&ctx->sqo_wait, &wait);
3356
Hristo Venev75b28af2019-08-26 17:23:46 +00003357 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003358 }
3359
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003360 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003361 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3362 if (ret > 0)
3363 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003364 }
3365
3366 set_fs(old_fs);
3367 if (cur_mm) {
3368 unuse_mm(cur_mm);
3369 mmput(cur_mm);
3370 }
Jens Axboe06058632019-04-13 09:26:03 -06003371
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003372 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003373
Jens Axboe6c271ce2019-01-10 11:22:30 -07003374 return 0;
3375}
3376
Jens Axboebda52162019-09-24 13:47:15 -06003377struct io_wait_queue {
3378 struct wait_queue_entry wq;
3379 struct io_ring_ctx *ctx;
3380 unsigned to_wait;
3381 unsigned nr_timeouts;
3382};
3383
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003384static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003385{
3386 struct io_ring_ctx *ctx = iowq->ctx;
3387
3388 /*
3389 * Wake up if we have enough events, or if a timeout occured since we
3390 * started waiting. For timeouts, we always want to return to userspace,
3391 * regardless of event count.
3392 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003393 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003394 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3395}
3396
3397static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3398 int wake_flags, void *key)
3399{
3400 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3401 wq);
3402
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003403 /* use noflush == true, as we can't safely rely on locking context */
3404 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003405 return -1;
3406
3407 return autoremove_wake_function(curr, mode, wake_flags, key);
3408}
3409
Jens Axboe2b188cc2019-01-07 10:46:33 -07003410/*
3411 * Wait until events become available, if we don't already have some. The
3412 * application must reap them itself, as they reside on the shared cq ring.
3413 */
3414static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3415 const sigset_t __user *sig, size_t sigsz)
3416{
Jens Axboebda52162019-09-24 13:47:15 -06003417 struct io_wait_queue iowq = {
3418 .wq = {
3419 .private = current,
3420 .func = io_wake_function,
3421 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3422 },
3423 .ctx = ctx,
3424 .to_wait = min_events,
3425 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003426 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003427 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003428
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003429 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003430 return 0;
3431
3432 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003433#ifdef CONFIG_COMPAT
3434 if (in_compat_syscall())
3435 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003436 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003437 else
3438#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003439 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003440
Jens Axboe2b188cc2019-01-07 10:46:33 -07003441 if (ret)
3442 return ret;
3443 }
3444
Jens Axboebda52162019-09-24 13:47:15 -06003445 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003446 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003447 do {
3448 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3449 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003450 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003451 break;
3452 schedule();
3453 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003454 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003455 break;
3456 }
3457 } while (1);
3458 finish_wait(&ctx->wait, &iowq.wq);
3459
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003460 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003461
Hristo Venev75b28af2019-08-26 17:23:46 +00003462 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003463}
3464
Jens Axboe6b063142019-01-10 22:13:58 -07003465static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3466{
3467#if defined(CONFIG_UNIX)
3468 if (ctx->ring_sock) {
3469 struct sock *sock = ctx->ring_sock->sk;
3470 struct sk_buff *skb;
3471
3472 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3473 kfree_skb(skb);
3474 }
3475#else
3476 int i;
3477
Jens Axboe65e19f52019-10-26 07:20:21 -06003478 for (i = 0; i < ctx->nr_user_files; i++) {
3479 struct file *file;
3480
3481 file = io_file_from_index(ctx, i);
3482 if (file)
3483 fput(file);
3484 }
Jens Axboe6b063142019-01-10 22:13:58 -07003485#endif
3486}
3487
3488static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3489{
Jens Axboe65e19f52019-10-26 07:20:21 -06003490 unsigned nr_tables, i;
3491
3492 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003493 return -ENXIO;
3494
3495 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003496 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3497 for (i = 0; i < nr_tables; i++)
3498 kfree(ctx->file_table[i].files);
3499 kfree(ctx->file_table);
3500 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003501 ctx->nr_user_files = 0;
3502 return 0;
3503}
3504
Jens Axboe6c271ce2019-01-10 11:22:30 -07003505static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3506{
3507 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003508 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003509 /*
3510 * The park is a bit of a work-around, without it we get
3511 * warning spews on shutdown with SQPOLL set and affinity
3512 * set to a single CPU.
3513 */
Jens Axboe06058632019-04-13 09:26:03 -06003514 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003515 kthread_stop(ctx->sqo_thread);
3516 ctx->sqo_thread = NULL;
3517 }
3518}
3519
Jens Axboe6b063142019-01-10 22:13:58 -07003520static void io_finish_async(struct io_ring_ctx *ctx)
3521{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003522 io_sq_thread_stop(ctx);
3523
Jens Axboe561fb042019-10-24 07:25:42 -06003524 if (ctx->io_wq) {
3525 io_wq_destroy(ctx->io_wq);
3526 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003527 }
3528}
3529
3530#if defined(CONFIG_UNIX)
3531static void io_destruct_skb(struct sk_buff *skb)
3532{
3533 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3534
Jens Axboe561fb042019-10-24 07:25:42 -06003535 if (ctx->io_wq)
3536 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003537
Jens Axboe6b063142019-01-10 22:13:58 -07003538 unix_destruct_scm(skb);
3539}
3540
3541/*
3542 * Ensure the UNIX gc is aware of our file set, so we are certain that
3543 * the io_uring can be safely unregistered on process exit, even if we have
3544 * loops in the file referencing.
3545 */
3546static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3547{
3548 struct sock *sk = ctx->ring_sock->sk;
3549 struct scm_fp_list *fpl;
3550 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003551 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003552
3553 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3554 unsigned long inflight = ctx->user->unix_inflight + nr;
3555
3556 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3557 return -EMFILE;
3558 }
3559
3560 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3561 if (!fpl)
3562 return -ENOMEM;
3563
3564 skb = alloc_skb(0, GFP_KERNEL);
3565 if (!skb) {
3566 kfree(fpl);
3567 return -ENOMEM;
3568 }
3569
3570 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003571
Jens Axboe08a45172019-10-03 08:11:03 -06003572 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003573 fpl->user = get_uid(ctx->user);
3574 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003575 struct file *file = io_file_from_index(ctx, i + offset);
3576
3577 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003578 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003579 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003580 unix_inflight(fpl->user, fpl->fp[nr_files]);
3581 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003582 }
3583
Jens Axboe08a45172019-10-03 08:11:03 -06003584 if (nr_files) {
3585 fpl->max = SCM_MAX_FD;
3586 fpl->count = nr_files;
3587 UNIXCB(skb).fp = fpl;
3588 skb->destructor = io_destruct_skb;
3589 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3590 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003591
Jens Axboe08a45172019-10-03 08:11:03 -06003592 for (i = 0; i < nr_files; i++)
3593 fput(fpl->fp[i]);
3594 } else {
3595 kfree_skb(skb);
3596 kfree(fpl);
3597 }
Jens Axboe6b063142019-01-10 22:13:58 -07003598
3599 return 0;
3600}
3601
3602/*
3603 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3604 * causes regular reference counting to break down. We rely on the UNIX
3605 * garbage collection to take care of this problem for us.
3606 */
3607static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3608{
3609 unsigned left, total;
3610 int ret = 0;
3611
3612 total = 0;
3613 left = ctx->nr_user_files;
3614 while (left) {
3615 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003616
3617 ret = __io_sqe_files_scm(ctx, this_files, total);
3618 if (ret)
3619 break;
3620 left -= this_files;
3621 total += this_files;
3622 }
3623
3624 if (!ret)
3625 return 0;
3626
3627 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003628 struct file *file = io_file_from_index(ctx, total);
3629
3630 if (file)
3631 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003632 total++;
3633 }
3634
3635 return ret;
3636}
3637#else
3638static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3639{
3640 return 0;
3641}
3642#endif
3643
Jens Axboe65e19f52019-10-26 07:20:21 -06003644static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3645 unsigned nr_files)
3646{
3647 int i;
3648
3649 for (i = 0; i < nr_tables; i++) {
3650 struct fixed_file_table *table = &ctx->file_table[i];
3651 unsigned this_files;
3652
3653 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3654 table->files = kcalloc(this_files, sizeof(struct file *),
3655 GFP_KERNEL);
3656 if (!table->files)
3657 break;
3658 nr_files -= this_files;
3659 }
3660
3661 if (i == nr_tables)
3662 return 0;
3663
3664 for (i = 0; i < nr_tables; i++) {
3665 struct fixed_file_table *table = &ctx->file_table[i];
3666 kfree(table->files);
3667 }
3668 return 1;
3669}
3670
Jens Axboe6b063142019-01-10 22:13:58 -07003671static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3672 unsigned nr_args)
3673{
3674 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003675 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003676 int fd, ret = 0;
3677 unsigned i;
3678
Jens Axboe65e19f52019-10-26 07:20:21 -06003679 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003680 return -EBUSY;
3681 if (!nr_args)
3682 return -EINVAL;
3683 if (nr_args > IORING_MAX_FIXED_FILES)
3684 return -EMFILE;
3685
Jens Axboe65e19f52019-10-26 07:20:21 -06003686 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3687 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3688 GFP_KERNEL);
3689 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003690 return -ENOMEM;
3691
Jens Axboe65e19f52019-10-26 07:20:21 -06003692 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3693 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003694 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003695 return -ENOMEM;
3696 }
3697
Jens Axboe08a45172019-10-03 08:11:03 -06003698 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003699 struct fixed_file_table *table;
3700 unsigned index;
3701
Jens Axboe6b063142019-01-10 22:13:58 -07003702 ret = -EFAULT;
3703 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3704 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003705 /* allow sparse sets */
3706 if (fd == -1) {
3707 ret = 0;
3708 continue;
3709 }
Jens Axboe6b063142019-01-10 22:13:58 -07003710
Jens Axboe65e19f52019-10-26 07:20:21 -06003711 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3712 index = i & IORING_FILE_TABLE_MASK;
3713 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003714
3715 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003716 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003717 break;
3718 /*
3719 * Don't allow io_uring instances to be registered. If UNIX
3720 * isn't enabled, then this causes a reference cycle and this
3721 * instance can never get freed. If UNIX is enabled we'll
3722 * handle it just fine, but there's still no point in allowing
3723 * a ring fd as it doesn't support regular read/write anyway.
3724 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003725 if (table->files[index]->f_op == &io_uring_fops) {
3726 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003727 break;
3728 }
Jens Axboe6b063142019-01-10 22:13:58 -07003729 ret = 0;
3730 }
3731
3732 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003733 for (i = 0; i < ctx->nr_user_files; i++) {
3734 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003735
Jens Axboe65e19f52019-10-26 07:20:21 -06003736 file = io_file_from_index(ctx, i);
3737 if (file)
3738 fput(file);
3739 }
3740 for (i = 0; i < nr_tables; i++)
3741 kfree(ctx->file_table[i].files);
3742
3743 kfree(ctx->file_table);
3744 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003745 ctx->nr_user_files = 0;
3746 return ret;
3747 }
3748
3749 ret = io_sqe_files_scm(ctx);
3750 if (ret)
3751 io_sqe_files_unregister(ctx);
3752
3753 return ret;
3754}
3755
Jens Axboec3a31e62019-10-03 13:59:56 -06003756static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3757{
3758#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003759 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003760 struct sock *sock = ctx->ring_sock->sk;
3761 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3762 struct sk_buff *skb;
3763 int i;
3764
3765 __skb_queue_head_init(&list);
3766
3767 /*
3768 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3769 * remove this entry and rearrange the file array.
3770 */
3771 skb = skb_dequeue(head);
3772 while (skb) {
3773 struct scm_fp_list *fp;
3774
3775 fp = UNIXCB(skb).fp;
3776 for (i = 0; i < fp->count; i++) {
3777 int left;
3778
3779 if (fp->fp[i] != file)
3780 continue;
3781
3782 unix_notinflight(fp->user, fp->fp[i]);
3783 left = fp->count - 1 - i;
3784 if (left) {
3785 memmove(&fp->fp[i], &fp->fp[i + 1],
3786 left * sizeof(struct file *));
3787 }
3788 fp->count--;
3789 if (!fp->count) {
3790 kfree_skb(skb);
3791 skb = NULL;
3792 } else {
3793 __skb_queue_tail(&list, skb);
3794 }
3795 fput(file);
3796 file = NULL;
3797 break;
3798 }
3799
3800 if (!file)
3801 break;
3802
3803 __skb_queue_tail(&list, skb);
3804
3805 skb = skb_dequeue(head);
3806 }
3807
3808 if (skb_peek(&list)) {
3809 spin_lock_irq(&head->lock);
3810 while ((skb = __skb_dequeue(&list)) != NULL)
3811 __skb_queue_tail(head, skb);
3812 spin_unlock_irq(&head->lock);
3813 }
3814#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003815 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003816#endif
3817}
3818
3819static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3820 int index)
3821{
3822#if defined(CONFIG_UNIX)
3823 struct sock *sock = ctx->ring_sock->sk;
3824 struct sk_buff_head *head = &sock->sk_receive_queue;
3825 struct sk_buff *skb;
3826
3827 /*
3828 * See if we can merge this file into an existing skb SCM_RIGHTS
3829 * file set. If there's no room, fall back to allocating a new skb
3830 * and filling it in.
3831 */
3832 spin_lock_irq(&head->lock);
3833 skb = skb_peek(head);
3834 if (skb) {
3835 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3836
3837 if (fpl->count < SCM_MAX_FD) {
3838 __skb_unlink(skb, head);
3839 spin_unlock_irq(&head->lock);
3840 fpl->fp[fpl->count] = get_file(file);
3841 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3842 fpl->count++;
3843 spin_lock_irq(&head->lock);
3844 __skb_queue_head(head, skb);
3845 } else {
3846 skb = NULL;
3847 }
3848 }
3849 spin_unlock_irq(&head->lock);
3850
3851 if (skb) {
3852 fput(file);
3853 return 0;
3854 }
3855
3856 return __io_sqe_files_scm(ctx, 1, index);
3857#else
3858 return 0;
3859#endif
3860}
3861
3862static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3863 unsigned nr_args)
3864{
3865 struct io_uring_files_update up;
3866 __s32 __user *fds;
3867 int fd, i, err;
3868 __u32 done;
3869
Jens Axboe65e19f52019-10-26 07:20:21 -06003870 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003871 return -ENXIO;
3872 if (!nr_args)
3873 return -EINVAL;
3874 if (copy_from_user(&up, arg, sizeof(up)))
3875 return -EFAULT;
3876 if (check_add_overflow(up.offset, nr_args, &done))
3877 return -EOVERFLOW;
3878 if (done > ctx->nr_user_files)
3879 return -EINVAL;
3880
3881 done = 0;
3882 fds = (__s32 __user *) up.fds;
3883 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003884 struct fixed_file_table *table;
3885 unsigned index;
3886
Jens Axboec3a31e62019-10-03 13:59:56 -06003887 err = 0;
3888 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3889 err = -EFAULT;
3890 break;
3891 }
3892 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003893 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3894 index = i & IORING_FILE_TABLE_MASK;
3895 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003896 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003897 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003898 }
3899 if (fd != -1) {
3900 struct file *file;
3901
3902 file = fget(fd);
3903 if (!file) {
3904 err = -EBADF;
3905 break;
3906 }
3907 /*
3908 * Don't allow io_uring instances to be registered. If
3909 * UNIX isn't enabled, then this causes a reference
3910 * cycle and this instance can never get freed. If UNIX
3911 * is enabled we'll handle it just fine, but there's
3912 * still no point in allowing a ring fd as it doesn't
3913 * support regular read/write anyway.
3914 */
3915 if (file->f_op == &io_uring_fops) {
3916 fput(file);
3917 err = -EBADF;
3918 break;
3919 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003920 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003921 err = io_sqe_file_register(ctx, file, i);
3922 if (err)
3923 break;
3924 }
3925 nr_args--;
3926 done++;
3927 up.offset++;
3928 }
3929
3930 return done ? done : err;
3931}
3932
Jens Axboe7d723062019-11-12 22:31:31 -07003933static void io_put_work(struct io_wq_work *work)
3934{
3935 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3936
3937 io_put_req(req);
3938}
3939
3940static void io_get_work(struct io_wq_work *work)
3941{
3942 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3943
3944 refcount_inc(&req->refs);
3945}
3946
Jens Axboe6c271ce2019-01-10 11:22:30 -07003947static int io_sq_offload_start(struct io_ring_ctx *ctx,
3948 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003949{
Jens Axboe561fb042019-10-24 07:25:42 -06003950 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003951 int ret;
3952
Jens Axboe6c271ce2019-01-10 11:22:30 -07003953 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003954 mmgrab(current->mm);
3955 ctx->sqo_mm = current->mm;
3956
Jens Axboe6c271ce2019-01-10 11:22:30 -07003957 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003958 ret = -EPERM;
3959 if (!capable(CAP_SYS_ADMIN))
3960 goto err;
3961
Jens Axboe917257d2019-04-13 09:28:55 -06003962 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3963 if (!ctx->sq_thread_idle)
3964 ctx->sq_thread_idle = HZ;
3965
Jens Axboe6c271ce2019-01-10 11:22:30 -07003966 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003967 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003968
Jens Axboe917257d2019-04-13 09:28:55 -06003969 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003970 if (cpu >= nr_cpu_ids)
3971 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003972 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003973 goto err;
3974
Jens Axboe6c271ce2019-01-10 11:22:30 -07003975 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3976 ctx, cpu,
3977 "io_uring-sq");
3978 } else {
3979 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3980 "io_uring-sq");
3981 }
3982 if (IS_ERR(ctx->sqo_thread)) {
3983 ret = PTR_ERR(ctx->sqo_thread);
3984 ctx->sqo_thread = NULL;
3985 goto err;
3986 }
3987 wake_up_process(ctx->sqo_thread);
3988 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3989 /* Can't have SQ_AFF without SQPOLL */
3990 ret = -EINVAL;
3991 goto err;
3992 }
3993
Jens Axboe561fb042019-10-24 07:25:42 -06003994 /* Do QD, or 4 * CPUS, whatever is smallest */
3995 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe7d723062019-11-12 22:31:31 -07003996 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user,
3997 io_get_work, io_put_work);
Jens Axboe975c99a52019-10-30 08:42:56 -06003998 if (IS_ERR(ctx->io_wq)) {
3999 ret = PTR_ERR(ctx->io_wq);
4000 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004001 goto err;
4002 }
4003
4004 return 0;
4005err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004006 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004007 mmdrop(ctx->sqo_mm);
4008 ctx->sqo_mm = NULL;
4009 return ret;
4010}
4011
4012static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4013{
4014 atomic_long_sub(nr_pages, &user->locked_vm);
4015}
4016
4017static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4018{
4019 unsigned long page_limit, cur_pages, new_pages;
4020
4021 /* Don't allow more pages than we can safely lock */
4022 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4023
4024 do {
4025 cur_pages = atomic_long_read(&user->locked_vm);
4026 new_pages = cur_pages + nr_pages;
4027 if (new_pages > page_limit)
4028 return -ENOMEM;
4029 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4030 new_pages) != cur_pages);
4031
4032 return 0;
4033}
4034
4035static void io_mem_free(void *ptr)
4036{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004037 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004038
Mark Rutland52e04ef2019-04-30 17:30:21 +01004039 if (!ptr)
4040 return;
4041
4042 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004043 if (put_page_testzero(page))
4044 free_compound_page(page);
4045}
4046
4047static void *io_mem_alloc(size_t size)
4048{
4049 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4050 __GFP_NORETRY;
4051
4052 return (void *) __get_free_pages(gfp_flags, get_order(size));
4053}
4054
Hristo Venev75b28af2019-08-26 17:23:46 +00004055static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4056 size_t *sq_offset)
4057{
4058 struct io_rings *rings;
4059 size_t off, sq_array_size;
4060
4061 off = struct_size(rings, cqes, cq_entries);
4062 if (off == SIZE_MAX)
4063 return SIZE_MAX;
4064
4065#ifdef CONFIG_SMP
4066 off = ALIGN(off, SMP_CACHE_BYTES);
4067 if (off == 0)
4068 return SIZE_MAX;
4069#endif
4070
4071 sq_array_size = array_size(sizeof(u32), sq_entries);
4072 if (sq_array_size == SIZE_MAX)
4073 return SIZE_MAX;
4074
4075 if (check_add_overflow(off, sq_array_size, &off))
4076 return SIZE_MAX;
4077
4078 if (sq_offset)
4079 *sq_offset = off;
4080
4081 return off;
4082}
4083
Jens Axboe2b188cc2019-01-07 10:46:33 -07004084static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4085{
Hristo Venev75b28af2019-08-26 17:23:46 +00004086 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004087
Hristo Venev75b28af2019-08-26 17:23:46 +00004088 pages = (size_t)1 << get_order(
4089 rings_size(sq_entries, cq_entries, NULL));
4090 pages += (size_t)1 << get_order(
4091 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004092
Hristo Venev75b28af2019-08-26 17:23:46 +00004093 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004094}
4095
Jens Axboeedafcce2019-01-09 09:16:05 -07004096static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4097{
4098 int i, j;
4099
4100 if (!ctx->user_bufs)
4101 return -ENXIO;
4102
4103 for (i = 0; i < ctx->nr_user_bufs; i++) {
4104 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4105
4106 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004107 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004108
4109 if (ctx->account_mem)
4110 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004111 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004112 imu->nr_bvecs = 0;
4113 }
4114
4115 kfree(ctx->user_bufs);
4116 ctx->user_bufs = NULL;
4117 ctx->nr_user_bufs = 0;
4118 return 0;
4119}
4120
4121static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4122 void __user *arg, unsigned index)
4123{
4124 struct iovec __user *src;
4125
4126#ifdef CONFIG_COMPAT
4127 if (ctx->compat) {
4128 struct compat_iovec __user *ciovs;
4129 struct compat_iovec ciov;
4130
4131 ciovs = (struct compat_iovec __user *) arg;
4132 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4133 return -EFAULT;
4134
4135 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4136 dst->iov_len = ciov.iov_len;
4137 return 0;
4138 }
4139#endif
4140 src = (struct iovec __user *) arg;
4141 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4142 return -EFAULT;
4143 return 0;
4144}
4145
4146static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4147 unsigned nr_args)
4148{
4149 struct vm_area_struct **vmas = NULL;
4150 struct page **pages = NULL;
4151 int i, j, got_pages = 0;
4152 int ret = -EINVAL;
4153
4154 if (ctx->user_bufs)
4155 return -EBUSY;
4156 if (!nr_args || nr_args > UIO_MAXIOV)
4157 return -EINVAL;
4158
4159 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4160 GFP_KERNEL);
4161 if (!ctx->user_bufs)
4162 return -ENOMEM;
4163
4164 for (i = 0; i < nr_args; i++) {
4165 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4166 unsigned long off, start, end, ubuf;
4167 int pret, nr_pages;
4168 struct iovec iov;
4169 size_t size;
4170
4171 ret = io_copy_iov(ctx, &iov, arg, i);
4172 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004173 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004174
4175 /*
4176 * Don't impose further limits on the size and buffer
4177 * constraints here, we'll -EINVAL later when IO is
4178 * submitted if they are wrong.
4179 */
4180 ret = -EFAULT;
4181 if (!iov.iov_base || !iov.iov_len)
4182 goto err;
4183
4184 /* arbitrary limit, but we need something */
4185 if (iov.iov_len > SZ_1G)
4186 goto err;
4187
4188 ubuf = (unsigned long) iov.iov_base;
4189 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4190 start = ubuf >> PAGE_SHIFT;
4191 nr_pages = end - start;
4192
4193 if (ctx->account_mem) {
4194 ret = io_account_mem(ctx->user, nr_pages);
4195 if (ret)
4196 goto err;
4197 }
4198
4199 ret = 0;
4200 if (!pages || nr_pages > got_pages) {
4201 kfree(vmas);
4202 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004203 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004204 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004205 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004206 sizeof(struct vm_area_struct *),
4207 GFP_KERNEL);
4208 if (!pages || !vmas) {
4209 ret = -ENOMEM;
4210 if (ctx->account_mem)
4211 io_unaccount_mem(ctx->user, nr_pages);
4212 goto err;
4213 }
4214 got_pages = nr_pages;
4215 }
4216
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004217 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004218 GFP_KERNEL);
4219 ret = -ENOMEM;
4220 if (!imu->bvec) {
4221 if (ctx->account_mem)
4222 io_unaccount_mem(ctx->user, nr_pages);
4223 goto err;
4224 }
4225
4226 ret = 0;
4227 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004228 pret = get_user_pages(ubuf, nr_pages,
4229 FOLL_WRITE | FOLL_LONGTERM,
4230 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004231 if (pret == nr_pages) {
4232 /* don't support file backed memory */
4233 for (j = 0; j < nr_pages; j++) {
4234 struct vm_area_struct *vma = vmas[j];
4235
4236 if (vma->vm_file &&
4237 !is_file_hugepages(vma->vm_file)) {
4238 ret = -EOPNOTSUPP;
4239 break;
4240 }
4241 }
4242 } else {
4243 ret = pret < 0 ? pret : -EFAULT;
4244 }
4245 up_read(&current->mm->mmap_sem);
4246 if (ret) {
4247 /*
4248 * if we did partial map, or found file backed vmas,
4249 * release any pages we did get
4250 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004251 if (pret > 0)
4252 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004253 if (ctx->account_mem)
4254 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004255 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004256 goto err;
4257 }
4258
4259 off = ubuf & ~PAGE_MASK;
4260 size = iov.iov_len;
4261 for (j = 0; j < nr_pages; j++) {
4262 size_t vec_len;
4263
4264 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4265 imu->bvec[j].bv_page = pages[j];
4266 imu->bvec[j].bv_len = vec_len;
4267 imu->bvec[j].bv_offset = off;
4268 off = 0;
4269 size -= vec_len;
4270 }
4271 /* store original address for later verification */
4272 imu->ubuf = ubuf;
4273 imu->len = iov.iov_len;
4274 imu->nr_bvecs = nr_pages;
4275
4276 ctx->nr_user_bufs++;
4277 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004278 kvfree(pages);
4279 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004280 return 0;
4281err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004282 kvfree(pages);
4283 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004284 io_sqe_buffer_unregister(ctx);
4285 return ret;
4286}
4287
Jens Axboe9b402842019-04-11 11:45:41 -06004288static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4289{
4290 __s32 __user *fds = arg;
4291 int fd;
4292
4293 if (ctx->cq_ev_fd)
4294 return -EBUSY;
4295
4296 if (copy_from_user(&fd, fds, sizeof(*fds)))
4297 return -EFAULT;
4298
4299 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4300 if (IS_ERR(ctx->cq_ev_fd)) {
4301 int ret = PTR_ERR(ctx->cq_ev_fd);
4302 ctx->cq_ev_fd = NULL;
4303 return ret;
4304 }
4305
4306 return 0;
4307}
4308
4309static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4310{
4311 if (ctx->cq_ev_fd) {
4312 eventfd_ctx_put(ctx->cq_ev_fd);
4313 ctx->cq_ev_fd = NULL;
4314 return 0;
4315 }
4316
4317 return -ENXIO;
4318}
4319
Jens Axboe2b188cc2019-01-07 10:46:33 -07004320static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4321{
Jens Axboe6b063142019-01-10 22:13:58 -07004322 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004323 if (ctx->sqo_mm)
4324 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004325
4326 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004327 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004328 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004329 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004330
Jens Axboe2b188cc2019-01-07 10:46:33 -07004331#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004332 if (ctx->ring_sock) {
4333 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004334 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004335 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004336#endif
4337
Hristo Venev75b28af2019-08-26 17:23:46 +00004338 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004339 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004340
4341 percpu_ref_exit(&ctx->refs);
4342 if (ctx->account_mem)
4343 io_unaccount_mem(ctx->user,
4344 ring_pages(ctx->sq_entries, ctx->cq_entries));
4345 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004346 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004347 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004348 kfree(ctx);
4349}
4350
4351static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4352{
4353 struct io_ring_ctx *ctx = file->private_data;
4354 __poll_t mask = 0;
4355
4356 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004357 /*
4358 * synchronizes with barrier from wq_has_sleeper call in
4359 * io_commit_cqring
4360 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004361 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004362 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4363 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004364 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004365 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004366 mask |= EPOLLIN | EPOLLRDNORM;
4367
4368 return mask;
4369}
4370
4371static int io_uring_fasync(int fd, struct file *file, int on)
4372{
4373 struct io_ring_ctx *ctx = file->private_data;
4374
4375 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4376}
4377
4378static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4379{
4380 mutex_lock(&ctx->uring_lock);
4381 percpu_ref_kill(&ctx->refs);
4382 mutex_unlock(&ctx->uring_lock);
4383
Jens Axboe5262f562019-09-17 12:26:57 -06004384 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004385 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004386
4387 if (ctx->io_wq)
4388 io_wq_cancel_all(ctx->io_wq);
4389
Jens Axboedef596e2019-01-09 08:59:42 -07004390 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004391 /* if we failed setting up the ctx, we might not have any rings */
4392 if (ctx->rings)
4393 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004394 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004395 io_ring_ctx_free(ctx);
4396}
4397
4398static int io_uring_release(struct inode *inode, struct file *file)
4399{
4400 struct io_ring_ctx *ctx = file->private_data;
4401
4402 file->private_data = NULL;
4403 io_ring_ctx_wait_and_kill(ctx);
4404 return 0;
4405}
4406
Jens Axboefcb323c2019-10-24 12:39:47 -06004407static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4408 struct files_struct *files)
4409{
4410 struct io_kiocb *req;
4411 DEFINE_WAIT(wait);
4412
4413 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004414 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004415
4416 spin_lock_irq(&ctx->inflight_lock);
4417 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004418 if (req->work.files != files)
4419 continue;
4420 /* req is being completed, ignore */
4421 if (!refcount_inc_not_zero(&req->refs))
4422 continue;
4423 cancel_req = req;
4424 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004425 }
Jens Axboe768134d2019-11-10 20:30:53 -07004426 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004427 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004428 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004429 spin_unlock_irq(&ctx->inflight_lock);
4430
Jens Axboe768134d2019-11-10 20:30:53 -07004431 /* We need to keep going until we don't find a matching req */
4432 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004433 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004434
4435 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4436 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004437 schedule();
4438 }
Jens Axboe768134d2019-11-10 20:30:53 -07004439 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004440}
4441
4442static int io_uring_flush(struct file *file, void *data)
4443{
4444 struct io_ring_ctx *ctx = file->private_data;
4445
4446 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004447 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4448 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004449 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004450 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004451 return 0;
4452}
4453
Jens Axboe2b188cc2019-01-07 10:46:33 -07004454static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4455{
4456 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4457 unsigned long sz = vma->vm_end - vma->vm_start;
4458 struct io_ring_ctx *ctx = file->private_data;
4459 unsigned long pfn;
4460 struct page *page;
4461 void *ptr;
4462
4463 switch (offset) {
4464 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004465 case IORING_OFF_CQ_RING:
4466 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004467 break;
4468 case IORING_OFF_SQES:
4469 ptr = ctx->sq_sqes;
4470 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004471 default:
4472 return -EINVAL;
4473 }
4474
4475 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004476 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004477 return -EINVAL;
4478
4479 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4480 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4481}
4482
4483SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4484 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4485 size_t, sigsz)
4486{
4487 struct io_ring_ctx *ctx;
4488 long ret = -EBADF;
4489 int submitted = 0;
4490 struct fd f;
4491
Jens Axboe6c271ce2019-01-10 11:22:30 -07004492 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004493 return -EINVAL;
4494
4495 f = fdget(fd);
4496 if (!f.file)
4497 return -EBADF;
4498
4499 ret = -EOPNOTSUPP;
4500 if (f.file->f_op != &io_uring_fops)
4501 goto out_fput;
4502
4503 ret = -ENXIO;
4504 ctx = f.file->private_data;
4505 if (!percpu_ref_tryget(&ctx->refs))
4506 goto out_fput;
4507
Jens Axboe6c271ce2019-01-10 11:22:30 -07004508 /*
4509 * For SQ polling, the thread will do all submissions and completions.
4510 * Just return the requested submit count, and wake the thread if
4511 * we were asked to.
4512 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004513 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004514 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004515 if (!list_empty_careful(&ctx->cq_overflow_list))
4516 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004517 if (flags & IORING_ENTER_SQ_WAKEUP)
4518 wake_up(&ctx->sqo_wait);
4519 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004520 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004521 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004522
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004523 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004524 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004525 /* already have mm, so io_submit_sqes() won't try to grab it */
4526 cur_mm = ctx->sqo_mm;
4527 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4528 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004529 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004530 }
4531 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004532 unsigned nr_events = 0;
4533
Jens Axboe2b188cc2019-01-07 10:46:33 -07004534 min_complete = min(min_complete, ctx->cq_entries);
4535
Jens Axboedef596e2019-01-09 08:59:42 -07004536 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004537 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004538 } else {
4539 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4540 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004541 }
4542
Pavel Begunkov6805b322019-10-08 02:18:42 +03004543 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004544out_fput:
4545 fdput(f);
4546 return submitted ? submitted : ret;
4547}
4548
4549static const struct file_operations io_uring_fops = {
4550 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004551 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004552 .mmap = io_uring_mmap,
4553 .poll = io_uring_poll,
4554 .fasync = io_uring_fasync,
4555};
4556
4557static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4558 struct io_uring_params *p)
4559{
Hristo Venev75b28af2019-08-26 17:23:46 +00004560 struct io_rings *rings;
4561 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004562
Hristo Venev75b28af2019-08-26 17:23:46 +00004563 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4564 if (size == SIZE_MAX)
4565 return -EOVERFLOW;
4566
4567 rings = io_mem_alloc(size);
4568 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004569 return -ENOMEM;
4570
Hristo Venev75b28af2019-08-26 17:23:46 +00004571 ctx->rings = rings;
4572 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4573 rings->sq_ring_mask = p->sq_entries - 1;
4574 rings->cq_ring_mask = p->cq_entries - 1;
4575 rings->sq_ring_entries = p->sq_entries;
4576 rings->cq_ring_entries = p->cq_entries;
4577 ctx->sq_mask = rings->sq_ring_mask;
4578 ctx->cq_mask = rings->cq_ring_mask;
4579 ctx->sq_entries = rings->sq_ring_entries;
4580 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004581
4582 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4583 if (size == SIZE_MAX)
4584 return -EOVERFLOW;
4585
4586 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004587 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004588 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004589
Jens Axboe2b188cc2019-01-07 10:46:33 -07004590 return 0;
4591}
4592
4593/*
4594 * Allocate an anonymous fd, this is what constitutes the application
4595 * visible backing of an io_uring instance. The application mmaps this
4596 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4597 * we have to tie this fd to a socket for file garbage collection purposes.
4598 */
4599static int io_uring_get_fd(struct io_ring_ctx *ctx)
4600{
4601 struct file *file;
4602 int ret;
4603
4604#if defined(CONFIG_UNIX)
4605 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4606 &ctx->ring_sock);
4607 if (ret)
4608 return ret;
4609#endif
4610
4611 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4612 if (ret < 0)
4613 goto err;
4614
4615 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4616 O_RDWR | O_CLOEXEC);
4617 if (IS_ERR(file)) {
4618 put_unused_fd(ret);
4619 ret = PTR_ERR(file);
4620 goto err;
4621 }
4622
4623#if defined(CONFIG_UNIX)
4624 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004625 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004626#endif
4627 fd_install(ret, file);
4628 return ret;
4629err:
4630#if defined(CONFIG_UNIX)
4631 sock_release(ctx->ring_sock);
4632 ctx->ring_sock = NULL;
4633#endif
4634 return ret;
4635}
4636
4637static int io_uring_create(unsigned entries, struct io_uring_params *p)
4638{
4639 struct user_struct *user = NULL;
4640 struct io_ring_ctx *ctx;
4641 bool account_mem;
4642 int ret;
4643
4644 if (!entries || entries > IORING_MAX_ENTRIES)
4645 return -EINVAL;
4646
4647 /*
4648 * Use twice as many entries for the CQ ring. It's possible for the
4649 * application to drive a higher depth than the size of the SQ ring,
4650 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004651 * some flexibility in overcommitting a bit. If the application has
4652 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4653 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004654 */
4655 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004656 if (p->flags & IORING_SETUP_CQSIZE) {
4657 /*
4658 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4659 * to a power-of-two, if it isn't already. We do NOT impose
4660 * any cq vs sq ring sizing.
4661 */
4662 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4663 return -EINVAL;
4664 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4665 } else {
4666 p->cq_entries = 2 * p->sq_entries;
4667 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004668
4669 user = get_uid(current_user());
4670 account_mem = !capable(CAP_IPC_LOCK);
4671
4672 if (account_mem) {
4673 ret = io_account_mem(user,
4674 ring_pages(p->sq_entries, p->cq_entries));
4675 if (ret) {
4676 free_uid(user);
4677 return ret;
4678 }
4679 }
4680
4681 ctx = io_ring_ctx_alloc(p);
4682 if (!ctx) {
4683 if (account_mem)
4684 io_unaccount_mem(user, ring_pages(p->sq_entries,
4685 p->cq_entries));
4686 free_uid(user);
4687 return -ENOMEM;
4688 }
4689 ctx->compat = in_compat_syscall();
4690 ctx->account_mem = account_mem;
4691 ctx->user = user;
4692
4693 ret = io_allocate_scq_urings(ctx, p);
4694 if (ret)
4695 goto err;
4696
Jens Axboe6c271ce2019-01-10 11:22:30 -07004697 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004698 if (ret)
4699 goto err;
4700
Jens Axboe2b188cc2019-01-07 10:46:33 -07004701 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004702 p->sq_off.head = offsetof(struct io_rings, sq.head);
4703 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4704 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4705 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4706 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4707 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4708 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004709
4710 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004711 p->cq_off.head = offsetof(struct io_rings, cq.head);
4712 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4713 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4714 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4715 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4716 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004717
Jens Axboe044c1ab2019-10-28 09:15:33 -06004718 /*
4719 * Install ring fd as the very last thing, so we don't risk someone
4720 * having closed it before we finish setup
4721 */
4722 ret = io_uring_get_fd(ctx);
4723 if (ret < 0)
4724 goto err;
4725
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004726 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004727 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004728 return ret;
4729err:
4730 io_ring_ctx_wait_and_kill(ctx);
4731 return ret;
4732}
4733
4734/*
4735 * Sets up an aio uring context, and returns the fd. Applications asks for a
4736 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4737 * params structure passed in.
4738 */
4739static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4740{
4741 struct io_uring_params p;
4742 long ret;
4743 int i;
4744
4745 if (copy_from_user(&p, params, sizeof(p)))
4746 return -EFAULT;
4747 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4748 if (p.resv[i])
4749 return -EINVAL;
4750 }
4751
Jens Axboe6c271ce2019-01-10 11:22:30 -07004752 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004753 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004754 return -EINVAL;
4755
4756 ret = io_uring_create(entries, &p);
4757 if (ret < 0)
4758 return ret;
4759
4760 if (copy_to_user(params, &p, sizeof(p)))
4761 return -EFAULT;
4762
4763 return ret;
4764}
4765
4766SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4767 struct io_uring_params __user *, params)
4768{
4769 return io_uring_setup(entries, params);
4770}
4771
Jens Axboeedafcce2019-01-09 09:16:05 -07004772static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4773 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004774 __releases(ctx->uring_lock)
4775 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004776{
4777 int ret;
4778
Jens Axboe35fa71a2019-04-22 10:23:23 -06004779 /*
4780 * We're inside the ring mutex, if the ref is already dying, then
4781 * someone else killed the ctx or is already going through
4782 * io_uring_register().
4783 */
4784 if (percpu_ref_is_dying(&ctx->refs))
4785 return -ENXIO;
4786
Jens Axboeedafcce2019-01-09 09:16:05 -07004787 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004788
4789 /*
4790 * Drop uring mutex before waiting for references to exit. If another
4791 * thread is currently inside io_uring_enter() it might need to grab
4792 * the uring_lock to make progress. If we hold it here across the drain
4793 * wait, then we can deadlock. It's safe to drop the mutex here, since
4794 * no new references will come in after we've killed the percpu ref.
4795 */
4796 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004797 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004798 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004799
4800 switch (opcode) {
4801 case IORING_REGISTER_BUFFERS:
4802 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4803 break;
4804 case IORING_UNREGISTER_BUFFERS:
4805 ret = -EINVAL;
4806 if (arg || nr_args)
4807 break;
4808 ret = io_sqe_buffer_unregister(ctx);
4809 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004810 case IORING_REGISTER_FILES:
4811 ret = io_sqe_files_register(ctx, arg, nr_args);
4812 break;
4813 case IORING_UNREGISTER_FILES:
4814 ret = -EINVAL;
4815 if (arg || nr_args)
4816 break;
4817 ret = io_sqe_files_unregister(ctx);
4818 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004819 case IORING_REGISTER_FILES_UPDATE:
4820 ret = io_sqe_files_update(ctx, arg, nr_args);
4821 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004822 case IORING_REGISTER_EVENTFD:
4823 ret = -EINVAL;
4824 if (nr_args != 1)
4825 break;
4826 ret = io_eventfd_register(ctx, arg);
4827 break;
4828 case IORING_UNREGISTER_EVENTFD:
4829 ret = -EINVAL;
4830 if (arg || nr_args)
4831 break;
4832 ret = io_eventfd_unregister(ctx);
4833 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004834 default:
4835 ret = -EINVAL;
4836 break;
4837 }
4838
4839 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004840 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004841 percpu_ref_reinit(&ctx->refs);
4842 return ret;
4843}
4844
4845SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4846 void __user *, arg, unsigned int, nr_args)
4847{
4848 struct io_ring_ctx *ctx;
4849 long ret = -EBADF;
4850 struct fd f;
4851
4852 f = fdget(fd);
4853 if (!f.file)
4854 return -EBADF;
4855
4856 ret = -EOPNOTSUPP;
4857 if (f.file->f_op != &io_uring_fops)
4858 goto out_fput;
4859
4860 ctx = f.file->private_data;
4861
4862 mutex_lock(&ctx->uring_lock);
4863 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4864 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004865 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4866 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004867out_fput:
4868 fdput(f);
4869 return ret;
4870}
4871
Jens Axboe2b188cc2019-01-07 10:46:33 -07004872static int __init io_uring_init(void)
4873{
4874 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4875 return 0;
4876};
4877__initcall(io_uring_init);