blob: d877c7f6368eec301e50b7da6eb95608001a7ebb [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 Axboede0617e2019-04-06 21:51:27 -0600386
Jens Axboe2b188cc2019-01-07 10:46:33 -0700387static struct kmem_cache *req_cachep;
388
389static const struct file_operations io_uring_fops;
390
391struct sock *io_uring_get_socket(struct file *file)
392{
393#if defined(CONFIG_UNIX)
394 if (file->f_op == &io_uring_fops) {
395 struct io_ring_ctx *ctx = file->private_data;
396
397 return ctx->ring_sock->sk;
398 }
399#endif
400 return NULL;
401}
402EXPORT_SYMBOL(io_uring_get_socket);
403
404static void io_ring_ctx_ref_free(struct percpu_ref *ref)
405{
406 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
407
Jens Axboe206aefd2019-11-07 18:27:42 -0700408 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700409}
410
411static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
412{
413 struct io_ring_ctx *ctx;
414
415 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
416 if (!ctx)
417 return NULL;
418
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700419 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
420 if (!ctx->fallback_req)
421 goto err;
422
Jens Axboe206aefd2019-11-07 18:27:42 -0700423 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
424 if (!ctx->completions)
425 goto err;
426
Roman Gushchin21482892019-05-07 10:01:48 -0700427 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700428 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
429 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700430
431 ctx->flags = p->flags;
432 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700433 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700434 init_completion(&ctx->completions[0]);
435 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700436 mutex_init(&ctx->uring_lock);
437 init_waitqueue_head(&ctx->wait);
438 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700439 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboeeac406c2019-11-14 12:09:58 -0700440 ctx->cancel_tree = RB_ROOT;
Jens Axboede0617e2019-04-06 21:51:27 -0600441 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600442 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600443 init_waitqueue_head(&ctx->inflight_wait);
444 spin_lock_init(&ctx->inflight_lock);
445 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700446 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700447err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700448 if (ctx->fallback_req)
449 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700450 kfree(ctx->completions);
451 kfree(ctx);
452 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700453}
454
Bob Liu9d858b22019-11-13 18:06:25 +0800455static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600456{
Jackie Liua197f662019-11-08 08:09:12 -0700457 struct io_ring_ctx *ctx = req->ctx;
458
Jens Axboe498ccd92019-10-25 10:04:25 -0600459 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
460 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600461}
462
Bob Liu9d858b22019-11-13 18:06:25 +0800463static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600464{
Bob Liu9d858b22019-11-13 18:06:25 +0800465 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
466 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600467
Bob Liu9d858b22019-11-13 18:06:25 +0800468 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600469}
470
471static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600472{
473 struct io_kiocb *req;
474
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600475 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800476 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600477 list_del_init(&req->list);
478 return req;
479 }
480
481 return NULL;
482}
483
Jens Axboe5262f562019-09-17 12:26:57 -0600484static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
485{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600486 struct io_kiocb *req;
487
488 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700489 if (req) {
490 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
491 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800492 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700493 list_del_init(&req->list);
494 return req;
495 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600496 }
497
498 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600499}
500
Jens Axboede0617e2019-04-06 21:51:27 -0600501static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700502{
Hristo Venev75b28af2019-08-26 17:23:46 +0000503 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700504
Hristo Venev75b28af2019-08-26 17:23:46 +0000505 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700506 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000507 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700508
Jens Axboe2b188cc2019-01-07 10:46:33 -0700509 if (wq_has_sleeper(&ctx->cq_wait)) {
510 wake_up_interruptible(&ctx->cq_wait);
511 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
512 }
513 }
514}
515
Jens Axboe561fb042019-10-24 07:25:42 -0600516static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600517{
Jens Axboe561fb042019-10-24 07:25:42 -0600518 u8 opcode = READ_ONCE(sqe->opcode);
519
520 return !(opcode == IORING_OP_READ_FIXED ||
521 opcode == IORING_OP_WRITE_FIXED);
522}
523
524static inline bool io_prep_async_work(struct io_kiocb *req)
525{
526 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600527
Jens Axboe6cc47d12019-09-18 11:18:23 -0600528 if (req->submit.sqe) {
529 switch (req->submit.sqe->opcode) {
530 case IORING_OP_WRITEV:
531 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600532 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700533 /* fall-through */
534 case IORING_OP_READV:
535 case IORING_OP_READ_FIXED:
536 case IORING_OP_SENDMSG:
537 case IORING_OP_RECVMSG:
538 case IORING_OP_ACCEPT:
539 case IORING_OP_POLL_ADD:
540 /*
541 * We know REQ_F_ISREG is not set on some of these
542 * opcodes, but this enables us to keep the check in
543 * just one place.
544 */
545 if (!(req->flags & REQ_F_ISREG))
546 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600547 break;
548 }
Jens Axboe561fb042019-10-24 07:25:42 -0600549 if (io_sqe_needs_user(req->submit.sqe))
550 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600551 }
552
Jens Axboe561fb042019-10-24 07:25:42 -0600553 return do_hashed;
554}
555
Jackie Liua197f662019-11-08 08:09:12 -0700556static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600557{
558 bool do_hashed = io_prep_async_work(req);
Jackie Liua197f662019-11-08 08:09:12 -0700559 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -0600560
561 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
562 req->flags);
563 if (!do_hashed) {
564 io_wq_enqueue(ctx->io_wq, &req->work);
565 } else {
566 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
567 file_inode(req->file));
568 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600569}
570
Jens Axboe5262f562019-09-17 12:26:57 -0600571static void io_kill_timeout(struct io_kiocb *req)
572{
573 int ret;
574
575 ret = hrtimer_try_to_cancel(&req->timeout.timer);
576 if (ret != -1) {
577 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600578 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700579 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800580 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600581 }
582}
583
584static void io_kill_timeouts(struct io_ring_ctx *ctx)
585{
586 struct io_kiocb *req, *tmp;
587
588 spin_lock_irq(&ctx->completion_lock);
589 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
590 io_kill_timeout(req);
591 spin_unlock_irq(&ctx->completion_lock);
592}
593
Jens Axboede0617e2019-04-06 21:51:27 -0600594static void io_commit_cqring(struct io_ring_ctx *ctx)
595{
596 struct io_kiocb *req;
597
Jens Axboe5262f562019-09-17 12:26:57 -0600598 while ((req = io_get_timeout_req(ctx)) != NULL)
599 io_kill_timeout(req);
600
Jens Axboede0617e2019-04-06 21:51:27 -0600601 __io_commit_cqring(ctx);
602
603 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800604 if (req->flags & REQ_F_SHADOW_DRAIN) {
605 /* Just for drain, free it. */
606 __io_free_req(req);
607 continue;
608 }
Jens Axboede0617e2019-04-06 21:51:27 -0600609 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700610 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600611 }
612}
613
Jens Axboe2b188cc2019-01-07 10:46:33 -0700614static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
615{
Hristo Venev75b28af2019-08-26 17:23:46 +0000616 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700617 unsigned tail;
618
619 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200620 /*
621 * writes to the cq entry need to come after reading head; the
622 * control dependency is enough as we're using WRITE_ONCE to
623 * fill the cq entry
624 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000625 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700626 return NULL;
627
628 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000629 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700630}
631
Jens Axboe8c838782019-03-12 15:48:16 -0600632static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
633{
634 if (waitqueue_active(&ctx->wait))
635 wake_up(&ctx->wait);
636 if (waitqueue_active(&ctx->sqo_wait))
637 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600638 if (ctx->cq_ev_fd)
639 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600640}
641
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700642static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700643{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700644 struct io_rings *rings = ctx->rings;
645 struct io_uring_cqe *cqe;
646 struct io_kiocb *req;
647 unsigned long flags;
648 LIST_HEAD(list);
649
650 if (!force) {
651 if (list_empty_careful(&ctx->cq_overflow_list))
652 return;
653 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
654 rings->cq_ring_entries))
655 return;
656 }
657
658 spin_lock_irqsave(&ctx->completion_lock, flags);
659
660 /* if force is set, the ring is going away. always drop after that */
661 if (force)
662 ctx->cq_overflow_flushed = true;
663
664 while (!list_empty(&ctx->cq_overflow_list)) {
665 cqe = io_get_cqring(ctx);
666 if (!cqe && !force)
667 break;
668
669 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
670 list);
671 list_move(&req->list, &list);
672 if (cqe) {
673 WRITE_ONCE(cqe->user_data, req->user_data);
674 WRITE_ONCE(cqe->res, req->result);
675 WRITE_ONCE(cqe->flags, 0);
676 } else {
677 WRITE_ONCE(ctx->rings->cq_overflow,
678 atomic_inc_return(&ctx->cached_cq_overflow));
679 }
680 }
681
682 io_commit_cqring(ctx);
683 spin_unlock_irqrestore(&ctx->completion_lock, flags);
684 io_cqring_ev_posted(ctx);
685
686 while (!list_empty(&list)) {
687 req = list_first_entry(&list, struct io_kiocb, list);
688 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800689 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700690 }
691}
692
Jens Axboe78e19bb2019-11-06 15:21:34 -0700693static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700694{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700695 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700696 struct io_uring_cqe *cqe;
697
Jens Axboe78e19bb2019-11-06 15:21:34 -0700698 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700699
Jens Axboe2b188cc2019-01-07 10:46:33 -0700700 /*
701 * If we can't get a cq entry, userspace overflowed the
702 * submission (by quite a lot). Increment the overflow count in
703 * the ring.
704 */
705 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700706 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700707 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700708 WRITE_ONCE(cqe->res, res);
709 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700710 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700711 WRITE_ONCE(ctx->rings->cq_overflow,
712 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700713 } else {
714 refcount_inc(&req->refs);
715 req->result = res;
716 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700717 }
718}
719
Jens Axboe78e19bb2019-11-06 15:21:34 -0700720static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700721{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700722 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700723 unsigned long flags;
724
725 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700726 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700727 io_commit_cqring(ctx);
728 spin_unlock_irqrestore(&ctx->completion_lock, flags);
729
Jens Axboe8c838782019-03-12 15:48:16 -0600730 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700731}
732
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700733static inline bool io_is_fallback_req(struct io_kiocb *req)
734{
735 return req == (struct io_kiocb *)
736 ((unsigned long) req->ctx->fallback_req & ~1UL);
737}
738
739static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
740{
741 struct io_kiocb *req;
742
743 req = ctx->fallback_req;
744 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
745 return req;
746
747 return NULL;
748}
749
Jens Axboe2579f912019-01-09 09:10:43 -0700750static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
751 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700752{
Jens Axboefd6fab22019-03-14 16:30:06 -0600753 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700754 struct io_kiocb *req;
755
756 if (!percpu_ref_tryget(&ctx->refs))
757 return NULL;
758
Jens Axboe2579f912019-01-09 09:10:43 -0700759 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600760 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700761 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700762 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700763 } else if (!state->free_reqs) {
764 size_t sz;
765 int ret;
766
767 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600768 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
769
770 /*
771 * Bulk alloc is all-or-nothing. If we fail to get a batch,
772 * retry single alloc to be on the safe side.
773 */
774 if (unlikely(ret <= 0)) {
775 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
776 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700777 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600778 ret = 1;
779 }
Jens Axboe2579f912019-01-09 09:10:43 -0700780 state->free_reqs = ret - 1;
781 state->cur_req = 1;
782 req = state->reqs[0];
783 } else {
784 req = state->reqs[state->cur_req];
785 state->free_reqs--;
786 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700787 }
788
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700789got_it:
Jens Axboe60c112b2019-06-21 10:20:18 -0600790 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700791 req->ctx = ctx;
792 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600793 /* one is dropped after submission, the other at completion */
794 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600795 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600796 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700797 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700798fallback:
799 req = io_get_fallback_req(ctx);
800 if (req)
801 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300802 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700803 return NULL;
804}
805
Jens Axboedef596e2019-01-09 08:59:42 -0700806static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
807{
808 if (*nr) {
809 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300810 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700811 *nr = 0;
812 }
813}
814
Jens Axboe9e645e112019-05-10 16:07:28 -0600815static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700816{
Jens Axboefcb323c2019-10-24 12:39:47 -0600817 struct io_ring_ctx *ctx = req->ctx;
818
Jens Axboe09bb8392019-03-13 12:39:28 -0600819 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
820 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600821 if (req->flags & REQ_F_INFLIGHT) {
822 unsigned long flags;
823
824 spin_lock_irqsave(&ctx->inflight_lock, flags);
825 list_del(&req->inflight_entry);
826 if (waitqueue_active(&ctx->inflight_wait))
827 wake_up(&ctx->inflight_wait);
828 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
829 }
830 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700831 if (likely(!io_is_fallback_req(req)))
832 kmem_cache_free(req_cachep, req);
833 else
834 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600835}
836
Jackie Liua197f662019-11-08 08:09:12 -0700837static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600838{
Jackie Liua197f662019-11-08 08:09:12 -0700839 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700840 int ret;
841
842 ret = hrtimer_try_to_cancel(&req->timeout.timer);
843 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700844 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700845 io_commit_cqring(ctx);
846 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800847 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700848 return true;
849 }
850
851 return false;
852}
853
Jens Axboeba816ad2019-09-28 11:36:45 -0600854static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600855{
Jens Axboe2665abf2019-11-05 12:40:47 -0700856 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600857 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700858 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600859
860 /*
861 * The list should never be empty when we are called here. But could
862 * potentially happen if the chain is messed up, check to be on the
863 * safe side.
864 */
865 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700866 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700867 list_del_init(&nxt->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600868 if (!list_empty(&req->link_list)) {
869 INIT_LIST_HEAD(&nxt->link_list);
870 list_splice(&req->link_list, &nxt->link_list);
871 nxt->flags |= REQ_F_LINK;
872 }
873
Jens Axboeba816ad2019-09-28 11:36:45 -0600874 /*
875 * If we're in async work, we can continue processing the chain
876 * in this context instead of having to queue up new async work.
877 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700878 if (req->flags & REQ_F_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700879 wake_ev = io_link_cancel_timeout(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700880
881 /* we dropped this link, get next */
882 nxt = list_first_entry_or_null(&req->link_list,
883 struct io_kiocb, list);
Jens Axboe960e4322019-11-12 07:56:39 -0700884 } else if (nxtptr && io_wq_current_is_worker()) {
Jens Axboeba816ad2019-09-28 11:36:45 -0600885 *nxtptr = nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700886 break;
887 } else {
Jackie Liua197f662019-11-08 08:09:12 -0700888 io_queue_async_work(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700889 break;
890 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600891 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700892
893 if (wake_ev)
894 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600895}
896
897/*
898 * Called if REQ_F_LINK is set, and we fail the head request
899 */
900static void io_fail_links(struct io_kiocb *req)
901{
Jens Axboe2665abf2019-11-05 12:40:47 -0700902 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600903 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700904 unsigned long flags;
905
906 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600907
908 while (!list_empty(&req->link_list)) {
909 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700910 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600911
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200912 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700913
914 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
915 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700916 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700917 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700918 io_cqring_fill_event(link, -ECANCELED);
919 io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700920 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600921 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700922
923 io_commit_cqring(ctx);
924 spin_unlock_irqrestore(&ctx->completion_lock, flags);
925 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600926}
927
Jackie Liuc69f8db2019-11-09 11:00:08 +0800928static void io_free_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600929{
Jens Axboe2665abf2019-11-05 12:40:47 -0700930 if (likely(!(req->flags & REQ_F_LINK))) {
931 __io_free_req(req);
932 return;
933 }
934
Jens Axboe9e645e112019-05-10 16:07:28 -0600935 /*
936 * If LINK is set, we have dependent requests in this chain. If we
937 * didn't fail this request, queue the first one up, moving any other
938 * dependencies to the next request. In case of failure, fail the rest
939 * of the chain.
940 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700941 if (req->flags & REQ_F_FAIL_LINK) {
942 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700943 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
944 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700945 struct io_ring_ctx *ctx = req->ctx;
946 unsigned long flags;
947
948 /*
949 * If this is a timeout link, we could be racing with the
950 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700951 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700952 */
953 spin_lock_irqsave(&ctx->completion_lock, flags);
954 io_req_link_next(req, nxt);
955 spin_unlock_irqrestore(&ctx->completion_lock, flags);
956 } else {
957 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600958 }
959
960 __io_free_req(req);
961}
962
Jackie Liuc69f8db2019-11-09 11:00:08 +0800963static void io_free_req(struct io_kiocb *req)
964{
965 io_free_req_find_next(req, NULL);
966}
967
Jens Axboeba816ad2019-09-28 11:36:45 -0600968/*
969 * Drop reference to request, return next in chain (if there is one) if this
970 * was the last reference to this request.
971 */
Jackie Liuec9c02a2019-11-08 23:50:36 +0800972static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -0600973{
Jens Axboeba816ad2019-09-28 11:36:45 -0600974 struct io_kiocb *nxt = NULL;
975
Jens Axboee65ef562019-03-12 10:16:44 -0600976 if (refcount_dec_and_test(&req->refs))
Jackie Liuc69f8db2019-11-09 11:00:08 +0800977 io_free_req_find_next(req, &nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600978
Jens Axboeba816ad2019-09-28 11:36:45 -0600979 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600980 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600981 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600982 else
Jackie Liua197f662019-11-08 08:09:12 -0700983 io_queue_async_work(nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600984 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700985}
986
Jens Axboe2b188cc2019-01-07 10:46:33 -0700987static void io_put_req(struct io_kiocb *req)
988{
Jens Axboedef596e2019-01-09 08:59:42 -0700989 if (refcount_dec_and_test(&req->refs))
990 io_free_req(req);
991}
992
Jens Axboe78e19bb2019-11-06 15:21:34 -0700993static void io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -0600994{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700995 /* drop both submit and complete references */
996 if (refcount_sub_and_test(2, &req->refs))
997 __io_free_req(req);
998}
999
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001000static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001001{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001002 struct io_rings *rings = ctx->rings;
1003
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001004 /*
1005 * noflush == true is from the waitqueue handler, just ensure we wake
1006 * up the task, and the next invocation will flush the entries. We
1007 * cannot safely to it from here.
1008 */
1009 if (noflush && !list_empty(&ctx->cq_overflow_list))
1010 return -1U;
1011
1012 io_cqring_overflow_flush(ctx, false);
1013
Jens Axboea3a0e432019-08-20 11:03:11 -06001014 /* See comment at the top of this file */
1015 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001016 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001017}
1018
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001019static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1020{
1021 struct io_rings *rings = ctx->rings;
1022
1023 /* make sure SQ entry isn't read before tail */
1024 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1025}
1026
Jens Axboedef596e2019-01-09 08:59:42 -07001027/*
1028 * Find and free completed poll iocbs
1029 */
1030static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1031 struct list_head *done)
1032{
1033 void *reqs[IO_IOPOLL_BATCH];
1034 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001035 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001036
Jens Axboe09bb8392019-03-13 12:39:28 -06001037 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001038 while (!list_empty(done)) {
1039 req = list_first_entry(done, struct io_kiocb, list);
1040 list_del(&req->list);
1041
Jens Axboe78e19bb2019-11-06 15:21:34 -07001042 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001043 (*nr_events)++;
1044
Jens Axboe09bb8392019-03-13 12:39:28 -06001045 if (refcount_dec_and_test(&req->refs)) {
1046 /* If we're not using fixed files, we have to pair the
1047 * completion part with the file put. Use regular
1048 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001049 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001050 */
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001051 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1052 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001053 reqs[to_free++] = req;
1054 if (to_free == ARRAY_SIZE(reqs))
1055 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001056 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001057 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001058 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001059 }
Jens Axboedef596e2019-01-09 08:59:42 -07001060 }
Jens Axboedef596e2019-01-09 08:59:42 -07001061
Jens Axboe09bb8392019-03-13 12:39:28 -06001062 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001063 io_free_req_many(ctx, reqs, &to_free);
1064}
1065
1066static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1067 long min)
1068{
1069 struct io_kiocb *req, *tmp;
1070 LIST_HEAD(done);
1071 bool spin;
1072 int ret;
1073
1074 /*
1075 * Only spin for completions if we don't have multiple devices hanging
1076 * off our complete list, and we're under the requested amount.
1077 */
1078 spin = !ctx->poll_multi_file && *nr_events < min;
1079
1080 ret = 0;
1081 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1082 struct kiocb *kiocb = &req->rw;
1083
1084 /*
1085 * Move completed entries to our local list. If we find a
1086 * request that requires polling, break out and complete
1087 * the done list first, if we have entries there.
1088 */
1089 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1090 list_move_tail(&req->list, &done);
1091 continue;
1092 }
1093 if (!list_empty(&done))
1094 break;
1095
1096 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1097 if (ret < 0)
1098 break;
1099
1100 if (ret && spin)
1101 spin = false;
1102 ret = 0;
1103 }
1104
1105 if (!list_empty(&done))
1106 io_iopoll_complete(ctx, nr_events, &done);
1107
1108 return ret;
1109}
1110
1111/*
1112 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1113 * non-spinning poll check - we'll still enter the driver poll loop, but only
1114 * as a non-spinning completion check.
1115 */
1116static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1117 long min)
1118{
Jens Axboe08f54392019-08-21 22:19:11 -06001119 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001120 int ret;
1121
1122 ret = io_do_iopoll(ctx, nr_events, min);
1123 if (ret < 0)
1124 return ret;
1125 if (!min || *nr_events >= min)
1126 return 0;
1127 }
1128
1129 return 1;
1130}
1131
1132/*
1133 * We can't just wait for polled events to come to us, we have to actively
1134 * find and complete them.
1135 */
1136static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1137{
1138 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1139 return;
1140
1141 mutex_lock(&ctx->uring_lock);
1142 while (!list_empty(&ctx->poll_list)) {
1143 unsigned int nr_events = 0;
1144
1145 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001146
1147 /*
1148 * Ensure we allow local-to-the-cpu processing to take place,
1149 * in this case we need to ensure that we reap all events.
1150 */
1151 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001152 }
1153 mutex_unlock(&ctx->uring_lock);
1154}
1155
Jens Axboe2b2ed972019-10-25 10:06:15 -06001156static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1157 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001158{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001159 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001160
1161 do {
1162 int tmin = 0;
1163
Jens Axboe500f9fb2019-08-19 12:15:59 -06001164 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001165 * Don't enter poll loop if we already have events pending.
1166 * If we do, we can potentially be spinning for commands that
1167 * already triggered a CQE (eg in error).
1168 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001169 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001170 break;
1171
1172 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001173 * If a submit got punted to a workqueue, we can have the
1174 * application entering polling for a command before it gets
1175 * issued. That app will hold the uring_lock for the duration
1176 * of the poll right here, so we need to take a breather every
1177 * now and then to ensure that the issue has a chance to add
1178 * the poll to the issued list. Otherwise we can spin here
1179 * forever, while the workqueue is stuck trying to acquire the
1180 * very same mutex.
1181 */
1182 if (!(++iters & 7)) {
1183 mutex_unlock(&ctx->uring_lock);
1184 mutex_lock(&ctx->uring_lock);
1185 }
1186
Jens Axboedef596e2019-01-09 08:59:42 -07001187 if (*nr_events < min)
1188 tmin = min - *nr_events;
1189
1190 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1191 if (ret <= 0)
1192 break;
1193 ret = 0;
1194 } while (min && !*nr_events && !need_resched());
1195
Jens Axboe2b2ed972019-10-25 10:06:15 -06001196 return ret;
1197}
1198
1199static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1200 long min)
1201{
1202 int ret;
1203
1204 /*
1205 * We disallow the app entering submit/complete with polling, but we
1206 * still need to lock the ring to prevent racing with polled issue
1207 * that got punted to a workqueue.
1208 */
1209 mutex_lock(&ctx->uring_lock);
1210 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001211 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001212 return ret;
1213}
1214
Jens Axboe491381ce2019-10-17 09:20:46 -06001215static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001216{
Jens Axboe491381ce2019-10-17 09:20:46 -06001217 /*
1218 * Tell lockdep we inherited freeze protection from submission
1219 * thread.
1220 */
1221 if (req->flags & REQ_F_ISREG) {
1222 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001223
Jens Axboe491381ce2019-10-17 09:20:46 -06001224 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001225 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001226 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001227}
1228
Jens Axboeba816ad2019-09-28 11:36:45 -06001229static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001230{
1231 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1232
Jens Axboe491381ce2019-10-17 09:20:46 -06001233 if (kiocb->ki_flags & IOCB_WRITE)
1234 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001235
Jens Axboe9e645e112019-05-10 16:07:28 -06001236 if ((req->flags & REQ_F_LINK) && res != req->result)
1237 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001238 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001239}
1240
1241static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1242{
1243 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1244
1245 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001246 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001247}
1248
Jens Axboeba816ad2019-09-28 11:36:45 -06001249static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1250{
1251 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001252 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001253
1254 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001255 io_put_req_find_next(req, &nxt);
1256
1257 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001258}
1259
Jens Axboedef596e2019-01-09 08:59:42 -07001260static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1261{
1262 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1263
Jens Axboe491381ce2019-10-17 09:20:46 -06001264 if (kiocb->ki_flags & IOCB_WRITE)
1265 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001266
Jens Axboe9e645e112019-05-10 16:07:28 -06001267 if ((req->flags & REQ_F_LINK) && res != req->result)
1268 req->flags |= REQ_F_FAIL_LINK;
1269 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001270 if (res != -EAGAIN)
1271 req->flags |= REQ_F_IOPOLL_COMPLETED;
1272}
1273
1274/*
1275 * After the iocb has been issued, it's safe to be found on the poll list.
1276 * Adding the kiocb to the list AFTER submission ensures that we don't
1277 * find it from a io_iopoll_getevents() thread before the issuer is done
1278 * accessing the kiocb cookie.
1279 */
1280static void io_iopoll_req_issued(struct io_kiocb *req)
1281{
1282 struct io_ring_ctx *ctx = req->ctx;
1283
1284 /*
1285 * Track whether we have multiple files in our lists. This will impact
1286 * how we do polling eventually, not spinning if we're on potentially
1287 * different devices.
1288 */
1289 if (list_empty(&ctx->poll_list)) {
1290 ctx->poll_multi_file = false;
1291 } else if (!ctx->poll_multi_file) {
1292 struct io_kiocb *list_req;
1293
1294 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1295 list);
1296 if (list_req->rw.ki_filp != req->rw.ki_filp)
1297 ctx->poll_multi_file = true;
1298 }
1299
1300 /*
1301 * For fast devices, IO may have already completed. If it has, add
1302 * it to the front so we find it first.
1303 */
1304 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1305 list_add(&req->list, &ctx->poll_list);
1306 else
1307 list_add_tail(&req->list, &ctx->poll_list);
1308}
1309
Jens Axboe3d6770f2019-04-13 11:50:54 -06001310static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001311{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001312 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001313 int diff = state->has_refs - state->used_refs;
1314
1315 if (diff)
1316 fput_many(state->file, diff);
1317 state->file = NULL;
1318 }
1319}
1320
1321/*
1322 * Get as many references to a file as we have IOs left in this submission,
1323 * assuming most submissions are for one file, or at least that each file
1324 * has more than one submission.
1325 */
1326static struct file *io_file_get(struct io_submit_state *state, int fd)
1327{
1328 if (!state)
1329 return fget(fd);
1330
1331 if (state->file) {
1332 if (state->fd == fd) {
1333 state->used_refs++;
1334 state->ios_left--;
1335 return state->file;
1336 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001337 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001338 }
1339 state->file = fget_many(fd, state->ios_left);
1340 if (!state->file)
1341 return NULL;
1342
1343 state->fd = fd;
1344 state->has_refs = state->ios_left;
1345 state->used_refs = 1;
1346 state->ios_left--;
1347 return state->file;
1348}
1349
Jens Axboe2b188cc2019-01-07 10:46:33 -07001350/*
1351 * If we tracked the file through the SCM inflight mechanism, we could support
1352 * any file. For now, just ensure that anything potentially problematic is done
1353 * inline.
1354 */
1355static bool io_file_supports_async(struct file *file)
1356{
1357 umode_t mode = file_inode(file)->i_mode;
1358
1359 if (S_ISBLK(mode) || S_ISCHR(mode))
1360 return true;
1361 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1362 return true;
1363
1364 return false;
1365}
1366
Pavel Begunkov267bc902019-11-07 01:41:08 +03001367static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001368{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001369 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001370 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001371 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001372 unsigned ioprio;
1373 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001374
Jens Axboe09bb8392019-03-13 12:39:28 -06001375 if (!req->file)
1376 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001377
Jens Axboe491381ce2019-10-17 09:20:46 -06001378 if (S_ISREG(file_inode(req->file)->i_mode))
1379 req->flags |= REQ_F_ISREG;
1380
1381 /*
1382 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1383 * we know to async punt it even if it was opened O_NONBLOCK
1384 */
1385 if (force_nonblock && !io_file_supports_async(req->file)) {
1386 req->flags |= REQ_F_MUST_PUNT;
1387 return -EAGAIN;
1388 }
Jens Axboe6b063142019-01-10 22:13:58 -07001389
Jens Axboe2b188cc2019-01-07 10:46:33 -07001390 kiocb->ki_pos = READ_ONCE(sqe->off);
1391 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1392 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1393
1394 ioprio = READ_ONCE(sqe->ioprio);
1395 if (ioprio) {
1396 ret = ioprio_check_cap(ioprio);
1397 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001398 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001399
1400 kiocb->ki_ioprio = ioprio;
1401 } else
1402 kiocb->ki_ioprio = get_current_ioprio();
1403
1404 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1405 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001406 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001407
1408 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001409 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1410 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001411 req->flags |= REQ_F_NOWAIT;
1412
1413 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001414 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001415
Jens Axboedef596e2019-01-09 08:59:42 -07001416 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001417 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1418 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001419 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420
Jens Axboedef596e2019-01-09 08:59:42 -07001421 kiocb->ki_flags |= IOCB_HIPRI;
1422 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001423 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001424 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001425 if (kiocb->ki_flags & IOCB_HIPRI)
1426 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001427 kiocb->ki_complete = io_complete_rw;
1428 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001429 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001430}
1431
1432static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1433{
1434 switch (ret) {
1435 case -EIOCBQUEUED:
1436 break;
1437 case -ERESTARTSYS:
1438 case -ERESTARTNOINTR:
1439 case -ERESTARTNOHAND:
1440 case -ERESTART_RESTARTBLOCK:
1441 /*
1442 * We can't just restart the syscall, since previously
1443 * submitted sqes may already be in progress. Just fail this
1444 * IO with EINTR.
1445 */
1446 ret = -EINTR;
1447 /* fall through */
1448 default:
1449 kiocb->ki_complete(kiocb, ret, 0);
1450 }
1451}
1452
Jens Axboeba816ad2019-09-28 11:36:45 -06001453static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1454 bool in_async)
1455{
1456 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1457 *nxt = __io_complete_rw(kiocb, ret);
1458 else
1459 io_rw_done(kiocb, ret);
1460}
1461
Jens Axboeedafcce2019-01-09 09:16:05 -07001462static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1463 const struct io_uring_sqe *sqe,
1464 struct iov_iter *iter)
1465{
1466 size_t len = READ_ONCE(sqe->len);
1467 struct io_mapped_ubuf *imu;
1468 unsigned index, buf_index;
1469 size_t offset;
1470 u64 buf_addr;
1471
1472 /* attempt to use fixed buffers without having provided iovecs */
1473 if (unlikely(!ctx->user_bufs))
1474 return -EFAULT;
1475
1476 buf_index = READ_ONCE(sqe->buf_index);
1477 if (unlikely(buf_index >= ctx->nr_user_bufs))
1478 return -EFAULT;
1479
1480 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1481 imu = &ctx->user_bufs[index];
1482 buf_addr = READ_ONCE(sqe->addr);
1483
1484 /* overflow */
1485 if (buf_addr + len < buf_addr)
1486 return -EFAULT;
1487 /* not inside the mapped region */
1488 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1489 return -EFAULT;
1490
1491 /*
1492 * May not be a start of buffer, set size appropriately
1493 * and advance us to the beginning.
1494 */
1495 offset = buf_addr - imu->ubuf;
1496 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001497
1498 if (offset) {
1499 /*
1500 * Don't use iov_iter_advance() here, as it's really slow for
1501 * using the latter parts of a big fixed buffer - it iterates
1502 * over each segment manually. We can cheat a bit here, because
1503 * we know that:
1504 *
1505 * 1) it's a BVEC iter, we set it up
1506 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1507 * first and last bvec
1508 *
1509 * So just find our index, and adjust the iterator afterwards.
1510 * If the offset is within the first bvec (or the whole first
1511 * bvec, just use iov_iter_advance(). This makes it easier
1512 * since we can just skip the first segment, which may not
1513 * be PAGE_SIZE aligned.
1514 */
1515 const struct bio_vec *bvec = imu->bvec;
1516
1517 if (offset <= bvec->bv_len) {
1518 iov_iter_advance(iter, offset);
1519 } else {
1520 unsigned long seg_skip;
1521
1522 /* skip first vec */
1523 offset -= bvec->bv_len;
1524 seg_skip = 1 + (offset >> PAGE_SHIFT);
1525
1526 iter->bvec = bvec + seg_skip;
1527 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001528 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001529 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001530 }
1531 }
1532
Jens Axboe5e559562019-11-13 16:12:46 -07001533 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001534}
1535
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001536static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1537 const struct sqe_submit *s, struct iovec **iovec,
1538 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001539{
1540 const struct io_uring_sqe *sqe = s->sqe;
1541 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1542 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001543 u8 opcode;
1544
1545 /*
1546 * We're reading ->opcode for the second time, but the first read
1547 * doesn't care whether it's _FIXED or not, so it doesn't matter
1548 * whether ->opcode changes concurrently. The first read does care
1549 * about whether it is a READ or a WRITE, so we don't trust this read
1550 * for that purpose and instead let the caller pass in the read/write
1551 * flag.
1552 */
1553 opcode = READ_ONCE(sqe->opcode);
1554 if (opcode == IORING_OP_READ_FIXED ||
1555 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001556 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001557 *iovec = NULL;
1558 return ret;
1559 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001560
1561 if (!s->has_user)
1562 return -EFAULT;
1563
1564#ifdef CONFIG_COMPAT
1565 if (ctx->compat)
1566 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1567 iovec, iter);
1568#endif
1569
1570 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1571}
1572
Jens Axboe32960612019-09-23 11:05:34 -06001573/*
1574 * For files that don't have ->read_iter() and ->write_iter(), handle them
1575 * by looping over ->read() or ->write() manually.
1576 */
1577static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1578 struct iov_iter *iter)
1579{
1580 ssize_t ret = 0;
1581
1582 /*
1583 * Don't support polled IO through this interface, and we can't
1584 * support non-blocking either. For the latter, this just causes
1585 * the kiocb to be handled from an async context.
1586 */
1587 if (kiocb->ki_flags & IOCB_HIPRI)
1588 return -EOPNOTSUPP;
1589 if (kiocb->ki_flags & IOCB_NOWAIT)
1590 return -EAGAIN;
1591
1592 while (iov_iter_count(iter)) {
1593 struct iovec iovec = iov_iter_iovec(iter);
1594 ssize_t nr;
1595
1596 if (rw == READ) {
1597 nr = file->f_op->read(file, iovec.iov_base,
1598 iovec.iov_len, &kiocb->ki_pos);
1599 } else {
1600 nr = file->f_op->write(file, iovec.iov_base,
1601 iovec.iov_len, &kiocb->ki_pos);
1602 }
1603
1604 if (nr < 0) {
1605 if (!ret)
1606 ret = nr;
1607 break;
1608 }
1609 ret += nr;
1610 if (nr != iovec.iov_len)
1611 break;
1612 iov_iter_advance(iter, nr);
1613 }
1614
1615 return ret;
1616}
1617
Pavel Begunkov267bc902019-11-07 01:41:08 +03001618static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001619 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001620{
1621 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1622 struct kiocb *kiocb = &req->rw;
1623 struct iov_iter iter;
1624 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001625 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001626 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001627
Pavel Begunkov267bc902019-11-07 01:41:08 +03001628 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001629 if (ret)
1630 return ret;
1631 file = kiocb->ki_filp;
1632
Jens Axboe2b188cc2019-01-07 10:46:33 -07001633 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001634 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001635
Pavel Begunkov267bc902019-11-07 01:41:08 +03001636 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001637 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001638 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001639
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001640 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001641 if (req->flags & REQ_F_LINK)
1642 req->result = read_size;
1643
Jens Axboe31b51512019-01-18 22:56:34 -07001644 iov_count = iov_iter_count(&iter);
1645 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001646 if (!ret) {
1647 ssize_t ret2;
1648
Jens Axboe32960612019-09-23 11:05:34 -06001649 if (file->f_op->read_iter)
1650 ret2 = call_read_iter(file, kiocb, &iter);
1651 else
1652 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1653
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001654 /*
1655 * In case of a short read, punt to async. This can happen
1656 * if we have data partially cached. Alternatively we can
1657 * return the short read, in which case the application will
1658 * need to issue another SQE and wait for it. That SQE will
1659 * need async punt anyway, so it's more efficient to do it
1660 * here.
1661 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001662 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1663 (req->flags & REQ_F_ISREG) &&
1664 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001665 ret2 = -EAGAIN;
1666 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001667 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001668 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001669 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001670 ret = -EAGAIN;
1671 }
1672 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001673 return ret;
1674}
1675
Pavel Begunkov267bc902019-11-07 01:41:08 +03001676static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001677 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001678{
1679 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1680 struct kiocb *kiocb = &req->rw;
1681 struct iov_iter iter;
1682 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001683 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001684 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001685
Pavel Begunkov267bc902019-11-07 01:41:08 +03001686 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001687 if (ret)
1688 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001689
Jens Axboe2b188cc2019-01-07 10:46:33 -07001690 file = kiocb->ki_filp;
1691 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001692 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001693
Pavel Begunkov267bc902019-11-07 01:41:08 +03001694 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001695 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001696 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001697
Jens Axboe9e645e112019-05-10 16:07:28 -06001698 if (req->flags & REQ_F_LINK)
1699 req->result = ret;
1700
Jens Axboe31b51512019-01-18 22:56:34 -07001701 iov_count = iov_iter_count(&iter);
1702
1703 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001704 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001705 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001706
1707 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001709 ssize_t ret2;
1710
Jens Axboe2b188cc2019-01-07 10:46:33 -07001711 /*
1712 * Open-code file_start_write here to grab freeze protection,
1713 * which will be released by another thread in
1714 * io_complete_rw(). Fool lockdep by telling it the lock got
1715 * released so that it doesn't complain about the held lock when
1716 * we return to userspace.
1717 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001718 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001719 __sb_start_write(file_inode(file)->i_sb,
1720 SB_FREEZE_WRITE, true);
1721 __sb_writers_release(file_inode(file)->i_sb,
1722 SB_FREEZE_WRITE);
1723 }
1724 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001725
Jens Axboe32960612019-09-23 11:05:34 -06001726 if (file->f_op->write_iter)
1727 ret2 = call_write_iter(file, kiocb, &iter);
1728 else
1729 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001730 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001731 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001732 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001733 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001734 }
Jens Axboe31b51512019-01-18 22:56:34 -07001735out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001736 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737 return ret;
1738}
1739
1740/*
1741 * IORING_OP_NOP just posts a completion event, nothing else.
1742 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001743static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001744{
1745 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001746
Jens Axboedef596e2019-01-09 08:59:42 -07001747 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1748 return -EINVAL;
1749
Jens Axboe78e19bb2019-11-06 15:21:34 -07001750 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001751 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001752 return 0;
1753}
1754
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001755static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1756{
Jens Axboe6b063142019-01-10 22:13:58 -07001757 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001758
Jens Axboe09bb8392019-03-13 12:39:28 -06001759 if (!req->file)
1760 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001761
Jens Axboe6b063142019-01-10 22:13:58 -07001762 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001763 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001764 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001765 return -EINVAL;
1766
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001767 return 0;
1768}
1769
1770static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001771 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001772{
1773 loff_t sqe_off = READ_ONCE(sqe->off);
1774 loff_t sqe_len = READ_ONCE(sqe->len);
1775 loff_t end = sqe_off + sqe_len;
1776 unsigned fsync_flags;
1777 int ret;
1778
1779 fsync_flags = READ_ONCE(sqe->fsync_flags);
1780 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1781 return -EINVAL;
1782
1783 ret = io_prep_fsync(req, sqe);
1784 if (ret)
1785 return ret;
1786
1787 /* fsync always requires a blocking context */
1788 if (force_nonblock)
1789 return -EAGAIN;
1790
1791 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1792 end > 0 ? end : LLONG_MAX,
1793 fsync_flags & IORING_FSYNC_DATASYNC);
1794
Jens Axboe9e645e112019-05-10 16:07:28 -06001795 if (ret < 0 && (req->flags & REQ_F_LINK))
1796 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001797 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001798 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001799 return 0;
1800}
1801
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001802static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1803{
1804 struct io_ring_ctx *ctx = req->ctx;
1805 int ret = 0;
1806
1807 if (!req->file)
1808 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001809
1810 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1811 return -EINVAL;
1812 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1813 return -EINVAL;
1814
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001815 return ret;
1816}
1817
1818static int io_sync_file_range(struct io_kiocb *req,
1819 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001820 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001821 bool force_nonblock)
1822{
1823 loff_t sqe_off;
1824 loff_t sqe_len;
1825 unsigned flags;
1826 int ret;
1827
1828 ret = io_prep_sfr(req, sqe);
1829 if (ret)
1830 return ret;
1831
1832 /* sync_file_range always requires a blocking context */
1833 if (force_nonblock)
1834 return -EAGAIN;
1835
1836 sqe_off = READ_ONCE(sqe->off);
1837 sqe_len = READ_ONCE(sqe->len);
1838 flags = READ_ONCE(sqe->sync_range_flags);
1839
1840 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1841
Jens Axboe9e645e112019-05-10 16:07:28 -06001842 if (ret < 0 && (req->flags & REQ_F_LINK))
1843 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001844 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001845 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001846 return 0;
1847}
1848
Jens Axboe0fa03c62019-04-19 13:34:07 -06001849#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001850static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001851 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001852 long (*fn)(struct socket *, struct user_msghdr __user *,
1853 unsigned int))
1854{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001855 struct socket *sock;
1856 int ret;
1857
1858 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1859 return -EINVAL;
1860
1861 sock = sock_from_file(req->file, &ret);
1862 if (sock) {
1863 struct user_msghdr __user *msg;
1864 unsigned flags;
1865
1866 flags = READ_ONCE(sqe->msg_flags);
1867 if (flags & MSG_DONTWAIT)
1868 req->flags |= REQ_F_NOWAIT;
1869 else if (force_nonblock)
1870 flags |= MSG_DONTWAIT;
1871
1872 msg = (struct user_msghdr __user *) (unsigned long)
1873 READ_ONCE(sqe->addr);
1874
Jens Axboeaa1fa282019-04-19 13:38:09 -06001875 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001876 if (force_nonblock && ret == -EAGAIN)
1877 return ret;
1878 }
1879
Jens Axboe78e19bb2019-11-06 15:21:34 -07001880 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001881 if (ret < 0 && (req->flags & REQ_F_LINK))
1882 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001883 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001884 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001885}
1886#endif
1887
1888static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001889 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001890{
1891#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001892 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1893 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001894#else
1895 return -EOPNOTSUPP;
1896#endif
1897}
1898
1899static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001900 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001901{
1902#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001903 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1904 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001905#else
1906 return -EOPNOTSUPP;
1907#endif
1908}
1909
Jens Axboe17f2fe32019-10-17 14:42:58 -06001910static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1911 struct io_kiocb **nxt, bool force_nonblock)
1912{
1913#if defined(CONFIG_NET)
1914 struct sockaddr __user *addr;
1915 int __user *addr_len;
1916 unsigned file_flags;
1917 int flags, ret;
1918
1919 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1920 return -EINVAL;
1921 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1922 return -EINVAL;
1923
1924 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1925 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1926 flags = READ_ONCE(sqe->accept_flags);
1927 file_flags = force_nonblock ? O_NONBLOCK : 0;
1928
1929 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1930 if (ret == -EAGAIN && force_nonblock) {
1931 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1932 return -EAGAIN;
1933 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001934 if (ret == -ERESTARTSYS)
1935 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001936 if (ret < 0 && (req->flags & REQ_F_LINK))
1937 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001938 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001939 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001940 return 0;
1941#else
1942 return -EOPNOTSUPP;
1943#endif
1944}
1945
Jens Axboeeac406c2019-11-14 12:09:58 -07001946static inline void io_poll_remove_req(struct io_kiocb *req)
1947{
1948 if (!RB_EMPTY_NODE(&req->rb_node)) {
1949 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
1950 RB_CLEAR_NODE(&req->rb_node);
1951 }
1952}
1953
Jens Axboe221c5eb2019-01-17 09:41:58 -07001954static void io_poll_remove_one(struct io_kiocb *req)
1955{
1956 struct io_poll_iocb *poll = &req->poll;
1957
1958 spin_lock(&poll->head->lock);
1959 WRITE_ONCE(poll->canceled, true);
1960 if (!list_empty(&poll->wait.entry)) {
1961 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07001962 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001963 }
1964 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07001965 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001966}
1967
1968static void io_poll_remove_all(struct io_ring_ctx *ctx)
1969{
Jens Axboeeac406c2019-11-14 12:09:58 -07001970 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001971 struct io_kiocb *req;
1972
1973 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07001974 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
1975 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001976 io_poll_remove_one(req);
1977 }
1978 spin_unlock_irq(&ctx->completion_lock);
1979}
1980
Jens Axboe47f46762019-11-09 17:43:02 -07001981static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
1982{
Jens Axboeeac406c2019-11-14 12:09:58 -07001983 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07001984 struct io_kiocb *req;
1985
Jens Axboeeac406c2019-11-14 12:09:58 -07001986 p = ctx->cancel_tree.rb_node;
1987 while (p) {
1988 parent = p;
1989 req = rb_entry(parent, struct io_kiocb, rb_node);
1990 if (sqe_addr < req->user_data) {
1991 p = p->rb_left;
1992 } else if (sqe_addr > req->user_data) {
1993 p = p->rb_right;
1994 } else {
1995 io_poll_remove_one(req);
1996 return 0;
1997 }
Jens Axboe47f46762019-11-09 17:43:02 -07001998 }
1999
2000 return -ENOENT;
2001}
2002
Jens Axboe221c5eb2019-01-17 09:41:58 -07002003/*
2004 * Find a running poll command that matches one specified in sqe->addr,
2005 * and remove it if found.
2006 */
2007static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2008{
2009 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002010 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002011
2012 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2013 return -EINVAL;
2014 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2015 sqe->poll_events)
2016 return -EINVAL;
2017
2018 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002019 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002020 spin_unlock_irq(&ctx->completion_lock);
2021
Jens Axboe78e19bb2019-11-06 15:21:34 -07002022 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002023 if (ret < 0 && (req->flags & REQ_F_LINK))
2024 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002025 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002026 return 0;
2027}
2028
Jackie Liua197f662019-11-08 08:09:12 -07002029static void io_poll_complete(struct io_kiocb *req, __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002030{
Jackie Liua197f662019-11-08 08:09:12 -07002031 struct io_ring_ctx *ctx = req->ctx;
2032
Jens Axboe8c838782019-03-12 15:48:16 -06002033 req->poll.done = true;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002034 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002035 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002036}
2037
Jens Axboe561fb042019-10-24 07:25:42 -06002038static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002039{
Jens Axboe561fb042019-10-24 07:25:42 -06002040 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002041 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2042 struct io_poll_iocb *poll = &req->poll;
2043 struct poll_table_struct pt = { ._key = poll->events };
2044 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002045 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002046 __poll_t mask = 0;
2047
Jens Axboe561fb042019-10-24 07:25:42 -06002048 if (work->flags & IO_WQ_WORK_CANCEL)
2049 WRITE_ONCE(poll->canceled, true);
2050
Jens Axboe221c5eb2019-01-17 09:41:58 -07002051 if (!READ_ONCE(poll->canceled))
2052 mask = vfs_poll(poll->file, &pt) & poll->events;
2053
2054 /*
2055 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2056 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2057 * synchronize with them. In the cancellation case the list_del_init
2058 * itself is not actually needed, but harmless so we keep it in to
2059 * avoid further branches in the fast path.
2060 */
2061 spin_lock_irq(&ctx->completion_lock);
2062 if (!mask && !READ_ONCE(poll->canceled)) {
2063 add_wait_queue(poll->head, &poll->wait);
2064 spin_unlock_irq(&ctx->completion_lock);
2065 return;
2066 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002067 io_poll_remove_req(req);
Jackie Liua197f662019-11-08 08:09:12 -07002068 io_poll_complete(req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002069 spin_unlock_irq(&ctx->completion_lock);
2070
Jens Axboe8c838782019-03-12 15:48:16 -06002071 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002072
Jackie Liuec9c02a2019-11-08 23:50:36 +08002073 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002074 if (nxt)
2075 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002076}
2077
2078static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2079 void *key)
2080{
2081 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2082 wait);
2083 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2084 struct io_ring_ctx *ctx = req->ctx;
2085 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002086 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002087
2088 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002089 if (mask && !(mask & poll->events))
2090 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002091
2092 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002093
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002094 /*
2095 * Run completion inline if we can. We're using trylock here because
2096 * we are violating the completion_lock -> poll wq lock ordering.
2097 * If we have a link timeout we're going to need the completion_lock
2098 * for finalizing the request, mark us as having grabbed that already.
2099 */
Jens Axboe8c838782019-03-12 15:48:16 -06002100 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002101 io_poll_remove_req(req);
Jackie Liua197f662019-11-08 08:09:12 -07002102 io_poll_complete(req, mask);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002103 req->flags |= REQ_F_COMP_LOCKED;
2104 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002105 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2106
2107 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002108 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002109 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002110 }
2111
Jens Axboe221c5eb2019-01-17 09:41:58 -07002112 return 1;
2113}
2114
2115struct io_poll_table {
2116 struct poll_table_struct pt;
2117 struct io_kiocb *req;
2118 int error;
2119};
2120
2121static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2122 struct poll_table_struct *p)
2123{
2124 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2125
2126 if (unlikely(pt->req->poll.head)) {
2127 pt->error = -EINVAL;
2128 return;
2129 }
2130
2131 pt->error = 0;
2132 pt->req->poll.head = head;
2133 add_wait_queue(head, &pt->req->poll.wait);
2134}
2135
Jens Axboeeac406c2019-11-14 12:09:58 -07002136static void io_poll_req_insert(struct io_kiocb *req)
2137{
2138 struct io_ring_ctx *ctx = req->ctx;
2139 struct rb_node **p = &ctx->cancel_tree.rb_node;
2140 struct rb_node *parent = NULL;
2141 struct io_kiocb *tmp;
2142
2143 while (*p) {
2144 parent = *p;
2145 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2146 if (req->user_data < tmp->user_data)
2147 p = &(*p)->rb_left;
2148 else
2149 p = &(*p)->rb_right;
2150 }
2151 rb_link_node(&req->rb_node, parent, p);
2152 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2153}
2154
Jens Axboe89723d02019-11-05 15:32:58 -07002155static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2156 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002157{
2158 struct io_poll_iocb *poll = &req->poll;
2159 struct io_ring_ctx *ctx = req->ctx;
2160 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002161 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002162 __poll_t mask;
2163 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002164
2165 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2166 return -EINVAL;
2167 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2168 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002169 if (!poll->file)
2170 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002171
Jens Axboe6cc47d12019-09-18 11:18:23 -06002172 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002173 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002174 events = READ_ONCE(sqe->poll_events);
2175 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002176 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002177
Jens Axboe221c5eb2019-01-17 09:41:58 -07002178 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002179 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002180 poll->canceled = false;
2181
2182 ipt.pt._qproc = io_poll_queue_proc;
2183 ipt.pt._key = poll->events;
2184 ipt.req = req;
2185 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2186
2187 /* initialized the list so that we can do list_empty checks */
2188 INIT_LIST_HEAD(&poll->wait.entry);
2189 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2190
Jens Axboe36703242019-07-25 10:20:18 -06002191 INIT_LIST_HEAD(&req->list);
2192
Jens Axboe221c5eb2019-01-17 09:41:58 -07002193 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002194
2195 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002196 if (likely(poll->head)) {
2197 spin_lock(&poll->head->lock);
2198 if (unlikely(list_empty(&poll->wait.entry))) {
2199 if (ipt.error)
2200 cancel = true;
2201 ipt.error = 0;
2202 mask = 0;
2203 }
2204 if (mask || ipt.error)
2205 list_del_init(&poll->wait.entry);
2206 else if (cancel)
2207 WRITE_ONCE(poll->canceled, true);
2208 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002209 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002210 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002211 }
Jens Axboe8c838782019-03-12 15:48:16 -06002212 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002213 ipt.error = 0;
Jackie Liua197f662019-11-08 08:09:12 -07002214 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06002215 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002216 spin_unlock_irq(&ctx->completion_lock);
2217
Jens Axboe8c838782019-03-12 15:48:16 -06002218 if (mask) {
2219 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002220 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002221 }
Jens Axboe8c838782019-03-12 15:48:16 -06002222 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002223}
2224
Jens Axboe5262f562019-09-17 12:26:57 -06002225static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2226{
2227 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002228 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06002229 unsigned long flags;
2230
2231 req = container_of(timer, struct io_kiocb, timeout.timer);
2232 ctx = req->ctx;
2233 atomic_inc(&ctx->cq_timeouts);
2234
2235 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002236 /*
Jens Axboe11365042019-10-16 09:08:32 -06002237 * We could be racing with timeout deletion. If the list is empty,
2238 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002239 */
Jens Axboe842f9612019-10-29 12:34:10 -06002240 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002241 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002242
Jens Axboe11365042019-10-16 09:08:32 -06002243 /*
2244 * Adjust the reqs sequence before the current one because it
2245 * will consume a slot in the cq_ring and the the cq_tail
2246 * pointer will be increased, otherwise other timeout reqs may
2247 * return in advance without waiting for enough wait_nr.
2248 */
2249 prev = req;
2250 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2251 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002252 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002253 }
Jens Axboe842f9612019-10-29 12:34:10 -06002254
Jens Axboe78e19bb2019-11-06 15:21:34 -07002255 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002256 io_commit_cqring(ctx);
2257 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2258
2259 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002260 if (req->flags & REQ_F_LINK)
2261 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002262 io_put_req(req);
2263 return HRTIMER_NORESTART;
2264}
2265
Jens Axboe47f46762019-11-09 17:43:02 -07002266static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2267{
2268 struct io_kiocb *req;
2269 int ret = -ENOENT;
2270
2271 list_for_each_entry(req, &ctx->timeout_list, list) {
2272 if (user_data == req->user_data) {
2273 list_del_init(&req->list);
2274 ret = 0;
2275 break;
2276 }
2277 }
2278
2279 if (ret == -ENOENT)
2280 return ret;
2281
2282 ret = hrtimer_try_to_cancel(&req->timeout.timer);
2283 if (ret == -1)
2284 return -EALREADY;
2285
2286 io_cqring_fill_event(req, -ECANCELED);
2287 io_put_req(req);
2288 return 0;
2289}
2290
Jens Axboe11365042019-10-16 09:08:32 -06002291/*
2292 * Remove or update an existing timeout command
2293 */
2294static int io_timeout_remove(struct io_kiocb *req,
2295 const struct io_uring_sqe *sqe)
2296{
2297 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002298 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002299 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002300
2301 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2302 return -EINVAL;
2303 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2304 return -EINVAL;
2305 flags = READ_ONCE(sqe->timeout_flags);
2306 if (flags)
2307 return -EINVAL;
2308
Jens Axboe11365042019-10-16 09:08:32 -06002309 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002310 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002311
Jens Axboe47f46762019-11-09 17:43:02 -07002312 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002313 io_commit_cqring(ctx);
2314 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002315 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002316 if (ret < 0 && req->flags & REQ_F_LINK)
2317 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002318 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002319 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002320}
2321
2322static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2323{
yangerkun5da0fb12019-10-15 21:59:29 +08002324 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002325 struct io_ring_ctx *ctx = req->ctx;
2326 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002327 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002328 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002329 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002330 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002331
2332 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2333 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002334 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2335 return -EINVAL;
2336 flags = READ_ONCE(sqe->timeout_flags);
2337 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002338 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002339
2340 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002341 return -EFAULT;
2342
Jens Axboe11365042019-10-16 09:08:32 -06002343 if (flags & IORING_TIMEOUT_ABS)
2344 mode = HRTIMER_MODE_ABS;
2345 else
2346 mode = HRTIMER_MODE_REL;
2347
2348 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002349 req->flags |= REQ_F_TIMEOUT;
2350
Jens Axboe5262f562019-09-17 12:26:57 -06002351 /*
2352 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002353 * timeout event to be satisfied. If it isn't set, then this is
2354 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002355 */
2356 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002357 if (!count) {
2358 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2359 spin_lock_irq(&ctx->completion_lock);
2360 entry = ctx->timeout_list.prev;
2361 goto add;
2362 }
Jens Axboe5262f562019-09-17 12:26:57 -06002363
2364 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002365 /* reuse it to store the count */
2366 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002367
2368 /*
2369 * Insertion sort, ensuring the first entry in the list is always
2370 * the one we need first.
2371 */
Jens Axboe5262f562019-09-17 12:26:57 -06002372 spin_lock_irq(&ctx->completion_lock);
2373 list_for_each_prev(entry, &ctx->timeout_list) {
2374 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002375 unsigned nxt_sq_head;
2376 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002377
Jens Axboe93bd25b2019-11-11 23:34:31 -07002378 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2379 continue;
2380
yangerkun5da0fb12019-10-15 21:59:29 +08002381 /*
2382 * Since cached_sq_head + count - 1 can overflow, use type long
2383 * long to store it.
2384 */
2385 tmp = (long long)ctx->cached_sq_head + count - 1;
2386 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2387 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2388
2389 /*
2390 * cached_sq_head may overflow, and it will never overflow twice
2391 * once there is some timeout req still be valid.
2392 */
2393 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002394 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002395
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002396 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002397 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002398
2399 /*
2400 * Sequence of reqs after the insert one and itself should
2401 * be adjusted because each timeout req consumes a slot.
2402 */
2403 span++;
2404 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002405 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002406 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002407add:
Jens Axboe5262f562019-09-17 12:26:57 -06002408 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002409 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002410 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002411 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002412 return 0;
2413}
2414
Jens Axboe62755e32019-10-28 21:49:21 -06002415static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002416{
Jens Axboe62755e32019-10-28 21:49:21 -06002417 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002418
Jens Axboe62755e32019-10-28 21:49:21 -06002419 return req->user_data == (unsigned long) data;
2420}
2421
Jens Axboee977d6d2019-11-05 12:39:45 -07002422static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002423{
Jens Axboe62755e32019-10-28 21:49:21 -06002424 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002425 int ret = 0;
2426
Jens Axboe62755e32019-10-28 21:49:21 -06002427 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2428 switch (cancel_ret) {
2429 case IO_WQ_CANCEL_OK:
2430 ret = 0;
2431 break;
2432 case IO_WQ_CANCEL_RUNNING:
2433 ret = -EALREADY;
2434 break;
2435 case IO_WQ_CANCEL_NOTFOUND:
2436 ret = -ENOENT;
2437 break;
2438 }
2439
Jens Axboee977d6d2019-11-05 12:39:45 -07002440 return ret;
2441}
2442
Jens Axboe47f46762019-11-09 17:43:02 -07002443static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2444 struct io_kiocb *req, __u64 sqe_addr,
2445 struct io_kiocb **nxt)
2446{
2447 unsigned long flags;
2448 int ret;
2449
2450 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2451 if (ret != -ENOENT) {
2452 spin_lock_irqsave(&ctx->completion_lock, flags);
2453 goto done;
2454 }
2455
2456 spin_lock_irqsave(&ctx->completion_lock, flags);
2457 ret = io_timeout_cancel(ctx, sqe_addr);
2458 if (ret != -ENOENT)
2459 goto done;
2460 ret = io_poll_cancel(ctx, sqe_addr);
2461done:
2462 io_cqring_fill_event(req, ret);
2463 io_commit_cqring(ctx);
2464 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2465 io_cqring_ev_posted(ctx);
2466
2467 if (ret < 0 && (req->flags & REQ_F_LINK))
2468 req->flags |= REQ_F_FAIL_LINK;
2469 io_put_req_find_next(req, nxt);
2470}
2471
Jens Axboee977d6d2019-11-05 12:39:45 -07002472static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2473 struct io_kiocb **nxt)
2474{
2475 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002476
2477 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2478 return -EINVAL;
2479 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2480 sqe->cancel_flags)
2481 return -EINVAL;
2482
Jens Axboe95a5bba2019-11-14 22:40:44 -07002483 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06002484 return 0;
2485}
2486
Jackie Liua197f662019-11-08 08:09:12 -07002487static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002488{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002489 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002490 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002491 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002492
Bob Liu9d858b22019-11-13 18:06:25 +08002493 /* Still need defer if there is pending req in defer list. */
2494 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002495 return 0;
2496
2497 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2498 if (!sqe_copy)
2499 return -EAGAIN;
2500
2501 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002502 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002503 spin_unlock_irq(&ctx->completion_lock);
2504 kfree(sqe_copy);
2505 return 0;
2506 }
2507
2508 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2509 req->submit.sqe = sqe_copy;
2510
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002511 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002512 list_add_tail(&req->list, &ctx->defer_list);
2513 spin_unlock_irq(&ctx->completion_lock);
2514 return -EIOCBQUEUED;
2515}
2516
Jackie Liua197f662019-11-08 08:09:12 -07002517static int __io_submit_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2518 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002519{
Jens Axboee0c5c572019-03-12 10:18:47 -06002520 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002521 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002522 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002523
2524 opcode = READ_ONCE(s->sqe->opcode);
2525 switch (opcode) {
2526 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002527 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002528 break;
2529 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002530 if (unlikely(s->sqe->buf_index))
2531 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002532 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002533 break;
2534 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002535 if (unlikely(s->sqe->buf_index))
2536 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002537 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002538 break;
2539 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002540 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002541 break;
2542 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002543 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002544 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002545 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002546 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002547 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002548 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002549 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002550 break;
2551 case IORING_OP_POLL_REMOVE:
2552 ret = io_poll_remove(req, s->sqe);
2553 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002554 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002555 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002556 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002557 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002558 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002559 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002560 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002561 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002562 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002563 case IORING_OP_TIMEOUT:
2564 ret = io_timeout(req, s->sqe);
2565 break;
Jens Axboe11365042019-10-16 09:08:32 -06002566 case IORING_OP_TIMEOUT_REMOVE:
2567 ret = io_timeout_remove(req, s->sqe);
2568 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002569 case IORING_OP_ACCEPT:
2570 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2571 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002572 case IORING_OP_ASYNC_CANCEL:
2573 ret = io_async_cancel(req, s->sqe, nxt);
2574 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002575 default:
2576 ret = -EINVAL;
2577 break;
2578 }
2579
Jens Axboedef596e2019-01-09 08:59:42 -07002580 if (ret)
2581 return ret;
2582
2583 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002584 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002585 return -EAGAIN;
2586
2587 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002588 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002589 mutex_lock(&ctx->uring_lock);
2590 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002591 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002592 mutex_unlock(&ctx->uring_lock);
2593 }
2594
2595 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002596}
2597
Jens Axboe561fb042019-10-24 07:25:42 -06002598static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002599{
Jens Axboe561fb042019-10-24 07:25:42 -06002600 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002601 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002602 struct sqe_submit *s = &req->submit;
2603 const struct io_uring_sqe *sqe = s->sqe;
2604 struct io_kiocb *nxt = NULL;
2605 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002606
Jens Axboe561fb042019-10-24 07:25:42 -06002607 /* Ensure we clear previously set non-block flag */
2608 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002609
Jens Axboe561fb042019-10-24 07:25:42 -06002610 if (work->flags & IO_WQ_WORK_CANCEL)
2611 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002612
Jens Axboe561fb042019-10-24 07:25:42 -06002613 if (!ret) {
2614 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2615 s->in_async = true;
2616 do {
Jackie Liua197f662019-11-08 08:09:12 -07002617 ret = __io_submit_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002618 /*
2619 * We can get EAGAIN for polled IO even though we're
2620 * forcing a sync submission from here, since we can't
2621 * wait for request slots on the block side.
2622 */
2623 if (ret != -EAGAIN)
2624 break;
2625 cond_resched();
2626 } while (1);
2627 }
Jens Axboe31b51512019-01-18 22:56:34 -07002628
Jens Axboe561fb042019-10-24 07:25:42 -06002629 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002630 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002631
Jens Axboe561fb042019-10-24 07:25:42 -06002632 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002633 if (req->flags & REQ_F_LINK)
2634 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002635 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002636 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002637 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002638
Jens Axboe561fb042019-10-24 07:25:42 -06002639 /* async context always use a copy of the sqe */
2640 kfree(sqe);
2641
2642 /* if a dependent link is ready, pass it back */
2643 if (!ret && nxt) {
2644 io_prep_async_work(nxt);
2645 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002646 }
Jens Axboe31b51512019-01-18 22:56:34 -07002647}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002648
Jens Axboe09bb8392019-03-13 12:39:28 -06002649static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2650{
2651 int op = READ_ONCE(sqe->opcode);
2652
2653 switch (op) {
2654 case IORING_OP_NOP:
2655 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002656 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002657 case IORING_OP_TIMEOUT_REMOVE:
2658 case IORING_OP_ASYNC_CANCEL:
2659 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002660 return false;
2661 default:
2662 return true;
2663 }
2664}
2665
Jens Axboe65e19f52019-10-26 07:20:21 -06002666static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2667 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002668{
Jens Axboe65e19f52019-10-26 07:20:21 -06002669 struct fixed_file_table *table;
2670
2671 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2672 return table->files[index & IORING_FILE_TABLE_MASK];
2673}
2674
Jackie Liua197f662019-11-08 08:09:12 -07002675static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002676{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002677 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002678 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002679 unsigned flags;
2680 int fd;
2681
2682 flags = READ_ONCE(s->sqe->flags);
2683 fd = READ_ONCE(s->sqe->fd);
2684
Jackie Liu4fe2c962019-09-09 20:50:40 +08002685 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002686 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002687 /*
2688 * All io need record the previous position, if LINK vs DARIN,
2689 * it can be used to mark the position of the first IO in the
2690 * link list.
2691 */
2692 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002693
Jens Axboe60c112b2019-06-21 10:20:18 -06002694 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002695 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002696
2697 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002698 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002699 (unsigned) fd >= ctx->nr_user_files))
2700 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002701 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002702 req->file = io_file_from_index(ctx, fd);
2703 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002704 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002705 req->flags |= REQ_F_FIXED_FILE;
2706 } else {
2707 if (s->needs_fixed_file)
2708 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002709 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002710 req->file = io_file_get(state, fd);
2711 if (unlikely(!req->file))
2712 return -EBADF;
2713 }
2714
2715 return 0;
2716}
2717
Jackie Liua197f662019-11-08 08:09:12 -07002718static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002719{
Jens Axboefcb323c2019-10-24 12:39:47 -06002720 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002721 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002722
2723 rcu_read_lock();
2724 spin_lock_irq(&ctx->inflight_lock);
2725 /*
2726 * We use the f_ops->flush() handler to ensure that we can flush
2727 * out work accessing these files if the fd is closed. Check if
2728 * the fd has changed since we started down this path, and disallow
2729 * this operation if it has.
2730 */
2731 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2732 list_add(&req->inflight_entry, &ctx->inflight_list);
2733 req->flags |= REQ_F_INFLIGHT;
2734 req->work.files = current->files;
2735 ret = 0;
2736 }
2737 spin_unlock_irq(&ctx->inflight_lock);
2738 rcu_read_unlock();
2739
2740 return ret;
2741}
2742
Jens Axboe2665abf2019-11-05 12:40:47 -07002743static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2744{
2745 struct io_kiocb *req = container_of(timer, struct io_kiocb,
2746 timeout.timer);
2747 struct io_ring_ctx *ctx = req->ctx;
2748 struct io_kiocb *prev = NULL;
2749 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002750
2751 spin_lock_irqsave(&ctx->completion_lock, flags);
2752
2753 /*
2754 * We don't expect the list to be empty, that will only happen if we
2755 * race with the completion of the linked work.
2756 */
2757 if (!list_empty(&req->list)) {
2758 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002759 if (refcount_inc_not_zero(&prev->refs))
2760 list_del_init(&req->list);
2761 else
2762 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002763 }
2764
2765 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2766
2767 if (prev) {
Jens Axboe47f46762019-11-09 17:43:02 -07002768 io_async_find_and_cancel(ctx, req, prev->user_data, NULL);
Jens Axboe76a46e02019-11-10 23:34:16 -07002769 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002770 } else {
2771 io_cqring_add_event(req, -ETIME);
2772 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002773 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002774 return HRTIMER_NORESTART;
2775}
2776
Jens Axboe76a46e02019-11-10 23:34:16 -07002777static void io_queue_linked_timeout(struct io_kiocb *req, struct timespec64 *ts,
2778 enum hrtimer_mode *mode)
Jens Axboe2665abf2019-11-05 12:40:47 -07002779{
Jens Axboe76a46e02019-11-10 23:34:16 -07002780 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002781
Jens Axboe76a46e02019-11-10 23:34:16 -07002782 /*
2783 * If the list is now empty, then our linked request finished before
2784 * we got a chance to setup the timer
2785 */
2786 spin_lock_irq(&ctx->completion_lock);
2787 if (!list_empty(&req->list)) {
2788 req->timeout.timer.function = io_link_timeout_fn;
2789 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(*ts),
2790 *mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002791 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002792 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002793
Jens Axboe2665abf2019-11-05 12:40:47 -07002794 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002795 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002796}
2797
Jens Axboe76a46e02019-11-10 23:34:16 -07002798static int io_validate_link_timeout(const struct io_uring_sqe *sqe,
2799 struct timespec64 *ts)
2800{
2801 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || sqe->off)
2802 return -EINVAL;
2803 if (sqe->timeout_flags & ~IORING_TIMEOUT_ABS)
2804 return -EINVAL;
2805 if (get_timespec64(ts, u64_to_user_ptr(sqe->addr)))
2806 return -EFAULT;
2807
2808 return 0;
2809}
2810
2811static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req,
2812 struct timespec64 *ts,
2813 enum hrtimer_mode *mode)
Jens Axboe2665abf2019-11-05 12:40:47 -07002814{
2815 struct io_kiocb *nxt;
Jens Axboee0c5c572019-03-12 10:18:47 -06002816 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002817
Jens Axboe2665abf2019-11-05 12:40:47 -07002818 if (!(req->flags & REQ_F_LINK))
2819 return NULL;
2820
2821 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002822 if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT)
2823 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002824
Jens Axboe76a46e02019-11-10 23:34:16 -07002825 ret = io_validate_link_timeout(nxt->submit.sqe, ts);
2826 if (ret) {
2827 list_del_init(&nxt->list);
2828 io_cqring_add_event(nxt, ret);
2829 io_double_put_req(nxt);
2830 return ERR_PTR(-ECANCELED);
2831 }
2832
2833 if (nxt->submit.sqe->timeout_flags & IORING_TIMEOUT_ABS)
2834 *mode = HRTIMER_MODE_ABS;
2835 else
2836 *mode = HRTIMER_MODE_REL;
2837
2838 req->flags |= REQ_F_LINK_TIMEOUT;
2839 hrtimer_init(&nxt->timeout.timer, CLOCK_MONOTONIC, *mode);
2840 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002841}
2842
Jens Axboe0e0702d2019-11-14 21:42:10 -07002843static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002844{
Jens Axboe76a46e02019-11-10 23:34:16 -07002845 enum hrtimer_mode mode;
Jens Axboe2665abf2019-11-05 12:40:47 -07002846 struct io_kiocb *nxt;
Jens Axboe76a46e02019-11-10 23:34:16 -07002847 struct timespec64 ts;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002848 int ret;
2849
Jens Axboe76a46e02019-11-10 23:34:16 -07002850 nxt = io_prep_linked_timeout(req, &ts, &mode);
2851 if (IS_ERR(nxt)) {
2852 ret = PTR_ERR(nxt);
2853 nxt = NULL;
2854 goto err;
Jens Axboe2665abf2019-11-05 12:40:47 -07002855 }
2856
Jackie Liua197f662019-11-08 08:09:12 -07002857 ret = __io_submit_sqe(req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002858
2859 /*
2860 * We async punt it if the file wasn't marked NOWAIT, or if the file
2861 * doesn't support non-blocking read/write attempts
2862 */
2863 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2864 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002865 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002866 struct io_uring_sqe *sqe_copy;
2867
Jackie Liu954dab12019-09-18 10:37:52 +08002868 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002869 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002870 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002871 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
Jackie Liua197f662019-11-08 08:09:12 -07002872 ret = io_grab_files(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06002873 if (ret) {
2874 kfree(sqe_copy);
2875 goto err;
2876 }
Jens Axboe31b51512019-01-18 22:56:34 -07002877 }
Jens Axboee65ef562019-03-12 10:16:44 -06002878
2879 /*
2880 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002881 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002882 */
Jackie Liua197f662019-11-08 08:09:12 -07002883 io_queue_async_work(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07002884
2885 if (nxt)
2886 io_queue_linked_timeout(nxt, &ts, &mode);
2887
Jens Axboe0e0702d2019-11-14 21:42:10 -07002888 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002889 }
2890 }
Jens Axboee65ef562019-03-12 10:16:44 -06002891
Jens Axboefcb323c2019-10-24 12:39:47 -06002892err:
Jens Axboee65ef562019-03-12 10:16:44 -06002893 /* drop submission reference */
2894 io_put_req(req);
2895
Jens Axboe76a46e02019-11-10 23:34:16 -07002896 if (nxt) {
2897 if (!ret)
2898 io_queue_linked_timeout(nxt, &ts, &mode);
2899 else
2900 io_put_req(nxt);
2901 }
2902
Jens Axboee65ef562019-03-12 10:16:44 -06002903 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002904 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002905 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002906 if (req->flags & REQ_F_LINK)
2907 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002908 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002909 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002910}
2911
Jens Axboe0e0702d2019-11-14 21:42:10 -07002912static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002913{
2914 int ret;
2915
Jackie Liua197f662019-11-08 08:09:12 -07002916 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002917 if (ret) {
2918 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002919 io_cqring_add_event(req, ret);
2920 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002921 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07002922 } else
2923 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002924}
2925
Jens Axboe0e0702d2019-11-14 21:42:10 -07002926static void io_queue_link_head(struct io_kiocb *req, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002927{
2928 int ret;
2929 int need_submit = false;
Jackie Liua197f662019-11-08 08:09:12 -07002930 struct io_ring_ctx *ctx = req->ctx;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002931
Jens Axboe0e0702d2019-11-14 21:42:10 -07002932 if (!shadow) {
2933 io_queue_sqe(req);
2934 return;
2935 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08002936
2937 /*
2938 * Mark the first IO in link list as DRAIN, let all the following
2939 * IOs enter the defer list. all IO needs to be completed before link
2940 * list.
2941 */
2942 req->flags |= REQ_F_IO_DRAIN;
Jackie Liua197f662019-11-08 08:09:12 -07002943 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002944 if (ret) {
2945 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002946 io_cqring_add_event(req, ret);
2947 io_double_put_req(req);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002948 __io_free_req(shadow);
Jens Axboe0e0702d2019-11-14 21:42:10 -07002949 return;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002950 }
2951 } else {
2952 /*
2953 * If ret == 0 means that all IOs in front of link io are
2954 * running done. let's queue link head.
2955 */
2956 need_submit = true;
2957 }
2958
2959 /* Insert shadow req to defer_list, blocking next IOs */
2960 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002961 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002962 list_add_tail(&shadow->list, &ctx->defer_list);
2963 spin_unlock_irq(&ctx->completion_lock);
2964
2965 if (need_submit)
Jens Axboe0e0702d2019-11-14 21:42:10 -07002966 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002967}
2968
Jens Axboe9e645e112019-05-10 16:07:28 -06002969#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2970
Jackie Liua197f662019-11-08 08:09:12 -07002971static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
2972 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002973{
2974 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002975 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002976 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06002977 int ret;
2978
Jens Axboe78e19bb2019-11-06 15:21:34 -07002979 req->user_data = s->sqe->user_data;
2980
Jens Axboe9e645e112019-05-10 16:07:28 -06002981 /* enforce forwards compatibility on users */
2982 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2983 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03002984 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002985 }
2986
Jackie Liua197f662019-11-08 08:09:12 -07002987 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002988 if (unlikely(ret)) {
2989err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002990 io_cqring_add_event(req, ret);
2991 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002992 return;
2993 }
2994
Jens Axboe9e645e112019-05-10 16:07:28 -06002995 /*
2996 * If we already have a head request, queue this one for async
2997 * submittal once the head completes. If we don't have a head but
2998 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2999 * submitted sync once the chain is complete. If none of those
3000 * conditions are true (normal request), then just queue it.
3001 */
3002 if (*link) {
3003 struct io_kiocb *prev = *link;
3004
3005 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
3006 if (!sqe_copy) {
3007 ret = -EAGAIN;
3008 goto err_req;
3009 }
3010
3011 s->sqe = sqe_copy;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003012 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003013 list_add_tail(&req->list, &prev->link_list);
3014 } else if (s->sqe->flags & IOSQE_IO_LINK) {
3015 req->flags |= REQ_F_LINK;
3016
Jens Axboe9e645e112019-05-10 16:07:28 -06003017 INIT_LIST_HEAD(&req->link_list);
3018 *link = req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003019 } else if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
3020 /* Only valid as a linked SQE */
3021 ret = -EINVAL;
3022 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003023 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003024 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003025 }
3026}
3027
Jens Axboe9a56a232019-01-09 09:06:50 -07003028/*
3029 * Batched submission is done, ensure local IO is flushed out.
3030 */
3031static void io_submit_state_end(struct io_submit_state *state)
3032{
3033 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003034 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003035 if (state->free_reqs)
3036 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3037 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003038}
3039
3040/*
3041 * Start submission side cache.
3042 */
3043static void io_submit_state_start(struct io_submit_state *state,
3044 struct io_ring_ctx *ctx, unsigned max_ios)
3045{
3046 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003047 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003048 state->file = NULL;
3049 state->ios_left = max_ios;
3050}
3051
Jens Axboe2b188cc2019-01-07 10:46:33 -07003052static void io_commit_sqring(struct io_ring_ctx *ctx)
3053{
Hristo Venev75b28af2019-08-26 17:23:46 +00003054 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003055
Hristo Venev75b28af2019-08-26 17:23:46 +00003056 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003057 /*
3058 * Ensure any loads from the SQEs are done at this point,
3059 * since once we write the new head, the application could
3060 * write new data to them.
3061 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003062 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003063 }
3064}
3065
3066/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003067 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3068 * that is mapped by userspace. This means that care needs to be taken to
3069 * ensure that reads are stable, as we cannot rely on userspace always
3070 * being a good citizen. If members of the sqe are validated and then later
3071 * used, it's important that those reads are done through READ_ONCE() to
3072 * prevent a re-load down the line.
3073 */
3074static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
3075{
Hristo Venev75b28af2019-08-26 17:23:46 +00003076 struct io_rings *rings = ctx->rings;
3077 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003078 unsigned head;
3079
3080 /*
3081 * The cached sq head (or cq tail) serves two purposes:
3082 *
3083 * 1) allows us to batch the cost of updating the user visible
3084 * head updates.
3085 * 2) allows the kernel side to track the head on its own, even
3086 * though the application is the one updating it.
3087 */
3088 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003089 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00003090 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003091 return false;
3092
Hristo Venev75b28af2019-08-26 17:23:46 +00003093 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003094 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003095 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003096 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003097 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003098 ctx->cached_sq_head++;
3099 return true;
3100 }
3101
3102 /* drop invalid entries */
3103 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003104 ctx->cached_sq_dropped++;
3105 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003106 return false;
3107}
3108
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003109static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003110 struct file *ring_file, int ring_fd,
3111 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003112{
3113 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003114 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003115 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003116 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003117 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003118
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003119 if (!list_empty(&ctx->cq_overflow_list)) {
3120 io_cqring_overflow_flush(ctx, false);
3121 return -EBUSY;
3122 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003123
3124 if (nr > IO_PLUG_THRESHOLD) {
3125 io_submit_state_start(&state, ctx, nr);
3126 statep = &state;
3127 }
3128
3129 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003130 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003131 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003132
Pavel Begunkov196be952019-11-07 01:41:06 +03003133 req = io_get_req(ctx, statep);
3134 if (unlikely(!req)) {
3135 if (!submitted)
3136 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003137 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003138 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003139 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003140 __io_free_req(req);
3141 break;
3142 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003143
Pavel Begunkov50585b92019-11-07 01:41:07 +03003144 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003145 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3146 if (!mm_fault) {
3147 use_mm(ctx->sqo_mm);
3148 *mm = ctx->sqo_mm;
3149 }
3150 }
3151
Pavel Begunkov50585b92019-11-07 01:41:07 +03003152 sqe_flags = req->submit.sqe->flags;
3153
3154 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08003155 if (!shadow_req) {
3156 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08003157 if (unlikely(!shadow_req))
3158 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003159 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
3160 refcount_dec(&shadow_req->refs);
3161 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003162 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003163 }
3164
Jackie Liua1041c22019-09-18 17:25:52 +08003165out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03003166 req->submit.ring_file = ring_file;
3167 req->submit.ring_fd = ring_fd;
3168 req->submit.has_user = *mm != NULL;
3169 req->submit.in_async = async;
3170 req->submit.needs_fixed_file = async;
3171 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3172 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003173 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003174 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003175
3176 /*
3177 * If previous wasn't linked and we have a linked command,
3178 * that's the end of the chain. Submit the previous link.
3179 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003180 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Jackie Liua197f662019-11-08 08:09:12 -07003181 io_queue_link_head(link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003182 link = NULL;
3183 shadow_req = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003184 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003185 }
3186
Jens Axboe9e645e112019-05-10 16:07:28 -06003187 if (link)
Jackie Liua197f662019-11-08 08:09:12 -07003188 io_queue_link_head(link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003189 if (statep)
3190 io_submit_state_end(&state);
3191
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003192 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3193 io_commit_sqring(ctx);
3194
Jens Axboe6c271ce2019-01-10 11:22:30 -07003195 return submitted;
3196}
3197
3198static int io_sq_thread(void *data)
3199{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003200 struct io_ring_ctx *ctx = data;
3201 struct mm_struct *cur_mm = NULL;
3202 mm_segment_t old_fs;
3203 DEFINE_WAIT(wait);
3204 unsigned inflight;
3205 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003206 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003207
Jens Axboe206aefd2019-11-07 18:27:42 -07003208 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003209
Jens Axboe6c271ce2019-01-10 11:22:30 -07003210 old_fs = get_fs();
3211 set_fs(USER_DS);
3212
Jens Axboec1edbf52019-11-10 16:56:04 -07003213 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003214 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003215 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003216
3217 if (inflight) {
3218 unsigned nr_events = 0;
3219
3220 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003221 /*
3222 * inflight is the count of the maximum possible
3223 * entries we submitted, but it can be smaller
3224 * if we dropped some of them. If we don't have
3225 * poll entries available, then we know that we
3226 * have nothing left to poll for. Reset the
3227 * inflight count to zero in that case.
3228 */
3229 mutex_lock(&ctx->uring_lock);
3230 if (!list_empty(&ctx->poll_list))
3231 __io_iopoll_check(ctx, &nr_events, 0);
3232 else
3233 inflight = 0;
3234 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003235 } else {
3236 /*
3237 * Normal IO, just pretend everything completed.
3238 * We don't have to poll completions for that.
3239 */
3240 nr_events = inflight;
3241 }
3242
3243 inflight -= nr_events;
3244 if (!inflight)
3245 timeout = jiffies + ctx->sq_thread_idle;
3246 }
3247
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003248 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003249
3250 /*
3251 * If submit got -EBUSY, flag us as needing the application
3252 * to enter the kernel to reap and flush events.
3253 */
3254 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003255 /*
3256 * We're polling. If we're within the defined idle
3257 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003258 * to sleep. The exception is if we got EBUSY doing
3259 * more IO, we should wait for the application to
3260 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003261 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003262 if (inflight ||
3263 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003264 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003265 continue;
3266 }
3267
3268 /*
3269 * Drop cur_mm before scheduling, we can't hold it for
3270 * long periods (or over schedule()). Do this before
3271 * adding ourselves to the waitqueue, as the unuse/drop
3272 * may sleep.
3273 */
3274 if (cur_mm) {
3275 unuse_mm(cur_mm);
3276 mmput(cur_mm);
3277 cur_mm = NULL;
3278 }
3279
3280 prepare_to_wait(&ctx->sqo_wait, &wait,
3281 TASK_INTERRUPTIBLE);
3282
3283 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003284 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003285 /* make sure to read SQ tail after writing flags */
3286 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003287
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003288 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003289 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003290 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003291 finish_wait(&ctx->sqo_wait, &wait);
3292 break;
3293 }
3294 if (signal_pending(current))
3295 flush_signals(current);
3296 schedule();
3297 finish_wait(&ctx->sqo_wait, &wait);
3298
Hristo Venev75b28af2019-08-26 17:23:46 +00003299 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003300 continue;
3301 }
3302 finish_wait(&ctx->sqo_wait, &wait);
3303
Hristo Venev75b28af2019-08-26 17:23:46 +00003304 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003305 }
3306
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003307 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003308 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3309 if (ret > 0)
3310 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003311 }
3312
3313 set_fs(old_fs);
3314 if (cur_mm) {
3315 unuse_mm(cur_mm);
3316 mmput(cur_mm);
3317 }
Jens Axboe06058632019-04-13 09:26:03 -06003318
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003319 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003320
Jens Axboe6c271ce2019-01-10 11:22:30 -07003321 return 0;
3322}
3323
Jens Axboebda52162019-09-24 13:47:15 -06003324struct io_wait_queue {
3325 struct wait_queue_entry wq;
3326 struct io_ring_ctx *ctx;
3327 unsigned to_wait;
3328 unsigned nr_timeouts;
3329};
3330
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003331static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003332{
3333 struct io_ring_ctx *ctx = iowq->ctx;
3334
3335 /*
3336 * Wake up if we have enough events, or if a timeout occured since we
3337 * started waiting. For timeouts, we always want to return to userspace,
3338 * regardless of event count.
3339 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003340 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003341 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3342}
3343
3344static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3345 int wake_flags, void *key)
3346{
3347 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3348 wq);
3349
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003350 /* use noflush == true, as we can't safely rely on locking context */
3351 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003352 return -1;
3353
3354 return autoremove_wake_function(curr, mode, wake_flags, key);
3355}
3356
Jens Axboe2b188cc2019-01-07 10:46:33 -07003357/*
3358 * Wait until events become available, if we don't already have some. The
3359 * application must reap them itself, as they reside on the shared cq ring.
3360 */
3361static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3362 const sigset_t __user *sig, size_t sigsz)
3363{
Jens Axboebda52162019-09-24 13:47:15 -06003364 struct io_wait_queue iowq = {
3365 .wq = {
3366 .private = current,
3367 .func = io_wake_function,
3368 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3369 },
3370 .ctx = ctx,
3371 .to_wait = min_events,
3372 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003373 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003374 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003375
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003376 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003377 return 0;
3378
3379 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003380#ifdef CONFIG_COMPAT
3381 if (in_compat_syscall())
3382 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003383 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003384 else
3385#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003386 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003387
Jens Axboe2b188cc2019-01-07 10:46:33 -07003388 if (ret)
3389 return ret;
3390 }
3391
Jens Axboebda52162019-09-24 13:47:15 -06003392 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003393 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003394 do {
3395 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3396 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003397 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003398 break;
3399 schedule();
3400 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003401 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003402 break;
3403 }
3404 } while (1);
3405 finish_wait(&ctx->wait, &iowq.wq);
3406
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003407 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003408
Hristo Venev75b28af2019-08-26 17:23:46 +00003409 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003410}
3411
Jens Axboe6b063142019-01-10 22:13:58 -07003412static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3413{
3414#if defined(CONFIG_UNIX)
3415 if (ctx->ring_sock) {
3416 struct sock *sock = ctx->ring_sock->sk;
3417 struct sk_buff *skb;
3418
3419 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3420 kfree_skb(skb);
3421 }
3422#else
3423 int i;
3424
Jens Axboe65e19f52019-10-26 07:20:21 -06003425 for (i = 0; i < ctx->nr_user_files; i++) {
3426 struct file *file;
3427
3428 file = io_file_from_index(ctx, i);
3429 if (file)
3430 fput(file);
3431 }
Jens Axboe6b063142019-01-10 22:13:58 -07003432#endif
3433}
3434
3435static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3436{
Jens Axboe65e19f52019-10-26 07:20:21 -06003437 unsigned nr_tables, i;
3438
3439 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003440 return -ENXIO;
3441
3442 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003443 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3444 for (i = 0; i < nr_tables; i++)
3445 kfree(ctx->file_table[i].files);
3446 kfree(ctx->file_table);
3447 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003448 ctx->nr_user_files = 0;
3449 return 0;
3450}
3451
Jens Axboe6c271ce2019-01-10 11:22:30 -07003452static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3453{
3454 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003455 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003456 /*
3457 * The park is a bit of a work-around, without it we get
3458 * warning spews on shutdown with SQPOLL set and affinity
3459 * set to a single CPU.
3460 */
Jens Axboe06058632019-04-13 09:26:03 -06003461 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003462 kthread_stop(ctx->sqo_thread);
3463 ctx->sqo_thread = NULL;
3464 }
3465}
3466
Jens Axboe6b063142019-01-10 22:13:58 -07003467static void io_finish_async(struct io_ring_ctx *ctx)
3468{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003469 io_sq_thread_stop(ctx);
3470
Jens Axboe561fb042019-10-24 07:25:42 -06003471 if (ctx->io_wq) {
3472 io_wq_destroy(ctx->io_wq);
3473 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003474 }
3475}
3476
3477#if defined(CONFIG_UNIX)
3478static void io_destruct_skb(struct sk_buff *skb)
3479{
3480 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3481
Jens Axboe561fb042019-10-24 07:25:42 -06003482 if (ctx->io_wq)
3483 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003484
Jens Axboe6b063142019-01-10 22:13:58 -07003485 unix_destruct_scm(skb);
3486}
3487
3488/*
3489 * Ensure the UNIX gc is aware of our file set, so we are certain that
3490 * the io_uring can be safely unregistered on process exit, even if we have
3491 * loops in the file referencing.
3492 */
3493static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3494{
3495 struct sock *sk = ctx->ring_sock->sk;
3496 struct scm_fp_list *fpl;
3497 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003498 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003499
3500 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3501 unsigned long inflight = ctx->user->unix_inflight + nr;
3502
3503 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3504 return -EMFILE;
3505 }
3506
3507 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3508 if (!fpl)
3509 return -ENOMEM;
3510
3511 skb = alloc_skb(0, GFP_KERNEL);
3512 if (!skb) {
3513 kfree(fpl);
3514 return -ENOMEM;
3515 }
3516
3517 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003518
Jens Axboe08a45172019-10-03 08:11:03 -06003519 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003520 fpl->user = get_uid(ctx->user);
3521 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003522 struct file *file = io_file_from_index(ctx, i + offset);
3523
3524 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003525 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003526 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003527 unix_inflight(fpl->user, fpl->fp[nr_files]);
3528 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003529 }
3530
Jens Axboe08a45172019-10-03 08:11:03 -06003531 if (nr_files) {
3532 fpl->max = SCM_MAX_FD;
3533 fpl->count = nr_files;
3534 UNIXCB(skb).fp = fpl;
3535 skb->destructor = io_destruct_skb;
3536 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3537 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003538
Jens Axboe08a45172019-10-03 08:11:03 -06003539 for (i = 0; i < nr_files; i++)
3540 fput(fpl->fp[i]);
3541 } else {
3542 kfree_skb(skb);
3543 kfree(fpl);
3544 }
Jens Axboe6b063142019-01-10 22:13:58 -07003545
3546 return 0;
3547}
3548
3549/*
3550 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3551 * causes regular reference counting to break down. We rely on the UNIX
3552 * garbage collection to take care of this problem for us.
3553 */
3554static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3555{
3556 unsigned left, total;
3557 int ret = 0;
3558
3559 total = 0;
3560 left = ctx->nr_user_files;
3561 while (left) {
3562 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003563
3564 ret = __io_sqe_files_scm(ctx, this_files, total);
3565 if (ret)
3566 break;
3567 left -= this_files;
3568 total += this_files;
3569 }
3570
3571 if (!ret)
3572 return 0;
3573
3574 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003575 struct file *file = io_file_from_index(ctx, total);
3576
3577 if (file)
3578 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003579 total++;
3580 }
3581
3582 return ret;
3583}
3584#else
3585static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3586{
3587 return 0;
3588}
3589#endif
3590
Jens Axboe65e19f52019-10-26 07:20:21 -06003591static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3592 unsigned nr_files)
3593{
3594 int i;
3595
3596 for (i = 0; i < nr_tables; i++) {
3597 struct fixed_file_table *table = &ctx->file_table[i];
3598 unsigned this_files;
3599
3600 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3601 table->files = kcalloc(this_files, sizeof(struct file *),
3602 GFP_KERNEL);
3603 if (!table->files)
3604 break;
3605 nr_files -= this_files;
3606 }
3607
3608 if (i == nr_tables)
3609 return 0;
3610
3611 for (i = 0; i < nr_tables; i++) {
3612 struct fixed_file_table *table = &ctx->file_table[i];
3613 kfree(table->files);
3614 }
3615 return 1;
3616}
3617
Jens Axboe6b063142019-01-10 22:13:58 -07003618static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3619 unsigned nr_args)
3620{
3621 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003622 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003623 int fd, ret = 0;
3624 unsigned i;
3625
Jens Axboe65e19f52019-10-26 07:20:21 -06003626 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003627 return -EBUSY;
3628 if (!nr_args)
3629 return -EINVAL;
3630 if (nr_args > IORING_MAX_FIXED_FILES)
3631 return -EMFILE;
3632
Jens Axboe65e19f52019-10-26 07:20:21 -06003633 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3634 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3635 GFP_KERNEL);
3636 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003637 return -ENOMEM;
3638
Jens Axboe65e19f52019-10-26 07:20:21 -06003639 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3640 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003641 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003642 return -ENOMEM;
3643 }
3644
Jens Axboe08a45172019-10-03 08:11:03 -06003645 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003646 struct fixed_file_table *table;
3647 unsigned index;
3648
Jens Axboe6b063142019-01-10 22:13:58 -07003649 ret = -EFAULT;
3650 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3651 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003652 /* allow sparse sets */
3653 if (fd == -1) {
3654 ret = 0;
3655 continue;
3656 }
Jens Axboe6b063142019-01-10 22:13:58 -07003657
Jens Axboe65e19f52019-10-26 07:20:21 -06003658 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3659 index = i & IORING_FILE_TABLE_MASK;
3660 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003661
3662 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003663 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003664 break;
3665 /*
3666 * Don't allow io_uring instances to be registered. If UNIX
3667 * isn't enabled, then this causes a reference cycle and this
3668 * instance can never get freed. If UNIX is enabled we'll
3669 * handle it just fine, but there's still no point in allowing
3670 * a ring fd as it doesn't support regular read/write anyway.
3671 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003672 if (table->files[index]->f_op == &io_uring_fops) {
3673 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003674 break;
3675 }
Jens Axboe6b063142019-01-10 22:13:58 -07003676 ret = 0;
3677 }
3678
3679 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003680 for (i = 0; i < ctx->nr_user_files; i++) {
3681 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003682
Jens Axboe65e19f52019-10-26 07:20:21 -06003683 file = io_file_from_index(ctx, i);
3684 if (file)
3685 fput(file);
3686 }
3687 for (i = 0; i < nr_tables; i++)
3688 kfree(ctx->file_table[i].files);
3689
3690 kfree(ctx->file_table);
3691 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003692 ctx->nr_user_files = 0;
3693 return ret;
3694 }
3695
3696 ret = io_sqe_files_scm(ctx);
3697 if (ret)
3698 io_sqe_files_unregister(ctx);
3699
3700 return ret;
3701}
3702
Jens Axboec3a31e62019-10-03 13:59:56 -06003703static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3704{
3705#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003706 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003707 struct sock *sock = ctx->ring_sock->sk;
3708 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3709 struct sk_buff *skb;
3710 int i;
3711
3712 __skb_queue_head_init(&list);
3713
3714 /*
3715 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3716 * remove this entry and rearrange the file array.
3717 */
3718 skb = skb_dequeue(head);
3719 while (skb) {
3720 struct scm_fp_list *fp;
3721
3722 fp = UNIXCB(skb).fp;
3723 for (i = 0; i < fp->count; i++) {
3724 int left;
3725
3726 if (fp->fp[i] != file)
3727 continue;
3728
3729 unix_notinflight(fp->user, fp->fp[i]);
3730 left = fp->count - 1 - i;
3731 if (left) {
3732 memmove(&fp->fp[i], &fp->fp[i + 1],
3733 left * sizeof(struct file *));
3734 }
3735 fp->count--;
3736 if (!fp->count) {
3737 kfree_skb(skb);
3738 skb = NULL;
3739 } else {
3740 __skb_queue_tail(&list, skb);
3741 }
3742 fput(file);
3743 file = NULL;
3744 break;
3745 }
3746
3747 if (!file)
3748 break;
3749
3750 __skb_queue_tail(&list, skb);
3751
3752 skb = skb_dequeue(head);
3753 }
3754
3755 if (skb_peek(&list)) {
3756 spin_lock_irq(&head->lock);
3757 while ((skb = __skb_dequeue(&list)) != NULL)
3758 __skb_queue_tail(head, skb);
3759 spin_unlock_irq(&head->lock);
3760 }
3761#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003762 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003763#endif
3764}
3765
3766static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3767 int index)
3768{
3769#if defined(CONFIG_UNIX)
3770 struct sock *sock = ctx->ring_sock->sk;
3771 struct sk_buff_head *head = &sock->sk_receive_queue;
3772 struct sk_buff *skb;
3773
3774 /*
3775 * See if we can merge this file into an existing skb SCM_RIGHTS
3776 * file set. If there's no room, fall back to allocating a new skb
3777 * and filling it in.
3778 */
3779 spin_lock_irq(&head->lock);
3780 skb = skb_peek(head);
3781 if (skb) {
3782 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3783
3784 if (fpl->count < SCM_MAX_FD) {
3785 __skb_unlink(skb, head);
3786 spin_unlock_irq(&head->lock);
3787 fpl->fp[fpl->count] = get_file(file);
3788 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3789 fpl->count++;
3790 spin_lock_irq(&head->lock);
3791 __skb_queue_head(head, skb);
3792 } else {
3793 skb = NULL;
3794 }
3795 }
3796 spin_unlock_irq(&head->lock);
3797
3798 if (skb) {
3799 fput(file);
3800 return 0;
3801 }
3802
3803 return __io_sqe_files_scm(ctx, 1, index);
3804#else
3805 return 0;
3806#endif
3807}
3808
3809static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3810 unsigned nr_args)
3811{
3812 struct io_uring_files_update up;
3813 __s32 __user *fds;
3814 int fd, i, err;
3815 __u32 done;
3816
Jens Axboe65e19f52019-10-26 07:20:21 -06003817 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003818 return -ENXIO;
3819 if (!nr_args)
3820 return -EINVAL;
3821 if (copy_from_user(&up, arg, sizeof(up)))
3822 return -EFAULT;
3823 if (check_add_overflow(up.offset, nr_args, &done))
3824 return -EOVERFLOW;
3825 if (done > ctx->nr_user_files)
3826 return -EINVAL;
3827
3828 done = 0;
3829 fds = (__s32 __user *) up.fds;
3830 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003831 struct fixed_file_table *table;
3832 unsigned index;
3833
Jens Axboec3a31e62019-10-03 13:59:56 -06003834 err = 0;
3835 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3836 err = -EFAULT;
3837 break;
3838 }
3839 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003840 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3841 index = i & IORING_FILE_TABLE_MASK;
3842 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003843 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003844 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003845 }
3846 if (fd != -1) {
3847 struct file *file;
3848
3849 file = fget(fd);
3850 if (!file) {
3851 err = -EBADF;
3852 break;
3853 }
3854 /*
3855 * Don't allow io_uring instances to be registered. If
3856 * UNIX isn't enabled, then this causes a reference
3857 * cycle and this instance can never get freed. If UNIX
3858 * is enabled we'll handle it just fine, but there's
3859 * still no point in allowing a ring fd as it doesn't
3860 * support regular read/write anyway.
3861 */
3862 if (file->f_op == &io_uring_fops) {
3863 fput(file);
3864 err = -EBADF;
3865 break;
3866 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003867 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003868 err = io_sqe_file_register(ctx, file, i);
3869 if (err)
3870 break;
3871 }
3872 nr_args--;
3873 done++;
3874 up.offset++;
3875 }
3876
3877 return done ? done : err;
3878}
3879
Jens Axboe7d723062019-11-12 22:31:31 -07003880static void io_put_work(struct io_wq_work *work)
3881{
3882 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3883
3884 io_put_req(req);
3885}
3886
3887static void io_get_work(struct io_wq_work *work)
3888{
3889 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3890
3891 refcount_inc(&req->refs);
3892}
3893
Jens Axboe6c271ce2019-01-10 11:22:30 -07003894static int io_sq_offload_start(struct io_ring_ctx *ctx,
3895 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003896{
Jens Axboe561fb042019-10-24 07:25:42 -06003897 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003898 int ret;
3899
Jens Axboe6c271ce2019-01-10 11:22:30 -07003900 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003901 mmgrab(current->mm);
3902 ctx->sqo_mm = current->mm;
3903
Jens Axboe6c271ce2019-01-10 11:22:30 -07003904 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003905 ret = -EPERM;
3906 if (!capable(CAP_SYS_ADMIN))
3907 goto err;
3908
Jens Axboe917257d2019-04-13 09:28:55 -06003909 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3910 if (!ctx->sq_thread_idle)
3911 ctx->sq_thread_idle = HZ;
3912
Jens Axboe6c271ce2019-01-10 11:22:30 -07003913 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003914 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003915
Jens Axboe917257d2019-04-13 09:28:55 -06003916 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003917 if (cpu >= nr_cpu_ids)
3918 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003919 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003920 goto err;
3921
Jens Axboe6c271ce2019-01-10 11:22:30 -07003922 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3923 ctx, cpu,
3924 "io_uring-sq");
3925 } else {
3926 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3927 "io_uring-sq");
3928 }
3929 if (IS_ERR(ctx->sqo_thread)) {
3930 ret = PTR_ERR(ctx->sqo_thread);
3931 ctx->sqo_thread = NULL;
3932 goto err;
3933 }
3934 wake_up_process(ctx->sqo_thread);
3935 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3936 /* Can't have SQ_AFF without SQPOLL */
3937 ret = -EINVAL;
3938 goto err;
3939 }
3940
Jens Axboe561fb042019-10-24 07:25:42 -06003941 /* Do QD, or 4 * CPUS, whatever is smallest */
3942 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe7d723062019-11-12 22:31:31 -07003943 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user,
3944 io_get_work, io_put_work);
Jens Axboe975c99a52019-10-30 08:42:56 -06003945 if (IS_ERR(ctx->io_wq)) {
3946 ret = PTR_ERR(ctx->io_wq);
3947 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003948 goto err;
3949 }
3950
3951 return 0;
3952err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003953 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003954 mmdrop(ctx->sqo_mm);
3955 ctx->sqo_mm = NULL;
3956 return ret;
3957}
3958
3959static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3960{
3961 atomic_long_sub(nr_pages, &user->locked_vm);
3962}
3963
3964static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3965{
3966 unsigned long page_limit, cur_pages, new_pages;
3967
3968 /* Don't allow more pages than we can safely lock */
3969 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3970
3971 do {
3972 cur_pages = atomic_long_read(&user->locked_vm);
3973 new_pages = cur_pages + nr_pages;
3974 if (new_pages > page_limit)
3975 return -ENOMEM;
3976 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3977 new_pages) != cur_pages);
3978
3979 return 0;
3980}
3981
3982static void io_mem_free(void *ptr)
3983{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003984 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003985
Mark Rutland52e04ef2019-04-30 17:30:21 +01003986 if (!ptr)
3987 return;
3988
3989 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003990 if (put_page_testzero(page))
3991 free_compound_page(page);
3992}
3993
3994static void *io_mem_alloc(size_t size)
3995{
3996 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3997 __GFP_NORETRY;
3998
3999 return (void *) __get_free_pages(gfp_flags, get_order(size));
4000}
4001
Hristo Venev75b28af2019-08-26 17:23:46 +00004002static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4003 size_t *sq_offset)
4004{
4005 struct io_rings *rings;
4006 size_t off, sq_array_size;
4007
4008 off = struct_size(rings, cqes, cq_entries);
4009 if (off == SIZE_MAX)
4010 return SIZE_MAX;
4011
4012#ifdef CONFIG_SMP
4013 off = ALIGN(off, SMP_CACHE_BYTES);
4014 if (off == 0)
4015 return SIZE_MAX;
4016#endif
4017
4018 sq_array_size = array_size(sizeof(u32), sq_entries);
4019 if (sq_array_size == SIZE_MAX)
4020 return SIZE_MAX;
4021
4022 if (check_add_overflow(off, sq_array_size, &off))
4023 return SIZE_MAX;
4024
4025 if (sq_offset)
4026 *sq_offset = off;
4027
4028 return off;
4029}
4030
Jens Axboe2b188cc2019-01-07 10:46:33 -07004031static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4032{
Hristo Venev75b28af2019-08-26 17:23:46 +00004033 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004034
Hristo Venev75b28af2019-08-26 17:23:46 +00004035 pages = (size_t)1 << get_order(
4036 rings_size(sq_entries, cq_entries, NULL));
4037 pages += (size_t)1 << get_order(
4038 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004039
Hristo Venev75b28af2019-08-26 17:23:46 +00004040 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004041}
4042
Jens Axboeedafcce2019-01-09 09:16:05 -07004043static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4044{
4045 int i, j;
4046
4047 if (!ctx->user_bufs)
4048 return -ENXIO;
4049
4050 for (i = 0; i < ctx->nr_user_bufs; i++) {
4051 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4052
4053 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004054 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004055
4056 if (ctx->account_mem)
4057 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004058 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004059 imu->nr_bvecs = 0;
4060 }
4061
4062 kfree(ctx->user_bufs);
4063 ctx->user_bufs = NULL;
4064 ctx->nr_user_bufs = 0;
4065 return 0;
4066}
4067
4068static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4069 void __user *arg, unsigned index)
4070{
4071 struct iovec __user *src;
4072
4073#ifdef CONFIG_COMPAT
4074 if (ctx->compat) {
4075 struct compat_iovec __user *ciovs;
4076 struct compat_iovec ciov;
4077
4078 ciovs = (struct compat_iovec __user *) arg;
4079 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4080 return -EFAULT;
4081
4082 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4083 dst->iov_len = ciov.iov_len;
4084 return 0;
4085 }
4086#endif
4087 src = (struct iovec __user *) arg;
4088 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4089 return -EFAULT;
4090 return 0;
4091}
4092
4093static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4094 unsigned nr_args)
4095{
4096 struct vm_area_struct **vmas = NULL;
4097 struct page **pages = NULL;
4098 int i, j, got_pages = 0;
4099 int ret = -EINVAL;
4100
4101 if (ctx->user_bufs)
4102 return -EBUSY;
4103 if (!nr_args || nr_args > UIO_MAXIOV)
4104 return -EINVAL;
4105
4106 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4107 GFP_KERNEL);
4108 if (!ctx->user_bufs)
4109 return -ENOMEM;
4110
4111 for (i = 0; i < nr_args; i++) {
4112 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4113 unsigned long off, start, end, ubuf;
4114 int pret, nr_pages;
4115 struct iovec iov;
4116 size_t size;
4117
4118 ret = io_copy_iov(ctx, &iov, arg, i);
4119 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004120 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004121
4122 /*
4123 * Don't impose further limits on the size and buffer
4124 * constraints here, we'll -EINVAL later when IO is
4125 * submitted if they are wrong.
4126 */
4127 ret = -EFAULT;
4128 if (!iov.iov_base || !iov.iov_len)
4129 goto err;
4130
4131 /* arbitrary limit, but we need something */
4132 if (iov.iov_len > SZ_1G)
4133 goto err;
4134
4135 ubuf = (unsigned long) iov.iov_base;
4136 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4137 start = ubuf >> PAGE_SHIFT;
4138 nr_pages = end - start;
4139
4140 if (ctx->account_mem) {
4141 ret = io_account_mem(ctx->user, nr_pages);
4142 if (ret)
4143 goto err;
4144 }
4145
4146 ret = 0;
4147 if (!pages || nr_pages > got_pages) {
4148 kfree(vmas);
4149 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004150 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004151 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004152 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004153 sizeof(struct vm_area_struct *),
4154 GFP_KERNEL);
4155 if (!pages || !vmas) {
4156 ret = -ENOMEM;
4157 if (ctx->account_mem)
4158 io_unaccount_mem(ctx->user, nr_pages);
4159 goto err;
4160 }
4161 got_pages = nr_pages;
4162 }
4163
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004164 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004165 GFP_KERNEL);
4166 ret = -ENOMEM;
4167 if (!imu->bvec) {
4168 if (ctx->account_mem)
4169 io_unaccount_mem(ctx->user, nr_pages);
4170 goto err;
4171 }
4172
4173 ret = 0;
4174 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004175 pret = get_user_pages(ubuf, nr_pages,
4176 FOLL_WRITE | FOLL_LONGTERM,
4177 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004178 if (pret == nr_pages) {
4179 /* don't support file backed memory */
4180 for (j = 0; j < nr_pages; j++) {
4181 struct vm_area_struct *vma = vmas[j];
4182
4183 if (vma->vm_file &&
4184 !is_file_hugepages(vma->vm_file)) {
4185 ret = -EOPNOTSUPP;
4186 break;
4187 }
4188 }
4189 } else {
4190 ret = pret < 0 ? pret : -EFAULT;
4191 }
4192 up_read(&current->mm->mmap_sem);
4193 if (ret) {
4194 /*
4195 * if we did partial map, or found file backed vmas,
4196 * release any pages we did get
4197 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004198 if (pret > 0)
4199 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004200 if (ctx->account_mem)
4201 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004202 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004203 goto err;
4204 }
4205
4206 off = ubuf & ~PAGE_MASK;
4207 size = iov.iov_len;
4208 for (j = 0; j < nr_pages; j++) {
4209 size_t vec_len;
4210
4211 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4212 imu->bvec[j].bv_page = pages[j];
4213 imu->bvec[j].bv_len = vec_len;
4214 imu->bvec[j].bv_offset = off;
4215 off = 0;
4216 size -= vec_len;
4217 }
4218 /* store original address for later verification */
4219 imu->ubuf = ubuf;
4220 imu->len = iov.iov_len;
4221 imu->nr_bvecs = nr_pages;
4222
4223 ctx->nr_user_bufs++;
4224 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004225 kvfree(pages);
4226 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004227 return 0;
4228err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004229 kvfree(pages);
4230 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004231 io_sqe_buffer_unregister(ctx);
4232 return ret;
4233}
4234
Jens Axboe9b402842019-04-11 11:45:41 -06004235static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4236{
4237 __s32 __user *fds = arg;
4238 int fd;
4239
4240 if (ctx->cq_ev_fd)
4241 return -EBUSY;
4242
4243 if (copy_from_user(&fd, fds, sizeof(*fds)))
4244 return -EFAULT;
4245
4246 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4247 if (IS_ERR(ctx->cq_ev_fd)) {
4248 int ret = PTR_ERR(ctx->cq_ev_fd);
4249 ctx->cq_ev_fd = NULL;
4250 return ret;
4251 }
4252
4253 return 0;
4254}
4255
4256static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4257{
4258 if (ctx->cq_ev_fd) {
4259 eventfd_ctx_put(ctx->cq_ev_fd);
4260 ctx->cq_ev_fd = NULL;
4261 return 0;
4262 }
4263
4264 return -ENXIO;
4265}
4266
Jens Axboe2b188cc2019-01-07 10:46:33 -07004267static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4268{
Jens Axboe6b063142019-01-10 22:13:58 -07004269 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004270 if (ctx->sqo_mm)
4271 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004272
4273 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004274 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004275 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004276 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004277
Jens Axboe2b188cc2019-01-07 10:46:33 -07004278#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004279 if (ctx->ring_sock) {
4280 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004281 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004282 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004283#endif
4284
Hristo Venev75b28af2019-08-26 17:23:46 +00004285 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004286 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004287
4288 percpu_ref_exit(&ctx->refs);
4289 if (ctx->account_mem)
4290 io_unaccount_mem(ctx->user,
4291 ring_pages(ctx->sq_entries, ctx->cq_entries));
4292 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004293 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004294 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004295 kfree(ctx);
4296}
4297
4298static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4299{
4300 struct io_ring_ctx *ctx = file->private_data;
4301 __poll_t mask = 0;
4302
4303 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004304 /*
4305 * synchronizes with barrier from wq_has_sleeper call in
4306 * io_commit_cqring
4307 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004308 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004309 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4310 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004311 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004312 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004313 mask |= EPOLLIN | EPOLLRDNORM;
4314
4315 return mask;
4316}
4317
4318static int io_uring_fasync(int fd, struct file *file, int on)
4319{
4320 struct io_ring_ctx *ctx = file->private_data;
4321
4322 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4323}
4324
4325static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4326{
4327 mutex_lock(&ctx->uring_lock);
4328 percpu_ref_kill(&ctx->refs);
4329 mutex_unlock(&ctx->uring_lock);
4330
Jens Axboe5262f562019-09-17 12:26:57 -06004331 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004332 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004333
4334 if (ctx->io_wq)
4335 io_wq_cancel_all(ctx->io_wq);
4336
Jens Axboedef596e2019-01-09 08:59:42 -07004337 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004338 /* if we failed setting up the ctx, we might not have any rings */
4339 if (ctx->rings)
4340 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004341 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004342 io_ring_ctx_free(ctx);
4343}
4344
4345static int io_uring_release(struct inode *inode, struct file *file)
4346{
4347 struct io_ring_ctx *ctx = file->private_data;
4348
4349 file->private_data = NULL;
4350 io_ring_ctx_wait_and_kill(ctx);
4351 return 0;
4352}
4353
Jens Axboefcb323c2019-10-24 12:39:47 -06004354static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4355 struct files_struct *files)
4356{
4357 struct io_kiocb *req;
4358 DEFINE_WAIT(wait);
4359
4360 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004361 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004362
4363 spin_lock_irq(&ctx->inflight_lock);
4364 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004365 if (req->work.files != files)
4366 continue;
4367 /* req is being completed, ignore */
4368 if (!refcount_inc_not_zero(&req->refs))
4369 continue;
4370 cancel_req = req;
4371 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004372 }
Jens Axboe768134d2019-11-10 20:30:53 -07004373 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004374 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004375 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004376 spin_unlock_irq(&ctx->inflight_lock);
4377
Jens Axboe768134d2019-11-10 20:30:53 -07004378 /* We need to keep going until we don't find a matching req */
4379 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004380 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004381
4382 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4383 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004384 schedule();
4385 }
Jens Axboe768134d2019-11-10 20:30:53 -07004386 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004387}
4388
4389static int io_uring_flush(struct file *file, void *data)
4390{
4391 struct io_ring_ctx *ctx = file->private_data;
4392
4393 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004394 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4395 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004396 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004397 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004398 return 0;
4399}
4400
Jens Axboe2b188cc2019-01-07 10:46:33 -07004401static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4402{
4403 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4404 unsigned long sz = vma->vm_end - vma->vm_start;
4405 struct io_ring_ctx *ctx = file->private_data;
4406 unsigned long pfn;
4407 struct page *page;
4408 void *ptr;
4409
4410 switch (offset) {
4411 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004412 case IORING_OFF_CQ_RING:
4413 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004414 break;
4415 case IORING_OFF_SQES:
4416 ptr = ctx->sq_sqes;
4417 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004418 default:
4419 return -EINVAL;
4420 }
4421
4422 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004423 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004424 return -EINVAL;
4425
4426 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4427 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4428}
4429
4430SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4431 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4432 size_t, sigsz)
4433{
4434 struct io_ring_ctx *ctx;
4435 long ret = -EBADF;
4436 int submitted = 0;
4437 struct fd f;
4438
Jens Axboe6c271ce2019-01-10 11:22:30 -07004439 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004440 return -EINVAL;
4441
4442 f = fdget(fd);
4443 if (!f.file)
4444 return -EBADF;
4445
4446 ret = -EOPNOTSUPP;
4447 if (f.file->f_op != &io_uring_fops)
4448 goto out_fput;
4449
4450 ret = -ENXIO;
4451 ctx = f.file->private_data;
4452 if (!percpu_ref_tryget(&ctx->refs))
4453 goto out_fput;
4454
Jens Axboe6c271ce2019-01-10 11:22:30 -07004455 /*
4456 * For SQ polling, the thread will do all submissions and completions.
4457 * Just return the requested submit count, and wake the thread if
4458 * we were asked to.
4459 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004460 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004461 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004462 if (!list_empty_careful(&ctx->cq_overflow_list))
4463 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004464 if (flags & IORING_ENTER_SQ_WAKEUP)
4465 wake_up(&ctx->sqo_wait);
4466 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004467 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004468 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004469
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004470 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004471 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004472 /* already have mm, so io_submit_sqes() won't try to grab it */
4473 cur_mm = ctx->sqo_mm;
4474 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4475 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004476 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004477 }
4478 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004479 unsigned nr_events = 0;
4480
Jens Axboe2b188cc2019-01-07 10:46:33 -07004481 min_complete = min(min_complete, ctx->cq_entries);
4482
Jens Axboedef596e2019-01-09 08:59:42 -07004483 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004484 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004485 } else {
4486 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4487 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004488 }
4489
Pavel Begunkov6805b322019-10-08 02:18:42 +03004490 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004491out_fput:
4492 fdput(f);
4493 return submitted ? submitted : ret;
4494}
4495
4496static const struct file_operations io_uring_fops = {
4497 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004498 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004499 .mmap = io_uring_mmap,
4500 .poll = io_uring_poll,
4501 .fasync = io_uring_fasync,
4502};
4503
4504static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4505 struct io_uring_params *p)
4506{
Hristo Venev75b28af2019-08-26 17:23:46 +00004507 struct io_rings *rings;
4508 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004509
Hristo Venev75b28af2019-08-26 17:23:46 +00004510 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4511 if (size == SIZE_MAX)
4512 return -EOVERFLOW;
4513
4514 rings = io_mem_alloc(size);
4515 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004516 return -ENOMEM;
4517
Hristo Venev75b28af2019-08-26 17:23:46 +00004518 ctx->rings = rings;
4519 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4520 rings->sq_ring_mask = p->sq_entries - 1;
4521 rings->cq_ring_mask = p->cq_entries - 1;
4522 rings->sq_ring_entries = p->sq_entries;
4523 rings->cq_ring_entries = p->cq_entries;
4524 ctx->sq_mask = rings->sq_ring_mask;
4525 ctx->cq_mask = rings->cq_ring_mask;
4526 ctx->sq_entries = rings->sq_ring_entries;
4527 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004528
4529 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4530 if (size == SIZE_MAX)
4531 return -EOVERFLOW;
4532
4533 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004534 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004535 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004536
Jens Axboe2b188cc2019-01-07 10:46:33 -07004537 return 0;
4538}
4539
4540/*
4541 * Allocate an anonymous fd, this is what constitutes the application
4542 * visible backing of an io_uring instance. The application mmaps this
4543 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4544 * we have to tie this fd to a socket for file garbage collection purposes.
4545 */
4546static int io_uring_get_fd(struct io_ring_ctx *ctx)
4547{
4548 struct file *file;
4549 int ret;
4550
4551#if defined(CONFIG_UNIX)
4552 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4553 &ctx->ring_sock);
4554 if (ret)
4555 return ret;
4556#endif
4557
4558 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4559 if (ret < 0)
4560 goto err;
4561
4562 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4563 O_RDWR | O_CLOEXEC);
4564 if (IS_ERR(file)) {
4565 put_unused_fd(ret);
4566 ret = PTR_ERR(file);
4567 goto err;
4568 }
4569
4570#if defined(CONFIG_UNIX)
4571 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004572 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004573#endif
4574 fd_install(ret, file);
4575 return ret;
4576err:
4577#if defined(CONFIG_UNIX)
4578 sock_release(ctx->ring_sock);
4579 ctx->ring_sock = NULL;
4580#endif
4581 return ret;
4582}
4583
4584static int io_uring_create(unsigned entries, struct io_uring_params *p)
4585{
4586 struct user_struct *user = NULL;
4587 struct io_ring_ctx *ctx;
4588 bool account_mem;
4589 int ret;
4590
4591 if (!entries || entries > IORING_MAX_ENTRIES)
4592 return -EINVAL;
4593
4594 /*
4595 * Use twice as many entries for the CQ ring. It's possible for the
4596 * application to drive a higher depth than the size of the SQ ring,
4597 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004598 * some flexibility in overcommitting a bit. If the application has
4599 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4600 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004601 */
4602 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004603 if (p->flags & IORING_SETUP_CQSIZE) {
4604 /*
4605 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4606 * to a power-of-two, if it isn't already. We do NOT impose
4607 * any cq vs sq ring sizing.
4608 */
4609 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4610 return -EINVAL;
4611 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4612 } else {
4613 p->cq_entries = 2 * p->sq_entries;
4614 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004615
4616 user = get_uid(current_user());
4617 account_mem = !capable(CAP_IPC_LOCK);
4618
4619 if (account_mem) {
4620 ret = io_account_mem(user,
4621 ring_pages(p->sq_entries, p->cq_entries));
4622 if (ret) {
4623 free_uid(user);
4624 return ret;
4625 }
4626 }
4627
4628 ctx = io_ring_ctx_alloc(p);
4629 if (!ctx) {
4630 if (account_mem)
4631 io_unaccount_mem(user, ring_pages(p->sq_entries,
4632 p->cq_entries));
4633 free_uid(user);
4634 return -ENOMEM;
4635 }
4636 ctx->compat = in_compat_syscall();
4637 ctx->account_mem = account_mem;
4638 ctx->user = user;
4639
4640 ret = io_allocate_scq_urings(ctx, p);
4641 if (ret)
4642 goto err;
4643
Jens Axboe6c271ce2019-01-10 11:22:30 -07004644 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004645 if (ret)
4646 goto err;
4647
Jens Axboe2b188cc2019-01-07 10:46:33 -07004648 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004649 p->sq_off.head = offsetof(struct io_rings, sq.head);
4650 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4651 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4652 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4653 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4654 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4655 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004656
4657 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004658 p->cq_off.head = offsetof(struct io_rings, cq.head);
4659 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4660 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4661 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4662 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4663 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004664
Jens Axboe044c1ab2019-10-28 09:15:33 -06004665 /*
4666 * Install ring fd as the very last thing, so we don't risk someone
4667 * having closed it before we finish setup
4668 */
4669 ret = io_uring_get_fd(ctx);
4670 if (ret < 0)
4671 goto err;
4672
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004673 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004674 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004675 return ret;
4676err:
4677 io_ring_ctx_wait_and_kill(ctx);
4678 return ret;
4679}
4680
4681/*
4682 * Sets up an aio uring context, and returns the fd. Applications asks for a
4683 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4684 * params structure passed in.
4685 */
4686static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4687{
4688 struct io_uring_params p;
4689 long ret;
4690 int i;
4691
4692 if (copy_from_user(&p, params, sizeof(p)))
4693 return -EFAULT;
4694 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4695 if (p.resv[i])
4696 return -EINVAL;
4697 }
4698
Jens Axboe6c271ce2019-01-10 11:22:30 -07004699 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004700 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004701 return -EINVAL;
4702
4703 ret = io_uring_create(entries, &p);
4704 if (ret < 0)
4705 return ret;
4706
4707 if (copy_to_user(params, &p, sizeof(p)))
4708 return -EFAULT;
4709
4710 return ret;
4711}
4712
4713SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4714 struct io_uring_params __user *, params)
4715{
4716 return io_uring_setup(entries, params);
4717}
4718
Jens Axboeedafcce2019-01-09 09:16:05 -07004719static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4720 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004721 __releases(ctx->uring_lock)
4722 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004723{
4724 int ret;
4725
Jens Axboe35fa71a2019-04-22 10:23:23 -06004726 /*
4727 * We're inside the ring mutex, if the ref is already dying, then
4728 * someone else killed the ctx or is already going through
4729 * io_uring_register().
4730 */
4731 if (percpu_ref_is_dying(&ctx->refs))
4732 return -ENXIO;
4733
Jens Axboeedafcce2019-01-09 09:16:05 -07004734 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004735
4736 /*
4737 * Drop uring mutex before waiting for references to exit. If another
4738 * thread is currently inside io_uring_enter() it might need to grab
4739 * the uring_lock to make progress. If we hold it here across the drain
4740 * wait, then we can deadlock. It's safe to drop the mutex here, since
4741 * no new references will come in after we've killed the percpu ref.
4742 */
4743 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004744 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004745 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004746
4747 switch (opcode) {
4748 case IORING_REGISTER_BUFFERS:
4749 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4750 break;
4751 case IORING_UNREGISTER_BUFFERS:
4752 ret = -EINVAL;
4753 if (arg || nr_args)
4754 break;
4755 ret = io_sqe_buffer_unregister(ctx);
4756 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004757 case IORING_REGISTER_FILES:
4758 ret = io_sqe_files_register(ctx, arg, nr_args);
4759 break;
4760 case IORING_UNREGISTER_FILES:
4761 ret = -EINVAL;
4762 if (arg || nr_args)
4763 break;
4764 ret = io_sqe_files_unregister(ctx);
4765 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004766 case IORING_REGISTER_FILES_UPDATE:
4767 ret = io_sqe_files_update(ctx, arg, nr_args);
4768 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004769 case IORING_REGISTER_EVENTFD:
4770 ret = -EINVAL;
4771 if (nr_args != 1)
4772 break;
4773 ret = io_eventfd_register(ctx, arg);
4774 break;
4775 case IORING_UNREGISTER_EVENTFD:
4776 ret = -EINVAL;
4777 if (arg || nr_args)
4778 break;
4779 ret = io_eventfd_unregister(ctx);
4780 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004781 default:
4782 ret = -EINVAL;
4783 break;
4784 }
4785
4786 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004787 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004788 percpu_ref_reinit(&ctx->refs);
4789 return ret;
4790}
4791
4792SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4793 void __user *, arg, unsigned int, nr_args)
4794{
4795 struct io_ring_ctx *ctx;
4796 long ret = -EBADF;
4797 struct fd f;
4798
4799 f = fdget(fd);
4800 if (!f.file)
4801 return -EBADF;
4802
4803 ret = -EOPNOTSUPP;
4804 if (f.file->f_op != &io_uring_fops)
4805 goto out_fput;
4806
4807 ctx = f.file->private_data;
4808
4809 mutex_lock(&ctx->uring_lock);
4810 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4811 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004812 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4813 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004814out_fput:
4815 fdput(f);
4816 return ret;
4817}
4818
Jens Axboe2b188cc2019-01-07 10:46:33 -07004819static int __init io_uring_init(void)
4820{
4821 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4822 return 0;
4823};
4824__initcall(io_uring_init);