blob: 9500780bcaeacb9e3a0089d04cf408c101a9de4f [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 Axboe7c9e7f02019-11-12 08:15:53 -0700344#define REQ_F_COMP_LOCKED 16384 /* completion under lock */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700345 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600346 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600347 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700348
Jens Axboefcb323c2019-10-24 12:39:47 -0600349 struct list_head inflight_entry;
350
Jens Axboe561fb042019-10-24 07:25:42 -0600351 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700352};
353
354#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700355#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700356
Jens Axboe9a56a232019-01-09 09:06:50 -0700357struct io_submit_state {
358 struct blk_plug plug;
359
360 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700361 * io_kiocb alloc cache
362 */
363 void *reqs[IO_IOPOLL_BATCH];
364 unsigned int free_reqs;
365 unsigned int cur_req;
366
367 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700368 * File reference cache
369 */
370 struct file *file;
371 unsigned int fd;
372 unsigned int has_refs;
373 unsigned int used_refs;
374 unsigned int ios_left;
375};
376
Jens Axboe561fb042019-10-24 07:25:42 -0600377static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700378static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800379static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800380static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700381static void io_double_put_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600382
Jens Axboe2b188cc2019-01-07 10:46:33 -0700383static struct kmem_cache *req_cachep;
384
385static const struct file_operations io_uring_fops;
386
387struct sock *io_uring_get_socket(struct file *file)
388{
389#if defined(CONFIG_UNIX)
390 if (file->f_op == &io_uring_fops) {
391 struct io_ring_ctx *ctx = file->private_data;
392
393 return ctx->ring_sock->sk;
394 }
395#endif
396 return NULL;
397}
398EXPORT_SYMBOL(io_uring_get_socket);
399
400static void io_ring_ctx_ref_free(struct percpu_ref *ref)
401{
402 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
403
Jens Axboe206aefd2019-11-07 18:27:42 -0700404 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700405}
406
407static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
408{
409 struct io_ring_ctx *ctx;
410
411 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
412 if (!ctx)
413 return NULL;
414
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700415 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
416 if (!ctx->fallback_req)
417 goto err;
418
Jens Axboe206aefd2019-11-07 18:27:42 -0700419 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
420 if (!ctx->completions)
421 goto err;
422
Roman Gushchin21482892019-05-07 10:01:48 -0700423 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700424 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
425 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700426
427 ctx->flags = p->flags;
428 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700429 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700430 init_completion(&ctx->completions[0]);
431 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700432 mutex_init(&ctx->uring_lock);
433 init_waitqueue_head(&ctx->wait);
434 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700435 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700436 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600437 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600438 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600439 init_waitqueue_head(&ctx->inflight_wait);
440 spin_lock_init(&ctx->inflight_lock);
441 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700442 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700443err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700444 if (ctx->fallback_req)
445 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700446 kfree(ctx->completions);
447 kfree(ctx);
448 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700449}
450
Bob Liu9d858b22019-11-13 18:06:25 +0800451static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600452{
Jackie Liua197f662019-11-08 08:09:12 -0700453 struct io_ring_ctx *ctx = req->ctx;
454
Jens Axboe498ccd92019-10-25 10:04:25 -0600455 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
456 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600457}
458
Bob Liu9d858b22019-11-13 18:06:25 +0800459static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600460{
Bob Liu9d858b22019-11-13 18:06:25 +0800461 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
462 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600463
Bob Liu9d858b22019-11-13 18:06:25 +0800464 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600465}
466
467static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600468{
469 struct io_kiocb *req;
470
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600471 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800472 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600473 list_del_init(&req->list);
474 return req;
475 }
476
477 return NULL;
478}
479
Jens Axboe5262f562019-09-17 12:26:57 -0600480static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
481{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600482 struct io_kiocb *req;
483
484 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800485 if (req && !__req_need_defer(req)) {
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600486 list_del_init(&req->list);
487 return req;
488 }
489
490 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600491}
492
Jens Axboede0617e2019-04-06 21:51:27 -0600493static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700494{
Hristo Venev75b28af2019-08-26 17:23:46 +0000495 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700496
Hristo Venev75b28af2019-08-26 17:23:46 +0000497 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700498 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000499 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700500
Jens Axboe2b188cc2019-01-07 10:46:33 -0700501 if (wq_has_sleeper(&ctx->cq_wait)) {
502 wake_up_interruptible(&ctx->cq_wait);
503 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
504 }
505 }
506}
507
Jens Axboe561fb042019-10-24 07:25:42 -0600508static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600509{
Jens Axboe561fb042019-10-24 07:25:42 -0600510 u8 opcode = READ_ONCE(sqe->opcode);
511
512 return !(opcode == IORING_OP_READ_FIXED ||
513 opcode == IORING_OP_WRITE_FIXED);
514}
515
516static inline bool io_prep_async_work(struct io_kiocb *req)
517{
518 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600519
Jens Axboe6cc47d12019-09-18 11:18:23 -0600520 if (req->submit.sqe) {
521 switch (req->submit.sqe->opcode) {
522 case IORING_OP_WRITEV:
523 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600524 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700525 /* fall-through */
526 case IORING_OP_READV:
527 case IORING_OP_READ_FIXED:
528 case IORING_OP_SENDMSG:
529 case IORING_OP_RECVMSG:
530 case IORING_OP_ACCEPT:
531 case IORING_OP_POLL_ADD:
532 /*
533 * We know REQ_F_ISREG is not set on some of these
534 * opcodes, but this enables us to keep the check in
535 * just one place.
536 */
537 if (!(req->flags & REQ_F_ISREG))
538 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600539 break;
540 }
Jens Axboe561fb042019-10-24 07:25:42 -0600541 if (io_sqe_needs_user(req->submit.sqe))
542 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600543 }
544
Jens Axboe561fb042019-10-24 07:25:42 -0600545 return do_hashed;
546}
547
Jackie Liua197f662019-11-08 08:09:12 -0700548static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600549{
550 bool do_hashed = io_prep_async_work(req);
Jackie Liua197f662019-11-08 08:09:12 -0700551 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -0600552
553 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
554 req->flags);
555 if (!do_hashed) {
556 io_wq_enqueue(ctx->io_wq, &req->work);
557 } else {
558 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
559 file_inode(req->file));
560 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600561}
562
Jens Axboe5262f562019-09-17 12:26:57 -0600563static void io_kill_timeout(struct io_kiocb *req)
564{
565 int ret;
566
567 ret = hrtimer_try_to_cancel(&req->timeout.timer);
568 if (ret != -1) {
569 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600570 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700571 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800572 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600573 }
574}
575
576static void io_kill_timeouts(struct io_ring_ctx *ctx)
577{
578 struct io_kiocb *req, *tmp;
579
580 spin_lock_irq(&ctx->completion_lock);
581 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
582 io_kill_timeout(req);
583 spin_unlock_irq(&ctx->completion_lock);
584}
585
Jens Axboede0617e2019-04-06 21:51:27 -0600586static void io_commit_cqring(struct io_ring_ctx *ctx)
587{
588 struct io_kiocb *req;
589
Jens Axboe5262f562019-09-17 12:26:57 -0600590 while ((req = io_get_timeout_req(ctx)) != NULL)
591 io_kill_timeout(req);
592
Jens Axboede0617e2019-04-06 21:51:27 -0600593 __io_commit_cqring(ctx);
594
595 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800596 if (req->flags & REQ_F_SHADOW_DRAIN) {
597 /* Just for drain, free it. */
598 __io_free_req(req);
599 continue;
600 }
Jens Axboede0617e2019-04-06 21:51:27 -0600601 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700602 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600603 }
604}
605
Jens Axboe2b188cc2019-01-07 10:46:33 -0700606static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
607{
Hristo Venev75b28af2019-08-26 17:23:46 +0000608 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700609 unsigned tail;
610
611 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200612 /*
613 * writes to the cq entry need to come after reading head; the
614 * control dependency is enough as we're using WRITE_ONCE to
615 * fill the cq entry
616 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000617 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700618 return NULL;
619
620 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000621 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700622}
623
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700624static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
625{
626 if (waitqueue_active(&ctx->wait))
627 wake_up(&ctx->wait);
628 if (waitqueue_active(&ctx->sqo_wait))
629 wake_up(&ctx->sqo_wait);
630 if (ctx->cq_ev_fd)
631 eventfd_signal(ctx->cq_ev_fd, 1);
632}
633
634static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
635{
636 struct io_rings *rings = ctx->rings;
637 struct io_uring_cqe *cqe;
638 struct io_kiocb *req;
639 unsigned long flags;
640 LIST_HEAD(list);
641
642 if (!force) {
643 if (list_empty_careful(&ctx->cq_overflow_list))
644 return;
645 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
646 rings->cq_ring_entries))
647 return;
648 }
649
650 spin_lock_irqsave(&ctx->completion_lock, flags);
651
652 /* if force is set, the ring is going away. always drop after that */
653 if (force)
654 ctx->cq_overflow_flushed = true;
655
656 while (!list_empty(&ctx->cq_overflow_list)) {
657 cqe = io_get_cqring(ctx);
658 if (!cqe && !force)
659 break;
660
661 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
662 list);
663 list_move(&req->list, &list);
664 if (cqe) {
665 WRITE_ONCE(cqe->user_data, req->user_data);
666 WRITE_ONCE(cqe->res, req->result);
667 WRITE_ONCE(cqe->flags, 0);
668 } else {
669 WRITE_ONCE(ctx->rings->cq_overflow,
670 atomic_inc_return(&ctx->cached_cq_overflow));
671 }
672 }
673
674 io_commit_cqring(ctx);
675 spin_unlock_irqrestore(&ctx->completion_lock, flags);
676 io_cqring_ev_posted(ctx);
677
678 while (!list_empty(&list)) {
679 req = list_first_entry(&list, struct io_kiocb, list);
680 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800681 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700682 }
683}
684
Jens Axboe78e19bb2019-11-06 15:21:34 -0700685static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700686{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700687 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700688 struct io_uring_cqe *cqe;
689
Jens Axboe78e19bb2019-11-06 15:21:34 -0700690 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700691
Jens Axboe2b188cc2019-01-07 10:46:33 -0700692 /*
693 * If we can't get a cq entry, userspace overflowed the
694 * submission (by quite a lot). Increment the overflow count in
695 * the ring.
696 */
697 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700698 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700699 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700700 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600701 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700702 } else if (ctx->cq_overflow_flushed) {
Jens Axboe498ccd92019-10-25 10:04:25 -0600703 WRITE_ONCE(ctx->rings->cq_overflow,
704 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700705 } else {
706 refcount_inc(&req->refs);
707 req->result = res;
708 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700709 }
710}
711
Jens Axboe78e19bb2019-11-06 15:21:34 -0700712static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700713{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700714 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700715 unsigned long flags;
716
717 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700718 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700719 io_commit_cqring(ctx);
720 spin_unlock_irqrestore(&ctx->completion_lock, flags);
721
Jens Axboe8c838782019-03-12 15:48:16 -0600722 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700723}
724
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700725static inline bool io_is_fallback_req(struct io_kiocb *req)
726{
727 return req == (struct io_kiocb *)
728 ((unsigned long) req->ctx->fallback_req & ~1UL);
729}
730
731static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
732{
733 struct io_kiocb *req;
734
735 req = ctx->fallback_req;
736 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
737 return req;
738
739 return NULL;
740}
741
Jens Axboe2579f912019-01-09 09:10:43 -0700742static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
743 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700744{
Jens Axboefd6fab22019-03-14 16:30:06 -0600745 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700746 struct io_kiocb *req;
747
748 if (!percpu_ref_tryget(&ctx->refs))
749 return NULL;
750
Jens Axboe2579f912019-01-09 09:10:43 -0700751 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600752 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700753 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700754 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700755 } else if (!state->free_reqs) {
756 size_t sz;
757 int ret;
758
759 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600760 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
761
762 /*
763 * Bulk alloc is all-or-nothing. If we fail to get a batch,
764 * retry single alloc to be on the safe side.
765 */
766 if (unlikely(ret <= 0)) {
767 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
768 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700769 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600770 ret = 1;
771 }
Jens Axboe2579f912019-01-09 09:10:43 -0700772 state->free_reqs = ret - 1;
773 state->cur_req = 1;
774 req = state->reqs[0];
775 } else {
776 req = state->reqs[state->cur_req];
777 state->free_reqs--;
778 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700779 }
780
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700781got_it:
Jens Axboe60c112b2019-06-21 10:20:18 -0600782 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700783 req->ctx = ctx;
784 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600785 /* one is dropped after submission, the other at completion */
786 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600787 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600788 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700789 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700790fallback:
791 req = io_get_fallback_req(ctx);
792 if (req)
793 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300794 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700795 return NULL;
796}
797
Jens Axboedef596e2019-01-09 08:59:42 -0700798static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
799{
800 if (*nr) {
801 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300802 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700803 *nr = 0;
804 }
805}
806
Jens Axboe9e645e112019-05-10 16:07:28 -0600807static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700808{
Jens Axboefcb323c2019-10-24 12:39:47 -0600809 struct io_ring_ctx *ctx = req->ctx;
810
Jens Axboe09bb8392019-03-13 12:39:28 -0600811 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
812 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600813 if (req->flags & REQ_F_INFLIGHT) {
814 unsigned long flags;
815
816 spin_lock_irqsave(&ctx->inflight_lock, flags);
817 list_del(&req->inflight_entry);
818 if (waitqueue_active(&ctx->inflight_wait))
819 wake_up(&ctx->inflight_wait);
820 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
821 }
822 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700823 if (likely(!io_is_fallback_req(req)))
824 kmem_cache_free(req_cachep, req);
825 else
826 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600827}
828
Jackie Liua197f662019-11-08 08:09:12 -0700829static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -0700830{
Jackie Liua197f662019-11-08 08:09:12 -0700831 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700832 int ret;
833
834 ret = hrtimer_try_to_cancel(&req->timeout.timer);
835 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700836 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700837 io_commit_cqring(ctx);
838 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800839 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700840 return true;
841 }
842
843 return false;
844}
845
Jens Axboeba816ad2019-09-28 11:36:45 -0600846static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600847{
Jens Axboe2665abf2019-11-05 12:40:47 -0700848 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600849 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700850 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600851
852 /*
853 * The list should never be empty when we are called here. But could
854 * potentially happen if the chain is messed up, check to be on the
855 * safe side.
856 */
857 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700858 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700859 list_del_init(&nxt->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600860 if (!list_empty(&req->link_list)) {
861 INIT_LIST_HEAD(&nxt->link_list);
862 list_splice(&req->link_list, &nxt->link_list);
863 nxt->flags |= REQ_F_LINK;
864 }
865
Jens Axboeba816ad2019-09-28 11:36:45 -0600866 /*
867 * If we're in async work, we can continue processing the chain
868 * in this context instead of having to queue up new async work.
869 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700870 if (req->flags & REQ_F_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700871 wake_ev = io_link_cancel_timeout(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700872
873 /* we dropped this link, get next */
874 nxt = list_first_entry_or_null(&req->link_list,
875 struct io_kiocb, list);
Jens Axboe960e4322019-11-12 07:56:39 -0700876 } else if (nxtptr && io_wq_current_is_worker()) {
Jens Axboeba816ad2019-09-28 11:36:45 -0600877 *nxtptr = nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700878 break;
879 } else {
Jackie Liua197f662019-11-08 08:09:12 -0700880 io_queue_async_work(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700881 break;
882 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600883 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700884
885 if (wake_ev)
886 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600887}
888
889/*
890 * Called if REQ_F_LINK is set, and we fail the head request
891 */
892static void io_fail_links(struct io_kiocb *req)
893{
Jens Axboe2665abf2019-11-05 12:40:47 -0700894 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600895 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700896 unsigned long flags;
897
898 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600899
900 while (!list_empty(&req->link_list)) {
901 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700902 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600903
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200904 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700905
906 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
907 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700908 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700909 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700910 io_cqring_fill_event(link, -ECANCELED);
911 io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700912 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600913 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700914
915 io_commit_cqring(ctx);
916 spin_unlock_irqrestore(&ctx->completion_lock, flags);
917 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600918}
919
Jackie Liuc69f8db2019-11-09 11:00:08 +0800920static void io_free_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600921{
Jens Axboe2665abf2019-11-05 12:40:47 -0700922 if (likely(!(req->flags & REQ_F_LINK))) {
923 __io_free_req(req);
924 return;
925 }
926
Jens Axboe9e645e112019-05-10 16:07:28 -0600927 /*
928 * If LINK is set, we have dependent requests in this chain. If we
929 * didn't fail this request, queue the first one up, moving any other
930 * dependencies to the next request. In case of failure, fail the rest
931 * of the chain.
932 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700933 if (req->flags & REQ_F_FAIL_LINK) {
934 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700935 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
936 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700937 struct io_ring_ctx *ctx = req->ctx;
938 unsigned long flags;
939
940 /*
941 * If this is a timeout link, we could be racing with the
942 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700943 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700944 */
945 spin_lock_irqsave(&ctx->completion_lock, flags);
946 io_req_link_next(req, nxt);
947 spin_unlock_irqrestore(&ctx->completion_lock, flags);
948 } else {
949 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600950 }
951
952 __io_free_req(req);
953}
954
Jackie Liuc69f8db2019-11-09 11:00:08 +0800955static void io_free_req(struct io_kiocb *req)
956{
957 io_free_req_find_next(req, NULL);
958}
959
Jens Axboeba816ad2019-09-28 11:36:45 -0600960/*
961 * Drop reference to request, return next in chain (if there is one) if this
962 * was the last reference to this request.
963 */
Jackie Liuec9c02a2019-11-08 23:50:36 +0800964static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -0600965{
Jens Axboeba816ad2019-09-28 11:36:45 -0600966 struct io_kiocb *nxt = NULL;
967
Jens Axboee65ef562019-03-12 10:16:44 -0600968 if (refcount_dec_and_test(&req->refs))
Jackie Liuc69f8db2019-11-09 11:00:08 +0800969 io_free_req_find_next(req, &nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600970
Jens Axboeba816ad2019-09-28 11:36:45 -0600971 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600972 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600973 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600974 else
Jackie Liua197f662019-11-08 08:09:12 -0700975 io_queue_async_work(nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600976 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700977}
978
Jackie Liuec9c02a2019-11-08 23:50:36 +0800979static void io_put_req(struct io_kiocb *req)
980{
981 if (refcount_dec_and_test(&req->refs))
Jackie Liuc69f8db2019-11-09 11:00:08 +0800982 io_free_req(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800983}
984
Jens Axboe78e19bb2019-11-06 15:21:34 -0700985static void io_double_put_req(struct io_kiocb *req)
986{
987 /* drop both submit and complete references */
988 if (refcount_sub_and_test(2, &req->refs))
989 __io_free_req(req);
990}
991
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700992static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -0600993{
Jens Axboe84f97dc2019-11-06 11:27:53 -0700994 struct io_rings *rings = ctx->rings;
995
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700996 /*
997 * noflush == true is from the waitqueue handler, just ensure we wake
998 * up the task, and the next invocation will flush the entries. We
999 * cannot safely to it from here.
1000 */
1001 if (noflush && !list_empty(&ctx->cq_overflow_list))
1002 return -1U;
1003
1004 io_cqring_overflow_flush(ctx, false);
1005
Jens Axboea3a0e432019-08-20 11:03:11 -06001006 /* See comment at the top of this file */
1007 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001008 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001009}
1010
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001011static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1012{
1013 struct io_rings *rings = ctx->rings;
1014
1015 /* make sure SQ entry isn't read before tail */
1016 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1017}
1018
Jens Axboedef596e2019-01-09 08:59:42 -07001019/*
1020 * Find and free completed poll iocbs
1021 */
1022static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1023 struct list_head *done)
1024{
1025 void *reqs[IO_IOPOLL_BATCH];
1026 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001027 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001028
Jens Axboe09bb8392019-03-13 12:39:28 -06001029 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001030 while (!list_empty(done)) {
1031 req = list_first_entry(done, struct io_kiocb, list);
1032 list_del(&req->list);
1033
Jens Axboe78e19bb2019-11-06 15:21:34 -07001034 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001035 (*nr_events)++;
1036
Jens Axboe09bb8392019-03-13 12:39:28 -06001037 if (refcount_dec_and_test(&req->refs)) {
1038 /* If we're not using fixed files, we have to pair the
1039 * completion part with the file put. Use regular
1040 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001041 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001042 */
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001043 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1044 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001045 reqs[to_free++] = req;
1046 if (to_free == ARRAY_SIZE(reqs))
1047 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001048 } else {
Jackie Liuc69f8db2019-11-09 11:00:08 +08001049 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001050 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001051 }
Jens Axboedef596e2019-01-09 08:59:42 -07001052 }
Jens Axboedef596e2019-01-09 08:59:42 -07001053
Jens Axboe09bb8392019-03-13 12:39:28 -06001054 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001055 io_free_req_many(ctx, reqs, &to_free);
1056}
1057
1058static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1059 long min)
1060{
1061 struct io_kiocb *req, *tmp;
1062 LIST_HEAD(done);
1063 bool spin;
1064 int ret;
1065
1066 /*
1067 * Only spin for completions if we don't have multiple devices hanging
1068 * off our complete list, and we're under the requested amount.
1069 */
1070 spin = !ctx->poll_multi_file && *nr_events < min;
1071
1072 ret = 0;
1073 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1074 struct kiocb *kiocb = &req->rw;
1075
1076 /*
1077 * Move completed entries to our local list. If we find a
1078 * request that requires polling, break out and complete
1079 * the done list first, if we have entries there.
1080 */
1081 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1082 list_move_tail(&req->list, &done);
1083 continue;
1084 }
1085 if (!list_empty(&done))
1086 break;
1087
1088 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1089 if (ret < 0)
1090 break;
1091
1092 if (ret && spin)
1093 spin = false;
1094 ret = 0;
1095 }
1096
1097 if (!list_empty(&done))
1098 io_iopoll_complete(ctx, nr_events, &done);
1099
1100 return ret;
1101}
1102
1103/*
1104 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1105 * non-spinning poll check - we'll still enter the driver poll loop, but only
1106 * as a non-spinning completion check.
1107 */
1108static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1109 long min)
1110{
Jens Axboe08f54392019-08-21 22:19:11 -06001111 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001112 int ret;
1113
1114 ret = io_do_iopoll(ctx, nr_events, min);
1115 if (ret < 0)
1116 return ret;
1117 if (!min || *nr_events >= min)
1118 return 0;
1119 }
1120
1121 return 1;
1122}
1123
1124/*
1125 * We can't just wait for polled events to come to us, we have to actively
1126 * find and complete them.
1127 */
1128static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1129{
1130 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1131 return;
1132
1133 mutex_lock(&ctx->uring_lock);
1134 while (!list_empty(&ctx->poll_list)) {
1135 unsigned int nr_events = 0;
1136
1137 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001138
1139 /*
1140 * Ensure we allow local-to-the-cpu processing to take place,
1141 * in this case we need to ensure that we reap all events.
1142 */
1143 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001144 }
1145 mutex_unlock(&ctx->uring_lock);
1146}
1147
Jens Axboe2b2ed972019-10-25 10:06:15 -06001148static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1149 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001150{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001151 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001152
1153 do {
1154 int tmin = 0;
1155
Jens Axboe500f9fb2019-08-19 12:15:59 -06001156 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001157 * Don't enter poll loop if we already have events pending.
1158 * If we do, we can potentially be spinning for commands that
1159 * already triggered a CQE (eg in error).
1160 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001161 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001162 break;
1163
1164 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001165 * If a submit got punted to a workqueue, we can have the
1166 * application entering polling for a command before it gets
1167 * issued. That app will hold the uring_lock for the duration
1168 * of the poll right here, so we need to take a breather every
1169 * now and then to ensure that the issue has a chance to add
1170 * the poll to the issued list. Otherwise we can spin here
1171 * forever, while the workqueue is stuck trying to acquire the
1172 * very same mutex.
1173 */
1174 if (!(++iters & 7)) {
1175 mutex_unlock(&ctx->uring_lock);
1176 mutex_lock(&ctx->uring_lock);
1177 }
1178
Jens Axboedef596e2019-01-09 08:59:42 -07001179 if (*nr_events < min)
1180 tmin = min - *nr_events;
1181
1182 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1183 if (ret <= 0)
1184 break;
1185 ret = 0;
1186 } while (min && !*nr_events && !need_resched());
1187
Jens Axboe2b2ed972019-10-25 10:06:15 -06001188 return ret;
1189}
1190
1191static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1192 long min)
1193{
1194 int ret;
1195
1196 /*
1197 * We disallow the app entering submit/complete with polling, but we
1198 * still need to lock the ring to prevent racing with polled issue
1199 * that got punted to a workqueue.
1200 */
1201 mutex_lock(&ctx->uring_lock);
1202 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001203 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001204 return ret;
1205}
1206
Jens Axboe491381ce2019-10-17 09:20:46 -06001207static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001208{
Jens Axboe491381ce2019-10-17 09:20:46 -06001209 /*
1210 * Tell lockdep we inherited freeze protection from submission
1211 * thread.
1212 */
1213 if (req->flags & REQ_F_ISREG) {
1214 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001215
Jens Axboe491381ce2019-10-17 09:20:46 -06001216 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001217 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001218 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001219}
1220
Jens Axboeba816ad2019-09-28 11:36:45 -06001221static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001222{
1223 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1224
Jens Axboe491381ce2019-10-17 09:20:46 -06001225 if (kiocb->ki_flags & IOCB_WRITE)
1226 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001227
Jens Axboe9e645e112019-05-10 16:07:28 -06001228 if ((req->flags & REQ_F_LINK) && res != req->result)
1229 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001230 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001231}
1232
1233static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1234{
1235 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1236
1237 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001238 io_put_req(req);
Jens Axboeba816ad2019-09-28 11:36:45 -06001239}
1240
1241static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1242{
1243 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001244 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001245
1246 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001247 io_put_req_find_next(req, &nxt);
1248
1249 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001250}
1251
Jens Axboedef596e2019-01-09 08:59:42 -07001252static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1253{
1254 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1255
Jens Axboe491381ce2019-10-17 09:20:46 -06001256 if (kiocb->ki_flags & IOCB_WRITE)
1257 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001258
Jens Axboe9e645e112019-05-10 16:07:28 -06001259 if ((req->flags & REQ_F_LINK) && res != req->result)
1260 req->flags |= REQ_F_FAIL_LINK;
1261 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001262 if (res != -EAGAIN)
1263 req->flags |= REQ_F_IOPOLL_COMPLETED;
1264}
1265
1266/*
1267 * After the iocb has been issued, it's safe to be found on the poll list.
1268 * Adding the kiocb to the list AFTER submission ensures that we don't
1269 * find it from a io_iopoll_getevents() thread before the issuer is done
1270 * accessing the kiocb cookie.
1271 */
1272static void io_iopoll_req_issued(struct io_kiocb *req)
1273{
1274 struct io_ring_ctx *ctx = req->ctx;
1275
1276 /*
1277 * Track whether we have multiple files in our lists. This will impact
1278 * how we do polling eventually, not spinning if we're on potentially
1279 * different devices.
1280 */
1281 if (list_empty(&ctx->poll_list)) {
1282 ctx->poll_multi_file = false;
1283 } else if (!ctx->poll_multi_file) {
1284 struct io_kiocb *list_req;
1285
1286 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1287 list);
1288 if (list_req->rw.ki_filp != req->rw.ki_filp)
1289 ctx->poll_multi_file = true;
1290 }
1291
1292 /*
1293 * For fast devices, IO may have already completed. If it has, add
1294 * it to the front so we find it first.
1295 */
1296 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1297 list_add(&req->list, &ctx->poll_list);
1298 else
1299 list_add_tail(&req->list, &ctx->poll_list);
1300}
1301
Jens Axboe3d6770f2019-04-13 11:50:54 -06001302static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001303{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001304 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001305 int diff = state->has_refs - state->used_refs;
1306
1307 if (diff)
1308 fput_many(state->file, diff);
1309 state->file = NULL;
1310 }
1311}
1312
1313/*
1314 * Get as many references to a file as we have IOs left in this submission,
1315 * assuming most submissions are for one file, or at least that each file
1316 * has more than one submission.
1317 */
1318static struct file *io_file_get(struct io_submit_state *state, int fd)
1319{
1320 if (!state)
1321 return fget(fd);
1322
1323 if (state->file) {
1324 if (state->fd == fd) {
1325 state->used_refs++;
1326 state->ios_left--;
1327 return state->file;
1328 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001329 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001330 }
1331 state->file = fget_many(fd, state->ios_left);
1332 if (!state->file)
1333 return NULL;
1334
1335 state->fd = fd;
1336 state->has_refs = state->ios_left;
1337 state->used_refs = 1;
1338 state->ios_left--;
1339 return state->file;
1340}
1341
Jens Axboe2b188cc2019-01-07 10:46:33 -07001342/*
1343 * If we tracked the file through the SCM inflight mechanism, we could support
1344 * any file. For now, just ensure that anything potentially problematic is done
1345 * inline.
1346 */
1347static bool io_file_supports_async(struct file *file)
1348{
1349 umode_t mode = file_inode(file)->i_mode;
1350
1351 if (S_ISBLK(mode) || S_ISCHR(mode))
1352 return true;
1353 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1354 return true;
1355
1356 return false;
1357}
1358
Pavel Begunkov267bc902019-11-07 01:41:08 +03001359static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001360{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001361 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001362 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001363 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001364 unsigned ioprio;
1365 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001366
Jens Axboe09bb8392019-03-13 12:39:28 -06001367 if (!req->file)
1368 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001369
Jens Axboe491381ce2019-10-17 09:20:46 -06001370 if (S_ISREG(file_inode(req->file)->i_mode))
1371 req->flags |= REQ_F_ISREG;
1372
1373 /*
1374 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1375 * we know to async punt it even if it was opened O_NONBLOCK
1376 */
1377 if (force_nonblock && !io_file_supports_async(req->file)) {
1378 req->flags |= REQ_F_MUST_PUNT;
1379 return -EAGAIN;
1380 }
Jens Axboe6b063142019-01-10 22:13:58 -07001381
Jens Axboe2b188cc2019-01-07 10:46:33 -07001382 kiocb->ki_pos = READ_ONCE(sqe->off);
1383 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1384 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1385
1386 ioprio = READ_ONCE(sqe->ioprio);
1387 if (ioprio) {
1388 ret = ioprio_check_cap(ioprio);
1389 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001390 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001391
1392 kiocb->ki_ioprio = ioprio;
1393 } else
1394 kiocb->ki_ioprio = get_current_ioprio();
1395
1396 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1397 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001398 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001399
1400 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001401 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1402 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001403 req->flags |= REQ_F_NOWAIT;
1404
1405 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001406 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001407
Jens Axboedef596e2019-01-09 08:59:42 -07001408 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001409 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1410 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001411 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001412
Jens Axboedef596e2019-01-09 08:59:42 -07001413 kiocb->ki_flags |= IOCB_HIPRI;
1414 kiocb->ki_complete = io_complete_rw_iopoll;
1415 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001416 if (kiocb->ki_flags & IOCB_HIPRI)
1417 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001418 kiocb->ki_complete = io_complete_rw;
1419 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001421}
1422
1423static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1424{
1425 switch (ret) {
1426 case -EIOCBQUEUED:
1427 break;
1428 case -ERESTARTSYS:
1429 case -ERESTARTNOINTR:
1430 case -ERESTARTNOHAND:
1431 case -ERESTART_RESTARTBLOCK:
1432 /*
1433 * We can't just restart the syscall, since previously
1434 * submitted sqes may already be in progress. Just fail this
1435 * IO with EINTR.
1436 */
1437 ret = -EINTR;
1438 /* fall through */
1439 default:
1440 kiocb->ki_complete(kiocb, ret, 0);
1441 }
1442}
1443
Jens Axboeba816ad2019-09-28 11:36:45 -06001444static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1445 bool in_async)
1446{
1447 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1448 *nxt = __io_complete_rw(kiocb, ret);
1449 else
1450 io_rw_done(kiocb, ret);
1451}
1452
Jens Axboeedafcce2019-01-09 09:16:05 -07001453static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1454 const struct io_uring_sqe *sqe,
1455 struct iov_iter *iter)
1456{
1457 size_t len = READ_ONCE(sqe->len);
1458 struct io_mapped_ubuf *imu;
1459 unsigned index, buf_index;
1460 size_t offset;
1461 u64 buf_addr;
1462
1463 /* attempt to use fixed buffers without having provided iovecs */
1464 if (unlikely(!ctx->user_bufs))
1465 return -EFAULT;
1466
1467 buf_index = READ_ONCE(sqe->buf_index);
1468 if (unlikely(buf_index >= ctx->nr_user_bufs))
1469 return -EFAULT;
1470
1471 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1472 imu = &ctx->user_bufs[index];
1473 buf_addr = READ_ONCE(sqe->addr);
1474
1475 /* overflow */
1476 if (buf_addr + len < buf_addr)
1477 return -EFAULT;
1478 /* not inside the mapped region */
1479 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1480 return -EFAULT;
1481
1482 /*
1483 * May not be a start of buffer, set size appropriately
1484 * and advance us to the beginning.
1485 */
1486 offset = buf_addr - imu->ubuf;
1487 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001488
1489 if (offset) {
1490 /*
1491 * Don't use iov_iter_advance() here, as it's really slow for
1492 * using the latter parts of a big fixed buffer - it iterates
1493 * over each segment manually. We can cheat a bit here, because
1494 * we know that:
1495 *
1496 * 1) it's a BVEC iter, we set it up
1497 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1498 * first and last bvec
1499 *
1500 * So just find our index, and adjust the iterator afterwards.
1501 * If the offset is within the first bvec (or the whole first
1502 * bvec, just use iov_iter_advance(). This makes it easier
1503 * since we can just skip the first segment, which may not
1504 * be PAGE_SIZE aligned.
1505 */
1506 const struct bio_vec *bvec = imu->bvec;
1507
1508 if (offset <= bvec->bv_len) {
1509 iov_iter_advance(iter, offset);
1510 } else {
1511 unsigned long seg_skip;
1512
1513 /* skip first vec */
1514 offset -= bvec->bv_len;
1515 seg_skip = 1 + (offset >> PAGE_SHIFT);
1516
1517 iter->bvec = bvec + seg_skip;
1518 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001519 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001520 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001521 }
1522 }
1523
Jens Axboeedafcce2019-01-09 09:16:05 -07001524 return 0;
1525}
1526
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001527static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1528 const struct sqe_submit *s, struct iovec **iovec,
1529 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001530{
1531 const struct io_uring_sqe *sqe = s->sqe;
1532 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1533 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001534 u8 opcode;
1535
1536 /*
1537 * We're reading ->opcode for the second time, but the first read
1538 * doesn't care whether it's _FIXED or not, so it doesn't matter
1539 * whether ->opcode changes concurrently. The first read does care
1540 * about whether it is a READ or a WRITE, so we don't trust this read
1541 * for that purpose and instead let the caller pass in the read/write
1542 * flag.
1543 */
1544 opcode = READ_ONCE(sqe->opcode);
1545 if (opcode == IORING_OP_READ_FIXED ||
1546 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001547 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001548 *iovec = NULL;
1549 return ret;
1550 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001551
1552 if (!s->has_user)
1553 return -EFAULT;
1554
1555#ifdef CONFIG_COMPAT
1556 if (ctx->compat)
1557 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1558 iovec, iter);
1559#endif
1560
1561 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1562}
1563
Jens Axboe32960612019-09-23 11:05:34 -06001564/*
1565 * For files that don't have ->read_iter() and ->write_iter(), handle them
1566 * by looping over ->read() or ->write() manually.
1567 */
1568static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1569 struct iov_iter *iter)
1570{
1571 ssize_t ret = 0;
1572
1573 /*
1574 * Don't support polled IO through this interface, and we can't
1575 * support non-blocking either. For the latter, this just causes
1576 * the kiocb to be handled from an async context.
1577 */
1578 if (kiocb->ki_flags & IOCB_HIPRI)
1579 return -EOPNOTSUPP;
1580 if (kiocb->ki_flags & IOCB_NOWAIT)
1581 return -EAGAIN;
1582
1583 while (iov_iter_count(iter)) {
1584 struct iovec iovec = iov_iter_iovec(iter);
1585 ssize_t nr;
1586
1587 if (rw == READ) {
1588 nr = file->f_op->read(file, iovec.iov_base,
1589 iovec.iov_len, &kiocb->ki_pos);
1590 } else {
1591 nr = file->f_op->write(file, iovec.iov_base,
1592 iovec.iov_len, &kiocb->ki_pos);
1593 }
1594
1595 if (nr < 0) {
1596 if (!ret)
1597 ret = nr;
1598 break;
1599 }
1600 ret += nr;
1601 if (nr != iovec.iov_len)
1602 break;
1603 iov_iter_advance(iter, nr);
1604 }
1605
1606 return ret;
1607}
1608
Pavel Begunkov267bc902019-11-07 01:41:08 +03001609static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1610 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001611{
1612 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1613 struct kiocb *kiocb = &req->rw;
1614 struct iov_iter iter;
1615 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001616 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001617 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001618
Pavel Begunkov267bc902019-11-07 01:41:08 +03001619 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001620 if (ret)
1621 return ret;
1622 file = kiocb->ki_filp;
1623
Jens Axboe2b188cc2019-01-07 10:46:33 -07001624 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001625 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001626
Pavel Begunkov267bc902019-11-07 01:41:08 +03001627 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001628 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001629 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001630
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001631 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001632 if (req->flags & REQ_F_LINK)
1633 req->result = read_size;
1634
Jens Axboe31b51512019-01-18 22:56:34 -07001635 iov_count = iov_iter_count(&iter);
1636 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001637 if (!ret) {
1638 ssize_t ret2;
1639
Jens Axboe32960612019-09-23 11:05:34 -06001640 if (file->f_op->read_iter)
1641 ret2 = call_read_iter(file, kiocb, &iter);
1642 else
1643 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1644
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001645 /*
1646 * In case of a short read, punt to async. This can happen
1647 * if we have data partially cached. Alternatively we can
1648 * return the short read, in which case the application will
1649 * need to issue another SQE and wait for it. That SQE will
1650 * need async punt anyway, so it's more efficient to do it
1651 * here.
1652 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001653 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1654 (req->flags & REQ_F_ISREG) &&
1655 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001656 ret2 = -EAGAIN;
1657 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001658 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001659 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001660 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001661 ret = -EAGAIN;
1662 }
1663 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001664 return ret;
1665}
1666
Pavel Begunkov267bc902019-11-07 01:41:08 +03001667static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1668 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001669{
1670 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1671 struct kiocb *kiocb = &req->rw;
1672 struct iov_iter iter;
1673 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001674 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001675 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001676
Pavel Begunkov267bc902019-11-07 01:41:08 +03001677 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001678 if (ret)
1679 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001680
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681 file = kiocb->ki_filp;
1682 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001683 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001684
Pavel Begunkov267bc902019-11-07 01:41:08 +03001685 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001686 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001687 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001688
Jens Axboe9e645e112019-05-10 16:07:28 -06001689 if (req->flags & REQ_F_LINK)
1690 req->result = ret;
1691
Jens Axboe31b51512019-01-18 22:56:34 -07001692 iov_count = iov_iter_count(&iter);
1693
1694 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001695 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001696 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001697
1698 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001699 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001700 ssize_t ret2;
1701
Jens Axboe2b188cc2019-01-07 10:46:33 -07001702 /*
1703 * Open-code file_start_write here to grab freeze protection,
1704 * which will be released by another thread in
1705 * io_complete_rw(). Fool lockdep by telling it the lock got
1706 * released so that it doesn't complain about the held lock when
1707 * we return to userspace.
1708 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001709 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001710 __sb_start_write(file_inode(file)->i_sb,
1711 SB_FREEZE_WRITE, true);
1712 __sb_writers_release(file_inode(file)->i_sb,
1713 SB_FREEZE_WRITE);
1714 }
1715 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001716
Jens Axboe32960612019-09-23 11:05:34 -06001717 if (file->f_op->write_iter)
1718 ret2 = call_write_iter(file, kiocb, &iter);
1719 else
1720 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001721 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001722 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001723 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001724 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001725 }
Jens Axboe31b51512019-01-18 22:56:34 -07001726out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001728 return ret;
1729}
1730
1731/*
1732 * IORING_OP_NOP just posts a completion event, nothing else.
1733 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001734static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735{
1736 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737
Jens Axboedef596e2019-01-09 08:59:42 -07001738 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1739 return -EINVAL;
1740
Jens Axboe78e19bb2019-11-06 15:21:34 -07001741 io_cqring_add_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001742 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001743 return 0;
1744}
1745
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001746static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1747{
Jens Axboe6b063142019-01-10 22:13:58 -07001748 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001749
Jens Axboe09bb8392019-03-13 12:39:28 -06001750 if (!req->file)
1751 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001752
Jens Axboe6b063142019-01-10 22:13:58 -07001753 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001754 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001755 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001756 return -EINVAL;
1757
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001758 return 0;
1759}
1760
1761static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001762 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001763{
1764 loff_t sqe_off = READ_ONCE(sqe->off);
1765 loff_t sqe_len = READ_ONCE(sqe->len);
1766 loff_t end = sqe_off + sqe_len;
1767 unsigned fsync_flags;
1768 int ret;
1769
1770 fsync_flags = READ_ONCE(sqe->fsync_flags);
1771 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1772 return -EINVAL;
1773
1774 ret = io_prep_fsync(req, sqe);
1775 if (ret)
1776 return ret;
1777
1778 /* fsync always requires a blocking context */
1779 if (force_nonblock)
1780 return -EAGAIN;
1781
1782 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1783 end > 0 ? end : LLONG_MAX,
1784 fsync_flags & IORING_FSYNC_DATASYNC);
1785
Jens Axboe9e645e112019-05-10 16:07:28 -06001786 if (ret < 0 && (req->flags & REQ_F_LINK))
1787 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001788 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001789 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001790 return 0;
1791}
1792
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001793static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1794{
1795 struct io_ring_ctx *ctx = req->ctx;
1796 int ret = 0;
1797
1798 if (!req->file)
1799 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001800
1801 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1802 return -EINVAL;
1803 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1804 return -EINVAL;
1805
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001806 return ret;
1807}
1808
1809static int io_sync_file_range(struct io_kiocb *req,
1810 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001811 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001812 bool force_nonblock)
1813{
1814 loff_t sqe_off;
1815 loff_t sqe_len;
1816 unsigned flags;
1817 int ret;
1818
1819 ret = io_prep_sfr(req, sqe);
1820 if (ret)
1821 return ret;
1822
1823 /* sync_file_range always requires a blocking context */
1824 if (force_nonblock)
1825 return -EAGAIN;
1826
1827 sqe_off = READ_ONCE(sqe->off);
1828 sqe_len = READ_ONCE(sqe->len);
1829 flags = READ_ONCE(sqe->sync_range_flags);
1830
1831 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1832
Jens Axboe9e645e112019-05-10 16:07:28 -06001833 if (ret < 0 && (req->flags & REQ_F_LINK))
1834 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001835 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001836 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001837 return 0;
1838}
1839
Jens Axboe0fa03c62019-04-19 13:34:07 -06001840#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001841static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001842 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001843 long (*fn)(struct socket *, struct user_msghdr __user *,
1844 unsigned int))
1845{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001846 struct socket *sock;
1847 int ret;
1848
1849 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1850 return -EINVAL;
1851
1852 sock = sock_from_file(req->file, &ret);
1853 if (sock) {
1854 struct user_msghdr __user *msg;
1855 unsigned flags;
1856
1857 flags = READ_ONCE(sqe->msg_flags);
1858 if (flags & MSG_DONTWAIT)
1859 req->flags |= REQ_F_NOWAIT;
1860 else if (force_nonblock)
1861 flags |= MSG_DONTWAIT;
1862
1863 msg = (struct user_msghdr __user *) (unsigned long)
1864 READ_ONCE(sqe->addr);
1865
Jens Axboeaa1fa282019-04-19 13:38:09 -06001866 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001867 if (force_nonblock && ret == -EAGAIN)
1868 return ret;
1869 }
1870
Jens Axboe78e19bb2019-11-06 15:21:34 -07001871 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001872 if (ret < 0 && (req->flags & REQ_F_LINK))
1873 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001874 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001875 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001876}
1877#endif
1878
1879static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001880 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001881{
1882#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001883 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1884 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001885#else
1886 return -EOPNOTSUPP;
1887#endif
1888}
1889
1890static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001891 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001892{
1893#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001894 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1895 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001896#else
1897 return -EOPNOTSUPP;
1898#endif
1899}
1900
Jens Axboe17f2fe32019-10-17 14:42:58 -06001901static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1902 struct io_kiocb **nxt, bool force_nonblock)
1903{
1904#if defined(CONFIG_NET)
1905 struct sockaddr __user *addr;
1906 int __user *addr_len;
1907 unsigned file_flags;
1908 int flags, ret;
1909
1910 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1911 return -EINVAL;
1912 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1913 return -EINVAL;
1914
1915 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1916 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1917 flags = READ_ONCE(sqe->accept_flags);
1918 file_flags = force_nonblock ? O_NONBLOCK : 0;
1919
1920 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1921 if (ret == -EAGAIN && force_nonblock) {
1922 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1923 return -EAGAIN;
1924 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001925 if (ret == -ERESTARTSYS)
1926 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001927 if (ret < 0 && (req->flags & REQ_F_LINK))
1928 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001929 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001930 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001931 return 0;
1932#else
1933 return -EOPNOTSUPP;
1934#endif
1935}
1936
Jens Axboe221c5eb2019-01-17 09:41:58 -07001937static void io_poll_remove_one(struct io_kiocb *req)
1938{
1939 struct io_poll_iocb *poll = &req->poll;
1940
1941 spin_lock(&poll->head->lock);
1942 WRITE_ONCE(poll->canceled, true);
1943 if (!list_empty(&poll->wait.entry)) {
1944 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07001945 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001946 }
1947 spin_unlock(&poll->head->lock);
1948
1949 list_del_init(&req->list);
1950}
1951
1952static void io_poll_remove_all(struct io_ring_ctx *ctx)
1953{
1954 struct io_kiocb *req;
1955
1956 spin_lock_irq(&ctx->completion_lock);
1957 while (!list_empty(&ctx->cancel_list)) {
1958 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1959 io_poll_remove_one(req);
1960 }
1961 spin_unlock_irq(&ctx->completion_lock);
1962}
1963
Jens Axboe47f46762019-11-09 17:43:02 -07001964static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
1965{
1966 struct io_kiocb *req;
1967
1968 list_for_each_entry(req, &ctx->cancel_list, list) {
1969 if (req->user_data != sqe_addr)
1970 continue;
1971 io_poll_remove_one(req);
1972 return 0;
1973 }
1974
1975 return -ENOENT;
1976}
1977
Jens Axboe221c5eb2019-01-17 09:41:58 -07001978/*
1979 * Find a running poll command that matches one specified in sqe->addr,
1980 * and remove it if found.
1981 */
1982static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1983{
1984 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07001985 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001986
1987 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1988 return -EINVAL;
1989 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1990 sqe->poll_events)
1991 return -EINVAL;
1992
1993 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07001994 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07001995 spin_unlock_irq(&ctx->completion_lock);
1996
Jens Axboe78e19bb2019-11-06 15:21:34 -07001997 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001998 if (ret < 0 && (req->flags & REQ_F_LINK))
1999 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002000 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002001 return 0;
2002}
2003
Jackie Liua197f662019-11-08 08:09:12 -07002004static void io_poll_complete(struct io_kiocb *req, __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002005{
Jackie Liua197f662019-11-08 08:09:12 -07002006 struct io_ring_ctx *ctx = req->ctx;
2007
Jens Axboe8c838782019-03-12 15:48:16 -06002008 req->poll.done = true;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002009 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002010 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002011}
2012
Jens Axboe561fb042019-10-24 07:25:42 -06002013static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002014{
Jens Axboe561fb042019-10-24 07:25:42 -06002015 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002016 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2017 struct io_poll_iocb *poll = &req->poll;
2018 struct poll_table_struct pt = { ._key = poll->events };
2019 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002020 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002021 __poll_t mask = 0;
2022
Jens Axboe561fb042019-10-24 07:25:42 -06002023 if (work->flags & IO_WQ_WORK_CANCEL)
2024 WRITE_ONCE(poll->canceled, true);
2025
Jens Axboe221c5eb2019-01-17 09:41:58 -07002026 if (!READ_ONCE(poll->canceled))
2027 mask = vfs_poll(poll->file, &pt) & poll->events;
2028
2029 /*
2030 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2031 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2032 * synchronize with them. In the cancellation case the list_del_init
2033 * itself is not actually needed, but harmless so we keep it in to
2034 * avoid further branches in the fast path.
2035 */
2036 spin_lock_irq(&ctx->completion_lock);
2037 if (!mask && !READ_ONCE(poll->canceled)) {
2038 add_wait_queue(poll->head, &poll->wait);
2039 spin_unlock_irq(&ctx->completion_lock);
2040 return;
2041 }
2042 list_del_init(&req->list);
Jackie Liua197f662019-11-08 08:09:12 -07002043 io_poll_complete(req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002044 spin_unlock_irq(&ctx->completion_lock);
2045
Jens Axboe8c838782019-03-12 15:48:16 -06002046 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002047
Jackie Liuec9c02a2019-11-08 23:50:36 +08002048 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002049 if (nxt)
2050 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002051}
2052
2053static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2054 void *key)
2055{
2056 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2057 wait);
2058 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2059 struct io_ring_ctx *ctx = req->ctx;
2060 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002061 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002062
2063 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002064 if (mask && !(mask & poll->events))
2065 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002066
2067 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002068
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002069 /*
2070 * Run completion inline if we can. We're using trylock here because
2071 * we are violating the completion_lock -> poll wq lock ordering.
2072 * If we have a link timeout we're going to need the completion_lock
2073 * for finalizing the request, mark us as having grabbed that already.
2074 */
Jens Axboe8c838782019-03-12 15:48:16 -06002075 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
2076 list_del(&req->list);
Jackie Liua197f662019-11-08 08:09:12 -07002077 io_poll_complete(req, mask);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002078 req->flags |= REQ_F_COMP_LOCKED;
2079 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002080 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2081
2082 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002083 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002084 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002085 }
2086
Jens Axboe221c5eb2019-01-17 09:41:58 -07002087 return 1;
2088}
2089
2090struct io_poll_table {
2091 struct poll_table_struct pt;
2092 struct io_kiocb *req;
2093 int error;
2094};
2095
2096static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2097 struct poll_table_struct *p)
2098{
2099 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2100
2101 if (unlikely(pt->req->poll.head)) {
2102 pt->error = -EINVAL;
2103 return;
2104 }
2105
2106 pt->error = 0;
2107 pt->req->poll.head = head;
2108 add_wait_queue(head, &pt->req->poll.wait);
2109}
2110
Jens Axboe89723d02019-11-05 15:32:58 -07002111static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2112 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002113{
2114 struct io_poll_iocb *poll = &req->poll;
2115 struct io_ring_ctx *ctx = req->ctx;
2116 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002117 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002118 __poll_t mask;
2119 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002120
2121 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2122 return -EINVAL;
2123 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2124 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002125 if (!poll->file)
2126 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002127
Jens Axboe6cc47d12019-09-18 11:18:23 -06002128 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002129 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002130 events = READ_ONCE(sqe->poll_events);
2131 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
2132
Jens Axboe221c5eb2019-01-17 09:41:58 -07002133 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002134 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002135 poll->canceled = false;
2136
2137 ipt.pt._qproc = io_poll_queue_proc;
2138 ipt.pt._key = poll->events;
2139 ipt.req = req;
2140 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2141
2142 /* initialized the list so that we can do list_empty checks */
2143 INIT_LIST_HEAD(&poll->wait.entry);
2144 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2145
Jens Axboe36703242019-07-25 10:20:18 -06002146 INIT_LIST_HEAD(&req->list);
2147
Jens Axboe221c5eb2019-01-17 09:41:58 -07002148 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002149
2150 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002151 if (likely(poll->head)) {
2152 spin_lock(&poll->head->lock);
2153 if (unlikely(list_empty(&poll->wait.entry))) {
2154 if (ipt.error)
2155 cancel = true;
2156 ipt.error = 0;
2157 mask = 0;
2158 }
2159 if (mask || ipt.error)
2160 list_del_init(&poll->wait.entry);
2161 else if (cancel)
2162 WRITE_ONCE(poll->canceled, true);
2163 else if (!poll->done) /* actually waiting for an event */
2164 list_add_tail(&req->list, &ctx->cancel_list);
2165 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002166 }
Jens Axboe8c838782019-03-12 15:48:16 -06002167 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002168 ipt.error = 0;
Jackie Liua197f662019-11-08 08:09:12 -07002169 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06002170 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002171 spin_unlock_irq(&ctx->completion_lock);
2172
Jens Axboe8c838782019-03-12 15:48:16 -06002173 if (mask) {
2174 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002175 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002176 }
Jens Axboe8c838782019-03-12 15:48:16 -06002177 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002178}
2179
Jens Axboe5262f562019-09-17 12:26:57 -06002180static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2181{
2182 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002183 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06002184 unsigned long flags;
2185
2186 req = container_of(timer, struct io_kiocb, timeout.timer);
2187 ctx = req->ctx;
2188 atomic_inc(&ctx->cq_timeouts);
2189
2190 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002191 /*
Jens Axboe11365042019-10-16 09:08:32 -06002192 * We could be racing with timeout deletion. If the list is empty,
2193 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002194 */
Jens Axboe842f9612019-10-29 12:34:10 -06002195 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002196 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002197
Jens Axboe11365042019-10-16 09:08:32 -06002198 /*
2199 * Adjust the reqs sequence before the current one because it
2200 * will consume a slot in the cq_ring and the the cq_tail
2201 * pointer will be increased, otherwise other timeout reqs may
2202 * return in advance without waiting for enough wait_nr.
2203 */
2204 prev = req;
2205 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2206 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002207 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002208 }
Jens Axboe842f9612019-10-29 12:34:10 -06002209
Jens Axboe78e19bb2019-11-06 15:21:34 -07002210 io_cqring_fill_event(req, -ETIME);
Jens Axboe842f9612019-10-29 12:34:10 -06002211 io_commit_cqring(ctx);
Jens Axboe5262f562019-09-17 12:26:57 -06002212 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2213
Jens Axboe842f9612019-10-29 12:34:10 -06002214 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002215 if (req->flags & REQ_F_LINK)
2216 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002217 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002218 return HRTIMER_NORESTART;
2219}
2220
Jens Axboe47f46762019-11-09 17:43:02 -07002221static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2222{
2223 struct io_kiocb *req;
2224 int ret = -ENOENT;
2225
2226 list_for_each_entry(req, &ctx->timeout_list, list) {
2227 if (user_data == req->user_data) {
2228 list_del_init(&req->list);
2229 ret = 0;
2230 break;
2231 }
2232 }
2233
2234 if (ret == -ENOENT)
2235 return ret;
2236
2237 ret = hrtimer_try_to_cancel(&req->timeout.timer);
2238 if (ret == -1)
2239 return -EALREADY;
2240
2241 io_cqring_fill_event(req, -ECANCELED);
2242 io_put_req(req);
2243 return 0;
2244}
2245
Jens Axboe11365042019-10-16 09:08:32 -06002246/*
2247 * Remove or update an existing timeout command
2248 */
2249static int io_timeout_remove(struct io_kiocb *req,
2250 const struct io_uring_sqe *sqe)
2251{
2252 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002253 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002254 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002255
2256 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2257 return -EINVAL;
2258 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2259 return -EINVAL;
2260 flags = READ_ONCE(sqe->timeout_flags);
2261 if (flags)
2262 return -EINVAL;
2263
Jens Axboe11365042019-10-16 09:08:32 -06002264 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002265 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002266
Jens Axboe47f46762019-11-09 17:43:02 -07002267 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002268 io_commit_cqring(ctx);
2269 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002270 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002271 if (ret < 0 && req->flags & REQ_F_LINK)
2272 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002273 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002274 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002275}
2276
2277static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2278{
yangerkun5da0fb12019-10-15 21:59:29 +08002279 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002280 struct io_ring_ctx *ctx = req->ctx;
2281 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002282 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002283 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002284 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002285 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002286
2287 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2288 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002289 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2290 return -EINVAL;
2291 flags = READ_ONCE(sqe->timeout_flags);
2292 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002293 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002294
2295 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002296 return -EFAULT;
2297
Jens Axboe11365042019-10-16 09:08:32 -06002298 if (flags & IORING_TIMEOUT_ABS)
2299 mode = HRTIMER_MODE_ABS;
2300 else
2301 mode = HRTIMER_MODE_REL;
2302
2303 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2304
Jens Axboe5262f562019-09-17 12:26:57 -06002305 /*
2306 * sqe->off holds how many events that need to occur for this
2307 * timeout event to be satisfied.
2308 */
2309 count = READ_ONCE(sqe->off);
2310 if (!count)
2311 count = 1;
2312
2313 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002314 /* reuse it to store the count */
2315 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002316 req->flags |= REQ_F_TIMEOUT;
2317
2318 /*
2319 * Insertion sort, ensuring the first entry in the list is always
2320 * the one we need first.
2321 */
Jens Axboe5262f562019-09-17 12:26:57 -06002322 spin_lock_irq(&ctx->completion_lock);
2323 list_for_each_prev(entry, &ctx->timeout_list) {
2324 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002325 unsigned nxt_sq_head;
2326 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002327
yangerkun5da0fb12019-10-15 21:59:29 +08002328 /*
2329 * Since cached_sq_head + count - 1 can overflow, use type long
2330 * long to store it.
2331 */
2332 tmp = (long long)ctx->cached_sq_head + count - 1;
2333 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2334 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2335
2336 /*
2337 * cached_sq_head may overflow, and it will never overflow twice
2338 * once there is some timeout req still be valid.
2339 */
2340 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002341 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002342
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002343 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002344 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002345
2346 /*
2347 * Sequence of reqs after the insert one and itself should
2348 * be adjusted because each timeout req consumes a slot.
2349 */
2350 span++;
2351 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002352 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002353 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002354 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002355 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002356 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002357 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002358 return 0;
2359}
2360
Jens Axboe62755e32019-10-28 21:49:21 -06002361static bool io_cancel_cb(struct io_wq_work *work, void *data)
2362{
2363 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2364
2365 return req->user_data == (unsigned long) data;
2366}
2367
Jens Axboee977d6d2019-11-05 12:39:45 -07002368static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002369{
Jens Axboe62755e32019-10-28 21:49:21 -06002370 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002371 int ret = 0;
2372
Jens Axboe62755e32019-10-28 21:49:21 -06002373 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2374 switch (cancel_ret) {
2375 case IO_WQ_CANCEL_OK:
2376 ret = 0;
2377 break;
2378 case IO_WQ_CANCEL_RUNNING:
2379 ret = -EALREADY;
2380 break;
2381 case IO_WQ_CANCEL_NOTFOUND:
2382 ret = -ENOENT;
2383 break;
2384 }
2385
Jens Axboee977d6d2019-11-05 12:39:45 -07002386 return ret;
2387}
2388
Jens Axboe47f46762019-11-09 17:43:02 -07002389static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2390 struct io_kiocb *req, __u64 sqe_addr,
2391 struct io_kiocb **nxt)
2392{
2393 unsigned long flags;
2394 int ret;
2395
2396 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2397 if (ret != -ENOENT) {
2398 spin_lock_irqsave(&ctx->completion_lock, flags);
2399 goto done;
2400 }
2401
2402 spin_lock_irqsave(&ctx->completion_lock, flags);
2403 ret = io_timeout_cancel(ctx, sqe_addr);
2404 if (ret != -ENOENT)
2405 goto done;
2406 ret = io_poll_cancel(ctx, sqe_addr);
2407done:
2408 io_cqring_fill_event(req, ret);
2409 io_commit_cqring(ctx);
2410 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2411 io_cqring_ev_posted(ctx);
2412
2413 if (ret < 0 && (req->flags & REQ_F_LINK))
2414 req->flags |= REQ_F_FAIL_LINK;
2415 io_put_req_find_next(req, nxt);
2416}
2417
Jens Axboee977d6d2019-11-05 12:39:45 -07002418static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2419 struct io_kiocb **nxt)
2420{
2421 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002422
2423 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2424 return -EINVAL;
2425 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2426 sqe->cancel_flags)
2427 return -EINVAL;
2428
Jens Axboe47f46762019-11-09 17:43:02 -07002429 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), NULL);
Jens Axboe62755e32019-10-28 21:49:21 -06002430 return 0;
2431}
2432
Jackie Liua197f662019-11-08 08:09:12 -07002433static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002434{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002435 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002436 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002437 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002438
Bob Liu9d858b22019-11-13 18:06:25 +08002439 /* Still need defer if there is pending req in defer list. */
2440 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002441 return 0;
2442
2443 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2444 if (!sqe_copy)
2445 return -EAGAIN;
2446
2447 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002448 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002449 spin_unlock_irq(&ctx->completion_lock);
2450 kfree(sqe_copy);
2451 return 0;
2452 }
2453
2454 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2455 req->submit.sqe = sqe_copy;
2456
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002457 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002458 list_add_tail(&req->list, &ctx->defer_list);
2459 spin_unlock_irq(&ctx->completion_lock);
2460 return -EIOCBQUEUED;
2461}
2462
Jackie Liua197f662019-11-08 08:09:12 -07002463static int __io_submit_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2464 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002465{
Jens Axboee0c5c572019-03-12 10:18:47 -06002466 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002467 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002468 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002469
Jens Axboe2b188cc2019-01-07 10:46:33 -07002470 opcode = READ_ONCE(s->sqe->opcode);
2471 switch (opcode) {
2472 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002473 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002474 break;
2475 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002476 if (unlikely(s->sqe->buf_index))
2477 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002478 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002479 break;
2480 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002481 if (unlikely(s->sqe->buf_index))
2482 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002483 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002484 break;
2485 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002486 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002487 break;
2488 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002489 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002490 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002491 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002492 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002493 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002494 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002495 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002496 break;
2497 case IORING_OP_POLL_REMOVE:
2498 ret = io_poll_remove(req, s->sqe);
2499 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002500 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002501 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002502 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002503 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002504 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002505 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002506 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002507 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002508 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002509 case IORING_OP_TIMEOUT:
2510 ret = io_timeout(req, s->sqe);
2511 break;
Jens Axboe11365042019-10-16 09:08:32 -06002512 case IORING_OP_TIMEOUT_REMOVE:
2513 ret = io_timeout_remove(req, s->sqe);
2514 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002515 case IORING_OP_ACCEPT:
2516 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2517 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002518 case IORING_OP_ASYNC_CANCEL:
2519 ret = io_async_cancel(req, s->sqe, nxt);
2520 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002521 default:
2522 ret = -EINVAL;
2523 break;
2524 }
2525
Jens Axboedef596e2019-01-09 08:59:42 -07002526 if (ret)
2527 return ret;
2528
2529 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002530 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002531 return -EAGAIN;
2532
2533 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002534 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002535 mutex_lock(&ctx->uring_lock);
2536 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002537 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002538 mutex_unlock(&ctx->uring_lock);
2539 }
2540
2541 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002542}
2543
Jens Axboe561fb042019-10-24 07:25:42 -06002544static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002545{
Jens Axboe561fb042019-10-24 07:25:42 -06002546 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002547 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002548 struct sqe_submit *s = &req->submit;
2549 const struct io_uring_sqe *sqe = s->sqe;
2550 struct io_kiocb *nxt = NULL;
2551 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002552
Jens Axboe561fb042019-10-24 07:25:42 -06002553 /* Ensure we clear previously set non-block flag */
2554 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002555
Jens Axboe561fb042019-10-24 07:25:42 -06002556 if (work->flags & IO_WQ_WORK_CANCEL)
2557 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002558
Jens Axboe561fb042019-10-24 07:25:42 -06002559 if (!ret) {
2560 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2561 s->in_async = true;
2562 do {
Jackie Liua197f662019-11-08 08:09:12 -07002563 ret = __io_submit_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002564 /*
2565 * We can get EAGAIN for polled IO even though we're
2566 * forcing a sync submission from here, since we can't
2567 * wait for request slots on the block side.
2568 */
2569 if (ret != -EAGAIN)
2570 break;
2571 cond_resched();
2572 } while (1);
2573 }
Jens Axboe31b51512019-01-18 22:56:34 -07002574
Jens Axboe561fb042019-10-24 07:25:42 -06002575 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002576 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002577
Jens Axboe561fb042019-10-24 07:25:42 -06002578 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002579 if (req->flags & REQ_F_LINK)
2580 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002581 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002582 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002583 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002584
Jens Axboe561fb042019-10-24 07:25:42 -06002585 /* async context always use a copy of the sqe */
2586 kfree(sqe);
2587
2588 /* if a dependent link is ready, pass it back */
2589 if (!ret && nxt) {
2590 io_prep_async_work(nxt);
2591 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002592 }
Jens Axboe31b51512019-01-18 22:56:34 -07002593}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002594
Jens Axboe09bb8392019-03-13 12:39:28 -06002595static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2596{
2597 int op = READ_ONCE(sqe->opcode);
2598
2599 switch (op) {
2600 case IORING_OP_NOP:
2601 case IORING_OP_POLL_REMOVE:
2602 return false;
2603 default:
2604 return true;
2605 }
2606}
2607
Jens Axboe65e19f52019-10-26 07:20:21 -06002608static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2609 int index)
2610{
2611 struct fixed_file_table *table;
2612
2613 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2614 return table->files[index & IORING_FILE_TABLE_MASK];
2615}
2616
Jackie Liua197f662019-11-08 08:09:12 -07002617static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002618{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002619 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002620 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002621 unsigned flags;
2622 int fd;
2623
2624 flags = READ_ONCE(s->sqe->flags);
2625 fd = READ_ONCE(s->sqe->fd);
2626
Jackie Liu4fe2c962019-09-09 20:50:40 +08002627 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002628 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002629 /*
2630 * All io need record the previous position, if LINK vs DARIN,
2631 * it can be used to mark the position of the first IO in the
2632 * link list.
2633 */
2634 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002635
Jens Axboe60c112b2019-06-21 10:20:18 -06002636 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002637 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002638
2639 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002640 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002641 (unsigned) fd >= ctx->nr_user_files))
2642 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002643 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002644 req->file = io_file_from_index(ctx, fd);
2645 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002646 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002647 req->flags |= REQ_F_FIXED_FILE;
2648 } else {
2649 if (s->needs_fixed_file)
2650 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002651 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002652 req->file = io_file_get(state, fd);
2653 if (unlikely(!req->file))
2654 return -EBADF;
2655 }
2656
2657 return 0;
2658}
2659
Jackie Liua197f662019-11-08 08:09:12 -07002660static int io_grab_files(struct io_kiocb *req)
Jens Axboefcb323c2019-10-24 12:39:47 -06002661{
2662 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002663 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002664
2665 rcu_read_lock();
2666 spin_lock_irq(&ctx->inflight_lock);
2667 /*
2668 * We use the f_ops->flush() handler to ensure that we can flush
2669 * out work accessing these files if the fd is closed. Check if
2670 * the fd has changed since we started down this path, and disallow
2671 * this operation if it has.
2672 */
2673 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2674 list_add(&req->inflight_entry, &ctx->inflight_list);
2675 req->flags |= REQ_F_INFLIGHT;
2676 req->work.files = current->files;
2677 ret = 0;
2678 }
2679 spin_unlock_irq(&ctx->inflight_lock);
2680 rcu_read_unlock();
2681
2682 return ret;
2683}
2684
Jens Axboe2665abf2019-11-05 12:40:47 -07002685static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2686{
2687 struct io_kiocb *req = container_of(timer, struct io_kiocb,
2688 timeout.timer);
2689 struct io_ring_ctx *ctx = req->ctx;
2690 struct io_kiocb *prev = NULL;
2691 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002692
2693 spin_lock_irqsave(&ctx->completion_lock, flags);
2694
2695 /*
2696 * We don't expect the list to be empty, that will only happen if we
2697 * race with the completion of the linked work.
2698 */
2699 if (!list_empty(&req->list)) {
2700 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002701 if (refcount_inc_not_zero(&prev->refs))
2702 list_del_init(&req->list);
2703 else
2704 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002705 }
2706
2707 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2708
2709 if (prev) {
Jens Axboe47f46762019-11-09 17:43:02 -07002710 io_async_find_and_cancel(ctx, req, prev->user_data, NULL);
Jens Axboe76a46e02019-11-10 23:34:16 -07002711 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002712 } else {
2713 io_cqring_add_event(req, -ETIME);
2714 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002715 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002716 return HRTIMER_NORESTART;
2717}
2718
Jens Axboe76a46e02019-11-10 23:34:16 -07002719static void io_queue_linked_timeout(struct io_kiocb *req, struct timespec64 *ts,
2720 enum hrtimer_mode *mode)
Jens Axboe2665abf2019-11-05 12:40:47 -07002721{
Jens Axboe76a46e02019-11-10 23:34:16 -07002722 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002723
Jens Axboe76a46e02019-11-10 23:34:16 -07002724 /*
2725 * If the list is now empty, then our linked request finished before
2726 * we got a chance to setup the timer
2727 */
2728 spin_lock_irq(&ctx->completion_lock);
2729 if (!list_empty(&req->list)) {
2730 req->timeout.timer.function = io_link_timeout_fn;
2731 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(*ts),
2732 *mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002733 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002734 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002735
Jens Axboe2665abf2019-11-05 12:40:47 -07002736 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002737 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002738}
2739
Jens Axboe76a46e02019-11-10 23:34:16 -07002740static int io_validate_link_timeout(const struct io_uring_sqe *sqe,
2741 struct timespec64 *ts)
2742{
2743 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || sqe->off)
2744 return -EINVAL;
2745 if (sqe->timeout_flags & ~IORING_TIMEOUT_ABS)
2746 return -EINVAL;
2747 if (get_timespec64(ts, u64_to_user_ptr(sqe->addr)))
2748 return -EFAULT;
2749
2750 return 0;
2751}
2752
2753static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req,
2754 struct timespec64 *ts,
2755 enum hrtimer_mode *mode)
Jens Axboe2665abf2019-11-05 12:40:47 -07002756{
2757 struct io_kiocb *nxt;
Jens Axboe76a46e02019-11-10 23:34:16 -07002758 int ret;
Jens Axboe2665abf2019-11-05 12:40:47 -07002759
2760 if (!(req->flags & REQ_F_LINK))
2761 return NULL;
2762
2763 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002764 if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT)
2765 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002766
Jens Axboe76a46e02019-11-10 23:34:16 -07002767 ret = io_validate_link_timeout(nxt->submit.sqe, ts);
2768 if (ret) {
2769 list_del_init(&nxt->list);
2770 io_cqring_add_event(nxt, ret);
2771 io_double_put_req(nxt);
2772 return ERR_PTR(-ECANCELED);
2773 }
2774
2775 if (nxt->submit.sqe->timeout_flags & IORING_TIMEOUT_ABS)
2776 *mode = HRTIMER_MODE_ABS;
2777 else
2778 *mode = HRTIMER_MODE_REL;
2779
2780 req->flags |= REQ_F_LINK_TIMEOUT;
2781 hrtimer_init(&nxt->timeout.timer, CLOCK_MONOTONIC, *mode);
2782 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002783}
2784
Jackie Liua197f662019-11-08 08:09:12 -07002785static int __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002786{
Jens Axboe76a46e02019-11-10 23:34:16 -07002787 enum hrtimer_mode mode;
Jens Axboe2665abf2019-11-05 12:40:47 -07002788 struct io_kiocb *nxt;
Jens Axboe76a46e02019-11-10 23:34:16 -07002789 struct timespec64 ts;
Jens Axboee0c5c572019-03-12 10:18:47 -06002790 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002791
Jens Axboe76a46e02019-11-10 23:34:16 -07002792 nxt = io_prep_linked_timeout(req, &ts, &mode);
2793 if (IS_ERR(nxt)) {
2794 ret = PTR_ERR(nxt);
2795 nxt = NULL;
2796 goto err;
Jens Axboe2665abf2019-11-05 12:40:47 -07002797 }
2798
Jackie Liua197f662019-11-08 08:09:12 -07002799 ret = __io_submit_sqe(req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002800
2801 /*
2802 * We async punt it if the file wasn't marked NOWAIT, or if the file
2803 * doesn't support non-blocking read/write attempts
2804 */
2805 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2806 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002807 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002808 struct io_uring_sqe *sqe_copy;
2809
Jackie Liu954dab12019-09-18 10:37:52 +08002810 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002811 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002812 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002813 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
Jackie Liua197f662019-11-08 08:09:12 -07002814 ret = io_grab_files(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06002815 if (ret) {
2816 kfree(sqe_copy);
2817 goto err;
2818 }
2819 }
Jens Axboee65ef562019-03-12 10:16:44 -06002820
2821 /*
2822 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002823 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002824 */
Jackie Liua197f662019-11-08 08:09:12 -07002825 io_queue_async_work(req);
Jens Axboe76a46e02019-11-10 23:34:16 -07002826
2827 if (nxt)
2828 io_queue_linked_timeout(nxt, &ts, &mode);
2829
Jens Axboee65ef562019-03-12 10:16:44 -06002830 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002831 }
2832 }
Jens Axboee65ef562019-03-12 10:16:44 -06002833
Jens Axboefcb323c2019-10-24 12:39:47 -06002834err:
Jens Axboe76a46e02019-11-10 23:34:16 -07002835 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002836 io_put_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002837
Jens Axboe76a46e02019-11-10 23:34:16 -07002838 if (nxt) {
2839 if (!ret)
2840 io_queue_linked_timeout(nxt, &ts, &mode);
2841 else
2842 io_put_req(nxt);
2843 }
2844
Jens Axboee65ef562019-03-12 10:16:44 -06002845 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002846 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002847 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002848 if (req->flags & REQ_F_LINK)
2849 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002850 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002851 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002852
2853 return ret;
2854}
2855
Jackie Liua197f662019-11-08 08:09:12 -07002856static int io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002857{
2858 int ret;
2859
Jackie Liua197f662019-11-08 08:09:12 -07002860 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002861 if (ret) {
2862 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002863 io_cqring_add_event(req, ret);
2864 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002865 }
2866 return 0;
2867 }
2868
Jackie Liua197f662019-11-08 08:09:12 -07002869 return __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002870}
2871
Jackie Liua197f662019-11-08 08:09:12 -07002872static int io_queue_link_head(struct io_kiocb *req, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002873{
2874 int ret;
2875 int need_submit = false;
Jackie Liua197f662019-11-08 08:09:12 -07002876 struct io_ring_ctx *ctx = req->ctx;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002877
2878 if (!shadow)
Jackie Liua197f662019-11-08 08:09:12 -07002879 return io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002880
2881 /*
2882 * Mark the first IO in link list as DRAIN, let all the following
2883 * IOs enter the defer list. all IO needs to be completed before link
2884 * list.
2885 */
2886 req->flags |= REQ_F_IO_DRAIN;
Jackie Liua197f662019-11-08 08:09:12 -07002887 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002888 if (ret) {
2889 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002890 io_cqring_add_event(req, ret);
2891 io_double_put_req(req);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002892 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002893 return 0;
2894 }
2895 } else {
2896 /*
2897 * If ret == 0 means that all IOs in front of link io are
2898 * running done. let's queue link head.
2899 */
2900 need_submit = true;
2901 }
2902
2903 /* Insert shadow req to defer_list, blocking next IOs */
2904 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002905 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002906 list_add_tail(&shadow->list, &ctx->defer_list);
2907 spin_unlock_irq(&ctx->completion_lock);
2908
2909 if (need_submit)
Jackie Liua197f662019-11-08 08:09:12 -07002910 return __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002911
2912 return 0;
2913}
2914
Jens Axboe9e645e112019-05-10 16:07:28 -06002915#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2916
Jackie Liua197f662019-11-08 08:09:12 -07002917static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
2918 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002919{
2920 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002921 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002922 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06002923 int ret;
2924
Jens Axboe78e19bb2019-11-06 15:21:34 -07002925 req->user_data = s->sqe->user_data;
2926
Jens Axboe9e645e112019-05-10 16:07:28 -06002927 /* enforce forwards compatibility on users */
2928 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2929 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03002930 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002931 }
2932
Jackie Liua197f662019-11-08 08:09:12 -07002933 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002934 if (unlikely(ret)) {
2935err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002936 io_cqring_add_event(req, ret);
2937 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002938 return;
2939 }
2940
Jens Axboe9e645e112019-05-10 16:07:28 -06002941 /*
2942 * If we already have a head request, queue this one for async
2943 * submittal once the head completes. If we don't have a head but
2944 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2945 * submitted sync once the chain is complete. If none of those
2946 * conditions are true (normal request), then just queue it.
2947 */
2948 if (*link) {
2949 struct io_kiocb *prev = *link;
2950
2951 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2952 if (!sqe_copy) {
2953 ret = -EAGAIN;
2954 goto err_req;
2955 }
2956
2957 s->sqe = sqe_copy;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002958 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002959 list_add_tail(&req->list, &prev->link_list);
2960 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2961 req->flags |= REQ_F_LINK;
2962
Jens Axboe9e645e112019-05-10 16:07:28 -06002963 INIT_LIST_HEAD(&req->link_list);
2964 *link = req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002965 } else if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
2966 /* Only valid as a linked SQE */
2967 ret = -EINVAL;
2968 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002969 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002970 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002971 }
2972}
2973
Jens Axboe9a56a232019-01-09 09:06:50 -07002974/*
2975 * Batched submission is done, ensure local IO is flushed out.
2976 */
2977static void io_submit_state_end(struct io_submit_state *state)
2978{
2979 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002980 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002981 if (state->free_reqs)
2982 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2983 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002984}
2985
2986/*
2987 * Start submission side cache.
2988 */
2989static void io_submit_state_start(struct io_submit_state *state,
2990 struct io_ring_ctx *ctx, unsigned max_ios)
2991{
2992 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002993 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002994 state->file = NULL;
2995 state->ios_left = max_ios;
2996}
2997
Jens Axboe2b188cc2019-01-07 10:46:33 -07002998static void io_commit_sqring(struct io_ring_ctx *ctx)
2999{
Hristo Venev75b28af2019-08-26 17:23:46 +00003000 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003001
Hristo Venev75b28af2019-08-26 17:23:46 +00003002 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003003 /*
3004 * Ensure any loads from the SQEs are done at this point,
3005 * since once we write the new head, the application could
3006 * write new data to them.
3007 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003008 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003009 }
3010}
3011
3012/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003013 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3014 * that is mapped by userspace. This means that care needs to be taken to
3015 * ensure that reads are stable, as we cannot rely on userspace always
3016 * being a good citizen. If members of the sqe are validated and then later
3017 * used, it's important that those reads are done through READ_ONCE() to
3018 * prevent a re-load down the line.
3019 */
3020static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
3021{
Hristo Venev75b28af2019-08-26 17:23:46 +00003022 struct io_rings *rings = ctx->rings;
3023 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003024 unsigned head;
3025
3026 /*
3027 * The cached sq head (or cq tail) serves two purposes:
3028 *
3029 * 1) allows us to batch the cost of updating the user visible
3030 * head updates.
3031 * 2) allows the kernel side to track the head on its own, even
3032 * though the application is the one updating it.
3033 */
3034 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003035 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00003036 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003037 return false;
3038
Hristo Venev75b28af2019-08-26 17:23:46 +00003039 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003040 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003041 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003042 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003043 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003044 ctx->cached_sq_head++;
3045 return true;
3046 }
3047
3048 /* drop invalid entries */
3049 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003050 ctx->cached_sq_dropped++;
3051 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003052 return false;
3053}
3054
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003055static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003056 struct file *ring_file, int ring_fd,
3057 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003058{
3059 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003060 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003061 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003062 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003063 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003064
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003065 if (!list_empty(&ctx->cq_overflow_list)) {
3066 io_cqring_overflow_flush(ctx, false);
3067 return -EBUSY;
3068 }
3069
Jens Axboe6c271ce2019-01-10 11:22:30 -07003070 if (nr > IO_PLUG_THRESHOLD) {
3071 io_submit_state_start(&state, ctx, nr);
3072 statep = &state;
3073 }
3074
3075 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003076 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003077 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003078
Pavel Begunkov196be952019-11-07 01:41:06 +03003079 req = io_get_req(ctx, statep);
3080 if (unlikely(!req)) {
3081 if (!submitted)
3082 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003083 break;
Pavel Begunkov196be952019-11-07 01:41:06 +03003084 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003085 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003086 __io_free_req(req);
3087 break;
3088 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003089
Pavel Begunkov50585b92019-11-07 01:41:07 +03003090 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003091 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3092 if (!mm_fault) {
3093 use_mm(ctx->sqo_mm);
3094 *mm = ctx->sqo_mm;
3095 }
3096 }
3097
Pavel Begunkov50585b92019-11-07 01:41:07 +03003098 sqe_flags = req->submit.sqe->flags;
3099
3100 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08003101 if (!shadow_req) {
3102 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08003103 if (unlikely(!shadow_req))
3104 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003105 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
3106 refcount_dec(&shadow_req->refs);
3107 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003108 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003109 }
3110
Jackie Liua1041c22019-09-18 17:25:52 +08003111out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03003112 req->submit.ring_file = ring_file;
3113 req->submit.ring_fd = ring_fd;
3114 req->submit.has_user = *mm != NULL;
3115 req->submit.in_async = async;
3116 req->submit.needs_fixed_file = async;
3117 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3118 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003119 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003120 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003121
3122 /*
3123 * If previous wasn't linked and we have a linked command,
3124 * that's the end of the chain. Submit the previous link.
3125 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003126 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Jackie Liua197f662019-11-08 08:09:12 -07003127 io_queue_link_head(link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003128 link = NULL;
3129 shadow_req = NULL;
3130 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003131 }
3132
Jens Axboe9e645e112019-05-10 16:07:28 -06003133 if (link)
Jackie Liua197f662019-11-08 08:09:12 -07003134 io_queue_link_head(link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003135 if (statep)
3136 io_submit_state_end(&state);
3137
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003138 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3139 io_commit_sqring(ctx);
3140
Jens Axboe6c271ce2019-01-10 11:22:30 -07003141 return submitted;
3142}
3143
3144static int io_sq_thread(void *data)
3145{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003146 struct io_ring_ctx *ctx = data;
3147 struct mm_struct *cur_mm = NULL;
3148 mm_segment_t old_fs;
3149 DEFINE_WAIT(wait);
3150 unsigned inflight;
3151 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003152 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003153
Jens Axboe206aefd2019-11-07 18:27:42 -07003154 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003155
Jens Axboe6c271ce2019-01-10 11:22:30 -07003156 old_fs = get_fs();
3157 set_fs(USER_DS);
3158
Jens Axboec1edbf52019-11-10 16:56:04 -07003159 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003160 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003161 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003162
3163 if (inflight) {
3164 unsigned nr_events = 0;
3165
3166 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003167 /*
3168 * inflight is the count of the maximum possible
3169 * entries we submitted, but it can be smaller
3170 * if we dropped some of them. If we don't have
3171 * poll entries available, then we know that we
3172 * have nothing left to poll for. Reset the
3173 * inflight count to zero in that case.
3174 */
3175 mutex_lock(&ctx->uring_lock);
3176 if (!list_empty(&ctx->poll_list))
3177 __io_iopoll_check(ctx, &nr_events, 0);
3178 else
3179 inflight = 0;
3180 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003181 } else {
3182 /*
3183 * Normal IO, just pretend everything completed.
3184 * We don't have to poll completions for that.
3185 */
3186 nr_events = inflight;
3187 }
3188
3189 inflight -= nr_events;
3190 if (!inflight)
3191 timeout = jiffies + ctx->sq_thread_idle;
3192 }
3193
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003194 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003195
3196 /*
3197 * If submit got -EBUSY, flag us as needing the application
3198 * to enter the kernel to reap and flush events.
3199 */
3200 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003201 /*
3202 * We're polling. If we're within the defined idle
3203 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003204 * to sleep. The exception is if we got EBUSY doing
3205 * more IO, we should wait for the application to
3206 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003207 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003208 if (inflight ||
3209 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003210 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003211 continue;
3212 }
3213
3214 /*
3215 * Drop cur_mm before scheduling, we can't hold it for
3216 * long periods (or over schedule()). Do this before
3217 * adding ourselves to the waitqueue, as the unuse/drop
3218 * may sleep.
3219 */
3220 if (cur_mm) {
3221 unuse_mm(cur_mm);
3222 mmput(cur_mm);
3223 cur_mm = NULL;
3224 }
3225
3226 prepare_to_wait(&ctx->sqo_wait, &wait,
3227 TASK_INTERRUPTIBLE);
3228
3229 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003230 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003231 /* make sure to read SQ tail after writing flags */
3232 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003233
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003234 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003235 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003236 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003237 finish_wait(&ctx->sqo_wait, &wait);
3238 break;
3239 }
3240 if (signal_pending(current))
3241 flush_signals(current);
3242 schedule();
3243 finish_wait(&ctx->sqo_wait, &wait);
3244
Hristo Venev75b28af2019-08-26 17:23:46 +00003245 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003246 continue;
3247 }
3248 finish_wait(&ctx->sqo_wait, &wait);
3249
Hristo Venev75b28af2019-08-26 17:23:46 +00003250 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003251 }
3252
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003253 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003254 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3255 if (ret > 0)
3256 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003257 }
3258
3259 set_fs(old_fs);
3260 if (cur_mm) {
3261 unuse_mm(cur_mm);
3262 mmput(cur_mm);
3263 }
Jens Axboe06058632019-04-13 09:26:03 -06003264
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003265 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003266
Jens Axboe6c271ce2019-01-10 11:22:30 -07003267 return 0;
3268}
3269
Jens Axboebda52162019-09-24 13:47:15 -06003270struct io_wait_queue {
3271 struct wait_queue_entry wq;
3272 struct io_ring_ctx *ctx;
3273 unsigned to_wait;
3274 unsigned nr_timeouts;
3275};
3276
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003277static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003278{
3279 struct io_ring_ctx *ctx = iowq->ctx;
3280
3281 /*
3282 * Wake up if we have enough events, or if a timeout occured since we
3283 * started waiting. For timeouts, we always want to return to userspace,
3284 * regardless of event count.
3285 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003286 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003287 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3288}
3289
3290static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3291 int wake_flags, void *key)
3292{
3293 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3294 wq);
3295
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003296 /* use noflush == true, as we can't safely rely on locking context */
3297 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003298 return -1;
3299
3300 return autoremove_wake_function(curr, mode, wake_flags, key);
3301}
3302
Jens Axboe2b188cc2019-01-07 10:46:33 -07003303/*
3304 * Wait until events become available, if we don't already have some. The
3305 * application must reap them itself, as they reside on the shared cq ring.
3306 */
3307static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3308 const sigset_t __user *sig, size_t sigsz)
3309{
Jens Axboebda52162019-09-24 13:47:15 -06003310 struct io_wait_queue iowq = {
3311 .wq = {
3312 .private = current,
3313 .func = io_wake_function,
3314 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3315 },
3316 .ctx = ctx,
3317 .to_wait = min_events,
3318 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003319 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003320 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003321
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003322 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003323 return 0;
3324
3325 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003326#ifdef CONFIG_COMPAT
3327 if (in_compat_syscall())
3328 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003329 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003330 else
3331#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003332 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003333
Jens Axboe2b188cc2019-01-07 10:46:33 -07003334 if (ret)
3335 return ret;
3336 }
3337
Jens Axboebda52162019-09-24 13:47:15 -06003338 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003339 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003340 do {
3341 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3342 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003343 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003344 break;
3345 schedule();
3346 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003347 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003348 break;
3349 }
3350 } while (1);
3351 finish_wait(&ctx->wait, &iowq.wq);
3352
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003353 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003354
Hristo Venev75b28af2019-08-26 17:23:46 +00003355 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003356}
3357
Jens Axboe6b063142019-01-10 22:13:58 -07003358static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3359{
3360#if defined(CONFIG_UNIX)
3361 if (ctx->ring_sock) {
3362 struct sock *sock = ctx->ring_sock->sk;
3363 struct sk_buff *skb;
3364
3365 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3366 kfree_skb(skb);
3367 }
3368#else
3369 int i;
3370
Jens Axboe65e19f52019-10-26 07:20:21 -06003371 for (i = 0; i < ctx->nr_user_files; i++) {
3372 struct file *file;
3373
3374 file = io_file_from_index(ctx, i);
3375 if (file)
3376 fput(file);
3377 }
Jens Axboe6b063142019-01-10 22:13:58 -07003378#endif
3379}
3380
3381static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3382{
Jens Axboe65e19f52019-10-26 07:20:21 -06003383 unsigned nr_tables, i;
3384
3385 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003386 return -ENXIO;
3387
3388 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003389 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3390 for (i = 0; i < nr_tables; i++)
3391 kfree(ctx->file_table[i].files);
3392 kfree(ctx->file_table);
3393 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003394 ctx->nr_user_files = 0;
3395 return 0;
3396}
3397
Jens Axboe6c271ce2019-01-10 11:22:30 -07003398static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3399{
3400 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003401 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003402 /*
3403 * The park is a bit of a work-around, without it we get
3404 * warning spews on shutdown with SQPOLL set and affinity
3405 * set to a single CPU.
3406 */
Jens Axboe06058632019-04-13 09:26:03 -06003407 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003408 kthread_stop(ctx->sqo_thread);
3409 ctx->sqo_thread = NULL;
3410 }
3411}
3412
Jens Axboe6b063142019-01-10 22:13:58 -07003413static void io_finish_async(struct io_ring_ctx *ctx)
3414{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003415 io_sq_thread_stop(ctx);
3416
Jens Axboe561fb042019-10-24 07:25:42 -06003417 if (ctx->io_wq) {
3418 io_wq_destroy(ctx->io_wq);
3419 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003420 }
3421}
3422
3423#if defined(CONFIG_UNIX)
3424static void io_destruct_skb(struct sk_buff *skb)
3425{
3426 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3427
Jens Axboe561fb042019-10-24 07:25:42 -06003428 if (ctx->io_wq)
3429 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003430
Jens Axboe6b063142019-01-10 22:13:58 -07003431 unix_destruct_scm(skb);
3432}
3433
3434/*
3435 * Ensure the UNIX gc is aware of our file set, so we are certain that
3436 * the io_uring can be safely unregistered on process exit, even if we have
3437 * loops in the file referencing.
3438 */
3439static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3440{
3441 struct sock *sk = ctx->ring_sock->sk;
3442 struct scm_fp_list *fpl;
3443 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003444 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003445
3446 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3447 unsigned long inflight = ctx->user->unix_inflight + nr;
3448
3449 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3450 return -EMFILE;
3451 }
3452
3453 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3454 if (!fpl)
3455 return -ENOMEM;
3456
3457 skb = alloc_skb(0, GFP_KERNEL);
3458 if (!skb) {
3459 kfree(fpl);
3460 return -ENOMEM;
3461 }
3462
3463 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003464
Jens Axboe08a45172019-10-03 08:11:03 -06003465 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003466 fpl->user = get_uid(ctx->user);
3467 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003468 struct file *file = io_file_from_index(ctx, i + offset);
3469
3470 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003471 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003472 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003473 unix_inflight(fpl->user, fpl->fp[nr_files]);
3474 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003475 }
3476
Jens Axboe08a45172019-10-03 08:11:03 -06003477 if (nr_files) {
3478 fpl->max = SCM_MAX_FD;
3479 fpl->count = nr_files;
3480 UNIXCB(skb).fp = fpl;
3481 skb->destructor = io_destruct_skb;
3482 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3483 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003484
Jens Axboe08a45172019-10-03 08:11:03 -06003485 for (i = 0; i < nr_files; i++)
3486 fput(fpl->fp[i]);
3487 } else {
3488 kfree_skb(skb);
3489 kfree(fpl);
3490 }
Jens Axboe6b063142019-01-10 22:13:58 -07003491
3492 return 0;
3493}
3494
3495/*
3496 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3497 * causes regular reference counting to break down. We rely on the UNIX
3498 * garbage collection to take care of this problem for us.
3499 */
3500static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3501{
3502 unsigned left, total;
3503 int ret = 0;
3504
3505 total = 0;
3506 left = ctx->nr_user_files;
3507 while (left) {
3508 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003509
3510 ret = __io_sqe_files_scm(ctx, this_files, total);
3511 if (ret)
3512 break;
3513 left -= this_files;
3514 total += this_files;
3515 }
3516
3517 if (!ret)
3518 return 0;
3519
3520 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003521 struct file *file = io_file_from_index(ctx, total);
3522
3523 if (file)
3524 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003525 total++;
3526 }
3527
3528 return ret;
3529}
3530#else
3531static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3532{
3533 return 0;
3534}
3535#endif
3536
Jens Axboe65e19f52019-10-26 07:20:21 -06003537static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3538 unsigned nr_files)
3539{
3540 int i;
3541
3542 for (i = 0; i < nr_tables; i++) {
3543 struct fixed_file_table *table = &ctx->file_table[i];
3544 unsigned this_files;
3545
3546 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3547 table->files = kcalloc(this_files, sizeof(struct file *),
3548 GFP_KERNEL);
3549 if (!table->files)
3550 break;
3551 nr_files -= this_files;
3552 }
3553
3554 if (i == nr_tables)
3555 return 0;
3556
3557 for (i = 0; i < nr_tables; i++) {
3558 struct fixed_file_table *table = &ctx->file_table[i];
3559 kfree(table->files);
3560 }
3561 return 1;
3562}
3563
Jens Axboe6b063142019-01-10 22:13:58 -07003564static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3565 unsigned nr_args)
3566{
3567 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003568 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003569 int fd, ret = 0;
3570 unsigned i;
3571
Jens Axboe65e19f52019-10-26 07:20:21 -06003572 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003573 return -EBUSY;
3574 if (!nr_args)
3575 return -EINVAL;
3576 if (nr_args > IORING_MAX_FIXED_FILES)
3577 return -EMFILE;
3578
Jens Axboe65e19f52019-10-26 07:20:21 -06003579 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3580 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3581 GFP_KERNEL);
3582 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003583 return -ENOMEM;
3584
Jens Axboe65e19f52019-10-26 07:20:21 -06003585 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3586 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003587 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003588 return -ENOMEM;
3589 }
3590
Jens Axboe08a45172019-10-03 08:11:03 -06003591 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003592 struct fixed_file_table *table;
3593 unsigned index;
3594
Jens Axboe6b063142019-01-10 22:13:58 -07003595 ret = -EFAULT;
3596 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3597 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003598 /* allow sparse sets */
3599 if (fd == -1) {
3600 ret = 0;
3601 continue;
3602 }
Jens Axboe6b063142019-01-10 22:13:58 -07003603
Jens Axboe65e19f52019-10-26 07:20:21 -06003604 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3605 index = i & IORING_FILE_TABLE_MASK;
3606 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003607
3608 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003609 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003610 break;
3611 /*
3612 * Don't allow io_uring instances to be registered. If UNIX
3613 * isn't enabled, then this causes a reference cycle and this
3614 * instance can never get freed. If UNIX is enabled we'll
3615 * handle it just fine, but there's still no point in allowing
3616 * a ring fd as it doesn't support regular read/write anyway.
3617 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003618 if (table->files[index]->f_op == &io_uring_fops) {
3619 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003620 break;
3621 }
Jens Axboe6b063142019-01-10 22:13:58 -07003622 ret = 0;
3623 }
3624
3625 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003626 for (i = 0; i < ctx->nr_user_files; i++) {
3627 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003628
Jens Axboe65e19f52019-10-26 07:20:21 -06003629 file = io_file_from_index(ctx, i);
3630 if (file)
3631 fput(file);
3632 }
3633 for (i = 0; i < nr_tables; i++)
3634 kfree(ctx->file_table[i].files);
3635
3636 kfree(ctx->file_table);
3637 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003638 ctx->nr_user_files = 0;
3639 return ret;
3640 }
3641
3642 ret = io_sqe_files_scm(ctx);
3643 if (ret)
3644 io_sqe_files_unregister(ctx);
3645
3646 return ret;
3647}
3648
Jens Axboec3a31e62019-10-03 13:59:56 -06003649static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3650{
3651#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003652 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003653 struct sock *sock = ctx->ring_sock->sk;
3654 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3655 struct sk_buff *skb;
3656 int i;
3657
3658 __skb_queue_head_init(&list);
3659
3660 /*
3661 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3662 * remove this entry and rearrange the file array.
3663 */
3664 skb = skb_dequeue(head);
3665 while (skb) {
3666 struct scm_fp_list *fp;
3667
3668 fp = UNIXCB(skb).fp;
3669 for (i = 0; i < fp->count; i++) {
3670 int left;
3671
3672 if (fp->fp[i] != file)
3673 continue;
3674
3675 unix_notinflight(fp->user, fp->fp[i]);
3676 left = fp->count - 1 - i;
3677 if (left) {
3678 memmove(&fp->fp[i], &fp->fp[i + 1],
3679 left * sizeof(struct file *));
3680 }
3681 fp->count--;
3682 if (!fp->count) {
3683 kfree_skb(skb);
3684 skb = NULL;
3685 } else {
3686 __skb_queue_tail(&list, skb);
3687 }
3688 fput(file);
3689 file = NULL;
3690 break;
3691 }
3692
3693 if (!file)
3694 break;
3695
3696 __skb_queue_tail(&list, skb);
3697
3698 skb = skb_dequeue(head);
3699 }
3700
3701 if (skb_peek(&list)) {
3702 spin_lock_irq(&head->lock);
3703 while ((skb = __skb_dequeue(&list)) != NULL)
3704 __skb_queue_tail(head, skb);
3705 spin_unlock_irq(&head->lock);
3706 }
3707#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003708 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003709#endif
3710}
3711
3712static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3713 int index)
3714{
3715#if defined(CONFIG_UNIX)
3716 struct sock *sock = ctx->ring_sock->sk;
3717 struct sk_buff_head *head = &sock->sk_receive_queue;
3718 struct sk_buff *skb;
3719
3720 /*
3721 * See if we can merge this file into an existing skb SCM_RIGHTS
3722 * file set. If there's no room, fall back to allocating a new skb
3723 * and filling it in.
3724 */
3725 spin_lock_irq(&head->lock);
3726 skb = skb_peek(head);
3727 if (skb) {
3728 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3729
3730 if (fpl->count < SCM_MAX_FD) {
3731 __skb_unlink(skb, head);
3732 spin_unlock_irq(&head->lock);
3733 fpl->fp[fpl->count] = get_file(file);
3734 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3735 fpl->count++;
3736 spin_lock_irq(&head->lock);
3737 __skb_queue_head(head, skb);
3738 } else {
3739 skb = NULL;
3740 }
3741 }
3742 spin_unlock_irq(&head->lock);
3743
3744 if (skb) {
3745 fput(file);
3746 return 0;
3747 }
3748
3749 return __io_sqe_files_scm(ctx, 1, index);
3750#else
3751 return 0;
3752#endif
3753}
3754
3755static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3756 unsigned nr_args)
3757{
3758 struct io_uring_files_update up;
3759 __s32 __user *fds;
3760 int fd, i, err;
3761 __u32 done;
3762
Jens Axboe65e19f52019-10-26 07:20:21 -06003763 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003764 return -ENXIO;
3765 if (!nr_args)
3766 return -EINVAL;
3767 if (copy_from_user(&up, arg, sizeof(up)))
3768 return -EFAULT;
3769 if (check_add_overflow(up.offset, nr_args, &done))
3770 return -EOVERFLOW;
3771 if (done > ctx->nr_user_files)
3772 return -EINVAL;
3773
3774 done = 0;
3775 fds = (__s32 __user *) up.fds;
3776 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003777 struct fixed_file_table *table;
3778 unsigned index;
3779
Jens Axboec3a31e62019-10-03 13:59:56 -06003780 err = 0;
3781 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3782 err = -EFAULT;
3783 break;
3784 }
3785 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003786 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3787 index = i & IORING_FILE_TABLE_MASK;
3788 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003789 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003790 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003791 }
3792 if (fd != -1) {
3793 struct file *file;
3794
3795 file = fget(fd);
3796 if (!file) {
3797 err = -EBADF;
3798 break;
3799 }
3800 /*
3801 * Don't allow io_uring instances to be registered. If
3802 * UNIX isn't enabled, then this causes a reference
3803 * cycle and this instance can never get freed. If UNIX
3804 * is enabled we'll handle it just fine, but there's
3805 * still no point in allowing a ring fd as it doesn't
3806 * support regular read/write anyway.
3807 */
3808 if (file->f_op == &io_uring_fops) {
3809 fput(file);
3810 err = -EBADF;
3811 break;
3812 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003813 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003814 err = io_sqe_file_register(ctx, file, i);
3815 if (err)
3816 break;
3817 }
3818 nr_args--;
3819 done++;
3820 up.offset++;
3821 }
3822
3823 return done ? done : err;
3824}
3825
Jens Axboe7d723062019-11-12 22:31:31 -07003826static void io_put_work(struct io_wq_work *work)
3827{
3828 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3829
3830 io_put_req(req);
3831}
3832
3833static void io_get_work(struct io_wq_work *work)
3834{
3835 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3836
3837 refcount_inc(&req->refs);
3838}
3839
Jens Axboe6c271ce2019-01-10 11:22:30 -07003840static int io_sq_offload_start(struct io_ring_ctx *ctx,
3841 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003842{
Jens Axboe561fb042019-10-24 07:25:42 -06003843 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003844 int ret;
3845
Jens Axboe6c271ce2019-01-10 11:22:30 -07003846 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003847 mmgrab(current->mm);
3848 ctx->sqo_mm = current->mm;
3849
Jens Axboe6c271ce2019-01-10 11:22:30 -07003850 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003851 ret = -EPERM;
3852 if (!capable(CAP_SYS_ADMIN))
3853 goto err;
3854
Jens Axboe917257d2019-04-13 09:28:55 -06003855 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3856 if (!ctx->sq_thread_idle)
3857 ctx->sq_thread_idle = HZ;
3858
Jens Axboe6c271ce2019-01-10 11:22:30 -07003859 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003860 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003861
Jens Axboe917257d2019-04-13 09:28:55 -06003862 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003863 if (cpu >= nr_cpu_ids)
3864 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003865 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003866 goto err;
3867
Jens Axboe6c271ce2019-01-10 11:22:30 -07003868 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3869 ctx, cpu,
3870 "io_uring-sq");
3871 } else {
3872 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3873 "io_uring-sq");
3874 }
3875 if (IS_ERR(ctx->sqo_thread)) {
3876 ret = PTR_ERR(ctx->sqo_thread);
3877 ctx->sqo_thread = NULL;
3878 goto err;
3879 }
3880 wake_up_process(ctx->sqo_thread);
3881 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3882 /* Can't have SQ_AFF without SQPOLL */
3883 ret = -EINVAL;
3884 goto err;
3885 }
3886
Jens Axboe561fb042019-10-24 07:25:42 -06003887 /* Do QD, or 4 * CPUS, whatever is smallest */
3888 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe7d723062019-11-12 22:31:31 -07003889 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user,
3890 io_get_work, io_put_work);
Jens Axboe975c99a52019-10-30 08:42:56 -06003891 if (IS_ERR(ctx->io_wq)) {
3892 ret = PTR_ERR(ctx->io_wq);
3893 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003894 goto err;
3895 }
3896
3897 return 0;
3898err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003899 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003900 mmdrop(ctx->sqo_mm);
3901 ctx->sqo_mm = NULL;
3902 return ret;
3903}
3904
3905static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3906{
3907 atomic_long_sub(nr_pages, &user->locked_vm);
3908}
3909
3910static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3911{
3912 unsigned long page_limit, cur_pages, new_pages;
3913
3914 /* Don't allow more pages than we can safely lock */
3915 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3916
3917 do {
3918 cur_pages = atomic_long_read(&user->locked_vm);
3919 new_pages = cur_pages + nr_pages;
3920 if (new_pages > page_limit)
3921 return -ENOMEM;
3922 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3923 new_pages) != cur_pages);
3924
3925 return 0;
3926}
3927
3928static void io_mem_free(void *ptr)
3929{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003930 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003931
Mark Rutland52e04ef2019-04-30 17:30:21 +01003932 if (!ptr)
3933 return;
3934
3935 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003936 if (put_page_testzero(page))
3937 free_compound_page(page);
3938}
3939
3940static void *io_mem_alloc(size_t size)
3941{
3942 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3943 __GFP_NORETRY;
3944
3945 return (void *) __get_free_pages(gfp_flags, get_order(size));
3946}
3947
Hristo Venev75b28af2019-08-26 17:23:46 +00003948static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3949 size_t *sq_offset)
3950{
3951 struct io_rings *rings;
3952 size_t off, sq_array_size;
3953
3954 off = struct_size(rings, cqes, cq_entries);
3955 if (off == SIZE_MAX)
3956 return SIZE_MAX;
3957
3958#ifdef CONFIG_SMP
3959 off = ALIGN(off, SMP_CACHE_BYTES);
3960 if (off == 0)
3961 return SIZE_MAX;
3962#endif
3963
3964 sq_array_size = array_size(sizeof(u32), sq_entries);
3965 if (sq_array_size == SIZE_MAX)
3966 return SIZE_MAX;
3967
3968 if (check_add_overflow(off, sq_array_size, &off))
3969 return SIZE_MAX;
3970
3971 if (sq_offset)
3972 *sq_offset = off;
3973
3974 return off;
3975}
3976
Jens Axboe2b188cc2019-01-07 10:46:33 -07003977static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3978{
Hristo Venev75b28af2019-08-26 17:23:46 +00003979 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003980
Hristo Venev75b28af2019-08-26 17:23:46 +00003981 pages = (size_t)1 << get_order(
3982 rings_size(sq_entries, cq_entries, NULL));
3983 pages += (size_t)1 << get_order(
3984 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003985
Hristo Venev75b28af2019-08-26 17:23:46 +00003986 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003987}
3988
Jens Axboeedafcce2019-01-09 09:16:05 -07003989static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3990{
3991 int i, j;
3992
3993 if (!ctx->user_bufs)
3994 return -ENXIO;
3995
3996 for (i = 0; i < ctx->nr_user_bufs; i++) {
3997 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3998
3999 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004000 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004001
4002 if (ctx->account_mem)
4003 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004004 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004005 imu->nr_bvecs = 0;
4006 }
4007
4008 kfree(ctx->user_bufs);
4009 ctx->user_bufs = NULL;
4010 ctx->nr_user_bufs = 0;
4011 return 0;
4012}
4013
4014static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4015 void __user *arg, unsigned index)
4016{
4017 struct iovec __user *src;
4018
4019#ifdef CONFIG_COMPAT
4020 if (ctx->compat) {
4021 struct compat_iovec __user *ciovs;
4022 struct compat_iovec ciov;
4023
4024 ciovs = (struct compat_iovec __user *) arg;
4025 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4026 return -EFAULT;
4027
4028 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4029 dst->iov_len = ciov.iov_len;
4030 return 0;
4031 }
4032#endif
4033 src = (struct iovec __user *) arg;
4034 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4035 return -EFAULT;
4036 return 0;
4037}
4038
4039static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4040 unsigned nr_args)
4041{
4042 struct vm_area_struct **vmas = NULL;
4043 struct page **pages = NULL;
4044 int i, j, got_pages = 0;
4045 int ret = -EINVAL;
4046
4047 if (ctx->user_bufs)
4048 return -EBUSY;
4049 if (!nr_args || nr_args > UIO_MAXIOV)
4050 return -EINVAL;
4051
4052 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4053 GFP_KERNEL);
4054 if (!ctx->user_bufs)
4055 return -ENOMEM;
4056
4057 for (i = 0; i < nr_args; i++) {
4058 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4059 unsigned long off, start, end, ubuf;
4060 int pret, nr_pages;
4061 struct iovec iov;
4062 size_t size;
4063
4064 ret = io_copy_iov(ctx, &iov, arg, i);
4065 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004066 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004067
4068 /*
4069 * Don't impose further limits on the size and buffer
4070 * constraints here, we'll -EINVAL later when IO is
4071 * submitted if they are wrong.
4072 */
4073 ret = -EFAULT;
4074 if (!iov.iov_base || !iov.iov_len)
4075 goto err;
4076
4077 /* arbitrary limit, but we need something */
4078 if (iov.iov_len > SZ_1G)
4079 goto err;
4080
4081 ubuf = (unsigned long) iov.iov_base;
4082 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4083 start = ubuf >> PAGE_SHIFT;
4084 nr_pages = end - start;
4085
4086 if (ctx->account_mem) {
4087 ret = io_account_mem(ctx->user, nr_pages);
4088 if (ret)
4089 goto err;
4090 }
4091
4092 ret = 0;
4093 if (!pages || nr_pages > got_pages) {
4094 kfree(vmas);
4095 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004096 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004097 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004098 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004099 sizeof(struct vm_area_struct *),
4100 GFP_KERNEL);
4101 if (!pages || !vmas) {
4102 ret = -ENOMEM;
4103 if (ctx->account_mem)
4104 io_unaccount_mem(ctx->user, nr_pages);
4105 goto err;
4106 }
4107 got_pages = nr_pages;
4108 }
4109
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004110 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004111 GFP_KERNEL);
4112 ret = -ENOMEM;
4113 if (!imu->bvec) {
4114 if (ctx->account_mem)
4115 io_unaccount_mem(ctx->user, nr_pages);
4116 goto err;
4117 }
4118
4119 ret = 0;
4120 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004121 pret = get_user_pages(ubuf, nr_pages,
4122 FOLL_WRITE | FOLL_LONGTERM,
4123 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004124 if (pret == nr_pages) {
4125 /* don't support file backed memory */
4126 for (j = 0; j < nr_pages; j++) {
4127 struct vm_area_struct *vma = vmas[j];
4128
4129 if (vma->vm_file &&
4130 !is_file_hugepages(vma->vm_file)) {
4131 ret = -EOPNOTSUPP;
4132 break;
4133 }
4134 }
4135 } else {
4136 ret = pret < 0 ? pret : -EFAULT;
4137 }
4138 up_read(&current->mm->mmap_sem);
4139 if (ret) {
4140 /*
4141 * if we did partial map, or found file backed vmas,
4142 * release any pages we did get
4143 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004144 if (pret > 0)
4145 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004146 if (ctx->account_mem)
4147 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004148 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004149 goto err;
4150 }
4151
4152 off = ubuf & ~PAGE_MASK;
4153 size = iov.iov_len;
4154 for (j = 0; j < nr_pages; j++) {
4155 size_t vec_len;
4156
4157 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4158 imu->bvec[j].bv_page = pages[j];
4159 imu->bvec[j].bv_len = vec_len;
4160 imu->bvec[j].bv_offset = off;
4161 off = 0;
4162 size -= vec_len;
4163 }
4164 /* store original address for later verification */
4165 imu->ubuf = ubuf;
4166 imu->len = iov.iov_len;
4167 imu->nr_bvecs = nr_pages;
4168
4169 ctx->nr_user_bufs++;
4170 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004171 kvfree(pages);
4172 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004173 return 0;
4174err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004175 kvfree(pages);
4176 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004177 io_sqe_buffer_unregister(ctx);
4178 return ret;
4179}
4180
Jens Axboe9b402842019-04-11 11:45:41 -06004181static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4182{
4183 __s32 __user *fds = arg;
4184 int fd;
4185
4186 if (ctx->cq_ev_fd)
4187 return -EBUSY;
4188
4189 if (copy_from_user(&fd, fds, sizeof(*fds)))
4190 return -EFAULT;
4191
4192 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4193 if (IS_ERR(ctx->cq_ev_fd)) {
4194 int ret = PTR_ERR(ctx->cq_ev_fd);
4195 ctx->cq_ev_fd = NULL;
4196 return ret;
4197 }
4198
4199 return 0;
4200}
4201
4202static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4203{
4204 if (ctx->cq_ev_fd) {
4205 eventfd_ctx_put(ctx->cq_ev_fd);
4206 ctx->cq_ev_fd = NULL;
4207 return 0;
4208 }
4209
4210 return -ENXIO;
4211}
4212
Jens Axboe2b188cc2019-01-07 10:46:33 -07004213static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4214{
Jens Axboe6b063142019-01-10 22:13:58 -07004215 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004216 if (ctx->sqo_mm)
4217 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004218
4219 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004220 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004221 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004222 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004223
Jens Axboe2b188cc2019-01-07 10:46:33 -07004224#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004225 if (ctx->ring_sock) {
4226 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004227 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004228 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004229#endif
4230
Hristo Venev75b28af2019-08-26 17:23:46 +00004231 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004232 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004233
4234 percpu_ref_exit(&ctx->refs);
4235 if (ctx->account_mem)
4236 io_unaccount_mem(ctx->user,
4237 ring_pages(ctx->sq_entries, ctx->cq_entries));
4238 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004239 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004240 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004241 kfree(ctx);
4242}
4243
4244static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4245{
4246 struct io_ring_ctx *ctx = file->private_data;
4247 __poll_t mask = 0;
4248
4249 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004250 /*
4251 * synchronizes with barrier from wq_has_sleeper call in
4252 * io_commit_cqring
4253 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004254 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004255 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4256 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004257 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004258 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004259 mask |= EPOLLIN | EPOLLRDNORM;
4260
4261 return mask;
4262}
4263
4264static int io_uring_fasync(int fd, struct file *file, int on)
4265{
4266 struct io_ring_ctx *ctx = file->private_data;
4267
4268 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4269}
4270
4271static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4272{
4273 mutex_lock(&ctx->uring_lock);
4274 percpu_ref_kill(&ctx->refs);
4275 mutex_unlock(&ctx->uring_lock);
4276
Jens Axboe5262f562019-09-17 12:26:57 -06004277 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004278 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004279
4280 if (ctx->io_wq)
4281 io_wq_cancel_all(ctx->io_wq);
4282
Jens Axboedef596e2019-01-09 08:59:42 -07004283 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004284 /* if we failed setting up the ctx, we might not have any rings */
4285 if (ctx->rings)
4286 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004287 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004288 io_ring_ctx_free(ctx);
4289}
4290
4291static int io_uring_release(struct inode *inode, struct file *file)
4292{
4293 struct io_ring_ctx *ctx = file->private_data;
4294
4295 file->private_data = NULL;
4296 io_ring_ctx_wait_and_kill(ctx);
4297 return 0;
4298}
4299
Jens Axboefcb323c2019-10-24 12:39:47 -06004300static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4301 struct files_struct *files)
4302{
4303 struct io_kiocb *req;
4304 DEFINE_WAIT(wait);
4305
4306 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004307 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004308
4309 spin_lock_irq(&ctx->inflight_lock);
4310 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004311 if (req->work.files != files)
4312 continue;
4313 /* req is being completed, ignore */
4314 if (!refcount_inc_not_zero(&req->refs))
4315 continue;
4316 cancel_req = req;
4317 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004318 }
Jens Axboe768134d2019-11-10 20:30:53 -07004319 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004320 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004321 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004322 spin_unlock_irq(&ctx->inflight_lock);
4323
Jens Axboe768134d2019-11-10 20:30:53 -07004324 /* We need to keep going until we don't find a matching req */
4325 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004326 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004327
4328 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4329 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004330 schedule();
4331 }
Jens Axboe768134d2019-11-10 20:30:53 -07004332 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004333}
4334
4335static int io_uring_flush(struct file *file, void *data)
4336{
4337 struct io_ring_ctx *ctx = file->private_data;
4338
4339 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004340 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4341 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004342 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004343 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004344 return 0;
4345}
4346
Jens Axboe2b188cc2019-01-07 10:46:33 -07004347static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4348{
4349 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4350 unsigned long sz = vma->vm_end - vma->vm_start;
4351 struct io_ring_ctx *ctx = file->private_data;
4352 unsigned long pfn;
4353 struct page *page;
4354 void *ptr;
4355
4356 switch (offset) {
4357 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004358 case IORING_OFF_CQ_RING:
4359 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004360 break;
4361 case IORING_OFF_SQES:
4362 ptr = ctx->sq_sqes;
4363 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004364 default:
4365 return -EINVAL;
4366 }
4367
4368 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004369 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004370 return -EINVAL;
4371
4372 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4373 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4374}
4375
4376SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4377 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4378 size_t, sigsz)
4379{
4380 struct io_ring_ctx *ctx;
4381 long ret = -EBADF;
4382 int submitted = 0;
4383 struct fd f;
4384
Jens Axboe6c271ce2019-01-10 11:22:30 -07004385 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004386 return -EINVAL;
4387
4388 f = fdget(fd);
4389 if (!f.file)
4390 return -EBADF;
4391
4392 ret = -EOPNOTSUPP;
4393 if (f.file->f_op != &io_uring_fops)
4394 goto out_fput;
4395
4396 ret = -ENXIO;
4397 ctx = f.file->private_data;
4398 if (!percpu_ref_tryget(&ctx->refs))
4399 goto out_fput;
4400
Jens Axboe6c271ce2019-01-10 11:22:30 -07004401 /*
4402 * For SQ polling, the thread will do all submissions and completions.
4403 * Just return the requested submit count, and wake the thread if
4404 * we were asked to.
4405 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004406 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004407 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004408 if (!list_empty_careful(&ctx->cq_overflow_list))
4409 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004410 if (flags & IORING_ENTER_SQ_WAKEUP)
4411 wake_up(&ctx->sqo_wait);
4412 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004413 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004414 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004415
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004416 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004417 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004418 /* already have mm, so io_submit_sqes() won't try to grab it */
4419 cur_mm = ctx->sqo_mm;
4420 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4421 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004422 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004423 }
4424 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004425 unsigned nr_events = 0;
4426
Jens Axboe2b188cc2019-01-07 10:46:33 -07004427 min_complete = min(min_complete, ctx->cq_entries);
4428
Jens Axboedef596e2019-01-09 08:59:42 -07004429 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004430 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004431 } else {
4432 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4433 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004434 }
4435
Pavel Begunkov6805b322019-10-08 02:18:42 +03004436 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004437out_fput:
4438 fdput(f);
4439 return submitted ? submitted : ret;
4440}
4441
4442static const struct file_operations io_uring_fops = {
4443 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004444 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004445 .mmap = io_uring_mmap,
4446 .poll = io_uring_poll,
4447 .fasync = io_uring_fasync,
4448};
4449
4450static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4451 struct io_uring_params *p)
4452{
Hristo Venev75b28af2019-08-26 17:23:46 +00004453 struct io_rings *rings;
4454 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004455
Hristo Venev75b28af2019-08-26 17:23:46 +00004456 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4457 if (size == SIZE_MAX)
4458 return -EOVERFLOW;
4459
4460 rings = io_mem_alloc(size);
4461 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004462 return -ENOMEM;
4463
Hristo Venev75b28af2019-08-26 17:23:46 +00004464 ctx->rings = rings;
4465 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4466 rings->sq_ring_mask = p->sq_entries - 1;
4467 rings->cq_ring_mask = p->cq_entries - 1;
4468 rings->sq_ring_entries = p->sq_entries;
4469 rings->cq_ring_entries = p->cq_entries;
4470 ctx->sq_mask = rings->sq_ring_mask;
4471 ctx->cq_mask = rings->cq_ring_mask;
4472 ctx->sq_entries = rings->sq_ring_entries;
4473 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004474
4475 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4476 if (size == SIZE_MAX)
4477 return -EOVERFLOW;
4478
4479 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004480 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004481 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004482
Jens Axboe2b188cc2019-01-07 10:46:33 -07004483 return 0;
4484}
4485
4486/*
4487 * Allocate an anonymous fd, this is what constitutes the application
4488 * visible backing of an io_uring instance. The application mmaps this
4489 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4490 * we have to tie this fd to a socket for file garbage collection purposes.
4491 */
4492static int io_uring_get_fd(struct io_ring_ctx *ctx)
4493{
4494 struct file *file;
4495 int ret;
4496
4497#if defined(CONFIG_UNIX)
4498 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4499 &ctx->ring_sock);
4500 if (ret)
4501 return ret;
4502#endif
4503
4504 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4505 if (ret < 0)
4506 goto err;
4507
4508 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4509 O_RDWR | O_CLOEXEC);
4510 if (IS_ERR(file)) {
4511 put_unused_fd(ret);
4512 ret = PTR_ERR(file);
4513 goto err;
4514 }
4515
4516#if defined(CONFIG_UNIX)
4517 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004518 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004519#endif
4520 fd_install(ret, file);
4521 return ret;
4522err:
4523#if defined(CONFIG_UNIX)
4524 sock_release(ctx->ring_sock);
4525 ctx->ring_sock = NULL;
4526#endif
4527 return ret;
4528}
4529
4530static int io_uring_create(unsigned entries, struct io_uring_params *p)
4531{
4532 struct user_struct *user = NULL;
4533 struct io_ring_ctx *ctx;
4534 bool account_mem;
4535 int ret;
4536
4537 if (!entries || entries > IORING_MAX_ENTRIES)
4538 return -EINVAL;
4539
4540 /*
4541 * Use twice as many entries for the CQ ring. It's possible for the
4542 * application to drive a higher depth than the size of the SQ ring,
4543 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004544 * some flexibility in overcommitting a bit. If the application has
4545 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4546 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004547 */
4548 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004549 if (p->flags & IORING_SETUP_CQSIZE) {
4550 /*
4551 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4552 * to a power-of-two, if it isn't already. We do NOT impose
4553 * any cq vs sq ring sizing.
4554 */
4555 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4556 return -EINVAL;
4557 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4558 } else {
4559 p->cq_entries = 2 * p->sq_entries;
4560 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004561
4562 user = get_uid(current_user());
4563 account_mem = !capable(CAP_IPC_LOCK);
4564
4565 if (account_mem) {
4566 ret = io_account_mem(user,
4567 ring_pages(p->sq_entries, p->cq_entries));
4568 if (ret) {
4569 free_uid(user);
4570 return ret;
4571 }
4572 }
4573
4574 ctx = io_ring_ctx_alloc(p);
4575 if (!ctx) {
4576 if (account_mem)
4577 io_unaccount_mem(user, ring_pages(p->sq_entries,
4578 p->cq_entries));
4579 free_uid(user);
4580 return -ENOMEM;
4581 }
4582 ctx->compat = in_compat_syscall();
4583 ctx->account_mem = account_mem;
4584 ctx->user = user;
4585
4586 ret = io_allocate_scq_urings(ctx, p);
4587 if (ret)
4588 goto err;
4589
Jens Axboe6c271ce2019-01-10 11:22:30 -07004590 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004591 if (ret)
4592 goto err;
4593
Jens Axboe2b188cc2019-01-07 10:46:33 -07004594 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004595 p->sq_off.head = offsetof(struct io_rings, sq.head);
4596 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4597 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4598 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4599 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4600 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4601 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004602
4603 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004604 p->cq_off.head = offsetof(struct io_rings, cq.head);
4605 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4606 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4607 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4608 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4609 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004610
Jens Axboe044c1ab2019-10-28 09:15:33 -06004611 /*
4612 * Install ring fd as the very last thing, so we don't risk someone
4613 * having closed it before we finish setup
4614 */
4615 ret = io_uring_get_fd(ctx);
4616 if (ret < 0)
4617 goto err;
4618
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004619 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004620 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004621 return ret;
4622err:
4623 io_ring_ctx_wait_and_kill(ctx);
4624 return ret;
4625}
4626
4627/*
4628 * Sets up an aio uring context, and returns the fd. Applications asks for a
4629 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4630 * params structure passed in.
4631 */
4632static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4633{
4634 struct io_uring_params p;
4635 long ret;
4636 int i;
4637
4638 if (copy_from_user(&p, params, sizeof(p)))
4639 return -EFAULT;
4640 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4641 if (p.resv[i])
4642 return -EINVAL;
4643 }
4644
Jens Axboe6c271ce2019-01-10 11:22:30 -07004645 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004646 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004647 return -EINVAL;
4648
4649 ret = io_uring_create(entries, &p);
4650 if (ret < 0)
4651 return ret;
4652
4653 if (copy_to_user(params, &p, sizeof(p)))
4654 return -EFAULT;
4655
4656 return ret;
4657}
4658
4659SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4660 struct io_uring_params __user *, params)
4661{
4662 return io_uring_setup(entries, params);
4663}
4664
Jens Axboeedafcce2019-01-09 09:16:05 -07004665static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4666 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004667 __releases(ctx->uring_lock)
4668 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004669{
4670 int ret;
4671
Jens Axboe35fa71a2019-04-22 10:23:23 -06004672 /*
4673 * We're inside the ring mutex, if the ref is already dying, then
4674 * someone else killed the ctx or is already going through
4675 * io_uring_register().
4676 */
4677 if (percpu_ref_is_dying(&ctx->refs))
4678 return -ENXIO;
4679
Jens Axboeedafcce2019-01-09 09:16:05 -07004680 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004681
4682 /*
4683 * Drop uring mutex before waiting for references to exit. If another
4684 * thread is currently inside io_uring_enter() it might need to grab
4685 * the uring_lock to make progress. If we hold it here across the drain
4686 * wait, then we can deadlock. It's safe to drop the mutex here, since
4687 * no new references will come in after we've killed the percpu ref.
4688 */
4689 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004690 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004691 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004692
4693 switch (opcode) {
4694 case IORING_REGISTER_BUFFERS:
4695 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4696 break;
4697 case IORING_UNREGISTER_BUFFERS:
4698 ret = -EINVAL;
4699 if (arg || nr_args)
4700 break;
4701 ret = io_sqe_buffer_unregister(ctx);
4702 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004703 case IORING_REGISTER_FILES:
4704 ret = io_sqe_files_register(ctx, arg, nr_args);
4705 break;
4706 case IORING_UNREGISTER_FILES:
4707 ret = -EINVAL;
4708 if (arg || nr_args)
4709 break;
4710 ret = io_sqe_files_unregister(ctx);
4711 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004712 case IORING_REGISTER_FILES_UPDATE:
4713 ret = io_sqe_files_update(ctx, arg, nr_args);
4714 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004715 case IORING_REGISTER_EVENTFD:
4716 ret = -EINVAL;
4717 if (nr_args != 1)
4718 break;
4719 ret = io_eventfd_register(ctx, arg);
4720 break;
4721 case IORING_UNREGISTER_EVENTFD:
4722 ret = -EINVAL;
4723 if (arg || nr_args)
4724 break;
4725 ret = io_eventfd_unregister(ctx);
4726 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004727 default:
4728 ret = -EINVAL;
4729 break;
4730 }
4731
4732 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004733 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004734 percpu_ref_reinit(&ctx->refs);
4735 return ret;
4736}
4737
4738SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4739 void __user *, arg, unsigned int, nr_args)
4740{
4741 struct io_ring_ctx *ctx;
4742 long ret = -EBADF;
4743 struct fd f;
4744
4745 f = fdget(fd);
4746 if (!f.file)
4747 return -EBADF;
4748
4749 ret = -EOPNOTSUPP;
4750 if (f.file->f_op != &io_uring_fops)
4751 goto out_fput;
4752
4753 ctx = f.file->private_data;
4754
4755 mutex_lock(&ctx->uring_lock);
4756 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4757 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004758 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4759 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004760out_fput:
4761 fdput(f);
4762 return ret;
4763}
4764
Jens Axboe2b188cc2019-01-07 10:46:33 -07004765static int __init io_uring_init(void)
4766{
4767 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4768 return 0;
4769};
4770__initcall(io_uring_init);