blob: bda27b52fd5bff75b5b3fad1d5981fe209b60d91 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070059#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070072
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020073#define CREATE_TRACE_POINTS
74#include <trace/events/io_uring.h>
75
Jens Axboe2b188cc2019-01-07 10:46:33 -070076#include <uapi/linux/io_uring.h>
77
78#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060079#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070080
Daniel Xu5277dea2019-09-14 14:23:45 -070081#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060082#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060083
84/*
85 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
86 */
87#define IORING_FILE_TABLE_SHIFT 9
88#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
89#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
90#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070091
92struct io_uring {
93 u32 head ____cacheline_aligned_in_smp;
94 u32 tail ____cacheline_aligned_in_smp;
95};
96
Stefan Bühler1e84b972019-04-24 23:54:16 +020097/*
Hristo Venev75b28af2019-08-26 17:23:46 +000098 * This data is shared with the application through the mmap at offsets
99 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200100 *
101 * The offsets to the member fields are published through struct
102 * io_sqring_offsets when calling io_uring_setup.
103 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000104struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 /*
106 * Head and tail offsets into the ring; the offsets need to be
107 * masked to get valid indices.
108 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000109 * The kernel controls head of the sq ring and the tail of the cq ring,
110 * and the application controls tail of the sq ring and the head of the
111 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000115 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200116 * ring_entries - 1)
117 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 u32 sq_ring_mask, cq_ring_mask;
119 /* Ring sizes (constant, power of 2) */
120 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 /*
122 * Number of invalid entries dropped by the kernel due to
123 * invalid index stored in array
124 *
125 * Written by the kernel, shouldn't be modified by the
126 * application (i.e. get number of "new events" by comparing to
127 * cached value).
128 *
129 * After a new SQ head value was read by the application this
130 * counter includes all submissions that were dropped reaching
131 * the new SQ head (and possibly more).
132 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 /*
135 * Runtime flags
136 *
137 * Written by the kernel, shouldn't be modified by the
138 * application.
139 *
140 * The application needs a full memory barrier before checking
141 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
145 * Number of completion events lost because the queue was full;
146 * this should be avoided by the application by making sure
147 * there are not more requests pending thatn there is space in
148 * the completion queue.
149 *
150 * Written by the kernel, shouldn't be modified by the
151 * application (i.e. get number of "new events" by comparing to
152 * cached value).
153 *
154 * As completion events come in out of order this counter is not
155 * ordered with any other data.
156 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000157 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 /*
159 * Ring buffer of completion events.
160 *
161 * The kernel writes completion events fresh every time they are
162 * produced, so the application is allowed to modify pending
163 * entries.
164 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000165 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700166};
167
Jens Axboeedafcce2019-01-09 09:16:05 -0700168struct io_mapped_ubuf {
169 u64 ubuf;
170 size_t len;
171 struct bio_vec *bvec;
172 unsigned int nr_bvecs;
173};
174
Jens Axboe65e19f52019-10-26 07:20:21 -0600175struct fixed_file_table {
176 struct file **files;
177};
178
Jens Axboe2b188cc2019-01-07 10:46:33 -0700179struct io_ring_ctx {
180 struct {
181 struct percpu_ref refs;
182 } ____cacheline_aligned_in_smp;
183
184 struct {
185 unsigned int flags;
186 bool compat;
187 bool account_mem;
188
Hristo Venev75b28af2019-08-26 17:23:46 +0000189 /*
190 * Ring buffer of indices into array of io_uring_sqe, which is
191 * mmapped by the application using the IORING_OFF_SQES offset.
192 *
193 * This indirection could e.g. be used to assign fixed
194 * io_uring_sqe entries to operations and only submit them to
195 * the queue when needed.
196 *
197 * The kernel modifies neither the indices array nor the entries
198 * array.
199 */
200 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700201 unsigned cached_sq_head;
202 unsigned sq_entries;
203 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700204 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600205 unsigned cached_sq_dropped;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700206 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600207
208 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600209 struct list_head timeout_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600210
211 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700212 } ____cacheline_aligned_in_smp;
213
214 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600215 struct io_wq *io_wq;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700216 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700217 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700218 wait_queue_head_t sqo_wait;
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800219 struct completion sqo_thread_started;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700220
221 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222 unsigned cached_cq_tail;
Jens Axboe498ccd92019-10-25 10:04:25 -0600223 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700224 unsigned cq_entries;
225 unsigned cq_mask;
226 struct wait_queue_head cq_wait;
227 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600228 struct eventfd_ctx *cq_ev_fd;
Jens Axboe5262f562019-09-17 12:26:57 -0600229 atomic_t cq_timeouts;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700230 } ____cacheline_aligned_in_smp;
231
Hristo Venev75b28af2019-08-26 17:23:46 +0000232 struct io_rings *rings;
233
Jens Axboe6b063142019-01-10 22:13:58 -0700234 /*
235 * If used, fixed file set. Writers must ensure that ->refs is dead,
236 * readers must ensure that ->refs is alive as long as the file* is
237 * used. Only updated through io_uring_register(2).
238 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600239 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700240 unsigned nr_user_files;
241
Jens Axboeedafcce2019-01-09 09:16:05 -0700242 /* if used, fixed mapped user buffers */
243 unsigned nr_user_bufs;
244 struct io_mapped_ubuf *user_bufs;
245
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246 struct user_struct *user;
247
248 struct completion ctx_done;
249
250 struct {
251 struct mutex uring_lock;
252 wait_queue_head_t wait;
253 } ____cacheline_aligned_in_smp;
254
255 struct {
256 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700257 bool poll_multi_file;
258 /*
259 * ->poll_list is protected by the ctx->uring_lock for
260 * io_uring instances that don't use IORING_SETUP_SQPOLL.
261 * For SQPOLL, only the single threaded io_sq_thread() will
262 * manipulate the list, hence no extra locking is needed there.
263 */
264 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700265 struct list_head cancel_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600266
267 spinlock_t inflight_lock;
268 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700269 } ____cacheline_aligned_in_smp;
270
271#if defined(CONFIG_UNIX)
272 struct socket *ring_sock;
273#endif
274};
275
276struct sqe_submit {
277 const struct io_uring_sqe *sqe;
Jens Axboefcb323c2019-10-24 12:39:47 -0600278 struct file *ring_file;
279 int ring_fd;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800280 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700281 bool has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +0800282 bool in_async;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700283 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700284};
285
Jens Axboe09bb8392019-03-13 12:39:28 -0600286/*
287 * First field must be the file pointer in all the
288 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
289 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700290struct io_poll_iocb {
291 struct file *file;
292 struct wait_queue_head *head;
293 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600294 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700295 bool canceled;
296 struct wait_queue_entry wait;
297};
298
Jens Axboe5262f562019-09-17 12:26:57 -0600299struct io_timeout {
300 struct file *file;
301 struct hrtimer timer;
302};
303
Jens Axboe09bb8392019-03-13 12:39:28 -0600304/*
305 * NOTE! Each of the iocb union members has the file pointer
306 * as the first entry in their struct definition. So you can
307 * access the file pointer through any of the sub-structs,
308 * or directly as just 'ki_filp' in this struct.
309 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700310struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700311 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600312 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700313 struct kiocb rw;
314 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600315 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700316 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700317
318 struct sqe_submit submit;
319
320 struct io_ring_ctx *ctx;
321 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600322 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700323 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700324 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200325#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700326#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700327#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700328#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200329#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
330#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600331#define REQ_F_LINK 64 /* linked sqes */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800332#define REQ_F_LINK_DONE 128 /* linked sqes done */
333#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800334#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600335#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600336#define REQ_F_ISREG 2048 /* regular file */
337#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboefcb323c2019-10-24 12:39:47 -0600338#define REQ_F_INFLIGHT 8192 /* on inflight list */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700339 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600340 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600341 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700342
Jens Axboefcb323c2019-10-24 12:39:47 -0600343 struct list_head inflight_entry;
344
Jens Axboe561fb042019-10-24 07:25:42 -0600345 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700346};
347
348#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700349#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700350
Jens Axboe9a56a232019-01-09 09:06:50 -0700351struct io_submit_state {
352 struct blk_plug plug;
353
354 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700355 * io_kiocb alloc cache
356 */
357 void *reqs[IO_IOPOLL_BATCH];
358 unsigned int free_reqs;
359 unsigned int cur_req;
360
361 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700362 * File reference cache
363 */
364 struct file *file;
365 unsigned int fd;
366 unsigned int has_refs;
367 unsigned int used_refs;
368 unsigned int ios_left;
369};
370
Jens Axboe561fb042019-10-24 07:25:42 -0600371static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe5262f562019-09-17 12:26:57 -0600372static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
373 long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800374static void __io_free_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600375
Jens Axboe2b188cc2019-01-07 10:46:33 -0700376static struct kmem_cache *req_cachep;
377
378static const struct file_operations io_uring_fops;
379
380struct sock *io_uring_get_socket(struct file *file)
381{
382#if defined(CONFIG_UNIX)
383 if (file->f_op == &io_uring_fops) {
384 struct io_ring_ctx *ctx = file->private_data;
385
386 return ctx->ring_sock->sk;
387 }
388#endif
389 return NULL;
390}
391EXPORT_SYMBOL(io_uring_get_socket);
392
393static void io_ring_ctx_ref_free(struct percpu_ref *ref)
394{
395 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
396
397 complete(&ctx->ctx_done);
398}
399
400static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
401{
402 struct io_ring_ctx *ctx;
403
404 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
405 if (!ctx)
406 return NULL;
407
Roman Gushchin21482892019-05-07 10:01:48 -0700408 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
409 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410 kfree(ctx);
411 return NULL;
412 }
413
414 ctx->flags = p->flags;
415 init_waitqueue_head(&ctx->cq_wait);
416 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800417 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700418 mutex_init(&ctx->uring_lock);
419 init_waitqueue_head(&ctx->wait);
420 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700421 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700422 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600423 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600424 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600425 init_waitqueue_head(&ctx->inflight_wait);
426 spin_lock_init(&ctx->inflight_lock);
427 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700428 return ctx;
429}
430
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600431static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
432 struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600433{
Jens Axboe498ccd92019-10-25 10:04:25 -0600434 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
435 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600436}
437
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600438static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
439 struct io_kiocb *req)
440{
441 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
442 return false;
443
444 return __io_sequence_defer(ctx, req);
445}
446
447static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600448{
449 struct io_kiocb *req;
450
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600451 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
452 if (req && !io_sequence_defer(ctx, req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600453 list_del_init(&req->list);
454 return req;
455 }
456
457 return NULL;
458}
459
Jens Axboe5262f562019-09-17 12:26:57 -0600460static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
461{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600462 struct io_kiocb *req;
463
464 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
465 if (req && !__io_sequence_defer(ctx, req)) {
466 list_del_init(&req->list);
467 return req;
468 }
469
470 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600471}
472
Jens Axboede0617e2019-04-06 21:51:27 -0600473static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700474{
Hristo Venev75b28af2019-08-26 17:23:46 +0000475 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700476
Hristo Venev75b28af2019-08-26 17:23:46 +0000477 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700478 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000479 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700480
Jens Axboe2b188cc2019-01-07 10:46:33 -0700481 if (wq_has_sleeper(&ctx->cq_wait)) {
482 wake_up_interruptible(&ctx->cq_wait);
483 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
484 }
485 }
486}
487
Jens Axboe561fb042019-10-24 07:25:42 -0600488static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600489{
Jens Axboe561fb042019-10-24 07:25:42 -0600490 u8 opcode = READ_ONCE(sqe->opcode);
491
492 return !(opcode == IORING_OP_READ_FIXED ||
493 opcode == IORING_OP_WRITE_FIXED);
494}
495
496static inline bool io_prep_async_work(struct io_kiocb *req)
497{
498 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600499
Jens Axboe6cc47d12019-09-18 11:18:23 -0600500 if (req->submit.sqe) {
501 switch (req->submit.sqe->opcode) {
502 case IORING_OP_WRITEV:
503 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600504 do_hashed = true;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600505 break;
506 }
Jens Axboe561fb042019-10-24 07:25:42 -0600507 if (io_sqe_needs_user(req->submit.sqe))
508 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600509 }
510
Jens Axboe561fb042019-10-24 07:25:42 -0600511 return do_hashed;
512}
513
514static inline void io_queue_async_work(struct io_ring_ctx *ctx,
515 struct io_kiocb *req)
516{
517 bool do_hashed = io_prep_async_work(req);
518
519 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
520 req->flags);
521 if (!do_hashed) {
522 io_wq_enqueue(ctx->io_wq, &req->work);
523 } else {
524 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
525 file_inode(req->file));
526 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600527}
528
Jens Axboe5262f562019-09-17 12:26:57 -0600529static void io_kill_timeout(struct io_kiocb *req)
530{
531 int ret;
532
533 ret = hrtimer_try_to_cancel(&req->timeout.timer);
534 if (ret != -1) {
535 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600536 list_del_init(&req->list);
Jens Axboe5262f562019-09-17 12:26:57 -0600537 io_cqring_fill_event(req->ctx, req->user_data, 0);
538 __io_free_req(req);
539 }
540}
541
542static void io_kill_timeouts(struct io_ring_ctx *ctx)
543{
544 struct io_kiocb *req, *tmp;
545
546 spin_lock_irq(&ctx->completion_lock);
547 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
548 io_kill_timeout(req);
549 spin_unlock_irq(&ctx->completion_lock);
550}
551
Jens Axboede0617e2019-04-06 21:51:27 -0600552static void io_commit_cqring(struct io_ring_ctx *ctx)
553{
554 struct io_kiocb *req;
555
Jens Axboe5262f562019-09-17 12:26:57 -0600556 while ((req = io_get_timeout_req(ctx)) != NULL)
557 io_kill_timeout(req);
558
Jens Axboede0617e2019-04-06 21:51:27 -0600559 __io_commit_cqring(ctx);
560
561 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800562 if (req->flags & REQ_F_SHADOW_DRAIN) {
563 /* Just for drain, free it. */
564 __io_free_req(req);
565 continue;
566 }
Jens Axboede0617e2019-04-06 21:51:27 -0600567 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600568 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600569 }
570}
571
Jens Axboe2b188cc2019-01-07 10:46:33 -0700572static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
573{
Hristo Venev75b28af2019-08-26 17:23:46 +0000574 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700575 unsigned tail;
576
577 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200578 /*
579 * writes to the cq entry need to come after reading head; the
580 * control dependency is enough as we're using WRITE_ONCE to
581 * fill the cq entry
582 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000583 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700584 return NULL;
585
586 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000587 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700588}
589
590static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600591 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700592{
593 struct io_uring_cqe *cqe;
594
Jens Axboe51c3ff62019-11-03 06:52:50 -0700595 trace_io_uring_complete(ctx, ki_user_data, res);
596
Jens Axboe2b188cc2019-01-07 10:46:33 -0700597 /*
598 * If we can't get a cq entry, userspace overflowed the
599 * submission (by quite a lot). Increment the overflow count in
600 * the ring.
601 */
602 cqe = io_get_cqring(ctx);
603 if (cqe) {
604 WRITE_ONCE(cqe->user_data, ki_user_data);
605 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600606 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700607 } else {
Jens Axboe498ccd92019-10-25 10:04:25 -0600608 WRITE_ONCE(ctx->rings->cq_overflow,
609 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe2b188cc2019-01-07 10:46:33 -0700610 }
611}
612
Jens Axboe8c838782019-03-12 15:48:16 -0600613static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
614{
615 if (waitqueue_active(&ctx->wait))
616 wake_up(&ctx->wait);
617 if (waitqueue_active(&ctx->sqo_wait))
618 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600619 if (ctx->cq_ev_fd)
620 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600621}
622
623static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600624 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700625{
626 unsigned long flags;
627
628 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600629 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700630 io_commit_cqring(ctx);
631 spin_unlock_irqrestore(&ctx->completion_lock, flags);
632
Jens Axboe8c838782019-03-12 15:48:16 -0600633 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700634}
635
Jens Axboe2579f912019-01-09 09:10:43 -0700636static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
637 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700638{
Jens Axboefd6fab22019-03-14 16:30:06 -0600639 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700640 struct io_kiocb *req;
641
642 if (!percpu_ref_tryget(&ctx->refs))
643 return NULL;
644
Jens Axboe2579f912019-01-09 09:10:43 -0700645 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600646 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700647 if (unlikely(!req))
648 goto out;
649 } else if (!state->free_reqs) {
650 size_t sz;
651 int ret;
652
653 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600654 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
655
656 /*
657 * Bulk alloc is all-or-nothing. If we fail to get a batch,
658 * retry single alloc to be on the safe side.
659 */
660 if (unlikely(ret <= 0)) {
661 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
662 if (!state->reqs[0])
663 goto out;
664 ret = 1;
665 }
Jens Axboe2579f912019-01-09 09:10:43 -0700666 state->free_reqs = ret - 1;
667 state->cur_req = 1;
668 req = state->reqs[0];
669 } else {
670 req = state->reqs[state->cur_req];
671 state->free_reqs--;
672 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700673 }
674
Jens Axboe60c112b2019-06-21 10:20:18 -0600675 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700676 req->ctx = ctx;
677 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600678 /* one is dropped after submission, the other at completion */
679 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600680 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600681 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700682 return req;
683out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300684 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700685 return NULL;
686}
687
Jens Axboedef596e2019-01-09 08:59:42 -0700688static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
689{
690 if (*nr) {
691 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300692 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700693 *nr = 0;
694 }
695}
696
Jens Axboe9e645e112019-05-10 16:07:28 -0600697static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700698{
Jens Axboefcb323c2019-10-24 12:39:47 -0600699 struct io_ring_ctx *ctx = req->ctx;
700
Jens Axboe09bb8392019-03-13 12:39:28 -0600701 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
702 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600703 if (req->flags & REQ_F_INFLIGHT) {
704 unsigned long flags;
705
706 spin_lock_irqsave(&ctx->inflight_lock, flags);
707 list_del(&req->inflight_entry);
708 if (waitqueue_active(&ctx->inflight_wait))
709 wake_up(&ctx->inflight_wait);
710 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
711 }
712 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600713 kmem_cache_free(req_cachep, req);
714}
715
Jens Axboeba816ad2019-09-28 11:36:45 -0600716static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600717{
718 struct io_kiocb *nxt;
719
720 /*
721 * The list should never be empty when we are called here. But could
722 * potentially happen if the chain is messed up, check to be on the
723 * safe side.
724 */
725 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
726 if (nxt) {
727 list_del(&nxt->list);
728 if (!list_empty(&req->link_list)) {
729 INIT_LIST_HEAD(&nxt->link_list);
730 list_splice(&req->link_list, &nxt->link_list);
731 nxt->flags |= REQ_F_LINK;
732 }
733
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800734 nxt->flags |= REQ_F_LINK_DONE;
Jens Axboeba816ad2019-09-28 11:36:45 -0600735 /*
736 * If we're in async work, we can continue processing the chain
737 * in this context instead of having to queue up new async work.
738 */
Jens Axboe561fb042019-10-24 07:25:42 -0600739 if (nxtptr && current_work())
Jens Axboeba816ad2019-09-28 11:36:45 -0600740 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600741 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600742 io_queue_async_work(req->ctx, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600743 }
744}
745
746/*
747 * Called if REQ_F_LINK is set, and we fail the head request
748 */
749static void io_fail_links(struct io_kiocb *req)
750{
751 struct io_kiocb *link;
752
753 while (!list_empty(&req->link_list)) {
754 link = list_first_entry(&req->link_list, struct io_kiocb, list);
755 list_del(&link->list);
756
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200757 trace_io_uring_fail_link(req, link);
Jens Axboe9e645e112019-05-10 16:07:28 -0600758 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
759 __io_free_req(link);
760 }
761}
762
Jens Axboeba816ad2019-09-28 11:36:45 -0600763static void io_free_req(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600764{
765 /*
766 * If LINK is set, we have dependent requests in this chain. If we
767 * didn't fail this request, queue the first one up, moving any other
768 * dependencies to the next request. In case of failure, fail the rest
769 * of the chain.
770 */
771 if (req->flags & REQ_F_LINK) {
772 if (req->flags & REQ_F_FAIL_LINK)
773 io_fail_links(req);
774 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600775 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600776 }
777
778 __io_free_req(req);
779}
780
Jens Axboeba816ad2019-09-28 11:36:45 -0600781/*
782 * Drop reference to request, return next in chain (if there is one) if this
783 * was the last reference to this request.
784 */
785static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -0600786{
Jens Axboeba816ad2019-09-28 11:36:45 -0600787 struct io_kiocb *nxt = NULL;
788
Jens Axboee65ef562019-03-12 10:16:44 -0600789 if (refcount_dec_and_test(&req->refs))
Jens Axboeba816ad2019-09-28 11:36:45 -0600790 io_free_req(req, &nxt);
791
792 return nxt;
793}
794
795static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr)
796{
797 struct io_kiocb *nxt;
798
799 nxt = io_put_req_find_next(req);
800 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600801 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600802 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600803 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600804 io_queue_async_work(nxt->ctx, nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600805 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700806}
807
Hristo Venev75b28af2019-08-26 17:23:46 +0000808static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600809{
810 /* See comment at the top of this file */
811 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000812 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600813}
814
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300815static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
816{
817 struct io_rings *rings = ctx->rings;
818
819 /* make sure SQ entry isn't read before tail */
820 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
821}
822
Jens Axboedef596e2019-01-09 08:59:42 -0700823/*
824 * Find and free completed poll iocbs
825 */
826static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
827 struct list_head *done)
828{
829 void *reqs[IO_IOPOLL_BATCH];
830 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600831 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700832
Jens Axboe09bb8392019-03-13 12:39:28 -0600833 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700834 while (!list_empty(done)) {
835 req = list_first_entry(done, struct io_kiocb, list);
836 list_del(&req->list);
837
Jens Axboe9e645e112019-05-10 16:07:28 -0600838 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700839 (*nr_events)++;
840
Jens Axboe09bb8392019-03-13 12:39:28 -0600841 if (refcount_dec_and_test(&req->refs)) {
842 /* If we're not using fixed files, we have to pair the
843 * completion part with the file put. Use regular
844 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600845 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600846 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600847 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
848 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600849 reqs[to_free++] = req;
850 if (to_free == ARRAY_SIZE(reqs))
851 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700852 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -0600853 io_free_req(req, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -0700854 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700855 }
Jens Axboedef596e2019-01-09 08:59:42 -0700856 }
Jens Axboedef596e2019-01-09 08:59:42 -0700857
Jens Axboe09bb8392019-03-13 12:39:28 -0600858 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700859 io_free_req_many(ctx, reqs, &to_free);
860}
861
862static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
863 long min)
864{
865 struct io_kiocb *req, *tmp;
866 LIST_HEAD(done);
867 bool spin;
868 int ret;
869
870 /*
871 * Only spin for completions if we don't have multiple devices hanging
872 * off our complete list, and we're under the requested amount.
873 */
874 spin = !ctx->poll_multi_file && *nr_events < min;
875
876 ret = 0;
877 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
878 struct kiocb *kiocb = &req->rw;
879
880 /*
881 * Move completed entries to our local list. If we find a
882 * request that requires polling, break out and complete
883 * the done list first, if we have entries there.
884 */
885 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
886 list_move_tail(&req->list, &done);
887 continue;
888 }
889 if (!list_empty(&done))
890 break;
891
892 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
893 if (ret < 0)
894 break;
895
896 if (ret && spin)
897 spin = false;
898 ret = 0;
899 }
900
901 if (!list_empty(&done))
902 io_iopoll_complete(ctx, nr_events, &done);
903
904 return ret;
905}
906
907/*
908 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
909 * non-spinning poll check - we'll still enter the driver poll loop, but only
910 * as a non-spinning completion check.
911 */
912static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
913 long min)
914{
Jens Axboe08f54392019-08-21 22:19:11 -0600915 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700916 int ret;
917
918 ret = io_do_iopoll(ctx, nr_events, min);
919 if (ret < 0)
920 return ret;
921 if (!min || *nr_events >= min)
922 return 0;
923 }
924
925 return 1;
926}
927
928/*
929 * We can't just wait for polled events to come to us, we have to actively
930 * find and complete them.
931 */
932static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
933{
934 if (!(ctx->flags & IORING_SETUP_IOPOLL))
935 return;
936
937 mutex_lock(&ctx->uring_lock);
938 while (!list_empty(&ctx->poll_list)) {
939 unsigned int nr_events = 0;
940
941 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -0600942
943 /*
944 * Ensure we allow local-to-the-cpu processing to take place,
945 * in this case we need to ensure that we reap all events.
946 */
947 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -0700948 }
949 mutex_unlock(&ctx->uring_lock);
950}
951
Jens Axboe2b2ed972019-10-25 10:06:15 -0600952static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
953 long min)
Jens Axboedef596e2019-01-09 08:59:42 -0700954{
Jens Axboe2b2ed972019-10-25 10:06:15 -0600955 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700956
957 do {
958 int tmin = 0;
959
Jens Axboe500f9fb2019-08-19 12:15:59 -0600960 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600961 * Don't enter poll loop if we already have events pending.
962 * If we do, we can potentially be spinning for commands that
963 * already triggered a CQE (eg in error).
964 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000965 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600966 break;
967
968 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600969 * If a submit got punted to a workqueue, we can have the
970 * application entering polling for a command before it gets
971 * issued. That app will hold the uring_lock for the duration
972 * of the poll right here, so we need to take a breather every
973 * now and then to ensure that the issue has a chance to add
974 * the poll to the issued list. Otherwise we can spin here
975 * forever, while the workqueue is stuck trying to acquire the
976 * very same mutex.
977 */
978 if (!(++iters & 7)) {
979 mutex_unlock(&ctx->uring_lock);
980 mutex_lock(&ctx->uring_lock);
981 }
982
Jens Axboedef596e2019-01-09 08:59:42 -0700983 if (*nr_events < min)
984 tmin = min - *nr_events;
985
986 ret = io_iopoll_getevents(ctx, nr_events, tmin);
987 if (ret <= 0)
988 break;
989 ret = 0;
990 } while (min && !*nr_events && !need_resched());
991
Jens Axboe2b2ed972019-10-25 10:06:15 -0600992 return ret;
993}
994
995static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
996 long min)
997{
998 int ret;
999
1000 /*
1001 * We disallow the app entering submit/complete with polling, but we
1002 * still need to lock the ring to prevent racing with polled issue
1003 * that got punted to a workqueue.
1004 */
1005 mutex_lock(&ctx->uring_lock);
1006 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001007 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001008 return ret;
1009}
1010
Jens Axboe491381ce2019-10-17 09:20:46 -06001011static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001012{
Jens Axboe491381ce2019-10-17 09:20:46 -06001013 /*
1014 * Tell lockdep we inherited freeze protection from submission
1015 * thread.
1016 */
1017 if (req->flags & REQ_F_ISREG) {
1018 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001019
Jens Axboe491381ce2019-10-17 09:20:46 -06001020 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001021 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001022 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001023}
1024
Jens Axboeba816ad2019-09-28 11:36:45 -06001025static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001026{
1027 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1028
Jens Axboe491381ce2019-10-17 09:20:46 -06001029 if (kiocb->ki_flags & IOCB_WRITE)
1030 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001031
Jens Axboe9e645e112019-05-10 16:07:28 -06001032 if ((req->flags & REQ_F_LINK) && res != req->result)
1033 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001034 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001035}
1036
1037static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1038{
1039 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1040
1041 io_complete_rw_common(kiocb, res);
1042 io_put_req(req, NULL);
1043}
1044
1045static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1046{
1047 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1048
1049 io_complete_rw_common(kiocb, res);
1050 return io_put_req_find_next(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001051}
1052
Jens Axboedef596e2019-01-09 08:59:42 -07001053static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1054{
1055 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1056
Jens Axboe491381ce2019-10-17 09:20:46 -06001057 if (kiocb->ki_flags & IOCB_WRITE)
1058 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001059
Jens Axboe9e645e112019-05-10 16:07:28 -06001060 if ((req->flags & REQ_F_LINK) && res != req->result)
1061 req->flags |= REQ_F_FAIL_LINK;
1062 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001063 if (res != -EAGAIN)
1064 req->flags |= REQ_F_IOPOLL_COMPLETED;
1065}
1066
1067/*
1068 * After the iocb has been issued, it's safe to be found on the poll list.
1069 * Adding the kiocb to the list AFTER submission ensures that we don't
1070 * find it from a io_iopoll_getevents() thread before the issuer is done
1071 * accessing the kiocb cookie.
1072 */
1073static void io_iopoll_req_issued(struct io_kiocb *req)
1074{
1075 struct io_ring_ctx *ctx = req->ctx;
1076
1077 /*
1078 * Track whether we have multiple files in our lists. This will impact
1079 * how we do polling eventually, not spinning if we're on potentially
1080 * different devices.
1081 */
1082 if (list_empty(&ctx->poll_list)) {
1083 ctx->poll_multi_file = false;
1084 } else if (!ctx->poll_multi_file) {
1085 struct io_kiocb *list_req;
1086
1087 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1088 list);
1089 if (list_req->rw.ki_filp != req->rw.ki_filp)
1090 ctx->poll_multi_file = true;
1091 }
1092
1093 /*
1094 * For fast devices, IO may have already completed. If it has, add
1095 * it to the front so we find it first.
1096 */
1097 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1098 list_add(&req->list, &ctx->poll_list);
1099 else
1100 list_add_tail(&req->list, &ctx->poll_list);
1101}
1102
Jens Axboe3d6770f2019-04-13 11:50:54 -06001103static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001104{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001105 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001106 int diff = state->has_refs - state->used_refs;
1107
1108 if (diff)
1109 fput_many(state->file, diff);
1110 state->file = NULL;
1111 }
1112}
1113
1114/*
1115 * Get as many references to a file as we have IOs left in this submission,
1116 * assuming most submissions are for one file, or at least that each file
1117 * has more than one submission.
1118 */
1119static struct file *io_file_get(struct io_submit_state *state, int fd)
1120{
1121 if (!state)
1122 return fget(fd);
1123
1124 if (state->file) {
1125 if (state->fd == fd) {
1126 state->used_refs++;
1127 state->ios_left--;
1128 return state->file;
1129 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001130 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001131 }
1132 state->file = fget_many(fd, state->ios_left);
1133 if (!state->file)
1134 return NULL;
1135
1136 state->fd = fd;
1137 state->has_refs = state->ios_left;
1138 state->used_refs = 1;
1139 state->ios_left--;
1140 return state->file;
1141}
1142
Jens Axboe2b188cc2019-01-07 10:46:33 -07001143/*
1144 * If we tracked the file through the SCM inflight mechanism, we could support
1145 * any file. For now, just ensure that anything potentially problematic is done
1146 * inline.
1147 */
1148static bool io_file_supports_async(struct file *file)
1149{
1150 umode_t mode = file_inode(file)->i_mode;
1151
1152 if (S_ISBLK(mode) || S_ISCHR(mode))
1153 return true;
1154 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1155 return true;
1156
1157 return false;
1158}
1159
Jens Axboe6c271ce2019-01-10 11:22:30 -07001160static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001161 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001162{
Jens Axboe6c271ce2019-01-10 11:22:30 -07001163 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001164 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001165 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001166 unsigned ioprio;
1167 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001168
Jens Axboe09bb8392019-03-13 12:39:28 -06001169 if (!req->file)
1170 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001171
Jens Axboe491381ce2019-10-17 09:20:46 -06001172 if (S_ISREG(file_inode(req->file)->i_mode))
1173 req->flags |= REQ_F_ISREG;
1174
1175 /*
1176 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1177 * we know to async punt it even if it was opened O_NONBLOCK
1178 */
1179 if (force_nonblock && !io_file_supports_async(req->file)) {
1180 req->flags |= REQ_F_MUST_PUNT;
1181 return -EAGAIN;
1182 }
Jens Axboe6b063142019-01-10 22:13:58 -07001183
Jens Axboe2b188cc2019-01-07 10:46:33 -07001184 kiocb->ki_pos = READ_ONCE(sqe->off);
1185 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1186 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1187
1188 ioprio = READ_ONCE(sqe->ioprio);
1189 if (ioprio) {
1190 ret = ioprio_check_cap(ioprio);
1191 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001192 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001193
1194 kiocb->ki_ioprio = ioprio;
1195 } else
1196 kiocb->ki_ioprio = get_current_ioprio();
1197
1198 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1199 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001200 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001201
1202 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001203 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1204 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001205 req->flags |= REQ_F_NOWAIT;
1206
1207 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001208 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001209
Jens Axboedef596e2019-01-09 08:59:42 -07001210 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001211 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1212 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001213 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001214
Jens Axboedef596e2019-01-09 08:59:42 -07001215 kiocb->ki_flags |= IOCB_HIPRI;
1216 kiocb->ki_complete = io_complete_rw_iopoll;
1217 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001218 if (kiocb->ki_flags & IOCB_HIPRI)
1219 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001220 kiocb->ki_complete = io_complete_rw;
1221 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001222 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001223}
1224
1225static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1226{
1227 switch (ret) {
1228 case -EIOCBQUEUED:
1229 break;
1230 case -ERESTARTSYS:
1231 case -ERESTARTNOINTR:
1232 case -ERESTARTNOHAND:
1233 case -ERESTART_RESTARTBLOCK:
1234 /*
1235 * We can't just restart the syscall, since previously
1236 * submitted sqes may already be in progress. Just fail this
1237 * IO with EINTR.
1238 */
1239 ret = -EINTR;
1240 /* fall through */
1241 default:
1242 kiocb->ki_complete(kiocb, ret, 0);
1243 }
1244}
1245
Jens Axboeba816ad2019-09-28 11:36:45 -06001246static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1247 bool in_async)
1248{
1249 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1250 *nxt = __io_complete_rw(kiocb, ret);
1251 else
1252 io_rw_done(kiocb, ret);
1253}
1254
Jens Axboeedafcce2019-01-09 09:16:05 -07001255static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1256 const struct io_uring_sqe *sqe,
1257 struct iov_iter *iter)
1258{
1259 size_t len = READ_ONCE(sqe->len);
1260 struct io_mapped_ubuf *imu;
1261 unsigned index, buf_index;
1262 size_t offset;
1263 u64 buf_addr;
1264
1265 /* attempt to use fixed buffers without having provided iovecs */
1266 if (unlikely(!ctx->user_bufs))
1267 return -EFAULT;
1268
1269 buf_index = READ_ONCE(sqe->buf_index);
1270 if (unlikely(buf_index >= ctx->nr_user_bufs))
1271 return -EFAULT;
1272
1273 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1274 imu = &ctx->user_bufs[index];
1275 buf_addr = READ_ONCE(sqe->addr);
1276
1277 /* overflow */
1278 if (buf_addr + len < buf_addr)
1279 return -EFAULT;
1280 /* not inside the mapped region */
1281 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1282 return -EFAULT;
1283
1284 /*
1285 * May not be a start of buffer, set size appropriately
1286 * and advance us to the beginning.
1287 */
1288 offset = buf_addr - imu->ubuf;
1289 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001290
1291 if (offset) {
1292 /*
1293 * Don't use iov_iter_advance() here, as it's really slow for
1294 * using the latter parts of a big fixed buffer - it iterates
1295 * over each segment manually. We can cheat a bit here, because
1296 * we know that:
1297 *
1298 * 1) it's a BVEC iter, we set it up
1299 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1300 * first and last bvec
1301 *
1302 * So just find our index, and adjust the iterator afterwards.
1303 * If the offset is within the first bvec (or the whole first
1304 * bvec, just use iov_iter_advance(). This makes it easier
1305 * since we can just skip the first segment, which may not
1306 * be PAGE_SIZE aligned.
1307 */
1308 const struct bio_vec *bvec = imu->bvec;
1309
1310 if (offset <= bvec->bv_len) {
1311 iov_iter_advance(iter, offset);
1312 } else {
1313 unsigned long seg_skip;
1314
1315 /* skip first vec */
1316 offset -= bvec->bv_len;
1317 seg_skip = 1 + (offset >> PAGE_SHIFT);
1318
1319 iter->bvec = bvec + seg_skip;
1320 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001321 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001322 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001323 }
1324 }
1325
Jens Axboeedafcce2019-01-09 09:16:05 -07001326 return 0;
1327}
1328
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001329static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1330 const struct sqe_submit *s, struct iovec **iovec,
1331 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001332{
1333 const struct io_uring_sqe *sqe = s->sqe;
1334 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1335 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001336 u8 opcode;
1337
1338 /*
1339 * We're reading ->opcode for the second time, but the first read
1340 * doesn't care whether it's _FIXED or not, so it doesn't matter
1341 * whether ->opcode changes concurrently. The first read does care
1342 * about whether it is a READ or a WRITE, so we don't trust this read
1343 * for that purpose and instead let the caller pass in the read/write
1344 * flag.
1345 */
1346 opcode = READ_ONCE(sqe->opcode);
1347 if (opcode == IORING_OP_READ_FIXED ||
1348 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001349 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001350 *iovec = NULL;
1351 return ret;
1352 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001353
1354 if (!s->has_user)
1355 return -EFAULT;
1356
1357#ifdef CONFIG_COMPAT
1358 if (ctx->compat)
1359 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1360 iovec, iter);
1361#endif
1362
1363 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1364}
1365
Jens Axboe32960612019-09-23 11:05:34 -06001366/*
1367 * For files that don't have ->read_iter() and ->write_iter(), handle them
1368 * by looping over ->read() or ->write() manually.
1369 */
1370static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1371 struct iov_iter *iter)
1372{
1373 ssize_t ret = 0;
1374
1375 /*
1376 * Don't support polled IO through this interface, and we can't
1377 * support non-blocking either. For the latter, this just causes
1378 * the kiocb to be handled from an async context.
1379 */
1380 if (kiocb->ki_flags & IOCB_HIPRI)
1381 return -EOPNOTSUPP;
1382 if (kiocb->ki_flags & IOCB_NOWAIT)
1383 return -EAGAIN;
1384
1385 while (iov_iter_count(iter)) {
1386 struct iovec iovec = iov_iter_iovec(iter);
1387 ssize_t nr;
1388
1389 if (rw == READ) {
1390 nr = file->f_op->read(file, iovec.iov_base,
1391 iovec.iov_len, &kiocb->ki_pos);
1392 } else {
1393 nr = file->f_op->write(file, iovec.iov_base,
1394 iovec.iov_len, &kiocb->ki_pos);
1395 }
1396
1397 if (nr < 0) {
1398 if (!ret)
1399 ret = nr;
1400 break;
1401 }
1402 ret += nr;
1403 if (nr != iovec.iov_len)
1404 break;
1405 iov_iter_advance(iter, nr);
1406 }
1407
1408 return ret;
1409}
1410
Jens Axboee0c5c572019-03-12 10:18:47 -06001411static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001412 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001413{
1414 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1415 struct kiocb *kiocb = &req->rw;
1416 struct iov_iter iter;
1417 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001418 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001419 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420
Jens Axboe8358e3a2019-04-23 08:17:58 -06001421 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001422 if (ret)
1423 return ret;
1424 file = kiocb->ki_filp;
1425
Jens Axboe2b188cc2019-01-07 10:46:33 -07001426 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001427 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001428
1429 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001430 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001431 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001432
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001433 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001434 if (req->flags & REQ_F_LINK)
1435 req->result = read_size;
1436
Jens Axboe31b51512019-01-18 22:56:34 -07001437 iov_count = iov_iter_count(&iter);
1438 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001439 if (!ret) {
1440 ssize_t ret2;
1441
Jens Axboe32960612019-09-23 11:05:34 -06001442 if (file->f_op->read_iter)
1443 ret2 = call_read_iter(file, kiocb, &iter);
1444 else
1445 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1446
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001447 /*
1448 * In case of a short read, punt to async. This can happen
1449 * if we have data partially cached. Alternatively we can
1450 * return the short read, in which case the application will
1451 * need to issue another SQE and wait for it. That SQE will
1452 * need async punt anyway, so it's more efficient to do it
1453 * here.
1454 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001455 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1456 (req->flags & REQ_F_ISREG) &&
1457 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001458 ret2 = -EAGAIN;
1459 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001460 if (!force_nonblock || ret2 != -EAGAIN)
Jackie Liuba5290c2019-10-09 09:19:59 +08001461 kiocb_done(kiocb, ret2, nxt, s->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001462 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001463 ret = -EAGAIN;
1464 }
1465 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001466 return ret;
1467}
1468
Jens Axboee0c5c572019-03-12 10:18:47 -06001469static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001470 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001471{
1472 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1473 struct kiocb *kiocb = &req->rw;
1474 struct iov_iter iter;
1475 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001476 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001477 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001478
Jens Axboe8358e3a2019-04-23 08:17:58 -06001479 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001480 if (ret)
1481 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001482
Jens Axboe2b188cc2019-01-07 10:46:33 -07001483 file = kiocb->ki_filp;
1484 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001485 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001486
1487 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001488 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001489 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001490
Jens Axboe9e645e112019-05-10 16:07:28 -06001491 if (req->flags & REQ_F_LINK)
1492 req->result = ret;
1493
Jens Axboe31b51512019-01-18 22:56:34 -07001494 iov_count = iov_iter_count(&iter);
1495
1496 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001497 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001498 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001499
1500 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001501 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001502 ssize_t ret2;
1503
Jens Axboe2b188cc2019-01-07 10:46:33 -07001504 /*
1505 * Open-code file_start_write here to grab freeze protection,
1506 * which will be released by another thread in
1507 * io_complete_rw(). Fool lockdep by telling it the lock got
1508 * released so that it doesn't complain about the held lock when
1509 * we return to userspace.
1510 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001511 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001512 __sb_start_write(file_inode(file)->i_sb,
1513 SB_FREEZE_WRITE, true);
1514 __sb_writers_release(file_inode(file)->i_sb,
1515 SB_FREEZE_WRITE);
1516 }
1517 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001518
Jens Axboe32960612019-09-23 11:05:34 -06001519 if (file->f_op->write_iter)
1520 ret2 = call_write_iter(file, kiocb, &iter);
1521 else
1522 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001523 if (!force_nonblock || ret2 != -EAGAIN)
Jackie Liuba5290c2019-10-09 09:19:59 +08001524 kiocb_done(kiocb, ret2, nxt, s->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001525 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001526 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001527 }
Jens Axboe31b51512019-01-18 22:56:34 -07001528out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001529 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001530 return ret;
1531}
1532
1533/*
1534 * IORING_OP_NOP just posts a completion event, nothing else.
1535 */
1536static int io_nop(struct io_kiocb *req, u64 user_data)
1537{
1538 struct io_ring_ctx *ctx = req->ctx;
1539 long err = 0;
1540
Jens Axboedef596e2019-01-09 08:59:42 -07001541 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1542 return -EINVAL;
1543
Jens Axboec71ffb62019-05-13 20:58:29 -06001544 io_cqring_add_event(ctx, user_data, err);
Jens Axboeba816ad2019-09-28 11:36:45 -06001545 io_put_req(req, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001546 return 0;
1547}
1548
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001549static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1550{
Jens Axboe6b063142019-01-10 22:13:58 -07001551 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001552
Jens Axboe09bb8392019-03-13 12:39:28 -06001553 if (!req->file)
1554 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001555
Jens Axboe6b063142019-01-10 22:13:58 -07001556 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001557 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001558 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001559 return -EINVAL;
1560
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001561 return 0;
1562}
1563
1564static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001565 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001566{
1567 loff_t sqe_off = READ_ONCE(sqe->off);
1568 loff_t sqe_len = READ_ONCE(sqe->len);
1569 loff_t end = sqe_off + sqe_len;
1570 unsigned fsync_flags;
1571 int ret;
1572
1573 fsync_flags = READ_ONCE(sqe->fsync_flags);
1574 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1575 return -EINVAL;
1576
1577 ret = io_prep_fsync(req, sqe);
1578 if (ret)
1579 return ret;
1580
1581 /* fsync always requires a blocking context */
1582 if (force_nonblock)
1583 return -EAGAIN;
1584
1585 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1586 end > 0 ? end : LLONG_MAX,
1587 fsync_flags & IORING_FSYNC_DATASYNC);
1588
Jens Axboe9e645e112019-05-10 16:07:28 -06001589 if (ret < 0 && (req->flags & REQ_F_LINK))
1590 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001591 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001592 io_put_req(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001593 return 0;
1594}
1595
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001596static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1597{
1598 struct io_ring_ctx *ctx = req->ctx;
1599 int ret = 0;
1600
1601 if (!req->file)
1602 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001603
1604 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1605 return -EINVAL;
1606 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1607 return -EINVAL;
1608
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001609 return ret;
1610}
1611
1612static int io_sync_file_range(struct io_kiocb *req,
1613 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001614 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001615 bool force_nonblock)
1616{
1617 loff_t sqe_off;
1618 loff_t sqe_len;
1619 unsigned flags;
1620 int ret;
1621
1622 ret = io_prep_sfr(req, sqe);
1623 if (ret)
1624 return ret;
1625
1626 /* sync_file_range always requires a blocking context */
1627 if (force_nonblock)
1628 return -EAGAIN;
1629
1630 sqe_off = READ_ONCE(sqe->off);
1631 sqe_len = READ_ONCE(sqe->len);
1632 flags = READ_ONCE(sqe->sync_range_flags);
1633
1634 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1635
Jens Axboe9e645e112019-05-10 16:07:28 -06001636 if (ret < 0 && (req->flags & REQ_F_LINK))
1637 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001638 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001639 io_put_req(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001640 return 0;
1641}
1642
Jens Axboe0fa03c62019-04-19 13:34:07 -06001643#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001644static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001645 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001646 long (*fn)(struct socket *, struct user_msghdr __user *,
1647 unsigned int))
1648{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001649 struct socket *sock;
1650 int ret;
1651
1652 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1653 return -EINVAL;
1654
1655 sock = sock_from_file(req->file, &ret);
1656 if (sock) {
1657 struct user_msghdr __user *msg;
1658 unsigned flags;
1659
1660 flags = READ_ONCE(sqe->msg_flags);
1661 if (flags & MSG_DONTWAIT)
1662 req->flags |= REQ_F_NOWAIT;
1663 else if (force_nonblock)
1664 flags |= MSG_DONTWAIT;
1665
1666 msg = (struct user_msghdr __user *) (unsigned long)
1667 READ_ONCE(sqe->addr);
1668
Jens Axboeaa1fa282019-04-19 13:38:09 -06001669 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001670 if (force_nonblock && ret == -EAGAIN)
1671 return ret;
1672 }
1673
1674 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001675 io_put_req(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001676 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001677}
1678#endif
1679
1680static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001681 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001682{
1683#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001684 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1685 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001686#else
1687 return -EOPNOTSUPP;
1688#endif
1689}
1690
1691static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001692 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001693{
1694#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001695 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1696 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001697#else
1698 return -EOPNOTSUPP;
1699#endif
1700}
1701
Jens Axboe17f2fe32019-10-17 14:42:58 -06001702static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1703 struct io_kiocb **nxt, bool force_nonblock)
1704{
1705#if defined(CONFIG_NET)
1706 struct sockaddr __user *addr;
1707 int __user *addr_len;
1708 unsigned file_flags;
1709 int flags, ret;
1710
1711 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1712 return -EINVAL;
1713 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1714 return -EINVAL;
1715
1716 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1717 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1718 flags = READ_ONCE(sqe->accept_flags);
1719 file_flags = force_nonblock ? O_NONBLOCK : 0;
1720
1721 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1722 if (ret == -EAGAIN && force_nonblock) {
1723 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1724 return -EAGAIN;
1725 }
1726 if (ret < 0 && (req->flags & REQ_F_LINK))
1727 req->flags |= REQ_F_FAIL_LINK;
1728 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1729 io_put_req(req, nxt);
1730 return 0;
1731#else
1732 return -EOPNOTSUPP;
1733#endif
1734}
1735
Jens Axboe221c5eb2019-01-17 09:41:58 -07001736static void io_poll_remove_one(struct io_kiocb *req)
1737{
1738 struct io_poll_iocb *poll = &req->poll;
1739
1740 spin_lock(&poll->head->lock);
1741 WRITE_ONCE(poll->canceled, true);
1742 if (!list_empty(&poll->wait.entry)) {
1743 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001744 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001745 }
1746 spin_unlock(&poll->head->lock);
1747
1748 list_del_init(&req->list);
1749}
1750
1751static void io_poll_remove_all(struct io_ring_ctx *ctx)
1752{
1753 struct io_kiocb *req;
1754
1755 spin_lock_irq(&ctx->completion_lock);
1756 while (!list_empty(&ctx->cancel_list)) {
1757 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1758 io_poll_remove_one(req);
1759 }
1760 spin_unlock_irq(&ctx->completion_lock);
1761}
1762
1763/*
1764 * Find a running poll command that matches one specified in sqe->addr,
1765 * and remove it if found.
1766 */
1767static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1768{
1769 struct io_ring_ctx *ctx = req->ctx;
1770 struct io_kiocb *poll_req, *next;
1771 int ret = -ENOENT;
1772
1773 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1774 return -EINVAL;
1775 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1776 sqe->poll_events)
1777 return -EINVAL;
1778
1779 spin_lock_irq(&ctx->completion_lock);
1780 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1781 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1782 io_poll_remove_one(poll_req);
1783 ret = 0;
1784 break;
1785 }
1786 }
1787 spin_unlock_irq(&ctx->completion_lock);
1788
Jens Axboec71ffb62019-05-13 20:58:29 -06001789 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001790 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001791 return 0;
1792}
1793
Jens Axboe8c838782019-03-12 15:48:16 -06001794static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1795 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001796{
Jens Axboe8c838782019-03-12 15:48:16 -06001797 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001798 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001799 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001800}
1801
Jens Axboe561fb042019-10-24 07:25:42 -06001802static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001803{
Jens Axboe561fb042019-10-24 07:25:42 -06001804 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001805 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1806 struct io_poll_iocb *poll = &req->poll;
1807 struct poll_table_struct pt = { ._key = poll->events };
1808 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07001809 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001810 __poll_t mask = 0;
1811
Jens Axboe561fb042019-10-24 07:25:42 -06001812 if (work->flags & IO_WQ_WORK_CANCEL)
1813 WRITE_ONCE(poll->canceled, true);
1814
Jens Axboe221c5eb2019-01-17 09:41:58 -07001815 if (!READ_ONCE(poll->canceled))
1816 mask = vfs_poll(poll->file, &pt) & poll->events;
1817
1818 /*
1819 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1820 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1821 * synchronize with them. In the cancellation case the list_del_init
1822 * itself is not actually needed, but harmless so we keep it in to
1823 * avoid further branches in the fast path.
1824 */
1825 spin_lock_irq(&ctx->completion_lock);
1826 if (!mask && !READ_ONCE(poll->canceled)) {
1827 add_wait_queue(poll->head, &poll->wait);
1828 spin_unlock_irq(&ctx->completion_lock);
1829 return;
1830 }
1831 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001832 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001833 spin_unlock_irq(&ctx->completion_lock);
1834
Jens Axboe8c838782019-03-12 15:48:16 -06001835 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07001836
1837 io_put_req(req, &nxt);
1838 if (nxt)
1839 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001840}
1841
1842static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1843 void *key)
1844{
1845 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1846 wait);
1847 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1848 struct io_ring_ctx *ctx = req->ctx;
1849 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001850 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001851
1852 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001853 if (mask && !(mask & poll->events))
1854 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001855
1856 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001857
1858 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1859 list_del(&req->list);
1860 io_poll_complete(ctx, req, mask);
1861 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1862
1863 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001864 io_put_req(req, NULL);
Jens Axboe8c838782019-03-12 15:48:16 -06001865 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001866 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001867 }
1868
Jens Axboe221c5eb2019-01-17 09:41:58 -07001869 return 1;
1870}
1871
1872struct io_poll_table {
1873 struct poll_table_struct pt;
1874 struct io_kiocb *req;
1875 int error;
1876};
1877
1878static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1879 struct poll_table_struct *p)
1880{
1881 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1882
1883 if (unlikely(pt->req->poll.head)) {
1884 pt->error = -EINVAL;
1885 return;
1886 }
1887
1888 pt->error = 0;
1889 pt->req->poll.head = head;
1890 add_wait_queue(head, &pt->req->poll.wait);
1891}
1892
Jens Axboe89723d02019-11-05 15:32:58 -07001893static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1894 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001895{
1896 struct io_poll_iocb *poll = &req->poll;
1897 struct io_ring_ctx *ctx = req->ctx;
1898 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001899 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001900 __poll_t mask;
1901 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001902
1903 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1904 return -EINVAL;
1905 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1906 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001907 if (!poll->file)
1908 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001909
Jens Axboe6cc47d12019-09-18 11:18:23 -06001910 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06001911 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001912 events = READ_ONCE(sqe->poll_events);
1913 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1914
Jens Axboe221c5eb2019-01-17 09:41:58 -07001915 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001916 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001917 poll->canceled = false;
1918
1919 ipt.pt._qproc = io_poll_queue_proc;
1920 ipt.pt._key = poll->events;
1921 ipt.req = req;
1922 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1923
1924 /* initialized the list so that we can do list_empty checks */
1925 INIT_LIST_HEAD(&poll->wait.entry);
1926 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1927
Jens Axboe36703242019-07-25 10:20:18 -06001928 INIT_LIST_HEAD(&req->list);
1929
Jens Axboe221c5eb2019-01-17 09:41:58 -07001930 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001931
1932 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001933 if (likely(poll->head)) {
1934 spin_lock(&poll->head->lock);
1935 if (unlikely(list_empty(&poll->wait.entry))) {
1936 if (ipt.error)
1937 cancel = true;
1938 ipt.error = 0;
1939 mask = 0;
1940 }
1941 if (mask || ipt.error)
1942 list_del_init(&poll->wait.entry);
1943 else if (cancel)
1944 WRITE_ONCE(poll->canceled, true);
1945 else if (!poll->done) /* actually waiting for an event */
1946 list_add_tail(&req->list, &ctx->cancel_list);
1947 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001948 }
Jens Axboe8c838782019-03-12 15:48:16 -06001949 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001950 ipt.error = 0;
1951 io_poll_complete(ctx, req, mask);
1952 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001953 spin_unlock_irq(&ctx->completion_lock);
1954
Jens Axboe8c838782019-03-12 15:48:16 -06001955 if (mask) {
1956 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07001957 io_put_req(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001958 }
Jens Axboe8c838782019-03-12 15:48:16 -06001959 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001960}
1961
Jens Axboe5262f562019-09-17 12:26:57 -06001962static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1963{
1964 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06001965 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06001966 unsigned long flags;
1967
1968 req = container_of(timer, struct io_kiocb, timeout.timer);
1969 ctx = req->ctx;
1970 atomic_inc(&ctx->cq_timeouts);
1971
1972 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08001973 /*
Jens Axboe11365042019-10-16 09:08:32 -06001974 * We could be racing with timeout deletion. If the list is empty,
1975 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08001976 */
Jens Axboe842f9612019-10-29 12:34:10 -06001977 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06001978 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06001979
Jens Axboe11365042019-10-16 09:08:32 -06001980 /*
1981 * Adjust the reqs sequence before the current one because it
1982 * will consume a slot in the cq_ring and the the cq_tail
1983 * pointer will be increased, otherwise other timeout reqs may
1984 * return in advance without waiting for enough wait_nr.
1985 */
1986 prev = req;
1987 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
1988 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06001989 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06001990 }
Jens Axboe842f9612019-10-29 12:34:10 -06001991
1992 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1993 io_commit_cqring(ctx);
Jens Axboe5262f562019-09-17 12:26:57 -06001994 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1995
Jens Axboe842f9612019-10-29 12:34:10 -06001996 io_cqring_ev_posted(ctx);
1997 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06001998 return HRTIMER_NORESTART;
1999}
2000
2001/*
2002 * Remove or update an existing timeout command
2003 */
2004static int io_timeout_remove(struct io_kiocb *req,
2005 const struct io_uring_sqe *sqe)
2006{
2007 struct io_ring_ctx *ctx = req->ctx;
2008 struct io_kiocb *treq;
2009 int ret = -ENOENT;
2010 __u64 user_data;
2011 unsigned flags;
2012
2013 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2014 return -EINVAL;
2015 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2016 return -EINVAL;
2017 flags = READ_ONCE(sqe->timeout_flags);
2018 if (flags)
2019 return -EINVAL;
2020
2021 user_data = READ_ONCE(sqe->addr);
2022 spin_lock_irq(&ctx->completion_lock);
2023 list_for_each_entry(treq, &ctx->timeout_list, list) {
2024 if (user_data == treq->user_data) {
2025 list_del_init(&treq->list);
2026 ret = 0;
2027 break;
2028 }
2029 }
2030
2031 /* didn't find timeout */
2032 if (ret) {
2033fill_ev:
2034 io_cqring_fill_event(ctx, req->user_data, ret);
2035 io_commit_cqring(ctx);
2036 spin_unlock_irq(&ctx->completion_lock);
2037 io_cqring_ev_posted(ctx);
2038 io_put_req(req, NULL);
2039 return 0;
2040 }
2041
2042 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
2043 if (ret == -1) {
2044 ret = -EBUSY;
2045 goto fill_ev;
2046 }
2047
2048 io_cqring_fill_event(ctx, req->user_data, 0);
2049 io_cqring_fill_event(ctx, treq->user_data, -ECANCELED);
2050 io_commit_cqring(ctx);
2051 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002052 io_cqring_ev_posted(ctx);
2053
Jens Axboe11365042019-10-16 09:08:32 -06002054 io_put_req(treq, NULL);
Jens Axboeba816ad2019-09-28 11:36:45 -06002055 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002056 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002057}
2058
2059static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2060{
yangerkun5da0fb12019-10-15 21:59:29 +08002061 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002062 struct io_ring_ctx *ctx = req->ctx;
2063 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002064 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002065 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002066 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002067 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002068
2069 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2070 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002071 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2072 return -EINVAL;
2073 flags = READ_ONCE(sqe->timeout_flags);
2074 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002075 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002076
2077 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002078 return -EFAULT;
2079
Jens Axboe11365042019-10-16 09:08:32 -06002080 if (flags & IORING_TIMEOUT_ABS)
2081 mode = HRTIMER_MODE_ABS;
2082 else
2083 mode = HRTIMER_MODE_REL;
2084
2085 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2086
Jens Axboe5262f562019-09-17 12:26:57 -06002087 /*
2088 * sqe->off holds how many events that need to occur for this
2089 * timeout event to be satisfied.
2090 */
2091 count = READ_ONCE(sqe->off);
2092 if (!count)
2093 count = 1;
2094
2095 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002096 /* reuse it to store the count */
2097 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002098 req->flags |= REQ_F_TIMEOUT;
2099
2100 /*
2101 * Insertion sort, ensuring the first entry in the list is always
2102 * the one we need first.
2103 */
Jens Axboe5262f562019-09-17 12:26:57 -06002104 spin_lock_irq(&ctx->completion_lock);
2105 list_for_each_prev(entry, &ctx->timeout_list) {
2106 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002107 unsigned nxt_sq_head;
2108 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002109
yangerkun5da0fb12019-10-15 21:59:29 +08002110 /*
2111 * Since cached_sq_head + count - 1 can overflow, use type long
2112 * long to store it.
2113 */
2114 tmp = (long long)ctx->cached_sq_head + count - 1;
2115 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2116 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2117
2118 /*
2119 * cached_sq_head may overflow, and it will never overflow twice
2120 * once there is some timeout req still be valid.
2121 */
2122 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002123 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002124
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002125 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002126 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002127
2128 /*
2129 * Sequence of reqs after the insert one and itself should
2130 * be adjusted because each timeout req consumes a slot.
2131 */
2132 span++;
2133 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002134 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002135 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002136 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002137 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002138 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002139 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002140 return 0;
2141}
2142
Jens Axboe62755e32019-10-28 21:49:21 -06002143static bool io_cancel_cb(struct io_wq_work *work, void *data)
2144{
2145 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2146
2147 return req->user_data == (unsigned long) data;
2148}
2149
2150static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2151 struct io_kiocb **nxt)
2152{
2153 struct io_ring_ctx *ctx = req->ctx;
2154 enum io_wq_cancel cancel_ret;
2155 void *sqe_addr;
2156 int ret = 0;
2157
2158 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2159 return -EINVAL;
2160 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2161 sqe->cancel_flags)
2162 return -EINVAL;
2163
2164 sqe_addr = (void *) (unsigned long) READ_ONCE(sqe->addr);
2165 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2166 switch (cancel_ret) {
2167 case IO_WQ_CANCEL_OK:
2168 ret = 0;
2169 break;
2170 case IO_WQ_CANCEL_RUNNING:
2171 ret = -EALREADY;
2172 break;
2173 case IO_WQ_CANCEL_NOTFOUND:
2174 ret = -ENOENT;
2175 break;
2176 }
2177
2178 if (ret < 0 && (req->flags & REQ_F_LINK))
2179 req->flags |= REQ_F_FAIL_LINK;
2180 io_cqring_add_event(req->ctx, sqe->user_data, ret);
2181 io_put_req(req, nxt);
2182 return 0;
2183}
2184
Jens Axboede0617e2019-04-06 21:51:27 -06002185static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
2186 const struct io_uring_sqe *sqe)
2187{
2188 struct io_uring_sqe *sqe_copy;
2189
2190 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2191 return 0;
2192
2193 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2194 if (!sqe_copy)
2195 return -EAGAIN;
2196
2197 spin_lock_irq(&ctx->completion_lock);
2198 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2199 spin_unlock_irq(&ctx->completion_lock);
2200 kfree(sqe_copy);
2201 return 0;
2202 }
2203
2204 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2205 req->submit.sqe = sqe_copy;
2206
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002207 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002208 list_add_tail(&req->list, &ctx->defer_list);
2209 spin_unlock_irq(&ctx->completion_lock);
2210 return -EIOCBQUEUED;
2211}
2212
Jens Axboe2b188cc2019-01-07 10:46:33 -07002213static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboeba816ad2019-09-28 11:36:45 -06002214 const struct sqe_submit *s, struct io_kiocb **nxt,
2215 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002216{
Jens Axboee0c5c572019-03-12 10:18:47 -06002217 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002218
Jens Axboe9e645e112019-05-10 16:07:28 -06002219 req->user_data = READ_ONCE(s->sqe->user_data);
2220
Jens Axboe2b188cc2019-01-07 10:46:33 -07002221 opcode = READ_ONCE(s->sqe->opcode);
2222 switch (opcode) {
2223 case IORING_OP_NOP:
2224 ret = io_nop(req, req->user_data);
2225 break;
2226 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002227 if (unlikely(s->sqe->buf_index))
2228 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002229 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002230 break;
2231 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002232 if (unlikely(s->sqe->buf_index))
2233 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002234 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002235 break;
2236 case IORING_OP_READ_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002237 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002238 break;
2239 case IORING_OP_WRITE_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002240 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002241 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002242 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002243 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002244 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002245 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002246 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002247 break;
2248 case IORING_OP_POLL_REMOVE:
2249 ret = io_poll_remove(req, s->sqe);
2250 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002251 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002252 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002253 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002254 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002255 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002256 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002257 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002258 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002259 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002260 case IORING_OP_TIMEOUT:
2261 ret = io_timeout(req, s->sqe);
2262 break;
Jens Axboe11365042019-10-16 09:08:32 -06002263 case IORING_OP_TIMEOUT_REMOVE:
2264 ret = io_timeout_remove(req, s->sqe);
2265 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002266 case IORING_OP_ACCEPT:
2267 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2268 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002269 case IORING_OP_ASYNC_CANCEL:
2270 ret = io_async_cancel(req, s->sqe, nxt);
2271 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002272 default:
2273 ret = -EINVAL;
2274 break;
2275 }
2276
Jens Axboedef596e2019-01-09 08:59:42 -07002277 if (ret)
2278 return ret;
2279
2280 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002281 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002282 return -EAGAIN;
2283
2284 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002285 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002286 mutex_lock(&ctx->uring_lock);
2287 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002288 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002289 mutex_unlock(&ctx->uring_lock);
2290 }
2291
2292 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002293}
2294
Jens Axboe561fb042019-10-24 07:25:42 -06002295static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002296{
Jens Axboe561fb042019-10-24 07:25:42 -06002297 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002298 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002299 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -06002300 struct sqe_submit *s = &req->submit;
2301 const struct io_uring_sqe *sqe = s->sqe;
2302 struct io_kiocb *nxt = NULL;
2303 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002304
Jens Axboe561fb042019-10-24 07:25:42 -06002305 /* Ensure we clear previously set non-block flag */
2306 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002307
Jens Axboe561fb042019-10-24 07:25:42 -06002308 if (work->flags & IO_WQ_WORK_CANCEL)
2309 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002310
Jens Axboe561fb042019-10-24 07:25:42 -06002311 if (!ret) {
2312 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2313 s->in_async = true;
2314 do {
2315 ret = __io_submit_sqe(ctx, req, s, &nxt, false);
2316 /*
2317 * We can get EAGAIN for polled IO even though we're
2318 * forcing a sync submission from here, since we can't
2319 * wait for request slots on the block side.
2320 */
2321 if (ret != -EAGAIN)
2322 break;
2323 cond_resched();
2324 } while (1);
2325 }
Jens Axboe31b51512019-01-18 22:56:34 -07002326
Jens Axboe561fb042019-10-24 07:25:42 -06002327 /* drop submission reference */
2328 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002329
Jens Axboe561fb042019-10-24 07:25:42 -06002330 if (ret) {
2331 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002332 io_put_req(req, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07002333 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002334
Jens Axboe561fb042019-10-24 07:25:42 -06002335 /* async context always use a copy of the sqe */
2336 kfree(sqe);
2337
2338 /* if a dependent link is ready, pass it back */
2339 if (!ret && nxt) {
2340 io_prep_async_work(nxt);
2341 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002342 }
Jens Axboe31b51512019-01-18 22:56:34 -07002343}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002344
Jens Axboe09bb8392019-03-13 12:39:28 -06002345static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2346{
2347 int op = READ_ONCE(sqe->opcode);
2348
2349 switch (op) {
2350 case IORING_OP_NOP:
2351 case IORING_OP_POLL_REMOVE:
2352 return false;
2353 default:
2354 return true;
2355 }
2356}
2357
Jens Axboe65e19f52019-10-26 07:20:21 -06002358static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2359 int index)
2360{
2361 struct fixed_file_table *table;
2362
2363 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2364 return table->files[index & IORING_FILE_TABLE_MASK];
2365}
2366
Jens Axboe09bb8392019-03-13 12:39:28 -06002367static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2368 struct io_submit_state *state, struct io_kiocb *req)
2369{
2370 unsigned flags;
2371 int fd;
2372
2373 flags = READ_ONCE(s->sqe->flags);
2374 fd = READ_ONCE(s->sqe->fd);
2375
Jackie Liu4fe2c962019-09-09 20:50:40 +08002376 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002377 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002378 /*
2379 * All io need record the previous position, if LINK vs DARIN,
2380 * it can be used to mark the position of the first IO in the
2381 * link list.
2382 */
2383 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002384
Jens Axboe60c112b2019-06-21 10:20:18 -06002385 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002386 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002387
2388 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002389 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002390 (unsigned) fd >= ctx->nr_user_files))
2391 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002392 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002393 req->file = io_file_from_index(ctx, fd);
2394 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002395 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002396 req->flags |= REQ_F_FIXED_FILE;
2397 } else {
2398 if (s->needs_fixed_file)
2399 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002400 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002401 req->file = io_file_get(state, fd);
2402 if (unlikely(!req->file))
2403 return -EBADF;
2404 }
2405
2406 return 0;
2407}
2408
Jens Axboefcb323c2019-10-24 12:39:47 -06002409static int io_grab_files(struct io_ring_ctx *ctx, struct io_kiocb *req)
2410{
2411 int ret = -EBADF;
2412
2413 rcu_read_lock();
2414 spin_lock_irq(&ctx->inflight_lock);
2415 /*
2416 * We use the f_ops->flush() handler to ensure that we can flush
2417 * out work accessing these files if the fd is closed. Check if
2418 * the fd has changed since we started down this path, and disallow
2419 * this operation if it has.
2420 */
2421 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2422 list_add(&req->inflight_entry, &ctx->inflight_list);
2423 req->flags |= REQ_F_INFLIGHT;
2424 req->work.files = current->files;
2425 ret = 0;
2426 }
2427 spin_unlock_irq(&ctx->inflight_lock);
2428 rcu_read_unlock();
2429
2430 return ret;
2431}
2432
Jackie Liu4fe2c962019-09-09 20:50:40 +08002433static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002434 struct sqe_submit *s)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002435{
Jens Axboee0c5c572019-03-12 10:18:47 -06002436 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002437
Jens Axboeba816ad2019-09-28 11:36:45 -06002438 ret = __io_submit_sqe(ctx, req, s, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002439
2440 /*
2441 * We async punt it if the file wasn't marked NOWAIT, or if the file
2442 * doesn't support non-blocking read/write attempts
2443 */
2444 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2445 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002446 struct io_uring_sqe *sqe_copy;
2447
Jackie Liu954dab12019-09-18 10:37:52 +08002448 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002449 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002450 s->sqe = sqe_copy;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002451 memcpy(&req->submit, s, sizeof(*s));
Jens Axboefcb323c2019-10-24 12:39:47 -06002452 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2453 ret = io_grab_files(ctx, req);
2454 if (ret) {
2455 kfree(sqe_copy);
2456 goto err;
2457 }
2458 }
Jens Axboee65ef562019-03-12 10:16:44 -06002459
2460 /*
2461 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002462 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002463 */
Jens Axboefcb323c2019-10-24 12:39:47 -06002464 io_queue_async_work(ctx, req);
Jens Axboee65ef562019-03-12 10:16:44 -06002465 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002466 }
2467 }
Jens Axboee65ef562019-03-12 10:16:44 -06002468
2469 /* drop submission reference */
Jens Axboefcb323c2019-10-24 12:39:47 -06002470err:
Jens Axboeba816ad2019-09-28 11:36:45 -06002471 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002472
2473 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002474 if (ret) {
2475 io_cqring_add_event(ctx, req->user_data, ret);
2476 if (req->flags & REQ_F_LINK)
2477 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002478 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002479 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002480
2481 return ret;
2482}
2483
Jackie Liu4fe2c962019-09-09 20:50:40 +08002484static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002485 struct sqe_submit *s)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002486{
2487 int ret;
2488
2489 ret = io_req_defer(ctx, req, s->sqe);
2490 if (ret) {
2491 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002492 io_free_req(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002493 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2494 }
2495 return 0;
2496 }
2497
Jens Axboebc808bc2019-10-22 13:14:37 -06002498 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002499}
2500
2501static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002502 struct sqe_submit *s, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002503{
2504 int ret;
2505 int need_submit = false;
2506
2507 if (!shadow)
Jens Axboebc808bc2019-10-22 13:14:37 -06002508 return io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002509
2510 /*
2511 * Mark the first IO in link list as DRAIN, let all the following
2512 * IOs enter the defer list. all IO needs to be completed before link
2513 * list.
2514 */
2515 req->flags |= REQ_F_IO_DRAIN;
2516 ret = io_req_defer(ctx, req, s->sqe);
2517 if (ret) {
2518 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002519 io_free_req(req, NULL);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002520 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002521 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2522 return 0;
2523 }
2524 } else {
2525 /*
2526 * If ret == 0 means that all IOs in front of link io are
2527 * running done. let's queue link head.
2528 */
2529 need_submit = true;
2530 }
2531
2532 /* Insert shadow req to defer_list, blocking next IOs */
2533 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002534 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002535 list_add_tail(&shadow->list, &ctx->defer_list);
2536 spin_unlock_irq(&ctx->completion_lock);
2537
2538 if (need_submit)
Jens Axboebc808bc2019-10-22 13:14:37 -06002539 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002540
2541 return 0;
2542}
2543
Jens Axboe9e645e112019-05-10 16:07:28 -06002544#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2545
2546static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboebc808bc2019-10-22 13:14:37 -06002547 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002548{
2549 struct io_uring_sqe *sqe_copy;
2550 struct io_kiocb *req;
2551 int ret;
2552
2553 /* enforce forwards compatibility on users */
2554 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2555 ret = -EINVAL;
2556 goto err;
2557 }
2558
2559 req = io_get_req(ctx, state);
2560 if (unlikely(!req)) {
2561 ret = -EAGAIN;
2562 goto err;
2563 }
2564
2565 ret = io_req_set_file(ctx, s, state, req);
2566 if (unlikely(ret)) {
2567err_req:
Jens Axboeba816ad2019-09-28 11:36:45 -06002568 io_free_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002569err:
2570 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2571 return;
2572 }
2573
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002574 req->user_data = s->sqe->user_data;
2575
Jens Axboe9e645e112019-05-10 16:07:28 -06002576 /*
2577 * If we already have a head request, queue this one for async
2578 * submittal once the head completes. If we don't have a head but
2579 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2580 * submitted sync once the chain is complete. If none of those
2581 * conditions are true (normal request), then just queue it.
2582 */
2583 if (*link) {
2584 struct io_kiocb *prev = *link;
2585
2586 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2587 if (!sqe_copy) {
2588 ret = -EAGAIN;
2589 goto err_req;
2590 }
2591
2592 s->sqe = sqe_copy;
2593 memcpy(&req->submit, s, sizeof(*s));
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002594 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002595 list_add_tail(&req->list, &prev->link_list);
2596 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2597 req->flags |= REQ_F_LINK;
2598
2599 memcpy(&req->submit, s, sizeof(*s));
2600 INIT_LIST_HEAD(&req->link_list);
2601 *link = req;
2602 } else {
Jens Axboebc808bc2019-10-22 13:14:37 -06002603 io_queue_sqe(ctx, req, s);
Jens Axboe9e645e112019-05-10 16:07:28 -06002604 }
2605}
2606
Jens Axboe9a56a232019-01-09 09:06:50 -07002607/*
2608 * Batched submission is done, ensure local IO is flushed out.
2609 */
2610static void io_submit_state_end(struct io_submit_state *state)
2611{
2612 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002613 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002614 if (state->free_reqs)
2615 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2616 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002617}
2618
2619/*
2620 * Start submission side cache.
2621 */
2622static void io_submit_state_start(struct io_submit_state *state,
2623 struct io_ring_ctx *ctx, unsigned max_ios)
2624{
2625 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002626 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002627 state->file = NULL;
2628 state->ios_left = max_ios;
2629}
2630
Jens Axboe2b188cc2019-01-07 10:46:33 -07002631static void io_commit_sqring(struct io_ring_ctx *ctx)
2632{
Hristo Venev75b28af2019-08-26 17:23:46 +00002633 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002634
Hristo Venev75b28af2019-08-26 17:23:46 +00002635 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002636 /*
2637 * Ensure any loads from the SQEs are done at this point,
2638 * since once we write the new head, the application could
2639 * write new data to them.
2640 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002641 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002642 }
2643}
2644
2645/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002646 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2647 * that is mapped by userspace. This means that care needs to be taken to
2648 * ensure that reads are stable, as we cannot rely on userspace always
2649 * being a good citizen. If members of the sqe are validated and then later
2650 * used, it's important that those reads are done through READ_ONCE() to
2651 * prevent a re-load down the line.
2652 */
2653static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2654{
Hristo Venev75b28af2019-08-26 17:23:46 +00002655 struct io_rings *rings = ctx->rings;
2656 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002657 unsigned head;
2658
2659 /*
2660 * The cached sq head (or cq tail) serves two purposes:
2661 *
2662 * 1) allows us to batch the cost of updating the user visible
2663 * head updates.
2664 * 2) allows the kernel side to track the head on its own, even
2665 * though the application is the one updating it.
2666 */
2667 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002668 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002669 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002670 return false;
2671
Hristo Venev75b28af2019-08-26 17:23:46 +00002672 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002673 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002674 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002675 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002676 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002677 ctx->cached_sq_head++;
2678 return true;
2679 }
2680
2681 /* drop invalid entries */
2682 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002683 ctx->cached_sq_dropped++;
2684 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002685 return false;
2686}
2687
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002688static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002689 struct mm_struct **mm)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002690{
2691 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002692 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002693 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002694 bool prev_was_link = false;
2695 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002696 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002697
2698 if (nr > IO_PLUG_THRESHOLD) {
2699 io_submit_state_start(&state, ctx, nr);
2700 statep = &state;
2701 }
2702
2703 for (i = 0; i < nr; i++) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002704 struct sqe_submit s;
2705
2706 if (!io_get_sqring(ctx, &s))
2707 break;
2708
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002709 if (io_sqe_needs_user(s.sqe) && !*mm) {
2710 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
2711 if (!mm_fault) {
2712 use_mm(ctx->sqo_mm);
2713 *mm = ctx->sqo_mm;
2714 }
2715 }
2716
Jens Axboe9e645e112019-05-10 16:07:28 -06002717 /*
2718 * If previous wasn't linked and we have a linked command,
2719 * that's the end of the chain. Submit the previous link.
2720 */
2721 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002722 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002723 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002724 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002725 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002726 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06002727
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002728 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002729 if (!shadow_req) {
2730 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002731 if (unlikely(!shadow_req))
2732 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002733 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2734 refcount_dec(&shadow_req->refs);
2735 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002736 shadow_req->sequence = s.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002737 }
2738
Jackie Liua1041c22019-09-18 17:25:52 +08002739out:
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002740 s.has_user = *mm != NULL;
2741 s.in_async = true;
2742 s.needs_fixed_file = true;
Jens Axboe51c3ff62019-11-03 06:52:50 -07002743 trace_io_uring_submit_sqe(ctx, s.sqe->user_data, true, true);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002744 io_submit_sqe(ctx, &s, statep, &link);
2745 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002746 }
2747
Jens Axboe9e645e112019-05-10 16:07:28 -06002748 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002749 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002750 if (statep)
2751 io_submit_state_end(&state);
2752
2753 return submitted;
2754}
2755
2756static int io_sq_thread(void *data)
2757{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002758 struct io_ring_ctx *ctx = data;
2759 struct mm_struct *cur_mm = NULL;
2760 mm_segment_t old_fs;
2761 DEFINE_WAIT(wait);
2762 unsigned inflight;
2763 unsigned long timeout;
2764
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002765 complete(&ctx->sqo_thread_started);
2766
Jens Axboe6c271ce2019-01-10 11:22:30 -07002767 old_fs = get_fs();
2768 set_fs(USER_DS);
2769
2770 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002771 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002772 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002773
2774 if (inflight) {
2775 unsigned nr_events = 0;
2776
2777 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002778 /*
2779 * inflight is the count of the maximum possible
2780 * entries we submitted, but it can be smaller
2781 * if we dropped some of them. If we don't have
2782 * poll entries available, then we know that we
2783 * have nothing left to poll for. Reset the
2784 * inflight count to zero in that case.
2785 */
2786 mutex_lock(&ctx->uring_lock);
2787 if (!list_empty(&ctx->poll_list))
2788 __io_iopoll_check(ctx, &nr_events, 0);
2789 else
2790 inflight = 0;
2791 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002792 } else {
2793 /*
2794 * Normal IO, just pretend everything completed.
2795 * We don't have to poll completions for that.
2796 */
2797 nr_events = inflight;
2798 }
2799
2800 inflight -= nr_events;
2801 if (!inflight)
2802 timeout = jiffies + ctx->sq_thread_idle;
2803 }
2804
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002805 to_submit = io_sqring_entries(ctx);
2806 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002807 /*
2808 * We're polling. If we're within the defined idle
2809 * period, then let us spin without work before going
2810 * to sleep.
2811 */
2812 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002813 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002814 continue;
2815 }
2816
2817 /*
2818 * Drop cur_mm before scheduling, we can't hold it for
2819 * long periods (or over schedule()). Do this before
2820 * adding ourselves to the waitqueue, as the unuse/drop
2821 * may sleep.
2822 */
2823 if (cur_mm) {
2824 unuse_mm(cur_mm);
2825 mmput(cur_mm);
2826 cur_mm = NULL;
2827 }
2828
2829 prepare_to_wait(&ctx->sqo_wait, &wait,
2830 TASK_INTERRUPTIBLE);
2831
2832 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002833 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002834 /* make sure to read SQ tail after writing flags */
2835 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002836
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002837 to_submit = io_sqring_entries(ctx);
2838 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002839 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002840 finish_wait(&ctx->sqo_wait, &wait);
2841 break;
2842 }
2843 if (signal_pending(current))
2844 flush_signals(current);
2845 schedule();
2846 finish_wait(&ctx->sqo_wait, &wait);
2847
Hristo Venev75b28af2019-08-26 17:23:46 +00002848 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002849 continue;
2850 }
2851 finish_wait(&ctx->sqo_wait, &wait);
2852
Hristo Venev75b28af2019-08-26 17:23:46 +00002853 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002854 }
2855
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002856 to_submit = min(to_submit, ctx->sq_entries);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002857 inflight += io_submit_sqes(ctx, to_submit, &cur_mm);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002858
2859 /* Commit SQ ring head once we've consumed all SQEs */
2860 io_commit_sqring(ctx);
2861 }
2862
2863 set_fs(old_fs);
2864 if (cur_mm) {
2865 unuse_mm(cur_mm);
2866 mmput(cur_mm);
2867 }
Jens Axboe06058632019-04-13 09:26:03 -06002868
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002869 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002870
Jens Axboe6c271ce2019-01-10 11:22:30 -07002871 return 0;
2872}
2873
Jens Axboefcb323c2019-10-24 12:39:47 -06002874static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit,
2875 struct file *ring_file, int ring_fd)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002876{
Jens Axboe9a56a232019-01-09 09:06:50 -07002877 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002878 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002879 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002880 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002881 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002882
Jens Axboe9a56a232019-01-09 09:06:50 -07002883 if (to_submit > IO_PLUG_THRESHOLD) {
2884 io_submit_state_start(&state, ctx, to_submit);
2885 statep = &state;
2886 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002887
2888 for (i = 0; i < to_submit; i++) {
2889 struct sqe_submit s;
2890
2891 if (!io_get_sqring(ctx, &s))
2892 break;
2893
Jens Axboe9e645e112019-05-10 16:07:28 -06002894 /*
2895 * If previous wasn't linked and we have a linked command,
2896 * that's the end of the chain. Submit the previous link.
2897 */
2898 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002899 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002900 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002901 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002902 }
2903 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2904
Jackie Liu4fe2c962019-09-09 20:50:40 +08002905 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2906 if (!shadow_req) {
2907 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002908 if (unlikely(!shadow_req))
2909 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002910 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2911 refcount_dec(&shadow_req->refs);
2912 }
2913 shadow_req->sequence = s.sequence;
2914 }
2915
Jackie Liua1041c22019-09-18 17:25:52 +08002916out:
Jens Axboefcb323c2019-10-24 12:39:47 -06002917 s.ring_file = ring_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002918 s.has_user = true;
Jackie Liuba5290c2019-10-09 09:19:59 +08002919 s.in_async = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002920 s.needs_fixed_file = false;
Jens Axboefcb323c2019-10-24 12:39:47 -06002921 s.ring_fd = ring_fd;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002922 submit++;
Jens Axboe51c3ff62019-11-03 06:52:50 -07002923 trace_io_uring_submit_sqe(ctx, s.sqe->user_data, true, false);
Jens Axboebc808bc2019-10-22 13:14:37 -06002924 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002925 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002926
Jens Axboe9e645e112019-05-10 16:07:28 -06002927 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002928 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9a56a232019-01-09 09:06:50 -07002929 if (statep)
2930 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002931
Pavel Begunkov935d1e42019-10-25 12:31:31 +03002932 io_commit_sqring(ctx);
2933
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002934 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002935}
2936
Jens Axboebda52162019-09-24 13:47:15 -06002937struct io_wait_queue {
2938 struct wait_queue_entry wq;
2939 struct io_ring_ctx *ctx;
2940 unsigned to_wait;
2941 unsigned nr_timeouts;
2942};
2943
2944static inline bool io_should_wake(struct io_wait_queue *iowq)
2945{
2946 struct io_ring_ctx *ctx = iowq->ctx;
2947
2948 /*
2949 * Wake up if we have enough events, or if a timeout occured since we
2950 * started waiting. For timeouts, we always want to return to userspace,
2951 * regardless of event count.
2952 */
2953 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2954 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2955}
2956
2957static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2958 int wake_flags, void *key)
2959{
2960 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
2961 wq);
2962
2963 if (!io_should_wake(iowq))
2964 return -1;
2965
2966 return autoremove_wake_function(curr, mode, wake_flags, key);
2967}
2968
Jens Axboe2b188cc2019-01-07 10:46:33 -07002969/*
2970 * Wait until events become available, if we don't already have some. The
2971 * application must reap them itself, as they reside on the shared cq ring.
2972 */
2973static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2974 const sigset_t __user *sig, size_t sigsz)
2975{
Jens Axboebda52162019-09-24 13:47:15 -06002976 struct io_wait_queue iowq = {
2977 .wq = {
2978 .private = current,
2979 .func = io_wake_function,
2980 .entry = LIST_HEAD_INIT(iowq.wq.entry),
2981 },
2982 .ctx = ctx,
2983 .to_wait = min_events,
2984 };
Hristo Venev75b28af2019-08-26 17:23:46 +00002985 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08002986 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002987
Hristo Venev75b28af2019-08-26 17:23:46 +00002988 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002989 return 0;
2990
2991 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002992#ifdef CONFIG_COMPAT
2993 if (in_compat_syscall())
2994 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002995 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002996 else
2997#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002998 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002999
Jens Axboe2b188cc2019-01-07 10:46:33 -07003000 if (ret)
3001 return ret;
3002 }
3003
Jens Axboebda52162019-09-24 13:47:15 -06003004 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003005 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003006 do {
3007 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3008 TASK_INTERRUPTIBLE);
3009 if (io_should_wake(&iowq))
3010 break;
3011 schedule();
3012 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003013 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003014 break;
3015 }
3016 } while (1);
3017 finish_wait(&ctx->wait, &iowq.wq);
3018
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003019 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003020
Hristo Venev75b28af2019-08-26 17:23:46 +00003021 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003022}
3023
Jens Axboe6b063142019-01-10 22:13:58 -07003024static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3025{
3026#if defined(CONFIG_UNIX)
3027 if (ctx->ring_sock) {
3028 struct sock *sock = ctx->ring_sock->sk;
3029 struct sk_buff *skb;
3030
3031 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3032 kfree_skb(skb);
3033 }
3034#else
3035 int i;
3036
Jens Axboe65e19f52019-10-26 07:20:21 -06003037 for (i = 0; i < ctx->nr_user_files; i++) {
3038 struct file *file;
3039
3040 file = io_file_from_index(ctx, i);
3041 if (file)
3042 fput(file);
3043 }
Jens Axboe6b063142019-01-10 22:13:58 -07003044#endif
3045}
3046
3047static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3048{
Jens Axboe65e19f52019-10-26 07:20:21 -06003049 unsigned nr_tables, i;
3050
3051 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003052 return -ENXIO;
3053
3054 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003055 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3056 for (i = 0; i < nr_tables; i++)
3057 kfree(ctx->file_table[i].files);
3058 kfree(ctx->file_table);
3059 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003060 ctx->nr_user_files = 0;
3061 return 0;
3062}
3063
Jens Axboe6c271ce2019-01-10 11:22:30 -07003064static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3065{
3066 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003067 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003068 /*
3069 * The park is a bit of a work-around, without it we get
3070 * warning spews on shutdown with SQPOLL set and affinity
3071 * set to a single CPU.
3072 */
Jens Axboe06058632019-04-13 09:26:03 -06003073 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003074 kthread_stop(ctx->sqo_thread);
3075 ctx->sqo_thread = NULL;
3076 }
3077}
3078
Jens Axboe6b063142019-01-10 22:13:58 -07003079static void io_finish_async(struct io_ring_ctx *ctx)
3080{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003081 io_sq_thread_stop(ctx);
3082
Jens Axboe561fb042019-10-24 07:25:42 -06003083 if (ctx->io_wq) {
3084 io_wq_destroy(ctx->io_wq);
3085 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003086 }
3087}
3088
3089#if defined(CONFIG_UNIX)
3090static void io_destruct_skb(struct sk_buff *skb)
3091{
3092 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3093
Jens Axboe561fb042019-10-24 07:25:42 -06003094 if (ctx->io_wq)
3095 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003096
Jens Axboe6b063142019-01-10 22:13:58 -07003097 unix_destruct_scm(skb);
3098}
3099
3100/*
3101 * Ensure the UNIX gc is aware of our file set, so we are certain that
3102 * the io_uring can be safely unregistered on process exit, even if we have
3103 * loops in the file referencing.
3104 */
3105static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3106{
3107 struct sock *sk = ctx->ring_sock->sk;
3108 struct scm_fp_list *fpl;
3109 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003110 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003111
3112 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3113 unsigned long inflight = ctx->user->unix_inflight + nr;
3114
3115 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3116 return -EMFILE;
3117 }
3118
3119 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3120 if (!fpl)
3121 return -ENOMEM;
3122
3123 skb = alloc_skb(0, GFP_KERNEL);
3124 if (!skb) {
3125 kfree(fpl);
3126 return -ENOMEM;
3127 }
3128
3129 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003130
Jens Axboe08a45172019-10-03 08:11:03 -06003131 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003132 fpl->user = get_uid(ctx->user);
3133 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003134 struct file *file = io_file_from_index(ctx, i + offset);
3135
3136 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003137 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003138 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003139 unix_inflight(fpl->user, fpl->fp[nr_files]);
3140 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003141 }
3142
Jens Axboe08a45172019-10-03 08:11:03 -06003143 if (nr_files) {
3144 fpl->max = SCM_MAX_FD;
3145 fpl->count = nr_files;
3146 UNIXCB(skb).fp = fpl;
3147 skb->destructor = io_destruct_skb;
3148 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3149 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003150
Jens Axboe08a45172019-10-03 08:11:03 -06003151 for (i = 0; i < nr_files; i++)
3152 fput(fpl->fp[i]);
3153 } else {
3154 kfree_skb(skb);
3155 kfree(fpl);
3156 }
Jens Axboe6b063142019-01-10 22:13:58 -07003157
3158 return 0;
3159}
3160
3161/*
3162 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3163 * causes regular reference counting to break down. We rely on the UNIX
3164 * garbage collection to take care of this problem for us.
3165 */
3166static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3167{
3168 unsigned left, total;
3169 int ret = 0;
3170
3171 total = 0;
3172 left = ctx->nr_user_files;
3173 while (left) {
3174 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003175
3176 ret = __io_sqe_files_scm(ctx, this_files, total);
3177 if (ret)
3178 break;
3179 left -= this_files;
3180 total += this_files;
3181 }
3182
3183 if (!ret)
3184 return 0;
3185
3186 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003187 struct file *file = io_file_from_index(ctx, total);
3188
3189 if (file)
3190 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003191 total++;
3192 }
3193
3194 return ret;
3195}
3196#else
3197static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3198{
3199 return 0;
3200}
3201#endif
3202
Jens Axboe65e19f52019-10-26 07:20:21 -06003203static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3204 unsigned nr_files)
3205{
3206 int i;
3207
3208 for (i = 0; i < nr_tables; i++) {
3209 struct fixed_file_table *table = &ctx->file_table[i];
3210 unsigned this_files;
3211
3212 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3213 table->files = kcalloc(this_files, sizeof(struct file *),
3214 GFP_KERNEL);
3215 if (!table->files)
3216 break;
3217 nr_files -= this_files;
3218 }
3219
3220 if (i == nr_tables)
3221 return 0;
3222
3223 for (i = 0; i < nr_tables; i++) {
3224 struct fixed_file_table *table = &ctx->file_table[i];
3225 kfree(table->files);
3226 }
3227 return 1;
3228}
3229
Jens Axboe6b063142019-01-10 22:13:58 -07003230static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3231 unsigned nr_args)
3232{
3233 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003234 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003235 int fd, ret = 0;
3236 unsigned i;
3237
Jens Axboe65e19f52019-10-26 07:20:21 -06003238 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003239 return -EBUSY;
3240 if (!nr_args)
3241 return -EINVAL;
3242 if (nr_args > IORING_MAX_FIXED_FILES)
3243 return -EMFILE;
3244
Jens Axboe65e19f52019-10-26 07:20:21 -06003245 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3246 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3247 GFP_KERNEL);
3248 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003249 return -ENOMEM;
3250
Jens Axboe65e19f52019-10-26 07:20:21 -06003251 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3252 kfree(ctx->file_table);
3253 return -ENOMEM;
3254 }
3255
Jens Axboe08a45172019-10-03 08:11:03 -06003256 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003257 struct fixed_file_table *table;
3258 unsigned index;
3259
Jens Axboe6b063142019-01-10 22:13:58 -07003260 ret = -EFAULT;
3261 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3262 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003263 /* allow sparse sets */
3264 if (fd == -1) {
3265 ret = 0;
3266 continue;
3267 }
Jens Axboe6b063142019-01-10 22:13:58 -07003268
Jens Axboe65e19f52019-10-26 07:20:21 -06003269 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3270 index = i & IORING_FILE_TABLE_MASK;
3271 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003272
3273 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003274 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003275 break;
3276 /*
3277 * Don't allow io_uring instances to be registered. If UNIX
3278 * isn't enabled, then this causes a reference cycle and this
3279 * instance can never get freed. If UNIX is enabled we'll
3280 * handle it just fine, but there's still no point in allowing
3281 * a ring fd as it doesn't support regular read/write anyway.
3282 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003283 if (table->files[index]->f_op == &io_uring_fops) {
3284 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003285 break;
3286 }
Jens Axboe6b063142019-01-10 22:13:58 -07003287 ret = 0;
3288 }
3289
3290 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003291 for (i = 0; i < ctx->nr_user_files; i++) {
3292 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003293
Jens Axboe65e19f52019-10-26 07:20:21 -06003294 file = io_file_from_index(ctx, i);
3295 if (file)
3296 fput(file);
3297 }
3298 for (i = 0; i < nr_tables; i++)
3299 kfree(ctx->file_table[i].files);
3300
3301 kfree(ctx->file_table);
3302 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003303 ctx->nr_user_files = 0;
3304 return ret;
3305 }
3306
3307 ret = io_sqe_files_scm(ctx);
3308 if (ret)
3309 io_sqe_files_unregister(ctx);
3310
3311 return ret;
3312}
3313
Jens Axboec3a31e62019-10-03 13:59:56 -06003314static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3315{
3316#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003317 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003318 struct sock *sock = ctx->ring_sock->sk;
3319 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3320 struct sk_buff *skb;
3321 int i;
3322
3323 __skb_queue_head_init(&list);
3324
3325 /*
3326 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3327 * remove this entry and rearrange the file array.
3328 */
3329 skb = skb_dequeue(head);
3330 while (skb) {
3331 struct scm_fp_list *fp;
3332
3333 fp = UNIXCB(skb).fp;
3334 for (i = 0; i < fp->count; i++) {
3335 int left;
3336
3337 if (fp->fp[i] != file)
3338 continue;
3339
3340 unix_notinflight(fp->user, fp->fp[i]);
3341 left = fp->count - 1 - i;
3342 if (left) {
3343 memmove(&fp->fp[i], &fp->fp[i + 1],
3344 left * sizeof(struct file *));
3345 }
3346 fp->count--;
3347 if (!fp->count) {
3348 kfree_skb(skb);
3349 skb = NULL;
3350 } else {
3351 __skb_queue_tail(&list, skb);
3352 }
3353 fput(file);
3354 file = NULL;
3355 break;
3356 }
3357
3358 if (!file)
3359 break;
3360
3361 __skb_queue_tail(&list, skb);
3362
3363 skb = skb_dequeue(head);
3364 }
3365
3366 if (skb_peek(&list)) {
3367 spin_lock_irq(&head->lock);
3368 while ((skb = __skb_dequeue(&list)) != NULL)
3369 __skb_queue_tail(head, skb);
3370 spin_unlock_irq(&head->lock);
3371 }
3372#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003373 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003374#endif
3375}
3376
3377static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3378 int index)
3379{
3380#if defined(CONFIG_UNIX)
3381 struct sock *sock = ctx->ring_sock->sk;
3382 struct sk_buff_head *head = &sock->sk_receive_queue;
3383 struct sk_buff *skb;
3384
3385 /*
3386 * See if we can merge this file into an existing skb SCM_RIGHTS
3387 * file set. If there's no room, fall back to allocating a new skb
3388 * and filling it in.
3389 */
3390 spin_lock_irq(&head->lock);
3391 skb = skb_peek(head);
3392 if (skb) {
3393 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3394
3395 if (fpl->count < SCM_MAX_FD) {
3396 __skb_unlink(skb, head);
3397 spin_unlock_irq(&head->lock);
3398 fpl->fp[fpl->count] = get_file(file);
3399 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3400 fpl->count++;
3401 spin_lock_irq(&head->lock);
3402 __skb_queue_head(head, skb);
3403 } else {
3404 skb = NULL;
3405 }
3406 }
3407 spin_unlock_irq(&head->lock);
3408
3409 if (skb) {
3410 fput(file);
3411 return 0;
3412 }
3413
3414 return __io_sqe_files_scm(ctx, 1, index);
3415#else
3416 return 0;
3417#endif
3418}
3419
3420static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3421 unsigned nr_args)
3422{
3423 struct io_uring_files_update up;
3424 __s32 __user *fds;
3425 int fd, i, err;
3426 __u32 done;
3427
Jens Axboe65e19f52019-10-26 07:20:21 -06003428 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003429 return -ENXIO;
3430 if (!nr_args)
3431 return -EINVAL;
3432 if (copy_from_user(&up, arg, sizeof(up)))
3433 return -EFAULT;
3434 if (check_add_overflow(up.offset, nr_args, &done))
3435 return -EOVERFLOW;
3436 if (done > ctx->nr_user_files)
3437 return -EINVAL;
3438
3439 done = 0;
3440 fds = (__s32 __user *) up.fds;
3441 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003442 struct fixed_file_table *table;
3443 unsigned index;
3444
Jens Axboec3a31e62019-10-03 13:59:56 -06003445 err = 0;
3446 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3447 err = -EFAULT;
3448 break;
3449 }
3450 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003451 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3452 index = i & IORING_FILE_TABLE_MASK;
3453 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003454 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003455 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003456 }
3457 if (fd != -1) {
3458 struct file *file;
3459
3460 file = fget(fd);
3461 if (!file) {
3462 err = -EBADF;
3463 break;
3464 }
3465 /*
3466 * Don't allow io_uring instances to be registered. If
3467 * UNIX isn't enabled, then this causes a reference
3468 * cycle and this instance can never get freed. If UNIX
3469 * is enabled we'll handle it just fine, but there's
3470 * still no point in allowing a ring fd as it doesn't
3471 * support regular read/write anyway.
3472 */
3473 if (file->f_op == &io_uring_fops) {
3474 fput(file);
3475 err = -EBADF;
3476 break;
3477 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003478 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003479 err = io_sqe_file_register(ctx, file, i);
3480 if (err)
3481 break;
3482 }
3483 nr_args--;
3484 done++;
3485 up.offset++;
3486 }
3487
3488 return done ? done : err;
3489}
3490
Jens Axboe6c271ce2019-01-10 11:22:30 -07003491static int io_sq_offload_start(struct io_ring_ctx *ctx,
3492 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003493{
Jens Axboe561fb042019-10-24 07:25:42 -06003494 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003495 int ret;
3496
Jens Axboe6c271ce2019-01-10 11:22:30 -07003497 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003498 mmgrab(current->mm);
3499 ctx->sqo_mm = current->mm;
3500
Jens Axboe6c271ce2019-01-10 11:22:30 -07003501 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003502 ret = -EPERM;
3503 if (!capable(CAP_SYS_ADMIN))
3504 goto err;
3505
Jens Axboe917257d2019-04-13 09:28:55 -06003506 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3507 if (!ctx->sq_thread_idle)
3508 ctx->sq_thread_idle = HZ;
3509
Jens Axboe6c271ce2019-01-10 11:22:30 -07003510 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003511 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003512
Jens Axboe917257d2019-04-13 09:28:55 -06003513 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003514 if (cpu >= nr_cpu_ids)
3515 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003516 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003517 goto err;
3518
Jens Axboe6c271ce2019-01-10 11:22:30 -07003519 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3520 ctx, cpu,
3521 "io_uring-sq");
3522 } else {
3523 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3524 "io_uring-sq");
3525 }
3526 if (IS_ERR(ctx->sqo_thread)) {
3527 ret = PTR_ERR(ctx->sqo_thread);
3528 ctx->sqo_thread = NULL;
3529 goto err;
3530 }
3531 wake_up_process(ctx->sqo_thread);
3532 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3533 /* Can't have SQ_AFF without SQPOLL */
3534 ret = -EINVAL;
3535 goto err;
3536 }
3537
Jens Axboe561fb042019-10-24 07:25:42 -06003538 /* Do QD, or 4 * CPUS, whatever is smallest */
3539 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
3540 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm);
Jens Axboe975c99a52019-10-30 08:42:56 -06003541 if (IS_ERR(ctx->io_wq)) {
3542 ret = PTR_ERR(ctx->io_wq);
3543 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003544 goto err;
3545 }
3546
3547 return 0;
3548err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003549 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003550 mmdrop(ctx->sqo_mm);
3551 ctx->sqo_mm = NULL;
3552 return ret;
3553}
3554
3555static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3556{
3557 atomic_long_sub(nr_pages, &user->locked_vm);
3558}
3559
3560static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3561{
3562 unsigned long page_limit, cur_pages, new_pages;
3563
3564 /* Don't allow more pages than we can safely lock */
3565 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3566
3567 do {
3568 cur_pages = atomic_long_read(&user->locked_vm);
3569 new_pages = cur_pages + nr_pages;
3570 if (new_pages > page_limit)
3571 return -ENOMEM;
3572 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3573 new_pages) != cur_pages);
3574
3575 return 0;
3576}
3577
3578static void io_mem_free(void *ptr)
3579{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003580 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003581
Mark Rutland52e04ef2019-04-30 17:30:21 +01003582 if (!ptr)
3583 return;
3584
3585 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003586 if (put_page_testzero(page))
3587 free_compound_page(page);
3588}
3589
3590static void *io_mem_alloc(size_t size)
3591{
3592 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3593 __GFP_NORETRY;
3594
3595 return (void *) __get_free_pages(gfp_flags, get_order(size));
3596}
3597
Hristo Venev75b28af2019-08-26 17:23:46 +00003598static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3599 size_t *sq_offset)
3600{
3601 struct io_rings *rings;
3602 size_t off, sq_array_size;
3603
3604 off = struct_size(rings, cqes, cq_entries);
3605 if (off == SIZE_MAX)
3606 return SIZE_MAX;
3607
3608#ifdef CONFIG_SMP
3609 off = ALIGN(off, SMP_CACHE_BYTES);
3610 if (off == 0)
3611 return SIZE_MAX;
3612#endif
3613
3614 sq_array_size = array_size(sizeof(u32), sq_entries);
3615 if (sq_array_size == SIZE_MAX)
3616 return SIZE_MAX;
3617
3618 if (check_add_overflow(off, sq_array_size, &off))
3619 return SIZE_MAX;
3620
3621 if (sq_offset)
3622 *sq_offset = off;
3623
3624 return off;
3625}
3626
Jens Axboe2b188cc2019-01-07 10:46:33 -07003627static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3628{
Hristo Venev75b28af2019-08-26 17:23:46 +00003629 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003630
Hristo Venev75b28af2019-08-26 17:23:46 +00003631 pages = (size_t)1 << get_order(
3632 rings_size(sq_entries, cq_entries, NULL));
3633 pages += (size_t)1 << get_order(
3634 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003635
Hristo Venev75b28af2019-08-26 17:23:46 +00003636 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003637}
3638
Jens Axboeedafcce2019-01-09 09:16:05 -07003639static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3640{
3641 int i, j;
3642
3643 if (!ctx->user_bufs)
3644 return -ENXIO;
3645
3646 for (i = 0; i < ctx->nr_user_bufs; i++) {
3647 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3648
3649 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003650 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003651
3652 if (ctx->account_mem)
3653 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003654 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003655 imu->nr_bvecs = 0;
3656 }
3657
3658 kfree(ctx->user_bufs);
3659 ctx->user_bufs = NULL;
3660 ctx->nr_user_bufs = 0;
3661 return 0;
3662}
3663
3664static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3665 void __user *arg, unsigned index)
3666{
3667 struct iovec __user *src;
3668
3669#ifdef CONFIG_COMPAT
3670 if (ctx->compat) {
3671 struct compat_iovec __user *ciovs;
3672 struct compat_iovec ciov;
3673
3674 ciovs = (struct compat_iovec __user *) arg;
3675 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3676 return -EFAULT;
3677
3678 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3679 dst->iov_len = ciov.iov_len;
3680 return 0;
3681 }
3682#endif
3683 src = (struct iovec __user *) arg;
3684 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3685 return -EFAULT;
3686 return 0;
3687}
3688
3689static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3690 unsigned nr_args)
3691{
3692 struct vm_area_struct **vmas = NULL;
3693 struct page **pages = NULL;
3694 int i, j, got_pages = 0;
3695 int ret = -EINVAL;
3696
3697 if (ctx->user_bufs)
3698 return -EBUSY;
3699 if (!nr_args || nr_args > UIO_MAXIOV)
3700 return -EINVAL;
3701
3702 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3703 GFP_KERNEL);
3704 if (!ctx->user_bufs)
3705 return -ENOMEM;
3706
3707 for (i = 0; i < nr_args; i++) {
3708 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3709 unsigned long off, start, end, ubuf;
3710 int pret, nr_pages;
3711 struct iovec iov;
3712 size_t size;
3713
3714 ret = io_copy_iov(ctx, &iov, arg, i);
3715 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003716 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003717
3718 /*
3719 * Don't impose further limits on the size and buffer
3720 * constraints here, we'll -EINVAL later when IO is
3721 * submitted if they are wrong.
3722 */
3723 ret = -EFAULT;
3724 if (!iov.iov_base || !iov.iov_len)
3725 goto err;
3726
3727 /* arbitrary limit, but we need something */
3728 if (iov.iov_len > SZ_1G)
3729 goto err;
3730
3731 ubuf = (unsigned long) iov.iov_base;
3732 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3733 start = ubuf >> PAGE_SHIFT;
3734 nr_pages = end - start;
3735
3736 if (ctx->account_mem) {
3737 ret = io_account_mem(ctx->user, nr_pages);
3738 if (ret)
3739 goto err;
3740 }
3741
3742 ret = 0;
3743 if (!pages || nr_pages > got_pages) {
3744 kfree(vmas);
3745 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003746 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003747 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003748 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003749 sizeof(struct vm_area_struct *),
3750 GFP_KERNEL);
3751 if (!pages || !vmas) {
3752 ret = -ENOMEM;
3753 if (ctx->account_mem)
3754 io_unaccount_mem(ctx->user, nr_pages);
3755 goto err;
3756 }
3757 got_pages = nr_pages;
3758 }
3759
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003760 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003761 GFP_KERNEL);
3762 ret = -ENOMEM;
3763 if (!imu->bvec) {
3764 if (ctx->account_mem)
3765 io_unaccount_mem(ctx->user, nr_pages);
3766 goto err;
3767 }
3768
3769 ret = 0;
3770 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003771 pret = get_user_pages(ubuf, nr_pages,
3772 FOLL_WRITE | FOLL_LONGTERM,
3773 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003774 if (pret == nr_pages) {
3775 /* don't support file backed memory */
3776 for (j = 0; j < nr_pages; j++) {
3777 struct vm_area_struct *vma = vmas[j];
3778
3779 if (vma->vm_file &&
3780 !is_file_hugepages(vma->vm_file)) {
3781 ret = -EOPNOTSUPP;
3782 break;
3783 }
3784 }
3785 } else {
3786 ret = pret < 0 ? pret : -EFAULT;
3787 }
3788 up_read(&current->mm->mmap_sem);
3789 if (ret) {
3790 /*
3791 * if we did partial map, or found file backed vmas,
3792 * release any pages we did get
3793 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003794 if (pret > 0)
3795 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003796 if (ctx->account_mem)
3797 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003798 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003799 goto err;
3800 }
3801
3802 off = ubuf & ~PAGE_MASK;
3803 size = iov.iov_len;
3804 for (j = 0; j < nr_pages; j++) {
3805 size_t vec_len;
3806
3807 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3808 imu->bvec[j].bv_page = pages[j];
3809 imu->bvec[j].bv_len = vec_len;
3810 imu->bvec[j].bv_offset = off;
3811 off = 0;
3812 size -= vec_len;
3813 }
3814 /* store original address for later verification */
3815 imu->ubuf = ubuf;
3816 imu->len = iov.iov_len;
3817 imu->nr_bvecs = nr_pages;
3818
3819 ctx->nr_user_bufs++;
3820 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003821 kvfree(pages);
3822 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003823 return 0;
3824err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003825 kvfree(pages);
3826 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003827 io_sqe_buffer_unregister(ctx);
3828 return ret;
3829}
3830
Jens Axboe9b402842019-04-11 11:45:41 -06003831static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3832{
3833 __s32 __user *fds = arg;
3834 int fd;
3835
3836 if (ctx->cq_ev_fd)
3837 return -EBUSY;
3838
3839 if (copy_from_user(&fd, fds, sizeof(*fds)))
3840 return -EFAULT;
3841
3842 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3843 if (IS_ERR(ctx->cq_ev_fd)) {
3844 int ret = PTR_ERR(ctx->cq_ev_fd);
3845 ctx->cq_ev_fd = NULL;
3846 return ret;
3847 }
3848
3849 return 0;
3850}
3851
3852static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3853{
3854 if (ctx->cq_ev_fd) {
3855 eventfd_ctx_put(ctx->cq_ev_fd);
3856 ctx->cq_ev_fd = NULL;
3857 return 0;
3858 }
3859
3860 return -ENXIO;
3861}
3862
Jens Axboe2b188cc2019-01-07 10:46:33 -07003863static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3864{
Jens Axboe6b063142019-01-10 22:13:58 -07003865 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003866 if (ctx->sqo_mm)
3867 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003868
3869 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003870 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003871 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003872 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003873
Jens Axboe2b188cc2019-01-07 10:46:33 -07003874#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003875 if (ctx->ring_sock) {
3876 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003877 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003878 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003879#endif
3880
Hristo Venev75b28af2019-08-26 17:23:46 +00003881 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003882 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003883
3884 percpu_ref_exit(&ctx->refs);
3885 if (ctx->account_mem)
3886 io_unaccount_mem(ctx->user,
3887 ring_pages(ctx->sq_entries, ctx->cq_entries));
3888 free_uid(ctx->user);
3889 kfree(ctx);
3890}
3891
3892static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3893{
3894 struct io_ring_ctx *ctx = file->private_data;
3895 __poll_t mask = 0;
3896
3897 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003898 /*
3899 * synchronizes with barrier from wq_has_sleeper call in
3900 * io_commit_cqring
3901 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003902 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003903 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3904 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003905 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003906 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003907 mask |= EPOLLIN | EPOLLRDNORM;
3908
3909 return mask;
3910}
3911
3912static int io_uring_fasync(int fd, struct file *file, int on)
3913{
3914 struct io_ring_ctx *ctx = file->private_data;
3915
3916 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3917}
3918
3919static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3920{
3921 mutex_lock(&ctx->uring_lock);
3922 percpu_ref_kill(&ctx->refs);
3923 mutex_unlock(&ctx->uring_lock);
3924
Jens Axboe5262f562019-09-17 12:26:57 -06003925 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003926 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06003927
3928 if (ctx->io_wq)
3929 io_wq_cancel_all(ctx->io_wq);
3930
Jens Axboedef596e2019-01-09 08:59:42 -07003931 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003932 wait_for_completion(&ctx->ctx_done);
3933 io_ring_ctx_free(ctx);
3934}
3935
3936static int io_uring_release(struct inode *inode, struct file *file)
3937{
3938 struct io_ring_ctx *ctx = file->private_data;
3939
3940 file->private_data = NULL;
3941 io_ring_ctx_wait_and_kill(ctx);
3942 return 0;
3943}
3944
Jens Axboefcb323c2019-10-24 12:39:47 -06003945static void io_uring_cancel_files(struct io_ring_ctx *ctx,
3946 struct files_struct *files)
3947{
3948 struct io_kiocb *req;
3949 DEFINE_WAIT(wait);
3950
3951 while (!list_empty_careful(&ctx->inflight_list)) {
3952 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
3953
3954 spin_lock_irq(&ctx->inflight_lock);
3955 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
3956 if (req->work.files == files) {
3957 ret = io_wq_cancel_work(ctx->io_wq, &req->work);
3958 break;
3959 }
3960 }
3961 if (ret == IO_WQ_CANCEL_RUNNING)
3962 prepare_to_wait(&ctx->inflight_wait, &wait,
3963 TASK_UNINTERRUPTIBLE);
3964
3965 spin_unlock_irq(&ctx->inflight_lock);
3966
3967 /*
3968 * We need to keep going until we get NOTFOUND. We only cancel
3969 * one work at the time.
3970 *
3971 * If we get CANCEL_RUNNING, then wait for a work to complete
3972 * before continuing.
3973 */
3974 if (ret == IO_WQ_CANCEL_OK)
3975 continue;
3976 else if (ret != IO_WQ_CANCEL_RUNNING)
3977 break;
3978 schedule();
3979 }
3980}
3981
3982static int io_uring_flush(struct file *file, void *data)
3983{
3984 struct io_ring_ctx *ctx = file->private_data;
3985
3986 io_uring_cancel_files(ctx, data);
3987 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
3988 io_wq_cancel_all(ctx->io_wq);
3989 return 0;
3990}
3991
Jens Axboe2b188cc2019-01-07 10:46:33 -07003992static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3993{
3994 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3995 unsigned long sz = vma->vm_end - vma->vm_start;
3996 struct io_ring_ctx *ctx = file->private_data;
3997 unsigned long pfn;
3998 struct page *page;
3999 void *ptr;
4000
4001 switch (offset) {
4002 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004003 case IORING_OFF_CQ_RING:
4004 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004005 break;
4006 case IORING_OFF_SQES:
4007 ptr = ctx->sq_sqes;
4008 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004009 default:
4010 return -EINVAL;
4011 }
4012
4013 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004014 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004015 return -EINVAL;
4016
4017 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4018 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4019}
4020
4021SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4022 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4023 size_t, sigsz)
4024{
4025 struct io_ring_ctx *ctx;
4026 long ret = -EBADF;
4027 int submitted = 0;
4028 struct fd f;
4029
Jens Axboe6c271ce2019-01-10 11:22:30 -07004030 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004031 return -EINVAL;
4032
4033 f = fdget(fd);
4034 if (!f.file)
4035 return -EBADF;
4036
4037 ret = -EOPNOTSUPP;
4038 if (f.file->f_op != &io_uring_fops)
4039 goto out_fput;
4040
4041 ret = -ENXIO;
4042 ctx = f.file->private_data;
4043 if (!percpu_ref_tryget(&ctx->refs))
4044 goto out_fput;
4045
Jens Axboe6c271ce2019-01-10 11:22:30 -07004046 /*
4047 * For SQ polling, the thread will do all submissions and completions.
4048 * Just return the requested submit count, and wake the thread if
4049 * we were asked to.
4050 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004051 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004052 if (ctx->flags & IORING_SETUP_SQPOLL) {
4053 if (flags & IORING_ENTER_SQ_WAKEUP)
4054 wake_up(&ctx->sqo_wait);
4055 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004056 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004057 to_submit = min(to_submit, ctx->sq_entries);
4058
4059 mutex_lock(&ctx->uring_lock);
Jens Axboefcb323c2019-10-24 12:39:47 -06004060 submitted = io_ring_submit(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004061 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004062 }
4063 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004064 unsigned nr_events = 0;
4065
Jens Axboe2b188cc2019-01-07 10:46:33 -07004066 min_complete = min(min_complete, ctx->cq_entries);
4067
Jens Axboedef596e2019-01-09 08:59:42 -07004068 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004069 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004070 } else {
4071 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4072 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004073 }
4074
Pavel Begunkov6805b322019-10-08 02:18:42 +03004075 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004076out_fput:
4077 fdput(f);
4078 return submitted ? submitted : ret;
4079}
4080
4081static const struct file_operations io_uring_fops = {
4082 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004083 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004084 .mmap = io_uring_mmap,
4085 .poll = io_uring_poll,
4086 .fasync = io_uring_fasync,
4087};
4088
4089static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4090 struct io_uring_params *p)
4091{
Hristo Venev75b28af2019-08-26 17:23:46 +00004092 struct io_rings *rings;
4093 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004094
Hristo Venev75b28af2019-08-26 17:23:46 +00004095 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4096 if (size == SIZE_MAX)
4097 return -EOVERFLOW;
4098
4099 rings = io_mem_alloc(size);
4100 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004101 return -ENOMEM;
4102
Hristo Venev75b28af2019-08-26 17:23:46 +00004103 ctx->rings = rings;
4104 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4105 rings->sq_ring_mask = p->sq_entries - 1;
4106 rings->cq_ring_mask = p->cq_entries - 1;
4107 rings->sq_ring_entries = p->sq_entries;
4108 rings->cq_ring_entries = p->cq_entries;
4109 ctx->sq_mask = rings->sq_ring_mask;
4110 ctx->cq_mask = rings->cq_ring_mask;
4111 ctx->sq_entries = rings->sq_ring_entries;
4112 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004113
4114 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4115 if (size == SIZE_MAX)
4116 return -EOVERFLOW;
4117
4118 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004119 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004120 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004121
Jens Axboe2b188cc2019-01-07 10:46:33 -07004122 return 0;
4123}
4124
4125/*
4126 * Allocate an anonymous fd, this is what constitutes the application
4127 * visible backing of an io_uring instance. The application mmaps this
4128 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4129 * we have to tie this fd to a socket for file garbage collection purposes.
4130 */
4131static int io_uring_get_fd(struct io_ring_ctx *ctx)
4132{
4133 struct file *file;
4134 int ret;
4135
4136#if defined(CONFIG_UNIX)
4137 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4138 &ctx->ring_sock);
4139 if (ret)
4140 return ret;
4141#endif
4142
4143 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4144 if (ret < 0)
4145 goto err;
4146
4147 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4148 O_RDWR | O_CLOEXEC);
4149 if (IS_ERR(file)) {
4150 put_unused_fd(ret);
4151 ret = PTR_ERR(file);
4152 goto err;
4153 }
4154
4155#if defined(CONFIG_UNIX)
4156 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004157 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004158#endif
4159 fd_install(ret, file);
4160 return ret;
4161err:
4162#if defined(CONFIG_UNIX)
4163 sock_release(ctx->ring_sock);
4164 ctx->ring_sock = NULL;
4165#endif
4166 return ret;
4167}
4168
4169static int io_uring_create(unsigned entries, struct io_uring_params *p)
4170{
4171 struct user_struct *user = NULL;
4172 struct io_ring_ctx *ctx;
4173 bool account_mem;
4174 int ret;
4175
4176 if (!entries || entries > IORING_MAX_ENTRIES)
4177 return -EINVAL;
4178
4179 /*
4180 * Use twice as many entries for the CQ ring. It's possible for the
4181 * application to drive a higher depth than the size of the SQ ring,
4182 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004183 * some flexibility in overcommitting a bit. If the application has
4184 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4185 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004186 */
4187 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004188 if (p->flags & IORING_SETUP_CQSIZE) {
4189 /*
4190 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4191 * to a power-of-two, if it isn't already. We do NOT impose
4192 * any cq vs sq ring sizing.
4193 */
4194 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4195 return -EINVAL;
4196 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4197 } else {
4198 p->cq_entries = 2 * p->sq_entries;
4199 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004200
4201 user = get_uid(current_user());
4202 account_mem = !capable(CAP_IPC_LOCK);
4203
4204 if (account_mem) {
4205 ret = io_account_mem(user,
4206 ring_pages(p->sq_entries, p->cq_entries));
4207 if (ret) {
4208 free_uid(user);
4209 return ret;
4210 }
4211 }
4212
4213 ctx = io_ring_ctx_alloc(p);
4214 if (!ctx) {
4215 if (account_mem)
4216 io_unaccount_mem(user, ring_pages(p->sq_entries,
4217 p->cq_entries));
4218 free_uid(user);
4219 return -ENOMEM;
4220 }
4221 ctx->compat = in_compat_syscall();
4222 ctx->account_mem = account_mem;
4223 ctx->user = user;
4224
4225 ret = io_allocate_scq_urings(ctx, p);
4226 if (ret)
4227 goto err;
4228
Jens Axboe6c271ce2019-01-10 11:22:30 -07004229 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004230 if (ret)
4231 goto err;
4232
Jens Axboe2b188cc2019-01-07 10:46:33 -07004233 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004234 p->sq_off.head = offsetof(struct io_rings, sq.head);
4235 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4236 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4237 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4238 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4239 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4240 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004241
4242 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004243 p->cq_off.head = offsetof(struct io_rings, cq.head);
4244 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4245 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4246 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4247 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4248 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004249
Jens Axboe044c1ab2019-10-28 09:15:33 -06004250 /*
4251 * Install ring fd as the very last thing, so we don't risk someone
4252 * having closed it before we finish setup
4253 */
4254 ret = io_uring_get_fd(ctx);
4255 if (ret < 0)
4256 goto err;
4257
Jens Axboeac90f242019-09-06 10:26:21 -06004258 p->features = IORING_FEAT_SINGLE_MMAP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004259 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004260 return ret;
4261err:
4262 io_ring_ctx_wait_and_kill(ctx);
4263 return ret;
4264}
4265
4266/*
4267 * Sets up an aio uring context, and returns the fd. Applications asks for a
4268 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4269 * params structure passed in.
4270 */
4271static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4272{
4273 struct io_uring_params p;
4274 long ret;
4275 int i;
4276
4277 if (copy_from_user(&p, params, sizeof(p)))
4278 return -EFAULT;
4279 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4280 if (p.resv[i])
4281 return -EINVAL;
4282 }
4283
Jens Axboe6c271ce2019-01-10 11:22:30 -07004284 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004285 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004286 return -EINVAL;
4287
4288 ret = io_uring_create(entries, &p);
4289 if (ret < 0)
4290 return ret;
4291
4292 if (copy_to_user(params, &p, sizeof(p)))
4293 return -EFAULT;
4294
4295 return ret;
4296}
4297
4298SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4299 struct io_uring_params __user *, params)
4300{
4301 return io_uring_setup(entries, params);
4302}
4303
Jens Axboeedafcce2019-01-09 09:16:05 -07004304static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4305 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004306 __releases(ctx->uring_lock)
4307 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004308{
4309 int ret;
4310
Jens Axboe35fa71a2019-04-22 10:23:23 -06004311 /*
4312 * We're inside the ring mutex, if the ref is already dying, then
4313 * someone else killed the ctx or is already going through
4314 * io_uring_register().
4315 */
4316 if (percpu_ref_is_dying(&ctx->refs))
4317 return -ENXIO;
4318
Jens Axboeedafcce2019-01-09 09:16:05 -07004319 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004320
4321 /*
4322 * Drop uring mutex before waiting for references to exit. If another
4323 * thread is currently inside io_uring_enter() it might need to grab
4324 * the uring_lock to make progress. If we hold it here across the drain
4325 * wait, then we can deadlock. It's safe to drop the mutex here, since
4326 * no new references will come in after we've killed the percpu ref.
4327 */
4328 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004329 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004330 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004331
4332 switch (opcode) {
4333 case IORING_REGISTER_BUFFERS:
4334 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4335 break;
4336 case IORING_UNREGISTER_BUFFERS:
4337 ret = -EINVAL;
4338 if (arg || nr_args)
4339 break;
4340 ret = io_sqe_buffer_unregister(ctx);
4341 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004342 case IORING_REGISTER_FILES:
4343 ret = io_sqe_files_register(ctx, arg, nr_args);
4344 break;
4345 case IORING_UNREGISTER_FILES:
4346 ret = -EINVAL;
4347 if (arg || nr_args)
4348 break;
4349 ret = io_sqe_files_unregister(ctx);
4350 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004351 case IORING_REGISTER_FILES_UPDATE:
4352 ret = io_sqe_files_update(ctx, arg, nr_args);
4353 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004354 case IORING_REGISTER_EVENTFD:
4355 ret = -EINVAL;
4356 if (nr_args != 1)
4357 break;
4358 ret = io_eventfd_register(ctx, arg);
4359 break;
4360 case IORING_UNREGISTER_EVENTFD:
4361 ret = -EINVAL;
4362 if (arg || nr_args)
4363 break;
4364 ret = io_eventfd_unregister(ctx);
4365 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004366 default:
4367 ret = -EINVAL;
4368 break;
4369 }
4370
4371 /* bring the ctx back to life */
4372 reinit_completion(&ctx->ctx_done);
4373 percpu_ref_reinit(&ctx->refs);
4374 return ret;
4375}
4376
4377SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4378 void __user *, arg, unsigned int, nr_args)
4379{
4380 struct io_ring_ctx *ctx;
4381 long ret = -EBADF;
4382 struct fd f;
4383
4384 f = fdget(fd);
4385 if (!f.file)
4386 return -EBADF;
4387
4388 ret = -EOPNOTSUPP;
4389 if (f.file->f_op != &io_uring_fops)
4390 goto out_fput;
4391
4392 ctx = f.file->private_data;
4393
4394 mutex_lock(&ctx->uring_lock);
4395 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4396 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004397 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4398 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004399out_fput:
4400 fdput(f);
4401 return ret;
4402}
4403
Jens Axboe2b188cc2019-01-07 10:46:33 -07004404static int __init io_uring_init(void)
4405{
4406 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4407 return 0;
4408};
4409__initcall(io_uring_init);