blob: ee33c7020828df309a28270840eba7b5974f8e3f [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
2155static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2156 struct io_kiocb **nxt)
2157{
2158 struct io_ring_ctx *ctx = req->ctx;
2159 enum io_wq_cancel cancel_ret;
2160 void *sqe_addr;
2161 int ret = 0;
2162
2163 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2164 return -EINVAL;
2165 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2166 sqe->cancel_flags)
2167 return -EINVAL;
2168
2169 sqe_addr = (void *) (unsigned long) READ_ONCE(sqe->addr);
2170 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2171 switch (cancel_ret) {
2172 case IO_WQ_CANCEL_OK:
2173 ret = 0;
2174 break;
2175 case IO_WQ_CANCEL_RUNNING:
2176 ret = -EALREADY;
2177 break;
2178 case IO_WQ_CANCEL_NOTFOUND:
2179 ret = -ENOENT;
2180 break;
2181 }
2182
2183 if (ret < 0 && (req->flags & REQ_F_LINK))
2184 req->flags |= REQ_F_FAIL_LINK;
2185 io_cqring_add_event(req->ctx, sqe->user_data, ret);
2186 io_put_req(req, nxt);
2187 return 0;
2188}
2189
Pavel Begunkov267bc902019-11-07 01:41:08 +03002190static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002191{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002192 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002193 struct io_uring_sqe *sqe_copy;
2194
2195 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2196 return 0;
2197
2198 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2199 if (!sqe_copy)
2200 return -EAGAIN;
2201
2202 spin_lock_irq(&ctx->completion_lock);
2203 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2204 spin_unlock_irq(&ctx->completion_lock);
2205 kfree(sqe_copy);
2206 return 0;
2207 }
2208
2209 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2210 req->submit.sqe = sqe_copy;
2211
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002212 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002213 list_add_tail(&req->list, &ctx->defer_list);
2214 spin_unlock_irq(&ctx->completion_lock);
2215 return -EIOCBQUEUED;
2216}
2217
Jens Axboe2b188cc2019-01-07 10:46:33 -07002218static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002219 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002220{
Jens Axboee0c5c572019-03-12 10:18:47 -06002221 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002222 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002223
Jens Axboe9e645e112019-05-10 16:07:28 -06002224 req->user_data = READ_ONCE(s->sqe->user_data);
2225
Jens Axboe2b188cc2019-01-07 10:46:33 -07002226 opcode = READ_ONCE(s->sqe->opcode);
2227 switch (opcode) {
2228 case IORING_OP_NOP:
2229 ret = io_nop(req, req->user_data);
2230 break;
2231 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002232 if (unlikely(s->sqe->buf_index))
2233 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002234 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002235 break;
2236 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002237 if (unlikely(s->sqe->buf_index))
2238 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002239 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002240 break;
2241 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002242 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002243 break;
2244 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002245 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002246 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002247 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002248 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002249 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002250 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002251 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002252 break;
2253 case IORING_OP_POLL_REMOVE:
2254 ret = io_poll_remove(req, s->sqe);
2255 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002256 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002257 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002258 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002259 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002260 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002261 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002262 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002263 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002264 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002265 case IORING_OP_TIMEOUT:
2266 ret = io_timeout(req, s->sqe);
2267 break;
Jens Axboe11365042019-10-16 09:08:32 -06002268 case IORING_OP_TIMEOUT_REMOVE:
2269 ret = io_timeout_remove(req, s->sqe);
2270 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002271 case IORING_OP_ACCEPT:
2272 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2273 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002274 case IORING_OP_ASYNC_CANCEL:
2275 ret = io_async_cancel(req, s->sqe, nxt);
2276 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002277 default:
2278 ret = -EINVAL;
2279 break;
2280 }
2281
Jens Axboedef596e2019-01-09 08:59:42 -07002282 if (ret)
2283 return ret;
2284
2285 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002286 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002287 return -EAGAIN;
2288
2289 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002290 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002291 mutex_lock(&ctx->uring_lock);
2292 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002293 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002294 mutex_unlock(&ctx->uring_lock);
2295 }
2296
2297 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002298}
2299
Jens Axboe561fb042019-10-24 07:25:42 -06002300static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002301{
Jens Axboe561fb042019-10-24 07:25:42 -06002302 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002303 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002304 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -06002305 struct sqe_submit *s = &req->submit;
2306 const struct io_uring_sqe *sqe = s->sqe;
2307 struct io_kiocb *nxt = NULL;
2308 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002309
Jens Axboe561fb042019-10-24 07:25:42 -06002310 /* Ensure we clear previously set non-block flag */
2311 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002312
Jens Axboe561fb042019-10-24 07:25:42 -06002313 if (work->flags & IO_WQ_WORK_CANCEL)
2314 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002315
Jens Axboe561fb042019-10-24 07:25:42 -06002316 if (!ret) {
2317 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2318 s->in_async = true;
2319 do {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002320 ret = __io_submit_sqe(ctx, req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002321 /*
2322 * We can get EAGAIN for polled IO even though we're
2323 * forcing a sync submission from here, since we can't
2324 * wait for request slots on the block side.
2325 */
2326 if (ret != -EAGAIN)
2327 break;
2328 cond_resched();
2329 } while (1);
2330 }
Jens Axboe31b51512019-01-18 22:56:34 -07002331
Jens Axboe561fb042019-10-24 07:25:42 -06002332 /* drop submission reference */
2333 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002334
Jens Axboe561fb042019-10-24 07:25:42 -06002335 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002336 if (req->flags & REQ_F_LINK)
2337 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe561fb042019-10-24 07:25:42 -06002338 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002339 io_put_req(req, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07002340 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002341
Jens Axboe561fb042019-10-24 07:25:42 -06002342 /* async context always use a copy of the sqe */
2343 kfree(sqe);
2344
2345 /* if a dependent link is ready, pass it back */
2346 if (!ret && nxt) {
2347 io_prep_async_work(nxt);
2348 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002349 }
Jens Axboe31b51512019-01-18 22:56:34 -07002350}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002351
Jens Axboe09bb8392019-03-13 12:39:28 -06002352static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2353{
2354 int op = READ_ONCE(sqe->opcode);
2355
2356 switch (op) {
2357 case IORING_OP_NOP:
2358 case IORING_OP_POLL_REMOVE:
2359 return false;
2360 default:
2361 return true;
2362 }
2363}
2364
Jens Axboe65e19f52019-10-26 07:20:21 -06002365static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2366 int index)
2367{
2368 struct fixed_file_table *table;
2369
2370 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2371 return table->files[index & IORING_FILE_TABLE_MASK];
2372}
2373
Pavel Begunkov267bc902019-11-07 01:41:08 +03002374static int io_req_set_file(struct io_ring_ctx *ctx,
Jens Axboe09bb8392019-03-13 12:39:28 -06002375 struct io_submit_state *state, struct io_kiocb *req)
2376{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002377 struct sqe_submit *s = &req->submit;
Jens Axboe09bb8392019-03-13 12:39:28 -06002378 unsigned flags;
2379 int fd;
2380
2381 flags = READ_ONCE(s->sqe->flags);
2382 fd = READ_ONCE(s->sqe->fd);
2383
Jackie Liu4fe2c962019-09-09 20:50:40 +08002384 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002385 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002386 /*
2387 * All io need record the previous position, if LINK vs DARIN,
2388 * it can be used to mark the position of the first IO in the
2389 * link list.
2390 */
2391 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002392
Jens Axboe60c112b2019-06-21 10:20:18 -06002393 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002394 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002395
2396 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002397 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002398 (unsigned) fd >= ctx->nr_user_files))
2399 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002400 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002401 req->file = io_file_from_index(ctx, fd);
2402 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002403 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002404 req->flags |= REQ_F_FIXED_FILE;
2405 } else {
2406 if (s->needs_fixed_file)
2407 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002408 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002409 req->file = io_file_get(state, fd);
2410 if (unlikely(!req->file))
2411 return -EBADF;
2412 }
2413
2414 return 0;
2415}
2416
Jens Axboefcb323c2019-10-24 12:39:47 -06002417static int io_grab_files(struct io_ring_ctx *ctx, struct io_kiocb *req)
2418{
2419 int ret = -EBADF;
2420
2421 rcu_read_lock();
2422 spin_lock_irq(&ctx->inflight_lock);
2423 /*
2424 * We use the f_ops->flush() handler to ensure that we can flush
2425 * out work accessing these files if the fd is closed. Check if
2426 * the fd has changed since we started down this path, and disallow
2427 * this operation if it has.
2428 */
2429 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2430 list_add(&req->inflight_entry, &ctx->inflight_list);
2431 req->flags |= REQ_F_INFLIGHT;
2432 req->work.files = current->files;
2433 ret = 0;
2434 }
2435 spin_unlock_irq(&ctx->inflight_lock);
2436 rcu_read_unlock();
2437
2438 return ret;
2439}
2440
Pavel Begunkov267bc902019-11-07 01:41:08 +03002441static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002442{
Jens Axboee0c5c572019-03-12 10:18:47 -06002443 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002444
Pavel Begunkov267bc902019-11-07 01:41:08 +03002445 ret = __io_submit_sqe(ctx, req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002446
2447 /*
2448 * We async punt it if the file wasn't marked NOWAIT, or if the file
2449 * doesn't support non-blocking read/write attempts
2450 */
2451 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2452 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002453 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002454 struct io_uring_sqe *sqe_copy;
2455
Jackie Liu954dab12019-09-18 10:37:52 +08002456 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002457 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002458 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002459 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2460 ret = io_grab_files(ctx, req);
2461 if (ret) {
2462 kfree(sqe_copy);
2463 goto err;
2464 }
2465 }
Jens Axboee65ef562019-03-12 10:16:44 -06002466
2467 /*
2468 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002469 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002470 */
Jens Axboefcb323c2019-10-24 12:39:47 -06002471 io_queue_async_work(ctx, req);
Jens Axboee65ef562019-03-12 10:16:44 -06002472 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002473 }
2474 }
Jens Axboee65ef562019-03-12 10:16:44 -06002475
2476 /* drop submission reference */
Jens Axboefcb323c2019-10-24 12:39:47 -06002477err:
Jens Axboeba816ad2019-09-28 11:36:45 -06002478 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002479
2480 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002481 if (ret) {
2482 io_cqring_add_event(ctx, req->user_data, ret);
2483 if (req->flags & REQ_F_LINK)
2484 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002485 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002486 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002487
2488 return ret;
2489}
2490
Pavel Begunkov267bc902019-11-07 01:41:08 +03002491static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002492{
2493 int ret;
2494
Pavel Begunkov267bc902019-11-07 01:41:08 +03002495 ret = io_req_defer(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002496 if (ret) {
2497 if (ret != -EIOCBQUEUED) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002498 io_cqring_add_event(ctx, req->submit.sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002499 io_free_req(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002500 }
2501 return 0;
2502 }
2503
Pavel Begunkov267bc902019-11-07 01:41:08 +03002504 return __io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002505}
2506
2507static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002508 struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002509{
2510 int ret;
2511 int need_submit = false;
2512
2513 if (!shadow)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002514 return io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002515
2516 /*
2517 * Mark the first IO in link list as DRAIN, let all the following
2518 * IOs enter the defer list. all IO needs to be completed before link
2519 * list.
2520 */
2521 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002522 ret = io_req_defer(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002523 if (ret) {
2524 if (ret != -EIOCBQUEUED) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002525 io_cqring_add_event(ctx, req->submit.sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002526 io_free_req(req, NULL);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002527 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002528 return 0;
2529 }
2530 } else {
2531 /*
2532 * If ret == 0 means that all IOs in front of link io are
2533 * running done. let's queue link head.
2534 */
2535 need_submit = true;
2536 }
2537
2538 /* Insert shadow req to defer_list, blocking next IOs */
2539 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002540 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002541 list_add_tail(&shadow->list, &ctx->defer_list);
2542 spin_unlock_irq(&ctx->completion_lock);
2543
2544 if (need_submit)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002545 return __io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002546
2547 return 0;
2548}
2549
Jens Axboe9e645e112019-05-10 16:07:28 -06002550#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2551
Pavel Begunkov196be952019-11-07 01:41:06 +03002552static void io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002553 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002554{
2555 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002556 struct sqe_submit *s = &req->submit;
Jens Axboe9e645e112019-05-10 16:07:28 -06002557 int ret;
2558
2559 /* enforce forwards compatibility on users */
2560 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2561 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03002562 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002563 }
2564
Pavel Begunkov267bc902019-11-07 01:41:08 +03002565 ret = io_req_set_file(ctx, state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002566 if (unlikely(ret)) {
2567err_req:
Jens Axboe9e645e112019-05-10 16:07:28 -06002568 io_cqring_add_event(ctx, s->sqe->user_data, ret);
Pavel Begunkov267bc902019-11-07 01:41:08 +03002569 io_free_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002570 return;
2571 }
2572
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002573 req->user_data = s->sqe->user_data;
2574
Jens Axboe9e645e112019-05-10 16:07:28 -06002575 /*
2576 * If we already have a head request, queue this one for async
2577 * submittal once the head completes. If we don't have a head but
2578 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2579 * submitted sync once the chain is complete. If none of those
2580 * conditions are true (normal request), then just queue it.
2581 */
2582 if (*link) {
2583 struct io_kiocb *prev = *link;
2584
2585 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2586 if (!sqe_copy) {
2587 ret = -EAGAIN;
2588 goto err_req;
2589 }
2590
2591 s->sqe = sqe_copy;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002592 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002593 list_add_tail(&req->list, &prev->link_list);
2594 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2595 req->flags |= REQ_F_LINK;
2596
Jens Axboe9e645e112019-05-10 16:07:28 -06002597 INIT_LIST_HEAD(&req->link_list);
2598 *link = req;
2599 } else {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002600 io_queue_sqe(ctx, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002601 }
2602}
2603
Jens Axboe9a56a232019-01-09 09:06:50 -07002604/*
2605 * Batched submission is done, ensure local IO is flushed out.
2606 */
2607static void io_submit_state_end(struct io_submit_state *state)
2608{
2609 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002610 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002611 if (state->free_reqs)
2612 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2613 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002614}
2615
2616/*
2617 * Start submission side cache.
2618 */
2619static void io_submit_state_start(struct io_submit_state *state,
2620 struct io_ring_ctx *ctx, unsigned max_ios)
2621{
2622 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002623 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002624 state->file = NULL;
2625 state->ios_left = max_ios;
2626}
2627
Jens Axboe2b188cc2019-01-07 10:46:33 -07002628static void io_commit_sqring(struct io_ring_ctx *ctx)
2629{
Hristo Venev75b28af2019-08-26 17:23:46 +00002630 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002631
Hristo Venev75b28af2019-08-26 17:23:46 +00002632 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002633 /*
2634 * Ensure any loads from the SQEs are done at this point,
2635 * since once we write the new head, the application could
2636 * write new data to them.
2637 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002638 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002639 }
2640}
2641
2642/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002643 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2644 * that is mapped by userspace. This means that care needs to be taken to
2645 * ensure that reads are stable, as we cannot rely on userspace always
2646 * being a good citizen. If members of the sqe are validated and then later
2647 * used, it's important that those reads are done through READ_ONCE() to
2648 * prevent a re-load down the line.
2649 */
2650static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2651{
Hristo Venev75b28af2019-08-26 17:23:46 +00002652 struct io_rings *rings = ctx->rings;
2653 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654 unsigned head;
2655
2656 /*
2657 * The cached sq head (or cq tail) serves two purposes:
2658 *
2659 * 1) allows us to batch the cost of updating the user visible
2660 * head updates.
2661 * 2) allows the kernel side to track the head on its own, even
2662 * though the application is the one updating it.
2663 */
2664 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002665 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002666 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002667 return false;
2668
Hristo Venev75b28af2019-08-26 17:23:46 +00002669 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002670 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002671 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002672 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002673 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002674 ctx->cached_sq_head++;
2675 return true;
2676 }
2677
2678 /* drop invalid entries */
2679 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002680 ctx->cached_sq_dropped++;
2681 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002682 return false;
2683}
2684
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002685static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002686 struct file *ring_file, int ring_fd,
2687 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002688{
2689 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002690 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002691 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002692 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002693 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002694
2695 if (nr > IO_PLUG_THRESHOLD) {
2696 io_submit_state_start(&state, ctx, nr);
2697 statep = &state;
2698 }
2699
2700 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002701 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03002702 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002703
Pavel Begunkov196be952019-11-07 01:41:06 +03002704 req = io_get_req(ctx, statep);
2705 if (unlikely(!req)) {
2706 if (!submitted)
2707 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002708 break;
Pavel Begunkov196be952019-11-07 01:41:06 +03002709 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03002710 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002711 __io_free_req(req);
2712 break;
2713 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002714
Pavel Begunkov50585b92019-11-07 01:41:07 +03002715 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002716 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
2717 if (!mm_fault) {
2718 use_mm(ctx->sqo_mm);
2719 *mm = ctx->sqo_mm;
2720 }
2721 }
2722
Pavel Begunkov50585b92019-11-07 01:41:07 +03002723 sqe_flags = req->submit.sqe->flags;
2724
2725 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002726 if (!shadow_req) {
2727 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002728 if (unlikely(!shadow_req))
2729 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002730 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2731 refcount_dec(&shadow_req->refs);
2732 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03002733 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002734 }
2735
Jackie Liua1041c22019-09-18 17:25:52 +08002736out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03002737 req->submit.ring_file = ring_file;
2738 req->submit.ring_fd = ring_fd;
2739 req->submit.has_user = *mm != NULL;
2740 req->submit.in_async = async;
2741 req->submit.needs_fixed_file = async;
2742 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
2743 true, async);
Pavel Begunkov267bc902019-11-07 01:41:08 +03002744 io_submit_sqe(ctx, req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002745 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03002746
2747 /*
2748 * If previous wasn't linked and we have a linked command,
2749 * that's the end of the chain. Submit the previous link.
2750 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03002751 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002752 io_queue_link_head(ctx, link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03002753 link = NULL;
2754 shadow_req = NULL;
2755 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002756 }
2757
Jens Axboe9e645e112019-05-10 16:07:28 -06002758 if (link)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002759 io_queue_link_head(ctx, link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002760 if (statep)
2761 io_submit_state_end(&state);
2762
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002763 /* Commit SQ ring head once we've consumed and submitted all SQEs */
2764 io_commit_sqring(ctx);
2765
Jens Axboe6c271ce2019-01-10 11:22:30 -07002766 return submitted;
2767}
2768
2769static int io_sq_thread(void *data)
2770{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002771 struct io_ring_ctx *ctx = data;
2772 struct mm_struct *cur_mm = NULL;
2773 mm_segment_t old_fs;
2774 DEFINE_WAIT(wait);
2775 unsigned inflight;
2776 unsigned long timeout;
2777
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002778 complete(&ctx->sqo_thread_started);
2779
Jens Axboe6c271ce2019-01-10 11:22:30 -07002780 old_fs = get_fs();
2781 set_fs(USER_DS);
2782
2783 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002784 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002785 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002786
2787 if (inflight) {
2788 unsigned nr_events = 0;
2789
2790 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002791 /*
2792 * inflight is the count of the maximum possible
2793 * entries we submitted, but it can be smaller
2794 * if we dropped some of them. If we don't have
2795 * poll entries available, then we know that we
2796 * have nothing left to poll for. Reset the
2797 * inflight count to zero in that case.
2798 */
2799 mutex_lock(&ctx->uring_lock);
2800 if (!list_empty(&ctx->poll_list))
2801 __io_iopoll_check(ctx, &nr_events, 0);
2802 else
2803 inflight = 0;
2804 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002805 } else {
2806 /*
2807 * Normal IO, just pretend everything completed.
2808 * We don't have to poll completions for that.
2809 */
2810 nr_events = inflight;
2811 }
2812
2813 inflight -= nr_events;
2814 if (!inflight)
2815 timeout = jiffies + ctx->sq_thread_idle;
2816 }
2817
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002818 to_submit = io_sqring_entries(ctx);
2819 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002820 /*
2821 * We're polling. If we're within the defined idle
2822 * period, then let us spin without work before going
2823 * to sleep.
2824 */
2825 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002826 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002827 continue;
2828 }
2829
2830 /*
2831 * Drop cur_mm before scheduling, we can't hold it for
2832 * long periods (or over schedule()). Do this before
2833 * adding ourselves to the waitqueue, as the unuse/drop
2834 * may sleep.
2835 */
2836 if (cur_mm) {
2837 unuse_mm(cur_mm);
2838 mmput(cur_mm);
2839 cur_mm = NULL;
2840 }
2841
2842 prepare_to_wait(&ctx->sqo_wait, &wait,
2843 TASK_INTERRUPTIBLE);
2844
2845 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002846 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002847 /* make sure to read SQ tail after writing flags */
2848 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002849
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002850 to_submit = io_sqring_entries(ctx);
2851 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002852 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002853 finish_wait(&ctx->sqo_wait, &wait);
2854 break;
2855 }
2856 if (signal_pending(current))
2857 flush_signals(current);
2858 schedule();
2859 finish_wait(&ctx->sqo_wait, &wait);
2860
Hristo Venev75b28af2019-08-26 17:23:46 +00002861 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002862 continue;
2863 }
2864 finish_wait(&ctx->sqo_wait, &wait);
2865
Hristo Venev75b28af2019-08-26 17:23:46 +00002866 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002867 }
2868
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002869 to_submit = min(to_submit, ctx->sq_entries);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002870 inflight += io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm,
2871 true);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002872 }
2873
2874 set_fs(old_fs);
2875 if (cur_mm) {
2876 unuse_mm(cur_mm);
2877 mmput(cur_mm);
2878 }
Jens Axboe06058632019-04-13 09:26:03 -06002879
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002880 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002881
Jens Axboe6c271ce2019-01-10 11:22:30 -07002882 return 0;
2883}
2884
Jens Axboebda52162019-09-24 13:47:15 -06002885struct io_wait_queue {
2886 struct wait_queue_entry wq;
2887 struct io_ring_ctx *ctx;
2888 unsigned to_wait;
2889 unsigned nr_timeouts;
2890};
2891
2892static inline bool io_should_wake(struct io_wait_queue *iowq)
2893{
2894 struct io_ring_ctx *ctx = iowq->ctx;
2895
2896 /*
2897 * Wake up if we have enough events, or if a timeout occured since we
2898 * started waiting. For timeouts, we always want to return to userspace,
2899 * regardless of event count.
2900 */
2901 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2902 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2903}
2904
2905static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2906 int wake_flags, void *key)
2907{
2908 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
2909 wq);
2910
2911 if (!io_should_wake(iowq))
2912 return -1;
2913
2914 return autoremove_wake_function(curr, mode, wake_flags, key);
2915}
2916
Jens Axboe2b188cc2019-01-07 10:46:33 -07002917/*
2918 * Wait until events become available, if we don't already have some. The
2919 * application must reap them itself, as they reside on the shared cq ring.
2920 */
2921static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2922 const sigset_t __user *sig, size_t sigsz)
2923{
Jens Axboebda52162019-09-24 13:47:15 -06002924 struct io_wait_queue iowq = {
2925 .wq = {
2926 .private = current,
2927 .func = io_wake_function,
2928 .entry = LIST_HEAD_INIT(iowq.wq.entry),
2929 },
2930 .ctx = ctx,
2931 .to_wait = min_events,
2932 };
Hristo Venev75b28af2019-08-26 17:23:46 +00002933 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08002934 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002935
Hristo Venev75b28af2019-08-26 17:23:46 +00002936 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002937 return 0;
2938
2939 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002940#ifdef CONFIG_COMPAT
2941 if (in_compat_syscall())
2942 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002943 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002944 else
2945#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002946 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002947
Jens Axboe2b188cc2019-01-07 10:46:33 -07002948 if (ret)
2949 return ret;
2950 }
2951
Jens Axboebda52162019-09-24 13:47:15 -06002952 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002953 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06002954 do {
2955 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
2956 TASK_INTERRUPTIBLE);
2957 if (io_should_wake(&iowq))
2958 break;
2959 schedule();
2960 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08002961 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06002962 break;
2963 }
2964 } while (1);
2965 finish_wait(&ctx->wait, &iowq.wq);
2966
Jackie Liue9ffa5c2019-10-29 11:16:42 +08002967 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002968
Hristo Venev75b28af2019-08-26 17:23:46 +00002969 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002970}
2971
Jens Axboe6b063142019-01-10 22:13:58 -07002972static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2973{
2974#if defined(CONFIG_UNIX)
2975 if (ctx->ring_sock) {
2976 struct sock *sock = ctx->ring_sock->sk;
2977 struct sk_buff *skb;
2978
2979 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2980 kfree_skb(skb);
2981 }
2982#else
2983 int i;
2984
Jens Axboe65e19f52019-10-26 07:20:21 -06002985 for (i = 0; i < ctx->nr_user_files; i++) {
2986 struct file *file;
2987
2988 file = io_file_from_index(ctx, i);
2989 if (file)
2990 fput(file);
2991 }
Jens Axboe6b063142019-01-10 22:13:58 -07002992#endif
2993}
2994
2995static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2996{
Jens Axboe65e19f52019-10-26 07:20:21 -06002997 unsigned nr_tables, i;
2998
2999 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003000 return -ENXIO;
3001
3002 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003003 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3004 for (i = 0; i < nr_tables; i++)
3005 kfree(ctx->file_table[i].files);
3006 kfree(ctx->file_table);
3007 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003008 ctx->nr_user_files = 0;
3009 return 0;
3010}
3011
Jens Axboe6c271ce2019-01-10 11:22:30 -07003012static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3013{
3014 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003015 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003016 /*
3017 * The park is a bit of a work-around, without it we get
3018 * warning spews on shutdown with SQPOLL set and affinity
3019 * set to a single CPU.
3020 */
Jens Axboe06058632019-04-13 09:26:03 -06003021 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003022 kthread_stop(ctx->sqo_thread);
3023 ctx->sqo_thread = NULL;
3024 }
3025}
3026
Jens Axboe6b063142019-01-10 22:13:58 -07003027static void io_finish_async(struct io_ring_ctx *ctx)
3028{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003029 io_sq_thread_stop(ctx);
3030
Jens Axboe561fb042019-10-24 07:25:42 -06003031 if (ctx->io_wq) {
3032 io_wq_destroy(ctx->io_wq);
3033 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003034 }
3035}
3036
3037#if defined(CONFIG_UNIX)
3038static void io_destruct_skb(struct sk_buff *skb)
3039{
3040 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3041
Jens Axboe561fb042019-10-24 07:25:42 -06003042 if (ctx->io_wq)
3043 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003044
Jens Axboe6b063142019-01-10 22:13:58 -07003045 unix_destruct_scm(skb);
3046}
3047
3048/*
3049 * Ensure the UNIX gc is aware of our file set, so we are certain that
3050 * the io_uring can be safely unregistered on process exit, even if we have
3051 * loops in the file referencing.
3052 */
3053static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3054{
3055 struct sock *sk = ctx->ring_sock->sk;
3056 struct scm_fp_list *fpl;
3057 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003058 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003059
3060 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3061 unsigned long inflight = ctx->user->unix_inflight + nr;
3062
3063 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3064 return -EMFILE;
3065 }
3066
3067 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3068 if (!fpl)
3069 return -ENOMEM;
3070
3071 skb = alloc_skb(0, GFP_KERNEL);
3072 if (!skb) {
3073 kfree(fpl);
3074 return -ENOMEM;
3075 }
3076
3077 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003078
Jens Axboe08a45172019-10-03 08:11:03 -06003079 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003080 fpl->user = get_uid(ctx->user);
3081 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003082 struct file *file = io_file_from_index(ctx, i + offset);
3083
3084 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003085 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003086 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003087 unix_inflight(fpl->user, fpl->fp[nr_files]);
3088 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003089 }
3090
Jens Axboe08a45172019-10-03 08:11:03 -06003091 if (nr_files) {
3092 fpl->max = SCM_MAX_FD;
3093 fpl->count = nr_files;
3094 UNIXCB(skb).fp = fpl;
3095 skb->destructor = io_destruct_skb;
3096 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3097 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003098
Jens Axboe08a45172019-10-03 08:11:03 -06003099 for (i = 0; i < nr_files; i++)
3100 fput(fpl->fp[i]);
3101 } else {
3102 kfree_skb(skb);
3103 kfree(fpl);
3104 }
Jens Axboe6b063142019-01-10 22:13:58 -07003105
3106 return 0;
3107}
3108
3109/*
3110 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3111 * causes regular reference counting to break down. We rely on the UNIX
3112 * garbage collection to take care of this problem for us.
3113 */
3114static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3115{
3116 unsigned left, total;
3117 int ret = 0;
3118
3119 total = 0;
3120 left = ctx->nr_user_files;
3121 while (left) {
3122 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003123
3124 ret = __io_sqe_files_scm(ctx, this_files, total);
3125 if (ret)
3126 break;
3127 left -= this_files;
3128 total += this_files;
3129 }
3130
3131 if (!ret)
3132 return 0;
3133
3134 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003135 struct file *file = io_file_from_index(ctx, total);
3136
3137 if (file)
3138 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003139 total++;
3140 }
3141
3142 return ret;
3143}
3144#else
3145static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3146{
3147 return 0;
3148}
3149#endif
3150
Jens Axboe65e19f52019-10-26 07:20:21 -06003151static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3152 unsigned nr_files)
3153{
3154 int i;
3155
3156 for (i = 0; i < nr_tables; i++) {
3157 struct fixed_file_table *table = &ctx->file_table[i];
3158 unsigned this_files;
3159
3160 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3161 table->files = kcalloc(this_files, sizeof(struct file *),
3162 GFP_KERNEL);
3163 if (!table->files)
3164 break;
3165 nr_files -= this_files;
3166 }
3167
3168 if (i == nr_tables)
3169 return 0;
3170
3171 for (i = 0; i < nr_tables; i++) {
3172 struct fixed_file_table *table = &ctx->file_table[i];
3173 kfree(table->files);
3174 }
3175 return 1;
3176}
3177
Jens Axboe6b063142019-01-10 22:13:58 -07003178static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3179 unsigned nr_args)
3180{
3181 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003182 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003183 int fd, ret = 0;
3184 unsigned i;
3185
Jens Axboe65e19f52019-10-26 07:20:21 -06003186 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003187 return -EBUSY;
3188 if (!nr_args)
3189 return -EINVAL;
3190 if (nr_args > IORING_MAX_FIXED_FILES)
3191 return -EMFILE;
3192
Jens Axboe65e19f52019-10-26 07:20:21 -06003193 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3194 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3195 GFP_KERNEL);
3196 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003197 return -ENOMEM;
3198
Jens Axboe65e19f52019-10-26 07:20:21 -06003199 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3200 kfree(ctx->file_table);
3201 return -ENOMEM;
3202 }
3203
Jens Axboe08a45172019-10-03 08:11:03 -06003204 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003205 struct fixed_file_table *table;
3206 unsigned index;
3207
Jens Axboe6b063142019-01-10 22:13:58 -07003208 ret = -EFAULT;
3209 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3210 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003211 /* allow sparse sets */
3212 if (fd == -1) {
3213 ret = 0;
3214 continue;
3215 }
Jens Axboe6b063142019-01-10 22:13:58 -07003216
Jens Axboe65e19f52019-10-26 07:20:21 -06003217 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3218 index = i & IORING_FILE_TABLE_MASK;
3219 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003220
3221 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003222 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003223 break;
3224 /*
3225 * Don't allow io_uring instances to be registered. If UNIX
3226 * isn't enabled, then this causes a reference cycle and this
3227 * instance can never get freed. If UNIX is enabled we'll
3228 * handle it just fine, but there's still no point in allowing
3229 * a ring fd as it doesn't support regular read/write anyway.
3230 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003231 if (table->files[index]->f_op == &io_uring_fops) {
3232 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003233 break;
3234 }
Jens Axboe6b063142019-01-10 22:13:58 -07003235 ret = 0;
3236 }
3237
3238 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003239 for (i = 0; i < ctx->nr_user_files; i++) {
3240 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003241
Jens Axboe65e19f52019-10-26 07:20:21 -06003242 file = io_file_from_index(ctx, i);
3243 if (file)
3244 fput(file);
3245 }
3246 for (i = 0; i < nr_tables; i++)
3247 kfree(ctx->file_table[i].files);
3248
3249 kfree(ctx->file_table);
3250 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003251 ctx->nr_user_files = 0;
3252 return ret;
3253 }
3254
3255 ret = io_sqe_files_scm(ctx);
3256 if (ret)
3257 io_sqe_files_unregister(ctx);
3258
3259 return ret;
3260}
3261
Jens Axboec3a31e62019-10-03 13:59:56 -06003262static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3263{
3264#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003265 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003266 struct sock *sock = ctx->ring_sock->sk;
3267 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3268 struct sk_buff *skb;
3269 int i;
3270
3271 __skb_queue_head_init(&list);
3272
3273 /*
3274 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3275 * remove this entry and rearrange the file array.
3276 */
3277 skb = skb_dequeue(head);
3278 while (skb) {
3279 struct scm_fp_list *fp;
3280
3281 fp = UNIXCB(skb).fp;
3282 for (i = 0; i < fp->count; i++) {
3283 int left;
3284
3285 if (fp->fp[i] != file)
3286 continue;
3287
3288 unix_notinflight(fp->user, fp->fp[i]);
3289 left = fp->count - 1 - i;
3290 if (left) {
3291 memmove(&fp->fp[i], &fp->fp[i + 1],
3292 left * sizeof(struct file *));
3293 }
3294 fp->count--;
3295 if (!fp->count) {
3296 kfree_skb(skb);
3297 skb = NULL;
3298 } else {
3299 __skb_queue_tail(&list, skb);
3300 }
3301 fput(file);
3302 file = NULL;
3303 break;
3304 }
3305
3306 if (!file)
3307 break;
3308
3309 __skb_queue_tail(&list, skb);
3310
3311 skb = skb_dequeue(head);
3312 }
3313
3314 if (skb_peek(&list)) {
3315 spin_lock_irq(&head->lock);
3316 while ((skb = __skb_dequeue(&list)) != NULL)
3317 __skb_queue_tail(head, skb);
3318 spin_unlock_irq(&head->lock);
3319 }
3320#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003321 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003322#endif
3323}
3324
3325static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3326 int index)
3327{
3328#if defined(CONFIG_UNIX)
3329 struct sock *sock = ctx->ring_sock->sk;
3330 struct sk_buff_head *head = &sock->sk_receive_queue;
3331 struct sk_buff *skb;
3332
3333 /*
3334 * See if we can merge this file into an existing skb SCM_RIGHTS
3335 * file set. If there's no room, fall back to allocating a new skb
3336 * and filling it in.
3337 */
3338 spin_lock_irq(&head->lock);
3339 skb = skb_peek(head);
3340 if (skb) {
3341 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3342
3343 if (fpl->count < SCM_MAX_FD) {
3344 __skb_unlink(skb, head);
3345 spin_unlock_irq(&head->lock);
3346 fpl->fp[fpl->count] = get_file(file);
3347 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3348 fpl->count++;
3349 spin_lock_irq(&head->lock);
3350 __skb_queue_head(head, skb);
3351 } else {
3352 skb = NULL;
3353 }
3354 }
3355 spin_unlock_irq(&head->lock);
3356
3357 if (skb) {
3358 fput(file);
3359 return 0;
3360 }
3361
3362 return __io_sqe_files_scm(ctx, 1, index);
3363#else
3364 return 0;
3365#endif
3366}
3367
3368static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3369 unsigned nr_args)
3370{
3371 struct io_uring_files_update up;
3372 __s32 __user *fds;
3373 int fd, i, err;
3374 __u32 done;
3375
Jens Axboe65e19f52019-10-26 07:20:21 -06003376 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003377 return -ENXIO;
3378 if (!nr_args)
3379 return -EINVAL;
3380 if (copy_from_user(&up, arg, sizeof(up)))
3381 return -EFAULT;
3382 if (check_add_overflow(up.offset, nr_args, &done))
3383 return -EOVERFLOW;
3384 if (done > ctx->nr_user_files)
3385 return -EINVAL;
3386
3387 done = 0;
3388 fds = (__s32 __user *) up.fds;
3389 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003390 struct fixed_file_table *table;
3391 unsigned index;
3392
Jens Axboec3a31e62019-10-03 13:59:56 -06003393 err = 0;
3394 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3395 err = -EFAULT;
3396 break;
3397 }
3398 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003399 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3400 index = i & IORING_FILE_TABLE_MASK;
3401 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003402 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003403 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003404 }
3405 if (fd != -1) {
3406 struct file *file;
3407
3408 file = fget(fd);
3409 if (!file) {
3410 err = -EBADF;
3411 break;
3412 }
3413 /*
3414 * Don't allow io_uring instances to be registered. If
3415 * UNIX isn't enabled, then this causes a reference
3416 * cycle and this instance can never get freed. If UNIX
3417 * is enabled we'll handle it just fine, but there's
3418 * still no point in allowing a ring fd as it doesn't
3419 * support regular read/write anyway.
3420 */
3421 if (file->f_op == &io_uring_fops) {
3422 fput(file);
3423 err = -EBADF;
3424 break;
3425 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003426 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003427 err = io_sqe_file_register(ctx, file, i);
3428 if (err)
3429 break;
3430 }
3431 nr_args--;
3432 done++;
3433 up.offset++;
3434 }
3435
3436 return done ? done : err;
3437}
3438
Jens Axboe6c271ce2019-01-10 11:22:30 -07003439static int io_sq_offload_start(struct io_ring_ctx *ctx,
3440 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003441{
Jens Axboe561fb042019-10-24 07:25:42 -06003442 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003443 int ret;
3444
Jens Axboe6c271ce2019-01-10 11:22:30 -07003445 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003446 mmgrab(current->mm);
3447 ctx->sqo_mm = current->mm;
3448
Jens Axboe6c271ce2019-01-10 11:22:30 -07003449 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003450 ret = -EPERM;
3451 if (!capable(CAP_SYS_ADMIN))
3452 goto err;
3453
Jens Axboe917257d2019-04-13 09:28:55 -06003454 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3455 if (!ctx->sq_thread_idle)
3456 ctx->sq_thread_idle = HZ;
3457
Jens Axboe6c271ce2019-01-10 11:22:30 -07003458 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003459 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003460
Jens Axboe917257d2019-04-13 09:28:55 -06003461 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003462 if (cpu >= nr_cpu_ids)
3463 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003464 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003465 goto err;
3466
Jens Axboe6c271ce2019-01-10 11:22:30 -07003467 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3468 ctx, cpu,
3469 "io_uring-sq");
3470 } else {
3471 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3472 "io_uring-sq");
3473 }
3474 if (IS_ERR(ctx->sqo_thread)) {
3475 ret = PTR_ERR(ctx->sqo_thread);
3476 ctx->sqo_thread = NULL;
3477 goto err;
3478 }
3479 wake_up_process(ctx->sqo_thread);
3480 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3481 /* Can't have SQ_AFF without SQPOLL */
3482 ret = -EINVAL;
3483 goto err;
3484 }
3485
Jens Axboe561fb042019-10-24 07:25:42 -06003486 /* Do QD, or 4 * CPUS, whatever is smallest */
3487 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
3488 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm);
Jens Axboe975c99a52019-10-30 08:42:56 -06003489 if (IS_ERR(ctx->io_wq)) {
3490 ret = PTR_ERR(ctx->io_wq);
3491 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003492 goto err;
3493 }
3494
3495 return 0;
3496err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003497 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003498 mmdrop(ctx->sqo_mm);
3499 ctx->sqo_mm = NULL;
3500 return ret;
3501}
3502
3503static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3504{
3505 atomic_long_sub(nr_pages, &user->locked_vm);
3506}
3507
3508static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3509{
3510 unsigned long page_limit, cur_pages, new_pages;
3511
3512 /* Don't allow more pages than we can safely lock */
3513 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3514
3515 do {
3516 cur_pages = atomic_long_read(&user->locked_vm);
3517 new_pages = cur_pages + nr_pages;
3518 if (new_pages > page_limit)
3519 return -ENOMEM;
3520 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3521 new_pages) != cur_pages);
3522
3523 return 0;
3524}
3525
3526static void io_mem_free(void *ptr)
3527{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003528 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003529
Mark Rutland52e04ef2019-04-30 17:30:21 +01003530 if (!ptr)
3531 return;
3532
3533 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003534 if (put_page_testzero(page))
3535 free_compound_page(page);
3536}
3537
3538static void *io_mem_alloc(size_t size)
3539{
3540 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3541 __GFP_NORETRY;
3542
3543 return (void *) __get_free_pages(gfp_flags, get_order(size));
3544}
3545
Hristo Venev75b28af2019-08-26 17:23:46 +00003546static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3547 size_t *sq_offset)
3548{
3549 struct io_rings *rings;
3550 size_t off, sq_array_size;
3551
3552 off = struct_size(rings, cqes, cq_entries);
3553 if (off == SIZE_MAX)
3554 return SIZE_MAX;
3555
3556#ifdef CONFIG_SMP
3557 off = ALIGN(off, SMP_CACHE_BYTES);
3558 if (off == 0)
3559 return SIZE_MAX;
3560#endif
3561
3562 sq_array_size = array_size(sizeof(u32), sq_entries);
3563 if (sq_array_size == SIZE_MAX)
3564 return SIZE_MAX;
3565
3566 if (check_add_overflow(off, sq_array_size, &off))
3567 return SIZE_MAX;
3568
3569 if (sq_offset)
3570 *sq_offset = off;
3571
3572 return off;
3573}
3574
Jens Axboe2b188cc2019-01-07 10:46:33 -07003575static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3576{
Hristo Venev75b28af2019-08-26 17:23:46 +00003577 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003578
Hristo Venev75b28af2019-08-26 17:23:46 +00003579 pages = (size_t)1 << get_order(
3580 rings_size(sq_entries, cq_entries, NULL));
3581 pages += (size_t)1 << get_order(
3582 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003583
Hristo Venev75b28af2019-08-26 17:23:46 +00003584 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003585}
3586
Jens Axboeedafcce2019-01-09 09:16:05 -07003587static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3588{
3589 int i, j;
3590
3591 if (!ctx->user_bufs)
3592 return -ENXIO;
3593
3594 for (i = 0; i < ctx->nr_user_bufs; i++) {
3595 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3596
3597 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003598 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003599
3600 if (ctx->account_mem)
3601 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003602 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003603 imu->nr_bvecs = 0;
3604 }
3605
3606 kfree(ctx->user_bufs);
3607 ctx->user_bufs = NULL;
3608 ctx->nr_user_bufs = 0;
3609 return 0;
3610}
3611
3612static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3613 void __user *arg, unsigned index)
3614{
3615 struct iovec __user *src;
3616
3617#ifdef CONFIG_COMPAT
3618 if (ctx->compat) {
3619 struct compat_iovec __user *ciovs;
3620 struct compat_iovec ciov;
3621
3622 ciovs = (struct compat_iovec __user *) arg;
3623 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3624 return -EFAULT;
3625
3626 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3627 dst->iov_len = ciov.iov_len;
3628 return 0;
3629 }
3630#endif
3631 src = (struct iovec __user *) arg;
3632 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3633 return -EFAULT;
3634 return 0;
3635}
3636
3637static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3638 unsigned nr_args)
3639{
3640 struct vm_area_struct **vmas = NULL;
3641 struct page **pages = NULL;
3642 int i, j, got_pages = 0;
3643 int ret = -EINVAL;
3644
3645 if (ctx->user_bufs)
3646 return -EBUSY;
3647 if (!nr_args || nr_args > UIO_MAXIOV)
3648 return -EINVAL;
3649
3650 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3651 GFP_KERNEL);
3652 if (!ctx->user_bufs)
3653 return -ENOMEM;
3654
3655 for (i = 0; i < nr_args; i++) {
3656 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3657 unsigned long off, start, end, ubuf;
3658 int pret, nr_pages;
3659 struct iovec iov;
3660 size_t size;
3661
3662 ret = io_copy_iov(ctx, &iov, arg, i);
3663 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003664 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003665
3666 /*
3667 * Don't impose further limits on the size and buffer
3668 * constraints here, we'll -EINVAL later when IO is
3669 * submitted if they are wrong.
3670 */
3671 ret = -EFAULT;
3672 if (!iov.iov_base || !iov.iov_len)
3673 goto err;
3674
3675 /* arbitrary limit, but we need something */
3676 if (iov.iov_len > SZ_1G)
3677 goto err;
3678
3679 ubuf = (unsigned long) iov.iov_base;
3680 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3681 start = ubuf >> PAGE_SHIFT;
3682 nr_pages = end - start;
3683
3684 if (ctx->account_mem) {
3685 ret = io_account_mem(ctx->user, nr_pages);
3686 if (ret)
3687 goto err;
3688 }
3689
3690 ret = 0;
3691 if (!pages || nr_pages > got_pages) {
3692 kfree(vmas);
3693 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003694 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003695 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003696 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003697 sizeof(struct vm_area_struct *),
3698 GFP_KERNEL);
3699 if (!pages || !vmas) {
3700 ret = -ENOMEM;
3701 if (ctx->account_mem)
3702 io_unaccount_mem(ctx->user, nr_pages);
3703 goto err;
3704 }
3705 got_pages = nr_pages;
3706 }
3707
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003708 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003709 GFP_KERNEL);
3710 ret = -ENOMEM;
3711 if (!imu->bvec) {
3712 if (ctx->account_mem)
3713 io_unaccount_mem(ctx->user, nr_pages);
3714 goto err;
3715 }
3716
3717 ret = 0;
3718 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003719 pret = get_user_pages(ubuf, nr_pages,
3720 FOLL_WRITE | FOLL_LONGTERM,
3721 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003722 if (pret == nr_pages) {
3723 /* don't support file backed memory */
3724 for (j = 0; j < nr_pages; j++) {
3725 struct vm_area_struct *vma = vmas[j];
3726
3727 if (vma->vm_file &&
3728 !is_file_hugepages(vma->vm_file)) {
3729 ret = -EOPNOTSUPP;
3730 break;
3731 }
3732 }
3733 } else {
3734 ret = pret < 0 ? pret : -EFAULT;
3735 }
3736 up_read(&current->mm->mmap_sem);
3737 if (ret) {
3738 /*
3739 * if we did partial map, or found file backed vmas,
3740 * release any pages we did get
3741 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003742 if (pret > 0)
3743 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003744 if (ctx->account_mem)
3745 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003746 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003747 goto err;
3748 }
3749
3750 off = ubuf & ~PAGE_MASK;
3751 size = iov.iov_len;
3752 for (j = 0; j < nr_pages; j++) {
3753 size_t vec_len;
3754
3755 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3756 imu->bvec[j].bv_page = pages[j];
3757 imu->bvec[j].bv_len = vec_len;
3758 imu->bvec[j].bv_offset = off;
3759 off = 0;
3760 size -= vec_len;
3761 }
3762 /* store original address for later verification */
3763 imu->ubuf = ubuf;
3764 imu->len = iov.iov_len;
3765 imu->nr_bvecs = nr_pages;
3766
3767 ctx->nr_user_bufs++;
3768 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003769 kvfree(pages);
3770 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003771 return 0;
3772err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003773 kvfree(pages);
3774 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003775 io_sqe_buffer_unregister(ctx);
3776 return ret;
3777}
3778
Jens Axboe9b402842019-04-11 11:45:41 -06003779static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3780{
3781 __s32 __user *fds = arg;
3782 int fd;
3783
3784 if (ctx->cq_ev_fd)
3785 return -EBUSY;
3786
3787 if (copy_from_user(&fd, fds, sizeof(*fds)))
3788 return -EFAULT;
3789
3790 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3791 if (IS_ERR(ctx->cq_ev_fd)) {
3792 int ret = PTR_ERR(ctx->cq_ev_fd);
3793 ctx->cq_ev_fd = NULL;
3794 return ret;
3795 }
3796
3797 return 0;
3798}
3799
3800static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3801{
3802 if (ctx->cq_ev_fd) {
3803 eventfd_ctx_put(ctx->cq_ev_fd);
3804 ctx->cq_ev_fd = NULL;
3805 return 0;
3806 }
3807
3808 return -ENXIO;
3809}
3810
Jens Axboe2b188cc2019-01-07 10:46:33 -07003811static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3812{
Jens Axboe6b063142019-01-10 22:13:58 -07003813 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003814 if (ctx->sqo_mm)
3815 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003816
3817 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003818 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003819 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003820 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003821
Jens Axboe2b188cc2019-01-07 10:46:33 -07003822#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003823 if (ctx->ring_sock) {
3824 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003825 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003826 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003827#endif
3828
Hristo Venev75b28af2019-08-26 17:23:46 +00003829 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003830 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003831
3832 percpu_ref_exit(&ctx->refs);
3833 if (ctx->account_mem)
3834 io_unaccount_mem(ctx->user,
3835 ring_pages(ctx->sq_entries, ctx->cq_entries));
3836 free_uid(ctx->user);
3837 kfree(ctx);
3838}
3839
3840static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3841{
3842 struct io_ring_ctx *ctx = file->private_data;
3843 __poll_t mask = 0;
3844
3845 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003846 /*
3847 * synchronizes with barrier from wq_has_sleeper call in
3848 * io_commit_cqring
3849 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003850 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003851 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3852 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003853 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003854 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003855 mask |= EPOLLIN | EPOLLRDNORM;
3856
3857 return mask;
3858}
3859
3860static int io_uring_fasync(int fd, struct file *file, int on)
3861{
3862 struct io_ring_ctx *ctx = file->private_data;
3863
3864 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3865}
3866
3867static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3868{
3869 mutex_lock(&ctx->uring_lock);
3870 percpu_ref_kill(&ctx->refs);
3871 mutex_unlock(&ctx->uring_lock);
3872
Jens Axboe5262f562019-09-17 12:26:57 -06003873 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003874 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06003875
3876 if (ctx->io_wq)
3877 io_wq_cancel_all(ctx->io_wq);
3878
Jens Axboedef596e2019-01-09 08:59:42 -07003879 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003880 wait_for_completion(&ctx->ctx_done);
3881 io_ring_ctx_free(ctx);
3882}
3883
3884static int io_uring_release(struct inode *inode, struct file *file)
3885{
3886 struct io_ring_ctx *ctx = file->private_data;
3887
3888 file->private_data = NULL;
3889 io_ring_ctx_wait_and_kill(ctx);
3890 return 0;
3891}
3892
Jens Axboefcb323c2019-10-24 12:39:47 -06003893static void io_uring_cancel_files(struct io_ring_ctx *ctx,
3894 struct files_struct *files)
3895{
3896 struct io_kiocb *req;
3897 DEFINE_WAIT(wait);
3898
3899 while (!list_empty_careful(&ctx->inflight_list)) {
3900 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
3901
3902 spin_lock_irq(&ctx->inflight_lock);
3903 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
3904 if (req->work.files == files) {
3905 ret = io_wq_cancel_work(ctx->io_wq, &req->work);
3906 break;
3907 }
3908 }
3909 if (ret == IO_WQ_CANCEL_RUNNING)
3910 prepare_to_wait(&ctx->inflight_wait, &wait,
3911 TASK_UNINTERRUPTIBLE);
3912
3913 spin_unlock_irq(&ctx->inflight_lock);
3914
3915 /*
3916 * We need to keep going until we get NOTFOUND. We only cancel
3917 * one work at the time.
3918 *
3919 * If we get CANCEL_RUNNING, then wait for a work to complete
3920 * before continuing.
3921 */
3922 if (ret == IO_WQ_CANCEL_OK)
3923 continue;
3924 else if (ret != IO_WQ_CANCEL_RUNNING)
3925 break;
3926 schedule();
3927 }
3928}
3929
3930static int io_uring_flush(struct file *file, void *data)
3931{
3932 struct io_ring_ctx *ctx = file->private_data;
3933
3934 io_uring_cancel_files(ctx, data);
3935 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
3936 io_wq_cancel_all(ctx->io_wq);
3937 return 0;
3938}
3939
Jens Axboe2b188cc2019-01-07 10:46:33 -07003940static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3941{
3942 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3943 unsigned long sz = vma->vm_end - vma->vm_start;
3944 struct io_ring_ctx *ctx = file->private_data;
3945 unsigned long pfn;
3946 struct page *page;
3947 void *ptr;
3948
3949 switch (offset) {
3950 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003951 case IORING_OFF_CQ_RING:
3952 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003953 break;
3954 case IORING_OFF_SQES:
3955 ptr = ctx->sq_sqes;
3956 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003957 default:
3958 return -EINVAL;
3959 }
3960
3961 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07003962 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003963 return -EINVAL;
3964
3965 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3966 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3967}
3968
3969SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3970 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3971 size_t, sigsz)
3972{
3973 struct io_ring_ctx *ctx;
3974 long ret = -EBADF;
3975 int submitted = 0;
3976 struct fd f;
3977
Jens Axboe6c271ce2019-01-10 11:22:30 -07003978 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003979 return -EINVAL;
3980
3981 f = fdget(fd);
3982 if (!f.file)
3983 return -EBADF;
3984
3985 ret = -EOPNOTSUPP;
3986 if (f.file->f_op != &io_uring_fops)
3987 goto out_fput;
3988
3989 ret = -ENXIO;
3990 ctx = f.file->private_data;
3991 if (!percpu_ref_tryget(&ctx->refs))
3992 goto out_fput;
3993
Jens Axboe6c271ce2019-01-10 11:22:30 -07003994 /*
3995 * For SQ polling, the thread will do all submissions and completions.
3996 * Just return the requested submit count, and wake the thread if
3997 * we were asked to.
3998 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003999 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004000 if (ctx->flags & IORING_SETUP_SQPOLL) {
4001 if (flags & IORING_ENTER_SQ_WAKEUP)
4002 wake_up(&ctx->sqo_wait);
4003 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004004 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004005 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004006
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004007 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004008 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004009 /* already have mm, so io_submit_sqes() won't try to grab it */
4010 cur_mm = ctx->sqo_mm;
4011 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4012 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004013 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004014 }
4015 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004016 unsigned nr_events = 0;
4017
Jens Axboe2b188cc2019-01-07 10:46:33 -07004018 min_complete = min(min_complete, ctx->cq_entries);
4019
Jens Axboedef596e2019-01-09 08:59:42 -07004020 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004021 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004022 } else {
4023 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4024 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004025 }
4026
Pavel Begunkov6805b322019-10-08 02:18:42 +03004027 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004028out_fput:
4029 fdput(f);
4030 return submitted ? submitted : ret;
4031}
4032
4033static const struct file_operations io_uring_fops = {
4034 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004035 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004036 .mmap = io_uring_mmap,
4037 .poll = io_uring_poll,
4038 .fasync = io_uring_fasync,
4039};
4040
4041static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4042 struct io_uring_params *p)
4043{
Hristo Venev75b28af2019-08-26 17:23:46 +00004044 struct io_rings *rings;
4045 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004046
Hristo Venev75b28af2019-08-26 17:23:46 +00004047 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4048 if (size == SIZE_MAX)
4049 return -EOVERFLOW;
4050
4051 rings = io_mem_alloc(size);
4052 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004053 return -ENOMEM;
4054
Hristo Venev75b28af2019-08-26 17:23:46 +00004055 ctx->rings = rings;
4056 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4057 rings->sq_ring_mask = p->sq_entries - 1;
4058 rings->cq_ring_mask = p->cq_entries - 1;
4059 rings->sq_ring_entries = p->sq_entries;
4060 rings->cq_ring_entries = p->cq_entries;
4061 ctx->sq_mask = rings->sq_ring_mask;
4062 ctx->cq_mask = rings->cq_ring_mask;
4063 ctx->sq_entries = rings->sq_ring_entries;
4064 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004065
4066 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4067 if (size == SIZE_MAX)
4068 return -EOVERFLOW;
4069
4070 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004071 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004072 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004073
Jens Axboe2b188cc2019-01-07 10:46:33 -07004074 return 0;
4075}
4076
4077/*
4078 * Allocate an anonymous fd, this is what constitutes the application
4079 * visible backing of an io_uring instance. The application mmaps this
4080 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4081 * we have to tie this fd to a socket for file garbage collection purposes.
4082 */
4083static int io_uring_get_fd(struct io_ring_ctx *ctx)
4084{
4085 struct file *file;
4086 int ret;
4087
4088#if defined(CONFIG_UNIX)
4089 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4090 &ctx->ring_sock);
4091 if (ret)
4092 return ret;
4093#endif
4094
4095 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4096 if (ret < 0)
4097 goto err;
4098
4099 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4100 O_RDWR | O_CLOEXEC);
4101 if (IS_ERR(file)) {
4102 put_unused_fd(ret);
4103 ret = PTR_ERR(file);
4104 goto err;
4105 }
4106
4107#if defined(CONFIG_UNIX)
4108 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004109 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004110#endif
4111 fd_install(ret, file);
4112 return ret;
4113err:
4114#if defined(CONFIG_UNIX)
4115 sock_release(ctx->ring_sock);
4116 ctx->ring_sock = NULL;
4117#endif
4118 return ret;
4119}
4120
4121static int io_uring_create(unsigned entries, struct io_uring_params *p)
4122{
4123 struct user_struct *user = NULL;
4124 struct io_ring_ctx *ctx;
4125 bool account_mem;
4126 int ret;
4127
4128 if (!entries || entries > IORING_MAX_ENTRIES)
4129 return -EINVAL;
4130
4131 /*
4132 * Use twice as many entries for the CQ ring. It's possible for the
4133 * application to drive a higher depth than the size of the SQ ring,
4134 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004135 * some flexibility in overcommitting a bit. If the application has
4136 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4137 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004138 */
4139 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004140 if (p->flags & IORING_SETUP_CQSIZE) {
4141 /*
4142 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4143 * to a power-of-two, if it isn't already. We do NOT impose
4144 * any cq vs sq ring sizing.
4145 */
4146 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4147 return -EINVAL;
4148 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4149 } else {
4150 p->cq_entries = 2 * p->sq_entries;
4151 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004152
4153 user = get_uid(current_user());
4154 account_mem = !capable(CAP_IPC_LOCK);
4155
4156 if (account_mem) {
4157 ret = io_account_mem(user,
4158 ring_pages(p->sq_entries, p->cq_entries));
4159 if (ret) {
4160 free_uid(user);
4161 return ret;
4162 }
4163 }
4164
4165 ctx = io_ring_ctx_alloc(p);
4166 if (!ctx) {
4167 if (account_mem)
4168 io_unaccount_mem(user, ring_pages(p->sq_entries,
4169 p->cq_entries));
4170 free_uid(user);
4171 return -ENOMEM;
4172 }
4173 ctx->compat = in_compat_syscall();
4174 ctx->account_mem = account_mem;
4175 ctx->user = user;
4176
4177 ret = io_allocate_scq_urings(ctx, p);
4178 if (ret)
4179 goto err;
4180
Jens Axboe6c271ce2019-01-10 11:22:30 -07004181 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004182 if (ret)
4183 goto err;
4184
Jens Axboe2b188cc2019-01-07 10:46:33 -07004185 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004186 p->sq_off.head = offsetof(struct io_rings, sq.head);
4187 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4188 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4189 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4190 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4191 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4192 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004193
4194 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004195 p->cq_off.head = offsetof(struct io_rings, cq.head);
4196 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4197 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4198 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4199 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4200 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004201
Jens Axboe044c1ab2019-10-28 09:15:33 -06004202 /*
4203 * Install ring fd as the very last thing, so we don't risk someone
4204 * having closed it before we finish setup
4205 */
4206 ret = io_uring_get_fd(ctx);
4207 if (ret < 0)
4208 goto err;
4209
Jens Axboeac90f242019-09-06 10:26:21 -06004210 p->features = IORING_FEAT_SINGLE_MMAP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004211 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004212 return ret;
4213err:
4214 io_ring_ctx_wait_and_kill(ctx);
4215 return ret;
4216}
4217
4218/*
4219 * Sets up an aio uring context, and returns the fd. Applications asks for a
4220 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4221 * params structure passed in.
4222 */
4223static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4224{
4225 struct io_uring_params p;
4226 long ret;
4227 int i;
4228
4229 if (copy_from_user(&p, params, sizeof(p)))
4230 return -EFAULT;
4231 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4232 if (p.resv[i])
4233 return -EINVAL;
4234 }
4235
Jens Axboe6c271ce2019-01-10 11:22:30 -07004236 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004237 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004238 return -EINVAL;
4239
4240 ret = io_uring_create(entries, &p);
4241 if (ret < 0)
4242 return ret;
4243
4244 if (copy_to_user(params, &p, sizeof(p)))
4245 return -EFAULT;
4246
4247 return ret;
4248}
4249
4250SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4251 struct io_uring_params __user *, params)
4252{
4253 return io_uring_setup(entries, params);
4254}
4255
Jens Axboeedafcce2019-01-09 09:16:05 -07004256static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4257 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004258 __releases(ctx->uring_lock)
4259 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004260{
4261 int ret;
4262
Jens Axboe35fa71a2019-04-22 10:23:23 -06004263 /*
4264 * We're inside the ring mutex, if the ref is already dying, then
4265 * someone else killed the ctx or is already going through
4266 * io_uring_register().
4267 */
4268 if (percpu_ref_is_dying(&ctx->refs))
4269 return -ENXIO;
4270
Jens Axboeedafcce2019-01-09 09:16:05 -07004271 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004272
4273 /*
4274 * Drop uring mutex before waiting for references to exit. If another
4275 * thread is currently inside io_uring_enter() it might need to grab
4276 * the uring_lock to make progress. If we hold it here across the drain
4277 * wait, then we can deadlock. It's safe to drop the mutex here, since
4278 * no new references will come in after we've killed the percpu ref.
4279 */
4280 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004281 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004282 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004283
4284 switch (opcode) {
4285 case IORING_REGISTER_BUFFERS:
4286 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4287 break;
4288 case IORING_UNREGISTER_BUFFERS:
4289 ret = -EINVAL;
4290 if (arg || nr_args)
4291 break;
4292 ret = io_sqe_buffer_unregister(ctx);
4293 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004294 case IORING_REGISTER_FILES:
4295 ret = io_sqe_files_register(ctx, arg, nr_args);
4296 break;
4297 case IORING_UNREGISTER_FILES:
4298 ret = -EINVAL;
4299 if (arg || nr_args)
4300 break;
4301 ret = io_sqe_files_unregister(ctx);
4302 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004303 case IORING_REGISTER_FILES_UPDATE:
4304 ret = io_sqe_files_update(ctx, arg, nr_args);
4305 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004306 case IORING_REGISTER_EVENTFD:
4307 ret = -EINVAL;
4308 if (nr_args != 1)
4309 break;
4310 ret = io_eventfd_register(ctx, arg);
4311 break;
4312 case IORING_UNREGISTER_EVENTFD:
4313 ret = -EINVAL;
4314 if (arg || nr_args)
4315 break;
4316 ret = io_eventfd_unregister(ctx);
4317 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004318 default:
4319 ret = -EINVAL;
4320 break;
4321 }
4322
4323 /* bring the ctx back to life */
4324 reinit_completion(&ctx->ctx_done);
4325 percpu_ref_reinit(&ctx->refs);
4326 return ret;
4327}
4328
4329SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4330 void __user *, arg, unsigned int, nr_args)
4331{
4332 struct io_ring_ctx *ctx;
4333 long ret = -EBADF;
4334 struct fd f;
4335
4336 f = fdget(fd);
4337 if (!f.file)
4338 return -EBADF;
4339
4340 ret = -EOPNOTSUPP;
4341 if (f.file->f_op != &io_uring_fops)
4342 goto out_fput;
4343
4344 ctx = f.file->private_data;
4345
4346 mutex_lock(&ctx->uring_lock);
4347 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4348 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004349 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4350 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004351out_fput:
4352 fdput(f);
4353 return ret;
4354}
4355
Jens Axboe2b188cc2019-01-07 10:46:33 -07004356static int __init io_uring_init(void)
4357{
4358 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4359 return 0;
4360};
4361__initcall(io_uring_init);