blob: 7813bc7d5b61c88f9cb5c278a66b662e0ea30804 [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;
1809 __poll_t mask = 0;
1810
Jens Axboe561fb042019-10-24 07:25:42 -06001811 if (work->flags & IO_WQ_WORK_CANCEL)
1812 WRITE_ONCE(poll->canceled, true);
1813
Jens Axboe221c5eb2019-01-17 09:41:58 -07001814 if (!READ_ONCE(poll->canceled))
1815 mask = vfs_poll(poll->file, &pt) & poll->events;
1816
1817 /*
1818 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1819 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1820 * synchronize with them. In the cancellation case the list_del_init
1821 * itself is not actually needed, but harmless so we keep it in to
1822 * avoid further branches in the fast path.
1823 */
1824 spin_lock_irq(&ctx->completion_lock);
1825 if (!mask && !READ_ONCE(poll->canceled)) {
1826 add_wait_queue(poll->head, &poll->wait);
1827 spin_unlock_irq(&ctx->completion_lock);
1828 return;
1829 }
1830 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001831 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001832 spin_unlock_irq(&ctx->completion_lock);
1833
Jens Axboe8c838782019-03-12 15:48:16 -06001834 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001835 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001836}
1837
1838static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1839 void *key)
1840{
1841 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1842 wait);
1843 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1844 struct io_ring_ctx *ctx = req->ctx;
1845 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001846 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001847
1848 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001849 if (mask && !(mask & poll->events))
1850 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001851
1852 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001853
1854 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1855 list_del(&req->list);
1856 io_poll_complete(ctx, req, mask);
1857 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1858
1859 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001860 io_put_req(req, NULL);
Jens Axboe8c838782019-03-12 15:48:16 -06001861 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001862 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001863 }
1864
Jens Axboe221c5eb2019-01-17 09:41:58 -07001865 return 1;
1866}
1867
1868struct io_poll_table {
1869 struct poll_table_struct pt;
1870 struct io_kiocb *req;
1871 int error;
1872};
1873
1874static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1875 struct poll_table_struct *p)
1876{
1877 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1878
1879 if (unlikely(pt->req->poll.head)) {
1880 pt->error = -EINVAL;
1881 return;
1882 }
1883
1884 pt->error = 0;
1885 pt->req->poll.head = head;
1886 add_wait_queue(head, &pt->req->poll.wait);
1887}
1888
1889static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1890{
1891 struct io_poll_iocb *poll = &req->poll;
1892 struct io_ring_ctx *ctx = req->ctx;
1893 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001894 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001895 __poll_t mask;
1896 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001897
1898 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1899 return -EINVAL;
1900 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1901 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001902 if (!poll->file)
1903 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001904
Jens Axboe6cc47d12019-09-18 11:18:23 -06001905 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06001906 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001907 events = READ_ONCE(sqe->poll_events);
1908 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1909
Jens Axboe221c5eb2019-01-17 09:41:58 -07001910 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001911 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001912 poll->canceled = false;
1913
1914 ipt.pt._qproc = io_poll_queue_proc;
1915 ipt.pt._key = poll->events;
1916 ipt.req = req;
1917 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1918
1919 /* initialized the list so that we can do list_empty checks */
1920 INIT_LIST_HEAD(&poll->wait.entry);
1921 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1922
Jens Axboe36703242019-07-25 10:20:18 -06001923 INIT_LIST_HEAD(&req->list);
1924
Jens Axboe221c5eb2019-01-17 09:41:58 -07001925 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001926
1927 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001928 if (likely(poll->head)) {
1929 spin_lock(&poll->head->lock);
1930 if (unlikely(list_empty(&poll->wait.entry))) {
1931 if (ipt.error)
1932 cancel = true;
1933 ipt.error = 0;
1934 mask = 0;
1935 }
1936 if (mask || ipt.error)
1937 list_del_init(&poll->wait.entry);
1938 else if (cancel)
1939 WRITE_ONCE(poll->canceled, true);
1940 else if (!poll->done) /* actually waiting for an event */
1941 list_add_tail(&req->list, &ctx->cancel_list);
1942 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001943 }
Jens Axboe8c838782019-03-12 15:48:16 -06001944 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001945 ipt.error = 0;
1946 io_poll_complete(ctx, req, mask);
1947 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001948 spin_unlock_irq(&ctx->completion_lock);
1949
Jens Axboe8c838782019-03-12 15:48:16 -06001950 if (mask) {
1951 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001952 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001953 }
Jens Axboe8c838782019-03-12 15:48:16 -06001954 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001955}
1956
Jens Axboe5262f562019-09-17 12:26:57 -06001957static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1958{
1959 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06001960 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06001961 unsigned long flags;
1962
1963 req = container_of(timer, struct io_kiocb, timeout.timer);
1964 ctx = req->ctx;
1965 atomic_inc(&ctx->cq_timeouts);
1966
1967 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08001968 /*
Jens Axboe11365042019-10-16 09:08:32 -06001969 * We could be racing with timeout deletion. If the list is empty,
1970 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08001971 */
Jens Axboe842f9612019-10-29 12:34:10 -06001972 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06001973 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06001974
Jens Axboe11365042019-10-16 09:08:32 -06001975 /*
1976 * Adjust the reqs sequence before the current one because it
1977 * will consume a slot in the cq_ring and the the cq_tail
1978 * pointer will be increased, otherwise other timeout reqs may
1979 * return in advance without waiting for enough wait_nr.
1980 */
1981 prev = req;
1982 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
1983 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06001984 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06001985 }
Jens Axboe842f9612019-10-29 12:34:10 -06001986
1987 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1988 io_commit_cqring(ctx);
Jens Axboe5262f562019-09-17 12:26:57 -06001989 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1990
Jens Axboe842f9612019-10-29 12:34:10 -06001991 io_cqring_ev_posted(ctx);
1992 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06001993 return HRTIMER_NORESTART;
1994}
1995
1996/*
1997 * Remove or update an existing timeout command
1998 */
1999static int io_timeout_remove(struct io_kiocb *req,
2000 const struct io_uring_sqe *sqe)
2001{
2002 struct io_ring_ctx *ctx = req->ctx;
2003 struct io_kiocb *treq;
2004 int ret = -ENOENT;
2005 __u64 user_data;
2006 unsigned flags;
2007
2008 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2009 return -EINVAL;
2010 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2011 return -EINVAL;
2012 flags = READ_ONCE(sqe->timeout_flags);
2013 if (flags)
2014 return -EINVAL;
2015
2016 user_data = READ_ONCE(sqe->addr);
2017 spin_lock_irq(&ctx->completion_lock);
2018 list_for_each_entry(treq, &ctx->timeout_list, list) {
2019 if (user_data == treq->user_data) {
2020 list_del_init(&treq->list);
2021 ret = 0;
2022 break;
2023 }
2024 }
2025
2026 /* didn't find timeout */
2027 if (ret) {
2028fill_ev:
2029 io_cqring_fill_event(ctx, req->user_data, ret);
2030 io_commit_cqring(ctx);
2031 spin_unlock_irq(&ctx->completion_lock);
2032 io_cqring_ev_posted(ctx);
2033 io_put_req(req, NULL);
2034 return 0;
2035 }
2036
2037 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
2038 if (ret == -1) {
2039 ret = -EBUSY;
2040 goto fill_ev;
2041 }
2042
2043 io_cqring_fill_event(ctx, req->user_data, 0);
2044 io_cqring_fill_event(ctx, treq->user_data, -ECANCELED);
2045 io_commit_cqring(ctx);
2046 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002047 io_cqring_ev_posted(ctx);
2048
Jens Axboe11365042019-10-16 09:08:32 -06002049 io_put_req(treq, NULL);
Jens Axboeba816ad2019-09-28 11:36:45 -06002050 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002051 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002052}
2053
2054static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2055{
yangerkun5da0fb12019-10-15 21:59:29 +08002056 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002057 struct io_ring_ctx *ctx = req->ctx;
2058 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002059 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002060 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002061 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002062 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002063
2064 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2065 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002066 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2067 return -EINVAL;
2068 flags = READ_ONCE(sqe->timeout_flags);
2069 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002070 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002071
2072 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002073 return -EFAULT;
2074
Jens Axboe11365042019-10-16 09:08:32 -06002075 if (flags & IORING_TIMEOUT_ABS)
2076 mode = HRTIMER_MODE_ABS;
2077 else
2078 mode = HRTIMER_MODE_REL;
2079
2080 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2081
Jens Axboe5262f562019-09-17 12:26:57 -06002082 /*
2083 * sqe->off holds how many events that need to occur for this
2084 * timeout event to be satisfied.
2085 */
2086 count = READ_ONCE(sqe->off);
2087 if (!count)
2088 count = 1;
2089
2090 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002091 /* reuse it to store the count */
2092 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002093 req->flags |= REQ_F_TIMEOUT;
2094
2095 /*
2096 * Insertion sort, ensuring the first entry in the list is always
2097 * the one we need first.
2098 */
Jens Axboe5262f562019-09-17 12:26:57 -06002099 spin_lock_irq(&ctx->completion_lock);
2100 list_for_each_prev(entry, &ctx->timeout_list) {
2101 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002102 unsigned nxt_sq_head;
2103 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002104
yangerkun5da0fb12019-10-15 21:59:29 +08002105 /*
2106 * Since cached_sq_head + count - 1 can overflow, use type long
2107 * long to store it.
2108 */
2109 tmp = (long long)ctx->cached_sq_head + count - 1;
2110 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2111 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2112
2113 /*
2114 * cached_sq_head may overflow, and it will never overflow twice
2115 * once there is some timeout req still be valid.
2116 */
2117 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002118 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002119
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002120 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002121 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002122
2123 /*
2124 * Sequence of reqs after the insert one and itself should
2125 * be adjusted because each timeout req consumes a slot.
2126 */
2127 span++;
2128 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002129 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002130 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002131 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002132 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002133 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002134 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002135 return 0;
2136}
2137
Jens Axboe62755e32019-10-28 21:49:21 -06002138static bool io_cancel_cb(struct io_wq_work *work, void *data)
2139{
2140 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2141
2142 return req->user_data == (unsigned long) data;
2143}
2144
2145static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2146 struct io_kiocb **nxt)
2147{
2148 struct io_ring_ctx *ctx = req->ctx;
2149 enum io_wq_cancel cancel_ret;
2150 void *sqe_addr;
2151 int ret = 0;
2152
2153 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2154 return -EINVAL;
2155 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2156 sqe->cancel_flags)
2157 return -EINVAL;
2158
2159 sqe_addr = (void *) (unsigned long) READ_ONCE(sqe->addr);
2160 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2161 switch (cancel_ret) {
2162 case IO_WQ_CANCEL_OK:
2163 ret = 0;
2164 break;
2165 case IO_WQ_CANCEL_RUNNING:
2166 ret = -EALREADY;
2167 break;
2168 case IO_WQ_CANCEL_NOTFOUND:
2169 ret = -ENOENT;
2170 break;
2171 }
2172
2173 if (ret < 0 && (req->flags & REQ_F_LINK))
2174 req->flags |= REQ_F_FAIL_LINK;
2175 io_cqring_add_event(req->ctx, sqe->user_data, ret);
2176 io_put_req(req, nxt);
2177 return 0;
2178}
2179
Jens Axboede0617e2019-04-06 21:51:27 -06002180static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
2181 const struct io_uring_sqe *sqe)
2182{
2183 struct io_uring_sqe *sqe_copy;
2184
2185 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2186 return 0;
2187
2188 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2189 if (!sqe_copy)
2190 return -EAGAIN;
2191
2192 spin_lock_irq(&ctx->completion_lock);
2193 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2194 spin_unlock_irq(&ctx->completion_lock);
2195 kfree(sqe_copy);
2196 return 0;
2197 }
2198
2199 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2200 req->submit.sqe = sqe_copy;
2201
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002202 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002203 list_add_tail(&req->list, &ctx->defer_list);
2204 spin_unlock_irq(&ctx->completion_lock);
2205 return -EIOCBQUEUED;
2206}
2207
Jens Axboe2b188cc2019-01-07 10:46:33 -07002208static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboeba816ad2019-09-28 11:36:45 -06002209 const struct sqe_submit *s, struct io_kiocb **nxt,
2210 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002211{
Jens Axboee0c5c572019-03-12 10:18:47 -06002212 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002213
Jens Axboe9e645e112019-05-10 16:07:28 -06002214 req->user_data = READ_ONCE(s->sqe->user_data);
2215
Jens Axboe2b188cc2019-01-07 10:46:33 -07002216 opcode = READ_ONCE(s->sqe->opcode);
2217 switch (opcode) {
2218 case IORING_OP_NOP:
2219 ret = io_nop(req, req->user_data);
2220 break;
2221 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002222 if (unlikely(s->sqe->buf_index))
2223 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002224 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002225 break;
2226 case IORING_OP_WRITEV:
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_write(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002230 break;
2231 case IORING_OP_READ_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002232 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002233 break;
2234 case IORING_OP_WRITE_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002235 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002236 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002237 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002238 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002239 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002240 case IORING_OP_POLL_ADD:
2241 ret = io_poll_add(req, s->sqe);
2242 break;
2243 case IORING_OP_POLL_REMOVE:
2244 ret = io_poll_remove(req, s->sqe);
2245 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002246 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002247 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002248 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002249 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002250 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002251 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002252 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002253 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002254 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002255 case IORING_OP_TIMEOUT:
2256 ret = io_timeout(req, s->sqe);
2257 break;
Jens Axboe11365042019-10-16 09:08:32 -06002258 case IORING_OP_TIMEOUT_REMOVE:
2259 ret = io_timeout_remove(req, s->sqe);
2260 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002261 case IORING_OP_ACCEPT:
2262 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2263 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002264 case IORING_OP_ASYNC_CANCEL:
2265 ret = io_async_cancel(req, s->sqe, nxt);
2266 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002267 default:
2268 ret = -EINVAL;
2269 break;
2270 }
2271
Jens Axboedef596e2019-01-09 08:59:42 -07002272 if (ret)
2273 return ret;
2274
2275 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002276 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002277 return -EAGAIN;
2278
2279 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002280 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002281 mutex_lock(&ctx->uring_lock);
2282 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002283 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002284 mutex_unlock(&ctx->uring_lock);
2285 }
2286
2287 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002288}
2289
Jens Axboe561fb042019-10-24 07:25:42 -06002290static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002291{
Jens Axboe561fb042019-10-24 07:25:42 -06002292 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002293 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002294 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -06002295 struct sqe_submit *s = &req->submit;
2296 const struct io_uring_sqe *sqe = s->sqe;
2297 struct io_kiocb *nxt = NULL;
2298 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002299
Jens Axboe561fb042019-10-24 07:25:42 -06002300 /* Ensure we clear previously set non-block flag */
2301 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002302
Jens Axboe561fb042019-10-24 07:25:42 -06002303 if (work->flags & IO_WQ_WORK_CANCEL)
2304 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002305
Jens Axboe561fb042019-10-24 07:25:42 -06002306 if (!ret) {
2307 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2308 s->in_async = true;
2309 do {
2310 ret = __io_submit_sqe(ctx, req, s, &nxt, false);
2311 /*
2312 * We can get EAGAIN for polled IO even though we're
2313 * forcing a sync submission from here, since we can't
2314 * wait for request slots on the block side.
2315 */
2316 if (ret != -EAGAIN)
2317 break;
2318 cond_resched();
2319 } while (1);
2320 }
Jens Axboe31b51512019-01-18 22:56:34 -07002321
Jens Axboe561fb042019-10-24 07:25:42 -06002322 /* drop submission reference */
2323 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002324
Jens Axboe561fb042019-10-24 07:25:42 -06002325 if (ret) {
2326 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002327 io_put_req(req, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07002328 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002329
Jens Axboe561fb042019-10-24 07:25:42 -06002330 /* async context always use a copy of the sqe */
2331 kfree(sqe);
2332
2333 /* if a dependent link is ready, pass it back */
2334 if (!ret && nxt) {
2335 io_prep_async_work(nxt);
2336 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002337 }
Jens Axboe31b51512019-01-18 22:56:34 -07002338}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002339
Jens Axboe09bb8392019-03-13 12:39:28 -06002340static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2341{
2342 int op = READ_ONCE(sqe->opcode);
2343
2344 switch (op) {
2345 case IORING_OP_NOP:
2346 case IORING_OP_POLL_REMOVE:
2347 return false;
2348 default:
2349 return true;
2350 }
2351}
2352
Jens Axboe65e19f52019-10-26 07:20:21 -06002353static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2354 int index)
2355{
2356 struct fixed_file_table *table;
2357
2358 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2359 return table->files[index & IORING_FILE_TABLE_MASK];
2360}
2361
Jens Axboe09bb8392019-03-13 12:39:28 -06002362static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2363 struct io_submit_state *state, struct io_kiocb *req)
2364{
2365 unsigned flags;
2366 int fd;
2367
2368 flags = READ_ONCE(s->sqe->flags);
2369 fd = READ_ONCE(s->sqe->fd);
2370
Jackie Liu4fe2c962019-09-09 20:50:40 +08002371 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002372 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002373 /*
2374 * All io need record the previous position, if LINK vs DARIN,
2375 * it can be used to mark the position of the first IO in the
2376 * link list.
2377 */
2378 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002379
Jens Axboe60c112b2019-06-21 10:20:18 -06002380 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002381 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002382
2383 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002384 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002385 (unsigned) fd >= ctx->nr_user_files))
2386 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002387 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002388 req->file = io_file_from_index(ctx, fd);
2389 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002390 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002391 req->flags |= REQ_F_FIXED_FILE;
2392 } else {
2393 if (s->needs_fixed_file)
2394 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002395 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002396 req->file = io_file_get(state, fd);
2397 if (unlikely(!req->file))
2398 return -EBADF;
2399 }
2400
2401 return 0;
2402}
2403
Jens Axboefcb323c2019-10-24 12:39:47 -06002404static int io_grab_files(struct io_ring_ctx *ctx, struct io_kiocb *req)
2405{
2406 int ret = -EBADF;
2407
2408 rcu_read_lock();
2409 spin_lock_irq(&ctx->inflight_lock);
2410 /*
2411 * We use the f_ops->flush() handler to ensure that we can flush
2412 * out work accessing these files if the fd is closed. Check if
2413 * the fd has changed since we started down this path, and disallow
2414 * this operation if it has.
2415 */
2416 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2417 list_add(&req->inflight_entry, &ctx->inflight_list);
2418 req->flags |= REQ_F_INFLIGHT;
2419 req->work.files = current->files;
2420 ret = 0;
2421 }
2422 spin_unlock_irq(&ctx->inflight_lock);
2423 rcu_read_unlock();
2424
2425 return ret;
2426}
2427
Jackie Liu4fe2c962019-09-09 20:50:40 +08002428static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002429 struct sqe_submit *s)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002430{
Jens Axboee0c5c572019-03-12 10:18:47 -06002431 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002432
Jens Axboeba816ad2019-09-28 11:36:45 -06002433 ret = __io_submit_sqe(ctx, req, s, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002434
2435 /*
2436 * We async punt it if the file wasn't marked NOWAIT, or if the file
2437 * doesn't support non-blocking read/write attempts
2438 */
2439 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2440 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002441 struct io_uring_sqe *sqe_copy;
2442
Jackie Liu954dab12019-09-18 10:37:52 +08002443 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002444 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002445 s->sqe = sqe_copy;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002446 memcpy(&req->submit, s, sizeof(*s));
Jens Axboefcb323c2019-10-24 12:39:47 -06002447 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2448 ret = io_grab_files(ctx, req);
2449 if (ret) {
2450 kfree(sqe_copy);
2451 goto err;
2452 }
2453 }
Jens Axboee65ef562019-03-12 10:16:44 -06002454
2455 /*
2456 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002457 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002458 */
Jens Axboefcb323c2019-10-24 12:39:47 -06002459 io_queue_async_work(ctx, req);
Jens Axboee65ef562019-03-12 10:16:44 -06002460 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002461 }
2462 }
Jens Axboee65ef562019-03-12 10:16:44 -06002463
2464 /* drop submission reference */
Jens Axboefcb323c2019-10-24 12:39:47 -06002465err:
Jens Axboeba816ad2019-09-28 11:36:45 -06002466 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002467
2468 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002469 if (ret) {
2470 io_cqring_add_event(ctx, req->user_data, ret);
2471 if (req->flags & REQ_F_LINK)
2472 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002473 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002474 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002475
2476 return ret;
2477}
2478
Jackie Liu4fe2c962019-09-09 20:50:40 +08002479static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002480 struct sqe_submit *s)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002481{
2482 int ret;
2483
2484 ret = io_req_defer(ctx, req, s->sqe);
2485 if (ret) {
2486 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002487 io_free_req(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002488 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2489 }
2490 return 0;
2491 }
2492
Jens Axboebc808bc2019-10-22 13:14:37 -06002493 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002494}
2495
2496static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002497 struct sqe_submit *s, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002498{
2499 int ret;
2500 int need_submit = false;
2501
2502 if (!shadow)
Jens Axboebc808bc2019-10-22 13:14:37 -06002503 return io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002504
2505 /*
2506 * Mark the first IO in link list as DRAIN, let all the following
2507 * IOs enter the defer list. all IO needs to be completed before link
2508 * list.
2509 */
2510 req->flags |= REQ_F_IO_DRAIN;
2511 ret = io_req_defer(ctx, req, s->sqe);
2512 if (ret) {
2513 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002514 io_free_req(req, NULL);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002515 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002516 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2517 return 0;
2518 }
2519 } else {
2520 /*
2521 * If ret == 0 means that all IOs in front of link io are
2522 * running done. let's queue link head.
2523 */
2524 need_submit = true;
2525 }
2526
2527 /* Insert shadow req to defer_list, blocking next IOs */
2528 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002529 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002530 list_add_tail(&shadow->list, &ctx->defer_list);
2531 spin_unlock_irq(&ctx->completion_lock);
2532
2533 if (need_submit)
Jens Axboebc808bc2019-10-22 13:14:37 -06002534 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002535
2536 return 0;
2537}
2538
Jens Axboe9e645e112019-05-10 16:07:28 -06002539#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2540
2541static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboebc808bc2019-10-22 13:14:37 -06002542 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002543{
2544 struct io_uring_sqe *sqe_copy;
2545 struct io_kiocb *req;
2546 int ret;
2547
2548 /* enforce forwards compatibility on users */
2549 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2550 ret = -EINVAL;
2551 goto err;
2552 }
2553
2554 req = io_get_req(ctx, state);
2555 if (unlikely(!req)) {
2556 ret = -EAGAIN;
2557 goto err;
2558 }
2559
2560 ret = io_req_set_file(ctx, s, state, req);
2561 if (unlikely(ret)) {
2562err_req:
Jens Axboeba816ad2019-09-28 11:36:45 -06002563 io_free_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002564err:
2565 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2566 return;
2567 }
2568
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002569 req->user_data = s->sqe->user_data;
2570
Jens Axboe9e645e112019-05-10 16:07:28 -06002571 /*
2572 * If we already have a head request, queue this one for async
2573 * submittal once the head completes. If we don't have a head but
2574 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2575 * submitted sync once the chain is complete. If none of those
2576 * conditions are true (normal request), then just queue it.
2577 */
2578 if (*link) {
2579 struct io_kiocb *prev = *link;
2580
2581 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2582 if (!sqe_copy) {
2583 ret = -EAGAIN;
2584 goto err_req;
2585 }
2586
2587 s->sqe = sqe_copy;
2588 memcpy(&req->submit, s, sizeof(*s));
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002589 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002590 list_add_tail(&req->list, &prev->link_list);
2591 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2592 req->flags |= REQ_F_LINK;
2593
2594 memcpy(&req->submit, s, sizeof(*s));
2595 INIT_LIST_HEAD(&req->link_list);
2596 *link = req;
2597 } else {
Jens Axboebc808bc2019-10-22 13:14:37 -06002598 io_queue_sqe(ctx, req, s);
Jens Axboe9e645e112019-05-10 16:07:28 -06002599 }
2600}
2601
Jens Axboe9a56a232019-01-09 09:06:50 -07002602/*
2603 * Batched submission is done, ensure local IO is flushed out.
2604 */
2605static void io_submit_state_end(struct io_submit_state *state)
2606{
2607 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002608 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002609 if (state->free_reqs)
2610 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2611 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002612}
2613
2614/*
2615 * Start submission side cache.
2616 */
2617static void io_submit_state_start(struct io_submit_state *state,
2618 struct io_ring_ctx *ctx, unsigned max_ios)
2619{
2620 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002621 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002622 state->file = NULL;
2623 state->ios_left = max_ios;
2624}
2625
Jens Axboe2b188cc2019-01-07 10:46:33 -07002626static void io_commit_sqring(struct io_ring_ctx *ctx)
2627{
Hristo Venev75b28af2019-08-26 17:23:46 +00002628 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002629
Hristo Venev75b28af2019-08-26 17:23:46 +00002630 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002631 /*
2632 * Ensure any loads from the SQEs are done at this point,
2633 * since once we write the new head, the application could
2634 * write new data to them.
2635 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002636 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002637 }
2638}
2639
2640/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002641 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2642 * that is mapped by userspace. This means that care needs to be taken to
2643 * ensure that reads are stable, as we cannot rely on userspace always
2644 * being a good citizen. If members of the sqe are validated and then later
2645 * used, it's important that those reads are done through READ_ONCE() to
2646 * prevent a re-load down the line.
2647 */
2648static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2649{
Hristo Venev75b28af2019-08-26 17:23:46 +00002650 struct io_rings *rings = ctx->rings;
2651 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002652 unsigned head;
2653
2654 /*
2655 * The cached sq head (or cq tail) serves two purposes:
2656 *
2657 * 1) allows us to batch the cost of updating the user visible
2658 * head updates.
2659 * 2) allows the kernel side to track the head on its own, even
2660 * though the application is the one updating it.
2661 */
2662 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002663 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002664 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002665 return false;
2666
Hristo Venev75b28af2019-08-26 17:23:46 +00002667 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002668 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002669 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002670 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002671 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002672 ctx->cached_sq_head++;
2673 return true;
2674 }
2675
2676 /* drop invalid entries */
2677 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002678 ctx->cached_sq_dropped++;
2679 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002680 return false;
2681}
2682
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002683static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002684 struct mm_struct **mm)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002685{
2686 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002687 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002688 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002689 bool prev_was_link = false;
2690 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002691 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002692
2693 if (nr > IO_PLUG_THRESHOLD) {
2694 io_submit_state_start(&state, ctx, nr);
2695 statep = &state;
2696 }
2697
2698 for (i = 0; i < nr; i++) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002699 struct sqe_submit s;
2700
2701 if (!io_get_sqring(ctx, &s))
2702 break;
2703
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002704 if (io_sqe_needs_user(s.sqe) && !*mm) {
2705 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
2706 if (!mm_fault) {
2707 use_mm(ctx->sqo_mm);
2708 *mm = ctx->sqo_mm;
2709 }
2710 }
2711
Jens Axboe9e645e112019-05-10 16:07:28 -06002712 /*
2713 * If previous wasn't linked and we have a linked command,
2714 * that's the end of the chain. Submit the previous link.
2715 */
2716 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002717 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002718 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002719 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002720 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002721 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06002722
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002723 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002724 if (!shadow_req) {
2725 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002726 if (unlikely(!shadow_req))
2727 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002728 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2729 refcount_dec(&shadow_req->refs);
2730 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002731 shadow_req->sequence = s.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002732 }
2733
Jackie Liua1041c22019-09-18 17:25:52 +08002734out:
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002735 s.has_user = *mm != NULL;
2736 s.in_async = true;
2737 s.needs_fixed_file = true;
Jens Axboe51c3ff62019-11-03 06:52:50 -07002738 trace_io_uring_submit_sqe(ctx, s.sqe->user_data, true, true);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002739 io_submit_sqe(ctx, &s, statep, &link);
2740 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002741 }
2742
Jens Axboe9e645e112019-05-10 16:07:28 -06002743 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002744 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002745 if (statep)
2746 io_submit_state_end(&state);
2747
2748 return submitted;
2749}
2750
2751static int io_sq_thread(void *data)
2752{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002753 struct io_ring_ctx *ctx = data;
2754 struct mm_struct *cur_mm = NULL;
2755 mm_segment_t old_fs;
2756 DEFINE_WAIT(wait);
2757 unsigned inflight;
2758 unsigned long timeout;
2759
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002760 complete(&ctx->sqo_thread_started);
2761
Jens Axboe6c271ce2019-01-10 11:22:30 -07002762 old_fs = get_fs();
2763 set_fs(USER_DS);
2764
2765 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002766 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002767 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002768
2769 if (inflight) {
2770 unsigned nr_events = 0;
2771
2772 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002773 /*
2774 * inflight is the count of the maximum possible
2775 * entries we submitted, but it can be smaller
2776 * if we dropped some of them. If we don't have
2777 * poll entries available, then we know that we
2778 * have nothing left to poll for. Reset the
2779 * inflight count to zero in that case.
2780 */
2781 mutex_lock(&ctx->uring_lock);
2782 if (!list_empty(&ctx->poll_list))
2783 __io_iopoll_check(ctx, &nr_events, 0);
2784 else
2785 inflight = 0;
2786 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002787 } else {
2788 /*
2789 * Normal IO, just pretend everything completed.
2790 * We don't have to poll completions for that.
2791 */
2792 nr_events = inflight;
2793 }
2794
2795 inflight -= nr_events;
2796 if (!inflight)
2797 timeout = jiffies + ctx->sq_thread_idle;
2798 }
2799
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002800 to_submit = io_sqring_entries(ctx);
2801 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002802 /*
2803 * We're polling. If we're within the defined idle
2804 * period, then let us spin without work before going
2805 * to sleep.
2806 */
2807 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002808 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002809 continue;
2810 }
2811
2812 /*
2813 * Drop cur_mm before scheduling, we can't hold it for
2814 * long periods (or over schedule()). Do this before
2815 * adding ourselves to the waitqueue, as the unuse/drop
2816 * may sleep.
2817 */
2818 if (cur_mm) {
2819 unuse_mm(cur_mm);
2820 mmput(cur_mm);
2821 cur_mm = NULL;
2822 }
2823
2824 prepare_to_wait(&ctx->sqo_wait, &wait,
2825 TASK_INTERRUPTIBLE);
2826
2827 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002828 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002829 /* make sure to read SQ tail after writing flags */
2830 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002831
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002832 to_submit = io_sqring_entries(ctx);
2833 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002834 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002835 finish_wait(&ctx->sqo_wait, &wait);
2836 break;
2837 }
2838 if (signal_pending(current))
2839 flush_signals(current);
2840 schedule();
2841 finish_wait(&ctx->sqo_wait, &wait);
2842
Hristo Venev75b28af2019-08-26 17:23:46 +00002843 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002844 continue;
2845 }
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 }
2850
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002851 to_submit = min(to_submit, ctx->sq_entries);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002852 inflight += io_submit_sqes(ctx, to_submit, &cur_mm);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002853
2854 /* Commit SQ ring head once we've consumed all SQEs */
2855 io_commit_sqring(ctx);
2856 }
2857
2858 set_fs(old_fs);
2859 if (cur_mm) {
2860 unuse_mm(cur_mm);
2861 mmput(cur_mm);
2862 }
Jens Axboe06058632019-04-13 09:26:03 -06002863
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002864 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002865
Jens Axboe6c271ce2019-01-10 11:22:30 -07002866 return 0;
2867}
2868
Jens Axboefcb323c2019-10-24 12:39:47 -06002869static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit,
2870 struct file *ring_file, int ring_fd)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002871{
Jens Axboe9a56a232019-01-09 09:06:50 -07002872 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002873 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002874 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002875 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002876 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002877
Jens Axboe9a56a232019-01-09 09:06:50 -07002878 if (to_submit > IO_PLUG_THRESHOLD) {
2879 io_submit_state_start(&state, ctx, to_submit);
2880 statep = &state;
2881 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002882
2883 for (i = 0; i < to_submit; i++) {
2884 struct sqe_submit s;
2885
2886 if (!io_get_sqring(ctx, &s))
2887 break;
2888
Jens Axboe9e645e112019-05-10 16:07:28 -06002889 /*
2890 * If previous wasn't linked and we have a linked command,
2891 * that's the end of the chain. Submit the previous link.
2892 */
2893 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002894 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002895 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002896 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002897 }
2898 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2899
Jackie Liu4fe2c962019-09-09 20:50:40 +08002900 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2901 if (!shadow_req) {
2902 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002903 if (unlikely(!shadow_req))
2904 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002905 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2906 refcount_dec(&shadow_req->refs);
2907 }
2908 shadow_req->sequence = s.sequence;
2909 }
2910
Jackie Liua1041c22019-09-18 17:25:52 +08002911out:
Jens Axboefcb323c2019-10-24 12:39:47 -06002912 s.ring_file = ring_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002913 s.has_user = true;
Jackie Liuba5290c2019-10-09 09:19:59 +08002914 s.in_async = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002915 s.needs_fixed_file = false;
Jens Axboefcb323c2019-10-24 12:39:47 -06002916 s.ring_fd = ring_fd;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002917 submit++;
Jens Axboe51c3ff62019-11-03 06:52:50 -07002918 trace_io_uring_submit_sqe(ctx, s.sqe->user_data, true, false);
Jens Axboebc808bc2019-10-22 13:14:37 -06002919 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002920 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002921
Jens Axboe9e645e112019-05-10 16:07:28 -06002922 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002923 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9a56a232019-01-09 09:06:50 -07002924 if (statep)
2925 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002926
Pavel Begunkov935d1e42019-10-25 12:31:31 +03002927 io_commit_sqring(ctx);
2928
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002929 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002930}
2931
Jens Axboebda52162019-09-24 13:47:15 -06002932struct io_wait_queue {
2933 struct wait_queue_entry wq;
2934 struct io_ring_ctx *ctx;
2935 unsigned to_wait;
2936 unsigned nr_timeouts;
2937};
2938
2939static inline bool io_should_wake(struct io_wait_queue *iowq)
2940{
2941 struct io_ring_ctx *ctx = iowq->ctx;
2942
2943 /*
2944 * Wake up if we have enough events, or if a timeout occured since we
2945 * started waiting. For timeouts, we always want to return to userspace,
2946 * regardless of event count.
2947 */
2948 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2949 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2950}
2951
2952static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2953 int wake_flags, void *key)
2954{
2955 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
2956 wq);
2957
2958 if (!io_should_wake(iowq))
2959 return -1;
2960
2961 return autoremove_wake_function(curr, mode, wake_flags, key);
2962}
2963
Jens Axboe2b188cc2019-01-07 10:46:33 -07002964/*
2965 * Wait until events become available, if we don't already have some. The
2966 * application must reap them itself, as they reside on the shared cq ring.
2967 */
2968static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2969 const sigset_t __user *sig, size_t sigsz)
2970{
Jens Axboebda52162019-09-24 13:47:15 -06002971 struct io_wait_queue iowq = {
2972 .wq = {
2973 .private = current,
2974 .func = io_wake_function,
2975 .entry = LIST_HEAD_INIT(iowq.wq.entry),
2976 },
2977 .ctx = ctx,
2978 .to_wait = min_events,
2979 };
Hristo Venev75b28af2019-08-26 17:23:46 +00002980 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08002981 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002982
Hristo Venev75b28af2019-08-26 17:23:46 +00002983 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002984 return 0;
2985
2986 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002987#ifdef CONFIG_COMPAT
2988 if (in_compat_syscall())
2989 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002990 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002991 else
2992#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002993 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002994
Jens Axboe2b188cc2019-01-07 10:46:33 -07002995 if (ret)
2996 return ret;
2997 }
2998
Jens Axboebda52162019-09-24 13:47:15 -06002999 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003000 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003001 do {
3002 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3003 TASK_INTERRUPTIBLE);
3004 if (io_should_wake(&iowq))
3005 break;
3006 schedule();
3007 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003008 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003009 break;
3010 }
3011 } while (1);
3012 finish_wait(&ctx->wait, &iowq.wq);
3013
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003014 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003015
Hristo Venev75b28af2019-08-26 17:23:46 +00003016 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003017}
3018
Jens Axboe6b063142019-01-10 22:13:58 -07003019static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3020{
3021#if defined(CONFIG_UNIX)
3022 if (ctx->ring_sock) {
3023 struct sock *sock = ctx->ring_sock->sk;
3024 struct sk_buff *skb;
3025
3026 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3027 kfree_skb(skb);
3028 }
3029#else
3030 int i;
3031
Jens Axboe65e19f52019-10-26 07:20:21 -06003032 for (i = 0; i < ctx->nr_user_files; i++) {
3033 struct file *file;
3034
3035 file = io_file_from_index(ctx, i);
3036 if (file)
3037 fput(file);
3038 }
Jens Axboe6b063142019-01-10 22:13:58 -07003039#endif
3040}
3041
3042static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3043{
Jens Axboe65e19f52019-10-26 07:20:21 -06003044 unsigned nr_tables, i;
3045
3046 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003047 return -ENXIO;
3048
3049 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003050 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3051 for (i = 0; i < nr_tables; i++)
3052 kfree(ctx->file_table[i].files);
3053 kfree(ctx->file_table);
3054 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003055 ctx->nr_user_files = 0;
3056 return 0;
3057}
3058
Jens Axboe6c271ce2019-01-10 11:22:30 -07003059static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3060{
3061 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003062 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003063 /*
3064 * The park is a bit of a work-around, without it we get
3065 * warning spews on shutdown with SQPOLL set and affinity
3066 * set to a single CPU.
3067 */
Jens Axboe06058632019-04-13 09:26:03 -06003068 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003069 kthread_stop(ctx->sqo_thread);
3070 ctx->sqo_thread = NULL;
3071 }
3072}
3073
Jens Axboe6b063142019-01-10 22:13:58 -07003074static void io_finish_async(struct io_ring_ctx *ctx)
3075{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003076 io_sq_thread_stop(ctx);
3077
Jens Axboe561fb042019-10-24 07:25:42 -06003078 if (ctx->io_wq) {
3079 io_wq_destroy(ctx->io_wq);
3080 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003081 }
3082}
3083
3084#if defined(CONFIG_UNIX)
3085static void io_destruct_skb(struct sk_buff *skb)
3086{
3087 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3088
Jens Axboe561fb042019-10-24 07:25:42 -06003089 if (ctx->io_wq)
3090 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003091
Jens Axboe6b063142019-01-10 22:13:58 -07003092 unix_destruct_scm(skb);
3093}
3094
3095/*
3096 * Ensure the UNIX gc is aware of our file set, so we are certain that
3097 * the io_uring can be safely unregistered on process exit, even if we have
3098 * loops in the file referencing.
3099 */
3100static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3101{
3102 struct sock *sk = ctx->ring_sock->sk;
3103 struct scm_fp_list *fpl;
3104 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003105 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003106
3107 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3108 unsigned long inflight = ctx->user->unix_inflight + nr;
3109
3110 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3111 return -EMFILE;
3112 }
3113
3114 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3115 if (!fpl)
3116 return -ENOMEM;
3117
3118 skb = alloc_skb(0, GFP_KERNEL);
3119 if (!skb) {
3120 kfree(fpl);
3121 return -ENOMEM;
3122 }
3123
3124 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003125
Jens Axboe08a45172019-10-03 08:11:03 -06003126 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003127 fpl->user = get_uid(ctx->user);
3128 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003129 struct file *file = io_file_from_index(ctx, i + offset);
3130
3131 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003132 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003133 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003134 unix_inflight(fpl->user, fpl->fp[nr_files]);
3135 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003136 }
3137
Jens Axboe08a45172019-10-03 08:11:03 -06003138 if (nr_files) {
3139 fpl->max = SCM_MAX_FD;
3140 fpl->count = nr_files;
3141 UNIXCB(skb).fp = fpl;
3142 skb->destructor = io_destruct_skb;
3143 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3144 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003145
Jens Axboe08a45172019-10-03 08:11:03 -06003146 for (i = 0; i < nr_files; i++)
3147 fput(fpl->fp[i]);
3148 } else {
3149 kfree_skb(skb);
3150 kfree(fpl);
3151 }
Jens Axboe6b063142019-01-10 22:13:58 -07003152
3153 return 0;
3154}
3155
3156/*
3157 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3158 * causes regular reference counting to break down. We rely on the UNIX
3159 * garbage collection to take care of this problem for us.
3160 */
3161static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3162{
3163 unsigned left, total;
3164 int ret = 0;
3165
3166 total = 0;
3167 left = ctx->nr_user_files;
3168 while (left) {
3169 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003170
3171 ret = __io_sqe_files_scm(ctx, this_files, total);
3172 if (ret)
3173 break;
3174 left -= this_files;
3175 total += this_files;
3176 }
3177
3178 if (!ret)
3179 return 0;
3180
3181 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003182 struct file *file = io_file_from_index(ctx, total);
3183
3184 if (file)
3185 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003186 total++;
3187 }
3188
3189 return ret;
3190}
3191#else
3192static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3193{
3194 return 0;
3195}
3196#endif
3197
Jens Axboe65e19f52019-10-26 07:20:21 -06003198static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3199 unsigned nr_files)
3200{
3201 int i;
3202
3203 for (i = 0; i < nr_tables; i++) {
3204 struct fixed_file_table *table = &ctx->file_table[i];
3205 unsigned this_files;
3206
3207 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3208 table->files = kcalloc(this_files, sizeof(struct file *),
3209 GFP_KERNEL);
3210 if (!table->files)
3211 break;
3212 nr_files -= this_files;
3213 }
3214
3215 if (i == nr_tables)
3216 return 0;
3217
3218 for (i = 0; i < nr_tables; i++) {
3219 struct fixed_file_table *table = &ctx->file_table[i];
3220 kfree(table->files);
3221 }
3222 return 1;
3223}
3224
Jens Axboe6b063142019-01-10 22:13:58 -07003225static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3226 unsigned nr_args)
3227{
3228 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003229 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003230 int fd, ret = 0;
3231 unsigned i;
3232
Jens Axboe65e19f52019-10-26 07:20:21 -06003233 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003234 return -EBUSY;
3235 if (!nr_args)
3236 return -EINVAL;
3237 if (nr_args > IORING_MAX_FIXED_FILES)
3238 return -EMFILE;
3239
Jens Axboe65e19f52019-10-26 07:20:21 -06003240 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3241 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3242 GFP_KERNEL);
3243 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003244 return -ENOMEM;
3245
Jens Axboe65e19f52019-10-26 07:20:21 -06003246 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3247 kfree(ctx->file_table);
3248 return -ENOMEM;
3249 }
3250
Jens Axboe08a45172019-10-03 08:11:03 -06003251 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003252 struct fixed_file_table *table;
3253 unsigned index;
3254
Jens Axboe6b063142019-01-10 22:13:58 -07003255 ret = -EFAULT;
3256 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3257 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003258 /* allow sparse sets */
3259 if (fd == -1) {
3260 ret = 0;
3261 continue;
3262 }
Jens Axboe6b063142019-01-10 22:13:58 -07003263
Jens Axboe65e19f52019-10-26 07:20:21 -06003264 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3265 index = i & IORING_FILE_TABLE_MASK;
3266 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003267
3268 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003269 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003270 break;
3271 /*
3272 * Don't allow io_uring instances to be registered. If UNIX
3273 * isn't enabled, then this causes a reference cycle and this
3274 * instance can never get freed. If UNIX is enabled we'll
3275 * handle it just fine, but there's still no point in allowing
3276 * a ring fd as it doesn't support regular read/write anyway.
3277 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003278 if (table->files[index]->f_op == &io_uring_fops) {
3279 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003280 break;
3281 }
Jens Axboe6b063142019-01-10 22:13:58 -07003282 ret = 0;
3283 }
3284
3285 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003286 for (i = 0; i < ctx->nr_user_files; i++) {
3287 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003288
Jens Axboe65e19f52019-10-26 07:20:21 -06003289 file = io_file_from_index(ctx, i);
3290 if (file)
3291 fput(file);
3292 }
3293 for (i = 0; i < nr_tables; i++)
3294 kfree(ctx->file_table[i].files);
3295
3296 kfree(ctx->file_table);
3297 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003298 ctx->nr_user_files = 0;
3299 return ret;
3300 }
3301
3302 ret = io_sqe_files_scm(ctx);
3303 if (ret)
3304 io_sqe_files_unregister(ctx);
3305
3306 return ret;
3307}
3308
Jens Axboec3a31e62019-10-03 13:59:56 -06003309static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3310{
3311#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003312 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003313 struct sock *sock = ctx->ring_sock->sk;
3314 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3315 struct sk_buff *skb;
3316 int i;
3317
3318 __skb_queue_head_init(&list);
3319
3320 /*
3321 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3322 * remove this entry and rearrange the file array.
3323 */
3324 skb = skb_dequeue(head);
3325 while (skb) {
3326 struct scm_fp_list *fp;
3327
3328 fp = UNIXCB(skb).fp;
3329 for (i = 0; i < fp->count; i++) {
3330 int left;
3331
3332 if (fp->fp[i] != file)
3333 continue;
3334
3335 unix_notinflight(fp->user, fp->fp[i]);
3336 left = fp->count - 1 - i;
3337 if (left) {
3338 memmove(&fp->fp[i], &fp->fp[i + 1],
3339 left * sizeof(struct file *));
3340 }
3341 fp->count--;
3342 if (!fp->count) {
3343 kfree_skb(skb);
3344 skb = NULL;
3345 } else {
3346 __skb_queue_tail(&list, skb);
3347 }
3348 fput(file);
3349 file = NULL;
3350 break;
3351 }
3352
3353 if (!file)
3354 break;
3355
3356 __skb_queue_tail(&list, skb);
3357
3358 skb = skb_dequeue(head);
3359 }
3360
3361 if (skb_peek(&list)) {
3362 spin_lock_irq(&head->lock);
3363 while ((skb = __skb_dequeue(&list)) != NULL)
3364 __skb_queue_tail(head, skb);
3365 spin_unlock_irq(&head->lock);
3366 }
3367#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003368 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003369#endif
3370}
3371
3372static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3373 int index)
3374{
3375#if defined(CONFIG_UNIX)
3376 struct sock *sock = ctx->ring_sock->sk;
3377 struct sk_buff_head *head = &sock->sk_receive_queue;
3378 struct sk_buff *skb;
3379
3380 /*
3381 * See if we can merge this file into an existing skb SCM_RIGHTS
3382 * file set. If there's no room, fall back to allocating a new skb
3383 * and filling it in.
3384 */
3385 spin_lock_irq(&head->lock);
3386 skb = skb_peek(head);
3387 if (skb) {
3388 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3389
3390 if (fpl->count < SCM_MAX_FD) {
3391 __skb_unlink(skb, head);
3392 spin_unlock_irq(&head->lock);
3393 fpl->fp[fpl->count] = get_file(file);
3394 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3395 fpl->count++;
3396 spin_lock_irq(&head->lock);
3397 __skb_queue_head(head, skb);
3398 } else {
3399 skb = NULL;
3400 }
3401 }
3402 spin_unlock_irq(&head->lock);
3403
3404 if (skb) {
3405 fput(file);
3406 return 0;
3407 }
3408
3409 return __io_sqe_files_scm(ctx, 1, index);
3410#else
3411 return 0;
3412#endif
3413}
3414
3415static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3416 unsigned nr_args)
3417{
3418 struct io_uring_files_update up;
3419 __s32 __user *fds;
3420 int fd, i, err;
3421 __u32 done;
3422
Jens Axboe65e19f52019-10-26 07:20:21 -06003423 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003424 return -ENXIO;
3425 if (!nr_args)
3426 return -EINVAL;
3427 if (copy_from_user(&up, arg, sizeof(up)))
3428 return -EFAULT;
3429 if (check_add_overflow(up.offset, nr_args, &done))
3430 return -EOVERFLOW;
3431 if (done > ctx->nr_user_files)
3432 return -EINVAL;
3433
3434 done = 0;
3435 fds = (__s32 __user *) up.fds;
3436 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003437 struct fixed_file_table *table;
3438 unsigned index;
3439
Jens Axboec3a31e62019-10-03 13:59:56 -06003440 err = 0;
3441 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3442 err = -EFAULT;
3443 break;
3444 }
3445 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003446 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3447 index = i & IORING_FILE_TABLE_MASK;
3448 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003449 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003450 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003451 }
3452 if (fd != -1) {
3453 struct file *file;
3454
3455 file = fget(fd);
3456 if (!file) {
3457 err = -EBADF;
3458 break;
3459 }
3460 /*
3461 * Don't allow io_uring instances to be registered. If
3462 * UNIX isn't enabled, then this causes a reference
3463 * cycle and this instance can never get freed. If UNIX
3464 * is enabled we'll handle it just fine, but there's
3465 * still no point in allowing a ring fd as it doesn't
3466 * support regular read/write anyway.
3467 */
3468 if (file->f_op == &io_uring_fops) {
3469 fput(file);
3470 err = -EBADF;
3471 break;
3472 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003473 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003474 err = io_sqe_file_register(ctx, file, i);
3475 if (err)
3476 break;
3477 }
3478 nr_args--;
3479 done++;
3480 up.offset++;
3481 }
3482
3483 return done ? done : err;
3484}
3485
Jens Axboe6c271ce2019-01-10 11:22:30 -07003486static int io_sq_offload_start(struct io_ring_ctx *ctx,
3487 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003488{
Jens Axboe561fb042019-10-24 07:25:42 -06003489 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003490 int ret;
3491
Jens Axboe6c271ce2019-01-10 11:22:30 -07003492 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003493 mmgrab(current->mm);
3494 ctx->sqo_mm = current->mm;
3495
Jens Axboe6c271ce2019-01-10 11:22:30 -07003496 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003497 ret = -EPERM;
3498 if (!capable(CAP_SYS_ADMIN))
3499 goto err;
3500
Jens Axboe917257d2019-04-13 09:28:55 -06003501 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3502 if (!ctx->sq_thread_idle)
3503 ctx->sq_thread_idle = HZ;
3504
Jens Axboe6c271ce2019-01-10 11:22:30 -07003505 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003506 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003507
Jens Axboe917257d2019-04-13 09:28:55 -06003508 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003509 if (cpu >= nr_cpu_ids)
3510 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003511 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003512 goto err;
3513
Jens Axboe6c271ce2019-01-10 11:22:30 -07003514 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3515 ctx, cpu,
3516 "io_uring-sq");
3517 } else {
3518 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3519 "io_uring-sq");
3520 }
3521 if (IS_ERR(ctx->sqo_thread)) {
3522 ret = PTR_ERR(ctx->sqo_thread);
3523 ctx->sqo_thread = NULL;
3524 goto err;
3525 }
3526 wake_up_process(ctx->sqo_thread);
3527 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3528 /* Can't have SQ_AFF without SQPOLL */
3529 ret = -EINVAL;
3530 goto err;
3531 }
3532
Jens Axboe561fb042019-10-24 07:25:42 -06003533 /* Do QD, or 4 * CPUS, whatever is smallest */
3534 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
3535 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm);
Jens Axboe975c99a52019-10-30 08:42:56 -06003536 if (IS_ERR(ctx->io_wq)) {
3537 ret = PTR_ERR(ctx->io_wq);
3538 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003539 goto err;
3540 }
3541
3542 return 0;
3543err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003544 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003545 mmdrop(ctx->sqo_mm);
3546 ctx->sqo_mm = NULL;
3547 return ret;
3548}
3549
3550static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3551{
3552 atomic_long_sub(nr_pages, &user->locked_vm);
3553}
3554
3555static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3556{
3557 unsigned long page_limit, cur_pages, new_pages;
3558
3559 /* Don't allow more pages than we can safely lock */
3560 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3561
3562 do {
3563 cur_pages = atomic_long_read(&user->locked_vm);
3564 new_pages = cur_pages + nr_pages;
3565 if (new_pages > page_limit)
3566 return -ENOMEM;
3567 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3568 new_pages) != cur_pages);
3569
3570 return 0;
3571}
3572
3573static void io_mem_free(void *ptr)
3574{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003575 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003576
Mark Rutland52e04ef2019-04-30 17:30:21 +01003577 if (!ptr)
3578 return;
3579
3580 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003581 if (put_page_testzero(page))
3582 free_compound_page(page);
3583}
3584
3585static void *io_mem_alloc(size_t size)
3586{
3587 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3588 __GFP_NORETRY;
3589
3590 return (void *) __get_free_pages(gfp_flags, get_order(size));
3591}
3592
Hristo Venev75b28af2019-08-26 17:23:46 +00003593static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3594 size_t *sq_offset)
3595{
3596 struct io_rings *rings;
3597 size_t off, sq_array_size;
3598
3599 off = struct_size(rings, cqes, cq_entries);
3600 if (off == SIZE_MAX)
3601 return SIZE_MAX;
3602
3603#ifdef CONFIG_SMP
3604 off = ALIGN(off, SMP_CACHE_BYTES);
3605 if (off == 0)
3606 return SIZE_MAX;
3607#endif
3608
3609 sq_array_size = array_size(sizeof(u32), sq_entries);
3610 if (sq_array_size == SIZE_MAX)
3611 return SIZE_MAX;
3612
3613 if (check_add_overflow(off, sq_array_size, &off))
3614 return SIZE_MAX;
3615
3616 if (sq_offset)
3617 *sq_offset = off;
3618
3619 return off;
3620}
3621
Jens Axboe2b188cc2019-01-07 10:46:33 -07003622static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3623{
Hristo Venev75b28af2019-08-26 17:23:46 +00003624 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003625
Hristo Venev75b28af2019-08-26 17:23:46 +00003626 pages = (size_t)1 << get_order(
3627 rings_size(sq_entries, cq_entries, NULL));
3628 pages += (size_t)1 << get_order(
3629 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003630
Hristo Venev75b28af2019-08-26 17:23:46 +00003631 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003632}
3633
Jens Axboeedafcce2019-01-09 09:16:05 -07003634static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3635{
3636 int i, j;
3637
3638 if (!ctx->user_bufs)
3639 return -ENXIO;
3640
3641 for (i = 0; i < ctx->nr_user_bufs; i++) {
3642 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3643
3644 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003645 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003646
3647 if (ctx->account_mem)
3648 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003649 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003650 imu->nr_bvecs = 0;
3651 }
3652
3653 kfree(ctx->user_bufs);
3654 ctx->user_bufs = NULL;
3655 ctx->nr_user_bufs = 0;
3656 return 0;
3657}
3658
3659static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3660 void __user *arg, unsigned index)
3661{
3662 struct iovec __user *src;
3663
3664#ifdef CONFIG_COMPAT
3665 if (ctx->compat) {
3666 struct compat_iovec __user *ciovs;
3667 struct compat_iovec ciov;
3668
3669 ciovs = (struct compat_iovec __user *) arg;
3670 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3671 return -EFAULT;
3672
3673 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3674 dst->iov_len = ciov.iov_len;
3675 return 0;
3676 }
3677#endif
3678 src = (struct iovec __user *) arg;
3679 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3680 return -EFAULT;
3681 return 0;
3682}
3683
3684static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3685 unsigned nr_args)
3686{
3687 struct vm_area_struct **vmas = NULL;
3688 struct page **pages = NULL;
3689 int i, j, got_pages = 0;
3690 int ret = -EINVAL;
3691
3692 if (ctx->user_bufs)
3693 return -EBUSY;
3694 if (!nr_args || nr_args > UIO_MAXIOV)
3695 return -EINVAL;
3696
3697 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3698 GFP_KERNEL);
3699 if (!ctx->user_bufs)
3700 return -ENOMEM;
3701
3702 for (i = 0; i < nr_args; i++) {
3703 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3704 unsigned long off, start, end, ubuf;
3705 int pret, nr_pages;
3706 struct iovec iov;
3707 size_t size;
3708
3709 ret = io_copy_iov(ctx, &iov, arg, i);
3710 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003711 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003712
3713 /*
3714 * Don't impose further limits on the size and buffer
3715 * constraints here, we'll -EINVAL later when IO is
3716 * submitted if they are wrong.
3717 */
3718 ret = -EFAULT;
3719 if (!iov.iov_base || !iov.iov_len)
3720 goto err;
3721
3722 /* arbitrary limit, but we need something */
3723 if (iov.iov_len > SZ_1G)
3724 goto err;
3725
3726 ubuf = (unsigned long) iov.iov_base;
3727 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3728 start = ubuf >> PAGE_SHIFT;
3729 nr_pages = end - start;
3730
3731 if (ctx->account_mem) {
3732 ret = io_account_mem(ctx->user, nr_pages);
3733 if (ret)
3734 goto err;
3735 }
3736
3737 ret = 0;
3738 if (!pages || nr_pages > got_pages) {
3739 kfree(vmas);
3740 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003741 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003742 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003743 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003744 sizeof(struct vm_area_struct *),
3745 GFP_KERNEL);
3746 if (!pages || !vmas) {
3747 ret = -ENOMEM;
3748 if (ctx->account_mem)
3749 io_unaccount_mem(ctx->user, nr_pages);
3750 goto err;
3751 }
3752 got_pages = nr_pages;
3753 }
3754
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003755 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003756 GFP_KERNEL);
3757 ret = -ENOMEM;
3758 if (!imu->bvec) {
3759 if (ctx->account_mem)
3760 io_unaccount_mem(ctx->user, nr_pages);
3761 goto err;
3762 }
3763
3764 ret = 0;
3765 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003766 pret = get_user_pages(ubuf, nr_pages,
3767 FOLL_WRITE | FOLL_LONGTERM,
3768 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003769 if (pret == nr_pages) {
3770 /* don't support file backed memory */
3771 for (j = 0; j < nr_pages; j++) {
3772 struct vm_area_struct *vma = vmas[j];
3773
3774 if (vma->vm_file &&
3775 !is_file_hugepages(vma->vm_file)) {
3776 ret = -EOPNOTSUPP;
3777 break;
3778 }
3779 }
3780 } else {
3781 ret = pret < 0 ? pret : -EFAULT;
3782 }
3783 up_read(&current->mm->mmap_sem);
3784 if (ret) {
3785 /*
3786 * if we did partial map, or found file backed vmas,
3787 * release any pages we did get
3788 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003789 if (pret > 0)
3790 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003791 if (ctx->account_mem)
3792 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003793 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003794 goto err;
3795 }
3796
3797 off = ubuf & ~PAGE_MASK;
3798 size = iov.iov_len;
3799 for (j = 0; j < nr_pages; j++) {
3800 size_t vec_len;
3801
3802 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3803 imu->bvec[j].bv_page = pages[j];
3804 imu->bvec[j].bv_len = vec_len;
3805 imu->bvec[j].bv_offset = off;
3806 off = 0;
3807 size -= vec_len;
3808 }
3809 /* store original address for later verification */
3810 imu->ubuf = ubuf;
3811 imu->len = iov.iov_len;
3812 imu->nr_bvecs = nr_pages;
3813
3814 ctx->nr_user_bufs++;
3815 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003816 kvfree(pages);
3817 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003818 return 0;
3819err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003820 kvfree(pages);
3821 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003822 io_sqe_buffer_unregister(ctx);
3823 return ret;
3824}
3825
Jens Axboe9b402842019-04-11 11:45:41 -06003826static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3827{
3828 __s32 __user *fds = arg;
3829 int fd;
3830
3831 if (ctx->cq_ev_fd)
3832 return -EBUSY;
3833
3834 if (copy_from_user(&fd, fds, sizeof(*fds)))
3835 return -EFAULT;
3836
3837 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3838 if (IS_ERR(ctx->cq_ev_fd)) {
3839 int ret = PTR_ERR(ctx->cq_ev_fd);
3840 ctx->cq_ev_fd = NULL;
3841 return ret;
3842 }
3843
3844 return 0;
3845}
3846
3847static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3848{
3849 if (ctx->cq_ev_fd) {
3850 eventfd_ctx_put(ctx->cq_ev_fd);
3851 ctx->cq_ev_fd = NULL;
3852 return 0;
3853 }
3854
3855 return -ENXIO;
3856}
3857
Jens Axboe2b188cc2019-01-07 10:46:33 -07003858static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3859{
Jens Axboe6b063142019-01-10 22:13:58 -07003860 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003861 if (ctx->sqo_mm)
3862 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003863
3864 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003865 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003866 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003867 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003868
Jens Axboe2b188cc2019-01-07 10:46:33 -07003869#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003870 if (ctx->ring_sock) {
3871 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003872 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003873 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003874#endif
3875
Hristo Venev75b28af2019-08-26 17:23:46 +00003876 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003877 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003878
3879 percpu_ref_exit(&ctx->refs);
3880 if (ctx->account_mem)
3881 io_unaccount_mem(ctx->user,
3882 ring_pages(ctx->sq_entries, ctx->cq_entries));
3883 free_uid(ctx->user);
3884 kfree(ctx);
3885}
3886
3887static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3888{
3889 struct io_ring_ctx *ctx = file->private_data;
3890 __poll_t mask = 0;
3891
3892 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003893 /*
3894 * synchronizes with barrier from wq_has_sleeper call in
3895 * io_commit_cqring
3896 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003897 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003898 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3899 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003900 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003901 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003902 mask |= EPOLLIN | EPOLLRDNORM;
3903
3904 return mask;
3905}
3906
3907static int io_uring_fasync(int fd, struct file *file, int on)
3908{
3909 struct io_ring_ctx *ctx = file->private_data;
3910
3911 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3912}
3913
3914static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3915{
3916 mutex_lock(&ctx->uring_lock);
3917 percpu_ref_kill(&ctx->refs);
3918 mutex_unlock(&ctx->uring_lock);
3919
Jens Axboe5262f562019-09-17 12:26:57 -06003920 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003921 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06003922
3923 if (ctx->io_wq)
3924 io_wq_cancel_all(ctx->io_wq);
3925
Jens Axboedef596e2019-01-09 08:59:42 -07003926 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003927 wait_for_completion(&ctx->ctx_done);
3928 io_ring_ctx_free(ctx);
3929}
3930
3931static int io_uring_release(struct inode *inode, struct file *file)
3932{
3933 struct io_ring_ctx *ctx = file->private_data;
3934
3935 file->private_data = NULL;
3936 io_ring_ctx_wait_and_kill(ctx);
3937 return 0;
3938}
3939
Jens Axboefcb323c2019-10-24 12:39:47 -06003940static void io_uring_cancel_files(struct io_ring_ctx *ctx,
3941 struct files_struct *files)
3942{
3943 struct io_kiocb *req;
3944 DEFINE_WAIT(wait);
3945
3946 while (!list_empty_careful(&ctx->inflight_list)) {
3947 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
3948
3949 spin_lock_irq(&ctx->inflight_lock);
3950 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
3951 if (req->work.files == files) {
3952 ret = io_wq_cancel_work(ctx->io_wq, &req->work);
3953 break;
3954 }
3955 }
3956 if (ret == IO_WQ_CANCEL_RUNNING)
3957 prepare_to_wait(&ctx->inflight_wait, &wait,
3958 TASK_UNINTERRUPTIBLE);
3959
3960 spin_unlock_irq(&ctx->inflight_lock);
3961
3962 /*
3963 * We need to keep going until we get NOTFOUND. We only cancel
3964 * one work at the time.
3965 *
3966 * If we get CANCEL_RUNNING, then wait for a work to complete
3967 * before continuing.
3968 */
3969 if (ret == IO_WQ_CANCEL_OK)
3970 continue;
3971 else if (ret != IO_WQ_CANCEL_RUNNING)
3972 break;
3973 schedule();
3974 }
3975}
3976
3977static int io_uring_flush(struct file *file, void *data)
3978{
3979 struct io_ring_ctx *ctx = file->private_data;
3980
3981 io_uring_cancel_files(ctx, data);
3982 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
3983 io_wq_cancel_all(ctx->io_wq);
3984 return 0;
3985}
3986
Jens Axboe2b188cc2019-01-07 10:46:33 -07003987static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3988{
3989 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3990 unsigned long sz = vma->vm_end - vma->vm_start;
3991 struct io_ring_ctx *ctx = file->private_data;
3992 unsigned long pfn;
3993 struct page *page;
3994 void *ptr;
3995
3996 switch (offset) {
3997 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003998 case IORING_OFF_CQ_RING:
3999 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004000 break;
4001 case IORING_OFF_SQES:
4002 ptr = ctx->sq_sqes;
4003 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004004 default:
4005 return -EINVAL;
4006 }
4007
4008 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004009 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004010 return -EINVAL;
4011
4012 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4013 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4014}
4015
4016SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4017 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4018 size_t, sigsz)
4019{
4020 struct io_ring_ctx *ctx;
4021 long ret = -EBADF;
4022 int submitted = 0;
4023 struct fd f;
4024
Jens Axboe6c271ce2019-01-10 11:22:30 -07004025 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004026 return -EINVAL;
4027
4028 f = fdget(fd);
4029 if (!f.file)
4030 return -EBADF;
4031
4032 ret = -EOPNOTSUPP;
4033 if (f.file->f_op != &io_uring_fops)
4034 goto out_fput;
4035
4036 ret = -ENXIO;
4037 ctx = f.file->private_data;
4038 if (!percpu_ref_tryget(&ctx->refs))
4039 goto out_fput;
4040
Jens Axboe6c271ce2019-01-10 11:22:30 -07004041 /*
4042 * For SQ polling, the thread will do all submissions and completions.
4043 * Just return the requested submit count, and wake the thread if
4044 * we were asked to.
4045 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004046 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004047 if (ctx->flags & IORING_SETUP_SQPOLL) {
4048 if (flags & IORING_ENTER_SQ_WAKEUP)
4049 wake_up(&ctx->sqo_wait);
4050 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004051 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004052 to_submit = min(to_submit, ctx->sq_entries);
4053
4054 mutex_lock(&ctx->uring_lock);
Jens Axboefcb323c2019-10-24 12:39:47 -06004055 submitted = io_ring_submit(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004056 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004057 }
4058 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004059 unsigned nr_events = 0;
4060
Jens Axboe2b188cc2019-01-07 10:46:33 -07004061 min_complete = min(min_complete, ctx->cq_entries);
4062
Jens Axboedef596e2019-01-09 08:59:42 -07004063 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004064 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004065 } else {
4066 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4067 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004068 }
4069
Pavel Begunkov6805b322019-10-08 02:18:42 +03004070 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004071out_fput:
4072 fdput(f);
4073 return submitted ? submitted : ret;
4074}
4075
4076static const struct file_operations io_uring_fops = {
4077 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004078 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004079 .mmap = io_uring_mmap,
4080 .poll = io_uring_poll,
4081 .fasync = io_uring_fasync,
4082};
4083
4084static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4085 struct io_uring_params *p)
4086{
Hristo Venev75b28af2019-08-26 17:23:46 +00004087 struct io_rings *rings;
4088 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004089
Hristo Venev75b28af2019-08-26 17:23:46 +00004090 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4091 if (size == SIZE_MAX)
4092 return -EOVERFLOW;
4093
4094 rings = io_mem_alloc(size);
4095 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004096 return -ENOMEM;
4097
Hristo Venev75b28af2019-08-26 17:23:46 +00004098 ctx->rings = rings;
4099 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4100 rings->sq_ring_mask = p->sq_entries - 1;
4101 rings->cq_ring_mask = p->cq_entries - 1;
4102 rings->sq_ring_entries = p->sq_entries;
4103 rings->cq_ring_entries = p->cq_entries;
4104 ctx->sq_mask = rings->sq_ring_mask;
4105 ctx->cq_mask = rings->cq_ring_mask;
4106 ctx->sq_entries = rings->sq_ring_entries;
4107 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004108
4109 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4110 if (size == SIZE_MAX)
4111 return -EOVERFLOW;
4112
4113 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004114 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004115 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004116
Jens Axboe2b188cc2019-01-07 10:46:33 -07004117 return 0;
4118}
4119
4120/*
4121 * Allocate an anonymous fd, this is what constitutes the application
4122 * visible backing of an io_uring instance. The application mmaps this
4123 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4124 * we have to tie this fd to a socket for file garbage collection purposes.
4125 */
4126static int io_uring_get_fd(struct io_ring_ctx *ctx)
4127{
4128 struct file *file;
4129 int ret;
4130
4131#if defined(CONFIG_UNIX)
4132 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4133 &ctx->ring_sock);
4134 if (ret)
4135 return ret;
4136#endif
4137
4138 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4139 if (ret < 0)
4140 goto err;
4141
4142 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4143 O_RDWR | O_CLOEXEC);
4144 if (IS_ERR(file)) {
4145 put_unused_fd(ret);
4146 ret = PTR_ERR(file);
4147 goto err;
4148 }
4149
4150#if defined(CONFIG_UNIX)
4151 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004152 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004153#endif
4154 fd_install(ret, file);
4155 return ret;
4156err:
4157#if defined(CONFIG_UNIX)
4158 sock_release(ctx->ring_sock);
4159 ctx->ring_sock = NULL;
4160#endif
4161 return ret;
4162}
4163
4164static int io_uring_create(unsigned entries, struct io_uring_params *p)
4165{
4166 struct user_struct *user = NULL;
4167 struct io_ring_ctx *ctx;
4168 bool account_mem;
4169 int ret;
4170
4171 if (!entries || entries > IORING_MAX_ENTRIES)
4172 return -EINVAL;
4173
4174 /*
4175 * Use twice as many entries for the CQ ring. It's possible for the
4176 * application to drive a higher depth than the size of the SQ ring,
4177 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004178 * some flexibility in overcommitting a bit. If the application has
4179 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4180 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004181 */
4182 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004183 if (p->flags & IORING_SETUP_CQSIZE) {
4184 /*
4185 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4186 * to a power-of-two, if it isn't already. We do NOT impose
4187 * any cq vs sq ring sizing.
4188 */
4189 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4190 return -EINVAL;
4191 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4192 } else {
4193 p->cq_entries = 2 * p->sq_entries;
4194 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004195
4196 user = get_uid(current_user());
4197 account_mem = !capable(CAP_IPC_LOCK);
4198
4199 if (account_mem) {
4200 ret = io_account_mem(user,
4201 ring_pages(p->sq_entries, p->cq_entries));
4202 if (ret) {
4203 free_uid(user);
4204 return ret;
4205 }
4206 }
4207
4208 ctx = io_ring_ctx_alloc(p);
4209 if (!ctx) {
4210 if (account_mem)
4211 io_unaccount_mem(user, ring_pages(p->sq_entries,
4212 p->cq_entries));
4213 free_uid(user);
4214 return -ENOMEM;
4215 }
4216 ctx->compat = in_compat_syscall();
4217 ctx->account_mem = account_mem;
4218 ctx->user = user;
4219
4220 ret = io_allocate_scq_urings(ctx, p);
4221 if (ret)
4222 goto err;
4223
Jens Axboe6c271ce2019-01-10 11:22:30 -07004224 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004225 if (ret)
4226 goto err;
4227
Jens Axboe2b188cc2019-01-07 10:46:33 -07004228 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004229 p->sq_off.head = offsetof(struct io_rings, sq.head);
4230 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4231 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4232 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4233 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4234 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4235 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004236
4237 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004238 p->cq_off.head = offsetof(struct io_rings, cq.head);
4239 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4240 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4241 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4242 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4243 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004244
Jens Axboe044c1ab2019-10-28 09:15:33 -06004245 /*
4246 * Install ring fd as the very last thing, so we don't risk someone
4247 * having closed it before we finish setup
4248 */
4249 ret = io_uring_get_fd(ctx);
4250 if (ret < 0)
4251 goto err;
4252
Jens Axboeac90f242019-09-06 10:26:21 -06004253 p->features = IORING_FEAT_SINGLE_MMAP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004254 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004255 return ret;
4256err:
4257 io_ring_ctx_wait_and_kill(ctx);
4258 return ret;
4259}
4260
4261/*
4262 * Sets up an aio uring context, and returns the fd. Applications asks for a
4263 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4264 * params structure passed in.
4265 */
4266static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4267{
4268 struct io_uring_params p;
4269 long ret;
4270 int i;
4271
4272 if (copy_from_user(&p, params, sizeof(p)))
4273 return -EFAULT;
4274 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4275 if (p.resv[i])
4276 return -EINVAL;
4277 }
4278
Jens Axboe6c271ce2019-01-10 11:22:30 -07004279 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004280 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004281 return -EINVAL;
4282
4283 ret = io_uring_create(entries, &p);
4284 if (ret < 0)
4285 return ret;
4286
4287 if (copy_to_user(params, &p, sizeof(p)))
4288 return -EFAULT;
4289
4290 return ret;
4291}
4292
4293SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4294 struct io_uring_params __user *, params)
4295{
4296 return io_uring_setup(entries, params);
4297}
4298
Jens Axboeedafcce2019-01-09 09:16:05 -07004299static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4300 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004301 __releases(ctx->uring_lock)
4302 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004303{
4304 int ret;
4305
Jens Axboe35fa71a2019-04-22 10:23:23 -06004306 /*
4307 * We're inside the ring mutex, if the ref is already dying, then
4308 * someone else killed the ctx or is already going through
4309 * io_uring_register().
4310 */
4311 if (percpu_ref_is_dying(&ctx->refs))
4312 return -ENXIO;
4313
Jens Axboeedafcce2019-01-09 09:16:05 -07004314 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004315
4316 /*
4317 * Drop uring mutex before waiting for references to exit. If another
4318 * thread is currently inside io_uring_enter() it might need to grab
4319 * the uring_lock to make progress. If we hold it here across the drain
4320 * wait, then we can deadlock. It's safe to drop the mutex here, since
4321 * no new references will come in after we've killed the percpu ref.
4322 */
4323 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004324 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004325 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004326
4327 switch (opcode) {
4328 case IORING_REGISTER_BUFFERS:
4329 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4330 break;
4331 case IORING_UNREGISTER_BUFFERS:
4332 ret = -EINVAL;
4333 if (arg || nr_args)
4334 break;
4335 ret = io_sqe_buffer_unregister(ctx);
4336 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004337 case IORING_REGISTER_FILES:
4338 ret = io_sqe_files_register(ctx, arg, nr_args);
4339 break;
4340 case IORING_UNREGISTER_FILES:
4341 ret = -EINVAL;
4342 if (arg || nr_args)
4343 break;
4344 ret = io_sqe_files_unregister(ctx);
4345 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004346 case IORING_REGISTER_FILES_UPDATE:
4347 ret = io_sqe_files_update(ctx, arg, nr_args);
4348 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004349 case IORING_REGISTER_EVENTFD:
4350 ret = -EINVAL;
4351 if (nr_args != 1)
4352 break;
4353 ret = io_eventfd_register(ctx, arg);
4354 break;
4355 case IORING_UNREGISTER_EVENTFD:
4356 ret = -EINVAL;
4357 if (arg || nr_args)
4358 break;
4359 ret = io_eventfd_unregister(ctx);
4360 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004361 default:
4362 ret = -EINVAL;
4363 break;
4364 }
4365
4366 /* bring the ctx back to life */
4367 reinit_completion(&ctx->ctx_done);
4368 percpu_ref_reinit(&ctx->refs);
4369 return ret;
4370}
4371
4372SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4373 void __user *, arg, unsigned int, nr_args)
4374{
4375 struct io_ring_ctx *ctx;
4376 long ret = -EBADF;
4377 struct fd f;
4378
4379 f = fdget(fd);
4380 if (!f.file)
4381 return -EBADF;
4382
4383 ret = -EOPNOTSUPP;
4384 if (f.file->f_op != &io_uring_fops)
4385 goto out_fput;
4386
4387 ctx = f.file->private_data;
4388
4389 mutex_lock(&ctx->uring_lock);
4390 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4391 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004392 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4393 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004394out_fput:
4395 fdput(f);
4396 return ret;
4397}
4398
Jens Axboe2b188cc2019-01-07 10:46:33 -07004399static int __init io_uring_init(void)
4400{
4401 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4402 return 0;
4403};
4404__initcall(io_uring_init);