blob: b709825023360e79edbde79c33b6b8d95d5098b4 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070059#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070072
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020073#define CREATE_TRACE_POINTS
74#include <trace/events/io_uring.h>
75
Jens Axboe2b188cc2019-01-07 10:46:33 -070076#include <uapi/linux/io_uring.h>
77
78#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060079#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070080
Daniel Xu5277dea2019-09-14 14:23:45 -070081#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060082#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060083
84/*
85 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
86 */
87#define IORING_FILE_TABLE_SHIFT 9
88#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
89#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
90#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070091
92struct io_uring {
93 u32 head ____cacheline_aligned_in_smp;
94 u32 tail ____cacheline_aligned_in_smp;
95};
96
Stefan Bühler1e84b972019-04-24 23:54:16 +020097/*
Hristo Venev75b28af2019-08-26 17:23:46 +000098 * This data is shared with the application through the mmap at offsets
99 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200100 *
101 * The offsets to the member fields are published through struct
102 * io_sqring_offsets when calling io_uring_setup.
103 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000104struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 /*
106 * Head and tail offsets into the ring; the offsets need to be
107 * masked to get valid indices.
108 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000109 * The kernel controls head of the sq ring and the tail of the cq ring,
110 * and the application controls tail of the sq ring and the head of the
111 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000115 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200116 * ring_entries - 1)
117 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 u32 sq_ring_mask, cq_ring_mask;
119 /* Ring sizes (constant, power of 2) */
120 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 /*
122 * Number of invalid entries dropped by the kernel due to
123 * invalid index stored in array
124 *
125 * Written by the kernel, shouldn't be modified by the
126 * application (i.e. get number of "new events" by comparing to
127 * cached value).
128 *
129 * After a new SQ head value was read by the application this
130 * counter includes all submissions that were dropped reaching
131 * the new SQ head (and possibly more).
132 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 /*
135 * Runtime flags
136 *
137 * Written by the kernel, shouldn't be modified by the
138 * application.
139 *
140 * The application needs a full memory barrier before checking
141 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
145 * Number of completion events lost because the queue was full;
146 * this should be avoided by the application by making sure
147 * there are not more requests pending thatn there is space in
148 * the completion queue.
149 *
150 * Written by the kernel, shouldn't be modified by the
151 * application (i.e. get number of "new events" by comparing to
152 * cached value).
153 *
154 * As completion events come in out of order this counter is not
155 * ordered with any other data.
156 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000157 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 /*
159 * Ring buffer of completion events.
160 *
161 * The kernel writes completion events fresh every time they are
162 * produced, so the application is allowed to modify pending
163 * entries.
164 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000165 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700166};
167
Jens Axboeedafcce2019-01-09 09:16:05 -0700168struct io_mapped_ubuf {
169 u64 ubuf;
170 size_t len;
171 struct bio_vec *bvec;
172 unsigned int nr_bvecs;
173};
174
Jens Axboe65e19f52019-10-26 07:20:21 -0600175struct fixed_file_table {
176 struct file **files;
177};
178
Jens Axboe2b188cc2019-01-07 10:46:33 -0700179struct io_ring_ctx {
180 struct {
181 struct percpu_ref refs;
182 } ____cacheline_aligned_in_smp;
183
184 struct {
185 unsigned int flags;
186 bool compat;
187 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700188 bool cq_overflow_flushed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700189
Hristo Venev75b28af2019-08-26 17:23:46 +0000190 /*
191 * Ring buffer of indices into array of io_uring_sqe, which is
192 * mmapped by the application using the IORING_OFF_SQES offset.
193 *
194 * This indirection could e.g. be used to assign fixed
195 * io_uring_sqe entries to operations and only submit them to
196 * the queue when needed.
197 *
198 * The kernel modifies neither the indices array nor the entries
199 * array.
200 */
201 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700202 unsigned cached_sq_head;
203 unsigned sq_entries;
204 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700205 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600206 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700207 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600209
210 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600211 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700212 struct list_head cq_overflow_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600213
214 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700215 } ____cacheline_aligned_in_smp;
216
Jens Axboe206aefd2019-11-07 18:27:42 -0700217 struct io_rings *rings;
218
Jens Axboe2b188cc2019-01-07 10:46:33 -0700219 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600220 struct io_wq *io_wq;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700221 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700223 wait_queue_head_t sqo_wait;
Hristo Venev75b28af2019-08-26 17:23:46 +0000224
Jens Axboe6b063142019-01-10 22:13:58 -0700225 /*
226 * If used, fixed file set. Writers must ensure that ->refs is dead,
227 * readers must ensure that ->refs is alive as long as the file* is
228 * used. Only updated through io_uring_register(2).
229 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600230 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700231 unsigned nr_user_files;
232
Jens Axboeedafcce2019-01-09 09:16:05 -0700233 /* if used, fixed mapped user buffers */
234 unsigned nr_user_bufs;
235 struct io_mapped_ubuf *user_bufs;
236
Jens Axboe2b188cc2019-01-07 10:46:33 -0700237 struct user_struct *user;
238
Jens Axboe206aefd2019-11-07 18:27:42 -0700239 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
240 struct completion *completions;
241
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700242 /* if all else fails... */
243 struct io_kiocb *fallback_req;
244
Jens Axboe206aefd2019-11-07 18:27:42 -0700245#if defined(CONFIG_UNIX)
246 struct socket *ring_sock;
247#endif
248
249 struct {
250 unsigned cached_cq_tail;
251 unsigned cq_entries;
252 unsigned cq_mask;
253 atomic_t cq_timeouts;
254 struct wait_queue_head cq_wait;
255 struct fasync_struct *cq_fasync;
256 struct eventfd_ctx *cq_ev_fd;
257 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258
259 struct {
260 struct mutex uring_lock;
261 wait_queue_head_t wait;
262 } ____cacheline_aligned_in_smp;
263
264 struct {
265 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700266 bool poll_multi_file;
267 /*
268 * ->poll_list is protected by the ctx->uring_lock for
269 * io_uring instances that don't use IORING_SETUP_SQPOLL.
270 * For SQPOLL, only the single threaded io_sq_thread() will
271 * manipulate the list, hence no extra locking is needed there.
272 */
273 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700274 struct list_head cancel_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600275
276 spinlock_t inflight_lock;
277 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700278 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279};
280
281struct sqe_submit {
282 const struct io_uring_sqe *sqe;
Jens Axboefcb323c2019-10-24 12:39:47 -0600283 struct file *ring_file;
284 int ring_fd;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800285 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700286 bool has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +0800287 bool in_async;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700288 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700289};
290
Jens Axboe09bb8392019-03-13 12:39:28 -0600291/*
292 * First field must be the file pointer in all the
293 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
294 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700295struct io_poll_iocb {
296 struct file *file;
297 struct wait_queue_head *head;
298 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600299 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700300 bool canceled;
301 struct wait_queue_entry wait;
302};
303
Jens Axboe5262f562019-09-17 12:26:57 -0600304struct io_timeout {
305 struct file *file;
306 struct hrtimer timer;
307};
308
Jens Axboe09bb8392019-03-13 12:39:28 -0600309/*
310 * NOTE! Each of the iocb union members has the file pointer
311 * as the first entry in their struct definition. So you can
312 * access the file pointer through any of the sub-structs,
313 * or directly as just 'ki_filp' in this struct.
314 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700315struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700316 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600317 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700318 struct kiocb rw;
319 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600320 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700321 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700322
323 struct sqe_submit submit;
324
325 struct io_ring_ctx *ctx;
326 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600327 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700328 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700329 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200330#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700331#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700332#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700333#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200334#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
335#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600336#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700337#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800338#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800339#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600340#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600341#define REQ_F_ISREG 2048 /* regular file */
342#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboefcb323c2019-10-24 12:39:47 -0600343#define REQ_F_INFLIGHT 8192 /* on inflight list */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600345 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600346 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700347
Jens Axboefcb323c2019-10-24 12:39:47 -0600348 struct list_head inflight_entry;
349
Jens Axboe561fb042019-10-24 07:25:42 -0600350 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700351};
352
353#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700354#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700355
Jens Axboe9a56a232019-01-09 09:06:50 -0700356struct io_submit_state {
357 struct blk_plug plug;
358
359 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700360 * io_kiocb alloc cache
361 */
362 void *reqs[IO_IOPOLL_BATCH];
363 unsigned int free_reqs;
364 unsigned int cur_req;
365
366 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700367 * File reference cache
368 */
369 struct file *file;
370 unsigned int fd;
371 unsigned int has_refs;
372 unsigned int used_refs;
373 unsigned int ios_left;
374};
375
Jens Axboe561fb042019-10-24 07:25:42 -0600376static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700377static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800378static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800379static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700380static void io_double_put_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600381
Jens Axboe2b188cc2019-01-07 10:46:33 -0700382static struct kmem_cache *req_cachep;
383
384static const struct file_operations io_uring_fops;
385
386struct sock *io_uring_get_socket(struct file *file)
387{
388#if defined(CONFIG_UNIX)
389 if (file->f_op == &io_uring_fops) {
390 struct io_ring_ctx *ctx = file->private_data;
391
392 return ctx->ring_sock->sk;
393 }
394#endif
395 return NULL;
396}
397EXPORT_SYMBOL(io_uring_get_socket);
398
399static void io_ring_ctx_ref_free(struct percpu_ref *ref)
400{
401 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
402
Jens Axboe206aefd2019-11-07 18:27:42 -0700403 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700404}
405
406static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
407{
408 struct io_ring_ctx *ctx;
409
410 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
411 if (!ctx)
412 return NULL;
413
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700414 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
415 if (!ctx->fallback_req)
416 goto err;
417
Jens Axboe206aefd2019-11-07 18:27:42 -0700418 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
419 if (!ctx->completions)
420 goto err;
421
Roman Gushchin21482892019-05-07 10:01:48 -0700422 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700423 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
424 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700425
426 ctx->flags = p->flags;
427 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700428 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700429 init_completion(&ctx->completions[0]);
430 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700431 mutex_init(&ctx->uring_lock);
432 init_waitqueue_head(&ctx->wait);
433 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700434 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700435 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600436 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600437 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600438 init_waitqueue_head(&ctx->inflight_wait);
439 spin_lock_init(&ctx->inflight_lock);
440 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700441 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700442err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700443 if (ctx->fallback_req)
444 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700445 kfree(ctx->completions);
446 kfree(ctx);
447 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700448}
449
Jackie Liua197f662019-11-08 08:09:12 -0700450static inline bool __io_sequence_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600451{
Jackie Liua197f662019-11-08 08:09:12 -0700452 struct io_ring_ctx *ctx = req->ctx;
453
Jens Axboe498ccd92019-10-25 10:04:25 -0600454 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
455 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600456}
457
Jackie Liua197f662019-11-08 08:09:12 -0700458static inline bool io_sequence_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600459{
460 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
461 return false;
462
Jackie Liua197f662019-11-08 08:09:12 -0700463 return __io_sequence_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600464}
465
466static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600467{
468 struct io_kiocb *req;
469
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600470 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Jackie Liua197f662019-11-08 08:09:12 -0700471 if (req && !io_sequence_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600472 list_del_init(&req->list);
473 return req;
474 }
475
476 return NULL;
477}
478
Jens Axboe5262f562019-09-17 12:26:57 -0600479static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
480{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600481 struct io_kiocb *req;
482
483 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jackie Liua197f662019-11-08 08:09:12 -0700484 if (req && !__io_sequence_defer(req)) {
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600485 list_del_init(&req->list);
486 return req;
487 }
488
489 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600490}
491
Jens Axboede0617e2019-04-06 21:51:27 -0600492static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700493{
Hristo Venev75b28af2019-08-26 17:23:46 +0000494 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700495
Hristo Venev75b28af2019-08-26 17:23:46 +0000496 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700497 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000498 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700499
Jens Axboe2b188cc2019-01-07 10:46:33 -0700500 if (wq_has_sleeper(&ctx->cq_wait)) {
501 wake_up_interruptible(&ctx->cq_wait);
502 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
503 }
504 }
505}
506
Jens Axboe561fb042019-10-24 07:25:42 -0600507static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600508{
Jens Axboe561fb042019-10-24 07:25:42 -0600509 u8 opcode = READ_ONCE(sqe->opcode);
510
511 return !(opcode == IORING_OP_READ_FIXED ||
512 opcode == IORING_OP_WRITE_FIXED);
513}
514
515static inline bool io_prep_async_work(struct io_kiocb *req)
516{
517 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600518
Jens Axboe6cc47d12019-09-18 11:18:23 -0600519 if (req->submit.sqe) {
520 switch (req->submit.sqe->opcode) {
521 case IORING_OP_WRITEV:
522 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600523 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700524 /* fall-through */
525 case IORING_OP_READV:
526 case IORING_OP_READ_FIXED:
527 case IORING_OP_SENDMSG:
528 case IORING_OP_RECVMSG:
529 case IORING_OP_ACCEPT:
530 case IORING_OP_POLL_ADD:
531 /*
532 * We know REQ_F_ISREG is not set on some of these
533 * opcodes, but this enables us to keep the check in
534 * just one place.
535 */
536 if (!(req->flags & REQ_F_ISREG))
537 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600538 break;
539 }
Jens Axboe561fb042019-10-24 07:25:42 -0600540 if (io_sqe_needs_user(req->submit.sqe))
541 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600542 }
543
Jens Axboe561fb042019-10-24 07:25:42 -0600544 return do_hashed;
545}
546
Jackie Liua197f662019-11-08 08:09:12 -0700547static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600548{
549 bool do_hashed = io_prep_async_work(req);
Jackie Liua197f662019-11-08 08:09:12 -0700550 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -0600551
552 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
553 req->flags);
554 if (!do_hashed) {
555 io_wq_enqueue(ctx->io_wq, &req->work);
556 } else {
557 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
558 file_inode(req->file));
559 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600560}
561
Jens Axboe5262f562019-09-17 12:26:57 -0600562static void io_kill_timeout(struct io_kiocb *req)
563{
564 int ret;
565
566 ret = hrtimer_try_to_cancel(&req->timeout.timer);
567 if (ret != -1) {
568 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600569 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700570 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800571 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600572 }
573}
574
575static void io_kill_timeouts(struct io_ring_ctx *ctx)
576{
577 struct io_kiocb *req, *tmp;
578
579 spin_lock_irq(&ctx->completion_lock);
580 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
581 io_kill_timeout(req);
582 spin_unlock_irq(&ctx->completion_lock);
583}
584
Jens Axboede0617e2019-04-06 21:51:27 -0600585static void io_commit_cqring(struct io_ring_ctx *ctx)
586{
587 struct io_kiocb *req;
588
Jens Axboe5262f562019-09-17 12:26:57 -0600589 while ((req = io_get_timeout_req(ctx)) != NULL)
590 io_kill_timeout(req);
591
Jens Axboede0617e2019-04-06 21:51:27 -0600592 __io_commit_cqring(ctx);
593
594 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800595 if (req->flags & REQ_F_SHADOW_DRAIN) {
596 /* Just for drain, free it. */
597 __io_free_req(req);
598 continue;
599 }
Jens Axboede0617e2019-04-06 21:51:27 -0600600 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700601 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600602 }
603}
604
Jens Axboe2b188cc2019-01-07 10:46:33 -0700605static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
606{
Hristo Venev75b28af2019-08-26 17:23:46 +0000607 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608 unsigned tail;
609
610 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200611 /*
612 * writes to the cq entry need to come after reading head; the
613 * control dependency is enough as we're using WRITE_ONCE to
614 * fill the cq entry
615 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000616 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700617 return NULL;
618
619 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000620 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700621}
622
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700623static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
624{
625 if (waitqueue_active(&ctx->wait))
626 wake_up(&ctx->wait);
627 if (waitqueue_active(&ctx->sqo_wait))
628 wake_up(&ctx->sqo_wait);
629 if (ctx->cq_ev_fd)
630 eventfd_signal(ctx->cq_ev_fd, 1);
631}
632
633static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
634{
635 struct io_rings *rings = ctx->rings;
636 struct io_uring_cqe *cqe;
637 struct io_kiocb *req;
638 unsigned long flags;
639 LIST_HEAD(list);
640
641 if (!force) {
642 if (list_empty_careful(&ctx->cq_overflow_list))
643 return;
644 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
645 rings->cq_ring_entries))
646 return;
647 }
648
649 spin_lock_irqsave(&ctx->completion_lock, flags);
650
651 /* if force is set, the ring is going away. always drop after that */
652 if (force)
653 ctx->cq_overflow_flushed = true;
654
655 while (!list_empty(&ctx->cq_overflow_list)) {
656 cqe = io_get_cqring(ctx);
657 if (!cqe && !force)
658 break;
659
660 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
661 list);
662 list_move(&req->list, &list);
663 if (cqe) {
664 WRITE_ONCE(cqe->user_data, req->user_data);
665 WRITE_ONCE(cqe->res, req->result);
666 WRITE_ONCE(cqe->flags, 0);
667 } else {
668 WRITE_ONCE(ctx->rings->cq_overflow,
669 atomic_inc_return(&ctx->cached_cq_overflow));
670 }
671 }
672
673 io_commit_cqring(ctx);
674 spin_unlock_irqrestore(&ctx->completion_lock, flags);
675 io_cqring_ev_posted(ctx);
676
677 while (!list_empty(&list)) {
678 req = list_first_entry(&list, struct io_kiocb, list);
679 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800680 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700681 }
682}
683
Jens Axboe78e19bb2019-11-06 15:21:34 -0700684static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700685{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700686 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700687 struct io_uring_cqe *cqe;
688
Jens Axboe78e19bb2019-11-06 15:21:34 -0700689 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700690
Jens Axboe2b188cc2019-01-07 10:46:33 -0700691 /*
692 * If we can't get a cq entry, userspace overflowed the
693 * submission (by quite a lot). Increment the overflow count in
694 * the ring.
695 */
696 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700697 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700698 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700699 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600700 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700701 } else if (ctx->cq_overflow_flushed) {
Jens Axboe498ccd92019-10-25 10:04:25 -0600702 WRITE_ONCE(ctx->rings->cq_overflow,
703 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700704 } else {
705 refcount_inc(&req->refs);
706 req->result = res;
707 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700708 }
709}
710
Jens Axboe78e19bb2019-11-06 15:21:34 -0700711static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700712{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700713 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700714 unsigned long flags;
715
716 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700717 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700718 io_commit_cqring(ctx);
719 spin_unlock_irqrestore(&ctx->completion_lock, flags);
720
Jens Axboe8c838782019-03-12 15:48:16 -0600721 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700722}
723
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700724static inline bool io_is_fallback_req(struct io_kiocb *req)
725{
726 return req == (struct io_kiocb *)
727 ((unsigned long) req->ctx->fallback_req & ~1UL);
728}
729
730static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
731{
732 struct io_kiocb *req;
733
734 req = ctx->fallback_req;
735 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
736 return req;
737
738 return NULL;
739}
740
Jens Axboe2579f912019-01-09 09:10:43 -0700741static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
742 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700743{
Jens Axboefd6fab22019-03-14 16:30:06 -0600744 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700745 struct io_kiocb *req;
746
747 if (!percpu_ref_tryget(&ctx->refs))
748 return NULL;
749
Jens Axboe2579f912019-01-09 09:10:43 -0700750 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600751 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700752 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700753 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700754 } else if (!state->free_reqs) {
755 size_t sz;
756 int ret;
757
758 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600759 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
760
761 /*
762 * Bulk alloc is all-or-nothing. If we fail to get a batch,
763 * retry single alloc to be on the safe side.
764 */
765 if (unlikely(ret <= 0)) {
766 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
767 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700768 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600769 ret = 1;
770 }
Jens Axboe2579f912019-01-09 09:10:43 -0700771 state->free_reqs = ret - 1;
772 state->cur_req = 1;
773 req = state->reqs[0];
774 } else {
775 req = state->reqs[state->cur_req];
776 state->free_reqs--;
777 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700778 }
779
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700780got_it:
Jens Axboe60c112b2019-06-21 10:20:18 -0600781 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700782 req->ctx = ctx;
783 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600784 /* one is dropped after submission, the other at completion */
785 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600786 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600787 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700788 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700789fallback:
790 req = io_get_fallback_req(ctx);
791 if (req)
792 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300793 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700794 return NULL;
795}
796
Jens Axboedef596e2019-01-09 08:59:42 -0700797static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
798{
799 if (*nr) {
800 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300801 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700802 *nr = 0;
803 }
804}
805
Jens Axboe9e645e112019-05-10 16:07:28 -0600806static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700807{
Jens Axboefcb323c2019-10-24 12:39:47 -0600808 struct io_ring_ctx *ctx = req->ctx;
809
Jens Axboe09bb8392019-03-13 12:39:28 -0600810 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
811 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600812 if (req->flags & REQ_F_INFLIGHT) {
813 unsigned long flags;
814
815 spin_lock_irqsave(&ctx->inflight_lock, flags);
816 list_del(&req->inflight_entry);
817 if (waitqueue_active(&ctx->inflight_wait))
818 wake_up(&ctx->inflight_wait);
819 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
820 }
821 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700822 if (likely(!io_is_fallback_req(req)))
823 kmem_cache_free(req_cachep, req);
824 else
825 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600826}
827
Jackie Liua197f662019-11-08 08:09:12 -0700828static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -0700829{
Jackie Liua197f662019-11-08 08:09:12 -0700830 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700831 int ret;
832
833 ret = hrtimer_try_to_cancel(&req->timeout.timer);
834 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700835 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700836 io_commit_cqring(ctx);
837 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800838 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700839 return true;
840 }
841
842 return false;
843}
844
Jens Axboeba816ad2019-09-28 11:36:45 -0600845static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600846{
Jens Axboe2665abf2019-11-05 12:40:47 -0700847 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600848 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700849 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600850
851 /*
852 * The list should never be empty when we are called here. But could
853 * potentially happen if the chain is messed up, check to be on the
854 * safe side.
855 */
856 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700857 while (nxt) {
Jens Axboe9e645e112019-05-10 16:07:28 -0600858 list_del(&nxt->list);
859 if (!list_empty(&req->link_list)) {
860 INIT_LIST_HEAD(&nxt->link_list);
861 list_splice(&req->link_list, &nxt->link_list);
862 nxt->flags |= REQ_F_LINK;
863 }
864
Jens Axboeba816ad2019-09-28 11:36:45 -0600865 /*
866 * If we're in async work, we can continue processing the chain
867 * in this context instead of having to queue up new async work.
868 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700869 if (req->flags & REQ_F_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700870 wake_ev = io_link_cancel_timeout(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700871
872 /* we dropped this link, get next */
873 nxt = list_first_entry_or_null(&req->link_list,
874 struct io_kiocb, list);
875 } else if (nxtptr && current_work()) {
Jens Axboeba816ad2019-09-28 11:36:45 -0600876 *nxtptr = nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700877 break;
878 } else {
Jackie Liua197f662019-11-08 08:09:12 -0700879 io_queue_async_work(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700880 break;
881 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600882 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700883
884 if (wake_ev)
885 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600886}
887
888/*
889 * Called if REQ_F_LINK is set, and we fail the head request
890 */
891static void io_fail_links(struct io_kiocb *req)
892{
Jens Axboe2665abf2019-11-05 12:40:47 -0700893 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600894 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700895 unsigned long flags;
896
897 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600898
899 while (!list_empty(&req->link_list)) {
900 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700901 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600902
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200903 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700904
905 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
906 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700907 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700908 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700909 io_cqring_fill_event(link, -ECANCELED);
910 io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700911 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600912 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700913
914 io_commit_cqring(ctx);
915 spin_unlock_irqrestore(&ctx->completion_lock, flags);
916 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600917}
918
Jackie Liuc69f8db2019-11-09 11:00:08 +0800919static void io_free_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600920{
Jens Axboe2665abf2019-11-05 12:40:47 -0700921 if (likely(!(req->flags & REQ_F_LINK))) {
922 __io_free_req(req);
923 return;
924 }
925
Jens Axboe9e645e112019-05-10 16:07:28 -0600926 /*
927 * If LINK is set, we have dependent requests in this chain. If we
928 * didn't fail this request, queue the first one up, moving any other
929 * dependencies to the next request. In case of failure, fail the rest
930 * of the chain.
931 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700932 if (req->flags & REQ_F_FAIL_LINK) {
933 io_fail_links(req);
934 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
935 struct io_ring_ctx *ctx = req->ctx;
936 unsigned long flags;
937
938 /*
939 * If this is a timeout link, we could be racing with the
940 * timeout timer. Grab the completion lock for this case to
941 * protection against that.
942 */
943 spin_lock_irqsave(&ctx->completion_lock, flags);
944 io_req_link_next(req, nxt);
945 spin_unlock_irqrestore(&ctx->completion_lock, flags);
946 } else {
947 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600948 }
949
950 __io_free_req(req);
951}
952
Jackie Liuc69f8db2019-11-09 11:00:08 +0800953static void io_free_req(struct io_kiocb *req)
954{
955 io_free_req_find_next(req, NULL);
956}
957
Jens Axboeba816ad2019-09-28 11:36:45 -0600958/*
959 * Drop reference to request, return next in chain (if there is one) if this
960 * was the last reference to this request.
961 */
Jackie Liuec9c02a2019-11-08 23:50:36 +0800962static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -0600963{
Jens Axboeba816ad2019-09-28 11:36:45 -0600964 struct io_kiocb *nxt = NULL;
965
Jens Axboee65ef562019-03-12 10:16:44 -0600966 if (refcount_dec_and_test(&req->refs))
Jackie Liuc69f8db2019-11-09 11:00:08 +0800967 io_free_req_find_next(req, &nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600968
Jens Axboeba816ad2019-09-28 11:36:45 -0600969 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600970 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600971 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600972 else
Jackie Liua197f662019-11-08 08:09:12 -0700973 io_queue_async_work(nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600974 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700975}
976
Jackie Liuec9c02a2019-11-08 23:50:36 +0800977static void io_put_req(struct io_kiocb *req)
978{
979 if (refcount_dec_and_test(&req->refs))
Jackie Liuc69f8db2019-11-09 11:00:08 +0800980 io_free_req(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800981}
982
Jens Axboe78e19bb2019-11-06 15:21:34 -0700983static void io_double_put_req(struct io_kiocb *req)
984{
985 /* drop both submit and complete references */
986 if (refcount_sub_and_test(2, &req->refs))
987 __io_free_req(req);
988}
989
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700990static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -0600991{
Jens Axboe84f97dc2019-11-06 11:27:53 -0700992 struct io_rings *rings = ctx->rings;
993
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700994 /*
995 * noflush == true is from the waitqueue handler, just ensure we wake
996 * up the task, and the next invocation will flush the entries. We
997 * cannot safely to it from here.
998 */
999 if (noflush && !list_empty(&ctx->cq_overflow_list))
1000 return -1U;
1001
1002 io_cqring_overflow_flush(ctx, false);
1003
Jens Axboea3a0e432019-08-20 11:03:11 -06001004 /* See comment at the top of this file */
1005 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001006 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001007}
1008
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001009static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1010{
1011 struct io_rings *rings = ctx->rings;
1012
1013 /* make sure SQ entry isn't read before tail */
1014 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1015}
1016
Jens Axboedef596e2019-01-09 08:59:42 -07001017/*
1018 * Find and free completed poll iocbs
1019 */
1020static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1021 struct list_head *done)
1022{
1023 void *reqs[IO_IOPOLL_BATCH];
1024 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001025 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001026
Jens Axboe09bb8392019-03-13 12:39:28 -06001027 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001028 while (!list_empty(done)) {
1029 req = list_first_entry(done, struct io_kiocb, list);
1030 list_del(&req->list);
1031
Jens Axboe78e19bb2019-11-06 15:21:34 -07001032 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001033 (*nr_events)++;
1034
Jens Axboe09bb8392019-03-13 12:39:28 -06001035 if (refcount_dec_and_test(&req->refs)) {
1036 /* If we're not using fixed files, we have to pair the
1037 * completion part with the file put. Use regular
1038 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001039 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001040 */
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001041 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1042 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001043 reqs[to_free++] = req;
1044 if (to_free == ARRAY_SIZE(reqs))
1045 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001046 } else {
Jackie Liuc69f8db2019-11-09 11:00:08 +08001047 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001048 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001049 }
Jens Axboedef596e2019-01-09 08:59:42 -07001050 }
Jens Axboedef596e2019-01-09 08:59:42 -07001051
Jens Axboe09bb8392019-03-13 12:39:28 -06001052 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001053 io_free_req_many(ctx, reqs, &to_free);
1054}
1055
1056static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1057 long min)
1058{
1059 struct io_kiocb *req, *tmp;
1060 LIST_HEAD(done);
1061 bool spin;
1062 int ret;
1063
1064 /*
1065 * Only spin for completions if we don't have multiple devices hanging
1066 * off our complete list, and we're under the requested amount.
1067 */
1068 spin = !ctx->poll_multi_file && *nr_events < min;
1069
1070 ret = 0;
1071 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1072 struct kiocb *kiocb = &req->rw;
1073
1074 /*
1075 * Move completed entries to our local list. If we find a
1076 * request that requires polling, break out and complete
1077 * the done list first, if we have entries there.
1078 */
1079 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1080 list_move_tail(&req->list, &done);
1081 continue;
1082 }
1083 if (!list_empty(&done))
1084 break;
1085
1086 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1087 if (ret < 0)
1088 break;
1089
1090 if (ret && spin)
1091 spin = false;
1092 ret = 0;
1093 }
1094
1095 if (!list_empty(&done))
1096 io_iopoll_complete(ctx, nr_events, &done);
1097
1098 return ret;
1099}
1100
1101/*
1102 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1103 * non-spinning poll check - we'll still enter the driver poll loop, but only
1104 * as a non-spinning completion check.
1105 */
1106static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1107 long min)
1108{
Jens Axboe08f54392019-08-21 22:19:11 -06001109 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001110 int ret;
1111
1112 ret = io_do_iopoll(ctx, nr_events, min);
1113 if (ret < 0)
1114 return ret;
1115 if (!min || *nr_events >= min)
1116 return 0;
1117 }
1118
1119 return 1;
1120}
1121
1122/*
1123 * We can't just wait for polled events to come to us, we have to actively
1124 * find and complete them.
1125 */
1126static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1127{
1128 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1129 return;
1130
1131 mutex_lock(&ctx->uring_lock);
1132 while (!list_empty(&ctx->poll_list)) {
1133 unsigned int nr_events = 0;
1134
1135 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001136
1137 /*
1138 * Ensure we allow local-to-the-cpu processing to take place,
1139 * in this case we need to ensure that we reap all events.
1140 */
1141 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001142 }
1143 mutex_unlock(&ctx->uring_lock);
1144}
1145
Jens Axboe2b2ed972019-10-25 10:06:15 -06001146static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1147 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001148{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001149 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001150
1151 do {
1152 int tmin = 0;
1153
Jens Axboe500f9fb2019-08-19 12:15:59 -06001154 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001155 * Don't enter poll loop if we already have events pending.
1156 * If we do, we can potentially be spinning for commands that
1157 * already triggered a CQE (eg in error).
1158 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001159 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001160 break;
1161
1162 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001163 * If a submit got punted to a workqueue, we can have the
1164 * application entering polling for a command before it gets
1165 * issued. That app will hold the uring_lock for the duration
1166 * of the poll right here, so we need to take a breather every
1167 * now and then to ensure that the issue has a chance to add
1168 * the poll to the issued list. Otherwise we can spin here
1169 * forever, while the workqueue is stuck trying to acquire the
1170 * very same mutex.
1171 */
1172 if (!(++iters & 7)) {
1173 mutex_unlock(&ctx->uring_lock);
1174 mutex_lock(&ctx->uring_lock);
1175 }
1176
Jens Axboedef596e2019-01-09 08:59:42 -07001177 if (*nr_events < min)
1178 tmin = min - *nr_events;
1179
1180 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1181 if (ret <= 0)
1182 break;
1183 ret = 0;
1184 } while (min && !*nr_events && !need_resched());
1185
Jens Axboe2b2ed972019-10-25 10:06:15 -06001186 return ret;
1187}
1188
1189static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1190 long min)
1191{
1192 int ret;
1193
1194 /*
1195 * We disallow the app entering submit/complete with polling, but we
1196 * still need to lock the ring to prevent racing with polled issue
1197 * that got punted to a workqueue.
1198 */
1199 mutex_lock(&ctx->uring_lock);
1200 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001201 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001202 return ret;
1203}
1204
Jens Axboe491381ce2019-10-17 09:20:46 -06001205static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001206{
Jens Axboe491381ce2019-10-17 09:20:46 -06001207 /*
1208 * Tell lockdep we inherited freeze protection from submission
1209 * thread.
1210 */
1211 if (req->flags & REQ_F_ISREG) {
1212 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001213
Jens Axboe491381ce2019-10-17 09:20:46 -06001214 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001215 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001216 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001217}
1218
Jens Axboeba816ad2019-09-28 11:36:45 -06001219static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001220{
1221 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1222
Jens Axboe491381ce2019-10-17 09:20:46 -06001223 if (kiocb->ki_flags & IOCB_WRITE)
1224 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001225
Jens Axboe9e645e112019-05-10 16:07:28 -06001226 if ((req->flags & REQ_F_LINK) && res != req->result)
1227 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001228 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001229}
1230
1231static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1232{
1233 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1234
1235 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001236 io_put_req(req);
Jens Axboeba816ad2019-09-28 11:36:45 -06001237}
1238
1239static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1240{
1241 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001242 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001243
1244 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001245 io_put_req_find_next(req, &nxt);
1246
1247 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001248}
1249
Jens Axboedef596e2019-01-09 08:59:42 -07001250static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1251{
1252 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1253
Jens Axboe491381ce2019-10-17 09:20:46 -06001254 if (kiocb->ki_flags & IOCB_WRITE)
1255 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001256
Jens Axboe9e645e112019-05-10 16:07:28 -06001257 if ((req->flags & REQ_F_LINK) && res != req->result)
1258 req->flags |= REQ_F_FAIL_LINK;
1259 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001260 if (res != -EAGAIN)
1261 req->flags |= REQ_F_IOPOLL_COMPLETED;
1262}
1263
1264/*
1265 * After the iocb has been issued, it's safe to be found on the poll list.
1266 * Adding the kiocb to the list AFTER submission ensures that we don't
1267 * find it from a io_iopoll_getevents() thread before the issuer is done
1268 * accessing the kiocb cookie.
1269 */
1270static void io_iopoll_req_issued(struct io_kiocb *req)
1271{
1272 struct io_ring_ctx *ctx = req->ctx;
1273
1274 /*
1275 * Track whether we have multiple files in our lists. This will impact
1276 * how we do polling eventually, not spinning if we're on potentially
1277 * different devices.
1278 */
1279 if (list_empty(&ctx->poll_list)) {
1280 ctx->poll_multi_file = false;
1281 } else if (!ctx->poll_multi_file) {
1282 struct io_kiocb *list_req;
1283
1284 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1285 list);
1286 if (list_req->rw.ki_filp != req->rw.ki_filp)
1287 ctx->poll_multi_file = true;
1288 }
1289
1290 /*
1291 * For fast devices, IO may have already completed. If it has, add
1292 * it to the front so we find it first.
1293 */
1294 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1295 list_add(&req->list, &ctx->poll_list);
1296 else
1297 list_add_tail(&req->list, &ctx->poll_list);
1298}
1299
Jens Axboe3d6770f2019-04-13 11:50:54 -06001300static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001301{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001302 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001303 int diff = state->has_refs - state->used_refs;
1304
1305 if (diff)
1306 fput_many(state->file, diff);
1307 state->file = NULL;
1308 }
1309}
1310
1311/*
1312 * Get as many references to a file as we have IOs left in this submission,
1313 * assuming most submissions are for one file, or at least that each file
1314 * has more than one submission.
1315 */
1316static struct file *io_file_get(struct io_submit_state *state, int fd)
1317{
1318 if (!state)
1319 return fget(fd);
1320
1321 if (state->file) {
1322 if (state->fd == fd) {
1323 state->used_refs++;
1324 state->ios_left--;
1325 return state->file;
1326 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001327 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001328 }
1329 state->file = fget_many(fd, state->ios_left);
1330 if (!state->file)
1331 return NULL;
1332
1333 state->fd = fd;
1334 state->has_refs = state->ios_left;
1335 state->used_refs = 1;
1336 state->ios_left--;
1337 return state->file;
1338}
1339
Jens Axboe2b188cc2019-01-07 10:46:33 -07001340/*
1341 * If we tracked the file through the SCM inflight mechanism, we could support
1342 * any file. For now, just ensure that anything potentially problematic is done
1343 * inline.
1344 */
1345static bool io_file_supports_async(struct file *file)
1346{
1347 umode_t mode = file_inode(file)->i_mode;
1348
1349 if (S_ISBLK(mode) || S_ISCHR(mode))
1350 return true;
1351 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1352 return true;
1353
1354 return false;
1355}
1356
Pavel Begunkov267bc902019-11-07 01:41:08 +03001357static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001358{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001359 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001360 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001361 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001362 unsigned ioprio;
1363 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364
Jens Axboe09bb8392019-03-13 12:39:28 -06001365 if (!req->file)
1366 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001367
Jens Axboe491381ce2019-10-17 09:20:46 -06001368 if (S_ISREG(file_inode(req->file)->i_mode))
1369 req->flags |= REQ_F_ISREG;
1370
1371 /*
1372 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1373 * we know to async punt it even if it was opened O_NONBLOCK
1374 */
1375 if (force_nonblock && !io_file_supports_async(req->file)) {
1376 req->flags |= REQ_F_MUST_PUNT;
1377 return -EAGAIN;
1378 }
Jens Axboe6b063142019-01-10 22:13:58 -07001379
Jens Axboe2b188cc2019-01-07 10:46:33 -07001380 kiocb->ki_pos = READ_ONCE(sqe->off);
1381 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1382 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1383
1384 ioprio = READ_ONCE(sqe->ioprio);
1385 if (ioprio) {
1386 ret = ioprio_check_cap(ioprio);
1387 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001388 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001389
1390 kiocb->ki_ioprio = ioprio;
1391 } else
1392 kiocb->ki_ioprio = get_current_ioprio();
1393
1394 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1395 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001396 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001397
1398 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001399 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1400 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001401 req->flags |= REQ_F_NOWAIT;
1402
1403 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001404 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001405
Jens Axboedef596e2019-01-09 08:59:42 -07001406 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001407 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1408 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001409 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001410
Jens Axboedef596e2019-01-09 08:59:42 -07001411 kiocb->ki_flags |= IOCB_HIPRI;
1412 kiocb->ki_complete = io_complete_rw_iopoll;
1413 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001414 if (kiocb->ki_flags & IOCB_HIPRI)
1415 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001416 kiocb->ki_complete = io_complete_rw;
1417 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001418 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001419}
1420
1421static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1422{
1423 switch (ret) {
1424 case -EIOCBQUEUED:
1425 break;
1426 case -ERESTARTSYS:
1427 case -ERESTARTNOINTR:
1428 case -ERESTARTNOHAND:
1429 case -ERESTART_RESTARTBLOCK:
1430 /*
1431 * We can't just restart the syscall, since previously
1432 * submitted sqes may already be in progress. Just fail this
1433 * IO with EINTR.
1434 */
1435 ret = -EINTR;
1436 /* fall through */
1437 default:
1438 kiocb->ki_complete(kiocb, ret, 0);
1439 }
1440}
1441
Jens Axboeba816ad2019-09-28 11:36:45 -06001442static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1443 bool in_async)
1444{
1445 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1446 *nxt = __io_complete_rw(kiocb, ret);
1447 else
1448 io_rw_done(kiocb, ret);
1449}
1450
Jens Axboeedafcce2019-01-09 09:16:05 -07001451static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1452 const struct io_uring_sqe *sqe,
1453 struct iov_iter *iter)
1454{
1455 size_t len = READ_ONCE(sqe->len);
1456 struct io_mapped_ubuf *imu;
1457 unsigned index, buf_index;
1458 size_t offset;
1459 u64 buf_addr;
1460
1461 /* attempt to use fixed buffers without having provided iovecs */
1462 if (unlikely(!ctx->user_bufs))
1463 return -EFAULT;
1464
1465 buf_index = READ_ONCE(sqe->buf_index);
1466 if (unlikely(buf_index >= ctx->nr_user_bufs))
1467 return -EFAULT;
1468
1469 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1470 imu = &ctx->user_bufs[index];
1471 buf_addr = READ_ONCE(sqe->addr);
1472
1473 /* overflow */
1474 if (buf_addr + len < buf_addr)
1475 return -EFAULT;
1476 /* not inside the mapped region */
1477 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1478 return -EFAULT;
1479
1480 /*
1481 * May not be a start of buffer, set size appropriately
1482 * and advance us to the beginning.
1483 */
1484 offset = buf_addr - imu->ubuf;
1485 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001486
1487 if (offset) {
1488 /*
1489 * Don't use iov_iter_advance() here, as it's really slow for
1490 * using the latter parts of a big fixed buffer - it iterates
1491 * over each segment manually. We can cheat a bit here, because
1492 * we know that:
1493 *
1494 * 1) it's a BVEC iter, we set it up
1495 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1496 * first and last bvec
1497 *
1498 * So just find our index, and adjust the iterator afterwards.
1499 * If the offset is within the first bvec (or the whole first
1500 * bvec, just use iov_iter_advance(). This makes it easier
1501 * since we can just skip the first segment, which may not
1502 * be PAGE_SIZE aligned.
1503 */
1504 const struct bio_vec *bvec = imu->bvec;
1505
1506 if (offset <= bvec->bv_len) {
1507 iov_iter_advance(iter, offset);
1508 } else {
1509 unsigned long seg_skip;
1510
1511 /* skip first vec */
1512 offset -= bvec->bv_len;
1513 seg_skip = 1 + (offset >> PAGE_SHIFT);
1514
1515 iter->bvec = bvec + seg_skip;
1516 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001517 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001518 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001519 }
1520 }
1521
Jens Axboeedafcce2019-01-09 09:16:05 -07001522 return 0;
1523}
1524
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001525static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1526 const struct sqe_submit *s, struct iovec **iovec,
1527 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001528{
1529 const struct io_uring_sqe *sqe = s->sqe;
1530 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1531 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001532 u8 opcode;
1533
1534 /*
1535 * We're reading ->opcode for the second time, but the first read
1536 * doesn't care whether it's _FIXED or not, so it doesn't matter
1537 * whether ->opcode changes concurrently. The first read does care
1538 * about whether it is a READ or a WRITE, so we don't trust this read
1539 * for that purpose and instead let the caller pass in the read/write
1540 * flag.
1541 */
1542 opcode = READ_ONCE(sqe->opcode);
1543 if (opcode == IORING_OP_READ_FIXED ||
1544 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001545 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001546 *iovec = NULL;
1547 return ret;
1548 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001549
1550 if (!s->has_user)
1551 return -EFAULT;
1552
1553#ifdef CONFIG_COMPAT
1554 if (ctx->compat)
1555 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1556 iovec, iter);
1557#endif
1558
1559 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1560}
1561
Jens Axboe32960612019-09-23 11:05:34 -06001562/*
1563 * For files that don't have ->read_iter() and ->write_iter(), handle them
1564 * by looping over ->read() or ->write() manually.
1565 */
1566static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1567 struct iov_iter *iter)
1568{
1569 ssize_t ret = 0;
1570
1571 /*
1572 * Don't support polled IO through this interface, and we can't
1573 * support non-blocking either. For the latter, this just causes
1574 * the kiocb to be handled from an async context.
1575 */
1576 if (kiocb->ki_flags & IOCB_HIPRI)
1577 return -EOPNOTSUPP;
1578 if (kiocb->ki_flags & IOCB_NOWAIT)
1579 return -EAGAIN;
1580
1581 while (iov_iter_count(iter)) {
1582 struct iovec iovec = iov_iter_iovec(iter);
1583 ssize_t nr;
1584
1585 if (rw == READ) {
1586 nr = file->f_op->read(file, iovec.iov_base,
1587 iovec.iov_len, &kiocb->ki_pos);
1588 } else {
1589 nr = file->f_op->write(file, iovec.iov_base,
1590 iovec.iov_len, &kiocb->ki_pos);
1591 }
1592
1593 if (nr < 0) {
1594 if (!ret)
1595 ret = nr;
1596 break;
1597 }
1598 ret += nr;
1599 if (nr != iovec.iov_len)
1600 break;
1601 iov_iter_advance(iter, nr);
1602 }
1603
1604 return ret;
1605}
1606
Pavel Begunkov267bc902019-11-07 01:41:08 +03001607static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1608 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001609{
1610 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1611 struct kiocb *kiocb = &req->rw;
1612 struct iov_iter iter;
1613 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001614 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001615 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001616
Pavel Begunkov267bc902019-11-07 01:41:08 +03001617 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001618 if (ret)
1619 return ret;
1620 file = kiocb->ki_filp;
1621
Jens Axboe2b188cc2019-01-07 10:46:33 -07001622 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001623 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001624
Pavel Begunkov267bc902019-11-07 01:41:08 +03001625 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001626 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001627 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001628
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001629 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001630 if (req->flags & REQ_F_LINK)
1631 req->result = read_size;
1632
Jens Axboe31b51512019-01-18 22:56:34 -07001633 iov_count = iov_iter_count(&iter);
1634 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001635 if (!ret) {
1636 ssize_t ret2;
1637
Jens Axboe32960612019-09-23 11:05:34 -06001638 if (file->f_op->read_iter)
1639 ret2 = call_read_iter(file, kiocb, &iter);
1640 else
1641 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1642
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001643 /*
1644 * In case of a short read, punt to async. This can happen
1645 * if we have data partially cached. Alternatively we can
1646 * return the short read, in which case the application will
1647 * need to issue another SQE and wait for it. That SQE will
1648 * need async punt anyway, so it's more efficient to do it
1649 * here.
1650 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001651 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1652 (req->flags & REQ_F_ISREG) &&
1653 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001654 ret2 = -EAGAIN;
1655 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001656 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001657 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001658 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001659 ret = -EAGAIN;
1660 }
1661 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001662 return ret;
1663}
1664
Pavel Begunkov267bc902019-11-07 01:41:08 +03001665static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1666 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001667{
1668 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1669 struct kiocb *kiocb = &req->rw;
1670 struct iov_iter iter;
1671 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001672 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001673 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674
Pavel Begunkov267bc902019-11-07 01:41:08 +03001675 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001676 if (ret)
1677 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001678
Jens Axboe2b188cc2019-01-07 10:46:33 -07001679 file = kiocb->ki_filp;
1680 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001681 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001682
Pavel Begunkov267bc902019-11-07 01:41:08 +03001683 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001684 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001685 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001686
Jens Axboe9e645e112019-05-10 16:07:28 -06001687 if (req->flags & REQ_F_LINK)
1688 req->result = ret;
1689
Jens Axboe31b51512019-01-18 22:56:34 -07001690 iov_count = iov_iter_count(&iter);
1691
1692 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001693 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001694 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001695
1696 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001697 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001698 ssize_t ret2;
1699
Jens Axboe2b188cc2019-01-07 10:46:33 -07001700 /*
1701 * Open-code file_start_write here to grab freeze protection,
1702 * which will be released by another thread in
1703 * io_complete_rw(). Fool lockdep by telling it the lock got
1704 * released so that it doesn't complain about the held lock when
1705 * we return to userspace.
1706 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001707 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708 __sb_start_write(file_inode(file)->i_sb,
1709 SB_FREEZE_WRITE, true);
1710 __sb_writers_release(file_inode(file)->i_sb,
1711 SB_FREEZE_WRITE);
1712 }
1713 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001714
Jens Axboe32960612019-09-23 11:05:34 -06001715 if (file->f_op->write_iter)
1716 ret2 = call_write_iter(file, kiocb, &iter);
1717 else
1718 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001719 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001720 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001721 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001722 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001723 }
Jens Axboe31b51512019-01-18 22:56:34 -07001724out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001725 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001726 return ret;
1727}
1728
1729/*
1730 * IORING_OP_NOP just posts a completion event, nothing else.
1731 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001732static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001733{
1734 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735
Jens Axboedef596e2019-01-09 08:59:42 -07001736 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1737 return -EINVAL;
1738
Jens Axboe78e19bb2019-11-06 15:21:34 -07001739 io_cqring_add_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001740 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001741 return 0;
1742}
1743
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001744static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1745{
Jens Axboe6b063142019-01-10 22:13:58 -07001746 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001747
Jens Axboe09bb8392019-03-13 12:39:28 -06001748 if (!req->file)
1749 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001750
Jens Axboe6b063142019-01-10 22:13:58 -07001751 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001752 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001753 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001754 return -EINVAL;
1755
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001756 return 0;
1757}
1758
1759static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001760 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001761{
1762 loff_t sqe_off = READ_ONCE(sqe->off);
1763 loff_t sqe_len = READ_ONCE(sqe->len);
1764 loff_t end = sqe_off + sqe_len;
1765 unsigned fsync_flags;
1766 int ret;
1767
1768 fsync_flags = READ_ONCE(sqe->fsync_flags);
1769 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1770 return -EINVAL;
1771
1772 ret = io_prep_fsync(req, sqe);
1773 if (ret)
1774 return ret;
1775
1776 /* fsync always requires a blocking context */
1777 if (force_nonblock)
1778 return -EAGAIN;
1779
1780 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1781 end > 0 ? end : LLONG_MAX,
1782 fsync_flags & IORING_FSYNC_DATASYNC);
1783
Jens Axboe9e645e112019-05-10 16:07:28 -06001784 if (ret < 0 && (req->flags & REQ_F_LINK))
1785 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001786 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001787 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001788 return 0;
1789}
1790
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001791static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1792{
1793 struct io_ring_ctx *ctx = req->ctx;
1794 int ret = 0;
1795
1796 if (!req->file)
1797 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001798
1799 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1800 return -EINVAL;
1801 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1802 return -EINVAL;
1803
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001804 return ret;
1805}
1806
1807static int io_sync_file_range(struct io_kiocb *req,
1808 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001809 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001810 bool force_nonblock)
1811{
1812 loff_t sqe_off;
1813 loff_t sqe_len;
1814 unsigned flags;
1815 int ret;
1816
1817 ret = io_prep_sfr(req, sqe);
1818 if (ret)
1819 return ret;
1820
1821 /* sync_file_range always requires a blocking context */
1822 if (force_nonblock)
1823 return -EAGAIN;
1824
1825 sqe_off = READ_ONCE(sqe->off);
1826 sqe_len = READ_ONCE(sqe->len);
1827 flags = READ_ONCE(sqe->sync_range_flags);
1828
1829 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1830
Jens Axboe9e645e112019-05-10 16:07:28 -06001831 if (ret < 0 && (req->flags & REQ_F_LINK))
1832 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001833 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001834 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001835 return 0;
1836}
1837
Jens Axboe0fa03c62019-04-19 13:34:07 -06001838#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001839static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001840 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001841 long (*fn)(struct socket *, struct user_msghdr __user *,
1842 unsigned int))
1843{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001844 struct socket *sock;
1845 int ret;
1846
1847 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1848 return -EINVAL;
1849
1850 sock = sock_from_file(req->file, &ret);
1851 if (sock) {
1852 struct user_msghdr __user *msg;
1853 unsigned flags;
1854
1855 flags = READ_ONCE(sqe->msg_flags);
1856 if (flags & MSG_DONTWAIT)
1857 req->flags |= REQ_F_NOWAIT;
1858 else if (force_nonblock)
1859 flags |= MSG_DONTWAIT;
1860
1861 msg = (struct user_msghdr __user *) (unsigned long)
1862 READ_ONCE(sqe->addr);
1863
Jens Axboeaa1fa282019-04-19 13:38:09 -06001864 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001865 if (force_nonblock && ret == -EAGAIN)
1866 return ret;
1867 }
1868
Jens Axboe78e19bb2019-11-06 15:21:34 -07001869 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001870 if (ret < 0 && (req->flags & REQ_F_LINK))
1871 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001872 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001873 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001874}
1875#endif
1876
1877static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001878 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001879{
1880#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001881 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1882 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001883#else
1884 return -EOPNOTSUPP;
1885#endif
1886}
1887
1888static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001889 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001890{
1891#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001892 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1893 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001894#else
1895 return -EOPNOTSUPP;
1896#endif
1897}
1898
Jens Axboe17f2fe32019-10-17 14:42:58 -06001899static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1900 struct io_kiocb **nxt, bool force_nonblock)
1901{
1902#if defined(CONFIG_NET)
1903 struct sockaddr __user *addr;
1904 int __user *addr_len;
1905 unsigned file_flags;
1906 int flags, ret;
1907
1908 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1909 return -EINVAL;
1910 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1911 return -EINVAL;
1912
1913 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1914 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1915 flags = READ_ONCE(sqe->accept_flags);
1916 file_flags = force_nonblock ? O_NONBLOCK : 0;
1917
1918 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1919 if (ret == -EAGAIN && force_nonblock) {
1920 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1921 return -EAGAIN;
1922 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001923 if (ret == -ERESTARTSYS)
1924 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001925 if (ret < 0 && (req->flags & REQ_F_LINK))
1926 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001927 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001928 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001929 return 0;
1930#else
1931 return -EOPNOTSUPP;
1932#endif
1933}
1934
Jens Axboe221c5eb2019-01-17 09:41:58 -07001935static void io_poll_remove_one(struct io_kiocb *req)
1936{
1937 struct io_poll_iocb *poll = &req->poll;
1938
1939 spin_lock(&poll->head->lock);
1940 WRITE_ONCE(poll->canceled, true);
1941 if (!list_empty(&poll->wait.entry)) {
1942 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07001943 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001944 }
1945 spin_unlock(&poll->head->lock);
1946
1947 list_del_init(&req->list);
1948}
1949
1950static void io_poll_remove_all(struct io_ring_ctx *ctx)
1951{
1952 struct io_kiocb *req;
1953
1954 spin_lock_irq(&ctx->completion_lock);
1955 while (!list_empty(&ctx->cancel_list)) {
1956 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1957 io_poll_remove_one(req);
1958 }
1959 spin_unlock_irq(&ctx->completion_lock);
1960}
1961
Jens Axboe47f46762019-11-09 17:43:02 -07001962static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
1963{
1964 struct io_kiocb *req;
1965
1966 list_for_each_entry(req, &ctx->cancel_list, list) {
1967 if (req->user_data != sqe_addr)
1968 continue;
1969 io_poll_remove_one(req);
1970 return 0;
1971 }
1972
1973 return -ENOENT;
1974}
1975
Jens Axboe221c5eb2019-01-17 09:41:58 -07001976/*
1977 * Find a running poll command that matches one specified in sqe->addr,
1978 * and remove it if found.
1979 */
1980static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1981{
1982 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07001983 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001984
1985 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1986 return -EINVAL;
1987 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1988 sqe->poll_events)
1989 return -EINVAL;
1990
1991 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07001992 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07001993 spin_unlock_irq(&ctx->completion_lock);
1994
Jens Axboe78e19bb2019-11-06 15:21:34 -07001995 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001996 if (ret < 0 && (req->flags & REQ_F_LINK))
1997 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001998 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001999 return 0;
2000}
2001
Jackie Liua197f662019-11-08 08:09:12 -07002002static void io_poll_complete(struct io_kiocb *req, __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002003{
Jackie Liua197f662019-11-08 08:09:12 -07002004 struct io_ring_ctx *ctx = req->ctx;
2005
Jens Axboe8c838782019-03-12 15:48:16 -06002006 req->poll.done = true;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002007 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002008 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002009}
2010
Jens Axboe561fb042019-10-24 07:25:42 -06002011static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002012{
Jens Axboe561fb042019-10-24 07:25:42 -06002013 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002014 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2015 struct io_poll_iocb *poll = &req->poll;
2016 struct poll_table_struct pt = { ._key = poll->events };
2017 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002018 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002019 __poll_t mask = 0;
2020
Jens Axboe561fb042019-10-24 07:25:42 -06002021 if (work->flags & IO_WQ_WORK_CANCEL)
2022 WRITE_ONCE(poll->canceled, true);
2023
Jens Axboe221c5eb2019-01-17 09:41:58 -07002024 if (!READ_ONCE(poll->canceled))
2025 mask = vfs_poll(poll->file, &pt) & poll->events;
2026
2027 /*
2028 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2029 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2030 * synchronize with them. In the cancellation case the list_del_init
2031 * itself is not actually needed, but harmless so we keep it in to
2032 * avoid further branches in the fast path.
2033 */
2034 spin_lock_irq(&ctx->completion_lock);
2035 if (!mask && !READ_ONCE(poll->canceled)) {
2036 add_wait_queue(poll->head, &poll->wait);
2037 spin_unlock_irq(&ctx->completion_lock);
2038 return;
2039 }
2040 list_del_init(&req->list);
Jackie Liua197f662019-11-08 08:09:12 -07002041 io_poll_complete(req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002042 spin_unlock_irq(&ctx->completion_lock);
2043
Jens Axboe8c838782019-03-12 15:48:16 -06002044 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002045
Jackie Liuec9c02a2019-11-08 23:50:36 +08002046 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002047 if (nxt)
2048 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002049}
2050
2051static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2052 void *key)
2053{
2054 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2055 wait);
2056 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2057 struct io_ring_ctx *ctx = req->ctx;
2058 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002059 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002060
2061 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002062 if (mask && !(mask & poll->events))
2063 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002064
2065 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002066
2067 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
2068 list_del(&req->list);
Jackie Liua197f662019-11-08 08:09:12 -07002069 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06002070 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2071
2072 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002073 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002074 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002075 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002076 }
2077
Jens Axboe221c5eb2019-01-17 09:41:58 -07002078 return 1;
2079}
2080
2081struct io_poll_table {
2082 struct poll_table_struct pt;
2083 struct io_kiocb *req;
2084 int error;
2085};
2086
2087static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2088 struct poll_table_struct *p)
2089{
2090 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2091
2092 if (unlikely(pt->req->poll.head)) {
2093 pt->error = -EINVAL;
2094 return;
2095 }
2096
2097 pt->error = 0;
2098 pt->req->poll.head = head;
2099 add_wait_queue(head, &pt->req->poll.wait);
2100}
2101
Jens Axboe89723d02019-11-05 15:32:58 -07002102static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2103 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002104{
2105 struct io_poll_iocb *poll = &req->poll;
2106 struct io_ring_ctx *ctx = req->ctx;
2107 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002108 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002109 __poll_t mask;
2110 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002111
2112 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2113 return -EINVAL;
2114 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2115 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002116 if (!poll->file)
2117 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002118
Jens Axboe6cc47d12019-09-18 11:18:23 -06002119 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002120 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002121 events = READ_ONCE(sqe->poll_events);
2122 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
2123
Jens Axboe221c5eb2019-01-17 09:41:58 -07002124 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002125 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002126 poll->canceled = false;
2127
2128 ipt.pt._qproc = io_poll_queue_proc;
2129 ipt.pt._key = poll->events;
2130 ipt.req = req;
2131 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2132
2133 /* initialized the list so that we can do list_empty checks */
2134 INIT_LIST_HEAD(&poll->wait.entry);
2135 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2136
Jens Axboe36703242019-07-25 10:20:18 -06002137 INIT_LIST_HEAD(&req->list);
2138
Jens Axboe221c5eb2019-01-17 09:41:58 -07002139 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002140
2141 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002142 if (likely(poll->head)) {
2143 spin_lock(&poll->head->lock);
2144 if (unlikely(list_empty(&poll->wait.entry))) {
2145 if (ipt.error)
2146 cancel = true;
2147 ipt.error = 0;
2148 mask = 0;
2149 }
2150 if (mask || ipt.error)
2151 list_del_init(&poll->wait.entry);
2152 else if (cancel)
2153 WRITE_ONCE(poll->canceled, true);
2154 else if (!poll->done) /* actually waiting for an event */
2155 list_add_tail(&req->list, &ctx->cancel_list);
2156 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002157 }
Jens Axboe8c838782019-03-12 15:48:16 -06002158 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002159 ipt.error = 0;
Jackie Liua197f662019-11-08 08:09:12 -07002160 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06002161 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002162 spin_unlock_irq(&ctx->completion_lock);
2163
Jens Axboe8c838782019-03-12 15:48:16 -06002164 if (mask) {
2165 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002166 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002167 }
Jens Axboe8c838782019-03-12 15:48:16 -06002168 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002169}
2170
Jens Axboe5262f562019-09-17 12:26:57 -06002171static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2172{
2173 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002174 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06002175 unsigned long flags;
2176
2177 req = container_of(timer, struct io_kiocb, timeout.timer);
2178 ctx = req->ctx;
2179 atomic_inc(&ctx->cq_timeouts);
2180
2181 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002182 /*
Jens Axboe11365042019-10-16 09:08:32 -06002183 * We could be racing with timeout deletion. If the list is empty,
2184 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002185 */
Jens Axboe842f9612019-10-29 12:34:10 -06002186 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002187 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002188
Jens Axboe11365042019-10-16 09:08:32 -06002189 /*
2190 * Adjust the reqs sequence before the current one because it
2191 * will consume a slot in the cq_ring and the the cq_tail
2192 * pointer will be increased, otherwise other timeout reqs may
2193 * return in advance without waiting for enough wait_nr.
2194 */
2195 prev = req;
2196 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2197 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002198 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002199 }
Jens Axboe842f9612019-10-29 12:34:10 -06002200
Jens Axboe78e19bb2019-11-06 15:21:34 -07002201 io_cqring_fill_event(req, -ETIME);
Jens Axboe842f9612019-10-29 12:34:10 -06002202 io_commit_cqring(ctx);
Jens Axboe5262f562019-09-17 12:26:57 -06002203 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2204
Jens Axboe842f9612019-10-29 12:34:10 -06002205 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002206 if (req->flags & REQ_F_LINK)
2207 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002208 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002209 return HRTIMER_NORESTART;
2210}
2211
Jens Axboe47f46762019-11-09 17:43:02 -07002212static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2213{
2214 struct io_kiocb *req;
2215 int ret = -ENOENT;
2216
2217 list_for_each_entry(req, &ctx->timeout_list, list) {
2218 if (user_data == req->user_data) {
2219 list_del_init(&req->list);
2220 ret = 0;
2221 break;
2222 }
2223 }
2224
2225 if (ret == -ENOENT)
2226 return ret;
2227
2228 ret = hrtimer_try_to_cancel(&req->timeout.timer);
2229 if (ret == -1)
2230 return -EALREADY;
2231
2232 io_cqring_fill_event(req, -ECANCELED);
2233 io_put_req(req);
2234 return 0;
2235}
2236
Jens Axboe11365042019-10-16 09:08:32 -06002237/*
2238 * Remove or update an existing timeout command
2239 */
2240static int io_timeout_remove(struct io_kiocb *req,
2241 const struct io_uring_sqe *sqe)
2242{
2243 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002244 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002245 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002246
2247 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2248 return -EINVAL;
2249 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2250 return -EINVAL;
2251 flags = READ_ONCE(sqe->timeout_flags);
2252 if (flags)
2253 return -EINVAL;
2254
Jens Axboe11365042019-10-16 09:08:32 -06002255 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002256 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002257
Jens Axboe47f46762019-11-09 17:43:02 -07002258 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002259 io_commit_cqring(ctx);
2260 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002261 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002262 if (ret < 0 && req->flags & REQ_F_LINK)
2263 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002264 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002265 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002266}
2267
2268static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2269{
yangerkun5da0fb12019-10-15 21:59:29 +08002270 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002271 struct io_ring_ctx *ctx = req->ctx;
2272 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002273 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002274 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002275 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002276 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002277
2278 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2279 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002280 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2281 return -EINVAL;
2282 flags = READ_ONCE(sqe->timeout_flags);
2283 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002284 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002285
2286 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002287 return -EFAULT;
2288
Jens Axboe11365042019-10-16 09:08:32 -06002289 if (flags & IORING_TIMEOUT_ABS)
2290 mode = HRTIMER_MODE_ABS;
2291 else
2292 mode = HRTIMER_MODE_REL;
2293
2294 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2295
Jens Axboe5262f562019-09-17 12:26:57 -06002296 /*
2297 * sqe->off holds how many events that need to occur for this
2298 * timeout event to be satisfied.
2299 */
2300 count = READ_ONCE(sqe->off);
2301 if (!count)
2302 count = 1;
2303
2304 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002305 /* reuse it to store the count */
2306 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002307 req->flags |= REQ_F_TIMEOUT;
2308
2309 /*
2310 * Insertion sort, ensuring the first entry in the list is always
2311 * the one we need first.
2312 */
Jens Axboe5262f562019-09-17 12:26:57 -06002313 spin_lock_irq(&ctx->completion_lock);
2314 list_for_each_prev(entry, &ctx->timeout_list) {
2315 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002316 unsigned nxt_sq_head;
2317 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002318
yangerkun5da0fb12019-10-15 21:59:29 +08002319 /*
2320 * Since cached_sq_head + count - 1 can overflow, use type long
2321 * long to store it.
2322 */
2323 tmp = (long long)ctx->cached_sq_head + count - 1;
2324 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2325 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2326
2327 /*
2328 * cached_sq_head may overflow, and it will never overflow twice
2329 * once there is some timeout req still be valid.
2330 */
2331 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002332 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002333
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002334 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002335 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002336
2337 /*
2338 * Sequence of reqs after the insert one and itself should
2339 * be adjusted because each timeout req consumes a slot.
2340 */
2341 span++;
2342 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002343 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002344 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002345 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002346 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002347 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002348 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002349 return 0;
2350}
2351
Jens Axboe62755e32019-10-28 21:49:21 -06002352static bool io_cancel_cb(struct io_wq_work *work, void *data)
2353{
2354 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2355
2356 return req->user_data == (unsigned long) data;
2357}
2358
Jens Axboee977d6d2019-11-05 12:39:45 -07002359static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002360{
Jens Axboe62755e32019-10-28 21:49:21 -06002361 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002362 int ret = 0;
2363
Jens Axboe62755e32019-10-28 21:49:21 -06002364 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2365 switch (cancel_ret) {
2366 case IO_WQ_CANCEL_OK:
2367 ret = 0;
2368 break;
2369 case IO_WQ_CANCEL_RUNNING:
2370 ret = -EALREADY;
2371 break;
2372 case IO_WQ_CANCEL_NOTFOUND:
2373 ret = -ENOENT;
2374 break;
2375 }
2376
Jens Axboee977d6d2019-11-05 12:39:45 -07002377 return ret;
2378}
2379
Jens Axboe47f46762019-11-09 17:43:02 -07002380static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2381 struct io_kiocb *req, __u64 sqe_addr,
2382 struct io_kiocb **nxt)
2383{
2384 unsigned long flags;
2385 int ret;
2386
2387 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2388 if (ret != -ENOENT) {
2389 spin_lock_irqsave(&ctx->completion_lock, flags);
2390 goto done;
2391 }
2392
2393 spin_lock_irqsave(&ctx->completion_lock, flags);
2394 ret = io_timeout_cancel(ctx, sqe_addr);
2395 if (ret != -ENOENT)
2396 goto done;
2397 ret = io_poll_cancel(ctx, sqe_addr);
2398done:
2399 io_cqring_fill_event(req, ret);
2400 io_commit_cqring(ctx);
2401 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2402 io_cqring_ev_posted(ctx);
2403
2404 if (ret < 0 && (req->flags & REQ_F_LINK))
2405 req->flags |= REQ_F_FAIL_LINK;
2406 io_put_req_find_next(req, nxt);
2407}
2408
Jens Axboee977d6d2019-11-05 12:39:45 -07002409static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2410 struct io_kiocb **nxt)
2411{
2412 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002413
2414 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2415 return -EINVAL;
2416 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2417 sqe->cancel_flags)
2418 return -EINVAL;
2419
Jens Axboe47f46762019-11-09 17:43:02 -07002420 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), NULL);
Jens Axboe62755e32019-10-28 21:49:21 -06002421 return 0;
2422}
2423
Jackie Liua197f662019-11-08 08:09:12 -07002424static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002425{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002426 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002427 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002428 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002429
Jackie Liua197f662019-11-08 08:09:12 -07002430 if (!io_sequence_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002431 return 0;
2432
2433 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2434 if (!sqe_copy)
2435 return -EAGAIN;
2436
2437 spin_lock_irq(&ctx->completion_lock);
Jackie Liua197f662019-11-08 08:09:12 -07002438 if (!io_sequence_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002439 spin_unlock_irq(&ctx->completion_lock);
2440 kfree(sqe_copy);
2441 return 0;
2442 }
2443
2444 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2445 req->submit.sqe = sqe_copy;
2446
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002447 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002448 list_add_tail(&req->list, &ctx->defer_list);
2449 spin_unlock_irq(&ctx->completion_lock);
2450 return -EIOCBQUEUED;
2451}
2452
Jackie Liua197f662019-11-08 08:09:12 -07002453static int __io_submit_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2454 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002455{
Jens Axboee0c5c572019-03-12 10:18:47 -06002456 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002457 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002458 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002459
Jens Axboe2b188cc2019-01-07 10:46:33 -07002460 opcode = READ_ONCE(s->sqe->opcode);
2461 switch (opcode) {
2462 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002463 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002464 break;
2465 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002466 if (unlikely(s->sqe->buf_index))
2467 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002468 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002469 break;
2470 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002471 if (unlikely(s->sqe->buf_index))
2472 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002473 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002474 break;
2475 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002476 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002477 break;
2478 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002479 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002480 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002481 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002482 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002483 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002484 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002485 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002486 break;
2487 case IORING_OP_POLL_REMOVE:
2488 ret = io_poll_remove(req, s->sqe);
2489 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002490 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002491 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002492 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002493 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002494 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002495 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002496 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002497 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002498 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002499 case IORING_OP_TIMEOUT:
2500 ret = io_timeout(req, s->sqe);
2501 break;
Jens Axboe11365042019-10-16 09:08:32 -06002502 case IORING_OP_TIMEOUT_REMOVE:
2503 ret = io_timeout_remove(req, s->sqe);
2504 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002505 case IORING_OP_ACCEPT:
2506 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2507 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002508 case IORING_OP_ASYNC_CANCEL:
2509 ret = io_async_cancel(req, s->sqe, nxt);
2510 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002511 default:
2512 ret = -EINVAL;
2513 break;
2514 }
2515
Jens Axboedef596e2019-01-09 08:59:42 -07002516 if (ret)
2517 return ret;
2518
2519 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002520 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002521 return -EAGAIN;
2522
2523 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002524 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002525 mutex_lock(&ctx->uring_lock);
2526 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002527 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002528 mutex_unlock(&ctx->uring_lock);
2529 }
2530
2531 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002532}
2533
Jens Axboe561fb042019-10-24 07:25:42 -06002534static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002535{
Jens Axboe561fb042019-10-24 07:25:42 -06002536 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002537 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002538 struct sqe_submit *s = &req->submit;
2539 const struct io_uring_sqe *sqe = s->sqe;
2540 struct io_kiocb *nxt = NULL;
2541 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002542
Jens Axboe561fb042019-10-24 07:25:42 -06002543 /* Ensure we clear previously set non-block flag */
2544 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002545
Jens Axboe561fb042019-10-24 07:25:42 -06002546 if (work->flags & IO_WQ_WORK_CANCEL)
2547 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002548
Jens Axboe561fb042019-10-24 07:25:42 -06002549 if (!ret) {
2550 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2551 s->in_async = true;
2552 do {
Jackie Liua197f662019-11-08 08:09:12 -07002553 ret = __io_submit_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002554 /*
2555 * We can get EAGAIN for polled IO even though we're
2556 * forcing a sync submission from here, since we can't
2557 * wait for request slots on the block side.
2558 */
2559 if (ret != -EAGAIN)
2560 break;
2561 cond_resched();
2562 } while (1);
2563 }
Jens Axboe31b51512019-01-18 22:56:34 -07002564
Jens Axboe561fb042019-10-24 07:25:42 -06002565 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002566 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002567
Jens Axboe561fb042019-10-24 07:25:42 -06002568 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002569 if (req->flags & REQ_F_LINK)
2570 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002571 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002572 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002573 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002574
Jens Axboe561fb042019-10-24 07:25:42 -06002575 /* async context always use a copy of the sqe */
2576 kfree(sqe);
2577
2578 /* if a dependent link is ready, pass it back */
2579 if (!ret && nxt) {
2580 io_prep_async_work(nxt);
2581 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002582 }
Jens Axboe31b51512019-01-18 22:56:34 -07002583}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002584
Jens Axboe09bb8392019-03-13 12:39:28 -06002585static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2586{
2587 int op = READ_ONCE(sqe->opcode);
2588
2589 switch (op) {
2590 case IORING_OP_NOP:
2591 case IORING_OP_POLL_REMOVE:
2592 return false;
2593 default:
2594 return true;
2595 }
2596}
2597
Jens Axboe65e19f52019-10-26 07:20:21 -06002598static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2599 int index)
2600{
2601 struct fixed_file_table *table;
2602
2603 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2604 return table->files[index & IORING_FILE_TABLE_MASK];
2605}
2606
Jackie Liua197f662019-11-08 08:09:12 -07002607static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002608{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002609 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002610 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002611 unsigned flags;
2612 int fd;
2613
2614 flags = READ_ONCE(s->sqe->flags);
2615 fd = READ_ONCE(s->sqe->fd);
2616
Jackie Liu4fe2c962019-09-09 20:50:40 +08002617 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002618 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002619 /*
2620 * All io need record the previous position, if LINK vs DARIN,
2621 * it can be used to mark the position of the first IO in the
2622 * link list.
2623 */
2624 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002625
Jens Axboe60c112b2019-06-21 10:20:18 -06002626 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002627 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002628
2629 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002630 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002631 (unsigned) fd >= ctx->nr_user_files))
2632 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002633 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002634 req->file = io_file_from_index(ctx, fd);
2635 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002636 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002637 req->flags |= REQ_F_FIXED_FILE;
2638 } else {
2639 if (s->needs_fixed_file)
2640 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002641 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002642 req->file = io_file_get(state, fd);
2643 if (unlikely(!req->file))
2644 return -EBADF;
2645 }
2646
2647 return 0;
2648}
2649
Jackie Liua197f662019-11-08 08:09:12 -07002650static int io_grab_files(struct io_kiocb *req)
Jens Axboefcb323c2019-10-24 12:39:47 -06002651{
2652 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002653 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002654
2655 rcu_read_lock();
2656 spin_lock_irq(&ctx->inflight_lock);
2657 /*
2658 * We use the f_ops->flush() handler to ensure that we can flush
2659 * out work accessing these files if the fd is closed. Check if
2660 * the fd has changed since we started down this path, and disallow
2661 * this operation if it has.
2662 */
2663 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2664 list_add(&req->inflight_entry, &ctx->inflight_list);
2665 req->flags |= REQ_F_INFLIGHT;
2666 req->work.files = current->files;
2667 ret = 0;
2668 }
2669 spin_unlock_irq(&ctx->inflight_lock);
2670 rcu_read_unlock();
2671
2672 return ret;
2673}
2674
Jens Axboe2665abf2019-11-05 12:40:47 -07002675static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2676{
2677 struct io_kiocb *req = container_of(timer, struct io_kiocb,
2678 timeout.timer);
2679 struct io_ring_ctx *ctx = req->ctx;
2680 struct io_kiocb *prev = NULL;
2681 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002682
2683 spin_lock_irqsave(&ctx->completion_lock, flags);
2684
2685 /*
2686 * We don't expect the list to be empty, that will only happen if we
2687 * race with the completion of the linked work.
2688 */
2689 if (!list_empty(&req->list)) {
2690 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
2691 list_del_init(&req->list);
2692 }
2693
2694 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2695
2696 if (prev) {
Jens Axboe47f46762019-11-09 17:43:02 -07002697 io_async_find_and_cancel(ctx, req, prev->user_data, NULL);
2698 } else {
2699 io_cqring_add_event(req, -ETIME);
2700 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002701 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002702 return HRTIMER_NORESTART;
2703}
2704
2705static int io_queue_linked_timeout(struct io_kiocb *req, struct io_kiocb *nxt)
2706{
2707 const struct io_uring_sqe *sqe = nxt->submit.sqe;
2708 enum hrtimer_mode mode;
2709 struct timespec64 ts;
2710 int ret = -EINVAL;
2711
2712 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || sqe->off)
2713 goto err;
2714 if (sqe->timeout_flags & ~IORING_TIMEOUT_ABS)
2715 goto err;
2716 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr))) {
2717 ret = -EFAULT;
2718 goto err;
2719 }
2720
2721 req->flags |= REQ_F_LINK_TIMEOUT;
2722
2723 if (sqe->timeout_flags & IORING_TIMEOUT_ABS)
2724 mode = HRTIMER_MODE_ABS;
2725 else
2726 mode = HRTIMER_MODE_REL;
2727 hrtimer_init(&nxt->timeout.timer, CLOCK_MONOTONIC, mode);
2728 nxt->timeout.timer.function = io_link_timeout_fn;
2729 hrtimer_start(&nxt->timeout.timer, timespec64_to_ktime(ts), mode);
2730 ret = 0;
2731err:
2732 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002733 io_put_req(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -07002734
2735 if (ret) {
2736 struct io_ring_ctx *ctx = req->ctx;
2737
2738 /*
2739 * Break the link and fail linked timeout, parent will get
2740 * failed by the regular submission path.
2741 */
2742 list_del(&nxt->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002743 io_cqring_fill_event(nxt, ret);
Jens Axboe2665abf2019-11-05 12:40:47 -07002744 trace_io_uring_fail_link(req, nxt);
2745 io_commit_cqring(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002746 io_put_req(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -07002747 ret = -ECANCELED;
2748 }
2749
2750 return ret;
2751}
2752
2753static inline struct io_kiocb *io_get_linked_timeout(struct io_kiocb *req)
2754{
2755 struct io_kiocb *nxt;
2756
2757 if (!(req->flags & REQ_F_LINK))
2758 return NULL;
2759
2760 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
2761 if (nxt && nxt->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT)
2762 return nxt;
2763
2764 return NULL;
2765}
2766
Jackie Liua197f662019-11-08 08:09:12 -07002767static int __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002768{
Jens Axboe2665abf2019-11-05 12:40:47 -07002769 struct io_kiocb *nxt;
Jens Axboee0c5c572019-03-12 10:18:47 -06002770 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002771
Jens Axboe2665abf2019-11-05 12:40:47 -07002772 nxt = io_get_linked_timeout(req);
2773 if (unlikely(nxt)) {
2774 ret = io_queue_linked_timeout(req, nxt);
2775 if (ret)
2776 goto err;
2777 }
2778
Jackie Liua197f662019-11-08 08:09:12 -07002779 ret = __io_submit_sqe(req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002780
2781 /*
2782 * We async punt it if the file wasn't marked NOWAIT, or if the file
2783 * doesn't support non-blocking read/write attempts
2784 */
2785 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2786 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002787 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002788 struct io_uring_sqe *sqe_copy;
2789
Jackie Liu954dab12019-09-18 10:37:52 +08002790 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002791 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002792 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002793 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
Jackie Liua197f662019-11-08 08:09:12 -07002794 ret = io_grab_files(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06002795 if (ret) {
2796 kfree(sqe_copy);
2797 goto err;
2798 }
2799 }
Jens Axboee65ef562019-03-12 10:16:44 -06002800
2801 /*
2802 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002803 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002804 */
Jackie Liua197f662019-11-08 08:09:12 -07002805 io_queue_async_work(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002806 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002807 }
2808 }
Jens Axboee65ef562019-03-12 10:16:44 -06002809
2810 /* drop submission reference */
Jens Axboefcb323c2019-10-24 12:39:47 -06002811err:
Jackie Liuec9c02a2019-11-08 23:50:36 +08002812 io_put_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002813
2814 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002815 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002816 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002817 if (req->flags & REQ_F_LINK)
2818 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002819 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002820 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002821
2822 return ret;
2823}
2824
Jackie Liua197f662019-11-08 08:09:12 -07002825static int io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002826{
2827 int ret;
2828
Jackie Liua197f662019-11-08 08:09:12 -07002829 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002830 if (ret) {
2831 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002832 io_cqring_add_event(req, ret);
2833 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002834 }
2835 return 0;
2836 }
2837
Jackie Liua197f662019-11-08 08:09:12 -07002838 return __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002839}
2840
Jackie Liua197f662019-11-08 08:09:12 -07002841static int io_queue_link_head(struct io_kiocb *req, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002842{
2843 int ret;
2844 int need_submit = false;
Jackie Liua197f662019-11-08 08:09:12 -07002845 struct io_ring_ctx *ctx = req->ctx;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002846
2847 if (!shadow)
Jackie Liua197f662019-11-08 08:09:12 -07002848 return io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002849
2850 /*
2851 * Mark the first IO in link list as DRAIN, let all the following
2852 * IOs enter the defer list. all IO needs to be completed before link
2853 * list.
2854 */
2855 req->flags |= REQ_F_IO_DRAIN;
Jackie Liua197f662019-11-08 08:09:12 -07002856 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002857 if (ret) {
2858 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002859 io_cqring_add_event(req, ret);
2860 io_double_put_req(req);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002861 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002862 return 0;
2863 }
2864 } else {
2865 /*
2866 * If ret == 0 means that all IOs in front of link io are
2867 * running done. let's queue link head.
2868 */
2869 need_submit = true;
2870 }
2871
2872 /* Insert shadow req to defer_list, blocking next IOs */
2873 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002874 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002875 list_add_tail(&shadow->list, &ctx->defer_list);
2876 spin_unlock_irq(&ctx->completion_lock);
2877
2878 if (need_submit)
Jackie Liua197f662019-11-08 08:09:12 -07002879 return __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002880
2881 return 0;
2882}
2883
Jens Axboe9e645e112019-05-10 16:07:28 -06002884#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2885
Jackie Liua197f662019-11-08 08:09:12 -07002886static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
2887 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002888{
2889 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002890 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002891 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06002892 int ret;
2893
Jens Axboe78e19bb2019-11-06 15:21:34 -07002894 req->user_data = s->sqe->user_data;
2895
Jens Axboe9e645e112019-05-10 16:07:28 -06002896 /* enforce forwards compatibility on users */
2897 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2898 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03002899 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002900 }
2901
Jackie Liua197f662019-11-08 08:09:12 -07002902 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002903 if (unlikely(ret)) {
2904err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002905 io_cqring_add_event(req, ret);
2906 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002907 return;
2908 }
2909
Jens Axboe9e645e112019-05-10 16:07:28 -06002910 /*
2911 * If we already have a head request, queue this one for async
2912 * submittal once the head completes. If we don't have a head but
2913 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2914 * submitted sync once the chain is complete. If none of those
2915 * conditions are true (normal request), then just queue it.
2916 */
2917 if (*link) {
2918 struct io_kiocb *prev = *link;
2919
2920 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2921 if (!sqe_copy) {
2922 ret = -EAGAIN;
2923 goto err_req;
2924 }
2925
2926 s->sqe = sqe_copy;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002927 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002928 list_add_tail(&req->list, &prev->link_list);
2929 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2930 req->flags |= REQ_F_LINK;
2931
Jens Axboe9e645e112019-05-10 16:07:28 -06002932 INIT_LIST_HEAD(&req->link_list);
2933 *link = req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002934 } else if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
2935 /* Only valid as a linked SQE */
2936 ret = -EINVAL;
2937 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002938 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002939 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002940 }
2941}
2942
Jens Axboe9a56a232019-01-09 09:06:50 -07002943/*
2944 * Batched submission is done, ensure local IO is flushed out.
2945 */
2946static void io_submit_state_end(struct io_submit_state *state)
2947{
2948 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002949 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002950 if (state->free_reqs)
2951 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2952 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002953}
2954
2955/*
2956 * Start submission side cache.
2957 */
2958static void io_submit_state_start(struct io_submit_state *state,
2959 struct io_ring_ctx *ctx, unsigned max_ios)
2960{
2961 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002962 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002963 state->file = NULL;
2964 state->ios_left = max_ios;
2965}
2966
Jens Axboe2b188cc2019-01-07 10:46:33 -07002967static void io_commit_sqring(struct io_ring_ctx *ctx)
2968{
Hristo Venev75b28af2019-08-26 17:23:46 +00002969 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002970
Hristo Venev75b28af2019-08-26 17:23:46 +00002971 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002972 /*
2973 * Ensure any loads from the SQEs are done at this point,
2974 * since once we write the new head, the application could
2975 * write new data to them.
2976 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002977 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002978 }
2979}
2980
2981/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002982 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2983 * that is mapped by userspace. This means that care needs to be taken to
2984 * ensure that reads are stable, as we cannot rely on userspace always
2985 * being a good citizen. If members of the sqe are validated and then later
2986 * used, it's important that those reads are done through READ_ONCE() to
2987 * prevent a re-load down the line.
2988 */
2989static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2990{
Hristo Venev75b28af2019-08-26 17:23:46 +00002991 struct io_rings *rings = ctx->rings;
2992 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002993 unsigned head;
2994
2995 /*
2996 * The cached sq head (or cq tail) serves two purposes:
2997 *
2998 * 1) allows us to batch the cost of updating the user visible
2999 * head updates.
3000 * 2) allows the kernel side to track the head on its own, even
3001 * though the application is the one updating it.
3002 */
3003 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003004 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00003005 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003006 return false;
3007
Hristo Venev75b28af2019-08-26 17:23:46 +00003008 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003009 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003010 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003011 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003012 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003013 ctx->cached_sq_head++;
3014 return true;
3015 }
3016
3017 /* drop invalid entries */
3018 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003019 ctx->cached_sq_dropped++;
3020 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003021 return false;
3022}
3023
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003024static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003025 struct file *ring_file, int ring_fd,
3026 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003027{
3028 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003029 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003030 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003031 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003032 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003033
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003034 if (!list_empty(&ctx->cq_overflow_list)) {
3035 io_cqring_overflow_flush(ctx, false);
3036 return -EBUSY;
3037 }
3038
Jens Axboe6c271ce2019-01-10 11:22:30 -07003039 if (nr > IO_PLUG_THRESHOLD) {
3040 io_submit_state_start(&state, ctx, nr);
3041 statep = &state;
3042 }
3043
3044 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003045 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003046 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003047
Pavel Begunkov196be952019-11-07 01:41:06 +03003048 req = io_get_req(ctx, statep);
3049 if (unlikely(!req)) {
3050 if (!submitted)
3051 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003052 break;
Pavel Begunkov196be952019-11-07 01:41:06 +03003053 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003054 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003055 __io_free_req(req);
3056 break;
3057 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003058
Pavel Begunkov50585b92019-11-07 01:41:07 +03003059 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003060 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3061 if (!mm_fault) {
3062 use_mm(ctx->sqo_mm);
3063 *mm = ctx->sqo_mm;
3064 }
3065 }
3066
Pavel Begunkov50585b92019-11-07 01:41:07 +03003067 sqe_flags = req->submit.sqe->flags;
3068
3069 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08003070 if (!shadow_req) {
3071 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08003072 if (unlikely(!shadow_req))
3073 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003074 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
3075 refcount_dec(&shadow_req->refs);
3076 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003077 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003078 }
3079
Jackie Liua1041c22019-09-18 17:25:52 +08003080out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03003081 req->submit.ring_file = ring_file;
3082 req->submit.ring_fd = ring_fd;
3083 req->submit.has_user = *mm != NULL;
3084 req->submit.in_async = async;
3085 req->submit.needs_fixed_file = async;
3086 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3087 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003088 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003089 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003090
3091 /*
3092 * If previous wasn't linked and we have a linked command,
3093 * that's the end of the chain. Submit the previous link.
3094 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003095 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Jackie Liua197f662019-11-08 08:09:12 -07003096 io_queue_link_head(link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003097 link = NULL;
3098 shadow_req = NULL;
3099 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003100 }
3101
Jens Axboe9e645e112019-05-10 16:07:28 -06003102 if (link)
Jackie Liua197f662019-11-08 08:09:12 -07003103 io_queue_link_head(link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003104 if (statep)
3105 io_submit_state_end(&state);
3106
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003107 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3108 io_commit_sqring(ctx);
3109
Jens Axboe6c271ce2019-01-10 11:22:30 -07003110 return submitted;
3111}
3112
3113static int io_sq_thread(void *data)
3114{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003115 struct io_ring_ctx *ctx = data;
3116 struct mm_struct *cur_mm = NULL;
3117 mm_segment_t old_fs;
3118 DEFINE_WAIT(wait);
3119 unsigned inflight;
3120 unsigned long timeout;
3121
Jens Axboe206aefd2019-11-07 18:27:42 -07003122 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003123
Jens Axboe6c271ce2019-01-10 11:22:30 -07003124 old_fs = get_fs();
3125 set_fs(USER_DS);
3126
3127 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003128 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003129 unsigned int to_submit;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003130 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003131
3132 if (inflight) {
3133 unsigned nr_events = 0;
3134
3135 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003136 /*
3137 * inflight is the count of the maximum possible
3138 * entries we submitted, but it can be smaller
3139 * if we dropped some of them. If we don't have
3140 * poll entries available, then we know that we
3141 * have nothing left to poll for. Reset the
3142 * inflight count to zero in that case.
3143 */
3144 mutex_lock(&ctx->uring_lock);
3145 if (!list_empty(&ctx->poll_list))
3146 __io_iopoll_check(ctx, &nr_events, 0);
3147 else
3148 inflight = 0;
3149 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003150 } else {
3151 /*
3152 * Normal IO, just pretend everything completed.
3153 * We don't have to poll completions for that.
3154 */
3155 nr_events = inflight;
3156 }
3157
3158 inflight -= nr_events;
3159 if (!inflight)
3160 timeout = jiffies + ctx->sq_thread_idle;
3161 }
3162
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003163 to_submit = io_sqring_entries(ctx);
3164 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003165 /*
3166 * We're polling. If we're within the defined idle
3167 * period, then let us spin without work before going
3168 * to sleep.
3169 */
3170 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003171 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003172 continue;
3173 }
3174
3175 /*
3176 * Drop cur_mm before scheduling, we can't hold it for
3177 * long periods (or over schedule()). Do this before
3178 * adding ourselves to the waitqueue, as the unuse/drop
3179 * may sleep.
3180 */
3181 if (cur_mm) {
3182 unuse_mm(cur_mm);
3183 mmput(cur_mm);
3184 cur_mm = NULL;
3185 }
3186
3187 prepare_to_wait(&ctx->sqo_wait, &wait,
3188 TASK_INTERRUPTIBLE);
3189
3190 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003191 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003192 /* make sure to read SQ tail after writing flags */
3193 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003194
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003195 to_submit = io_sqring_entries(ctx);
3196 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003197 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003198 finish_wait(&ctx->sqo_wait, &wait);
3199 break;
3200 }
3201 if (signal_pending(current))
3202 flush_signals(current);
3203 schedule();
3204 finish_wait(&ctx->sqo_wait, &wait);
3205
Hristo Venev75b28af2019-08-26 17:23:46 +00003206 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003207 continue;
3208 }
3209 finish_wait(&ctx->sqo_wait, &wait);
3210
Hristo Venev75b28af2019-08-26 17:23:46 +00003211 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003212 }
3213
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003214 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003215 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3216 if (ret > 0)
3217 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003218 }
3219
3220 set_fs(old_fs);
3221 if (cur_mm) {
3222 unuse_mm(cur_mm);
3223 mmput(cur_mm);
3224 }
Jens Axboe06058632019-04-13 09:26:03 -06003225
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003226 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003227
Jens Axboe6c271ce2019-01-10 11:22:30 -07003228 return 0;
3229}
3230
Jens Axboebda52162019-09-24 13:47:15 -06003231struct io_wait_queue {
3232 struct wait_queue_entry wq;
3233 struct io_ring_ctx *ctx;
3234 unsigned to_wait;
3235 unsigned nr_timeouts;
3236};
3237
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003238static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003239{
3240 struct io_ring_ctx *ctx = iowq->ctx;
3241
3242 /*
3243 * Wake up if we have enough events, or if a timeout occured since we
3244 * started waiting. For timeouts, we always want to return to userspace,
3245 * regardless of event count.
3246 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003247 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003248 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3249}
3250
3251static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3252 int wake_flags, void *key)
3253{
3254 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3255 wq);
3256
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003257 /* use noflush == true, as we can't safely rely on locking context */
3258 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003259 return -1;
3260
3261 return autoremove_wake_function(curr, mode, wake_flags, key);
3262}
3263
Jens Axboe2b188cc2019-01-07 10:46:33 -07003264/*
3265 * Wait until events become available, if we don't already have some. The
3266 * application must reap them itself, as they reside on the shared cq ring.
3267 */
3268static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3269 const sigset_t __user *sig, size_t sigsz)
3270{
Jens Axboebda52162019-09-24 13:47:15 -06003271 struct io_wait_queue iowq = {
3272 .wq = {
3273 .private = current,
3274 .func = io_wake_function,
3275 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3276 },
3277 .ctx = ctx,
3278 .to_wait = min_events,
3279 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003280 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003281 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003282
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003283 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003284 return 0;
3285
3286 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003287#ifdef CONFIG_COMPAT
3288 if (in_compat_syscall())
3289 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003290 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003291 else
3292#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003293 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003294
Jens Axboe2b188cc2019-01-07 10:46:33 -07003295 if (ret)
3296 return ret;
3297 }
3298
Jens Axboebda52162019-09-24 13:47:15 -06003299 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003300 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003301 do {
3302 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3303 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003304 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003305 break;
3306 schedule();
3307 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003308 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003309 break;
3310 }
3311 } while (1);
3312 finish_wait(&ctx->wait, &iowq.wq);
3313
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003314 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003315
Hristo Venev75b28af2019-08-26 17:23:46 +00003316 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003317}
3318
Jens Axboe6b063142019-01-10 22:13:58 -07003319static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3320{
3321#if defined(CONFIG_UNIX)
3322 if (ctx->ring_sock) {
3323 struct sock *sock = ctx->ring_sock->sk;
3324 struct sk_buff *skb;
3325
3326 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3327 kfree_skb(skb);
3328 }
3329#else
3330 int i;
3331
Jens Axboe65e19f52019-10-26 07:20:21 -06003332 for (i = 0; i < ctx->nr_user_files; i++) {
3333 struct file *file;
3334
3335 file = io_file_from_index(ctx, i);
3336 if (file)
3337 fput(file);
3338 }
Jens Axboe6b063142019-01-10 22:13:58 -07003339#endif
3340}
3341
3342static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3343{
Jens Axboe65e19f52019-10-26 07:20:21 -06003344 unsigned nr_tables, i;
3345
3346 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003347 return -ENXIO;
3348
3349 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003350 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3351 for (i = 0; i < nr_tables; i++)
3352 kfree(ctx->file_table[i].files);
3353 kfree(ctx->file_table);
3354 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003355 ctx->nr_user_files = 0;
3356 return 0;
3357}
3358
Jens Axboe6c271ce2019-01-10 11:22:30 -07003359static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3360{
3361 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003362 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003363 /*
3364 * The park is a bit of a work-around, without it we get
3365 * warning spews on shutdown with SQPOLL set and affinity
3366 * set to a single CPU.
3367 */
Jens Axboe06058632019-04-13 09:26:03 -06003368 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003369 kthread_stop(ctx->sqo_thread);
3370 ctx->sqo_thread = NULL;
3371 }
3372}
3373
Jens Axboe6b063142019-01-10 22:13:58 -07003374static void io_finish_async(struct io_ring_ctx *ctx)
3375{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003376 io_sq_thread_stop(ctx);
3377
Jens Axboe561fb042019-10-24 07:25:42 -06003378 if (ctx->io_wq) {
3379 io_wq_destroy(ctx->io_wq);
3380 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003381 }
3382}
3383
3384#if defined(CONFIG_UNIX)
3385static void io_destruct_skb(struct sk_buff *skb)
3386{
3387 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3388
Jens Axboe561fb042019-10-24 07:25:42 -06003389 if (ctx->io_wq)
3390 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003391
Jens Axboe6b063142019-01-10 22:13:58 -07003392 unix_destruct_scm(skb);
3393}
3394
3395/*
3396 * Ensure the UNIX gc is aware of our file set, so we are certain that
3397 * the io_uring can be safely unregistered on process exit, even if we have
3398 * loops in the file referencing.
3399 */
3400static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3401{
3402 struct sock *sk = ctx->ring_sock->sk;
3403 struct scm_fp_list *fpl;
3404 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003405 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003406
3407 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3408 unsigned long inflight = ctx->user->unix_inflight + nr;
3409
3410 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3411 return -EMFILE;
3412 }
3413
3414 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3415 if (!fpl)
3416 return -ENOMEM;
3417
3418 skb = alloc_skb(0, GFP_KERNEL);
3419 if (!skb) {
3420 kfree(fpl);
3421 return -ENOMEM;
3422 }
3423
3424 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003425
Jens Axboe08a45172019-10-03 08:11:03 -06003426 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003427 fpl->user = get_uid(ctx->user);
3428 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003429 struct file *file = io_file_from_index(ctx, i + offset);
3430
3431 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003432 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003433 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003434 unix_inflight(fpl->user, fpl->fp[nr_files]);
3435 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003436 }
3437
Jens Axboe08a45172019-10-03 08:11:03 -06003438 if (nr_files) {
3439 fpl->max = SCM_MAX_FD;
3440 fpl->count = nr_files;
3441 UNIXCB(skb).fp = fpl;
3442 skb->destructor = io_destruct_skb;
3443 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3444 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003445
Jens Axboe08a45172019-10-03 08:11:03 -06003446 for (i = 0; i < nr_files; i++)
3447 fput(fpl->fp[i]);
3448 } else {
3449 kfree_skb(skb);
3450 kfree(fpl);
3451 }
Jens Axboe6b063142019-01-10 22:13:58 -07003452
3453 return 0;
3454}
3455
3456/*
3457 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3458 * causes regular reference counting to break down. We rely on the UNIX
3459 * garbage collection to take care of this problem for us.
3460 */
3461static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3462{
3463 unsigned left, total;
3464 int ret = 0;
3465
3466 total = 0;
3467 left = ctx->nr_user_files;
3468 while (left) {
3469 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003470
3471 ret = __io_sqe_files_scm(ctx, this_files, total);
3472 if (ret)
3473 break;
3474 left -= this_files;
3475 total += this_files;
3476 }
3477
3478 if (!ret)
3479 return 0;
3480
3481 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003482 struct file *file = io_file_from_index(ctx, total);
3483
3484 if (file)
3485 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003486 total++;
3487 }
3488
3489 return ret;
3490}
3491#else
3492static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3493{
3494 return 0;
3495}
3496#endif
3497
Jens Axboe65e19f52019-10-26 07:20:21 -06003498static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3499 unsigned nr_files)
3500{
3501 int i;
3502
3503 for (i = 0; i < nr_tables; i++) {
3504 struct fixed_file_table *table = &ctx->file_table[i];
3505 unsigned this_files;
3506
3507 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3508 table->files = kcalloc(this_files, sizeof(struct file *),
3509 GFP_KERNEL);
3510 if (!table->files)
3511 break;
3512 nr_files -= this_files;
3513 }
3514
3515 if (i == nr_tables)
3516 return 0;
3517
3518 for (i = 0; i < nr_tables; i++) {
3519 struct fixed_file_table *table = &ctx->file_table[i];
3520 kfree(table->files);
3521 }
3522 return 1;
3523}
3524
Jens Axboe6b063142019-01-10 22:13:58 -07003525static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3526 unsigned nr_args)
3527{
3528 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003529 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003530 int fd, ret = 0;
3531 unsigned i;
3532
Jens Axboe65e19f52019-10-26 07:20:21 -06003533 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003534 return -EBUSY;
3535 if (!nr_args)
3536 return -EINVAL;
3537 if (nr_args > IORING_MAX_FIXED_FILES)
3538 return -EMFILE;
3539
Jens Axboe65e19f52019-10-26 07:20:21 -06003540 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3541 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3542 GFP_KERNEL);
3543 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003544 return -ENOMEM;
3545
Jens Axboe65e19f52019-10-26 07:20:21 -06003546 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3547 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003548 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003549 return -ENOMEM;
3550 }
3551
Jens Axboe08a45172019-10-03 08:11:03 -06003552 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003553 struct fixed_file_table *table;
3554 unsigned index;
3555
Jens Axboe6b063142019-01-10 22:13:58 -07003556 ret = -EFAULT;
3557 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3558 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003559 /* allow sparse sets */
3560 if (fd == -1) {
3561 ret = 0;
3562 continue;
3563 }
Jens Axboe6b063142019-01-10 22:13:58 -07003564
Jens Axboe65e19f52019-10-26 07:20:21 -06003565 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3566 index = i & IORING_FILE_TABLE_MASK;
3567 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003568
3569 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003570 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003571 break;
3572 /*
3573 * Don't allow io_uring instances to be registered. If UNIX
3574 * isn't enabled, then this causes a reference cycle and this
3575 * instance can never get freed. If UNIX is enabled we'll
3576 * handle it just fine, but there's still no point in allowing
3577 * a ring fd as it doesn't support regular read/write anyway.
3578 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003579 if (table->files[index]->f_op == &io_uring_fops) {
3580 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003581 break;
3582 }
Jens Axboe6b063142019-01-10 22:13:58 -07003583 ret = 0;
3584 }
3585
3586 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003587 for (i = 0; i < ctx->nr_user_files; i++) {
3588 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003589
Jens Axboe65e19f52019-10-26 07:20:21 -06003590 file = io_file_from_index(ctx, i);
3591 if (file)
3592 fput(file);
3593 }
3594 for (i = 0; i < nr_tables; i++)
3595 kfree(ctx->file_table[i].files);
3596
3597 kfree(ctx->file_table);
3598 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003599 ctx->nr_user_files = 0;
3600 return ret;
3601 }
3602
3603 ret = io_sqe_files_scm(ctx);
3604 if (ret)
3605 io_sqe_files_unregister(ctx);
3606
3607 return ret;
3608}
3609
Jens Axboec3a31e62019-10-03 13:59:56 -06003610static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3611{
3612#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003613 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003614 struct sock *sock = ctx->ring_sock->sk;
3615 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3616 struct sk_buff *skb;
3617 int i;
3618
3619 __skb_queue_head_init(&list);
3620
3621 /*
3622 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3623 * remove this entry and rearrange the file array.
3624 */
3625 skb = skb_dequeue(head);
3626 while (skb) {
3627 struct scm_fp_list *fp;
3628
3629 fp = UNIXCB(skb).fp;
3630 for (i = 0; i < fp->count; i++) {
3631 int left;
3632
3633 if (fp->fp[i] != file)
3634 continue;
3635
3636 unix_notinflight(fp->user, fp->fp[i]);
3637 left = fp->count - 1 - i;
3638 if (left) {
3639 memmove(&fp->fp[i], &fp->fp[i + 1],
3640 left * sizeof(struct file *));
3641 }
3642 fp->count--;
3643 if (!fp->count) {
3644 kfree_skb(skb);
3645 skb = NULL;
3646 } else {
3647 __skb_queue_tail(&list, skb);
3648 }
3649 fput(file);
3650 file = NULL;
3651 break;
3652 }
3653
3654 if (!file)
3655 break;
3656
3657 __skb_queue_tail(&list, skb);
3658
3659 skb = skb_dequeue(head);
3660 }
3661
3662 if (skb_peek(&list)) {
3663 spin_lock_irq(&head->lock);
3664 while ((skb = __skb_dequeue(&list)) != NULL)
3665 __skb_queue_tail(head, skb);
3666 spin_unlock_irq(&head->lock);
3667 }
3668#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003669 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003670#endif
3671}
3672
3673static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3674 int index)
3675{
3676#if defined(CONFIG_UNIX)
3677 struct sock *sock = ctx->ring_sock->sk;
3678 struct sk_buff_head *head = &sock->sk_receive_queue;
3679 struct sk_buff *skb;
3680
3681 /*
3682 * See if we can merge this file into an existing skb SCM_RIGHTS
3683 * file set. If there's no room, fall back to allocating a new skb
3684 * and filling it in.
3685 */
3686 spin_lock_irq(&head->lock);
3687 skb = skb_peek(head);
3688 if (skb) {
3689 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3690
3691 if (fpl->count < SCM_MAX_FD) {
3692 __skb_unlink(skb, head);
3693 spin_unlock_irq(&head->lock);
3694 fpl->fp[fpl->count] = get_file(file);
3695 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3696 fpl->count++;
3697 spin_lock_irq(&head->lock);
3698 __skb_queue_head(head, skb);
3699 } else {
3700 skb = NULL;
3701 }
3702 }
3703 spin_unlock_irq(&head->lock);
3704
3705 if (skb) {
3706 fput(file);
3707 return 0;
3708 }
3709
3710 return __io_sqe_files_scm(ctx, 1, index);
3711#else
3712 return 0;
3713#endif
3714}
3715
3716static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3717 unsigned nr_args)
3718{
3719 struct io_uring_files_update up;
3720 __s32 __user *fds;
3721 int fd, i, err;
3722 __u32 done;
3723
Jens Axboe65e19f52019-10-26 07:20:21 -06003724 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003725 return -ENXIO;
3726 if (!nr_args)
3727 return -EINVAL;
3728 if (copy_from_user(&up, arg, sizeof(up)))
3729 return -EFAULT;
3730 if (check_add_overflow(up.offset, nr_args, &done))
3731 return -EOVERFLOW;
3732 if (done > ctx->nr_user_files)
3733 return -EINVAL;
3734
3735 done = 0;
3736 fds = (__s32 __user *) up.fds;
3737 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003738 struct fixed_file_table *table;
3739 unsigned index;
3740
Jens Axboec3a31e62019-10-03 13:59:56 -06003741 err = 0;
3742 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3743 err = -EFAULT;
3744 break;
3745 }
3746 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003747 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3748 index = i & IORING_FILE_TABLE_MASK;
3749 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003750 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003751 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003752 }
3753 if (fd != -1) {
3754 struct file *file;
3755
3756 file = fget(fd);
3757 if (!file) {
3758 err = -EBADF;
3759 break;
3760 }
3761 /*
3762 * Don't allow io_uring instances to be registered. If
3763 * UNIX isn't enabled, then this causes a reference
3764 * cycle and this instance can never get freed. If UNIX
3765 * is enabled we'll handle it just fine, but there's
3766 * still no point in allowing a ring fd as it doesn't
3767 * support regular read/write anyway.
3768 */
3769 if (file->f_op == &io_uring_fops) {
3770 fput(file);
3771 err = -EBADF;
3772 break;
3773 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003774 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003775 err = io_sqe_file_register(ctx, file, i);
3776 if (err)
3777 break;
3778 }
3779 nr_args--;
3780 done++;
3781 up.offset++;
3782 }
3783
3784 return done ? done : err;
3785}
3786
Jens Axboe6c271ce2019-01-10 11:22:30 -07003787static int io_sq_offload_start(struct io_ring_ctx *ctx,
3788 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003789{
Jens Axboe561fb042019-10-24 07:25:42 -06003790 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003791 int ret;
3792
Jens Axboe6c271ce2019-01-10 11:22:30 -07003793 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003794 mmgrab(current->mm);
3795 ctx->sqo_mm = current->mm;
3796
Jens Axboe6c271ce2019-01-10 11:22:30 -07003797 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003798 ret = -EPERM;
3799 if (!capable(CAP_SYS_ADMIN))
3800 goto err;
3801
Jens Axboe917257d2019-04-13 09:28:55 -06003802 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3803 if (!ctx->sq_thread_idle)
3804 ctx->sq_thread_idle = HZ;
3805
Jens Axboe6c271ce2019-01-10 11:22:30 -07003806 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003807 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003808
Jens Axboe917257d2019-04-13 09:28:55 -06003809 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003810 if (cpu >= nr_cpu_ids)
3811 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003812 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003813 goto err;
3814
Jens Axboe6c271ce2019-01-10 11:22:30 -07003815 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3816 ctx, cpu,
3817 "io_uring-sq");
3818 } else {
3819 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3820 "io_uring-sq");
3821 }
3822 if (IS_ERR(ctx->sqo_thread)) {
3823 ret = PTR_ERR(ctx->sqo_thread);
3824 ctx->sqo_thread = NULL;
3825 goto err;
3826 }
3827 wake_up_process(ctx->sqo_thread);
3828 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3829 /* Can't have SQ_AFF without SQPOLL */
3830 ret = -EINVAL;
3831 goto err;
3832 }
3833
Jens Axboe561fb042019-10-24 07:25:42 -06003834 /* Do QD, or 4 * CPUS, whatever is smallest */
3835 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe5f8fd2d32019-11-07 10:57:36 -07003836 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user);
Jens Axboe975c99a52019-10-30 08:42:56 -06003837 if (IS_ERR(ctx->io_wq)) {
3838 ret = PTR_ERR(ctx->io_wq);
3839 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003840 goto err;
3841 }
3842
3843 return 0;
3844err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003845 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003846 mmdrop(ctx->sqo_mm);
3847 ctx->sqo_mm = NULL;
3848 return ret;
3849}
3850
3851static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3852{
3853 atomic_long_sub(nr_pages, &user->locked_vm);
3854}
3855
3856static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3857{
3858 unsigned long page_limit, cur_pages, new_pages;
3859
3860 /* Don't allow more pages than we can safely lock */
3861 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3862
3863 do {
3864 cur_pages = atomic_long_read(&user->locked_vm);
3865 new_pages = cur_pages + nr_pages;
3866 if (new_pages > page_limit)
3867 return -ENOMEM;
3868 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3869 new_pages) != cur_pages);
3870
3871 return 0;
3872}
3873
3874static void io_mem_free(void *ptr)
3875{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003876 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003877
Mark Rutland52e04ef2019-04-30 17:30:21 +01003878 if (!ptr)
3879 return;
3880
3881 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003882 if (put_page_testzero(page))
3883 free_compound_page(page);
3884}
3885
3886static void *io_mem_alloc(size_t size)
3887{
3888 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3889 __GFP_NORETRY;
3890
3891 return (void *) __get_free_pages(gfp_flags, get_order(size));
3892}
3893
Hristo Venev75b28af2019-08-26 17:23:46 +00003894static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3895 size_t *sq_offset)
3896{
3897 struct io_rings *rings;
3898 size_t off, sq_array_size;
3899
3900 off = struct_size(rings, cqes, cq_entries);
3901 if (off == SIZE_MAX)
3902 return SIZE_MAX;
3903
3904#ifdef CONFIG_SMP
3905 off = ALIGN(off, SMP_CACHE_BYTES);
3906 if (off == 0)
3907 return SIZE_MAX;
3908#endif
3909
3910 sq_array_size = array_size(sizeof(u32), sq_entries);
3911 if (sq_array_size == SIZE_MAX)
3912 return SIZE_MAX;
3913
3914 if (check_add_overflow(off, sq_array_size, &off))
3915 return SIZE_MAX;
3916
3917 if (sq_offset)
3918 *sq_offset = off;
3919
3920 return off;
3921}
3922
Jens Axboe2b188cc2019-01-07 10:46:33 -07003923static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3924{
Hristo Venev75b28af2019-08-26 17:23:46 +00003925 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003926
Hristo Venev75b28af2019-08-26 17:23:46 +00003927 pages = (size_t)1 << get_order(
3928 rings_size(sq_entries, cq_entries, NULL));
3929 pages += (size_t)1 << get_order(
3930 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003931
Hristo Venev75b28af2019-08-26 17:23:46 +00003932 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003933}
3934
Jens Axboeedafcce2019-01-09 09:16:05 -07003935static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3936{
3937 int i, j;
3938
3939 if (!ctx->user_bufs)
3940 return -ENXIO;
3941
3942 for (i = 0; i < ctx->nr_user_bufs; i++) {
3943 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3944
3945 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003946 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003947
3948 if (ctx->account_mem)
3949 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003950 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003951 imu->nr_bvecs = 0;
3952 }
3953
3954 kfree(ctx->user_bufs);
3955 ctx->user_bufs = NULL;
3956 ctx->nr_user_bufs = 0;
3957 return 0;
3958}
3959
3960static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3961 void __user *arg, unsigned index)
3962{
3963 struct iovec __user *src;
3964
3965#ifdef CONFIG_COMPAT
3966 if (ctx->compat) {
3967 struct compat_iovec __user *ciovs;
3968 struct compat_iovec ciov;
3969
3970 ciovs = (struct compat_iovec __user *) arg;
3971 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3972 return -EFAULT;
3973
3974 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3975 dst->iov_len = ciov.iov_len;
3976 return 0;
3977 }
3978#endif
3979 src = (struct iovec __user *) arg;
3980 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3981 return -EFAULT;
3982 return 0;
3983}
3984
3985static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3986 unsigned nr_args)
3987{
3988 struct vm_area_struct **vmas = NULL;
3989 struct page **pages = NULL;
3990 int i, j, got_pages = 0;
3991 int ret = -EINVAL;
3992
3993 if (ctx->user_bufs)
3994 return -EBUSY;
3995 if (!nr_args || nr_args > UIO_MAXIOV)
3996 return -EINVAL;
3997
3998 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3999 GFP_KERNEL);
4000 if (!ctx->user_bufs)
4001 return -ENOMEM;
4002
4003 for (i = 0; i < nr_args; i++) {
4004 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4005 unsigned long off, start, end, ubuf;
4006 int pret, nr_pages;
4007 struct iovec iov;
4008 size_t size;
4009
4010 ret = io_copy_iov(ctx, &iov, arg, i);
4011 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004012 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004013
4014 /*
4015 * Don't impose further limits on the size and buffer
4016 * constraints here, we'll -EINVAL later when IO is
4017 * submitted if they are wrong.
4018 */
4019 ret = -EFAULT;
4020 if (!iov.iov_base || !iov.iov_len)
4021 goto err;
4022
4023 /* arbitrary limit, but we need something */
4024 if (iov.iov_len > SZ_1G)
4025 goto err;
4026
4027 ubuf = (unsigned long) iov.iov_base;
4028 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4029 start = ubuf >> PAGE_SHIFT;
4030 nr_pages = end - start;
4031
4032 if (ctx->account_mem) {
4033 ret = io_account_mem(ctx->user, nr_pages);
4034 if (ret)
4035 goto err;
4036 }
4037
4038 ret = 0;
4039 if (!pages || nr_pages > got_pages) {
4040 kfree(vmas);
4041 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004042 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004043 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004044 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004045 sizeof(struct vm_area_struct *),
4046 GFP_KERNEL);
4047 if (!pages || !vmas) {
4048 ret = -ENOMEM;
4049 if (ctx->account_mem)
4050 io_unaccount_mem(ctx->user, nr_pages);
4051 goto err;
4052 }
4053 got_pages = nr_pages;
4054 }
4055
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004056 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004057 GFP_KERNEL);
4058 ret = -ENOMEM;
4059 if (!imu->bvec) {
4060 if (ctx->account_mem)
4061 io_unaccount_mem(ctx->user, nr_pages);
4062 goto err;
4063 }
4064
4065 ret = 0;
4066 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004067 pret = get_user_pages(ubuf, nr_pages,
4068 FOLL_WRITE | FOLL_LONGTERM,
4069 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004070 if (pret == nr_pages) {
4071 /* don't support file backed memory */
4072 for (j = 0; j < nr_pages; j++) {
4073 struct vm_area_struct *vma = vmas[j];
4074
4075 if (vma->vm_file &&
4076 !is_file_hugepages(vma->vm_file)) {
4077 ret = -EOPNOTSUPP;
4078 break;
4079 }
4080 }
4081 } else {
4082 ret = pret < 0 ? pret : -EFAULT;
4083 }
4084 up_read(&current->mm->mmap_sem);
4085 if (ret) {
4086 /*
4087 * if we did partial map, or found file backed vmas,
4088 * release any pages we did get
4089 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004090 if (pret > 0)
4091 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004092 if (ctx->account_mem)
4093 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004094 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004095 goto err;
4096 }
4097
4098 off = ubuf & ~PAGE_MASK;
4099 size = iov.iov_len;
4100 for (j = 0; j < nr_pages; j++) {
4101 size_t vec_len;
4102
4103 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4104 imu->bvec[j].bv_page = pages[j];
4105 imu->bvec[j].bv_len = vec_len;
4106 imu->bvec[j].bv_offset = off;
4107 off = 0;
4108 size -= vec_len;
4109 }
4110 /* store original address for later verification */
4111 imu->ubuf = ubuf;
4112 imu->len = iov.iov_len;
4113 imu->nr_bvecs = nr_pages;
4114
4115 ctx->nr_user_bufs++;
4116 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004117 kvfree(pages);
4118 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004119 return 0;
4120err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004121 kvfree(pages);
4122 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004123 io_sqe_buffer_unregister(ctx);
4124 return ret;
4125}
4126
Jens Axboe9b402842019-04-11 11:45:41 -06004127static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4128{
4129 __s32 __user *fds = arg;
4130 int fd;
4131
4132 if (ctx->cq_ev_fd)
4133 return -EBUSY;
4134
4135 if (copy_from_user(&fd, fds, sizeof(*fds)))
4136 return -EFAULT;
4137
4138 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4139 if (IS_ERR(ctx->cq_ev_fd)) {
4140 int ret = PTR_ERR(ctx->cq_ev_fd);
4141 ctx->cq_ev_fd = NULL;
4142 return ret;
4143 }
4144
4145 return 0;
4146}
4147
4148static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4149{
4150 if (ctx->cq_ev_fd) {
4151 eventfd_ctx_put(ctx->cq_ev_fd);
4152 ctx->cq_ev_fd = NULL;
4153 return 0;
4154 }
4155
4156 return -ENXIO;
4157}
4158
Jens Axboe2b188cc2019-01-07 10:46:33 -07004159static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4160{
Jens Axboe6b063142019-01-10 22:13:58 -07004161 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004162 if (ctx->sqo_mm)
4163 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004164
4165 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004166 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004167 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004168 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004169
Jens Axboe2b188cc2019-01-07 10:46:33 -07004170#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004171 if (ctx->ring_sock) {
4172 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004173 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004174 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004175#endif
4176
Hristo Venev75b28af2019-08-26 17:23:46 +00004177 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004178 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004179
4180 percpu_ref_exit(&ctx->refs);
4181 if (ctx->account_mem)
4182 io_unaccount_mem(ctx->user,
4183 ring_pages(ctx->sq_entries, ctx->cq_entries));
4184 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004185 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004186 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004187 kfree(ctx);
4188}
4189
4190static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4191{
4192 struct io_ring_ctx *ctx = file->private_data;
4193 __poll_t mask = 0;
4194
4195 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004196 /*
4197 * synchronizes with barrier from wq_has_sleeper call in
4198 * io_commit_cqring
4199 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004200 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004201 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4202 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004203 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004204 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004205 mask |= EPOLLIN | EPOLLRDNORM;
4206
4207 return mask;
4208}
4209
4210static int io_uring_fasync(int fd, struct file *file, int on)
4211{
4212 struct io_ring_ctx *ctx = file->private_data;
4213
4214 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4215}
4216
4217static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4218{
4219 mutex_lock(&ctx->uring_lock);
4220 percpu_ref_kill(&ctx->refs);
4221 mutex_unlock(&ctx->uring_lock);
4222
Jens Axboe5262f562019-09-17 12:26:57 -06004223 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004224 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004225
4226 if (ctx->io_wq)
4227 io_wq_cancel_all(ctx->io_wq);
4228
Jens Axboedef596e2019-01-09 08:59:42 -07004229 io_iopoll_reap_events(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004230 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004231 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004232 io_ring_ctx_free(ctx);
4233}
4234
4235static int io_uring_release(struct inode *inode, struct file *file)
4236{
4237 struct io_ring_ctx *ctx = file->private_data;
4238
4239 file->private_data = NULL;
4240 io_ring_ctx_wait_and_kill(ctx);
4241 return 0;
4242}
4243
Jens Axboefcb323c2019-10-24 12:39:47 -06004244static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4245 struct files_struct *files)
4246{
4247 struct io_kiocb *req;
4248 DEFINE_WAIT(wait);
4249
4250 while (!list_empty_careful(&ctx->inflight_list)) {
4251 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
4252
4253 spin_lock_irq(&ctx->inflight_lock);
4254 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
4255 if (req->work.files == files) {
4256 ret = io_wq_cancel_work(ctx->io_wq, &req->work);
4257 break;
4258 }
4259 }
4260 if (ret == IO_WQ_CANCEL_RUNNING)
4261 prepare_to_wait(&ctx->inflight_wait, &wait,
4262 TASK_UNINTERRUPTIBLE);
4263
4264 spin_unlock_irq(&ctx->inflight_lock);
4265
4266 /*
4267 * We need to keep going until we get NOTFOUND. We only cancel
4268 * one work at the time.
4269 *
4270 * If we get CANCEL_RUNNING, then wait for a work to complete
4271 * before continuing.
4272 */
4273 if (ret == IO_WQ_CANCEL_OK)
4274 continue;
4275 else if (ret != IO_WQ_CANCEL_RUNNING)
4276 break;
4277 schedule();
4278 }
4279}
4280
4281static int io_uring_flush(struct file *file, void *data)
4282{
4283 struct io_ring_ctx *ctx = file->private_data;
4284
4285 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004286 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4287 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004288 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004289 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004290 return 0;
4291}
4292
Jens Axboe2b188cc2019-01-07 10:46:33 -07004293static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4294{
4295 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4296 unsigned long sz = vma->vm_end - vma->vm_start;
4297 struct io_ring_ctx *ctx = file->private_data;
4298 unsigned long pfn;
4299 struct page *page;
4300 void *ptr;
4301
4302 switch (offset) {
4303 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004304 case IORING_OFF_CQ_RING:
4305 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004306 break;
4307 case IORING_OFF_SQES:
4308 ptr = ctx->sq_sqes;
4309 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004310 default:
4311 return -EINVAL;
4312 }
4313
4314 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004315 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004316 return -EINVAL;
4317
4318 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4319 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4320}
4321
4322SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4323 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4324 size_t, sigsz)
4325{
4326 struct io_ring_ctx *ctx;
4327 long ret = -EBADF;
4328 int submitted = 0;
4329 struct fd f;
4330
Jens Axboe6c271ce2019-01-10 11:22:30 -07004331 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004332 return -EINVAL;
4333
4334 f = fdget(fd);
4335 if (!f.file)
4336 return -EBADF;
4337
4338 ret = -EOPNOTSUPP;
4339 if (f.file->f_op != &io_uring_fops)
4340 goto out_fput;
4341
4342 ret = -ENXIO;
4343 ctx = f.file->private_data;
4344 if (!percpu_ref_tryget(&ctx->refs))
4345 goto out_fput;
4346
Jens Axboe6c271ce2019-01-10 11:22:30 -07004347 /*
4348 * For SQ polling, the thread will do all submissions and completions.
4349 * Just return the requested submit count, and wake the thread if
4350 * we were asked to.
4351 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004352 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004353 if (ctx->flags & IORING_SETUP_SQPOLL) {
4354 if (flags & IORING_ENTER_SQ_WAKEUP)
4355 wake_up(&ctx->sqo_wait);
4356 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004357 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004358 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004359
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004360 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004361 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004362 /* already have mm, so io_submit_sqes() won't try to grab it */
4363 cur_mm = ctx->sqo_mm;
4364 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4365 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004366 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004367 }
4368 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004369 unsigned nr_events = 0;
4370
Jens Axboe2b188cc2019-01-07 10:46:33 -07004371 min_complete = min(min_complete, ctx->cq_entries);
4372
Jens Axboedef596e2019-01-09 08:59:42 -07004373 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004374 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004375 } else {
4376 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4377 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004378 }
4379
Pavel Begunkov6805b322019-10-08 02:18:42 +03004380 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004381out_fput:
4382 fdput(f);
4383 return submitted ? submitted : ret;
4384}
4385
4386static const struct file_operations io_uring_fops = {
4387 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004388 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004389 .mmap = io_uring_mmap,
4390 .poll = io_uring_poll,
4391 .fasync = io_uring_fasync,
4392};
4393
4394static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4395 struct io_uring_params *p)
4396{
Hristo Venev75b28af2019-08-26 17:23:46 +00004397 struct io_rings *rings;
4398 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004399
Hristo Venev75b28af2019-08-26 17:23:46 +00004400 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4401 if (size == SIZE_MAX)
4402 return -EOVERFLOW;
4403
4404 rings = io_mem_alloc(size);
4405 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004406 return -ENOMEM;
4407
Hristo Venev75b28af2019-08-26 17:23:46 +00004408 ctx->rings = rings;
4409 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4410 rings->sq_ring_mask = p->sq_entries - 1;
4411 rings->cq_ring_mask = p->cq_entries - 1;
4412 rings->sq_ring_entries = p->sq_entries;
4413 rings->cq_ring_entries = p->cq_entries;
4414 ctx->sq_mask = rings->sq_ring_mask;
4415 ctx->cq_mask = rings->cq_ring_mask;
4416 ctx->sq_entries = rings->sq_ring_entries;
4417 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004418
4419 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4420 if (size == SIZE_MAX)
4421 return -EOVERFLOW;
4422
4423 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004424 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004425 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004426
Jens Axboe2b188cc2019-01-07 10:46:33 -07004427 return 0;
4428}
4429
4430/*
4431 * Allocate an anonymous fd, this is what constitutes the application
4432 * visible backing of an io_uring instance. The application mmaps this
4433 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4434 * we have to tie this fd to a socket for file garbage collection purposes.
4435 */
4436static int io_uring_get_fd(struct io_ring_ctx *ctx)
4437{
4438 struct file *file;
4439 int ret;
4440
4441#if defined(CONFIG_UNIX)
4442 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4443 &ctx->ring_sock);
4444 if (ret)
4445 return ret;
4446#endif
4447
4448 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4449 if (ret < 0)
4450 goto err;
4451
4452 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4453 O_RDWR | O_CLOEXEC);
4454 if (IS_ERR(file)) {
4455 put_unused_fd(ret);
4456 ret = PTR_ERR(file);
4457 goto err;
4458 }
4459
4460#if defined(CONFIG_UNIX)
4461 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004462 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004463#endif
4464 fd_install(ret, file);
4465 return ret;
4466err:
4467#if defined(CONFIG_UNIX)
4468 sock_release(ctx->ring_sock);
4469 ctx->ring_sock = NULL;
4470#endif
4471 return ret;
4472}
4473
4474static int io_uring_create(unsigned entries, struct io_uring_params *p)
4475{
4476 struct user_struct *user = NULL;
4477 struct io_ring_ctx *ctx;
4478 bool account_mem;
4479 int ret;
4480
4481 if (!entries || entries > IORING_MAX_ENTRIES)
4482 return -EINVAL;
4483
4484 /*
4485 * Use twice as many entries for the CQ ring. It's possible for the
4486 * application to drive a higher depth than the size of the SQ ring,
4487 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004488 * some flexibility in overcommitting a bit. If the application has
4489 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4490 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004491 */
4492 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004493 if (p->flags & IORING_SETUP_CQSIZE) {
4494 /*
4495 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4496 * to a power-of-two, if it isn't already. We do NOT impose
4497 * any cq vs sq ring sizing.
4498 */
4499 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4500 return -EINVAL;
4501 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4502 } else {
4503 p->cq_entries = 2 * p->sq_entries;
4504 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004505
4506 user = get_uid(current_user());
4507 account_mem = !capable(CAP_IPC_LOCK);
4508
4509 if (account_mem) {
4510 ret = io_account_mem(user,
4511 ring_pages(p->sq_entries, p->cq_entries));
4512 if (ret) {
4513 free_uid(user);
4514 return ret;
4515 }
4516 }
4517
4518 ctx = io_ring_ctx_alloc(p);
4519 if (!ctx) {
4520 if (account_mem)
4521 io_unaccount_mem(user, ring_pages(p->sq_entries,
4522 p->cq_entries));
4523 free_uid(user);
4524 return -ENOMEM;
4525 }
4526 ctx->compat = in_compat_syscall();
4527 ctx->account_mem = account_mem;
4528 ctx->user = user;
4529
4530 ret = io_allocate_scq_urings(ctx, p);
4531 if (ret)
4532 goto err;
4533
Jens Axboe6c271ce2019-01-10 11:22:30 -07004534 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004535 if (ret)
4536 goto err;
4537
Jens Axboe2b188cc2019-01-07 10:46:33 -07004538 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004539 p->sq_off.head = offsetof(struct io_rings, sq.head);
4540 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4541 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4542 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4543 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4544 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4545 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004546
4547 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004548 p->cq_off.head = offsetof(struct io_rings, cq.head);
4549 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4550 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4551 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4552 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4553 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004554
Jens Axboe044c1ab2019-10-28 09:15:33 -06004555 /*
4556 * Install ring fd as the very last thing, so we don't risk someone
4557 * having closed it before we finish setup
4558 */
4559 ret = io_uring_get_fd(ctx);
4560 if (ret < 0)
4561 goto err;
4562
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004563 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004564 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004565 return ret;
4566err:
4567 io_ring_ctx_wait_and_kill(ctx);
4568 return ret;
4569}
4570
4571/*
4572 * Sets up an aio uring context, and returns the fd. Applications asks for a
4573 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4574 * params structure passed in.
4575 */
4576static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4577{
4578 struct io_uring_params p;
4579 long ret;
4580 int i;
4581
4582 if (copy_from_user(&p, params, sizeof(p)))
4583 return -EFAULT;
4584 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4585 if (p.resv[i])
4586 return -EINVAL;
4587 }
4588
Jens Axboe6c271ce2019-01-10 11:22:30 -07004589 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004590 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004591 return -EINVAL;
4592
4593 ret = io_uring_create(entries, &p);
4594 if (ret < 0)
4595 return ret;
4596
4597 if (copy_to_user(params, &p, sizeof(p)))
4598 return -EFAULT;
4599
4600 return ret;
4601}
4602
4603SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4604 struct io_uring_params __user *, params)
4605{
4606 return io_uring_setup(entries, params);
4607}
4608
Jens Axboeedafcce2019-01-09 09:16:05 -07004609static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4610 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004611 __releases(ctx->uring_lock)
4612 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004613{
4614 int ret;
4615
Jens Axboe35fa71a2019-04-22 10:23:23 -06004616 /*
4617 * We're inside the ring mutex, if the ref is already dying, then
4618 * someone else killed the ctx or is already going through
4619 * io_uring_register().
4620 */
4621 if (percpu_ref_is_dying(&ctx->refs))
4622 return -ENXIO;
4623
Jens Axboeedafcce2019-01-09 09:16:05 -07004624 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004625
4626 /*
4627 * Drop uring mutex before waiting for references to exit. If another
4628 * thread is currently inside io_uring_enter() it might need to grab
4629 * the uring_lock to make progress. If we hold it here across the drain
4630 * wait, then we can deadlock. It's safe to drop the mutex here, since
4631 * no new references will come in after we've killed the percpu ref.
4632 */
4633 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004634 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004635 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004636
4637 switch (opcode) {
4638 case IORING_REGISTER_BUFFERS:
4639 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4640 break;
4641 case IORING_UNREGISTER_BUFFERS:
4642 ret = -EINVAL;
4643 if (arg || nr_args)
4644 break;
4645 ret = io_sqe_buffer_unregister(ctx);
4646 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004647 case IORING_REGISTER_FILES:
4648 ret = io_sqe_files_register(ctx, arg, nr_args);
4649 break;
4650 case IORING_UNREGISTER_FILES:
4651 ret = -EINVAL;
4652 if (arg || nr_args)
4653 break;
4654 ret = io_sqe_files_unregister(ctx);
4655 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004656 case IORING_REGISTER_FILES_UPDATE:
4657 ret = io_sqe_files_update(ctx, arg, nr_args);
4658 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004659 case IORING_REGISTER_EVENTFD:
4660 ret = -EINVAL;
4661 if (nr_args != 1)
4662 break;
4663 ret = io_eventfd_register(ctx, arg);
4664 break;
4665 case IORING_UNREGISTER_EVENTFD:
4666 ret = -EINVAL;
4667 if (arg || nr_args)
4668 break;
4669 ret = io_eventfd_unregister(ctx);
4670 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004671 default:
4672 ret = -EINVAL;
4673 break;
4674 }
4675
4676 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004677 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004678 percpu_ref_reinit(&ctx->refs);
4679 return ret;
4680}
4681
4682SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4683 void __user *, arg, unsigned int, nr_args)
4684{
4685 struct io_ring_ctx *ctx;
4686 long ret = -EBADF;
4687 struct fd f;
4688
4689 f = fdget(fd);
4690 if (!f.file)
4691 return -EBADF;
4692
4693 ret = -EOPNOTSUPP;
4694 if (f.file->f_op != &io_uring_fops)
4695 goto out_fput;
4696
4697 ctx = f.file->private_data;
4698
4699 mutex_lock(&ctx->uring_lock);
4700 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4701 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004702 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4703 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004704out_fput:
4705 fdput(f);
4706 return ret;
4707}
4708
Jens Axboe2b188cc2019-01-07 10:46:33 -07004709static int __init io_uring_init(void)
4710{
4711 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4712 return 0;
4713};
4714__initcall(io_uring_init);