blob: 5360c3dd262b9919c7f9ef8552d7d0718fcee0c0 [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_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800333#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600334#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600335#define REQ_F_ISREG 2048 /* regular file */
336#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboefcb323c2019-10-24 12:39:47 -0600337#define REQ_F_INFLIGHT 8192 /* on inflight list */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700338 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600339 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600340 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700341
Jens Axboefcb323c2019-10-24 12:39:47 -0600342 struct list_head inflight_entry;
343
Jens Axboe561fb042019-10-24 07:25:42 -0600344 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700345};
346
347#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700348#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700349
Jens Axboe9a56a232019-01-09 09:06:50 -0700350struct io_submit_state {
351 struct blk_plug plug;
352
353 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700354 * io_kiocb alloc cache
355 */
356 void *reqs[IO_IOPOLL_BATCH];
357 unsigned int free_reqs;
358 unsigned int cur_req;
359
360 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700361 * File reference cache
362 */
363 struct file *file;
364 unsigned int fd;
365 unsigned int has_refs;
366 unsigned int used_refs;
367 unsigned int ios_left;
368};
369
Jens Axboe561fb042019-10-24 07:25:42 -0600370static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe5262f562019-09-17 12:26:57 -0600371static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
372 long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800373static void __io_free_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600374
Jens Axboe2b188cc2019-01-07 10:46:33 -0700375static struct kmem_cache *req_cachep;
376
377static const struct file_operations io_uring_fops;
378
379struct sock *io_uring_get_socket(struct file *file)
380{
381#if defined(CONFIG_UNIX)
382 if (file->f_op == &io_uring_fops) {
383 struct io_ring_ctx *ctx = file->private_data;
384
385 return ctx->ring_sock->sk;
386 }
387#endif
388 return NULL;
389}
390EXPORT_SYMBOL(io_uring_get_socket);
391
392static void io_ring_ctx_ref_free(struct percpu_ref *ref)
393{
394 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
395
396 complete(&ctx->ctx_done);
397}
398
399static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
400{
401 struct io_ring_ctx *ctx;
402
403 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
404 if (!ctx)
405 return NULL;
406
Roman Gushchin21482892019-05-07 10:01:48 -0700407 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
408 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700409 kfree(ctx);
410 return NULL;
411 }
412
413 ctx->flags = p->flags;
414 init_waitqueue_head(&ctx->cq_wait);
415 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800416 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700417 mutex_init(&ctx->uring_lock);
418 init_waitqueue_head(&ctx->wait);
419 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700420 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700421 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600422 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600423 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600424 init_waitqueue_head(&ctx->inflight_wait);
425 spin_lock_init(&ctx->inflight_lock);
426 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700427 return ctx;
428}
429
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600430static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
431 struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600432{
Jens Axboe498ccd92019-10-25 10:04:25 -0600433 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
434 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600435}
436
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600437static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
438 struct io_kiocb *req)
439{
440 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
441 return false;
442
443 return __io_sequence_defer(ctx, req);
444}
445
446static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600447{
448 struct io_kiocb *req;
449
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600450 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
451 if (req && !io_sequence_defer(ctx, req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600452 list_del_init(&req->list);
453 return req;
454 }
455
456 return NULL;
457}
458
Jens Axboe5262f562019-09-17 12:26:57 -0600459static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
460{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600461 struct io_kiocb *req;
462
463 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
464 if (req && !__io_sequence_defer(ctx, req)) {
465 list_del_init(&req->list);
466 return req;
467 }
468
469 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600470}
471
Jens Axboede0617e2019-04-06 21:51:27 -0600472static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700473{
Hristo Venev75b28af2019-08-26 17:23:46 +0000474 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700475
Hristo Venev75b28af2019-08-26 17:23:46 +0000476 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700477 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000478 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700479
Jens Axboe2b188cc2019-01-07 10:46:33 -0700480 if (wq_has_sleeper(&ctx->cq_wait)) {
481 wake_up_interruptible(&ctx->cq_wait);
482 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
483 }
484 }
485}
486
Jens Axboe561fb042019-10-24 07:25:42 -0600487static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600488{
Jens Axboe561fb042019-10-24 07:25:42 -0600489 u8 opcode = READ_ONCE(sqe->opcode);
490
491 return !(opcode == IORING_OP_READ_FIXED ||
492 opcode == IORING_OP_WRITE_FIXED);
493}
494
495static inline bool io_prep_async_work(struct io_kiocb *req)
496{
497 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600498
Jens Axboe6cc47d12019-09-18 11:18:23 -0600499 if (req->submit.sqe) {
500 switch (req->submit.sqe->opcode) {
501 case IORING_OP_WRITEV:
502 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600503 do_hashed = true;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600504 break;
505 }
Jens Axboe561fb042019-10-24 07:25:42 -0600506 if (io_sqe_needs_user(req->submit.sqe))
507 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600508 }
509
Jens Axboe561fb042019-10-24 07:25:42 -0600510 return do_hashed;
511}
512
513static inline void io_queue_async_work(struct io_ring_ctx *ctx,
514 struct io_kiocb *req)
515{
516 bool do_hashed = io_prep_async_work(req);
517
518 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
519 req->flags);
520 if (!do_hashed) {
521 io_wq_enqueue(ctx->io_wq, &req->work);
522 } else {
523 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
524 file_inode(req->file));
525 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600526}
527
Jens Axboe5262f562019-09-17 12:26:57 -0600528static void io_kill_timeout(struct io_kiocb *req)
529{
530 int ret;
531
532 ret = hrtimer_try_to_cancel(&req->timeout.timer);
533 if (ret != -1) {
534 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600535 list_del_init(&req->list);
Jens Axboe5262f562019-09-17 12:26:57 -0600536 io_cqring_fill_event(req->ctx, req->user_data, 0);
537 __io_free_req(req);
538 }
539}
540
541static void io_kill_timeouts(struct io_ring_ctx *ctx)
542{
543 struct io_kiocb *req, *tmp;
544
545 spin_lock_irq(&ctx->completion_lock);
546 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
547 io_kill_timeout(req);
548 spin_unlock_irq(&ctx->completion_lock);
549}
550
Jens Axboede0617e2019-04-06 21:51:27 -0600551static void io_commit_cqring(struct io_ring_ctx *ctx)
552{
553 struct io_kiocb *req;
554
Jens Axboe5262f562019-09-17 12:26:57 -0600555 while ((req = io_get_timeout_req(ctx)) != NULL)
556 io_kill_timeout(req);
557
Jens Axboede0617e2019-04-06 21:51:27 -0600558 __io_commit_cqring(ctx);
559
560 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800561 if (req->flags & REQ_F_SHADOW_DRAIN) {
562 /* Just for drain, free it. */
563 __io_free_req(req);
564 continue;
565 }
Jens Axboede0617e2019-04-06 21:51:27 -0600566 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600567 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600568 }
569}
570
Jens Axboe2b188cc2019-01-07 10:46:33 -0700571static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
572{
Hristo Venev75b28af2019-08-26 17:23:46 +0000573 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700574 unsigned tail;
575
576 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200577 /*
578 * writes to the cq entry need to come after reading head; the
579 * control dependency is enough as we're using WRITE_ONCE to
580 * fill the cq entry
581 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000582 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700583 return NULL;
584
585 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000586 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700587}
588
589static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600590 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700591{
592 struct io_uring_cqe *cqe;
593
Jens Axboe51c3ff62019-11-03 06:52:50 -0700594 trace_io_uring_complete(ctx, ki_user_data, res);
595
Jens Axboe2b188cc2019-01-07 10:46:33 -0700596 /*
597 * If we can't get a cq entry, userspace overflowed the
598 * submission (by quite a lot). Increment the overflow count in
599 * the ring.
600 */
601 cqe = io_get_cqring(ctx);
602 if (cqe) {
603 WRITE_ONCE(cqe->user_data, ki_user_data);
604 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600605 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700606 } else {
Jens Axboe498ccd92019-10-25 10:04:25 -0600607 WRITE_ONCE(ctx->rings->cq_overflow,
608 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe2b188cc2019-01-07 10:46:33 -0700609 }
610}
611
Jens Axboe8c838782019-03-12 15:48:16 -0600612static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
613{
614 if (waitqueue_active(&ctx->wait))
615 wake_up(&ctx->wait);
616 if (waitqueue_active(&ctx->sqo_wait))
617 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600618 if (ctx->cq_ev_fd)
619 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600620}
621
622static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600623 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700624{
625 unsigned long flags;
626
627 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600628 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700629 io_commit_cqring(ctx);
630 spin_unlock_irqrestore(&ctx->completion_lock, flags);
631
Jens Axboe8c838782019-03-12 15:48:16 -0600632 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700633}
634
Jens Axboe2579f912019-01-09 09:10:43 -0700635static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
636 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700637{
Jens Axboefd6fab22019-03-14 16:30:06 -0600638 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700639 struct io_kiocb *req;
640
641 if (!percpu_ref_tryget(&ctx->refs))
642 return NULL;
643
Jens Axboe2579f912019-01-09 09:10:43 -0700644 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600645 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700646 if (unlikely(!req))
647 goto out;
648 } else if (!state->free_reqs) {
649 size_t sz;
650 int ret;
651
652 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600653 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
654
655 /*
656 * Bulk alloc is all-or-nothing. If we fail to get a batch,
657 * retry single alloc to be on the safe side.
658 */
659 if (unlikely(ret <= 0)) {
660 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
661 if (!state->reqs[0])
662 goto out;
663 ret = 1;
664 }
Jens Axboe2579f912019-01-09 09:10:43 -0700665 state->free_reqs = ret - 1;
666 state->cur_req = 1;
667 req = state->reqs[0];
668 } else {
669 req = state->reqs[state->cur_req];
670 state->free_reqs--;
671 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700672 }
673
Jens Axboe60c112b2019-06-21 10:20:18 -0600674 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700675 req->ctx = ctx;
676 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600677 /* one is dropped after submission, the other at completion */
678 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600679 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600680 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700681 return req;
682out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300683 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700684 return NULL;
685}
686
Jens Axboedef596e2019-01-09 08:59:42 -0700687static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
688{
689 if (*nr) {
690 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300691 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700692 *nr = 0;
693 }
694}
695
Jens Axboe9e645e112019-05-10 16:07:28 -0600696static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700697{
Jens Axboefcb323c2019-10-24 12:39:47 -0600698 struct io_ring_ctx *ctx = req->ctx;
699
Jens Axboe09bb8392019-03-13 12:39:28 -0600700 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
701 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600702 if (req->flags & REQ_F_INFLIGHT) {
703 unsigned long flags;
704
705 spin_lock_irqsave(&ctx->inflight_lock, flags);
706 list_del(&req->inflight_entry);
707 if (waitqueue_active(&ctx->inflight_wait))
708 wake_up(&ctx->inflight_wait);
709 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
710 }
711 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600712 kmem_cache_free(req_cachep, req);
713}
714
Jens Axboeba816ad2019-09-28 11:36:45 -0600715static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600716{
717 struct io_kiocb *nxt;
718
719 /*
720 * The list should never be empty when we are called here. But could
721 * potentially happen if the chain is messed up, check to be on the
722 * safe side.
723 */
724 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
725 if (nxt) {
726 list_del(&nxt->list);
727 if (!list_empty(&req->link_list)) {
728 INIT_LIST_HEAD(&nxt->link_list);
729 list_splice(&req->link_list, &nxt->link_list);
730 nxt->flags |= REQ_F_LINK;
731 }
732
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
Pavel Begunkov267bc902019-11-07 01:41:08 +03001158static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001159{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001160 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001161 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001162 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001163 unsigned ioprio;
1164 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001165
Jens Axboe09bb8392019-03-13 12:39:28 -06001166 if (!req->file)
1167 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001168
Jens Axboe491381ce2019-10-17 09:20:46 -06001169 if (S_ISREG(file_inode(req->file)->i_mode))
1170 req->flags |= REQ_F_ISREG;
1171
1172 /*
1173 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1174 * we know to async punt it even if it was opened O_NONBLOCK
1175 */
1176 if (force_nonblock && !io_file_supports_async(req->file)) {
1177 req->flags |= REQ_F_MUST_PUNT;
1178 return -EAGAIN;
1179 }
Jens Axboe6b063142019-01-10 22:13:58 -07001180
Jens Axboe2b188cc2019-01-07 10:46:33 -07001181 kiocb->ki_pos = READ_ONCE(sqe->off);
1182 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1183 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1184
1185 ioprio = READ_ONCE(sqe->ioprio);
1186 if (ioprio) {
1187 ret = ioprio_check_cap(ioprio);
1188 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001189 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001190
1191 kiocb->ki_ioprio = ioprio;
1192 } else
1193 kiocb->ki_ioprio = get_current_ioprio();
1194
1195 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1196 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001197 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001198
1199 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001200 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1201 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001202 req->flags |= REQ_F_NOWAIT;
1203
1204 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001205 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001206
Jens Axboedef596e2019-01-09 08:59:42 -07001207 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001208 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1209 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001210 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001211
Jens Axboedef596e2019-01-09 08:59:42 -07001212 kiocb->ki_flags |= IOCB_HIPRI;
1213 kiocb->ki_complete = io_complete_rw_iopoll;
1214 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001215 if (kiocb->ki_flags & IOCB_HIPRI)
1216 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001217 kiocb->ki_complete = io_complete_rw;
1218 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001219 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001220}
1221
1222static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1223{
1224 switch (ret) {
1225 case -EIOCBQUEUED:
1226 break;
1227 case -ERESTARTSYS:
1228 case -ERESTARTNOINTR:
1229 case -ERESTARTNOHAND:
1230 case -ERESTART_RESTARTBLOCK:
1231 /*
1232 * We can't just restart the syscall, since previously
1233 * submitted sqes may already be in progress. Just fail this
1234 * IO with EINTR.
1235 */
1236 ret = -EINTR;
1237 /* fall through */
1238 default:
1239 kiocb->ki_complete(kiocb, ret, 0);
1240 }
1241}
1242
Jens Axboeba816ad2019-09-28 11:36:45 -06001243static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1244 bool in_async)
1245{
1246 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1247 *nxt = __io_complete_rw(kiocb, ret);
1248 else
1249 io_rw_done(kiocb, ret);
1250}
1251
Jens Axboeedafcce2019-01-09 09:16:05 -07001252static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1253 const struct io_uring_sqe *sqe,
1254 struct iov_iter *iter)
1255{
1256 size_t len = READ_ONCE(sqe->len);
1257 struct io_mapped_ubuf *imu;
1258 unsigned index, buf_index;
1259 size_t offset;
1260 u64 buf_addr;
1261
1262 /* attempt to use fixed buffers without having provided iovecs */
1263 if (unlikely(!ctx->user_bufs))
1264 return -EFAULT;
1265
1266 buf_index = READ_ONCE(sqe->buf_index);
1267 if (unlikely(buf_index >= ctx->nr_user_bufs))
1268 return -EFAULT;
1269
1270 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1271 imu = &ctx->user_bufs[index];
1272 buf_addr = READ_ONCE(sqe->addr);
1273
1274 /* overflow */
1275 if (buf_addr + len < buf_addr)
1276 return -EFAULT;
1277 /* not inside the mapped region */
1278 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1279 return -EFAULT;
1280
1281 /*
1282 * May not be a start of buffer, set size appropriately
1283 * and advance us to the beginning.
1284 */
1285 offset = buf_addr - imu->ubuf;
1286 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001287
1288 if (offset) {
1289 /*
1290 * Don't use iov_iter_advance() here, as it's really slow for
1291 * using the latter parts of a big fixed buffer - it iterates
1292 * over each segment manually. We can cheat a bit here, because
1293 * we know that:
1294 *
1295 * 1) it's a BVEC iter, we set it up
1296 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1297 * first and last bvec
1298 *
1299 * So just find our index, and adjust the iterator afterwards.
1300 * If the offset is within the first bvec (or the whole first
1301 * bvec, just use iov_iter_advance(). This makes it easier
1302 * since we can just skip the first segment, which may not
1303 * be PAGE_SIZE aligned.
1304 */
1305 const struct bio_vec *bvec = imu->bvec;
1306
1307 if (offset <= bvec->bv_len) {
1308 iov_iter_advance(iter, offset);
1309 } else {
1310 unsigned long seg_skip;
1311
1312 /* skip first vec */
1313 offset -= bvec->bv_len;
1314 seg_skip = 1 + (offset >> PAGE_SHIFT);
1315
1316 iter->bvec = bvec + seg_skip;
1317 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001318 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001319 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001320 }
1321 }
1322
Jens Axboeedafcce2019-01-09 09:16:05 -07001323 return 0;
1324}
1325
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001326static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1327 const struct sqe_submit *s, struct iovec **iovec,
1328 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001329{
1330 const struct io_uring_sqe *sqe = s->sqe;
1331 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1332 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001333 u8 opcode;
1334
1335 /*
1336 * We're reading ->opcode for the second time, but the first read
1337 * doesn't care whether it's _FIXED or not, so it doesn't matter
1338 * whether ->opcode changes concurrently. The first read does care
1339 * about whether it is a READ or a WRITE, so we don't trust this read
1340 * for that purpose and instead let the caller pass in the read/write
1341 * flag.
1342 */
1343 opcode = READ_ONCE(sqe->opcode);
1344 if (opcode == IORING_OP_READ_FIXED ||
1345 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001346 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001347 *iovec = NULL;
1348 return ret;
1349 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001350
1351 if (!s->has_user)
1352 return -EFAULT;
1353
1354#ifdef CONFIG_COMPAT
1355 if (ctx->compat)
1356 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1357 iovec, iter);
1358#endif
1359
1360 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1361}
1362
Jens Axboe32960612019-09-23 11:05:34 -06001363/*
1364 * For files that don't have ->read_iter() and ->write_iter(), handle them
1365 * by looping over ->read() or ->write() manually.
1366 */
1367static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1368 struct iov_iter *iter)
1369{
1370 ssize_t ret = 0;
1371
1372 /*
1373 * Don't support polled IO through this interface, and we can't
1374 * support non-blocking either. For the latter, this just causes
1375 * the kiocb to be handled from an async context.
1376 */
1377 if (kiocb->ki_flags & IOCB_HIPRI)
1378 return -EOPNOTSUPP;
1379 if (kiocb->ki_flags & IOCB_NOWAIT)
1380 return -EAGAIN;
1381
1382 while (iov_iter_count(iter)) {
1383 struct iovec iovec = iov_iter_iovec(iter);
1384 ssize_t nr;
1385
1386 if (rw == READ) {
1387 nr = file->f_op->read(file, iovec.iov_base,
1388 iovec.iov_len, &kiocb->ki_pos);
1389 } else {
1390 nr = file->f_op->write(file, iovec.iov_base,
1391 iovec.iov_len, &kiocb->ki_pos);
1392 }
1393
1394 if (nr < 0) {
1395 if (!ret)
1396 ret = nr;
1397 break;
1398 }
1399 ret += nr;
1400 if (nr != iovec.iov_len)
1401 break;
1402 iov_iter_advance(iter, nr);
1403 }
1404
1405 return ret;
1406}
1407
Pavel Begunkov267bc902019-11-07 01:41:08 +03001408static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1409 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001410{
1411 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1412 struct kiocb *kiocb = &req->rw;
1413 struct iov_iter iter;
1414 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001415 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001416 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001417
Pavel Begunkov267bc902019-11-07 01:41:08 +03001418 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001419 if (ret)
1420 return ret;
1421 file = kiocb->ki_filp;
1422
Jens Axboe2b188cc2019-01-07 10:46:33 -07001423 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001424 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001425
Pavel Begunkov267bc902019-11-07 01:41:08 +03001426 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001427 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001428 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001429
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001430 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001431 if (req->flags & REQ_F_LINK)
1432 req->result = read_size;
1433
Jens Axboe31b51512019-01-18 22:56:34 -07001434 iov_count = iov_iter_count(&iter);
1435 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001436 if (!ret) {
1437 ssize_t ret2;
1438
Jens Axboe32960612019-09-23 11:05:34 -06001439 if (file->f_op->read_iter)
1440 ret2 = call_read_iter(file, kiocb, &iter);
1441 else
1442 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1443
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001444 /*
1445 * In case of a short read, punt to async. This can happen
1446 * if we have data partially cached. Alternatively we can
1447 * return the short read, in which case the application will
1448 * need to issue another SQE and wait for it. That SQE will
1449 * need async punt anyway, so it's more efficient to do it
1450 * here.
1451 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001452 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1453 (req->flags & REQ_F_ISREG) &&
1454 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001455 ret2 = -EAGAIN;
1456 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001457 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001458 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001459 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001460 ret = -EAGAIN;
1461 }
1462 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001463 return ret;
1464}
1465
Pavel Begunkov267bc902019-11-07 01:41:08 +03001466static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1467 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001468{
1469 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1470 struct kiocb *kiocb = &req->rw;
1471 struct iov_iter iter;
1472 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001473 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001474 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001475
Pavel Begunkov267bc902019-11-07 01:41:08 +03001476 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001477 if (ret)
1478 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001479
Jens Axboe2b188cc2019-01-07 10:46:33 -07001480 file = kiocb->ki_filp;
1481 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001482 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001483
Pavel Begunkov267bc902019-11-07 01:41:08 +03001484 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001485 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001486 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001487
Jens Axboe9e645e112019-05-10 16:07:28 -06001488 if (req->flags & REQ_F_LINK)
1489 req->result = ret;
1490
Jens Axboe31b51512019-01-18 22:56:34 -07001491 iov_count = iov_iter_count(&iter);
1492
1493 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001494 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001495 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001496
1497 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001498 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001499 ssize_t ret2;
1500
Jens Axboe2b188cc2019-01-07 10:46:33 -07001501 /*
1502 * Open-code file_start_write here to grab freeze protection,
1503 * which will be released by another thread in
1504 * io_complete_rw(). Fool lockdep by telling it the lock got
1505 * released so that it doesn't complain about the held lock when
1506 * we return to userspace.
1507 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001508 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001509 __sb_start_write(file_inode(file)->i_sb,
1510 SB_FREEZE_WRITE, true);
1511 __sb_writers_release(file_inode(file)->i_sb,
1512 SB_FREEZE_WRITE);
1513 }
1514 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001515
Jens Axboe32960612019-09-23 11:05:34 -06001516 if (file->f_op->write_iter)
1517 ret2 = call_write_iter(file, kiocb, &iter);
1518 else
1519 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001520 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001521 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001522 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001523 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001524 }
Jens Axboe31b51512019-01-18 22:56:34 -07001525out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001526 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001527 return ret;
1528}
1529
1530/*
1531 * IORING_OP_NOP just posts a completion event, nothing else.
1532 */
1533static int io_nop(struct io_kiocb *req, u64 user_data)
1534{
1535 struct io_ring_ctx *ctx = req->ctx;
1536 long err = 0;
1537
Jens Axboedef596e2019-01-09 08:59:42 -07001538 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1539 return -EINVAL;
1540
Jens Axboec71ffb62019-05-13 20:58:29 -06001541 io_cqring_add_event(ctx, user_data, err);
Jens Axboeba816ad2019-09-28 11:36:45 -06001542 io_put_req(req, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001543 return 0;
1544}
1545
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001546static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1547{
Jens Axboe6b063142019-01-10 22:13:58 -07001548 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001549
Jens Axboe09bb8392019-03-13 12:39:28 -06001550 if (!req->file)
1551 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001552
Jens Axboe6b063142019-01-10 22:13:58 -07001553 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001554 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001555 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001556 return -EINVAL;
1557
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001558 return 0;
1559}
1560
1561static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001562 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001563{
1564 loff_t sqe_off = READ_ONCE(sqe->off);
1565 loff_t sqe_len = READ_ONCE(sqe->len);
1566 loff_t end = sqe_off + sqe_len;
1567 unsigned fsync_flags;
1568 int ret;
1569
1570 fsync_flags = READ_ONCE(sqe->fsync_flags);
1571 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1572 return -EINVAL;
1573
1574 ret = io_prep_fsync(req, sqe);
1575 if (ret)
1576 return ret;
1577
1578 /* fsync always requires a blocking context */
1579 if (force_nonblock)
1580 return -EAGAIN;
1581
1582 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1583 end > 0 ? end : LLONG_MAX,
1584 fsync_flags & IORING_FSYNC_DATASYNC);
1585
Jens Axboe9e645e112019-05-10 16:07:28 -06001586 if (ret < 0 && (req->flags & REQ_F_LINK))
1587 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001588 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001589 io_put_req(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001590 return 0;
1591}
1592
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001593static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1594{
1595 struct io_ring_ctx *ctx = req->ctx;
1596 int ret = 0;
1597
1598 if (!req->file)
1599 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001600
1601 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1602 return -EINVAL;
1603 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1604 return -EINVAL;
1605
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001606 return ret;
1607}
1608
1609static int io_sync_file_range(struct io_kiocb *req,
1610 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001611 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001612 bool force_nonblock)
1613{
1614 loff_t sqe_off;
1615 loff_t sqe_len;
1616 unsigned flags;
1617 int ret;
1618
1619 ret = io_prep_sfr(req, sqe);
1620 if (ret)
1621 return ret;
1622
1623 /* sync_file_range always requires a blocking context */
1624 if (force_nonblock)
1625 return -EAGAIN;
1626
1627 sqe_off = READ_ONCE(sqe->off);
1628 sqe_len = READ_ONCE(sqe->len);
1629 flags = READ_ONCE(sqe->sync_range_flags);
1630
1631 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1632
Jens Axboe9e645e112019-05-10 16:07:28 -06001633 if (ret < 0 && (req->flags & REQ_F_LINK))
1634 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001635 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001636 io_put_req(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001637 return 0;
1638}
1639
Jens Axboe0fa03c62019-04-19 13:34:07 -06001640#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001641static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001642 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001643 long (*fn)(struct socket *, struct user_msghdr __user *,
1644 unsigned int))
1645{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001646 struct socket *sock;
1647 int ret;
1648
1649 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1650 return -EINVAL;
1651
1652 sock = sock_from_file(req->file, &ret);
1653 if (sock) {
1654 struct user_msghdr __user *msg;
1655 unsigned flags;
1656
1657 flags = READ_ONCE(sqe->msg_flags);
1658 if (flags & MSG_DONTWAIT)
1659 req->flags |= REQ_F_NOWAIT;
1660 else if (force_nonblock)
1661 flags |= MSG_DONTWAIT;
1662
1663 msg = (struct user_msghdr __user *) (unsigned long)
1664 READ_ONCE(sqe->addr);
1665
Jens Axboeaa1fa282019-04-19 13:38:09 -06001666 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001667 if (force_nonblock && ret == -EAGAIN)
1668 return ret;
1669 }
1670
1671 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001672 if (ret < 0 && (req->flags & REQ_F_LINK))
1673 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06001674 io_put_req(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001675 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001676}
1677#endif
1678
1679static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001680 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001681{
1682#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001683 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1684 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001685#else
1686 return -EOPNOTSUPP;
1687#endif
1688}
1689
1690static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001691 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001692{
1693#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001694 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1695 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001696#else
1697 return -EOPNOTSUPP;
1698#endif
1699}
1700
Jens Axboe17f2fe32019-10-17 14:42:58 -06001701static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1702 struct io_kiocb **nxt, bool force_nonblock)
1703{
1704#if defined(CONFIG_NET)
1705 struct sockaddr __user *addr;
1706 int __user *addr_len;
1707 unsigned file_flags;
1708 int flags, ret;
1709
1710 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1711 return -EINVAL;
1712 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1713 return -EINVAL;
1714
1715 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1716 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1717 flags = READ_ONCE(sqe->accept_flags);
1718 file_flags = force_nonblock ? O_NONBLOCK : 0;
1719
1720 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1721 if (ret == -EAGAIN && force_nonblock) {
1722 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1723 return -EAGAIN;
1724 }
1725 if (ret < 0 && (req->flags & REQ_F_LINK))
1726 req->flags |= REQ_F_FAIL_LINK;
1727 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1728 io_put_req(req, nxt);
1729 return 0;
1730#else
1731 return -EOPNOTSUPP;
1732#endif
1733}
1734
Jens Axboe221c5eb2019-01-17 09:41:58 -07001735static void io_poll_remove_one(struct io_kiocb *req)
1736{
1737 struct io_poll_iocb *poll = &req->poll;
1738
1739 spin_lock(&poll->head->lock);
1740 WRITE_ONCE(poll->canceled, true);
1741 if (!list_empty(&poll->wait.entry)) {
1742 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001743 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001744 }
1745 spin_unlock(&poll->head->lock);
1746
1747 list_del_init(&req->list);
1748}
1749
1750static void io_poll_remove_all(struct io_ring_ctx *ctx)
1751{
1752 struct io_kiocb *req;
1753
1754 spin_lock_irq(&ctx->completion_lock);
1755 while (!list_empty(&ctx->cancel_list)) {
1756 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1757 io_poll_remove_one(req);
1758 }
1759 spin_unlock_irq(&ctx->completion_lock);
1760}
1761
1762/*
1763 * Find a running poll command that matches one specified in sqe->addr,
1764 * and remove it if found.
1765 */
1766static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1767{
1768 struct io_ring_ctx *ctx = req->ctx;
1769 struct io_kiocb *poll_req, *next;
1770 int ret = -ENOENT;
1771
1772 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1773 return -EINVAL;
1774 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1775 sqe->poll_events)
1776 return -EINVAL;
1777
1778 spin_lock_irq(&ctx->completion_lock);
1779 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1780 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1781 io_poll_remove_one(poll_req);
1782 ret = 0;
1783 break;
1784 }
1785 }
1786 spin_unlock_irq(&ctx->completion_lock);
1787
Jens Axboec71ffb62019-05-13 20:58:29 -06001788 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001789 if (ret < 0 && (req->flags & REQ_F_LINK))
1790 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06001791 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001792 return 0;
1793}
1794
Jens Axboe8c838782019-03-12 15:48:16 -06001795static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1796 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001797{
Jens Axboe8c838782019-03-12 15:48:16 -06001798 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001799 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001800 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001801}
1802
Jens Axboe561fb042019-10-24 07:25:42 -06001803static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001804{
Jens Axboe561fb042019-10-24 07:25:42 -06001805 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001806 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1807 struct io_poll_iocb *poll = &req->poll;
1808 struct poll_table_struct pt = { ._key = poll->events };
1809 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07001810 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001811 __poll_t mask = 0;
1812
Jens Axboe561fb042019-10-24 07:25:42 -06001813 if (work->flags & IO_WQ_WORK_CANCEL)
1814 WRITE_ONCE(poll->canceled, true);
1815
Jens Axboe221c5eb2019-01-17 09:41:58 -07001816 if (!READ_ONCE(poll->canceled))
1817 mask = vfs_poll(poll->file, &pt) & poll->events;
1818
1819 /*
1820 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1821 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1822 * synchronize with them. In the cancellation case the list_del_init
1823 * itself is not actually needed, but harmless so we keep it in to
1824 * avoid further branches in the fast path.
1825 */
1826 spin_lock_irq(&ctx->completion_lock);
1827 if (!mask && !READ_ONCE(poll->canceled)) {
1828 add_wait_queue(poll->head, &poll->wait);
1829 spin_unlock_irq(&ctx->completion_lock);
1830 return;
1831 }
1832 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001833 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001834 spin_unlock_irq(&ctx->completion_lock);
1835
Jens Axboe8c838782019-03-12 15:48:16 -06001836 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07001837
1838 io_put_req(req, &nxt);
1839 if (nxt)
1840 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001841}
1842
1843static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1844 void *key)
1845{
1846 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1847 wait);
1848 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1849 struct io_ring_ctx *ctx = req->ctx;
1850 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001851 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001852
1853 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001854 if (mask && !(mask & poll->events))
1855 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001856
1857 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001858
1859 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1860 list_del(&req->list);
1861 io_poll_complete(ctx, req, mask);
1862 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1863
1864 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001865 io_put_req(req, NULL);
Jens Axboe8c838782019-03-12 15:48:16 -06001866 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001867 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001868 }
1869
Jens Axboe221c5eb2019-01-17 09:41:58 -07001870 return 1;
1871}
1872
1873struct io_poll_table {
1874 struct poll_table_struct pt;
1875 struct io_kiocb *req;
1876 int error;
1877};
1878
1879static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1880 struct poll_table_struct *p)
1881{
1882 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1883
1884 if (unlikely(pt->req->poll.head)) {
1885 pt->error = -EINVAL;
1886 return;
1887 }
1888
1889 pt->error = 0;
1890 pt->req->poll.head = head;
1891 add_wait_queue(head, &pt->req->poll.wait);
1892}
1893
Jens Axboe89723d02019-11-05 15:32:58 -07001894static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1895 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001896{
1897 struct io_poll_iocb *poll = &req->poll;
1898 struct io_ring_ctx *ctx = req->ctx;
1899 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001900 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001901 __poll_t mask;
1902 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001903
1904 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1905 return -EINVAL;
1906 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1907 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001908 if (!poll->file)
1909 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001910
Jens Axboe6cc47d12019-09-18 11:18:23 -06001911 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06001912 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001913 events = READ_ONCE(sqe->poll_events);
1914 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1915
Jens Axboe221c5eb2019-01-17 09:41:58 -07001916 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001917 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001918 poll->canceled = false;
1919
1920 ipt.pt._qproc = io_poll_queue_proc;
1921 ipt.pt._key = poll->events;
1922 ipt.req = req;
1923 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1924
1925 /* initialized the list so that we can do list_empty checks */
1926 INIT_LIST_HEAD(&poll->wait.entry);
1927 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1928
Jens Axboe36703242019-07-25 10:20:18 -06001929 INIT_LIST_HEAD(&req->list);
1930
Jens Axboe221c5eb2019-01-17 09:41:58 -07001931 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001932
1933 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001934 if (likely(poll->head)) {
1935 spin_lock(&poll->head->lock);
1936 if (unlikely(list_empty(&poll->wait.entry))) {
1937 if (ipt.error)
1938 cancel = true;
1939 ipt.error = 0;
1940 mask = 0;
1941 }
1942 if (mask || ipt.error)
1943 list_del_init(&poll->wait.entry);
1944 else if (cancel)
1945 WRITE_ONCE(poll->canceled, true);
1946 else if (!poll->done) /* actually waiting for an event */
1947 list_add_tail(&req->list, &ctx->cancel_list);
1948 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001949 }
Jens Axboe8c838782019-03-12 15:48:16 -06001950 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001951 ipt.error = 0;
1952 io_poll_complete(ctx, req, mask);
1953 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001954 spin_unlock_irq(&ctx->completion_lock);
1955
Jens Axboe8c838782019-03-12 15:48:16 -06001956 if (mask) {
1957 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07001958 io_put_req(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001959 }
Jens Axboe8c838782019-03-12 15:48:16 -06001960 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001961}
1962
Jens Axboe5262f562019-09-17 12:26:57 -06001963static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1964{
1965 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06001966 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06001967 unsigned long flags;
1968
1969 req = container_of(timer, struct io_kiocb, timeout.timer);
1970 ctx = req->ctx;
1971 atomic_inc(&ctx->cq_timeouts);
1972
1973 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08001974 /*
Jens Axboe11365042019-10-16 09:08:32 -06001975 * We could be racing with timeout deletion. If the list is empty,
1976 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08001977 */
Jens Axboe842f9612019-10-29 12:34:10 -06001978 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06001979 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06001980
Jens Axboe11365042019-10-16 09:08:32 -06001981 /*
1982 * Adjust the reqs sequence before the current one because it
1983 * will consume a slot in the cq_ring and the the cq_tail
1984 * pointer will be increased, otherwise other timeout reqs may
1985 * return in advance without waiting for enough wait_nr.
1986 */
1987 prev = req;
1988 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
1989 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06001990 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06001991 }
Jens Axboe842f9612019-10-29 12:34:10 -06001992
1993 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1994 io_commit_cqring(ctx);
Jens Axboe5262f562019-09-17 12:26:57 -06001995 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1996
Jens Axboe842f9612019-10-29 12:34:10 -06001997 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07001998 if (req->flags & REQ_F_LINK)
1999 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe842f9612019-10-29 12:34:10 -06002000 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002001 return HRTIMER_NORESTART;
2002}
2003
2004/*
2005 * Remove or update an existing timeout command
2006 */
2007static int io_timeout_remove(struct io_kiocb *req,
2008 const struct io_uring_sqe *sqe)
2009{
2010 struct io_ring_ctx *ctx = req->ctx;
2011 struct io_kiocb *treq;
2012 int ret = -ENOENT;
2013 __u64 user_data;
2014 unsigned flags;
2015
2016 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2017 return -EINVAL;
2018 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2019 return -EINVAL;
2020 flags = READ_ONCE(sqe->timeout_flags);
2021 if (flags)
2022 return -EINVAL;
2023
2024 user_data = READ_ONCE(sqe->addr);
2025 spin_lock_irq(&ctx->completion_lock);
2026 list_for_each_entry(treq, &ctx->timeout_list, list) {
2027 if (user_data == treq->user_data) {
2028 list_del_init(&treq->list);
2029 ret = 0;
2030 break;
2031 }
2032 }
2033
2034 /* didn't find timeout */
2035 if (ret) {
2036fill_ev:
2037 io_cqring_fill_event(ctx, req->user_data, ret);
2038 io_commit_cqring(ctx);
2039 spin_unlock_irq(&ctx->completion_lock);
2040 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002041 if (req->flags & REQ_F_LINK)
2042 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe11365042019-10-16 09:08:32 -06002043 io_put_req(req, NULL);
2044 return 0;
2045 }
2046
2047 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
2048 if (ret == -1) {
2049 ret = -EBUSY;
2050 goto fill_ev;
2051 }
2052
2053 io_cqring_fill_event(ctx, req->user_data, 0);
2054 io_cqring_fill_event(ctx, treq->user_data, -ECANCELED);
2055 io_commit_cqring(ctx);
2056 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002057 io_cqring_ev_posted(ctx);
2058
Jens Axboe11365042019-10-16 09:08:32 -06002059 io_put_req(treq, NULL);
Jens Axboeba816ad2019-09-28 11:36:45 -06002060 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002061 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002062}
2063
2064static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2065{
yangerkun5da0fb12019-10-15 21:59:29 +08002066 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002067 struct io_ring_ctx *ctx = req->ctx;
2068 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002069 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002070 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002071 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002072 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002073
2074 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2075 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002076 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2077 return -EINVAL;
2078 flags = READ_ONCE(sqe->timeout_flags);
2079 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002080 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002081
2082 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002083 return -EFAULT;
2084
Jens Axboe11365042019-10-16 09:08:32 -06002085 if (flags & IORING_TIMEOUT_ABS)
2086 mode = HRTIMER_MODE_ABS;
2087 else
2088 mode = HRTIMER_MODE_REL;
2089
2090 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2091
Jens Axboe5262f562019-09-17 12:26:57 -06002092 /*
2093 * sqe->off holds how many events that need to occur for this
2094 * timeout event to be satisfied.
2095 */
2096 count = READ_ONCE(sqe->off);
2097 if (!count)
2098 count = 1;
2099
2100 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002101 /* reuse it to store the count */
2102 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002103 req->flags |= REQ_F_TIMEOUT;
2104
2105 /*
2106 * Insertion sort, ensuring the first entry in the list is always
2107 * the one we need first.
2108 */
Jens Axboe5262f562019-09-17 12:26:57 -06002109 spin_lock_irq(&ctx->completion_lock);
2110 list_for_each_prev(entry, &ctx->timeout_list) {
2111 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002112 unsigned nxt_sq_head;
2113 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002114
yangerkun5da0fb12019-10-15 21:59:29 +08002115 /*
2116 * Since cached_sq_head + count - 1 can overflow, use type long
2117 * long to store it.
2118 */
2119 tmp = (long long)ctx->cached_sq_head + count - 1;
2120 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2121 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2122
2123 /*
2124 * cached_sq_head may overflow, and it will never overflow twice
2125 * once there is some timeout req still be valid.
2126 */
2127 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002128 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002129
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002130 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002131 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002132
2133 /*
2134 * Sequence of reqs after the insert one and itself should
2135 * be adjusted because each timeout req consumes a slot.
2136 */
2137 span++;
2138 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002139 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002140 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002141 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002142 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002143 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002144 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002145 return 0;
2146}
2147
Jens Axboe62755e32019-10-28 21:49:21 -06002148static bool io_cancel_cb(struct io_wq_work *work, void *data)
2149{
2150 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2151
2152 return req->user_data == (unsigned long) data;
2153}
2154
Jens Axboee977d6d2019-11-05 12:39:45 -07002155static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002156{
Jens Axboe62755e32019-10-28 21:49:21 -06002157 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002158 int ret = 0;
2159
Jens Axboe62755e32019-10-28 21:49:21 -06002160 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2161 switch (cancel_ret) {
2162 case IO_WQ_CANCEL_OK:
2163 ret = 0;
2164 break;
2165 case IO_WQ_CANCEL_RUNNING:
2166 ret = -EALREADY;
2167 break;
2168 case IO_WQ_CANCEL_NOTFOUND:
2169 ret = -ENOENT;
2170 break;
2171 }
2172
Jens Axboee977d6d2019-11-05 12:39:45 -07002173 return ret;
2174}
2175
2176static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2177 struct io_kiocb **nxt)
2178{
2179 struct io_ring_ctx *ctx = req->ctx;
2180 void *sqe_addr;
2181 int ret;
2182
2183 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2184 return -EINVAL;
2185 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2186 sqe->cancel_flags)
2187 return -EINVAL;
2188
2189 sqe_addr = (void *) (unsigned long) READ_ONCE(sqe->addr);
2190 ret = io_async_cancel_one(ctx, sqe_addr);
2191
Jens Axboe62755e32019-10-28 21:49:21 -06002192 if (ret < 0 && (req->flags & REQ_F_LINK))
2193 req->flags |= REQ_F_FAIL_LINK;
2194 io_cqring_add_event(req->ctx, sqe->user_data, ret);
2195 io_put_req(req, nxt);
2196 return 0;
2197}
2198
Pavel Begunkov267bc902019-11-07 01:41:08 +03002199static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002200{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002201 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002202 struct io_uring_sqe *sqe_copy;
2203
2204 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2205 return 0;
2206
2207 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2208 if (!sqe_copy)
2209 return -EAGAIN;
2210
2211 spin_lock_irq(&ctx->completion_lock);
2212 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2213 spin_unlock_irq(&ctx->completion_lock);
2214 kfree(sqe_copy);
2215 return 0;
2216 }
2217
2218 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2219 req->submit.sqe = sqe_copy;
2220
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002221 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002222 list_add_tail(&req->list, &ctx->defer_list);
2223 spin_unlock_irq(&ctx->completion_lock);
2224 return -EIOCBQUEUED;
2225}
2226
Jens Axboe2b188cc2019-01-07 10:46:33 -07002227static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002228 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002229{
Jens Axboee0c5c572019-03-12 10:18:47 -06002230 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002231 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002232
Jens Axboe9e645e112019-05-10 16:07:28 -06002233 req->user_data = READ_ONCE(s->sqe->user_data);
2234
Jens Axboe2b188cc2019-01-07 10:46:33 -07002235 opcode = READ_ONCE(s->sqe->opcode);
2236 switch (opcode) {
2237 case IORING_OP_NOP:
2238 ret = io_nop(req, req->user_data);
2239 break;
2240 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002241 if (unlikely(s->sqe->buf_index))
2242 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002243 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002244 break;
2245 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002246 if (unlikely(s->sqe->buf_index))
2247 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002248 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002249 break;
2250 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002251 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002252 break;
2253 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002254 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002255 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002256 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002257 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002258 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002259 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002260 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002261 break;
2262 case IORING_OP_POLL_REMOVE:
2263 ret = io_poll_remove(req, s->sqe);
2264 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002265 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002266 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002267 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002268 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002269 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002270 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002271 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002272 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002273 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002274 case IORING_OP_TIMEOUT:
2275 ret = io_timeout(req, s->sqe);
2276 break;
Jens Axboe11365042019-10-16 09:08:32 -06002277 case IORING_OP_TIMEOUT_REMOVE:
2278 ret = io_timeout_remove(req, s->sqe);
2279 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002280 case IORING_OP_ACCEPT:
2281 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2282 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002283 case IORING_OP_ASYNC_CANCEL:
2284 ret = io_async_cancel(req, s->sqe, nxt);
2285 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002286 default:
2287 ret = -EINVAL;
2288 break;
2289 }
2290
Jens Axboedef596e2019-01-09 08:59:42 -07002291 if (ret)
2292 return ret;
2293
2294 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002295 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002296 return -EAGAIN;
2297
2298 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002299 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002300 mutex_lock(&ctx->uring_lock);
2301 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002302 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002303 mutex_unlock(&ctx->uring_lock);
2304 }
2305
2306 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002307}
2308
Jens Axboe561fb042019-10-24 07:25:42 -06002309static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002310{
Jens Axboe561fb042019-10-24 07:25:42 -06002311 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002312 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002313 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -06002314 struct sqe_submit *s = &req->submit;
2315 const struct io_uring_sqe *sqe = s->sqe;
2316 struct io_kiocb *nxt = NULL;
2317 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002318
Jens Axboe561fb042019-10-24 07:25:42 -06002319 /* Ensure we clear previously set non-block flag */
2320 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321
Jens Axboe561fb042019-10-24 07:25:42 -06002322 if (work->flags & IO_WQ_WORK_CANCEL)
2323 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002324
Jens Axboe561fb042019-10-24 07:25:42 -06002325 if (!ret) {
2326 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2327 s->in_async = true;
2328 do {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002329 ret = __io_submit_sqe(ctx, req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002330 /*
2331 * We can get EAGAIN for polled IO even though we're
2332 * forcing a sync submission from here, since we can't
2333 * wait for request slots on the block side.
2334 */
2335 if (ret != -EAGAIN)
2336 break;
2337 cond_resched();
2338 } while (1);
2339 }
Jens Axboe31b51512019-01-18 22:56:34 -07002340
Jens Axboe561fb042019-10-24 07:25:42 -06002341 /* drop submission reference */
2342 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002343
Jens Axboe561fb042019-10-24 07:25:42 -06002344 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002345 if (req->flags & REQ_F_LINK)
2346 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe561fb042019-10-24 07:25:42 -06002347 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002348 io_put_req(req, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07002349 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002350
Jens Axboe561fb042019-10-24 07:25:42 -06002351 /* async context always use a copy of the sqe */
2352 kfree(sqe);
2353
2354 /* if a dependent link is ready, pass it back */
2355 if (!ret && nxt) {
2356 io_prep_async_work(nxt);
2357 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002358 }
Jens Axboe31b51512019-01-18 22:56:34 -07002359}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002360
Jens Axboe09bb8392019-03-13 12:39:28 -06002361static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2362{
2363 int op = READ_ONCE(sqe->opcode);
2364
2365 switch (op) {
2366 case IORING_OP_NOP:
2367 case IORING_OP_POLL_REMOVE:
2368 return false;
2369 default:
2370 return true;
2371 }
2372}
2373
Jens Axboe65e19f52019-10-26 07:20:21 -06002374static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2375 int index)
2376{
2377 struct fixed_file_table *table;
2378
2379 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2380 return table->files[index & IORING_FILE_TABLE_MASK];
2381}
2382
Pavel Begunkov267bc902019-11-07 01:41:08 +03002383static int io_req_set_file(struct io_ring_ctx *ctx,
Jens Axboe09bb8392019-03-13 12:39:28 -06002384 struct io_submit_state *state, struct io_kiocb *req)
2385{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002386 struct sqe_submit *s = &req->submit;
Jens Axboe09bb8392019-03-13 12:39:28 -06002387 unsigned flags;
2388 int fd;
2389
2390 flags = READ_ONCE(s->sqe->flags);
2391 fd = READ_ONCE(s->sqe->fd);
2392
Jackie Liu4fe2c962019-09-09 20:50:40 +08002393 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002394 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002395 /*
2396 * All io need record the previous position, if LINK vs DARIN,
2397 * it can be used to mark the position of the first IO in the
2398 * link list.
2399 */
2400 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002401
Jens Axboe60c112b2019-06-21 10:20:18 -06002402 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002403 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002404
2405 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002406 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002407 (unsigned) fd >= ctx->nr_user_files))
2408 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002409 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002410 req->file = io_file_from_index(ctx, fd);
2411 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002412 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002413 req->flags |= REQ_F_FIXED_FILE;
2414 } else {
2415 if (s->needs_fixed_file)
2416 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002417 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002418 req->file = io_file_get(state, fd);
2419 if (unlikely(!req->file))
2420 return -EBADF;
2421 }
2422
2423 return 0;
2424}
2425
Jens Axboefcb323c2019-10-24 12:39:47 -06002426static int io_grab_files(struct io_ring_ctx *ctx, struct io_kiocb *req)
2427{
2428 int ret = -EBADF;
2429
2430 rcu_read_lock();
2431 spin_lock_irq(&ctx->inflight_lock);
2432 /*
2433 * We use the f_ops->flush() handler to ensure that we can flush
2434 * out work accessing these files if the fd is closed. Check if
2435 * the fd has changed since we started down this path, and disallow
2436 * this operation if it has.
2437 */
2438 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2439 list_add(&req->inflight_entry, &ctx->inflight_list);
2440 req->flags |= REQ_F_INFLIGHT;
2441 req->work.files = current->files;
2442 ret = 0;
2443 }
2444 spin_unlock_irq(&ctx->inflight_lock);
2445 rcu_read_unlock();
2446
2447 return ret;
2448}
2449
Pavel Begunkov267bc902019-11-07 01:41:08 +03002450static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002451{
Jens Axboee0c5c572019-03-12 10:18:47 -06002452 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002453
Pavel Begunkov267bc902019-11-07 01:41:08 +03002454 ret = __io_submit_sqe(ctx, req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002455
2456 /*
2457 * We async punt it if the file wasn't marked NOWAIT, or if the file
2458 * doesn't support non-blocking read/write attempts
2459 */
2460 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2461 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002462 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002463 struct io_uring_sqe *sqe_copy;
2464
Jackie Liu954dab12019-09-18 10:37:52 +08002465 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002466 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002467 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002468 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2469 ret = io_grab_files(ctx, req);
2470 if (ret) {
2471 kfree(sqe_copy);
2472 goto err;
2473 }
2474 }
Jens Axboee65ef562019-03-12 10:16:44 -06002475
2476 /*
2477 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002478 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002479 */
Jens Axboefcb323c2019-10-24 12:39:47 -06002480 io_queue_async_work(ctx, req);
Jens Axboee65ef562019-03-12 10:16:44 -06002481 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002482 }
2483 }
Jens Axboee65ef562019-03-12 10:16:44 -06002484
2485 /* drop submission reference */
Jens Axboefcb323c2019-10-24 12:39:47 -06002486err:
Jens Axboeba816ad2019-09-28 11:36:45 -06002487 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002488
2489 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002490 if (ret) {
2491 io_cqring_add_event(ctx, req->user_data, ret);
2492 if (req->flags & REQ_F_LINK)
2493 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002494 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002495 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002496
2497 return ret;
2498}
2499
Pavel Begunkov267bc902019-11-07 01:41:08 +03002500static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002501{
2502 int ret;
2503
Pavel Begunkov267bc902019-11-07 01:41:08 +03002504 ret = io_req_defer(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002505 if (ret) {
2506 if (ret != -EIOCBQUEUED) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002507 io_cqring_add_event(ctx, req->submit.sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002508 io_free_req(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002509 }
2510 return 0;
2511 }
2512
Pavel Begunkov267bc902019-11-07 01:41:08 +03002513 return __io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002514}
2515
2516static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002517 struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002518{
2519 int ret;
2520 int need_submit = false;
2521
2522 if (!shadow)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002523 return io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002524
2525 /*
2526 * Mark the first IO in link list as DRAIN, let all the following
2527 * IOs enter the defer list. all IO needs to be completed before link
2528 * list.
2529 */
2530 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002531 ret = io_req_defer(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002532 if (ret) {
2533 if (ret != -EIOCBQUEUED) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002534 io_cqring_add_event(ctx, req->submit.sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002535 io_free_req(req, NULL);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002536 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002537 return 0;
2538 }
2539 } else {
2540 /*
2541 * If ret == 0 means that all IOs in front of link io are
2542 * running done. let's queue link head.
2543 */
2544 need_submit = true;
2545 }
2546
2547 /* Insert shadow req to defer_list, blocking next IOs */
2548 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002549 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002550 list_add_tail(&shadow->list, &ctx->defer_list);
2551 spin_unlock_irq(&ctx->completion_lock);
2552
2553 if (need_submit)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002554 return __io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002555
2556 return 0;
2557}
2558
Jens Axboe9e645e112019-05-10 16:07:28 -06002559#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2560
Pavel Begunkov196be952019-11-07 01:41:06 +03002561static void io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002562 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002563{
2564 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002565 struct sqe_submit *s = &req->submit;
Jens Axboe9e645e112019-05-10 16:07:28 -06002566 int ret;
2567
2568 /* enforce forwards compatibility on users */
2569 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2570 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03002571 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002572 }
2573
Pavel Begunkov267bc902019-11-07 01:41:08 +03002574 ret = io_req_set_file(ctx, state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002575 if (unlikely(ret)) {
2576err_req:
Jens Axboe9e645e112019-05-10 16:07:28 -06002577 io_cqring_add_event(ctx, s->sqe->user_data, ret);
Pavel Begunkov267bc902019-11-07 01:41:08 +03002578 io_free_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002579 return;
2580 }
2581
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002582 req->user_data = s->sqe->user_data;
2583
Jens Axboe9e645e112019-05-10 16:07:28 -06002584 /*
2585 * If we already have a head request, queue this one for async
2586 * submittal once the head completes. If we don't have a head but
2587 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2588 * submitted sync once the chain is complete. If none of those
2589 * conditions are true (normal request), then just queue it.
2590 */
2591 if (*link) {
2592 struct io_kiocb *prev = *link;
2593
2594 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2595 if (!sqe_copy) {
2596 ret = -EAGAIN;
2597 goto err_req;
2598 }
2599
2600 s->sqe = sqe_copy;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002601 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002602 list_add_tail(&req->list, &prev->link_list);
2603 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2604 req->flags |= REQ_F_LINK;
2605
Jens Axboe9e645e112019-05-10 16:07:28 -06002606 INIT_LIST_HEAD(&req->link_list);
2607 *link = req;
2608 } else {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002609 io_queue_sqe(ctx, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002610 }
2611}
2612
Jens Axboe9a56a232019-01-09 09:06:50 -07002613/*
2614 * Batched submission is done, ensure local IO is flushed out.
2615 */
2616static void io_submit_state_end(struct io_submit_state *state)
2617{
2618 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002619 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002620 if (state->free_reqs)
2621 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2622 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002623}
2624
2625/*
2626 * Start submission side cache.
2627 */
2628static void io_submit_state_start(struct io_submit_state *state,
2629 struct io_ring_ctx *ctx, unsigned max_ios)
2630{
2631 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002632 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002633 state->file = NULL;
2634 state->ios_left = max_ios;
2635}
2636
Jens Axboe2b188cc2019-01-07 10:46:33 -07002637static void io_commit_sqring(struct io_ring_ctx *ctx)
2638{
Hristo Venev75b28af2019-08-26 17:23:46 +00002639 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002640
Hristo Venev75b28af2019-08-26 17:23:46 +00002641 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002642 /*
2643 * Ensure any loads from the SQEs are done at this point,
2644 * since once we write the new head, the application could
2645 * write new data to them.
2646 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002647 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002648 }
2649}
2650
2651/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002652 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2653 * that is mapped by userspace. This means that care needs to be taken to
2654 * ensure that reads are stable, as we cannot rely on userspace always
2655 * being a good citizen. If members of the sqe are validated and then later
2656 * used, it's important that those reads are done through READ_ONCE() to
2657 * prevent a re-load down the line.
2658 */
2659static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2660{
Hristo Venev75b28af2019-08-26 17:23:46 +00002661 struct io_rings *rings = ctx->rings;
2662 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002663 unsigned head;
2664
2665 /*
2666 * The cached sq head (or cq tail) serves two purposes:
2667 *
2668 * 1) allows us to batch the cost of updating the user visible
2669 * head updates.
2670 * 2) allows the kernel side to track the head on its own, even
2671 * though the application is the one updating it.
2672 */
2673 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002674 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002675 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002676 return false;
2677
Hristo Venev75b28af2019-08-26 17:23:46 +00002678 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002679 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002680 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002681 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002682 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002683 ctx->cached_sq_head++;
2684 return true;
2685 }
2686
2687 /* drop invalid entries */
2688 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002689 ctx->cached_sq_dropped++;
2690 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002691 return false;
2692}
2693
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002694static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002695 struct file *ring_file, int ring_fd,
2696 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002697{
2698 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002699 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002700 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002701 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002702 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002703
2704 if (nr > IO_PLUG_THRESHOLD) {
2705 io_submit_state_start(&state, ctx, nr);
2706 statep = &state;
2707 }
2708
2709 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002710 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03002711 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002712
Pavel Begunkov196be952019-11-07 01:41:06 +03002713 req = io_get_req(ctx, statep);
2714 if (unlikely(!req)) {
2715 if (!submitted)
2716 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002717 break;
Pavel Begunkov196be952019-11-07 01:41:06 +03002718 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03002719 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002720 __io_free_req(req);
2721 break;
2722 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002723
Pavel Begunkov50585b92019-11-07 01:41:07 +03002724 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002725 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
2726 if (!mm_fault) {
2727 use_mm(ctx->sqo_mm);
2728 *mm = ctx->sqo_mm;
2729 }
2730 }
2731
Pavel Begunkov50585b92019-11-07 01:41:07 +03002732 sqe_flags = req->submit.sqe->flags;
2733
2734 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002735 if (!shadow_req) {
2736 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002737 if (unlikely(!shadow_req))
2738 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002739 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2740 refcount_dec(&shadow_req->refs);
2741 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03002742 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002743 }
2744
Jackie Liua1041c22019-09-18 17:25:52 +08002745out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03002746 req->submit.ring_file = ring_file;
2747 req->submit.ring_fd = ring_fd;
2748 req->submit.has_user = *mm != NULL;
2749 req->submit.in_async = async;
2750 req->submit.needs_fixed_file = async;
2751 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
2752 true, async);
Pavel Begunkov267bc902019-11-07 01:41:08 +03002753 io_submit_sqe(ctx, req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002754 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03002755
2756 /*
2757 * If previous wasn't linked and we have a linked command,
2758 * that's the end of the chain. Submit the previous link.
2759 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03002760 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002761 io_queue_link_head(ctx, link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03002762 link = NULL;
2763 shadow_req = NULL;
2764 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002765 }
2766
Jens Axboe9e645e112019-05-10 16:07:28 -06002767 if (link)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002768 io_queue_link_head(ctx, link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002769 if (statep)
2770 io_submit_state_end(&state);
2771
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002772 /* Commit SQ ring head once we've consumed and submitted all SQEs */
2773 io_commit_sqring(ctx);
2774
Jens Axboe6c271ce2019-01-10 11:22:30 -07002775 return submitted;
2776}
2777
2778static int io_sq_thread(void *data)
2779{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002780 struct io_ring_ctx *ctx = data;
2781 struct mm_struct *cur_mm = NULL;
2782 mm_segment_t old_fs;
2783 DEFINE_WAIT(wait);
2784 unsigned inflight;
2785 unsigned long timeout;
2786
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002787 complete(&ctx->sqo_thread_started);
2788
Jens Axboe6c271ce2019-01-10 11:22:30 -07002789 old_fs = get_fs();
2790 set_fs(USER_DS);
2791
2792 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002793 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002794 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002795
2796 if (inflight) {
2797 unsigned nr_events = 0;
2798
2799 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002800 /*
2801 * inflight is the count of the maximum possible
2802 * entries we submitted, but it can be smaller
2803 * if we dropped some of them. If we don't have
2804 * poll entries available, then we know that we
2805 * have nothing left to poll for. Reset the
2806 * inflight count to zero in that case.
2807 */
2808 mutex_lock(&ctx->uring_lock);
2809 if (!list_empty(&ctx->poll_list))
2810 __io_iopoll_check(ctx, &nr_events, 0);
2811 else
2812 inflight = 0;
2813 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002814 } else {
2815 /*
2816 * Normal IO, just pretend everything completed.
2817 * We don't have to poll completions for that.
2818 */
2819 nr_events = inflight;
2820 }
2821
2822 inflight -= nr_events;
2823 if (!inflight)
2824 timeout = jiffies + ctx->sq_thread_idle;
2825 }
2826
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002827 to_submit = io_sqring_entries(ctx);
2828 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002829 /*
2830 * We're polling. If we're within the defined idle
2831 * period, then let us spin without work before going
2832 * to sleep.
2833 */
2834 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002835 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002836 continue;
2837 }
2838
2839 /*
2840 * Drop cur_mm before scheduling, we can't hold it for
2841 * long periods (or over schedule()). Do this before
2842 * adding ourselves to the waitqueue, as the unuse/drop
2843 * may sleep.
2844 */
2845 if (cur_mm) {
2846 unuse_mm(cur_mm);
2847 mmput(cur_mm);
2848 cur_mm = NULL;
2849 }
2850
2851 prepare_to_wait(&ctx->sqo_wait, &wait,
2852 TASK_INTERRUPTIBLE);
2853
2854 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002855 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002856 /* make sure to read SQ tail after writing flags */
2857 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002858
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002859 to_submit = io_sqring_entries(ctx);
2860 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002861 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002862 finish_wait(&ctx->sqo_wait, &wait);
2863 break;
2864 }
2865 if (signal_pending(current))
2866 flush_signals(current);
2867 schedule();
2868 finish_wait(&ctx->sqo_wait, &wait);
2869
Hristo Venev75b28af2019-08-26 17:23:46 +00002870 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002871 continue;
2872 }
2873 finish_wait(&ctx->sqo_wait, &wait);
2874
Hristo Venev75b28af2019-08-26 17:23:46 +00002875 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002876 }
2877
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002878 to_submit = min(to_submit, ctx->sq_entries);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002879 inflight += io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm,
2880 true);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002881 }
2882
2883 set_fs(old_fs);
2884 if (cur_mm) {
2885 unuse_mm(cur_mm);
2886 mmput(cur_mm);
2887 }
Jens Axboe06058632019-04-13 09:26:03 -06002888
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002889 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002890
Jens Axboe6c271ce2019-01-10 11:22:30 -07002891 return 0;
2892}
2893
Jens Axboebda52162019-09-24 13:47:15 -06002894struct io_wait_queue {
2895 struct wait_queue_entry wq;
2896 struct io_ring_ctx *ctx;
2897 unsigned to_wait;
2898 unsigned nr_timeouts;
2899};
2900
2901static inline bool io_should_wake(struct io_wait_queue *iowq)
2902{
2903 struct io_ring_ctx *ctx = iowq->ctx;
2904
2905 /*
2906 * Wake up if we have enough events, or if a timeout occured since we
2907 * started waiting. For timeouts, we always want to return to userspace,
2908 * regardless of event count.
2909 */
2910 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2911 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2912}
2913
2914static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2915 int wake_flags, void *key)
2916{
2917 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
2918 wq);
2919
2920 if (!io_should_wake(iowq))
2921 return -1;
2922
2923 return autoremove_wake_function(curr, mode, wake_flags, key);
2924}
2925
Jens Axboe2b188cc2019-01-07 10:46:33 -07002926/*
2927 * Wait until events become available, if we don't already have some. The
2928 * application must reap them itself, as they reside on the shared cq ring.
2929 */
2930static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2931 const sigset_t __user *sig, size_t sigsz)
2932{
Jens Axboebda52162019-09-24 13:47:15 -06002933 struct io_wait_queue iowq = {
2934 .wq = {
2935 .private = current,
2936 .func = io_wake_function,
2937 .entry = LIST_HEAD_INIT(iowq.wq.entry),
2938 },
2939 .ctx = ctx,
2940 .to_wait = min_events,
2941 };
Hristo Venev75b28af2019-08-26 17:23:46 +00002942 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08002943 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002944
Hristo Venev75b28af2019-08-26 17:23:46 +00002945 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002946 return 0;
2947
2948 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002949#ifdef CONFIG_COMPAT
2950 if (in_compat_syscall())
2951 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002952 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002953 else
2954#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002955 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002956
Jens Axboe2b188cc2019-01-07 10:46:33 -07002957 if (ret)
2958 return ret;
2959 }
2960
Jens Axboebda52162019-09-24 13:47:15 -06002961 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002962 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06002963 do {
2964 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
2965 TASK_INTERRUPTIBLE);
2966 if (io_should_wake(&iowq))
2967 break;
2968 schedule();
2969 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08002970 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06002971 break;
2972 }
2973 } while (1);
2974 finish_wait(&ctx->wait, &iowq.wq);
2975
Jackie Liue9ffa5c2019-10-29 11:16:42 +08002976 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002977
Hristo Venev75b28af2019-08-26 17:23:46 +00002978 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002979}
2980
Jens Axboe6b063142019-01-10 22:13:58 -07002981static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2982{
2983#if defined(CONFIG_UNIX)
2984 if (ctx->ring_sock) {
2985 struct sock *sock = ctx->ring_sock->sk;
2986 struct sk_buff *skb;
2987
2988 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2989 kfree_skb(skb);
2990 }
2991#else
2992 int i;
2993
Jens Axboe65e19f52019-10-26 07:20:21 -06002994 for (i = 0; i < ctx->nr_user_files; i++) {
2995 struct file *file;
2996
2997 file = io_file_from_index(ctx, i);
2998 if (file)
2999 fput(file);
3000 }
Jens Axboe6b063142019-01-10 22:13:58 -07003001#endif
3002}
3003
3004static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3005{
Jens Axboe65e19f52019-10-26 07:20:21 -06003006 unsigned nr_tables, i;
3007
3008 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003009 return -ENXIO;
3010
3011 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003012 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3013 for (i = 0; i < nr_tables; i++)
3014 kfree(ctx->file_table[i].files);
3015 kfree(ctx->file_table);
3016 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003017 ctx->nr_user_files = 0;
3018 return 0;
3019}
3020
Jens Axboe6c271ce2019-01-10 11:22:30 -07003021static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3022{
3023 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003024 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003025 /*
3026 * The park is a bit of a work-around, without it we get
3027 * warning spews on shutdown with SQPOLL set and affinity
3028 * set to a single CPU.
3029 */
Jens Axboe06058632019-04-13 09:26:03 -06003030 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003031 kthread_stop(ctx->sqo_thread);
3032 ctx->sqo_thread = NULL;
3033 }
3034}
3035
Jens Axboe6b063142019-01-10 22:13:58 -07003036static void io_finish_async(struct io_ring_ctx *ctx)
3037{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003038 io_sq_thread_stop(ctx);
3039
Jens Axboe561fb042019-10-24 07:25:42 -06003040 if (ctx->io_wq) {
3041 io_wq_destroy(ctx->io_wq);
3042 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003043 }
3044}
3045
3046#if defined(CONFIG_UNIX)
3047static void io_destruct_skb(struct sk_buff *skb)
3048{
3049 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3050
Jens Axboe561fb042019-10-24 07:25:42 -06003051 if (ctx->io_wq)
3052 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003053
Jens Axboe6b063142019-01-10 22:13:58 -07003054 unix_destruct_scm(skb);
3055}
3056
3057/*
3058 * Ensure the UNIX gc is aware of our file set, so we are certain that
3059 * the io_uring can be safely unregistered on process exit, even if we have
3060 * loops in the file referencing.
3061 */
3062static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3063{
3064 struct sock *sk = ctx->ring_sock->sk;
3065 struct scm_fp_list *fpl;
3066 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003067 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003068
3069 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3070 unsigned long inflight = ctx->user->unix_inflight + nr;
3071
3072 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3073 return -EMFILE;
3074 }
3075
3076 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3077 if (!fpl)
3078 return -ENOMEM;
3079
3080 skb = alloc_skb(0, GFP_KERNEL);
3081 if (!skb) {
3082 kfree(fpl);
3083 return -ENOMEM;
3084 }
3085
3086 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003087
Jens Axboe08a45172019-10-03 08:11:03 -06003088 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003089 fpl->user = get_uid(ctx->user);
3090 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003091 struct file *file = io_file_from_index(ctx, i + offset);
3092
3093 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003094 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003095 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003096 unix_inflight(fpl->user, fpl->fp[nr_files]);
3097 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003098 }
3099
Jens Axboe08a45172019-10-03 08:11:03 -06003100 if (nr_files) {
3101 fpl->max = SCM_MAX_FD;
3102 fpl->count = nr_files;
3103 UNIXCB(skb).fp = fpl;
3104 skb->destructor = io_destruct_skb;
3105 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3106 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003107
Jens Axboe08a45172019-10-03 08:11:03 -06003108 for (i = 0; i < nr_files; i++)
3109 fput(fpl->fp[i]);
3110 } else {
3111 kfree_skb(skb);
3112 kfree(fpl);
3113 }
Jens Axboe6b063142019-01-10 22:13:58 -07003114
3115 return 0;
3116}
3117
3118/*
3119 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3120 * causes regular reference counting to break down. We rely on the UNIX
3121 * garbage collection to take care of this problem for us.
3122 */
3123static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3124{
3125 unsigned left, total;
3126 int ret = 0;
3127
3128 total = 0;
3129 left = ctx->nr_user_files;
3130 while (left) {
3131 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003132
3133 ret = __io_sqe_files_scm(ctx, this_files, total);
3134 if (ret)
3135 break;
3136 left -= this_files;
3137 total += this_files;
3138 }
3139
3140 if (!ret)
3141 return 0;
3142
3143 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003144 struct file *file = io_file_from_index(ctx, total);
3145
3146 if (file)
3147 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003148 total++;
3149 }
3150
3151 return ret;
3152}
3153#else
3154static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3155{
3156 return 0;
3157}
3158#endif
3159
Jens Axboe65e19f52019-10-26 07:20:21 -06003160static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3161 unsigned nr_files)
3162{
3163 int i;
3164
3165 for (i = 0; i < nr_tables; i++) {
3166 struct fixed_file_table *table = &ctx->file_table[i];
3167 unsigned this_files;
3168
3169 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3170 table->files = kcalloc(this_files, sizeof(struct file *),
3171 GFP_KERNEL);
3172 if (!table->files)
3173 break;
3174 nr_files -= this_files;
3175 }
3176
3177 if (i == nr_tables)
3178 return 0;
3179
3180 for (i = 0; i < nr_tables; i++) {
3181 struct fixed_file_table *table = &ctx->file_table[i];
3182 kfree(table->files);
3183 }
3184 return 1;
3185}
3186
Jens Axboe6b063142019-01-10 22:13:58 -07003187static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3188 unsigned nr_args)
3189{
3190 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003191 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003192 int fd, ret = 0;
3193 unsigned i;
3194
Jens Axboe65e19f52019-10-26 07:20:21 -06003195 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003196 return -EBUSY;
3197 if (!nr_args)
3198 return -EINVAL;
3199 if (nr_args > IORING_MAX_FIXED_FILES)
3200 return -EMFILE;
3201
Jens Axboe65e19f52019-10-26 07:20:21 -06003202 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3203 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3204 GFP_KERNEL);
3205 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003206 return -ENOMEM;
3207
Jens Axboe65e19f52019-10-26 07:20:21 -06003208 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3209 kfree(ctx->file_table);
3210 return -ENOMEM;
3211 }
3212
Jens Axboe08a45172019-10-03 08:11:03 -06003213 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003214 struct fixed_file_table *table;
3215 unsigned index;
3216
Jens Axboe6b063142019-01-10 22:13:58 -07003217 ret = -EFAULT;
3218 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3219 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003220 /* allow sparse sets */
3221 if (fd == -1) {
3222 ret = 0;
3223 continue;
3224 }
Jens Axboe6b063142019-01-10 22:13:58 -07003225
Jens Axboe65e19f52019-10-26 07:20:21 -06003226 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3227 index = i & IORING_FILE_TABLE_MASK;
3228 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003229
3230 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003231 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003232 break;
3233 /*
3234 * Don't allow io_uring instances to be registered. If UNIX
3235 * isn't enabled, then this causes a reference cycle and this
3236 * instance can never get freed. If UNIX is enabled we'll
3237 * handle it just fine, but there's still no point in allowing
3238 * a ring fd as it doesn't support regular read/write anyway.
3239 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003240 if (table->files[index]->f_op == &io_uring_fops) {
3241 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003242 break;
3243 }
Jens Axboe6b063142019-01-10 22:13:58 -07003244 ret = 0;
3245 }
3246
3247 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003248 for (i = 0; i < ctx->nr_user_files; i++) {
3249 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003250
Jens Axboe65e19f52019-10-26 07:20:21 -06003251 file = io_file_from_index(ctx, i);
3252 if (file)
3253 fput(file);
3254 }
3255 for (i = 0; i < nr_tables; i++)
3256 kfree(ctx->file_table[i].files);
3257
3258 kfree(ctx->file_table);
3259 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003260 ctx->nr_user_files = 0;
3261 return ret;
3262 }
3263
3264 ret = io_sqe_files_scm(ctx);
3265 if (ret)
3266 io_sqe_files_unregister(ctx);
3267
3268 return ret;
3269}
3270
Jens Axboec3a31e62019-10-03 13:59:56 -06003271static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3272{
3273#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003274 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003275 struct sock *sock = ctx->ring_sock->sk;
3276 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3277 struct sk_buff *skb;
3278 int i;
3279
3280 __skb_queue_head_init(&list);
3281
3282 /*
3283 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3284 * remove this entry and rearrange the file array.
3285 */
3286 skb = skb_dequeue(head);
3287 while (skb) {
3288 struct scm_fp_list *fp;
3289
3290 fp = UNIXCB(skb).fp;
3291 for (i = 0; i < fp->count; i++) {
3292 int left;
3293
3294 if (fp->fp[i] != file)
3295 continue;
3296
3297 unix_notinflight(fp->user, fp->fp[i]);
3298 left = fp->count - 1 - i;
3299 if (left) {
3300 memmove(&fp->fp[i], &fp->fp[i + 1],
3301 left * sizeof(struct file *));
3302 }
3303 fp->count--;
3304 if (!fp->count) {
3305 kfree_skb(skb);
3306 skb = NULL;
3307 } else {
3308 __skb_queue_tail(&list, skb);
3309 }
3310 fput(file);
3311 file = NULL;
3312 break;
3313 }
3314
3315 if (!file)
3316 break;
3317
3318 __skb_queue_tail(&list, skb);
3319
3320 skb = skb_dequeue(head);
3321 }
3322
3323 if (skb_peek(&list)) {
3324 spin_lock_irq(&head->lock);
3325 while ((skb = __skb_dequeue(&list)) != NULL)
3326 __skb_queue_tail(head, skb);
3327 spin_unlock_irq(&head->lock);
3328 }
3329#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003330 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003331#endif
3332}
3333
3334static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3335 int index)
3336{
3337#if defined(CONFIG_UNIX)
3338 struct sock *sock = ctx->ring_sock->sk;
3339 struct sk_buff_head *head = &sock->sk_receive_queue;
3340 struct sk_buff *skb;
3341
3342 /*
3343 * See if we can merge this file into an existing skb SCM_RIGHTS
3344 * file set. If there's no room, fall back to allocating a new skb
3345 * and filling it in.
3346 */
3347 spin_lock_irq(&head->lock);
3348 skb = skb_peek(head);
3349 if (skb) {
3350 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3351
3352 if (fpl->count < SCM_MAX_FD) {
3353 __skb_unlink(skb, head);
3354 spin_unlock_irq(&head->lock);
3355 fpl->fp[fpl->count] = get_file(file);
3356 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3357 fpl->count++;
3358 spin_lock_irq(&head->lock);
3359 __skb_queue_head(head, skb);
3360 } else {
3361 skb = NULL;
3362 }
3363 }
3364 spin_unlock_irq(&head->lock);
3365
3366 if (skb) {
3367 fput(file);
3368 return 0;
3369 }
3370
3371 return __io_sqe_files_scm(ctx, 1, index);
3372#else
3373 return 0;
3374#endif
3375}
3376
3377static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3378 unsigned nr_args)
3379{
3380 struct io_uring_files_update up;
3381 __s32 __user *fds;
3382 int fd, i, err;
3383 __u32 done;
3384
Jens Axboe65e19f52019-10-26 07:20:21 -06003385 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003386 return -ENXIO;
3387 if (!nr_args)
3388 return -EINVAL;
3389 if (copy_from_user(&up, arg, sizeof(up)))
3390 return -EFAULT;
3391 if (check_add_overflow(up.offset, nr_args, &done))
3392 return -EOVERFLOW;
3393 if (done > ctx->nr_user_files)
3394 return -EINVAL;
3395
3396 done = 0;
3397 fds = (__s32 __user *) up.fds;
3398 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003399 struct fixed_file_table *table;
3400 unsigned index;
3401
Jens Axboec3a31e62019-10-03 13:59:56 -06003402 err = 0;
3403 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3404 err = -EFAULT;
3405 break;
3406 }
3407 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003408 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3409 index = i & IORING_FILE_TABLE_MASK;
3410 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003411 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003412 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003413 }
3414 if (fd != -1) {
3415 struct file *file;
3416
3417 file = fget(fd);
3418 if (!file) {
3419 err = -EBADF;
3420 break;
3421 }
3422 /*
3423 * Don't allow io_uring instances to be registered. If
3424 * UNIX isn't enabled, then this causes a reference
3425 * cycle and this instance can never get freed. If UNIX
3426 * is enabled we'll handle it just fine, but there's
3427 * still no point in allowing a ring fd as it doesn't
3428 * support regular read/write anyway.
3429 */
3430 if (file->f_op == &io_uring_fops) {
3431 fput(file);
3432 err = -EBADF;
3433 break;
3434 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003435 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003436 err = io_sqe_file_register(ctx, file, i);
3437 if (err)
3438 break;
3439 }
3440 nr_args--;
3441 done++;
3442 up.offset++;
3443 }
3444
3445 return done ? done : err;
3446}
3447
Jens Axboe6c271ce2019-01-10 11:22:30 -07003448static int io_sq_offload_start(struct io_ring_ctx *ctx,
3449 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003450{
Jens Axboe561fb042019-10-24 07:25:42 -06003451 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003452 int ret;
3453
Jens Axboe6c271ce2019-01-10 11:22:30 -07003454 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003455 mmgrab(current->mm);
3456 ctx->sqo_mm = current->mm;
3457
Jens Axboe6c271ce2019-01-10 11:22:30 -07003458 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003459 ret = -EPERM;
3460 if (!capable(CAP_SYS_ADMIN))
3461 goto err;
3462
Jens Axboe917257d2019-04-13 09:28:55 -06003463 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3464 if (!ctx->sq_thread_idle)
3465 ctx->sq_thread_idle = HZ;
3466
Jens Axboe6c271ce2019-01-10 11:22:30 -07003467 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003468 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003469
Jens Axboe917257d2019-04-13 09:28:55 -06003470 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003471 if (cpu >= nr_cpu_ids)
3472 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003473 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003474 goto err;
3475
Jens Axboe6c271ce2019-01-10 11:22:30 -07003476 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3477 ctx, cpu,
3478 "io_uring-sq");
3479 } else {
3480 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3481 "io_uring-sq");
3482 }
3483 if (IS_ERR(ctx->sqo_thread)) {
3484 ret = PTR_ERR(ctx->sqo_thread);
3485 ctx->sqo_thread = NULL;
3486 goto err;
3487 }
3488 wake_up_process(ctx->sqo_thread);
3489 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3490 /* Can't have SQ_AFF without SQPOLL */
3491 ret = -EINVAL;
3492 goto err;
3493 }
3494
Jens Axboe561fb042019-10-24 07:25:42 -06003495 /* Do QD, or 4 * CPUS, whatever is smallest */
3496 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
3497 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm);
Jens Axboe975c99a52019-10-30 08:42:56 -06003498 if (IS_ERR(ctx->io_wq)) {
3499 ret = PTR_ERR(ctx->io_wq);
3500 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003501 goto err;
3502 }
3503
3504 return 0;
3505err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003506 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003507 mmdrop(ctx->sqo_mm);
3508 ctx->sqo_mm = NULL;
3509 return ret;
3510}
3511
3512static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3513{
3514 atomic_long_sub(nr_pages, &user->locked_vm);
3515}
3516
3517static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3518{
3519 unsigned long page_limit, cur_pages, new_pages;
3520
3521 /* Don't allow more pages than we can safely lock */
3522 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3523
3524 do {
3525 cur_pages = atomic_long_read(&user->locked_vm);
3526 new_pages = cur_pages + nr_pages;
3527 if (new_pages > page_limit)
3528 return -ENOMEM;
3529 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3530 new_pages) != cur_pages);
3531
3532 return 0;
3533}
3534
3535static void io_mem_free(void *ptr)
3536{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003537 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003538
Mark Rutland52e04ef2019-04-30 17:30:21 +01003539 if (!ptr)
3540 return;
3541
3542 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003543 if (put_page_testzero(page))
3544 free_compound_page(page);
3545}
3546
3547static void *io_mem_alloc(size_t size)
3548{
3549 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3550 __GFP_NORETRY;
3551
3552 return (void *) __get_free_pages(gfp_flags, get_order(size));
3553}
3554
Hristo Venev75b28af2019-08-26 17:23:46 +00003555static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3556 size_t *sq_offset)
3557{
3558 struct io_rings *rings;
3559 size_t off, sq_array_size;
3560
3561 off = struct_size(rings, cqes, cq_entries);
3562 if (off == SIZE_MAX)
3563 return SIZE_MAX;
3564
3565#ifdef CONFIG_SMP
3566 off = ALIGN(off, SMP_CACHE_BYTES);
3567 if (off == 0)
3568 return SIZE_MAX;
3569#endif
3570
3571 sq_array_size = array_size(sizeof(u32), sq_entries);
3572 if (sq_array_size == SIZE_MAX)
3573 return SIZE_MAX;
3574
3575 if (check_add_overflow(off, sq_array_size, &off))
3576 return SIZE_MAX;
3577
3578 if (sq_offset)
3579 *sq_offset = off;
3580
3581 return off;
3582}
3583
Jens Axboe2b188cc2019-01-07 10:46:33 -07003584static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3585{
Hristo Venev75b28af2019-08-26 17:23:46 +00003586 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003587
Hristo Venev75b28af2019-08-26 17:23:46 +00003588 pages = (size_t)1 << get_order(
3589 rings_size(sq_entries, cq_entries, NULL));
3590 pages += (size_t)1 << get_order(
3591 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003592
Hristo Venev75b28af2019-08-26 17:23:46 +00003593 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003594}
3595
Jens Axboeedafcce2019-01-09 09:16:05 -07003596static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3597{
3598 int i, j;
3599
3600 if (!ctx->user_bufs)
3601 return -ENXIO;
3602
3603 for (i = 0; i < ctx->nr_user_bufs; i++) {
3604 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3605
3606 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003607 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003608
3609 if (ctx->account_mem)
3610 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003611 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003612 imu->nr_bvecs = 0;
3613 }
3614
3615 kfree(ctx->user_bufs);
3616 ctx->user_bufs = NULL;
3617 ctx->nr_user_bufs = 0;
3618 return 0;
3619}
3620
3621static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3622 void __user *arg, unsigned index)
3623{
3624 struct iovec __user *src;
3625
3626#ifdef CONFIG_COMPAT
3627 if (ctx->compat) {
3628 struct compat_iovec __user *ciovs;
3629 struct compat_iovec ciov;
3630
3631 ciovs = (struct compat_iovec __user *) arg;
3632 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3633 return -EFAULT;
3634
3635 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3636 dst->iov_len = ciov.iov_len;
3637 return 0;
3638 }
3639#endif
3640 src = (struct iovec __user *) arg;
3641 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3642 return -EFAULT;
3643 return 0;
3644}
3645
3646static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3647 unsigned nr_args)
3648{
3649 struct vm_area_struct **vmas = NULL;
3650 struct page **pages = NULL;
3651 int i, j, got_pages = 0;
3652 int ret = -EINVAL;
3653
3654 if (ctx->user_bufs)
3655 return -EBUSY;
3656 if (!nr_args || nr_args > UIO_MAXIOV)
3657 return -EINVAL;
3658
3659 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3660 GFP_KERNEL);
3661 if (!ctx->user_bufs)
3662 return -ENOMEM;
3663
3664 for (i = 0; i < nr_args; i++) {
3665 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3666 unsigned long off, start, end, ubuf;
3667 int pret, nr_pages;
3668 struct iovec iov;
3669 size_t size;
3670
3671 ret = io_copy_iov(ctx, &iov, arg, i);
3672 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003673 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003674
3675 /*
3676 * Don't impose further limits on the size and buffer
3677 * constraints here, we'll -EINVAL later when IO is
3678 * submitted if they are wrong.
3679 */
3680 ret = -EFAULT;
3681 if (!iov.iov_base || !iov.iov_len)
3682 goto err;
3683
3684 /* arbitrary limit, but we need something */
3685 if (iov.iov_len > SZ_1G)
3686 goto err;
3687
3688 ubuf = (unsigned long) iov.iov_base;
3689 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3690 start = ubuf >> PAGE_SHIFT;
3691 nr_pages = end - start;
3692
3693 if (ctx->account_mem) {
3694 ret = io_account_mem(ctx->user, nr_pages);
3695 if (ret)
3696 goto err;
3697 }
3698
3699 ret = 0;
3700 if (!pages || nr_pages > got_pages) {
3701 kfree(vmas);
3702 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003703 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003704 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003705 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003706 sizeof(struct vm_area_struct *),
3707 GFP_KERNEL);
3708 if (!pages || !vmas) {
3709 ret = -ENOMEM;
3710 if (ctx->account_mem)
3711 io_unaccount_mem(ctx->user, nr_pages);
3712 goto err;
3713 }
3714 got_pages = nr_pages;
3715 }
3716
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003717 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003718 GFP_KERNEL);
3719 ret = -ENOMEM;
3720 if (!imu->bvec) {
3721 if (ctx->account_mem)
3722 io_unaccount_mem(ctx->user, nr_pages);
3723 goto err;
3724 }
3725
3726 ret = 0;
3727 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003728 pret = get_user_pages(ubuf, nr_pages,
3729 FOLL_WRITE | FOLL_LONGTERM,
3730 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003731 if (pret == nr_pages) {
3732 /* don't support file backed memory */
3733 for (j = 0; j < nr_pages; j++) {
3734 struct vm_area_struct *vma = vmas[j];
3735
3736 if (vma->vm_file &&
3737 !is_file_hugepages(vma->vm_file)) {
3738 ret = -EOPNOTSUPP;
3739 break;
3740 }
3741 }
3742 } else {
3743 ret = pret < 0 ? pret : -EFAULT;
3744 }
3745 up_read(&current->mm->mmap_sem);
3746 if (ret) {
3747 /*
3748 * if we did partial map, or found file backed vmas,
3749 * release any pages we did get
3750 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003751 if (pret > 0)
3752 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003753 if (ctx->account_mem)
3754 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003755 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003756 goto err;
3757 }
3758
3759 off = ubuf & ~PAGE_MASK;
3760 size = iov.iov_len;
3761 for (j = 0; j < nr_pages; j++) {
3762 size_t vec_len;
3763
3764 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3765 imu->bvec[j].bv_page = pages[j];
3766 imu->bvec[j].bv_len = vec_len;
3767 imu->bvec[j].bv_offset = off;
3768 off = 0;
3769 size -= vec_len;
3770 }
3771 /* store original address for later verification */
3772 imu->ubuf = ubuf;
3773 imu->len = iov.iov_len;
3774 imu->nr_bvecs = nr_pages;
3775
3776 ctx->nr_user_bufs++;
3777 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003778 kvfree(pages);
3779 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003780 return 0;
3781err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003782 kvfree(pages);
3783 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003784 io_sqe_buffer_unregister(ctx);
3785 return ret;
3786}
3787
Jens Axboe9b402842019-04-11 11:45:41 -06003788static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3789{
3790 __s32 __user *fds = arg;
3791 int fd;
3792
3793 if (ctx->cq_ev_fd)
3794 return -EBUSY;
3795
3796 if (copy_from_user(&fd, fds, sizeof(*fds)))
3797 return -EFAULT;
3798
3799 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3800 if (IS_ERR(ctx->cq_ev_fd)) {
3801 int ret = PTR_ERR(ctx->cq_ev_fd);
3802 ctx->cq_ev_fd = NULL;
3803 return ret;
3804 }
3805
3806 return 0;
3807}
3808
3809static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3810{
3811 if (ctx->cq_ev_fd) {
3812 eventfd_ctx_put(ctx->cq_ev_fd);
3813 ctx->cq_ev_fd = NULL;
3814 return 0;
3815 }
3816
3817 return -ENXIO;
3818}
3819
Jens Axboe2b188cc2019-01-07 10:46:33 -07003820static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3821{
Jens Axboe6b063142019-01-10 22:13:58 -07003822 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003823 if (ctx->sqo_mm)
3824 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003825
3826 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003827 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003828 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003829 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003830
Jens Axboe2b188cc2019-01-07 10:46:33 -07003831#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003832 if (ctx->ring_sock) {
3833 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003834 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003835 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003836#endif
3837
Hristo Venev75b28af2019-08-26 17:23:46 +00003838 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003839 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003840
3841 percpu_ref_exit(&ctx->refs);
3842 if (ctx->account_mem)
3843 io_unaccount_mem(ctx->user,
3844 ring_pages(ctx->sq_entries, ctx->cq_entries));
3845 free_uid(ctx->user);
3846 kfree(ctx);
3847}
3848
3849static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3850{
3851 struct io_ring_ctx *ctx = file->private_data;
3852 __poll_t mask = 0;
3853
3854 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003855 /*
3856 * synchronizes with barrier from wq_has_sleeper call in
3857 * io_commit_cqring
3858 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003859 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003860 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3861 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003862 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003863 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003864 mask |= EPOLLIN | EPOLLRDNORM;
3865
3866 return mask;
3867}
3868
3869static int io_uring_fasync(int fd, struct file *file, int on)
3870{
3871 struct io_ring_ctx *ctx = file->private_data;
3872
3873 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3874}
3875
3876static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3877{
3878 mutex_lock(&ctx->uring_lock);
3879 percpu_ref_kill(&ctx->refs);
3880 mutex_unlock(&ctx->uring_lock);
3881
Jens Axboe5262f562019-09-17 12:26:57 -06003882 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003883 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06003884
3885 if (ctx->io_wq)
3886 io_wq_cancel_all(ctx->io_wq);
3887
Jens Axboedef596e2019-01-09 08:59:42 -07003888 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003889 wait_for_completion(&ctx->ctx_done);
3890 io_ring_ctx_free(ctx);
3891}
3892
3893static int io_uring_release(struct inode *inode, struct file *file)
3894{
3895 struct io_ring_ctx *ctx = file->private_data;
3896
3897 file->private_data = NULL;
3898 io_ring_ctx_wait_and_kill(ctx);
3899 return 0;
3900}
3901
Jens Axboefcb323c2019-10-24 12:39:47 -06003902static void io_uring_cancel_files(struct io_ring_ctx *ctx,
3903 struct files_struct *files)
3904{
3905 struct io_kiocb *req;
3906 DEFINE_WAIT(wait);
3907
3908 while (!list_empty_careful(&ctx->inflight_list)) {
3909 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
3910
3911 spin_lock_irq(&ctx->inflight_lock);
3912 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
3913 if (req->work.files == files) {
3914 ret = io_wq_cancel_work(ctx->io_wq, &req->work);
3915 break;
3916 }
3917 }
3918 if (ret == IO_WQ_CANCEL_RUNNING)
3919 prepare_to_wait(&ctx->inflight_wait, &wait,
3920 TASK_UNINTERRUPTIBLE);
3921
3922 spin_unlock_irq(&ctx->inflight_lock);
3923
3924 /*
3925 * We need to keep going until we get NOTFOUND. We only cancel
3926 * one work at the time.
3927 *
3928 * If we get CANCEL_RUNNING, then wait for a work to complete
3929 * before continuing.
3930 */
3931 if (ret == IO_WQ_CANCEL_OK)
3932 continue;
3933 else if (ret != IO_WQ_CANCEL_RUNNING)
3934 break;
3935 schedule();
3936 }
3937}
3938
3939static int io_uring_flush(struct file *file, void *data)
3940{
3941 struct io_ring_ctx *ctx = file->private_data;
3942
3943 io_uring_cancel_files(ctx, data);
3944 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
3945 io_wq_cancel_all(ctx->io_wq);
3946 return 0;
3947}
3948
Jens Axboe2b188cc2019-01-07 10:46:33 -07003949static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3950{
3951 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3952 unsigned long sz = vma->vm_end - vma->vm_start;
3953 struct io_ring_ctx *ctx = file->private_data;
3954 unsigned long pfn;
3955 struct page *page;
3956 void *ptr;
3957
3958 switch (offset) {
3959 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003960 case IORING_OFF_CQ_RING:
3961 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003962 break;
3963 case IORING_OFF_SQES:
3964 ptr = ctx->sq_sqes;
3965 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003966 default:
3967 return -EINVAL;
3968 }
3969
3970 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07003971 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003972 return -EINVAL;
3973
3974 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3975 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3976}
3977
3978SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3979 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3980 size_t, sigsz)
3981{
3982 struct io_ring_ctx *ctx;
3983 long ret = -EBADF;
3984 int submitted = 0;
3985 struct fd f;
3986
Jens Axboe6c271ce2019-01-10 11:22:30 -07003987 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003988 return -EINVAL;
3989
3990 f = fdget(fd);
3991 if (!f.file)
3992 return -EBADF;
3993
3994 ret = -EOPNOTSUPP;
3995 if (f.file->f_op != &io_uring_fops)
3996 goto out_fput;
3997
3998 ret = -ENXIO;
3999 ctx = f.file->private_data;
4000 if (!percpu_ref_tryget(&ctx->refs))
4001 goto out_fput;
4002
Jens Axboe6c271ce2019-01-10 11:22:30 -07004003 /*
4004 * For SQ polling, the thread will do all submissions and completions.
4005 * Just return the requested submit count, and wake the thread if
4006 * we were asked to.
4007 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004008 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004009 if (ctx->flags & IORING_SETUP_SQPOLL) {
4010 if (flags & IORING_ENTER_SQ_WAKEUP)
4011 wake_up(&ctx->sqo_wait);
4012 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004013 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004014 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004015
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004016 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004017 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004018 /* already have mm, so io_submit_sqes() won't try to grab it */
4019 cur_mm = ctx->sqo_mm;
4020 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4021 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004022 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004023 }
4024 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004025 unsigned nr_events = 0;
4026
Jens Axboe2b188cc2019-01-07 10:46:33 -07004027 min_complete = min(min_complete, ctx->cq_entries);
4028
Jens Axboedef596e2019-01-09 08:59:42 -07004029 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004030 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004031 } else {
4032 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4033 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004034 }
4035
Pavel Begunkov6805b322019-10-08 02:18:42 +03004036 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004037out_fput:
4038 fdput(f);
4039 return submitted ? submitted : ret;
4040}
4041
4042static const struct file_operations io_uring_fops = {
4043 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004044 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004045 .mmap = io_uring_mmap,
4046 .poll = io_uring_poll,
4047 .fasync = io_uring_fasync,
4048};
4049
4050static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4051 struct io_uring_params *p)
4052{
Hristo Venev75b28af2019-08-26 17:23:46 +00004053 struct io_rings *rings;
4054 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004055
Hristo Venev75b28af2019-08-26 17:23:46 +00004056 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4057 if (size == SIZE_MAX)
4058 return -EOVERFLOW;
4059
4060 rings = io_mem_alloc(size);
4061 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004062 return -ENOMEM;
4063
Hristo Venev75b28af2019-08-26 17:23:46 +00004064 ctx->rings = rings;
4065 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4066 rings->sq_ring_mask = p->sq_entries - 1;
4067 rings->cq_ring_mask = p->cq_entries - 1;
4068 rings->sq_ring_entries = p->sq_entries;
4069 rings->cq_ring_entries = p->cq_entries;
4070 ctx->sq_mask = rings->sq_ring_mask;
4071 ctx->cq_mask = rings->cq_ring_mask;
4072 ctx->sq_entries = rings->sq_ring_entries;
4073 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004074
4075 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4076 if (size == SIZE_MAX)
4077 return -EOVERFLOW;
4078
4079 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004080 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004081 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004082
Jens Axboe2b188cc2019-01-07 10:46:33 -07004083 return 0;
4084}
4085
4086/*
4087 * Allocate an anonymous fd, this is what constitutes the application
4088 * visible backing of an io_uring instance. The application mmaps this
4089 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4090 * we have to tie this fd to a socket for file garbage collection purposes.
4091 */
4092static int io_uring_get_fd(struct io_ring_ctx *ctx)
4093{
4094 struct file *file;
4095 int ret;
4096
4097#if defined(CONFIG_UNIX)
4098 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4099 &ctx->ring_sock);
4100 if (ret)
4101 return ret;
4102#endif
4103
4104 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4105 if (ret < 0)
4106 goto err;
4107
4108 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4109 O_RDWR | O_CLOEXEC);
4110 if (IS_ERR(file)) {
4111 put_unused_fd(ret);
4112 ret = PTR_ERR(file);
4113 goto err;
4114 }
4115
4116#if defined(CONFIG_UNIX)
4117 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004118 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004119#endif
4120 fd_install(ret, file);
4121 return ret;
4122err:
4123#if defined(CONFIG_UNIX)
4124 sock_release(ctx->ring_sock);
4125 ctx->ring_sock = NULL;
4126#endif
4127 return ret;
4128}
4129
4130static int io_uring_create(unsigned entries, struct io_uring_params *p)
4131{
4132 struct user_struct *user = NULL;
4133 struct io_ring_ctx *ctx;
4134 bool account_mem;
4135 int ret;
4136
4137 if (!entries || entries > IORING_MAX_ENTRIES)
4138 return -EINVAL;
4139
4140 /*
4141 * Use twice as many entries for the CQ ring. It's possible for the
4142 * application to drive a higher depth than the size of the SQ ring,
4143 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004144 * some flexibility in overcommitting a bit. If the application has
4145 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4146 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004147 */
4148 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004149 if (p->flags & IORING_SETUP_CQSIZE) {
4150 /*
4151 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4152 * to a power-of-two, if it isn't already. We do NOT impose
4153 * any cq vs sq ring sizing.
4154 */
4155 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4156 return -EINVAL;
4157 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4158 } else {
4159 p->cq_entries = 2 * p->sq_entries;
4160 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004161
4162 user = get_uid(current_user());
4163 account_mem = !capable(CAP_IPC_LOCK);
4164
4165 if (account_mem) {
4166 ret = io_account_mem(user,
4167 ring_pages(p->sq_entries, p->cq_entries));
4168 if (ret) {
4169 free_uid(user);
4170 return ret;
4171 }
4172 }
4173
4174 ctx = io_ring_ctx_alloc(p);
4175 if (!ctx) {
4176 if (account_mem)
4177 io_unaccount_mem(user, ring_pages(p->sq_entries,
4178 p->cq_entries));
4179 free_uid(user);
4180 return -ENOMEM;
4181 }
4182 ctx->compat = in_compat_syscall();
4183 ctx->account_mem = account_mem;
4184 ctx->user = user;
4185
4186 ret = io_allocate_scq_urings(ctx, p);
4187 if (ret)
4188 goto err;
4189
Jens Axboe6c271ce2019-01-10 11:22:30 -07004190 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004191 if (ret)
4192 goto err;
4193
Jens Axboe2b188cc2019-01-07 10:46:33 -07004194 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004195 p->sq_off.head = offsetof(struct io_rings, sq.head);
4196 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4197 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4198 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4199 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4200 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4201 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004202
4203 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004204 p->cq_off.head = offsetof(struct io_rings, cq.head);
4205 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4206 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4207 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4208 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4209 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004210
Jens Axboe044c1ab2019-10-28 09:15:33 -06004211 /*
4212 * Install ring fd as the very last thing, so we don't risk someone
4213 * having closed it before we finish setup
4214 */
4215 ret = io_uring_get_fd(ctx);
4216 if (ret < 0)
4217 goto err;
4218
Jens Axboeac90f242019-09-06 10:26:21 -06004219 p->features = IORING_FEAT_SINGLE_MMAP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004220 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004221 return ret;
4222err:
4223 io_ring_ctx_wait_and_kill(ctx);
4224 return ret;
4225}
4226
4227/*
4228 * Sets up an aio uring context, and returns the fd. Applications asks for a
4229 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4230 * params structure passed in.
4231 */
4232static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4233{
4234 struct io_uring_params p;
4235 long ret;
4236 int i;
4237
4238 if (copy_from_user(&p, params, sizeof(p)))
4239 return -EFAULT;
4240 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4241 if (p.resv[i])
4242 return -EINVAL;
4243 }
4244
Jens Axboe6c271ce2019-01-10 11:22:30 -07004245 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004246 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004247 return -EINVAL;
4248
4249 ret = io_uring_create(entries, &p);
4250 if (ret < 0)
4251 return ret;
4252
4253 if (copy_to_user(params, &p, sizeof(p)))
4254 return -EFAULT;
4255
4256 return ret;
4257}
4258
4259SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4260 struct io_uring_params __user *, params)
4261{
4262 return io_uring_setup(entries, params);
4263}
4264
Jens Axboeedafcce2019-01-09 09:16:05 -07004265static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4266 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004267 __releases(ctx->uring_lock)
4268 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004269{
4270 int ret;
4271
Jens Axboe35fa71a2019-04-22 10:23:23 -06004272 /*
4273 * We're inside the ring mutex, if the ref is already dying, then
4274 * someone else killed the ctx or is already going through
4275 * io_uring_register().
4276 */
4277 if (percpu_ref_is_dying(&ctx->refs))
4278 return -ENXIO;
4279
Jens Axboeedafcce2019-01-09 09:16:05 -07004280 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004281
4282 /*
4283 * Drop uring mutex before waiting for references to exit. If another
4284 * thread is currently inside io_uring_enter() it might need to grab
4285 * the uring_lock to make progress. If we hold it here across the drain
4286 * wait, then we can deadlock. It's safe to drop the mutex here, since
4287 * no new references will come in after we've killed the percpu ref.
4288 */
4289 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004290 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004291 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004292
4293 switch (opcode) {
4294 case IORING_REGISTER_BUFFERS:
4295 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4296 break;
4297 case IORING_UNREGISTER_BUFFERS:
4298 ret = -EINVAL;
4299 if (arg || nr_args)
4300 break;
4301 ret = io_sqe_buffer_unregister(ctx);
4302 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004303 case IORING_REGISTER_FILES:
4304 ret = io_sqe_files_register(ctx, arg, nr_args);
4305 break;
4306 case IORING_UNREGISTER_FILES:
4307 ret = -EINVAL;
4308 if (arg || nr_args)
4309 break;
4310 ret = io_sqe_files_unregister(ctx);
4311 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004312 case IORING_REGISTER_FILES_UPDATE:
4313 ret = io_sqe_files_update(ctx, arg, nr_args);
4314 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004315 case IORING_REGISTER_EVENTFD:
4316 ret = -EINVAL;
4317 if (nr_args != 1)
4318 break;
4319 ret = io_eventfd_register(ctx, arg);
4320 break;
4321 case IORING_UNREGISTER_EVENTFD:
4322 ret = -EINVAL;
4323 if (arg || nr_args)
4324 break;
4325 ret = io_eventfd_unregister(ctx);
4326 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004327 default:
4328 ret = -EINVAL;
4329 break;
4330 }
4331
4332 /* bring the ctx back to life */
4333 reinit_completion(&ctx->ctx_done);
4334 percpu_ref_reinit(&ctx->refs);
4335 return ret;
4336}
4337
4338SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4339 void __user *, arg, unsigned int, nr_args)
4340{
4341 struct io_ring_ctx *ctx;
4342 long ret = -EBADF;
4343 struct fd f;
4344
4345 f = fdget(fd);
4346 if (!f.file)
4347 return -EBADF;
4348
4349 ret = -EOPNOTSUPP;
4350 if (f.file->f_op != &io_uring_fops)
4351 goto out_fput;
4352
4353 ctx = f.file->private_data;
4354
4355 mutex_lock(&ctx->uring_lock);
4356 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4357 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004358 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4359 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004360out_fput:
4361 fdput(f);
4362 return ret;
4363}
4364
Jens Axboe2b188cc2019-01-07 10:46:33 -07004365static int __init io_uring_init(void)
4366{
4367 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4368 return 0;
4369};
4370__initcall(io_uring_init);