blob: d8e15cce936e636833e64115261922c5db2078f7 [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 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700332#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800333#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 Axboe2665abf2019-11-05 12:40:47 -0700375static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr);
Jens Axboede0617e2019-04-06 21:51:27 -0600376
Jens Axboe2b188cc2019-01-07 10:46:33 -0700377static struct kmem_cache *req_cachep;
378
379static const struct file_operations io_uring_fops;
380
381struct sock *io_uring_get_socket(struct file *file)
382{
383#if defined(CONFIG_UNIX)
384 if (file->f_op == &io_uring_fops) {
385 struct io_ring_ctx *ctx = file->private_data;
386
387 return ctx->ring_sock->sk;
388 }
389#endif
390 return NULL;
391}
392EXPORT_SYMBOL(io_uring_get_socket);
393
394static void io_ring_ctx_ref_free(struct percpu_ref *ref)
395{
396 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
397
398 complete(&ctx->ctx_done);
399}
400
401static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
402{
403 struct io_ring_ctx *ctx;
404
405 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
406 if (!ctx)
407 return NULL;
408
Roman Gushchin21482892019-05-07 10:01:48 -0700409 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
410 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700411 kfree(ctx);
412 return NULL;
413 }
414
415 ctx->flags = p->flags;
416 init_waitqueue_head(&ctx->cq_wait);
417 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800418 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700419 mutex_init(&ctx->uring_lock);
420 init_waitqueue_head(&ctx->wait);
421 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700422 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700423 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600424 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600425 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600426 init_waitqueue_head(&ctx->inflight_wait);
427 spin_lock_init(&ctx->inflight_lock);
428 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700429 return ctx;
430}
431
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600432static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
433 struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600434{
Jens Axboe498ccd92019-10-25 10:04:25 -0600435 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
436 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600437}
438
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600439static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
440 struct io_kiocb *req)
441{
442 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
443 return false;
444
445 return __io_sequence_defer(ctx, req);
446}
447
448static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600449{
450 struct io_kiocb *req;
451
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600452 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
453 if (req && !io_sequence_defer(ctx, req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600454 list_del_init(&req->list);
455 return req;
456 }
457
458 return NULL;
459}
460
Jens Axboe5262f562019-09-17 12:26:57 -0600461static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
462{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600463 struct io_kiocb *req;
464
465 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
466 if (req && !__io_sequence_defer(ctx, req)) {
467 list_del_init(&req->list);
468 return req;
469 }
470
471 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600472}
473
Jens Axboede0617e2019-04-06 21:51:27 -0600474static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700475{
Hristo Venev75b28af2019-08-26 17:23:46 +0000476 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700477
Hristo Venev75b28af2019-08-26 17:23:46 +0000478 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700479 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000480 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700481
Jens Axboe2b188cc2019-01-07 10:46:33 -0700482 if (wq_has_sleeper(&ctx->cq_wait)) {
483 wake_up_interruptible(&ctx->cq_wait);
484 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
485 }
486 }
487}
488
Jens Axboe561fb042019-10-24 07:25:42 -0600489static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600490{
Jens Axboe561fb042019-10-24 07:25:42 -0600491 u8 opcode = READ_ONCE(sqe->opcode);
492
493 return !(opcode == IORING_OP_READ_FIXED ||
494 opcode == IORING_OP_WRITE_FIXED);
495}
496
497static inline bool io_prep_async_work(struct io_kiocb *req)
498{
499 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600500
Jens Axboe6cc47d12019-09-18 11:18:23 -0600501 if (req->submit.sqe) {
502 switch (req->submit.sqe->opcode) {
503 case IORING_OP_WRITEV:
504 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600505 do_hashed = true;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600506 break;
507 }
Jens Axboe561fb042019-10-24 07:25:42 -0600508 if (io_sqe_needs_user(req->submit.sqe))
509 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600510 }
511
Jens Axboe561fb042019-10-24 07:25:42 -0600512 return do_hashed;
513}
514
515static inline void io_queue_async_work(struct io_ring_ctx *ctx,
516 struct io_kiocb *req)
517{
518 bool do_hashed = io_prep_async_work(req);
519
520 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
521 req->flags);
522 if (!do_hashed) {
523 io_wq_enqueue(ctx->io_wq, &req->work);
524 } else {
525 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
526 file_inode(req->file));
527 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600528}
529
Jens Axboe5262f562019-09-17 12:26:57 -0600530static void io_kill_timeout(struct io_kiocb *req)
531{
532 int ret;
533
534 ret = hrtimer_try_to_cancel(&req->timeout.timer);
535 if (ret != -1) {
536 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600537 list_del_init(&req->list);
Jens Axboe5262f562019-09-17 12:26:57 -0600538 io_cqring_fill_event(req->ctx, req->user_data, 0);
539 __io_free_req(req);
540 }
541}
542
543static void io_kill_timeouts(struct io_ring_ctx *ctx)
544{
545 struct io_kiocb *req, *tmp;
546
547 spin_lock_irq(&ctx->completion_lock);
548 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
549 io_kill_timeout(req);
550 spin_unlock_irq(&ctx->completion_lock);
551}
552
Jens Axboede0617e2019-04-06 21:51:27 -0600553static void io_commit_cqring(struct io_ring_ctx *ctx)
554{
555 struct io_kiocb *req;
556
Jens Axboe5262f562019-09-17 12:26:57 -0600557 while ((req = io_get_timeout_req(ctx)) != NULL)
558 io_kill_timeout(req);
559
Jens Axboede0617e2019-04-06 21:51:27 -0600560 __io_commit_cqring(ctx);
561
562 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800563 if (req->flags & REQ_F_SHADOW_DRAIN) {
564 /* Just for drain, free it. */
565 __io_free_req(req);
566 continue;
567 }
Jens Axboede0617e2019-04-06 21:51:27 -0600568 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600569 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600570 }
571}
572
Jens Axboe2b188cc2019-01-07 10:46:33 -0700573static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
574{
Hristo Venev75b28af2019-08-26 17:23:46 +0000575 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700576 unsigned tail;
577
578 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200579 /*
580 * writes to the cq entry need to come after reading head; the
581 * control dependency is enough as we're using WRITE_ONCE to
582 * fill the cq entry
583 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000584 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700585 return NULL;
586
587 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000588 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700589}
590
591static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600592 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700593{
594 struct io_uring_cqe *cqe;
595
Jens Axboe51c3ff62019-11-03 06:52:50 -0700596 trace_io_uring_complete(ctx, ki_user_data, res);
597
Jens Axboe2b188cc2019-01-07 10:46:33 -0700598 /*
599 * If we can't get a cq entry, userspace overflowed the
600 * submission (by quite a lot). Increment the overflow count in
601 * the ring.
602 */
603 cqe = io_get_cqring(ctx);
604 if (cqe) {
605 WRITE_ONCE(cqe->user_data, ki_user_data);
606 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600607 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608 } else {
Jens Axboe498ccd92019-10-25 10:04:25 -0600609 WRITE_ONCE(ctx->rings->cq_overflow,
610 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe2b188cc2019-01-07 10:46:33 -0700611 }
612}
613
Jens Axboe8c838782019-03-12 15:48:16 -0600614static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
615{
616 if (waitqueue_active(&ctx->wait))
617 wake_up(&ctx->wait);
618 if (waitqueue_active(&ctx->sqo_wait))
619 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600620 if (ctx->cq_ev_fd)
621 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600622}
623
624static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600625 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700626{
627 unsigned long flags;
628
629 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600630 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700631 io_commit_cqring(ctx);
632 spin_unlock_irqrestore(&ctx->completion_lock, flags);
633
Jens Axboe8c838782019-03-12 15:48:16 -0600634 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700635}
636
Jens Axboe2579f912019-01-09 09:10:43 -0700637static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
638 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700639{
Jens Axboefd6fab22019-03-14 16:30:06 -0600640 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700641 struct io_kiocb *req;
642
643 if (!percpu_ref_tryget(&ctx->refs))
644 return NULL;
645
Jens Axboe2579f912019-01-09 09:10:43 -0700646 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600647 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700648 if (unlikely(!req))
649 goto out;
650 } else if (!state->free_reqs) {
651 size_t sz;
652 int ret;
653
654 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600655 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
656
657 /*
658 * Bulk alloc is all-or-nothing. If we fail to get a batch,
659 * retry single alloc to be on the safe side.
660 */
661 if (unlikely(ret <= 0)) {
662 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
663 if (!state->reqs[0])
664 goto out;
665 ret = 1;
666 }
Jens Axboe2579f912019-01-09 09:10:43 -0700667 state->free_reqs = ret - 1;
668 state->cur_req = 1;
669 req = state->reqs[0];
670 } else {
671 req = state->reqs[state->cur_req];
672 state->free_reqs--;
673 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700674 }
675
Jens Axboe60c112b2019-06-21 10:20:18 -0600676 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700677 req->ctx = ctx;
678 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600679 /* one is dropped after submission, the other at completion */
680 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600681 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600682 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700683 return req;
684out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300685 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700686 return NULL;
687}
688
Jens Axboedef596e2019-01-09 08:59:42 -0700689static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
690{
691 if (*nr) {
692 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300693 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700694 *nr = 0;
695 }
696}
697
Jens Axboe9e645e112019-05-10 16:07:28 -0600698static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700699{
Jens Axboefcb323c2019-10-24 12:39:47 -0600700 struct io_ring_ctx *ctx = req->ctx;
701
Jens Axboe09bb8392019-03-13 12:39:28 -0600702 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
703 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600704 if (req->flags & REQ_F_INFLIGHT) {
705 unsigned long flags;
706
707 spin_lock_irqsave(&ctx->inflight_lock, flags);
708 list_del(&req->inflight_entry);
709 if (waitqueue_active(&ctx->inflight_wait))
710 wake_up(&ctx->inflight_wait);
711 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
712 }
713 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600714 kmem_cache_free(req_cachep, req);
715}
716
Jens Axboe2665abf2019-11-05 12:40:47 -0700717static bool io_link_cancel_timeout(struct io_ring_ctx *ctx,
718 struct io_kiocb *req)
719{
720 int ret;
721
722 ret = hrtimer_try_to_cancel(&req->timeout.timer);
723 if (ret != -1) {
724 io_cqring_fill_event(ctx, req->user_data, -ECANCELED);
725 io_commit_cqring(ctx);
726 req->flags &= ~REQ_F_LINK;
727 __io_free_req(req);
728 return true;
729 }
730
731 return false;
732}
733
Jens Axboeba816ad2019-09-28 11:36:45 -0600734static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600735{
Jens Axboe2665abf2019-11-05 12:40:47 -0700736 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600737 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700738 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600739
740 /*
741 * The list should never be empty when we are called here. But could
742 * potentially happen if the chain is messed up, check to be on the
743 * safe side.
744 */
745 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700746 while (nxt) {
Jens Axboe9e645e112019-05-10 16:07:28 -0600747 list_del(&nxt->list);
748 if (!list_empty(&req->link_list)) {
749 INIT_LIST_HEAD(&nxt->link_list);
750 list_splice(&req->link_list, &nxt->link_list);
751 nxt->flags |= REQ_F_LINK;
752 }
753
Jens Axboeba816ad2019-09-28 11:36:45 -0600754 /*
755 * If we're in async work, we can continue processing the chain
756 * in this context instead of having to queue up new async work.
757 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700758 if (req->flags & REQ_F_LINK_TIMEOUT) {
759 wake_ev = io_link_cancel_timeout(ctx, nxt);
760
761 /* we dropped this link, get next */
762 nxt = list_first_entry_or_null(&req->link_list,
763 struct io_kiocb, list);
764 } else if (nxtptr && current_work()) {
Jens Axboeba816ad2019-09-28 11:36:45 -0600765 *nxtptr = nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700766 break;
767 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -0600768 io_queue_async_work(req->ctx, nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700769 break;
770 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600771 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700772
773 if (wake_ev)
774 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600775}
776
777/*
778 * Called if REQ_F_LINK is set, and we fail the head request
779 */
780static void io_fail_links(struct io_kiocb *req)
781{
Jens Axboe2665abf2019-11-05 12:40:47 -0700782 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600783 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700784 unsigned long flags;
785
786 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600787
788 while (!list_empty(&req->link_list)) {
789 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700790 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600791
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200792 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700793
794 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
795 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
796 io_link_cancel_timeout(ctx, link);
797 } else {
798 io_cqring_fill_event(ctx, link->user_data, -ECANCELED);
799 __io_free_req(link);
800 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600801 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700802
803 io_commit_cqring(ctx);
804 spin_unlock_irqrestore(&ctx->completion_lock, flags);
805 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600806}
807
Jens Axboeba816ad2019-09-28 11:36:45 -0600808static void io_free_req(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600809{
Jens Axboe2665abf2019-11-05 12:40:47 -0700810 if (likely(!(req->flags & REQ_F_LINK))) {
811 __io_free_req(req);
812 return;
813 }
814
Jens Axboe9e645e112019-05-10 16:07:28 -0600815 /*
816 * If LINK is set, we have dependent requests in this chain. If we
817 * didn't fail this request, queue the first one up, moving any other
818 * dependencies to the next request. In case of failure, fail the rest
819 * of the chain.
820 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700821 if (req->flags & REQ_F_FAIL_LINK) {
822 io_fail_links(req);
823 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
824 struct io_ring_ctx *ctx = req->ctx;
825 unsigned long flags;
826
827 /*
828 * If this is a timeout link, we could be racing with the
829 * timeout timer. Grab the completion lock for this case to
830 * protection against that.
831 */
832 spin_lock_irqsave(&ctx->completion_lock, flags);
833 io_req_link_next(req, nxt);
834 spin_unlock_irqrestore(&ctx->completion_lock, flags);
835 } else {
836 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600837 }
838
839 __io_free_req(req);
840}
841
Jens Axboeba816ad2019-09-28 11:36:45 -0600842/*
843 * Drop reference to request, return next in chain (if there is one) if this
844 * was the last reference to this request.
845 */
846static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -0600847{
Jens Axboeba816ad2019-09-28 11:36:45 -0600848 struct io_kiocb *nxt = NULL;
849
Jens Axboee65ef562019-03-12 10:16:44 -0600850 if (refcount_dec_and_test(&req->refs))
Jens Axboeba816ad2019-09-28 11:36:45 -0600851 io_free_req(req, &nxt);
852
853 return nxt;
854}
855
856static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr)
857{
858 struct io_kiocb *nxt;
859
860 nxt = io_put_req_find_next(req);
861 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600862 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600863 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600864 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600865 io_queue_async_work(nxt->ctx, nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600866 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700867}
868
Jens Axboe84f97dc2019-11-06 11:27:53 -0700869static unsigned io_cqring_events(struct io_ring_ctx *ctx)
Jens Axboea3a0e432019-08-20 11:03:11 -0600870{
Jens Axboe84f97dc2019-11-06 11:27:53 -0700871 struct io_rings *rings = ctx->rings;
872
Jens Axboea3a0e432019-08-20 11:03:11 -0600873 /* See comment at the top of this file */
874 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000875 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600876}
877
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300878static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
879{
880 struct io_rings *rings = ctx->rings;
881
882 /* make sure SQ entry isn't read before tail */
883 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
884}
885
Jens Axboedef596e2019-01-09 08:59:42 -0700886/*
887 * Find and free completed poll iocbs
888 */
889static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
890 struct list_head *done)
891{
892 void *reqs[IO_IOPOLL_BATCH];
893 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600894 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700895
Jens Axboe09bb8392019-03-13 12:39:28 -0600896 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700897 while (!list_empty(done)) {
898 req = list_first_entry(done, struct io_kiocb, list);
899 list_del(&req->list);
900
Jens Axboe9e645e112019-05-10 16:07:28 -0600901 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700902 (*nr_events)++;
903
Jens Axboe09bb8392019-03-13 12:39:28 -0600904 if (refcount_dec_and_test(&req->refs)) {
905 /* If we're not using fixed files, we have to pair the
906 * completion part with the file put. Use regular
907 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600908 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600909 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600910 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
911 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600912 reqs[to_free++] = req;
913 if (to_free == ARRAY_SIZE(reqs))
914 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700915 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -0600916 io_free_req(req, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -0700917 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700918 }
Jens Axboedef596e2019-01-09 08:59:42 -0700919 }
Jens Axboedef596e2019-01-09 08:59:42 -0700920
Jens Axboe09bb8392019-03-13 12:39:28 -0600921 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700922 io_free_req_many(ctx, reqs, &to_free);
923}
924
925static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
926 long min)
927{
928 struct io_kiocb *req, *tmp;
929 LIST_HEAD(done);
930 bool spin;
931 int ret;
932
933 /*
934 * Only spin for completions if we don't have multiple devices hanging
935 * off our complete list, and we're under the requested amount.
936 */
937 spin = !ctx->poll_multi_file && *nr_events < min;
938
939 ret = 0;
940 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
941 struct kiocb *kiocb = &req->rw;
942
943 /*
944 * Move completed entries to our local list. If we find a
945 * request that requires polling, break out and complete
946 * the done list first, if we have entries there.
947 */
948 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
949 list_move_tail(&req->list, &done);
950 continue;
951 }
952 if (!list_empty(&done))
953 break;
954
955 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
956 if (ret < 0)
957 break;
958
959 if (ret && spin)
960 spin = false;
961 ret = 0;
962 }
963
964 if (!list_empty(&done))
965 io_iopoll_complete(ctx, nr_events, &done);
966
967 return ret;
968}
969
970/*
971 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
972 * non-spinning poll check - we'll still enter the driver poll loop, but only
973 * as a non-spinning completion check.
974 */
975static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
976 long min)
977{
Jens Axboe08f54392019-08-21 22:19:11 -0600978 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700979 int ret;
980
981 ret = io_do_iopoll(ctx, nr_events, min);
982 if (ret < 0)
983 return ret;
984 if (!min || *nr_events >= min)
985 return 0;
986 }
987
988 return 1;
989}
990
991/*
992 * We can't just wait for polled events to come to us, we have to actively
993 * find and complete them.
994 */
995static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
996{
997 if (!(ctx->flags & IORING_SETUP_IOPOLL))
998 return;
999
1000 mutex_lock(&ctx->uring_lock);
1001 while (!list_empty(&ctx->poll_list)) {
1002 unsigned int nr_events = 0;
1003
1004 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001005
1006 /*
1007 * Ensure we allow local-to-the-cpu processing to take place,
1008 * in this case we need to ensure that we reap all events.
1009 */
1010 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001011 }
1012 mutex_unlock(&ctx->uring_lock);
1013}
1014
Jens Axboe2b2ed972019-10-25 10:06:15 -06001015static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1016 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001017{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001018 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001019
1020 do {
1021 int tmin = 0;
1022
Jens Axboe500f9fb2019-08-19 12:15:59 -06001023 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001024 * Don't enter poll loop if we already have events pending.
1025 * If we do, we can potentially be spinning for commands that
1026 * already triggered a CQE (eg in error).
1027 */
Jens Axboe84f97dc2019-11-06 11:27:53 -07001028 if (io_cqring_events(ctx))
Jens Axboea3a0e432019-08-20 11:03:11 -06001029 break;
1030
1031 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001032 * If a submit got punted to a workqueue, we can have the
1033 * application entering polling for a command before it gets
1034 * issued. That app will hold the uring_lock for the duration
1035 * of the poll right here, so we need to take a breather every
1036 * now and then to ensure that the issue has a chance to add
1037 * the poll to the issued list. Otherwise we can spin here
1038 * forever, while the workqueue is stuck trying to acquire the
1039 * very same mutex.
1040 */
1041 if (!(++iters & 7)) {
1042 mutex_unlock(&ctx->uring_lock);
1043 mutex_lock(&ctx->uring_lock);
1044 }
1045
Jens Axboedef596e2019-01-09 08:59:42 -07001046 if (*nr_events < min)
1047 tmin = min - *nr_events;
1048
1049 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1050 if (ret <= 0)
1051 break;
1052 ret = 0;
1053 } while (min && !*nr_events && !need_resched());
1054
Jens Axboe2b2ed972019-10-25 10:06:15 -06001055 return ret;
1056}
1057
1058static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1059 long min)
1060{
1061 int ret;
1062
1063 /*
1064 * We disallow the app entering submit/complete with polling, but we
1065 * still need to lock the ring to prevent racing with polled issue
1066 * that got punted to a workqueue.
1067 */
1068 mutex_lock(&ctx->uring_lock);
1069 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001070 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001071 return ret;
1072}
1073
Jens Axboe491381ce2019-10-17 09:20:46 -06001074static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001075{
Jens Axboe491381ce2019-10-17 09:20:46 -06001076 /*
1077 * Tell lockdep we inherited freeze protection from submission
1078 * thread.
1079 */
1080 if (req->flags & REQ_F_ISREG) {
1081 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001082
Jens Axboe491381ce2019-10-17 09:20:46 -06001083 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001085 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001086}
1087
Jens Axboeba816ad2019-09-28 11:36:45 -06001088static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001089{
1090 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1091
Jens Axboe491381ce2019-10-17 09:20:46 -06001092 if (kiocb->ki_flags & IOCB_WRITE)
1093 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001094
Jens Axboe9e645e112019-05-10 16:07:28 -06001095 if ((req->flags & REQ_F_LINK) && res != req->result)
1096 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001097 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001098}
1099
1100static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1101{
1102 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1103
1104 io_complete_rw_common(kiocb, res);
1105 io_put_req(req, NULL);
1106}
1107
1108static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1109{
1110 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1111
1112 io_complete_rw_common(kiocb, res);
1113 return io_put_req_find_next(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001114}
1115
Jens Axboedef596e2019-01-09 08:59:42 -07001116static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1117{
1118 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1119
Jens Axboe491381ce2019-10-17 09:20:46 -06001120 if (kiocb->ki_flags & IOCB_WRITE)
1121 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001122
Jens Axboe9e645e112019-05-10 16:07:28 -06001123 if ((req->flags & REQ_F_LINK) && res != req->result)
1124 req->flags |= REQ_F_FAIL_LINK;
1125 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001126 if (res != -EAGAIN)
1127 req->flags |= REQ_F_IOPOLL_COMPLETED;
1128}
1129
1130/*
1131 * After the iocb has been issued, it's safe to be found on the poll list.
1132 * Adding the kiocb to the list AFTER submission ensures that we don't
1133 * find it from a io_iopoll_getevents() thread before the issuer is done
1134 * accessing the kiocb cookie.
1135 */
1136static void io_iopoll_req_issued(struct io_kiocb *req)
1137{
1138 struct io_ring_ctx *ctx = req->ctx;
1139
1140 /*
1141 * Track whether we have multiple files in our lists. This will impact
1142 * how we do polling eventually, not spinning if we're on potentially
1143 * different devices.
1144 */
1145 if (list_empty(&ctx->poll_list)) {
1146 ctx->poll_multi_file = false;
1147 } else if (!ctx->poll_multi_file) {
1148 struct io_kiocb *list_req;
1149
1150 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1151 list);
1152 if (list_req->rw.ki_filp != req->rw.ki_filp)
1153 ctx->poll_multi_file = true;
1154 }
1155
1156 /*
1157 * For fast devices, IO may have already completed. If it has, add
1158 * it to the front so we find it first.
1159 */
1160 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1161 list_add(&req->list, &ctx->poll_list);
1162 else
1163 list_add_tail(&req->list, &ctx->poll_list);
1164}
1165
Jens Axboe3d6770f2019-04-13 11:50:54 -06001166static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001167{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001168 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001169 int diff = state->has_refs - state->used_refs;
1170
1171 if (diff)
1172 fput_many(state->file, diff);
1173 state->file = NULL;
1174 }
1175}
1176
1177/*
1178 * Get as many references to a file as we have IOs left in this submission,
1179 * assuming most submissions are for one file, or at least that each file
1180 * has more than one submission.
1181 */
1182static struct file *io_file_get(struct io_submit_state *state, int fd)
1183{
1184 if (!state)
1185 return fget(fd);
1186
1187 if (state->file) {
1188 if (state->fd == fd) {
1189 state->used_refs++;
1190 state->ios_left--;
1191 return state->file;
1192 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001193 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001194 }
1195 state->file = fget_many(fd, state->ios_left);
1196 if (!state->file)
1197 return NULL;
1198
1199 state->fd = fd;
1200 state->has_refs = state->ios_left;
1201 state->used_refs = 1;
1202 state->ios_left--;
1203 return state->file;
1204}
1205
Jens Axboe2b188cc2019-01-07 10:46:33 -07001206/*
1207 * If we tracked the file through the SCM inflight mechanism, we could support
1208 * any file. For now, just ensure that anything potentially problematic is done
1209 * inline.
1210 */
1211static bool io_file_supports_async(struct file *file)
1212{
1213 umode_t mode = file_inode(file)->i_mode;
1214
1215 if (S_ISBLK(mode) || S_ISCHR(mode))
1216 return true;
1217 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1218 return true;
1219
1220 return false;
1221}
1222
Pavel Begunkov267bc902019-11-07 01:41:08 +03001223static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001224{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001225 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001226 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001227 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001228 unsigned ioprio;
1229 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001230
Jens Axboe09bb8392019-03-13 12:39:28 -06001231 if (!req->file)
1232 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001233
Jens Axboe491381ce2019-10-17 09:20:46 -06001234 if (S_ISREG(file_inode(req->file)->i_mode))
1235 req->flags |= REQ_F_ISREG;
1236
1237 /*
1238 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1239 * we know to async punt it even if it was opened O_NONBLOCK
1240 */
1241 if (force_nonblock && !io_file_supports_async(req->file)) {
1242 req->flags |= REQ_F_MUST_PUNT;
1243 return -EAGAIN;
1244 }
Jens Axboe6b063142019-01-10 22:13:58 -07001245
Jens Axboe2b188cc2019-01-07 10:46:33 -07001246 kiocb->ki_pos = READ_ONCE(sqe->off);
1247 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1248 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1249
1250 ioprio = READ_ONCE(sqe->ioprio);
1251 if (ioprio) {
1252 ret = ioprio_check_cap(ioprio);
1253 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001254 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001255
1256 kiocb->ki_ioprio = ioprio;
1257 } else
1258 kiocb->ki_ioprio = get_current_ioprio();
1259
1260 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1261 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001262 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001263
1264 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001265 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1266 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001267 req->flags |= REQ_F_NOWAIT;
1268
1269 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001271
Jens Axboedef596e2019-01-09 08:59:42 -07001272 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001273 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1274 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001275 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001276
Jens Axboedef596e2019-01-09 08:59:42 -07001277 kiocb->ki_flags |= IOCB_HIPRI;
1278 kiocb->ki_complete = io_complete_rw_iopoll;
1279 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001280 if (kiocb->ki_flags & IOCB_HIPRI)
1281 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001282 kiocb->ki_complete = io_complete_rw;
1283 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001284 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001285}
1286
1287static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1288{
1289 switch (ret) {
1290 case -EIOCBQUEUED:
1291 break;
1292 case -ERESTARTSYS:
1293 case -ERESTARTNOINTR:
1294 case -ERESTARTNOHAND:
1295 case -ERESTART_RESTARTBLOCK:
1296 /*
1297 * We can't just restart the syscall, since previously
1298 * submitted sqes may already be in progress. Just fail this
1299 * IO with EINTR.
1300 */
1301 ret = -EINTR;
1302 /* fall through */
1303 default:
1304 kiocb->ki_complete(kiocb, ret, 0);
1305 }
1306}
1307
Jens Axboeba816ad2019-09-28 11:36:45 -06001308static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1309 bool in_async)
1310{
1311 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1312 *nxt = __io_complete_rw(kiocb, ret);
1313 else
1314 io_rw_done(kiocb, ret);
1315}
1316
Jens Axboeedafcce2019-01-09 09:16:05 -07001317static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1318 const struct io_uring_sqe *sqe,
1319 struct iov_iter *iter)
1320{
1321 size_t len = READ_ONCE(sqe->len);
1322 struct io_mapped_ubuf *imu;
1323 unsigned index, buf_index;
1324 size_t offset;
1325 u64 buf_addr;
1326
1327 /* attempt to use fixed buffers without having provided iovecs */
1328 if (unlikely(!ctx->user_bufs))
1329 return -EFAULT;
1330
1331 buf_index = READ_ONCE(sqe->buf_index);
1332 if (unlikely(buf_index >= ctx->nr_user_bufs))
1333 return -EFAULT;
1334
1335 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1336 imu = &ctx->user_bufs[index];
1337 buf_addr = READ_ONCE(sqe->addr);
1338
1339 /* overflow */
1340 if (buf_addr + len < buf_addr)
1341 return -EFAULT;
1342 /* not inside the mapped region */
1343 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1344 return -EFAULT;
1345
1346 /*
1347 * May not be a start of buffer, set size appropriately
1348 * and advance us to the beginning.
1349 */
1350 offset = buf_addr - imu->ubuf;
1351 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001352
1353 if (offset) {
1354 /*
1355 * Don't use iov_iter_advance() here, as it's really slow for
1356 * using the latter parts of a big fixed buffer - it iterates
1357 * over each segment manually. We can cheat a bit here, because
1358 * we know that:
1359 *
1360 * 1) it's a BVEC iter, we set it up
1361 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1362 * first and last bvec
1363 *
1364 * So just find our index, and adjust the iterator afterwards.
1365 * If the offset is within the first bvec (or the whole first
1366 * bvec, just use iov_iter_advance(). This makes it easier
1367 * since we can just skip the first segment, which may not
1368 * be PAGE_SIZE aligned.
1369 */
1370 const struct bio_vec *bvec = imu->bvec;
1371
1372 if (offset <= bvec->bv_len) {
1373 iov_iter_advance(iter, offset);
1374 } else {
1375 unsigned long seg_skip;
1376
1377 /* skip first vec */
1378 offset -= bvec->bv_len;
1379 seg_skip = 1 + (offset >> PAGE_SHIFT);
1380
1381 iter->bvec = bvec + seg_skip;
1382 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001383 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001384 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001385 }
1386 }
1387
Jens Axboeedafcce2019-01-09 09:16:05 -07001388 return 0;
1389}
1390
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001391static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1392 const struct sqe_submit *s, struct iovec **iovec,
1393 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001394{
1395 const struct io_uring_sqe *sqe = s->sqe;
1396 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1397 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001398 u8 opcode;
1399
1400 /*
1401 * We're reading ->opcode for the second time, but the first read
1402 * doesn't care whether it's _FIXED or not, so it doesn't matter
1403 * whether ->opcode changes concurrently. The first read does care
1404 * about whether it is a READ or a WRITE, so we don't trust this read
1405 * for that purpose and instead let the caller pass in the read/write
1406 * flag.
1407 */
1408 opcode = READ_ONCE(sqe->opcode);
1409 if (opcode == IORING_OP_READ_FIXED ||
1410 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001411 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001412 *iovec = NULL;
1413 return ret;
1414 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001415
1416 if (!s->has_user)
1417 return -EFAULT;
1418
1419#ifdef CONFIG_COMPAT
1420 if (ctx->compat)
1421 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1422 iovec, iter);
1423#endif
1424
1425 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1426}
1427
Jens Axboe32960612019-09-23 11:05:34 -06001428/*
1429 * For files that don't have ->read_iter() and ->write_iter(), handle them
1430 * by looping over ->read() or ->write() manually.
1431 */
1432static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1433 struct iov_iter *iter)
1434{
1435 ssize_t ret = 0;
1436
1437 /*
1438 * Don't support polled IO through this interface, and we can't
1439 * support non-blocking either. For the latter, this just causes
1440 * the kiocb to be handled from an async context.
1441 */
1442 if (kiocb->ki_flags & IOCB_HIPRI)
1443 return -EOPNOTSUPP;
1444 if (kiocb->ki_flags & IOCB_NOWAIT)
1445 return -EAGAIN;
1446
1447 while (iov_iter_count(iter)) {
1448 struct iovec iovec = iov_iter_iovec(iter);
1449 ssize_t nr;
1450
1451 if (rw == READ) {
1452 nr = file->f_op->read(file, iovec.iov_base,
1453 iovec.iov_len, &kiocb->ki_pos);
1454 } else {
1455 nr = file->f_op->write(file, iovec.iov_base,
1456 iovec.iov_len, &kiocb->ki_pos);
1457 }
1458
1459 if (nr < 0) {
1460 if (!ret)
1461 ret = nr;
1462 break;
1463 }
1464 ret += nr;
1465 if (nr != iovec.iov_len)
1466 break;
1467 iov_iter_advance(iter, nr);
1468 }
1469
1470 return ret;
1471}
1472
Pavel Begunkov267bc902019-11-07 01:41:08 +03001473static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1474 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001475{
1476 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1477 struct kiocb *kiocb = &req->rw;
1478 struct iov_iter iter;
1479 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001480 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001481 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001482
Pavel Begunkov267bc902019-11-07 01:41:08 +03001483 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001484 if (ret)
1485 return ret;
1486 file = kiocb->ki_filp;
1487
Jens Axboe2b188cc2019-01-07 10:46:33 -07001488 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001489 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001490
Pavel Begunkov267bc902019-11-07 01:41:08 +03001491 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001492 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001493 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001494
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001495 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001496 if (req->flags & REQ_F_LINK)
1497 req->result = read_size;
1498
Jens Axboe31b51512019-01-18 22:56:34 -07001499 iov_count = iov_iter_count(&iter);
1500 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001501 if (!ret) {
1502 ssize_t ret2;
1503
Jens Axboe32960612019-09-23 11:05:34 -06001504 if (file->f_op->read_iter)
1505 ret2 = call_read_iter(file, kiocb, &iter);
1506 else
1507 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1508
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001509 /*
1510 * In case of a short read, punt to async. This can happen
1511 * if we have data partially cached. Alternatively we can
1512 * return the short read, in which case the application will
1513 * need to issue another SQE and wait for it. That SQE will
1514 * need async punt anyway, so it's more efficient to do it
1515 * here.
1516 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001517 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1518 (req->flags & REQ_F_ISREG) &&
1519 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001520 ret2 = -EAGAIN;
1521 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001522 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001523 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001524 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001525 ret = -EAGAIN;
1526 }
1527 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001528 return ret;
1529}
1530
Pavel Begunkov267bc902019-11-07 01:41:08 +03001531static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1532 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001533{
1534 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1535 struct kiocb *kiocb = &req->rw;
1536 struct iov_iter iter;
1537 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001538 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001539 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001540
Pavel Begunkov267bc902019-11-07 01:41:08 +03001541 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001542 if (ret)
1543 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001544
Jens Axboe2b188cc2019-01-07 10:46:33 -07001545 file = kiocb->ki_filp;
1546 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001547 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001548
Pavel Begunkov267bc902019-11-07 01:41:08 +03001549 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001550 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001551 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001552
Jens Axboe9e645e112019-05-10 16:07:28 -06001553 if (req->flags & REQ_F_LINK)
1554 req->result = ret;
1555
Jens Axboe31b51512019-01-18 22:56:34 -07001556 iov_count = iov_iter_count(&iter);
1557
1558 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001559 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001560 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001561
1562 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001563 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001564 ssize_t ret2;
1565
Jens Axboe2b188cc2019-01-07 10:46:33 -07001566 /*
1567 * Open-code file_start_write here to grab freeze protection,
1568 * which will be released by another thread in
1569 * io_complete_rw(). Fool lockdep by telling it the lock got
1570 * released so that it doesn't complain about the held lock when
1571 * we return to userspace.
1572 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001573 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001574 __sb_start_write(file_inode(file)->i_sb,
1575 SB_FREEZE_WRITE, true);
1576 __sb_writers_release(file_inode(file)->i_sb,
1577 SB_FREEZE_WRITE);
1578 }
1579 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001580
Jens Axboe32960612019-09-23 11:05:34 -06001581 if (file->f_op->write_iter)
1582 ret2 = call_write_iter(file, kiocb, &iter);
1583 else
1584 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001585 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001586 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001587 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001588 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001589 }
Jens Axboe31b51512019-01-18 22:56:34 -07001590out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001591 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001592 return ret;
1593}
1594
1595/*
1596 * IORING_OP_NOP just posts a completion event, nothing else.
1597 */
1598static int io_nop(struct io_kiocb *req, u64 user_data)
1599{
1600 struct io_ring_ctx *ctx = req->ctx;
1601 long err = 0;
1602
Jens Axboedef596e2019-01-09 08:59:42 -07001603 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1604 return -EINVAL;
1605
Jens Axboec71ffb62019-05-13 20:58:29 -06001606 io_cqring_add_event(ctx, user_data, err);
Jens Axboeba816ad2019-09-28 11:36:45 -06001607 io_put_req(req, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001608 return 0;
1609}
1610
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001611static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1612{
Jens Axboe6b063142019-01-10 22:13:58 -07001613 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001614
Jens Axboe09bb8392019-03-13 12:39:28 -06001615 if (!req->file)
1616 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001617
Jens Axboe6b063142019-01-10 22:13:58 -07001618 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001619 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001620 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001621 return -EINVAL;
1622
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001623 return 0;
1624}
1625
1626static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001627 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001628{
1629 loff_t sqe_off = READ_ONCE(sqe->off);
1630 loff_t sqe_len = READ_ONCE(sqe->len);
1631 loff_t end = sqe_off + sqe_len;
1632 unsigned fsync_flags;
1633 int ret;
1634
1635 fsync_flags = READ_ONCE(sqe->fsync_flags);
1636 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1637 return -EINVAL;
1638
1639 ret = io_prep_fsync(req, sqe);
1640 if (ret)
1641 return ret;
1642
1643 /* fsync always requires a blocking context */
1644 if (force_nonblock)
1645 return -EAGAIN;
1646
1647 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1648 end > 0 ? end : LLONG_MAX,
1649 fsync_flags & IORING_FSYNC_DATASYNC);
1650
Jens Axboe9e645e112019-05-10 16:07:28 -06001651 if (ret < 0 && (req->flags & REQ_F_LINK))
1652 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001653 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001654 io_put_req(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001655 return 0;
1656}
1657
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001658static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1659{
1660 struct io_ring_ctx *ctx = req->ctx;
1661 int ret = 0;
1662
1663 if (!req->file)
1664 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001665
1666 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1667 return -EINVAL;
1668 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1669 return -EINVAL;
1670
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001671 return ret;
1672}
1673
1674static int io_sync_file_range(struct io_kiocb *req,
1675 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001676 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001677 bool force_nonblock)
1678{
1679 loff_t sqe_off;
1680 loff_t sqe_len;
1681 unsigned flags;
1682 int ret;
1683
1684 ret = io_prep_sfr(req, sqe);
1685 if (ret)
1686 return ret;
1687
1688 /* sync_file_range always requires a blocking context */
1689 if (force_nonblock)
1690 return -EAGAIN;
1691
1692 sqe_off = READ_ONCE(sqe->off);
1693 sqe_len = READ_ONCE(sqe->len);
1694 flags = READ_ONCE(sqe->sync_range_flags);
1695
1696 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1697
Jens Axboe9e645e112019-05-10 16:07:28 -06001698 if (ret < 0 && (req->flags & REQ_F_LINK))
1699 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001700 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001701 io_put_req(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001702 return 0;
1703}
1704
Jens Axboe0fa03c62019-04-19 13:34:07 -06001705#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001706static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001707 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001708 long (*fn)(struct socket *, struct user_msghdr __user *,
1709 unsigned int))
1710{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001711 struct socket *sock;
1712 int ret;
1713
1714 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1715 return -EINVAL;
1716
1717 sock = sock_from_file(req->file, &ret);
1718 if (sock) {
1719 struct user_msghdr __user *msg;
1720 unsigned flags;
1721
1722 flags = READ_ONCE(sqe->msg_flags);
1723 if (flags & MSG_DONTWAIT)
1724 req->flags |= REQ_F_NOWAIT;
1725 else if (force_nonblock)
1726 flags |= MSG_DONTWAIT;
1727
1728 msg = (struct user_msghdr __user *) (unsigned long)
1729 READ_ONCE(sqe->addr);
1730
Jens Axboeaa1fa282019-04-19 13:38:09 -06001731 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001732 if (force_nonblock && ret == -EAGAIN)
1733 return ret;
1734 }
1735
1736 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001737 if (ret < 0 && (req->flags & REQ_F_LINK))
1738 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06001739 io_put_req(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001740 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001741}
1742#endif
1743
1744static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001745 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001746{
1747#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001748 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1749 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001750#else
1751 return -EOPNOTSUPP;
1752#endif
1753}
1754
1755static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001756 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001757{
1758#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001759 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1760 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001761#else
1762 return -EOPNOTSUPP;
1763#endif
1764}
1765
Jens Axboe17f2fe32019-10-17 14:42:58 -06001766static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1767 struct io_kiocb **nxt, bool force_nonblock)
1768{
1769#if defined(CONFIG_NET)
1770 struct sockaddr __user *addr;
1771 int __user *addr_len;
1772 unsigned file_flags;
1773 int flags, ret;
1774
1775 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1776 return -EINVAL;
1777 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1778 return -EINVAL;
1779
1780 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1781 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1782 flags = READ_ONCE(sqe->accept_flags);
1783 file_flags = force_nonblock ? O_NONBLOCK : 0;
1784
1785 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1786 if (ret == -EAGAIN && force_nonblock) {
1787 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1788 return -EAGAIN;
1789 }
1790 if (ret < 0 && (req->flags & REQ_F_LINK))
1791 req->flags |= REQ_F_FAIL_LINK;
1792 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1793 io_put_req(req, nxt);
1794 return 0;
1795#else
1796 return -EOPNOTSUPP;
1797#endif
1798}
1799
Jens Axboe221c5eb2019-01-17 09:41:58 -07001800static void io_poll_remove_one(struct io_kiocb *req)
1801{
1802 struct io_poll_iocb *poll = &req->poll;
1803
1804 spin_lock(&poll->head->lock);
1805 WRITE_ONCE(poll->canceled, true);
1806 if (!list_empty(&poll->wait.entry)) {
1807 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001808 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001809 }
1810 spin_unlock(&poll->head->lock);
1811
1812 list_del_init(&req->list);
1813}
1814
1815static void io_poll_remove_all(struct io_ring_ctx *ctx)
1816{
1817 struct io_kiocb *req;
1818
1819 spin_lock_irq(&ctx->completion_lock);
1820 while (!list_empty(&ctx->cancel_list)) {
1821 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1822 io_poll_remove_one(req);
1823 }
1824 spin_unlock_irq(&ctx->completion_lock);
1825}
1826
1827/*
1828 * Find a running poll command that matches one specified in sqe->addr,
1829 * and remove it if found.
1830 */
1831static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1832{
1833 struct io_ring_ctx *ctx = req->ctx;
1834 struct io_kiocb *poll_req, *next;
1835 int ret = -ENOENT;
1836
1837 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1838 return -EINVAL;
1839 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1840 sqe->poll_events)
1841 return -EINVAL;
1842
1843 spin_lock_irq(&ctx->completion_lock);
1844 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1845 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1846 io_poll_remove_one(poll_req);
1847 ret = 0;
1848 break;
1849 }
1850 }
1851 spin_unlock_irq(&ctx->completion_lock);
1852
Jens Axboec71ffb62019-05-13 20:58:29 -06001853 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001854 if (ret < 0 && (req->flags & REQ_F_LINK))
1855 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06001856 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001857 return 0;
1858}
1859
Jens Axboe8c838782019-03-12 15:48:16 -06001860static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1861 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001862{
Jens Axboe8c838782019-03-12 15:48:16 -06001863 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001864 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001865 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001866}
1867
Jens Axboe561fb042019-10-24 07:25:42 -06001868static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001869{
Jens Axboe561fb042019-10-24 07:25:42 -06001870 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001871 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1872 struct io_poll_iocb *poll = &req->poll;
1873 struct poll_table_struct pt = { ._key = poll->events };
1874 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07001875 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001876 __poll_t mask = 0;
1877
Jens Axboe561fb042019-10-24 07:25:42 -06001878 if (work->flags & IO_WQ_WORK_CANCEL)
1879 WRITE_ONCE(poll->canceled, true);
1880
Jens Axboe221c5eb2019-01-17 09:41:58 -07001881 if (!READ_ONCE(poll->canceled))
1882 mask = vfs_poll(poll->file, &pt) & poll->events;
1883
1884 /*
1885 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1886 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1887 * synchronize with them. In the cancellation case the list_del_init
1888 * itself is not actually needed, but harmless so we keep it in to
1889 * avoid further branches in the fast path.
1890 */
1891 spin_lock_irq(&ctx->completion_lock);
1892 if (!mask && !READ_ONCE(poll->canceled)) {
1893 add_wait_queue(poll->head, &poll->wait);
1894 spin_unlock_irq(&ctx->completion_lock);
1895 return;
1896 }
1897 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001898 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001899 spin_unlock_irq(&ctx->completion_lock);
1900
Jens Axboe8c838782019-03-12 15:48:16 -06001901 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07001902
1903 io_put_req(req, &nxt);
1904 if (nxt)
1905 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001906}
1907
1908static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1909 void *key)
1910{
1911 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1912 wait);
1913 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1914 struct io_ring_ctx *ctx = req->ctx;
1915 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001916 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001917
1918 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001919 if (mask && !(mask & poll->events))
1920 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001921
1922 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001923
1924 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1925 list_del(&req->list);
1926 io_poll_complete(ctx, req, mask);
1927 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1928
1929 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001930 io_put_req(req, NULL);
Jens Axboe8c838782019-03-12 15:48:16 -06001931 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001932 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001933 }
1934
Jens Axboe221c5eb2019-01-17 09:41:58 -07001935 return 1;
1936}
1937
1938struct io_poll_table {
1939 struct poll_table_struct pt;
1940 struct io_kiocb *req;
1941 int error;
1942};
1943
1944static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1945 struct poll_table_struct *p)
1946{
1947 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1948
1949 if (unlikely(pt->req->poll.head)) {
1950 pt->error = -EINVAL;
1951 return;
1952 }
1953
1954 pt->error = 0;
1955 pt->req->poll.head = head;
1956 add_wait_queue(head, &pt->req->poll.wait);
1957}
1958
Jens Axboe89723d02019-11-05 15:32:58 -07001959static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1960 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001961{
1962 struct io_poll_iocb *poll = &req->poll;
1963 struct io_ring_ctx *ctx = req->ctx;
1964 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001965 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001966 __poll_t mask;
1967 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001968
1969 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1970 return -EINVAL;
1971 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1972 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001973 if (!poll->file)
1974 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001975
Jens Axboe6cc47d12019-09-18 11:18:23 -06001976 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06001977 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001978 events = READ_ONCE(sqe->poll_events);
1979 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1980
Jens Axboe221c5eb2019-01-17 09:41:58 -07001981 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001982 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001983 poll->canceled = false;
1984
1985 ipt.pt._qproc = io_poll_queue_proc;
1986 ipt.pt._key = poll->events;
1987 ipt.req = req;
1988 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1989
1990 /* initialized the list so that we can do list_empty checks */
1991 INIT_LIST_HEAD(&poll->wait.entry);
1992 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1993
Jens Axboe36703242019-07-25 10:20:18 -06001994 INIT_LIST_HEAD(&req->list);
1995
Jens Axboe221c5eb2019-01-17 09:41:58 -07001996 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001997
1998 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001999 if (likely(poll->head)) {
2000 spin_lock(&poll->head->lock);
2001 if (unlikely(list_empty(&poll->wait.entry))) {
2002 if (ipt.error)
2003 cancel = true;
2004 ipt.error = 0;
2005 mask = 0;
2006 }
2007 if (mask || ipt.error)
2008 list_del_init(&poll->wait.entry);
2009 else if (cancel)
2010 WRITE_ONCE(poll->canceled, true);
2011 else if (!poll->done) /* actually waiting for an event */
2012 list_add_tail(&req->list, &ctx->cancel_list);
2013 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002014 }
Jens Axboe8c838782019-03-12 15:48:16 -06002015 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002016 ipt.error = 0;
2017 io_poll_complete(ctx, req, mask);
2018 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002019 spin_unlock_irq(&ctx->completion_lock);
2020
Jens Axboe8c838782019-03-12 15:48:16 -06002021 if (mask) {
2022 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002023 io_put_req(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002024 }
Jens Axboe8c838782019-03-12 15:48:16 -06002025 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002026}
2027
Jens Axboe5262f562019-09-17 12:26:57 -06002028static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2029{
2030 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002031 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06002032 unsigned long flags;
2033
2034 req = container_of(timer, struct io_kiocb, timeout.timer);
2035 ctx = req->ctx;
2036 atomic_inc(&ctx->cq_timeouts);
2037
2038 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002039 /*
Jens Axboe11365042019-10-16 09:08:32 -06002040 * We could be racing with timeout deletion. If the list is empty,
2041 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002042 */
Jens Axboe842f9612019-10-29 12:34:10 -06002043 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002044 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002045
Jens Axboe11365042019-10-16 09:08:32 -06002046 /*
2047 * Adjust the reqs sequence before the current one because it
2048 * will consume a slot in the cq_ring and the the cq_tail
2049 * pointer will be increased, otherwise other timeout reqs may
2050 * return in advance without waiting for enough wait_nr.
2051 */
2052 prev = req;
2053 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2054 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002055 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002056 }
Jens Axboe842f9612019-10-29 12:34:10 -06002057
2058 io_cqring_fill_event(ctx, req->user_data, -ETIME);
2059 io_commit_cqring(ctx);
Jens Axboe5262f562019-09-17 12:26:57 -06002060 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2061
Jens Axboe842f9612019-10-29 12:34:10 -06002062 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002063 if (req->flags & REQ_F_LINK)
2064 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe842f9612019-10-29 12:34:10 -06002065 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002066 return HRTIMER_NORESTART;
2067}
2068
2069/*
2070 * Remove or update an existing timeout command
2071 */
2072static int io_timeout_remove(struct io_kiocb *req,
2073 const struct io_uring_sqe *sqe)
2074{
2075 struct io_ring_ctx *ctx = req->ctx;
2076 struct io_kiocb *treq;
2077 int ret = -ENOENT;
2078 __u64 user_data;
2079 unsigned flags;
2080
2081 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2082 return -EINVAL;
2083 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2084 return -EINVAL;
2085 flags = READ_ONCE(sqe->timeout_flags);
2086 if (flags)
2087 return -EINVAL;
2088
2089 user_data = READ_ONCE(sqe->addr);
2090 spin_lock_irq(&ctx->completion_lock);
2091 list_for_each_entry(treq, &ctx->timeout_list, list) {
2092 if (user_data == treq->user_data) {
2093 list_del_init(&treq->list);
2094 ret = 0;
2095 break;
2096 }
2097 }
2098
2099 /* didn't find timeout */
2100 if (ret) {
2101fill_ev:
2102 io_cqring_fill_event(ctx, req->user_data, ret);
2103 io_commit_cqring(ctx);
2104 spin_unlock_irq(&ctx->completion_lock);
2105 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002106 if (req->flags & REQ_F_LINK)
2107 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe11365042019-10-16 09:08:32 -06002108 io_put_req(req, NULL);
2109 return 0;
2110 }
2111
2112 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
2113 if (ret == -1) {
2114 ret = -EBUSY;
2115 goto fill_ev;
2116 }
2117
2118 io_cqring_fill_event(ctx, req->user_data, 0);
2119 io_cqring_fill_event(ctx, treq->user_data, -ECANCELED);
2120 io_commit_cqring(ctx);
2121 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002122 io_cqring_ev_posted(ctx);
2123
Jens Axboe11365042019-10-16 09:08:32 -06002124 io_put_req(treq, NULL);
Jens Axboeba816ad2019-09-28 11:36:45 -06002125 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002126 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002127}
2128
2129static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2130{
yangerkun5da0fb12019-10-15 21:59:29 +08002131 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002132 struct io_ring_ctx *ctx = req->ctx;
2133 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002134 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002135 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002136 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002137 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002138
2139 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2140 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002141 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2142 return -EINVAL;
2143 flags = READ_ONCE(sqe->timeout_flags);
2144 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002145 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002146
2147 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002148 return -EFAULT;
2149
Jens Axboe11365042019-10-16 09:08:32 -06002150 if (flags & IORING_TIMEOUT_ABS)
2151 mode = HRTIMER_MODE_ABS;
2152 else
2153 mode = HRTIMER_MODE_REL;
2154
2155 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2156
Jens Axboe5262f562019-09-17 12:26:57 -06002157 /*
2158 * sqe->off holds how many events that need to occur for this
2159 * timeout event to be satisfied.
2160 */
2161 count = READ_ONCE(sqe->off);
2162 if (!count)
2163 count = 1;
2164
2165 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002166 /* reuse it to store the count */
2167 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002168 req->flags |= REQ_F_TIMEOUT;
2169
2170 /*
2171 * Insertion sort, ensuring the first entry in the list is always
2172 * the one we need first.
2173 */
Jens Axboe5262f562019-09-17 12:26:57 -06002174 spin_lock_irq(&ctx->completion_lock);
2175 list_for_each_prev(entry, &ctx->timeout_list) {
2176 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002177 unsigned nxt_sq_head;
2178 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002179
yangerkun5da0fb12019-10-15 21:59:29 +08002180 /*
2181 * Since cached_sq_head + count - 1 can overflow, use type long
2182 * long to store it.
2183 */
2184 tmp = (long long)ctx->cached_sq_head + count - 1;
2185 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2186 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2187
2188 /*
2189 * cached_sq_head may overflow, and it will never overflow twice
2190 * once there is some timeout req still be valid.
2191 */
2192 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002193 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002194
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002195 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002196 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002197
2198 /*
2199 * Sequence of reqs after the insert one and itself should
2200 * be adjusted because each timeout req consumes a slot.
2201 */
2202 span++;
2203 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002204 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002205 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002206 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002207 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002208 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002209 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002210 return 0;
2211}
2212
Jens Axboe62755e32019-10-28 21:49:21 -06002213static bool io_cancel_cb(struct io_wq_work *work, void *data)
2214{
2215 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2216
2217 return req->user_data == (unsigned long) data;
2218}
2219
Jens Axboee977d6d2019-11-05 12:39:45 -07002220static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002221{
Jens Axboe62755e32019-10-28 21:49:21 -06002222 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002223 int ret = 0;
2224
Jens Axboe62755e32019-10-28 21:49:21 -06002225 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2226 switch (cancel_ret) {
2227 case IO_WQ_CANCEL_OK:
2228 ret = 0;
2229 break;
2230 case IO_WQ_CANCEL_RUNNING:
2231 ret = -EALREADY;
2232 break;
2233 case IO_WQ_CANCEL_NOTFOUND:
2234 ret = -ENOENT;
2235 break;
2236 }
2237
Jens Axboee977d6d2019-11-05 12:39:45 -07002238 return ret;
2239}
2240
2241static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2242 struct io_kiocb **nxt)
2243{
2244 struct io_ring_ctx *ctx = req->ctx;
2245 void *sqe_addr;
2246 int ret;
2247
2248 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2249 return -EINVAL;
2250 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2251 sqe->cancel_flags)
2252 return -EINVAL;
2253
2254 sqe_addr = (void *) (unsigned long) READ_ONCE(sqe->addr);
2255 ret = io_async_cancel_one(ctx, sqe_addr);
2256
Jens Axboe62755e32019-10-28 21:49:21 -06002257 if (ret < 0 && (req->flags & REQ_F_LINK))
2258 req->flags |= REQ_F_FAIL_LINK;
2259 io_cqring_add_event(req->ctx, sqe->user_data, ret);
2260 io_put_req(req, nxt);
2261 return 0;
2262}
2263
Pavel Begunkov267bc902019-11-07 01:41:08 +03002264static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002265{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002266 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002267 struct io_uring_sqe *sqe_copy;
2268
2269 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2270 return 0;
2271
2272 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2273 if (!sqe_copy)
2274 return -EAGAIN;
2275
2276 spin_lock_irq(&ctx->completion_lock);
2277 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2278 spin_unlock_irq(&ctx->completion_lock);
2279 kfree(sqe_copy);
2280 return 0;
2281 }
2282
2283 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2284 req->submit.sqe = sqe_copy;
2285
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002286 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002287 list_add_tail(&req->list, &ctx->defer_list);
2288 spin_unlock_irq(&ctx->completion_lock);
2289 return -EIOCBQUEUED;
2290}
2291
Jens Axboe2b188cc2019-01-07 10:46:33 -07002292static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002293 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002294{
Jens Axboee0c5c572019-03-12 10:18:47 -06002295 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002296 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002297
Jens Axboe9e645e112019-05-10 16:07:28 -06002298 req->user_data = READ_ONCE(s->sqe->user_data);
2299
Jens Axboe2b188cc2019-01-07 10:46:33 -07002300 opcode = READ_ONCE(s->sqe->opcode);
2301 switch (opcode) {
2302 case IORING_OP_NOP:
2303 ret = io_nop(req, req->user_data);
2304 break;
2305 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002306 if (unlikely(s->sqe->buf_index))
2307 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002308 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002309 break;
2310 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002311 if (unlikely(s->sqe->buf_index))
2312 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002313 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002314 break;
2315 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002316 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002317 break;
2318 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002319 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002320 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002321 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002322 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002323 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002324 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002325 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002326 break;
2327 case IORING_OP_POLL_REMOVE:
2328 ret = io_poll_remove(req, s->sqe);
2329 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002330 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002331 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002332 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002333 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002334 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002335 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002336 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002337 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002338 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002339 case IORING_OP_TIMEOUT:
2340 ret = io_timeout(req, s->sqe);
2341 break;
Jens Axboe11365042019-10-16 09:08:32 -06002342 case IORING_OP_TIMEOUT_REMOVE:
2343 ret = io_timeout_remove(req, s->sqe);
2344 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002345 case IORING_OP_ACCEPT:
2346 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2347 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002348 case IORING_OP_ASYNC_CANCEL:
2349 ret = io_async_cancel(req, s->sqe, nxt);
2350 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002351 default:
2352 ret = -EINVAL;
2353 break;
2354 }
2355
Jens Axboedef596e2019-01-09 08:59:42 -07002356 if (ret)
2357 return ret;
2358
2359 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002360 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002361 return -EAGAIN;
2362
2363 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002364 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002365 mutex_lock(&ctx->uring_lock);
2366 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002367 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002368 mutex_unlock(&ctx->uring_lock);
2369 }
2370
2371 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002372}
2373
Jens Axboe561fb042019-10-24 07:25:42 -06002374static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002375{
Jens Axboe561fb042019-10-24 07:25:42 -06002376 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002377 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002378 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -06002379 struct sqe_submit *s = &req->submit;
2380 const struct io_uring_sqe *sqe = s->sqe;
2381 struct io_kiocb *nxt = NULL;
2382 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002383
Jens Axboe561fb042019-10-24 07:25:42 -06002384 /* Ensure we clear previously set non-block flag */
2385 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002386
Jens Axboe561fb042019-10-24 07:25:42 -06002387 if (work->flags & IO_WQ_WORK_CANCEL)
2388 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002389
Jens Axboe561fb042019-10-24 07:25:42 -06002390 if (!ret) {
2391 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2392 s->in_async = true;
2393 do {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002394 ret = __io_submit_sqe(ctx, req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002395 /*
2396 * We can get EAGAIN for polled IO even though we're
2397 * forcing a sync submission from here, since we can't
2398 * wait for request slots on the block side.
2399 */
2400 if (ret != -EAGAIN)
2401 break;
2402 cond_resched();
2403 } while (1);
2404 }
Jens Axboe31b51512019-01-18 22:56:34 -07002405
Jens Axboe561fb042019-10-24 07:25:42 -06002406 /* drop submission reference */
2407 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002408
Jens Axboe561fb042019-10-24 07:25:42 -06002409 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002410 if (req->flags & REQ_F_LINK)
2411 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe561fb042019-10-24 07:25:42 -06002412 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002413 io_put_req(req, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07002414 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002415
Jens Axboe561fb042019-10-24 07:25:42 -06002416 /* async context always use a copy of the sqe */
2417 kfree(sqe);
2418
2419 /* if a dependent link is ready, pass it back */
2420 if (!ret && nxt) {
2421 io_prep_async_work(nxt);
2422 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002423 }
Jens Axboe31b51512019-01-18 22:56:34 -07002424}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002425
Jens Axboe09bb8392019-03-13 12:39:28 -06002426static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2427{
2428 int op = READ_ONCE(sqe->opcode);
2429
2430 switch (op) {
2431 case IORING_OP_NOP:
2432 case IORING_OP_POLL_REMOVE:
2433 return false;
2434 default:
2435 return true;
2436 }
2437}
2438
Jens Axboe65e19f52019-10-26 07:20:21 -06002439static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2440 int index)
2441{
2442 struct fixed_file_table *table;
2443
2444 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2445 return table->files[index & IORING_FILE_TABLE_MASK];
2446}
2447
Pavel Begunkov267bc902019-11-07 01:41:08 +03002448static int io_req_set_file(struct io_ring_ctx *ctx,
Jens Axboe09bb8392019-03-13 12:39:28 -06002449 struct io_submit_state *state, struct io_kiocb *req)
2450{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002451 struct sqe_submit *s = &req->submit;
Jens Axboe09bb8392019-03-13 12:39:28 -06002452 unsigned flags;
2453 int fd;
2454
2455 flags = READ_ONCE(s->sqe->flags);
2456 fd = READ_ONCE(s->sqe->fd);
2457
Jackie Liu4fe2c962019-09-09 20:50:40 +08002458 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002459 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002460 /*
2461 * All io need record the previous position, if LINK vs DARIN,
2462 * it can be used to mark the position of the first IO in the
2463 * link list.
2464 */
2465 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002466
Jens Axboe60c112b2019-06-21 10:20:18 -06002467 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002468 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002469
2470 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002471 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002472 (unsigned) fd >= ctx->nr_user_files))
2473 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002474 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002475 req->file = io_file_from_index(ctx, fd);
2476 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002477 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002478 req->flags |= REQ_F_FIXED_FILE;
2479 } else {
2480 if (s->needs_fixed_file)
2481 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002482 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002483 req->file = io_file_get(state, fd);
2484 if (unlikely(!req->file))
2485 return -EBADF;
2486 }
2487
2488 return 0;
2489}
2490
Jens Axboefcb323c2019-10-24 12:39:47 -06002491static int io_grab_files(struct io_ring_ctx *ctx, struct io_kiocb *req)
2492{
2493 int ret = -EBADF;
2494
2495 rcu_read_lock();
2496 spin_lock_irq(&ctx->inflight_lock);
2497 /*
2498 * We use the f_ops->flush() handler to ensure that we can flush
2499 * out work accessing these files if the fd is closed. Check if
2500 * the fd has changed since we started down this path, and disallow
2501 * this operation if it has.
2502 */
2503 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2504 list_add(&req->inflight_entry, &ctx->inflight_list);
2505 req->flags |= REQ_F_INFLIGHT;
2506 req->work.files = current->files;
2507 ret = 0;
2508 }
2509 spin_unlock_irq(&ctx->inflight_lock);
2510 rcu_read_unlock();
2511
2512 return ret;
2513}
2514
Jens Axboe2665abf2019-11-05 12:40:47 -07002515static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2516{
2517 struct io_kiocb *req = container_of(timer, struct io_kiocb,
2518 timeout.timer);
2519 struct io_ring_ctx *ctx = req->ctx;
2520 struct io_kiocb *prev = NULL;
2521 unsigned long flags;
2522 int ret = -ETIME;
2523
2524 spin_lock_irqsave(&ctx->completion_lock, flags);
2525
2526 /*
2527 * We don't expect the list to be empty, that will only happen if we
2528 * race with the completion of the linked work.
2529 */
2530 if (!list_empty(&req->list)) {
2531 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
2532 list_del_init(&req->list);
2533 }
2534
2535 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2536
2537 if (prev) {
2538 void *user_data = (void *) (unsigned long) prev->user_data;
2539 ret = io_async_cancel_one(ctx, user_data);
2540 }
2541
2542 io_cqring_add_event(ctx, req->user_data, ret);
2543 io_put_req(req, NULL);
2544 return HRTIMER_NORESTART;
2545}
2546
2547static int io_queue_linked_timeout(struct io_kiocb *req, struct io_kiocb *nxt)
2548{
2549 const struct io_uring_sqe *sqe = nxt->submit.sqe;
2550 enum hrtimer_mode mode;
2551 struct timespec64 ts;
2552 int ret = -EINVAL;
2553
2554 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || sqe->off)
2555 goto err;
2556 if (sqe->timeout_flags & ~IORING_TIMEOUT_ABS)
2557 goto err;
2558 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr))) {
2559 ret = -EFAULT;
2560 goto err;
2561 }
2562
2563 req->flags |= REQ_F_LINK_TIMEOUT;
2564
2565 if (sqe->timeout_flags & IORING_TIMEOUT_ABS)
2566 mode = HRTIMER_MODE_ABS;
2567 else
2568 mode = HRTIMER_MODE_REL;
2569 hrtimer_init(&nxt->timeout.timer, CLOCK_MONOTONIC, mode);
2570 nxt->timeout.timer.function = io_link_timeout_fn;
2571 hrtimer_start(&nxt->timeout.timer, timespec64_to_ktime(ts), mode);
2572 ret = 0;
2573err:
2574 /* drop submission reference */
2575 io_put_req(nxt, NULL);
2576
2577 if (ret) {
2578 struct io_ring_ctx *ctx = req->ctx;
2579
2580 /*
2581 * Break the link and fail linked timeout, parent will get
2582 * failed by the regular submission path.
2583 */
2584 list_del(&nxt->list);
2585 io_cqring_fill_event(ctx, nxt->user_data, ret);
2586 trace_io_uring_fail_link(req, nxt);
2587 io_commit_cqring(ctx);
2588 io_put_req(nxt, NULL);
2589 ret = -ECANCELED;
2590 }
2591
2592 return ret;
2593}
2594
2595static inline struct io_kiocb *io_get_linked_timeout(struct io_kiocb *req)
2596{
2597 struct io_kiocb *nxt;
2598
2599 if (!(req->flags & REQ_F_LINK))
2600 return NULL;
2601
2602 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
2603 if (nxt && nxt->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT)
2604 return nxt;
2605
2606 return NULL;
2607}
2608
Pavel Begunkov267bc902019-11-07 01:41:08 +03002609static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002610{
Jens Axboe2665abf2019-11-05 12:40:47 -07002611 struct io_kiocb *nxt;
Jens Axboee0c5c572019-03-12 10:18:47 -06002612 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002613
Jens Axboe2665abf2019-11-05 12:40:47 -07002614 nxt = io_get_linked_timeout(req);
2615 if (unlikely(nxt)) {
2616 ret = io_queue_linked_timeout(req, nxt);
2617 if (ret)
2618 goto err;
2619 }
2620
Pavel Begunkov267bc902019-11-07 01:41:08 +03002621 ret = __io_submit_sqe(ctx, req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002622
2623 /*
2624 * We async punt it if the file wasn't marked NOWAIT, or if the file
2625 * doesn't support non-blocking read/write attempts
2626 */
2627 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2628 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002629 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002630 struct io_uring_sqe *sqe_copy;
2631
Jackie Liu954dab12019-09-18 10:37:52 +08002632 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002633 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002634 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002635 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2636 ret = io_grab_files(ctx, req);
2637 if (ret) {
2638 kfree(sqe_copy);
2639 goto err;
2640 }
2641 }
Jens Axboee65ef562019-03-12 10:16:44 -06002642
2643 /*
2644 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002645 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002646 */
Jens Axboefcb323c2019-10-24 12:39:47 -06002647 io_queue_async_work(ctx, req);
Jens Axboee65ef562019-03-12 10:16:44 -06002648 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002649 }
2650 }
Jens Axboee65ef562019-03-12 10:16:44 -06002651
2652 /* drop submission reference */
Jens Axboefcb323c2019-10-24 12:39:47 -06002653err:
Jens Axboeba816ad2019-09-28 11:36:45 -06002654 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002655
2656 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002657 if (ret) {
2658 io_cqring_add_event(ctx, req->user_data, ret);
2659 if (req->flags & REQ_F_LINK)
2660 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002661 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002662 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002663
2664 return ret;
2665}
2666
Pavel Begunkov267bc902019-11-07 01:41:08 +03002667static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002668{
2669 int ret;
2670
Pavel Begunkov267bc902019-11-07 01:41:08 +03002671 ret = io_req_defer(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002672 if (ret) {
2673 if (ret != -EIOCBQUEUED) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002674 io_cqring_add_event(ctx, req->submit.sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002675 io_free_req(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002676 }
2677 return 0;
2678 }
2679
Pavel Begunkov267bc902019-11-07 01:41:08 +03002680 return __io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002681}
2682
2683static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002684 struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002685{
2686 int ret;
2687 int need_submit = false;
2688
2689 if (!shadow)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002690 return io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002691
2692 /*
2693 * Mark the first IO in link list as DRAIN, let all the following
2694 * IOs enter the defer list. all IO needs to be completed before link
2695 * list.
2696 */
2697 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002698 ret = io_req_defer(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002699 if (ret) {
2700 if (ret != -EIOCBQUEUED) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002701 io_cqring_add_event(ctx, req->submit.sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002702 io_free_req(req, NULL);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002703 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002704 return 0;
2705 }
2706 } else {
2707 /*
2708 * If ret == 0 means that all IOs in front of link io are
2709 * running done. let's queue link head.
2710 */
2711 need_submit = true;
2712 }
2713
2714 /* Insert shadow req to defer_list, blocking next IOs */
2715 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002716 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002717 list_add_tail(&shadow->list, &ctx->defer_list);
2718 spin_unlock_irq(&ctx->completion_lock);
2719
2720 if (need_submit)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002721 return __io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002722
2723 return 0;
2724}
2725
Jens Axboe9e645e112019-05-10 16:07:28 -06002726#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2727
Pavel Begunkov196be952019-11-07 01:41:06 +03002728static void io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002729 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002730{
2731 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002732 struct sqe_submit *s = &req->submit;
Jens Axboe9e645e112019-05-10 16:07:28 -06002733 int ret;
2734
2735 /* enforce forwards compatibility on users */
2736 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2737 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03002738 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002739 }
2740
Pavel Begunkov267bc902019-11-07 01:41:08 +03002741 ret = io_req_set_file(ctx, state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002742 if (unlikely(ret)) {
2743err_req:
Jens Axboe9e645e112019-05-10 16:07:28 -06002744 io_cqring_add_event(ctx, s->sqe->user_data, ret);
Pavel Begunkov267bc902019-11-07 01:41:08 +03002745 io_free_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002746 return;
2747 }
2748
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002749 req->user_data = s->sqe->user_data;
2750
Jens Axboe9e645e112019-05-10 16:07:28 -06002751 /*
2752 * If we already have a head request, queue this one for async
2753 * submittal once the head completes. If we don't have a head but
2754 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2755 * submitted sync once the chain is complete. If none of those
2756 * conditions are true (normal request), then just queue it.
2757 */
2758 if (*link) {
2759 struct io_kiocb *prev = *link;
2760
2761 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2762 if (!sqe_copy) {
2763 ret = -EAGAIN;
2764 goto err_req;
2765 }
2766
2767 s->sqe = sqe_copy;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002768 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002769 list_add_tail(&req->list, &prev->link_list);
2770 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2771 req->flags |= REQ_F_LINK;
2772
Jens Axboe9e645e112019-05-10 16:07:28 -06002773 INIT_LIST_HEAD(&req->link_list);
2774 *link = req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002775 } else if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
2776 /* Only valid as a linked SQE */
2777 ret = -EINVAL;
2778 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002779 } else {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002780 io_queue_sqe(ctx, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002781 }
2782}
2783
Jens Axboe9a56a232019-01-09 09:06:50 -07002784/*
2785 * Batched submission is done, ensure local IO is flushed out.
2786 */
2787static void io_submit_state_end(struct io_submit_state *state)
2788{
2789 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002790 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002791 if (state->free_reqs)
2792 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2793 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002794}
2795
2796/*
2797 * Start submission side cache.
2798 */
2799static void io_submit_state_start(struct io_submit_state *state,
2800 struct io_ring_ctx *ctx, unsigned max_ios)
2801{
2802 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002803 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002804 state->file = NULL;
2805 state->ios_left = max_ios;
2806}
2807
Jens Axboe2b188cc2019-01-07 10:46:33 -07002808static void io_commit_sqring(struct io_ring_ctx *ctx)
2809{
Hristo Venev75b28af2019-08-26 17:23:46 +00002810 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002811
Hristo Venev75b28af2019-08-26 17:23:46 +00002812 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002813 /*
2814 * Ensure any loads from the SQEs are done at this point,
2815 * since once we write the new head, the application could
2816 * write new data to them.
2817 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002818 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002819 }
2820}
2821
2822/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002823 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2824 * that is mapped by userspace. This means that care needs to be taken to
2825 * ensure that reads are stable, as we cannot rely on userspace always
2826 * being a good citizen. If members of the sqe are validated and then later
2827 * used, it's important that those reads are done through READ_ONCE() to
2828 * prevent a re-load down the line.
2829 */
2830static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2831{
Hristo Venev75b28af2019-08-26 17:23:46 +00002832 struct io_rings *rings = ctx->rings;
2833 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002834 unsigned head;
2835
2836 /*
2837 * The cached sq head (or cq tail) serves two purposes:
2838 *
2839 * 1) allows us to batch the cost of updating the user visible
2840 * head updates.
2841 * 2) allows the kernel side to track the head on its own, even
2842 * though the application is the one updating it.
2843 */
2844 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002845 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002846 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002847 return false;
2848
Hristo Venev75b28af2019-08-26 17:23:46 +00002849 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002850 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002851 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002852 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002853 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002854 ctx->cached_sq_head++;
2855 return true;
2856 }
2857
2858 /* drop invalid entries */
2859 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002860 ctx->cached_sq_dropped++;
2861 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002862 return false;
2863}
2864
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002865static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002866 struct file *ring_file, int ring_fd,
2867 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002868{
2869 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002870 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002871 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002872 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002873 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002874
2875 if (nr > IO_PLUG_THRESHOLD) {
2876 io_submit_state_start(&state, ctx, nr);
2877 statep = &state;
2878 }
2879
2880 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002881 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03002882 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002883
Pavel Begunkov196be952019-11-07 01:41:06 +03002884 req = io_get_req(ctx, statep);
2885 if (unlikely(!req)) {
2886 if (!submitted)
2887 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002888 break;
Pavel Begunkov196be952019-11-07 01:41:06 +03002889 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03002890 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002891 __io_free_req(req);
2892 break;
2893 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002894
Pavel Begunkov50585b92019-11-07 01:41:07 +03002895 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002896 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
2897 if (!mm_fault) {
2898 use_mm(ctx->sqo_mm);
2899 *mm = ctx->sqo_mm;
2900 }
2901 }
2902
Pavel Begunkov50585b92019-11-07 01:41:07 +03002903 sqe_flags = req->submit.sqe->flags;
2904
2905 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002906 if (!shadow_req) {
2907 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002908 if (unlikely(!shadow_req))
2909 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002910 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2911 refcount_dec(&shadow_req->refs);
2912 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03002913 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002914 }
2915
Jackie Liua1041c22019-09-18 17:25:52 +08002916out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03002917 req->submit.ring_file = ring_file;
2918 req->submit.ring_fd = ring_fd;
2919 req->submit.has_user = *mm != NULL;
2920 req->submit.in_async = async;
2921 req->submit.needs_fixed_file = async;
2922 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
2923 true, async);
Pavel Begunkov267bc902019-11-07 01:41:08 +03002924 io_submit_sqe(ctx, req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002925 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03002926
2927 /*
2928 * If previous wasn't linked and we have a linked command,
2929 * that's the end of the chain. Submit the previous link.
2930 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03002931 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002932 io_queue_link_head(ctx, link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03002933 link = NULL;
2934 shadow_req = NULL;
2935 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002936 }
2937
Jens Axboe9e645e112019-05-10 16:07:28 -06002938 if (link)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002939 io_queue_link_head(ctx, link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002940 if (statep)
2941 io_submit_state_end(&state);
2942
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002943 /* Commit SQ ring head once we've consumed and submitted all SQEs */
2944 io_commit_sqring(ctx);
2945
Jens Axboe6c271ce2019-01-10 11:22:30 -07002946 return submitted;
2947}
2948
2949static int io_sq_thread(void *data)
2950{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002951 struct io_ring_ctx *ctx = data;
2952 struct mm_struct *cur_mm = NULL;
2953 mm_segment_t old_fs;
2954 DEFINE_WAIT(wait);
2955 unsigned inflight;
2956 unsigned long timeout;
2957
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002958 complete(&ctx->sqo_thread_started);
2959
Jens Axboe6c271ce2019-01-10 11:22:30 -07002960 old_fs = get_fs();
2961 set_fs(USER_DS);
2962
2963 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002964 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002965 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002966
2967 if (inflight) {
2968 unsigned nr_events = 0;
2969
2970 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002971 /*
2972 * inflight is the count of the maximum possible
2973 * entries we submitted, but it can be smaller
2974 * if we dropped some of them. If we don't have
2975 * poll entries available, then we know that we
2976 * have nothing left to poll for. Reset the
2977 * inflight count to zero in that case.
2978 */
2979 mutex_lock(&ctx->uring_lock);
2980 if (!list_empty(&ctx->poll_list))
2981 __io_iopoll_check(ctx, &nr_events, 0);
2982 else
2983 inflight = 0;
2984 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002985 } else {
2986 /*
2987 * Normal IO, just pretend everything completed.
2988 * We don't have to poll completions for that.
2989 */
2990 nr_events = inflight;
2991 }
2992
2993 inflight -= nr_events;
2994 if (!inflight)
2995 timeout = jiffies + ctx->sq_thread_idle;
2996 }
2997
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002998 to_submit = io_sqring_entries(ctx);
2999 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003000 /*
3001 * We're polling. If we're within the defined idle
3002 * period, then let us spin without work before going
3003 * to sleep.
3004 */
3005 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003006 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003007 continue;
3008 }
3009
3010 /*
3011 * Drop cur_mm before scheduling, we can't hold it for
3012 * long periods (or over schedule()). Do this before
3013 * adding ourselves to the waitqueue, as the unuse/drop
3014 * may sleep.
3015 */
3016 if (cur_mm) {
3017 unuse_mm(cur_mm);
3018 mmput(cur_mm);
3019 cur_mm = NULL;
3020 }
3021
3022 prepare_to_wait(&ctx->sqo_wait, &wait,
3023 TASK_INTERRUPTIBLE);
3024
3025 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003026 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003027 /* make sure to read SQ tail after writing flags */
3028 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003029
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003030 to_submit = io_sqring_entries(ctx);
3031 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003032 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003033 finish_wait(&ctx->sqo_wait, &wait);
3034 break;
3035 }
3036 if (signal_pending(current))
3037 flush_signals(current);
3038 schedule();
3039 finish_wait(&ctx->sqo_wait, &wait);
3040
Hristo Venev75b28af2019-08-26 17:23:46 +00003041 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003042 continue;
3043 }
3044 finish_wait(&ctx->sqo_wait, &wait);
3045
Hristo Venev75b28af2019-08-26 17:23:46 +00003046 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003047 }
3048
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003049 to_submit = min(to_submit, ctx->sq_entries);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003050 inflight += io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm,
3051 true);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003052 }
3053
3054 set_fs(old_fs);
3055 if (cur_mm) {
3056 unuse_mm(cur_mm);
3057 mmput(cur_mm);
3058 }
Jens Axboe06058632019-04-13 09:26:03 -06003059
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003060 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003061
Jens Axboe6c271ce2019-01-10 11:22:30 -07003062 return 0;
3063}
3064
Jens Axboebda52162019-09-24 13:47:15 -06003065struct io_wait_queue {
3066 struct wait_queue_entry wq;
3067 struct io_ring_ctx *ctx;
3068 unsigned to_wait;
3069 unsigned nr_timeouts;
3070};
3071
3072static inline bool io_should_wake(struct io_wait_queue *iowq)
3073{
3074 struct io_ring_ctx *ctx = iowq->ctx;
3075
3076 /*
3077 * Wake up if we have enough events, or if a timeout occured since we
3078 * started waiting. For timeouts, we always want to return to userspace,
3079 * regardless of event count.
3080 */
Jens Axboe84f97dc2019-11-06 11:27:53 -07003081 return io_cqring_events(ctx) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003082 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3083}
3084
3085static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3086 int wake_flags, void *key)
3087{
3088 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3089 wq);
3090
3091 if (!io_should_wake(iowq))
3092 return -1;
3093
3094 return autoremove_wake_function(curr, mode, wake_flags, key);
3095}
3096
Jens Axboe2b188cc2019-01-07 10:46:33 -07003097/*
3098 * Wait until events become available, if we don't already have some. The
3099 * application must reap them itself, as they reside on the shared cq ring.
3100 */
3101static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3102 const sigset_t __user *sig, size_t sigsz)
3103{
Jens Axboebda52162019-09-24 13:47:15 -06003104 struct io_wait_queue iowq = {
3105 .wq = {
3106 .private = current,
3107 .func = io_wake_function,
3108 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3109 },
3110 .ctx = ctx,
3111 .to_wait = min_events,
3112 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003113 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003114 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003115
Jens Axboe84f97dc2019-11-06 11:27:53 -07003116 if (io_cqring_events(ctx) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003117 return 0;
3118
3119 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003120#ifdef CONFIG_COMPAT
3121 if (in_compat_syscall())
3122 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003123 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003124 else
3125#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003126 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003127
Jens Axboe2b188cc2019-01-07 10:46:33 -07003128 if (ret)
3129 return ret;
3130 }
3131
Jens Axboebda52162019-09-24 13:47:15 -06003132 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003133 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003134 do {
3135 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3136 TASK_INTERRUPTIBLE);
3137 if (io_should_wake(&iowq))
3138 break;
3139 schedule();
3140 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003141 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003142 break;
3143 }
3144 } while (1);
3145 finish_wait(&ctx->wait, &iowq.wq);
3146
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003147 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003148
Hristo Venev75b28af2019-08-26 17:23:46 +00003149 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003150}
3151
Jens Axboe6b063142019-01-10 22:13:58 -07003152static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3153{
3154#if defined(CONFIG_UNIX)
3155 if (ctx->ring_sock) {
3156 struct sock *sock = ctx->ring_sock->sk;
3157 struct sk_buff *skb;
3158
3159 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3160 kfree_skb(skb);
3161 }
3162#else
3163 int i;
3164
Jens Axboe65e19f52019-10-26 07:20:21 -06003165 for (i = 0; i < ctx->nr_user_files; i++) {
3166 struct file *file;
3167
3168 file = io_file_from_index(ctx, i);
3169 if (file)
3170 fput(file);
3171 }
Jens Axboe6b063142019-01-10 22:13:58 -07003172#endif
3173}
3174
3175static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3176{
Jens Axboe65e19f52019-10-26 07:20:21 -06003177 unsigned nr_tables, i;
3178
3179 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003180 return -ENXIO;
3181
3182 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003183 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3184 for (i = 0; i < nr_tables; i++)
3185 kfree(ctx->file_table[i].files);
3186 kfree(ctx->file_table);
3187 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003188 ctx->nr_user_files = 0;
3189 return 0;
3190}
3191
Jens Axboe6c271ce2019-01-10 11:22:30 -07003192static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3193{
3194 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003195 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003196 /*
3197 * The park is a bit of a work-around, without it we get
3198 * warning spews on shutdown with SQPOLL set and affinity
3199 * set to a single CPU.
3200 */
Jens Axboe06058632019-04-13 09:26:03 -06003201 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003202 kthread_stop(ctx->sqo_thread);
3203 ctx->sqo_thread = NULL;
3204 }
3205}
3206
Jens Axboe6b063142019-01-10 22:13:58 -07003207static void io_finish_async(struct io_ring_ctx *ctx)
3208{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003209 io_sq_thread_stop(ctx);
3210
Jens Axboe561fb042019-10-24 07:25:42 -06003211 if (ctx->io_wq) {
3212 io_wq_destroy(ctx->io_wq);
3213 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003214 }
3215}
3216
3217#if defined(CONFIG_UNIX)
3218static void io_destruct_skb(struct sk_buff *skb)
3219{
3220 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3221
Jens Axboe561fb042019-10-24 07:25:42 -06003222 if (ctx->io_wq)
3223 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003224
Jens Axboe6b063142019-01-10 22:13:58 -07003225 unix_destruct_scm(skb);
3226}
3227
3228/*
3229 * Ensure the UNIX gc is aware of our file set, so we are certain that
3230 * the io_uring can be safely unregistered on process exit, even if we have
3231 * loops in the file referencing.
3232 */
3233static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3234{
3235 struct sock *sk = ctx->ring_sock->sk;
3236 struct scm_fp_list *fpl;
3237 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003238 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003239
3240 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3241 unsigned long inflight = ctx->user->unix_inflight + nr;
3242
3243 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3244 return -EMFILE;
3245 }
3246
3247 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3248 if (!fpl)
3249 return -ENOMEM;
3250
3251 skb = alloc_skb(0, GFP_KERNEL);
3252 if (!skb) {
3253 kfree(fpl);
3254 return -ENOMEM;
3255 }
3256
3257 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003258
Jens Axboe08a45172019-10-03 08:11:03 -06003259 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003260 fpl->user = get_uid(ctx->user);
3261 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003262 struct file *file = io_file_from_index(ctx, i + offset);
3263
3264 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003265 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003266 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003267 unix_inflight(fpl->user, fpl->fp[nr_files]);
3268 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003269 }
3270
Jens Axboe08a45172019-10-03 08:11:03 -06003271 if (nr_files) {
3272 fpl->max = SCM_MAX_FD;
3273 fpl->count = nr_files;
3274 UNIXCB(skb).fp = fpl;
3275 skb->destructor = io_destruct_skb;
3276 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3277 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003278
Jens Axboe08a45172019-10-03 08:11:03 -06003279 for (i = 0; i < nr_files; i++)
3280 fput(fpl->fp[i]);
3281 } else {
3282 kfree_skb(skb);
3283 kfree(fpl);
3284 }
Jens Axboe6b063142019-01-10 22:13:58 -07003285
3286 return 0;
3287}
3288
3289/*
3290 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3291 * causes regular reference counting to break down. We rely on the UNIX
3292 * garbage collection to take care of this problem for us.
3293 */
3294static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3295{
3296 unsigned left, total;
3297 int ret = 0;
3298
3299 total = 0;
3300 left = ctx->nr_user_files;
3301 while (left) {
3302 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003303
3304 ret = __io_sqe_files_scm(ctx, this_files, total);
3305 if (ret)
3306 break;
3307 left -= this_files;
3308 total += this_files;
3309 }
3310
3311 if (!ret)
3312 return 0;
3313
3314 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003315 struct file *file = io_file_from_index(ctx, total);
3316
3317 if (file)
3318 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003319 total++;
3320 }
3321
3322 return ret;
3323}
3324#else
3325static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3326{
3327 return 0;
3328}
3329#endif
3330
Jens Axboe65e19f52019-10-26 07:20:21 -06003331static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3332 unsigned nr_files)
3333{
3334 int i;
3335
3336 for (i = 0; i < nr_tables; i++) {
3337 struct fixed_file_table *table = &ctx->file_table[i];
3338 unsigned this_files;
3339
3340 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3341 table->files = kcalloc(this_files, sizeof(struct file *),
3342 GFP_KERNEL);
3343 if (!table->files)
3344 break;
3345 nr_files -= this_files;
3346 }
3347
3348 if (i == nr_tables)
3349 return 0;
3350
3351 for (i = 0; i < nr_tables; i++) {
3352 struct fixed_file_table *table = &ctx->file_table[i];
3353 kfree(table->files);
3354 }
3355 return 1;
3356}
3357
Jens Axboe6b063142019-01-10 22:13:58 -07003358static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3359 unsigned nr_args)
3360{
3361 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003362 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003363 int fd, ret = 0;
3364 unsigned i;
3365
Jens Axboe65e19f52019-10-26 07:20:21 -06003366 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003367 return -EBUSY;
3368 if (!nr_args)
3369 return -EINVAL;
3370 if (nr_args > IORING_MAX_FIXED_FILES)
3371 return -EMFILE;
3372
Jens Axboe65e19f52019-10-26 07:20:21 -06003373 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3374 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3375 GFP_KERNEL);
3376 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003377 return -ENOMEM;
3378
Jens Axboe65e19f52019-10-26 07:20:21 -06003379 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3380 kfree(ctx->file_table);
3381 return -ENOMEM;
3382 }
3383
Jens Axboe08a45172019-10-03 08:11:03 -06003384 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003385 struct fixed_file_table *table;
3386 unsigned index;
3387
Jens Axboe6b063142019-01-10 22:13:58 -07003388 ret = -EFAULT;
3389 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3390 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003391 /* allow sparse sets */
3392 if (fd == -1) {
3393 ret = 0;
3394 continue;
3395 }
Jens Axboe6b063142019-01-10 22:13:58 -07003396
Jens Axboe65e19f52019-10-26 07:20:21 -06003397 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3398 index = i & IORING_FILE_TABLE_MASK;
3399 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003400
3401 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003402 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003403 break;
3404 /*
3405 * Don't allow io_uring instances to be registered. If UNIX
3406 * isn't enabled, then this causes a reference cycle and this
3407 * instance can never get freed. If UNIX is enabled we'll
3408 * handle it just fine, but there's still no point in allowing
3409 * a ring fd as it doesn't support regular read/write anyway.
3410 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003411 if (table->files[index]->f_op == &io_uring_fops) {
3412 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003413 break;
3414 }
Jens Axboe6b063142019-01-10 22:13:58 -07003415 ret = 0;
3416 }
3417
3418 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003419 for (i = 0; i < ctx->nr_user_files; i++) {
3420 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003421
Jens Axboe65e19f52019-10-26 07:20:21 -06003422 file = io_file_from_index(ctx, i);
3423 if (file)
3424 fput(file);
3425 }
3426 for (i = 0; i < nr_tables; i++)
3427 kfree(ctx->file_table[i].files);
3428
3429 kfree(ctx->file_table);
3430 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003431 ctx->nr_user_files = 0;
3432 return ret;
3433 }
3434
3435 ret = io_sqe_files_scm(ctx);
3436 if (ret)
3437 io_sqe_files_unregister(ctx);
3438
3439 return ret;
3440}
3441
Jens Axboec3a31e62019-10-03 13:59:56 -06003442static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3443{
3444#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003445 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003446 struct sock *sock = ctx->ring_sock->sk;
3447 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3448 struct sk_buff *skb;
3449 int i;
3450
3451 __skb_queue_head_init(&list);
3452
3453 /*
3454 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3455 * remove this entry and rearrange the file array.
3456 */
3457 skb = skb_dequeue(head);
3458 while (skb) {
3459 struct scm_fp_list *fp;
3460
3461 fp = UNIXCB(skb).fp;
3462 for (i = 0; i < fp->count; i++) {
3463 int left;
3464
3465 if (fp->fp[i] != file)
3466 continue;
3467
3468 unix_notinflight(fp->user, fp->fp[i]);
3469 left = fp->count - 1 - i;
3470 if (left) {
3471 memmove(&fp->fp[i], &fp->fp[i + 1],
3472 left * sizeof(struct file *));
3473 }
3474 fp->count--;
3475 if (!fp->count) {
3476 kfree_skb(skb);
3477 skb = NULL;
3478 } else {
3479 __skb_queue_tail(&list, skb);
3480 }
3481 fput(file);
3482 file = NULL;
3483 break;
3484 }
3485
3486 if (!file)
3487 break;
3488
3489 __skb_queue_tail(&list, skb);
3490
3491 skb = skb_dequeue(head);
3492 }
3493
3494 if (skb_peek(&list)) {
3495 spin_lock_irq(&head->lock);
3496 while ((skb = __skb_dequeue(&list)) != NULL)
3497 __skb_queue_tail(head, skb);
3498 spin_unlock_irq(&head->lock);
3499 }
3500#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003501 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003502#endif
3503}
3504
3505static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3506 int index)
3507{
3508#if defined(CONFIG_UNIX)
3509 struct sock *sock = ctx->ring_sock->sk;
3510 struct sk_buff_head *head = &sock->sk_receive_queue;
3511 struct sk_buff *skb;
3512
3513 /*
3514 * See if we can merge this file into an existing skb SCM_RIGHTS
3515 * file set. If there's no room, fall back to allocating a new skb
3516 * and filling it in.
3517 */
3518 spin_lock_irq(&head->lock);
3519 skb = skb_peek(head);
3520 if (skb) {
3521 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3522
3523 if (fpl->count < SCM_MAX_FD) {
3524 __skb_unlink(skb, head);
3525 spin_unlock_irq(&head->lock);
3526 fpl->fp[fpl->count] = get_file(file);
3527 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3528 fpl->count++;
3529 spin_lock_irq(&head->lock);
3530 __skb_queue_head(head, skb);
3531 } else {
3532 skb = NULL;
3533 }
3534 }
3535 spin_unlock_irq(&head->lock);
3536
3537 if (skb) {
3538 fput(file);
3539 return 0;
3540 }
3541
3542 return __io_sqe_files_scm(ctx, 1, index);
3543#else
3544 return 0;
3545#endif
3546}
3547
3548static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3549 unsigned nr_args)
3550{
3551 struct io_uring_files_update up;
3552 __s32 __user *fds;
3553 int fd, i, err;
3554 __u32 done;
3555
Jens Axboe65e19f52019-10-26 07:20:21 -06003556 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003557 return -ENXIO;
3558 if (!nr_args)
3559 return -EINVAL;
3560 if (copy_from_user(&up, arg, sizeof(up)))
3561 return -EFAULT;
3562 if (check_add_overflow(up.offset, nr_args, &done))
3563 return -EOVERFLOW;
3564 if (done > ctx->nr_user_files)
3565 return -EINVAL;
3566
3567 done = 0;
3568 fds = (__s32 __user *) up.fds;
3569 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003570 struct fixed_file_table *table;
3571 unsigned index;
3572
Jens Axboec3a31e62019-10-03 13:59:56 -06003573 err = 0;
3574 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3575 err = -EFAULT;
3576 break;
3577 }
3578 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003579 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3580 index = i & IORING_FILE_TABLE_MASK;
3581 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003582 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003583 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003584 }
3585 if (fd != -1) {
3586 struct file *file;
3587
3588 file = fget(fd);
3589 if (!file) {
3590 err = -EBADF;
3591 break;
3592 }
3593 /*
3594 * Don't allow io_uring instances to be registered. If
3595 * UNIX isn't enabled, then this causes a reference
3596 * cycle and this instance can never get freed. If UNIX
3597 * is enabled we'll handle it just fine, but there's
3598 * still no point in allowing a ring fd as it doesn't
3599 * support regular read/write anyway.
3600 */
3601 if (file->f_op == &io_uring_fops) {
3602 fput(file);
3603 err = -EBADF;
3604 break;
3605 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003606 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003607 err = io_sqe_file_register(ctx, file, i);
3608 if (err)
3609 break;
3610 }
3611 nr_args--;
3612 done++;
3613 up.offset++;
3614 }
3615
3616 return done ? done : err;
3617}
3618
Jens Axboe6c271ce2019-01-10 11:22:30 -07003619static int io_sq_offload_start(struct io_ring_ctx *ctx,
3620 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003621{
Jens Axboe561fb042019-10-24 07:25:42 -06003622 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003623 int ret;
3624
Jens Axboe6c271ce2019-01-10 11:22:30 -07003625 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003626 mmgrab(current->mm);
3627 ctx->sqo_mm = current->mm;
3628
Jens Axboe6c271ce2019-01-10 11:22:30 -07003629 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003630 ret = -EPERM;
3631 if (!capable(CAP_SYS_ADMIN))
3632 goto err;
3633
Jens Axboe917257d2019-04-13 09:28:55 -06003634 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3635 if (!ctx->sq_thread_idle)
3636 ctx->sq_thread_idle = HZ;
3637
Jens Axboe6c271ce2019-01-10 11:22:30 -07003638 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003639 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003640
Jens Axboe917257d2019-04-13 09:28:55 -06003641 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003642 if (cpu >= nr_cpu_ids)
3643 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003644 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003645 goto err;
3646
Jens Axboe6c271ce2019-01-10 11:22:30 -07003647 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3648 ctx, cpu,
3649 "io_uring-sq");
3650 } else {
3651 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3652 "io_uring-sq");
3653 }
3654 if (IS_ERR(ctx->sqo_thread)) {
3655 ret = PTR_ERR(ctx->sqo_thread);
3656 ctx->sqo_thread = NULL;
3657 goto err;
3658 }
3659 wake_up_process(ctx->sqo_thread);
3660 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3661 /* Can't have SQ_AFF without SQPOLL */
3662 ret = -EINVAL;
3663 goto err;
3664 }
3665
Jens Axboe561fb042019-10-24 07:25:42 -06003666 /* Do QD, or 4 * CPUS, whatever is smallest */
3667 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
3668 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm);
Jens Axboe975c99a52019-10-30 08:42:56 -06003669 if (IS_ERR(ctx->io_wq)) {
3670 ret = PTR_ERR(ctx->io_wq);
3671 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003672 goto err;
3673 }
3674
3675 return 0;
3676err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003677 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003678 mmdrop(ctx->sqo_mm);
3679 ctx->sqo_mm = NULL;
3680 return ret;
3681}
3682
3683static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3684{
3685 atomic_long_sub(nr_pages, &user->locked_vm);
3686}
3687
3688static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3689{
3690 unsigned long page_limit, cur_pages, new_pages;
3691
3692 /* Don't allow more pages than we can safely lock */
3693 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3694
3695 do {
3696 cur_pages = atomic_long_read(&user->locked_vm);
3697 new_pages = cur_pages + nr_pages;
3698 if (new_pages > page_limit)
3699 return -ENOMEM;
3700 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3701 new_pages) != cur_pages);
3702
3703 return 0;
3704}
3705
3706static void io_mem_free(void *ptr)
3707{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003708 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003709
Mark Rutland52e04ef2019-04-30 17:30:21 +01003710 if (!ptr)
3711 return;
3712
3713 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003714 if (put_page_testzero(page))
3715 free_compound_page(page);
3716}
3717
3718static void *io_mem_alloc(size_t size)
3719{
3720 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3721 __GFP_NORETRY;
3722
3723 return (void *) __get_free_pages(gfp_flags, get_order(size));
3724}
3725
Hristo Venev75b28af2019-08-26 17:23:46 +00003726static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3727 size_t *sq_offset)
3728{
3729 struct io_rings *rings;
3730 size_t off, sq_array_size;
3731
3732 off = struct_size(rings, cqes, cq_entries);
3733 if (off == SIZE_MAX)
3734 return SIZE_MAX;
3735
3736#ifdef CONFIG_SMP
3737 off = ALIGN(off, SMP_CACHE_BYTES);
3738 if (off == 0)
3739 return SIZE_MAX;
3740#endif
3741
3742 sq_array_size = array_size(sizeof(u32), sq_entries);
3743 if (sq_array_size == SIZE_MAX)
3744 return SIZE_MAX;
3745
3746 if (check_add_overflow(off, sq_array_size, &off))
3747 return SIZE_MAX;
3748
3749 if (sq_offset)
3750 *sq_offset = off;
3751
3752 return off;
3753}
3754
Jens Axboe2b188cc2019-01-07 10:46:33 -07003755static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3756{
Hristo Venev75b28af2019-08-26 17:23:46 +00003757 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003758
Hristo Venev75b28af2019-08-26 17:23:46 +00003759 pages = (size_t)1 << get_order(
3760 rings_size(sq_entries, cq_entries, NULL));
3761 pages += (size_t)1 << get_order(
3762 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003763
Hristo Venev75b28af2019-08-26 17:23:46 +00003764 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003765}
3766
Jens Axboeedafcce2019-01-09 09:16:05 -07003767static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3768{
3769 int i, j;
3770
3771 if (!ctx->user_bufs)
3772 return -ENXIO;
3773
3774 for (i = 0; i < ctx->nr_user_bufs; i++) {
3775 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3776
3777 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003778 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003779
3780 if (ctx->account_mem)
3781 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003782 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003783 imu->nr_bvecs = 0;
3784 }
3785
3786 kfree(ctx->user_bufs);
3787 ctx->user_bufs = NULL;
3788 ctx->nr_user_bufs = 0;
3789 return 0;
3790}
3791
3792static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3793 void __user *arg, unsigned index)
3794{
3795 struct iovec __user *src;
3796
3797#ifdef CONFIG_COMPAT
3798 if (ctx->compat) {
3799 struct compat_iovec __user *ciovs;
3800 struct compat_iovec ciov;
3801
3802 ciovs = (struct compat_iovec __user *) arg;
3803 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3804 return -EFAULT;
3805
3806 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3807 dst->iov_len = ciov.iov_len;
3808 return 0;
3809 }
3810#endif
3811 src = (struct iovec __user *) arg;
3812 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3813 return -EFAULT;
3814 return 0;
3815}
3816
3817static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3818 unsigned nr_args)
3819{
3820 struct vm_area_struct **vmas = NULL;
3821 struct page **pages = NULL;
3822 int i, j, got_pages = 0;
3823 int ret = -EINVAL;
3824
3825 if (ctx->user_bufs)
3826 return -EBUSY;
3827 if (!nr_args || nr_args > UIO_MAXIOV)
3828 return -EINVAL;
3829
3830 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3831 GFP_KERNEL);
3832 if (!ctx->user_bufs)
3833 return -ENOMEM;
3834
3835 for (i = 0; i < nr_args; i++) {
3836 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3837 unsigned long off, start, end, ubuf;
3838 int pret, nr_pages;
3839 struct iovec iov;
3840 size_t size;
3841
3842 ret = io_copy_iov(ctx, &iov, arg, i);
3843 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003844 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003845
3846 /*
3847 * Don't impose further limits on the size and buffer
3848 * constraints here, we'll -EINVAL later when IO is
3849 * submitted if they are wrong.
3850 */
3851 ret = -EFAULT;
3852 if (!iov.iov_base || !iov.iov_len)
3853 goto err;
3854
3855 /* arbitrary limit, but we need something */
3856 if (iov.iov_len > SZ_1G)
3857 goto err;
3858
3859 ubuf = (unsigned long) iov.iov_base;
3860 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3861 start = ubuf >> PAGE_SHIFT;
3862 nr_pages = end - start;
3863
3864 if (ctx->account_mem) {
3865 ret = io_account_mem(ctx->user, nr_pages);
3866 if (ret)
3867 goto err;
3868 }
3869
3870 ret = 0;
3871 if (!pages || nr_pages > got_pages) {
3872 kfree(vmas);
3873 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003874 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003875 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003876 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003877 sizeof(struct vm_area_struct *),
3878 GFP_KERNEL);
3879 if (!pages || !vmas) {
3880 ret = -ENOMEM;
3881 if (ctx->account_mem)
3882 io_unaccount_mem(ctx->user, nr_pages);
3883 goto err;
3884 }
3885 got_pages = nr_pages;
3886 }
3887
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003888 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003889 GFP_KERNEL);
3890 ret = -ENOMEM;
3891 if (!imu->bvec) {
3892 if (ctx->account_mem)
3893 io_unaccount_mem(ctx->user, nr_pages);
3894 goto err;
3895 }
3896
3897 ret = 0;
3898 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003899 pret = get_user_pages(ubuf, nr_pages,
3900 FOLL_WRITE | FOLL_LONGTERM,
3901 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003902 if (pret == nr_pages) {
3903 /* don't support file backed memory */
3904 for (j = 0; j < nr_pages; j++) {
3905 struct vm_area_struct *vma = vmas[j];
3906
3907 if (vma->vm_file &&
3908 !is_file_hugepages(vma->vm_file)) {
3909 ret = -EOPNOTSUPP;
3910 break;
3911 }
3912 }
3913 } else {
3914 ret = pret < 0 ? pret : -EFAULT;
3915 }
3916 up_read(&current->mm->mmap_sem);
3917 if (ret) {
3918 /*
3919 * if we did partial map, or found file backed vmas,
3920 * release any pages we did get
3921 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003922 if (pret > 0)
3923 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003924 if (ctx->account_mem)
3925 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003926 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003927 goto err;
3928 }
3929
3930 off = ubuf & ~PAGE_MASK;
3931 size = iov.iov_len;
3932 for (j = 0; j < nr_pages; j++) {
3933 size_t vec_len;
3934
3935 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3936 imu->bvec[j].bv_page = pages[j];
3937 imu->bvec[j].bv_len = vec_len;
3938 imu->bvec[j].bv_offset = off;
3939 off = 0;
3940 size -= vec_len;
3941 }
3942 /* store original address for later verification */
3943 imu->ubuf = ubuf;
3944 imu->len = iov.iov_len;
3945 imu->nr_bvecs = nr_pages;
3946
3947 ctx->nr_user_bufs++;
3948 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003949 kvfree(pages);
3950 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003951 return 0;
3952err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003953 kvfree(pages);
3954 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003955 io_sqe_buffer_unregister(ctx);
3956 return ret;
3957}
3958
Jens Axboe9b402842019-04-11 11:45:41 -06003959static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3960{
3961 __s32 __user *fds = arg;
3962 int fd;
3963
3964 if (ctx->cq_ev_fd)
3965 return -EBUSY;
3966
3967 if (copy_from_user(&fd, fds, sizeof(*fds)))
3968 return -EFAULT;
3969
3970 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3971 if (IS_ERR(ctx->cq_ev_fd)) {
3972 int ret = PTR_ERR(ctx->cq_ev_fd);
3973 ctx->cq_ev_fd = NULL;
3974 return ret;
3975 }
3976
3977 return 0;
3978}
3979
3980static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3981{
3982 if (ctx->cq_ev_fd) {
3983 eventfd_ctx_put(ctx->cq_ev_fd);
3984 ctx->cq_ev_fd = NULL;
3985 return 0;
3986 }
3987
3988 return -ENXIO;
3989}
3990
Jens Axboe2b188cc2019-01-07 10:46:33 -07003991static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3992{
Jens Axboe6b063142019-01-10 22:13:58 -07003993 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003994 if (ctx->sqo_mm)
3995 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003996
3997 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003998 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003999 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004000 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004001
Jens Axboe2b188cc2019-01-07 10:46:33 -07004002#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004003 if (ctx->ring_sock) {
4004 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004005 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004006 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004007#endif
4008
Hristo Venev75b28af2019-08-26 17:23:46 +00004009 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004010 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004011
4012 percpu_ref_exit(&ctx->refs);
4013 if (ctx->account_mem)
4014 io_unaccount_mem(ctx->user,
4015 ring_pages(ctx->sq_entries, ctx->cq_entries));
4016 free_uid(ctx->user);
4017 kfree(ctx);
4018}
4019
4020static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4021{
4022 struct io_ring_ctx *ctx = file->private_data;
4023 __poll_t mask = 0;
4024
4025 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004026 /*
4027 * synchronizes with barrier from wq_has_sleeper call in
4028 * io_commit_cqring
4029 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004030 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004031 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4032 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004033 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004034 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004035 mask |= EPOLLIN | EPOLLRDNORM;
4036
4037 return mask;
4038}
4039
4040static int io_uring_fasync(int fd, struct file *file, int on)
4041{
4042 struct io_ring_ctx *ctx = file->private_data;
4043
4044 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4045}
4046
4047static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4048{
4049 mutex_lock(&ctx->uring_lock);
4050 percpu_ref_kill(&ctx->refs);
4051 mutex_unlock(&ctx->uring_lock);
4052
Jens Axboe5262f562019-09-17 12:26:57 -06004053 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004054 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004055
4056 if (ctx->io_wq)
4057 io_wq_cancel_all(ctx->io_wq);
4058
Jens Axboedef596e2019-01-09 08:59:42 -07004059 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004060 wait_for_completion(&ctx->ctx_done);
4061 io_ring_ctx_free(ctx);
4062}
4063
4064static int io_uring_release(struct inode *inode, struct file *file)
4065{
4066 struct io_ring_ctx *ctx = file->private_data;
4067
4068 file->private_data = NULL;
4069 io_ring_ctx_wait_and_kill(ctx);
4070 return 0;
4071}
4072
Jens Axboefcb323c2019-10-24 12:39:47 -06004073static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4074 struct files_struct *files)
4075{
4076 struct io_kiocb *req;
4077 DEFINE_WAIT(wait);
4078
4079 while (!list_empty_careful(&ctx->inflight_list)) {
4080 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
4081
4082 spin_lock_irq(&ctx->inflight_lock);
4083 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
4084 if (req->work.files == files) {
4085 ret = io_wq_cancel_work(ctx->io_wq, &req->work);
4086 break;
4087 }
4088 }
4089 if (ret == IO_WQ_CANCEL_RUNNING)
4090 prepare_to_wait(&ctx->inflight_wait, &wait,
4091 TASK_UNINTERRUPTIBLE);
4092
4093 spin_unlock_irq(&ctx->inflight_lock);
4094
4095 /*
4096 * We need to keep going until we get NOTFOUND. We only cancel
4097 * one work at the time.
4098 *
4099 * If we get CANCEL_RUNNING, then wait for a work to complete
4100 * before continuing.
4101 */
4102 if (ret == IO_WQ_CANCEL_OK)
4103 continue;
4104 else if (ret != IO_WQ_CANCEL_RUNNING)
4105 break;
4106 schedule();
4107 }
4108}
4109
4110static int io_uring_flush(struct file *file, void *data)
4111{
4112 struct io_ring_ctx *ctx = file->private_data;
4113
4114 io_uring_cancel_files(ctx, data);
4115 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
4116 io_wq_cancel_all(ctx->io_wq);
4117 return 0;
4118}
4119
Jens Axboe2b188cc2019-01-07 10:46:33 -07004120static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4121{
4122 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4123 unsigned long sz = vma->vm_end - vma->vm_start;
4124 struct io_ring_ctx *ctx = file->private_data;
4125 unsigned long pfn;
4126 struct page *page;
4127 void *ptr;
4128
4129 switch (offset) {
4130 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004131 case IORING_OFF_CQ_RING:
4132 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004133 break;
4134 case IORING_OFF_SQES:
4135 ptr = ctx->sq_sqes;
4136 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004137 default:
4138 return -EINVAL;
4139 }
4140
4141 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004142 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004143 return -EINVAL;
4144
4145 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4146 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4147}
4148
4149SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4150 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4151 size_t, sigsz)
4152{
4153 struct io_ring_ctx *ctx;
4154 long ret = -EBADF;
4155 int submitted = 0;
4156 struct fd f;
4157
Jens Axboe6c271ce2019-01-10 11:22:30 -07004158 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004159 return -EINVAL;
4160
4161 f = fdget(fd);
4162 if (!f.file)
4163 return -EBADF;
4164
4165 ret = -EOPNOTSUPP;
4166 if (f.file->f_op != &io_uring_fops)
4167 goto out_fput;
4168
4169 ret = -ENXIO;
4170 ctx = f.file->private_data;
4171 if (!percpu_ref_tryget(&ctx->refs))
4172 goto out_fput;
4173
Jens Axboe6c271ce2019-01-10 11:22:30 -07004174 /*
4175 * For SQ polling, the thread will do all submissions and completions.
4176 * Just return the requested submit count, and wake the thread if
4177 * we were asked to.
4178 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004179 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004180 if (ctx->flags & IORING_SETUP_SQPOLL) {
4181 if (flags & IORING_ENTER_SQ_WAKEUP)
4182 wake_up(&ctx->sqo_wait);
4183 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004184 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004185 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004186
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004187 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004188 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004189 /* already have mm, so io_submit_sqes() won't try to grab it */
4190 cur_mm = ctx->sqo_mm;
4191 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4192 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004193 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004194 }
4195 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004196 unsigned nr_events = 0;
4197
Jens Axboe2b188cc2019-01-07 10:46:33 -07004198 min_complete = min(min_complete, ctx->cq_entries);
4199
Jens Axboedef596e2019-01-09 08:59:42 -07004200 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004201 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004202 } else {
4203 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4204 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004205 }
4206
Pavel Begunkov6805b322019-10-08 02:18:42 +03004207 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004208out_fput:
4209 fdput(f);
4210 return submitted ? submitted : ret;
4211}
4212
4213static const struct file_operations io_uring_fops = {
4214 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004215 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004216 .mmap = io_uring_mmap,
4217 .poll = io_uring_poll,
4218 .fasync = io_uring_fasync,
4219};
4220
4221static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4222 struct io_uring_params *p)
4223{
Hristo Venev75b28af2019-08-26 17:23:46 +00004224 struct io_rings *rings;
4225 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004226
Hristo Venev75b28af2019-08-26 17:23:46 +00004227 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4228 if (size == SIZE_MAX)
4229 return -EOVERFLOW;
4230
4231 rings = io_mem_alloc(size);
4232 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004233 return -ENOMEM;
4234
Hristo Venev75b28af2019-08-26 17:23:46 +00004235 ctx->rings = rings;
4236 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4237 rings->sq_ring_mask = p->sq_entries - 1;
4238 rings->cq_ring_mask = p->cq_entries - 1;
4239 rings->sq_ring_entries = p->sq_entries;
4240 rings->cq_ring_entries = p->cq_entries;
4241 ctx->sq_mask = rings->sq_ring_mask;
4242 ctx->cq_mask = rings->cq_ring_mask;
4243 ctx->sq_entries = rings->sq_ring_entries;
4244 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004245
4246 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4247 if (size == SIZE_MAX)
4248 return -EOVERFLOW;
4249
4250 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004251 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004252 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004253
Jens Axboe2b188cc2019-01-07 10:46:33 -07004254 return 0;
4255}
4256
4257/*
4258 * Allocate an anonymous fd, this is what constitutes the application
4259 * visible backing of an io_uring instance. The application mmaps this
4260 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4261 * we have to tie this fd to a socket for file garbage collection purposes.
4262 */
4263static int io_uring_get_fd(struct io_ring_ctx *ctx)
4264{
4265 struct file *file;
4266 int ret;
4267
4268#if defined(CONFIG_UNIX)
4269 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4270 &ctx->ring_sock);
4271 if (ret)
4272 return ret;
4273#endif
4274
4275 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4276 if (ret < 0)
4277 goto err;
4278
4279 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4280 O_RDWR | O_CLOEXEC);
4281 if (IS_ERR(file)) {
4282 put_unused_fd(ret);
4283 ret = PTR_ERR(file);
4284 goto err;
4285 }
4286
4287#if defined(CONFIG_UNIX)
4288 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004289 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004290#endif
4291 fd_install(ret, file);
4292 return ret;
4293err:
4294#if defined(CONFIG_UNIX)
4295 sock_release(ctx->ring_sock);
4296 ctx->ring_sock = NULL;
4297#endif
4298 return ret;
4299}
4300
4301static int io_uring_create(unsigned entries, struct io_uring_params *p)
4302{
4303 struct user_struct *user = NULL;
4304 struct io_ring_ctx *ctx;
4305 bool account_mem;
4306 int ret;
4307
4308 if (!entries || entries > IORING_MAX_ENTRIES)
4309 return -EINVAL;
4310
4311 /*
4312 * Use twice as many entries for the CQ ring. It's possible for the
4313 * application to drive a higher depth than the size of the SQ ring,
4314 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004315 * some flexibility in overcommitting a bit. If the application has
4316 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4317 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004318 */
4319 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004320 if (p->flags & IORING_SETUP_CQSIZE) {
4321 /*
4322 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4323 * to a power-of-two, if it isn't already. We do NOT impose
4324 * any cq vs sq ring sizing.
4325 */
4326 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4327 return -EINVAL;
4328 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4329 } else {
4330 p->cq_entries = 2 * p->sq_entries;
4331 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004332
4333 user = get_uid(current_user());
4334 account_mem = !capable(CAP_IPC_LOCK);
4335
4336 if (account_mem) {
4337 ret = io_account_mem(user,
4338 ring_pages(p->sq_entries, p->cq_entries));
4339 if (ret) {
4340 free_uid(user);
4341 return ret;
4342 }
4343 }
4344
4345 ctx = io_ring_ctx_alloc(p);
4346 if (!ctx) {
4347 if (account_mem)
4348 io_unaccount_mem(user, ring_pages(p->sq_entries,
4349 p->cq_entries));
4350 free_uid(user);
4351 return -ENOMEM;
4352 }
4353 ctx->compat = in_compat_syscall();
4354 ctx->account_mem = account_mem;
4355 ctx->user = user;
4356
4357 ret = io_allocate_scq_urings(ctx, p);
4358 if (ret)
4359 goto err;
4360
Jens Axboe6c271ce2019-01-10 11:22:30 -07004361 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004362 if (ret)
4363 goto err;
4364
Jens Axboe2b188cc2019-01-07 10:46:33 -07004365 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004366 p->sq_off.head = offsetof(struct io_rings, sq.head);
4367 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4368 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4369 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4370 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4371 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4372 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004373
4374 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004375 p->cq_off.head = offsetof(struct io_rings, cq.head);
4376 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4377 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4378 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4379 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4380 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004381
Jens Axboe044c1ab2019-10-28 09:15:33 -06004382 /*
4383 * Install ring fd as the very last thing, so we don't risk someone
4384 * having closed it before we finish setup
4385 */
4386 ret = io_uring_get_fd(ctx);
4387 if (ret < 0)
4388 goto err;
4389
Jens Axboeac90f242019-09-06 10:26:21 -06004390 p->features = IORING_FEAT_SINGLE_MMAP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004391 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004392 return ret;
4393err:
4394 io_ring_ctx_wait_and_kill(ctx);
4395 return ret;
4396}
4397
4398/*
4399 * Sets up an aio uring context, and returns the fd. Applications asks for a
4400 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4401 * params structure passed in.
4402 */
4403static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4404{
4405 struct io_uring_params p;
4406 long ret;
4407 int i;
4408
4409 if (copy_from_user(&p, params, sizeof(p)))
4410 return -EFAULT;
4411 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4412 if (p.resv[i])
4413 return -EINVAL;
4414 }
4415
Jens Axboe6c271ce2019-01-10 11:22:30 -07004416 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004417 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004418 return -EINVAL;
4419
4420 ret = io_uring_create(entries, &p);
4421 if (ret < 0)
4422 return ret;
4423
4424 if (copy_to_user(params, &p, sizeof(p)))
4425 return -EFAULT;
4426
4427 return ret;
4428}
4429
4430SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4431 struct io_uring_params __user *, params)
4432{
4433 return io_uring_setup(entries, params);
4434}
4435
Jens Axboeedafcce2019-01-09 09:16:05 -07004436static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4437 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004438 __releases(ctx->uring_lock)
4439 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004440{
4441 int ret;
4442
Jens Axboe35fa71a2019-04-22 10:23:23 -06004443 /*
4444 * We're inside the ring mutex, if the ref is already dying, then
4445 * someone else killed the ctx or is already going through
4446 * io_uring_register().
4447 */
4448 if (percpu_ref_is_dying(&ctx->refs))
4449 return -ENXIO;
4450
Jens Axboeedafcce2019-01-09 09:16:05 -07004451 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004452
4453 /*
4454 * Drop uring mutex before waiting for references to exit. If another
4455 * thread is currently inside io_uring_enter() it might need to grab
4456 * the uring_lock to make progress. If we hold it here across the drain
4457 * wait, then we can deadlock. It's safe to drop the mutex here, since
4458 * no new references will come in after we've killed the percpu ref.
4459 */
4460 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004461 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004462 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004463
4464 switch (opcode) {
4465 case IORING_REGISTER_BUFFERS:
4466 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4467 break;
4468 case IORING_UNREGISTER_BUFFERS:
4469 ret = -EINVAL;
4470 if (arg || nr_args)
4471 break;
4472 ret = io_sqe_buffer_unregister(ctx);
4473 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004474 case IORING_REGISTER_FILES:
4475 ret = io_sqe_files_register(ctx, arg, nr_args);
4476 break;
4477 case IORING_UNREGISTER_FILES:
4478 ret = -EINVAL;
4479 if (arg || nr_args)
4480 break;
4481 ret = io_sqe_files_unregister(ctx);
4482 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004483 case IORING_REGISTER_FILES_UPDATE:
4484 ret = io_sqe_files_update(ctx, arg, nr_args);
4485 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004486 case IORING_REGISTER_EVENTFD:
4487 ret = -EINVAL;
4488 if (nr_args != 1)
4489 break;
4490 ret = io_eventfd_register(ctx, arg);
4491 break;
4492 case IORING_UNREGISTER_EVENTFD:
4493 ret = -EINVAL;
4494 if (arg || nr_args)
4495 break;
4496 ret = io_eventfd_unregister(ctx);
4497 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004498 default:
4499 ret = -EINVAL;
4500 break;
4501 }
4502
4503 /* bring the ctx back to life */
4504 reinit_completion(&ctx->ctx_done);
4505 percpu_ref_reinit(&ctx->refs);
4506 return ret;
4507}
4508
4509SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4510 void __user *, arg, unsigned int, nr_args)
4511{
4512 struct io_ring_ctx *ctx;
4513 long ret = -EBADF;
4514 struct fd f;
4515
4516 f = fdget(fd);
4517 if (!f.file)
4518 return -EBADF;
4519
4520 ret = -EOPNOTSUPP;
4521 if (f.file->f_op != &io_uring_fops)
4522 goto out_fput;
4523
4524 ctx = f.file->private_data;
4525
4526 mutex_lock(&ctx->uring_lock);
4527 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4528 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004529 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4530 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004531out_fput:
4532 fdput(f);
4533 return ret;
4534}
4535
Jens Axboe2b188cc2019-01-07 10:46:33 -07004536static int __init io_uring_init(void)
4537{
4538 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4539 return 0;
4540};
4541__initcall(io_uring_init);