blob: 3c573f0578a82ea544c1e50e6ca98b07a7a2c376 [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;
177};
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 Axboefcb323c2019-10-24 12:39:47 -0600213
214 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700215 } ____cacheline_aligned_in_smp;
216
Jens Axboe206aefd2019-11-07 18:27:42 -0700217 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 Axboe6c271ce2019-01-10 11:22:30 -0700221 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700223 wait_queue_head_t sqo_wait;
Hristo Venev75b28af2019-08-26 17:23:46 +0000224
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 Axboe221c5eb2019-01-17 09:41:58 -0700274 struct list_head cancel_list;
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;
326 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600327 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700328 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700329 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200330#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700331#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700332#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700333#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200334#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
335#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600336#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700337#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800338#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800339#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600340#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600341#define REQ_F_ISREG 2048 /* regular file */
342#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboefcb323c2019-10-24 12:39:47 -0600343#define REQ_F_INFLIGHT 8192 /* on inflight list */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600345 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600346 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700347
Jens Axboefcb323c2019-10-24 12:39:47 -0600348 struct list_head inflight_entry;
349
Jens Axboe561fb042019-10-24 07:25:42 -0600350 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700351};
352
353#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700354#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700355
Jens Axboe9a56a232019-01-09 09:06:50 -0700356struct io_submit_state {
357 struct blk_plug plug;
358
359 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700360 * io_kiocb alloc cache
361 */
362 void *reqs[IO_IOPOLL_BATCH];
363 unsigned int free_reqs;
364 unsigned int cur_req;
365
366 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700367 * File reference cache
368 */
369 struct file *file;
370 unsigned int fd;
371 unsigned int has_refs;
372 unsigned int used_refs;
373 unsigned int ios_left;
374};
375
Jens Axboe561fb042019-10-24 07:25:42 -0600376static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700377static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800378static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800379static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700380static void io_double_put_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600381
Jens Axboe2b188cc2019-01-07 10:46:33 -0700382static struct kmem_cache *req_cachep;
383
384static const struct file_operations io_uring_fops;
385
386struct sock *io_uring_get_socket(struct file *file)
387{
388#if defined(CONFIG_UNIX)
389 if (file->f_op == &io_uring_fops) {
390 struct io_ring_ctx *ctx = file->private_data;
391
392 return ctx->ring_sock->sk;
393 }
394#endif
395 return NULL;
396}
397EXPORT_SYMBOL(io_uring_get_socket);
398
399static void io_ring_ctx_ref_free(struct percpu_ref *ref)
400{
401 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
402
Jens Axboe206aefd2019-11-07 18:27:42 -0700403 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700404}
405
406static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
407{
408 struct io_ring_ctx *ctx;
409
410 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
411 if (!ctx)
412 return NULL;
413
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700414 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
415 if (!ctx->fallback_req)
416 goto err;
417
Jens Axboe206aefd2019-11-07 18:27:42 -0700418 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
419 if (!ctx->completions)
420 goto err;
421
Roman Gushchin21482892019-05-07 10:01:48 -0700422 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700423 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
424 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700425
426 ctx->flags = p->flags;
427 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700428 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700429 init_completion(&ctx->completions[0]);
430 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700431 mutex_init(&ctx->uring_lock);
432 init_waitqueue_head(&ctx->wait);
433 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700434 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700435 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600436 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600437 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600438 init_waitqueue_head(&ctx->inflight_wait);
439 spin_lock_init(&ctx->inflight_lock);
440 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700441 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700442err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700443 if (ctx->fallback_req)
444 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700445 kfree(ctx->completions);
446 kfree(ctx);
447 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700448}
449
Jackie Liua197f662019-11-08 08:09:12 -0700450static inline bool __io_sequence_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600451{
Jackie Liua197f662019-11-08 08:09:12 -0700452 struct io_ring_ctx *ctx = req->ctx;
453
Jens Axboe498ccd92019-10-25 10:04:25 -0600454 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
455 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600456}
457
Jackie Liua197f662019-11-08 08:09:12 -0700458static inline bool io_sequence_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600459{
460 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
461 return false;
462
Jackie Liua197f662019-11-08 08:09:12 -0700463 return __io_sequence_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600464}
465
466static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600467{
468 struct io_kiocb *req;
469
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600470 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Jackie Liua197f662019-11-08 08:09:12 -0700471 if (req && !io_sequence_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600472 list_del_init(&req->list);
473 return req;
474 }
475
476 return NULL;
477}
478
Jens Axboe5262f562019-09-17 12:26:57 -0600479static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
480{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600481 struct io_kiocb *req;
482
483 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jackie Liua197f662019-11-08 08:09:12 -0700484 if (req && !__io_sequence_defer(req)) {
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600485 list_del_init(&req->list);
486 return req;
487 }
488
489 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600490}
491
Jens Axboede0617e2019-04-06 21:51:27 -0600492static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700493{
Hristo Venev75b28af2019-08-26 17:23:46 +0000494 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700495
Hristo Venev75b28af2019-08-26 17:23:46 +0000496 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700497 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000498 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700499
Jens Axboe2b188cc2019-01-07 10:46:33 -0700500 if (wq_has_sleeper(&ctx->cq_wait)) {
501 wake_up_interruptible(&ctx->cq_wait);
502 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
503 }
504 }
505}
506
Jens Axboe561fb042019-10-24 07:25:42 -0600507static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600508{
Jens Axboe561fb042019-10-24 07:25:42 -0600509 u8 opcode = READ_ONCE(sqe->opcode);
510
511 return !(opcode == IORING_OP_READ_FIXED ||
512 opcode == IORING_OP_WRITE_FIXED);
513}
514
515static inline bool io_prep_async_work(struct io_kiocb *req)
516{
517 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600518
Jens Axboe6cc47d12019-09-18 11:18:23 -0600519 if (req->submit.sqe) {
520 switch (req->submit.sqe->opcode) {
521 case IORING_OP_WRITEV:
522 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600523 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700524 /* fall-through */
525 case IORING_OP_READV:
526 case IORING_OP_READ_FIXED:
527 case IORING_OP_SENDMSG:
528 case IORING_OP_RECVMSG:
529 case IORING_OP_ACCEPT:
530 case IORING_OP_POLL_ADD:
531 /*
532 * We know REQ_F_ISREG is not set on some of these
533 * opcodes, but this enables us to keep the check in
534 * just one place.
535 */
536 if (!(req->flags & REQ_F_ISREG))
537 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600538 break;
539 }
Jens Axboe561fb042019-10-24 07:25:42 -0600540 if (io_sqe_needs_user(req->submit.sqe))
541 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600542 }
543
Jens Axboe561fb042019-10-24 07:25:42 -0600544 return do_hashed;
545}
546
Jackie Liua197f662019-11-08 08:09:12 -0700547static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600548{
549 bool do_hashed = io_prep_async_work(req);
Jackie Liua197f662019-11-08 08:09:12 -0700550 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -0600551
552 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
553 req->flags);
554 if (!do_hashed) {
555 io_wq_enqueue(ctx->io_wq, &req->work);
556 } else {
557 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
558 file_inode(req->file));
559 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600560}
561
Jens Axboe5262f562019-09-17 12:26:57 -0600562static void io_kill_timeout(struct io_kiocb *req)
563{
564 int ret;
565
566 ret = hrtimer_try_to_cancel(&req->timeout.timer);
567 if (ret != -1) {
568 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600569 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700570 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800571 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600572 }
573}
574
575static void io_kill_timeouts(struct io_ring_ctx *ctx)
576{
577 struct io_kiocb *req, *tmp;
578
579 spin_lock_irq(&ctx->completion_lock);
580 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
581 io_kill_timeout(req);
582 spin_unlock_irq(&ctx->completion_lock);
583}
584
Jens Axboede0617e2019-04-06 21:51:27 -0600585static void io_commit_cqring(struct io_ring_ctx *ctx)
586{
587 struct io_kiocb *req;
588
Jens Axboe5262f562019-09-17 12:26:57 -0600589 while ((req = io_get_timeout_req(ctx)) != NULL)
590 io_kill_timeout(req);
591
Jens Axboede0617e2019-04-06 21:51:27 -0600592 __io_commit_cqring(ctx);
593
594 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800595 if (req->flags & REQ_F_SHADOW_DRAIN) {
596 /* Just for drain, free it. */
597 __io_free_req(req);
598 continue;
599 }
Jens Axboede0617e2019-04-06 21:51:27 -0600600 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700601 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600602 }
603}
604
Jens Axboe2b188cc2019-01-07 10:46:33 -0700605static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
606{
Hristo Venev75b28af2019-08-26 17:23:46 +0000607 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608 unsigned tail;
609
610 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200611 /*
612 * writes to the cq entry need to come after reading head; the
613 * control dependency is enough as we're using WRITE_ONCE to
614 * fill the cq entry
615 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000616 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700617 return NULL;
618
619 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000620 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700621}
622
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700623static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
624{
625 if (waitqueue_active(&ctx->wait))
626 wake_up(&ctx->wait);
627 if (waitqueue_active(&ctx->sqo_wait))
628 wake_up(&ctx->sqo_wait);
629 if (ctx->cq_ev_fd)
630 eventfd_signal(ctx->cq_ev_fd, 1);
631}
632
633static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
634{
635 struct io_rings *rings = ctx->rings;
636 struct io_uring_cqe *cqe;
637 struct io_kiocb *req;
638 unsigned long flags;
639 LIST_HEAD(list);
640
641 if (!force) {
642 if (list_empty_careful(&ctx->cq_overflow_list))
643 return;
644 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
645 rings->cq_ring_entries))
646 return;
647 }
648
649 spin_lock_irqsave(&ctx->completion_lock, flags);
650
651 /* if force is set, the ring is going away. always drop after that */
652 if (force)
653 ctx->cq_overflow_flushed = true;
654
655 while (!list_empty(&ctx->cq_overflow_list)) {
656 cqe = io_get_cqring(ctx);
657 if (!cqe && !force)
658 break;
659
660 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
661 list);
662 list_move(&req->list, &list);
663 if (cqe) {
664 WRITE_ONCE(cqe->user_data, req->user_data);
665 WRITE_ONCE(cqe->res, req->result);
666 WRITE_ONCE(cqe->flags, 0);
667 } else {
668 WRITE_ONCE(ctx->rings->cq_overflow,
669 atomic_inc_return(&ctx->cached_cq_overflow));
670 }
671 }
672
673 io_commit_cqring(ctx);
674 spin_unlock_irqrestore(&ctx->completion_lock, flags);
675 io_cqring_ev_posted(ctx);
676
677 while (!list_empty(&list)) {
678 req = list_first_entry(&list, struct io_kiocb, list);
679 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800680 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700681 }
682}
683
Jens Axboe78e19bb2019-11-06 15:21:34 -0700684static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700685{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700686 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700687 struct io_uring_cqe *cqe;
688
Jens Axboe78e19bb2019-11-06 15:21:34 -0700689 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700690
Jens Axboe2b188cc2019-01-07 10:46:33 -0700691 /*
692 * If we can't get a cq entry, userspace overflowed the
693 * submission (by quite a lot). Increment the overflow count in
694 * the ring.
695 */
696 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700697 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700698 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700699 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600700 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700701 } else if (ctx->cq_overflow_flushed) {
Jens Axboe498ccd92019-10-25 10:04:25 -0600702 WRITE_ONCE(ctx->rings->cq_overflow,
703 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700704 } else {
705 refcount_inc(&req->refs);
706 req->result = res;
707 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700708 }
709}
710
Jens Axboe78e19bb2019-11-06 15:21:34 -0700711static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700712{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700713 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700714 unsigned long flags;
715
716 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700717 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700718 io_commit_cqring(ctx);
719 spin_unlock_irqrestore(&ctx->completion_lock, flags);
720
Jens Axboe8c838782019-03-12 15:48:16 -0600721 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700722}
723
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700724static inline bool io_is_fallback_req(struct io_kiocb *req)
725{
726 return req == (struct io_kiocb *)
727 ((unsigned long) req->ctx->fallback_req & ~1UL);
728}
729
730static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
731{
732 struct io_kiocb *req;
733
734 req = ctx->fallback_req;
735 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
736 return req;
737
738 return NULL;
739}
740
Jens Axboe2579f912019-01-09 09:10:43 -0700741static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
742 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700743{
Jens Axboefd6fab22019-03-14 16:30:06 -0600744 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700745 struct io_kiocb *req;
746
747 if (!percpu_ref_tryget(&ctx->refs))
748 return NULL;
749
Jens Axboe2579f912019-01-09 09:10:43 -0700750 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600751 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700752 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700753 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700754 } else if (!state->free_reqs) {
755 size_t sz;
756 int ret;
757
758 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600759 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
760
761 /*
762 * Bulk alloc is all-or-nothing. If we fail to get a batch,
763 * retry single alloc to be on the safe side.
764 */
765 if (unlikely(ret <= 0)) {
766 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
767 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700768 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600769 ret = 1;
770 }
Jens Axboe2579f912019-01-09 09:10:43 -0700771 state->free_reqs = ret - 1;
772 state->cur_req = 1;
773 req = state->reqs[0];
774 } else {
775 req = state->reqs[state->cur_req];
776 state->free_reqs--;
777 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700778 }
779
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700780got_it:
Jens Axboe60c112b2019-06-21 10:20:18 -0600781 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700782 req->ctx = ctx;
783 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600784 /* one is dropped after submission, the other at completion */
785 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600786 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600787 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700788 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700789fallback:
790 req = io_get_fallback_req(ctx);
791 if (req)
792 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300793 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700794 return NULL;
795}
796
Jens Axboedef596e2019-01-09 08:59:42 -0700797static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
798{
799 if (*nr) {
800 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300801 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700802 *nr = 0;
803 }
804}
805
Jens Axboe9e645e112019-05-10 16:07:28 -0600806static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700807{
Jens Axboefcb323c2019-10-24 12:39:47 -0600808 struct io_ring_ctx *ctx = req->ctx;
809
Jens Axboe09bb8392019-03-13 12:39:28 -0600810 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
811 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600812 if (req->flags & REQ_F_INFLIGHT) {
813 unsigned long flags;
814
815 spin_lock_irqsave(&ctx->inflight_lock, flags);
816 list_del(&req->inflight_entry);
817 if (waitqueue_active(&ctx->inflight_wait))
818 wake_up(&ctx->inflight_wait);
819 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
820 }
821 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700822 if (likely(!io_is_fallback_req(req)))
823 kmem_cache_free(req_cachep, req);
824 else
825 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600826}
827
Jackie Liua197f662019-11-08 08:09:12 -0700828static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -0700829{
Jackie Liua197f662019-11-08 08:09:12 -0700830 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700831 int ret;
832
833 ret = hrtimer_try_to_cancel(&req->timeout.timer);
834 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700835 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700836 io_commit_cqring(ctx);
837 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800838 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700839 return true;
840 }
841
842 return false;
843}
844
Jens Axboeba816ad2019-09-28 11:36:45 -0600845static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600846{
Jens Axboe2665abf2019-11-05 12:40:47 -0700847 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600848 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700849 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600850
851 /*
852 * The list should never be empty when we are called here. But could
853 * potentially happen if the chain is messed up, check to be on the
854 * safe side.
855 */
856 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700857 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700858 list_del_init(&nxt->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600859 if (!list_empty(&req->link_list)) {
860 INIT_LIST_HEAD(&nxt->link_list);
861 list_splice(&req->link_list, &nxt->link_list);
862 nxt->flags |= REQ_F_LINK;
863 }
864
Jens Axboeba816ad2019-09-28 11:36:45 -0600865 /*
866 * If we're in async work, we can continue processing the chain
867 * in this context instead of having to queue up new async work.
868 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700869 if (req->flags & REQ_F_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700870 wake_ev = io_link_cancel_timeout(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700871
872 /* we dropped this link, get next */
873 nxt = list_first_entry_or_null(&req->link_list,
874 struct io_kiocb, list);
Jens Axboe960e4322019-11-12 07:56:39 -0700875 } else if (nxtptr && io_wq_current_is_worker()) {
Jens Axboeba816ad2019-09-28 11:36:45 -0600876 *nxtptr = nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700877 break;
878 } else {
Jackie Liua197f662019-11-08 08:09:12 -0700879 io_queue_async_work(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700880 break;
881 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600882 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700883
884 if (wake_ev)
885 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600886}
887
888/*
889 * Called if REQ_F_LINK is set, and we fail the head request
890 */
891static void io_fail_links(struct io_kiocb *req)
892{
Jens Axboe2665abf2019-11-05 12:40:47 -0700893 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600894 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700895 unsigned long flags;
896
897 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600898
899 while (!list_empty(&req->link_list)) {
900 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700901 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600902
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200903 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700904
905 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
906 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700907 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700908 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700909 io_cqring_fill_event(link, -ECANCELED);
910 io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700911 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600912 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700913
914 io_commit_cqring(ctx);
915 spin_unlock_irqrestore(&ctx->completion_lock, flags);
916 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600917}
918
Jackie Liuc69f8db2019-11-09 11:00:08 +0800919static void io_free_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600920{
Jens Axboe2665abf2019-11-05 12:40:47 -0700921 if (likely(!(req->flags & REQ_F_LINK))) {
922 __io_free_req(req);
923 return;
924 }
925
Jens Axboe9e645e112019-05-10 16:07:28 -0600926 /*
927 * If LINK is set, we have dependent requests in this chain. If we
928 * didn't fail this request, queue the first one up, moving any other
929 * dependencies to the next request. In case of failure, fail the rest
930 * of the chain.
931 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700932 if (req->flags & REQ_F_FAIL_LINK) {
933 io_fail_links(req);
934 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
935 struct io_ring_ctx *ctx = req->ctx;
936 unsigned long flags;
937
938 /*
939 * If this is a timeout link, we could be racing with the
940 * timeout timer. Grab the completion lock for this case to
941 * protection against that.
942 */
943 spin_lock_irqsave(&ctx->completion_lock, flags);
944 io_req_link_next(req, nxt);
945 spin_unlock_irqrestore(&ctx->completion_lock, flags);
946 } else {
947 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600948 }
949
950 __io_free_req(req);
951}
952
Jackie Liuc69f8db2019-11-09 11:00:08 +0800953static void io_free_req(struct io_kiocb *req)
954{
955 io_free_req_find_next(req, NULL);
956}
957
Jens Axboeba816ad2019-09-28 11:36:45 -0600958/*
959 * Drop reference to request, return next in chain (if there is one) if this
960 * was the last reference to this request.
961 */
Jackie Liuec9c02a2019-11-08 23:50:36 +0800962static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -0600963{
Jens Axboeba816ad2019-09-28 11:36:45 -0600964 struct io_kiocb *nxt = NULL;
965
Jens Axboee65ef562019-03-12 10:16:44 -0600966 if (refcount_dec_and_test(&req->refs))
Jackie Liuc69f8db2019-11-09 11:00:08 +0800967 io_free_req_find_next(req, &nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600968
Jens Axboeba816ad2019-09-28 11:36:45 -0600969 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600970 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600971 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600972 else
Jackie Liua197f662019-11-08 08:09:12 -0700973 io_queue_async_work(nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600974 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700975}
976
Jackie Liuec9c02a2019-11-08 23:50:36 +0800977static void io_put_req(struct io_kiocb *req)
978{
979 if (refcount_dec_and_test(&req->refs))
Jackie Liuc69f8db2019-11-09 11:00:08 +0800980 io_free_req(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800981}
982
Jens Axboe78e19bb2019-11-06 15:21:34 -0700983static void io_double_put_req(struct io_kiocb *req)
984{
985 /* drop both submit and complete references */
986 if (refcount_sub_and_test(2, &req->refs))
987 __io_free_req(req);
988}
989
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700990static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -0600991{
Jens Axboe84f97dc2019-11-06 11:27:53 -0700992 struct io_rings *rings = ctx->rings;
993
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700994 /*
995 * noflush == true is from the waitqueue handler, just ensure we wake
996 * up the task, and the next invocation will flush the entries. We
997 * cannot safely to it from here.
998 */
999 if (noflush && !list_empty(&ctx->cq_overflow_list))
1000 return -1U;
1001
1002 io_cqring_overflow_flush(ctx, false);
1003
Jens Axboea3a0e432019-08-20 11:03:11 -06001004 /* See comment at the top of this file */
1005 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001006 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001007}
1008
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001009static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1010{
1011 struct io_rings *rings = ctx->rings;
1012
1013 /* make sure SQ entry isn't read before tail */
1014 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1015}
1016
Jens Axboedef596e2019-01-09 08:59:42 -07001017/*
1018 * Find and free completed poll iocbs
1019 */
1020static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1021 struct list_head *done)
1022{
1023 void *reqs[IO_IOPOLL_BATCH];
1024 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001025 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001026
Jens Axboe09bb8392019-03-13 12:39:28 -06001027 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001028 while (!list_empty(done)) {
1029 req = list_first_entry(done, struct io_kiocb, list);
1030 list_del(&req->list);
1031
Jens Axboe78e19bb2019-11-06 15:21:34 -07001032 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001033 (*nr_events)++;
1034
Jens Axboe09bb8392019-03-13 12:39:28 -06001035 if (refcount_dec_and_test(&req->refs)) {
1036 /* If we're not using fixed files, we have to pair the
1037 * completion part with the file put. Use regular
1038 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001039 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001040 */
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001041 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1042 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001043 reqs[to_free++] = req;
1044 if (to_free == ARRAY_SIZE(reqs))
1045 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001046 } else {
Jackie Liuc69f8db2019-11-09 11:00:08 +08001047 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001048 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001049 }
Jens Axboedef596e2019-01-09 08:59:42 -07001050 }
Jens Axboedef596e2019-01-09 08:59:42 -07001051
Jens Axboe09bb8392019-03-13 12:39:28 -06001052 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001053 io_free_req_many(ctx, reqs, &to_free);
1054}
1055
1056static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1057 long min)
1058{
1059 struct io_kiocb *req, *tmp;
1060 LIST_HEAD(done);
1061 bool spin;
1062 int ret;
1063
1064 /*
1065 * Only spin for completions if we don't have multiple devices hanging
1066 * off our complete list, and we're under the requested amount.
1067 */
1068 spin = !ctx->poll_multi_file && *nr_events < min;
1069
1070 ret = 0;
1071 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1072 struct kiocb *kiocb = &req->rw;
1073
1074 /*
1075 * Move completed entries to our local list. If we find a
1076 * request that requires polling, break out and complete
1077 * the done list first, if we have entries there.
1078 */
1079 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1080 list_move_tail(&req->list, &done);
1081 continue;
1082 }
1083 if (!list_empty(&done))
1084 break;
1085
1086 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1087 if (ret < 0)
1088 break;
1089
1090 if (ret && spin)
1091 spin = false;
1092 ret = 0;
1093 }
1094
1095 if (!list_empty(&done))
1096 io_iopoll_complete(ctx, nr_events, &done);
1097
1098 return ret;
1099}
1100
1101/*
1102 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1103 * non-spinning poll check - we'll still enter the driver poll loop, but only
1104 * as a non-spinning completion check.
1105 */
1106static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1107 long min)
1108{
Jens Axboe08f54392019-08-21 22:19:11 -06001109 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001110 int ret;
1111
1112 ret = io_do_iopoll(ctx, nr_events, min);
1113 if (ret < 0)
1114 return ret;
1115 if (!min || *nr_events >= min)
1116 return 0;
1117 }
1118
1119 return 1;
1120}
1121
1122/*
1123 * We can't just wait for polled events to come to us, we have to actively
1124 * find and complete them.
1125 */
1126static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1127{
1128 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1129 return;
1130
1131 mutex_lock(&ctx->uring_lock);
1132 while (!list_empty(&ctx->poll_list)) {
1133 unsigned int nr_events = 0;
1134
1135 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001136
1137 /*
1138 * Ensure we allow local-to-the-cpu processing to take place,
1139 * in this case we need to ensure that we reap all events.
1140 */
1141 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001142 }
1143 mutex_unlock(&ctx->uring_lock);
1144}
1145
Jens Axboe2b2ed972019-10-25 10:06:15 -06001146static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1147 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001148{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001149 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001150
1151 do {
1152 int tmin = 0;
1153
Jens Axboe500f9fb2019-08-19 12:15:59 -06001154 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001155 * Don't enter poll loop if we already have events pending.
1156 * If we do, we can potentially be spinning for commands that
1157 * already triggered a CQE (eg in error).
1158 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001159 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001160 break;
1161
1162 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001163 * If a submit got punted to a workqueue, we can have the
1164 * application entering polling for a command before it gets
1165 * issued. That app will hold the uring_lock for the duration
1166 * of the poll right here, so we need to take a breather every
1167 * now and then to ensure that the issue has a chance to add
1168 * the poll to the issued list. Otherwise we can spin here
1169 * forever, while the workqueue is stuck trying to acquire the
1170 * very same mutex.
1171 */
1172 if (!(++iters & 7)) {
1173 mutex_unlock(&ctx->uring_lock);
1174 mutex_lock(&ctx->uring_lock);
1175 }
1176
Jens Axboedef596e2019-01-09 08:59:42 -07001177 if (*nr_events < min)
1178 tmin = min - *nr_events;
1179
1180 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1181 if (ret <= 0)
1182 break;
1183 ret = 0;
1184 } while (min && !*nr_events && !need_resched());
1185
Jens Axboe2b2ed972019-10-25 10:06:15 -06001186 return ret;
1187}
1188
1189static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1190 long min)
1191{
1192 int ret;
1193
1194 /*
1195 * We disallow the app entering submit/complete with polling, but we
1196 * still need to lock the ring to prevent racing with polled issue
1197 * that got punted to a workqueue.
1198 */
1199 mutex_lock(&ctx->uring_lock);
1200 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001201 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001202 return ret;
1203}
1204
Jens Axboe491381ce2019-10-17 09:20:46 -06001205static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001206{
Jens Axboe491381ce2019-10-17 09:20:46 -06001207 /*
1208 * Tell lockdep we inherited freeze protection from submission
1209 * thread.
1210 */
1211 if (req->flags & REQ_F_ISREG) {
1212 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001213
Jens Axboe491381ce2019-10-17 09:20:46 -06001214 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001215 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001216 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001217}
1218
Jens Axboeba816ad2019-09-28 11:36:45 -06001219static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001220{
1221 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1222
Jens Axboe491381ce2019-10-17 09:20:46 -06001223 if (kiocb->ki_flags & IOCB_WRITE)
1224 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001225
Jens Axboe9e645e112019-05-10 16:07:28 -06001226 if ((req->flags & REQ_F_LINK) && res != req->result)
1227 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001228 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001229}
1230
1231static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1232{
1233 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1234
1235 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001236 io_put_req(req);
Jens Axboeba816ad2019-09-28 11:36:45 -06001237}
1238
1239static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1240{
1241 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001242 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001243
1244 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001245 io_put_req_find_next(req, &nxt);
1246
1247 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001248}
1249
Jens Axboedef596e2019-01-09 08:59:42 -07001250static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1251{
1252 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1253
Jens Axboe491381ce2019-10-17 09:20:46 -06001254 if (kiocb->ki_flags & IOCB_WRITE)
1255 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001256
Jens Axboe9e645e112019-05-10 16:07:28 -06001257 if ((req->flags & REQ_F_LINK) && res != req->result)
1258 req->flags |= REQ_F_FAIL_LINK;
1259 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001260 if (res != -EAGAIN)
1261 req->flags |= REQ_F_IOPOLL_COMPLETED;
1262}
1263
1264/*
1265 * After the iocb has been issued, it's safe to be found on the poll list.
1266 * Adding the kiocb to the list AFTER submission ensures that we don't
1267 * find it from a io_iopoll_getevents() thread before the issuer is done
1268 * accessing the kiocb cookie.
1269 */
1270static void io_iopoll_req_issued(struct io_kiocb *req)
1271{
1272 struct io_ring_ctx *ctx = req->ctx;
1273
1274 /*
1275 * Track whether we have multiple files in our lists. This will impact
1276 * how we do polling eventually, not spinning if we're on potentially
1277 * different devices.
1278 */
1279 if (list_empty(&ctx->poll_list)) {
1280 ctx->poll_multi_file = false;
1281 } else if (!ctx->poll_multi_file) {
1282 struct io_kiocb *list_req;
1283
1284 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1285 list);
1286 if (list_req->rw.ki_filp != req->rw.ki_filp)
1287 ctx->poll_multi_file = true;
1288 }
1289
1290 /*
1291 * For fast devices, IO may have already completed. If it has, add
1292 * it to the front so we find it first.
1293 */
1294 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1295 list_add(&req->list, &ctx->poll_list);
1296 else
1297 list_add_tail(&req->list, &ctx->poll_list);
1298}
1299
Jens Axboe3d6770f2019-04-13 11:50:54 -06001300static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001301{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001302 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001303 int diff = state->has_refs - state->used_refs;
1304
1305 if (diff)
1306 fput_many(state->file, diff);
1307 state->file = NULL;
1308 }
1309}
1310
1311/*
1312 * Get as many references to a file as we have IOs left in this submission,
1313 * assuming most submissions are for one file, or at least that each file
1314 * has more than one submission.
1315 */
1316static struct file *io_file_get(struct io_submit_state *state, int fd)
1317{
1318 if (!state)
1319 return fget(fd);
1320
1321 if (state->file) {
1322 if (state->fd == fd) {
1323 state->used_refs++;
1324 state->ios_left--;
1325 return state->file;
1326 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001327 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001328 }
1329 state->file = fget_many(fd, state->ios_left);
1330 if (!state->file)
1331 return NULL;
1332
1333 state->fd = fd;
1334 state->has_refs = state->ios_left;
1335 state->used_refs = 1;
1336 state->ios_left--;
1337 return state->file;
1338}
1339
Jens Axboe2b188cc2019-01-07 10:46:33 -07001340/*
1341 * If we tracked the file through the SCM inflight mechanism, we could support
1342 * any file. For now, just ensure that anything potentially problematic is done
1343 * inline.
1344 */
1345static bool io_file_supports_async(struct file *file)
1346{
1347 umode_t mode = file_inode(file)->i_mode;
1348
1349 if (S_ISBLK(mode) || S_ISCHR(mode))
1350 return true;
1351 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1352 return true;
1353
1354 return false;
1355}
1356
Pavel Begunkov267bc902019-11-07 01:41:08 +03001357static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001358{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001359 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001360 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001361 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001362 unsigned ioprio;
1363 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364
Jens Axboe09bb8392019-03-13 12:39:28 -06001365 if (!req->file)
1366 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001367
Jens Axboe491381ce2019-10-17 09:20:46 -06001368 if (S_ISREG(file_inode(req->file)->i_mode))
1369 req->flags |= REQ_F_ISREG;
1370
1371 /*
1372 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1373 * we know to async punt it even if it was opened O_NONBLOCK
1374 */
1375 if (force_nonblock && !io_file_supports_async(req->file)) {
1376 req->flags |= REQ_F_MUST_PUNT;
1377 return -EAGAIN;
1378 }
Jens Axboe6b063142019-01-10 22:13:58 -07001379
Jens Axboe2b188cc2019-01-07 10:46:33 -07001380 kiocb->ki_pos = READ_ONCE(sqe->off);
1381 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1382 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1383
1384 ioprio = READ_ONCE(sqe->ioprio);
1385 if (ioprio) {
1386 ret = ioprio_check_cap(ioprio);
1387 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001388 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001389
1390 kiocb->ki_ioprio = ioprio;
1391 } else
1392 kiocb->ki_ioprio = get_current_ioprio();
1393
1394 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1395 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001396 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001397
1398 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001399 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1400 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001401 req->flags |= REQ_F_NOWAIT;
1402
1403 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001404 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001405
Jens Axboedef596e2019-01-09 08:59:42 -07001406 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001407 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1408 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001409 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001410
Jens Axboedef596e2019-01-09 08:59:42 -07001411 kiocb->ki_flags |= IOCB_HIPRI;
1412 kiocb->ki_complete = io_complete_rw_iopoll;
1413 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001414 if (kiocb->ki_flags & IOCB_HIPRI)
1415 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001416 kiocb->ki_complete = io_complete_rw;
1417 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001418 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001419}
1420
1421static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1422{
1423 switch (ret) {
1424 case -EIOCBQUEUED:
1425 break;
1426 case -ERESTARTSYS:
1427 case -ERESTARTNOINTR:
1428 case -ERESTARTNOHAND:
1429 case -ERESTART_RESTARTBLOCK:
1430 /*
1431 * We can't just restart the syscall, since previously
1432 * submitted sqes may already be in progress. Just fail this
1433 * IO with EINTR.
1434 */
1435 ret = -EINTR;
1436 /* fall through */
1437 default:
1438 kiocb->ki_complete(kiocb, ret, 0);
1439 }
1440}
1441
Jens Axboeba816ad2019-09-28 11:36:45 -06001442static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1443 bool in_async)
1444{
1445 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1446 *nxt = __io_complete_rw(kiocb, ret);
1447 else
1448 io_rw_done(kiocb, ret);
1449}
1450
Jens Axboeedafcce2019-01-09 09:16:05 -07001451static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1452 const struct io_uring_sqe *sqe,
1453 struct iov_iter *iter)
1454{
1455 size_t len = READ_ONCE(sqe->len);
1456 struct io_mapped_ubuf *imu;
1457 unsigned index, buf_index;
1458 size_t offset;
1459 u64 buf_addr;
1460
1461 /* attempt to use fixed buffers without having provided iovecs */
1462 if (unlikely(!ctx->user_bufs))
1463 return -EFAULT;
1464
1465 buf_index = READ_ONCE(sqe->buf_index);
1466 if (unlikely(buf_index >= ctx->nr_user_bufs))
1467 return -EFAULT;
1468
1469 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1470 imu = &ctx->user_bufs[index];
1471 buf_addr = READ_ONCE(sqe->addr);
1472
1473 /* overflow */
1474 if (buf_addr + len < buf_addr)
1475 return -EFAULT;
1476 /* not inside the mapped region */
1477 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1478 return -EFAULT;
1479
1480 /*
1481 * May not be a start of buffer, set size appropriately
1482 * and advance us to the beginning.
1483 */
1484 offset = buf_addr - imu->ubuf;
1485 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001486
1487 if (offset) {
1488 /*
1489 * Don't use iov_iter_advance() here, as it's really slow for
1490 * using the latter parts of a big fixed buffer - it iterates
1491 * over each segment manually. We can cheat a bit here, because
1492 * we know that:
1493 *
1494 * 1) it's a BVEC iter, we set it up
1495 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1496 * first and last bvec
1497 *
1498 * So just find our index, and adjust the iterator afterwards.
1499 * If the offset is within the first bvec (or the whole first
1500 * bvec, just use iov_iter_advance(). This makes it easier
1501 * since we can just skip the first segment, which may not
1502 * be PAGE_SIZE aligned.
1503 */
1504 const struct bio_vec *bvec = imu->bvec;
1505
1506 if (offset <= bvec->bv_len) {
1507 iov_iter_advance(iter, offset);
1508 } else {
1509 unsigned long seg_skip;
1510
1511 /* skip first vec */
1512 offset -= bvec->bv_len;
1513 seg_skip = 1 + (offset >> PAGE_SHIFT);
1514
1515 iter->bvec = bvec + seg_skip;
1516 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001517 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001518 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001519 }
1520 }
1521
Jens Axboeedafcce2019-01-09 09:16:05 -07001522 return 0;
1523}
1524
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001525static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1526 const struct sqe_submit *s, struct iovec **iovec,
1527 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001528{
1529 const struct io_uring_sqe *sqe = s->sqe;
1530 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1531 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001532 u8 opcode;
1533
1534 /*
1535 * We're reading ->opcode for the second time, but the first read
1536 * doesn't care whether it's _FIXED or not, so it doesn't matter
1537 * whether ->opcode changes concurrently. The first read does care
1538 * about whether it is a READ or a WRITE, so we don't trust this read
1539 * for that purpose and instead let the caller pass in the read/write
1540 * flag.
1541 */
1542 opcode = READ_ONCE(sqe->opcode);
1543 if (opcode == IORING_OP_READ_FIXED ||
1544 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001545 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001546 *iovec = NULL;
1547 return ret;
1548 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001549
1550 if (!s->has_user)
1551 return -EFAULT;
1552
1553#ifdef CONFIG_COMPAT
1554 if (ctx->compat)
1555 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1556 iovec, iter);
1557#endif
1558
1559 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1560}
1561
Jens Axboe32960612019-09-23 11:05:34 -06001562/*
1563 * For files that don't have ->read_iter() and ->write_iter(), handle them
1564 * by looping over ->read() or ->write() manually.
1565 */
1566static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1567 struct iov_iter *iter)
1568{
1569 ssize_t ret = 0;
1570
1571 /*
1572 * Don't support polled IO through this interface, and we can't
1573 * support non-blocking either. For the latter, this just causes
1574 * the kiocb to be handled from an async context.
1575 */
1576 if (kiocb->ki_flags & IOCB_HIPRI)
1577 return -EOPNOTSUPP;
1578 if (kiocb->ki_flags & IOCB_NOWAIT)
1579 return -EAGAIN;
1580
1581 while (iov_iter_count(iter)) {
1582 struct iovec iovec = iov_iter_iovec(iter);
1583 ssize_t nr;
1584
1585 if (rw == READ) {
1586 nr = file->f_op->read(file, iovec.iov_base,
1587 iovec.iov_len, &kiocb->ki_pos);
1588 } else {
1589 nr = file->f_op->write(file, iovec.iov_base,
1590 iovec.iov_len, &kiocb->ki_pos);
1591 }
1592
1593 if (nr < 0) {
1594 if (!ret)
1595 ret = nr;
1596 break;
1597 }
1598 ret += nr;
1599 if (nr != iovec.iov_len)
1600 break;
1601 iov_iter_advance(iter, nr);
1602 }
1603
1604 return ret;
1605}
1606
Pavel Begunkov267bc902019-11-07 01:41:08 +03001607static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1608 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001609{
1610 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1611 struct kiocb *kiocb = &req->rw;
1612 struct iov_iter iter;
1613 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001614 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001615 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001616
Pavel Begunkov267bc902019-11-07 01:41:08 +03001617 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001618 if (ret)
1619 return ret;
1620 file = kiocb->ki_filp;
1621
Jens Axboe2b188cc2019-01-07 10:46:33 -07001622 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001623 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001624
Pavel Begunkov267bc902019-11-07 01:41:08 +03001625 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001626 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001627 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001628
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001629 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001630 if (req->flags & REQ_F_LINK)
1631 req->result = read_size;
1632
Jens Axboe31b51512019-01-18 22:56:34 -07001633 iov_count = iov_iter_count(&iter);
1634 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001635 if (!ret) {
1636 ssize_t ret2;
1637
Jens Axboe32960612019-09-23 11:05:34 -06001638 if (file->f_op->read_iter)
1639 ret2 = call_read_iter(file, kiocb, &iter);
1640 else
1641 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1642
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001643 /*
1644 * In case of a short read, punt to async. This can happen
1645 * if we have data partially cached. Alternatively we can
1646 * return the short read, in which case the application will
1647 * need to issue another SQE and wait for it. That SQE will
1648 * need async punt anyway, so it's more efficient to do it
1649 * here.
1650 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001651 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1652 (req->flags & REQ_F_ISREG) &&
1653 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001654 ret2 = -EAGAIN;
1655 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001656 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001657 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001658 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001659 ret = -EAGAIN;
1660 }
1661 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001662 return ret;
1663}
1664
Pavel Begunkov267bc902019-11-07 01:41:08 +03001665static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1666 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001667{
1668 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1669 struct kiocb *kiocb = &req->rw;
1670 struct iov_iter iter;
1671 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001672 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001673 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674
Pavel Begunkov267bc902019-11-07 01:41:08 +03001675 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001676 if (ret)
1677 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001678
Jens Axboe2b188cc2019-01-07 10:46:33 -07001679 file = kiocb->ki_filp;
1680 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001681 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001682
Pavel Begunkov267bc902019-11-07 01:41:08 +03001683 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001684 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001685 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001686
Jens Axboe9e645e112019-05-10 16:07:28 -06001687 if (req->flags & REQ_F_LINK)
1688 req->result = ret;
1689
Jens Axboe31b51512019-01-18 22:56:34 -07001690 iov_count = iov_iter_count(&iter);
1691
1692 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001693 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001694 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001695
1696 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001697 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001698 ssize_t ret2;
1699
Jens Axboe2b188cc2019-01-07 10:46:33 -07001700 /*
1701 * Open-code file_start_write here to grab freeze protection,
1702 * which will be released by another thread in
1703 * io_complete_rw(). Fool lockdep by telling it the lock got
1704 * released so that it doesn't complain about the held lock when
1705 * we return to userspace.
1706 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001707 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708 __sb_start_write(file_inode(file)->i_sb,
1709 SB_FREEZE_WRITE, true);
1710 __sb_writers_release(file_inode(file)->i_sb,
1711 SB_FREEZE_WRITE);
1712 }
1713 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001714
Jens Axboe32960612019-09-23 11:05:34 -06001715 if (file->f_op->write_iter)
1716 ret2 = call_write_iter(file, kiocb, &iter);
1717 else
1718 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001719 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001720 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001721 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001722 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001723 }
Jens Axboe31b51512019-01-18 22:56:34 -07001724out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001725 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001726 return ret;
1727}
1728
1729/*
1730 * IORING_OP_NOP just posts a completion event, nothing else.
1731 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001732static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001733{
1734 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735
Jens Axboedef596e2019-01-09 08:59:42 -07001736 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1737 return -EINVAL;
1738
Jens Axboe78e19bb2019-11-06 15:21:34 -07001739 io_cqring_add_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001740 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001741 return 0;
1742}
1743
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001744static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1745{
Jens Axboe6b063142019-01-10 22:13:58 -07001746 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001747
Jens Axboe09bb8392019-03-13 12:39:28 -06001748 if (!req->file)
1749 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001750
Jens Axboe6b063142019-01-10 22:13:58 -07001751 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001752 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001753 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001754 return -EINVAL;
1755
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001756 return 0;
1757}
1758
1759static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001760 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001761{
1762 loff_t sqe_off = READ_ONCE(sqe->off);
1763 loff_t sqe_len = READ_ONCE(sqe->len);
1764 loff_t end = sqe_off + sqe_len;
1765 unsigned fsync_flags;
1766 int ret;
1767
1768 fsync_flags = READ_ONCE(sqe->fsync_flags);
1769 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1770 return -EINVAL;
1771
1772 ret = io_prep_fsync(req, sqe);
1773 if (ret)
1774 return ret;
1775
1776 /* fsync always requires a blocking context */
1777 if (force_nonblock)
1778 return -EAGAIN;
1779
1780 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1781 end > 0 ? end : LLONG_MAX,
1782 fsync_flags & IORING_FSYNC_DATASYNC);
1783
Jens Axboe9e645e112019-05-10 16:07:28 -06001784 if (ret < 0 && (req->flags & REQ_F_LINK))
1785 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001786 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001787 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001788 return 0;
1789}
1790
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001791static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1792{
1793 struct io_ring_ctx *ctx = req->ctx;
1794 int ret = 0;
1795
1796 if (!req->file)
1797 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001798
1799 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1800 return -EINVAL;
1801 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1802 return -EINVAL;
1803
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001804 return ret;
1805}
1806
1807static int io_sync_file_range(struct io_kiocb *req,
1808 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001809 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001810 bool force_nonblock)
1811{
1812 loff_t sqe_off;
1813 loff_t sqe_len;
1814 unsigned flags;
1815 int ret;
1816
1817 ret = io_prep_sfr(req, sqe);
1818 if (ret)
1819 return ret;
1820
1821 /* sync_file_range always requires a blocking context */
1822 if (force_nonblock)
1823 return -EAGAIN;
1824
1825 sqe_off = READ_ONCE(sqe->off);
1826 sqe_len = READ_ONCE(sqe->len);
1827 flags = READ_ONCE(sqe->sync_range_flags);
1828
1829 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1830
Jens Axboe9e645e112019-05-10 16:07:28 -06001831 if (ret < 0 && (req->flags & REQ_F_LINK))
1832 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001833 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001834 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001835 return 0;
1836}
1837
Jens Axboe0fa03c62019-04-19 13:34:07 -06001838#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001839static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001840 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001841 long (*fn)(struct socket *, struct user_msghdr __user *,
1842 unsigned int))
1843{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001844 struct socket *sock;
1845 int ret;
1846
1847 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1848 return -EINVAL;
1849
1850 sock = sock_from_file(req->file, &ret);
1851 if (sock) {
1852 struct user_msghdr __user *msg;
1853 unsigned flags;
1854
1855 flags = READ_ONCE(sqe->msg_flags);
1856 if (flags & MSG_DONTWAIT)
1857 req->flags |= REQ_F_NOWAIT;
1858 else if (force_nonblock)
1859 flags |= MSG_DONTWAIT;
1860
1861 msg = (struct user_msghdr __user *) (unsigned long)
1862 READ_ONCE(sqe->addr);
1863
Jens Axboeaa1fa282019-04-19 13:38:09 -06001864 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001865 if (force_nonblock && ret == -EAGAIN)
1866 return ret;
1867 }
1868
Jens Axboe78e19bb2019-11-06 15:21:34 -07001869 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001870 if (ret < 0 && (req->flags & REQ_F_LINK))
1871 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001872 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001873 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001874}
1875#endif
1876
1877static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001878 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001879{
1880#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001881 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1882 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001883#else
1884 return -EOPNOTSUPP;
1885#endif
1886}
1887
1888static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001889 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001890{
1891#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001892 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1893 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001894#else
1895 return -EOPNOTSUPP;
1896#endif
1897}
1898
Jens Axboe17f2fe32019-10-17 14:42:58 -06001899static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1900 struct io_kiocb **nxt, bool force_nonblock)
1901{
1902#if defined(CONFIG_NET)
1903 struct sockaddr __user *addr;
1904 int __user *addr_len;
1905 unsigned file_flags;
1906 int flags, ret;
1907
1908 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1909 return -EINVAL;
1910 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1911 return -EINVAL;
1912
1913 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1914 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1915 flags = READ_ONCE(sqe->accept_flags);
1916 file_flags = force_nonblock ? O_NONBLOCK : 0;
1917
1918 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1919 if (ret == -EAGAIN && force_nonblock) {
1920 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1921 return -EAGAIN;
1922 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001923 if (ret == -ERESTARTSYS)
1924 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001925 if (ret < 0 && (req->flags & REQ_F_LINK))
1926 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001927 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001928 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001929 return 0;
1930#else
1931 return -EOPNOTSUPP;
1932#endif
1933}
1934
Jens Axboe221c5eb2019-01-17 09:41:58 -07001935static void io_poll_remove_one(struct io_kiocb *req)
1936{
1937 struct io_poll_iocb *poll = &req->poll;
1938
1939 spin_lock(&poll->head->lock);
1940 WRITE_ONCE(poll->canceled, true);
1941 if (!list_empty(&poll->wait.entry)) {
1942 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07001943 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001944 }
1945 spin_unlock(&poll->head->lock);
1946
1947 list_del_init(&req->list);
1948}
1949
1950static void io_poll_remove_all(struct io_ring_ctx *ctx)
1951{
1952 struct io_kiocb *req;
1953
1954 spin_lock_irq(&ctx->completion_lock);
1955 while (!list_empty(&ctx->cancel_list)) {
1956 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1957 io_poll_remove_one(req);
1958 }
1959 spin_unlock_irq(&ctx->completion_lock);
1960}
1961
Jens Axboe47f46762019-11-09 17:43:02 -07001962static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
1963{
1964 struct io_kiocb *req;
1965
1966 list_for_each_entry(req, &ctx->cancel_list, list) {
1967 if (req->user_data != sqe_addr)
1968 continue;
1969 io_poll_remove_one(req);
1970 return 0;
1971 }
1972
1973 return -ENOENT;
1974}
1975
Jens Axboe221c5eb2019-01-17 09:41:58 -07001976/*
1977 * Find a running poll command that matches one specified in sqe->addr,
1978 * and remove it if found.
1979 */
1980static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1981{
1982 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07001983 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001984
1985 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1986 return -EINVAL;
1987 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1988 sqe->poll_events)
1989 return -EINVAL;
1990
1991 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07001992 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07001993 spin_unlock_irq(&ctx->completion_lock);
1994
Jens Axboe78e19bb2019-11-06 15:21:34 -07001995 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001996 if (ret < 0 && (req->flags & REQ_F_LINK))
1997 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001998 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001999 return 0;
2000}
2001
Jackie Liua197f662019-11-08 08:09:12 -07002002static void io_poll_complete(struct io_kiocb *req, __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002003{
Jackie Liua197f662019-11-08 08:09:12 -07002004 struct io_ring_ctx *ctx = req->ctx;
2005
Jens Axboe8c838782019-03-12 15:48:16 -06002006 req->poll.done = true;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002007 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002008 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002009}
2010
Jens Axboe561fb042019-10-24 07:25:42 -06002011static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002012{
Jens Axboe561fb042019-10-24 07:25:42 -06002013 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002014 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2015 struct io_poll_iocb *poll = &req->poll;
2016 struct poll_table_struct pt = { ._key = poll->events };
2017 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002018 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002019 __poll_t mask = 0;
2020
Jens Axboe561fb042019-10-24 07:25:42 -06002021 if (work->flags & IO_WQ_WORK_CANCEL)
2022 WRITE_ONCE(poll->canceled, true);
2023
Jens Axboe221c5eb2019-01-17 09:41:58 -07002024 if (!READ_ONCE(poll->canceled))
2025 mask = vfs_poll(poll->file, &pt) & poll->events;
2026
2027 /*
2028 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2029 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2030 * synchronize with them. In the cancellation case the list_del_init
2031 * itself is not actually needed, but harmless so we keep it in to
2032 * avoid further branches in the fast path.
2033 */
2034 spin_lock_irq(&ctx->completion_lock);
2035 if (!mask && !READ_ONCE(poll->canceled)) {
2036 add_wait_queue(poll->head, &poll->wait);
2037 spin_unlock_irq(&ctx->completion_lock);
2038 return;
2039 }
2040 list_del_init(&req->list);
Jackie Liua197f662019-11-08 08:09:12 -07002041 io_poll_complete(req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002042 spin_unlock_irq(&ctx->completion_lock);
2043
Jens Axboe8c838782019-03-12 15:48:16 -06002044 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002045
Jackie Liuec9c02a2019-11-08 23:50:36 +08002046 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002047 if (nxt)
2048 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002049}
2050
2051static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2052 void *key)
2053{
2054 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2055 wait);
2056 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2057 struct io_ring_ctx *ctx = req->ctx;
2058 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002059 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002060
2061 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002062 if (mask && !(mask & poll->events))
2063 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002064
2065 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002066
2067 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
2068 list_del(&req->list);
Jackie Liua197f662019-11-08 08:09:12 -07002069 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06002070 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2071
2072 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002073 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002074 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002075 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002076 }
2077
Jens Axboe221c5eb2019-01-17 09:41:58 -07002078 return 1;
2079}
2080
2081struct io_poll_table {
2082 struct poll_table_struct pt;
2083 struct io_kiocb *req;
2084 int error;
2085};
2086
2087static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2088 struct poll_table_struct *p)
2089{
2090 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2091
2092 if (unlikely(pt->req->poll.head)) {
2093 pt->error = -EINVAL;
2094 return;
2095 }
2096
2097 pt->error = 0;
2098 pt->req->poll.head = head;
2099 add_wait_queue(head, &pt->req->poll.wait);
2100}
2101
Jens Axboe89723d02019-11-05 15:32:58 -07002102static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2103 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002104{
2105 struct io_poll_iocb *poll = &req->poll;
2106 struct io_ring_ctx *ctx = req->ctx;
2107 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002108 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002109 __poll_t mask;
2110 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002111
2112 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2113 return -EINVAL;
2114 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2115 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002116 if (!poll->file)
2117 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002118
Jens Axboe6cc47d12019-09-18 11:18:23 -06002119 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002120 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002121 events = READ_ONCE(sqe->poll_events);
2122 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
2123
Jens Axboe221c5eb2019-01-17 09:41:58 -07002124 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002125 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002126 poll->canceled = false;
2127
2128 ipt.pt._qproc = io_poll_queue_proc;
2129 ipt.pt._key = poll->events;
2130 ipt.req = req;
2131 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2132
2133 /* initialized the list so that we can do list_empty checks */
2134 INIT_LIST_HEAD(&poll->wait.entry);
2135 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2136
Jens Axboe36703242019-07-25 10:20:18 -06002137 INIT_LIST_HEAD(&req->list);
2138
Jens Axboe221c5eb2019-01-17 09:41:58 -07002139 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002140
2141 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002142 if (likely(poll->head)) {
2143 spin_lock(&poll->head->lock);
2144 if (unlikely(list_empty(&poll->wait.entry))) {
2145 if (ipt.error)
2146 cancel = true;
2147 ipt.error = 0;
2148 mask = 0;
2149 }
2150 if (mask || ipt.error)
2151 list_del_init(&poll->wait.entry);
2152 else if (cancel)
2153 WRITE_ONCE(poll->canceled, true);
2154 else if (!poll->done) /* actually waiting for an event */
2155 list_add_tail(&req->list, &ctx->cancel_list);
2156 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002157 }
Jens Axboe8c838782019-03-12 15:48:16 -06002158 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002159 ipt.error = 0;
Jackie Liua197f662019-11-08 08:09:12 -07002160 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06002161 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002162 spin_unlock_irq(&ctx->completion_lock);
2163
Jens Axboe8c838782019-03-12 15:48:16 -06002164 if (mask) {
2165 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002166 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002167 }
Jens Axboe8c838782019-03-12 15:48:16 -06002168 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002169}
2170
Jens Axboe5262f562019-09-17 12:26:57 -06002171static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2172{
2173 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002174 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06002175 unsigned long flags;
2176
2177 req = container_of(timer, struct io_kiocb, timeout.timer);
2178 ctx = req->ctx;
2179 atomic_inc(&ctx->cq_timeouts);
2180
2181 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002182 /*
Jens Axboe11365042019-10-16 09:08:32 -06002183 * We could be racing with timeout deletion. If the list is empty,
2184 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002185 */
Jens Axboe842f9612019-10-29 12:34:10 -06002186 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002187 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002188
Jens Axboe11365042019-10-16 09:08:32 -06002189 /*
2190 * Adjust the reqs sequence before the current one because it
2191 * will consume a slot in the cq_ring and the the cq_tail
2192 * pointer will be increased, otherwise other timeout reqs may
2193 * return in advance without waiting for enough wait_nr.
2194 */
2195 prev = req;
2196 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2197 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002198 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002199 }
Jens Axboe842f9612019-10-29 12:34:10 -06002200
Jens Axboe78e19bb2019-11-06 15:21:34 -07002201 io_cqring_fill_event(req, -ETIME);
Jens Axboe842f9612019-10-29 12:34:10 -06002202 io_commit_cqring(ctx);
Jens Axboe5262f562019-09-17 12:26:57 -06002203 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2204
Jens Axboe842f9612019-10-29 12:34:10 -06002205 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002206 if (req->flags & REQ_F_LINK)
2207 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002208 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002209 return HRTIMER_NORESTART;
2210}
2211
Jens Axboe47f46762019-11-09 17:43:02 -07002212static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2213{
2214 struct io_kiocb *req;
2215 int ret = -ENOENT;
2216
2217 list_for_each_entry(req, &ctx->timeout_list, list) {
2218 if (user_data == req->user_data) {
2219 list_del_init(&req->list);
2220 ret = 0;
2221 break;
2222 }
2223 }
2224
2225 if (ret == -ENOENT)
2226 return ret;
2227
2228 ret = hrtimer_try_to_cancel(&req->timeout.timer);
2229 if (ret == -1)
2230 return -EALREADY;
2231
2232 io_cqring_fill_event(req, -ECANCELED);
2233 io_put_req(req);
2234 return 0;
2235}
2236
Jens Axboe11365042019-10-16 09:08:32 -06002237/*
2238 * Remove or update an existing timeout command
2239 */
2240static int io_timeout_remove(struct io_kiocb *req,
2241 const struct io_uring_sqe *sqe)
2242{
2243 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002244 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002245 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002246
2247 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2248 return -EINVAL;
2249 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2250 return -EINVAL;
2251 flags = READ_ONCE(sqe->timeout_flags);
2252 if (flags)
2253 return -EINVAL;
2254
Jens Axboe11365042019-10-16 09:08:32 -06002255 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002256 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002257
Jens Axboe47f46762019-11-09 17:43:02 -07002258 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002259 io_commit_cqring(ctx);
2260 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002261 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002262 if (ret < 0 && req->flags & REQ_F_LINK)
2263 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002264 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002265 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002266}
2267
2268static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2269{
yangerkun5da0fb12019-10-15 21:59:29 +08002270 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002271 struct io_ring_ctx *ctx = req->ctx;
2272 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002273 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002274 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002275 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002276 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002277
2278 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2279 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002280 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2281 return -EINVAL;
2282 flags = READ_ONCE(sqe->timeout_flags);
2283 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002284 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002285
2286 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002287 return -EFAULT;
2288
Jens Axboe11365042019-10-16 09:08:32 -06002289 if (flags & IORING_TIMEOUT_ABS)
2290 mode = HRTIMER_MODE_ABS;
2291 else
2292 mode = HRTIMER_MODE_REL;
2293
2294 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2295
Jens Axboe5262f562019-09-17 12:26:57 -06002296 /*
2297 * sqe->off holds how many events that need to occur for this
2298 * timeout event to be satisfied.
2299 */
2300 count = READ_ONCE(sqe->off);
2301 if (!count)
2302 count = 1;
2303
2304 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002305 /* reuse it to store the count */
2306 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002307 req->flags |= REQ_F_TIMEOUT;
2308
2309 /*
2310 * Insertion sort, ensuring the first entry in the list is always
2311 * the one we need first.
2312 */
Jens Axboe5262f562019-09-17 12:26:57 -06002313 spin_lock_irq(&ctx->completion_lock);
2314 list_for_each_prev(entry, &ctx->timeout_list) {
2315 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002316 unsigned nxt_sq_head;
2317 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002318
yangerkun5da0fb12019-10-15 21:59:29 +08002319 /*
2320 * Since cached_sq_head + count - 1 can overflow, use type long
2321 * long to store it.
2322 */
2323 tmp = (long long)ctx->cached_sq_head + count - 1;
2324 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2325 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2326
2327 /*
2328 * cached_sq_head may overflow, and it will never overflow twice
2329 * once there is some timeout req still be valid.
2330 */
2331 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002332 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002333
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002334 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002335 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002336
2337 /*
2338 * Sequence of reqs after the insert one and itself should
2339 * be adjusted because each timeout req consumes a slot.
2340 */
2341 span++;
2342 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002343 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002344 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002345 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002346 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002347 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002348 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002349 return 0;
2350}
2351
Jens Axboe62755e32019-10-28 21:49:21 -06002352static bool io_cancel_cb(struct io_wq_work *work, void *data)
2353{
2354 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2355
2356 return req->user_data == (unsigned long) data;
2357}
2358
Jens Axboee977d6d2019-11-05 12:39:45 -07002359static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002360{
Jens Axboe62755e32019-10-28 21:49:21 -06002361 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002362 int ret = 0;
2363
Jens Axboe62755e32019-10-28 21:49:21 -06002364 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2365 switch (cancel_ret) {
2366 case IO_WQ_CANCEL_OK:
2367 ret = 0;
2368 break;
2369 case IO_WQ_CANCEL_RUNNING:
2370 ret = -EALREADY;
2371 break;
2372 case IO_WQ_CANCEL_NOTFOUND:
2373 ret = -ENOENT;
2374 break;
2375 }
2376
Jens Axboee977d6d2019-11-05 12:39:45 -07002377 return ret;
2378}
2379
Jens Axboe47f46762019-11-09 17:43:02 -07002380static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2381 struct io_kiocb *req, __u64 sqe_addr,
2382 struct io_kiocb **nxt)
2383{
2384 unsigned long flags;
2385 int ret;
2386
2387 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2388 if (ret != -ENOENT) {
2389 spin_lock_irqsave(&ctx->completion_lock, flags);
2390 goto done;
2391 }
2392
2393 spin_lock_irqsave(&ctx->completion_lock, flags);
2394 ret = io_timeout_cancel(ctx, sqe_addr);
2395 if (ret != -ENOENT)
2396 goto done;
2397 ret = io_poll_cancel(ctx, sqe_addr);
2398done:
2399 io_cqring_fill_event(req, ret);
2400 io_commit_cqring(ctx);
2401 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2402 io_cqring_ev_posted(ctx);
2403
2404 if (ret < 0 && (req->flags & REQ_F_LINK))
2405 req->flags |= REQ_F_FAIL_LINK;
2406 io_put_req_find_next(req, nxt);
2407}
2408
Jens Axboee977d6d2019-11-05 12:39:45 -07002409static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2410 struct io_kiocb **nxt)
2411{
2412 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002413
2414 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2415 return -EINVAL;
2416 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2417 sqe->cancel_flags)
2418 return -EINVAL;
2419
Jens Axboe47f46762019-11-09 17:43:02 -07002420 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), NULL);
Jens Axboe62755e32019-10-28 21:49:21 -06002421 return 0;
2422}
2423
Jackie Liua197f662019-11-08 08:09:12 -07002424static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002425{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002426 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002427 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002428 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002429
Jackie Liua197f662019-11-08 08:09:12 -07002430 if (!io_sequence_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002431 return 0;
2432
2433 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2434 if (!sqe_copy)
2435 return -EAGAIN;
2436
2437 spin_lock_irq(&ctx->completion_lock);
Jackie Liua197f662019-11-08 08:09:12 -07002438 if (!io_sequence_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002439 spin_unlock_irq(&ctx->completion_lock);
2440 kfree(sqe_copy);
2441 return 0;
2442 }
2443
2444 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2445 req->submit.sqe = sqe_copy;
2446
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002447 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002448 list_add_tail(&req->list, &ctx->defer_list);
2449 spin_unlock_irq(&ctx->completion_lock);
2450 return -EIOCBQUEUED;
2451}
2452
Jackie Liua197f662019-11-08 08:09:12 -07002453static int __io_submit_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2454 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002455{
Jens Axboee0c5c572019-03-12 10:18:47 -06002456 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002457 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002458 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002459
Jens Axboe2b188cc2019-01-07 10:46:33 -07002460 opcode = READ_ONCE(s->sqe->opcode);
2461 switch (opcode) {
2462 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002463 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002464 break;
2465 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002466 if (unlikely(s->sqe->buf_index))
2467 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002468 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002469 break;
2470 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002471 if (unlikely(s->sqe->buf_index))
2472 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002473 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002474 break;
2475 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002476 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002477 break;
2478 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002479 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002480 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002481 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002482 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002483 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002484 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002485 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002486 break;
2487 case IORING_OP_POLL_REMOVE:
2488 ret = io_poll_remove(req, s->sqe);
2489 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002490 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002491 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002492 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002493 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002494 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002495 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002496 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002497 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002498 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002499 case IORING_OP_TIMEOUT:
2500 ret = io_timeout(req, s->sqe);
2501 break;
Jens Axboe11365042019-10-16 09:08:32 -06002502 case IORING_OP_TIMEOUT_REMOVE:
2503 ret = io_timeout_remove(req, s->sqe);
2504 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002505 case IORING_OP_ACCEPT:
2506 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2507 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002508 case IORING_OP_ASYNC_CANCEL:
2509 ret = io_async_cancel(req, s->sqe, nxt);
2510 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002511 default:
2512 ret = -EINVAL;
2513 break;
2514 }
2515
Jens Axboedef596e2019-01-09 08:59:42 -07002516 if (ret)
2517 return ret;
2518
2519 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002520 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002521 return -EAGAIN;
2522
2523 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002524 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002525 mutex_lock(&ctx->uring_lock);
2526 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002527 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002528 mutex_unlock(&ctx->uring_lock);
2529 }
2530
2531 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002532}
2533
Jens Axboe561fb042019-10-24 07:25:42 -06002534static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002535{
Jens Axboe561fb042019-10-24 07:25:42 -06002536 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002537 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002538 struct sqe_submit *s = &req->submit;
2539 const struct io_uring_sqe *sqe = s->sqe;
2540 struct io_kiocb *nxt = NULL;
2541 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002542
Jens Axboe561fb042019-10-24 07:25:42 -06002543 /* Ensure we clear previously set non-block flag */
2544 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002545
Jens Axboe561fb042019-10-24 07:25:42 -06002546 if (work->flags & IO_WQ_WORK_CANCEL)
2547 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002548
Jens Axboe561fb042019-10-24 07:25:42 -06002549 if (!ret) {
2550 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2551 s->in_async = true;
2552 do {
Jackie Liua197f662019-11-08 08:09:12 -07002553 ret = __io_submit_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002554 /*
2555 * We can get EAGAIN for polled IO even though we're
2556 * forcing a sync submission from here, since we can't
2557 * wait for request slots on the block side.
2558 */
2559 if (ret != -EAGAIN)
2560 break;
2561 cond_resched();
2562 } while (1);
2563 }
Jens Axboe31b51512019-01-18 22:56:34 -07002564
Jens Axboe561fb042019-10-24 07:25:42 -06002565 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002566 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002567
Jens Axboe561fb042019-10-24 07:25:42 -06002568 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002569 if (req->flags & REQ_F_LINK)
2570 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002571 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002572 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002573 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002574
Jens Axboe561fb042019-10-24 07:25:42 -06002575 /* async context always use a copy of the sqe */
2576 kfree(sqe);
2577
2578 /* if a dependent link is ready, pass it back */
2579 if (!ret && nxt) {
2580 io_prep_async_work(nxt);
2581 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002582 }
Jens Axboe31b51512019-01-18 22:56:34 -07002583}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002584
Jens Axboe09bb8392019-03-13 12:39:28 -06002585static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2586{
2587 int op = READ_ONCE(sqe->opcode);
2588
2589 switch (op) {
2590 case IORING_OP_NOP:
2591 case IORING_OP_POLL_REMOVE:
2592 return false;
2593 default:
2594 return true;
2595 }
2596}
2597
Jens Axboe65e19f52019-10-26 07:20:21 -06002598static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2599 int index)
2600{
2601 struct fixed_file_table *table;
2602
2603 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2604 return table->files[index & IORING_FILE_TABLE_MASK];
2605}
2606
Jackie Liua197f662019-11-08 08:09:12 -07002607static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002608{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002609 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002610 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002611 unsigned flags;
2612 int fd;
2613
2614 flags = READ_ONCE(s->sqe->flags);
2615 fd = READ_ONCE(s->sqe->fd);
2616
Jackie Liu4fe2c962019-09-09 20:50:40 +08002617 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002618 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002619 /*
2620 * All io need record the previous position, if LINK vs DARIN,
2621 * it can be used to mark the position of the first IO in the
2622 * link list.
2623 */
2624 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002625
Jens Axboe60c112b2019-06-21 10:20:18 -06002626 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002627 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002628
2629 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002630 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002631 (unsigned) fd >= ctx->nr_user_files))
2632 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002633 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002634 req->file = io_file_from_index(ctx, fd);
2635 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002636 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002637 req->flags |= REQ_F_FIXED_FILE;
2638 } else {
2639 if (s->needs_fixed_file)
2640 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002641 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002642 req->file = io_file_get(state, fd);
2643 if (unlikely(!req->file))
2644 return -EBADF;
2645 }
2646
2647 return 0;
2648}
2649
Jackie Liua197f662019-11-08 08:09:12 -07002650static int io_grab_files(struct io_kiocb *req)
Jens Axboefcb323c2019-10-24 12:39:47 -06002651{
2652 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002653 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002654
2655 rcu_read_lock();
2656 spin_lock_irq(&ctx->inflight_lock);
2657 /*
2658 * We use the f_ops->flush() handler to ensure that we can flush
2659 * out work accessing these files if the fd is closed. Check if
2660 * the fd has changed since we started down this path, and disallow
2661 * this operation if it has.
2662 */
2663 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2664 list_add(&req->inflight_entry, &ctx->inflight_list);
2665 req->flags |= REQ_F_INFLIGHT;
2666 req->work.files = current->files;
2667 ret = 0;
2668 }
2669 spin_unlock_irq(&ctx->inflight_lock);
2670 rcu_read_unlock();
2671
2672 return ret;
2673}
2674
Jens Axboe2665abf2019-11-05 12:40:47 -07002675static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2676{
2677 struct io_kiocb *req = container_of(timer, struct io_kiocb,
2678 timeout.timer);
2679 struct io_ring_ctx *ctx = req->ctx;
2680 struct io_kiocb *prev = NULL;
2681 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002682
2683 spin_lock_irqsave(&ctx->completion_lock, flags);
2684
2685 /*
2686 * We don't expect the list to be empty, that will only happen if we
2687 * race with the completion of the linked work.
2688 */
2689 if (!list_empty(&req->list)) {
2690 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002691 if (refcount_inc_not_zero(&prev->refs))
2692 list_del_init(&req->list);
2693 else
2694 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002695 }
2696
2697 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2698
2699 if (prev) {
Jens Axboe47f46762019-11-09 17:43:02 -07002700 io_async_find_and_cancel(ctx, req, prev->user_data, NULL);
Jens Axboe76a46e02019-11-10 23:34:16 -07002701 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002702 } else {
2703 io_cqring_add_event(req, -ETIME);
2704 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002705 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002706 return HRTIMER_NORESTART;
2707}
2708
Jens Axboe76a46e02019-11-10 23:34:16 -07002709static void io_queue_linked_timeout(struct io_kiocb *req, struct timespec64 *ts,
2710 enum hrtimer_mode *mode)
Jens Axboe2665abf2019-11-05 12:40:47 -07002711{
Jens Axboe76a46e02019-11-10 23:34:16 -07002712 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002713
Jens Axboe76a46e02019-11-10 23:34:16 -07002714 /*
2715 * If the list is now empty, then our linked request finished before
2716 * we got a chance to setup the timer
2717 */
2718 spin_lock_irq(&ctx->completion_lock);
2719 if (!list_empty(&req->list)) {
2720 req->timeout.timer.function = io_link_timeout_fn;
2721 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(*ts),
2722 *mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002723 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002724 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002725
Jens Axboe2665abf2019-11-05 12:40:47 -07002726 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002727 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002728}
2729
Jens Axboe76a46e02019-11-10 23:34:16 -07002730static int io_validate_link_timeout(const struct io_uring_sqe *sqe,
2731 struct timespec64 *ts)
2732{
2733 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || sqe->off)
2734 return -EINVAL;
2735 if (sqe->timeout_flags & ~IORING_TIMEOUT_ABS)
2736 return -EINVAL;
2737 if (get_timespec64(ts, u64_to_user_ptr(sqe->addr)))
2738 return -EFAULT;
2739
2740 return 0;
2741}
2742
2743static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req,
2744 struct timespec64 *ts,
2745 enum hrtimer_mode *mode)
Jens Axboe2665abf2019-11-05 12:40:47 -07002746{
2747 struct io_kiocb *nxt;
Jens Axboe76a46e02019-11-10 23:34:16 -07002748 int ret;
Jens Axboe2665abf2019-11-05 12:40:47 -07002749
2750 if (!(req->flags & REQ_F_LINK))
2751 return NULL;
2752
2753 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002754 if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT)
2755 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002756
Jens Axboe76a46e02019-11-10 23:34:16 -07002757 ret = io_validate_link_timeout(nxt->submit.sqe, ts);
2758 if (ret) {
2759 list_del_init(&nxt->list);
2760 io_cqring_add_event(nxt, ret);
2761 io_double_put_req(nxt);
2762 return ERR_PTR(-ECANCELED);
2763 }
2764
2765 if (nxt->submit.sqe->timeout_flags & IORING_TIMEOUT_ABS)
2766 *mode = HRTIMER_MODE_ABS;
2767 else
2768 *mode = HRTIMER_MODE_REL;
2769
2770 req->flags |= REQ_F_LINK_TIMEOUT;
2771 hrtimer_init(&nxt->timeout.timer, CLOCK_MONOTONIC, *mode);
2772 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002773}
2774
Jackie Liua197f662019-11-08 08:09:12 -07002775static int __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002776{
Jens Axboe76a46e02019-11-10 23:34:16 -07002777 enum hrtimer_mode mode;
Jens Axboe2665abf2019-11-05 12:40:47 -07002778 struct io_kiocb *nxt;
Jens Axboe76a46e02019-11-10 23:34:16 -07002779 struct timespec64 ts;
Jens Axboee0c5c572019-03-12 10:18:47 -06002780 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002781
Jens Axboe76a46e02019-11-10 23:34:16 -07002782 nxt = io_prep_linked_timeout(req, &ts, &mode);
2783 if (IS_ERR(nxt)) {
2784 ret = PTR_ERR(nxt);
2785 nxt = NULL;
2786 goto err;
Jens Axboe2665abf2019-11-05 12:40:47 -07002787 }
2788
Jackie Liua197f662019-11-08 08:09:12 -07002789 ret = __io_submit_sqe(req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002790
2791 /*
2792 * We async punt it if the file wasn't marked NOWAIT, or if the file
2793 * doesn't support non-blocking read/write attempts
2794 */
2795 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2796 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002797 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002798 struct io_uring_sqe *sqe_copy;
2799
Jackie Liu954dab12019-09-18 10:37:52 +08002800 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002801 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002802 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002803 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
Jackie Liua197f662019-11-08 08:09:12 -07002804 ret = io_grab_files(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06002805 if (ret) {
2806 kfree(sqe_copy);
2807 goto err;
2808 }
2809 }
Jens Axboee65ef562019-03-12 10:16:44 -06002810
2811 /*
2812 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002813 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002814 */
Jackie Liua197f662019-11-08 08:09:12 -07002815 io_queue_async_work(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07002816
2817 if (nxt)
2818 io_queue_linked_timeout(nxt, &ts, &mode);
2819
Jens Axboee65ef562019-03-12 10:16:44 -06002820 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002821 }
2822 }
Jens Axboee65ef562019-03-12 10:16:44 -06002823
Jens Axboefcb323c2019-10-24 12:39:47 -06002824err:
Jens Axboe76a46e02019-11-10 23:34:16 -07002825 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002826 io_put_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002827
Jens Axboe76a46e02019-11-10 23:34:16 -07002828 if (nxt) {
2829 if (!ret)
2830 io_queue_linked_timeout(nxt, &ts, &mode);
2831 else
2832 io_put_req(nxt);
2833 }
2834
Jens Axboee65ef562019-03-12 10:16:44 -06002835 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002836 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002837 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002838 if (req->flags & REQ_F_LINK)
2839 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002840 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002841 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002842
2843 return ret;
2844}
2845
Jackie Liua197f662019-11-08 08:09:12 -07002846static int io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002847{
2848 int ret;
2849
Jackie Liua197f662019-11-08 08:09:12 -07002850 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002851 if (ret) {
2852 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002853 io_cqring_add_event(req, ret);
2854 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002855 }
2856 return 0;
2857 }
2858
Jackie Liua197f662019-11-08 08:09:12 -07002859 return __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002860}
2861
Jackie Liua197f662019-11-08 08:09:12 -07002862static int io_queue_link_head(struct io_kiocb *req, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002863{
2864 int ret;
2865 int need_submit = false;
Jackie Liua197f662019-11-08 08:09:12 -07002866 struct io_ring_ctx *ctx = req->ctx;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002867
2868 if (!shadow)
Jackie Liua197f662019-11-08 08:09:12 -07002869 return io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002870
2871 /*
2872 * Mark the first IO in link list as DRAIN, let all the following
2873 * IOs enter the defer list. all IO needs to be completed before link
2874 * list.
2875 */
2876 req->flags |= REQ_F_IO_DRAIN;
Jackie Liua197f662019-11-08 08:09:12 -07002877 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002878 if (ret) {
2879 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002880 io_cqring_add_event(req, ret);
2881 io_double_put_req(req);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002882 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002883 return 0;
2884 }
2885 } else {
2886 /*
2887 * If ret == 0 means that all IOs in front of link io are
2888 * running done. let's queue link head.
2889 */
2890 need_submit = true;
2891 }
2892
2893 /* Insert shadow req to defer_list, blocking next IOs */
2894 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002895 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002896 list_add_tail(&shadow->list, &ctx->defer_list);
2897 spin_unlock_irq(&ctx->completion_lock);
2898
2899 if (need_submit)
Jackie Liua197f662019-11-08 08:09:12 -07002900 return __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002901
2902 return 0;
2903}
2904
Jens Axboe9e645e112019-05-10 16:07:28 -06002905#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2906
Jackie Liua197f662019-11-08 08:09:12 -07002907static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
2908 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002909{
2910 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002911 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002912 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06002913 int ret;
2914
Jens Axboe78e19bb2019-11-06 15:21:34 -07002915 req->user_data = s->sqe->user_data;
2916
Jens Axboe9e645e112019-05-10 16:07:28 -06002917 /* enforce forwards compatibility on users */
2918 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2919 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03002920 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002921 }
2922
Jackie Liua197f662019-11-08 08:09:12 -07002923 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002924 if (unlikely(ret)) {
2925err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002926 io_cqring_add_event(req, ret);
2927 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002928 return;
2929 }
2930
Jens Axboe9e645e112019-05-10 16:07:28 -06002931 /*
2932 * If we already have a head request, queue this one for async
2933 * submittal once the head completes. If we don't have a head but
2934 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2935 * submitted sync once the chain is complete. If none of those
2936 * conditions are true (normal request), then just queue it.
2937 */
2938 if (*link) {
2939 struct io_kiocb *prev = *link;
2940
2941 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2942 if (!sqe_copy) {
2943 ret = -EAGAIN;
2944 goto err_req;
2945 }
2946
2947 s->sqe = sqe_copy;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002948 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002949 list_add_tail(&req->list, &prev->link_list);
2950 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2951 req->flags |= REQ_F_LINK;
2952
Jens Axboe9e645e112019-05-10 16:07:28 -06002953 INIT_LIST_HEAD(&req->link_list);
2954 *link = req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002955 } else if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
2956 /* Only valid as a linked SQE */
2957 ret = -EINVAL;
2958 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002959 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002960 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002961 }
2962}
2963
Jens Axboe9a56a232019-01-09 09:06:50 -07002964/*
2965 * Batched submission is done, ensure local IO is flushed out.
2966 */
2967static void io_submit_state_end(struct io_submit_state *state)
2968{
2969 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002970 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002971 if (state->free_reqs)
2972 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2973 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002974}
2975
2976/*
2977 * Start submission side cache.
2978 */
2979static void io_submit_state_start(struct io_submit_state *state,
2980 struct io_ring_ctx *ctx, unsigned max_ios)
2981{
2982 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002983 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002984 state->file = NULL;
2985 state->ios_left = max_ios;
2986}
2987
Jens Axboe2b188cc2019-01-07 10:46:33 -07002988static void io_commit_sqring(struct io_ring_ctx *ctx)
2989{
Hristo Venev75b28af2019-08-26 17:23:46 +00002990 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002991
Hristo Venev75b28af2019-08-26 17:23:46 +00002992 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002993 /*
2994 * Ensure any loads from the SQEs are done at this point,
2995 * since once we write the new head, the application could
2996 * write new data to them.
2997 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002998 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002999 }
3000}
3001
3002/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003003 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3004 * that is mapped by userspace. This means that care needs to be taken to
3005 * ensure that reads are stable, as we cannot rely on userspace always
3006 * being a good citizen. If members of the sqe are validated and then later
3007 * used, it's important that those reads are done through READ_ONCE() to
3008 * prevent a re-load down the line.
3009 */
3010static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
3011{
Hristo Venev75b28af2019-08-26 17:23:46 +00003012 struct io_rings *rings = ctx->rings;
3013 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003014 unsigned head;
3015
3016 /*
3017 * The cached sq head (or cq tail) serves two purposes:
3018 *
3019 * 1) allows us to batch the cost of updating the user visible
3020 * head updates.
3021 * 2) allows the kernel side to track the head on its own, even
3022 * though the application is the one updating it.
3023 */
3024 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003025 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00003026 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003027 return false;
3028
Hristo Venev75b28af2019-08-26 17:23:46 +00003029 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003030 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003031 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003032 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003033 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003034 ctx->cached_sq_head++;
3035 return true;
3036 }
3037
3038 /* drop invalid entries */
3039 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003040 ctx->cached_sq_dropped++;
3041 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003042 return false;
3043}
3044
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003045static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003046 struct file *ring_file, int ring_fd,
3047 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003048{
3049 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003050 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003051 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003052 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003053 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003054
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003055 if (!list_empty(&ctx->cq_overflow_list)) {
3056 io_cqring_overflow_flush(ctx, false);
3057 return -EBUSY;
3058 }
3059
Jens Axboe6c271ce2019-01-10 11:22:30 -07003060 if (nr > IO_PLUG_THRESHOLD) {
3061 io_submit_state_start(&state, ctx, nr);
3062 statep = &state;
3063 }
3064
3065 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003066 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003067 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003068
Pavel Begunkov196be952019-11-07 01:41:06 +03003069 req = io_get_req(ctx, statep);
3070 if (unlikely(!req)) {
3071 if (!submitted)
3072 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003073 break;
Pavel Begunkov196be952019-11-07 01:41:06 +03003074 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003075 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003076 __io_free_req(req);
3077 break;
3078 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003079
Pavel Begunkov50585b92019-11-07 01:41:07 +03003080 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003081 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3082 if (!mm_fault) {
3083 use_mm(ctx->sqo_mm);
3084 *mm = ctx->sqo_mm;
3085 }
3086 }
3087
Pavel Begunkov50585b92019-11-07 01:41:07 +03003088 sqe_flags = req->submit.sqe->flags;
3089
3090 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08003091 if (!shadow_req) {
3092 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08003093 if (unlikely(!shadow_req))
3094 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003095 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
3096 refcount_dec(&shadow_req->refs);
3097 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003098 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003099 }
3100
Jackie Liua1041c22019-09-18 17:25:52 +08003101out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03003102 req->submit.ring_file = ring_file;
3103 req->submit.ring_fd = ring_fd;
3104 req->submit.has_user = *mm != NULL;
3105 req->submit.in_async = async;
3106 req->submit.needs_fixed_file = async;
3107 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3108 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003109 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003110 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003111
3112 /*
3113 * If previous wasn't linked and we have a linked command,
3114 * that's the end of the chain. Submit the previous link.
3115 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003116 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Jackie Liua197f662019-11-08 08:09:12 -07003117 io_queue_link_head(link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003118 link = NULL;
3119 shadow_req = NULL;
3120 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003121 }
3122
Jens Axboe9e645e112019-05-10 16:07:28 -06003123 if (link)
Jackie Liua197f662019-11-08 08:09:12 -07003124 io_queue_link_head(link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003125 if (statep)
3126 io_submit_state_end(&state);
3127
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003128 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3129 io_commit_sqring(ctx);
3130
Jens Axboe6c271ce2019-01-10 11:22:30 -07003131 return submitted;
3132}
3133
3134static int io_sq_thread(void *data)
3135{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003136 struct io_ring_ctx *ctx = data;
3137 struct mm_struct *cur_mm = NULL;
3138 mm_segment_t old_fs;
3139 DEFINE_WAIT(wait);
3140 unsigned inflight;
3141 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003142 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003143
Jens Axboe206aefd2019-11-07 18:27:42 -07003144 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003145
Jens Axboe6c271ce2019-01-10 11:22:30 -07003146 old_fs = get_fs();
3147 set_fs(USER_DS);
3148
Jens Axboec1edbf52019-11-10 16:56:04 -07003149 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003150 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003151 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003152
3153 if (inflight) {
3154 unsigned nr_events = 0;
3155
3156 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003157 /*
3158 * inflight is the count of the maximum possible
3159 * entries we submitted, but it can be smaller
3160 * if we dropped some of them. If we don't have
3161 * poll entries available, then we know that we
3162 * have nothing left to poll for. Reset the
3163 * inflight count to zero in that case.
3164 */
3165 mutex_lock(&ctx->uring_lock);
3166 if (!list_empty(&ctx->poll_list))
3167 __io_iopoll_check(ctx, &nr_events, 0);
3168 else
3169 inflight = 0;
3170 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003171 } else {
3172 /*
3173 * Normal IO, just pretend everything completed.
3174 * We don't have to poll completions for that.
3175 */
3176 nr_events = inflight;
3177 }
3178
3179 inflight -= nr_events;
3180 if (!inflight)
3181 timeout = jiffies + ctx->sq_thread_idle;
3182 }
3183
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003184 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003185
3186 /*
3187 * If submit got -EBUSY, flag us as needing the application
3188 * to enter the kernel to reap and flush events.
3189 */
3190 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003191 /*
3192 * We're polling. If we're within the defined idle
3193 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003194 * to sleep. The exception is if we got EBUSY doing
3195 * more IO, we should wait for the application to
3196 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003197 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003198 if (inflight ||
3199 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003200 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003201 continue;
3202 }
3203
3204 /*
3205 * Drop cur_mm before scheduling, we can't hold it for
3206 * long periods (or over schedule()). Do this before
3207 * adding ourselves to the waitqueue, as the unuse/drop
3208 * may sleep.
3209 */
3210 if (cur_mm) {
3211 unuse_mm(cur_mm);
3212 mmput(cur_mm);
3213 cur_mm = NULL;
3214 }
3215
3216 prepare_to_wait(&ctx->sqo_wait, &wait,
3217 TASK_INTERRUPTIBLE);
3218
3219 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003220 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003221 /* make sure to read SQ tail after writing flags */
3222 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003223
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003224 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003225 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003226 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003227 finish_wait(&ctx->sqo_wait, &wait);
3228 break;
3229 }
3230 if (signal_pending(current))
3231 flush_signals(current);
3232 schedule();
3233 finish_wait(&ctx->sqo_wait, &wait);
3234
Hristo Venev75b28af2019-08-26 17:23:46 +00003235 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003236 continue;
3237 }
3238 finish_wait(&ctx->sqo_wait, &wait);
3239
Hristo Venev75b28af2019-08-26 17:23:46 +00003240 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003241 }
3242
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003243 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003244 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3245 if (ret > 0)
3246 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003247 }
3248
3249 set_fs(old_fs);
3250 if (cur_mm) {
3251 unuse_mm(cur_mm);
3252 mmput(cur_mm);
3253 }
Jens Axboe06058632019-04-13 09:26:03 -06003254
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003255 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003256
Jens Axboe6c271ce2019-01-10 11:22:30 -07003257 return 0;
3258}
3259
Jens Axboebda52162019-09-24 13:47:15 -06003260struct io_wait_queue {
3261 struct wait_queue_entry wq;
3262 struct io_ring_ctx *ctx;
3263 unsigned to_wait;
3264 unsigned nr_timeouts;
3265};
3266
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003267static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003268{
3269 struct io_ring_ctx *ctx = iowq->ctx;
3270
3271 /*
3272 * Wake up if we have enough events, or if a timeout occured since we
3273 * started waiting. For timeouts, we always want to return to userspace,
3274 * regardless of event count.
3275 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003276 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003277 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3278}
3279
3280static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3281 int wake_flags, void *key)
3282{
3283 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3284 wq);
3285
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003286 /* use noflush == true, as we can't safely rely on locking context */
3287 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003288 return -1;
3289
3290 return autoremove_wake_function(curr, mode, wake_flags, key);
3291}
3292
Jens Axboe2b188cc2019-01-07 10:46:33 -07003293/*
3294 * Wait until events become available, if we don't already have some. The
3295 * application must reap them itself, as they reside on the shared cq ring.
3296 */
3297static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3298 const sigset_t __user *sig, size_t sigsz)
3299{
Jens Axboebda52162019-09-24 13:47:15 -06003300 struct io_wait_queue iowq = {
3301 .wq = {
3302 .private = current,
3303 .func = io_wake_function,
3304 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3305 },
3306 .ctx = ctx,
3307 .to_wait = min_events,
3308 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003309 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003310 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003311
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003312 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003313 return 0;
3314
3315 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003316#ifdef CONFIG_COMPAT
3317 if (in_compat_syscall())
3318 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003319 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003320 else
3321#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003322 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003323
Jens Axboe2b188cc2019-01-07 10:46:33 -07003324 if (ret)
3325 return ret;
3326 }
3327
Jens Axboebda52162019-09-24 13:47:15 -06003328 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003329 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003330 do {
3331 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3332 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003333 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003334 break;
3335 schedule();
3336 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003337 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003338 break;
3339 }
3340 } while (1);
3341 finish_wait(&ctx->wait, &iowq.wq);
3342
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003343 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003344
Hristo Venev75b28af2019-08-26 17:23:46 +00003345 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003346}
3347
Jens Axboe6b063142019-01-10 22:13:58 -07003348static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3349{
3350#if defined(CONFIG_UNIX)
3351 if (ctx->ring_sock) {
3352 struct sock *sock = ctx->ring_sock->sk;
3353 struct sk_buff *skb;
3354
3355 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3356 kfree_skb(skb);
3357 }
3358#else
3359 int i;
3360
Jens Axboe65e19f52019-10-26 07:20:21 -06003361 for (i = 0; i < ctx->nr_user_files; i++) {
3362 struct file *file;
3363
3364 file = io_file_from_index(ctx, i);
3365 if (file)
3366 fput(file);
3367 }
Jens Axboe6b063142019-01-10 22:13:58 -07003368#endif
3369}
3370
3371static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3372{
Jens Axboe65e19f52019-10-26 07:20:21 -06003373 unsigned nr_tables, i;
3374
3375 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003376 return -ENXIO;
3377
3378 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003379 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3380 for (i = 0; i < nr_tables; i++)
3381 kfree(ctx->file_table[i].files);
3382 kfree(ctx->file_table);
3383 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003384 ctx->nr_user_files = 0;
3385 return 0;
3386}
3387
Jens Axboe6c271ce2019-01-10 11:22:30 -07003388static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3389{
3390 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003391 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003392 /*
3393 * The park is a bit of a work-around, without it we get
3394 * warning spews on shutdown with SQPOLL set and affinity
3395 * set to a single CPU.
3396 */
Jens Axboe06058632019-04-13 09:26:03 -06003397 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003398 kthread_stop(ctx->sqo_thread);
3399 ctx->sqo_thread = NULL;
3400 }
3401}
3402
Jens Axboe6b063142019-01-10 22:13:58 -07003403static void io_finish_async(struct io_ring_ctx *ctx)
3404{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003405 io_sq_thread_stop(ctx);
3406
Jens Axboe561fb042019-10-24 07:25:42 -06003407 if (ctx->io_wq) {
3408 io_wq_destroy(ctx->io_wq);
3409 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003410 }
3411}
3412
3413#if defined(CONFIG_UNIX)
3414static void io_destruct_skb(struct sk_buff *skb)
3415{
3416 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3417
Jens Axboe561fb042019-10-24 07:25:42 -06003418 if (ctx->io_wq)
3419 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003420
Jens Axboe6b063142019-01-10 22:13:58 -07003421 unix_destruct_scm(skb);
3422}
3423
3424/*
3425 * Ensure the UNIX gc is aware of our file set, so we are certain that
3426 * the io_uring can be safely unregistered on process exit, even if we have
3427 * loops in the file referencing.
3428 */
3429static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3430{
3431 struct sock *sk = ctx->ring_sock->sk;
3432 struct scm_fp_list *fpl;
3433 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003434 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003435
3436 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3437 unsigned long inflight = ctx->user->unix_inflight + nr;
3438
3439 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3440 return -EMFILE;
3441 }
3442
3443 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3444 if (!fpl)
3445 return -ENOMEM;
3446
3447 skb = alloc_skb(0, GFP_KERNEL);
3448 if (!skb) {
3449 kfree(fpl);
3450 return -ENOMEM;
3451 }
3452
3453 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003454
Jens Axboe08a45172019-10-03 08:11:03 -06003455 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003456 fpl->user = get_uid(ctx->user);
3457 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003458 struct file *file = io_file_from_index(ctx, i + offset);
3459
3460 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003461 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003462 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003463 unix_inflight(fpl->user, fpl->fp[nr_files]);
3464 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003465 }
3466
Jens Axboe08a45172019-10-03 08:11:03 -06003467 if (nr_files) {
3468 fpl->max = SCM_MAX_FD;
3469 fpl->count = nr_files;
3470 UNIXCB(skb).fp = fpl;
3471 skb->destructor = io_destruct_skb;
3472 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3473 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003474
Jens Axboe08a45172019-10-03 08:11:03 -06003475 for (i = 0; i < nr_files; i++)
3476 fput(fpl->fp[i]);
3477 } else {
3478 kfree_skb(skb);
3479 kfree(fpl);
3480 }
Jens Axboe6b063142019-01-10 22:13:58 -07003481
3482 return 0;
3483}
3484
3485/*
3486 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3487 * causes regular reference counting to break down. We rely on the UNIX
3488 * garbage collection to take care of this problem for us.
3489 */
3490static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3491{
3492 unsigned left, total;
3493 int ret = 0;
3494
3495 total = 0;
3496 left = ctx->nr_user_files;
3497 while (left) {
3498 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003499
3500 ret = __io_sqe_files_scm(ctx, this_files, total);
3501 if (ret)
3502 break;
3503 left -= this_files;
3504 total += this_files;
3505 }
3506
3507 if (!ret)
3508 return 0;
3509
3510 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003511 struct file *file = io_file_from_index(ctx, total);
3512
3513 if (file)
3514 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003515 total++;
3516 }
3517
3518 return ret;
3519}
3520#else
3521static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3522{
3523 return 0;
3524}
3525#endif
3526
Jens Axboe65e19f52019-10-26 07:20:21 -06003527static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3528 unsigned nr_files)
3529{
3530 int i;
3531
3532 for (i = 0; i < nr_tables; i++) {
3533 struct fixed_file_table *table = &ctx->file_table[i];
3534 unsigned this_files;
3535
3536 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3537 table->files = kcalloc(this_files, sizeof(struct file *),
3538 GFP_KERNEL);
3539 if (!table->files)
3540 break;
3541 nr_files -= this_files;
3542 }
3543
3544 if (i == nr_tables)
3545 return 0;
3546
3547 for (i = 0; i < nr_tables; i++) {
3548 struct fixed_file_table *table = &ctx->file_table[i];
3549 kfree(table->files);
3550 }
3551 return 1;
3552}
3553
Jens Axboe6b063142019-01-10 22:13:58 -07003554static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3555 unsigned nr_args)
3556{
3557 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003558 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003559 int fd, ret = 0;
3560 unsigned i;
3561
Jens Axboe65e19f52019-10-26 07:20:21 -06003562 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003563 return -EBUSY;
3564 if (!nr_args)
3565 return -EINVAL;
3566 if (nr_args > IORING_MAX_FIXED_FILES)
3567 return -EMFILE;
3568
Jens Axboe65e19f52019-10-26 07:20:21 -06003569 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3570 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3571 GFP_KERNEL);
3572 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003573 return -ENOMEM;
3574
Jens Axboe65e19f52019-10-26 07:20:21 -06003575 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3576 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003577 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003578 return -ENOMEM;
3579 }
3580
Jens Axboe08a45172019-10-03 08:11:03 -06003581 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003582 struct fixed_file_table *table;
3583 unsigned index;
3584
Jens Axboe6b063142019-01-10 22:13:58 -07003585 ret = -EFAULT;
3586 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3587 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003588 /* allow sparse sets */
3589 if (fd == -1) {
3590 ret = 0;
3591 continue;
3592 }
Jens Axboe6b063142019-01-10 22:13:58 -07003593
Jens Axboe65e19f52019-10-26 07:20:21 -06003594 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3595 index = i & IORING_FILE_TABLE_MASK;
3596 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003597
3598 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003599 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003600 break;
3601 /*
3602 * Don't allow io_uring instances to be registered. If UNIX
3603 * isn't enabled, then this causes a reference cycle and this
3604 * instance can never get freed. If UNIX is enabled we'll
3605 * handle it just fine, but there's still no point in allowing
3606 * a ring fd as it doesn't support regular read/write anyway.
3607 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003608 if (table->files[index]->f_op == &io_uring_fops) {
3609 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003610 break;
3611 }
Jens Axboe6b063142019-01-10 22:13:58 -07003612 ret = 0;
3613 }
3614
3615 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003616 for (i = 0; i < ctx->nr_user_files; i++) {
3617 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003618
Jens Axboe65e19f52019-10-26 07:20:21 -06003619 file = io_file_from_index(ctx, i);
3620 if (file)
3621 fput(file);
3622 }
3623 for (i = 0; i < nr_tables; i++)
3624 kfree(ctx->file_table[i].files);
3625
3626 kfree(ctx->file_table);
3627 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003628 ctx->nr_user_files = 0;
3629 return ret;
3630 }
3631
3632 ret = io_sqe_files_scm(ctx);
3633 if (ret)
3634 io_sqe_files_unregister(ctx);
3635
3636 return ret;
3637}
3638
Jens Axboec3a31e62019-10-03 13:59:56 -06003639static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3640{
3641#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003642 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003643 struct sock *sock = ctx->ring_sock->sk;
3644 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3645 struct sk_buff *skb;
3646 int i;
3647
3648 __skb_queue_head_init(&list);
3649
3650 /*
3651 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3652 * remove this entry and rearrange the file array.
3653 */
3654 skb = skb_dequeue(head);
3655 while (skb) {
3656 struct scm_fp_list *fp;
3657
3658 fp = UNIXCB(skb).fp;
3659 for (i = 0; i < fp->count; i++) {
3660 int left;
3661
3662 if (fp->fp[i] != file)
3663 continue;
3664
3665 unix_notinflight(fp->user, fp->fp[i]);
3666 left = fp->count - 1 - i;
3667 if (left) {
3668 memmove(&fp->fp[i], &fp->fp[i + 1],
3669 left * sizeof(struct file *));
3670 }
3671 fp->count--;
3672 if (!fp->count) {
3673 kfree_skb(skb);
3674 skb = NULL;
3675 } else {
3676 __skb_queue_tail(&list, skb);
3677 }
3678 fput(file);
3679 file = NULL;
3680 break;
3681 }
3682
3683 if (!file)
3684 break;
3685
3686 __skb_queue_tail(&list, skb);
3687
3688 skb = skb_dequeue(head);
3689 }
3690
3691 if (skb_peek(&list)) {
3692 spin_lock_irq(&head->lock);
3693 while ((skb = __skb_dequeue(&list)) != NULL)
3694 __skb_queue_tail(head, skb);
3695 spin_unlock_irq(&head->lock);
3696 }
3697#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003698 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003699#endif
3700}
3701
3702static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3703 int index)
3704{
3705#if defined(CONFIG_UNIX)
3706 struct sock *sock = ctx->ring_sock->sk;
3707 struct sk_buff_head *head = &sock->sk_receive_queue;
3708 struct sk_buff *skb;
3709
3710 /*
3711 * See if we can merge this file into an existing skb SCM_RIGHTS
3712 * file set. If there's no room, fall back to allocating a new skb
3713 * and filling it in.
3714 */
3715 spin_lock_irq(&head->lock);
3716 skb = skb_peek(head);
3717 if (skb) {
3718 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3719
3720 if (fpl->count < SCM_MAX_FD) {
3721 __skb_unlink(skb, head);
3722 spin_unlock_irq(&head->lock);
3723 fpl->fp[fpl->count] = get_file(file);
3724 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3725 fpl->count++;
3726 spin_lock_irq(&head->lock);
3727 __skb_queue_head(head, skb);
3728 } else {
3729 skb = NULL;
3730 }
3731 }
3732 spin_unlock_irq(&head->lock);
3733
3734 if (skb) {
3735 fput(file);
3736 return 0;
3737 }
3738
3739 return __io_sqe_files_scm(ctx, 1, index);
3740#else
3741 return 0;
3742#endif
3743}
3744
3745static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3746 unsigned nr_args)
3747{
3748 struct io_uring_files_update up;
3749 __s32 __user *fds;
3750 int fd, i, err;
3751 __u32 done;
3752
Jens Axboe65e19f52019-10-26 07:20:21 -06003753 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003754 return -ENXIO;
3755 if (!nr_args)
3756 return -EINVAL;
3757 if (copy_from_user(&up, arg, sizeof(up)))
3758 return -EFAULT;
3759 if (check_add_overflow(up.offset, nr_args, &done))
3760 return -EOVERFLOW;
3761 if (done > ctx->nr_user_files)
3762 return -EINVAL;
3763
3764 done = 0;
3765 fds = (__s32 __user *) up.fds;
3766 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003767 struct fixed_file_table *table;
3768 unsigned index;
3769
Jens Axboec3a31e62019-10-03 13:59:56 -06003770 err = 0;
3771 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3772 err = -EFAULT;
3773 break;
3774 }
3775 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003776 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3777 index = i & IORING_FILE_TABLE_MASK;
3778 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003779 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003780 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003781 }
3782 if (fd != -1) {
3783 struct file *file;
3784
3785 file = fget(fd);
3786 if (!file) {
3787 err = -EBADF;
3788 break;
3789 }
3790 /*
3791 * Don't allow io_uring instances to be registered. If
3792 * UNIX isn't enabled, then this causes a reference
3793 * cycle and this instance can never get freed. If UNIX
3794 * is enabled we'll handle it just fine, but there's
3795 * still no point in allowing a ring fd as it doesn't
3796 * support regular read/write anyway.
3797 */
3798 if (file->f_op == &io_uring_fops) {
3799 fput(file);
3800 err = -EBADF;
3801 break;
3802 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003803 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003804 err = io_sqe_file_register(ctx, file, i);
3805 if (err)
3806 break;
3807 }
3808 nr_args--;
3809 done++;
3810 up.offset++;
3811 }
3812
3813 return done ? done : err;
3814}
3815
Jens Axboe6c271ce2019-01-10 11:22:30 -07003816static int io_sq_offload_start(struct io_ring_ctx *ctx,
3817 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003818{
Jens Axboe561fb042019-10-24 07:25:42 -06003819 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003820 int ret;
3821
Jens Axboe6c271ce2019-01-10 11:22:30 -07003822 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003823 mmgrab(current->mm);
3824 ctx->sqo_mm = current->mm;
3825
Jens Axboe6c271ce2019-01-10 11:22:30 -07003826 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003827 ret = -EPERM;
3828 if (!capable(CAP_SYS_ADMIN))
3829 goto err;
3830
Jens Axboe917257d2019-04-13 09:28:55 -06003831 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3832 if (!ctx->sq_thread_idle)
3833 ctx->sq_thread_idle = HZ;
3834
Jens Axboe6c271ce2019-01-10 11:22:30 -07003835 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003836 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003837
Jens Axboe917257d2019-04-13 09:28:55 -06003838 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003839 if (cpu >= nr_cpu_ids)
3840 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003841 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003842 goto err;
3843
Jens Axboe6c271ce2019-01-10 11:22:30 -07003844 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3845 ctx, cpu,
3846 "io_uring-sq");
3847 } else {
3848 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3849 "io_uring-sq");
3850 }
3851 if (IS_ERR(ctx->sqo_thread)) {
3852 ret = PTR_ERR(ctx->sqo_thread);
3853 ctx->sqo_thread = NULL;
3854 goto err;
3855 }
3856 wake_up_process(ctx->sqo_thread);
3857 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3858 /* Can't have SQ_AFF without SQPOLL */
3859 ret = -EINVAL;
3860 goto err;
3861 }
3862
Jens Axboe561fb042019-10-24 07:25:42 -06003863 /* Do QD, or 4 * CPUS, whatever is smallest */
3864 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe5f8fd2d32019-11-07 10:57:36 -07003865 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user);
Jens Axboe975c99a52019-10-30 08:42:56 -06003866 if (IS_ERR(ctx->io_wq)) {
3867 ret = PTR_ERR(ctx->io_wq);
3868 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003869 goto err;
3870 }
3871
3872 return 0;
3873err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003874 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003875 mmdrop(ctx->sqo_mm);
3876 ctx->sqo_mm = NULL;
3877 return ret;
3878}
3879
3880static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3881{
3882 atomic_long_sub(nr_pages, &user->locked_vm);
3883}
3884
3885static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3886{
3887 unsigned long page_limit, cur_pages, new_pages;
3888
3889 /* Don't allow more pages than we can safely lock */
3890 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3891
3892 do {
3893 cur_pages = atomic_long_read(&user->locked_vm);
3894 new_pages = cur_pages + nr_pages;
3895 if (new_pages > page_limit)
3896 return -ENOMEM;
3897 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3898 new_pages) != cur_pages);
3899
3900 return 0;
3901}
3902
3903static void io_mem_free(void *ptr)
3904{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003905 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003906
Mark Rutland52e04ef2019-04-30 17:30:21 +01003907 if (!ptr)
3908 return;
3909
3910 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003911 if (put_page_testzero(page))
3912 free_compound_page(page);
3913}
3914
3915static void *io_mem_alloc(size_t size)
3916{
3917 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3918 __GFP_NORETRY;
3919
3920 return (void *) __get_free_pages(gfp_flags, get_order(size));
3921}
3922
Hristo Venev75b28af2019-08-26 17:23:46 +00003923static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3924 size_t *sq_offset)
3925{
3926 struct io_rings *rings;
3927 size_t off, sq_array_size;
3928
3929 off = struct_size(rings, cqes, cq_entries);
3930 if (off == SIZE_MAX)
3931 return SIZE_MAX;
3932
3933#ifdef CONFIG_SMP
3934 off = ALIGN(off, SMP_CACHE_BYTES);
3935 if (off == 0)
3936 return SIZE_MAX;
3937#endif
3938
3939 sq_array_size = array_size(sizeof(u32), sq_entries);
3940 if (sq_array_size == SIZE_MAX)
3941 return SIZE_MAX;
3942
3943 if (check_add_overflow(off, sq_array_size, &off))
3944 return SIZE_MAX;
3945
3946 if (sq_offset)
3947 *sq_offset = off;
3948
3949 return off;
3950}
3951
Jens Axboe2b188cc2019-01-07 10:46:33 -07003952static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3953{
Hristo Venev75b28af2019-08-26 17:23:46 +00003954 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003955
Hristo Venev75b28af2019-08-26 17:23:46 +00003956 pages = (size_t)1 << get_order(
3957 rings_size(sq_entries, cq_entries, NULL));
3958 pages += (size_t)1 << get_order(
3959 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003960
Hristo Venev75b28af2019-08-26 17:23:46 +00003961 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003962}
3963
Jens Axboeedafcce2019-01-09 09:16:05 -07003964static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3965{
3966 int i, j;
3967
3968 if (!ctx->user_bufs)
3969 return -ENXIO;
3970
3971 for (i = 0; i < ctx->nr_user_bufs; i++) {
3972 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3973
3974 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003975 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003976
3977 if (ctx->account_mem)
3978 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003979 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003980 imu->nr_bvecs = 0;
3981 }
3982
3983 kfree(ctx->user_bufs);
3984 ctx->user_bufs = NULL;
3985 ctx->nr_user_bufs = 0;
3986 return 0;
3987}
3988
3989static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3990 void __user *arg, unsigned index)
3991{
3992 struct iovec __user *src;
3993
3994#ifdef CONFIG_COMPAT
3995 if (ctx->compat) {
3996 struct compat_iovec __user *ciovs;
3997 struct compat_iovec ciov;
3998
3999 ciovs = (struct compat_iovec __user *) arg;
4000 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4001 return -EFAULT;
4002
4003 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4004 dst->iov_len = ciov.iov_len;
4005 return 0;
4006 }
4007#endif
4008 src = (struct iovec __user *) arg;
4009 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4010 return -EFAULT;
4011 return 0;
4012}
4013
4014static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4015 unsigned nr_args)
4016{
4017 struct vm_area_struct **vmas = NULL;
4018 struct page **pages = NULL;
4019 int i, j, got_pages = 0;
4020 int ret = -EINVAL;
4021
4022 if (ctx->user_bufs)
4023 return -EBUSY;
4024 if (!nr_args || nr_args > UIO_MAXIOV)
4025 return -EINVAL;
4026
4027 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4028 GFP_KERNEL);
4029 if (!ctx->user_bufs)
4030 return -ENOMEM;
4031
4032 for (i = 0; i < nr_args; i++) {
4033 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4034 unsigned long off, start, end, ubuf;
4035 int pret, nr_pages;
4036 struct iovec iov;
4037 size_t size;
4038
4039 ret = io_copy_iov(ctx, &iov, arg, i);
4040 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004041 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004042
4043 /*
4044 * Don't impose further limits on the size and buffer
4045 * constraints here, we'll -EINVAL later when IO is
4046 * submitted if they are wrong.
4047 */
4048 ret = -EFAULT;
4049 if (!iov.iov_base || !iov.iov_len)
4050 goto err;
4051
4052 /* arbitrary limit, but we need something */
4053 if (iov.iov_len > SZ_1G)
4054 goto err;
4055
4056 ubuf = (unsigned long) iov.iov_base;
4057 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4058 start = ubuf >> PAGE_SHIFT;
4059 nr_pages = end - start;
4060
4061 if (ctx->account_mem) {
4062 ret = io_account_mem(ctx->user, nr_pages);
4063 if (ret)
4064 goto err;
4065 }
4066
4067 ret = 0;
4068 if (!pages || nr_pages > got_pages) {
4069 kfree(vmas);
4070 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004071 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004072 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004073 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004074 sizeof(struct vm_area_struct *),
4075 GFP_KERNEL);
4076 if (!pages || !vmas) {
4077 ret = -ENOMEM;
4078 if (ctx->account_mem)
4079 io_unaccount_mem(ctx->user, nr_pages);
4080 goto err;
4081 }
4082 got_pages = nr_pages;
4083 }
4084
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004085 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004086 GFP_KERNEL);
4087 ret = -ENOMEM;
4088 if (!imu->bvec) {
4089 if (ctx->account_mem)
4090 io_unaccount_mem(ctx->user, nr_pages);
4091 goto err;
4092 }
4093
4094 ret = 0;
4095 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004096 pret = get_user_pages(ubuf, nr_pages,
4097 FOLL_WRITE | FOLL_LONGTERM,
4098 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004099 if (pret == nr_pages) {
4100 /* don't support file backed memory */
4101 for (j = 0; j < nr_pages; j++) {
4102 struct vm_area_struct *vma = vmas[j];
4103
4104 if (vma->vm_file &&
4105 !is_file_hugepages(vma->vm_file)) {
4106 ret = -EOPNOTSUPP;
4107 break;
4108 }
4109 }
4110 } else {
4111 ret = pret < 0 ? pret : -EFAULT;
4112 }
4113 up_read(&current->mm->mmap_sem);
4114 if (ret) {
4115 /*
4116 * if we did partial map, or found file backed vmas,
4117 * release any pages we did get
4118 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004119 if (pret > 0)
4120 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004121 if (ctx->account_mem)
4122 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004123 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004124 goto err;
4125 }
4126
4127 off = ubuf & ~PAGE_MASK;
4128 size = iov.iov_len;
4129 for (j = 0; j < nr_pages; j++) {
4130 size_t vec_len;
4131
4132 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4133 imu->bvec[j].bv_page = pages[j];
4134 imu->bvec[j].bv_len = vec_len;
4135 imu->bvec[j].bv_offset = off;
4136 off = 0;
4137 size -= vec_len;
4138 }
4139 /* store original address for later verification */
4140 imu->ubuf = ubuf;
4141 imu->len = iov.iov_len;
4142 imu->nr_bvecs = nr_pages;
4143
4144 ctx->nr_user_bufs++;
4145 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004146 kvfree(pages);
4147 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004148 return 0;
4149err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004150 kvfree(pages);
4151 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004152 io_sqe_buffer_unregister(ctx);
4153 return ret;
4154}
4155
Jens Axboe9b402842019-04-11 11:45:41 -06004156static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4157{
4158 __s32 __user *fds = arg;
4159 int fd;
4160
4161 if (ctx->cq_ev_fd)
4162 return -EBUSY;
4163
4164 if (copy_from_user(&fd, fds, sizeof(*fds)))
4165 return -EFAULT;
4166
4167 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4168 if (IS_ERR(ctx->cq_ev_fd)) {
4169 int ret = PTR_ERR(ctx->cq_ev_fd);
4170 ctx->cq_ev_fd = NULL;
4171 return ret;
4172 }
4173
4174 return 0;
4175}
4176
4177static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4178{
4179 if (ctx->cq_ev_fd) {
4180 eventfd_ctx_put(ctx->cq_ev_fd);
4181 ctx->cq_ev_fd = NULL;
4182 return 0;
4183 }
4184
4185 return -ENXIO;
4186}
4187
Jens Axboe2b188cc2019-01-07 10:46:33 -07004188static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4189{
Jens Axboe6b063142019-01-10 22:13:58 -07004190 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004191 if (ctx->sqo_mm)
4192 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004193
4194 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004195 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004196 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004197 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004198
Jens Axboe2b188cc2019-01-07 10:46:33 -07004199#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004200 if (ctx->ring_sock) {
4201 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004202 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004203 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004204#endif
4205
Hristo Venev75b28af2019-08-26 17:23:46 +00004206 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004207 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004208
4209 percpu_ref_exit(&ctx->refs);
4210 if (ctx->account_mem)
4211 io_unaccount_mem(ctx->user,
4212 ring_pages(ctx->sq_entries, ctx->cq_entries));
4213 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004214 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004215 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004216 kfree(ctx);
4217}
4218
4219static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4220{
4221 struct io_ring_ctx *ctx = file->private_data;
4222 __poll_t mask = 0;
4223
4224 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004225 /*
4226 * synchronizes with barrier from wq_has_sleeper call in
4227 * io_commit_cqring
4228 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004229 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004230 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4231 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004232 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004233 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004234 mask |= EPOLLIN | EPOLLRDNORM;
4235
4236 return mask;
4237}
4238
4239static int io_uring_fasync(int fd, struct file *file, int on)
4240{
4241 struct io_ring_ctx *ctx = file->private_data;
4242
4243 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4244}
4245
4246static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4247{
4248 mutex_lock(&ctx->uring_lock);
4249 percpu_ref_kill(&ctx->refs);
4250 mutex_unlock(&ctx->uring_lock);
4251
Jens Axboe5262f562019-09-17 12:26:57 -06004252 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004253 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004254
4255 if (ctx->io_wq)
4256 io_wq_cancel_all(ctx->io_wq);
4257
Jens Axboedef596e2019-01-09 08:59:42 -07004258 io_iopoll_reap_events(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004259 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004260 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004261 io_ring_ctx_free(ctx);
4262}
4263
4264static int io_uring_release(struct inode *inode, struct file *file)
4265{
4266 struct io_ring_ctx *ctx = file->private_data;
4267
4268 file->private_data = NULL;
4269 io_ring_ctx_wait_and_kill(ctx);
4270 return 0;
4271}
4272
Jens Axboefcb323c2019-10-24 12:39:47 -06004273static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4274 struct files_struct *files)
4275{
4276 struct io_kiocb *req;
4277 DEFINE_WAIT(wait);
4278
4279 while (!list_empty_careful(&ctx->inflight_list)) {
4280 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
Jens Axboe768134d2019-11-10 20:30:53 -07004281 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004282
4283 spin_lock_irq(&ctx->inflight_lock);
4284 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004285 if (req->work.files != files)
4286 continue;
4287 /* req is being completed, ignore */
4288 if (!refcount_inc_not_zero(&req->refs))
4289 continue;
4290 cancel_req = req;
4291 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004292 }
Jens Axboe768134d2019-11-10 20:30:53 -07004293 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004294 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004295 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004296 spin_unlock_irq(&ctx->inflight_lock);
4297
Jens Axboe768134d2019-11-10 20:30:53 -07004298 if (cancel_req) {
4299 ret = io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4300 io_put_req(cancel_req);
4301 }
4302
4303 /* We need to keep going until we don't find a matching req */
4304 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004305 break;
4306 schedule();
4307 }
Jens Axboe768134d2019-11-10 20:30:53 -07004308 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004309}
4310
4311static int io_uring_flush(struct file *file, void *data)
4312{
4313 struct io_ring_ctx *ctx = file->private_data;
4314
4315 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004316 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4317 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004318 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004319 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004320 return 0;
4321}
4322
Jens Axboe2b188cc2019-01-07 10:46:33 -07004323static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4324{
4325 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4326 unsigned long sz = vma->vm_end - vma->vm_start;
4327 struct io_ring_ctx *ctx = file->private_data;
4328 unsigned long pfn;
4329 struct page *page;
4330 void *ptr;
4331
4332 switch (offset) {
4333 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004334 case IORING_OFF_CQ_RING:
4335 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004336 break;
4337 case IORING_OFF_SQES:
4338 ptr = ctx->sq_sqes;
4339 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004340 default:
4341 return -EINVAL;
4342 }
4343
4344 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004345 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004346 return -EINVAL;
4347
4348 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4349 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4350}
4351
4352SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4353 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4354 size_t, sigsz)
4355{
4356 struct io_ring_ctx *ctx;
4357 long ret = -EBADF;
4358 int submitted = 0;
4359 struct fd f;
4360
Jens Axboe6c271ce2019-01-10 11:22:30 -07004361 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004362 return -EINVAL;
4363
4364 f = fdget(fd);
4365 if (!f.file)
4366 return -EBADF;
4367
4368 ret = -EOPNOTSUPP;
4369 if (f.file->f_op != &io_uring_fops)
4370 goto out_fput;
4371
4372 ret = -ENXIO;
4373 ctx = f.file->private_data;
4374 if (!percpu_ref_tryget(&ctx->refs))
4375 goto out_fput;
4376
Jens Axboe6c271ce2019-01-10 11:22:30 -07004377 /*
4378 * For SQ polling, the thread will do all submissions and completions.
4379 * Just return the requested submit count, and wake the thread if
4380 * we were asked to.
4381 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004382 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004383 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004384 if (!list_empty_careful(&ctx->cq_overflow_list))
4385 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004386 if (flags & IORING_ENTER_SQ_WAKEUP)
4387 wake_up(&ctx->sqo_wait);
4388 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004389 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004390 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004391
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004392 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004393 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004394 /* already have mm, so io_submit_sqes() won't try to grab it */
4395 cur_mm = ctx->sqo_mm;
4396 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4397 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004398 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004399 }
4400 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004401 unsigned nr_events = 0;
4402
Jens Axboe2b188cc2019-01-07 10:46:33 -07004403 min_complete = min(min_complete, ctx->cq_entries);
4404
Jens Axboedef596e2019-01-09 08:59:42 -07004405 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004406 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004407 } else {
4408 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4409 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004410 }
4411
Pavel Begunkov6805b322019-10-08 02:18:42 +03004412 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004413out_fput:
4414 fdput(f);
4415 return submitted ? submitted : ret;
4416}
4417
4418static const struct file_operations io_uring_fops = {
4419 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004420 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004421 .mmap = io_uring_mmap,
4422 .poll = io_uring_poll,
4423 .fasync = io_uring_fasync,
4424};
4425
4426static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4427 struct io_uring_params *p)
4428{
Hristo Venev75b28af2019-08-26 17:23:46 +00004429 struct io_rings *rings;
4430 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004431
Hristo Venev75b28af2019-08-26 17:23:46 +00004432 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4433 if (size == SIZE_MAX)
4434 return -EOVERFLOW;
4435
4436 rings = io_mem_alloc(size);
4437 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004438 return -ENOMEM;
4439
Hristo Venev75b28af2019-08-26 17:23:46 +00004440 ctx->rings = rings;
4441 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4442 rings->sq_ring_mask = p->sq_entries - 1;
4443 rings->cq_ring_mask = p->cq_entries - 1;
4444 rings->sq_ring_entries = p->sq_entries;
4445 rings->cq_ring_entries = p->cq_entries;
4446 ctx->sq_mask = rings->sq_ring_mask;
4447 ctx->cq_mask = rings->cq_ring_mask;
4448 ctx->sq_entries = rings->sq_ring_entries;
4449 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004450
4451 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4452 if (size == SIZE_MAX)
4453 return -EOVERFLOW;
4454
4455 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004456 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004457 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004458
Jens Axboe2b188cc2019-01-07 10:46:33 -07004459 return 0;
4460}
4461
4462/*
4463 * Allocate an anonymous fd, this is what constitutes the application
4464 * visible backing of an io_uring instance. The application mmaps this
4465 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4466 * we have to tie this fd to a socket for file garbage collection purposes.
4467 */
4468static int io_uring_get_fd(struct io_ring_ctx *ctx)
4469{
4470 struct file *file;
4471 int ret;
4472
4473#if defined(CONFIG_UNIX)
4474 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4475 &ctx->ring_sock);
4476 if (ret)
4477 return ret;
4478#endif
4479
4480 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4481 if (ret < 0)
4482 goto err;
4483
4484 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4485 O_RDWR | O_CLOEXEC);
4486 if (IS_ERR(file)) {
4487 put_unused_fd(ret);
4488 ret = PTR_ERR(file);
4489 goto err;
4490 }
4491
4492#if defined(CONFIG_UNIX)
4493 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004494 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004495#endif
4496 fd_install(ret, file);
4497 return ret;
4498err:
4499#if defined(CONFIG_UNIX)
4500 sock_release(ctx->ring_sock);
4501 ctx->ring_sock = NULL;
4502#endif
4503 return ret;
4504}
4505
4506static int io_uring_create(unsigned entries, struct io_uring_params *p)
4507{
4508 struct user_struct *user = NULL;
4509 struct io_ring_ctx *ctx;
4510 bool account_mem;
4511 int ret;
4512
4513 if (!entries || entries > IORING_MAX_ENTRIES)
4514 return -EINVAL;
4515
4516 /*
4517 * Use twice as many entries for the CQ ring. It's possible for the
4518 * application to drive a higher depth than the size of the SQ ring,
4519 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004520 * some flexibility in overcommitting a bit. If the application has
4521 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4522 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004523 */
4524 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004525 if (p->flags & IORING_SETUP_CQSIZE) {
4526 /*
4527 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4528 * to a power-of-two, if it isn't already. We do NOT impose
4529 * any cq vs sq ring sizing.
4530 */
4531 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4532 return -EINVAL;
4533 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4534 } else {
4535 p->cq_entries = 2 * p->sq_entries;
4536 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004537
4538 user = get_uid(current_user());
4539 account_mem = !capable(CAP_IPC_LOCK);
4540
4541 if (account_mem) {
4542 ret = io_account_mem(user,
4543 ring_pages(p->sq_entries, p->cq_entries));
4544 if (ret) {
4545 free_uid(user);
4546 return ret;
4547 }
4548 }
4549
4550 ctx = io_ring_ctx_alloc(p);
4551 if (!ctx) {
4552 if (account_mem)
4553 io_unaccount_mem(user, ring_pages(p->sq_entries,
4554 p->cq_entries));
4555 free_uid(user);
4556 return -ENOMEM;
4557 }
4558 ctx->compat = in_compat_syscall();
4559 ctx->account_mem = account_mem;
4560 ctx->user = user;
4561
4562 ret = io_allocate_scq_urings(ctx, p);
4563 if (ret)
4564 goto err;
4565
Jens Axboe6c271ce2019-01-10 11:22:30 -07004566 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004567 if (ret)
4568 goto err;
4569
Jens Axboe2b188cc2019-01-07 10:46:33 -07004570 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004571 p->sq_off.head = offsetof(struct io_rings, sq.head);
4572 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4573 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4574 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4575 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4576 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4577 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004578
4579 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004580 p->cq_off.head = offsetof(struct io_rings, cq.head);
4581 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4582 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4583 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4584 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4585 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004586
Jens Axboe044c1ab2019-10-28 09:15:33 -06004587 /*
4588 * Install ring fd as the very last thing, so we don't risk someone
4589 * having closed it before we finish setup
4590 */
4591 ret = io_uring_get_fd(ctx);
4592 if (ret < 0)
4593 goto err;
4594
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004595 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004596 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004597 return ret;
4598err:
4599 io_ring_ctx_wait_and_kill(ctx);
4600 return ret;
4601}
4602
4603/*
4604 * Sets up an aio uring context, and returns the fd. Applications asks for a
4605 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4606 * params structure passed in.
4607 */
4608static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4609{
4610 struct io_uring_params p;
4611 long ret;
4612 int i;
4613
4614 if (copy_from_user(&p, params, sizeof(p)))
4615 return -EFAULT;
4616 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4617 if (p.resv[i])
4618 return -EINVAL;
4619 }
4620
Jens Axboe6c271ce2019-01-10 11:22:30 -07004621 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004622 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004623 return -EINVAL;
4624
4625 ret = io_uring_create(entries, &p);
4626 if (ret < 0)
4627 return ret;
4628
4629 if (copy_to_user(params, &p, sizeof(p)))
4630 return -EFAULT;
4631
4632 return ret;
4633}
4634
4635SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4636 struct io_uring_params __user *, params)
4637{
4638 return io_uring_setup(entries, params);
4639}
4640
Jens Axboeedafcce2019-01-09 09:16:05 -07004641static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4642 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004643 __releases(ctx->uring_lock)
4644 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004645{
4646 int ret;
4647
Jens Axboe35fa71a2019-04-22 10:23:23 -06004648 /*
4649 * We're inside the ring mutex, if the ref is already dying, then
4650 * someone else killed the ctx or is already going through
4651 * io_uring_register().
4652 */
4653 if (percpu_ref_is_dying(&ctx->refs))
4654 return -ENXIO;
4655
Jens Axboeedafcce2019-01-09 09:16:05 -07004656 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004657
4658 /*
4659 * Drop uring mutex before waiting for references to exit. If another
4660 * thread is currently inside io_uring_enter() it might need to grab
4661 * the uring_lock to make progress. If we hold it here across the drain
4662 * wait, then we can deadlock. It's safe to drop the mutex here, since
4663 * no new references will come in after we've killed the percpu ref.
4664 */
4665 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004666 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004667 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004668
4669 switch (opcode) {
4670 case IORING_REGISTER_BUFFERS:
4671 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4672 break;
4673 case IORING_UNREGISTER_BUFFERS:
4674 ret = -EINVAL;
4675 if (arg || nr_args)
4676 break;
4677 ret = io_sqe_buffer_unregister(ctx);
4678 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004679 case IORING_REGISTER_FILES:
4680 ret = io_sqe_files_register(ctx, arg, nr_args);
4681 break;
4682 case IORING_UNREGISTER_FILES:
4683 ret = -EINVAL;
4684 if (arg || nr_args)
4685 break;
4686 ret = io_sqe_files_unregister(ctx);
4687 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004688 case IORING_REGISTER_FILES_UPDATE:
4689 ret = io_sqe_files_update(ctx, arg, nr_args);
4690 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004691 case IORING_REGISTER_EVENTFD:
4692 ret = -EINVAL;
4693 if (nr_args != 1)
4694 break;
4695 ret = io_eventfd_register(ctx, arg);
4696 break;
4697 case IORING_UNREGISTER_EVENTFD:
4698 ret = -EINVAL;
4699 if (arg || nr_args)
4700 break;
4701 ret = io_eventfd_unregister(ctx);
4702 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004703 default:
4704 ret = -EINVAL;
4705 break;
4706 }
4707
4708 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004709 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004710 percpu_ref_reinit(&ctx->refs);
4711 return ret;
4712}
4713
4714SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4715 void __user *, arg, unsigned int, nr_args)
4716{
4717 struct io_ring_ctx *ctx;
4718 long ret = -EBADF;
4719 struct fd f;
4720
4721 f = fdget(fd);
4722 if (!f.file)
4723 return -EBADF;
4724
4725 ret = -EOPNOTSUPP;
4726 if (f.file->f_op != &io_uring_fops)
4727 goto out_fput;
4728
4729 ctx = f.file->private_data;
4730
4731 mutex_lock(&ctx->uring_lock);
4732 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4733 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004734 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4735 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004736out_fput:
4737 fdput(f);
4738 return ret;
4739}
4740
Jens Axboe2b188cc2019-01-07 10:46:33 -07004741static int __init io_uring_init(void)
4742{
4743 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4744 return 0;
4745};
4746__initcall(io_uring_init);