blob: 1da103df2bfaef50e029a375563d41652453b613 [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 Axboe5262f562019-09-17 12:26:57 -0600304struct io_timeout {
305 struct file *file;
306 struct hrtimer timer;
307};
308
Jens Axboe09bb8392019-03-13 12:39:28 -0600309/*
310 * NOTE! Each of the iocb union members has the file pointer
311 * as the first entry in their struct definition. So you can
312 * access the file pointer through any of the sub-structs,
313 * or directly as just 'ki_filp' in this struct.
314 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700315struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700316 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600317 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700318 struct kiocb rw;
319 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600320 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700321 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700322
323 struct sqe_submit submit;
324
325 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700326 union {
327 struct list_head list;
328 struct rb_node rb_node;
329 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600330 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700332 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200333#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700334#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700335#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700336#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200337#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
338#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600339#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700340#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800341#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800342#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600343#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600344#define REQ_F_ISREG 2048 /* regular file */
345#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700346#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800347#define REQ_F_INFLIGHT 16384 /* on inflight list */
348#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700349 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600350 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600351 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700352
Jens Axboefcb323c2019-10-24 12:39:47 -0600353 struct list_head inflight_entry;
354
Jens Axboe561fb042019-10-24 07:25:42 -0600355 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700356};
357
358#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700359#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700360
Jens Axboe9a56a232019-01-09 09:06:50 -0700361struct io_submit_state {
362 struct blk_plug plug;
363
364 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700365 * io_kiocb alloc cache
366 */
367 void *reqs[IO_IOPOLL_BATCH];
368 unsigned int free_reqs;
369 unsigned int cur_req;
370
371 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700372 * File reference cache
373 */
374 struct file *file;
375 unsigned int fd;
376 unsigned int has_refs;
377 unsigned int used_refs;
378 unsigned int ios_left;
379};
380
Jens Axboe561fb042019-10-24 07:25:42 -0600381static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700382static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800383static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800384static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700385static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700386static void __io_double_put_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600387
Jens Axboe2b188cc2019-01-07 10:46:33 -0700388static struct kmem_cache *req_cachep;
389
390static const struct file_operations io_uring_fops;
391
392struct sock *io_uring_get_socket(struct file *file)
393{
394#if defined(CONFIG_UNIX)
395 if (file->f_op == &io_uring_fops) {
396 struct io_ring_ctx *ctx = file->private_data;
397
398 return ctx->ring_sock->sk;
399 }
400#endif
401 return NULL;
402}
403EXPORT_SYMBOL(io_uring_get_socket);
404
405static void io_ring_ctx_ref_free(struct percpu_ref *ref)
406{
407 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
408
Jens Axboe206aefd2019-11-07 18:27:42 -0700409 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410}
411
412static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
413{
414 struct io_ring_ctx *ctx;
415
416 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
417 if (!ctx)
418 return NULL;
419
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700420 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
421 if (!ctx->fallback_req)
422 goto err;
423
Jens Axboe206aefd2019-11-07 18:27:42 -0700424 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
425 if (!ctx->completions)
426 goto err;
427
Roman Gushchin21482892019-05-07 10:01:48 -0700428 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700429 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
430 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700431
432 ctx->flags = p->flags;
433 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700434 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700435 init_completion(&ctx->completions[0]);
436 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700437 mutex_init(&ctx->uring_lock);
438 init_waitqueue_head(&ctx->wait);
439 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700440 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboeeac406c2019-11-14 12:09:58 -0700441 ctx->cancel_tree = RB_ROOT;
Jens Axboede0617e2019-04-06 21:51:27 -0600442 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600443 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600444 init_waitqueue_head(&ctx->inflight_wait);
445 spin_lock_init(&ctx->inflight_lock);
446 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700447 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700448err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700449 if (ctx->fallback_req)
450 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700451 kfree(ctx->completions);
452 kfree(ctx);
453 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700454}
455
Bob Liu9d858b22019-11-13 18:06:25 +0800456static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600457{
Jackie Liua197f662019-11-08 08:09:12 -0700458 struct io_ring_ctx *ctx = req->ctx;
459
Jens Axboe498ccd92019-10-25 10:04:25 -0600460 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
461 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600462}
463
Bob Liu9d858b22019-11-13 18:06:25 +0800464static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600465{
Bob Liu9d858b22019-11-13 18:06:25 +0800466 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
467 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600468
Bob Liu9d858b22019-11-13 18:06:25 +0800469 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600470}
471
472static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600473{
474 struct io_kiocb *req;
475
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600476 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800477 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600478 list_del_init(&req->list);
479 return req;
480 }
481
482 return NULL;
483}
484
Jens Axboe5262f562019-09-17 12:26:57 -0600485static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
486{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600487 struct io_kiocb *req;
488
489 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700490 if (req) {
491 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
492 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800493 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700494 list_del_init(&req->list);
495 return req;
496 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600497 }
498
499 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600500}
501
Jens Axboede0617e2019-04-06 21:51:27 -0600502static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700503{
Hristo Venev75b28af2019-08-26 17:23:46 +0000504 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700505
Hristo Venev75b28af2019-08-26 17:23:46 +0000506 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700507 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000508 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700509
Jens Axboe2b188cc2019-01-07 10:46:33 -0700510 if (wq_has_sleeper(&ctx->cq_wait)) {
511 wake_up_interruptible(&ctx->cq_wait);
512 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
513 }
514 }
515}
516
Jens Axboe561fb042019-10-24 07:25:42 -0600517static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600518{
Jens Axboe561fb042019-10-24 07:25:42 -0600519 u8 opcode = READ_ONCE(sqe->opcode);
520
521 return !(opcode == IORING_OP_READ_FIXED ||
522 opcode == IORING_OP_WRITE_FIXED);
523}
524
525static inline bool io_prep_async_work(struct io_kiocb *req)
526{
527 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600528
Jens Axboe6cc47d12019-09-18 11:18:23 -0600529 if (req->submit.sqe) {
530 switch (req->submit.sqe->opcode) {
531 case IORING_OP_WRITEV:
532 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600533 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700534 /* fall-through */
535 case IORING_OP_READV:
536 case IORING_OP_READ_FIXED:
537 case IORING_OP_SENDMSG:
538 case IORING_OP_RECVMSG:
539 case IORING_OP_ACCEPT:
540 case IORING_OP_POLL_ADD:
541 /*
542 * We know REQ_F_ISREG is not set on some of these
543 * opcodes, but this enables us to keep the check in
544 * just one place.
545 */
546 if (!(req->flags & REQ_F_ISREG))
547 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600548 break;
549 }
Jens Axboe561fb042019-10-24 07:25:42 -0600550 if (io_sqe_needs_user(req->submit.sqe))
551 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600552 }
553
Jens Axboe561fb042019-10-24 07:25:42 -0600554 return do_hashed;
555}
556
Jackie Liua197f662019-11-08 08:09:12 -0700557static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600558{
559 bool do_hashed = io_prep_async_work(req);
Jackie Liua197f662019-11-08 08:09:12 -0700560 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -0600561
562 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
563 req->flags);
564 if (!do_hashed) {
565 io_wq_enqueue(ctx->io_wq, &req->work);
566 } else {
567 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
568 file_inode(req->file));
569 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600570}
571
Jens Axboe5262f562019-09-17 12:26:57 -0600572static void io_kill_timeout(struct io_kiocb *req)
573{
574 int ret;
575
576 ret = hrtimer_try_to_cancel(&req->timeout.timer);
577 if (ret != -1) {
578 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600579 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700580 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800581 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600582 }
583}
584
585static void io_kill_timeouts(struct io_ring_ctx *ctx)
586{
587 struct io_kiocb *req, *tmp;
588
589 spin_lock_irq(&ctx->completion_lock);
590 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
591 io_kill_timeout(req);
592 spin_unlock_irq(&ctx->completion_lock);
593}
594
Jens Axboede0617e2019-04-06 21:51:27 -0600595static void io_commit_cqring(struct io_ring_ctx *ctx)
596{
597 struct io_kiocb *req;
598
Jens Axboe5262f562019-09-17 12:26:57 -0600599 while ((req = io_get_timeout_req(ctx)) != NULL)
600 io_kill_timeout(req);
601
Jens Axboede0617e2019-04-06 21:51:27 -0600602 __io_commit_cqring(ctx);
603
604 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800605 if (req->flags & REQ_F_SHADOW_DRAIN) {
606 /* Just for drain, free it. */
607 __io_free_req(req);
608 continue;
609 }
Jens Axboede0617e2019-04-06 21:51:27 -0600610 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700611 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600612 }
613}
614
Jens Axboe2b188cc2019-01-07 10:46:33 -0700615static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
616{
Hristo Venev75b28af2019-08-26 17:23:46 +0000617 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700618 unsigned tail;
619
620 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200621 /*
622 * writes to the cq entry need to come after reading head; the
623 * control dependency is enough as we're using WRITE_ONCE to
624 * fill the cq entry
625 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000626 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700627 return NULL;
628
629 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000630 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700631}
632
Jens Axboe8c838782019-03-12 15:48:16 -0600633static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
634{
635 if (waitqueue_active(&ctx->wait))
636 wake_up(&ctx->wait);
637 if (waitqueue_active(&ctx->sqo_wait))
638 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600639 if (ctx->cq_ev_fd)
640 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600641}
642
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700643static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700644{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700645 struct io_rings *rings = ctx->rings;
646 struct io_uring_cqe *cqe;
647 struct io_kiocb *req;
648 unsigned long flags;
649 LIST_HEAD(list);
650
651 if (!force) {
652 if (list_empty_careful(&ctx->cq_overflow_list))
653 return;
654 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
655 rings->cq_ring_entries))
656 return;
657 }
658
659 spin_lock_irqsave(&ctx->completion_lock, flags);
660
661 /* if force is set, the ring is going away. always drop after that */
662 if (force)
663 ctx->cq_overflow_flushed = true;
664
665 while (!list_empty(&ctx->cq_overflow_list)) {
666 cqe = io_get_cqring(ctx);
667 if (!cqe && !force)
668 break;
669
670 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
671 list);
672 list_move(&req->list, &list);
673 if (cqe) {
674 WRITE_ONCE(cqe->user_data, req->user_data);
675 WRITE_ONCE(cqe->res, req->result);
676 WRITE_ONCE(cqe->flags, 0);
677 } else {
678 WRITE_ONCE(ctx->rings->cq_overflow,
679 atomic_inc_return(&ctx->cached_cq_overflow));
680 }
681 }
682
683 io_commit_cqring(ctx);
684 spin_unlock_irqrestore(&ctx->completion_lock, flags);
685 io_cqring_ev_posted(ctx);
686
687 while (!list_empty(&list)) {
688 req = list_first_entry(&list, struct io_kiocb, list);
689 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800690 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700691 }
692}
693
Jens Axboe78e19bb2019-11-06 15:21:34 -0700694static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700695{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700696 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700697 struct io_uring_cqe *cqe;
698
Jens Axboe78e19bb2019-11-06 15:21:34 -0700699 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700700
Jens Axboe2b188cc2019-01-07 10:46:33 -0700701 /*
702 * If we can't get a cq entry, userspace overflowed the
703 * submission (by quite a lot). Increment the overflow count in
704 * the ring.
705 */
706 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700707 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700708 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700709 WRITE_ONCE(cqe->res, res);
710 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700711 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700712 WRITE_ONCE(ctx->rings->cq_overflow,
713 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700714 } else {
715 refcount_inc(&req->refs);
716 req->result = res;
717 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700718 }
719}
720
Jens Axboe78e19bb2019-11-06 15:21:34 -0700721static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700722{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700723 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700724 unsigned long flags;
725
726 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700727 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700728 io_commit_cqring(ctx);
729 spin_unlock_irqrestore(&ctx->completion_lock, flags);
730
Jens Axboe8c838782019-03-12 15:48:16 -0600731 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700732}
733
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700734static inline bool io_is_fallback_req(struct io_kiocb *req)
735{
736 return req == (struct io_kiocb *)
737 ((unsigned long) req->ctx->fallback_req & ~1UL);
738}
739
740static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
741{
742 struct io_kiocb *req;
743
744 req = ctx->fallback_req;
745 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
746 return req;
747
748 return NULL;
749}
750
Jens Axboe2579f912019-01-09 09:10:43 -0700751static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
752 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700753{
Jens Axboefd6fab22019-03-14 16:30:06 -0600754 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700755 struct io_kiocb *req;
756
757 if (!percpu_ref_tryget(&ctx->refs))
758 return NULL;
759
Jens Axboe2579f912019-01-09 09:10:43 -0700760 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600761 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700762 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700763 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700764 } else if (!state->free_reqs) {
765 size_t sz;
766 int ret;
767
768 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600769 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
770
771 /*
772 * Bulk alloc is all-or-nothing. If we fail to get a batch,
773 * retry single alloc to be on the safe side.
774 */
775 if (unlikely(ret <= 0)) {
776 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
777 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700778 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600779 ret = 1;
780 }
Jens Axboe2579f912019-01-09 09:10:43 -0700781 state->free_reqs = ret - 1;
782 state->cur_req = 1;
783 req = state->reqs[0];
784 } else {
785 req = state->reqs[state->cur_req];
786 state->free_reqs--;
787 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700788 }
789
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700790got_it:
Jens Axboe60c112b2019-06-21 10:20:18 -0600791 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700792 req->ctx = ctx;
793 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600794 /* one is dropped after submission, the other at completion */
795 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600796 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600797 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700798 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700799fallback:
800 req = io_get_fallback_req(ctx);
801 if (req)
802 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300803 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700804 return NULL;
805}
806
Jens Axboedef596e2019-01-09 08:59:42 -0700807static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
808{
809 if (*nr) {
810 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300811 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700812 *nr = 0;
813 }
814}
815
Jens Axboe9e645e112019-05-10 16:07:28 -0600816static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700817{
Jens Axboefcb323c2019-10-24 12:39:47 -0600818 struct io_ring_ctx *ctx = req->ctx;
819
Jens Axboe09bb8392019-03-13 12:39:28 -0600820 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
821 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600822 if (req->flags & REQ_F_INFLIGHT) {
823 unsigned long flags;
824
825 spin_lock_irqsave(&ctx->inflight_lock, flags);
826 list_del(&req->inflight_entry);
827 if (waitqueue_active(&ctx->inflight_wait))
828 wake_up(&ctx->inflight_wait);
829 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
830 }
831 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700832 if (likely(!io_is_fallback_req(req)))
833 kmem_cache_free(req_cachep, req);
834 else
835 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600836}
837
Jackie Liua197f662019-11-08 08:09:12 -0700838static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600839{
Jackie Liua197f662019-11-08 08:09:12 -0700840 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700841 int ret;
842
843 ret = hrtimer_try_to_cancel(&req->timeout.timer);
844 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700845 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700846 io_commit_cqring(ctx);
847 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800848 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700849 return true;
850 }
851
852 return false;
853}
854
Jens Axboeba816ad2019-09-28 11:36:45 -0600855static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600856{
Jens Axboe2665abf2019-11-05 12:40:47 -0700857 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600858 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700859 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600860
861 /*
862 * The list should never be empty when we are called here. But could
863 * potentially happen if the chain is messed up, check to be on the
864 * safe side.
865 */
866 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700867 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700868 list_del_init(&nxt->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600869 if (!list_empty(&req->link_list)) {
870 INIT_LIST_HEAD(&nxt->link_list);
871 list_splice(&req->link_list, &nxt->link_list);
872 nxt->flags |= REQ_F_LINK;
873 }
874
Jens Axboeba816ad2019-09-28 11:36:45 -0600875 /*
876 * If we're in async work, we can continue processing the chain
877 * in this context instead of having to queue up new async work.
878 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700879 if (req->flags & REQ_F_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700880 wake_ev = io_link_cancel_timeout(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700881
882 /* we dropped this link, get next */
883 nxt = list_first_entry_or_null(&req->link_list,
884 struct io_kiocb, list);
Jens Axboe960e4322019-11-12 07:56:39 -0700885 } else if (nxtptr && io_wq_current_is_worker()) {
Jens Axboeba816ad2019-09-28 11:36:45 -0600886 *nxtptr = nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700887 break;
888 } else {
Jackie Liua197f662019-11-08 08:09:12 -0700889 io_queue_async_work(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700890 break;
891 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600892 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700893
894 if (wake_ev)
895 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600896}
897
898/*
899 * Called if REQ_F_LINK is set, and we fail the head request
900 */
901static void io_fail_links(struct io_kiocb *req)
902{
Jens Axboe2665abf2019-11-05 12:40:47 -0700903 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600904 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700905 unsigned long flags;
906
907 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600908
909 while (!list_empty(&req->link_list)) {
910 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700911 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600912
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200913 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700914
915 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
916 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700917 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700918 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700919 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700920 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700921 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600922 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700923
924 io_commit_cqring(ctx);
925 spin_unlock_irqrestore(&ctx->completion_lock, flags);
926 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600927}
928
Jackie Liuc69f8db2019-11-09 11:00:08 +0800929static void io_free_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600930{
Jens Axboe2665abf2019-11-05 12:40:47 -0700931 if (likely(!(req->flags & REQ_F_LINK))) {
932 __io_free_req(req);
933 return;
934 }
935
Jens Axboe9e645e112019-05-10 16:07:28 -0600936 /*
937 * If LINK is set, we have dependent requests in this chain. If we
938 * didn't fail this request, queue the first one up, moving any other
939 * dependencies to the next request. In case of failure, fail the rest
940 * of the chain.
941 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700942 if (req->flags & REQ_F_FAIL_LINK) {
943 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700944 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
945 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700946 struct io_ring_ctx *ctx = req->ctx;
947 unsigned long flags;
948
949 /*
950 * If this is a timeout link, we could be racing with the
951 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700952 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700953 */
954 spin_lock_irqsave(&ctx->completion_lock, flags);
955 io_req_link_next(req, nxt);
956 spin_unlock_irqrestore(&ctx->completion_lock, flags);
957 } else {
958 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600959 }
960
961 __io_free_req(req);
962}
963
Jackie Liuc69f8db2019-11-09 11:00:08 +0800964static void io_free_req(struct io_kiocb *req)
965{
966 io_free_req_find_next(req, NULL);
967}
968
Jens Axboeba816ad2019-09-28 11:36:45 -0600969/*
970 * Drop reference to request, return next in chain (if there is one) if this
971 * was the last reference to this request.
972 */
Jackie Liuec9c02a2019-11-08 23:50:36 +0800973static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -0600974{
Jens Axboeba816ad2019-09-28 11:36:45 -0600975 struct io_kiocb *nxt = NULL;
976
Jens Axboee65ef562019-03-12 10:16:44 -0600977 if (refcount_dec_and_test(&req->refs))
Jackie Liuc69f8db2019-11-09 11:00:08 +0800978 io_free_req_find_next(req, &nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600979
Jens Axboeba816ad2019-09-28 11:36:45 -0600980 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600981 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600982 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600983 else
Jackie Liua197f662019-11-08 08:09:12 -0700984 io_queue_async_work(nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600985 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700986}
987
Jens Axboe2b188cc2019-01-07 10:46:33 -0700988static void io_put_req(struct io_kiocb *req)
989{
Jens Axboedef596e2019-01-09 08:59:42 -0700990 if (refcount_dec_and_test(&req->refs))
991 io_free_req(req);
992}
993
Jens Axboe978db572019-11-14 22:39:04 -0700994/*
995 * Must only be used if we don't need to care about links, usually from
996 * within the completion handling itself.
997 */
998static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -0600999{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001000 /* drop both submit and complete references */
1001 if (refcount_sub_and_test(2, &req->refs))
1002 __io_free_req(req);
1003}
1004
Jens Axboe978db572019-11-14 22:39:04 -07001005static void io_double_put_req(struct io_kiocb *req)
1006{
1007 /* drop both submit and complete references */
1008 if (refcount_sub_and_test(2, &req->refs))
1009 io_free_req(req);
1010}
1011
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001012static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001013{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001014 struct io_rings *rings = ctx->rings;
1015
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001016 /*
1017 * noflush == true is from the waitqueue handler, just ensure we wake
1018 * up the task, and the next invocation will flush the entries. We
1019 * cannot safely to it from here.
1020 */
1021 if (noflush && !list_empty(&ctx->cq_overflow_list))
1022 return -1U;
1023
1024 io_cqring_overflow_flush(ctx, false);
1025
Jens Axboea3a0e432019-08-20 11:03:11 -06001026 /* See comment at the top of this file */
1027 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001028 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001029}
1030
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001031static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1032{
1033 struct io_rings *rings = ctx->rings;
1034
1035 /* make sure SQ entry isn't read before tail */
1036 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1037}
1038
Jens Axboedef596e2019-01-09 08:59:42 -07001039/*
1040 * Find and free completed poll iocbs
1041 */
1042static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1043 struct list_head *done)
1044{
1045 void *reqs[IO_IOPOLL_BATCH];
1046 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001047 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001048
Jens Axboe09bb8392019-03-13 12:39:28 -06001049 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001050 while (!list_empty(done)) {
1051 req = list_first_entry(done, struct io_kiocb, list);
1052 list_del(&req->list);
1053
Jens Axboe78e19bb2019-11-06 15:21:34 -07001054 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001055 (*nr_events)++;
1056
Jens Axboe09bb8392019-03-13 12:39:28 -06001057 if (refcount_dec_and_test(&req->refs)) {
1058 /* If we're not using fixed files, we have to pair the
1059 * completion part with the file put. Use regular
1060 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001061 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001062 */
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001063 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1064 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001065 reqs[to_free++] = req;
1066 if (to_free == ARRAY_SIZE(reqs))
1067 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001068 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001069 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001070 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001071 }
Jens Axboedef596e2019-01-09 08:59:42 -07001072 }
Jens Axboedef596e2019-01-09 08:59:42 -07001073
Jens Axboe09bb8392019-03-13 12:39:28 -06001074 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001075 io_free_req_many(ctx, reqs, &to_free);
1076}
1077
1078static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1079 long min)
1080{
1081 struct io_kiocb *req, *tmp;
1082 LIST_HEAD(done);
1083 bool spin;
1084 int ret;
1085
1086 /*
1087 * Only spin for completions if we don't have multiple devices hanging
1088 * off our complete list, and we're under the requested amount.
1089 */
1090 spin = !ctx->poll_multi_file && *nr_events < min;
1091
1092 ret = 0;
1093 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1094 struct kiocb *kiocb = &req->rw;
1095
1096 /*
1097 * Move completed entries to our local list. If we find a
1098 * request that requires polling, break out and complete
1099 * the done list first, if we have entries there.
1100 */
1101 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1102 list_move_tail(&req->list, &done);
1103 continue;
1104 }
1105 if (!list_empty(&done))
1106 break;
1107
1108 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1109 if (ret < 0)
1110 break;
1111
1112 if (ret && spin)
1113 spin = false;
1114 ret = 0;
1115 }
1116
1117 if (!list_empty(&done))
1118 io_iopoll_complete(ctx, nr_events, &done);
1119
1120 return ret;
1121}
1122
1123/*
1124 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1125 * non-spinning poll check - we'll still enter the driver poll loop, but only
1126 * as a non-spinning completion check.
1127 */
1128static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1129 long min)
1130{
Jens Axboe08f54392019-08-21 22:19:11 -06001131 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001132 int ret;
1133
1134 ret = io_do_iopoll(ctx, nr_events, min);
1135 if (ret < 0)
1136 return ret;
1137 if (!min || *nr_events >= min)
1138 return 0;
1139 }
1140
1141 return 1;
1142}
1143
1144/*
1145 * We can't just wait for polled events to come to us, we have to actively
1146 * find and complete them.
1147 */
1148static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1149{
1150 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1151 return;
1152
1153 mutex_lock(&ctx->uring_lock);
1154 while (!list_empty(&ctx->poll_list)) {
1155 unsigned int nr_events = 0;
1156
1157 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001158
1159 /*
1160 * Ensure we allow local-to-the-cpu processing to take place,
1161 * in this case we need to ensure that we reap all events.
1162 */
1163 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001164 }
1165 mutex_unlock(&ctx->uring_lock);
1166}
1167
Jens Axboe2b2ed972019-10-25 10:06:15 -06001168static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1169 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001170{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001171 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001172
1173 do {
1174 int tmin = 0;
1175
Jens Axboe500f9fb2019-08-19 12:15:59 -06001176 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001177 * Don't enter poll loop if we already have events pending.
1178 * If we do, we can potentially be spinning for commands that
1179 * already triggered a CQE (eg in error).
1180 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001181 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001182 break;
1183
1184 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001185 * If a submit got punted to a workqueue, we can have the
1186 * application entering polling for a command before it gets
1187 * issued. That app will hold the uring_lock for the duration
1188 * of the poll right here, so we need to take a breather every
1189 * now and then to ensure that the issue has a chance to add
1190 * the poll to the issued list. Otherwise we can spin here
1191 * forever, while the workqueue is stuck trying to acquire the
1192 * very same mutex.
1193 */
1194 if (!(++iters & 7)) {
1195 mutex_unlock(&ctx->uring_lock);
1196 mutex_lock(&ctx->uring_lock);
1197 }
1198
Jens Axboedef596e2019-01-09 08:59:42 -07001199 if (*nr_events < min)
1200 tmin = min - *nr_events;
1201
1202 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1203 if (ret <= 0)
1204 break;
1205 ret = 0;
1206 } while (min && !*nr_events && !need_resched());
1207
Jens Axboe2b2ed972019-10-25 10:06:15 -06001208 return ret;
1209}
1210
1211static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1212 long min)
1213{
1214 int ret;
1215
1216 /*
1217 * We disallow the app entering submit/complete with polling, but we
1218 * still need to lock the ring to prevent racing with polled issue
1219 * that got punted to a workqueue.
1220 */
1221 mutex_lock(&ctx->uring_lock);
1222 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001223 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001224 return ret;
1225}
1226
Jens Axboe491381ce2019-10-17 09:20:46 -06001227static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001228{
Jens Axboe491381ce2019-10-17 09:20:46 -06001229 /*
1230 * Tell lockdep we inherited freeze protection from submission
1231 * thread.
1232 */
1233 if (req->flags & REQ_F_ISREG) {
1234 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001235
Jens Axboe491381ce2019-10-17 09:20:46 -06001236 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001237 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001238 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001239}
1240
Jens Axboeba816ad2019-09-28 11:36:45 -06001241static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001242{
1243 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1244
Jens Axboe491381ce2019-10-17 09:20:46 -06001245 if (kiocb->ki_flags & IOCB_WRITE)
1246 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001247
Jens Axboe9e645e112019-05-10 16:07:28 -06001248 if ((req->flags & REQ_F_LINK) && res != req->result)
1249 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001250 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001251}
1252
1253static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1254{
1255 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1256
1257 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001258 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001259}
1260
Jens Axboeba816ad2019-09-28 11:36:45 -06001261static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1262{
1263 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001264 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001265
1266 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001267 io_put_req_find_next(req, &nxt);
1268
1269 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270}
1271
Jens Axboedef596e2019-01-09 08:59:42 -07001272static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1273{
1274 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1275
Jens Axboe491381ce2019-10-17 09:20:46 -06001276 if (kiocb->ki_flags & IOCB_WRITE)
1277 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001278
Jens Axboe9e645e112019-05-10 16:07:28 -06001279 if ((req->flags & REQ_F_LINK) && res != req->result)
1280 req->flags |= REQ_F_FAIL_LINK;
1281 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001282 if (res != -EAGAIN)
1283 req->flags |= REQ_F_IOPOLL_COMPLETED;
1284}
1285
1286/*
1287 * After the iocb has been issued, it's safe to be found on the poll list.
1288 * Adding the kiocb to the list AFTER submission ensures that we don't
1289 * find it from a io_iopoll_getevents() thread before the issuer is done
1290 * accessing the kiocb cookie.
1291 */
1292static void io_iopoll_req_issued(struct io_kiocb *req)
1293{
1294 struct io_ring_ctx *ctx = req->ctx;
1295
1296 /*
1297 * Track whether we have multiple files in our lists. This will impact
1298 * how we do polling eventually, not spinning if we're on potentially
1299 * different devices.
1300 */
1301 if (list_empty(&ctx->poll_list)) {
1302 ctx->poll_multi_file = false;
1303 } else if (!ctx->poll_multi_file) {
1304 struct io_kiocb *list_req;
1305
1306 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1307 list);
1308 if (list_req->rw.ki_filp != req->rw.ki_filp)
1309 ctx->poll_multi_file = true;
1310 }
1311
1312 /*
1313 * For fast devices, IO may have already completed. If it has, add
1314 * it to the front so we find it first.
1315 */
1316 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1317 list_add(&req->list, &ctx->poll_list);
1318 else
1319 list_add_tail(&req->list, &ctx->poll_list);
1320}
1321
Jens Axboe3d6770f2019-04-13 11:50:54 -06001322static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001323{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001324 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001325 int diff = state->has_refs - state->used_refs;
1326
1327 if (diff)
1328 fput_many(state->file, diff);
1329 state->file = NULL;
1330 }
1331}
1332
1333/*
1334 * Get as many references to a file as we have IOs left in this submission,
1335 * assuming most submissions are for one file, or at least that each file
1336 * has more than one submission.
1337 */
1338static struct file *io_file_get(struct io_submit_state *state, int fd)
1339{
1340 if (!state)
1341 return fget(fd);
1342
1343 if (state->file) {
1344 if (state->fd == fd) {
1345 state->used_refs++;
1346 state->ios_left--;
1347 return state->file;
1348 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001349 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001350 }
1351 state->file = fget_many(fd, state->ios_left);
1352 if (!state->file)
1353 return NULL;
1354
1355 state->fd = fd;
1356 state->has_refs = state->ios_left;
1357 state->used_refs = 1;
1358 state->ios_left--;
1359 return state->file;
1360}
1361
Jens Axboe2b188cc2019-01-07 10:46:33 -07001362/*
1363 * If we tracked the file through the SCM inflight mechanism, we could support
1364 * any file. For now, just ensure that anything potentially problematic is done
1365 * inline.
1366 */
1367static bool io_file_supports_async(struct file *file)
1368{
1369 umode_t mode = file_inode(file)->i_mode;
1370
1371 if (S_ISBLK(mode) || S_ISCHR(mode))
1372 return true;
1373 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1374 return true;
1375
1376 return false;
1377}
1378
Pavel Begunkov267bc902019-11-07 01:41:08 +03001379static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001380{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001381 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001382 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001383 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001384 unsigned ioprio;
1385 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001386
Jens Axboe09bb8392019-03-13 12:39:28 -06001387 if (!req->file)
1388 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001389
Jens Axboe491381ce2019-10-17 09:20:46 -06001390 if (S_ISREG(file_inode(req->file)->i_mode))
1391 req->flags |= REQ_F_ISREG;
1392
1393 /*
1394 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1395 * we know to async punt it even if it was opened O_NONBLOCK
1396 */
1397 if (force_nonblock && !io_file_supports_async(req->file)) {
1398 req->flags |= REQ_F_MUST_PUNT;
1399 return -EAGAIN;
1400 }
Jens Axboe6b063142019-01-10 22:13:58 -07001401
Jens Axboe2b188cc2019-01-07 10:46:33 -07001402 kiocb->ki_pos = READ_ONCE(sqe->off);
1403 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1404 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1405
1406 ioprio = READ_ONCE(sqe->ioprio);
1407 if (ioprio) {
1408 ret = ioprio_check_cap(ioprio);
1409 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001410 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001411
1412 kiocb->ki_ioprio = ioprio;
1413 } else
1414 kiocb->ki_ioprio = get_current_ioprio();
1415
1416 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1417 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001418 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001419
1420 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001421 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1422 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001423 req->flags |= REQ_F_NOWAIT;
1424
1425 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001426 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001427
Jens Axboedef596e2019-01-09 08:59:42 -07001428 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001429 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1430 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001431 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001432
Jens Axboedef596e2019-01-09 08:59:42 -07001433 kiocb->ki_flags |= IOCB_HIPRI;
1434 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001435 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001436 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001437 if (kiocb->ki_flags & IOCB_HIPRI)
1438 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001439 kiocb->ki_complete = io_complete_rw;
1440 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001441 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001442}
1443
1444static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1445{
1446 switch (ret) {
1447 case -EIOCBQUEUED:
1448 break;
1449 case -ERESTARTSYS:
1450 case -ERESTARTNOINTR:
1451 case -ERESTARTNOHAND:
1452 case -ERESTART_RESTARTBLOCK:
1453 /*
1454 * We can't just restart the syscall, since previously
1455 * submitted sqes may already be in progress. Just fail this
1456 * IO with EINTR.
1457 */
1458 ret = -EINTR;
1459 /* fall through */
1460 default:
1461 kiocb->ki_complete(kiocb, ret, 0);
1462 }
1463}
1464
Jens Axboeba816ad2019-09-28 11:36:45 -06001465static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1466 bool in_async)
1467{
1468 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1469 *nxt = __io_complete_rw(kiocb, ret);
1470 else
1471 io_rw_done(kiocb, ret);
1472}
1473
Jens Axboeedafcce2019-01-09 09:16:05 -07001474static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1475 const struct io_uring_sqe *sqe,
1476 struct iov_iter *iter)
1477{
1478 size_t len = READ_ONCE(sqe->len);
1479 struct io_mapped_ubuf *imu;
1480 unsigned index, buf_index;
1481 size_t offset;
1482 u64 buf_addr;
1483
1484 /* attempt to use fixed buffers without having provided iovecs */
1485 if (unlikely(!ctx->user_bufs))
1486 return -EFAULT;
1487
1488 buf_index = READ_ONCE(sqe->buf_index);
1489 if (unlikely(buf_index >= ctx->nr_user_bufs))
1490 return -EFAULT;
1491
1492 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1493 imu = &ctx->user_bufs[index];
1494 buf_addr = READ_ONCE(sqe->addr);
1495
1496 /* overflow */
1497 if (buf_addr + len < buf_addr)
1498 return -EFAULT;
1499 /* not inside the mapped region */
1500 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1501 return -EFAULT;
1502
1503 /*
1504 * May not be a start of buffer, set size appropriately
1505 * and advance us to the beginning.
1506 */
1507 offset = buf_addr - imu->ubuf;
1508 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001509
1510 if (offset) {
1511 /*
1512 * Don't use iov_iter_advance() here, as it's really slow for
1513 * using the latter parts of a big fixed buffer - it iterates
1514 * over each segment manually. We can cheat a bit here, because
1515 * we know that:
1516 *
1517 * 1) it's a BVEC iter, we set it up
1518 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1519 * first and last bvec
1520 *
1521 * So just find our index, and adjust the iterator afterwards.
1522 * If the offset is within the first bvec (or the whole first
1523 * bvec, just use iov_iter_advance(). This makes it easier
1524 * since we can just skip the first segment, which may not
1525 * be PAGE_SIZE aligned.
1526 */
1527 const struct bio_vec *bvec = imu->bvec;
1528
1529 if (offset <= bvec->bv_len) {
1530 iov_iter_advance(iter, offset);
1531 } else {
1532 unsigned long seg_skip;
1533
1534 /* skip first vec */
1535 offset -= bvec->bv_len;
1536 seg_skip = 1 + (offset >> PAGE_SHIFT);
1537
1538 iter->bvec = bvec + seg_skip;
1539 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001540 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001541 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001542 }
1543 }
1544
Jens Axboe5e559562019-11-13 16:12:46 -07001545 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001546}
1547
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001548static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1549 const struct sqe_submit *s, struct iovec **iovec,
1550 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001551{
1552 const struct io_uring_sqe *sqe = s->sqe;
1553 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1554 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001555 u8 opcode;
1556
1557 /*
1558 * We're reading ->opcode for the second time, but the first read
1559 * doesn't care whether it's _FIXED or not, so it doesn't matter
1560 * whether ->opcode changes concurrently. The first read does care
1561 * about whether it is a READ or a WRITE, so we don't trust this read
1562 * for that purpose and instead let the caller pass in the read/write
1563 * flag.
1564 */
1565 opcode = READ_ONCE(sqe->opcode);
1566 if (opcode == IORING_OP_READ_FIXED ||
1567 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001568 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001569 *iovec = NULL;
1570 return ret;
1571 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001572
1573 if (!s->has_user)
1574 return -EFAULT;
1575
1576#ifdef CONFIG_COMPAT
1577 if (ctx->compat)
1578 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1579 iovec, iter);
1580#endif
1581
1582 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1583}
1584
Jens Axboe32960612019-09-23 11:05:34 -06001585/*
1586 * For files that don't have ->read_iter() and ->write_iter(), handle them
1587 * by looping over ->read() or ->write() manually.
1588 */
1589static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1590 struct iov_iter *iter)
1591{
1592 ssize_t ret = 0;
1593
1594 /*
1595 * Don't support polled IO through this interface, and we can't
1596 * support non-blocking either. For the latter, this just causes
1597 * the kiocb to be handled from an async context.
1598 */
1599 if (kiocb->ki_flags & IOCB_HIPRI)
1600 return -EOPNOTSUPP;
1601 if (kiocb->ki_flags & IOCB_NOWAIT)
1602 return -EAGAIN;
1603
1604 while (iov_iter_count(iter)) {
1605 struct iovec iovec = iov_iter_iovec(iter);
1606 ssize_t nr;
1607
1608 if (rw == READ) {
1609 nr = file->f_op->read(file, iovec.iov_base,
1610 iovec.iov_len, &kiocb->ki_pos);
1611 } else {
1612 nr = file->f_op->write(file, iovec.iov_base,
1613 iovec.iov_len, &kiocb->ki_pos);
1614 }
1615
1616 if (nr < 0) {
1617 if (!ret)
1618 ret = nr;
1619 break;
1620 }
1621 ret += nr;
1622 if (nr != iovec.iov_len)
1623 break;
1624 iov_iter_advance(iter, nr);
1625 }
1626
1627 return ret;
1628}
1629
Pavel Begunkov267bc902019-11-07 01:41:08 +03001630static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001631 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001632{
1633 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1634 struct kiocb *kiocb = &req->rw;
1635 struct iov_iter iter;
1636 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001637 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001638 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001639
Pavel Begunkov267bc902019-11-07 01:41:08 +03001640 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001641 if (ret)
1642 return ret;
1643 file = kiocb->ki_filp;
1644
Jens Axboe2b188cc2019-01-07 10:46:33 -07001645 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001646 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001647
Pavel Begunkov267bc902019-11-07 01:41:08 +03001648 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001649 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001650 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001651
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001652 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001653 if (req->flags & REQ_F_LINK)
1654 req->result = read_size;
1655
Jens Axboe31b51512019-01-18 22:56:34 -07001656 iov_count = iov_iter_count(&iter);
1657 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001658 if (!ret) {
1659 ssize_t ret2;
1660
Jens Axboe32960612019-09-23 11:05:34 -06001661 if (file->f_op->read_iter)
1662 ret2 = call_read_iter(file, kiocb, &iter);
1663 else
1664 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1665
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001666 /*
1667 * In case of a short read, punt to async. This can happen
1668 * if we have data partially cached. Alternatively we can
1669 * return the short read, in which case the application will
1670 * need to issue another SQE and wait for it. That SQE will
1671 * need async punt anyway, so it's more efficient to do it
1672 * here.
1673 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001674 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1675 (req->flags & REQ_F_ISREG) &&
1676 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001677 ret2 = -EAGAIN;
1678 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001679 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001680 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001681 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001682 ret = -EAGAIN;
1683 }
1684 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001685 return ret;
1686}
1687
Pavel Begunkov267bc902019-11-07 01:41:08 +03001688static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001689 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001690{
1691 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1692 struct kiocb *kiocb = &req->rw;
1693 struct iov_iter iter;
1694 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001695 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001696 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001697
Pavel Begunkov267bc902019-11-07 01:41:08 +03001698 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001699 if (ret)
1700 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001701
Jens Axboe2b188cc2019-01-07 10:46:33 -07001702 file = kiocb->ki_filp;
1703 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001704 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001705
Pavel Begunkov267bc902019-11-07 01:41:08 +03001706 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001707 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001708 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001709
Jens Axboe9e645e112019-05-10 16:07:28 -06001710 if (req->flags & REQ_F_LINK)
1711 req->result = ret;
1712
Jens Axboe31b51512019-01-18 22:56:34 -07001713 iov_count = iov_iter_count(&iter);
1714
1715 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001716 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001717 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001718
1719 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001720 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001721 ssize_t ret2;
1722
Jens Axboe2b188cc2019-01-07 10:46:33 -07001723 /*
1724 * Open-code file_start_write here to grab freeze protection,
1725 * which will be released by another thread in
1726 * io_complete_rw(). Fool lockdep by telling it the lock got
1727 * released so that it doesn't complain about the held lock when
1728 * we return to userspace.
1729 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001730 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001731 __sb_start_write(file_inode(file)->i_sb,
1732 SB_FREEZE_WRITE, true);
1733 __sb_writers_release(file_inode(file)->i_sb,
1734 SB_FREEZE_WRITE);
1735 }
1736 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001737
Jens Axboe32960612019-09-23 11:05:34 -06001738 if (file->f_op->write_iter)
1739 ret2 = call_write_iter(file, kiocb, &iter);
1740 else
1741 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001742 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001743 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001744 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001745 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001746 }
Jens Axboe31b51512019-01-18 22:56:34 -07001747out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001748 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001749 return ret;
1750}
1751
1752/*
1753 * IORING_OP_NOP just posts a completion event, nothing else.
1754 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001755static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001756{
1757 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001758
Jens Axboedef596e2019-01-09 08:59:42 -07001759 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1760 return -EINVAL;
1761
Jens Axboe78e19bb2019-11-06 15:21:34 -07001762 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001763 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001764 return 0;
1765}
1766
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001767static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1768{
Jens Axboe6b063142019-01-10 22:13:58 -07001769 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001770
Jens Axboe09bb8392019-03-13 12:39:28 -06001771 if (!req->file)
1772 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001773
Jens Axboe6b063142019-01-10 22:13:58 -07001774 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001775 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001776 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001777 return -EINVAL;
1778
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001779 return 0;
1780}
1781
1782static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001783 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001784{
1785 loff_t sqe_off = READ_ONCE(sqe->off);
1786 loff_t sqe_len = READ_ONCE(sqe->len);
1787 loff_t end = sqe_off + sqe_len;
1788 unsigned fsync_flags;
1789 int ret;
1790
1791 fsync_flags = READ_ONCE(sqe->fsync_flags);
1792 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1793 return -EINVAL;
1794
1795 ret = io_prep_fsync(req, sqe);
1796 if (ret)
1797 return ret;
1798
1799 /* fsync always requires a blocking context */
1800 if (force_nonblock)
1801 return -EAGAIN;
1802
1803 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1804 end > 0 ? end : LLONG_MAX,
1805 fsync_flags & IORING_FSYNC_DATASYNC);
1806
Jens Axboe9e645e112019-05-10 16:07:28 -06001807 if (ret < 0 && (req->flags & REQ_F_LINK))
1808 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001809 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001810 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001811 return 0;
1812}
1813
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001814static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1815{
1816 struct io_ring_ctx *ctx = req->ctx;
1817 int ret = 0;
1818
1819 if (!req->file)
1820 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001821
1822 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1823 return -EINVAL;
1824 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1825 return -EINVAL;
1826
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001827 return ret;
1828}
1829
1830static int io_sync_file_range(struct io_kiocb *req,
1831 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001832 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001833 bool force_nonblock)
1834{
1835 loff_t sqe_off;
1836 loff_t sqe_len;
1837 unsigned flags;
1838 int ret;
1839
1840 ret = io_prep_sfr(req, sqe);
1841 if (ret)
1842 return ret;
1843
1844 /* sync_file_range always requires a blocking context */
1845 if (force_nonblock)
1846 return -EAGAIN;
1847
1848 sqe_off = READ_ONCE(sqe->off);
1849 sqe_len = READ_ONCE(sqe->len);
1850 flags = READ_ONCE(sqe->sync_range_flags);
1851
1852 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1853
Jens Axboe9e645e112019-05-10 16:07:28 -06001854 if (ret < 0 && (req->flags & REQ_F_LINK))
1855 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001856 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001857 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001858 return 0;
1859}
1860
Jens Axboe0fa03c62019-04-19 13:34:07 -06001861#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001862static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001863 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001864 long (*fn)(struct socket *, struct user_msghdr __user *,
1865 unsigned int))
1866{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001867 struct socket *sock;
1868 int ret;
1869
1870 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1871 return -EINVAL;
1872
1873 sock = sock_from_file(req->file, &ret);
1874 if (sock) {
1875 struct user_msghdr __user *msg;
1876 unsigned flags;
1877
1878 flags = READ_ONCE(sqe->msg_flags);
1879 if (flags & MSG_DONTWAIT)
1880 req->flags |= REQ_F_NOWAIT;
1881 else if (force_nonblock)
1882 flags |= MSG_DONTWAIT;
1883
1884 msg = (struct user_msghdr __user *) (unsigned long)
1885 READ_ONCE(sqe->addr);
1886
Jens Axboeaa1fa282019-04-19 13:38:09 -06001887 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001888 if (force_nonblock && ret == -EAGAIN)
1889 return ret;
1890 }
1891
Jens Axboe78e19bb2019-11-06 15:21:34 -07001892 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001893 if (ret < 0 && (req->flags & REQ_F_LINK))
1894 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001895 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001896 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001897}
1898#endif
1899
1900static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001901 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001902{
1903#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001904 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1905 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001906#else
1907 return -EOPNOTSUPP;
1908#endif
1909}
1910
1911static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001912 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001913{
1914#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001915 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1916 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001917#else
1918 return -EOPNOTSUPP;
1919#endif
1920}
1921
Jens Axboe17f2fe32019-10-17 14:42:58 -06001922static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1923 struct io_kiocb **nxt, bool force_nonblock)
1924{
1925#if defined(CONFIG_NET)
1926 struct sockaddr __user *addr;
1927 int __user *addr_len;
1928 unsigned file_flags;
1929 int flags, ret;
1930
1931 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1932 return -EINVAL;
1933 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1934 return -EINVAL;
1935
1936 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1937 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1938 flags = READ_ONCE(sqe->accept_flags);
1939 file_flags = force_nonblock ? O_NONBLOCK : 0;
1940
1941 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1942 if (ret == -EAGAIN && force_nonblock) {
1943 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1944 return -EAGAIN;
1945 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001946 if (ret == -ERESTARTSYS)
1947 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001948 if (ret < 0 && (req->flags & REQ_F_LINK))
1949 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001950 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001951 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001952 return 0;
1953#else
1954 return -EOPNOTSUPP;
1955#endif
1956}
1957
Jens Axboeeac406c2019-11-14 12:09:58 -07001958static inline void io_poll_remove_req(struct io_kiocb *req)
1959{
1960 if (!RB_EMPTY_NODE(&req->rb_node)) {
1961 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
1962 RB_CLEAR_NODE(&req->rb_node);
1963 }
1964}
1965
Jens Axboe221c5eb2019-01-17 09:41:58 -07001966static void io_poll_remove_one(struct io_kiocb *req)
1967{
1968 struct io_poll_iocb *poll = &req->poll;
1969
1970 spin_lock(&poll->head->lock);
1971 WRITE_ONCE(poll->canceled, true);
1972 if (!list_empty(&poll->wait.entry)) {
1973 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07001974 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001975 }
1976 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07001977 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001978}
1979
1980static void io_poll_remove_all(struct io_ring_ctx *ctx)
1981{
Jens Axboeeac406c2019-11-14 12:09:58 -07001982 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001983 struct io_kiocb *req;
1984
1985 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07001986 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
1987 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001988 io_poll_remove_one(req);
1989 }
1990 spin_unlock_irq(&ctx->completion_lock);
1991}
1992
Jens Axboe47f46762019-11-09 17:43:02 -07001993static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
1994{
Jens Axboeeac406c2019-11-14 12:09:58 -07001995 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07001996 struct io_kiocb *req;
1997
Jens Axboeeac406c2019-11-14 12:09:58 -07001998 p = ctx->cancel_tree.rb_node;
1999 while (p) {
2000 parent = p;
2001 req = rb_entry(parent, struct io_kiocb, rb_node);
2002 if (sqe_addr < req->user_data) {
2003 p = p->rb_left;
2004 } else if (sqe_addr > req->user_data) {
2005 p = p->rb_right;
2006 } else {
2007 io_poll_remove_one(req);
2008 return 0;
2009 }
Jens Axboe47f46762019-11-09 17:43:02 -07002010 }
2011
2012 return -ENOENT;
2013}
2014
Jens Axboe221c5eb2019-01-17 09:41:58 -07002015/*
2016 * Find a running poll command that matches one specified in sqe->addr,
2017 * and remove it if found.
2018 */
2019static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2020{
2021 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002022 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002023
2024 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2025 return -EINVAL;
2026 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2027 sqe->poll_events)
2028 return -EINVAL;
2029
2030 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002031 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002032 spin_unlock_irq(&ctx->completion_lock);
2033
Jens Axboe78e19bb2019-11-06 15:21:34 -07002034 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002035 if (ret < 0 && (req->flags & REQ_F_LINK))
2036 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002037 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002038 return 0;
2039}
2040
Jackie Liua197f662019-11-08 08:09:12 -07002041static void io_poll_complete(struct io_kiocb *req, __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002042{
Jackie Liua197f662019-11-08 08:09:12 -07002043 struct io_ring_ctx *ctx = req->ctx;
2044
Jens Axboe8c838782019-03-12 15:48:16 -06002045 req->poll.done = true;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002046 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002047 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002048}
2049
Jens Axboe561fb042019-10-24 07:25:42 -06002050static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002051{
Jens Axboe561fb042019-10-24 07:25:42 -06002052 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002053 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2054 struct io_poll_iocb *poll = &req->poll;
2055 struct poll_table_struct pt = { ._key = poll->events };
2056 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002057 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002058 __poll_t mask = 0;
2059
Jens Axboe561fb042019-10-24 07:25:42 -06002060 if (work->flags & IO_WQ_WORK_CANCEL)
2061 WRITE_ONCE(poll->canceled, true);
2062
Jens Axboe221c5eb2019-01-17 09:41:58 -07002063 if (!READ_ONCE(poll->canceled))
2064 mask = vfs_poll(poll->file, &pt) & poll->events;
2065
2066 /*
2067 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2068 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2069 * synchronize with them. In the cancellation case the list_del_init
2070 * itself is not actually needed, but harmless so we keep it in to
2071 * avoid further branches in the fast path.
2072 */
2073 spin_lock_irq(&ctx->completion_lock);
2074 if (!mask && !READ_ONCE(poll->canceled)) {
2075 add_wait_queue(poll->head, &poll->wait);
2076 spin_unlock_irq(&ctx->completion_lock);
2077 return;
2078 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002079 io_poll_remove_req(req);
Jackie Liua197f662019-11-08 08:09:12 -07002080 io_poll_complete(req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002081 spin_unlock_irq(&ctx->completion_lock);
2082
Jens Axboe8c838782019-03-12 15:48:16 -06002083 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002084
Jackie Liuec9c02a2019-11-08 23:50:36 +08002085 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002086 if (nxt)
2087 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002088}
2089
2090static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2091 void *key)
2092{
2093 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2094 wait);
2095 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2096 struct io_ring_ctx *ctx = req->ctx;
2097 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002098 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002099
2100 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002101 if (mask && !(mask & poll->events))
2102 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002103
2104 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002105
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002106 /*
2107 * Run completion inline if we can. We're using trylock here because
2108 * we are violating the completion_lock -> poll wq lock ordering.
2109 * If we have a link timeout we're going to need the completion_lock
2110 * for finalizing the request, mark us as having grabbed that already.
2111 */
Jens Axboe8c838782019-03-12 15:48:16 -06002112 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002113 io_poll_remove_req(req);
Jackie Liua197f662019-11-08 08:09:12 -07002114 io_poll_complete(req, mask);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002115 req->flags |= REQ_F_COMP_LOCKED;
2116 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002117 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2118
2119 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002120 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002121 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002122 }
2123
Jens Axboe221c5eb2019-01-17 09:41:58 -07002124 return 1;
2125}
2126
2127struct io_poll_table {
2128 struct poll_table_struct pt;
2129 struct io_kiocb *req;
2130 int error;
2131};
2132
2133static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2134 struct poll_table_struct *p)
2135{
2136 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2137
2138 if (unlikely(pt->req->poll.head)) {
2139 pt->error = -EINVAL;
2140 return;
2141 }
2142
2143 pt->error = 0;
2144 pt->req->poll.head = head;
2145 add_wait_queue(head, &pt->req->poll.wait);
2146}
2147
Jens Axboeeac406c2019-11-14 12:09:58 -07002148static void io_poll_req_insert(struct io_kiocb *req)
2149{
2150 struct io_ring_ctx *ctx = req->ctx;
2151 struct rb_node **p = &ctx->cancel_tree.rb_node;
2152 struct rb_node *parent = NULL;
2153 struct io_kiocb *tmp;
2154
2155 while (*p) {
2156 parent = *p;
2157 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2158 if (req->user_data < tmp->user_data)
2159 p = &(*p)->rb_left;
2160 else
2161 p = &(*p)->rb_right;
2162 }
2163 rb_link_node(&req->rb_node, parent, p);
2164 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2165}
2166
Jens Axboe89723d02019-11-05 15:32:58 -07002167static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2168 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002169{
2170 struct io_poll_iocb *poll = &req->poll;
2171 struct io_ring_ctx *ctx = req->ctx;
2172 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002173 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002174 __poll_t mask;
2175 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002176
2177 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2178 return -EINVAL;
2179 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2180 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002181 if (!poll->file)
2182 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002183
Jens Axboe6cc47d12019-09-18 11:18:23 -06002184 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002185 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002186 events = READ_ONCE(sqe->poll_events);
2187 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002188 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002189
Jens Axboe221c5eb2019-01-17 09:41:58 -07002190 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002191 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002192 poll->canceled = false;
2193
2194 ipt.pt._qproc = io_poll_queue_proc;
2195 ipt.pt._key = poll->events;
2196 ipt.req = req;
2197 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2198
2199 /* initialized the list so that we can do list_empty checks */
2200 INIT_LIST_HEAD(&poll->wait.entry);
2201 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2202
Jens Axboe36703242019-07-25 10:20:18 -06002203 INIT_LIST_HEAD(&req->list);
2204
Jens Axboe221c5eb2019-01-17 09:41:58 -07002205 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002206
2207 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002208 if (likely(poll->head)) {
2209 spin_lock(&poll->head->lock);
2210 if (unlikely(list_empty(&poll->wait.entry))) {
2211 if (ipt.error)
2212 cancel = true;
2213 ipt.error = 0;
2214 mask = 0;
2215 }
2216 if (mask || ipt.error)
2217 list_del_init(&poll->wait.entry);
2218 else if (cancel)
2219 WRITE_ONCE(poll->canceled, true);
2220 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002221 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002222 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002223 }
Jens Axboe8c838782019-03-12 15:48:16 -06002224 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002225 ipt.error = 0;
Jackie Liua197f662019-11-08 08:09:12 -07002226 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06002227 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002228 spin_unlock_irq(&ctx->completion_lock);
2229
Jens Axboe8c838782019-03-12 15:48:16 -06002230 if (mask) {
2231 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002232 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002233 }
Jens Axboe8c838782019-03-12 15:48:16 -06002234 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002235}
2236
Jens Axboe5262f562019-09-17 12:26:57 -06002237static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2238{
2239 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002240 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06002241 unsigned long flags;
2242
2243 req = container_of(timer, struct io_kiocb, timeout.timer);
2244 ctx = req->ctx;
2245 atomic_inc(&ctx->cq_timeouts);
2246
2247 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002248 /*
Jens Axboe11365042019-10-16 09:08:32 -06002249 * We could be racing with timeout deletion. If the list is empty,
2250 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002251 */
Jens Axboe842f9612019-10-29 12:34:10 -06002252 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002253 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002254
Jens Axboe11365042019-10-16 09:08:32 -06002255 /*
2256 * Adjust the reqs sequence before the current one because it
2257 * will consume a slot in the cq_ring and the the cq_tail
2258 * pointer will be increased, otherwise other timeout reqs may
2259 * return in advance without waiting for enough wait_nr.
2260 */
2261 prev = req;
2262 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2263 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002264 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002265 }
Jens Axboe842f9612019-10-29 12:34:10 -06002266
Jens Axboe78e19bb2019-11-06 15:21:34 -07002267 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002268 io_commit_cqring(ctx);
2269 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2270
2271 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002272 if (req->flags & REQ_F_LINK)
2273 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002274 io_put_req(req);
2275 return HRTIMER_NORESTART;
2276}
2277
Jens Axboe47f46762019-11-09 17:43:02 -07002278static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2279{
2280 struct io_kiocb *req;
2281 int ret = -ENOENT;
2282
2283 list_for_each_entry(req, &ctx->timeout_list, list) {
2284 if (user_data == req->user_data) {
2285 list_del_init(&req->list);
2286 ret = 0;
2287 break;
2288 }
2289 }
2290
2291 if (ret == -ENOENT)
2292 return ret;
2293
2294 ret = hrtimer_try_to_cancel(&req->timeout.timer);
2295 if (ret == -1)
2296 return -EALREADY;
2297
2298 io_cqring_fill_event(req, -ECANCELED);
2299 io_put_req(req);
2300 return 0;
2301}
2302
Jens Axboe11365042019-10-16 09:08:32 -06002303/*
2304 * Remove or update an existing timeout command
2305 */
2306static int io_timeout_remove(struct io_kiocb *req,
2307 const struct io_uring_sqe *sqe)
2308{
2309 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002310 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002311 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002312
2313 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2314 return -EINVAL;
2315 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2316 return -EINVAL;
2317 flags = READ_ONCE(sqe->timeout_flags);
2318 if (flags)
2319 return -EINVAL;
2320
Jens Axboe11365042019-10-16 09:08:32 -06002321 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002322 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002323
Jens Axboe47f46762019-11-09 17:43:02 -07002324 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002325 io_commit_cqring(ctx);
2326 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002327 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002328 if (ret < 0 && req->flags & REQ_F_LINK)
2329 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002330 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002331 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002332}
2333
2334static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2335{
yangerkun5da0fb12019-10-15 21:59:29 +08002336 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002337 struct io_ring_ctx *ctx = req->ctx;
2338 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002339 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002340 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002341 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002342 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002343
2344 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2345 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002346 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2347 return -EINVAL;
2348 flags = READ_ONCE(sqe->timeout_flags);
2349 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002350 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002351
2352 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002353 return -EFAULT;
2354
Jens Axboe11365042019-10-16 09:08:32 -06002355 if (flags & IORING_TIMEOUT_ABS)
2356 mode = HRTIMER_MODE_ABS;
2357 else
2358 mode = HRTIMER_MODE_REL;
2359
2360 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002361 req->flags |= REQ_F_TIMEOUT;
2362
Jens Axboe5262f562019-09-17 12:26:57 -06002363 /*
2364 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002365 * timeout event to be satisfied. If it isn't set, then this is
2366 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002367 */
2368 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002369 if (!count) {
2370 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2371 spin_lock_irq(&ctx->completion_lock);
2372 entry = ctx->timeout_list.prev;
2373 goto add;
2374 }
Jens Axboe5262f562019-09-17 12:26:57 -06002375
2376 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002377 /* reuse it to store the count */
2378 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002379
2380 /*
2381 * Insertion sort, ensuring the first entry in the list is always
2382 * the one we need first.
2383 */
Jens Axboe5262f562019-09-17 12:26:57 -06002384 spin_lock_irq(&ctx->completion_lock);
2385 list_for_each_prev(entry, &ctx->timeout_list) {
2386 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002387 unsigned nxt_sq_head;
2388 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002389
Jens Axboe93bd25b2019-11-11 23:34:31 -07002390 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2391 continue;
2392
yangerkun5da0fb12019-10-15 21:59:29 +08002393 /*
2394 * Since cached_sq_head + count - 1 can overflow, use type long
2395 * long to store it.
2396 */
2397 tmp = (long long)ctx->cached_sq_head + count - 1;
2398 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2399 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2400
2401 /*
2402 * cached_sq_head may overflow, and it will never overflow twice
2403 * once there is some timeout req still be valid.
2404 */
2405 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002406 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002407
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002408 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002409 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002410
2411 /*
2412 * Sequence of reqs after the insert one and itself should
2413 * be adjusted because each timeout req consumes a slot.
2414 */
2415 span++;
2416 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002417 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002418 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002419add:
Jens Axboe5262f562019-09-17 12:26:57 -06002420 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002421 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002422 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002423 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002424 return 0;
2425}
2426
Jens Axboe62755e32019-10-28 21:49:21 -06002427static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002428{
Jens Axboe62755e32019-10-28 21:49:21 -06002429 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002430
Jens Axboe62755e32019-10-28 21:49:21 -06002431 return req->user_data == (unsigned long) data;
2432}
2433
Jens Axboee977d6d2019-11-05 12:39:45 -07002434static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002435{
Jens Axboe62755e32019-10-28 21:49:21 -06002436 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002437 int ret = 0;
2438
Jens Axboe62755e32019-10-28 21:49:21 -06002439 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2440 switch (cancel_ret) {
2441 case IO_WQ_CANCEL_OK:
2442 ret = 0;
2443 break;
2444 case IO_WQ_CANCEL_RUNNING:
2445 ret = -EALREADY;
2446 break;
2447 case IO_WQ_CANCEL_NOTFOUND:
2448 ret = -ENOENT;
2449 break;
2450 }
2451
Jens Axboee977d6d2019-11-05 12:39:45 -07002452 return ret;
2453}
2454
Jens Axboe47f46762019-11-09 17:43:02 -07002455static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2456 struct io_kiocb *req, __u64 sqe_addr,
2457 struct io_kiocb **nxt)
2458{
2459 unsigned long flags;
2460 int ret;
2461
2462 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2463 if (ret != -ENOENT) {
2464 spin_lock_irqsave(&ctx->completion_lock, flags);
2465 goto done;
2466 }
2467
2468 spin_lock_irqsave(&ctx->completion_lock, flags);
2469 ret = io_timeout_cancel(ctx, sqe_addr);
2470 if (ret != -ENOENT)
2471 goto done;
2472 ret = io_poll_cancel(ctx, sqe_addr);
2473done:
2474 io_cqring_fill_event(req, ret);
2475 io_commit_cqring(ctx);
2476 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2477 io_cqring_ev_posted(ctx);
2478
2479 if (ret < 0 && (req->flags & REQ_F_LINK))
2480 req->flags |= REQ_F_FAIL_LINK;
2481 io_put_req_find_next(req, nxt);
2482}
2483
Jens Axboee977d6d2019-11-05 12:39:45 -07002484static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2485 struct io_kiocb **nxt)
2486{
2487 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002488
2489 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2490 return -EINVAL;
2491 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2492 sqe->cancel_flags)
2493 return -EINVAL;
2494
Jens Axboe95a5bba2019-11-14 22:40:44 -07002495 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06002496 return 0;
2497}
2498
Jackie Liua197f662019-11-08 08:09:12 -07002499static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002500{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002501 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002502 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002503 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002504
Bob Liu9d858b22019-11-13 18:06:25 +08002505 /* Still need defer if there is pending req in defer list. */
2506 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002507 return 0;
2508
2509 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2510 if (!sqe_copy)
2511 return -EAGAIN;
2512
2513 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002514 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002515 spin_unlock_irq(&ctx->completion_lock);
2516 kfree(sqe_copy);
2517 return 0;
2518 }
2519
2520 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2521 req->submit.sqe = sqe_copy;
2522
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002523 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002524 list_add_tail(&req->list, &ctx->defer_list);
2525 spin_unlock_irq(&ctx->completion_lock);
2526 return -EIOCBQUEUED;
2527}
2528
Jackie Liua197f662019-11-08 08:09:12 -07002529static int __io_submit_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2530 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002531{
Jens Axboee0c5c572019-03-12 10:18:47 -06002532 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002533 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002534 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002535
2536 opcode = READ_ONCE(s->sqe->opcode);
2537 switch (opcode) {
2538 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002539 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002540 break;
2541 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002542 if (unlikely(s->sqe->buf_index))
2543 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002544 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002545 break;
2546 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002547 if (unlikely(s->sqe->buf_index))
2548 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002549 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002550 break;
2551 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002552 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002553 break;
2554 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002555 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002556 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002557 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002558 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002559 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002560 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002561 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002562 break;
2563 case IORING_OP_POLL_REMOVE:
2564 ret = io_poll_remove(req, s->sqe);
2565 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002566 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002567 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002568 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002569 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002570 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002571 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002572 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002573 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002574 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002575 case IORING_OP_TIMEOUT:
2576 ret = io_timeout(req, s->sqe);
2577 break;
Jens Axboe11365042019-10-16 09:08:32 -06002578 case IORING_OP_TIMEOUT_REMOVE:
2579 ret = io_timeout_remove(req, s->sqe);
2580 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002581 case IORING_OP_ACCEPT:
2582 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2583 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002584 case IORING_OP_ASYNC_CANCEL:
2585 ret = io_async_cancel(req, s->sqe, nxt);
2586 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002587 default:
2588 ret = -EINVAL;
2589 break;
2590 }
2591
Jens Axboedef596e2019-01-09 08:59:42 -07002592 if (ret)
2593 return ret;
2594
2595 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002596 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002597 return -EAGAIN;
2598
2599 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002600 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002601 mutex_lock(&ctx->uring_lock);
2602 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002603 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002604 mutex_unlock(&ctx->uring_lock);
2605 }
2606
2607 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002608}
2609
Jens Axboe561fb042019-10-24 07:25:42 -06002610static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002611{
Jens Axboe561fb042019-10-24 07:25:42 -06002612 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002613 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002614 struct sqe_submit *s = &req->submit;
2615 const struct io_uring_sqe *sqe = s->sqe;
2616 struct io_kiocb *nxt = NULL;
2617 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002618
Jens Axboe561fb042019-10-24 07:25:42 -06002619 /* Ensure we clear previously set non-block flag */
2620 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002621
Jens Axboe561fb042019-10-24 07:25:42 -06002622 if (work->flags & IO_WQ_WORK_CANCEL)
2623 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002624
Jens Axboe561fb042019-10-24 07:25:42 -06002625 if (!ret) {
2626 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2627 s->in_async = true;
2628 do {
Jackie Liua197f662019-11-08 08:09:12 -07002629 ret = __io_submit_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002630 /*
2631 * We can get EAGAIN for polled IO even though we're
2632 * forcing a sync submission from here, since we can't
2633 * wait for request slots on the block side.
2634 */
2635 if (ret != -EAGAIN)
2636 break;
2637 cond_resched();
2638 } while (1);
2639 }
Jens Axboe31b51512019-01-18 22:56:34 -07002640
Jens Axboe561fb042019-10-24 07:25:42 -06002641 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002642 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002643
Jens Axboe561fb042019-10-24 07:25:42 -06002644 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002645 if (req->flags & REQ_F_LINK)
2646 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002647 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002648 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002649 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002650
Jens Axboe561fb042019-10-24 07:25:42 -06002651 /* async context always use a copy of the sqe */
2652 kfree(sqe);
2653
2654 /* if a dependent link is ready, pass it back */
2655 if (!ret && nxt) {
2656 io_prep_async_work(nxt);
2657 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002658 }
Jens Axboe31b51512019-01-18 22:56:34 -07002659}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002660
Jens Axboe09bb8392019-03-13 12:39:28 -06002661static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2662{
2663 int op = READ_ONCE(sqe->opcode);
2664
2665 switch (op) {
2666 case IORING_OP_NOP:
2667 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002668 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002669 case IORING_OP_TIMEOUT_REMOVE:
2670 case IORING_OP_ASYNC_CANCEL:
2671 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002672 return false;
2673 default:
2674 return true;
2675 }
2676}
2677
Jens Axboe65e19f52019-10-26 07:20:21 -06002678static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2679 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002680{
Jens Axboe65e19f52019-10-26 07:20:21 -06002681 struct fixed_file_table *table;
2682
2683 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2684 return table->files[index & IORING_FILE_TABLE_MASK];
2685}
2686
Jackie Liua197f662019-11-08 08:09:12 -07002687static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002688{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002689 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002690 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002691 unsigned flags;
2692 int fd;
2693
2694 flags = READ_ONCE(s->sqe->flags);
2695 fd = READ_ONCE(s->sqe->fd);
2696
Jackie Liu4fe2c962019-09-09 20:50:40 +08002697 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002698 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002699 /*
2700 * All io need record the previous position, if LINK vs DARIN,
2701 * it can be used to mark the position of the first IO in the
2702 * link list.
2703 */
2704 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002705
Jens Axboe60c112b2019-06-21 10:20:18 -06002706 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002707 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002708
2709 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002710 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002711 (unsigned) fd >= ctx->nr_user_files))
2712 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002713 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002714 req->file = io_file_from_index(ctx, fd);
2715 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002716 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002717 req->flags |= REQ_F_FIXED_FILE;
2718 } else {
2719 if (s->needs_fixed_file)
2720 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002721 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002722 req->file = io_file_get(state, fd);
2723 if (unlikely(!req->file))
2724 return -EBADF;
2725 }
2726
2727 return 0;
2728}
2729
Jackie Liua197f662019-11-08 08:09:12 -07002730static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002731{
Jens Axboefcb323c2019-10-24 12:39:47 -06002732 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002733 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002734
2735 rcu_read_lock();
2736 spin_lock_irq(&ctx->inflight_lock);
2737 /*
2738 * We use the f_ops->flush() handler to ensure that we can flush
2739 * out work accessing these files if the fd is closed. Check if
2740 * the fd has changed since we started down this path, and disallow
2741 * this operation if it has.
2742 */
2743 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2744 list_add(&req->inflight_entry, &ctx->inflight_list);
2745 req->flags |= REQ_F_INFLIGHT;
2746 req->work.files = current->files;
2747 ret = 0;
2748 }
2749 spin_unlock_irq(&ctx->inflight_lock);
2750 rcu_read_unlock();
2751
2752 return ret;
2753}
2754
Jens Axboe2665abf2019-11-05 12:40:47 -07002755static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2756{
2757 struct io_kiocb *req = container_of(timer, struct io_kiocb,
2758 timeout.timer);
2759 struct io_ring_ctx *ctx = req->ctx;
2760 struct io_kiocb *prev = NULL;
2761 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002762
2763 spin_lock_irqsave(&ctx->completion_lock, flags);
2764
2765 /*
2766 * We don't expect the list to be empty, that will only happen if we
2767 * race with the completion of the linked work.
2768 */
2769 if (!list_empty(&req->list)) {
2770 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002771 if (refcount_inc_not_zero(&prev->refs))
2772 list_del_init(&req->list);
2773 else
2774 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002775 }
2776
2777 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2778
2779 if (prev) {
Jens Axboe47f46762019-11-09 17:43:02 -07002780 io_async_find_and_cancel(ctx, req, prev->user_data, NULL);
Jens Axboe76a46e02019-11-10 23:34:16 -07002781 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002782 } else {
2783 io_cqring_add_event(req, -ETIME);
2784 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002785 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002786 return HRTIMER_NORESTART;
2787}
2788
Jens Axboe76a46e02019-11-10 23:34:16 -07002789static void io_queue_linked_timeout(struct io_kiocb *req, struct timespec64 *ts,
2790 enum hrtimer_mode *mode)
Jens Axboe2665abf2019-11-05 12:40:47 -07002791{
Jens Axboe76a46e02019-11-10 23:34:16 -07002792 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002793
Jens Axboe76a46e02019-11-10 23:34:16 -07002794 /*
2795 * If the list is now empty, then our linked request finished before
2796 * we got a chance to setup the timer
2797 */
2798 spin_lock_irq(&ctx->completion_lock);
2799 if (!list_empty(&req->list)) {
2800 req->timeout.timer.function = io_link_timeout_fn;
2801 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(*ts),
2802 *mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002803 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002804 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002805
Jens Axboe2665abf2019-11-05 12:40:47 -07002806 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002807 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002808}
2809
Jens Axboe76a46e02019-11-10 23:34:16 -07002810static int io_validate_link_timeout(const struct io_uring_sqe *sqe,
2811 struct timespec64 *ts)
2812{
2813 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || sqe->off)
2814 return -EINVAL;
2815 if (sqe->timeout_flags & ~IORING_TIMEOUT_ABS)
2816 return -EINVAL;
2817 if (get_timespec64(ts, u64_to_user_ptr(sqe->addr)))
2818 return -EFAULT;
2819
2820 return 0;
2821}
2822
2823static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req,
2824 struct timespec64 *ts,
2825 enum hrtimer_mode *mode)
Jens Axboe2665abf2019-11-05 12:40:47 -07002826{
2827 struct io_kiocb *nxt;
Jens Axboee0c5c572019-03-12 10:18:47 -06002828 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002829
Jens Axboe2665abf2019-11-05 12:40:47 -07002830 if (!(req->flags & REQ_F_LINK))
2831 return NULL;
2832
2833 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002834 if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT)
2835 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002836
Jens Axboe76a46e02019-11-10 23:34:16 -07002837 ret = io_validate_link_timeout(nxt->submit.sqe, ts);
2838 if (ret) {
2839 list_del_init(&nxt->list);
2840 io_cqring_add_event(nxt, ret);
2841 io_double_put_req(nxt);
2842 return ERR_PTR(-ECANCELED);
2843 }
2844
2845 if (nxt->submit.sqe->timeout_flags & IORING_TIMEOUT_ABS)
2846 *mode = HRTIMER_MODE_ABS;
2847 else
2848 *mode = HRTIMER_MODE_REL;
2849
2850 req->flags |= REQ_F_LINK_TIMEOUT;
2851 hrtimer_init(&nxt->timeout.timer, CLOCK_MONOTONIC, *mode);
2852 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002853}
2854
Jens Axboe0e0702d2019-11-14 21:42:10 -07002855static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002856{
Jens Axboe76a46e02019-11-10 23:34:16 -07002857 enum hrtimer_mode mode;
Jens Axboe2665abf2019-11-05 12:40:47 -07002858 struct io_kiocb *nxt;
Jens Axboe76a46e02019-11-10 23:34:16 -07002859 struct timespec64 ts;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002860 int ret;
2861
Jens Axboe76a46e02019-11-10 23:34:16 -07002862 nxt = io_prep_linked_timeout(req, &ts, &mode);
2863 if (IS_ERR(nxt)) {
2864 ret = PTR_ERR(nxt);
2865 nxt = NULL;
2866 goto err;
Jens Axboe2665abf2019-11-05 12:40:47 -07002867 }
2868
Jackie Liua197f662019-11-08 08:09:12 -07002869 ret = __io_submit_sqe(req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002870
2871 /*
2872 * We async punt it if the file wasn't marked NOWAIT, or if the file
2873 * doesn't support non-blocking read/write attempts
2874 */
2875 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2876 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002877 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002878 struct io_uring_sqe *sqe_copy;
2879
Jackie Liu954dab12019-09-18 10:37:52 +08002880 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002881 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002882 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002883 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
Jackie Liua197f662019-11-08 08:09:12 -07002884 ret = io_grab_files(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06002885 if (ret) {
2886 kfree(sqe_copy);
2887 goto err;
2888 }
Jens Axboe31b51512019-01-18 22:56:34 -07002889 }
Jens Axboee65ef562019-03-12 10:16:44 -06002890
2891 /*
2892 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002893 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002894 */
Jackie Liua197f662019-11-08 08:09:12 -07002895 io_queue_async_work(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07002896
2897 if (nxt)
2898 io_queue_linked_timeout(nxt, &ts, &mode);
2899
Jens Axboe0e0702d2019-11-14 21:42:10 -07002900 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002901 }
2902 }
Jens Axboee65ef562019-03-12 10:16:44 -06002903
Jens Axboefcb323c2019-10-24 12:39:47 -06002904err:
Jens Axboee65ef562019-03-12 10:16:44 -06002905 /* drop submission reference */
2906 io_put_req(req);
2907
Jens Axboe76a46e02019-11-10 23:34:16 -07002908 if (nxt) {
2909 if (!ret)
2910 io_queue_linked_timeout(nxt, &ts, &mode);
2911 else
2912 io_put_req(nxt);
2913 }
2914
Jens Axboee65ef562019-03-12 10:16:44 -06002915 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002916 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002917 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002918 if (req->flags & REQ_F_LINK)
2919 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002920 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002921 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002922}
2923
Jens Axboe0e0702d2019-11-14 21:42:10 -07002924static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002925{
2926 int ret;
2927
Jackie Liua197f662019-11-08 08:09:12 -07002928 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002929 if (ret) {
2930 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002931 io_cqring_add_event(req, ret);
2932 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002933 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07002934 } else
2935 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002936}
2937
Jens Axboe0e0702d2019-11-14 21:42:10 -07002938static void io_queue_link_head(struct io_kiocb *req, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002939{
2940 int ret;
2941 int need_submit = false;
Jackie Liua197f662019-11-08 08:09:12 -07002942 struct io_ring_ctx *ctx = req->ctx;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002943
Jens Axboe0e0702d2019-11-14 21:42:10 -07002944 if (!shadow) {
2945 io_queue_sqe(req);
2946 return;
2947 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08002948
2949 /*
2950 * Mark the first IO in link list as DRAIN, let all the following
2951 * IOs enter the defer list. all IO needs to be completed before link
2952 * list.
2953 */
2954 req->flags |= REQ_F_IO_DRAIN;
Jackie Liua197f662019-11-08 08:09:12 -07002955 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002956 if (ret) {
2957 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002958 io_cqring_add_event(req, ret);
2959 io_double_put_req(req);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002960 __io_free_req(shadow);
Jens Axboe0e0702d2019-11-14 21:42:10 -07002961 return;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002962 }
2963 } else {
2964 /*
2965 * If ret == 0 means that all IOs in front of link io are
2966 * running done. let's queue link head.
2967 */
2968 need_submit = true;
2969 }
2970
2971 /* Insert shadow req to defer_list, blocking next IOs */
2972 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002973 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002974 list_add_tail(&shadow->list, &ctx->defer_list);
2975 spin_unlock_irq(&ctx->completion_lock);
2976
2977 if (need_submit)
Jens Axboe0e0702d2019-11-14 21:42:10 -07002978 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002979}
2980
Jens Axboe9e645e112019-05-10 16:07:28 -06002981#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2982
Jackie Liua197f662019-11-08 08:09:12 -07002983static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
2984 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002985{
2986 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002987 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002988 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06002989 int ret;
2990
Jens Axboe78e19bb2019-11-06 15:21:34 -07002991 req->user_data = s->sqe->user_data;
2992
Jens Axboe9e645e112019-05-10 16:07:28 -06002993 /* enforce forwards compatibility on users */
2994 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2995 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03002996 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002997 }
2998
Jackie Liua197f662019-11-08 08:09:12 -07002999 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003000 if (unlikely(ret)) {
3001err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003002 io_cqring_add_event(req, ret);
3003 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003004 return;
3005 }
3006
Jens Axboe9e645e112019-05-10 16:07:28 -06003007 /*
3008 * If we already have a head request, queue this one for async
3009 * submittal once the head completes. If we don't have a head but
3010 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3011 * submitted sync once the chain is complete. If none of those
3012 * conditions are true (normal request), then just queue it.
3013 */
3014 if (*link) {
3015 struct io_kiocb *prev = *link;
3016
3017 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
3018 if (!sqe_copy) {
3019 ret = -EAGAIN;
3020 goto err_req;
3021 }
3022
3023 s->sqe = sqe_copy;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003024 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003025 list_add_tail(&req->list, &prev->link_list);
3026 } else if (s->sqe->flags & IOSQE_IO_LINK) {
3027 req->flags |= REQ_F_LINK;
3028
Jens Axboe9e645e112019-05-10 16:07:28 -06003029 INIT_LIST_HEAD(&req->link_list);
3030 *link = req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003031 } else if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
3032 /* Only valid as a linked SQE */
3033 ret = -EINVAL;
3034 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003035 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003036 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003037 }
3038}
3039
Jens Axboe9a56a232019-01-09 09:06:50 -07003040/*
3041 * Batched submission is done, ensure local IO is flushed out.
3042 */
3043static void io_submit_state_end(struct io_submit_state *state)
3044{
3045 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003046 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003047 if (state->free_reqs)
3048 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3049 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003050}
3051
3052/*
3053 * Start submission side cache.
3054 */
3055static void io_submit_state_start(struct io_submit_state *state,
3056 struct io_ring_ctx *ctx, unsigned max_ios)
3057{
3058 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003059 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003060 state->file = NULL;
3061 state->ios_left = max_ios;
3062}
3063
Jens Axboe2b188cc2019-01-07 10:46:33 -07003064static void io_commit_sqring(struct io_ring_ctx *ctx)
3065{
Hristo Venev75b28af2019-08-26 17:23:46 +00003066 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003067
Hristo Venev75b28af2019-08-26 17:23:46 +00003068 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003069 /*
3070 * Ensure any loads from the SQEs are done at this point,
3071 * since once we write the new head, the application could
3072 * write new data to them.
3073 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003074 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003075 }
3076}
3077
3078/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003079 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3080 * that is mapped by userspace. This means that care needs to be taken to
3081 * ensure that reads are stable, as we cannot rely on userspace always
3082 * being a good citizen. If members of the sqe are validated and then later
3083 * used, it's important that those reads are done through READ_ONCE() to
3084 * prevent a re-load down the line.
3085 */
3086static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
3087{
Hristo Venev75b28af2019-08-26 17:23:46 +00003088 struct io_rings *rings = ctx->rings;
3089 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003090 unsigned head;
3091
3092 /*
3093 * The cached sq head (or cq tail) serves two purposes:
3094 *
3095 * 1) allows us to batch the cost of updating the user visible
3096 * head updates.
3097 * 2) allows the kernel side to track the head on its own, even
3098 * though the application is the one updating it.
3099 */
3100 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003101 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00003102 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003103 return false;
3104
Hristo Venev75b28af2019-08-26 17:23:46 +00003105 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003106 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003107 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003108 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003109 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003110 ctx->cached_sq_head++;
3111 return true;
3112 }
3113
3114 /* drop invalid entries */
3115 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003116 ctx->cached_sq_dropped++;
3117 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003118 return false;
3119}
3120
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003121static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003122 struct file *ring_file, int ring_fd,
3123 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003124{
3125 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003126 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003127 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003128 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003129 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003130
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003131 if (!list_empty(&ctx->cq_overflow_list)) {
3132 io_cqring_overflow_flush(ctx, false);
3133 return -EBUSY;
3134 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003135
3136 if (nr > IO_PLUG_THRESHOLD) {
3137 io_submit_state_start(&state, ctx, nr);
3138 statep = &state;
3139 }
3140
3141 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003142 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003143 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003144
Pavel Begunkov196be952019-11-07 01:41:06 +03003145 req = io_get_req(ctx, statep);
3146 if (unlikely(!req)) {
3147 if (!submitted)
3148 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003149 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003150 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003151 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003152 __io_free_req(req);
3153 break;
3154 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003155
Pavel Begunkov50585b92019-11-07 01:41:07 +03003156 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003157 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3158 if (!mm_fault) {
3159 use_mm(ctx->sqo_mm);
3160 *mm = ctx->sqo_mm;
3161 }
3162 }
3163
Pavel Begunkov50585b92019-11-07 01:41:07 +03003164 sqe_flags = req->submit.sqe->flags;
3165
3166 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08003167 if (!shadow_req) {
3168 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08003169 if (unlikely(!shadow_req))
3170 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003171 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
3172 refcount_dec(&shadow_req->refs);
3173 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003174 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003175 }
3176
Jackie Liua1041c22019-09-18 17:25:52 +08003177out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03003178 req->submit.ring_file = ring_file;
3179 req->submit.ring_fd = ring_fd;
3180 req->submit.has_user = *mm != NULL;
3181 req->submit.in_async = async;
3182 req->submit.needs_fixed_file = async;
3183 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3184 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003185 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003186 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003187
3188 /*
3189 * If previous wasn't linked and we have a linked command,
3190 * that's the end of the chain. Submit the previous link.
3191 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003192 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Jackie Liua197f662019-11-08 08:09:12 -07003193 io_queue_link_head(link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003194 link = NULL;
3195 shadow_req = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003196 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003197 }
3198
Jens Axboe9e645e112019-05-10 16:07:28 -06003199 if (link)
Jackie Liua197f662019-11-08 08:09:12 -07003200 io_queue_link_head(link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003201 if (statep)
3202 io_submit_state_end(&state);
3203
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003204 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3205 io_commit_sqring(ctx);
3206
Jens Axboe6c271ce2019-01-10 11:22:30 -07003207 return submitted;
3208}
3209
3210static int io_sq_thread(void *data)
3211{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003212 struct io_ring_ctx *ctx = data;
3213 struct mm_struct *cur_mm = NULL;
3214 mm_segment_t old_fs;
3215 DEFINE_WAIT(wait);
3216 unsigned inflight;
3217 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003218 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003219
Jens Axboe206aefd2019-11-07 18:27:42 -07003220 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003221
Jens Axboe6c271ce2019-01-10 11:22:30 -07003222 old_fs = get_fs();
3223 set_fs(USER_DS);
3224
Jens Axboec1edbf52019-11-10 16:56:04 -07003225 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003226 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003227 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003228
3229 if (inflight) {
3230 unsigned nr_events = 0;
3231
3232 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003233 /*
3234 * inflight is the count of the maximum possible
3235 * entries we submitted, but it can be smaller
3236 * if we dropped some of them. If we don't have
3237 * poll entries available, then we know that we
3238 * have nothing left to poll for. Reset the
3239 * inflight count to zero in that case.
3240 */
3241 mutex_lock(&ctx->uring_lock);
3242 if (!list_empty(&ctx->poll_list))
3243 __io_iopoll_check(ctx, &nr_events, 0);
3244 else
3245 inflight = 0;
3246 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003247 } else {
3248 /*
3249 * Normal IO, just pretend everything completed.
3250 * We don't have to poll completions for that.
3251 */
3252 nr_events = inflight;
3253 }
3254
3255 inflight -= nr_events;
3256 if (!inflight)
3257 timeout = jiffies + ctx->sq_thread_idle;
3258 }
3259
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003260 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003261
3262 /*
3263 * If submit got -EBUSY, flag us as needing the application
3264 * to enter the kernel to reap and flush events.
3265 */
3266 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003267 /*
3268 * We're polling. If we're within the defined idle
3269 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003270 * to sleep. The exception is if we got EBUSY doing
3271 * more IO, we should wait for the application to
3272 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003273 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003274 if (inflight ||
3275 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003276 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003277 continue;
3278 }
3279
3280 /*
3281 * Drop cur_mm before scheduling, we can't hold it for
3282 * long periods (or over schedule()). Do this before
3283 * adding ourselves to the waitqueue, as the unuse/drop
3284 * may sleep.
3285 */
3286 if (cur_mm) {
3287 unuse_mm(cur_mm);
3288 mmput(cur_mm);
3289 cur_mm = NULL;
3290 }
3291
3292 prepare_to_wait(&ctx->sqo_wait, &wait,
3293 TASK_INTERRUPTIBLE);
3294
3295 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003296 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003297 /* make sure to read SQ tail after writing flags */
3298 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003299
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003300 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003301 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003302 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003303 finish_wait(&ctx->sqo_wait, &wait);
3304 break;
3305 }
3306 if (signal_pending(current))
3307 flush_signals(current);
3308 schedule();
3309 finish_wait(&ctx->sqo_wait, &wait);
3310
Hristo Venev75b28af2019-08-26 17:23:46 +00003311 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003312 continue;
3313 }
3314 finish_wait(&ctx->sqo_wait, &wait);
3315
Hristo Venev75b28af2019-08-26 17:23:46 +00003316 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003317 }
3318
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003319 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003320 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3321 if (ret > 0)
3322 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003323 }
3324
3325 set_fs(old_fs);
3326 if (cur_mm) {
3327 unuse_mm(cur_mm);
3328 mmput(cur_mm);
3329 }
Jens Axboe06058632019-04-13 09:26:03 -06003330
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003331 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003332
Jens Axboe6c271ce2019-01-10 11:22:30 -07003333 return 0;
3334}
3335
Jens Axboebda52162019-09-24 13:47:15 -06003336struct io_wait_queue {
3337 struct wait_queue_entry wq;
3338 struct io_ring_ctx *ctx;
3339 unsigned to_wait;
3340 unsigned nr_timeouts;
3341};
3342
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003343static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003344{
3345 struct io_ring_ctx *ctx = iowq->ctx;
3346
3347 /*
3348 * Wake up if we have enough events, or if a timeout occured since we
3349 * started waiting. For timeouts, we always want to return to userspace,
3350 * regardless of event count.
3351 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003352 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003353 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3354}
3355
3356static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3357 int wake_flags, void *key)
3358{
3359 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3360 wq);
3361
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003362 /* use noflush == true, as we can't safely rely on locking context */
3363 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003364 return -1;
3365
3366 return autoremove_wake_function(curr, mode, wake_flags, key);
3367}
3368
Jens Axboe2b188cc2019-01-07 10:46:33 -07003369/*
3370 * Wait until events become available, if we don't already have some. The
3371 * application must reap them itself, as they reside on the shared cq ring.
3372 */
3373static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3374 const sigset_t __user *sig, size_t sigsz)
3375{
Jens Axboebda52162019-09-24 13:47:15 -06003376 struct io_wait_queue iowq = {
3377 .wq = {
3378 .private = current,
3379 .func = io_wake_function,
3380 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3381 },
3382 .ctx = ctx,
3383 .to_wait = min_events,
3384 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003385 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003386 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003387
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003388 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003389 return 0;
3390
3391 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003392#ifdef CONFIG_COMPAT
3393 if (in_compat_syscall())
3394 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003395 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003396 else
3397#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003398 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003399
Jens Axboe2b188cc2019-01-07 10:46:33 -07003400 if (ret)
3401 return ret;
3402 }
3403
Jens Axboebda52162019-09-24 13:47:15 -06003404 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003405 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003406 do {
3407 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3408 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003409 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003410 break;
3411 schedule();
3412 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003413 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003414 break;
3415 }
3416 } while (1);
3417 finish_wait(&ctx->wait, &iowq.wq);
3418
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003419 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003420
Hristo Venev75b28af2019-08-26 17:23:46 +00003421 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003422}
3423
Jens Axboe6b063142019-01-10 22:13:58 -07003424static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3425{
3426#if defined(CONFIG_UNIX)
3427 if (ctx->ring_sock) {
3428 struct sock *sock = ctx->ring_sock->sk;
3429 struct sk_buff *skb;
3430
3431 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3432 kfree_skb(skb);
3433 }
3434#else
3435 int i;
3436
Jens Axboe65e19f52019-10-26 07:20:21 -06003437 for (i = 0; i < ctx->nr_user_files; i++) {
3438 struct file *file;
3439
3440 file = io_file_from_index(ctx, i);
3441 if (file)
3442 fput(file);
3443 }
Jens Axboe6b063142019-01-10 22:13:58 -07003444#endif
3445}
3446
3447static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3448{
Jens Axboe65e19f52019-10-26 07:20:21 -06003449 unsigned nr_tables, i;
3450
3451 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003452 return -ENXIO;
3453
3454 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003455 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3456 for (i = 0; i < nr_tables; i++)
3457 kfree(ctx->file_table[i].files);
3458 kfree(ctx->file_table);
3459 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003460 ctx->nr_user_files = 0;
3461 return 0;
3462}
3463
Jens Axboe6c271ce2019-01-10 11:22:30 -07003464static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3465{
3466 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003467 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003468 /*
3469 * The park is a bit of a work-around, without it we get
3470 * warning spews on shutdown with SQPOLL set and affinity
3471 * set to a single CPU.
3472 */
Jens Axboe06058632019-04-13 09:26:03 -06003473 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003474 kthread_stop(ctx->sqo_thread);
3475 ctx->sqo_thread = NULL;
3476 }
3477}
3478
Jens Axboe6b063142019-01-10 22:13:58 -07003479static void io_finish_async(struct io_ring_ctx *ctx)
3480{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003481 io_sq_thread_stop(ctx);
3482
Jens Axboe561fb042019-10-24 07:25:42 -06003483 if (ctx->io_wq) {
3484 io_wq_destroy(ctx->io_wq);
3485 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003486 }
3487}
3488
3489#if defined(CONFIG_UNIX)
3490static void io_destruct_skb(struct sk_buff *skb)
3491{
3492 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3493
Jens Axboe561fb042019-10-24 07:25:42 -06003494 if (ctx->io_wq)
3495 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003496
Jens Axboe6b063142019-01-10 22:13:58 -07003497 unix_destruct_scm(skb);
3498}
3499
3500/*
3501 * Ensure the UNIX gc is aware of our file set, so we are certain that
3502 * the io_uring can be safely unregistered on process exit, even if we have
3503 * loops in the file referencing.
3504 */
3505static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3506{
3507 struct sock *sk = ctx->ring_sock->sk;
3508 struct scm_fp_list *fpl;
3509 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003510 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003511
3512 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3513 unsigned long inflight = ctx->user->unix_inflight + nr;
3514
3515 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3516 return -EMFILE;
3517 }
3518
3519 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3520 if (!fpl)
3521 return -ENOMEM;
3522
3523 skb = alloc_skb(0, GFP_KERNEL);
3524 if (!skb) {
3525 kfree(fpl);
3526 return -ENOMEM;
3527 }
3528
3529 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003530
Jens Axboe08a45172019-10-03 08:11:03 -06003531 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003532 fpl->user = get_uid(ctx->user);
3533 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003534 struct file *file = io_file_from_index(ctx, i + offset);
3535
3536 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003537 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003538 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003539 unix_inflight(fpl->user, fpl->fp[nr_files]);
3540 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003541 }
3542
Jens Axboe08a45172019-10-03 08:11:03 -06003543 if (nr_files) {
3544 fpl->max = SCM_MAX_FD;
3545 fpl->count = nr_files;
3546 UNIXCB(skb).fp = fpl;
3547 skb->destructor = io_destruct_skb;
3548 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3549 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003550
Jens Axboe08a45172019-10-03 08:11:03 -06003551 for (i = 0; i < nr_files; i++)
3552 fput(fpl->fp[i]);
3553 } else {
3554 kfree_skb(skb);
3555 kfree(fpl);
3556 }
Jens Axboe6b063142019-01-10 22:13:58 -07003557
3558 return 0;
3559}
3560
3561/*
3562 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3563 * causes regular reference counting to break down. We rely on the UNIX
3564 * garbage collection to take care of this problem for us.
3565 */
3566static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3567{
3568 unsigned left, total;
3569 int ret = 0;
3570
3571 total = 0;
3572 left = ctx->nr_user_files;
3573 while (left) {
3574 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003575
3576 ret = __io_sqe_files_scm(ctx, this_files, total);
3577 if (ret)
3578 break;
3579 left -= this_files;
3580 total += this_files;
3581 }
3582
3583 if (!ret)
3584 return 0;
3585
3586 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003587 struct file *file = io_file_from_index(ctx, total);
3588
3589 if (file)
3590 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003591 total++;
3592 }
3593
3594 return ret;
3595}
3596#else
3597static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3598{
3599 return 0;
3600}
3601#endif
3602
Jens Axboe65e19f52019-10-26 07:20:21 -06003603static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3604 unsigned nr_files)
3605{
3606 int i;
3607
3608 for (i = 0; i < nr_tables; i++) {
3609 struct fixed_file_table *table = &ctx->file_table[i];
3610 unsigned this_files;
3611
3612 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3613 table->files = kcalloc(this_files, sizeof(struct file *),
3614 GFP_KERNEL);
3615 if (!table->files)
3616 break;
3617 nr_files -= this_files;
3618 }
3619
3620 if (i == nr_tables)
3621 return 0;
3622
3623 for (i = 0; i < nr_tables; i++) {
3624 struct fixed_file_table *table = &ctx->file_table[i];
3625 kfree(table->files);
3626 }
3627 return 1;
3628}
3629
Jens Axboe6b063142019-01-10 22:13:58 -07003630static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3631 unsigned nr_args)
3632{
3633 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003634 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003635 int fd, ret = 0;
3636 unsigned i;
3637
Jens Axboe65e19f52019-10-26 07:20:21 -06003638 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003639 return -EBUSY;
3640 if (!nr_args)
3641 return -EINVAL;
3642 if (nr_args > IORING_MAX_FIXED_FILES)
3643 return -EMFILE;
3644
Jens Axboe65e19f52019-10-26 07:20:21 -06003645 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3646 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3647 GFP_KERNEL);
3648 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003649 return -ENOMEM;
3650
Jens Axboe65e19f52019-10-26 07:20:21 -06003651 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3652 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003653 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003654 return -ENOMEM;
3655 }
3656
Jens Axboe08a45172019-10-03 08:11:03 -06003657 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003658 struct fixed_file_table *table;
3659 unsigned index;
3660
Jens Axboe6b063142019-01-10 22:13:58 -07003661 ret = -EFAULT;
3662 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3663 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003664 /* allow sparse sets */
3665 if (fd == -1) {
3666 ret = 0;
3667 continue;
3668 }
Jens Axboe6b063142019-01-10 22:13:58 -07003669
Jens Axboe65e19f52019-10-26 07:20:21 -06003670 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3671 index = i & IORING_FILE_TABLE_MASK;
3672 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003673
3674 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003675 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003676 break;
3677 /*
3678 * Don't allow io_uring instances to be registered. If UNIX
3679 * isn't enabled, then this causes a reference cycle and this
3680 * instance can never get freed. If UNIX is enabled we'll
3681 * handle it just fine, but there's still no point in allowing
3682 * a ring fd as it doesn't support regular read/write anyway.
3683 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003684 if (table->files[index]->f_op == &io_uring_fops) {
3685 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003686 break;
3687 }
Jens Axboe6b063142019-01-10 22:13:58 -07003688 ret = 0;
3689 }
3690
3691 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003692 for (i = 0; i < ctx->nr_user_files; i++) {
3693 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003694
Jens Axboe65e19f52019-10-26 07:20:21 -06003695 file = io_file_from_index(ctx, i);
3696 if (file)
3697 fput(file);
3698 }
3699 for (i = 0; i < nr_tables; i++)
3700 kfree(ctx->file_table[i].files);
3701
3702 kfree(ctx->file_table);
3703 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003704 ctx->nr_user_files = 0;
3705 return ret;
3706 }
3707
3708 ret = io_sqe_files_scm(ctx);
3709 if (ret)
3710 io_sqe_files_unregister(ctx);
3711
3712 return ret;
3713}
3714
Jens Axboec3a31e62019-10-03 13:59:56 -06003715static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3716{
3717#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003718 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003719 struct sock *sock = ctx->ring_sock->sk;
3720 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3721 struct sk_buff *skb;
3722 int i;
3723
3724 __skb_queue_head_init(&list);
3725
3726 /*
3727 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3728 * remove this entry and rearrange the file array.
3729 */
3730 skb = skb_dequeue(head);
3731 while (skb) {
3732 struct scm_fp_list *fp;
3733
3734 fp = UNIXCB(skb).fp;
3735 for (i = 0; i < fp->count; i++) {
3736 int left;
3737
3738 if (fp->fp[i] != file)
3739 continue;
3740
3741 unix_notinflight(fp->user, fp->fp[i]);
3742 left = fp->count - 1 - i;
3743 if (left) {
3744 memmove(&fp->fp[i], &fp->fp[i + 1],
3745 left * sizeof(struct file *));
3746 }
3747 fp->count--;
3748 if (!fp->count) {
3749 kfree_skb(skb);
3750 skb = NULL;
3751 } else {
3752 __skb_queue_tail(&list, skb);
3753 }
3754 fput(file);
3755 file = NULL;
3756 break;
3757 }
3758
3759 if (!file)
3760 break;
3761
3762 __skb_queue_tail(&list, skb);
3763
3764 skb = skb_dequeue(head);
3765 }
3766
3767 if (skb_peek(&list)) {
3768 spin_lock_irq(&head->lock);
3769 while ((skb = __skb_dequeue(&list)) != NULL)
3770 __skb_queue_tail(head, skb);
3771 spin_unlock_irq(&head->lock);
3772 }
3773#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003774 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003775#endif
3776}
3777
3778static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3779 int index)
3780{
3781#if defined(CONFIG_UNIX)
3782 struct sock *sock = ctx->ring_sock->sk;
3783 struct sk_buff_head *head = &sock->sk_receive_queue;
3784 struct sk_buff *skb;
3785
3786 /*
3787 * See if we can merge this file into an existing skb SCM_RIGHTS
3788 * file set. If there's no room, fall back to allocating a new skb
3789 * and filling it in.
3790 */
3791 spin_lock_irq(&head->lock);
3792 skb = skb_peek(head);
3793 if (skb) {
3794 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3795
3796 if (fpl->count < SCM_MAX_FD) {
3797 __skb_unlink(skb, head);
3798 spin_unlock_irq(&head->lock);
3799 fpl->fp[fpl->count] = get_file(file);
3800 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3801 fpl->count++;
3802 spin_lock_irq(&head->lock);
3803 __skb_queue_head(head, skb);
3804 } else {
3805 skb = NULL;
3806 }
3807 }
3808 spin_unlock_irq(&head->lock);
3809
3810 if (skb) {
3811 fput(file);
3812 return 0;
3813 }
3814
3815 return __io_sqe_files_scm(ctx, 1, index);
3816#else
3817 return 0;
3818#endif
3819}
3820
3821static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3822 unsigned nr_args)
3823{
3824 struct io_uring_files_update up;
3825 __s32 __user *fds;
3826 int fd, i, err;
3827 __u32 done;
3828
Jens Axboe65e19f52019-10-26 07:20:21 -06003829 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003830 return -ENXIO;
3831 if (!nr_args)
3832 return -EINVAL;
3833 if (copy_from_user(&up, arg, sizeof(up)))
3834 return -EFAULT;
3835 if (check_add_overflow(up.offset, nr_args, &done))
3836 return -EOVERFLOW;
3837 if (done > ctx->nr_user_files)
3838 return -EINVAL;
3839
3840 done = 0;
3841 fds = (__s32 __user *) up.fds;
3842 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003843 struct fixed_file_table *table;
3844 unsigned index;
3845
Jens Axboec3a31e62019-10-03 13:59:56 -06003846 err = 0;
3847 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3848 err = -EFAULT;
3849 break;
3850 }
3851 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003852 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3853 index = i & IORING_FILE_TABLE_MASK;
3854 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003855 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003856 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003857 }
3858 if (fd != -1) {
3859 struct file *file;
3860
3861 file = fget(fd);
3862 if (!file) {
3863 err = -EBADF;
3864 break;
3865 }
3866 /*
3867 * Don't allow io_uring instances to be registered. If
3868 * UNIX isn't enabled, then this causes a reference
3869 * cycle and this instance can never get freed. If UNIX
3870 * is enabled we'll handle it just fine, but there's
3871 * still no point in allowing a ring fd as it doesn't
3872 * support regular read/write anyway.
3873 */
3874 if (file->f_op == &io_uring_fops) {
3875 fput(file);
3876 err = -EBADF;
3877 break;
3878 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003879 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003880 err = io_sqe_file_register(ctx, file, i);
3881 if (err)
3882 break;
3883 }
3884 nr_args--;
3885 done++;
3886 up.offset++;
3887 }
3888
3889 return done ? done : err;
3890}
3891
Jens Axboe7d723062019-11-12 22:31:31 -07003892static void io_put_work(struct io_wq_work *work)
3893{
3894 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3895
3896 io_put_req(req);
3897}
3898
3899static void io_get_work(struct io_wq_work *work)
3900{
3901 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3902
3903 refcount_inc(&req->refs);
3904}
3905
Jens Axboe6c271ce2019-01-10 11:22:30 -07003906static int io_sq_offload_start(struct io_ring_ctx *ctx,
3907 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003908{
Jens Axboe561fb042019-10-24 07:25:42 -06003909 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003910 int ret;
3911
Jens Axboe6c271ce2019-01-10 11:22:30 -07003912 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003913 mmgrab(current->mm);
3914 ctx->sqo_mm = current->mm;
3915
Jens Axboe6c271ce2019-01-10 11:22:30 -07003916 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003917 ret = -EPERM;
3918 if (!capable(CAP_SYS_ADMIN))
3919 goto err;
3920
Jens Axboe917257d2019-04-13 09:28:55 -06003921 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3922 if (!ctx->sq_thread_idle)
3923 ctx->sq_thread_idle = HZ;
3924
Jens Axboe6c271ce2019-01-10 11:22:30 -07003925 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003926 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003927
Jens Axboe917257d2019-04-13 09:28:55 -06003928 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003929 if (cpu >= nr_cpu_ids)
3930 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003931 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003932 goto err;
3933
Jens Axboe6c271ce2019-01-10 11:22:30 -07003934 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3935 ctx, cpu,
3936 "io_uring-sq");
3937 } else {
3938 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3939 "io_uring-sq");
3940 }
3941 if (IS_ERR(ctx->sqo_thread)) {
3942 ret = PTR_ERR(ctx->sqo_thread);
3943 ctx->sqo_thread = NULL;
3944 goto err;
3945 }
3946 wake_up_process(ctx->sqo_thread);
3947 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3948 /* Can't have SQ_AFF without SQPOLL */
3949 ret = -EINVAL;
3950 goto err;
3951 }
3952
Jens Axboe561fb042019-10-24 07:25:42 -06003953 /* Do QD, or 4 * CPUS, whatever is smallest */
3954 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe7d723062019-11-12 22:31:31 -07003955 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user,
3956 io_get_work, io_put_work);
Jens Axboe975c99a52019-10-30 08:42:56 -06003957 if (IS_ERR(ctx->io_wq)) {
3958 ret = PTR_ERR(ctx->io_wq);
3959 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003960 goto err;
3961 }
3962
3963 return 0;
3964err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003965 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003966 mmdrop(ctx->sqo_mm);
3967 ctx->sqo_mm = NULL;
3968 return ret;
3969}
3970
3971static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3972{
3973 atomic_long_sub(nr_pages, &user->locked_vm);
3974}
3975
3976static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3977{
3978 unsigned long page_limit, cur_pages, new_pages;
3979
3980 /* Don't allow more pages than we can safely lock */
3981 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3982
3983 do {
3984 cur_pages = atomic_long_read(&user->locked_vm);
3985 new_pages = cur_pages + nr_pages;
3986 if (new_pages > page_limit)
3987 return -ENOMEM;
3988 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3989 new_pages) != cur_pages);
3990
3991 return 0;
3992}
3993
3994static void io_mem_free(void *ptr)
3995{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003996 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003997
Mark Rutland52e04ef2019-04-30 17:30:21 +01003998 if (!ptr)
3999 return;
4000
4001 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004002 if (put_page_testzero(page))
4003 free_compound_page(page);
4004}
4005
4006static void *io_mem_alloc(size_t size)
4007{
4008 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4009 __GFP_NORETRY;
4010
4011 return (void *) __get_free_pages(gfp_flags, get_order(size));
4012}
4013
Hristo Venev75b28af2019-08-26 17:23:46 +00004014static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4015 size_t *sq_offset)
4016{
4017 struct io_rings *rings;
4018 size_t off, sq_array_size;
4019
4020 off = struct_size(rings, cqes, cq_entries);
4021 if (off == SIZE_MAX)
4022 return SIZE_MAX;
4023
4024#ifdef CONFIG_SMP
4025 off = ALIGN(off, SMP_CACHE_BYTES);
4026 if (off == 0)
4027 return SIZE_MAX;
4028#endif
4029
4030 sq_array_size = array_size(sizeof(u32), sq_entries);
4031 if (sq_array_size == SIZE_MAX)
4032 return SIZE_MAX;
4033
4034 if (check_add_overflow(off, sq_array_size, &off))
4035 return SIZE_MAX;
4036
4037 if (sq_offset)
4038 *sq_offset = off;
4039
4040 return off;
4041}
4042
Jens Axboe2b188cc2019-01-07 10:46:33 -07004043static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4044{
Hristo Venev75b28af2019-08-26 17:23:46 +00004045 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004046
Hristo Venev75b28af2019-08-26 17:23:46 +00004047 pages = (size_t)1 << get_order(
4048 rings_size(sq_entries, cq_entries, NULL));
4049 pages += (size_t)1 << get_order(
4050 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004051
Hristo Venev75b28af2019-08-26 17:23:46 +00004052 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004053}
4054
Jens Axboeedafcce2019-01-09 09:16:05 -07004055static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4056{
4057 int i, j;
4058
4059 if (!ctx->user_bufs)
4060 return -ENXIO;
4061
4062 for (i = 0; i < ctx->nr_user_bufs; i++) {
4063 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4064
4065 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004066 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004067
4068 if (ctx->account_mem)
4069 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004070 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004071 imu->nr_bvecs = 0;
4072 }
4073
4074 kfree(ctx->user_bufs);
4075 ctx->user_bufs = NULL;
4076 ctx->nr_user_bufs = 0;
4077 return 0;
4078}
4079
4080static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4081 void __user *arg, unsigned index)
4082{
4083 struct iovec __user *src;
4084
4085#ifdef CONFIG_COMPAT
4086 if (ctx->compat) {
4087 struct compat_iovec __user *ciovs;
4088 struct compat_iovec ciov;
4089
4090 ciovs = (struct compat_iovec __user *) arg;
4091 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4092 return -EFAULT;
4093
4094 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4095 dst->iov_len = ciov.iov_len;
4096 return 0;
4097 }
4098#endif
4099 src = (struct iovec __user *) arg;
4100 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4101 return -EFAULT;
4102 return 0;
4103}
4104
4105static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4106 unsigned nr_args)
4107{
4108 struct vm_area_struct **vmas = NULL;
4109 struct page **pages = NULL;
4110 int i, j, got_pages = 0;
4111 int ret = -EINVAL;
4112
4113 if (ctx->user_bufs)
4114 return -EBUSY;
4115 if (!nr_args || nr_args > UIO_MAXIOV)
4116 return -EINVAL;
4117
4118 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4119 GFP_KERNEL);
4120 if (!ctx->user_bufs)
4121 return -ENOMEM;
4122
4123 for (i = 0; i < nr_args; i++) {
4124 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4125 unsigned long off, start, end, ubuf;
4126 int pret, nr_pages;
4127 struct iovec iov;
4128 size_t size;
4129
4130 ret = io_copy_iov(ctx, &iov, arg, i);
4131 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004132 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004133
4134 /*
4135 * Don't impose further limits on the size and buffer
4136 * constraints here, we'll -EINVAL later when IO is
4137 * submitted if they are wrong.
4138 */
4139 ret = -EFAULT;
4140 if (!iov.iov_base || !iov.iov_len)
4141 goto err;
4142
4143 /* arbitrary limit, but we need something */
4144 if (iov.iov_len > SZ_1G)
4145 goto err;
4146
4147 ubuf = (unsigned long) iov.iov_base;
4148 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4149 start = ubuf >> PAGE_SHIFT;
4150 nr_pages = end - start;
4151
4152 if (ctx->account_mem) {
4153 ret = io_account_mem(ctx->user, nr_pages);
4154 if (ret)
4155 goto err;
4156 }
4157
4158 ret = 0;
4159 if (!pages || nr_pages > got_pages) {
4160 kfree(vmas);
4161 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004162 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004163 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004164 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004165 sizeof(struct vm_area_struct *),
4166 GFP_KERNEL);
4167 if (!pages || !vmas) {
4168 ret = -ENOMEM;
4169 if (ctx->account_mem)
4170 io_unaccount_mem(ctx->user, nr_pages);
4171 goto err;
4172 }
4173 got_pages = nr_pages;
4174 }
4175
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004176 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004177 GFP_KERNEL);
4178 ret = -ENOMEM;
4179 if (!imu->bvec) {
4180 if (ctx->account_mem)
4181 io_unaccount_mem(ctx->user, nr_pages);
4182 goto err;
4183 }
4184
4185 ret = 0;
4186 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004187 pret = get_user_pages(ubuf, nr_pages,
4188 FOLL_WRITE | FOLL_LONGTERM,
4189 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004190 if (pret == nr_pages) {
4191 /* don't support file backed memory */
4192 for (j = 0; j < nr_pages; j++) {
4193 struct vm_area_struct *vma = vmas[j];
4194
4195 if (vma->vm_file &&
4196 !is_file_hugepages(vma->vm_file)) {
4197 ret = -EOPNOTSUPP;
4198 break;
4199 }
4200 }
4201 } else {
4202 ret = pret < 0 ? pret : -EFAULT;
4203 }
4204 up_read(&current->mm->mmap_sem);
4205 if (ret) {
4206 /*
4207 * if we did partial map, or found file backed vmas,
4208 * release any pages we did get
4209 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004210 if (pret > 0)
4211 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004212 if (ctx->account_mem)
4213 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004214 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004215 goto err;
4216 }
4217
4218 off = ubuf & ~PAGE_MASK;
4219 size = iov.iov_len;
4220 for (j = 0; j < nr_pages; j++) {
4221 size_t vec_len;
4222
4223 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4224 imu->bvec[j].bv_page = pages[j];
4225 imu->bvec[j].bv_len = vec_len;
4226 imu->bvec[j].bv_offset = off;
4227 off = 0;
4228 size -= vec_len;
4229 }
4230 /* store original address for later verification */
4231 imu->ubuf = ubuf;
4232 imu->len = iov.iov_len;
4233 imu->nr_bvecs = nr_pages;
4234
4235 ctx->nr_user_bufs++;
4236 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004237 kvfree(pages);
4238 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004239 return 0;
4240err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004241 kvfree(pages);
4242 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004243 io_sqe_buffer_unregister(ctx);
4244 return ret;
4245}
4246
Jens Axboe9b402842019-04-11 11:45:41 -06004247static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4248{
4249 __s32 __user *fds = arg;
4250 int fd;
4251
4252 if (ctx->cq_ev_fd)
4253 return -EBUSY;
4254
4255 if (copy_from_user(&fd, fds, sizeof(*fds)))
4256 return -EFAULT;
4257
4258 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4259 if (IS_ERR(ctx->cq_ev_fd)) {
4260 int ret = PTR_ERR(ctx->cq_ev_fd);
4261 ctx->cq_ev_fd = NULL;
4262 return ret;
4263 }
4264
4265 return 0;
4266}
4267
4268static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4269{
4270 if (ctx->cq_ev_fd) {
4271 eventfd_ctx_put(ctx->cq_ev_fd);
4272 ctx->cq_ev_fd = NULL;
4273 return 0;
4274 }
4275
4276 return -ENXIO;
4277}
4278
Jens Axboe2b188cc2019-01-07 10:46:33 -07004279static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4280{
Jens Axboe6b063142019-01-10 22:13:58 -07004281 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004282 if (ctx->sqo_mm)
4283 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004284
4285 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004286 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004287 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004288 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004289
Jens Axboe2b188cc2019-01-07 10:46:33 -07004290#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004291 if (ctx->ring_sock) {
4292 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004293 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004294 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004295#endif
4296
Hristo Venev75b28af2019-08-26 17:23:46 +00004297 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004298 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004299
4300 percpu_ref_exit(&ctx->refs);
4301 if (ctx->account_mem)
4302 io_unaccount_mem(ctx->user,
4303 ring_pages(ctx->sq_entries, ctx->cq_entries));
4304 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004305 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004306 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004307 kfree(ctx);
4308}
4309
4310static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4311{
4312 struct io_ring_ctx *ctx = file->private_data;
4313 __poll_t mask = 0;
4314
4315 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004316 /*
4317 * synchronizes with barrier from wq_has_sleeper call in
4318 * io_commit_cqring
4319 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004320 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004321 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4322 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004323 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004324 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004325 mask |= EPOLLIN | EPOLLRDNORM;
4326
4327 return mask;
4328}
4329
4330static int io_uring_fasync(int fd, struct file *file, int on)
4331{
4332 struct io_ring_ctx *ctx = file->private_data;
4333
4334 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4335}
4336
4337static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4338{
4339 mutex_lock(&ctx->uring_lock);
4340 percpu_ref_kill(&ctx->refs);
4341 mutex_unlock(&ctx->uring_lock);
4342
Jens Axboe5262f562019-09-17 12:26:57 -06004343 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004344 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004345
4346 if (ctx->io_wq)
4347 io_wq_cancel_all(ctx->io_wq);
4348
Jens Axboedef596e2019-01-09 08:59:42 -07004349 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004350 /* if we failed setting up the ctx, we might not have any rings */
4351 if (ctx->rings)
4352 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004353 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004354 io_ring_ctx_free(ctx);
4355}
4356
4357static int io_uring_release(struct inode *inode, struct file *file)
4358{
4359 struct io_ring_ctx *ctx = file->private_data;
4360
4361 file->private_data = NULL;
4362 io_ring_ctx_wait_and_kill(ctx);
4363 return 0;
4364}
4365
Jens Axboefcb323c2019-10-24 12:39:47 -06004366static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4367 struct files_struct *files)
4368{
4369 struct io_kiocb *req;
4370 DEFINE_WAIT(wait);
4371
4372 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004373 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004374
4375 spin_lock_irq(&ctx->inflight_lock);
4376 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004377 if (req->work.files != files)
4378 continue;
4379 /* req is being completed, ignore */
4380 if (!refcount_inc_not_zero(&req->refs))
4381 continue;
4382 cancel_req = req;
4383 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004384 }
Jens Axboe768134d2019-11-10 20:30:53 -07004385 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004386 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004387 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004388 spin_unlock_irq(&ctx->inflight_lock);
4389
Jens Axboe768134d2019-11-10 20:30:53 -07004390 /* We need to keep going until we don't find a matching req */
4391 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004392 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004393
4394 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4395 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004396 schedule();
4397 }
Jens Axboe768134d2019-11-10 20:30:53 -07004398 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004399}
4400
4401static int io_uring_flush(struct file *file, void *data)
4402{
4403 struct io_ring_ctx *ctx = file->private_data;
4404
4405 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004406 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4407 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004408 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004409 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004410 return 0;
4411}
4412
Jens Axboe2b188cc2019-01-07 10:46:33 -07004413static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4414{
4415 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4416 unsigned long sz = vma->vm_end - vma->vm_start;
4417 struct io_ring_ctx *ctx = file->private_data;
4418 unsigned long pfn;
4419 struct page *page;
4420 void *ptr;
4421
4422 switch (offset) {
4423 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004424 case IORING_OFF_CQ_RING:
4425 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004426 break;
4427 case IORING_OFF_SQES:
4428 ptr = ctx->sq_sqes;
4429 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004430 default:
4431 return -EINVAL;
4432 }
4433
4434 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004435 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004436 return -EINVAL;
4437
4438 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4439 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4440}
4441
4442SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4443 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4444 size_t, sigsz)
4445{
4446 struct io_ring_ctx *ctx;
4447 long ret = -EBADF;
4448 int submitted = 0;
4449 struct fd f;
4450
Jens Axboe6c271ce2019-01-10 11:22:30 -07004451 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004452 return -EINVAL;
4453
4454 f = fdget(fd);
4455 if (!f.file)
4456 return -EBADF;
4457
4458 ret = -EOPNOTSUPP;
4459 if (f.file->f_op != &io_uring_fops)
4460 goto out_fput;
4461
4462 ret = -ENXIO;
4463 ctx = f.file->private_data;
4464 if (!percpu_ref_tryget(&ctx->refs))
4465 goto out_fput;
4466
Jens Axboe6c271ce2019-01-10 11:22:30 -07004467 /*
4468 * For SQ polling, the thread will do all submissions and completions.
4469 * Just return the requested submit count, and wake the thread if
4470 * we were asked to.
4471 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004472 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004473 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004474 if (!list_empty_careful(&ctx->cq_overflow_list))
4475 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004476 if (flags & IORING_ENTER_SQ_WAKEUP)
4477 wake_up(&ctx->sqo_wait);
4478 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004479 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004480 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004481
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004482 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004483 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004484 /* already have mm, so io_submit_sqes() won't try to grab it */
4485 cur_mm = ctx->sqo_mm;
4486 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4487 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004488 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004489 }
4490 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004491 unsigned nr_events = 0;
4492
Jens Axboe2b188cc2019-01-07 10:46:33 -07004493 min_complete = min(min_complete, ctx->cq_entries);
4494
Jens Axboedef596e2019-01-09 08:59:42 -07004495 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004496 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004497 } else {
4498 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4499 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004500 }
4501
Pavel Begunkov6805b322019-10-08 02:18:42 +03004502 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004503out_fput:
4504 fdput(f);
4505 return submitted ? submitted : ret;
4506}
4507
4508static const struct file_operations io_uring_fops = {
4509 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004510 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004511 .mmap = io_uring_mmap,
4512 .poll = io_uring_poll,
4513 .fasync = io_uring_fasync,
4514};
4515
4516static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4517 struct io_uring_params *p)
4518{
Hristo Venev75b28af2019-08-26 17:23:46 +00004519 struct io_rings *rings;
4520 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004521
Hristo Venev75b28af2019-08-26 17:23:46 +00004522 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4523 if (size == SIZE_MAX)
4524 return -EOVERFLOW;
4525
4526 rings = io_mem_alloc(size);
4527 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004528 return -ENOMEM;
4529
Hristo Venev75b28af2019-08-26 17:23:46 +00004530 ctx->rings = rings;
4531 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4532 rings->sq_ring_mask = p->sq_entries - 1;
4533 rings->cq_ring_mask = p->cq_entries - 1;
4534 rings->sq_ring_entries = p->sq_entries;
4535 rings->cq_ring_entries = p->cq_entries;
4536 ctx->sq_mask = rings->sq_ring_mask;
4537 ctx->cq_mask = rings->cq_ring_mask;
4538 ctx->sq_entries = rings->sq_ring_entries;
4539 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004540
4541 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4542 if (size == SIZE_MAX)
4543 return -EOVERFLOW;
4544
4545 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004546 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004547 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004548
Jens Axboe2b188cc2019-01-07 10:46:33 -07004549 return 0;
4550}
4551
4552/*
4553 * Allocate an anonymous fd, this is what constitutes the application
4554 * visible backing of an io_uring instance. The application mmaps this
4555 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4556 * we have to tie this fd to a socket for file garbage collection purposes.
4557 */
4558static int io_uring_get_fd(struct io_ring_ctx *ctx)
4559{
4560 struct file *file;
4561 int ret;
4562
4563#if defined(CONFIG_UNIX)
4564 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4565 &ctx->ring_sock);
4566 if (ret)
4567 return ret;
4568#endif
4569
4570 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4571 if (ret < 0)
4572 goto err;
4573
4574 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4575 O_RDWR | O_CLOEXEC);
4576 if (IS_ERR(file)) {
4577 put_unused_fd(ret);
4578 ret = PTR_ERR(file);
4579 goto err;
4580 }
4581
4582#if defined(CONFIG_UNIX)
4583 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004584 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004585#endif
4586 fd_install(ret, file);
4587 return ret;
4588err:
4589#if defined(CONFIG_UNIX)
4590 sock_release(ctx->ring_sock);
4591 ctx->ring_sock = NULL;
4592#endif
4593 return ret;
4594}
4595
4596static int io_uring_create(unsigned entries, struct io_uring_params *p)
4597{
4598 struct user_struct *user = NULL;
4599 struct io_ring_ctx *ctx;
4600 bool account_mem;
4601 int ret;
4602
4603 if (!entries || entries > IORING_MAX_ENTRIES)
4604 return -EINVAL;
4605
4606 /*
4607 * Use twice as many entries for the CQ ring. It's possible for the
4608 * application to drive a higher depth than the size of the SQ ring,
4609 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004610 * some flexibility in overcommitting a bit. If the application has
4611 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4612 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004613 */
4614 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004615 if (p->flags & IORING_SETUP_CQSIZE) {
4616 /*
4617 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4618 * to a power-of-two, if it isn't already. We do NOT impose
4619 * any cq vs sq ring sizing.
4620 */
4621 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4622 return -EINVAL;
4623 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4624 } else {
4625 p->cq_entries = 2 * p->sq_entries;
4626 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004627
4628 user = get_uid(current_user());
4629 account_mem = !capable(CAP_IPC_LOCK);
4630
4631 if (account_mem) {
4632 ret = io_account_mem(user,
4633 ring_pages(p->sq_entries, p->cq_entries));
4634 if (ret) {
4635 free_uid(user);
4636 return ret;
4637 }
4638 }
4639
4640 ctx = io_ring_ctx_alloc(p);
4641 if (!ctx) {
4642 if (account_mem)
4643 io_unaccount_mem(user, ring_pages(p->sq_entries,
4644 p->cq_entries));
4645 free_uid(user);
4646 return -ENOMEM;
4647 }
4648 ctx->compat = in_compat_syscall();
4649 ctx->account_mem = account_mem;
4650 ctx->user = user;
4651
4652 ret = io_allocate_scq_urings(ctx, p);
4653 if (ret)
4654 goto err;
4655
Jens Axboe6c271ce2019-01-10 11:22:30 -07004656 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004657 if (ret)
4658 goto err;
4659
Jens Axboe2b188cc2019-01-07 10:46:33 -07004660 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004661 p->sq_off.head = offsetof(struct io_rings, sq.head);
4662 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4663 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4664 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4665 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4666 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4667 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004668
4669 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004670 p->cq_off.head = offsetof(struct io_rings, cq.head);
4671 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4672 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4673 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4674 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4675 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004676
Jens Axboe044c1ab2019-10-28 09:15:33 -06004677 /*
4678 * Install ring fd as the very last thing, so we don't risk someone
4679 * having closed it before we finish setup
4680 */
4681 ret = io_uring_get_fd(ctx);
4682 if (ret < 0)
4683 goto err;
4684
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004685 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004686 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004687 return ret;
4688err:
4689 io_ring_ctx_wait_and_kill(ctx);
4690 return ret;
4691}
4692
4693/*
4694 * Sets up an aio uring context, and returns the fd. Applications asks for a
4695 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4696 * params structure passed in.
4697 */
4698static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4699{
4700 struct io_uring_params p;
4701 long ret;
4702 int i;
4703
4704 if (copy_from_user(&p, params, sizeof(p)))
4705 return -EFAULT;
4706 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4707 if (p.resv[i])
4708 return -EINVAL;
4709 }
4710
Jens Axboe6c271ce2019-01-10 11:22:30 -07004711 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004712 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004713 return -EINVAL;
4714
4715 ret = io_uring_create(entries, &p);
4716 if (ret < 0)
4717 return ret;
4718
4719 if (copy_to_user(params, &p, sizeof(p)))
4720 return -EFAULT;
4721
4722 return ret;
4723}
4724
4725SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4726 struct io_uring_params __user *, params)
4727{
4728 return io_uring_setup(entries, params);
4729}
4730
Jens Axboeedafcce2019-01-09 09:16:05 -07004731static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4732 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004733 __releases(ctx->uring_lock)
4734 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004735{
4736 int ret;
4737
Jens Axboe35fa71a2019-04-22 10:23:23 -06004738 /*
4739 * We're inside the ring mutex, if the ref is already dying, then
4740 * someone else killed the ctx or is already going through
4741 * io_uring_register().
4742 */
4743 if (percpu_ref_is_dying(&ctx->refs))
4744 return -ENXIO;
4745
Jens Axboeedafcce2019-01-09 09:16:05 -07004746 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004747
4748 /*
4749 * Drop uring mutex before waiting for references to exit. If another
4750 * thread is currently inside io_uring_enter() it might need to grab
4751 * the uring_lock to make progress. If we hold it here across the drain
4752 * wait, then we can deadlock. It's safe to drop the mutex here, since
4753 * no new references will come in after we've killed the percpu ref.
4754 */
4755 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004756 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004757 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004758
4759 switch (opcode) {
4760 case IORING_REGISTER_BUFFERS:
4761 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4762 break;
4763 case IORING_UNREGISTER_BUFFERS:
4764 ret = -EINVAL;
4765 if (arg || nr_args)
4766 break;
4767 ret = io_sqe_buffer_unregister(ctx);
4768 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004769 case IORING_REGISTER_FILES:
4770 ret = io_sqe_files_register(ctx, arg, nr_args);
4771 break;
4772 case IORING_UNREGISTER_FILES:
4773 ret = -EINVAL;
4774 if (arg || nr_args)
4775 break;
4776 ret = io_sqe_files_unregister(ctx);
4777 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004778 case IORING_REGISTER_FILES_UPDATE:
4779 ret = io_sqe_files_update(ctx, arg, nr_args);
4780 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004781 case IORING_REGISTER_EVENTFD:
4782 ret = -EINVAL;
4783 if (nr_args != 1)
4784 break;
4785 ret = io_eventfd_register(ctx, arg);
4786 break;
4787 case IORING_UNREGISTER_EVENTFD:
4788 ret = -EINVAL;
4789 if (arg || nr_args)
4790 break;
4791 ret = io_eventfd_unregister(ctx);
4792 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004793 default:
4794 ret = -EINVAL;
4795 break;
4796 }
4797
4798 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004799 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004800 percpu_ref_reinit(&ctx->refs);
4801 return ret;
4802}
4803
4804SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4805 void __user *, arg, unsigned int, nr_args)
4806{
4807 struct io_ring_ctx *ctx;
4808 long ret = -EBADF;
4809 struct fd f;
4810
4811 f = fdget(fd);
4812 if (!f.file)
4813 return -EBADF;
4814
4815 ret = -EOPNOTSUPP;
4816 if (f.file->f_op != &io_uring_fops)
4817 goto out_fput;
4818
4819 ctx = f.file->private_data;
4820
4821 mutex_lock(&ctx->uring_lock);
4822 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4823 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004824 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4825 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004826out_fput:
4827 fdput(f);
4828 return ret;
4829}
4830
Jens Axboe2b188cc2019-01-07 10:46:33 -07004831static int __init io_uring_init(void)
4832{
4833 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4834 return 0;
4835};
4836__initcall(io_uring_init);