blob: 281d0b7597cf31499b796820e622662ddf40addd [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;
188
Hristo Venev75b28af2019-08-26 17:23:46 +0000189 /*
190 * Ring buffer of indices into array of io_uring_sqe, which is
191 * mmapped by the application using the IORING_OFF_SQES offset.
192 *
193 * This indirection could e.g. be used to assign fixed
194 * io_uring_sqe entries to operations and only submit them to
195 * the queue when needed.
196 *
197 * The kernel modifies neither the indices array nor the entries
198 * array.
199 */
200 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700201 unsigned cached_sq_head;
202 unsigned sq_entries;
203 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700204 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600205 unsigned cached_sq_dropped;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700206 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600207
208 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600209 struct list_head timeout_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600210
211 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700212 } ____cacheline_aligned_in_smp;
213
214 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600215 struct io_wq *io_wq;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700216 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700217 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700218 wait_queue_head_t sqo_wait;
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800219 struct completion sqo_thread_started;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700220
221 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222 unsigned cached_cq_tail;
Jens Axboe498ccd92019-10-25 10:04:25 -0600223 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700224 unsigned cq_entries;
225 unsigned cq_mask;
226 struct wait_queue_head cq_wait;
227 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600228 struct eventfd_ctx *cq_ev_fd;
Jens Axboe5262f562019-09-17 12:26:57 -0600229 atomic_t cq_timeouts;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700230 } ____cacheline_aligned_in_smp;
231
Hristo Venev75b28af2019-08-26 17:23:46 +0000232 struct io_rings *rings;
233
Jens Axboe6b063142019-01-10 22:13:58 -0700234 /*
235 * If used, fixed file set. Writers must ensure that ->refs is dead,
236 * readers must ensure that ->refs is alive as long as the file* is
237 * used. Only updated through io_uring_register(2).
238 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600239 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700240 unsigned nr_user_files;
241
Jens Axboeedafcce2019-01-09 09:16:05 -0700242 /* if used, fixed mapped user buffers */
243 unsigned nr_user_bufs;
244 struct io_mapped_ubuf *user_bufs;
245
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246 struct user_struct *user;
247
248 struct completion ctx_done;
249
250 struct {
251 struct mutex uring_lock;
252 wait_queue_head_t wait;
253 } ____cacheline_aligned_in_smp;
254
255 struct {
256 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700257 bool poll_multi_file;
258 /*
259 * ->poll_list is protected by the ctx->uring_lock for
260 * io_uring instances that don't use IORING_SETUP_SQPOLL.
261 * For SQPOLL, only the single threaded io_sq_thread() will
262 * manipulate the list, hence no extra locking is needed there.
263 */
264 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700265 struct list_head cancel_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600266
267 spinlock_t inflight_lock;
268 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700269 } ____cacheline_aligned_in_smp;
270
271#if defined(CONFIG_UNIX)
272 struct socket *ring_sock;
273#endif
274};
275
276struct sqe_submit {
277 const struct io_uring_sqe *sqe;
Jens Axboefcb323c2019-10-24 12:39:47 -0600278 struct file *ring_file;
279 int ring_fd;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800280 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700281 bool has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +0800282 bool in_async;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700283 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700284};
285
Jens Axboe09bb8392019-03-13 12:39:28 -0600286/*
287 * First field must be the file pointer in all the
288 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
289 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700290struct io_poll_iocb {
291 struct file *file;
292 struct wait_queue_head *head;
293 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600294 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700295 bool canceled;
296 struct wait_queue_entry wait;
297};
298
Jens Axboe5262f562019-09-17 12:26:57 -0600299struct io_timeout {
300 struct file *file;
301 struct hrtimer timer;
302};
303
Jens Axboe09bb8392019-03-13 12:39:28 -0600304/*
305 * NOTE! Each of the iocb union members has the file pointer
306 * as the first entry in their struct definition. So you can
307 * access the file pointer through any of the sub-structs,
308 * or directly as just 'ki_filp' in this struct.
309 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700310struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700311 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600312 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700313 struct kiocb rw;
314 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600315 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700316 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700317
318 struct sqe_submit submit;
319
320 struct io_ring_ctx *ctx;
321 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600322 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700323 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700324 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200325#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700326#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700327#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700328#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200329#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
330#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600331#define REQ_F_LINK 64 /* linked sqes */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800332#define REQ_F_LINK_DONE 128 /* linked sqes done */
333#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800334#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600335#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600336#define REQ_F_ISREG 2048 /* regular file */
337#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboefcb323c2019-10-24 12:39:47 -0600338#define REQ_F_INFLIGHT 8192 /* on inflight list */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700339 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600340 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600341 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700342
Jens Axboefcb323c2019-10-24 12:39:47 -0600343 struct list_head inflight_entry;
344
Jens Axboe561fb042019-10-24 07:25:42 -0600345 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700346};
347
348#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700349#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700350
Jens Axboe9a56a232019-01-09 09:06:50 -0700351struct io_submit_state {
352 struct blk_plug plug;
353
354 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700355 * io_kiocb alloc cache
356 */
357 void *reqs[IO_IOPOLL_BATCH];
358 unsigned int free_reqs;
359 unsigned int cur_req;
360
361 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700362 * File reference cache
363 */
364 struct file *file;
365 unsigned int fd;
366 unsigned int has_refs;
367 unsigned int used_refs;
368 unsigned int ios_left;
369};
370
Jens Axboe561fb042019-10-24 07:25:42 -0600371static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe5262f562019-09-17 12:26:57 -0600372static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
373 long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800374static void __io_free_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600375
Jens Axboe2b188cc2019-01-07 10:46:33 -0700376static struct kmem_cache *req_cachep;
377
378static const struct file_operations io_uring_fops;
379
380struct sock *io_uring_get_socket(struct file *file)
381{
382#if defined(CONFIG_UNIX)
383 if (file->f_op == &io_uring_fops) {
384 struct io_ring_ctx *ctx = file->private_data;
385
386 return ctx->ring_sock->sk;
387 }
388#endif
389 return NULL;
390}
391EXPORT_SYMBOL(io_uring_get_socket);
392
393static void io_ring_ctx_ref_free(struct percpu_ref *ref)
394{
395 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
396
397 complete(&ctx->ctx_done);
398}
399
400static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
401{
402 struct io_ring_ctx *ctx;
403
404 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
405 if (!ctx)
406 return NULL;
407
Roman Gushchin21482892019-05-07 10:01:48 -0700408 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
409 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410 kfree(ctx);
411 return NULL;
412 }
413
414 ctx->flags = p->flags;
415 init_waitqueue_head(&ctx->cq_wait);
416 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800417 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700418 mutex_init(&ctx->uring_lock);
419 init_waitqueue_head(&ctx->wait);
420 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700421 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700422 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600423 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600424 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600425 init_waitqueue_head(&ctx->inflight_wait);
426 spin_lock_init(&ctx->inflight_lock);
427 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700428 return ctx;
429}
430
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600431static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
432 struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600433{
Jens Axboe498ccd92019-10-25 10:04:25 -0600434 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
435 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600436}
437
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600438static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
439 struct io_kiocb *req)
440{
441 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
442 return false;
443
444 return __io_sequence_defer(ctx, req);
445}
446
447static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600448{
449 struct io_kiocb *req;
450
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600451 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
452 if (req && !io_sequence_defer(ctx, req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600453 list_del_init(&req->list);
454 return req;
455 }
456
457 return NULL;
458}
459
Jens Axboe5262f562019-09-17 12:26:57 -0600460static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
461{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600462 struct io_kiocb *req;
463
464 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
465 if (req && !__io_sequence_defer(ctx, req)) {
466 list_del_init(&req->list);
467 return req;
468 }
469
470 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600471}
472
Jens Axboede0617e2019-04-06 21:51:27 -0600473static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700474{
Hristo Venev75b28af2019-08-26 17:23:46 +0000475 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700476
Hristo Venev75b28af2019-08-26 17:23:46 +0000477 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700478 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000479 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700480
Jens Axboe2b188cc2019-01-07 10:46:33 -0700481 if (wq_has_sleeper(&ctx->cq_wait)) {
482 wake_up_interruptible(&ctx->cq_wait);
483 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
484 }
485 }
486}
487
Jens Axboe561fb042019-10-24 07:25:42 -0600488static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600489{
Jens Axboe561fb042019-10-24 07:25:42 -0600490 u8 opcode = READ_ONCE(sqe->opcode);
491
492 return !(opcode == IORING_OP_READ_FIXED ||
493 opcode == IORING_OP_WRITE_FIXED);
494}
495
496static inline bool io_prep_async_work(struct io_kiocb *req)
497{
498 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600499
Jens Axboe6cc47d12019-09-18 11:18:23 -0600500 if (req->submit.sqe) {
501 switch (req->submit.sqe->opcode) {
502 case IORING_OP_WRITEV:
503 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600504 do_hashed = true;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600505 break;
506 }
Jens Axboe561fb042019-10-24 07:25:42 -0600507 if (io_sqe_needs_user(req->submit.sqe))
508 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600509 }
510
Jens Axboe561fb042019-10-24 07:25:42 -0600511 return do_hashed;
512}
513
514static inline void io_queue_async_work(struct io_ring_ctx *ctx,
515 struct io_kiocb *req)
516{
517 bool do_hashed = io_prep_async_work(req);
518
519 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
520 req->flags);
521 if (!do_hashed) {
522 io_wq_enqueue(ctx->io_wq, &req->work);
523 } else {
524 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
525 file_inode(req->file));
526 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600527}
528
Jens Axboe5262f562019-09-17 12:26:57 -0600529static void io_kill_timeout(struct io_kiocb *req)
530{
531 int ret;
532
533 ret = hrtimer_try_to_cancel(&req->timeout.timer);
534 if (ret != -1) {
535 atomic_inc(&req->ctx->cq_timeouts);
536 list_del(&req->list);
537 io_cqring_fill_event(req->ctx, req->user_data, 0);
538 __io_free_req(req);
539 }
540}
541
542static void io_kill_timeouts(struct io_ring_ctx *ctx)
543{
544 struct io_kiocb *req, *tmp;
545
546 spin_lock_irq(&ctx->completion_lock);
547 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
548 io_kill_timeout(req);
549 spin_unlock_irq(&ctx->completion_lock);
550}
551
Jens Axboede0617e2019-04-06 21:51:27 -0600552static void io_commit_cqring(struct io_ring_ctx *ctx)
553{
554 struct io_kiocb *req;
555
Jens Axboe5262f562019-09-17 12:26:57 -0600556 while ((req = io_get_timeout_req(ctx)) != NULL)
557 io_kill_timeout(req);
558
Jens Axboede0617e2019-04-06 21:51:27 -0600559 __io_commit_cqring(ctx);
560
561 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800562 if (req->flags & REQ_F_SHADOW_DRAIN) {
563 /* Just for drain, free it. */
564 __io_free_req(req);
565 continue;
566 }
Jens Axboede0617e2019-04-06 21:51:27 -0600567 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600568 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600569 }
570}
571
Jens Axboe2b188cc2019-01-07 10:46:33 -0700572static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
573{
Hristo Venev75b28af2019-08-26 17:23:46 +0000574 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700575 unsigned tail;
576
577 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200578 /*
579 * writes to the cq entry need to come after reading head; the
580 * control dependency is enough as we're using WRITE_ONCE to
581 * fill the cq entry
582 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000583 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700584 return NULL;
585
586 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000587 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700588}
589
590static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600591 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700592{
593 struct io_uring_cqe *cqe;
594
595 /*
596 * If we can't get a cq entry, userspace overflowed the
597 * submission (by quite a lot). Increment the overflow count in
598 * the ring.
599 */
600 cqe = io_get_cqring(ctx);
601 if (cqe) {
602 WRITE_ONCE(cqe->user_data, ki_user_data);
603 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600604 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700605 } else {
Jens Axboe498ccd92019-10-25 10:04:25 -0600606 WRITE_ONCE(ctx->rings->cq_overflow,
607 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608 }
609}
610
Jens Axboe8c838782019-03-12 15:48:16 -0600611static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
612{
613 if (waitqueue_active(&ctx->wait))
614 wake_up(&ctx->wait);
615 if (waitqueue_active(&ctx->sqo_wait))
616 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600617 if (ctx->cq_ev_fd)
618 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600619}
620
621static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600622 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700623{
624 unsigned long flags;
625
626 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600627 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700628 io_commit_cqring(ctx);
629 spin_unlock_irqrestore(&ctx->completion_lock, flags);
630
Jens Axboe8c838782019-03-12 15:48:16 -0600631 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700632}
633
Jens Axboe2579f912019-01-09 09:10:43 -0700634static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
635 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700636{
Jens Axboefd6fab22019-03-14 16:30:06 -0600637 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700638 struct io_kiocb *req;
639
640 if (!percpu_ref_tryget(&ctx->refs))
641 return NULL;
642
Jens Axboe2579f912019-01-09 09:10:43 -0700643 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600644 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700645 if (unlikely(!req))
646 goto out;
647 } else if (!state->free_reqs) {
648 size_t sz;
649 int ret;
650
651 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600652 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
653
654 /*
655 * Bulk alloc is all-or-nothing. If we fail to get a batch,
656 * retry single alloc to be on the safe side.
657 */
658 if (unlikely(ret <= 0)) {
659 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
660 if (!state->reqs[0])
661 goto out;
662 ret = 1;
663 }
Jens Axboe2579f912019-01-09 09:10:43 -0700664 state->free_reqs = ret - 1;
665 state->cur_req = 1;
666 req = state->reqs[0];
667 } else {
668 req = state->reqs[state->cur_req];
669 state->free_reqs--;
670 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700671 }
672
Jens Axboe60c112b2019-06-21 10:20:18 -0600673 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700674 req->ctx = ctx;
675 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600676 /* one is dropped after submission, the other at completion */
677 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600678 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600679 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700680 return req;
681out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300682 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700683 return NULL;
684}
685
Jens Axboedef596e2019-01-09 08:59:42 -0700686static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
687{
688 if (*nr) {
689 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300690 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700691 *nr = 0;
692 }
693}
694
Jens Axboe9e645e112019-05-10 16:07:28 -0600695static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700696{
Jens Axboefcb323c2019-10-24 12:39:47 -0600697 struct io_ring_ctx *ctx = req->ctx;
698
Jens Axboe09bb8392019-03-13 12:39:28 -0600699 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
700 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600701 if (req->flags & REQ_F_INFLIGHT) {
702 unsigned long flags;
703
704 spin_lock_irqsave(&ctx->inflight_lock, flags);
705 list_del(&req->inflight_entry);
706 if (waitqueue_active(&ctx->inflight_wait))
707 wake_up(&ctx->inflight_wait);
708 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
709 }
710 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600711 kmem_cache_free(req_cachep, req);
712}
713
Jens Axboeba816ad2019-09-28 11:36:45 -0600714static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600715{
716 struct io_kiocb *nxt;
717
718 /*
719 * The list should never be empty when we are called here. But could
720 * potentially happen if the chain is messed up, check to be on the
721 * safe side.
722 */
723 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
724 if (nxt) {
725 list_del(&nxt->list);
726 if (!list_empty(&req->link_list)) {
727 INIT_LIST_HEAD(&nxt->link_list);
728 list_splice(&req->link_list, &nxt->link_list);
729 nxt->flags |= REQ_F_LINK;
730 }
731
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800732 nxt->flags |= REQ_F_LINK_DONE;
Jens Axboeba816ad2019-09-28 11:36:45 -0600733 /*
734 * If we're in async work, we can continue processing the chain
735 * in this context instead of having to queue up new async work.
736 */
Jens Axboe561fb042019-10-24 07:25:42 -0600737 if (nxtptr && current_work())
Jens Axboeba816ad2019-09-28 11:36:45 -0600738 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600739 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600740 io_queue_async_work(req->ctx, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600741 }
742}
743
744/*
745 * Called if REQ_F_LINK is set, and we fail the head request
746 */
747static void io_fail_links(struct io_kiocb *req)
748{
749 struct io_kiocb *link;
750
751 while (!list_empty(&req->link_list)) {
752 link = list_first_entry(&req->link_list, struct io_kiocb, list);
753 list_del(&link->list);
754
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200755 trace_io_uring_fail_link(req, link);
Jens Axboe9e645e112019-05-10 16:07:28 -0600756 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
757 __io_free_req(link);
758 }
759}
760
Jens Axboeba816ad2019-09-28 11:36:45 -0600761static void io_free_req(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600762{
763 /*
764 * If LINK is set, we have dependent requests in this chain. If we
765 * didn't fail this request, queue the first one up, moving any other
766 * dependencies to the next request. In case of failure, fail the rest
767 * of the chain.
768 */
769 if (req->flags & REQ_F_LINK) {
770 if (req->flags & REQ_F_FAIL_LINK)
771 io_fail_links(req);
772 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600773 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600774 }
775
776 __io_free_req(req);
777}
778
Jens Axboeba816ad2019-09-28 11:36:45 -0600779/*
780 * Drop reference to request, return next in chain (if there is one) if this
781 * was the last reference to this request.
782 */
783static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -0600784{
Jens Axboeba816ad2019-09-28 11:36:45 -0600785 struct io_kiocb *nxt = NULL;
786
Jens Axboee65ef562019-03-12 10:16:44 -0600787 if (refcount_dec_and_test(&req->refs))
Jens Axboeba816ad2019-09-28 11:36:45 -0600788 io_free_req(req, &nxt);
789
790 return nxt;
791}
792
793static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr)
794{
795 struct io_kiocb *nxt;
796
797 nxt = io_put_req_find_next(req);
798 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600799 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600800 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600801 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600802 io_queue_async_work(nxt->ctx, nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600803 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700804}
805
Hristo Venev75b28af2019-08-26 17:23:46 +0000806static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600807{
808 /* See comment at the top of this file */
809 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000810 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600811}
812
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300813static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
814{
815 struct io_rings *rings = ctx->rings;
816
817 /* make sure SQ entry isn't read before tail */
818 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
819}
820
Jens Axboedef596e2019-01-09 08:59:42 -0700821/*
822 * Find and free completed poll iocbs
823 */
824static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
825 struct list_head *done)
826{
827 void *reqs[IO_IOPOLL_BATCH];
828 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600829 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700830
Jens Axboe09bb8392019-03-13 12:39:28 -0600831 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700832 while (!list_empty(done)) {
833 req = list_first_entry(done, struct io_kiocb, list);
834 list_del(&req->list);
835
Jens Axboe9e645e112019-05-10 16:07:28 -0600836 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700837 (*nr_events)++;
838
Jens Axboe09bb8392019-03-13 12:39:28 -0600839 if (refcount_dec_and_test(&req->refs)) {
840 /* If we're not using fixed files, we have to pair the
841 * completion part with the file put. Use regular
842 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600843 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600844 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600845 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
846 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600847 reqs[to_free++] = req;
848 if (to_free == ARRAY_SIZE(reqs))
849 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700850 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -0600851 io_free_req(req, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -0700852 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700853 }
Jens Axboedef596e2019-01-09 08:59:42 -0700854 }
Jens Axboedef596e2019-01-09 08:59:42 -0700855
Jens Axboe09bb8392019-03-13 12:39:28 -0600856 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700857 io_free_req_many(ctx, reqs, &to_free);
858}
859
860static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
861 long min)
862{
863 struct io_kiocb *req, *tmp;
864 LIST_HEAD(done);
865 bool spin;
866 int ret;
867
868 /*
869 * Only spin for completions if we don't have multiple devices hanging
870 * off our complete list, and we're under the requested amount.
871 */
872 spin = !ctx->poll_multi_file && *nr_events < min;
873
874 ret = 0;
875 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
876 struct kiocb *kiocb = &req->rw;
877
878 /*
879 * Move completed entries to our local list. If we find a
880 * request that requires polling, break out and complete
881 * the done list first, if we have entries there.
882 */
883 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
884 list_move_tail(&req->list, &done);
885 continue;
886 }
887 if (!list_empty(&done))
888 break;
889
890 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
891 if (ret < 0)
892 break;
893
894 if (ret && spin)
895 spin = false;
896 ret = 0;
897 }
898
899 if (!list_empty(&done))
900 io_iopoll_complete(ctx, nr_events, &done);
901
902 return ret;
903}
904
905/*
906 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
907 * non-spinning poll check - we'll still enter the driver poll loop, but only
908 * as a non-spinning completion check.
909 */
910static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
911 long min)
912{
Jens Axboe08f54392019-08-21 22:19:11 -0600913 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700914 int ret;
915
916 ret = io_do_iopoll(ctx, nr_events, min);
917 if (ret < 0)
918 return ret;
919 if (!min || *nr_events >= min)
920 return 0;
921 }
922
923 return 1;
924}
925
926/*
927 * We can't just wait for polled events to come to us, we have to actively
928 * find and complete them.
929 */
930static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
931{
932 if (!(ctx->flags & IORING_SETUP_IOPOLL))
933 return;
934
935 mutex_lock(&ctx->uring_lock);
936 while (!list_empty(&ctx->poll_list)) {
937 unsigned int nr_events = 0;
938
939 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -0600940
941 /*
942 * Ensure we allow local-to-the-cpu processing to take place,
943 * in this case we need to ensure that we reap all events.
944 */
945 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -0700946 }
947 mutex_unlock(&ctx->uring_lock);
948}
949
Jens Axboe2b2ed972019-10-25 10:06:15 -0600950static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
951 long min)
Jens Axboedef596e2019-01-09 08:59:42 -0700952{
Jens Axboe2b2ed972019-10-25 10:06:15 -0600953 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700954
955 do {
956 int tmin = 0;
957
Jens Axboe500f9fb2019-08-19 12:15:59 -0600958 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600959 * Don't enter poll loop if we already have events pending.
960 * If we do, we can potentially be spinning for commands that
961 * already triggered a CQE (eg in error).
962 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000963 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600964 break;
965
966 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600967 * If a submit got punted to a workqueue, we can have the
968 * application entering polling for a command before it gets
969 * issued. That app will hold the uring_lock for the duration
970 * of the poll right here, so we need to take a breather every
971 * now and then to ensure that the issue has a chance to add
972 * the poll to the issued list. Otherwise we can spin here
973 * forever, while the workqueue is stuck trying to acquire the
974 * very same mutex.
975 */
976 if (!(++iters & 7)) {
977 mutex_unlock(&ctx->uring_lock);
978 mutex_lock(&ctx->uring_lock);
979 }
980
Jens Axboedef596e2019-01-09 08:59:42 -0700981 if (*nr_events < min)
982 tmin = min - *nr_events;
983
984 ret = io_iopoll_getevents(ctx, nr_events, tmin);
985 if (ret <= 0)
986 break;
987 ret = 0;
988 } while (min && !*nr_events && !need_resched());
989
Jens Axboe2b2ed972019-10-25 10:06:15 -0600990 return ret;
991}
992
993static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
994 long min)
995{
996 int ret;
997
998 /*
999 * We disallow the app entering submit/complete with polling, but we
1000 * still need to lock the ring to prevent racing with polled issue
1001 * that got punted to a workqueue.
1002 */
1003 mutex_lock(&ctx->uring_lock);
1004 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001005 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001006 return ret;
1007}
1008
Jens Axboe491381ce2019-10-17 09:20:46 -06001009static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001010{
Jens Axboe491381ce2019-10-17 09:20:46 -06001011 /*
1012 * Tell lockdep we inherited freeze protection from submission
1013 * thread.
1014 */
1015 if (req->flags & REQ_F_ISREG) {
1016 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001017
Jens Axboe491381ce2019-10-17 09:20:46 -06001018 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001019 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001020 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001021}
1022
Jens Axboeba816ad2019-09-28 11:36:45 -06001023static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001024{
1025 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1026
Jens Axboe491381ce2019-10-17 09:20:46 -06001027 if (kiocb->ki_flags & IOCB_WRITE)
1028 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001029
Jens Axboe9e645e112019-05-10 16:07:28 -06001030 if ((req->flags & REQ_F_LINK) && res != req->result)
1031 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001032 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001033}
1034
1035static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1036{
1037 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1038
1039 io_complete_rw_common(kiocb, res);
1040 io_put_req(req, NULL);
1041}
1042
1043static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1044{
1045 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1046
1047 io_complete_rw_common(kiocb, res);
1048 return io_put_req_find_next(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049}
1050
Jens Axboedef596e2019-01-09 08:59:42 -07001051static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1052{
1053 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1054
Jens Axboe491381ce2019-10-17 09:20:46 -06001055 if (kiocb->ki_flags & IOCB_WRITE)
1056 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001057
Jens Axboe9e645e112019-05-10 16:07:28 -06001058 if ((req->flags & REQ_F_LINK) && res != req->result)
1059 req->flags |= REQ_F_FAIL_LINK;
1060 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001061 if (res != -EAGAIN)
1062 req->flags |= REQ_F_IOPOLL_COMPLETED;
1063}
1064
1065/*
1066 * After the iocb has been issued, it's safe to be found on the poll list.
1067 * Adding the kiocb to the list AFTER submission ensures that we don't
1068 * find it from a io_iopoll_getevents() thread before the issuer is done
1069 * accessing the kiocb cookie.
1070 */
1071static void io_iopoll_req_issued(struct io_kiocb *req)
1072{
1073 struct io_ring_ctx *ctx = req->ctx;
1074
1075 /*
1076 * Track whether we have multiple files in our lists. This will impact
1077 * how we do polling eventually, not spinning if we're on potentially
1078 * different devices.
1079 */
1080 if (list_empty(&ctx->poll_list)) {
1081 ctx->poll_multi_file = false;
1082 } else if (!ctx->poll_multi_file) {
1083 struct io_kiocb *list_req;
1084
1085 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1086 list);
1087 if (list_req->rw.ki_filp != req->rw.ki_filp)
1088 ctx->poll_multi_file = true;
1089 }
1090
1091 /*
1092 * For fast devices, IO may have already completed. If it has, add
1093 * it to the front so we find it first.
1094 */
1095 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1096 list_add(&req->list, &ctx->poll_list);
1097 else
1098 list_add_tail(&req->list, &ctx->poll_list);
1099}
1100
Jens Axboe3d6770f2019-04-13 11:50:54 -06001101static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001102{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001103 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001104 int diff = state->has_refs - state->used_refs;
1105
1106 if (diff)
1107 fput_many(state->file, diff);
1108 state->file = NULL;
1109 }
1110}
1111
1112/*
1113 * Get as many references to a file as we have IOs left in this submission,
1114 * assuming most submissions are for one file, or at least that each file
1115 * has more than one submission.
1116 */
1117static struct file *io_file_get(struct io_submit_state *state, int fd)
1118{
1119 if (!state)
1120 return fget(fd);
1121
1122 if (state->file) {
1123 if (state->fd == fd) {
1124 state->used_refs++;
1125 state->ios_left--;
1126 return state->file;
1127 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001128 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001129 }
1130 state->file = fget_many(fd, state->ios_left);
1131 if (!state->file)
1132 return NULL;
1133
1134 state->fd = fd;
1135 state->has_refs = state->ios_left;
1136 state->used_refs = 1;
1137 state->ios_left--;
1138 return state->file;
1139}
1140
Jens Axboe2b188cc2019-01-07 10:46:33 -07001141/*
1142 * If we tracked the file through the SCM inflight mechanism, we could support
1143 * any file. For now, just ensure that anything potentially problematic is done
1144 * inline.
1145 */
1146static bool io_file_supports_async(struct file *file)
1147{
1148 umode_t mode = file_inode(file)->i_mode;
1149
1150 if (S_ISBLK(mode) || S_ISCHR(mode))
1151 return true;
1152 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1153 return true;
1154
1155 return false;
1156}
1157
Jens Axboe6c271ce2019-01-10 11:22:30 -07001158static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001159 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001160{
Jens Axboe6c271ce2019-01-10 11:22:30 -07001161 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001162 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001163 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001164 unsigned ioprio;
1165 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001166
Jens Axboe09bb8392019-03-13 12:39:28 -06001167 if (!req->file)
1168 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001169
Jens Axboe491381ce2019-10-17 09:20:46 -06001170 if (S_ISREG(file_inode(req->file)->i_mode))
1171 req->flags |= REQ_F_ISREG;
1172
1173 /*
1174 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1175 * we know to async punt it even if it was opened O_NONBLOCK
1176 */
1177 if (force_nonblock && !io_file_supports_async(req->file)) {
1178 req->flags |= REQ_F_MUST_PUNT;
1179 return -EAGAIN;
1180 }
Jens Axboe6b063142019-01-10 22:13:58 -07001181
Jens Axboe2b188cc2019-01-07 10:46:33 -07001182 kiocb->ki_pos = READ_ONCE(sqe->off);
1183 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1184 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1185
1186 ioprio = READ_ONCE(sqe->ioprio);
1187 if (ioprio) {
1188 ret = ioprio_check_cap(ioprio);
1189 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001190 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001191
1192 kiocb->ki_ioprio = ioprio;
1193 } else
1194 kiocb->ki_ioprio = get_current_ioprio();
1195
1196 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1197 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001198 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001199
1200 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001201 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1202 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001203 req->flags |= REQ_F_NOWAIT;
1204
1205 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001206 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001207
Jens Axboedef596e2019-01-09 08:59:42 -07001208 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001209 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1210 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001211 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001212
Jens Axboedef596e2019-01-09 08:59:42 -07001213 kiocb->ki_flags |= IOCB_HIPRI;
1214 kiocb->ki_complete = io_complete_rw_iopoll;
1215 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001216 if (kiocb->ki_flags & IOCB_HIPRI)
1217 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001218 kiocb->ki_complete = io_complete_rw;
1219 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001220 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001221}
1222
1223static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1224{
1225 switch (ret) {
1226 case -EIOCBQUEUED:
1227 break;
1228 case -ERESTARTSYS:
1229 case -ERESTARTNOINTR:
1230 case -ERESTARTNOHAND:
1231 case -ERESTART_RESTARTBLOCK:
1232 /*
1233 * We can't just restart the syscall, since previously
1234 * submitted sqes may already be in progress. Just fail this
1235 * IO with EINTR.
1236 */
1237 ret = -EINTR;
1238 /* fall through */
1239 default:
1240 kiocb->ki_complete(kiocb, ret, 0);
1241 }
1242}
1243
Jens Axboeba816ad2019-09-28 11:36:45 -06001244static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1245 bool in_async)
1246{
1247 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1248 *nxt = __io_complete_rw(kiocb, ret);
1249 else
1250 io_rw_done(kiocb, ret);
1251}
1252
Jens Axboeedafcce2019-01-09 09:16:05 -07001253static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1254 const struct io_uring_sqe *sqe,
1255 struct iov_iter *iter)
1256{
1257 size_t len = READ_ONCE(sqe->len);
1258 struct io_mapped_ubuf *imu;
1259 unsigned index, buf_index;
1260 size_t offset;
1261 u64 buf_addr;
1262
1263 /* attempt to use fixed buffers without having provided iovecs */
1264 if (unlikely(!ctx->user_bufs))
1265 return -EFAULT;
1266
1267 buf_index = READ_ONCE(sqe->buf_index);
1268 if (unlikely(buf_index >= ctx->nr_user_bufs))
1269 return -EFAULT;
1270
1271 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1272 imu = &ctx->user_bufs[index];
1273 buf_addr = READ_ONCE(sqe->addr);
1274
1275 /* overflow */
1276 if (buf_addr + len < buf_addr)
1277 return -EFAULT;
1278 /* not inside the mapped region */
1279 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1280 return -EFAULT;
1281
1282 /*
1283 * May not be a start of buffer, set size appropriately
1284 * and advance us to the beginning.
1285 */
1286 offset = buf_addr - imu->ubuf;
1287 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001288
1289 if (offset) {
1290 /*
1291 * Don't use iov_iter_advance() here, as it's really slow for
1292 * using the latter parts of a big fixed buffer - it iterates
1293 * over each segment manually. We can cheat a bit here, because
1294 * we know that:
1295 *
1296 * 1) it's a BVEC iter, we set it up
1297 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1298 * first and last bvec
1299 *
1300 * So just find our index, and adjust the iterator afterwards.
1301 * If the offset is within the first bvec (or the whole first
1302 * bvec, just use iov_iter_advance(). This makes it easier
1303 * since we can just skip the first segment, which may not
1304 * be PAGE_SIZE aligned.
1305 */
1306 const struct bio_vec *bvec = imu->bvec;
1307
1308 if (offset <= bvec->bv_len) {
1309 iov_iter_advance(iter, offset);
1310 } else {
1311 unsigned long seg_skip;
1312
1313 /* skip first vec */
1314 offset -= bvec->bv_len;
1315 seg_skip = 1 + (offset >> PAGE_SHIFT);
1316
1317 iter->bvec = bvec + seg_skip;
1318 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001319 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001320 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001321 }
1322 }
1323
Jens Axboeedafcce2019-01-09 09:16:05 -07001324 return 0;
1325}
1326
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001327static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1328 const struct sqe_submit *s, struct iovec **iovec,
1329 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001330{
1331 const struct io_uring_sqe *sqe = s->sqe;
1332 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1333 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001334 u8 opcode;
1335
1336 /*
1337 * We're reading ->opcode for the second time, but the first read
1338 * doesn't care whether it's _FIXED or not, so it doesn't matter
1339 * whether ->opcode changes concurrently. The first read does care
1340 * about whether it is a READ or a WRITE, so we don't trust this read
1341 * for that purpose and instead let the caller pass in the read/write
1342 * flag.
1343 */
1344 opcode = READ_ONCE(sqe->opcode);
1345 if (opcode == IORING_OP_READ_FIXED ||
1346 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001347 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001348 *iovec = NULL;
1349 return ret;
1350 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001351
1352 if (!s->has_user)
1353 return -EFAULT;
1354
1355#ifdef CONFIG_COMPAT
1356 if (ctx->compat)
1357 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1358 iovec, iter);
1359#endif
1360
1361 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1362}
1363
Jens Axboe32960612019-09-23 11:05:34 -06001364/*
1365 * For files that don't have ->read_iter() and ->write_iter(), handle them
1366 * by looping over ->read() or ->write() manually.
1367 */
1368static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1369 struct iov_iter *iter)
1370{
1371 ssize_t ret = 0;
1372
1373 /*
1374 * Don't support polled IO through this interface, and we can't
1375 * support non-blocking either. For the latter, this just causes
1376 * the kiocb to be handled from an async context.
1377 */
1378 if (kiocb->ki_flags & IOCB_HIPRI)
1379 return -EOPNOTSUPP;
1380 if (kiocb->ki_flags & IOCB_NOWAIT)
1381 return -EAGAIN;
1382
1383 while (iov_iter_count(iter)) {
1384 struct iovec iovec = iov_iter_iovec(iter);
1385 ssize_t nr;
1386
1387 if (rw == READ) {
1388 nr = file->f_op->read(file, iovec.iov_base,
1389 iovec.iov_len, &kiocb->ki_pos);
1390 } else {
1391 nr = file->f_op->write(file, iovec.iov_base,
1392 iovec.iov_len, &kiocb->ki_pos);
1393 }
1394
1395 if (nr < 0) {
1396 if (!ret)
1397 ret = nr;
1398 break;
1399 }
1400 ret += nr;
1401 if (nr != iovec.iov_len)
1402 break;
1403 iov_iter_advance(iter, nr);
1404 }
1405
1406 return ret;
1407}
1408
Jens Axboee0c5c572019-03-12 10:18:47 -06001409static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001410 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001411{
1412 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1413 struct kiocb *kiocb = &req->rw;
1414 struct iov_iter iter;
1415 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001416 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001417 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001418
Jens Axboe8358e3a2019-04-23 08:17:58 -06001419 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420 if (ret)
1421 return ret;
1422 file = kiocb->ki_filp;
1423
Jens Axboe2b188cc2019-01-07 10:46:33 -07001424 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001425 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001426
1427 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001428 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001429 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001430
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001431 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001432 if (req->flags & REQ_F_LINK)
1433 req->result = read_size;
1434
Jens Axboe31b51512019-01-18 22:56:34 -07001435 iov_count = iov_iter_count(&iter);
1436 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001437 if (!ret) {
1438 ssize_t ret2;
1439
Jens Axboe32960612019-09-23 11:05:34 -06001440 if (file->f_op->read_iter)
1441 ret2 = call_read_iter(file, kiocb, &iter);
1442 else
1443 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1444
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001445 /*
1446 * In case of a short read, punt to async. This can happen
1447 * if we have data partially cached. Alternatively we can
1448 * return the short read, in which case the application will
1449 * need to issue another SQE and wait for it. That SQE will
1450 * need async punt anyway, so it's more efficient to do it
1451 * here.
1452 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001453 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1454 (req->flags & REQ_F_ISREG) &&
1455 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001456 ret2 = -EAGAIN;
1457 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001458 if (!force_nonblock || ret2 != -EAGAIN)
Jackie Liuba5290c2019-10-09 09:19:59 +08001459 kiocb_done(kiocb, ret2, nxt, s->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001460 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001461 ret = -EAGAIN;
1462 }
1463 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001464 return ret;
1465}
1466
Jens Axboee0c5c572019-03-12 10:18:47 -06001467static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001468 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001469{
1470 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1471 struct kiocb *kiocb = &req->rw;
1472 struct iov_iter iter;
1473 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001474 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001475 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001476
Jens Axboe8358e3a2019-04-23 08:17:58 -06001477 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001478 if (ret)
1479 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001480
Jens Axboe2b188cc2019-01-07 10:46:33 -07001481 file = kiocb->ki_filp;
1482 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001483 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001484
1485 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001486 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001487 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001488
Jens Axboe9e645e112019-05-10 16:07:28 -06001489 if (req->flags & REQ_F_LINK)
1490 req->result = ret;
1491
Jens Axboe31b51512019-01-18 22:56:34 -07001492 iov_count = iov_iter_count(&iter);
1493
1494 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001495 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001496 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001497
1498 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001499 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001500 ssize_t ret2;
1501
Jens Axboe2b188cc2019-01-07 10:46:33 -07001502 /*
1503 * Open-code file_start_write here to grab freeze protection,
1504 * which will be released by another thread in
1505 * io_complete_rw(). Fool lockdep by telling it the lock got
1506 * released so that it doesn't complain about the held lock when
1507 * we return to userspace.
1508 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001509 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001510 __sb_start_write(file_inode(file)->i_sb,
1511 SB_FREEZE_WRITE, true);
1512 __sb_writers_release(file_inode(file)->i_sb,
1513 SB_FREEZE_WRITE);
1514 }
1515 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001516
Jens Axboe32960612019-09-23 11:05:34 -06001517 if (file->f_op->write_iter)
1518 ret2 = call_write_iter(file, kiocb, &iter);
1519 else
1520 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001521 if (!force_nonblock || ret2 != -EAGAIN)
Jackie Liuba5290c2019-10-09 09:19:59 +08001522 kiocb_done(kiocb, ret2, nxt, s->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001523 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001524 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001525 }
Jens Axboe31b51512019-01-18 22:56:34 -07001526out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001527 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001528 return ret;
1529}
1530
1531/*
1532 * IORING_OP_NOP just posts a completion event, nothing else.
1533 */
1534static int io_nop(struct io_kiocb *req, u64 user_data)
1535{
1536 struct io_ring_ctx *ctx = req->ctx;
1537 long err = 0;
1538
Jens Axboedef596e2019-01-09 08:59:42 -07001539 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1540 return -EINVAL;
1541
Jens Axboec71ffb62019-05-13 20:58:29 -06001542 io_cqring_add_event(ctx, user_data, err);
Jens Axboeba816ad2019-09-28 11:36:45 -06001543 io_put_req(req, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001544 return 0;
1545}
1546
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001547static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1548{
Jens Axboe6b063142019-01-10 22:13:58 -07001549 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001550
Jens Axboe09bb8392019-03-13 12:39:28 -06001551 if (!req->file)
1552 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001553
Jens Axboe6b063142019-01-10 22:13:58 -07001554 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001555 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001556 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001557 return -EINVAL;
1558
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001559 return 0;
1560}
1561
1562static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001563 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001564{
1565 loff_t sqe_off = READ_ONCE(sqe->off);
1566 loff_t sqe_len = READ_ONCE(sqe->len);
1567 loff_t end = sqe_off + sqe_len;
1568 unsigned fsync_flags;
1569 int ret;
1570
1571 fsync_flags = READ_ONCE(sqe->fsync_flags);
1572 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1573 return -EINVAL;
1574
1575 ret = io_prep_fsync(req, sqe);
1576 if (ret)
1577 return ret;
1578
1579 /* fsync always requires a blocking context */
1580 if (force_nonblock)
1581 return -EAGAIN;
1582
1583 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1584 end > 0 ? end : LLONG_MAX,
1585 fsync_flags & IORING_FSYNC_DATASYNC);
1586
Jens Axboe9e645e112019-05-10 16:07:28 -06001587 if (ret < 0 && (req->flags & REQ_F_LINK))
1588 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001589 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001590 io_put_req(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001591 return 0;
1592}
1593
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001594static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1595{
1596 struct io_ring_ctx *ctx = req->ctx;
1597 int ret = 0;
1598
1599 if (!req->file)
1600 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001601
1602 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1603 return -EINVAL;
1604 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1605 return -EINVAL;
1606
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001607 return ret;
1608}
1609
1610static int io_sync_file_range(struct io_kiocb *req,
1611 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001612 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001613 bool force_nonblock)
1614{
1615 loff_t sqe_off;
1616 loff_t sqe_len;
1617 unsigned flags;
1618 int ret;
1619
1620 ret = io_prep_sfr(req, sqe);
1621 if (ret)
1622 return ret;
1623
1624 /* sync_file_range always requires a blocking context */
1625 if (force_nonblock)
1626 return -EAGAIN;
1627
1628 sqe_off = READ_ONCE(sqe->off);
1629 sqe_len = READ_ONCE(sqe->len);
1630 flags = READ_ONCE(sqe->sync_range_flags);
1631
1632 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1633
Jens Axboe9e645e112019-05-10 16:07:28 -06001634 if (ret < 0 && (req->flags & REQ_F_LINK))
1635 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001636 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001637 io_put_req(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001638 return 0;
1639}
1640
Jens Axboe0fa03c62019-04-19 13:34:07 -06001641#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001642static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001643 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001644 long (*fn)(struct socket *, struct user_msghdr __user *,
1645 unsigned int))
1646{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001647 struct socket *sock;
1648 int ret;
1649
1650 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1651 return -EINVAL;
1652
1653 sock = sock_from_file(req->file, &ret);
1654 if (sock) {
1655 struct user_msghdr __user *msg;
1656 unsigned flags;
1657
1658 flags = READ_ONCE(sqe->msg_flags);
1659 if (flags & MSG_DONTWAIT)
1660 req->flags |= REQ_F_NOWAIT;
1661 else if (force_nonblock)
1662 flags |= MSG_DONTWAIT;
1663
1664 msg = (struct user_msghdr __user *) (unsigned long)
1665 READ_ONCE(sqe->addr);
1666
Jens Axboeaa1fa282019-04-19 13:38:09 -06001667 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001668 if (force_nonblock && ret == -EAGAIN)
1669 return ret;
1670 }
1671
1672 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001673 io_put_req(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001674 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001675}
1676#endif
1677
1678static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001679 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001680{
1681#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001682 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1683 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001684#else
1685 return -EOPNOTSUPP;
1686#endif
1687}
1688
1689static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001690 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001691{
1692#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001693 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1694 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001695#else
1696 return -EOPNOTSUPP;
1697#endif
1698}
1699
Jens Axboe17f2fe32019-10-17 14:42:58 -06001700static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1701 struct io_kiocb **nxt, bool force_nonblock)
1702{
1703#if defined(CONFIG_NET)
1704 struct sockaddr __user *addr;
1705 int __user *addr_len;
1706 unsigned file_flags;
1707 int flags, ret;
1708
1709 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1710 return -EINVAL;
1711 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1712 return -EINVAL;
1713
1714 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1715 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1716 flags = READ_ONCE(sqe->accept_flags);
1717 file_flags = force_nonblock ? O_NONBLOCK : 0;
1718
1719 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1720 if (ret == -EAGAIN && force_nonblock) {
1721 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1722 return -EAGAIN;
1723 }
1724 if (ret < 0 && (req->flags & REQ_F_LINK))
1725 req->flags |= REQ_F_FAIL_LINK;
1726 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1727 io_put_req(req, nxt);
1728 return 0;
1729#else
1730 return -EOPNOTSUPP;
1731#endif
1732}
1733
Jens Axboe221c5eb2019-01-17 09:41:58 -07001734static void io_poll_remove_one(struct io_kiocb *req)
1735{
1736 struct io_poll_iocb *poll = &req->poll;
1737
1738 spin_lock(&poll->head->lock);
1739 WRITE_ONCE(poll->canceled, true);
1740 if (!list_empty(&poll->wait.entry)) {
1741 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001742 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001743 }
1744 spin_unlock(&poll->head->lock);
1745
1746 list_del_init(&req->list);
1747}
1748
1749static void io_poll_remove_all(struct io_ring_ctx *ctx)
1750{
1751 struct io_kiocb *req;
1752
1753 spin_lock_irq(&ctx->completion_lock);
1754 while (!list_empty(&ctx->cancel_list)) {
1755 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1756 io_poll_remove_one(req);
1757 }
1758 spin_unlock_irq(&ctx->completion_lock);
1759}
1760
1761/*
1762 * Find a running poll command that matches one specified in sqe->addr,
1763 * and remove it if found.
1764 */
1765static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1766{
1767 struct io_ring_ctx *ctx = req->ctx;
1768 struct io_kiocb *poll_req, *next;
1769 int ret = -ENOENT;
1770
1771 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1772 return -EINVAL;
1773 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1774 sqe->poll_events)
1775 return -EINVAL;
1776
1777 spin_lock_irq(&ctx->completion_lock);
1778 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1779 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1780 io_poll_remove_one(poll_req);
1781 ret = 0;
1782 break;
1783 }
1784 }
1785 spin_unlock_irq(&ctx->completion_lock);
1786
Jens Axboec71ffb62019-05-13 20:58:29 -06001787 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001788 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001789 return 0;
1790}
1791
Jens Axboe8c838782019-03-12 15:48:16 -06001792static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1793 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001794{
Jens Axboe8c838782019-03-12 15:48:16 -06001795 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001796 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001797 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001798}
1799
Jens Axboe561fb042019-10-24 07:25:42 -06001800static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001801{
Jens Axboe561fb042019-10-24 07:25:42 -06001802 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001803 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1804 struct io_poll_iocb *poll = &req->poll;
1805 struct poll_table_struct pt = { ._key = poll->events };
1806 struct io_ring_ctx *ctx = req->ctx;
1807 __poll_t mask = 0;
1808
Jens Axboe561fb042019-10-24 07:25:42 -06001809 if (work->flags & IO_WQ_WORK_CANCEL)
1810 WRITE_ONCE(poll->canceled, true);
1811
Jens Axboe221c5eb2019-01-17 09:41:58 -07001812 if (!READ_ONCE(poll->canceled))
1813 mask = vfs_poll(poll->file, &pt) & poll->events;
1814
1815 /*
1816 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1817 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1818 * synchronize with them. In the cancellation case the list_del_init
1819 * itself is not actually needed, but harmless so we keep it in to
1820 * avoid further branches in the fast path.
1821 */
1822 spin_lock_irq(&ctx->completion_lock);
1823 if (!mask && !READ_ONCE(poll->canceled)) {
1824 add_wait_queue(poll->head, &poll->wait);
1825 spin_unlock_irq(&ctx->completion_lock);
1826 return;
1827 }
1828 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001829 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001830 spin_unlock_irq(&ctx->completion_lock);
1831
Jens Axboe8c838782019-03-12 15:48:16 -06001832 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001833 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001834}
1835
1836static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1837 void *key)
1838{
1839 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1840 wait);
1841 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1842 struct io_ring_ctx *ctx = req->ctx;
1843 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001844 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001845
1846 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001847 if (mask && !(mask & poll->events))
1848 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001849
1850 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001851
1852 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1853 list_del(&req->list);
1854 io_poll_complete(ctx, req, mask);
1855 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1856
1857 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001858 io_put_req(req, NULL);
Jens Axboe8c838782019-03-12 15:48:16 -06001859 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001860 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001861 }
1862
Jens Axboe221c5eb2019-01-17 09:41:58 -07001863 return 1;
1864}
1865
1866struct io_poll_table {
1867 struct poll_table_struct pt;
1868 struct io_kiocb *req;
1869 int error;
1870};
1871
1872static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1873 struct poll_table_struct *p)
1874{
1875 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1876
1877 if (unlikely(pt->req->poll.head)) {
1878 pt->error = -EINVAL;
1879 return;
1880 }
1881
1882 pt->error = 0;
1883 pt->req->poll.head = head;
1884 add_wait_queue(head, &pt->req->poll.wait);
1885}
1886
1887static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1888{
1889 struct io_poll_iocb *poll = &req->poll;
1890 struct io_ring_ctx *ctx = req->ctx;
1891 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001892 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001893 __poll_t mask;
1894 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001895
1896 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1897 return -EINVAL;
1898 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1899 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001900 if (!poll->file)
1901 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001902
Jens Axboe6cc47d12019-09-18 11:18:23 -06001903 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06001904 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001905 events = READ_ONCE(sqe->poll_events);
1906 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1907
Jens Axboe221c5eb2019-01-17 09:41:58 -07001908 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001909 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001910 poll->canceled = false;
1911
1912 ipt.pt._qproc = io_poll_queue_proc;
1913 ipt.pt._key = poll->events;
1914 ipt.req = req;
1915 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1916
1917 /* initialized the list so that we can do list_empty checks */
1918 INIT_LIST_HEAD(&poll->wait.entry);
1919 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1920
Jens Axboe36703242019-07-25 10:20:18 -06001921 INIT_LIST_HEAD(&req->list);
1922
Jens Axboe221c5eb2019-01-17 09:41:58 -07001923 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001924
1925 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001926 if (likely(poll->head)) {
1927 spin_lock(&poll->head->lock);
1928 if (unlikely(list_empty(&poll->wait.entry))) {
1929 if (ipt.error)
1930 cancel = true;
1931 ipt.error = 0;
1932 mask = 0;
1933 }
1934 if (mask || ipt.error)
1935 list_del_init(&poll->wait.entry);
1936 else if (cancel)
1937 WRITE_ONCE(poll->canceled, true);
1938 else if (!poll->done) /* actually waiting for an event */
1939 list_add_tail(&req->list, &ctx->cancel_list);
1940 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001941 }
Jens Axboe8c838782019-03-12 15:48:16 -06001942 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001943 ipt.error = 0;
1944 io_poll_complete(ctx, req, mask);
1945 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001946 spin_unlock_irq(&ctx->completion_lock);
1947
Jens Axboe8c838782019-03-12 15:48:16 -06001948 if (mask) {
1949 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001950 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001951 }
Jens Axboe8c838782019-03-12 15:48:16 -06001952 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001953}
1954
Jens Axboe5262f562019-09-17 12:26:57 -06001955static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1956{
1957 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06001958 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06001959 unsigned long flags;
Jens Axboe11365042019-10-16 09:08:32 -06001960 bool comp;
Jens Axboe5262f562019-09-17 12:26:57 -06001961
1962 req = container_of(timer, struct io_kiocb, timeout.timer);
1963 ctx = req->ctx;
1964 atomic_inc(&ctx->cq_timeouts);
1965
1966 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08001967 /*
Jens Axboe11365042019-10-16 09:08:32 -06001968 * We could be racing with timeout deletion. If the list is empty,
1969 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08001970 */
Jens Axboe11365042019-10-16 09:08:32 -06001971 comp = !list_empty(&req->list);
1972 if (comp) {
1973 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06001974
Jens Axboe11365042019-10-16 09:08:32 -06001975 /*
1976 * Adjust the reqs sequence before the current one because it
1977 * will consume a slot in the cq_ring and the the cq_tail
1978 * pointer will be increased, otherwise other timeout reqs may
1979 * return in advance without waiting for enough wait_nr.
1980 */
1981 prev = req;
1982 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
1983 prev->sequence++;
1984
1985 list_del_init(&req->list);
1986 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1987 io_commit_cqring(ctx);
1988 }
Jens Axboe5262f562019-09-17 12:26:57 -06001989 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1990
Jens Axboe11365042019-10-16 09:08:32 -06001991 if (comp) {
1992 io_cqring_ev_posted(ctx);
1993 io_put_req(req, NULL);
1994 }
1995 return HRTIMER_NORESTART;
1996}
1997
1998/*
1999 * Remove or update an existing timeout command
2000 */
2001static int io_timeout_remove(struct io_kiocb *req,
2002 const struct io_uring_sqe *sqe)
2003{
2004 struct io_ring_ctx *ctx = req->ctx;
2005 struct io_kiocb *treq;
2006 int ret = -ENOENT;
2007 __u64 user_data;
2008 unsigned flags;
2009
2010 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2011 return -EINVAL;
2012 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2013 return -EINVAL;
2014 flags = READ_ONCE(sqe->timeout_flags);
2015 if (flags)
2016 return -EINVAL;
2017
2018 user_data = READ_ONCE(sqe->addr);
2019 spin_lock_irq(&ctx->completion_lock);
2020 list_for_each_entry(treq, &ctx->timeout_list, list) {
2021 if (user_data == treq->user_data) {
2022 list_del_init(&treq->list);
2023 ret = 0;
2024 break;
2025 }
2026 }
2027
2028 /* didn't find timeout */
2029 if (ret) {
2030fill_ev:
2031 io_cqring_fill_event(ctx, req->user_data, ret);
2032 io_commit_cqring(ctx);
2033 spin_unlock_irq(&ctx->completion_lock);
2034 io_cqring_ev_posted(ctx);
2035 io_put_req(req, NULL);
2036 return 0;
2037 }
2038
2039 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
2040 if (ret == -1) {
2041 ret = -EBUSY;
2042 goto fill_ev;
2043 }
2044
2045 io_cqring_fill_event(ctx, req->user_data, 0);
2046 io_cqring_fill_event(ctx, treq->user_data, -ECANCELED);
2047 io_commit_cqring(ctx);
2048 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002049 io_cqring_ev_posted(ctx);
2050
Jens Axboe11365042019-10-16 09:08:32 -06002051 io_put_req(treq, NULL);
Jens Axboeba816ad2019-09-28 11:36:45 -06002052 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002053 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002054}
2055
2056static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2057{
yangerkun5da0fb12019-10-15 21:59:29 +08002058 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002059 struct io_ring_ctx *ctx = req->ctx;
2060 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002061 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002062 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002063 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002064 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002065
2066 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2067 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002068 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2069 return -EINVAL;
2070 flags = READ_ONCE(sqe->timeout_flags);
2071 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002072 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002073
2074 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002075 return -EFAULT;
2076
Jens Axboe11365042019-10-16 09:08:32 -06002077 if (flags & IORING_TIMEOUT_ABS)
2078 mode = HRTIMER_MODE_ABS;
2079 else
2080 mode = HRTIMER_MODE_REL;
2081
2082 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2083
Jens Axboe5262f562019-09-17 12:26:57 -06002084 /*
2085 * sqe->off holds how many events that need to occur for this
2086 * timeout event to be satisfied.
2087 */
2088 count = READ_ONCE(sqe->off);
2089 if (!count)
2090 count = 1;
2091
2092 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002093 /* reuse it to store the count */
2094 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002095 req->flags |= REQ_F_TIMEOUT;
2096
2097 /*
2098 * Insertion sort, ensuring the first entry in the list is always
2099 * the one we need first.
2100 */
Jens Axboe5262f562019-09-17 12:26:57 -06002101 spin_lock_irq(&ctx->completion_lock);
2102 list_for_each_prev(entry, &ctx->timeout_list) {
2103 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002104 unsigned nxt_sq_head;
2105 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002106
yangerkun5da0fb12019-10-15 21:59:29 +08002107 /*
2108 * Since cached_sq_head + count - 1 can overflow, use type long
2109 * long to store it.
2110 */
2111 tmp = (long long)ctx->cached_sq_head + count - 1;
2112 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2113 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2114
2115 /*
2116 * cached_sq_head may overflow, and it will never overflow twice
2117 * once there is some timeout req still be valid.
2118 */
2119 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002120 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002121
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002122 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002123 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002124
2125 /*
2126 * Sequence of reqs after the insert one and itself should
2127 * be adjusted because each timeout req consumes a slot.
2128 */
2129 span++;
2130 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002131 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002132 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002133 list_add(&req->list, entry);
2134 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002135 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002136 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe5262f562019-09-17 12:26:57 -06002137 return 0;
2138}
2139
Jens Axboede0617e2019-04-06 21:51:27 -06002140static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
2141 const struct io_uring_sqe *sqe)
2142{
2143 struct io_uring_sqe *sqe_copy;
2144
2145 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2146 return 0;
2147
2148 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2149 if (!sqe_copy)
2150 return -EAGAIN;
2151
2152 spin_lock_irq(&ctx->completion_lock);
2153 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2154 spin_unlock_irq(&ctx->completion_lock);
2155 kfree(sqe_copy);
2156 return 0;
2157 }
2158
2159 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2160 req->submit.sqe = sqe_copy;
2161
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002162 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002163 list_add_tail(&req->list, &ctx->defer_list);
2164 spin_unlock_irq(&ctx->completion_lock);
2165 return -EIOCBQUEUED;
2166}
2167
Jens Axboe2b188cc2019-01-07 10:46:33 -07002168static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboeba816ad2019-09-28 11:36:45 -06002169 const struct sqe_submit *s, struct io_kiocb **nxt,
2170 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002171{
Jens Axboee0c5c572019-03-12 10:18:47 -06002172 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002173
Jens Axboe9e645e112019-05-10 16:07:28 -06002174 req->user_data = READ_ONCE(s->sqe->user_data);
2175
Jens Axboe2b188cc2019-01-07 10:46:33 -07002176 opcode = READ_ONCE(s->sqe->opcode);
2177 switch (opcode) {
2178 case IORING_OP_NOP:
2179 ret = io_nop(req, req->user_data);
2180 break;
2181 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002182 if (unlikely(s->sqe->buf_index))
2183 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002184 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002185 break;
2186 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002187 if (unlikely(s->sqe->buf_index))
2188 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002189 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002190 break;
2191 case IORING_OP_READ_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002192 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002193 break;
2194 case IORING_OP_WRITE_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002195 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002196 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002197 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002198 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002199 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002200 case IORING_OP_POLL_ADD:
2201 ret = io_poll_add(req, s->sqe);
2202 break;
2203 case IORING_OP_POLL_REMOVE:
2204 ret = io_poll_remove(req, s->sqe);
2205 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002206 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002207 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002208 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002209 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002210 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002211 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002212 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002213 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002214 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002215 case IORING_OP_TIMEOUT:
2216 ret = io_timeout(req, s->sqe);
2217 break;
Jens Axboe11365042019-10-16 09:08:32 -06002218 case IORING_OP_TIMEOUT_REMOVE:
2219 ret = io_timeout_remove(req, s->sqe);
2220 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002221 case IORING_OP_ACCEPT:
2222 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2223 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002224 default:
2225 ret = -EINVAL;
2226 break;
2227 }
2228
Jens Axboedef596e2019-01-09 08:59:42 -07002229 if (ret)
2230 return ret;
2231
2232 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002233 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002234 return -EAGAIN;
2235
2236 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002237 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002238 mutex_lock(&ctx->uring_lock);
2239 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002240 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002241 mutex_unlock(&ctx->uring_lock);
2242 }
2243
2244 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002245}
2246
Jens Axboe561fb042019-10-24 07:25:42 -06002247static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002248{
Jens Axboe561fb042019-10-24 07:25:42 -06002249 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002250 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002251 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -06002252 struct sqe_submit *s = &req->submit;
2253 const struct io_uring_sqe *sqe = s->sqe;
2254 struct io_kiocb *nxt = NULL;
2255 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002256
Jens Axboe561fb042019-10-24 07:25:42 -06002257 /* Ensure we clear previously set non-block flag */
2258 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002259
Jens Axboe561fb042019-10-24 07:25:42 -06002260 if (work->flags & IO_WQ_WORK_CANCEL)
2261 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002262
Jens Axboe561fb042019-10-24 07:25:42 -06002263 if (!ret) {
2264 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2265 s->in_async = true;
2266 do {
2267 ret = __io_submit_sqe(ctx, req, s, &nxt, false);
2268 /*
2269 * We can get EAGAIN for polled IO even though we're
2270 * forcing a sync submission from here, since we can't
2271 * wait for request slots on the block side.
2272 */
2273 if (ret != -EAGAIN)
2274 break;
2275 cond_resched();
2276 } while (1);
2277 }
Jens Axboe31b51512019-01-18 22:56:34 -07002278
Jens Axboe561fb042019-10-24 07:25:42 -06002279 /* drop submission reference */
2280 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002281
Jens Axboe561fb042019-10-24 07:25:42 -06002282 if (ret) {
2283 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002284 io_put_req(req, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07002285 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002286
Jens Axboe561fb042019-10-24 07:25:42 -06002287 /* async context always use a copy of the sqe */
2288 kfree(sqe);
2289
2290 /* if a dependent link is ready, pass it back */
2291 if (!ret && nxt) {
2292 io_prep_async_work(nxt);
2293 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002294 }
Jens Axboe31b51512019-01-18 22:56:34 -07002295}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002296
Jens Axboe09bb8392019-03-13 12:39:28 -06002297static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2298{
2299 int op = READ_ONCE(sqe->opcode);
2300
2301 switch (op) {
2302 case IORING_OP_NOP:
2303 case IORING_OP_POLL_REMOVE:
2304 return false;
2305 default:
2306 return true;
2307 }
2308}
2309
Jens Axboe65e19f52019-10-26 07:20:21 -06002310static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2311 int index)
2312{
2313 struct fixed_file_table *table;
2314
2315 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2316 return table->files[index & IORING_FILE_TABLE_MASK];
2317}
2318
Jens Axboe09bb8392019-03-13 12:39:28 -06002319static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2320 struct io_submit_state *state, struct io_kiocb *req)
2321{
2322 unsigned flags;
2323 int fd;
2324
2325 flags = READ_ONCE(s->sqe->flags);
2326 fd = READ_ONCE(s->sqe->fd);
2327
Jackie Liu4fe2c962019-09-09 20:50:40 +08002328 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002329 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002330 /*
2331 * All io need record the previous position, if LINK vs DARIN,
2332 * it can be used to mark the position of the first IO in the
2333 * link list.
2334 */
2335 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002336
Jens Axboe60c112b2019-06-21 10:20:18 -06002337 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002338 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002339
2340 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002341 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002342 (unsigned) fd >= ctx->nr_user_files))
2343 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002344 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002345 req->file = io_file_from_index(ctx, fd);
2346 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002347 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002348 req->flags |= REQ_F_FIXED_FILE;
2349 } else {
2350 if (s->needs_fixed_file)
2351 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002352 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002353 req->file = io_file_get(state, fd);
2354 if (unlikely(!req->file))
2355 return -EBADF;
2356 }
2357
2358 return 0;
2359}
2360
Jens Axboefcb323c2019-10-24 12:39:47 -06002361static int io_grab_files(struct io_ring_ctx *ctx, struct io_kiocb *req)
2362{
2363 int ret = -EBADF;
2364
2365 rcu_read_lock();
2366 spin_lock_irq(&ctx->inflight_lock);
2367 /*
2368 * We use the f_ops->flush() handler to ensure that we can flush
2369 * out work accessing these files if the fd is closed. Check if
2370 * the fd has changed since we started down this path, and disallow
2371 * this operation if it has.
2372 */
2373 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2374 list_add(&req->inflight_entry, &ctx->inflight_list);
2375 req->flags |= REQ_F_INFLIGHT;
2376 req->work.files = current->files;
2377 ret = 0;
2378 }
2379 spin_unlock_irq(&ctx->inflight_lock);
2380 rcu_read_unlock();
2381
2382 return ret;
2383}
2384
Jackie Liu4fe2c962019-09-09 20:50:40 +08002385static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002386 struct sqe_submit *s)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002387{
Jens Axboee0c5c572019-03-12 10:18:47 -06002388 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002389
Jens Axboeba816ad2019-09-28 11:36:45 -06002390 ret = __io_submit_sqe(ctx, req, s, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002391
2392 /*
2393 * We async punt it if the file wasn't marked NOWAIT, or if the file
2394 * doesn't support non-blocking read/write attempts
2395 */
2396 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2397 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002398 struct io_uring_sqe *sqe_copy;
2399
Jackie Liu954dab12019-09-18 10:37:52 +08002400 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002401 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002402 s->sqe = sqe_copy;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002403 memcpy(&req->submit, s, sizeof(*s));
Jens Axboefcb323c2019-10-24 12:39:47 -06002404 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2405 ret = io_grab_files(ctx, req);
2406 if (ret) {
2407 kfree(sqe_copy);
2408 goto err;
2409 }
2410 }
Jens Axboee65ef562019-03-12 10:16:44 -06002411
2412 /*
2413 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002414 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002415 */
Jens Axboefcb323c2019-10-24 12:39:47 -06002416 io_queue_async_work(ctx, req);
Jens Axboee65ef562019-03-12 10:16:44 -06002417 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002418 }
2419 }
Jens Axboee65ef562019-03-12 10:16:44 -06002420
2421 /* drop submission reference */
Jens Axboefcb323c2019-10-24 12:39:47 -06002422err:
Jens Axboeba816ad2019-09-28 11:36:45 -06002423 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002424
2425 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002426 if (ret) {
2427 io_cqring_add_event(ctx, req->user_data, ret);
2428 if (req->flags & REQ_F_LINK)
2429 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002430 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002431 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002432
2433 return ret;
2434}
2435
Jackie Liu4fe2c962019-09-09 20:50:40 +08002436static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002437 struct sqe_submit *s)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002438{
2439 int ret;
2440
2441 ret = io_req_defer(ctx, req, s->sqe);
2442 if (ret) {
2443 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002444 io_free_req(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002445 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2446 }
2447 return 0;
2448 }
2449
Jens Axboebc808bc2019-10-22 13:14:37 -06002450 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002451}
2452
2453static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002454 struct sqe_submit *s, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002455{
2456 int ret;
2457 int need_submit = false;
2458
2459 if (!shadow)
Jens Axboebc808bc2019-10-22 13:14:37 -06002460 return io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002461
2462 /*
2463 * Mark the first IO in link list as DRAIN, let all the following
2464 * IOs enter the defer list. all IO needs to be completed before link
2465 * list.
2466 */
2467 req->flags |= REQ_F_IO_DRAIN;
2468 ret = io_req_defer(ctx, req, s->sqe);
2469 if (ret) {
2470 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002471 io_free_req(req, NULL);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002472 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002473 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2474 return 0;
2475 }
2476 } else {
2477 /*
2478 * If ret == 0 means that all IOs in front of link io are
2479 * running done. let's queue link head.
2480 */
2481 need_submit = true;
2482 }
2483
2484 /* Insert shadow req to defer_list, blocking next IOs */
2485 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002486 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002487 list_add_tail(&shadow->list, &ctx->defer_list);
2488 spin_unlock_irq(&ctx->completion_lock);
2489
2490 if (need_submit)
Jens Axboebc808bc2019-10-22 13:14:37 -06002491 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002492
2493 return 0;
2494}
2495
Jens Axboe9e645e112019-05-10 16:07:28 -06002496#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2497
2498static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboebc808bc2019-10-22 13:14:37 -06002499 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002500{
2501 struct io_uring_sqe *sqe_copy;
2502 struct io_kiocb *req;
2503 int ret;
2504
2505 /* enforce forwards compatibility on users */
2506 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2507 ret = -EINVAL;
2508 goto err;
2509 }
2510
2511 req = io_get_req(ctx, state);
2512 if (unlikely(!req)) {
2513 ret = -EAGAIN;
2514 goto err;
2515 }
2516
2517 ret = io_req_set_file(ctx, s, state, req);
2518 if (unlikely(ret)) {
2519err_req:
Jens Axboeba816ad2019-09-28 11:36:45 -06002520 io_free_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002521err:
2522 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2523 return;
2524 }
2525
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002526 req->user_data = s->sqe->user_data;
2527
Jens Axboe9e645e112019-05-10 16:07:28 -06002528 /*
2529 * If we already have a head request, queue this one for async
2530 * submittal once the head completes. If we don't have a head but
2531 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2532 * submitted sync once the chain is complete. If none of those
2533 * conditions are true (normal request), then just queue it.
2534 */
2535 if (*link) {
2536 struct io_kiocb *prev = *link;
2537
2538 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2539 if (!sqe_copy) {
2540 ret = -EAGAIN;
2541 goto err_req;
2542 }
2543
2544 s->sqe = sqe_copy;
2545 memcpy(&req->submit, s, sizeof(*s));
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002546 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002547 list_add_tail(&req->list, &prev->link_list);
2548 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2549 req->flags |= REQ_F_LINK;
2550
2551 memcpy(&req->submit, s, sizeof(*s));
2552 INIT_LIST_HEAD(&req->link_list);
2553 *link = req;
2554 } else {
Jens Axboebc808bc2019-10-22 13:14:37 -06002555 io_queue_sqe(ctx, req, s);
Jens Axboe9e645e112019-05-10 16:07:28 -06002556 }
2557}
2558
Jens Axboe9a56a232019-01-09 09:06:50 -07002559/*
2560 * Batched submission is done, ensure local IO is flushed out.
2561 */
2562static void io_submit_state_end(struct io_submit_state *state)
2563{
2564 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002565 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002566 if (state->free_reqs)
2567 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2568 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002569}
2570
2571/*
2572 * Start submission side cache.
2573 */
2574static void io_submit_state_start(struct io_submit_state *state,
2575 struct io_ring_ctx *ctx, unsigned max_ios)
2576{
2577 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002578 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002579 state->file = NULL;
2580 state->ios_left = max_ios;
2581}
2582
Jens Axboe2b188cc2019-01-07 10:46:33 -07002583static void io_commit_sqring(struct io_ring_ctx *ctx)
2584{
Hristo Venev75b28af2019-08-26 17:23:46 +00002585 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002586
Hristo Venev75b28af2019-08-26 17:23:46 +00002587 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002588 /*
2589 * Ensure any loads from the SQEs are done at this point,
2590 * since once we write the new head, the application could
2591 * write new data to them.
2592 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002593 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002594 }
2595}
2596
2597/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002598 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2599 * that is mapped by userspace. This means that care needs to be taken to
2600 * ensure that reads are stable, as we cannot rely on userspace always
2601 * being a good citizen. If members of the sqe are validated and then later
2602 * used, it's important that those reads are done through READ_ONCE() to
2603 * prevent a re-load down the line.
2604 */
2605static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2606{
Hristo Venev75b28af2019-08-26 17:23:46 +00002607 struct io_rings *rings = ctx->rings;
2608 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002609 unsigned head;
2610
2611 /*
2612 * The cached sq head (or cq tail) serves two purposes:
2613 *
2614 * 1) allows us to batch the cost of updating the user visible
2615 * head updates.
2616 * 2) allows the kernel side to track the head on its own, even
2617 * though the application is the one updating it.
2618 */
2619 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002620 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002621 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002622 return false;
2623
Hristo Venev75b28af2019-08-26 17:23:46 +00002624 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002625 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002626 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002627 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002628 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002629 ctx->cached_sq_head++;
2630 return true;
2631 }
2632
2633 /* drop invalid entries */
2634 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002635 ctx->cached_sq_dropped++;
2636 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002637 return false;
2638}
2639
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002640static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002641 struct mm_struct **mm)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002642{
2643 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002644 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002645 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002646 bool prev_was_link = false;
2647 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002648 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002649
2650 if (nr > IO_PLUG_THRESHOLD) {
2651 io_submit_state_start(&state, ctx, nr);
2652 statep = &state;
2653 }
2654
2655 for (i = 0; i < nr; i++) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002656 struct sqe_submit s;
2657
2658 if (!io_get_sqring(ctx, &s))
2659 break;
2660
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002661 if (io_sqe_needs_user(s.sqe) && !*mm) {
2662 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
2663 if (!mm_fault) {
2664 use_mm(ctx->sqo_mm);
2665 *mm = ctx->sqo_mm;
2666 }
2667 }
2668
Jens Axboe9e645e112019-05-10 16:07:28 -06002669 /*
2670 * If previous wasn't linked and we have a linked command,
2671 * that's the end of the chain. Submit the previous link.
2672 */
2673 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002674 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002675 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002676 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002677 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002678 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06002679
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002680 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002681 if (!shadow_req) {
2682 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002683 if (unlikely(!shadow_req))
2684 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002685 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2686 refcount_dec(&shadow_req->refs);
2687 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002688 shadow_req->sequence = s.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002689 }
2690
Jackie Liua1041c22019-09-18 17:25:52 +08002691out:
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002692 s.has_user = *mm != NULL;
2693 s.in_async = true;
2694 s.needs_fixed_file = true;
2695 trace_io_uring_submit_sqe(ctx, true, true);
2696 io_submit_sqe(ctx, &s, statep, &link);
2697 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002698 }
2699
Jens Axboe9e645e112019-05-10 16:07:28 -06002700 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002701 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002702 if (statep)
2703 io_submit_state_end(&state);
2704
2705 return submitted;
2706}
2707
2708static int io_sq_thread(void *data)
2709{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002710 struct io_ring_ctx *ctx = data;
2711 struct mm_struct *cur_mm = NULL;
2712 mm_segment_t old_fs;
2713 DEFINE_WAIT(wait);
2714 unsigned inflight;
2715 unsigned long timeout;
2716
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002717 complete(&ctx->sqo_thread_started);
2718
Jens Axboe6c271ce2019-01-10 11:22:30 -07002719 old_fs = get_fs();
2720 set_fs(USER_DS);
2721
2722 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002723 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002724 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002725
2726 if (inflight) {
2727 unsigned nr_events = 0;
2728
2729 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002730 /*
2731 * inflight is the count of the maximum possible
2732 * entries we submitted, but it can be smaller
2733 * if we dropped some of them. If we don't have
2734 * poll entries available, then we know that we
2735 * have nothing left to poll for. Reset the
2736 * inflight count to zero in that case.
2737 */
2738 mutex_lock(&ctx->uring_lock);
2739 if (!list_empty(&ctx->poll_list))
2740 __io_iopoll_check(ctx, &nr_events, 0);
2741 else
2742 inflight = 0;
2743 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002744 } else {
2745 /*
2746 * Normal IO, just pretend everything completed.
2747 * We don't have to poll completions for that.
2748 */
2749 nr_events = inflight;
2750 }
2751
2752 inflight -= nr_events;
2753 if (!inflight)
2754 timeout = jiffies + ctx->sq_thread_idle;
2755 }
2756
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002757 to_submit = io_sqring_entries(ctx);
2758 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002759 /*
2760 * We're polling. If we're within the defined idle
2761 * period, then let us spin without work before going
2762 * to sleep.
2763 */
2764 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002765 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002766 continue;
2767 }
2768
2769 /*
2770 * Drop cur_mm before scheduling, we can't hold it for
2771 * long periods (or over schedule()). Do this before
2772 * adding ourselves to the waitqueue, as the unuse/drop
2773 * may sleep.
2774 */
2775 if (cur_mm) {
2776 unuse_mm(cur_mm);
2777 mmput(cur_mm);
2778 cur_mm = NULL;
2779 }
2780
2781 prepare_to_wait(&ctx->sqo_wait, &wait,
2782 TASK_INTERRUPTIBLE);
2783
2784 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002785 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002786 /* make sure to read SQ tail after writing flags */
2787 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002788
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002789 to_submit = io_sqring_entries(ctx);
2790 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002791 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002792 finish_wait(&ctx->sqo_wait, &wait);
2793 break;
2794 }
2795 if (signal_pending(current))
2796 flush_signals(current);
2797 schedule();
2798 finish_wait(&ctx->sqo_wait, &wait);
2799
Hristo Venev75b28af2019-08-26 17:23:46 +00002800 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002801 continue;
2802 }
2803 finish_wait(&ctx->sqo_wait, &wait);
2804
Hristo Venev75b28af2019-08-26 17:23:46 +00002805 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002806 }
2807
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002808 to_submit = min(to_submit, ctx->sq_entries);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002809 inflight += io_submit_sqes(ctx, to_submit, &cur_mm);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002810
2811 /* Commit SQ ring head once we've consumed all SQEs */
2812 io_commit_sqring(ctx);
2813 }
2814
2815 set_fs(old_fs);
2816 if (cur_mm) {
2817 unuse_mm(cur_mm);
2818 mmput(cur_mm);
2819 }
Jens Axboe06058632019-04-13 09:26:03 -06002820
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002821 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002822
Jens Axboe6c271ce2019-01-10 11:22:30 -07002823 return 0;
2824}
2825
Jens Axboefcb323c2019-10-24 12:39:47 -06002826static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit,
2827 struct file *ring_file, int ring_fd)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002828{
Jens Axboe9a56a232019-01-09 09:06:50 -07002829 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002830 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002831 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002832 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002833 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002834
Jens Axboe9a56a232019-01-09 09:06:50 -07002835 if (to_submit > IO_PLUG_THRESHOLD) {
2836 io_submit_state_start(&state, ctx, to_submit);
2837 statep = &state;
2838 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002839
2840 for (i = 0; i < to_submit; i++) {
2841 struct sqe_submit s;
2842
2843 if (!io_get_sqring(ctx, &s))
2844 break;
2845
Jens Axboe9e645e112019-05-10 16:07:28 -06002846 /*
2847 * If previous wasn't linked and we have a linked command,
2848 * that's the end of the chain. Submit the previous link.
2849 */
2850 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002851 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002852 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002853 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002854 }
2855 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2856
Jackie Liu4fe2c962019-09-09 20:50:40 +08002857 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2858 if (!shadow_req) {
2859 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002860 if (unlikely(!shadow_req))
2861 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002862 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2863 refcount_dec(&shadow_req->refs);
2864 }
2865 shadow_req->sequence = s.sequence;
2866 }
2867
Jackie Liua1041c22019-09-18 17:25:52 +08002868out:
Jens Axboefcb323c2019-10-24 12:39:47 -06002869 s.ring_file = ring_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002870 s.has_user = true;
Jackie Liuba5290c2019-10-09 09:19:59 +08002871 s.in_async = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002872 s.needs_fixed_file = false;
Jens Axboefcb323c2019-10-24 12:39:47 -06002873 s.ring_fd = ring_fd;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002874 submit++;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002875 trace_io_uring_submit_sqe(ctx, true, false);
Jens Axboebc808bc2019-10-22 13:14:37 -06002876 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002877 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002878
Jens Axboe9e645e112019-05-10 16:07:28 -06002879 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002880 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9a56a232019-01-09 09:06:50 -07002881 if (statep)
2882 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002883
Pavel Begunkov935d1e42019-10-25 12:31:31 +03002884 io_commit_sqring(ctx);
2885
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002886 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002887}
2888
Jens Axboebda52162019-09-24 13:47:15 -06002889struct io_wait_queue {
2890 struct wait_queue_entry wq;
2891 struct io_ring_ctx *ctx;
2892 unsigned to_wait;
2893 unsigned nr_timeouts;
2894};
2895
2896static inline bool io_should_wake(struct io_wait_queue *iowq)
2897{
2898 struct io_ring_ctx *ctx = iowq->ctx;
2899
2900 /*
2901 * Wake up if we have enough events, or if a timeout occured since we
2902 * started waiting. For timeouts, we always want to return to userspace,
2903 * regardless of event count.
2904 */
2905 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2906 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2907}
2908
2909static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2910 int wake_flags, void *key)
2911{
2912 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
2913 wq);
2914
2915 if (!io_should_wake(iowq))
2916 return -1;
2917
2918 return autoremove_wake_function(curr, mode, wake_flags, key);
2919}
2920
Jens Axboe2b188cc2019-01-07 10:46:33 -07002921/*
2922 * Wait until events become available, if we don't already have some. The
2923 * application must reap them itself, as they reside on the shared cq ring.
2924 */
2925static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2926 const sigset_t __user *sig, size_t sigsz)
2927{
Jens Axboebda52162019-09-24 13:47:15 -06002928 struct io_wait_queue iowq = {
2929 .wq = {
2930 .private = current,
2931 .func = io_wake_function,
2932 .entry = LIST_HEAD_INIT(iowq.wq.entry),
2933 },
2934 .ctx = ctx,
2935 .to_wait = min_events,
2936 };
Hristo Venev75b28af2019-08-26 17:23:46 +00002937 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002938 int ret;
2939
Hristo Venev75b28af2019-08-26 17:23:46 +00002940 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002941 return 0;
2942
2943 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002944#ifdef CONFIG_COMPAT
2945 if (in_compat_syscall())
2946 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002947 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002948 else
2949#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002950 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002951
Jens Axboe2b188cc2019-01-07 10:46:33 -07002952 if (ret)
2953 return ret;
2954 }
2955
Jens Axboebda52162019-09-24 13:47:15 -06002956 ret = 0;
2957 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002958 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06002959 do {
2960 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
2961 TASK_INTERRUPTIBLE);
2962 if (io_should_wake(&iowq))
2963 break;
2964 schedule();
2965 if (signal_pending(current)) {
2966 ret = -ERESTARTSYS;
2967 break;
2968 }
2969 } while (1);
2970 finish_wait(&ctx->wait, &iowq.wq);
2971
Oleg Nesterovb7724342019-07-16 16:29:53 -07002972 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07002973 if (ret == -ERESTARTSYS)
2974 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002975
Hristo Venev75b28af2019-08-26 17:23:46 +00002976 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002977}
2978
Jens Axboe6b063142019-01-10 22:13:58 -07002979static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2980{
2981#if defined(CONFIG_UNIX)
2982 if (ctx->ring_sock) {
2983 struct sock *sock = ctx->ring_sock->sk;
2984 struct sk_buff *skb;
2985
2986 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2987 kfree_skb(skb);
2988 }
2989#else
2990 int i;
2991
Jens Axboe65e19f52019-10-26 07:20:21 -06002992 for (i = 0; i < ctx->nr_user_files; i++) {
2993 struct file *file;
2994
2995 file = io_file_from_index(ctx, i);
2996 if (file)
2997 fput(file);
2998 }
Jens Axboe6b063142019-01-10 22:13:58 -07002999#endif
3000}
3001
3002static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3003{
Jens Axboe65e19f52019-10-26 07:20:21 -06003004 unsigned nr_tables, i;
3005
3006 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003007 return -ENXIO;
3008
3009 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003010 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3011 for (i = 0; i < nr_tables; i++)
3012 kfree(ctx->file_table[i].files);
3013 kfree(ctx->file_table);
3014 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003015 ctx->nr_user_files = 0;
3016 return 0;
3017}
3018
Jens Axboe6c271ce2019-01-10 11:22:30 -07003019static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3020{
3021 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003022 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003023 /*
3024 * The park is a bit of a work-around, without it we get
3025 * warning spews on shutdown with SQPOLL set and affinity
3026 * set to a single CPU.
3027 */
Jens Axboe06058632019-04-13 09:26:03 -06003028 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003029 kthread_stop(ctx->sqo_thread);
3030 ctx->sqo_thread = NULL;
3031 }
3032}
3033
Jens Axboe6b063142019-01-10 22:13:58 -07003034static void io_finish_async(struct io_ring_ctx *ctx)
3035{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003036 io_sq_thread_stop(ctx);
3037
Jens Axboe561fb042019-10-24 07:25:42 -06003038 if (ctx->io_wq) {
3039 io_wq_destroy(ctx->io_wq);
3040 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003041 }
3042}
3043
3044#if defined(CONFIG_UNIX)
3045static void io_destruct_skb(struct sk_buff *skb)
3046{
3047 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3048
Jens Axboe561fb042019-10-24 07:25:42 -06003049 if (ctx->io_wq)
3050 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003051
Jens Axboe6b063142019-01-10 22:13:58 -07003052 unix_destruct_scm(skb);
3053}
3054
3055/*
3056 * Ensure the UNIX gc is aware of our file set, so we are certain that
3057 * the io_uring can be safely unregistered on process exit, even if we have
3058 * loops in the file referencing.
3059 */
3060static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3061{
3062 struct sock *sk = ctx->ring_sock->sk;
3063 struct scm_fp_list *fpl;
3064 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003065 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003066
3067 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3068 unsigned long inflight = ctx->user->unix_inflight + nr;
3069
3070 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3071 return -EMFILE;
3072 }
3073
3074 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3075 if (!fpl)
3076 return -ENOMEM;
3077
3078 skb = alloc_skb(0, GFP_KERNEL);
3079 if (!skb) {
3080 kfree(fpl);
3081 return -ENOMEM;
3082 }
3083
3084 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003085
Jens Axboe08a45172019-10-03 08:11:03 -06003086 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003087 fpl->user = get_uid(ctx->user);
3088 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003089 struct file *file = io_file_from_index(ctx, i + offset);
3090
3091 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003092 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003093 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003094 unix_inflight(fpl->user, fpl->fp[nr_files]);
3095 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003096 }
3097
Jens Axboe08a45172019-10-03 08:11:03 -06003098 if (nr_files) {
3099 fpl->max = SCM_MAX_FD;
3100 fpl->count = nr_files;
3101 UNIXCB(skb).fp = fpl;
3102 skb->destructor = io_destruct_skb;
3103 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3104 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003105
Jens Axboe08a45172019-10-03 08:11:03 -06003106 for (i = 0; i < nr_files; i++)
3107 fput(fpl->fp[i]);
3108 } else {
3109 kfree_skb(skb);
3110 kfree(fpl);
3111 }
Jens Axboe6b063142019-01-10 22:13:58 -07003112
3113 return 0;
3114}
3115
3116/*
3117 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3118 * causes regular reference counting to break down. We rely on the UNIX
3119 * garbage collection to take care of this problem for us.
3120 */
3121static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3122{
3123 unsigned left, total;
3124 int ret = 0;
3125
3126 total = 0;
3127 left = ctx->nr_user_files;
3128 while (left) {
3129 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003130
3131 ret = __io_sqe_files_scm(ctx, this_files, total);
3132 if (ret)
3133 break;
3134 left -= this_files;
3135 total += this_files;
3136 }
3137
3138 if (!ret)
3139 return 0;
3140
3141 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003142 struct file *file = io_file_from_index(ctx, total);
3143
3144 if (file)
3145 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003146 total++;
3147 }
3148
3149 return ret;
3150}
3151#else
3152static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3153{
3154 return 0;
3155}
3156#endif
3157
Jens Axboe65e19f52019-10-26 07:20:21 -06003158static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3159 unsigned nr_files)
3160{
3161 int i;
3162
3163 for (i = 0; i < nr_tables; i++) {
3164 struct fixed_file_table *table = &ctx->file_table[i];
3165 unsigned this_files;
3166
3167 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3168 table->files = kcalloc(this_files, sizeof(struct file *),
3169 GFP_KERNEL);
3170 if (!table->files)
3171 break;
3172 nr_files -= this_files;
3173 }
3174
3175 if (i == nr_tables)
3176 return 0;
3177
3178 for (i = 0; i < nr_tables; i++) {
3179 struct fixed_file_table *table = &ctx->file_table[i];
3180 kfree(table->files);
3181 }
3182 return 1;
3183}
3184
Jens Axboe6b063142019-01-10 22:13:58 -07003185static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3186 unsigned nr_args)
3187{
3188 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003189 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003190 int fd, ret = 0;
3191 unsigned i;
3192
Jens Axboe65e19f52019-10-26 07:20:21 -06003193 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003194 return -EBUSY;
3195 if (!nr_args)
3196 return -EINVAL;
3197 if (nr_args > IORING_MAX_FIXED_FILES)
3198 return -EMFILE;
3199
Jens Axboe65e19f52019-10-26 07:20:21 -06003200 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3201 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3202 GFP_KERNEL);
3203 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003204 return -ENOMEM;
3205
Jens Axboe65e19f52019-10-26 07:20:21 -06003206 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3207 kfree(ctx->file_table);
3208 return -ENOMEM;
3209 }
3210
Jens Axboe08a45172019-10-03 08:11:03 -06003211 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003212 struct fixed_file_table *table;
3213 unsigned index;
3214
Jens Axboe6b063142019-01-10 22:13:58 -07003215 ret = -EFAULT;
3216 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3217 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003218 /* allow sparse sets */
3219 if (fd == -1) {
3220 ret = 0;
3221 continue;
3222 }
Jens Axboe6b063142019-01-10 22:13:58 -07003223
Jens Axboe65e19f52019-10-26 07:20:21 -06003224 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3225 index = i & IORING_FILE_TABLE_MASK;
3226 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003227
3228 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003229 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003230 break;
3231 /*
3232 * Don't allow io_uring instances to be registered. If UNIX
3233 * isn't enabled, then this causes a reference cycle and this
3234 * instance can never get freed. If UNIX is enabled we'll
3235 * handle it just fine, but there's still no point in allowing
3236 * a ring fd as it doesn't support regular read/write anyway.
3237 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003238 if (table->files[index]->f_op == &io_uring_fops) {
3239 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003240 break;
3241 }
Jens Axboe6b063142019-01-10 22:13:58 -07003242 ret = 0;
3243 }
3244
3245 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003246 for (i = 0; i < ctx->nr_user_files; i++) {
3247 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003248
Jens Axboe65e19f52019-10-26 07:20:21 -06003249 file = io_file_from_index(ctx, i);
3250 if (file)
3251 fput(file);
3252 }
3253 for (i = 0; i < nr_tables; i++)
3254 kfree(ctx->file_table[i].files);
3255
3256 kfree(ctx->file_table);
3257 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003258 ctx->nr_user_files = 0;
3259 return ret;
3260 }
3261
3262 ret = io_sqe_files_scm(ctx);
3263 if (ret)
3264 io_sqe_files_unregister(ctx);
3265
3266 return ret;
3267}
3268
Jens Axboec3a31e62019-10-03 13:59:56 -06003269static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3270{
3271#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003272 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003273 struct sock *sock = ctx->ring_sock->sk;
3274 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3275 struct sk_buff *skb;
3276 int i;
3277
3278 __skb_queue_head_init(&list);
3279
3280 /*
3281 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3282 * remove this entry and rearrange the file array.
3283 */
3284 skb = skb_dequeue(head);
3285 while (skb) {
3286 struct scm_fp_list *fp;
3287
3288 fp = UNIXCB(skb).fp;
3289 for (i = 0; i < fp->count; i++) {
3290 int left;
3291
3292 if (fp->fp[i] != file)
3293 continue;
3294
3295 unix_notinflight(fp->user, fp->fp[i]);
3296 left = fp->count - 1 - i;
3297 if (left) {
3298 memmove(&fp->fp[i], &fp->fp[i + 1],
3299 left * sizeof(struct file *));
3300 }
3301 fp->count--;
3302 if (!fp->count) {
3303 kfree_skb(skb);
3304 skb = NULL;
3305 } else {
3306 __skb_queue_tail(&list, skb);
3307 }
3308 fput(file);
3309 file = NULL;
3310 break;
3311 }
3312
3313 if (!file)
3314 break;
3315
3316 __skb_queue_tail(&list, skb);
3317
3318 skb = skb_dequeue(head);
3319 }
3320
3321 if (skb_peek(&list)) {
3322 spin_lock_irq(&head->lock);
3323 while ((skb = __skb_dequeue(&list)) != NULL)
3324 __skb_queue_tail(head, skb);
3325 spin_unlock_irq(&head->lock);
3326 }
3327#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003328 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003329#endif
3330}
3331
3332static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3333 int index)
3334{
3335#if defined(CONFIG_UNIX)
3336 struct sock *sock = ctx->ring_sock->sk;
3337 struct sk_buff_head *head = &sock->sk_receive_queue;
3338 struct sk_buff *skb;
3339
3340 /*
3341 * See if we can merge this file into an existing skb SCM_RIGHTS
3342 * file set. If there's no room, fall back to allocating a new skb
3343 * and filling it in.
3344 */
3345 spin_lock_irq(&head->lock);
3346 skb = skb_peek(head);
3347 if (skb) {
3348 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3349
3350 if (fpl->count < SCM_MAX_FD) {
3351 __skb_unlink(skb, head);
3352 spin_unlock_irq(&head->lock);
3353 fpl->fp[fpl->count] = get_file(file);
3354 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3355 fpl->count++;
3356 spin_lock_irq(&head->lock);
3357 __skb_queue_head(head, skb);
3358 } else {
3359 skb = NULL;
3360 }
3361 }
3362 spin_unlock_irq(&head->lock);
3363
3364 if (skb) {
3365 fput(file);
3366 return 0;
3367 }
3368
3369 return __io_sqe_files_scm(ctx, 1, index);
3370#else
3371 return 0;
3372#endif
3373}
3374
3375static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3376 unsigned nr_args)
3377{
3378 struct io_uring_files_update up;
3379 __s32 __user *fds;
3380 int fd, i, err;
3381 __u32 done;
3382
Jens Axboe65e19f52019-10-26 07:20:21 -06003383 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003384 return -ENXIO;
3385 if (!nr_args)
3386 return -EINVAL;
3387 if (copy_from_user(&up, arg, sizeof(up)))
3388 return -EFAULT;
3389 if (check_add_overflow(up.offset, nr_args, &done))
3390 return -EOVERFLOW;
3391 if (done > ctx->nr_user_files)
3392 return -EINVAL;
3393
3394 done = 0;
3395 fds = (__s32 __user *) up.fds;
3396 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003397 struct fixed_file_table *table;
3398 unsigned index;
3399
Jens Axboec3a31e62019-10-03 13:59:56 -06003400 err = 0;
3401 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3402 err = -EFAULT;
3403 break;
3404 }
3405 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003406 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3407 index = i & IORING_FILE_TABLE_MASK;
3408 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003409 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003410 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003411 }
3412 if (fd != -1) {
3413 struct file *file;
3414
3415 file = fget(fd);
3416 if (!file) {
3417 err = -EBADF;
3418 break;
3419 }
3420 /*
3421 * Don't allow io_uring instances to be registered. If
3422 * UNIX isn't enabled, then this causes a reference
3423 * cycle and this instance can never get freed. If UNIX
3424 * is enabled we'll handle it just fine, but there's
3425 * still no point in allowing a ring fd as it doesn't
3426 * support regular read/write anyway.
3427 */
3428 if (file->f_op == &io_uring_fops) {
3429 fput(file);
3430 err = -EBADF;
3431 break;
3432 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003433 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003434 err = io_sqe_file_register(ctx, file, i);
3435 if (err)
3436 break;
3437 }
3438 nr_args--;
3439 done++;
3440 up.offset++;
3441 }
3442
3443 return done ? done : err;
3444}
3445
Jens Axboe6c271ce2019-01-10 11:22:30 -07003446static int io_sq_offload_start(struct io_ring_ctx *ctx,
3447 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003448{
Jens Axboe561fb042019-10-24 07:25:42 -06003449 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003450 int ret;
3451
Jens Axboe6c271ce2019-01-10 11:22:30 -07003452 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003453 mmgrab(current->mm);
3454 ctx->sqo_mm = current->mm;
3455
Jens Axboe6c271ce2019-01-10 11:22:30 -07003456 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003457 ret = -EPERM;
3458 if (!capable(CAP_SYS_ADMIN))
3459 goto err;
3460
Jens Axboe917257d2019-04-13 09:28:55 -06003461 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3462 if (!ctx->sq_thread_idle)
3463 ctx->sq_thread_idle = HZ;
3464
Jens Axboe6c271ce2019-01-10 11:22:30 -07003465 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003466 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003467
Jens Axboe917257d2019-04-13 09:28:55 -06003468 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003469 if (cpu >= nr_cpu_ids)
3470 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003471 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003472 goto err;
3473
Jens Axboe6c271ce2019-01-10 11:22:30 -07003474 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3475 ctx, cpu,
3476 "io_uring-sq");
3477 } else {
3478 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3479 "io_uring-sq");
3480 }
3481 if (IS_ERR(ctx->sqo_thread)) {
3482 ret = PTR_ERR(ctx->sqo_thread);
3483 ctx->sqo_thread = NULL;
3484 goto err;
3485 }
3486 wake_up_process(ctx->sqo_thread);
3487 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3488 /* Can't have SQ_AFF without SQPOLL */
3489 ret = -EINVAL;
3490 goto err;
3491 }
3492
Jens Axboe561fb042019-10-24 07:25:42 -06003493 /* Do QD, or 4 * CPUS, whatever is smallest */
3494 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
3495 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm);
3496 if (!ctx->io_wq) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003497 ret = -ENOMEM;
3498 goto err;
3499 }
3500
3501 return 0;
3502err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003503 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003504 mmdrop(ctx->sqo_mm);
3505 ctx->sqo_mm = NULL;
3506 return ret;
3507}
3508
3509static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3510{
3511 atomic_long_sub(nr_pages, &user->locked_vm);
3512}
3513
3514static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3515{
3516 unsigned long page_limit, cur_pages, new_pages;
3517
3518 /* Don't allow more pages than we can safely lock */
3519 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3520
3521 do {
3522 cur_pages = atomic_long_read(&user->locked_vm);
3523 new_pages = cur_pages + nr_pages;
3524 if (new_pages > page_limit)
3525 return -ENOMEM;
3526 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3527 new_pages) != cur_pages);
3528
3529 return 0;
3530}
3531
3532static void io_mem_free(void *ptr)
3533{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003534 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003535
Mark Rutland52e04ef2019-04-30 17:30:21 +01003536 if (!ptr)
3537 return;
3538
3539 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003540 if (put_page_testzero(page))
3541 free_compound_page(page);
3542}
3543
3544static void *io_mem_alloc(size_t size)
3545{
3546 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3547 __GFP_NORETRY;
3548
3549 return (void *) __get_free_pages(gfp_flags, get_order(size));
3550}
3551
Hristo Venev75b28af2019-08-26 17:23:46 +00003552static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3553 size_t *sq_offset)
3554{
3555 struct io_rings *rings;
3556 size_t off, sq_array_size;
3557
3558 off = struct_size(rings, cqes, cq_entries);
3559 if (off == SIZE_MAX)
3560 return SIZE_MAX;
3561
3562#ifdef CONFIG_SMP
3563 off = ALIGN(off, SMP_CACHE_BYTES);
3564 if (off == 0)
3565 return SIZE_MAX;
3566#endif
3567
3568 sq_array_size = array_size(sizeof(u32), sq_entries);
3569 if (sq_array_size == SIZE_MAX)
3570 return SIZE_MAX;
3571
3572 if (check_add_overflow(off, sq_array_size, &off))
3573 return SIZE_MAX;
3574
3575 if (sq_offset)
3576 *sq_offset = off;
3577
3578 return off;
3579}
3580
Jens Axboe2b188cc2019-01-07 10:46:33 -07003581static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3582{
Hristo Venev75b28af2019-08-26 17:23:46 +00003583 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003584
Hristo Venev75b28af2019-08-26 17:23:46 +00003585 pages = (size_t)1 << get_order(
3586 rings_size(sq_entries, cq_entries, NULL));
3587 pages += (size_t)1 << get_order(
3588 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003589
Hristo Venev75b28af2019-08-26 17:23:46 +00003590 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003591}
3592
Jens Axboeedafcce2019-01-09 09:16:05 -07003593static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3594{
3595 int i, j;
3596
3597 if (!ctx->user_bufs)
3598 return -ENXIO;
3599
3600 for (i = 0; i < ctx->nr_user_bufs; i++) {
3601 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3602
3603 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003604 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003605
3606 if (ctx->account_mem)
3607 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003608 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003609 imu->nr_bvecs = 0;
3610 }
3611
3612 kfree(ctx->user_bufs);
3613 ctx->user_bufs = NULL;
3614 ctx->nr_user_bufs = 0;
3615 return 0;
3616}
3617
3618static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3619 void __user *arg, unsigned index)
3620{
3621 struct iovec __user *src;
3622
3623#ifdef CONFIG_COMPAT
3624 if (ctx->compat) {
3625 struct compat_iovec __user *ciovs;
3626 struct compat_iovec ciov;
3627
3628 ciovs = (struct compat_iovec __user *) arg;
3629 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3630 return -EFAULT;
3631
3632 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3633 dst->iov_len = ciov.iov_len;
3634 return 0;
3635 }
3636#endif
3637 src = (struct iovec __user *) arg;
3638 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3639 return -EFAULT;
3640 return 0;
3641}
3642
3643static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3644 unsigned nr_args)
3645{
3646 struct vm_area_struct **vmas = NULL;
3647 struct page **pages = NULL;
3648 int i, j, got_pages = 0;
3649 int ret = -EINVAL;
3650
3651 if (ctx->user_bufs)
3652 return -EBUSY;
3653 if (!nr_args || nr_args > UIO_MAXIOV)
3654 return -EINVAL;
3655
3656 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3657 GFP_KERNEL);
3658 if (!ctx->user_bufs)
3659 return -ENOMEM;
3660
3661 for (i = 0; i < nr_args; i++) {
3662 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3663 unsigned long off, start, end, ubuf;
3664 int pret, nr_pages;
3665 struct iovec iov;
3666 size_t size;
3667
3668 ret = io_copy_iov(ctx, &iov, arg, i);
3669 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003670 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003671
3672 /*
3673 * Don't impose further limits on the size and buffer
3674 * constraints here, we'll -EINVAL later when IO is
3675 * submitted if they are wrong.
3676 */
3677 ret = -EFAULT;
3678 if (!iov.iov_base || !iov.iov_len)
3679 goto err;
3680
3681 /* arbitrary limit, but we need something */
3682 if (iov.iov_len > SZ_1G)
3683 goto err;
3684
3685 ubuf = (unsigned long) iov.iov_base;
3686 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3687 start = ubuf >> PAGE_SHIFT;
3688 nr_pages = end - start;
3689
3690 if (ctx->account_mem) {
3691 ret = io_account_mem(ctx->user, nr_pages);
3692 if (ret)
3693 goto err;
3694 }
3695
3696 ret = 0;
3697 if (!pages || nr_pages > got_pages) {
3698 kfree(vmas);
3699 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003700 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003701 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003702 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003703 sizeof(struct vm_area_struct *),
3704 GFP_KERNEL);
3705 if (!pages || !vmas) {
3706 ret = -ENOMEM;
3707 if (ctx->account_mem)
3708 io_unaccount_mem(ctx->user, nr_pages);
3709 goto err;
3710 }
3711 got_pages = nr_pages;
3712 }
3713
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003714 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003715 GFP_KERNEL);
3716 ret = -ENOMEM;
3717 if (!imu->bvec) {
3718 if (ctx->account_mem)
3719 io_unaccount_mem(ctx->user, nr_pages);
3720 goto err;
3721 }
3722
3723 ret = 0;
3724 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003725 pret = get_user_pages(ubuf, nr_pages,
3726 FOLL_WRITE | FOLL_LONGTERM,
3727 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003728 if (pret == nr_pages) {
3729 /* don't support file backed memory */
3730 for (j = 0; j < nr_pages; j++) {
3731 struct vm_area_struct *vma = vmas[j];
3732
3733 if (vma->vm_file &&
3734 !is_file_hugepages(vma->vm_file)) {
3735 ret = -EOPNOTSUPP;
3736 break;
3737 }
3738 }
3739 } else {
3740 ret = pret < 0 ? pret : -EFAULT;
3741 }
3742 up_read(&current->mm->mmap_sem);
3743 if (ret) {
3744 /*
3745 * if we did partial map, or found file backed vmas,
3746 * release any pages we did get
3747 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003748 if (pret > 0)
3749 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003750 if (ctx->account_mem)
3751 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003752 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003753 goto err;
3754 }
3755
3756 off = ubuf & ~PAGE_MASK;
3757 size = iov.iov_len;
3758 for (j = 0; j < nr_pages; j++) {
3759 size_t vec_len;
3760
3761 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3762 imu->bvec[j].bv_page = pages[j];
3763 imu->bvec[j].bv_len = vec_len;
3764 imu->bvec[j].bv_offset = off;
3765 off = 0;
3766 size -= vec_len;
3767 }
3768 /* store original address for later verification */
3769 imu->ubuf = ubuf;
3770 imu->len = iov.iov_len;
3771 imu->nr_bvecs = nr_pages;
3772
3773 ctx->nr_user_bufs++;
3774 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003775 kvfree(pages);
3776 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003777 return 0;
3778err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003779 kvfree(pages);
3780 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003781 io_sqe_buffer_unregister(ctx);
3782 return ret;
3783}
3784
Jens Axboe9b402842019-04-11 11:45:41 -06003785static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3786{
3787 __s32 __user *fds = arg;
3788 int fd;
3789
3790 if (ctx->cq_ev_fd)
3791 return -EBUSY;
3792
3793 if (copy_from_user(&fd, fds, sizeof(*fds)))
3794 return -EFAULT;
3795
3796 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3797 if (IS_ERR(ctx->cq_ev_fd)) {
3798 int ret = PTR_ERR(ctx->cq_ev_fd);
3799 ctx->cq_ev_fd = NULL;
3800 return ret;
3801 }
3802
3803 return 0;
3804}
3805
3806static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3807{
3808 if (ctx->cq_ev_fd) {
3809 eventfd_ctx_put(ctx->cq_ev_fd);
3810 ctx->cq_ev_fd = NULL;
3811 return 0;
3812 }
3813
3814 return -ENXIO;
3815}
3816
Jens Axboe2b188cc2019-01-07 10:46:33 -07003817static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3818{
Jens Axboe6b063142019-01-10 22:13:58 -07003819 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003820 if (ctx->sqo_mm)
3821 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003822
3823 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003824 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003825 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003826 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003827
Jens Axboe2b188cc2019-01-07 10:46:33 -07003828#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003829 if (ctx->ring_sock) {
3830 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003831 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003832 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003833#endif
3834
Hristo Venev75b28af2019-08-26 17:23:46 +00003835 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003836 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003837
3838 percpu_ref_exit(&ctx->refs);
3839 if (ctx->account_mem)
3840 io_unaccount_mem(ctx->user,
3841 ring_pages(ctx->sq_entries, ctx->cq_entries));
3842 free_uid(ctx->user);
3843 kfree(ctx);
3844}
3845
3846static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3847{
3848 struct io_ring_ctx *ctx = file->private_data;
3849 __poll_t mask = 0;
3850
3851 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003852 /*
3853 * synchronizes with barrier from wq_has_sleeper call in
3854 * io_commit_cqring
3855 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003856 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003857 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3858 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003859 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003860 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003861 mask |= EPOLLIN | EPOLLRDNORM;
3862
3863 return mask;
3864}
3865
3866static int io_uring_fasync(int fd, struct file *file, int on)
3867{
3868 struct io_ring_ctx *ctx = file->private_data;
3869
3870 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3871}
3872
3873static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3874{
3875 mutex_lock(&ctx->uring_lock);
3876 percpu_ref_kill(&ctx->refs);
3877 mutex_unlock(&ctx->uring_lock);
3878
Jens Axboe5262f562019-09-17 12:26:57 -06003879 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003880 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06003881
3882 if (ctx->io_wq)
3883 io_wq_cancel_all(ctx->io_wq);
3884
Jens Axboedef596e2019-01-09 08:59:42 -07003885 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003886 wait_for_completion(&ctx->ctx_done);
3887 io_ring_ctx_free(ctx);
3888}
3889
3890static int io_uring_release(struct inode *inode, struct file *file)
3891{
3892 struct io_ring_ctx *ctx = file->private_data;
3893
3894 file->private_data = NULL;
3895 io_ring_ctx_wait_and_kill(ctx);
3896 return 0;
3897}
3898
Jens Axboefcb323c2019-10-24 12:39:47 -06003899static void io_uring_cancel_files(struct io_ring_ctx *ctx,
3900 struct files_struct *files)
3901{
3902 struct io_kiocb *req;
3903 DEFINE_WAIT(wait);
3904
3905 while (!list_empty_careful(&ctx->inflight_list)) {
3906 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
3907
3908 spin_lock_irq(&ctx->inflight_lock);
3909 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
3910 if (req->work.files == files) {
3911 ret = io_wq_cancel_work(ctx->io_wq, &req->work);
3912 break;
3913 }
3914 }
3915 if (ret == IO_WQ_CANCEL_RUNNING)
3916 prepare_to_wait(&ctx->inflight_wait, &wait,
3917 TASK_UNINTERRUPTIBLE);
3918
3919 spin_unlock_irq(&ctx->inflight_lock);
3920
3921 /*
3922 * We need to keep going until we get NOTFOUND. We only cancel
3923 * one work at the time.
3924 *
3925 * If we get CANCEL_RUNNING, then wait for a work to complete
3926 * before continuing.
3927 */
3928 if (ret == IO_WQ_CANCEL_OK)
3929 continue;
3930 else if (ret != IO_WQ_CANCEL_RUNNING)
3931 break;
3932 schedule();
3933 }
3934}
3935
3936static int io_uring_flush(struct file *file, void *data)
3937{
3938 struct io_ring_ctx *ctx = file->private_data;
3939
3940 io_uring_cancel_files(ctx, data);
3941 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
3942 io_wq_cancel_all(ctx->io_wq);
3943 return 0;
3944}
3945
Jens Axboe2b188cc2019-01-07 10:46:33 -07003946static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3947{
3948 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3949 unsigned long sz = vma->vm_end - vma->vm_start;
3950 struct io_ring_ctx *ctx = file->private_data;
3951 unsigned long pfn;
3952 struct page *page;
3953 void *ptr;
3954
3955 switch (offset) {
3956 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003957 case IORING_OFF_CQ_RING:
3958 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003959 break;
3960 case IORING_OFF_SQES:
3961 ptr = ctx->sq_sqes;
3962 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003963 default:
3964 return -EINVAL;
3965 }
3966
3967 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07003968 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003969 return -EINVAL;
3970
3971 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3972 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3973}
3974
3975SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3976 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3977 size_t, sigsz)
3978{
3979 struct io_ring_ctx *ctx;
3980 long ret = -EBADF;
3981 int submitted = 0;
3982 struct fd f;
3983
Jens Axboe6c271ce2019-01-10 11:22:30 -07003984 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003985 return -EINVAL;
3986
3987 f = fdget(fd);
3988 if (!f.file)
3989 return -EBADF;
3990
3991 ret = -EOPNOTSUPP;
3992 if (f.file->f_op != &io_uring_fops)
3993 goto out_fput;
3994
3995 ret = -ENXIO;
3996 ctx = f.file->private_data;
3997 if (!percpu_ref_tryget(&ctx->refs))
3998 goto out_fput;
3999
Jens Axboe6c271ce2019-01-10 11:22:30 -07004000 /*
4001 * For SQ polling, the thread will do all submissions and completions.
4002 * Just return the requested submit count, and wake the thread if
4003 * we were asked to.
4004 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004005 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004006 if (ctx->flags & IORING_SETUP_SQPOLL) {
4007 if (flags & IORING_ENTER_SQ_WAKEUP)
4008 wake_up(&ctx->sqo_wait);
4009 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004010 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004011 to_submit = min(to_submit, ctx->sq_entries);
4012
4013 mutex_lock(&ctx->uring_lock);
Jens Axboefcb323c2019-10-24 12:39:47 -06004014 submitted = io_ring_submit(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004015 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004016 }
4017 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004018 unsigned nr_events = 0;
4019
Jens Axboe2b188cc2019-01-07 10:46:33 -07004020 min_complete = min(min_complete, ctx->cq_entries);
4021
Jens Axboedef596e2019-01-09 08:59:42 -07004022 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004023 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004024 } else {
4025 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4026 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004027 }
4028
Pavel Begunkov6805b322019-10-08 02:18:42 +03004029 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004030out_fput:
4031 fdput(f);
4032 return submitted ? submitted : ret;
4033}
4034
4035static const struct file_operations io_uring_fops = {
4036 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004037 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004038 .mmap = io_uring_mmap,
4039 .poll = io_uring_poll,
4040 .fasync = io_uring_fasync,
4041};
4042
4043static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4044 struct io_uring_params *p)
4045{
Hristo Venev75b28af2019-08-26 17:23:46 +00004046 struct io_rings *rings;
4047 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004048
Hristo Venev75b28af2019-08-26 17:23:46 +00004049 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4050 if (size == SIZE_MAX)
4051 return -EOVERFLOW;
4052
4053 rings = io_mem_alloc(size);
4054 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004055 return -ENOMEM;
4056
Hristo Venev75b28af2019-08-26 17:23:46 +00004057 ctx->rings = rings;
4058 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4059 rings->sq_ring_mask = p->sq_entries - 1;
4060 rings->cq_ring_mask = p->cq_entries - 1;
4061 rings->sq_ring_entries = p->sq_entries;
4062 rings->cq_ring_entries = p->cq_entries;
4063 ctx->sq_mask = rings->sq_ring_mask;
4064 ctx->cq_mask = rings->cq_ring_mask;
4065 ctx->sq_entries = rings->sq_ring_entries;
4066 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004067
4068 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4069 if (size == SIZE_MAX)
4070 return -EOVERFLOW;
4071
4072 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004073 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004074 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004075
Jens Axboe2b188cc2019-01-07 10:46:33 -07004076 return 0;
4077}
4078
4079/*
4080 * Allocate an anonymous fd, this is what constitutes the application
4081 * visible backing of an io_uring instance. The application mmaps this
4082 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4083 * we have to tie this fd to a socket for file garbage collection purposes.
4084 */
4085static int io_uring_get_fd(struct io_ring_ctx *ctx)
4086{
4087 struct file *file;
4088 int ret;
4089
4090#if defined(CONFIG_UNIX)
4091 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4092 &ctx->ring_sock);
4093 if (ret)
4094 return ret;
4095#endif
4096
4097 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4098 if (ret < 0)
4099 goto err;
4100
4101 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4102 O_RDWR | O_CLOEXEC);
4103 if (IS_ERR(file)) {
4104 put_unused_fd(ret);
4105 ret = PTR_ERR(file);
4106 goto err;
4107 }
4108
4109#if defined(CONFIG_UNIX)
4110 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004111 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004112#endif
4113 fd_install(ret, file);
4114 return ret;
4115err:
4116#if defined(CONFIG_UNIX)
4117 sock_release(ctx->ring_sock);
4118 ctx->ring_sock = NULL;
4119#endif
4120 return ret;
4121}
4122
4123static int io_uring_create(unsigned entries, struct io_uring_params *p)
4124{
4125 struct user_struct *user = NULL;
4126 struct io_ring_ctx *ctx;
4127 bool account_mem;
4128 int ret;
4129
4130 if (!entries || entries > IORING_MAX_ENTRIES)
4131 return -EINVAL;
4132
4133 /*
4134 * Use twice as many entries for the CQ ring. It's possible for the
4135 * application to drive a higher depth than the size of the SQ ring,
4136 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004137 * some flexibility in overcommitting a bit. If the application has
4138 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4139 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004140 */
4141 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004142 if (p->flags & IORING_SETUP_CQSIZE) {
4143 /*
4144 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4145 * to a power-of-two, if it isn't already. We do NOT impose
4146 * any cq vs sq ring sizing.
4147 */
4148 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4149 return -EINVAL;
4150 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4151 } else {
4152 p->cq_entries = 2 * p->sq_entries;
4153 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004154
4155 user = get_uid(current_user());
4156 account_mem = !capable(CAP_IPC_LOCK);
4157
4158 if (account_mem) {
4159 ret = io_account_mem(user,
4160 ring_pages(p->sq_entries, p->cq_entries));
4161 if (ret) {
4162 free_uid(user);
4163 return ret;
4164 }
4165 }
4166
4167 ctx = io_ring_ctx_alloc(p);
4168 if (!ctx) {
4169 if (account_mem)
4170 io_unaccount_mem(user, ring_pages(p->sq_entries,
4171 p->cq_entries));
4172 free_uid(user);
4173 return -ENOMEM;
4174 }
4175 ctx->compat = in_compat_syscall();
4176 ctx->account_mem = account_mem;
4177 ctx->user = user;
4178
4179 ret = io_allocate_scq_urings(ctx, p);
4180 if (ret)
4181 goto err;
4182
Jens Axboe6c271ce2019-01-10 11:22:30 -07004183 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004184 if (ret)
4185 goto err;
4186
Jens Axboe2b188cc2019-01-07 10:46:33 -07004187 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004188 p->sq_off.head = offsetof(struct io_rings, sq.head);
4189 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4190 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4191 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4192 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4193 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4194 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004195
4196 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004197 p->cq_off.head = offsetof(struct io_rings, cq.head);
4198 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4199 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4200 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4201 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4202 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004203
Jens Axboe044c1ab2019-10-28 09:15:33 -06004204 /*
4205 * Install ring fd as the very last thing, so we don't risk someone
4206 * having closed it before we finish setup
4207 */
4208 ret = io_uring_get_fd(ctx);
4209 if (ret < 0)
4210 goto err;
4211
Jens Axboeac90f242019-09-06 10:26:21 -06004212 p->features = IORING_FEAT_SINGLE_MMAP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004213 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004214 return ret;
4215err:
4216 io_ring_ctx_wait_and_kill(ctx);
4217 return ret;
4218}
4219
4220/*
4221 * Sets up an aio uring context, and returns the fd. Applications asks for a
4222 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4223 * params structure passed in.
4224 */
4225static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4226{
4227 struct io_uring_params p;
4228 long ret;
4229 int i;
4230
4231 if (copy_from_user(&p, params, sizeof(p)))
4232 return -EFAULT;
4233 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4234 if (p.resv[i])
4235 return -EINVAL;
4236 }
4237
Jens Axboe6c271ce2019-01-10 11:22:30 -07004238 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004239 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004240 return -EINVAL;
4241
4242 ret = io_uring_create(entries, &p);
4243 if (ret < 0)
4244 return ret;
4245
4246 if (copy_to_user(params, &p, sizeof(p)))
4247 return -EFAULT;
4248
4249 return ret;
4250}
4251
4252SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4253 struct io_uring_params __user *, params)
4254{
4255 return io_uring_setup(entries, params);
4256}
4257
Jens Axboeedafcce2019-01-09 09:16:05 -07004258static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4259 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004260 __releases(ctx->uring_lock)
4261 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004262{
4263 int ret;
4264
Jens Axboe35fa71a2019-04-22 10:23:23 -06004265 /*
4266 * We're inside the ring mutex, if the ref is already dying, then
4267 * someone else killed the ctx or is already going through
4268 * io_uring_register().
4269 */
4270 if (percpu_ref_is_dying(&ctx->refs))
4271 return -ENXIO;
4272
Jens Axboeedafcce2019-01-09 09:16:05 -07004273 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004274
4275 /*
4276 * Drop uring mutex before waiting for references to exit. If another
4277 * thread is currently inside io_uring_enter() it might need to grab
4278 * the uring_lock to make progress. If we hold it here across the drain
4279 * wait, then we can deadlock. It's safe to drop the mutex here, since
4280 * no new references will come in after we've killed the percpu ref.
4281 */
4282 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004283 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004284 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004285
4286 switch (opcode) {
4287 case IORING_REGISTER_BUFFERS:
4288 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4289 break;
4290 case IORING_UNREGISTER_BUFFERS:
4291 ret = -EINVAL;
4292 if (arg || nr_args)
4293 break;
4294 ret = io_sqe_buffer_unregister(ctx);
4295 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004296 case IORING_REGISTER_FILES:
4297 ret = io_sqe_files_register(ctx, arg, nr_args);
4298 break;
4299 case IORING_UNREGISTER_FILES:
4300 ret = -EINVAL;
4301 if (arg || nr_args)
4302 break;
4303 ret = io_sqe_files_unregister(ctx);
4304 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004305 case IORING_REGISTER_FILES_UPDATE:
4306 ret = io_sqe_files_update(ctx, arg, nr_args);
4307 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004308 case IORING_REGISTER_EVENTFD:
4309 ret = -EINVAL;
4310 if (nr_args != 1)
4311 break;
4312 ret = io_eventfd_register(ctx, arg);
4313 break;
4314 case IORING_UNREGISTER_EVENTFD:
4315 ret = -EINVAL;
4316 if (arg || nr_args)
4317 break;
4318 ret = io_eventfd_unregister(ctx);
4319 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004320 default:
4321 ret = -EINVAL;
4322 break;
4323 }
4324
4325 /* bring the ctx back to life */
4326 reinit_completion(&ctx->ctx_done);
4327 percpu_ref_reinit(&ctx->refs);
4328 return ret;
4329}
4330
4331SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4332 void __user *, arg, unsigned int, nr_args)
4333{
4334 struct io_ring_ctx *ctx;
4335 long ret = -EBADF;
4336 struct fd f;
4337
4338 f = fdget(fd);
4339 if (!f.file)
4340 return -EBADF;
4341
4342 ret = -EOPNOTSUPP;
4343 if (f.file->f_op != &io_uring_fops)
4344 goto out_fput;
4345
4346 ctx = f.file->private_data;
4347
4348 mutex_lock(&ctx->uring_lock);
4349 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4350 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004351 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4352 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004353out_fput:
4354 fdput(f);
4355 return ret;
4356}
4357
Jens Axboe2b188cc2019-01-07 10:46:33 -07004358static int __init io_uring_init(void)
4359{
4360 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4361 return 0;
4362};
4363__initcall(io_uring_init);